hello-stephen commented on code in PR #69304: URL: https://github.com/apache/doris/pull/69304#discussion_r4752428295
########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PushDownAggStateCast.java: ########## @@ -0,0 +1,313 @@ +// 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.LogicalGenerate; +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, LogicalGenerate.class)) Review Comment: [P2] Resolve sink codec casts through subquery APPLY With query version 21 and `old_sink.s` using codec 20 for `AGG_STATE<SUM(BIGINT NULL)>`, this previously valid insert leaves a backward codec cast: ```sql INSERT INTO old_sink SELECT q.k, q.s FROM (SELECT k, sum_combine(v) s FROM src GROUP BY k) q WHERE q.k < (SELECT max(k) FROM dim); ``` Reduced analyzed plan inferred from the source (`src.v` is nullable BIGINT; forwarding projects omitted): ```text Sink(old_sink.s@20) Project(k, Cast(s@21 AS state@20)) Filter(k < max_k) Apply Aggregate(k, sum_combine(v) AS s@21) Scan(src) Aggregate(max(k) AS max_k) Scan(dim) ``` `SubqueryToApply` creates this boundary during analysis. This pattern omits `LogicalApply`, and `retargetSlot` cannot follow its left output to the producer. `MergeProjectable` cannot cross APPLY either. `Rewriter` then runs expression normalization and strict `CheckCast` before subquery unnesting, so it rejects the remaining 21-to-20 cast before APPLY becomes a supported join. This also affects codec-19 sinks. Follow the forwarded producer through APPLY, or resolve it before final cast validation, and add a legacy-sink regression with a scalar-subquery filter. -- 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]
