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


##########
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:
   This is partially because of oddities between what can be done with V2 
functions in the session catalog and elsewhere (non-session catalogs).
   
   I addded the tests in these styles because this is how the storage 
partitioned joins function resolution actually happens. The function is created 
with a "fake" input Schema to addd `numBuckets` and then called through this 
pass.
   
   And then not testing certain other things through plain SQL is because of 
limitations / differences in what can be done in the session catalog vs 
elsewhere. Per the Spark 3.3 tests for example, only the session catalog can 
use describe (and my own findings).
   
   I'll try to refactor to be more in line with your thought though as I don't 
disagree 👍 



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