morrySnow opened a new pull request, #61813:
URL: https://github.com/apache/doris/pull/61813

   ### What problem does this PR solve?
   
   Issue Number: close #xxx
   
   Problem Summary:
   When parsing a simple CASE expression with a subquery as the value
   (e.g., `CASE (SELECT sum(col) FROM t) WHEN 1 THEN 2 WHEN 3 THEN 4 END`),
   the parser (`LogicalPlanBuilder.visitSimpleCase`) duplicated the same 
subquery
   expression into multiple `EqualTo` nodes across WhenClauses. Since the 
subquery
   carried a fixed `RelationId`, this created duplicate RelationIds in the plan 
tree.
   Later, different SlotIds were assigned to the same `SlotReference` in 
different
   copies, preventing them from being merged back into one subquery. This caused
   the **"groupExpression already exists in memo"** error.
   
   **How to reproduce:**
   ```sql
   CREATE TABLE test (
       big_key int,
       int_1 int
   ) PROPERTIES (
       "replication_allocation" = "tag.location.default: 1"
   );
   
   select case (select sum(int_1) from test)
       when 1 then 2
       when 3 then 4
       else 5
   end a3 from test;
   -- Error: groupExpression already exists in memo, maybe a bug
   ```
   
   **Solution:**
   
   The fix defers `EqualTo` construction from parse time to analysis time:
   
   1. Added a `value` field to `CaseWhen` expression, stored as `children[0]` 
when present. Children layout: `[value?, WhenClause+, defaultValue?]`
   2. `LogicalPlanBuilder.visitSimpleCase` now passes the value to `CaseWhen` 
directly instead of wrapping each WhenClause operand in `EqualTo(value, 
operand)`
   3. `ExpressionAnalyzer.visitCaseWhen` handles simple case: analyzes the 
value once, then constructs `EqualTo(analyzedValue, analyzedOperand)` per 
WhenClause with proper type coercion via 
`TypeCoercionUtils.processComparisonPredicate`, producing a standard 
searched-case `CaseWhen` (no value field). This ensures the subquery is 
analyzed exactly once with a single `RelationId`.
   
   After analysis, all downstream rules see a standard searched-case `CaseWhen` 
— no changes needed elsewhere.
   
   ### Release note
   
   Fixed a bug where using a scalar subquery as the value in a simple CASE 
expression (e.g., `CASE (SELECT ...) WHEN val THEN ...`) caused a 
"groupExpression already exists in memo" planning error.
   
   ### Check List (For Author)
   
   - Test: Unit Test (CaseWhenTest - 7 tests) / Regression Test 
(test_subquery_in_simple_case - 7 cases)
   - Behavior changed: No
   - Does this need documentation: No


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to