Skip to content

Commit dba34f3

Browse files
committed
as/asdb/astbl fixed
1 parent 8371616 commit dba34f3

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

src/ClickHouseSQLParser.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,8 @@ public function tableCreaterFnParser($sql) {
699699
'table' => 'table',
700700
'dbtb' => 'dbtb',
701701
'as' => 'as',
702-
'asid' => 'asid',
702+
'asdb' => 'asdb',
703+
'astbl' => 'astbl',
703704
'oncl' =>'on_cluster',
704705
'clus' => 'cluster',
705706
]);
@@ -712,14 +713,16 @@ public function tableCreaterFnParser($sql) {
712713
$dbpat = "({$idpax(12, $db)}\.)?";
713714
$tbpat = "({$idpax(15, $table)})";
714715
$pdbtb = "(?<$dbtb>{$dbpat}{$tbpat})\s";
715-
$aspat = "(?<$as>AS\s{$idpax(18, $asid)}\s)?";
716-
$oncpat = "(?<$oncl>ON\sCLUSTER\s{$idpax(21, $clus)}\s)?";
716+
$dbaspat = "({$idpax(19, $asdb)}\.)?";
717+
$aspat = "(?<$as>AS\s{$dbaspat}{$idpax(21, $astbl)}\s)?";
718+
$oncpat = "(?<$oncl>ON\sCLUSTER\s{$idpax(24, $clus)}\s)?";
717719

718720
$sumpat = "#(?<$crefn>^CREATE(\s+){$tmpat}TABLE(\s+){$ifnxpat}"
719721
. "{$pdbtb}{$aspat}{$oncpat})(?<{$crepar}>.*)#is";
720722

721723
$matches = $ret = [];
722724
\preg_match_all($sumpat, $sql, $matches);
725+
723726
if (!empty($matches[$crefn]) && !empty($matches[$crepar])) {
724727
foreach ($interesting_vars as $key) {
725728
$ret[$key] = \trim($matches[$key][0]);
@@ -841,7 +844,7 @@ public function parseCreateTableSql($create_full)
841844
// $create_fn, $create_par
842845
// $temporary (bool), $if_not_exists (bool)
843846
// $database (str|false), $table (str), $dbtb (str),
844-
// $as (bool), $asid (str)
847+
// $as (bool), $astbl (str)
845848
// $on_cluster (bool), $cluster (str)
846849

847850
if (\strtolower(\substr($create_par, 0, 6)) == 'engine') {
@@ -875,7 +878,8 @@ public function parseCreateTableSql($create_full)
875878
'database', //false|str
876879
'temporary', //false|true
877880
'if_not_exists', //false|true
878-
'asid', //false|str
881+
'asdb', //false|str
882+
'astbl', //false|str
879883
'cluster', //false|str
880884

881885
'create_fields', //str Second part of sql request, optional

tests/src/CreaterTableParserTest.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ public function tableCreaterFnParserProvider()
150150
'table' => 'x',
151151
'dbtb' => 'db.x',
152152
'as' => false,
153-
'asid' => false,
153+
'asdb' => false,
154+
'astbl' => false,
154155
'on_cluster' => false,
155156
'cluster' => false,
156157
],
@@ -168,25 +169,27 @@ public function tableCreaterFnParserProvider()
168169
'table' => 'x',
169170
'dbtb' => 'db.x',
170171
'as' => true,
171-
'asid' => 'abc',
172+
'asdb' => false,
173+
'astbl' => 'abc',
172174
'on_cluster' => false,
173175
'cluster' => false,
174176
],
175177
0,0],
176178

177179
[ //sql
178-
"CREATE TEMPORARY TABLE IF NOT EXISTS db.x AS abc ON CLUSTER xxx $fields $engine",
180+
"CREATE TEMPORARY TABLE IF NOT EXISTS db.x AS `xxx yyy`.abc ON CLUSTER xxx $fields $engine",
179181
//regpars
180182
[
181-
'create_fn' => 'CREATE TEMPORARY TABLE IF NOT EXISTS db.x AS abc ON CLUSTER xxx',
183+
'create_fn' => 'CREATE TEMPORARY TABLE IF NOT EXISTS db.x AS `xxx yyy`.abc ON CLUSTER xxx',
182184
'create_par' => "$fields $engine",
183185
'temporary' => true,
184186
'if_not_exists' => true,
185187
'database' => 'db',
186188
'table' => 'x',
187189
'dbtb' => 'db.x',
188190
'as' => true,
189-
'asid' => 'abc',
191+
'asdb' => 'xxx yyy',
192+
'astbl' => 'abc',
190193
'on_cluster' => true,
191194
'cluster' => 'xxx',
192195
],
@@ -204,7 +207,8 @@ public function tableCreaterFnParserProvider()
204207
'table' => 'x',
205208
'dbtb' => 'db.x',
206209
'as' => false,
207-
'asid' => false,
210+
'asdb' => false,
211+
'astbl' => false,
208212
'on_cluster' => true,
209213
'cluster' => 'The cluster',
210214
],
@@ -316,7 +320,8 @@ public function tableCreaterFnParserProvider()
316320
'temporary' => true,
317321
'if_not_exists' => true,
318322
'as' => true,
319-
'asid' => 'tbl',
323+
'asdb' => false,
324+
'astbl' => 'tbl',
320325
'on_cluster' => true,
321326
'cluster' => 'clust',
322327
],
@@ -364,7 +369,8 @@ public function createTableSQLRequestsProvider()
364369
'database' => 'db',
365370
'temporary' => false,
366371
'if_not_exists' => false,
367-
'asid' => false,
372+
'asdb' => false,
373+
'astbl' => false,
368374
'cluster' => false,
369375
'create_fields' => 3,
370376
'names' => ['id', 'dt', 's'],
@@ -402,15 +408,16 @@ public function createTableSQLRequestsProvider()
402408
],
403409

404410
[
405-
"CREATE TABLE `dv`.`partd` AS `part` ENGINE = "
411+
"CREATE TABLE `dv`.`partd` AS dx.`part` ENGINE = "
406412
. "Distributed(perftest_3shards_1replicas, default, part, rand());",
407413
[
408-
'create_fn' => 'CREATE TABLE `dv`.`partd` AS `part`',
414+
'create_fn' => 'CREATE TABLE `dv`.`partd` AS dx.`part`',
409415
'table' => 'partd',
410416
'database' => 'dv',
411417
'temporary' => false,
412418
'if_not_exists' => false,
413-
'asid' => 'part',
419+
'asdb' => 'dx',
420+
'astbl' => 'part',
414421
'cluster' => false,
415422
'create_fields' => 0,
416423
'create_engine' => 'ENGINE = Distributed(perftest_3shards_1replicas, default, part, rand());',
@@ -440,7 +447,8 @@ public function createTableSQLRequestsProvider()
440447
'database' => false,
441448
'temporary' => false,
442449
'if_not_exists' => true,
443-
'asid' => false,
450+
'asdb' => false,
451+
'astbl' => false,
444452
'cluster' => 'cluster',
445453
'names' => ['p', 'i'],
446454
'create_fields' => 2,
@@ -470,7 +478,8 @@ public function createTableSQLRequestsProvider()
470478
'database' => false,
471479
'temporary' => false,
472480
'if_not_exists' => false,
473-
'asid' => false,
481+
'asdb' => false,
482+
'astbl' => false,
474483
'cluster' => false,
475484
'create_fields' => 3,
476485
'parse_fields' => 3,

0 commit comments

Comments
 (0)