fix(query): preserve parentheses in UNION queries#19587
Open
sundy-li wants to merge 2 commits intodatabendlabs:mainfrom
Open
fix(query): preserve parentheses in UNION queries#19587sundy-li wants to merge 2 commits intodatabendlabs:mainfrom
sundy-li wants to merge 2 commits intodatabendlabs:mainfrom
Conversation
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.
Contributor
🤖 CI Job Analysis
📊 Summary
❌ NO RETRY NEEDEDAll failures appear to be code/test issues requiring manual fixes. 🔍 Job Details
🤖 AboutAutomated 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.
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.
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 inassert_reparsewhen trying to display the query.Root Cause: In
src/query/ast/src/parser/query.rs, theSetOperationParser::primary()method directly returnedSetOperationElement::Group(expr)asexpr, losing the parentheses information.Solution: Modified
SetOperationParser::primarymethod to properly wrap grouped expressions asSetExpr::Query.Changes:
Changed
SetOperationElement::Group(expr) => exprto:This ensures parentheses are preserved when converting AST back to SQL
fixes: parse assertion failed #19578
Tests
Testing: All UNION expressions with parentheses now parse and serialize correctly:
SELECT 1 UNION (SELECT 1 UNION SELECT 1 UNION SELECT 1)- original issueSELECT 1 UNION (SELECT 2)(SELECT 1) UNION SELECT 2Type of change
This change is