Skip to content

fix(query): preserve parentheses in UNION queries#19587

Open
sundy-li wants to merge 2 commits intodatabendlabs:mainfrom
sundy-li:fix-issue-19578-union-parentheses
Open

fix(query): preserve parentheses in UNION queries#19587
sundy-li wants to merge 2 commits intodatabendlabs:mainfrom
sundy-li:fix-issue-19578-union-parentheses

Conversation

@sundy-li
Copy link
Member

@sundy-li sundy-li commented Mar 21, 2026

I hereby agree to the terms of the CLA available at: https://docs.databend.com/dev/policies/cla/

Summary

This PR fixes issue #19578 where SQL parser assertions fail when parsing UNION expressions with parentheses.

Problem: When parsing SQL with parentheses around UNION expressions (e.g., SELECT 1 UNION (SELECT 1 UNION SELECT 1 UNION SELECT 1)), the parentheses were lost during AST conversion, causing panics in assert_reparse when trying to display the query.

Root Cause: In src/query/ast/src/parser/query.rs, the SetOperationParser::primary() method directly returned SetOperationElement::Group(expr) as expr, losing the parentheses information.

Solution: Modified SetOperationParser::primary method to properly wrap grouped expressions as SetExpr::Query.

Changes:

  • Changed SetOperationElement::Group(expr) => expr to:

    SetOperationElement::Group(expr) => {
        let mut query = expr.into_query();
        query.span = transform_span(input.span.tokens);
        SetExpr::Query(Box::new(query))
    }
  • This ensures parentheses are preserved when converting AST back to SQL

  • fixes: parse assertion failed #19578

Tests

  • Unit Test
  • Logic Test
  • Benchmark Test
  • No Test - Explain why

Testing: All UNION expressions with parentheses now parse and serialize correctly:

  • SELECT 1 UNION (SELECT 1 UNION SELECT 1 UNION SELECT 1) - original issue
  • SELECT 1 UNION (SELECT 2)
  • (SELECT 1) UNION SELECT 2
  • Other UNION/INTERSECT/EXCEPT combinations with parentheses

Type of change

  • Bug Fix (non-breaking change which fixes an issue)
  • New Feature (non-breaking change which adds functionality)
  • Breaking Change (fix or feature that could cause existing functionality not to work as expected)
  • Documentation Update
  • Refactoring
  • Performance Improvement
  • Other (please describe):

This change is Reviewable

Fix issue databendlabs#19578 where parentheses in UNION queries were lost during display and re-parse. The problem was that SetOperationElement::Group was directly returning the inner expression without wrapping it in SetExpr::Query, causing the parentheses information to be lost.

Changes:
- In SetOperationParser::primary(), convert SetOperationElement::Group to SetExpr::Query instead of directly returning the inner expression.
- This ensures that parentheses are preserved when displaying the AST.
@github-actions github-actions bot added the pr-bugfix this PR patches a bug in codebase label Mar 21, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Mar 21, 2026

🤖 CI Job Analysis

Workflow: 23375557721

📊 Summary

  • Total Jobs: 25
  • Failed Jobs: 2
  • Retryable: 0
  • Code Issues: 2

NO RETRY NEEDED

All failures appear to be code/test issues requiring manual fixes.

🔍 Job Details

  • linux / check: Not retryable (Code/Test)
  • linux / test_unit: Not retryable (Code/Test)

🤖 About

Automated analysis using job annotations to distinguish infrastructure issues (auto-retried) from code/test issues (manual fixes needed).

Add comprehensive tests for issue databendlabs#19578 to ensure parentheses
are preserved when parsing UNION expressions with parentheses.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix this PR patches a bug in codebase

Projects

None yet

Development

Successfully merging this pull request may close these issues.

parse assertion failed

1 participant