Skip to content

bug: SELECT WITH(0) is accepted, but renders as invalid SELECT WITH(, 0) #425

Description

@makenowjust

Found by fuzzing (see #415).

Repro

stmt, err := memefish.ParseQuery("", "SELECT WITH(0)")
// err == nil
sql := stmt.SQL()
// sql == "SELECT WITH(, 0)"
_, err = memefish.ParseQuery("", sql)
// err != nil: syntax error: :1:13: unexpected token: ,

Cause

parseWithExpr (parser.go:2651) accepts zero name AS expr variables — the lookahead loop can run zero times — but WithExpr.SQL() (ast/sql.go:583) renders "WITH(" + sqlJoin(w.Vars, ", ") + ", " + w.Expr.SQL() + ")", producing a leading comma when Vars is empty.

Per the GoogleSQL grammar (googlesql/parser/googlesql.tm), a WITH expression requires at least one variable:

with_expression {ASTExpression*}:
    KW_WITH_STARTING_WITH_EXPRESSION "(" with_expression_variable_prefix "," expression ")"

(with_expression_variable_prefix is one-or-more identifier AS expression.)

So WITH(0) is not valid GoogleSQL, and the right fix is in the parser: parseWithExpr should require at least one variable. The renderer is then always correct as written.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions