fix: raise clear parse error for invalid derivation expressions#222
Open
nielspardon wants to merge 1 commit into
Open
fix: raise clear parse error for invalid derivation expressions#222nielspardon wants to merge 1 commit into
nielspardon wants to merge 1 commit into
Conversation
## Problem
`evaluate()` builds the ANTLR parser in `_parse()` but installs no
error-raising listener. On invalid syntax, ANTLR's default error recovery
prints a warning to stderr and returns a **partial** parse tree containing
`None` children, which `_evaluate` then walks into — surfacing as the
confusing `Exception: Unknown token type <class 'NoneType'>`.
This is easy to hit with any precision-parameterized type used without its
required argument, e.g. `evaluate("interval_day")`,
`evaluate("precision_timestamp")`, `evaluate("decimal")`. Per the type
grammar all of `decimal`, `interval_day`, `interval_compound`,
`precision_time`, `precision_timestamp`, and `precision_timestamp_tz`
require explicit parameters, so these are genuine syntax errors.
## Fix
Attach a custom ANTLR error listener to both the lexer and parser in
`_parse()` (after removing the default listeners, which also silences the
stray stderr warnings). On any syntax error it raises a dedicated
`DerivationExpressionParseError` naming the offending input, chaining the
underlying ANTLR exception:
```
DerivationExpressionParseError: Could not parse derivation expression
'interval_day' at line 1:12: mismatched input '<EOF>' expecting {'<', '?'}
```
## Fallout
Enabling strict parsing surfaced that `test_nstruct_simple` /
`test_nstruct_nested` were only passing because of the silent recovery this
change removes: they used `nStruct<a i32, b i32>`, but the grammar
(substrait-antlr 0.96.0) requires colons — `nStruct<a: i32, b: i32>`. ANTLR
had been inserting the missing `:` and continuing. Updated those tests to
the valid colon syntax; the asserted results are unchanged.
## Testing
Added a parametrized test covering every precision-parameterized type used
without arguments, plus a general malformed-input case. `pytest` — 519
passed, 30 skipped; `ruff check` and `ruff format --check` clean.
Fixes substrait-io#216.
🤖 Generated with AI
nielspardon
force-pushed
the
fix/derivation-expression-parse-error
branch
from
July 16, 2026 17:44
51ce39a to
1a43573
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
evaluate()builds the ANTLR parser in_parse()but installs no error-raising listener. On invalid syntax, ANTLR's default error recovery prints a warning to stderr and returns a partial parse tree containingNonechildren, which_evaluatethen walks into — surfacing as the confusingException: Unknown token type <class 'NoneType'>.This is easy to hit with any precision-parameterized type used without its required argument, e.g.
evaluate("interval_day"),evaluate("precision_timestamp"),evaluate("decimal"). Per the type grammar all ofdecimal,interval_day,interval_compound,precision_time,precision_timestamp, andprecision_timestamp_tzrequire explicit parameters, so these are genuine syntax errors.Fix
Attach a custom ANTLR error listener to both the lexer and parser in
_parse()(after removing the default listeners, which also silences the stray stderr warnings). On any syntax error it raises a dedicatedDerivationExpressionParseErrornaming the offending input, chaining the underlying ANTLR exception:Fallout
Enabling strict parsing surfaced that
test_nstruct_simple/test_nstruct_nestedwere only passing because of the silent recovery this change removes: they usednStruct<a i32, b i32>, but the grammar (substrait-antlr 0.96.0) requires colons —nStruct<a: i32, b: i32>. ANTLR had been inserting the missing:and continuing. Updated those tests to the valid colon syntax; the asserted results are unchanged.Testing
Added a parametrized test covering every precision-parameterized type used without arguments, plus a general malformed-input case.
pytest— 519 passed, 30 skipped;ruff checkandruff format --checkclean.Fixes #216.
🤖 Generated with AI