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
70 changes: 24 additions & 46 deletions fe/src/main/java/org/apache/doris/optimizer/OptExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
package org.apache.doris.optimizer;

import com.google.common.collect.Lists;
import org.apache.doris.optimizer.base.OptItemProperty;
import org.apache.doris.optimizer.base.OptLogicalProperty;
import org.apache.doris.optimizer.base.OptPhysicalProperty;
import org.apache.doris.optimizer.base.OptProperty;
import org.apache.doris.optimizer.operator.OptOperator;
import org.apache.doris.optimizer.property.OptLogicalProperty;
import org.apache.doris.optimizer.property.OptPhysicalProperty;
import org.apache.doris.optimizer.property.OptProperty;
import org.apache.doris.optimizer.stat.Statistics;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -44,11 +45,10 @@ public class OptExpression {
private OptOperator op;
private List<OptExpression> inputs;

// Store where this Expression has bound from, used to
// store where this Expression has bound from, used to
private MultiExpression mExpr;
// Store the logical property including schema ...
private OptProperty logicalProperty;
private OptProperty physicalProperty;
private OptProperty property;
private Statistics statistics;

private OptExpression(OptOperator op) {
Expand Down Expand Up @@ -78,7 +78,7 @@ private OptExpression(MultiExpression mExpr, List<OptExpression> inputs) {

private void copyPropertyAndStatistics() {
this.op = mExpr.getOp();
this.logicalProperty = mExpr.getGroup().getLogicalProperty();
this.property = mExpr.getGroup().getLogicalProperty();
this.statistics = mExpr.getGroup().getStatistics();
}

Expand All @@ -103,8 +103,10 @@ public static OptExpression createBindingInternalExpression(
public int arity() { return inputs.size(); }
public OptExpression getInput(int idx) { return inputs.get(idx); }
public MultiExpression getMExpr() { return mExpr; }
public OptProperty getLogicalProperty() { return logicalProperty; }
public void setLogicalProperty(OptProperty property) { this.logicalProperty = property; }
public OptProperty getProperty() { return property; }
public OptLogicalProperty getLogicalProperty() { return (OptLogicalProperty) property; }
public OptItemProperty getItemProperty() { return (OptItemProperty) property; }
public OptPhysicalProperty getPhysicalProperty() { return (OptPhysicalProperty) property; }
public Statistics getStatistics() { return statistics; }
public void setStatistics(Statistics statistics) { this.statistics = statistics; }

Expand All @@ -124,43 +126,6 @@ public boolean matchMultiExpression(MultiExpression mExpr) {
return arity() == mExpr.arity();
}

private OptProperty getRightProperty() {
if (getOp().isLogical()) {
return logicalProperty;
} else if (getOp().isPhysical()) {
return physicalProperty;
}
return null;
}

private OptProperty createRightProperty() {
if (getOp().isLogical()) {
logicalProperty = new OptLogicalProperty();
return logicalProperty;
} else if (getOp().isPhysical()) {
physicalProperty = new OptPhysicalProperty();
return physicalProperty;
}
return null;
}

public OptProperty deriveProperty() {
OptProperty property = getRightProperty();
if (property != null) {
return property;
}
// Derive children's property.
property = createRightProperty();
final List<OptProperty> childrenProperty = Lists.newArrayList();
for (OptExpression expr : inputs) {
childrenProperty.add(expr.deriveProperty());
}
// Derive current property.
final OptExpressionWapper wapper = new OptExpressionWapper(this);
property.derive(wapper, childrenProperty);
return property;
}

public void deriveStatistics() {

}
Expand All @@ -181,4 +146,17 @@ public String getExplainString(String headlinePrefix, String detailPrefix) {
}
return sb.toString();
}

// This function will drive inputs' property first, then derive itself's
// property
public void deriveProperty() {
if (property != null) {
return;
}
for (OptExpression input : inputs) {
input.deriveProperty();
}
property = op.createProperty();
property.derive(this);
}
}
13 changes: 5 additions & 8 deletions fe/src/main/java/org/apache/doris/optimizer/OptGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.doris.optimizer.property.OptProperty;
import org.apache.doris.optimizer.base.OptProperty;
import org.apache.doris.optimizer.search.OptimizationContext;
import org.apache.doris.optimizer.stat.Statistics;
import org.apache.logging.log4j.LogManager;
Expand All @@ -39,12 +39,13 @@ public class OptGroup {
private static int nextMExprId = 1;
private GState status;
private Map<OptimizationContext, OptimizationContext> optContextMap;
private OptProperty logicalProperty;
private OptProperty property;
private Statistics statistics;

public OptGroup(int id) {
public OptGroup(int id, OptProperty property) {
this.id = id;
this.status = GState.Unimplemented;
this.property = property;
this.optContextMap = Maps.newHashMap();
}

Expand Down Expand Up @@ -99,18 +100,14 @@ public MultiExpression getFirstMultiExpression() {
return mExprs.get(0);
}

public void deriveProperty() {
}

public int getId() { return id; }
public boolean isImplemented() { return status == GState.Implemented; }
public boolean isOptimized() { return status == GState.Optimized; }
public List<MultiExpression> getMultiExpressions() { return mExprs; }
public OptimizationContext lookUp(OptimizationContext newContext) { return optContextMap.get(newContext); }
public void setStatus(GState status) { this.status = status; }
public GState getStatus() { return status; }
public OptProperty getLogicalProperty() { return logicalProperty; }
public void setLogicalProperty(OptProperty property) { this.logicalProperty = property; }
public OptProperty getProperty() { return property; }
public Statistics getStatistics() { return statistics; }
public void setStatistics(Statistics statistics) { this.statistics = statistics; }

Expand Down
9 changes: 5 additions & 4 deletions fe/src/main/java/org/apache/doris/optimizer/OptMemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,14 @@ public MultiExpression copyIn(OptGroup targetGroup, OptExpression expr, OptRuleT
targetGroup.addMExpr(mExpr);
return mExpr;
}

// targetGroup is null, we should create new OptGroup
OptGroup newGroup = new OptGroup(nextGroupId++);
// we should derive property first to make sure expression has property
expr.deriveProperty();

OptGroup newGroup = new OptGroup(nextGroupId++, expr.getProperty());
newGroup.addMExpr(mExpr);
groups.add(newGroup);
// Derive property
expr.deriveProperty();
newGroup.setLogicalProperty(expr.getLogicalProperty());
return mExpr;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public OptColumnRefSet() {
bitSet = new BitSet(1024);
}

public OptColumnRefSet(int id) {
bitSet = new BitSet(1024);
bitSet.set(id);
}

public OptColumnRefSet(List<OptColumnRef> refs) {
bitSet = new BitSet(1024);
for (OptColumnRef ref : refs) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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.optimizer.base;

import com.google.common.base.Preconditions;
import org.apache.doris.optimizer.OptExpression;
import org.apache.doris.optimizer.operator.OptItem;

public class OptItemProperty implements OptProperty {
private OptColumnRefSet usedColumns;

public OptColumnRefSet getUsedColumns() { return usedColumns; }

@Override
public void derive(OptExpression expression) {
Preconditions.checkArgument(expression.getOp() instanceof OptItem,
"op is not item, op=" + expression.getOp());
OptItem item = (OptItem) expression.getOp();
usedColumns = item.getUsedColumns();
for (OptExpression input : expression.getInputs()) {
if (!input.getOp().isItem()) {
continue;
}
OptItemProperty property = (OptItemProperty) input.getProperty();
// Include input's used columns
usedColumns.include(property.getUsedColumns());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,19 @@
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.optimizer.property;
package org.apache.doris.optimizer.base;

import com.google.common.collect.Lists;
import org.apache.doris.optimizer.OptExpressionWapper;
import org.apache.doris.optimizer.base.OptColumnRef;
import org.apache.doris.optimizer.OptExpression;
import org.apache.doris.optimizer.operator.OptLogical;

import java.util.List;

public class OptLogicalProperty extends OptProperty {

private List<OptColumnRef> outputs;

public OptLogicalProperty() {
this.outputs = Lists.newArrayList();
}

public List<OptColumnRef> getOutputs() { return outputs; }
public void setOutputs(List<OptColumnRef> columns) { outputs.addAll(columns); }
public class OptLogicalProperty implements OptProperty {
private OptColumnRefSet outputColumns;

public OptColumnRefSet getOutputColumns() { return outputColumns; }
@Override
public void derive(OptExpressionWapper wapper, List<OptProperty> childrenProperty) {
// Derive columns
final OptLogical logical = (OptLogical) wapper.getExpression().getOp();
outputs.addAll(logical.deriveOuput(wapper));
public void derive(OptExpression expression) {
OptLogical op = (OptLogical) expression.getOp();

outputColumns = op.getOutputColumns(expression);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,12 @@
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.optimizer.property;
package org.apache.doris.optimizer.base;

import org.apache.doris.optimizer.OptExpressionWapper;

import java.util.List;

public class OptPhysicalProperty extends OptProperty {
import org.apache.doris.optimizer.OptExpression;

public class OptPhysicalProperty implements OptProperty {
@Override
public void derive(OptExpressionWapper wapper, List<OptProperty> childrenProperty) {

public void derive(OptExpression expression) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,10 @@
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.optimizer.property;
package org.apache.doris.optimizer.base;

import org.apache.doris.optimizer.OptExpressionWapper;

import java.util.List;

public abstract class OptProperty {

public abstract void derive(OptExpressionWapper wapper, List<OptProperty> childrenProperty);
import org.apache.doris.optimizer.OptExpression;

public interface OptProperty {
void derive(OptExpression expression);
}
11 changes: 11 additions & 0 deletions fe/src/main/java/org/apache/doris/optimizer/operator/OptItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
package org.apache.doris.optimizer.operator;

import org.apache.doris.catalog.Type;
import org.apache.doris.optimizer.base.OptColumnRefSet;
import org.apache.doris.optimizer.base.OptItemProperty;
import org.apache.doris.optimizer.base.OptProperty;

public abstract class OptItem extends OptOperator {
protected OptItem(OptOperatorType type) {
Expand All @@ -27,10 +30,18 @@ protected OptItem(OptOperatorType type) {
@Override
public boolean isItem() { return true; }

@Override
public OptProperty createProperty() {
return new OptItemProperty();
}

// Scalar return value's type, every scalar has a return value,
// such as compare function would return a boolean type result
public abstract Type getReturnType();

public boolean isConstant() { return false; }
public boolean isAlwaysTrue() { return false; }
public OptColumnRefSet getUsedColumns() {
return new OptColumnRefSet();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.doris.catalog.Type;
import org.apache.doris.optimizer.OptUtils;
import org.apache.doris.optimizer.base.OptColumnRef;
import org.apache.doris.optimizer.base.OptColumnRefSet;

// Reference to a column
public class OptItemColumnRef extends OptItem {
Expand Down Expand Up @@ -55,4 +56,9 @@ public String getExplainString(String prefix) {
sb.append(prefix).append("ItemColumnRef(ref=").append(ref).append(")");
return sb.toString();
}

@Override
public OptColumnRefSet getUsedColumns() {
return new OptColumnRefSet(ref.getId());
}
}
Loading