Skip to content

Commit

Permalink
Remove Long, Decimal, Boolean and Double literals
Browse files Browse the repository at this point in the history
Replace them with GenericLiteral. The values are stored as
native carrier type instead of String inside the object.
  • Loading branch information
martint committed Mar 15, 2024
1 parent 2008196 commit 82fcef8
Show file tree
Hide file tree
Showing 186 changed files with 2,277 additions and 2,658 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.trino.sql.ir.ComparisonExpression;
import io.trino.sql.ir.Expression;
import io.trino.sql.ir.FunctionCall;
import io.trino.sql.ir.GenericLiteral;
import io.trino.sql.ir.InPredicate;
import io.trino.sql.ir.IrUtils;
import io.trino.sql.ir.IrVisitor;
Expand Down Expand Up @@ -260,16 +261,20 @@ private PlanNodeStatsEstimate estimateLogicalOr(List<Expression> terms)
}

@Override
protected PlanNodeStatsEstimate visitBooleanLiteral(BooleanLiteral node, Void context)
protected PlanNodeStatsEstimate visitGenericLiteral(GenericLiteral node, Void context)
{
if (node.getValue()) {
return input;
if (node.getType().equals(BOOLEAN)) {
if (Boolean.parseBoolean(node.getValue())) {
return input;
}

PlanNodeStatsEstimate.Builder result = PlanNodeStatsEstimate.builder();
result.setOutputRowCount(0.0);
input.getSymbolsWithKnownStatistics().forEach(symbol -> result.addSymbolStatistics(symbol, SymbolStatsEstimate.zero()));
return result.build();
}

PlanNodeStatsEstimate.Builder result = PlanNodeStatsEstimate.builder();
result.setOutputRowCount(0.0);
input.getSymbolsWithKnownStatistics().forEach(symbol -> result.addSymbolStatistics(symbol, SymbolStatsEstimate.zero()));
return result.build();
return super.visitGenericLiteral(node, context);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
import io.trino.spi.type.BooleanType;
import io.trino.spi.type.Type;
import io.trino.spi.type.VarcharType;
import io.trino.sql.ir.BooleanLiteral;
import io.trino.sql.ir.Cast;
import io.trino.sql.ir.ComparisonExpression;
import io.trino.sql.ir.Expression;
import io.trino.sql.ir.FunctionCall;
import io.trino.sql.ir.GenericLiteral;
import io.trino.sql.ir.StringLiteral;
import io.trino.sql.ir.SymbolReference;
import io.trino.sql.planner.BuiltinFunctionCallBuilder;
Expand Down Expand Up @@ -179,8 +179,8 @@ public static Optional<Descriptor> getDescriptor(Expression expression)
String id = ((StringLiteral) idExpression).getValue();

Expression nullAllowedExpression = arguments.get(3);
checkArgument(nullAllowedExpression instanceof BooleanLiteral, "nullAllowedExpression is expected to be an instance of BooleanLiteral: %s", nullAllowedExpression.getClass().getSimpleName());
boolean nullAllowed = ((BooleanLiteral) nullAllowedExpression).getValue();
checkArgument(nullAllowedExpression instanceof GenericLiteral literal && literal.getType().equals(BooleanType.BOOLEAN), "nullAllowedExpression is expected to be a boolean constant: %s", nullAllowedExpression.getClass().getSimpleName());
boolean nullAllowed = Boolean.parseBoolean(((GenericLiteral) nullAllowedExpression).getValue());
return Optional.of(new Descriptor(new DynamicFilterId(id), probeSymbol, operator, nullAllowed));
}

Expand Down
63 changes: 4 additions & 59 deletions core/trino-main/src/main/java/io/trino/sql/ir/BooleanLiteral.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,67 +13,12 @@
*/
package io.trino.sql.ir;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.ImmutableList;

import java.util.List;
import java.util.Objects;
import static io.trino.spi.type.BooleanType.BOOLEAN;

public final class BooleanLiteral
extends Literal
{
public static final BooleanLiteral TRUE_LITERAL = new BooleanLiteral(true);
public static final BooleanLiteral FALSE_LITERAL = new BooleanLiteral(false);

private final boolean value;

@JsonCreator
public BooleanLiteral(boolean value)
{
this.value = value;
}

@JsonProperty
public boolean getValue()
{
return value;
}

@Override
public <R, C> R accept(IrVisitor<R, C> visitor, C context)
{
return visitor.visitBooleanLiteral(this, context);
}

@Override
public List<? extends Expression> getChildren()
{
return ImmutableList.of();
}

@Override
public int hashCode()
{
return Objects.hash(value);
}

@Override
public boolean equals(Object obj)
{
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
BooleanLiteral other = (BooleanLiteral) obj;
return Objects.equals(this.value, other.value);
}
public static final GenericLiteral TRUE_LITERAL = GenericLiteral.constant(BOOLEAN, true);
public static final GenericLiteral FALSE_LITERAL = GenericLiteral.constant(BOOLEAN, false);

@Override
public String toString()
{
return Boolean.toString(value);
}
private BooleanLiteral() {}
}
78 changes: 0 additions & 78 deletions core/trino-main/src/main/java/io/trino/sql/ir/DecimalLiteral.java

This file was deleted.

83 changes: 0 additions & 83 deletions core/trino-main/src/main/java/io/trino/sql/ir/DoubleLiteral.java

This file was deleted.

3 changes: 0 additions & 3 deletions core/trino-main/src/main/java/io/trino/sql/ir/Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
@JsonSubTypes.Type(value = Cast.class, name = "cast"),
@JsonSubTypes.Type(value = CoalesceExpression.class, name = "coalesce"),
@JsonSubTypes.Type(value = ComparisonExpression.class, name = "comparison"),
@JsonSubTypes.Type(value = DecimalLiteral.class, name = "decimal"),
@JsonSubTypes.Type(value = DoubleLiteral.class, name = "double"),
@JsonSubTypes.Type(value = FunctionCall.class, name = "call"),
@JsonSubTypes.Type(value = GenericLiteral.class, name = "constant"),
@JsonSubTypes.Type(value = IfExpression.class, name = "if"),
Expand All @@ -43,7 +41,6 @@
@JsonSubTypes.Type(value = IsNullPredicate.class, name = "isNull"),
@JsonSubTypes.Type(value = LambdaExpression.class, name = "lambda"),
@JsonSubTypes.Type(value = LogicalExpression.class, name = "logicalBinary"),
@JsonSubTypes.Type(value = LongLiteral.class, name = "long"),
@JsonSubTypes.Type(value = NotExpression.class, name = "not"),
@JsonSubTypes.Type(value = NullIfExpression.class, name = "nullif"),
@JsonSubTypes.Type(value = NullLiteral.class, name = "null"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.io.BaseEncoding;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.function.Function;

Expand All @@ -30,9 +27,6 @@

public final class ExpressionFormatter
{
private static final ThreadLocal<DecimalFormat> doubleFormatter = ThreadLocal.withInitial(
() -> new DecimalFormat("0.###################E0###", new DecimalFormatSymbols(Locale.US)));

private ExpressionFormatter() {}

public static String formatExpression(Expression expression)
Expand Down Expand Up @@ -68,14 +62,6 @@ protected String visitExpression(Expression node, Void context)
throw new UnsupportedOperationException("not yet implemented: %s.visit%s".formatted(getClass().getName(), node.getClass().getSimpleName()));
}

@Override
protected String visitBooleanLiteral(BooleanLiteral node, Void context)
{
return literalFormatter
.map(formatter -> formatter.apply(node))
.orElseGet(() -> String.valueOf(node.getValue()));
}

@Override
protected String visitStringLiteral(StringLiteral node, Void context)
{
Expand Down Expand Up @@ -106,31 +92,6 @@ protected String visitSubscriptExpression(SubscriptExpression node, Void context
return formatExpression(node.getBase()) + "[" + formatExpression(node.getIndex()) + "]";
}

@Override
protected String visitLongLiteral(LongLiteral node, Void context)
{
return literalFormatter
.map(formatter -> formatter.apply(node))
.orElseGet(() -> Long.toString(node.getValue()));
}

@Override
protected String visitDoubleLiteral(DoubleLiteral node, Void context)
{
return literalFormatter
.map(formatter -> formatter.apply(node))
.orElseGet(() -> doubleFormatter.get().format(node.getValue()));
}

@Override
protected String visitDecimalLiteral(DecimalLiteral node, Void context)
{
return literalFormatter
.map(formatter -> formatter.apply(node))
// TODO return node value without "DECIMAL '..'" when FeaturesConfig#parseDecimalLiteralsAsDouble switch is removed
.orElseGet(() -> "DECIMAL '" + node.getValue() + "'");
}

@Override
protected String visitGenericLiteral(GenericLiteral node, Void context)
{
Expand Down
Loading

0 comments on commit 82fcef8

Please sign in to comment.