Skip to content

Commit d30697f

Browse files
authored
chore(query): alternative to tbl_name FROM db_name syntax is db_name.tbl_name (#17848)
1 parent 572642a commit d30697f

File tree

5 files changed

+116
-9
lines changed

5 files changed

+116
-9
lines changed

โ€Žsrc/query/ast/src/parser/statement.rs

+25-8
Original file line numberDiff line numberDiff line change
@@ -786,20 +786,37 @@ pub fn statement_body(i: Input) -> IResult<Statement> {
786786
})
787787
},
788788
);
789+
790+
pub fn from_tables(i: Input) -> IResult<(Option<Identifier>, Option<Identifier>, Identifier)> {
791+
let from_dot_table = map(
792+
rule! {
793+
( FROM | IN ) ~ ^#dot_separated_idents_1_to_3
794+
},
795+
|(_, (catalog, database, table))| (catalog, database, table),
796+
);
797+
798+
let from_table = map(
799+
rule! {
800+
( FROM | IN ) ~ #ident
801+
~ ( FROM | IN ) ~ ^#dot_separated_idents_1_to_2
802+
},
803+
|(_, table, _, (catalog, database))| (catalog, Some(database), table),
804+
);
805+
806+
rule!(
807+
#from_table
808+
| #from_dot_table
809+
)(i)
810+
}
811+
789812
let show_columns = map(
790813
rule! {
791814
SHOW
792815
~ FULL? ~ COLUMNS
793-
~ ( FROM | IN ) ~ #ident
794-
~ (( FROM | IN ) ~ ^#dot_separated_idents_1_to_2)?
816+
~ #from_tables
795817
~ #show_limit?
796818
},
797-
|(_, opt_full, _, _, table, ctl_db, limit)| {
798-
let (catalog, database) = match ctl_db {
799-
Some((_, (Some(c), d))) => (Some(c), Some(d)),
800-
Some((_, (None, d))) => (None, Some(d)),
801-
_ => (None, None),
802-
};
819+
|(_, opt_full, _, (catalog, database, table), limit)| {
803820
Statement::ShowColumns(ShowColumnsStmt {
804821
catalog,
805822
database,

โ€Žsrc/query/ast/tests/it/parser.rs

+3
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ fn test_statement() {
106106
r#"show full tables from ctl.db"#,
107107
r#"show full columns in t in db"#,
108108
r#"show columns in t from ctl.db"#,
109+
r#"show columns from ctl.db.t"#,
109110
r#"show full columns from t from db like 'id%'"#,
111+
r#"show full columns from db.t like 'id%'"#,
110112
r#"show processlist like 't%' limit 2;"#,
111113
r#"show processlist where database='default' limit 2;"#,
112114
r#"show create table a.b;"#,
@@ -970,6 +972,7 @@ fn test_statement_error() {
970972
r#"CALL system$test(a"#,
971973
r#"show settings ilike 'enable%'"#,
972974
r#"PRESIGN INVALID @my_stage/path/to/file"#,
975+
r#"show columns from db1.t from ctl.db"#,
973976
r#"SELECT c a as FROM t"#,
974977
r#"SELECT c a as b FROM t"#,
975978
r#"SELECT top -1 c a as b FROM t"#,

โ€Žsrc/query/ast/tests/it/testdata/stmt-error.txt

+10
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,16 @@ error:
474474
| while parsing `PRESIGN [{DOWNLOAD | UPLOAD}] <location> [EXPIRE = 3600]`
475475

476476

477+
---------- Input ----------
478+
show columns from db1.t from ctl.db
479+
---------- Output ---------
480+
error:
481+
--> SQL:1:25
482+
|
483+
1 | show columns from db1.t from ctl.db
484+
| ^^^^ unexpected `from`, expecting `FORMAT`, `.`, `LIKE`, `WHERE`, or `;`
485+
486+
477487
---------- Input ----------
478488
SELECT c a as FROM t
479489
---------- Output ---------

โ€Žsrc/query/ast/tests/it/testdata/stmt.txt

+77
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,47 @@ ShowColumns(
490490
)
491491

492492

493+
---------- Input ----------
494+
show columns from ctl.db.t
495+
---------- Output ---------
496+
SHOW COLUMNS FROM t FROM ctl.db
497+
---------- AST ------------
498+
ShowColumns(
499+
ShowColumnsStmt {
500+
catalog: Some(
501+
Identifier {
502+
span: Some(
503+
18..21,
504+
),
505+
name: "ctl",
506+
quote: None,
507+
ident_type: None,
508+
},
509+
),
510+
database: Some(
511+
Identifier {
512+
span: Some(
513+
22..24,
514+
),
515+
name: "db",
516+
quote: None,
517+
ident_type: None,
518+
},
519+
),
520+
table: Identifier {
521+
span: Some(
522+
25..26,
523+
),
524+
name: "t",
525+
quote: None,
526+
ident_type: None,
527+
},
528+
full: false,
529+
limit: None,
530+
},
531+
)
532+
533+
493534
---------- Input ----------
494535
show full columns from t from db like 'id%'
495536
---------- Output ---------
@@ -526,6 +567,42 @@ ShowColumns(
526567
)
527568

528569

570+
---------- Input ----------
571+
show full columns from db.t like 'id%'
572+
---------- Output ---------
573+
SHOW FULL COLUMNS FROM t FROM db LIKE 'id%'
574+
---------- AST ------------
575+
ShowColumns(
576+
ShowColumnsStmt {
577+
catalog: None,
578+
database: Some(
579+
Identifier {
580+
span: Some(
581+
23..25,
582+
),
583+
name: "db",
584+
quote: None,
585+
ident_type: None,
586+
},
587+
),
588+
table: Identifier {
589+
span: Some(
590+
26..27,
591+
),
592+
name: "t",
593+
quote: None,
594+
ident_type: None,
595+
},
596+
full: true,
597+
limit: Some(
598+
Like {
599+
pattern: "id%",
600+
},
601+
),
602+
},
603+
)
604+
605+
529606
---------- Input ----------
530607
show processlist like 't%' limit 2;
531608
---------- Output ---------

โ€Žtests/sqllogictests/suites/base/06_show/06_0015_show_columns.test

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ statement ok
1414
CREATE TABLE showcolumn.t3(c1 int null default 4, c2 Datetime not null default '2022-02-02 12:00:00', c3 String not null Default 'c3') ENGINE = Null;
1515

1616
query TTTTTT
17-
SHOW COLUMNS FROM t3 FROM showcolumn
17+
SHOW COLUMNS FROM default.showcolumn.t3
1818
----
1919
c1 INT YES 4 NULL NULL
2020
c2 TIMESTAMP NO '2022-02-02 12:00:00.000000' NULL NULL

0 commit comments

Comments
ย (0)