voonhous commented on code in PR #19289:
URL: https://github.com/apache/hudi/pull/19289#discussion_r3594184146
##########
hudi-sync/hudi-hive-sync/src/main/java/org/apache/hudi/hive/util/HiveSchemaUtil.java:
##########
@@ -336,30 +338,62 @@ public static String generateSchemaString(HoodieSchema
storageSchema, List<Strin
}
public static String generateSchemaString(HoodieSchema storageSchema,
List<String> colsToSkip, boolean supportTimestamp) throws IOException {
+ return generateSchemaString(storageSchema, colsToSkip, supportTimestamp,
Collections.emptyMap());
+ }
+
+ public static String generateSchemaString(HoodieSchema storageSchema,
List<String> colsToSkip, boolean supportTimestamp,
+ Map<String, String> fieldDocs)
throws IOException {
Map<String, String> hiveSchema = convertSchemaToHiveSchema(storageSchema,
supportTimestamp);
StringBuilder columns = new StringBuilder();
for (Map.Entry<String, String> hiveSchemaEntry : hiveSchema.entrySet()) {
- if
(!colsToSkip.contains(removeSurroundingTick(hiveSchemaEntry.getKey()))) {
+ String fieldName = removeSurroundingTick(hiveSchemaEntry.getKey());
+ if (!colsToSkip.contains(fieldName)) {
columns.append(hiveSchemaEntry.getKey()).append(" ");
- columns.append(hiveSchemaEntry.getValue()).append(", ");
+ columns.append(hiveSchemaEntry.getValue());
+ String doc = fieldDocs.get(fieldName.toLowerCase(Locale.ROOT));
+ if (doc != null) {
+ columns.append(" COMMENT
'").append(escapeSqlComment(doc)).append("'");
+ }
+ columns.append(", ");
}
}
// Remove the last ", "
columns.delete(columns.length() - 2, columns.length());
return columns.toString();
}
+ private static String escapeSqlComment(String doc) {
+ // strip single quotes to avoid breaking the SQL string literal
+ return doc.replace("'", "");
Review Comment:
If a doc ends with a backslash this produces `COMMENT 'foo\'` and the
backslash eats the closing quote, so the DDL fails to parse - the same failure
this PR is fixing. Escaping instead of stripping avoids that and keeps the
quotes: `doc.replace("\\", "\\\\").replace("'", "\\'")`. It would also make hms
and jdbc/hiveql store the same comment when a doc has quotes in it.
##########
hudi-sync/hudi-hive-sync/src/main/java/org/apache/hudi/hive/ddl/HMSDDLExecutor.java:
##########
@@ -266,13 +274,11 @@ public void updateTableComments(String tableName,
Map<String, Pair<String, Strin
try {
Table table = client.getTable(databaseName, tableName);
StorageDescriptor sd = new StorageDescriptor(table.getSd());
- for (FieldSchema fieldSchema : sd.getCols()) {
- if (alterSchema.containsKey(fieldSchema.getName())) {
- String comment = alterSchema.get(fieldSchema.getName()).getRight();
- fieldSchema.setComment(comment);
- }
- }
+ setFieldComments(sd.getCols(), alterSchema);
table.setSd(sd);
+ if (table.getPartitionKeys() != null) {
Review Comment:
nit: thrift returns an empty list here, not null, so the guard can go.
##########
hudi-sync/hudi-hive-sync/src/main/java/org/apache/hudi/hive/util/HiveSchemaUtil.java:
##########
@@ -336,30 +338,62 @@ public static String generateSchemaString(HoodieSchema
storageSchema, List<Strin
}
public static String generateSchemaString(HoodieSchema storageSchema,
List<String> colsToSkip, boolean supportTimestamp) throws IOException {
+ return generateSchemaString(storageSchema, colsToSkip, supportTimestamp,
Collections.emptyMap());
+ }
+
+ public static String generateSchemaString(HoodieSchema storageSchema,
List<String> colsToSkip, boolean supportTimestamp,
+ Map<String, String> fieldDocs)
throws IOException {
Map<String, String> hiveSchema = convertSchemaToHiveSchema(storageSchema,
supportTimestamp);
StringBuilder columns = new StringBuilder();
for (Map.Entry<String, String> hiveSchemaEntry : hiveSchema.entrySet()) {
- if
(!colsToSkip.contains(removeSurroundingTick(hiveSchemaEntry.getKey()))) {
+ String fieldName = removeSurroundingTick(hiveSchemaEntry.getKey());
+ if (!colsToSkip.contains(fieldName)) {
columns.append(hiveSchemaEntry.getKey()).append(" ");
- columns.append(hiveSchemaEntry.getValue()).append(", ");
+ columns.append(hiveSchemaEntry.getValue());
+ String doc = fieldDocs.get(fieldName.toLowerCase(Locale.ROOT));
+ if (doc != null) {
+ columns.append(" COMMENT
'").append(escapeSqlComment(doc)).append("'");
+ }
+ columns.append(", ");
}
}
// Remove the last ", "
columns.delete(columns.length() - 2, columns.length());
return columns.toString();
}
+ private static String escapeSqlComment(String doc) {
+ // strip single quotes to avoid breaking the SQL string literal
+ return doc.replace("'", "");
+ }
+
+ /**
+ * Returns the doc of each field of the schema keyed by lower-cased field
name, fields without doc are skipped.
+ */
+ public static Map<String, String> getFieldDocs(HoodieSchema schema) {
+ return schema.getFields().stream()
+ .filter(field -> field.doc().map(doc -> !doc.isEmpty()).orElse(false))
+ .collect(Collectors.toMap(field ->
field.name().toLowerCase(Locale.ROOT), field -> field.doc().get(), (existing,
duplicate) -> existing));
Review Comment:
nit: two fields colliding case-insensitively silently keeps the first doc.
Fine in practice since Hive lowercases everything, but maybe say so in the
javadoc.
##########
hudi-sync/hudi-hive-sync/src/main/java/org/apache/hudi/hive/ddl/QueryBasedDDLExecutor.java:
##########
@@ -138,8 +141,16 @@ public void updatePartitionsToTable(String tableName,
List<String> changedPartit
@Override
public void updateTableComments(String tableName, Map<String, Pair<String,
String>> newSchema) {
+ Set<String> partitionFields =
config.getSplitStrings(META_SYNC_PARTITION_FIELDS).stream()
+ .map(partitionField -> partitionField.toLowerCase(Locale.ROOT))
+ .collect(Collectors.toSet());
for (Map.Entry<String, Pair<String,String>> field : newSchema.entrySet()) {
String name = field.getKey();
+ // ALTER TABLE ... CHANGE COLUMN fails on partition columns; only HMS
sync mode can update their comments
+ if (partitionFields.contains(name.toLowerCase(Locale.ROOT))) {
+ log.warn("Skipping comment update for partition column {} of table {}:
not supported in sync mode jdbc/hiveql, use hms sync mode instead", name,
tableName);
Review Comment:
The diff is built in HoodieHiveSyncClient before this check, so a doced
partition column logs this warn on every sync forever, and updateTableComments
up there still returns true even when everything got skipped here. Might be
cleaner to filter partition fields out of the diff at the client level for the
query-based executors.
##########
hudi-sync/hudi-hive-sync/src/test/java/org/apache/hudi/hive/TestHiveSyncTool.java:
##########
@@ -1032,6 +1032,67 @@ public void testSyncWithCommentedSchema(String syncMode)
throws Exception {
assertEquals(2, commentCnt, "hive schema field comment numbers should
match the avro schema field doc numbers");
}
+ @ParameterizedTest
+ @MethodSource("syncMode")
+ public void testSyncCommentsForPartitionColumns(String syncMode) throws
Exception {
+ hiveSyncProps.setProperty(HIVE_SYNC_MODE.key(), syncMode);
+ hiveSyncProps.setProperty(HIVE_SYNC_COMMENT.key(), "true");
+ String commitTime = "100";
+ HiveTestUtil.createCOWTableWithSchema(commitTime,
"/partition-doced-test.avsc");
+
+ reInitHiveSyncClient();
+ reSyncHiveTable();
+
+ Map<String, String> commentsByField =
hiveClient.getMetastoreFieldSchemas(HiveTestUtil.TABLE_NAME)
+ .stream()
+ .collect(Collectors.toMap(FieldSchema::getName,
FieldSchema::getCommentOrEmpty));
+ assertEquals("name_comment", commentsByField.get("name"),
+ "comment of a regular column should be synced on table creation");
+ assertEquals("partition_datestr_comment", commentsByField.get("datestr"),
+ "comment of the partition column should be synced on table creation");
+
+ SessionState.start(HiveTestUtil.getHiveConf());
+ Driver hiveDriver = new
org.apache.hadoop.hive.ql.Driver(HiveTestUtil.getHiveConf());
Review Comment:
nit: `Driver` is already imported, the fully qualified name can go.
##########
hudi-sync/hudi-hive-sync/src/test/resources/partition-doced-test.avsc:
##########
@@ -0,0 +1,28 @@
+/*
+ * 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.
+ */
+{
+"namespace": "example.avro",
+ "type": "record",
+ "name": "User",
+ "fields": [
+ {"name": "name", "type": "string","doc":"name_comment"},
Review Comment:
Can one of these docs get a single quote or a trailing backslash? Nothing
exercises escapeSqlComment through the jdbc/hiveql create path right now, and
that is exactly where the escaping matters.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]