singhpk234 commented on a change in pull request #4401:
URL: https://github.com/apache/iceberg/pull/4401#discussion_r835810512



##########
File path: 
spark/v3.2/spark-extensions/src/test/java/org/apache/iceberg/spark/extensions/TestGenerateSymlinkFormatManifestsProcedure.java
##########
@@ -0,0 +1,153 @@
+/*
+ * 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.
+ */
+
+package org.apache.iceberg.spark.extensions;
+
+import java.net.URI;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+import org.apache.iceberg.AssertHelpers;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestGenerateSymlinkFormatManifestsProcedure extends 
SparkExtensionsTestBase {
+
+  public TestGenerateSymlinkFormatManifestsProcedure(
+      String catalogName, String implementation, Map<String, String> config) {
+    super(catalogName, implementation, config);
+  }
+
+  @After
+  public void removeTables() throws Exception {
+    sql("DROP TABLE IF EXISTS %s", tableName);
+  }
+
+  @Test
+  public void testGenerateSymlinkFormatManifestsEmptyTable() {
+    sql("CREATE TABLE %s (id bigint NOT NULL, data string) USING iceberg", 
tableName);
+    AssertHelpers.assertThrows("Should not support generate symlink manifest 
for empty table",
+        ValidationException.class,
+        "Cannot generate symlink manifests for an empty table",
+        () -> sql("CALL %s.system.generate_symlink_format_manifest('%s')", 
catalogName, tableIdent));
+  }
+
+  @Test
+  public void testGenerateSymlinkFormatManifestsUnpartitioned() {
+    sql("CREATE TABLE %s (id bigint NOT NULL, data string) USING iceberg", 
tableName);
+    sql("INSERT INTO TABLE %s VALUES (1, 'a')", tableName);
+    sql("INSERT INTO TABLE %s VALUES (2, 'b')", tableName);
+    List<Object[]> result = sql("CALL 
%s.system.generate_symlink_format_manifest('%s')", catalogName, tableIdent);
+    Table table = validationCatalog.loadTable(tableIdent);
+    List<Object[]> expected = Lists.newArrayList();
+    expected.add(row(table.currentSnapshot().snapshotId(), 2L));
+    assertEquals("Should find 2 files", expected, result);
+    checkDirectoryExists(table.location(), "_symlink_format_manifest");
+    checkDirectoryExists(table.location(), "_symlink_format_manifest",
+        Long.toString(table.currentSnapshot().snapshotId()));
+  }
+
+  @Test
+  public void testGenerateSymlinkFormatManifestsPartitioned() {
+    sql("CREATE TABLE %s (id bigint NOT NULL, data string) USING iceberg 
PARTITIONED BY (data)", tableName);
+    sql("INSERT INTO TABLE %s VALUES (1, 'a'), (1, 'b'), (1, 'c')", tableName);
+    sql("INSERT INTO TABLE %s VALUES (2, 'b'), (2, 'c'), (2, 'd')", tableName);
+    List<Object[]> result = sql("CALL 
%s.system.generate_symlink_format_manifest('%s')", catalogName, tableIdent);
+    Table table = validationCatalog.loadTable(tableIdent);
+    List<Object[]> expected = Lists.newArrayList();
+    expected.add(row(table.currentSnapshot().snapshotId(), 6L));
+    assertEquals("Should find 6 files", expected, result);
+    checkDirectoryExists(table.location(), "_symlink_format_manifest");
+    checkDirectoryExists(table.location(), "_symlink_format_manifest",
+        Long.toString(table.currentSnapshot().snapshotId()));
+    checkDirectoryExists(table.location(), "_symlink_format_manifest",
+        Long.toString(table.currentSnapshot().snapshotId()), "data=a");
+    checkDirectoryExists(table.location(), "_symlink_format_manifest",
+        Long.toString(table.currentSnapshot().snapshotId()), "data=b");
+    checkDirectoryExists(table.location(), "_symlink_format_manifest",
+        Long.toString(table.currentSnapshot().snapshotId()), "data=c");
+    checkDirectoryExists(table.location(), "_symlink_format_manifest",
+        Long.toString(table.currentSnapshot().snapshotId()), "data=d");
+  }
+
+  @Test
+  public void testGenerateSymlinkFormatManifestsHiddenPartitioned() {
+    sql("CREATE TABLE %s (id bigint NOT NULL, data string) USING iceberg " +
+        "PARTITIONED BY (bucket(16, data))", tableName);
+    sql("INSERT INTO TABLE %s VALUES (1, 'a'), (1, 'b'), (1, 'c')", tableName);
+    sql("INSERT INTO TABLE %s VALUES (2, 'b'), (2, 'c'), (2, 'd')", tableName);
+    List<Object[]> result = sql("CALL 
%s.system.generate_symlink_format_manifest('%s')", catalogName, tableIdent);
+    Table table = validationCatalog.loadTable(tableIdent);
+    String location = table.location();
+    List<Object[]> expected = Lists.newArrayList();
+    expected.add(row(table.currentSnapshot().snapshotId(), 6L));
+    assertEquals("Should find 6 files", expected, result);
+    checkDirectoryExists(table.location(), "_symlink_format_manifest");
+    checkDirectoryExists(table.location(), "_symlink_format_manifest",
+        Long.toString(table.currentSnapshot().snapshotId()));
+    checkDirectoryExists(table.location(), "_symlink_format_manifest",
+        Long.toString(table.currentSnapshot().snapshotId()), "data_bucket=15");
+    checkDirectoryExists(table.location(), "_symlink_format_manifest",
+        Long.toString(table.currentSnapshot().snapshotId()), "data_bucket=2");
+    checkDirectoryExists(table.location(), "_symlink_format_manifest",
+        Long.toString(table.currentSnapshot().snapshotId()), "data_bucket=3");
+  }
+
+  @Test
+  public void 
testGenerateSymlinkFormatManifestsCustomLocationPositionArgument() {
+    sql("CREATE TABLE %s (id bigint NOT NULL, data string) USING iceberg", 
tableName);
+    sql("INSERT INTO TABLE %s VALUES (1, 'a')", tableName);
+    sql("INSERT INTO TABLE %s VALUES (2, 'b')", tableName);
+    Table table = validationCatalog.loadTable(tableIdent);
+    String customLocation = table.location() + "/" + UUID.randomUUID();
+    List<Object[]> result = sql("CALL 
%s.system.generate_symlink_format_manifest('%s', '%s')",
+        catalogName, tableIdent, customLocation);
+    List<Object[]> expected = Lists.newArrayList();
+    expected.add(row(table.currentSnapshot().snapshotId(), 2L));
+    assertEquals("Should find 2 files", expected, result);
+    checkDirectoryExists(customLocation);
+  }
+
+  @Test
+  public void testGenerateSymlinkFormatManifestsCustomLocationNamedArgument() {
+    sql("CREATE TABLE %s (id bigint NOT NULL, data string) USING iceberg", 
tableName);
+    sql("INSERT INTO TABLE %s VALUES (1, 'a')", tableName);
+    sql("INSERT INTO TABLE %s VALUES (2, 'b')", tableName);
+    Table table = validationCatalog.loadTable(tableIdent);
+    String customLocation = table.location() + "/" + UUID.randomUUID();
+    List<Object[]> result = sql(
+        "CALL %s.system.generate_symlink_format_manifest(symlink_root_location 
=> '%s', table => '%s')",
+        catalogName, customLocation, tableIdent);
+    List<Object[]> expected = Lists.newArrayList();
+    expected.add(row(table.currentSnapshot().snapshotId(), 2L));
+    assertEquals("Should find 2 files", expected, result);
+    checkDirectoryExists(customLocation);
+  }
+
+  private void checkDirectoryExists(String... paths) {
+    String path = String.join("/", paths);
+    Assert.assertTrue("Directory should exist: " + path, 
Files.isDirectory(Paths.get(URI.create(path))));
+  }

Review comment:
       should we also check the symlink folder when being used as the external 
table is return's same result as iceberg table ? WDYT

##########
File path: 
spark/v3.2/spark/src/main/java/org/apache/iceberg/spark/procedures/GenerateSymlinkFormatManifestProcedure.java
##########
@@ -0,0 +1,142 @@
+/*
+ * 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.
+ */
+
+package org.apache.iceberg.spark.procedures;
+
+import java.util.Arrays;
+import org.apache.iceberg.MetadataTableType;
+import org.apache.iceberg.Partitioning;
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.spark.Spark3Util;
+import org.apache.iceberg.spark.SparkTableUtil;
+import org.apache.iceberg.spark.source.SparkTable;
+import org.apache.iceberg.types.Types;
+import org.apache.spark.sql.Dataset;
+import org.apache.spark.sql.Row;
+import org.apache.spark.sql.catalyst.InternalRow;
+import org.apache.spark.sql.connector.catalog.CatalogPlugin;
+import org.apache.spark.sql.connector.catalog.TableCatalog;
+import org.apache.spark.sql.connector.iceberg.catalog.ProcedureParameter;
+import org.apache.spark.sql.types.DataTypes;
+import org.apache.spark.sql.types.Metadata;
+import org.apache.spark.sql.types.StructField;
+import org.apache.spark.sql.types.StructType;
+
+class GenerateSymlinkFormatManifestProcedure extends BaseProcedure {
+  private static final ProcedureParameter[] PARAMETERS = new 
ProcedureParameter[]{
+      ProcedureParameter.required("table", DataTypes.StringType),
+      ProcedureParameter.optional("symlink_root_location", 
DataTypes.StringType)
+  };
+
+  private static final StructType OUTPUT_TYPE = new StructType(new 
StructField[]{
+      new StructField("snapshot_id", DataTypes.LongType, false, 
Metadata.empty()),
+      new StructField("data_file_count", DataTypes.LongType, false, 
Metadata.empty())
+  });
+
+  private static final String PROCEDURE_CONTEXT = "generate symlink format 
manifest";
+  private static final String SYMLINK_DIRECTORY_DEFAULT = 
"_symlink_format_manifest";
+
+  private GenerateSymlinkFormatManifestProcedure(TableCatalog tableCatalog) {
+    super(tableCatalog);
+  }
+
+  public static SparkProcedures.ProcedureBuilder builder() {
+    return new Builder<GenerateSymlinkFormatManifestProcedure>() {
+      @Override
+      protected GenerateSymlinkFormatManifestProcedure doBuild() {
+        return new GenerateSymlinkFormatManifestProcedure(tableCatalog());
+      }
+    };
+  }
+
+  @Override
+  public ProcedureParameter[] parameters() {
+    return PARAMETERS;
+  }
+
+  @Override
+  public StructType outputType() {
+    return OUTPUT_TYPE;
+  }
+
+  @Override
+  public InternalRow[] call(InternalRow args) {
+    String tableIdent = args.getString(0);
+    Preconditions.checkArgument(tableIdent != null && !tableIdent.isEmpty(),
+        "Cannot handle an empty identifier for argument table");
+
+    CatalogPlugin defaultCatalog = 
spark().sessionState().catalogManager().currentCatalog();
+    Spark3Util.CatalogAndIdentifier catalogAndIdent = 
Spark3Util.catalogAndIdentifier(
+        PROCEDURE_CONTEXT, spark(), tableIdent, defaultCatalog);
+    SparkTable sparkTable = loadSparkTable(catalogAndIdent.identifier());
+    org.apache.iceberg.Table icebergTable = sparkTable.table();
+    ValidationException.check(icebergTable.currentSnapshot() != null,
+        "Cannot generate symlink manifests for an empty table");
+
+    long snapshotId = icebergTable.currentSnapshot().snapshotId();
+    String symlinkRootLocation = args.isNullAt(1) ?
+        defaultSymlinkManifestRootLocation(icebergTable.location(), 
snapshotId) : args.getString(1);
+
+    Types.StructType partitionType = Partitioning.partitionType(icebergTable);
+    Dataset<Row> entries = SparkTableUtil.loadCatalogMetadataTable(spark(), 
icebergTable, MetadataTableType.ENTRIES)
+        .filter("status < 2 AND data_file.content = 0");

Review comment:
       we should cache the DF as well, otherwise we will end up scanning the 
metaDataTable twice. in :
   1. count()
   2. write()




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