Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -554,12 +554,8 @@ public void analyze(Analyzer analyzer) throws UserException {
}
resultExprs.add(rewriteQueryExprByMvColumnExpr(item.getExpr(), analyzer));
String columnLabel = null;
Class<? extends StatementBase> statementClazz = analyzer.getRootStatementClazz();
if (statementClazz != null
&& (!QueryStmt.class.isAssignableFrom(statementClazz) || hasOutFileClause())) {
// Infer column name when item is expr
columnLabel = item.toColumnLabel(i);
}
// Infer column name when item is expr, both query and ddl
columnLabel = item.toColumnLabel(i);
if (columnLabel == null) {
// column label without position is applicative for query and do not infer
// column name when item is expr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public String getName() {
}

@Override
protected String getExpressionName() {
public String getExpressionName() {
return Utils.normalizeName(getName(), DEFAULT_EXPRESSION_NAME);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.apache.doris.nereids.trees.expressions.Alias;
import org.apache.doris.nereids.trees.expressions.BoundStar;
import org.apache.doris.nereids.trees.expressions.EqualTo;
import org.apache.doris.nereids.trees.expressions.ExprId;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.NamedExpression;
import org.apache.doris.nereids.trees.expressions.Properties;
Expand Down Expand Up @@ -72,12 +73,14 @@
import org.apache.doris.nereids.trees.plans.logical.LogicalSubQueryAlias;
import org.apache.doris.nereids.trees.plans.logical.LogicalTVFRelation;
import org.apache.doris.nereids.trees.plans.logical.UsingJoin;
import org.apache.doris.nereids.trees.plans.visitor.InferPlanOutputAlias;
import org.apache.doris.nereids.util.TypeCoercionUtils;
import org.apache.doris.qe.ConnectContext;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -564,10 +567,16 @@ protected boolean condition(Rule rule, Plan plan) {
),
RuleType.BINDING_RESULT_SINK.build(
unboundResultSink().then(sink -> {
List<NamedExpression> outputExprs = sink.child().getOutput().stream()
.map(NamedExpression.class::cast)
.collect(ImmutableList.toImmutableList());
return new LogicalResultSink<>(outputExprs, sink.child());

final ImmutableListMultimap.Builder<ExprId, Integer> exprIdToIndexMapBuilder =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not see how to process ctas

@seawinde seawinde Nov 5, 2023

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ctas stmt is supported in nereids now and it's also call the method org.apache.doris.nereids.NereidsPlanner#plan, this pr is also useful. the old and new optimizer should keep consistency when query and ddl

ImmutableListMultimap.builder();
List<Slot> childOutput = sink.child().getOutput();
for (int index = 0; index < childOutput.size(); index++) {
exprIdToIndexMapBuilder.put(childOutput.get(index).getExprId(), index);
}
InferPlanOutputAlias aliasInfer = new InferPlanOutputAlias(childOutput);
sink.child().accept(aliasInfer, exprIdToIndexMapBuilder.build());
return new LogicalResultSink<>(aliasInfer.getOutputs(), sink.child());
})
)
).stream().map(ruleCondition).collect(ImmutableList.toImmutableList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public String toString() {
}

@Override
protected String getExpressionName() {
public String getExpressionName() {
return Utils.normalizeName(function.getName(), DEFAULT_EXPRESSION_NAME);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,8 @@ public Alias withChildren(List<Expression> children) {
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
return visitor.visitAlias(this, context);
}

public boolean isNameFromChild() {
return nameFromChild;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public String toSql() {
}

@Override
protected String getExpressionName() {
public String getExpressionName() {
return assertion.name().toLowerCase();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public Alias alias(String alias) {

// Name of expr, this is used by generating column name automatically when there is no
// alias
protected String getExpressionName() {
public String getExpressionName() {
return this.exprName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public String getQualifiedName() throws UnboundException {
}

@Override
protected String getExpressionName() {
public String getExpressionName() {
return Utils.normalizeName(getName(), DEFAULT_EXPRESSION_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public String toSql() {
}

@Override
protected String getExpressionName() {
public String getExpressionName() {
return "subquery";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public String getName() {
}

@Override
protected String getExpressionName() {
public String getExpressionName() {
return Utils.normalizeName(getName(), DEFAULT_EXPRESSION_NAME);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public String toSql() {
}

@Override
protected String getExpressionName() {
public String getExpressionName() {
return "literal";
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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.trees.plans.visitor;

import org.apache.doris.nereids.trees.expressions.Alias;
import org.apache.doris.nereids.trees.expressions.ExprId;
import org.apache.doris.nereids.trees.expressions.NamedExpression;
import org.apache.doris.nereids.trees.expressions.Slot;
import org.apache.doris.nereids.trees.plans.Plan;

import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* Infer output column name when it refers an expression and not has an alias manually.
*/
public class InferPlanOutputAlias extends DefaultPlanVisitor<Void, ImmutableMultimap<ExprId, Integer>> {

private final List<Slot> currentOutputs;
private final List<NamedExpression> finalOutputs;

public InferPlanOutputAlias(List<Slot> currentOutputs) {
this.currentOutputs = currentOutputs;
this.finalOutputs = new ArrayList<>(currentOutputs);
}

@Override
public Void visit(Plan plan, ImmutableMultimap<ExprId, Integer> currentExprIdAndIndexMap) {

List<Alias> aliasProjects = plan.getExpressions().stream()
.filter(expression -> expression instanceof Alias)
.map(Alias.class::cast)
.collect(Collectors.toList());

ImmutableSet<ExprId> currentOutputExprIdSet = currentExprIdAndIndexMap.keySet();
for (Alias projectItem : aliasProjects) {
ExprId exprId = projectItem.getExprId();
// Infer name when alias child is expression and alias's name is from child
if (currentOutputExprIdSet.contains(projectItem.getExprId())
&& projectItem.isNameFromChild()) {
String inferredAliasName = projectItem.child().getExpressionName();
ImmutableCollection<Integer> outPutExprIndexes = currentExprIdAndIndexMap.get(exprId);
// replace output name by inferred name
outPutExprIndexes.forEach(index -> {
Slot slot = currentOutputs.get(index);
finalOutputs.set(index, slot.withName("__" + inferredAliasName + "_" + index));
});
}
}
return super.visit(plan, currentExprIdAndIndexMap);
}

public List<NamedExpression> getOutputs() {
return finalOutputs;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1044,10 +1044,10 @@ public void testSubSelect() throws Exception {
cache.rewriteSelectStmt(null);
LOG.warn("Sub nokey={}", cache.getNokeyStmt().toSql());
Assert.assertEquals(cache.getNokeyStmt().toSql(),
"SELECT <slot 7> `eventdate` AS `eventdate`, <slot 8> sum(`pv`) AS `sum(``pv``)` FROM ("
+ "SELECT <slot 3> `eventdate` AS `eventdate`, <slot 4> count(`userid`) AS `pv` FROM "
+ "`testCluster:testDb`.`appevent` WHERE `eventid` = 1"
+ " GROUP BY `eventdate`) tbl GROUP BY `eventdate`");
"SELECT <slot 7> `eventdate` AS `eventdate`, <slot 8> sum(`pv`) AS `__sum_1` "
+ "FROM (SELECT <slot 3> `eventdate` AS `eventdate`, <slot 4> count(`userid`) AS `pv` "
+ "FROM `testCluster:testDb`.`appevent` WHERE `eventid` = 1 "
+ "GROUP BY `eventdate`) tbl GROUP BY `eventdate`");

PartitionRange range = cache.getPartitionRange();
boolean flag = range.analytics();
Expand All @@ -1066,11 +1066,11 @@ public void testSubSelect() throws Exception {
sql = ca.getRewriteStmt().toSql();
LOG.warn("Sub rewrite={}", sql);
Assert.assertEquals(sql,
"SELECT <slot 7> `eventdate` AS `eventdate`, <slot 8> sum(`pv`) AS `sum(``pv``)` FROM ("
+ "SELECT <slot 3> `eventdate` AS `eventdate`, <slot 4> count(`userid`) AS `pv` FROM "
+ "`testCluster:testDb`.`appevent` WHERE "
+ "`eventdate` > '2020-01-13' AND `eventdate` < '2020-01-16' AND `eventid` = 1 GROUP BY "
+ "`eventdate`) tbl GROUP BY `eventdate`");
"SELECT <slot 7> `eventdate` AS `eventdate`, <slot 8> sum(`pv`) AS `__sum_1` "
+ "FROM (SELECT <slot 3> `eventdate` AS `eventdate`, <slot 4> count(`userid`) AS `pv` "
+ "FROM `testCluster:testDb`.`appevent` WHERE `eventdate` > '2020-01-13' "
+ "AND `eventdate` < '2020-01-16' AND `eventid` = 1 "
+ "GROUP BY `eventdate`) tbl GROUP BY `eventdate`");
} catch (Exception e) {
LOG.warn("sub ex={}", e);
Assert.fail(e.getMessage());
Expand Down Expand Up @@ -1122,8 +1122,8 @@ public void testSqlCacheKey() {

SqlCache sqlCache = (SqlCache) ca.getCache();
String cacheKey = sqlCache.getSqlWithViewStmt();
Assert.assertEquals(cacheKey, "SELECT <slot 2> `eventdate` AS `eventdate`, <slot 3> count(`userid`) AS "
+ "`count(``userid``)` FROM `testCluster:testDb`.`appevent` WHERE `eventdate` >= '2020-01-12' AND "
Assert.assertEquals(cacheKey, "SELECT <slot 2> `eventdate` AS `eventdate`, <slot 3> count(`userid`) "
+ "AS `__count_1` FROM `testCluster:testDb`.`appevent` WHERE `eventdate` >= '2020-01-12' AND "
+ "`eventdate` <= '2020-01-14' GROUP BY `eventdate`|");
Assert.assertEquals(selectedPartitionIds.size(), sqlCache.getSumOfPartitionNum());
}
Expand Down
Loading