Skip to content

Commit f24aa16

Browse files
committed
Merge #423 - Fix #377 - PARTITION syntax errors
Pull-request: #423 Fixes: #377 Signed-off-by: William Desportes <williamdes@wdes.fr>
2 parents 0ae405f + 72c52dc commit f24aa16

6 files changed

Lines changed: 833 additions & 19 deletions

File tree

src/Components/AlterOperation.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,8 @@ public static function parse(Parser $parser, TokensList $list, array $options =
440440
$ret->unknown[] = $token;
441441
} elseif ($state === 3) {
442442
if ($partitionState === 0) {
443-
$list->idx++; // Ignore the current token
444-
$nextToken = $list->getNext();
443+
$list->idx++; // Ignore the current token
444+
$nextToken = $list->getNext();
445445
if (
446446
($token->type === Token::TYPE_KEYWORD)
447447
&& (($token->keyword === 'PARTITION BY')
@@ -460,12 +460,27 @@ public static function parse(Parser $parser, TokensList $list, array $options =
460460

461461
++$list->idx; // to index the idx by one, because the last getPrevious returned and decreased it.
462462
} elseif ($partitionState === 1) {
463+
// Fetch the next token in a way the current index is reset to manage whitespaces in "field".
464+
$currIdx = $list->idx;
465+
++$list->idx;
466+
$nextToken = $list->getNext();
467+
$list->idx = $currIdx;
463468
// Building the expression used for partitioning.
464469
if (empty($ret->field)) {
465470
$ret->field = '';
466471
}
467472

468-
$ret->field .= $token->type === Token::TYPE_WHITESPACE ? ' ' : $token->token;
473+
if (
474+
$token->type === Token::TYPE_OPERATOR
475+
&& $token->value === '('
476+
&& $nextToken
477+
&& $nextToken->keyword === 'PARTITION'
478+
) {
479+
$partitionState = 2;
480+
--$list->idx; // Current idx is on "(". We need a step back for ArrayObj::parse incoming.
481+
} else {
482+
$ret->field .= $token->type === Token::TYPE_WHITESPACE ? ' ' : $token->token;
483+
}
469484
} elseif ($partitionState === 2) {
470485
$ret->partitions = ArrayObj::parse(
471486
$parser,

tests/Builder/AlterStatementTest.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -127,20 +127,21 @@ public function testBuilderPartitions(): void
127127
$stmt = $parser->statements[0];
128128

129129
$this->assertEquals(
130-
'ALTER TABLE trips PARTITION BY RANGE (MONTH(trip_date)) ('
131-
. ' PARTITION p01 VALUES LESS THAN (02),'
132-
. ' PARTITION p02 VALUES LESS THAN (03),'
133-
. ' PARTITION p03 VALUES LESS THAN (04),'
134-
. ' PARTITION p04 VALUES LESS THAN (05),'
135-
. ' PARTITION p05 VALUES LESS THAN (06),'
136-
. ' PARTITION p06 VALUES LESS THAN (07),'
137-
. ' PARTITION p07 VALUES LESS THAN (08),'
138-
. ' PARTITION p08 VALUES LESS THAN (09),'
139-
. ' PARTITION p09 VALUES LESS THAN (10),'
140-
. ' PARTITION p10 VALUES LESS THAN (11),'
141-
. ' PARTITION p11 VALUES LESS THAN (12),'
142-
. ' PARTITION p12 VALUES LESS THAN (13),'
143-
. ' PARTITION pmaxval VALUES LESS THAN MAXVALUE )',
130+
'ALTER TABLE trips PARTITION BY RANGE (MONTH(trip_date)) (' . "\n"
131+
. 'PARTITION p01 VALUES LESS THAN (02),' . "\n"
132+
. 'PARTITION p02 VALUES LESS THAN (03),' . "\n"
133+
. 'PARTITION p03 VALUES LESS THAN (04),' . "\n"
134+
. 'PARTITION p04 VALUES LESS THAN (05),' . "\n"
135+
. 'PARTITION p05 VALUES LESS THAN (06),' . "\n"
136+
. 'PARTITION p06 VALUES LESS THAN (07),' . "\n"
137+
. 'PARTITION p07 VALUES LESS THAN (08),' . "\n"
138+
. 'PARTITION p08 VALUES LESS THAN (09),' . "\n"
139+
. 'PARTITION p09 VALUES LESS THAN (10),' . "\n"
140+
. 'PARTITION p10 VALUES LESS THAN (11),' . "\n"
141+
. 'PARTITION p11 VALUES LESS THAN (12),' . "\n"
142+
. 'PARTITION p12 VALUES LESS THAN (13),' . "\n"
143+
. 'PARTITION pmaxval VALUES LESS THAN MAXVALUE' . "\n"
144+
. ')',
144145
$stmt->build()
145146
);
146147
}

tests/Parser/AlterStatementTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public function alterProvider(): array
4242
['parser/parseAlterErr4'],
4343
['parser/parseAlterTableRenameIndex'],
4444
['parser/parseAlterTablePartitionByRange1'],
45+
['parser/parseAlterTablePartitionByRange2'],
4546
['parser/parseAlterWithInvisible'],
4647
['parser/parseAlterTableCharacterSet1'],
4748
['parser/parseAlterTableCharacterSet2'],

tests/data/parser/parseAlterTablePartitionByRange1.out

Lines changed: 267 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,8 +1736,273 @@
17361736
"2": "PARTITION BY"
17371737
}
17381738
},
1739-
"field": " RANGE (MONTH(trip_date)) ( PARTITION p01 VALUES LESS THAN (02), PARTITION p02 VALUES LESS THAN (03), PARTITION p03 VALUES LESS THAN (04), PARTITION p04 VALUES LESS THAN (05), PARTITION p05 VALUES LESS THAN (06), PARTITION p06 VALUES LESS THAN (07), PARTITION p07 VALUES LESS THAN (08), PARTITION p08 VALUES LESS THAN (09), PARTITION p09 VALUES LESS THAN (10), PARTITION p10 VALUES LESS THAN (11), PARTITION p11 VALUES LESS THAN (12), PARTITION p12 VALUES LESS THAN (13), PARTITION pmaxval VALUES LESS THAN MAXVALUE )",
1740-
"partitions": null,
1739+
"field": " RANGE (MONTH(trip_date)) ",
1740+
"partitions": [
1741+
{
1742+
"@type": "PhpMyAdmin\\SqlParser\\Components\\PartitionDefinition",
1743+
"isSubpartition": false,
1744+
"name": "p01",
1745+
"type": "LESS THAN",
1746+
"expr": {
1747+
"@type": "PhpMyAdmin\\SqlParser\\Components\\Expression",
1748+
"database": null,
1749+
"table": null,
1750+
"column": null,
1751+
"expr": "(02)",
1752+
"alias": null,
1753+
"function": null,
1754+
"subquery": null
1755+
},
1756+
"subpartitions": null,
1757+
"options": {
1758+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
1759+
"options": []
1760+
}
1761+
},
1762+
{
1763+
"@type": "PhpMyAdmin\\SqlParser\\Components\\PartitionDefinition",
1764+
"isSubpartition": false,
1765+
"name": "p02",
1766+
"type": "LESS THAN",
1767+
"expr": {
1768+
"@type": "PhpMyAdmin\\SqlParser\\Components\\Expression",
1769+
"database": null,
1770+
"table": null,
1771+
"column": null,
1772+
"expr": "(03)",
1773+
"alias": null,
1774+
"function": null,
1775+
"subquery": null
1776+
},
1777+
"subpartitions": null,
1778+
"options": {
1779+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
1780+
"options": []
1781+
}
1782+
},
1783+
{
1784+
"@type": "PhpMyAdmin\\SqlParser\\Components\\PartitionDefinition",
1785+
"isSubpartition": false,
1786+
"name": "p03",
1787+
"type": "LESS THAN",
1788+
"expr": {
1789+
"@type": "PhpMyAdmin\\SqlParser\\Components\\Expression",
1790+
"database": null,
1791+
"table": null,
1792+
"column": null,
1793+
"expr": "(04)",
1794+
"alias": null,
1795+
"function": null,
1796+
"subquery": null
1797+
},
1798+
"subpartitions": null,
1799+
"options": {
1800+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
1801+
"options": []
1802+
}
1803+
},
1804+
{
1805+
"@type": "PhpMyAdmin\\SqlParser\\Components\\PartitionDefinition",
1806+
"isSubpartition": false,
1807+
"name": "p04",
1808+
"type": "LESS THAN",
1809+
"expr": {
1810+
"@type": "PhpMyAdmin\\SqlParser\\Components\\Expression",
1811+
"database": null,
1812+
"table": null,
1813+
"column": null,
1814+
"expr": "(05)",
1815+
"alias": null,
1816+
"function": null,
1817+
"subquery": null
1818+
},
1819+
"subpartitions": null,
1820+
"options": {
1821+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
1822+
"options": []
1823+
}
1824+
},
1825+
{
1826+
"@type": "PhpMyAdmin\\SqlParser\\Components\\PartitionDefinition",
1827+
"isSubpartition": false,
1828+
"name": "p05",
1829+
"type": "LESS THAN",
1830+
"expr": {
1831+
"@type": "PhpMyAdmin\\SqlParser\\Components\\Expression",
1832+
"database": null,
1833+
"table": null,
1834+
"column": null,
1835+
"expr": "(06)",
1836+
"alias": null,
1837+
"function": null,
1838+
"subquery": null
1839+
},
1840+
"subpartitions": null,
1841+
"options": {
1842+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
1843+
"options": []
1844+
}
1845+
},
1846+
{
1847+
"@type": "PhpMyAdmin\\SqlParser\\Components\\PartitionDefinition",
1848+
"isSubpartition": false,
1849+
"name": "p06",
1850+
"type": "LESS THAN",
1851+
"expr": {
1852+
"@type": "PhpMyAdmin\\SqlParser\\Components\\Expression",
1853+
"database": null,
1854+
"table": null,
1855+
"column": null,
1856+
"expr": "(07)",
1857+
"alias": null,
1858+
"function": null,
1859+
"subquery": null
1860+
},
1861+
"subpartitions": null,
1862+
"options": {
1863+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
1864+
"options": []
1865+
}
1866+
},
1867+
{
1868+
"@type": "PhpMyAdmin\\SqlParser\\Components\\PartitionDefinition",
1869+
"isSubpartition": false,
1870+
"name": "p07",
1871+
"type": "LESS THAN",
1872+
"expr": {
1873+
"@type": "PhpMyAdmin\\SqlParser\\Components\\Expression",
1874+
"database": null,
1875+
"table": null,
1876+
"column": null,
1877+
"expr": "(08)",
1878+
"alias": null,
1879+
"function": null,
1880+
"subquery": null
1881+
},
1882+
"subpartitions": null,
1883+
"options": {
1884+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
1885+
"options": []
1886+
}
1887+
},
1888+
{
1889+
"@type": "PhpMyAdmin\\SqlParser\\Components\\PartitionDefinition",
1890+
"isSubpartition": false,
1891+
"name": "p08",
1892+
"type": "LESS THAN",
1893+
"expr": {
1894+
"@type": "PhpMyAdmin\\SqlParser\\Components\\Expression",
1895+
"database": null,
1896+
"table": null,
1897+
"column": null,
1898+
"expr": "(09)",
1899+
"alias": null,
1900+
"function": null,
1901+
"subquery": null
1902+
},
1903+
"subpartitions": null,
1904+
"options": {
1905+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
1906+
"options": []
1907+
}
1908+
},
1909+
{
1910+
"@type": "PhpMyAdmin\\SqlParser\\Components\\PartitionDefinition",
1911+
"isSubpartition": false,
1912+
"name": "p09",
1913+
"type": "LESS THAN",
1914+
"expr": {
1915+
"@type": "PhpMyAdmin\\SqlParser\\Components\\Expression",
1916+
"database": null,
1917+
"table": null,
1918+
"column": null,
1919+
"expr": "(10)",
1920+
"alias": null,
1921+
"function": null,
1922+
"subquery": null
1923+
},
1924+
"subpartitions": null,
1925+
"options": {
1926+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
1927+
"options": []
1928+
}
1929+
},
1930+
{
1931+
"@type": "PhpMyAdmin\\SqlParser\\Components\\PartitionDefinition",
1932+
"isSubpartition": false,
1933+
"name": "p10",
1934+
"type": "LESS THAN",
1935+
"expr": {
1936+
"@type": "PhpMyAdmin\\SqlParser\\Components\\Expression",
1937+
"database": null,
1938+
"table": null,
1939+
"column": null,
1940+
"expr": "(11)",
1941+
"alias": null,
1942+
"function": null,
1943+
"subquery": null
1944+
},
1945+
"subpartitions": null,
1946+
"options": {
1947+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
1948+
"options": []
1949+
}
1950+
},
1951+
{
1952+
"@type": "PhpMyAdmin\\SqlParser\\Components\\PartitionDefinition",
1953+
"isSubpartition": false,
1954+
"name": "p11",
1955+
"type": "LESS THAN",
1956+
"expr": {
1957+
"@type": "PhpMyAdmin\\SqlParser\\Components\\Expression",
1958+
"database": null,
1959+
"table": null,
1960+
"column": null,
1961+
"expr": "(12)",
1962+
"alias": null,
1963+
"function": null,
1964+
"subquery": null
1965+
},
1966+
"subpartitions": null,
1967+
"options": {
1968+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
1969+
"options": []
1970+
}
1971+
},
1972+
{
1973+
"@type": "PhpMyAdmin\\SqlParser\\Components\\PartitionDefinition",
1974+
"isSubpartition": false,
1975+
"name": "p12",
1976+
"type": "LESS THAN",
1977+
"expr": {
1978+
"@type": "PhpMyAdmin\\SqlParser\\Components\\Expression",
1979+
"database": null,
1980+
"table": null,
1981+
"column": null,
1982+
"expr": "(13)",
1983+
"alias": null,
1984+
"function": null,
1985+
"subquery": null
1986+
},
1987+
"subpartitions": null,
1988+
"options": {
1989+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
1990+
"options": []
1991+
}
1992+
},
1993+
{
1994+
"@type": "PhpMyAdmin\\SqlParser\\Components\\PartitionDefinition",
1995+
"isSubpartition": false,
1996+
"name": "pmaxval",
1997+
"type": "LESS THAN",
1998+
"expr": "MAXVALUE",
1999+
"subpartitions": null,
2000+
"options": {
2001+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
2002+
"options": []
2003+
}
2004+
}
2005+
],
17412006
"unknown": []
17422007
}
17432008
],
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)