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 @@ -28,16 +28,21 @@
import org.apache.doris.analysis.CastExpr;
import org.apache.doris.analysis.CompoundPredicate;
import org.apache.doris.analysis.Expr;
import org.apache.doris.analysis.ExprSubstitutionMap;
import org.apache.doris.analysis.FunctionCallExpr;
import org.apache.doris.analysis.InPredicate;
import org.apache.doris.analysis.InlineViewRef;
import org.apache.doris.analysis.IsNullPredicate;
import org.apache.doris.analysis.LikePredicate;
import org.apache.doris.analysis.LiteralExpr;
import org.apache.doris.analysis.QueryStmt;
import org.apache.doris.analysis.SelectStmt;
import org.apache.doris.analysis.SlotDescriptor;
import org.apache.doris.analysis.SlotRef;
import org.apache.doris.analysis.TableRef;
import org.apache.doris.analysis.TupleDescriptor;
import org.apache.doris.analysis.UnionStmt;
import org.apache.doris.catalog.OlapTable;
import org.apache.doris.optimizer.base.ItemUtils;
import org.apache.doris.optimizer.base.OptColumnRef;
import org.apache.doris.optimizer.base.OptColumnRefFactory;
Expand Down Expand Up @@ -94,10 +99,32 @@ public OptExpression convertSelect(SelectStmt stmt) {
return root;
}

// Create OptLogicalAggregate and its children from AggregateInfo
public OptExpression convertAggregation(OptExpression root, AggregateInfo aggInfo) {
final List<OptExpression> aggregateInputs = Lists.newArrayList();
aggregateInputs.add(root);
return OptExpression.create(null, aggregateInputs);
// 1. create projection from aggregateInfo
List<OptExpression> projectElements = Lists.newArrayList();

List<OptColumnRef> groupByColumns = Lists.newArrayList();
for (int i = 0; i < aggInfo.getOutputTupleDesc().getSlots().size(); ++i) {
SlotDescriptor slotDesc = aggInfo.getOutputTupleDesc().getSlots().get(i);
OptColumnRef col = columnRefFactory.create(slotDesc.getType());
Expr expr = null;
if (i < aggInfo.getGroupingExprs().size()) {
expr = aggInfo.getGroupingExprs().get(i);
groupByColumns.add(col);
} else {
expr = aggInfo.getAggregateExprs().get(i - aggInfo.getGroupingExprs().size());
}
OptExpression projItem = convertExpr(expr);
OptExpression projElement = OptExpression.create(new OptItemProjectElement(col), projItem);

projectElements.add(projElement);
}
OptExpression projection = OptExpression.create(new OptItemProjectList(), projectElements);


return OptExpression.create(
new OptLogicalAggregate(groupByColumns, OptLogicalAggregate.AggType.GB_GLOBAL), root, projection);
}

