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


##########
spark/v3.3/spark/src/test/java/org/apache/iceberg/spark/TestFunctionCatalog.java:
##########
@@ -0,0 +1,129 @@
+/*
+ * 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 org.apache.iceberg.AssertHelpers;
+import org.apache.iceberg.IcebergBuild;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.apache.iceberg.spark.functions.IcebergVersionFunction;
+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.BoundFunction;
+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.apache.spark.unsafe.types.UTF8String;
+import org.assertj.core.api.Assertions;
+import org.junit.Assert;
+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}};
+  }
+
+  private static final String[] EMPTY_NAMESPACE = new String[] {};
+  private static final String[] SYSTEM_NAMESPACE = new String[] {"system"};
+  private final FunctionCatalog asFunctionCatalog;
+
+  public TestFunctionCatalog(SparkCatalogConfig catalogConfig) {
+    super(catalogConfig);
+    this.asFunctionCatalog = castToFunctionCatalog(catalogName);
+  }
+
+  @Test
+  public void testListFunctionsViaCatalog() throws NoSuchNamespaceException {
+    Assertions.assertThat(asFunctionCatalog.listFunctions(EMPTY_NAMESPACE))
+        .anyMatch(func -> "iceberg_version".equalsIgnoreCase(func.name()));
+
+    Assertions.assertThat(asFunctionCatalog.listFunctions(SYSTEM_NAMESPACE))
+        .anyMatch(func -> "iceberg_version".equalsIgnoreCase(func.name()));
+  }
+
+  @Test
+  public void testLoadAndDescribeUnboundFunctionsViaCatalog() throws 
NoSuchFunctionException {
+    for (String[] namespace : ImmutableList.of(EMPTY_NAMESPACE, 
SYSTEM_NAMESPACE)) {
+      Identifier identifier = Identifier.of(namespace, "iceberg_version");
+      UnboundFunction func = asFunctionCatalog.loadFunction(identifier);
+
+      
Assertions.assertThat(func).isExactlyInstanceOf(IcebergVersionFunction.class);
+      
Assertions.assertThat(func.name()).asString().isEqualTo("iceberg_version");
+      Assertions.assertThat(func.description())
+          .containsIgnoringCase("Returns the runtime Iceberg version");
+    }
+  }
+
+  @Test
+  public void testBindAndUseFunctionsViaCatalog() throws 
NoSuchFunctionException {
+    for (String[] namespace : ImmutableList.of(EMPTY_NAMESPACE, 
SYSTEM_NAMESPACE)) {
+      Identifier identifier = Identifier.of(namespace, "iceberg_version");
+      UnboundFunction unboundFunc = asFunctionCatalog.loadFunction(identifier);
+
+      BoundFunction boundFunc = unboundFunc.bind(new StructType());
+      Assertions.assertThat(boundFunc.getClass().toString())
+          .containsIgnoringCase("IcebergVersionFunctionImpl");
+      
Assertions.assertThat(boundFunc.canonicalName()).isEqualTo("iceberg.iceberg_version");
+      Assertions.assertThat(boundFunc.isResultNullable()).isFalse();
+      Assertions.assertThat(boundFunc.isDeterministic()).isTrue();
+
+      // Ensure the bound result produces the expected UTF8String type.
+      ScalarFunction<UTF8String> scalarFunc = (ScalarFunction<UTF8String>) 
boundFunc;
+      Assert.assertEquals(
+          UTF8String.fromString(IcebergBuild.version()),
+          scalarFunc.produceResult(InternalRow.empty()));
+    }
+  }
+
+  @Test
+  public void testCannotLoadFunctionsFromInvalidNamespace() {
+    String dbNamespace = catalogName + ".db";
+    try {
+      sql("CREATE NAMESPACE IF NOT EXISTS %s", dbNamespace);
+      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()", dbNamespace));

Review Comment:
   Oh I see your note about moving `TestIcebergVersionFunction` over. Will do.



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