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


##########
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:
   ```
   scalarSQL("DESCRIBE FUNCTION iceberg_version");
   ```
   
   class org.apache.iceberg.spark.functions.IcebergVersionFunction cannot be 
cast to class org.apache.spark.sql.internal.connector.V1Function 
(org.apache.iceberg.spark.functions.IcebergVersionFunction and 
org.apache.spark.sql.internal.connector.V1Function are in unnamed module of 
loader 'app')
   java.lang.ClassCastException: class 
org.apache.iceberg.spark.functions.IcebergVersionFunction cannot be cast to 
class org.apache.spark.sql.internal.connector.V1Function 
(org.apache.iceberg.spark.functions.IcebergVersionFunction and 
org.apache.spark.sql.internal.connector.V1Function are in unnamed module of 
loader 'app')
        at 
org.apache.spark.sql.catalyst.analysis.ResolveSessionCatalog$$anonfun$apply$1.applyOrElse(ResolveSessionCatalog.scala:389)
        at 
org.apache.spark.sql.catalyst.analysis.ResolveSessionCatalog$$anonfun$apply$1.applyOrElse(ResolveSessionCatalog.scala:49)
        at 
org.apache.spark.sql.catalyst.plans.logical.AnalysisHelper.$anonfun$resolveOperatorsUpWithPruning$3(AnalysisHelper.scala:138)
   
   So it seems that we can't `DESCRIBE` on a non-v1 function from within the 
tests. The tests in Spark also support this idea.
   
   So calling the function directly with code is the best it seems we can do 
for those, but the function resolution (e.e. `SELECT system.iceberg_version` 
could be moved over here and we could get rid of `TestIcebergVersionFunction`.



-- 
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: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to