Skip to content
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

Add handling of index hints on join clauses too. Fixes #593 and #497. #613

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
20 changes: 16 additions & 4 deletions src/Components/JoinKeyword.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpMyAdmin\SqlParser\Component;
use PhpMyAdmin\SqlParser\Parsers\Conditions;
use PhpMyAdmin\SqlParser\Parsers\IndexHints;

use function array_search;

Expand Down Expand Up @@ -59,31 +60,42 @@
*/
public ArrayObj|null $using = null;

/**
* Index hints
*
* @var IndexHint[]
*/
public array $indexHints = [];

/**
* @see JoinKeyword::JOINS
*
* @param string|null $type Join type
* @param Expression|null $expr join expression
* @param Condition[]|null $on join conditions
* @param ArrayObj|null $using columns joined
* @param string|null $type Join type
* @param Expression|null $expr join expression
* @param Condition[]|null $on join conditions
* @param ArrayObj|null $using columns joined
* @param IndexHint[] $indexHints index hints
*/
public function __construct(
string|null $type = null,
Expression|null $expr = null,
array|null $on = null,
ArrayObj|null $using = null,
array $indexHints = [],
) {
$this->type = $type;
$this->expr = $expr;
$this->on = $on;
$this->using = $using;
$this->indexHints = $indexHints;
}

public function build(): string
{
return array_search($this->type, self::JOINS) . ' ' . $this->expr

Check warning on line 95 in src/Components/JoinKeyword.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "Concat": --- Original +++ New @@ @@ } public function build() : string { - return array_search($this->type, self::JOINS) . ' ' . $this->expr . ($this->indexHints !== [] ? ' ' . IndexHints::buildAll($this->indexHints) : '') . (!empty($this->on) ? ' ON ' . Conditions::buildAll($this->on) : '') . (!empty($this->using) ? ' USING ' . $this->using->build() : ''); + return array_search($this->type, self::JOINS) . ' ' . $this->expr . ($this->indexHints !== [] ? ' ' . IndexHints::buildAll($this->indexHints) : '') . (!empty($this->using) ? ' USING ' . $this->using->build() : '') . (!empty($this->on) ? ' ON ' . Conditions::buildAll($this->on) : ''); } public function __toString() : string {

Check warning on line 95 in src/Components/JoinKeyword.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ } public function build() : string { - return array_search($this->type, self::JOINS) . ' ' . $this->expr . ($this->indexHints !== [] ? ' ' . IndexHints::buildAll($this->indexHints) : '') . (!empty($this->on) ? ' ON ' . Conditions::buildAll($this->on) : '') . (!empty($this->using) ? ' USING ' . $this->using->build() : ''); + return array_search($this->type, self::JOINS) . ' ' . $this->expr . ($this->indexHints !== [] ? ' ' . IndexHints::buildAll($this->indexHints) : '') . (!empty($this->on) ? ' ON ' . Conditions::buildAll($this->on) : ''); } public function __toString() : string {
. ($this->indexHints !== [] ? ' ' . IndexHints::buildAll($this->indexHints) : '')
. (! empty($this->on) ? ' ON ' . Conditions::buildAll($this->on) : '')
. (! empty($this->using) ? ' USING ' . $this->using->build() : '');

Check warning on line 98 in src/Components/JoinKeyword.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "Concat": --- Original +++ New @@ @@ } public function build() : string { - return array_search($this->type, self::JOINS) . ' ' . $this->expr . ($this->indexHints !== [] ? ' ' . IndexHints::buildAll($this->indexHints) : '') . (!empty($this->on) ? ' ON ' . Conditions::buildAll($this->on) : '') . (!empty($this->using) ? ' USING ' . $this->using->build() : ''); + return array_search($this->type, self::JOINS) . ' ' . $this->expr . ($this->indexHints !== [] ? ' ' . IndexHints::buildAll($this->indexHints) : '') . (!empty($this->on) ? ' ON ' . Conditions::buildAll($this->on) : '') . (!empty($this->using) ? $this->using->build() . ' USING ' : ''); } public function __toString() : string {

Check warning on line 98 in src/Components/JoinKeyword.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ } public function build() : string { - return array_search($this->type, self::JOINS) . ' ' . $this->expr . ($this->indexHints !== [] ? ' ' . IndexHints::buildAll($this->indexHints) : '') . (!empty($this->on) ? ' ON ' . Conditions::buildAll($this->on) : '') . (!empty($this->using) ? ' USING ' . $this->using->build() : ''); + return array_search($this->type, self::JOINS) . ' ' . $this->expr . ($this->indexHints !== [] ? ' ' . IndexHints::buildAll($this->indexHints) : '') . (!empty($this->on) ? ' ON ' . Conditions::buildAll($this->on) : '') . (!empty($this->using) ? $this->using->build() : ''); } public function __toString() : string {

Check warning on line 98 in src/Components/JoinKeyword.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ } public function build() : string { - return array_search($this->type, self::JOINS) . ' ' . $this->expr . ($this->indexHints !== [] ? ' ' . IndexHints::buildAll($this->indexHints) : '') . (!empty($this->on) ? ' ON ' . Conditions::buildAll($this->on) : '') . (!empty($this->using) ? ' USING ' . $this->using->build() : ''); + return array_search($this->type, self::JOINS) . ' ' . $this->expr . ($this->indexHints !== [] ? ' ' . IndexHints::buildAll($this->indexHints) : '') . (!empty($this->on) ? ' ON ' . Conditions::buildAll($this->on) : '') . (!empty($this->using) ? ' USING ' : ''); } public function __toString() : string {
}

