Skip to content
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
193 changes: 193 additions & 0 deletions src/Components/IndexHint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
<?php

/**
* Parses an Index hint.
*/

namespace PhpMyAdmin\SqlParser\Components;

use PhpMyAdmin\SqlParser\Component;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;

/**
* Parses an Index hint.
*
* @category Components
*
* @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+
*/
class IndexHint extends Component
{
/**
* The type of hint (USE/FORCE/IGNORE)
*
* @var string
*/
public $type;

/**
* What the hint is for (INDEX/KEY)
*
* @var string
*/
public $indexOrKey;

/**
* The clause for which this hint is (JOIN/ORDER BY/GROUP BY)
*
* @var string
*/
public $for;

/**
* List of indexes in this hint
*
* @var array
*/
public $indexes = array();

/**
* Constructor.
*
* @param string $type the type of hint (USE/FORCE/IGNORE)
* @param string $indexOrKey What the hint is for (INDEX/KEY)
* @param string $for the clause for which this hint is (JOIN/ORDER BY/GROUP BY)
* @param string $indexes List of indexes in this hint
*/
public function __construct(string $type = null, string $indexOrKey = null, string $for = null, array $indexes = array())
{
$this->type = $type;
$this->indexOrKey = $indexOrKey;
$this->for = $for;
$this->indexes = $indexes;
}

/**
* @param Parser $parser the parser that serves as context
* @param TokensList $list the list of tokens that are being parsed
* @param array $options parameters for parsing
*
* @return IndexHint|Component[]
*/
public static function parse(Parser $parser, TokensList $list, array $options = array())
{
$ret = array();
$expr = new self();
$expr->type = isset($options['type']) ? $options['type'] : null;
/**
* The state of the parser.
*
* Below are the states of the parser.
* 0 ----------------- [ USE/IGNORE/FORCE ]-----------------> 1
* 1 -------------------- [ INDEX/KEY ] --------------------> 2
* 2 ----------------------- [ FOR ] -----------------------> 3
* 2 -------------------- [ expr_list ] --------------------> 0
* 3 -------------- [ JOIN/GROUP BY/ORDER BY ] -------------> 4
* 4 -------------------- [ expr_list ] --------------------> 0
* @var int
*/
$state = 0;

// By design, the parser will parse first token after the keyword. So, the keyword
// must be analyzed too, in order to determine the type of this index hint.
if ($list->idx > 0) {
--$list->idx;
}
for (; $list->idx < $list->count; ++$list->idx) {
/**
* Token parsed at this moment.
*
* @var Token
*/
$token = $list->tokens[$list->idx];

// End of statement.
if ($token->type === Token::TYPE_DELIMITER) {
break;
}
// Skipping whitespaces and comments.
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
continue;
}

switch ($state) {
case 0:
if ($token->type === Token::TYPE_KEYWORD) {
if ($token->keyword === 'USE' || $token->keyword === 'IGNORE' || $token->keyword === 'FORCE') {
$expr->type = $token->keyword;
$state = 1;
} else {
break 2;
}
}
break;
case 1:
if ($token->type === Token::TYPE_KEYWORD) {
if ($token->keyword === 'INDEX' || $token->keyword === 'KEY') {
$expr->indexOrKey = $token->keyword;
} else {
$parser->error('Unexpected keyword.', $token);
}
$state = 2;
} else {
// we expect the token to be a keyword
$parser->error('Unexpected token.', $token);
}
break;
case 2:
if ($token->type === Token::TYPE_KEYWORD && $token->keyword === 'FOR') {
$state = 3;
} else {
$expr->indexes = ExpressionArray::parse($parser, $list);
$state = 0;
$ret[] = $expr;
$expr = new self();
}
break;
case 3:
if ($token->type === Token::TYPE_KEYWORD) {
if ($token->keyword === 'JOIN' || $token->keyword === 'GROUP BY' || $token->keyword === 'ORDER BY') {
$expr->for = $token->keyword;
} else {
$parser->error('Unexpected keyword.', $token);
}
$state = 4;
} else {
// we expect the token to be a keyword
$parser->error('Unexpected token.', $token);
}
break;
case 4:
$expr->indexes = ExpressionArray::parse($parser, $list);
$state = 0;
$ret[] = $expr;
$expr = new self();
break;
}
}
--$list->idx;

return $ret;
}

