Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#1385] feat(api): Add Decimal class for holding decimal literal value (#1389)

mchadesc0a1a804482c
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 6bf70bdfb8..c5b8df7738 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
@@ -4,6 +4,7 @@
*/
package com.datastrato.gravitino.rel.expressions.literals;
+import com.datastrato.gravitino.rel.types.Decimal;
import com.datastrato.gravitino.rel.types.Type;
import com.datastrato.gravitino.rel.types.Types;
import java.time.LocalDate;
@@ -64,7 +65,7 @@ public class Literals {
* @param value the integer literal value
* @return a new {@link Literal} instance
*/
- public static LiteralImpl<Integer> integer(Integer value) {
+ public static LiteralImpl<Integer> integerLiteral(Integer value) {
return of(value, Types.IntegerType.get());
}
@@ -98,13 +99,24 @@ public class Literals {
return of(value, Types.DoubleType.get());
}
+ /**
+ * Creates a decimal type literal with the given value.
+ *
+ * @param value the decimal literal value
+ * @return a new {@link Literal} instance
+ */
+ public static LiteralImpl<Decimal> decimalLiteral(Decimal value) {
+
+ return of(value, Types.DecimalType.of(value.precision(), value.scale()));
+ }
+
/**
* 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) {
+ public static LiteralImpl<LocalDate> dateLiteral(LocalDate value) {
return of(value, Types.DateType.get());
}
@@ -114,7 +126,7 @@ public class Literals {
* @param value the time literal value
* @return a new {@link Literal} instance
*/
- public static LiteralImpl<LocalTime> time(LocalTime value) {
+ public static LiteralImpl<LocalTime> timeLiteral(LocalTime value) {
return of(value, Types.TimeType.get());
}
@@ -124,7 +136,7 @@ public class Literals {
* @param value the timestamp literal value
* @return a new {@link Literal} instance
*/
- public static LiteralImpl<LocalDateTime> timestamp(LocalDateTime value) {
+ public static LiteralImpl<LocalDateTime> timestampLiteral(LocalDateTime value) {
return of(value, Types.TimestampType.withoutTimeZone());
}
@@ -134,7 +146,7 @@ public class Literals {
* @param value the string literal value
* @return a new {@link Literal} instance
*/
- public static LiteralImpl<String> string(String value) {
+ public static LiteralImpl<String> stringLiteral(String value) {
return of(value, Types.StringType.get());
}
diff --git a/api/src/main/java/com/datastrato/gravitino/rel/expressions/transforms/Transforms.java b/api/src/main/java/com/datastrato/gravitino/rel/expressions/transforms/Transforms.java
index 88fd61c0c0..66d3fff256 100644
--- a/api/src/main/java/com/datastrato/gravitino/rel/expressions/transforms/Transforms.java
+++ b/api/src/main/java/com/datastrato/gravitino/rel/expressions/transforms/Transforms.java
@@ -134,7 +134,7 @@ public class Transforms {
*/
public static BucketTransform bucket(int numBuckets, String[]... fieldNames) {
return new BucketTransform(
- Literals.integer(numBuckets),
+ Literals.integerLiteral(numBuckets),
Arrays.stream(fieldNames).map(NamedReference::field).toArray(NamedReference[]::new));
}
@@ -167,7 +167,7 @@ public class Transforms {
* @return The created transform
*/
public static TruncateTransform truncate(int width, String[] fieldName) {
- return new TruncateTransform(Literals.integer(width), NamedReference.field(fieldName));
+ return new TruncateTransform(Literals.integerLiteral(width), NamedReference.field(fieldName));
}
/**
diff --git a/api/src/main/java/com/datastrato/gravitino/rel/types/Decimal.java b/api/src/main/java/com/datastrato/gravitino/rel/types/Decimal.java
new file mode 100644
index 0000000000..30ea83c76e
--- /dev/null
+++ b/api/src/main/java/com/datastrato/gravitino/rel/types/Decimal.java
@@ -0,0 +1,124 @@
+/*
+ * Copyright 2024 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+package com.datastrato.gravitino.rel.types;
+
+import static com.datastrato.gravitino.rel.types.Types.DecimalType.checkPrecisionScale;
+
+import com.google.common.base.Preconditions;
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+import java.util.Objects;
+
+/**
+ * Used to represent a {@link Types.DecimalType} value in Gravitino.
+ *
+ * <p>For Decimal, we expect the precision is equal to or larger than the scale, however, in {@link
+ * BigDecimal}, the digit count starts from the leftmost nonzero digit of the exact result. For
+ * example, the precision of 0.01 equals to 1 based on the definition, but the scale is 2. The
+ * expected precision should be 2.
+ */
+public class Decimal {
+
+ private final BigDecimal value;
+ private final int precision;
+ private final int scale;
+
+ /**
+ * Creates a decimal value with the given value, precision and scale.
+ *
+ * @param value The value of the decimal.
+ * @param precision The precision of the decimal.
+ * @param scale The scale of the decimal.
+ * @return A new {@link Decimal} instance.
+ */
+ public static Decimal of(BigDecimal value, int precision, int scale) {
+ return new Decimal(value, precision, scale);
+ }
+
+ /**
+ * Creates a decimal value with the given value.
+ *
+ * @param value The value of the decimal.
+ * @return A new {@link Decimal} instance.
+ */
+ public static Decimal of(BigDecimal value) {
+ return new Decimal(value);
+ }
+
+ /**
+ * Creates a decimal value with the given value.
+ *
+ * @param value The value of the decimal.
+ * @return A new {@link Decimal} instance.
+ */
+ public static Decimal of(String value) {
+ return of(new BigDecimal(value));
+ }
+
+ /**
+ * Creates a decimal value with the given value, precision and scale.
+ *
+ * @param value The value of the decimal.
+ * @param precision The precision of the decimal.
+ * @param scale The scale of the decimal.
+ * @return A new {@link Decimal} instance.
+ */
+ public static Decimal of(String value, int precision, int scale) {
+ return of(new BigDecimal(value), precision, scale);
+ }
+
+ /** @return The value of the decimal. */
+ public BigDecimal value() {
+ return value;
+ }
+
+ /** @return The precision of the decimal. */
+ public int precision() {
+ return precision;
+ }
+
+ /** @return The scale of the decimal. */
+ public int scale() {
+ return scale;
+ }
+
+ private Decimal(BigDecimal value, int precision, int scale) {
+ checkPrecisionScale(precision, scale);
+ Preconditions.checkArgument(
+ precision >= value.precision(),
+ "Precision of value cannot be greater than precision of decimal: %s > %s",
+ value.precision(),
+ precision);
+ this.value = value.setScale(scale, RoundingMode.HALF_UP);
+ this.scale = scale;
+ this.precision = precision;
+ }
+
+ private Decimal(BigDecimal value) {
+ this(value, Math.max(value.precision(), value.scale()), value.scale());
+ }
+
+ @Override
+ public String toString() {
+ return value.toString();
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ Decimal decimal = (Decimal) o;
+ return scale == decimal.scale && Objects.equals(value, decimal.value);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(value, precision, scale);
+ }
+}
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 66d6dcb550..f4687e5a2b 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
@@ -203,14 +203,18 @@ public class Types {
private final int scale;
private DecimalType(int precision, int scale) {
+ checkPrecisionScale(precision, scale);
+ this.precision = precision;
+ this.scale = scale;
+ }
+
+ public static void checkPrecisionScale(int precision, int scale) {
Preconditions.checkArgument(
precision <= 38,
"Decimals with precision larger than 38 are not supported: %s",
precision);
Preconditions.checkArgument(
scale <= precision, "Scale cannot be larger than precision: %s > %s", scale, precision);
- this.precision = precision;
- this.scale = scale;
}
@Override
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 32109f6c71..0f91e52057 100644
--- a/api/src/test/java/com/datastrato/gravitino/rel/TestLiteral.java
+++ b/api/src/test/java/com/datastrato/gravitino/rel/TestLiteral.java
@@ -6,19 +6,22 @@ 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.dateLiteral;
+import static com.datastrato.gravitino.rel.expressions.literals.Literals.decimalLiteral;
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.integerLiteral;
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 static com.datastrato.gravitino.rel.expressions.literals.Literals.stringLiteral;
+import static com.datastrato.gravitino.rel.expressions.literals.Literals.timeLiteral;
+import static com.datastrato.gravitino.rel.expressions.literals.Literals.timestampLiteral;
import com.datastrato.gravitino.rel.expressions.literals.Literal;
import com.datastrato.gravitino.rel.expressions.literals.Literals;
+import com.datastrato.gravitino.rel.types.Decimal;
import com.datastrato.gravitino.rel.types.Types;
+import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
@@ -29,7 +32,7 @@ public class TestLiteral {
@Test
public void testLiterals() {
- Literal literal = booleanLiteral(Boolean.valueOf("true"));
+ Literal<?> literal = booleanLiteral(Boolean.valueOf("true"));
Assertions.assertEquals(literal.value(), true);
Assertions.assertEquals(literal.dataType(), Types.BooleanType.get());
@@ -41,7 +44,7 @@ public class TestLiteral {
Assertions.assertEquals(literal.value(), (short) 1);
Assertions.assertEquals(literal.dataType(), Types.ShortType.get());
- literal = integer(Integer.valueOf("1"));
+ literal = integerLiteral(Integer.valueOf("1"));
Assertions.assertEquals(literal.value(), 1);
Assertions.assertEquals(literal.dataType(), Types.IntegerType.get());
@@ -57,22 +60,26 @@ public class TestLiteral {
Assertions.assertEquals(literal.value(), 1.234d);
Assertions.assertEquals(literal.dataType(), Types.DoubleType.get());
- literal = date(LocalDate.parse("2020-01-01"));
+ literal = dateLiteral(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"));
+ literal = timeLiteral(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"));
+ literal = timestampLiteral(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");
+ literal = stringLiteral("hello");
Assertions.assertEquals(literal.value(), "hello");
Assertions.assertEquals(literal.dataType(), Types.StringType.get());
Assertions.assertEquals(Literals.of(null, Types.NullType.get()), Literals.NULL);
+
+ literal = decimalLiteral(Decimal.of("0.00"));
+ Assertions.assertEquals(literal.value(), Decimal.of(new BigDecimal("0.00")));
+ Assertions.assertEquals(literal.dataType(), Types.DecimalType.of(2, 2));
}
}
diff --git a/api/src/test/java/com/datastrato/gravitino/rel/TestTransforms.java b/api/src/test/java/com/datastrato/gravitino/rel/TestTransforms.java
index c53aeb22ce..0688ffee63 100644
--- a/api/src/test/java/com/datastrato/gravitino/rel/TestTransforms.java
+++ b/api/src/test/java/com/datastrato/gravitino/rel/TestTransforms.java
@@ -110,7 +110,7 @@ public class TestTransforms {
};
// partition by foo(col_1, 'bar')
NamedReference.FieldReference arg1 = field(column.name());
- Literals.LiteralImpl<String> arg2 = Literals.string("bar");
+ Literals.LiteralImpl<String> arg2 = Literals.stringLiteral("bar");
Transform applyTransform = apply("foo", new Expression[] {arg1, arg2});
Assertions.assertEquals("foo", applyTransform.name());
Assertions.assertEquals(2, applyTransform.arguments().length);