[opt](meta) Improve the performance of getting expr name (#26341)

CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, name)
It's time-consuming when call many times. So lazy call when necessary
This commit is contained in:
seawinde
2023-11-08 17:14:15 +08:00
committed by GitHub
parent 58bf79f79e
commit 96d2e3394a
15 changed files with 78 additions and 23 deletions

View File

@ -44,6 +44,7 @@ import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
/**
* Representation of an analytic function call with OVER clause.
@ -147,8 +148,11 @@ public class AnalyticExpr extends Expr {
}
@Override
protected String getExprName() {
return Utils.normalizeName(getFnCall().getExprName(), DEFAULT_EXPR_NAME);
public String getExprName() {
if (!this.exprName.isPresent()) {
this.exprName = Optional.of(Utils.normalizeName(getFnCall().getExprName(), DEFAULT_EXPR_NAME));
}
return this.exprName.get();
}
@Override

View File

@ -26,6 +26,8 @@ import org.apache.doris.thrift.TExprNodeType;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.Optional;
public class ColumnRefExpr extends Expr {
private static final Logger LOG = LogManager.getLogger(ColumnRefExpr.class);
private String columnName;
@ -55,8 +57,11 @@ public class ColumnRefExpr extends Expr {
}
@Override
protected String getExprName() {
return Utils.normalizeName(getName(), DEFAULT_EXPR_NAME);
public String getExprName() {
if (!this.exprName.isPresent()) {
this.exprName = Optional.of(Utils.normalizeName(getName(), DEFAULT_EXPR_NAME));
}
return this.exprName.get();
}
public void setName(String name) {

View File

@ -69,6 +69,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
/**
@ -296,7 +297,7 @@ public abstract class Expr extends TreeNode<Expr> implements ParseNode, Cloneabl
// Flag to indicate whether to wrap this expr's toSql() in parenthesis. Set by parser.
// Needed for properly capturing expr precedences in the SQL string.
protected boolean printSqlInParens = false;
protected final String exprName = Utils.normalizeName(this.getClass().getSimpleName(), DEFAULT_EXPR_NAME);
protected Optional<String> exprName = Optional.empty();
protected Expr() {
super();
@ -340,8 +341,11 @@ public abstract class Expr extends TreeNode<Expr> implements ParseNode, Cloneabl
// Name of expr, this is used by generating column name automatically when there is no
// alias or is not slotRef
protected String getExprName() {
return this.exprName;
public String getExprName() {
if (!this.exprName.isPresent()) {
this.exprName = Optional.of(Utils.normalizeName(this.getClass().getSimpleName(), DEFAULT_EXPR_NAME));
}
return this.exprName.get();
}
public Type getType() {

View File

@ -313,8 +313,11 @@ public class FunctionCallExpr extends Expr {
}
@Override
protected String getExprName() {
return Utils.normalizeName(this.getFnName().getFunction(), DEFAULT_EXPR_NAME);
public String getExprName() {
if (!this.exprName.isPresent()) {
this.exprName = Optional.of(Utils.normalizeName(this.getFnName().getFunction(), DEFAULT_EXPR_NAME));
}
return this.exprName.get();
}
public FunctionCallExpr(String functionName, List<Expr> params) {

View File

@ -36,6 +36,7 @@ import java.io.DataOutput;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Optional;
public abstract class LiteralExpr extends Expr implements Comparable<LiteralExpr> {
private static final Logger LOG = LogManager.getLogger(LiteralExpr.class);
@ -365,8 +366,11 @@ public abstract class LiteralExpr extends Expr implements Comparable<LiteralExpr
}
@Override
protected String getExprName() {
return "literal";
public String getExprName() {
if (!this.exprName.isPresent()) {
this.exprName = Optional.of("literal");
}
return this.exprName.get();
}
// Port from mysql get_param_length

View File

@ -45,6 +45,7 @@ import java.io.DataOutput;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
@ -307,8 +308,11 @@ public class SlotRef extends Expr {
}
@Override
protected String getExprName() {
return toColumnLabel();
public String getExprName() {
if (!this.exprName.isPresent()) {
this.exprName = Optional.of(toColumnLabel());
}
return this.exprName.get();
}
@Override

View File

@ -123,7 +123,7 @@ public class VirtualSlotRef extends SlotRef {
}
@Override
protected String getExprName() {
public String getExprName() {
return super.getExprName();
}
}

View File

@ -28,6 +28,7 @@ import com.google.common.base.Joiner;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
/**
@ -63,7 +64,10 @@ public class UnboundFunction extends Function implements Unbound, PropagateNulla
@Override
public String getExpressionName() {
return Utils.normalizeName(getName(), DEFAULT_EXPRESSION_NAME);
if (!this.exprName.isPresent()) {
this.exprName = Optional.of(Utils.normalizeName(getName(), DEFAULT_EXPRESSION_NAME));
}
return this.exprName.get();
}
public String getDbName() {

View File

@ -30,6 +30,7 @@ import com.google.common.base.Preconditions;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
/**
* AggregateExpression.
@ -120,7 +121,10 @@ public class AggregateExpression extends Expression implements UnaryExpression {
@Override
public String getExpressionName() {
return Utils.normalizeName(function.getName(), DEFAULT_EXPRESSION_NAME);
if (!this.exprName.isPresent()) {
this.exprName = Optional.of(Utils.normalizeName(function.getName(), DEFAULT_EXPRESSION_NAME));
}
return this.exprName.get();
}
@Override

View File

@ -27,6 +27,7 @@ import com.google.common.collect.ImmutableList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
/**
* Number of rows returned by inspection in subquery.
@ -89,7 +90,10 @@ public class AssertNumRowsElement extends Expression implements LeafExpression,
@Override
public String getExpressionName() {
return assertion.name().toLowerCase();
if (!this.exprName.isPresent()) {
this.exprName = Optional.of(Utils.normalizeName(assertion.name().toLowerCase(), DEFAULT_EXPRESSION_NAME));
}
return this.exprName.get();
}
@Override

View File

@ -44,6 +44,7 @@ import org.apache.commons.lang3.StringUtils;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
/**
@ -53,7 +54,7 @@ public abstract class Expression extends AbstractTreeNode<Expression> implements
public static final String DEFAULT_EXPRESSION_NAME = "expression";
// Mask this expression is generated by rule, should be removed.
public boolean isGeneratedIsNotNull = false;
protected final String exprName = Utils.normalizeName(this.getClass().getSimpleName(), DEFAULT_EXPRESSION_NAME);
protected Optional<String> exprName = Optional.empty();
private final int depth;
private final int width;
@ -97,7 +98,10 @@ public abstract class Expression extends AbstractTreeNode<Expression> implements
// Name of expr, this is used by generating column name automatically when there is no
// alias
public String getExpressionName() {
return this.exprName;
if (!this.exprName.isPresent()) {
this.exprName = Optional.of(Utils.normalizeName(this.getClass().getSimpleName(), DEFAULT_EXPRESSION_NAME));
}
return this.exprName.get();
}
/**

View File

@ -21,6 +21,7 @@ import org.apache.doris.nereids.exceptions.UnboundException;
import org.apache.doris.nereids.util.Utils;
import java.util.List;
import java.util.Optional;
/**
* Expression in Nereids that having name.
@ -59,6 +60,9 @@ public abstract class NamedExpression extends Expression {
@Override
public String getExpressionName() {
return Utils.normalizeName(getName(), DEFAULT_EXPRESSION_NAME);
if (!this.exprName.isPresent()) {
this.exprName = Optional.of(Utils.normalizeName(getName(), DEFAULT_EXPRESSION_NAME));
}
return this.exprName.get();
}
}

View File

@ -86,7 +86,10 @@ public abstract class SubqueryExpr extends Expression implements LeafExpression
@Override
public String getExpressionName() {
return "subquery";
if (!this.exprName.isPresent()) {
this.exprName = Optional.of("subquery");
}
return this.exprName.get();
}
@Override

View File

@ -27,6 +27,7 @@ import com.google.common.base.Suppliers;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Collectors;
@ -57,7 +58,10 @@ public abstract class BoundFunction extends Function implements ComputeSignature
@Override
public String getExpressionName() {
return Utils.normalizeName(getName(), DEFAULT_EXPRESSION_NAME);
if (!this.exprName.isPresent()) {
this.exprName = Optional.of(Utils.normalizeName(getName(), DEFAULT_EXPRESSION_NAME));
}
return this.exprName.get();
}
public FunctionSignature getSignature() {

View File

@ -41,6 +41,7 @@ import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
/**
* All data type literal expression in Nereids.
@ -133,7 +134,10 @@ public abstract class Literal extends Expression implements LeafExpression, Comp
@Override
public String getExpressionName() {
return "literal";
if (!this.exprName.isPresent()) {
this.exprName = Optional.of("literal");
}
return this.exprName.get();
}
@Override