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
820 stars 183 forks source link

TypeScript types for binary expressions preclude nested binary expression on one side #2157

Closed TrevorBurnham closed 1 month ago

TrevorBurnham commented 1 month ago

Describe the bug The type definition for binary expressions requires that a binary expression either:

  1. Have left and right both be binary expressions, or
  2. Have left and right both be other types of expressions.

The typechecker fails for ASTs produced from SQL statements that use a binary expression where either left or right is a binary expression, but not both.

Database Engine PostgreSQL

To Reproduce In node-sql-parser@5.3.2, run:

const parser = new SQLParser();
const ast = parser.astify(`SELECT * FROM "table" WHERE ("year" || "month") = '202301'`);

The value of ast.where is:

{
  "type": "binary_expr",
  "operator": "=",
  "left": {
    "type": "binary_expr",
    "operator": "||",
    "left": { "type": "double_quote_string", "value": "year" },
    "right": { "type": "double_quote_string", "value": "month" },
    "parentheses": true
  },
  "right": { "type": "single_quote_string", "value": "202301" }
}

Yet setting ast.where to this value throws a type error.

Expected behavior

Any value emitted by astify should satisfy the TypeScript types.