laserninja commented on code in PR #10757:
URL: https://github.com/apache/gravitino/pull/10757#discussion_r3140572873


##########
trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/GravitinoMetadata.java:
##########
@@ -220,6 +221,46 @@ public void createTable(
     catalogConnectorMetadata.createTable(table, saveMode == SaveMode.IGNORE);
   }
 
+  @Override
+  public ConnectorOutputTableHandle beginCreateTable(
+      ConnectorSession session,
+      ConnectorTableMetadata tableMetadata,
+      Optional<ConnectorTableLayout> layout,
+      RetryMode retryMode,
+      boolean noExistingData) {
+    // First, create the table in the Gravitino catalog
+    GravitinoTable table = metadataAdapter.createTable(tableMetadata);
+    catalogConnectorMetadata.createTable(table, false);
+
+    // Get the table handle from the internal connector for the newly created 
table
+    SchemaTableName tableName = tableMetadata.getTable();
+    ConnectorTableHandle internalTableHandle =
+        internalMetadata.getTableHandle(session, tableName, Optional.empty(), 
Optional.empty());
+
+    // Delegate to the internal connector's insert path to write data,
+    // avoiding double table creation in the original connector
+    List<ColumnHandle> columns =
+        new ArrayList<>(internalMetadata.getColumnHandles(session, 
internalTableHandle).values());

Review Comment:
   Fixed. Changed from iterating getColumnHandles().values() (unordered Map) to 
iterating tableMetadata.getColumns() and looking up each column handle by name, 
ensuring deterministic column ordering that matches the table definition.



##########
trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/GravitinoMetadata.java:
##########
@@ -220,6 +221,46 @@ public void createTable(
     catalogConnectorMetadata.createTable(table, saveMode == SaveMode.IGNORE);
   }
 
+  @Override
+  public ConnectorOutputTableHandle beginCreateTable(
+      ConnectorSession session,
+      ConnectorTableMetadata tableMetadata,
+      Optional<ConnectorTableLayout> layout,
+      RetryMode retryMode,
+      boolean noExistingData) {
+    // First, create the table in the Gravitino catalog
+    GravitinoTable table = metadataAdapter.createTable(tableMetadata);
+    catalogConnectorMetadata.createTable(table, false);
+
+    // Get the table handle from the internal connector for the newly created 
table
+    SchemaTableName tableName = tableMetadata.getTable();
+    ConnectorTableHandle internalTableHandle =
+        internalMetadata.getTableHandle(session, tableName, Optional.empty(), 
Optional.empty());
+
+    // Delegate to the internal connector's insert path to write data,
+    // avoiding double table creation in the original connector
+    List<ColumnHandle> columns =
+        new ArrayList<>(internalMetadata.getColumnHandles(session, 
internalTableHandle).values());
+    ConnectorInsertTableHandle insertTableHandle =
+        internalMetadata.beginInsert(session, internalTableHandle, columns, 
retryMode);
+    return new GravitinoOutputTableHandle(insertTableHandle, tableName);

Review Comment:
   Added a try/catch block around the getTableHandle/beginInsert sequence, if 
either fails after the table has been created in Gravitino, we now call 
catalogConnectorMetadata.dropTable(tableName) to clean up the orphaned table 
before re-throwing the exception. Also added a null check for 
internalTableHandle to handle the case where getTableHandle returns null.



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