Skip to content

fix: support BigQuery UNNEST WITH OFFSET without AS alias (#6547)#6596

Open
Zjianru wants to merge 1 commit intoalibaba:masterfrom
Zjianru:fix/bigquery-unnest-offset
Open

fix: support BigQuery UNNEST WITH OFFSET without AS alias (#6547)#6596
Zjianru wants to merge 1 commit intoalibaba:masterfrom
Zjianru:fix/bigquery-unnest-offset

Conversation

@Zjianru
Copy link

@Zjianru Zjianru commented Feb 9, 2026

Problem / 问题

BigQuery SQL SELECT * FROM UNNEST([10,20,30]) as numbers WITH OFFSET fails with ParserException because the parser unconditionally calls exprParser.expr() after WITH OFFSET, even when no AS alias is specified.

BigQuery SQL SELECT * FROM UNNEST([10,20,30]) as numbers WITH OFFSET 解析失败,抛出 ParserException。原因是解析器在 WITH OFFSET 后无条件调用 exprParser.expr(),即使没有 AS alias

Root Cause / 根因

In SQLSelectParser.parseUnnestTableSource(), the code was:

lexer.nextIf(Token.AS);  // AS is optional
unnest.setOffset(this.exprParser.expr());  // always called — fails on ";"

When WITH OFFSET has no AS alias, the next token is ; (or EOF), and expr() cannot parse it.

SQLSelectParser.parseUnnestTableSource() 中,AS 是可选的,但 expr() 总是被调用。当 WITH OFFSET 后面没有 AS alias 时,下一个 token 是 ;(或 EOF),expr() 无法解析。

Fix / 修复

  1. SQLSelectParser.java: Make the offset alias truly optional — only parse an alias expression when AS is present or a valid identifier follows.
  2. SQLUnnestTableSource.java: Add boolean withOffset field to distinguish "has WITH OFFSET but no alias" from "no WITH OFFSET at all".
  3. SQLASTOutputVisitor.java: Output WITH OFFSET (without AS) when withOffset is true but no alias expression exists.

Tests / 测试

Added 3 test cases to UnnestTest.java:

  • test_unnest_with_offset_no_alias: WITH OFFSET without any alias (the reported bug)
  • test_unnest_with_offset_as_alias: WITH OFFSET AS off (existing behavior, regression check)
  • test_unnest_with_offset_implicit_alias: WITH OFFSET off (without AS keyword, but with alias)

All 4 tests pass (including the original test_0).

Closes #6547

The parser required an expression after WITH OFFSET even when no AS
alias was specified, causing a ParserException on valid BigQuery SQL
like: SELECT * FROM UNNEST([10,20,30]) as numbers WITH OFFSET;

Changes:
- SQLSelectParser: make the offset alias optional after WITH OFFSET
- SQLUnnestTableSource: add withOffset boolean field to distinguish
  'WITH OFFSET' (no alias) from no WITH OFFSET at all
- SQLASTOutputVisitor: output 'WITH OFFSET' without 'AS' when no
  alias is present
- UnnestTest: add 3 test cases covering no alias, AS alias, and
  implicit alias

Closes alibaba#6547
@CLAassistant
Copy link

CLAassistant commented Feb 9, 2026

CLA assistant check
All committers have signed the CLA.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sql解析报错: Bigquery UNNEST operator with offset without as

2 participants