This is an automated email from the ASF dual-hosted git repository.
fokko pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iceberg.git
The following commit(s) were added to refs/heads/master by this push:
new 776c6e6e61 API: Allow spaces in string type definitions (#6917)
776c6e6e61 is described below
commit 776c6e6e61bc90573545f9dc788ee40eb451254d
Author: clesaec <[email protected]>
AuthorDate: Fri Feb 24 09:12:20 2023 +0100
API: Allow spaces in string type definitions (#6917)
* Fix Types from string, allow spaces
* Use Assertj
---
.../main/java/org/apache/iceberg/types/Types.java | 5 ++-
.../java/org/apache/iceberg/types/TestTypes.java | 47 ++++++++++++++++++++++
2 files changed, 50 insertions(+), 2 deletions(-)
diff --git a/api/src/main/java/org/apache/iceberg/types/Types.java
b/api/src/main/java/org/apache/iceberg/types/Types.java
index 4361f95f1e..da70dd9ac6 100644
--- a/api/src/main/java/org/apache/iceberg/types/Types.java
+++ b/api/src/main/java/org/apache/iceberg/types/Types.java
@@ -53,8 +53,9 @@ public class Types {
.put(BinaryType.get().toString(), BinaryType.get())
.buildOrThrow();
- private static final Pattern FIXED = Pattern.compile("fixed\\[(\\d+)\\]");
- private static final Pattern DECIMAL =
Pattern.compile("decimal\\((\\d+),\\s+(\\d+)\\)");
+ private static final Pattern FIXED =
Pattern.compile("fixed\\[\\s*(\\d+)\\s*\\]");
+ private static final Pattern DECIMAL =
+ Pattern.compile("decimal\\(\\s*(\\d+)\\s*,\\s*(\\d+)\\s*\\)");
public static PrimitiveType fromPrimitiveString(String typeString) {
String lowerTypeString = typeString.toLowerCase(Locale.ROOT);
diff --git a/api/src/test/java/org/apache/iceberg/types/TestTypes.java
b/api/src/test/java/org/apache/iceberg/types/TestTypes.java
new file mode 100644
index 0000000000..20a393f7dc
--- /dev/null
+++ b/api/src/test/java/org/apache/iceberg/types/TestTypes.java
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.types;
+
+import org.assertj.core.api.Assertions;
+import org.junit.Test;
+
+public class TestTypes {
+
+ @Test
+ public void fromPrimitiveString() {
+
Assertions.assertThat(Types.fromPrimitiveString("boolean")).isSameAs(Types.BooleanType.get());
+
Assertions.assertThat(Types.fromPrimitiveString("BooLean")).isSameAs(Types.BooleanType.get());
+
+ Assertions.assertThat(Types.fromPrimitiveString("timestamp"))
+ .isSameAs(Types.TimestampType.withoutZone());
+
+ Assertions.assertThat(Types.fromPrimitiveString("Fixed[ 3 ]"))
+ .isEqualTo(Types.FixedType.ofLength(3));
+
+ Assertions.assertThat(Types.fromPrimitiveString("Decimal( 2 , 3 )"))
+ .isEqualTo(Types.DecimalType.of(2, 3));
+
+ Assertions.assertThat(Types.fromPrimitiveString("Decimal(2,3)"))
+ .isEqualTo(Types.DecimalType.of(2, 3));
+
+ Assertions.assertThatExceptionOfType(IllegalArgumentException.class)
+ .isThrownBy(() -> Types.fromPrimitiveString("Unknown"))
+ .withMessageContaining("Unknown");
+ }
+}