Skip to content

fix(sql): between and inside sub-expression#7044

Merged
bluestreak01 merged 1 commit into
questdb:masterfrom
brunocalza:bcalza/fix-between-and-depth
May 2, 2026
Merged

fix(sql): between and inside sub-expression#7044
bluestreak01 merged 1 commit into
questdb:masterfrom
brunocalza:bcalza/fix-between-and-depth

Conversation

@brunocalza
Copy link
Copy Markdown
Contributor

@brunocalza brunocalza commented Apr 29, 2026

Summary

Fixes #6534

The issue here is not the malformed query. The problem is the AND inside a sub-expression. Currently, the parser thinks that the AND inside the sub-expression refers to BETWEEN's AND operator.

There was a special treatment for BETWEEN-CASE, so the problem does not arise in

SELECT x BETWEEN 0 AND CASE WHEN 1 AND 2 THEN 5 ELSE 10 END

but in other cases, such as

SELECT 1 BETWEEN (1 and 2) and 3;
SELECT 1 BETWEEN ARRAY[0 AND 1] AND 2 
SELECT 1 BETWEEN (ARRAY[10, 20, 30])[1 AND 1] AND 3
SELECT 1 BETWEEN CAST(0 AND 1 AS INT) AND 3

the issue arises. These are syntactically correct statements, and they should throw a type mismatch error.

The fix

We keep track of the scope depth when we reach BETWEEN on a variable called betweenStartScopeDepth, and whenever we see an AND we use the current scope depth to know whether the AND refers to BETWEEN or not.

This fix is identical to the treatment given to the BETWEEN-CASE, but it's more generic. We had a betweenStartCaseCount variable that we removed.

How this was tested

This was manually tested, and a unit test was added for each one of the scope cases.

Signed-off-by: Bruno Calza <brunoangelicalza@gmail.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 29, 2026

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: f121fce4-8021-4a6c-b3e4-3160636c1d6c

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Member

@bluestreak01 bluestreak01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix is correct and minimal. Verified against existing BETWEEN tests in SqlParserTest, ExpressionParserTest (295), WhereClauseParserTest (842), and IntervalFilterTest (26) — all pass. The new scopeStack.size() check is a strict superset of the old caseCount check, so CASE behavior is preserved while PAREN/BRACKET/ARRAY/CAST scopes are now handled correctly.

Non-blocking suggestions:

  • PR title is missing the verb per CLAUDE.md (fix(sql): fix BETWEEN ...).
  • Fixes <URL> won't auto-close issue #6534; use Fixes #6534.
  • Add Bug and SQL labels.
  • Consider adding an execution-level test that asserts the resulting type-mismatch error, since the original bug surfaced as a runtime AssertionError.

@bluestreak01 bluestreak01 merged commit 2cd1489 into questdb:master May 2, 2026
27 checks passed
nwoolmer added a commit that referenced this pull request May 6, 2026
Brings in 12 upstream commits (b6b3b15..c0c5638). Notable
upstream changes:

* feat(core): compact posting index for SYMBOL columns (#6861) plus
  follow-ups #7051 (seal indexes after WAL fast-lag commit) and #7052
  (refresh covering sidecar mappings on reader reload). Adds DirectBitSet,
  bp_bitpack / FSST native helpers, and PostingIndexBenchmarkSuite.
* fix(sql): preserve outer-join predicates, fix NOT in UNION, isolate
  parser state (#7027).
* fix(sql): BETWEEN inside sub-expression (#7044).
* fix(sql): JIT filter wrong results with UUID bind variables (#7049).
* fix(core): avoid server-main error on WAL table rename race (#7046).
* chore(ilp): fix QWP string-to-numeric parse status classification (#7038).
* Build/CI chores: aux-job moved to Hetzner (#7054), latest IntelliJ for
  formatting (#7040), 'qwp' allowed as PR-title subtype, level switch
  added to review-pr skill (#7055).

Conflict: core/src/main/resources/io/questdb/bin/linux-x86-64/libquestdb.so
is a prebuilt native library; took master's version (same resolution as
the prior 046c874 merge). Other native libs were updated cleanly
without conflict.

Auto-merged: SqlCompilerImpl.java, SqlCodeGenerator.java, SqlParser.java,
SqlKeywords.java, TableColumnMetadata.java, TableWriter.java,
AbstractCairoTest.java. Errand-specific files (MemFdFilesFacade,
HybridCairoConfiguration, ArrowBulkExport, ArrowBulkIngest) untouched.
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.

assertion error with invalid SQL

2 participants