Skip to content

Fix the behavior of HAVING clause #154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions tests/WP_SQLite_Translator_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -3048,6 +3048,85 @@ public function testCurrentTimestamp() {
$this->assertQuery( 'DELETE FROM _dates WHERE option_value = CURRENT_TIMESTAMP()' );
}

public function testGroupByHaving() {
$this->assertQuery(
'CREATE TABLE _tmp_table (
name varchar(20)
);'
);

$this->assertQuery(
"INSERT INTO _tmp_table VALUES ('a'), ('b'), ('b'), ('c'), ('c'), ('c')"
);

$result = $this->assertQuery(
'SELECT name, COUNT(*) as count FROM _tmp_table GROUP BY name HAVING COUNT(*) > 1'
);
$this->assertEquals(
array(
(object) array(
'name' => 'b',
'count' => '2',
),
(object) array(
'name' => 'c',
'count' => '3',
),
),
$result
);
}

public function testHavingWithoutGroupBy() {
$this->assertQuery(
'CREATE TABLE _tmp_table (
name varchar(20)
);'
);

$this->assertQuery(
"INSERT INTO _tmp_table VALUES ('a'), ('b'), ('b'), ('c'), ('c'), ('c')"
);

// HAVING condition satisfied
$result = $this->assertQuery(
"SELECT 'T' FROM _tmp_table HAVING COUNT(*) > 1"
);
$this->assertEquals(
array(
(object) array(
':param0' => 'T',
),
),
$result
);

// HAVING condition not satisfied
$result = $this->assertQuery(
"SELECT 'T' FROM _tmp_table HAVING COUNT(*) > 100"
);
$this->assertEquals(
array(),
$result
);

// DISTINCT ... HAVING, where only some results meet the HAVING condition
$result = $this->assertQuery(
'SELECT DISTINCT name FROM _tmp_table HAVING COUNT(*) > 1'
);
$this->assertEquals(
array(
(object) array(
'name' => 'b',
),
(object) array(
'name' => 'c',
),
),
$result
);
}

/**
* @dataProvider mysqlVariablesToTest
*/
Expand Down
19 changes: 11 additions & 8 deletions wp-includes/sqlite/class-wp-sqlite-translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2607,23 +2607,19 @@ private function capture_group_by( $token ) {
! $token->matches(
WP_SQLite_Token::TYPE_KEYWORD,
WP_SQLite_Token::FLAG_KEYWORD_RESERVED,
array( 'GROUP' )
array( 'GROUP BY' )
)
) {
return false;
}
$next = $this->rewriter->peek_nth( 2 )->value;
if ( 'BY' !== strtoupper( $next ?? '' ) ) {
return false;
}

$this->has_group_by = true;

return false;
}

/**
* Translate WHERE something HAVING something to WHERE something AND something.
* Translate HAVING without GROUP BY to GROUP BY 1 HAVING.
*
* @param WP_SQLite_Token $token The token to translate.
*
Expand All @@ -2642,8 +2638,15 @@ private function translate_ungrouped_having( $token ) {
if ( $this->has_group_by ) {
return false;
}
$this->rewriter->skip();
$this->rewriter->add( new WP_SQLite_Token( 'AND', WP_SQLite_Token::TYPE_KEYWORD ) );

// GROUP BY is missing, add "GROUP BY 1" before the HAVING clause.
$having = $this->rewriter->skip();
$this->rewriter->add( new WP_SQLite_Token( ' ', WP_SQLite_Token::TYPE_DELIMITER ) );
$this->rewriter->add( new WP_SQLite_Token( 'GROUP BY', WP_SQLite_Token::TYPE_KEYWORD ) );
$this->rewriter->add( new WP_SQLite_Token( ' ', WP_SQLite_Token::TYPE_DELIMITER ) );
$this->rewriter->add( new WP_SQLite_Token( '1', WP_SQLite_Token::TYPE_NUMBER ) );
$this->rewriter->add( new WP_SQLite_Token( ' ', WP_SQLite_Token::TYPE_DELIMITER ) );
$this->rewriter->add( $having );

return true;
}
Expand Down
Loading