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 @@ -17,6 +17,7 @@

package org.apache.doris.nereids.trees.expressions;

import org.apache.doris.common.Config;
import org.apache.doris.nereids.analyzer.Unbound;
import org.apache.doris.nereids.exceptions.AnalysisException;
import org.apache.doris.nereids.trees.AbstractTreeNode;
Expand All @@ -39,6 +40,7 @@
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand All @@ -52,12 +54,40 @@ public abstract class Expression extends AbstractTreeNode<Expression> implements
// Mask this expression is generated by rule, should be removed.
public boolean isGeneratedIsNotNull = false;

private final int depth;
private final int width;

protected Expression(Expression... children) {
super(children);
depth = Arrays.stream(children)
.mapToInt(e -> e.depth)
.max().orElse(0) + 1;
width = Arrays.stream(children)
.mapToInt(e -> e.width)
.sum() + (children.length == 0 ? 1 : 0);
checkLimit();
}

protected Expression(List<Expression> children) {
super(Optional.empty(), children);
depth = children.stream()
.mapToInt(e -> e.depth)
.max().orElse(0) + 1;
width = children.stream()
.mapToInt(e -> e.width)
.sum() + (children.isEmpty() ? 1 : 0);
checkLimit();
}

private void checkLimit() {
if (depth > Config.expr_depth_limit) {
throw new AnalysisException(String.format("Exceeded the maximum depth of an "
+ "expression tree (%s).", Config.expr_depth_limit));
}
if (width > Config.expr_children_limit) {
throw new AnalysisException(String.format("Exceeded the maximum children of an "
+ "expression tree (%s).", Config.expr_children_limit));
}
}

public Alias alias(String alias) {
Expand Down Expand Up @@ -125,6 +155,14 @@ public Expression child(int index) {
return children.get(index);
}

public int getWidth() {
return width;
}

public int getDepth() {
return depth;
}

@Override
public Expression withChildren(List<Expression> children) {
throw new RuntimeException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,4 +346,15 @@ public void testParseCast() {
Assertions.assertEquals(6, decimalV2Type.getScale());
}
}

@Test
void testParseExprDepthWidth() {
String sql = "SELECT 1+2 = 3 from t";
NereidsParser nereidsParser = new NereidsParser();
LogicalPlan logicalPlan = (LogicalPlan) nereidsParser.parseSingle(sql).child(0);
System.out.println(logicalPlan);
// alias (1 + 2 = 3)
Assertions.assertEquals(4, logicalPlan.getExpressions().get(0).getDepth());
Assertions.assertEquals(3, logicalPlan.getExpressions().get(0).getWidth());
}
}