Skip to content

Commit daeb308

Browse files
Merge pull request taozhi8833998#2602 from natheihei/add-trino-describe
add trino DESCRIBE statement support
2 parents a2865fc + 53abf31 commit daeb308

File tree

5 files changed

+40
-8
lines changed

5 files changed

+40
-8
lines changed

pegjs/trino.pegjs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
'DELETE': true,
2323
'DESC': true,
24+
'DESCRIBE': true,
2425
'DISTINCT': true,
2526
'DROP': true,
2627

@@ -254,6 +255,7 @@ cmd_stmt
254255
/ set_stmt
255256
/ lock_stmt
256257
/ show_stmt
258+
/ desc_stmt
257259
/ deallocate_stmt
258260

259261
create_stmt
@@ -1988,6 +1990,25 @@ show_stmt
19881990
}
19891991
}
19901992

1993+
desc_stmt
1994+
= KW_DESCRIBE __ t:table_name {
1995+
/*
1996+
export interface desc_stmt_node {
1997+
type: 'describe';
1998+
table: table_name;
1999+
}
2000+
=> AstStatement<desc_stmt_node>
2001+
*/
2002+
return {
2003+
tableList: Array.from(tableList),
2004+
columnList: columnListTableAlias(columnList),
2005+
ast: {
2006+
type: 'describe',
2007+
table: t
2008+
}
2009+
};
2010+
}
2011+
19912012
deallocate_stmt
19922013
= KW_DEALLOCATE __ p:('PREPARE'i)? __ i:(ident_name / KW_ALL) {
19932014
return {
@@ -4348,6 +4369,7 @@ KW_OFFSET = "OFFSET"i !ident_start { return 'OFFSET' }
43484369

43494370
KW_ASC = "ASC"i !ident_start { return 'ASC'; }
43504371
KW_DESC = "DESC"i !ident_start { return 'DESC'; }
4372+
KW_DESCRIBE = "DESCRIBE"i !ident_start { return 'DESCRIBE'; }
43514373

43524374
KW_ALL = "ALL"i !ident_start { return 'ALL'; }
43534375
KW_DISTINCT = "DISTINCT"i !ident_start { return 'DISTINCT';}

src/command.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ function commonCmdToSQL(stmt) {
4646
function descToSQL(stmt) {
4747
const { type, table } = stmt
4848
const action = toUpper(type)
49-
return `${action} ${identifierToSql(table)}`
49+
const tableName = typeof table === 'string' ? identifierToSql(table) : tableToSQL(table)
50+
return `${action} ${tableName}`
5051
}
5152

5253
function executeToSQL(stmt) {

src/sql.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { unionToSQL, multipleToSQL } from './union'
22

3-
const supportedTypes = ['analyze', 'attach', 'select', 'deallocate', 'delete', 'exec', 'update', 'insert', 'drop', 'rename', 'truncate', 'call', 'desc', 'use', 'alter', 'set', 'create', 'lock', 'unlock', 'declare', 'show', 'replace', 'if', 'grant', 'revoke', 'proc', 'raise', 'execute', 'transaction', 'explain', 'comment', 'load_data']
3+
const supportedTypes = ['analyze', 'attach', 'select', 'deallocate', 'delete', 'exec', 'update', 'insert', 'drop', 'rename', 'truncate', 'call', 'desc', 'describe', 'use', 'alter', 'set', 'create', 'lock', 'unlock', 'declare', 'show', 'replace', 'if', 'grant', 'revoke', 'proc', 'raise', 'execute', 'transaction', 'explain', 'comment', 'load_data']
44

55
function checkSupported(expr) {
66
const ast = expr && expr.ast ? expr.ast : expr

src/union.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ const typeToSQLFn = {
5757
rename : renameToSQL,
5858
call : callToSQL,
5959
desc : descToSQL,
60+
describe : descToSQL,
6061
set : setVarToSQL,
6162
lock : lockUnlockToSQL,
6263
unlock : lockUnlockToSQL,

test/trino.spec.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,21 @@ describe('trino', () => {
101101
{
102102
title: 'ROW expression',
103103
sql: [
104-
'SELECT ROW(12 AS x, 122 y) FROM t',
104+
"SELECT ROW(12 AS x, 122 y) FROM t",
105105
'SELECT ROW(12 AS "x", 122 AS "y") FROM "t"'
106-
]
107-
}
108-
]
109-
SQL_LIST.forEach(sqlInfo => {
110-
const { title, sql } = sqlInfo
106+
],
107+
},
108+
{
109+
title: "DESCRIBE statement",
110+
sql: ["DESCRIBE my_table", 'DESCRIBE "my_table"'],
111+
},
112+
{
113+
title: "DESCRIBE statement with fully qualified name",
114+
sql: ['DESCRIBE my_catalog.my_schema.my_table', 'DESCRIBE "my_catalog"."my_schema"."my_table"'],
115+
},
116+
];
117+
SQL_LIST.forEach((sqlInfo) => {
118+
const { title, sql } = sqlInfo;
111119
it(`should support ${title}`, () => {
112120
expect(getParsedSql(sql[0], opt)).to.equal(sql[1])
113121
})

0 commit comments

Comments
 (0)