hello-stephen commented on code in PR #69304:
URL: https://github.com/apache/doris/pull/69304#discussion_r4735794365


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/types/AggStateType.java:
##########
@@ -128,6 +173,11 @@ public boolean equals(Object o) {
         }
 
         AggStateType rhs = (AggStateType) o;
+        // A sink must not treat version 21 state bytes as legacy state bytes.
+        // Keep the cast so that the STATE producer can use the target codec.
+        if (beExecVersion != rhs.beExecVersion) {

Review Comment:
   [P2] Infer a common codec for compatible stored AGG_STATE inputs
   
   A table created before this change has effective codec version 20 when 
queried at 21, while a newly created table with the same `AGG_STATE<SUM(BIGINT 
NULL)>` signature has version 21. A previously valid read then has this reduced 
plan:
   
   ```text
   Aggregate(sum_merge(s))
     UnionAll
       Scan(old_table.s@20)
       Scan(new_table.s@21)
   ```
   
   Both `LogicalSetOperation.getAssignmentCompatibleType` paths lose their 
equality fast path here, and neither common-type implementation handles unequal 
AGG_STATE types, so binding throws before inserting casts. The new 
`canUpgradeStoredStateFrom` allowance cannot help because no common target was 
selected. This needs only the already-supported forward 20-to-21 conversion. 
Add compatible AGG_STATE common-type selection and cover merging states from 
pre-upgrade and post-upgrade tables.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PushDownAggStateCast.java:
##########
@@ -0,0 +1,188 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.rules.rewrite;
+
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.rules.expression.rules.ConvertAggStateCast;
+import org.apache.doris.nereids.trees.expressions.Alias;
+import org.apache.doris.nereids.trees.expressions.Cast;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import 
org.apache.doris.nereids.trees.expressions.functions.NoneMovableFunction;
+import 
org.apache.doris.nereids.trees.expressions.functions.combinator.CombineCombinator;
+import org.apache.doris.nereids.trees.plans.Plan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+import org.apache.doris.nereids.trees.plans.logical.LogicalFilter;
+import org.apache.doris.nereids.trees.plans.logical.LogicalLimit;
+import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
+import org.apache.doris.nereids.trees.plans.logical.LogicalSort;
+import org.apache.doris.nereids.types.AggStateType;
+
+import com.google.common.collect.ImmutableList;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+/** Move codec-only state casts to STATE or COMBINE before its bytes are 
written. */
+public class PushDownAggStateCast extends OneRewriteRuleFactory {
+    @Override
+    public Rule build() {
+        return logicalProject(subTree(LogicalAggregate.class, 
LogicalFilter.class,

Review Comment:
   [P2] Resolve sink codec casts through join children
   
   With an old version-19 `AGG_STATE<SUM(BIGINT NULL)>` sink and planning at 
21, inserting the result of a grouped subquery joined to a dimension produces 
this reduced plan:
   
   ```text
   Sink(old_sink.s@19)
     Project(k, Cast(s@21 AS state@19))
       Join(a.k = dim.k)
         Aggregate(k, sum_combine(v) AS s@21)
         Scan(dim)
   ```
   
   This pattern excludes `LogicalJoin`, and the traversal also stops there when 
a projection intervenes. `MergeProjectable` cannot cross the join either. The 
first expression-normalization pass consequently leaves `Cast(Slot(s))`, which 
`CheckCast` rejects before later join rewrites. Thus an existing INSERT SELECT 
fails after upgrade even though its COMBINE producer could emit the target 
codec. Resolve producer lineage through the appropriate join child before final 
cast validation and add this joined-subquery sink case.



-- 
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