Skip to content

Commit

Permalink
add nullType for null literal value
Browse files Browse the repository at this point in the history
  • Loading branch information
mchades committed Jan 8, 2024
1 parent 487e802 commit d3a56fa
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

/** The helper class to create literals to pass into Gravitino. */
public class Literals {
/** Used to represent a null literal. */
public static final Literal<Types.NullType> NULL = new LiteralImpl<>(null, Types.NullType.get());

/**
* Creates a literal with the given value and data type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ enum Name {
STRUCT,
LIST,
MAP,
UNION
UNION,
NULL
}

/** The base type of all primitive types. */
Expand Down
22 changes: 22 additions & 0 deletions api/src/main/java/com/datastrato/gravitino/rel/types/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,28 @@
/** The helper class for {@link Type}. */
public class Types {

/** The data type representing `NULL` values. */
public static class NullType implements Type {
private static final NullType INSTANCE = new NullType();

/** @return The singleton instance of {@link NullType}. */
public static NullType get() {
return INSTANCE;
}

private NullType() {}

@Override
public Name name() {
return Name.NULL;
}

@Override
public String simpleString() {
return "null";
}
}

/** The boolean type in Gravitino. */
public static class BooleanType extends Type.PrimitiveType {
private static final BooleanType INSTANCE = new BooleanType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static com.datastrato.gravitino.rel.expressions.literals.Literals.timestamp;

import com.datastrato.gravitino.rel.expressions.literals.Literal;
import com.datastrato.gravitino.rel.expressions.literals.Literals;
import com.datastrato.gravitino.rel.types.Types;
import java.time.LocalDate;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -71,5 +72,7 @@ public void testLiterals() {
literal = string("hello");
Assertions.assertEquals(literal.value(), "hello");
Assertions.assertEquals(literal.dataType(), Types.StringType.get());

Assertions.assertEquals(Literals.of(null, Types.NullType.get()), Literals.NULL);
}
}
5 changes: 5 additions & 0 deletions api/src/test/java/com/datastrato/gravitino/rel/TestTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public void testPrimitiveTypes() {
Assertions.assertSame(booleanType, Types.BooleanType.get());
Assertions.assertEquals("boolean", booleanType.simpleString());

Types.NullType nullType = Types.NullType.get();
Assertions.assertEquals(Type.Name.NULL, nullType.name());
Assertions.assertSame(nullType, Types.NullType.get());
Assertions.assertEquals("null", nullType.simpleString());

Types.ByteType byteType = Types.ByteType.get();
Assertions.assertEquals(Type.Name.BYTE, byteType.name());
Assertions.assertSame(byteType, Types.ByteType.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ public static FunctionArg toFunctionArg(Expression expression) {

if (expression instanceof Literal) {
return new LiteralDTO.Builder()
.withValue(((Literal<String>) expression).value())
.withDataType(((Literal<String>) expression).dataType())
.withValue(((Literal<?>) expression).value().toString())
.withDataType(((Literal<?>) expression).dataType())
.build();
} else if (expression instanceof NamedReference.FieldReference) {
return new FieldReferenceDTO.Builder()
Expand Down Expand Up @@ -222,14 +222,14 @@ public static FunctionArg[] toFunctionArg(Expression[] expressions) {

public static SortOrderDTO[] toDTOs(SortOrder[] sortOrders) {
if (ArrayUtils.isEmpty(sortOrders)) {
return new SortOrderDTO[0];
return SortOrderDTO.EMPTY_SORT;
}
return Arrays.stream(sortOrders).map(DTOConverters::toDTO).toArray(SortOrderDTO[]::new);
}

public static Partitioning[] toDTOs(Transform[] transforms) {
if (ArrayUtils.isEmpty(transforms)) {
return new Partitioning[0];
return Partitioning.EMPTY_PARTITIONING;
}
return Arrays.stream(transforms).map(DTOConverters::toDTO).toArray(Partitioning[]::new);
}
Expand All @@ -242,10 +242,10 @@ public static Distribution fromDTO(DistributionDTO distributionDTO) {
return Distributions.of(
distributionDTO.strategy(),
distributionDTO.number(),
fromFunctionArg(distributionDTO.args()));
fromFunctionArgs(distributionDTO.args()));
}

public static Expression[] fromFunctionArg(FunctionArg[] args) {
public static Expression[] fromFunctionArgs(FunctionArg[] args) {
if (ArrayUtils.isEmpty(args)) {
return Expression.EMPTY_EXPRESSION;
}
Expand All @@ -255,13 +255,16 @@ public static Expression[] fromFunctionArg(FunctionArg[] args) {
public static Expression fromFunctionArg(FunctionArg arg) {
switch (arg.argType()) {
case LITERAL:
if (((LiteralDTO) arg).value() == null) {
return Literals.NULL;
}
return Literals.of(((LiteralDTO) arg).value(), ((LiteralDTO) arg).dataType());
case FIELD:
return NamedReference.field(((FieldReferenceDTO) arg).fieldName());
case FUNCTION:
return FunctionExpression.of(
((FuncExpressionDTO) arg).functionName(),
fromFunctionArg(((FuncExpressionDTO) arg).args()));
fromFunctionArgs(((FuncExpressionDTO) arg).args()));
default:
throw new IllegalArgumentException("Unsupported expression type: " + arg.getClass());
}
Expand Down Expand Up @@ -317,7 +320,7 @@ public static Transform fromDTO(Partitioning partitioning) {
case FUNCTION:
return Transforms.apply(
((FunctionPartitioningDTO) partitioning).functionName(),
fromFunctionArg(((FunctionPartitioningDTO) partitioning).args()));
fromFunctionArgs(((FunctionPartitioningDTO) partitioning).args()));
default:
throw new IllegalArgumentException("Unsupported partitioning: " + partitioning.strategy());
}
Expand Down

0 comments on commit d3a56fa

Please sign in to comment.