Skip to content

chore(query): alternative to tbl_name FROM db_name syntax is db_name.tbl_name #17848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2025
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
33 changes: 25 additions & 8 deletions src/query/ast/src/parser/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -786,20 +786,37 @@ pub fn statement_body(i: Input) -> IResult<Statement> {
})
},
);

pub fn from_tables(i: Input) -> IResult<(Option<Identifier>, Option<Identifier>, Identifier)> {
let from_dot_table = map(
rule! {
( FROM | IN ) ~ ^#dot_separated_idents_1_to_3
},
|(_, (catalog, database, table))| (catalog, database, table),
);

let from_table = map(
rule! {
( FROM | IN ) ~ #ident
~ ( FROM | IN ) ~ ^#dot_separated_idents_1_to_2
},
|(_, table, _, (catalog, database))| (catalog, Some(database), table),
);

rule!(
#from_table
| #from_dot_table
)(i)
}

let show_columns = map(
rule! {
SHOW
~ FULL? ~ COLUMNS
~ ( FROM | IN ) ~ #ident
~ (( FROM | IN ) ~ ^#dot_separated_idents_1_to_2)?
~ #from_tables
~ #show_limit?
},
|(_, opt_full, _, _, table, ctl_db, limit)| {
let (catalog, database) = match ctl_db {
Some((_, (Some(c), d))) => (Some(c), Some(d)),
Some((_, (None, d))) => (None, Some(d)),
_ => (None, None),
};
|(_, opt_full, _, (catalog, database, table), limit)| {
Statement::ShowColumns(ShowColumnsStmt {
catalog,
database,
Expand Down
3 changes: 3 additions & 0 deletions src/query/ast/tests/it/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ fn test_statement() {
r#"show full tables from ctl.db"#,
r#"show full columns in t in db"#,
r#"show columns in t from ctl.db"#,
r#"show columns from ctl.db.t"#,
r#"show full columns from t from db like 'id%'"#,
r#"show full columns from db.t like 'id%'"#,
r#"show processlist like 't%' limit 2;"#,
r#"show processlist where database='default' limit 2;"#,
r#"show create table a.b;"#,
Expand Down Expand Up @@ -968,6 +970,7 @@ fn test_statement_error() {
r#"CALL system$test(a"#,
r#"show settings ilike 'enable%'"#,
r#"PRESIGN INVALID @my_stage/path/to/file"#,
r#"show columns from db1.t from ctl.db"#,
r#"SELECT c a as FROM t"#,
r#"SELECT c a as b FROM t"#,
r#"SELECT top -1 c a as b FROM t"#,
Expand Down
10 changes: 10 additions & 0 deletions src/query/ast/tests/it/testdata/stmt-error.txt
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,16 @@ error:
| while parsing `PRESIGN [{DOWNLOAD | UPLOAD}] <location> [EXPIRE = 3600]`


---------- Input ----------
show columns from db1.t from ctl.db
---------- Output ---------
error:
--> SQL:1:25
|
1 | show columns from db1.t from ctl.db
| ^^^^ unexpected `from`, expecting `FORMAT`, `.`, `LIKE`, `WHERE`, or `;`


---------- Input ----------
SELECT c a as FROM t
---------- Output ---------
Expand Down
77 changes: 77 additions & 0 deletions src/query/ast/tests/it/testdata/stmt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,47 @@ ShowColumns(
)


---------- Input ----------
show columns from ctl.db.t
---------- Output ---------
SHOW COLUMNS FROM t FROM ctl.db
---------- AST ------------
ShowColumns(
ShowColumnsStmt {
catalog: Some(
Identifier {
span: Some(
18..21,
),
name: "ctl",
quote: None,
ident_type: None,
},
),
database: Some(
Identifier {
span: Some(
22..24,
),
name: "db",
quote: None,
ident_type: None,
},
),
table: Identifier {
span: Some(
25..26,
),
name: "t",
quote: None,
ident_type: None,
},
full: false,
limit: None,
},
)


---------- Input ----------
show full columns from t from db like 'id%'
---------- Output ---------
Expand Down Expand Up @@ -526,6 +567,42 @@ ShowColumns(
)


---------- Input ----------
show full columns from db.t like 'id%'
---------- Output ---------
SHOW FULL COLUMNS FROM t FROM db LIKE 'id%'
---------- AST ------------
ShowColumns(
ShowColumnsStmt {
catalog: None,
database: Some(
Identifier {
span: Some(
23..25,
),
name: "db",
quote: None,
ident_type: None,
},
),
table: Identifier {
span: Some(
26..27,
),
name: "t",
quote: None,
ident_type: None,
},
full: true,
limit: Some(
Like {
pattern: "id%",
},
),
},
)


---------- Input ----------
show processlist like 't%' limit 2;
---------- Output ---------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ statement ok
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;

query TTTTTT
SHOW COLUMNS FROM t3 FROM showcolumn
SHOW COLUMNS FROM default.showcolumn.t3
----
c1 INT YES 4 NULL NULL
c2 TIMESTAMP NO '2022-02-02 12:00:00.000000' NULL NULL
Expand Down
Loading