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/paimon.git
The following commit(s) were added to refs/heads/master by this push:
new 20d2d4b764 [core] Add validation for default value (#6164)
20d2d4b764 is described below
commit 20d2d4b76452ef64b1bd1b122d3bd5abe34d2600
Author: Zouxxyy <[email protected]>
AuthorDate: Thu Aug 28 12:04:52 2025 +0800
[core] Add validation for default value (#6164)
---
.../org/apache/paimon/casting/DefaultValueRow.java | 19 ++-----
.../org/apache/paimon/utils/DefaultValueUtils.java | 61 ++++++++++++++++++++++
.../org/apache/paimon/catalog/CatalogUtils.java | 5 ++
.../org/apache/paimon/schema/SchemaManager.java | 17 +++---
.../org/apache/paimon/flink/BranchSqlITCase.java | 8 +++
.../apache/paimon/spark/sql/DefaultValueTest.scala | 52 ++++++++++++++++++
paimon-spark/pom.xml | 4 ++
7 files changed, 143 insertions(+), 23 deletions(-)
diff --git
a/paimon-common/src/main/java/org/apache/paimon/casting/DefaultValueRow.java
b/paimon-common/src/main/java/org/apache/paimon/casting/DefaultValueRow.java
index 4e62bf47ed..18bfc96390 100644
--- a/paimon-common/src/main/java/org/apache/paimon/casting/DefaultValueRow.java
+++ b/paimon-common/src/main/java/org/apache/paimon/casting/DefaultValueRow.java
@@ -29,12 +29,13 @@ import org.apache.paimon.data.variant.Variant;
import org.apache.paimon.types.DataField;
import org.apache.paimon.types.RowKind;
import org.apache.paimon.types.RowType;
-import org.apache.paimon.types.VarCharType;
import javax.annotation.Nullable;
import java.util.List;
+import static org.apache.paimon.utils.DefaultValueUtils.convertDefaultValue;
+
/**
* An implementation of {@link InternalRow} which provides a default value for
the underlying {@link
* InternalRow}.
@@ -215,21 +216,7 @@ public class DefaultValueRow implements InternalRow {
}
containsDefaultValue = true;
- @SuppressWarnings("unchecked")
- CastExecutor<Object, Object> resolve =
- (CastExecutor<Object, Object>)
- CastExecutors.resolve(VarCharType.STRING_TYPE,
dataField.type());
-
- if (resolve == null) {
- throw new RuntimeException(
- "Default value do not support the type of " +
dataField.type());
- }
-
- if (defaultValueStr.startsWith("'") &&
defaultValueStr.endsWith("'")) {
- defaultValueStr = defaultValueStr.substring(1,
defaultValueStr.length() - 1);
- }
-
- Object defaultValue =
resolve.cast(BinaryString.fromString(defaultValueStr));
+ Object defaultValue = convertDefaultValue(dataField.type(),
defaultValueStr);
row.setField(i, defaultValue);
}
diff --git
a/paimon-common/src/main/java/org/apache/paimon/utils/DefaultValueUtils.java
b/paimon-common/src/main/java/org/apache/paimon/utils/DefaultValueUtils.java
new file mode 100644
index 0000000000..b82f5f77e7
--- /dev/null
+++ b/paimon-common/src/main/java/org/apache/paimon/utils/DefaultValueUtils.java
@@ -0,0 +1,61 @@
+/*
+ * 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.paimon.utils;
+
+import org.apache.paimon.casting.CastExecutor;
+import org.apache.paimon.casting.CastExecutors;
+import org.apache.paimon.data.BinaryString;
+import org.apache.paimon.types.DataType;
+import org.apache.paimon.types.VarCharType;
+
+import javax.annotation.Nullable;
+
+/** Utils for default value. */
+public class DefaultValueUtils {
+
+ public static Object convertDefaultValue(DataType dataType, String
defaultValueStr) {
+ @SuppressWarnings("unchecked")
+ CastExecutor<Object, Object> resolve =
+ (CastExecutor<Object, Object>)
+ CastExecutors.resolve(VarCharType.STRING_TYPE,
dataType);
+
+ if (resolve == null) {
+ throw new RuntimeException("Default value do not support the type
of " + dataType);
+ }
+
+ if (defaultValueStr.startsWith("'") && defaultValueStr.endsWith("'")) {
+ defaultValueStr = defaultValueStr.substring(1,
defaultValueStr.length() - 1);
+ }
+
+ return resolve.cast(BinaryString.fromString(defaultValueStr));
+ }
+
+ public static void validateDefaultValue(DataType dataType, @Nullable
String defaultValueStr) {
+ if (defaultValueStr == null) {
+ return;
+ }
+
+ try {
+ convertDefaultValue(dataType, defaultValueStr);
+ } catch (Exception e) {
+ throw new RuntimeException(
+ "Unsupported default value `" + defaultValueStr + "` for
type " + dataType, e);
+ }
+ }
+}
diff --git
a/paimon-core/src/main/java/org/apache/paimon/catalog/CatalogUtils.java
b/paimon-core/src/main/java/org/apache/paimon/catalog/CatalogUtils.java
index 1a04f7a328..7bfc2d543c 100644
--- a/paimon-core/src/main/java/org/apache/paimon/catalog/CatalogUtils.java
+++ b/paimon-core/src/main/java/org/apache/paimon/catalog/CatalogUtils.java
@@ -39,6 +39,7 @@ import org.apache.paimon.table.object.ObjectTable;
import org.apache.paimon.table.system.AllTableOptionsTable;
import org.apache.paimon.table.system.CatalogOptionsTable;
import org.apache.paimon.table.system.SystemTableLoader;
+import org.apache.paimon.types.DataField;
import org.apache.paimon.utils.InternalRowPartitionComputer;
import org.apache.paimon.utils.Preconditions;
@@ -61,6 +62,7 @@ import static
org.apache.paimon.catalog.Catalog.TABLE_DEFAULT_OPTION_PREFIX;
import static
org.apache.paimon.options.OptionsUtils.convertToPropertiesPrefixKey;
import static
org.apache.paimon.table.system.AllTableOptionsTable.ALL_TABLE_OPTIONS;
import static
org.apache.paimon.table.system.CatalogOptionsTable.CATALOG_OPTIONS;
+import static org.apache.paimon.utils.DefaultValueUtils.validateDefaultValue;
import static org.apache.paimon.utils.Preconditions.checkArgument;
/** Utils for {@link Catalog}. */
@@ -147,6 +149,9 @@ public class CatalogUtils {
"Cannot define %s for format table.",
PRIMARY_KEY.key());
}
+ for (DataField field : schema.fields()) {
+ validateDefaultValue(field.type(), field.defaultValue());
+ }
}
public static void validateNamePattern(Catalog catalog, String
namePattern) {
diff --git
a/paimon-core/src/main/java/org/apache/paimon/schema/SchemaManager.java
b/paimon-core/src/main/java/org/apache/paimon/schema/SchemaManager.java
index a45005744e..fed004c434 100644
--- a/paimon-core/src/main/java/org/apache/paimon/schema/SchemaManager.java
+++ b/paimon-core/src/main/java/org/apache/paimon/schema/SchemaManager.java
@@ -91,6 +91,7 @@ import static
org.apache.paimon.catalog.AbstractCatalog.DB_SUFFIX;
import static org.apache.paimon.catalog.Identifier.DEFAULT_MAIN_BRANCH;
import static org.apache.paimon.catalog.Identifier.UNKNOWN_DATABASE;
import static
org.apache.paimon.mergetree.compact.PartialUpdateMergeFunction.SEQUENCE_GROUP;
+import static org.apache.paimon.utils.DefaultValueUtils.validateDefaultValue;
import static org.apache.paimon.utils.FileUtils.listVersionedFiles;
import static org.apache.paimon.utils.Preconditions.checkArgument;
import static org.apache.paimon.utils.Preconditions.checkState;
@@ -559,13 +560,15 @@ public class SchemaManager implements Serializable {
updateNestedColumn(
newFields,
update.fieldNames(),
- (field, depth) ->
- new DataField(
- field.id(),
- field.name(),
- field.type(),
- field.description(),
- update.newDefaultValue()),
+ (field, depth) -> {
+ validateDefaultValue(field.type(),
update.newDefaultValue());
+ return new DataField(
+ field.id(),
+ field.name(),
+ field.type(),
+ field.description(),
+ update.newDefaultValue());
+ },
lazyIdentifier);
} else {
throw new UnsupportedOperationException("Unsupported change: "
+ change.getClass());
diff --git
a/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/BranchSqlITCase.java
b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/BranchSqlITCase.java
index 8430ff3e9e..c593db273b 100644
---
a/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/BranchSqlITCase.java
+++
b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/BranchSqlITCase.java
@@ -52,6 +52,14 @@ public class BranchSqlITCase extends CatalogITCaseBase {
.containsExactlyInAnyOrder("+I[1, 5]", "+I[2, 5]");
}
+ @Test
+ public void testUnsupportedDefaultValue() {
+ sql("CREATE TABLE T (a INT, b INT)");
+ assertThatThrownBy(
+ () -> sql("CALL
sys.alter_column_default_value('default.T', 'b', 'ddd')"))
+ .hasMessageContaining("Unsupported default value `ddd` for
type INT");
+ }
+
@Test
public void testArrayDefaultValue() throws Exception {
sql("CREATE TABLE T_ARRAY (id INT, tags ARRAY<STRING>, numbers
ARRAY<INT>)");
diff --git
a/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/sql/DefaultValueTest.scala
b/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/sql/DefaultValueTest.scala
new file mode 100644
index 0000000000..9be4d4b377
--- /dev/null
+++
b/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/sql/DefaultValueTest.scala
@@ -0,0 +1,52 @@
+/*
+ * 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.paimon.spark.sql
+
+import org.apache.paimon.spark.PaimonSparkTestBase
+
+class DefaultValueTest extends PaimonSparkTestBase {
+
+ test("Default Value: unsupported default value") {
+ withTimeZone("Asia/Shanghai") {
+ withTable("t") {
+ assert(
+ intercept[Throwable] {
+ sql("""
+ |CREATE TABLE t (
+ | a INT,
+ | b TIMESTAMP DEFAULT current_timestamp(),
+ | c TIMESTAMP_NTZ DEFAULT current_timestamp,
+ | d DATE DEFAULT current_date()
+ |)
+ |""".stripMargin)
+ }.getMessage
+ .contains("Unsupported default value `current_timestamp()`"))
+
+ sql("CREATE TABLE t (a INT DEFAULT 1)")
+ assert(
+ intercept[Throwable] {
+ sql("""
+ |ALTER TABLE t ALTER COLUMN a SET DEFAULT current_timestamp()
+ |""".stripMargin)
+ }.getMessage
+ .contains("Unsupported default value `current_timestamp()`"))
+ }
+ }
+ }
+}
diff --git a/paimon-spark/pom.xml b/paimon-spark/pom.xml
index 4d688ac146..0cfe5007c7 100644
--- a/paimon-spark/pom.xml
+++ b/paimon-spark/pom.xml
@@ -58,6 +58,10 @@ under the License.
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
</exclusion>
+ <exclusion>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-lang3</artifactId>
+ </exclusion>
</exclusions>
</dependency>