mchades commented on code in PR #11926:
URL: https://github.com/apache/gravitino/pull/11926#discussion_r4292954315


##########
trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/util/GeneralDataTypeTransformer.java:
##########
@@ -177,14 +179,29 @@ private RowType 
gravitinoRowTypeToTrinoRowType(Types.StructType structType) {
   }
 
   private StructType trinoRowTypeToGravitinoRowType(RowType rowType) {
-    StructType.Field[] fields =
-        rowType.getFields().stream()
-            .map(
-                field ->
-                    StructType.Field.nullableField(
-                        field.getName().get(), 
getGravitinoType(field.getType())))
-            .toArray(StructType.Field[]::new);
-
+    List<Field> trinoFields = rowType.getFields();
+    Set<String> usedNames = new HashSet<>();
+    for (Field trinoField : trinoFields) {
+      trinoField.getName().ifPresent(usedNames::add);
+    }
+    StructType.Field[] fields = new StructType.Field[trinoFields.size()];
+    int anonymousSeq = 0;
+    for (int i = 0; i < trinoFields.size(); i++) {
+      Field trinoField = trinoFields.get(i);
+      String name;
+      if (trinoField.getName().isPresent()) {
+        name = trinoField.getName().get();
+      } else {
+        // Trino allows anonymous row fields (e.g. ROW(1, 'x') with no AS), 
but Gravitino's
+        // StructType.Field requires a name, so synthesize one that cannot 
collide with an
+        // explicitly-named sibling field (e.g. ROW(1, 'x' AS field0)).
+        do {
+          name = "field" + anonymousSeq++;
+        } while (usedNames.contains(name));

Review Comment:
   **[P2] Avoid case-insensitive row field collisions.** For `CREATE VIEW v AS 
SELECT CAST(ROW(1, 'x') AS ROW(INTEGER, "FIELD0" VARCHAR(1))) AS r`, this loop 
generates `field0`, so the persisted type contains both `field0` and `FIELD0`. 
I reproduced that round trip with an in-memory Hive client and Iceberg 
`InMemoryCatalog` on `f0ebaa38`. [Trino resolves row fields 
case-insensitively](https://github.com/trinodb/trino/blob/440/core/trino-main/src/main/java/io/trino/sql/analyzer/ExpressionAnalyzer.java#L869-L876),
 so `SELECT r.FIELD0 FROM v` becomes ambiguous. Please reserve names 
case-insensitively while preserving explicit names, and add a regression test 
for this mixed anonymous/uppercase case.



-- 
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]

Reply via email to