taozhi8833998 / node-sql-parser

Parse simple SQL statements into an abstract syntax tree (AST) with the visited tableList and convert it back to SQL
https://taozhi8833998.github.io/node-sql-parser/
Apache License 2.0
823 stars 184 forks source link

Add `_next` property to the Select interface type for UNION queries #2134

Closed aok-solutions closed 2 months ago

aok-solutions commented 2 months ago

Describe the bug When SQL Select statements use a UNION operator, the generated AST has a _next property that contains the ASTs for the other tables being unioned. However this property is not exposed in the Select interface type so the typechecker fails when trying to access those nodes.

Database Engine Snowflake

To Reproduce node: 18.20.1 node-sql-parser: 5.3.2

import { Parser } from "node-sql-parser";

const parser = new Parser();
const opts = { database: "Snowflake" };

const query = `
        SELECT user_id, timestamp
        from login.account.password_reset
        union
        select user_id, timestamp
        from "LOGIN"."ACCOUNT"."PASSWORD_UPDATED"
        union
        select user_id, timestamp
        from LOGIN.ACCOUNT.SUCCESSFUL_LOGIN;
`;

const { ast } = parser.parse(query, opts);
const firstUnionTable = ast[0]._next
error TS2339: Property '_next' does not exist on type 'Select'

Expected behavior should be able to access _next without typescript throwing an error