Skip to content

Commit

Permalink
Handle enum literals in SqlToRowExpressionTranslator
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-ohayon authored and Rongrong Zhong committed Oct 23, 2020
1 parent c32704a commit 9c5f9f9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
import com.facebook.presto.common.type.CharType;
import com.facebook.presto.common.type.DecimalType;
import com.facebook.presto.common.type.Decimals;
import com.facebook.presto.common.type.EnumType;
import com.facebook.presto.common.type.FunctionType;
import com.facebook.presto.common.type.MapType;
import com.facebook.presto.common.type.RowType;
import com.facebook.presto.common.type.SqlDate;
import com.facebook.presto.common.type.StandardTypes;
import com.facebook.presto.common.type.Type;
import com.facebook.presto.common.type.TypeSignature;
import com.facebook.presto.common.type.VarcharEnumType;
import com.facebook.presto.common.type.VarcharType;
import com.facebook.presto.operator.scalar.VarbinaryFunctions;
import com.facebook.presto.spi.function.Signature;
Expand All @@ -37,6 +39,7 @@
import com.facebook.presto.sql.tree.Cast;
import com.facebook.presto.sql.tree.DecimalLiteral;
import com.facebook.presto.sql.tree.DoubleLiteral;
import com.facebook.presto.sql.tree.EnumLiteral;
import com.facebook.presto.sql.tree.Expression;
import com.facebook.presto.sql.tree.FunctionCall;
import com.facebook.presto.sql.tree.GenericLiteral;
Expand Down Expand Up @@ -231,6 +234,10 @@ public Expression toExpression(Object object, Type type)
// This if condition will evaluate to true: object instanceof Slice && !type.equals(VARCHAR)
}

if (type instanceof EnumType) {
return new EnumLiteral(type.getTypeSignature().toString(), object);
}

Signature signature = getMagicLiteralFunctionSignature(type);
if (object instanceof Slice) {
// HACK: we need to serialize VARBINARY in a format that can be embedded in an expression to be
Expand Down Expand Up @@ -312,7 +319,7 @@ private static Type typeForMagicLiteral(Type type)
return DOUBLE;
}
if (!clazz.isPrimitive()) {
if (type instanceof VarcharType) {
if (type instanceof VarcharType || type instanceof VarcharEnumType) {
return type;
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import com.facebook.presto.sql.tree.DecimalLiteral;
import com.facebook.presto.sql.tree.DereferenceExpression;
import com.facebook.presto.sql.tree.DoubleLiteral;
import com.facebook.presto.sql.tree.EnumLiteral;
import com.facebook.presto.sql.tree.Expression;
import com.facebook.presto.sql.tree.FieldReference;
import com.facebook.presto.sql.tree.FunctionCall;
Expand Down Expand Up @@ -287,6 +288,20 @@ protected RowExpression visitBinaryLiteral(BinaryLiteral node, Void context)
return constant(node.getValue(), VARBINARY);
}

@Override
protected RowExpression visitEnumLiteral(EnumLiteral node, Void context)
{
Type type;
try {
type = functionAndTypeManager.getType(parseTypeSignature(node.getType()));
}
catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Unsupported type: " + node.getType());
}

return constant(node.getValue(), type);
}

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

0 comments on commit 9c5f9f9

Please sign in to comment.