/**
* @param ArrayObj|ArrayObj[] $component the component to be built
* @param array $options parameters for building
*
* @return string
*/
public static function build($component, array $options = array())
{
if (is_array($component)) {
return implode(' ', $component);
}

$ret = $component->type . ' ' . $component->indexOrKey . ' ';
if ($component->for !== null) {
$ret .= 'FOR ' . $component->for . ' ';
}
return $ret . ExpressionArray::build($component->indexes);
}
}
12 changes: 12 additions & 0 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ class Parser extends Core
'field' => 'fields',
'options' => array('parseField' => 'table'),
),
'FORCE' => array(
'class' => 'PhpMyAdmin\\SqlParser\\Components\\IndexHint',
'field' => 'index_hints',
),
'FROM' => array(
'class' => 'PhpMyAdmin\\SqlParser\\Components\\ExpressionArray',
'field' => 'from',
Expand All @@ -190,6 +194,10 @@ class Parser extends Core
'class' => 'PhpMyAdmin\\SqlParser\\Components\\Condition',
'field' => 'having',
),
'IGNORE' => array(
'class' => 'PhpMyAdmin\\SqlParser\\Components\\IndexHint',
'field' => 'index_hints',
),
'INTO' => array(
'class' => 'PhpMyAdmin\\SqlParser\\Components\\IntoKeyword',
'field' => 'into',
Expand Down Expand Up @@ -308,6 +316,10 @@ class Parser extends Core
'field' => 'tables',
'options' => array('parseField' => 'table'),
),
'USE' => array(
'class' => 'PhpMyAdmin\\SqlParser\\Components\\IndexHint',
'field' => 'index_hints',
),
'VALUE' => array(
'class' => 'PhpMyAdmin\\SqlParser\\Components\\Array2d',
'field' => 'values',
Expand Down
11 changes: 11 additions & 0 deletions src/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,17 @@ public function validateClauseOrder($parser, $list)
$clauseType
);

if ($clauseStartIdx !== -1
&& $this instanceof Statements\SelectStatement
&& ($clauseType === 'FORCE'
|| $clauseType === 'IGNORE'
|| $clauseType === 'USE')
) {
// TODO: ordering of clauses in a SELECT statement with
// Index hints is not supported
return true;
}

