InvisibleProgrammer commented on code in PR #6449:
URL: https://github.com/apache/hive/pull/6449#discussion_r3347369964


##########
iceberg/iceberg-catalog/src/main/java/org/apache/iceberg/hive/client/HiveRESTCatalogClient.java:
##########
@@ -178,38 +205,86 @@ public Database getDatabase(String catName, String 
dbName) throws NoSuchObjectEx
   @Override
   public Table getTable(GetTableRequest tableRequest) throws TException {
     validateCurrentCatalog(tableRequest.getCatName());
-    org.apache.iceberg.Table icebergTable;
+    TableIdentifier id =
+        TableIdentifier.of(tableRequest.getDbName(), 
tableRequest.getTblName());
     try {
-      icebergTable = 
restCatalog.loadTable(TableIdentifier.of(tableRequest.getDbName(),
-          tableRequest.getTblName()));
-    } catch (NoSuchTableException exception) {
+      org.apache.iceberg.Table icebergTable = restCatalog.loadTable(id);
+      return MetastoreUtil.toHiveTable(icebergTable, conf);
+    } catch (NoSuchTableException tableMissing) {
+      if (restCatalog instanceof ViewCatalog viewCatalog) {
+        try {
+          View icebergView = viewCatalog.loadView(id);
+          return MetastoreUtil.toHiveView(icebergView, conf);
+        } catch (NoSuchViewException viewMissing) {
+          throw new NoSuchObjectException();
+        }
+      }
       throw new NoSuchObjectException();
     }
-    return MetastoreUtil.toHiveTable(icebergTable, conf);
+  }
+
+  private static boolean hasIcebergNativeViewTableType(Table table) {
+    if (!TableType.VIRTUAL_VIEW.toString().equals(table.getTableType())) {
+      return false;
+    }
+    Map<String, String> params = table.getParameters();
+    if (params == null) {
+      return false;
+    }
+    return 
IcebergNativeLogicalViewSupport.ICEBERG_VIEW_HMS_TABLE_TYPE_VALUE.equals(
+        params.get(BaseMetastoreTableOperations.TABLE_TYPE_PROP));
+  }
+
+  @Override
+  public void alter_table(String catName, String dbName, String tblName, Table 
newTable,
+      EnvironmentContext envContext, String validWriteIdList) throws 
TException {
+    validateCurrentCatalog(catName);
+    if (hasIcebergNativeViewTableType(newTable) && restCatalog instanceof 
ViewCatalog) {
+      createOrReplaceLogicalView(newTable, dbName, tblName);
+    }
   }
 
   @Override
   public void createTable(CreateTableRequest request) throws TException {
     Table table = request.getTable();
+    if (hasIcebergNativeViewTableType(table) && restCatalog instanceof 
ViewCatalog) {
+      createOrReplaceLogicalView(table, table.getDbName(), 
table.getTableName());
+    } else {
+      List<FieldSchema> cols = Lists.newArrayList(table.getSd().getCols());
+      if (table.isSetPartitionKeys() && !table.getPartitionKeys().isEmpty()) {
+        cols.addAll(table.getPartitionKeys());
+      }
+      Properties tableProperties = 
IcebergTableProperties.getTableProperties(table, conf);
+      Schema schema = HiveSchemaUtil.convert(cols, Collections.emptyMap(), 
true);
+      Map<String, String> envCtxProps = 
Optional.ofNullable(request.getEnvContext())
+          .map(EnvironmentContext::getProperties)
+          .orElse(Collections.emptyMap());
+      org.apache.iceberg.PartitionSpec partitionSpec =
+          HMSTablePropertyHelper.getPartitionSpec(envCtxProps, schema);
+      SortOrder sortOrder = 
HMSTablePropertyHelper.getSortOrder(tableProperties, schema);
+
+      restCatalog.buildTable(TableIdentifier.of(table.getDbName(), 
table.getTableName()), schema)
+          .withPartitionSpec(partitionSpec)
+          
.withLocation(tableProperties.getProperty(IcebergTableProperties.LOCATION))
+          .withSortOrder(sortOrder)
+          .withProperties(Maps.fromProperties(tableProperties))
+          .create();
+    }
+  }
+
+  private void createOrReplaceLogicalView(Table table, String dbName, String 
tableName) {
+
     List<FieldSchema> cols = Lists.newArrayList(table.getSd().getCols());
     if (table.isSetPartitionKeys() && !table.getPartitionKeys().isEmpty()) {
       cols.addAll(table.getPartitionKeys());
     }
-    Properties tableProperties = 
IcebergTableProperties.getTableProperties(table, conf);
-    Schema schema = HiveSchemaUtil.convert(cols, Collections.emptyMap(), true);
-    Map<String, String> envCtxProps = 
Optional.ofNullable(request.getEnvContext())
-        .map(EnvironmentContext::getProperties)
-        .orElse(Collections.emptyMap());
-    org.apache.iceberg.PartitionSpec partitionSpec =
-        HMSTablePropertyHelper.getPartitionSpec(envCtxProps, schema);
-    SortOrder sortOrder = HMSTablePropertyHelper.getSortOrder(tableProperties, 
schema);
-
-    restCatalog.buildTable(TableIdentifier.of(table.getDbName(), 
table.getTableName()), schema)
-        .withPartitionSpec(partitionSpec)
-        
.withLocation(tableProperties.getProperty(IcebergTableProperties.LOCATION))
-        .withSortOrder(sortOrder)
-        .withProperties(Maps.fromProperties(tableProperties))
-        .create();
+
+    Map<String, String> tblProps =
+        table.getParameters() == null ? Maps.newHashMap() : 
Maps.newHashMap(table.getParameters());
+
+    String comment = tblProps.get("comment");
+    IcebergNativeLogicalViewSupport.createOrReplaceNativeView(
+        conf, dbName, tableName, cols, table.getViewExpandedText(), tblProps, 
comment);

Review Comment:
   The view text is pretty interesting difference between Hive and Iceberg: 
   Hive stores it in two versions: original and expanded text. 
   
   Iceberg has only one field, sql. 
   
   I think if we want to be compatible with both Hive and Iceberg, I would 
rather choose the original text here and add the expanded text as a 
tableproperty. 
   
   But also, there is a catch: the sql is stored in ViewVersion so it can 
differ for each version. But in Hive, it is a single property of the table 
metadata. 
   
   Honestly, I'm not sure what is the correct solution to do that but if we 
store only one of them, I fear we can loose functionality on Hive side. 



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to