This is an automated email from the ASF dual-hosted git repository.
lzljs3620320 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-paimon.git
The following commit(s) were added to refs/heads/master by this push:
new 283ed108f [core] Introduce getPrecision and getLength to DataTypes
(#1685)
283ed108f is described below
commit 283ed108ff892b9b8e4f1c611dfa7b611649d33c
Author: Jingsong Lee <[email protected]>
AuthorDate: Mon Jul 31 13:59:35 2023 +0800
[core] Introduce getPrecision and getLength to DataTypes (#1685)
---
.../org/apache/paimon/types/DataTypeChecks.java | 54 ++++++-----------
.../java/org/apache/paimon/types/DataTypes.java | 69 ++++++++++++++++++++++
2 files changed, 88 insertions(+), 35 deletions(-)
diff --git
a/paimon-common/src/main/java/org/apache/paimon/types/DataTypeChecks.java
b/paimon-common/src/main/java/org/apache/paimon/types/DataTypeChecks.java
index bbe5505a3..9889e98a2 100644
--- a/paimon-common/src/main/java/org/apache/paimon/types/DataTypeChecks.java
+++ b/paimon-common/src/main/java/org/apache/paimon/types/DataTypeChecks.java
@@ -21,6 +21,7 @@ package org.apache.paimon.types;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
+import java.util.OptionalInt;
import static org.apache.paimon.types.DataTypeRoot.ROW;
@@ -145,48 +146,31 @@ public final class DataTypeChecks {
}
private static class LengthExtractor extends Extractor<Integer> {
-
- @Override
- public Integer visit(CharType charType) {
- return charType.getLength();
- }
-
@Override
- public Integer visit(VarCharType varCharType) {
- return varCharType.getLength();
- }
-
- @Override
- public Integer visit(BinaryType binaryType) {
- return binaryType.getLength();
- }
-
- @Override
- public Integer visit(VarBinaryType varBinaryType) {
- return varBinaryType.getLength();
+ protected Integer defaultMethod(DataType dataType) {
+ OptionalInt length = DataTypes.getLength(dataType);
+ if (length.isPresent()) {
+ return length.getAsInt();
+ }
+ throw new IllegalArgumentException(
+ String.format(
+ "Invalid use of extractor %s. Called on logical
type: %s",
+ this.getClass().getName(), dataType));
}
}
private static class PrecisionExtractor extends Extractor<Integer> {
@Override
- public Integer visit(DecimalType decimalType) {
- return decimalType.getPrecision();
- }
-
- @Override
- public Integer visit(TimeType timeType) {
- return timeType.getPrecision();
- }
-
- @Override
- public Integer visit(TimestampType timestampType) {
- return timestampType.getPrecision();
- }
-
- @Override
- public Integer visit(LocalZonedTimestampType localZonedTimestampType) {
- return localZonedTimestampType.getPrecision();
+ protected Integer defaultMethod(DataType dataType) {
+ OptionalInt precision = DataTypes.getPrecision(dataType);
+ if (precision.isPresent()) {
+ return precision.getAsInt();
+ }
+ throw new IllegalArgumentException(
+ String.format(
+ "Invalid use of extractor %s. Called on logical
type: %s",
+ this.getClass().getName(), dataType));
}
}
diff --git a/paimon-common/src/main/java/org/apache/paimon/types/DataTypes.java
b/paimon-common/src/main/java/org/apache/paimon/types/DataTypes.java
index 6ce356d83..659212b06 100644
--- a/paimon-common/src/main/java/org/apache/paimon/types/DataTypes.java
+++ b/paimon-common/src/main/java/org/apache/paimon/types/DataTypes.java
@@ -21,6 +21,7 @@ package org.apache.paimon.types;
import org.apache.paimon.annotation.Public;
import java.util.Arrays;
+import java.util.OptionalInt;
/**
* Utils for creating {@link DataType}s.
@@ -145,4 +146,72 @@ public class DataTypes {
public static MultisetType MULTISET(DataType elementType) {
return new MultisetType(elementType);
}
+
+ public static OptionalInt getPrecision(DataType dataType) {
+ return dataType.accept(PRECISION_EXTRACTOR);
+ }
+
+ public static OptionalInt getLength(DataType dataType) {
+ return dataType.accept(LENGTH_EXTRACTOR);
+ }
+
+ private static final PrecisionExtractor PRECISION_EXTRACTOR = new
PrecisionExtractor();
+
+ private static final LengthExtractor LENGTH_EXTRACTOR = new
LengthExtractor();
+
+ private static class PrecisionExtractor extends
DataTypeDefaultVisitor<OptionalInt> {
+
+ @Override
+ public OptionalInt visit(DecimalType decimalType) {
+ return OptionalInt.of(decimalType.getPrecision());
+ }
+
+ @Override
+ public OptionalInt visit(TimeType timeType) {
+ return OptionalInt.of(timeType.getPrecision());
+ }
+
+ @Override
+ public OptionalInt visit(TimestampType timestampType) {
+ return OptionalInt.of(timestampType.getPrecision());
+ }
+
+ @Override
+ public OptionalInt visit(LocalZonedTimestampType
localZonedTimestampType) {
+ return OptionalInt.of(localZonedTimestampType.getPrecision());
+ }
+
+ @Override
+ protected OptionalInt defaultMethod(DataType dataType) {
+ return OptionalInt.empty();
+ }
+ }
+
+ private static class LengthExtractor extends
DataTypeDefaultVisitor<OptionalInt> {
+
+ @Override
+ public OptionalInt visit(CharType charType) {
+ return OptionalInt.of(charType.getLength());
+ }
+
+ @Override
+ public OptionalInt visit(VarCharType varCharType) {
+ return OptionalInt.of(varCharType.getLength());
+ }
+
+ @Override
+ public OptionalInt visit(BinaryType binaryType) {
+ return OptionalInt.of(binaryType.getLength());
+ }
+
+ @Override
+ public OptionalInt visit(VarBinaryType varBinaryType) {
+ return OptionalInt.of(varBinaryType.getLength());
+ }
+
+ @Override
+ protected OptionalInt defaultMethod(DataType dataType) {
+ return OptionalInt.empty();
+ }
+ }
}