taozhi8833998 / taozhi8833998/node-sql-parser

Typescript type for Select.distinct does not match runtime value when parsing Snowflake SQL

Open
#2,220 0 comments 0 reactions 1 assignee View on GitHub

@taozhi8833998 is already working on this.

Since Nov 16, 2024.

Dominant language
PEG.js
Stars
1k
Forks
244
PR merge metrics
No merged PRs in 30d

Description

Describe the bug
Typescript type for Select.distinct does not match runtime value when parsing Snowflake SQL.

Database Engine
Snowflake

To Reproduce
node-sql-parser version: 5.3.4
node version: 22.11.0

parser.astify('SELECT * from foo;', {database: 'Snowflake'});

[
  {
    with: null,
    type: 'select',
    options: null,
    distinct: { type: null },
    columns: [ [Object] ],
    into: { position: null },
    from: [ [Object] ],
    where: null,
    groupby: null,
    having: null,
    qualify: null,
    orderby: null,
    top: null,
    limit: { seperator: '', value: [] },
    window: null
  }
]

Expected behavior
Declared type as follows. Note that distinct should be the string DISTINCT or null, but is {type: null} at runtime.

export interface Select {
  with: With[] | null;
  type: "select";
  options: any[] | null;
  distinct: "DISTINCT" | null;
  columns: any[] | Column[];
  from: From[] | TableExpr | null ;
  where: Binary | Function | null;
  groupby: { columns: ColumnRef[] | null, modifiers: ValueExpr<string>[] };
  having: any[] | null;
  orderby: OrderBy[] | null;
  limit: Limit | null;
  window?: WindowExpr;
  qualify?: any[] | null;
  _orderby?: OrderBy[] | null;
  _limit?: Limit | null;
  parentheses_symbol?: boolean;
  _parentheses?: boolean;
  loc?: LocationRange;
  _next?: Select;
  set_op?: string;
}

As a side note, there are some other mismatched/broken types in here. Examples include:

  • options is any-typed. Can we strongly type it?
  • columns is carrying an any[] option - can we eliminate that in favor of strong typing?
  • groupby is declared as being a mandatory object, but we see null at runtime.
  • having is any-typed. Can we strongly type it?
  • qualify is declared as any[] | null | undefined. Why does that field support both null and undefined when the others don't? Can we strongly type the any[] away?
  • limit is returned with a useless object at runtime, but should probably be null?
  • window is declared as window?: WindowExpr, but we see null at runtime. Maybe fix the type to be window: WindowExpr | null to match the other fields?
  • Why have both _limit and limit? Same with orderby and _orderby?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.