diff --git a/pegjs/trino.pegjs b/pegjs/trino.pegjs index b01d7d22..40eadaf9 100644 --- a/pegjs/trino.pegjs +++ b/pegjs/trino.pegjs @@ -21,6 +21,7 @@ 'DELETE': true, 'DESC': true, + 'DESCRIBE': true, 'DISTINCT': true, 'DROP': true, @@ -254,6 +255,7 @@ cmd_stmt / set_stmt / lock_stmt / show_stmt + / desc_stmt / deallocate_stmt create_stmt @@ -1988,6 +1990,25 @@ show_stmt } } +desc_stmt + = KW_DESCRIBE __ t:table_name { + /* + export interface desc_stmt_node { + type: 'describe'; + table: table_name; + } + => AstStatement + */ + return { + tableList: Array.from(tableList), + columnList: columnListTableAlias(columnList), + ast: { + type: 'describe', + table: t + } + }; + } + deallocate_stmt = KW_DEALLOCATE __ p:('PREPARE'i)? __ i:(ident_name / KW_ALL) { return { @@ -4348,6 +4369,7 @@ KW_OFFSET = "OFFSET"i !ident_start { return 'OFFSET' } KW_ASC = "ASC"i !ident_start { return 'ASC'; } KW_DESC = "DESC"i !ident_start { return 'DESC'; } +KW_DESCRIBE = "DESCRIBE"i !ident_start { return 'DESCRIBE'; } KW_ALL = "ALL"i !ident_start { return 'ALL'; } KW_DISTINCT = "DISTINCT"i !ident_start { return 'DISTINCT';} diff --git a/src/command.js b/src/command.js index 36577dca..7bbf33bf 100644 --- a/src/command.js +++ b/src/command.js @@ -46,7 +46,8 @@ function commonCmdToSQL(stmt) { function descToSQL(stmt) { const { type, table } = stmt const action = toUpper(type) - return `${action} ${identifierToSql(table)}` + const tableName = typeof table === 'string' ? identifierToSql(table) : tableToSQL(table) + return `${action} ${tableName}` } function executeToSQL(stmt) { diff --git a/src/sql.js b/src/sql.js index 23b7117c..73bf788e 100644 --- a/src/sql.js +++ b/src/sql.js @@ -1,6 +1,6 @@ import { unionToSQL, multipleToSQL } from './union' -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'] +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'] function checkSupported(expr) { const ast = expr && expr.ast ? expr.ast : expr diff --git a/src/union.js b/src/union.js index 191d5af7..56fefef5 100644 --- a/src/union.js +++ b/src/union.js @@ -57,6 +57,7 @@ const typeToSQLFn = { rename : renameToSQL, call : callToSQL, desc : descToSQL, + describe : descToSQL, set : setVarToSQL, lock : lockUnlockToSQL, unlock : lockUnlockToSQL, diff --git a/test/trino.spec.js b/test/trino.spec.js index 249a39c3..131234c4 100644 --- a/test/trino.spec.js +++ b/test/trino.spec.js @@ -101,13 +101,21 @@ describe('trino', () => { { title: 'ROW expression', sql: [ - 'SELECT ROW(12 AS x, 122 y) FROM t', + "SELECT ROW(12 AS x, 122 y) FROM t", 'SELECT ROW(12 AS "x", 122 AS "y") FROM "t"' - ] - } - ] - SQL_LIST.forEach(sqlInfo => { - const { title, sql } = sqlInfo + ], + }, + { + title: "DESCRIBE statement", + sql: ["DESCRIBE my_table", 'DESCRIBE "my_table"'], + }, + { + title: "DESCRIBE statement with fully qualified name", + sql: ['DESCRIBE my_catalog.my_schema.my_table', 'DESCRIBE "my_catalog"."my_schema"."my_table"'], + }, + ]; + SQL_LIST.forEach((sqlInfo) => { + const { title, sql } = sqlInfo; it(`should support ${title}`, () => { expect(getParsedSql(sql[0], opt)).to.equal(sql[1]) })