public function __toString(): string
Expand Down
7 changes: 7 additions & 0 deletions src/Parsers/JoinKeywords.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
*
* 1 -----------------------[ expr ]----------------------> 2
*
* 2 -------------------[ index_hints ]-------------------> 2
* 2 ------------------------[ ON ]-----------------------> 3
* 2 -----------------------[ USING ]---------------------> 4
*
Expand All @@ -55,7 +56,7 @@
--$list->idx;
}

for (; $list->idx < $list->count; ++$list->idx) {

Check warning on line 59 in src/Parsers/JoinKeywords.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "LessThan": --- Original +++ New @@ @@ if ($list->idx > 0) { --$list->idx; } - for (; $list->idx < $list->count; ++$list->idx) { + for (; $list->idx <= $list->count; ++$list->idx) { /** * Token parsed at this moment. */
/**
* Token parsed at this moment.
*/
Expand All @@ -67,7 +68,7 @@
}

// Skipping whitespaces and comments.
if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {

Check warning on line 71 in src/Parsers/JoinKeywords.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "LogicalOr": --- Original +++ New @@ @@ break; } // Skipping whitespaces and comments. - if ($token->type === TokenType::Whitespace || $token->type === TokenType::Comment) { + if ($token->type === TokenType::Whitespace && $token->type === TokenType::Comment) { continue; } if ($state === 0) {
continue;
}

Expand All @@ -83,13 +84,19 @@
$state = 2;
} elseif ($state === 2) {
if ($token->type === TokenType::Keyword) {
switch ($token->keyword) {

Check warning on line 87 in src/Parsers/JoinKeywords.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "SharedCaseRemoval": --- Original +++ New @@ @@ $state = 4; break; case 'USE': - case 'IGNORE': case 'FORCE': // Adding index hint on the JOIN clause. $expr->indexHints = IndexHints::parse($parser, $list);
case 'ON':
$state = 3;
break;
case 'USING':
$state = 4;

Check warning on line 92 in src/Parsers/JoinKeywords.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "IncrementInteger": --- Original +++ New @@ @@ $state = 3; break; case 'USING': - $state = 4; + $state = 5; break; case 'USE': case 'IGNORE':
break;
case 'USE':
case 'IGNORE':
case 'FORCE':
// Adding index hint on the JOIN clause.
$expr->indexHints = IndexHints::parse($parser, $list);
break;
default:
if (empty(JoinKeyword::JOINS[$token->keyword])) {
/* Next clause is starting */
Expand All @@ -113,7 +120,7 @@
$expr->using = ArrayObjs::parse($parser, $list);
$ret[] = $expr;
$expr = new JoinKeyword();
$state = 0;

Check warning on line 123 in src/Parsers/JoinKeywords.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "DecrementInteger": --- Original +++ New @@ @@ $expr->using = ArrayObjs::parse($parser, $list); $ret[] = $expr; $expr = new JoinKeyword(); - $state = 0; + $state = -1; } } if (!empty($expr->type)) {
}
}

Expand Down
61 changes: 61 additions & 0 deletions tests/Builder/SelectStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,4 +353,65 @@ public function testBuilderSurroundedByParanthesisWithLimit(): void
$stmt->build(),
);
}

public function testBuilderSelectFromWithForceIndex(): void
{
$query = 'SELECT *'
. ' FROM uno FORCE INDEX (id)';
$parser = new Parser($query);
$stmt = $parser->statements[0];

self::assertSame($query, $stmt->build());
}

/**
* Ensures issue #497 is fixed.
*/
public function testBuilderSelectFromJoinWithForceIndex(): void
{
$query = 'SELECT *'
. ' FROM uno'
. ' JOIN dos FORCE INDEX (two_id) ON dos.id = uno.id';
$parser = new Parser($query);
$stmt = $parser->statements[0];

self::assertSame($query, $stmt->build());
}

/**
* Ensures issue #593 is fixed.
*/
public function testBuilderSelectFromInnerJoinWithForceIndex(): void
{
$query = 'SELECT a.id, a.name, b.order_id, b.total'
. ' FROM customers a'
. ' INNER JOIN orders b FORCE INDEX (idx_customer_id)'
. ' ON a.id = b.customer_id'
. " WHERE a.status = 'active'";

$parser = new Parser($query);
$stmt = $parser->statements[0];

$expectedQuery = 'SELECT a.id, a.name, b.order_id, b.total'
. ' FROM customers AS `a`'
. ' INNER JOIN orders AS `b` FORCE INDEX (idx_customer_id)'
. ' ON a.id = b.customer_id'
. " WHERE a.status = 'active'";

self::assertSame($expectedQuery, $stmt->build());
}

public function testBuilderSelectAllFormsOfIndexHints(): void
{
$query = 'SELECT *'
. ' FROM one USE INDEX (col1) IGNORE INDEX (col1, col2) FORCE INDEX (col1, col2, col3)'
. ' INNER JOIN two USE INDEX (col3) IGNORE INDEX (col2, col3) FORCE INDEX (col1, col2, col3)'
. ' ON one.col1 = two.col2'
. ' WHERE 1 = 1';

$parser = new Parser($query);
$stmt = $parser->statements[0];

self::assertSame($query, $stmt->build());
}
}
Loading