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


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PushDownAggStateCast.java:
##########
@@ -0,0 +1,304 @@
+// 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.SlotReference;
+import org.apache.doris.nereids.trees.expressions.StatementScopeIdGenerator;
+import 
org.apache.doris.nereids.trees.expressions.functions.NoneMovableFunction;
+import org.apache.doris.nereids.trees.expressions.functions.agg.AnyValue;
+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.algebra.SetOperation.Qualifier;
+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.LogicalJoin;
+import org.apache.doris.nereids.trees.plans.logical.LogicalLimit;
+import org.apache.doris.nereids.trees.plans.logical.LogicalOneRowRelation;
+import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
+import org.apache.doris.nereids.trees.plans.logical.LogicalSort;
+import org.apache.doris.nereids.trees.plans.logical.LogicalSubQueryAlias;
+import org.apache.doris.nereids.trees.plans.logical.LogicalUnion;
+import org.apache.doris.nereids.types.AggStateType;
+
+import com.google.common.base.Preconditions;
+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,
+                LogicalSort.class, LogicalLimit.class, LogicalProject.class, 
LogicalJoin.class,
+                LogicalSubQueryAlias.class, LogicalUnion.class, 
LogicalOneRowRelation.class))

Review Comment:
   [P2] Follow STATE producers through LATERAL VIEW
   
   With a legacy `AGG_STATE<SUM(BIGINT NULL)>` sink using codec 20 and planning 
at 21, this previously valid insert leaves a backward codec cast above the 
generator:
   
   ```sql
   INSERT INTO old_sink
   SELECT q.k, q.s
   FROM (SELECT k, sum_state(v) AS s FROM src) q
   LATERAL VIEW explode_split('a,b', ',') lv AS x;
   ```
   
   Reduced analyzed plan inferred from the source:
   
   ```text
   Sink(old_sink.s@20)
     Project(k, Cast(s@21 AS state@20))
       Generate(explode_split(...))
         Project(k, sum_state(v) AS s@21)
           Scan(src: v BIGINT NULL)
   ```
   
   `LogicalGenerate` is absent from this pattern and from `retargetSlot`'s 
forwarding cases. `MergeProjectable` cannot cross it either, so 
`ConvertAggStateCast` sees only the slot and the first `CheckCast` rejects 
21-to-20. Pre-upgrade catalog types without an explicit codec resolve to 20 on 
this head, so this affects ordinary existing tables; codec-19 sinks fail 
similarly. Follow forwarded child slots through Generate while preserving its 
generated outputs and row multiplicity, and add this legacy-sink regression.



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