Skip to content

Commit

Permalink
Handle null Constant value
Browse files Browse the repository at this point in the history
  • Loading branch information
findepi committed Jun 22, 2022
1 parent df33eb5 commit 15839ea
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
import io.trino.sql.tree.StringLiteral;
import io.trino.sql.tree.TimestampLiteral;

import javax.annotation.Nullable;

import java.util.List;

import static com.google.common.base.Preconditions.checkArgument;
Expand Down Expand Up @@ -99,7 +101,7 @@ public List<Expression> toExpressions(Session session, List<?> objects, List<? e
return expressions.build();
}

public Expression toExpression(Session session, Object object, Type type)
public Expression toExpression(Session session, @Nullable Object object, Type type)
{
requireNonNull(type, "type is null");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import io.trino.spi.type.BooleanType;
import io.trino.spi.type.Type;

import javax.annotation.Nullable;

import java.util.List;
import java.util.Objects;

Expand All @@ -33,12 +35,13 @@ public class Constant
/**
* @param value the value encoded using the native "stack" representation for the given type.
*/
public Constant(Object value, Type type)
public Constant(@Nullable Object value, Type type)
{
super(type);
this.value = value;
}

@Nullable
public Object getValue()
{
return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public Pattern<Constant> getPattern()
public Optional<String> rewrite(Constant constant, Captures captures, RewriteContext<String> context)
{
Type type = constant.getType();
if (constant.getValue() == null) {
return Optional.empty();
}
if (type == TINYINT || type == SMALLINT || type == INTEGER || type == BIGINT) {
return Optional.of(Long.toString((long) constant.getValue()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public Pattern<Constant> getPattern()
@Override
public Optional<String> rewrite(Constant constant, Captures captures, RewriteContext<String> context)
{
if (constant.getValue() == null) {
return Optional.empty();
}
Slice slice = (Slice) constant.getValue();
return Optional.of("'" + slice.toStringUtf8().replace("'", "''") + "'");
}
Expand Down

0 comments on commit 15839ea

Please sign in to comment.