NormalizeMatchInputs.java
// 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.analysis.MatchInputResolver;
import org.apache.doris.nereids.rules.analysis.MatchInputResolver.InputKind;
import org.apache.doris.nereids.rules.expression.AbstractExpressionRewriteRule;
import org.apache.doris.nereids.rules.expression.ExpressionRewrite;
import org.apache.doris.nereids.rules.expression.ExpressionRewriteContext;
import org.apache.doris.nereids.trees.expressions.Cast;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.Match;
import org.apache.doris.nereids.trees.expressions.NamedExpression;
import org.apache.doris.nereids.trees.plans.logical.LogicalOlapScan;
import org.apache.doris.nereids.types.StringType;
import com.google.common.collect.ImmutableList;
import java.util.List;
import java.util.stream.Collectors;
/** Restore scalar coercion after project/branch replacement, before checks and lowering. */
public class NormalizeMatchInputs extends ExpressionRewrite {
private static final AbstractExpressionRewriteRule NORMALIZER = new AbstractExpressionRewriteRule() {
@Override
public Expression visitMatch(Match match, ExpressionRewriteContext context) {
Match rewritten = (Match) super.visitMatch(match, context);
Expression left = rewritten.left();
if (left.getDataType().isVariantType()
&& new MatchInputResolver(context.cascadesContext).resolve(left, context.plan.orElse(null))
== InputKind.SCALAR) {
return rewritten.withChildren(new Cast(left, StringType.INSTANCE), rewritten.right());
}
return rewritten;
}
};
public NormalizeMatchInputs() {
super(NORMALIZER);
}
/** Normalize expressions at physical substitution sites without another plan traversal. */
public static Expression normalize(Expression expression, ExpressionRewriteContext context) {
return NORMALIZER.rewrite(expression, context);
}
@Override
public List<Rule> buildRules() {
// MATCH projections can already reside in scan virtual expressions at a late pass.
Rule scanRule = logicalOlapScan().when(scan -> !scan.getVirtualColumns().isEmpty()).thenApply(ctx -> {
LogicalOlapScan scan = ctx.root;
ExpressionRewriteContext context = new ExpressionRewriteContext(scan, ctx.cascadesContext);
List<NamedExpression> normalized = scan.getVirtualColumns().stream()
.map(expression -> (NamedExpression) rewrite(expression, context)).collect(Collectors.toList());
return normalized.equals(scan.getVirtualColumns()) ? null : scan.withVirtualColumns(normalized);
}).toRule(RuleType.NORMALIZE_MATCH_INPUTS);
return ImmutableList.<Rule>builder().addAll(super.buildRules()).add(scanRule).build();
}
}