This is an automated email from the ASF dual-hosted git repository.
jmclean pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gravitino.git
The following commit(s) were added to refs/heads/main by this push:
new 855cf14415 [#8679] cli: support decimal(precision) with default
scale=0 (#8679)
855cf14415 is described below
commit 855cf14415cb5bedf22a262ebc1b5744a83c6663
Author: ChangJun Rho <[email protected]>
AuthorDate: Sat Sep 27 06:42:01 2025 +0900
[#8679] cli: support decimal(precision) with default scale=0 (#8679)
## Summary
- Added support for parsing `decimal(p)` as `precision=p, scale=0`
- Preserved existing behavior for `decimal(p,s)`
- Updated regex in ParseType to allow spaces after type name
- Added unit tests for `decimal(10)`, case-insensitivity, and invalid
inputs
## Verification
- All unit tests in TestParseType passed locally
This is my first contribution to Gravitino.
Feedback and suggestions are very welcome. Thank you!
---
.../java/org/apache/gravitino/cli/ParseType.java | 31 ++++++++++++++++------
.../org/apache/gravitino/cli/TestParseType.java | 18 +++++++++++++
2 files changed, 41 insertions(+), 8 deletions(-)
diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/ParseType.java
b/clients/cli/src/main/java/org/apache/gravitino/cli/ParseType.java
index f452ad0ba7..f59d150753 100644
--- a/clients/cli/src/main/java/org/apache/gravitino/cli/ParseType.java
+++ b/clients/cli/src/main/java/org/apache/gravitino/cli/ParseType.java
@@ -42,7 +42,7 @@ public class ParseType {
* @throws IllegalArgumentException if the data type format is unsupported
or malformed
*/
public static ParsedType parseBasicType(String datatype) {
- Pattern pattern =
Pattern.compile("^(\\w+)\\(\\s*(\\d+)\\s*(?:,\\s*(\\d+)\\s*)?\\)$");
+ Pattern pattern =
Pattern.compile("^(\\w+)\\s*\\(\\s*(\\d+)\\s*(?:,\\s*(\\d+)\\s*)?\\)$");
Matcher matcher = pattern.matcher(datatype);
if (matcher.matches()) {
@@ -66,6 +66,21 @@ public class ParseType {
ParsedType parsed = parseBasicType(datatype);
if (parsed != null) {
+ // Special handling for DECIMAL type
+ // If only one argument is provided (e.g., "decimal(10)"),
+ // treat it as DECIMAL with precision = arg and scale = 0.
+ if ("decimal".equalsIgnoreCase(parsed.getTypeName())) {
+ if (parsed.getPrecision() != null && parsed.getScale() != null) {
+ // Standard case: decimal(p, s)
+ return TypeConverter.convert(datatype, parsed.getPrecision(),
parsed.getScale());
+ } else if (parsed.getLength() != null) {
+ // decimal(p) → precision=p, scale=0
+ int precision = parsed.getLength();
+ return Types.DecimalType.of(precision, 0);
+ }
+ }
+
+ // Fallback for other types (e.g., varchar, char, etc.)
if (parsed.getPrecision() != null && parsed.getScale() != null) {
return TypeConverter.convert(datatype, parsed.getPrecision(),
parsed.getScale());
} else if (parsed.getLength() != null) {
@@ -73,6 +88,7 @@ public class ParseType {
}
}
+ // If no match, use generic conversion
return TypeConverter.convert(datatype);
}
@@ -105,13 +121,12 @@ public class ParseType {
* @return a {@link org.apache.gravitino.rel.types.Type} object representing
the parsed type.
*/
public static Type toType(String datatype) {
- if (datatype.startsWith("list")) {
- return toListType(datatype);
- } else if (datatype.startsWith("map")) {
- return toMapType(datatype);
+ String dt = datatype.trim(); // normalize input
+ if (dt.startsWith("list")) {
+ return toListType(dt);
+ } else if (dt.startsWith("map")) {
+ return toMapType(dt);
}
-
- // fallback: if not complex type, parse as primitive type
- return toBasicType(datatype);
+ return toBasicType(dt);
}
}
diff --git
a/clients/cli/src/test/java/org/apache/gravitino/cli/TestParseType.java
b/clients/cli/src/test/java/org/apache/gravitino/cli/TestParseType.java
index c4ceac0a25..35a372b3d0 100644
--- a/clients/cli/src/test/java/org/apache/gravitino/cli/TestParseType.java
+++ b/clients/cli/src/test/java/org/apache/gravitino/cli/TestParseType.java
@@ -155,4 +155,22 @@ public class TestParseType {
assertThat(type, instanceOf(Types.VarCharType.class));
assertEquals(10, ((Types.VarCharType) type).length());
}
+
+ /** Ensures that "decimal(10)" is parsed as DecimalType with precision=10
and scale=0. */
+ @Test
+ public void testParseTypeDecimalWithPrecisionOnly() {
+ Type type = ParseType.toType("decimal(10)");
+ assertThat(type, instanceOf(Types.DecimalType.class));
+ assertEquals(10, ((Types.DecimalType) type).precision());
+ assertEquals(0, ((Types.DecimalType) type).scale());
+ }
+
+ /** Accepts case-insensitive input and extra spaces for the single-arg form.
*/
+ @Test
+ public void testParseTypeDecimalWithPrecisionOnlySpacesAndCase() {
+ Type type = ParseType.toType("DECIMAL ( 10 ) ");
+ assertThat(type, instanceOf(Types.DecimalType.class));
+ assertEquals(10, ((Types.DecimalType) type).precision());
+ assertEquals(0, ((Types.DecimalType) type).scale());
+ }
}