Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#1384] feat(api): Add NullType for representing NULL values #1388

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it a typical way to use NullType for Null value?

Copy link
Contributor Author

@mchades mchades Jan 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so. There are some refer:


/**
* 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
Loading