// Handle ordering of Multiple Joins in a query
if ($clauseStartIdx !== -1) {
if ($minJoin === 0 && stripos($clauseType, 'JOIN')) {
Expand Down
10 changes: 10 additions & 0 deletions src/Statements/SelectStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ class SelectStatement extends Statement
'_SELECT' => array('SELECT', 1),
'INTO' => array('INTO', 3),
'FROM' => array('FROM', 3),
'FORCE' => array('FORCE', 1),
'USE' => array('USE', 1),
'IGNORE' => array('IGNORE', 3),
'PARTITION' => array('PARTITION', 3),

'JOIN' => array('JOIN', 1),
Expand Down Expand Up @@ -135,6 +138,13 @@ class SelectStatement extends Statement
*/
public $from = array();

/**
* Index hints
*
* @var IndexHint[]
*/
public $index_hints;

/**
* Partitions used as source for this statement.
*
Expand Down
12 changes: 12 additions & 0 deletions tests/Builder/SelectStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,16 @@ public function testBuildGroupBy()
$stmt->build()
);
}

public function testBuildIndexHint()
{
$query = 'SELECT * FROM address FORCE INDEX (idx_fk_city_id) IGNORE KEY FOR GROUP BY (a, b,c) WHERE city_id<0 ';
$parser = new Parser($query);
$stmt = $parser->statements[0];

$this->assertEquals(
$query,
$stmt->build()
);
}
}
6 changes: 6 additions & 0 deletions tests/Parser/SelectStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ public function testSelectProvider()
array('parser/parseSelectEndOptionsErr'),
array('parser/parseSelectUnion'),
array('parser/parseSelectUnion2'),
array('parser/parseSelectIndexHint1'),
array('parser/parseSelectIndexHint2'),
array('parser/parseSelectIndexHintErr1'),
array('parser/parseSelectIndexHintErr2'),
array('parser/parseSelectIndexHintErr3'),
array('parser/parseSelectIndexHintErr4'),
);
}
}
1 change: 1 addition & 0 deletions tests/data/parser/parseSelectIndexHint1.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT * FROM address FORCE INDEX (idx_fk_city_id) IGNORE KEY FOR GROUP BY (a, b,c) WHERE city_id<0;
1 change: 1 addition & 0 deletions tests/data/parser/parseSelectIndexHint1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a:4:{s:5:"query";s:101:"SELECT * FROM address FORCE INDEX (idx_fk_city_id) IGNORE KEY FOR GROUP BY (a, b,c) WHERE city_id<0;";s:5:"lexer";O:26:"PhpMyAdmin\SqlParser\Lexer":8:{s:3:"str";s:101:"SELECT * FROM address FORCE INDEX (idx_fk_city_id) IGNORE KEY FOR GROUP BY (a, b,c) WHERE city_id<0;";s:3:"len";i:101;s:4:"last";i:101;s:4:"list";O:31:"PhpMyAdmin\SqlParser\TokensList":3:{s:6:"tokens";a:40:{i:0;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:6:"SELECT";s:5:"value";s:6:"SELECT";s:7:"keyword";s:6:"SELECT";s:4:"type";i:1;s:5:"flags";i:3;s:8:"position";i:0;}i:1;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:2:" ";s:5:"value";s:1:" ";s:7:"keyword";N;s:4:"type";i:3;s:5:"flags";i:0;s:8:"position";i:6;}i:2;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:"*";s:5:"value";s:1:"*";s:7:"keyword";N;s:4:"type";i:2;s:5:"flags";i:1;s:8:"position";i:8;}i:3;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:" ";s:5:"value";s:1:" ";s:7:"keyword";N;s:4:"type";i:3;s:5:"flags";i:0;s:8:"position";i:9;}i:4;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:4:"FROM";s:5:"value";s:4:"FROM";s:7:"keyword";s:4:"FROM";s:4:"type";i:1;s:5:"flags";i:3;s:8:"position";i:10;}i:5;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:" ";s:5:"value";s:1:" ";s:7:"keyword";N;s:4:"type";i:3;s:5:"flags";i:0;s:8:"position";i:14;}i:6;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:7:"address";s:5:"value";s:7:"address";s:7:"keyword";N;s:4:"type";i:0;s:5:"flags";i:0;s:8:"position";i:15;}i:7;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:" ";s:5:"value";s:1:" ";s:7:"keyword";N;s:4:"type";i:3;s:5:"flags";i:0;s:8:"position";i:22;}i:8;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:5:"FORCE";s:5:"value";s:5:"FORCE";s:7:"keyword";s:5:"FORCE";s:4:"type";i:1;s:5:"flags";i:3;s:8:"position";i:23;}i:9;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:" ";s:5:"value";s:1:" ";s:7:"keyword";N;s:4:"type";i:3;s:5:"flags";i:0;s:8:"position";i:28;}i:10;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:5:"INDEX";s:5:"value";s:5:"INDEX";s:7:"keyword";s:5:"INDEX";s:4:"type";i:1;s:5:"flags";i:19;s:8:"position";i:29;}i:11;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:" ";s:5:"value";s:1:" ";s:7:"keyword";N;s:4:"type";i:3;s:5:"flags";i:0;s:8:"position";i:34;}i:12;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:"(";s:5:"value";s:1:"(";s:7:"keyword";N;s:4:"type";i:2;s:5:"flags";i:16;s:8:"position";i:35;}i:13;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:14:"idx_fk_city_id";s:5:"value";s:14:"idx_fk_city_id";s:7:"keyword";N;s:4:"type";i:0;s:5:"flags";i:0;s:8:"position";i:36;}i:14;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:")";s:5:"value";s:1:")";s:7:"keyword";N;s:4:"type";i:2;s:5:"flags";i:16;s:8:"position";i:50;}i:15;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:" ";s:5:"value";s:1:" ";s:7:"keyword";N;s:4:"type";i:3;s:5:"flags";i:0;s:8:"position";i:51;}i:16;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:6:"IGNORE";s:5:"value";s:6:"IGNORE";s:7:"keyword";s:6:"IGNORE";s:4:"type";i:1;s:5:"flags";i:3;s:8:"position";i:52;}i:17;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:" ";s:5:"value";s:1:" ";s:7:"keyword";N;s:4:"type";i:3;s:5:"flags";i:0;s:8:"position";i:58;}i:18;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:3:"KEY";s:5:"value";s:3:"KEY";s:7:"keyword";s:3:"KEY";s:4:"type";i:1;s:5:"flags";i:19;s:8:"position";i:59;}i:19;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:" ";s:5:"value";s:1:" ";s:7:"keyword";N;s:4:"type";i:3;s:5:"flags";i:0;s:8:"position";i:62;}i:20;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:3:"FOR";s:5:"value";s:3:"FOR";s:7:"keyword";s:3:"FOR";s:4:"type";i:1;s:5:"flags";i:3;s:8:"position";i:63;}i:21;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:" ";s:5:"value";s:1:" ";s:7:"keyword";N;s:4:"type";i:3;s:5:"flags";i:0;s:8:"position";i:66;}i:22;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:8:"GROUP BY";s:5:"value";s:8:"GROUP BY";s:7:"keyword";s:8:"GROUP BY";s:4:"type";i:1;s:5:"flags";i:7;s:8:"position";i:67;}i:23;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:" ";s:5:"value";s:1:" ";s:7:"keyword";N;s:4:"type";i:3;s:5:"flags";i:0;s:8:"position";i:75;}i:24;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:"(";s:5:"value";s:1:"(";s:7:"keyword";N;s:4:"type";i:2;s:5:"flags";i:16;s:8:"position";i:76;}i:25;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:"a";s:5:"value";s:1:"a";s:7:"keyword";N;s:4:"type";i:0;s:5:"flags";i:0;s:8:"position";i:77;}i:26;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:",";s:5:"value";s:1:",";s:7:"keyword";N;s:4:"type";i:2;s:5:"flags";i:16;s:8:"position";i:78;}i:27;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:" ";s:5:"value";s:1:" ";s:7:"keyword";N;s:4:"type";i:3;s:5:"flags";i:0;s:8:"position";i:79;}i:28;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:"b";s:5:"value";s:1:"b";s:7:"keyword";N;s:4:"type";i:0;s:5:"flags";i:0;s:8:"position";i:80;}i:29;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:",";s:5:"value";s:1:",";s:7:"keyword";N;s:4:"type";i:2;s:5:"flags";i:16;s:8:"position";i:81;}i:30;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:"c";s:5:"value";s:1:"c";s:7:"keyword";N;s:4:"type";i:0;s:5:"flags";i:0;s:8:"position";i:82;}i:31;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:")";s:5:"value";s:1:")";s:7:"keyword";N;s:4:"type";i:2;s:5:"flags";i:16;s:8:"position";i:83;}i:32;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:" ";s:5:"value";s:1:" ";s:7:"keyword";N;s:4:"type";i:3;s:5:"flags";i:0;s:8:"position";i:84;}i:33;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:5:"WHERE";s:5:"value";s:5:"WHERE";s:7:"keyword";s:5:"WHERE";s:4:"type";i:1;s:5:"flags";i:3;s:8:"position";i:85;}i:34;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:" ";s:5:"value";s:1:" ";s:7:"keyword";N;s:4:"type";i:3;s:5:"flags";i:0;s:8:"position";i:90;}i:35;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:7:"city_id";s:5:"value";s:7:"city_id";s:7:"keyword";N;s:4:"type";i:0;s:5:"flags";i:0;s:8:"position";i:91;}i:36;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:"<";s:5:"value";s:1:"<";s:7:"keyword";N;s:4:"type";i:2;s:5:"flags";i:2;s:8:"position";i:98;}i:37;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:"0";s:5:"value";i:0;s:7:"keyword";N;s:4:"type";i:6;s:5:"flags";i:0;s:8:"position";i:99;}i:38;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";s:1:";";s:5:"value";s:1:";";s:7:"keyword";N;s:4:"type";i:9;s:5:"flags";i:0;s:8:"position";i:100;}i:39;O:26:"PhpMyAdmin\SqlParser\Token":6:{s:5:"token";N;s:5:"value";N;s:7:"keyword";N;s:4:"type";i:9;s:5:"flags";i:0;s:8:"position";N;}}s:5:"count";i:40;s:3:"idx";i:40;}s:9:"delimiter";s:1:";";s:12:"delimiterLen";i:1;s:6:"strict";b:0;s:6:"errors";a:0:{}}s:6:"parser";O:27:"PhpMyAdmin\SqlParser\Parser":5:{s:4:"list";r:7;s:10:"statements";a:1:{i:0;O:47:"PhpMyAdmin\SqlParser\Statements\SelectStatement":17:{s:4:"expr";a:1:{i:0;O:42:"PhpMyAdmin\SqlParser\Components\Expression":7:{s:8:"database";N;s:5:"table";N;s:6:"column";N;s:4:"expr";s:1:"*";s:5:"alias";N;s:8:"function";N;s:8:"subquery";N;}}s:4:"from";a:1:{i:0;O:42:"PhpMyAdmin\SqlParser\Components\Expression":7:{s:8:"database";N;s:5:"table";s:7:"address";s:6:"column";N;s:4:"expr";s:7:"address";s:5:"alias";N;s:8:"function";N;s:8:"subquery";N;}}s:11:"index_hints";a:2:{i:0;O:41:"PhpMyAdmin\SqlParser\Components\IndexHint":4:{s:4:"type";s:5:"FORCE";s:10:"indexOrKey";s:5:"INDEX";s:3:"for";N;s:7:"indexes";a:1:{i:0;O:42:"PhpMyAdmin\SqlParser\Components\Expression":7:{s:8:"database";N;s:5:"table";N;s:6:"column";N;s:4:"expr";s:16:"(idx_fk_city_id)";s:5:"alias";N;s:8:"function";N;s:8:"subquery";N;}}}i:1;O:41:"PhpMyAdmin\SqlParser\Components\IndexHint":4:{s:4:"type";s:6:"IGNORE";s:10:"indexOrKey";s:3:"KEY";s:3:"for";s:8:"GROUP BY";s:7:"indexes";a:1:{i:0;O:42:"PhpMyAdmin\SqlParser\Components\Expression":7:{s:8:"database";N;s:5:"table";N;s:6:"column";N;s:4:"expr";s:8:"(a, b,c)";s:5:"alias";N;s:8:"function";N;s:8:"subquery";N;}}}}s:9:"partition";N;s:5:"where";a:1:{i:0;O:41:"PhpMyAdmin\SqlParser\Components\Condition":3:{s:11:"identifiers";a:1:{i:0;s:7:"city_id";}s:10:"isOperator";b:0;s:4:"expr";s:9:"city_id<0";}}s:5:"group";N;s:6:"having";N;s:5:"order";N;s:5:"limit";N;s:9:"procedure";N;s:4:"into";N;s:4:"join";N;s:5:"union";a:0:{}s:11:"end_options";N;s:7:"options";O:44:"PhpMyAdmin\SqlParser\Components\OptionsArray":1:{s:7:"options";a:0:{}}s:5:"first";i:0;s:4:"last";i:37;}}s:8:"brackets";i:0;s:6:"strict";b:0;s:6:"errors";a:0:{}}s:6:"errors";a:2:{s:5:"lexer";a:0:{}s:6:"parser";a:0:{}}}
1 change: 1 addition & 0 deletions tests/data/parser/parseSelectIndexHint2.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT * FROM address USE INDEX (idx_fk_city_id) FORCE KEY FOR GROUP BY (a, b,c) WHERE city_id<0
Loading