You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by zh...@apache.org on 2025/09/06 12:49:05 UTC

(calcite) 01/01: [CALCITE-7141] Add missing getter to FunctionSqlType

This is an automated email from the ASF dual-hosted git repository.

zhenchen pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git

commit d22c8f31a7aaa1f2be0faa5b86300f838678ac12
Author: Michal Stutzmann <mi...@swisscom.com>
AuthorDate: Sat Sep 6 20:43:36 2025 +0800

    [CALCITE-7141] Add missing getter to FunctionSqlType
---
 .../apache/calcite/sql/type/FunctionSqlType.java   | 34 ++++++++--
 .../calcite/sql/type/FunctionSqlTypeTest.java      | 72 ++++++++++++++++++++++
 2 files changed, 101 insertions(+), 5 deletions(-)

diff --git a/core/src/main/java/org/apache/calcite/sql/type/FunctionSqlType.java b/core/src/main/java/org/apache/calcite/sql/type/FunctionSqlType.java
index 277a8ffca4..009808a3e5 100644
--- a/core/src/main/java/org/apache/calcite/sql/type/FunctionSqlType.java
+++ b/core/src/main/java/org/apache/calcite/sql/type/FunctionSqlType.java
@@ -25,16 +25,26 @@
 
 /**
  * Function type.
- * The type of lambda expression can be represented by a function type.
+ *
+ * <p>The type of lambda expression can be represented by a function type.
  */
 public class FunctionSqlType extends AbstractSqlType {
-  private final RelDataType parameterType;
+  private final RelDataType parameterTypes;
   private final RelDataType returnType;
 
+  /**
+   * Constructs a new function SQL type. This should only be called from a factory method.
+   *
+   * @param parameterTypes a struct type wrapping function's input parameter types.
+   * @param returnType function's return type.
+   */
   public FunctionSqlType(
-      RelDataType parameterType, RelDataType returnType) {
+      RelDataType parameterTypes, RelDataType returnType) {
     super(SqlTypeName.FUNCTION, true, null);
-    this.parameterType = requireNonNull(parameterType, "parameterType");
+    this.parameterTypes = requireNonNull(parameterTypes, "parameterTypes");
+    if (!parameterTypes.isStruct()) {
+      throw new IllegalArgumentException("ParameterType must be a struct");
+    }
     this.returnType = requireNonNull(returnType, "returnType");
     computeDigest();
   }
@@ -42,7 +52,7 @@ public FunctionSqlType(
   @Override protected void generateTypeString(StringBuilder sb, boolean withDetail) {
     sb.append("Function");
     sb.append("(");
-    for (Ord<RelDataTypeField> ord : Ord.zip(parameterType.getFieldList())) {
+    for (Ord<RelDataTypeField> ord : Ord.zip(parameterTypes.getFieldList())) {
       if (ord.i > 0) {
         sb.append(", ");
       }
@@ -58,6 +68,20 @@ public FunctionSqlType(
     return this;
   }
 
+  /**
+   * Returns the parameter types of the function.
+   *
+   * @return a struct wrapping function's parameter types.
+   */
+  public RelDataType getParameterTypes() {
+    return parameterTypes;
+  }
+
+  /**
+   * Returns the return type of the function.
+   *
+   * @return the function's return type.
+   */
   public RelDataType getReturnType() {
     return returnType;
   }
diff --git a/core/src/test/java/org/apache/calcite/sql/type/FunctionSqlTypeTest.java b/core/src/test/java/org/apache/calcite/sql/type/FunctionSqlTypeTest.java
new file mode 100644
index 0000000000..41bb3af475
--- /dev/null
+++ b/core/src/test/java/org/apache/calcite/sql/type/FunctionSqlTypeTest.java
@@ -0,0 +1,72 @@
+/*
+ * 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.calcite.sql.type;
+
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rel.type.RelDataTypeSystem;
+
+import com.google.common.collect.ImmutableList;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+/**
+ * Tests for {@link FunctionSqlType}.
+ */
+public class FunctionSqlTypeTest {
+  final RelDataTypeFactory sqlTypeFactory =
+      new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
+
+  final RelDataType parameterTypes =
+      sqlTypeFactory.createStructType(
+          ImmutableList.of(sqlTypeFactory.createSqlType(SqlTypeName.BOOLEAN)),
+          ImmutableList.of("field1"));
+  final RelDataType nonStructParameterTypes = sqlTypeFactory.createSqlType(SqlTypeName.BOOLEAN);
+  final RelDataType returnType = sqlTypeFactory.createSqlType(SqlTypeName.BOOLEAN);
+  final FunctionSqlType functionSqlType =
+      new FunctionSqlType(parameterTypes, returnType);
+
+  @Test void testFailsOnNullParameterType() {
+    assertThrows(NullPointerException.class, () -> {
+      new FunctionSqlType(null, returnType);
+    }, "parameterType");
+  }
+
+  @Test void testFailsOnNonStructParameterTypes() {
+    assertThrows(IllegalArgumentException.class, () -> {
+      new FunctionSqlType(nonStructParameterTypes, returnType);
+    }, "ParameterType must be a struct");
+  }
+
+  @Test void testFailsOnNullReturnType() {
+    assertThrows(NullPointerException.class, () -> {
+      new FunctionSqlType(parameterTypes, null);
+    });
+  }
+
+  @Test void testGetParameterTypes() {
+    assertEquals(parameterTypes, functionSqlType.getParameterTypes());
+  }
+
+  @Test void testGetReturnType() {
+    assertEquals(returnType, functionSqlType.getReturnType());
+  }
+
+}