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 c126e4061b [#8035] fix: correct CREATE TABLE SQL generation in
SqliteTableOperations (#8105)
c126e4061b is described below
commit c126e4061bc5f847252150697f9c9ec2005ad9b9
Author: KWON TAE HEON <[email protected]>
AuthorDate: Sat Aug 16 10:33:10 2025 +0900
[#8035] fix: correct CREATE TABLE SQL generation in SqliteTableOperations
(#8105)
### What changes were proposed in this pull request?
Update SqliteTableOperations table creation SQL generation to:
- Prevent malformed statements when a comment is specified.
- Always append a semicolon (;) to the statement.
### Why are the changes needed?
- The creation of comments and properties interfere with each other.
- A missing semicolon may cause statement execution failures.
Fix: #8035
### Does this PR introduce _any_ user-facing change?
- Yes.
- Generated CREATE TABLE SQL now includes the table comment and ends
with a semicolon.
### How was this patch tested?
- Added a test in `TestSqliteTableOperations.java` to validate COMMENT
inclusion and semicolon termination.
- Verified existing tests pass to ensure no regressions.
---
.../jdbc/operation/SqliteTableOperations.java | 17 ++--
.../jdbc/operation/TestSqliteTableOperations.java | 113 +++++++++++++++++++++
2 files changed, 121 insertions(+), 9 deletions(-)
diff --git
a/catalogs/catalog-jdbc-common/src/test/java/org/apache/gravitino/catalog/jdbc/operation/SqliteTableOperations.java
b/catalogs/catalog-jdbc-common/src/test/java/org/apache/gravitino/catalog/jdbc/operation/SqliteTableOperations.java
index 0b48dd9652..0cef47e201 100644
---
a/catalogs/catalog-jdbc-common/src/test/java/org/apache/gravitino/catalog/jdbc/operation/SqliteTableOperations.java
+++
b/catalogs/catalog-jdbc-common/src/test/java/org/apache/gravitino/catalog/jdbc/operation/SqliteTableOperations.java
@@ -72,18 +72,17 @@ public class SqliteTableOperations extends
JdbcTableOperations {
if (columns.length > 0) {
sqlBuilder.deleteCharAt(sqlBuilder.length() - 1);
}
-
+ sqlBuilder.append(")");
if (comment != null && !comment.isEmpty()) {
- sqlBuilder.append(") COMMENT '").append(comment).append("'");
- } else {
- sqlBuilder.append(")");
- if (properties != null && !properties.isEmpty()) {
- for (Map.Entry<String, String> entry : properties.entrySet()) {
- sqlBuilder.append("
").append(entry.getKey()).append("=").append(entry.getValue());
- }
+ sqlBuilder.append(" COMMENT '").append(comment).append("'");
+ }
+
+ if (properties != null && !properties.isEmpty()) {
+ for (Map.Entry<String, String> entry : properties.entrySet()) {
+ sqlBuilder.append("
").append(entry.getKey()).append("=").append(entry.getValue());
}
- sqlBuilder.append(";");
}
+ sqlBuilder.append(";");
return sqlBuilder.toString();
}
diff --git
a/catalogs/catalog-jdbc-common/src/test/java/org/apache/gravitino/catalog/jdbc/operation/TestSqliteTableOperations.java
b/catalogs/catalog-jdbc-common/src/test/java/org/apache/gravitino/catalog/jdbc/operation/TestSqliteTableOperations.java
new file mode 100644
index 0000000000..4ccf8c4433
--- /dev/null
+++
b/catalogs/catalog-jdbc-common/src/test/java/org/apache/gravitino/catalog/jdbc/operation/TestSqliteTableOperations.java
@@ -0,0 +1,113 @@
+/*
+ * 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.gravitino.catalog.jdbc.operation;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+import org.apache.gravitino.catalog.jdbc.JdbcColumn;
+import org.apache.gravitino.rel.expressions.distributions.Distributions;
+import org.apache.gravitino.rel.expressions.transforms.Transform;
+import org.apache.gravitino.rel.indexes.Index;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/** Tests for {@link
org.apache.gravitino.catalog.jdbc.operation.SqliteTableOperations}. */
+public class TestSqliteTableOperations {
+ /**
+ * Tests that COMMENT ends with a semicolon and escapes quotes in comment.
+ *
+ * <p><b>Verify:</b>
+ *
+ * <ul>
+ * <li>Escapes quotes in comment
+ * <li>Ends with semicolon (;)
+ * </ul>
+ *
+ * <b>Expected:</b>
+ *
+ * <pre>
+ * CREATE TABLE test_table () COMMENT 'comment';
+ * </pre>
+ */
+ @Test
+ public void testGenerateCreateTableSqlWithCommentEndsWithSemicolon() {
+ SqliteTableOperations ops = new SqliteTableOperations();
+
+ String sql =
+ ops.generateCreateTableSql(
+ "test_table",
+ new JdbcColumn[0],
+ "comment",
+ null,
+ new Transform[0],
+ Distributions.NONE,
+ new Index[0]);
+
+ Assertions.assertTrue(
+ sql.trim().endsWith("COMMENT 'comment';"),
+ "Generated SQL should end with COMMENT 'comment';");
+ }
+
+ /**
+ * Tests that COMMENT appears before properties, properties keep order, and
the statement ends
+ * with a semicolon.
+ *
+ * <p><b>Verify:</b>
+ *
+ * <ul>
+ * <li>COMMENT before properties
+ * <li>Properties preserve insertion order
+ * <li>Ends with semicolon (;)
+ * </ul>
+ *
+ * <b>Expected:</b>
+ *
+ * <pre>
+ * CREATE TABLE test_table () COMMENT 'comment' k1=v1 k2=v2;
+ * </pre>
+ */
+ @Test
+ public void
testGenerateCreateTableSqlWithCommentAndPropertiesOrderAndSemicolon() {
+ SqliteTableOperations ops = new SqliteTableOperations();
+
+ Map<String, String> props = new LinkedHashMap<>();
+ props.put("k1", "v1");
+ props.put("k2", "v2");
+
+ String sql =
+ ops.generateCreateTableSql(
+ "test_table",
+ new JdbcColumn[0],
+ "comment",
+ props,
+ new Transform[0],
+ Distributions.NONE,
+ new Index[0]);
+
+ int idxComment = sql.indexOf(" COMMENT 'comment'");
+ int idxK1 = sql.indexOf(" k1=v1");
+ int idxK2 = sql.indexOf(" k2=v2");
+
+ Assertions.assertTrue(idxComment > 0, "COMMENT clause missing:\n" + sql);
+ Assertions.assertTrue(
+ idxK1 > idxComment && idxK2 > idxK1,
+ "COMMENT must come before properties and preserve order:\n" + sql);
+ Assertions.assertTrue(sql.trim().endsWith(";"), "SQL must end with a
semicolon:\n" + sql);
+ }
+}