Skip to content

Commit

Permalink
cache legacy literal
Browse files Browse the repository at this point in the history
  • Loading branch information
yujun777 committed Jan 3, 2025
1 parent 53c3ea8 commit f357b86
Show file tree
Hide file tree
Showing 26 changed files with 38 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public List<Literal> getValue() {
}

@Override
public LiteralExpr toLegacyLiteral() {
protected LiteralExpr computeLegacyLiteral() {
LiteralExpr[] itemExprs = items.stream()
.map(Literal::toLegacyLiteral)
.toArray(LiteralExpr[]::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
}

@Override
public LiteralExpr toLegacyLiteral() {
protected LiteralExpr computeLegacyLiteral() {
try {
return new IntLiteral(value, Type.BIGINT);
} catch (AnalysisException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public String toString() {
}

@Override
public LiteralExpr toLegacyLiteral() {
protected LiteralExpr computeLegacyLiteral() {
return new BoolLiteral(value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
}

@Override
public LiteralExpr toLegacyLiteral() {
protected LiteralExpr computeLegacyLiteral() {
return new StringLiteral(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ public String toString() {
}

@Override
public LiteralExpr toLegacyLiteral() {
protected LiteralExpr computeLegacyLiteral() {
return new org.apache.doris.analysis.DateLiteral(year, month, day, Type.DATE);
}

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

@Override
public LiteralExpr toLegacyLiteral() {
protected LiteralExpr computeLegacyLiteral() {
return new org.apache.doris.analysis.DateLiteral(year, month, day, hour, minute, second, Type.DATETIME);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
}

@Override
public LiteralExpr toLegacyLiteral() {
protected LiteralExpr computeLegacyLiteral() {
return new org.apache.doris.analysis.DateLiteral(year, month, day, hour, minute, second, microSecond,
getDataType().toCatalogDataType());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,20 @@

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

import org.apache.doris.analysis.LiteralExpr;
import org.apache.doris.catalog.Type;
import org.apache.doris.nereids.exceptions.AnalysisException;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.DateTimeV2Type;
import org.apache.doris.nereids.types.DateV2Type;

import com.google.common.base.Suppliers;

import java.time.LocalDateTime;
import java.util.function.Supplier;

/**
* date v2 literal for nereids
*/
public class DateV2Literal extends DateLiteral {
private final Supplier<org.apache.doris.analysis.DateLiteral> legacyLiteral = Suppliers.memoize(() ->
new org.apache.doris.analysis.DateLiteral(year, month, day, Type.DATEV2)
);

public DateV2Literal(String s) throws AnalysisException {
super(DateV2Type.INSTANCE, s);
Expand All @@ -46,8 +41,8 @@ public DateV2Literal(long year, long month, long day) {
}

@Override
public org.apache.doris.analysis.DateLiteral toLegacyLiteral() {
return legacyLiteral.get();
protected LiteralExpr computeLegacyLiteral() {
return new org.apache.doris.analysis.DateLiteral(year, month, day, Type.DATEV2);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
}

@Override
public LiteralExpr toLegacyLiteral() {
protected LiteralExpr computeLegacyLiteral() {
return new org.apache.doris.analysis.DecimalLiteral(value, dataType.toCatalogDataType());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
}

@Override
public LiteralExpr toLegacyLiteral() {
protected LiteralExpr computeLegacyLiteral() {
return new org.apache.doris.analysis.DecimalLiteral(value, getDataType().toCatalogDataType());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
}

@Override
public LiteralExpr toLegacyLiteral() {
protected LiteralExpr computeLegacyLiteral() {
return new FloatLiteral(value, Type.DOUBLE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
}

@Override
public LiteralExpr toLegacyLiteral() {
protected LiteralExpr computeLegacyLiteral() {
return new org.apache.doris.analysis.FloatLiteral(getDouble(), Type.FLOAT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
}

@Override
public LiteralExpr toLegacyLiteral() {
protected LiteralExpr computeLegacyLiteral() {
return new org.apache.doris.analysis.IPv4Literal(value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
}

@Override
public LiteralExpr toLegacyLiteral() {
protected LiteralExpr computeLegacyLiteral() {
try {
return new org.apache.doris.analysis.IPv6Literal(value);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
}

@Override
public LiteralExpr toLegacyLiteral() {
protected LiteralExpr computeLegacyLiteral() {
try {
return new IntLiteral(value, Type.INT);
} catch (AnalysisException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public String getValue() {
}

@Override
public LiteralExpr toLegacyLiteral() {
protected LiteralExpr computeLegacyLiteral() {
try {
return new org.apache.doris.analysis.JsonLiteral(value);
} catch (Throwable t) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
}

@Override
public LiteralExpr toLegacyLiteral() {
protected LiteralExpr computeLegacyLiteral() {
try {
return new org.apache.doris.analysis.LargeIntLiteral(value.toString());
} catch (AnalysisException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

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

import com.google.common.base.Suppliers;
import org.apache.doris.analysis.BoolLiteral;
import org.apache.doris.analysis.LiteralExpr;
import org.apache.doris.catalog.MysqlColType;
Expand Down Expand Up @@ -49,6 +50,7 @@
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;

/**
* All data type literal expression in Nereids.
Expand All @@ -58,6 +60,10 @@ public abstract class Literal extends Expression implements LeafExpression, Comp

protected final DataType dataType;

// as 'https://github.com/apache/doris/pull/45181' test,
// toLegacyLiteral cost a lot of time when sort literals, so cache the legacy literal.
private final Supplier<LiteralExpr> legacyLiteral = Suppliers.memoize(this::computeLegacyLiteral);

/**
* Constructor for Literal.
*
Expand Down Expand Up @@ -375,7 +381,11 @@ public String toString() {
return String.valueOf(getValue());
}

public abstract LiteralExpr toLegacyLiteral();
public final LiteralExpr toLegacyLiteral() {
return legacyLiteral.get();
}

protected abstract LiteralExpr computeLegacyLiteral();

public boolean isStringLikeLiteral() {
return dataType.isStringLikeType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected Expression uncheckedCastTo(DataType targetType) throws AnalysisExcepti
}

@Override
public LiteralExpr toLegacyLiteral() {
protected LiteralExpr computeLegacyLiteral() {
List<LiteralExpr> keyExprs = keys.stream()
.map(Literal::toLegacyLiteral)
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public Object getValue() {
}

@Override
public LiteralExpr toLegacyLiteral() {
protected LiteralExpr computeLegacyLiteral() {
return org.apache.doris.analysis.MaxLiteral.MAX_VALUE;
}

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

@Override
public LiteralExpr toLegacyLiteral() {
protected LiteralExpr computeLegacyLiteral() {
return org.apache.doris.analysis.NullLiteral.create(dataType.toCatalogDataType());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
}

@Override
public LiteralExpr toLegacyLiteral() {
protected LiteralExpr computeLegacyLiteral() {
try {
return new IntLiteral(value, Type.SMALLINT);
} catch (AnalysisException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
}

@Override
public LiteralExpr toLegacyLiteral() {
protected LiteralExpr computeLegacyLiteral() {
return new org.apache.doris.analysis.StringLiteral(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected Expression uncheckedCastTo(DataType targetType) throws AnalysisExcepti
}

@Override
public LiteralExpr toLegacyLiteral() {
protected LiteralExpr computeLegacyLiteral() {
try {
return new org.apache.doris.analysis.StructLiteral(dataType.toCatalogDataType(),
fields.stream().map(Literal::toLegacyLiteral).toArray(LiteralExpr[]::new)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
}

@Override
public LiteralExpr toLegacyLiteral() {
protected LiteralExpr computeLegacyLiteral() {
try {
return new IntLiteral(value, Type.TINYINT);
} catch (AnalysisException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
}

@Override
public LiteralExpr toLegacyLiteral() {
protected LiteralExpr computeLegacyLiteral() {
return new StringLiteral(value);
}
}

0 comments on commit f357b86

Please sign in to comment.