Skip to content

Commit

Permalink
[#1383] Add more static methods of Literal for Java client usage (#1386)
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?

Add more static methods of Literal for Java client usage

### Why are the changes needed?

Fix: #1383 

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

no, but users can use `Literal` more conveniently by Java client

### How was this patch tested?

UTs added
  • Loading branch information
mchades authored Jan 9, 2024
1 parent 1370327 commit f4bc086
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

import com.datastrato.gravitino.rel.types.Type;
import com.datastrato.gravitino.rel.types.Types;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Objects;

/** The helper class to create literals to pass into Gravitino. */
Expand All @@ -23,6 +26,36 @@ public static <T> LiteralImpl<T> of(T value, Type dataType) {
return new LiteralImpl<>(value, dataType);
}

/**
* Creates a boolean type literal with the given value.
*
* @param value the boolean literal value
* @return a new {@link Literal} instance
*/
public static LiteralImpl<Boolean> booleanLiteral(Boolean value) {
return of(value, Types.BooleanType.get());
}

/**
* Creates a byte type literal with the given value.
*
* @param value the byte literal value
* @return a new {@link Literal} instance
*/
public static LiteralImpl<Byte> byteLiteral(Byte value) {
return of(value, Types.ByteType.get());
}

/**
* Creates a short type literal with the given value.
*
* @param value the short literal value
* @return a new {@link Literal} instance
*/
public static LiteralImpl<Short> shortLiteral(Short value) {
return of(value, Types.ShortType.get());
}

/**
* Creates an integer type literal with the given value.
*
Expand All @@ -33,6 +66,66 @@ public static LiteralImpl<Integer> integer(Integer value) {
return of(value, Types.IntegerType.get());
}

/**
* Creates a long type literal with the given value.
*
* @param value the long literal value
* @return a new {@link Literal} instance
*/
public static LiteralImpl<Long> longLiteral(Long value) {
return of(value, Types.LongType.get());
}

/**
* Creates a float type literal with the given value.
*
* @param value the float literal value
* @return a new {@link Literal} instance
*/
public static LiteralImpl<Float> floatLiteral(Float value) {
return of(value, Types.FloatType.get());
}

/**
* Creates a double type literal with the given value.
*
* @param value the double literal value
* @return a new {@link Literal} instance
*/
public static LiteralImpl<Double> doubleLiteral(Double value) {
return of(value, Types.DoubleType.get());
}

/**
* Creates a date type literal with the given value.
*
* @param value the date literal value
* @return a new {@link Literal} instance
*/
public static LiteralImpl<LocalDate> date(LocalDate value) {
return of(value, Types.DateType.get());
}

/**
* Creates a time type literal with the given value.
*
* @param value the time literal value
* @return a new {@link Literal} instance
*/
public static LiteralImpl<LocalTime> time(LocalTime value) {
return of(value, Types.TimeType.get());
}

/**
* Creates a timestamp type literal with the given value.
*
* @param value the timestamp literal value
* @return a new {@link Literal} instance
*/
public static LiteralImpl<LocalDateTime> timestamp(LocalDateTime value) {
return of(value, Types.TimestampType.withoutTimeZone());
}

/**
* Creates a string type literal with the given value.
*
Expand Down
75 changes: 75 additions & 0 deletions api/src/test/java/com/datastrato/gravitino/rel/TestLiteral.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2024 Datastrato Pvt Ltd.
* This software is licensed under the Apache License version 2.
*/
package com.datastrato.gravitino.rel;

import static com.datastrato.gravitino.rel.expressions.literals.Literals.booleanLiteral;
import static com.datastrato.gravitino.rel.expressions.literals.Literals.byteLiteral;
import static com.datastrato.gravitino.rel.expressions.literals.Literals.date;
import static com.datastrato.gravitino.rel.expressions.literals.Literals.doubleLiteral;
import static com.datastrato.gravitino.rel.expressions.literals.Literals.floatLiteral;
import static com.datastrato.gravitino.rel.expressions.literals.Literals.integer;
import static com.datastrato.gravitino.rel.expressions.literals.Literals.longLiteral;
import static com.datastrato.gravitino.rel.expressions.literals.Literals.shortLiteral;
import static com.datastrato.gravitino.rel.expressions.literals.Literals.string;
import static com.datastrato.gravitino.rel.expressions.literals.Literals.time;
import static com.datastrato.gravitino.rel.expressions.literals.Literals.timestamp;

import com.datastrato.gravitino.rel.expressions.literals.Literal;
import com.datastrato.gravitino.rel.types.Types;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class TestLiteral {

@Test
public void testLiterals() {
Literal literal = booleanLiteral(Boolean.valueOf("true"));
Assertions.assertEquals(literal.value(), true);
Assertions.assertEquals(literal.dataType(), Types.BooleanType.get());

literal = byteLiteral(Byte.valueOf("1"));
Assertions.assertEquals(literal.value(), (byte) 1);
Assertions.assertEquals(literal.dataType(), Types.ByteType.get());

literal = shortLiteral(Short.valueOf("1"));
Assertions.assertEquals(literal.value(), (short) 1);
Assertions.assertEquals(literal.dataType(), Types.ShortType.get());

literal = integer(Integer.valueOf("1"));
Assertions.assertEquals(literal.value(), 1);
Assertions.assertEquals(literal.dataType(), Types.IntegerType.get());

literal = longLiteral(Long.valueOf("1"));
Assertions.assertEquals(literal.value(), 1L);
Assertions.assertEquals(literal.dataType(), Types.LongType.get());

literal = floatLiteral(Float.valueOf("1.234"));
Assertions.assertEquals(literal.value(), 1.234f);
Assertions.assertEquals(literal.dataType(), Types.FloatType.get());

literal = doubleLiteral(Double.valueOf("1.234"));
Assertions.assertEquals(literal.value(), 1.234d);
Assertions.assertEquals(literal.dataType(), Types.DoubleType.get());

literal = date(LocalDate.parse("2020-01-01"));
Assertions.assertEquals(literal.value(), LocalDate.of(2020, 1, 1));
Assertions.assertEquals(literal.dataType(), Types.DateType.get());

literal = time(LocalTime.parse("12:34:56"));
Assertions.assertEquals(literal.value(), LocalTime.of(12, 34, 56));
Assertions.assertEquals(literal.dataType(), Types.TimeType.get());

literal = timestamp(LocalDateTime.parse("2020-01-01T12:34:56"));
Assertions.assertEquals(literal.value(), LocalDateTime.of(2020, 1, 1, 12, 34, 56));
Assertions.assertEquals(literal.dataType(), Types.TimestampType.withoutTimeZone());

literal = string("hello");
Assertions.assertEquals(literal.value(), "hello");
Assertions.assertEquals(literal.dataType(), Types.StringType.get());
}
}

0 comments on commit f4bc086

Please sign in to comment.