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


##########
trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/GravitinoMetadata.java:
##########
@@ -223,6 +224,72 @@ 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);
+
+    SchemaTableName tableName = tableMetadata.getTable();
+    try {
+      // Get the table handle from the internal connector for the newly 
created table
+      ConnectorTableHandle internalTableHandle =
+          internalMetadata.getTableHandle(session, tableName, 
Optional.empty(), Optional.empty());
+      if (internalTableHandle == null) {
+        throw new TrinoException(
+            GRAVITINO_TABLE_NOT_EXISTS,
+            "Internal connector could not find newly created table: " + 
tableName);
+      }
+
+      // Build column list in the same order as tableMetadata to preserve 
column ordering
+      Map<String, ColumnHandle> internalColumnHandles =
+          internalMetadata.getColumnHandles(session, internalTableHandle);
+      List<ColumnHandle> columns = new 
ArrayList<>(tableMetadata.getColumns().size());
+      for (ColumnMetadata columnMetadata : tableMetadata.getColumns()) {
+        ColumnHandle handle = 
internalColumnHandles.get(columnMetadata.getName());
+        if (handle == null) {
+          throw new TrinoException(
+              GRAVITINO_COLUMN_NOT_EXISTS,
+              "Column '"
+                  + columnMetadata.getName()
+                  + "' not found in internal connector for table: "
+                  + tableName);
+        }
+        columns.add(handle);
+      }
+
+      // Delegate to the internal connector's insert path to write data,
+      // avoiding double table creation in the original connector
+      ConnectorInsertTableHandle insertTableHandle =
+          internalMetadata.beginInsert(session, internalTableHandle, columns, 
retryMode);
+      return new GravitinoOutputTableHandle(insertTableHandle, tableName);
+    } catch (Exception e) {
+      // Clean up the table created in the Gravitino catalog on failure
+      catalogConnectorMetadata.dropTable(tableName);

Review Comment:
   its is now wrapped in try/catch with LOG.warn so cleanup failures don't mask 
the original exception.



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