Skip to content

Commit

Permalink
[#1384] feat(api): Add NullType for representing NULL values (#1388)
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?

Add `NullType` for representing NULL values

### Why are the changes needed?

Fix: #1384 

### Does this PR introduce _any_ user-facing change?

yes, users now can use null literal

### How was this patch tested?

UTs added
  • Loading branch information
mchades authored Jan 10, 2024
1 parent fbd19c8 commit 3551ada
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 1 deletion.
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

0 comments on commit 3551ada

Please sign in to comment.