Skip to content

Commit dc7184e

Browse files
committed
Proposal to fix #377: understand correctly PARTITION BY RANGE.
1 parent 9412413 commit dc7184e

File tree

4 files changed

+1052
-3
lines changed

4 files changed

+1052
-3
lines changed

src/Components/AlterOperation.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,8 @@ public static function parse(Parser $parser, TokensList $list, array $options =
432432
$ret->unknown[] = $token;
433433
} elseif ($state === 3) {
434434
if ($partitionState === 0) {
435-
$list->idx++; // Ignore the current token
436-
$nextToken = $list->getNext();
435+
$list->idx++; // Ignore the current token
436+
$nextToken = $list->getNext();
437437
if (
438438
($token->type === Token::TYPE_KEYWORD)
439439
&& (($token->keyword === 'PARTITION BY')
@@ -452,12 +452,27 @@ public static function parse(Parser $parser, TokensList $list, array $options =
452452

453453
++$list->idx; // to index the idx by one, because the last getPrevious returned and decreased it.
454454
} elseif ($partitionState === 1) {
455+
// Fetch the next token in a way the current index is reset to manage whitespaces in "field".
456+
$currIdx = $list->idx;
457+
++$list->idx;
458+
$nextToken = $list->getNext();
459+
$list->idx = $currIdx;
455460
// Building the expression used for partitioning.
456461
if (empty($ret->field)) {
457462
$ret->field = '';
458463
}
459464

460-
$ret->field .= $token->type === Token::TYPE_WHITESPACE ? ' ' : $token->token;
465+
if (
466+
$token->type === Token::TYPE_OPERATOR
467+
&& $token->value === '('
468+
&& $nextToken
469+
&& $nextToken->keyword === 'PARTITION'
470+
) {
471+
$partitionState = 2;
472+
--$list->idx; // Current idx is on "(". We need a step back for ArrayObj::parse incoming.
473+
} else {
474+
$ret->field .= $token->type === Token::TYPE_WHITESPACE ? ' ' : $token->token;
475+
}
461476
} elseif ($partitionState === 2) {
462477
$ret->partitions = ArrayObj::parse(
463478
$parser,

tests/Parser/AlterStatementTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function alterProvider(): array
4848
['parser/parseAlterTableCharacterSet5'],
4949
['parser/parseAlterTableCharacterSet6'],
5050
['parser/parseAlterTableCharacterSet7'],
51+
['parser/parseAlterTablePartitionByRange'],
5152
['parser/parseAlterUser'],
5253
['parser/parseAlterUser1'],
5354
['parser/parseAlterUser2'],
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ALTER TABLE d PARTITION BY RANGE (MONTH(departure_date))
2+
(
3+
PARTITION p01 VALUES LESS THAN (02) ,
4+
PARTITION pmaxval VALUES LESS THAN MAXVALUE
5+
);

0 commit comments

Comments
 (0)