rdblue commented on code in PR #5377:
URL: https://github.com/apache/iceberg/pull/5377#discussion_r932749591


##########
spark/v3.3/spark/src/test/java/org/apache/iceberg/spark/TestFunctionCatalog.java:
##########
@@ -0,0 +1,165 @@
+/*
+ * 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;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+import org.apache.iceberg.AssertHelpers;
+import org.apache.iceberg.IcebergBuild;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.spark.functions.SparkFunctions;
+import org.apache.spark.sql.AnalysisException;
+import org.apache.spark.sql.catalyst.InternalRow;
+import org.apache.spark.sql.catalyst.analysis.NoSuchFunctionException;
+import org.apache.spark.sql.catalyst.analysis.NoSuchNamespaceException;
+import org.apache.spark.sql.connector.catalog.FunctionCatalog;
+import org.apache.spark.sql.connector.catalog.Identifier;
+import org.apache.spark.sql.connector.catalog.functions.ScalarFunction;
+import org.apache.spark.sql.connector.catalog.functions.UnboundFunction;
+import org.apache.spark.sql.types.StructType;
+import org.assertj.core.api.Assertions;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+@RunWith(Parameterized.class)
+public class TestFunctionCatalog extends SparkCatalogTestBase {
+  @Parameterized.Parameters(name = "catalogConfig = {0}")
+  public static Object[][] parameters() {
+    return new Object[][] {
+      {SparkCatalogConfig.HADOOP}, {SparkCatalogConfig.HIVE}, 
{SparkCatalogConfig.SPARK}
+    };
+  }
+
+  private static final Namespace NS = Namespace.of("db");
+  private final boolean isSessionCatalog;
+  private final String fullNamespace;
+  private final FunctionCatalog asFunctionCatalog;
+
+  public TestFunctionCatalog(SparkCatalogConfig catalogConfig) {
+    super(catalogConfig);
+    this.isSessionCatalog = "spark_catalog".equals(catalogName);
+    this.fullNamespace = (isSessionCatalog ? "" : catalogName + ".") + NS;
+    this.asFunctionCatalog = castToFunctionCatalog(catalogName);
+  }
+
+  @Before
+  public void createNamespace() {
+    sql("CREATE NAMESPACE IF NOT EXISTS %s", fullNamespace);
+  }
+
+  @After
+  public void cleanNamespaces() {
+    sql("DROP NAMESPACE IF EXISTS %s", fullNamespace);
+  }
+
+  @Test
+  public void testLoadListAndUseFunctionsFromSystemNamespace()
+      throws NoSuchFunctionException, NoSuchNamespaceException {
+    // Session catalog requires that the namespace actually exists
+    if (isSessionCatalog) {
+      sql("CREATE NAMESPACE IF NOT EXISTS system");
+    }
+
+    try {
+      String[] namespace = {"system"};
+      String name = "iceberg_version";
+      Identifier identifier = Identifier.of(namespace, name);
+
+      assertListingLoadingAndBindingFrom(identifier);
+    } finally {
+      if (isSessionCatalog) {
+        sql("DROP NAMESPACE IF EXISTS system");
+      }
+    }
+  }
+
+  @Test
+  public void testLoadListAndUseFunctionsFromEmptyNamespace()
+      throws NoSuchFunctionException, NoSuchNamespaceException {
+    String[] namespace = {};
+    String name = "iceberg_version";
+    Identifier identifier = Identifier.of(namespace, name);
+
+    assertListingLoadingAndBindingFrom(identifier);
+  }
+
+  @Test
+  public void testCannotLoadFunctionsFromInvalidNamespace() {
+    AssertHelpers.assertThrows(
+        "Function Catalog functions should only be accessible from the system 
namespace and empty namespace",
+        AnalysisException.class,
+        "Undefined function",
+        () -> sql("SELECT %s.iceberg_version()", fullNamespace));
+  }
+
+  @Test
+  public void testCannotUseUndefinedFunction() {
+    AssertHelpers.assertThrows(
+        "Using an undefined function should throw",
+        AnalysisException.class,
+        "Undefined function",
+        () -> sql("SELECT undefined_function(1, 2)"));
+  }
+
+  private void assertListingLoadingAndBindingFrom(Identifier identifier)

Review Comment:
   What is the value of testing all of these things directly? I would expect 
these tests to run SQL, like `SELECT iceberg_version()` and verify the result.
   
   Think about what you need to validate in generic catalog-level tests. You 
need to validate that you can load and call a function, assuming that it works 
properly. You don't need to test the function itself (unbound to bound, for 
example) because that isn't the goal of this suite.
   
   I think that this suite should verify, using SQL, that you can call the 
`iceberg_version` method and it shows up in listings. You can also verify that 
it is in both `system` and the empty namespace. Beyond that, I don't think this 
needs to do much.



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