From 7fea9f8916ed9fb66b92454e5876dffd6db439c0 Mon Sep 17 00:00:00 2001 From: mchades Date: Mon, 8 Jan 2024 21:07:58 +0800 Subject: [PATCH] add nullType for null literal value --- .../rel/expressions/literals/Literals.java | 2 ++ .../datastrato/gravitino/rel/types/Type.java | 3 ++- .../datastrato/gravitino/rel/types/Types.java | 22 +++++++++++++++++++ .../datastrato/gravitino/rel/TestLiteral.java | 3 +++ .../datastrato/gravitino/rel/TestTypes.java | 5 +++++ 5 files changed, 34 insertions(+), 1 deletion(-) diff --git a/api/src/main/java/com/datastrato/gravitino/rel/expressions/literals/Literals.java b/api/src/main/java/com/datastrato/gravitino/rel/expressions/literals/Literals.java index 08c04266604..6bf70bdfb8a 100644 --- a/api/src/main/java/com/datastrato/gravitino/rel/expressions/literals/Literals.java +++ b/api/src/main/java/com/datastrato/gravitino/rel/expressions/literals/Literals.java @@ -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 NULL = new LiteralImpl<>(null, Types.NullType.get()); /** * Creates a literal with the given value and data type. diff --git a/api/src/main/java/com/datastrato/gravitino/rel/types/Type.java b/api/src/main/java/com/datastrato/gravitino/rel/types/Type.java index 68a5b63748a..d8af929f69e 100644 --- a/api/src/main/java/com/datastrato/gravitino/rel/types/Type.java +++ b/api/src/main/java/com/datastrato/gravitino/rel/types/Type.java @@ -36,7 +36,8 @@ enum Name { STRUCT, LIST, MAP, - UNION + UNION, + NULL } /** The base type of all primitive types. */ diff --git a/api/src/main/java/com/datastrato/gravitino/rel/types/Types.java b/api/src/main/java/com/datastrato/gravitino/rel/types/Types.java index bb5f74186b3..66d6dcb5508 100644 --- a/api/src/main/java/com/datastrato/gravitino/rel/types/Types.java +++ b/api/src/main/java/com/datastrato/gravitino/rel/types/Types.java @@ -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(); diff --git a/api/src/test/java/com/datastrato/gravitino/rel/TestLiteral.java b/api/src/test/java/com/datastrato/gravitino/rel/TestLiteral.java index 66ff76a146e..32109f6c71a 100644 --- a/api/src/test/java/com/datastrato/gravitino/rel/TestLiteral.java +++ b/api/src/test/java/com/datastrato/gravitino/rel/TestLiteral.java @@ -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; @@ -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); } } diff --git a/api/src/test/java/com/datastrato/gravitino/rel/TestTypes.java b/api/src/test/java/com/datastrato/gravitino/rel/TestTypes.java index 67fe7f5fd8b..830e44851de 100644 --- a/api/src/test/java/com/datastrato/gravitino/rel/TestTypes.java +++ b/api/src/test/java/com/datastrato/gravitino/rel/TestTypes.java @@ -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());