public OptExpression convertJoin(OptExpression outerExpression, OptExpression innerExpression, TableRef tableRef) {
Expand All @@ -120,14 +147,48 @@ public OptExpression convertTableRef(TableRef ref) {
if (ref instanceof BaseTableRef) {
return convertBaseTableRef((BaseTableRef) ref);
} else {
// inline view
return convertInlineView((InlineViewRef) ref);
}
return null;
}

public OptExpression convertBaseTableRef(BaseTableRef ref) {
OptLogicalScan scan = new OptLogicalScan(ref);
return OptExpression.create(scan);
List<OptColumnRef> columnRefs = convertTuple(ref.getDesc());
OptLogical op = null;
switch (ref.getTable().getType()) {
case OLAP:
op = new OptLogicalScan((OlapTable) ref.getTable(), columnRefs);
break;
}
return OptExpression.create(op);
}

private OptExpression convertInlineView(InlineViewRef ref) {
OptExpression childExpr = convertQuery(ref.getViewStmt());
// New a projection node

List<Expr> resultExprs = ref.getViewStmt().getResultExprs();

List<OptColumnRef> columnRefs = convertTuple(ref.getDesc());

List<OptExpression> projElements = Lists.newArrayList();
for (int i = 0 ; i < columnRefs.size(); ++i) {
OptExpression projItem = convertExpr(resultExprs.get(i));
projElements.add(OptExpression.create(new OptItemProjectElement(columnRefs.get(i)), projItem));
}
OptExpression projList = OptExpression.create(new OptItemProjectList(), projElements);

return OptExpression.create(new OptLogicalProject(), childExpr, projList);
}

private List<OptColumnRef> convertTuple(TupleDescriptor desc) {
List<OptColumnRef> refs = Lists.newArrayList();
for (SlotDescriptor slot : desc.getSlots()) {
OptColumnRef ref = columnRefFactory.create(slot.getLabel(), slot.getType());

slotIdToColumnRef.put(slot.getId().asInt(), ref);
refs.add(ref);
}
return refs;
}

public OptExpression convertExpr(Expr expr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ public class OptColumnRef {
private final String name;
private final int hashCode;

// temporary exists
public OptColumnRef() {
this.id = -1;
this.type = null;
this.name = "";
this.hashCode = generateHashCode();
}

public OptColumnRef(int id, Type type, String name) {
this.id = id;
this.type = type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,17 @@ public class OptColumnRefFactory {
private int nextId = 1;
private Map<Integer, OptColumnRef> columnRefMap = Maps.newHashMap();

public OptColumnRef create(String name, Type type) {
public OptColumnRef create(Type type) {
int id = nextId++;
String name = "ColRef_" + id;
return create(id, name, type);
}

public OptColumnRef create(String name, Type type) {
return create(nextId++, name, type);
}

private OptColumnRef create(int id, String name, Type type) {
OptColumnRef columnRef = new OptColumnRef(id, type, name);
columnRefMap.put(id, columnRef);
return columnRef;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public class OptItemProjectElement extends OptItem {
private OptColumnRef ref;

protected OptItemProjectElement(OptColumnRef ref) {
public OptItemProjectElement(OptColumnRef ref) {
super(OptOperatorType.OP_ITEM_PROJECT_ELEMENT);
this.ref = ref;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

public class OptItemProjectList extends OptItem {

protected OptItemProjectList() {
public OptItemProjectList() {
super(OptOperatorType.OP_ITEM_PROJECT_LIST);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

public class OptLogicalProject extends OptLogical {

protected OptLogicalProject() {
public OptLogicalProject() {
super(OptOperatorType.OP_LOGICAL_PROJECT);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.doris.analysis.BaseTableRef;
import org.apache.doris.analysis.SlotDescriptor;
import org.apache.doris.catalog.OlapTable;
import org.apache.doris.catalog.Table;
import org.apache.doris.optimizer.base.OptColumnRef;
import org.apache.doris.optimizer.base.OptColumnRefSet;
import org.apache.doris.optimizer.base.RequiredLogicalProperty;
Expand All @@ -33,32 +31,20 @@

import java.util.BitSet;
import java.util.List;
import java.util.Map;

public class OptLogicalScan extends OptLogical {

private List<OptColumnRef> outputs;
private Map<Integer, SlotDescriptor> idSlotMap;
private BaseTableRef table;
private OlapTable table;

public OptLogicalScan() {
this(null);
this(null, null);
}

public OptLogicalScan(BaseTableRef table) {
public OptLogicalScan(OlapTable table, List<OptColumnRef> outputs) {
super(OptOperatorType.OP_LOGICAL_SCAN);
this.outputs = Lists.newArrayList();
this.outputs = outputs;
this.table = table;
this.idSlotMap = Maps.newHashMap();
createOutputColumns();
}

private void createOutputColumns() {
for (SlotDescriptor slot : table.getDesc().getMaterializedSlots()) {
final OptColumnRef columnRef = new OptColumnRef(slot.getId().asInt(), slot.getType(), slot.getColumn().getName());
outputs.add(columnRef);
idSlotMap.put(slot.getId().asInt(), slot);
}
}

@Override
Expand Down