From 6d4841853cde7ca0d2bbf9ca4d37fdee991db886 Mon Sep 17 00:00:00 2001
From: Vladimir Steshin <vladsz83@gmail.com>
Date: Tue, 28 Jan 2025 18:52:31 +0300
Subject: [PATCH] + test with a class type. trivial renamings, fixes

---
 .../AbstractBasicIntegrationTest.java         | 13 +++
 .../UserDefinedFunctionsIntegrationTest.java  | 97 +++++++++++--------
 2 files changed, 69 insertions(+), 41 deletions(-)

diff --git a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/AbstractBasicIntegrationTest.java b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/AbstractBasicIntegrationTest.java
index 81e6024b56bc5..c07049ed80d97 100644
--- a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/AbstractBasicIntegrationTest.java
+++ b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/AbstractBasicIntegrationTest.java
@@ -336,5 +336,18 @@ public Employer(String name, Double salary) {
             this.name = name;
             this.salary = salary;
         }
+
+        /** {@inheritDoc} */
+        @Override public boolean equals(Object o) {
+            if (this == o)
+                return true;
+
+            if (o == null || getClass() != o.getClass())
+                return false;
+
+            Employer employer = (Employer)o;
+
+            return name.equals(employer.name) && salary.equals(employer.salary);
+        }
     }
 }
diff --git a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/UserDefinedFunctionsIntegrationTest.java b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/UserDefinedFunctionsIntegrationTest.java
index 86b909493671c..a6bbbf52edb7d 100644
--- a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/UserDefinedFunctionsIntegrationTest.java
+++ b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/UserDefinedFunctionsIntegrationTest.java
@@ -130,7 +130,7 @@ public void testFunctions() throws Exception {
     public void testTableFunctions() throws Exception {
         IgniteCache<Integer, Employer> emp = client.getOrCreateCache(new CacheConfiguration<Integer, Employer>("emp")
             .setSqlSchema("PUBLIC")
-            .setSqlFunctionClasses(TableFunctions.class)
+            .setSqlFunctionClasses(TableFunctionsLibrary.class)
             .setQueryEntities(F.asList(new QueryEntity(Integer.class, Employer.class).setTableName("emp")))
         );
 
@@ -139,78 +139,84 @@ public void testTableFunctions() throws Exception {
 
         awaitPartitionMapExchange();
 
-        assertQuery("SELECT * from tbl_fun_it(?)").withParams(1)
+        assertQuery("SELECT * from iteratorRow(?)").withParams(1)
             .returns(2, 3, 4)
             .returns(5, 6, 7)
             .returns(8, 9, 10)
             .check();
 
-        assertQuery("SELECT * from tbl_fun_it(?) WHERE COL_2=4").withParams(2)
+        assertQuery("SELECT * from iteratorRow(?) WHERE COL_2=4").withParams(2)
             .returns(3, 4, 5)
             .check();
 
-        assertQuery("SELECT COL_1, COL_3 from tbl_fun_it(?) WHERE COL_2=3").withParams(1)
+        assertQuery("SELECT COL_1, COL_3 from iteratorRow(?) WHERE COL_2=3").withParams(1)
             .returns(2, 4)
             .check();
 
         // Overrides.
-        assertQuery("SELECT * from tbl_fun_it(?, 2, ?)").withParams(1, 3)
+        assertQuery("SELECT * from iteratorRow(?, 2, ?)").withParams(1, 3)
             .returns(11, 22, 33)
             .returns(41, 52, 63)
             .returns(71, 82, 93)
             .check();
 
-        assertQuery("SELECT * from tbl_fun_arr(?)").withParams(1)
+        assertQuery("SELECT * from arrayRow(?)").withParams(1)
             .returns(2, 3, 4)
             .returns(5, 6, 7)
             .returns(8, 9, 10)
             .check();
 
-        assertQuery("SELECT * from tbl_fun_arr_and_it(1) WHERE COL_1>4 AND COL_2>? AND COL_3>6").withParams(5)
+        assertQuery("SELECT * from arrayRow_and_it(1) WHERE COL_1>4 AND COL_2>? AND COL_3>6").withParams(5)
             .returns(5, 6, 7)
             .returns(8, 9, 10)
             .check();
 
-        assertQuery("SELECT * from tbl_fun_boxing(1, ?, ?, 4.0::FLOAT)").withParams(1, 4.0d)
+        assertQuery("SELECT * from boxingUnboxing(1, ?, ?, 4.0::FLOAT)").withParams(1, 4.0d)
             .returns(1, 1, 4.0d, 4.0d)
             .check();
 
-        assertQuery("SELECT * from tbl_fun_boxing(1, 1, 2, 2)")
+        assertQuery("SELECT * from boxingUnboxing(1, 1, 2, 2)")
             .returns(1, 1, 2.0d, 2.0d)
             .check();
 
-        assertQuery("SELECT * from tbl_fun_boxing(?, ?, ?, ?)").withParams(1, 1, 2.0d, 2.0d)
+        assertQuery("SELECT * from boxingUnboxing(?, ?, ?, ?)").withParams(1, 1, 2.0d, 2.0d)
             .returns(1, 1, 2.0d, 2.0d)
             .check();
 
-        assertQuery("SELECT STR_COL from tbl_fun_col_names(1001) where INT_COL>1000")
+        assertQuery("SELECT STR_COL from withColumnNames(1001) where INT_COL>1000")
             .returns("1001")
             .returns("empty")
             .check();
 
-        assertQuery("SELECT * from emp WHERE SALARY >= (SELECT COL_1 from tbl_fun_it(1) WHERE COL_2=3)")
+        assertQuery("SELECT * from emp WHERE SALARY >= (SELECT COL_1 from iteratorRow(1) WHERE COL_2=3)")
             .returns("Roman1", 2d)
             .check();
 
-        assertQuery("SELECT * from aliased_fun_name(?)").withParams(1)
+        assertQuery("SELECT * from aliasedName(?)").withParams(1)
             .returns(2, 3, 4)
             .returns(5, 6, 7)
             .check();
 
-        assertQuery("SELECT * from tbl_fun_exception(?, ?, ?)").withParams(1, "test", false)
+        assertQuery("SELECT * from raiseException(?, ?, ?)").withParams(1, "test", false)
             .returns(2, "test2")
             .returns(3, "test3")
             .check();
 
-        assertThrows("SELECT * from tbl_fun_exception(?, ?, ?)", IgniteSQLException.class, "An error occurred while query executing",
+        assertThrows("SELECT * from raiseException(?, ?, ?)", IgniteSQLException.class, "An error occurred while query executing",
             1, "test", true);
-        assertThrows("SELECT * from tbl_fun_exception(?, ?, ?)", RuntimeException.class, "Test exception",
+        assertThrows("SELECT * from raiseException(?, ?, ?)", RuntimeException.class, "Test exception",
             1, "test", true);
+
+        // Object type.
+        assertQuery("SELECT * from withClassType(1)")
+            .returns(1, new Employer("emp1", 1000d))
+            .returns(10, new Employer("emp10", 10000d))
+            .check();
     }
 
     /** */
     @Test
-    public void testIncorrentTableFunctions() throws Exception {
+    public void testIncorrectTableFunctions() throws Exception {
         LogListener logChecker0 = LogListener.matches("One or more column names is not unique")
             .andMatches("either be empty or match the number of column types")
             .andMatches("The method is expected to return a collection (iteratable)")
@@ -221,7 +227,7 @@ public void testIncorrentTableFunctions() throws Exception {
 
         IgniteCache<Integer, Employer> emp = client.getOrCreateCache(new CacheConfiguration<Integer, Employer>("emp")
             .setSqlSchema("PUBLIC")
-            .setSqlFunctionClasses(IncorrectTableFunctions.class)
+            .setSqlFunctionClasses(IncorrectTableFunctionsLibrary.class)
             .setQueryEntities(F.asList(new QueryEntity(Integer.class, Employer.class).setTableName("emp")))
         );
 
@@ -237,14 +243,14 @@ public void testIncorrentTableFunctions() throws Exception {
 
         logChecker0.check(getTestTimeout());
 
-        assertThrows("SELECT * FROM tbl_fun_dupl_col_nm", SqlValidatorException.class, "not found");
+        assertThrows("SELECT * FROM duplicateColumnName", SqlValidatorException.class, "not found");
 
-        assertThrows("SELECT * FROM tbl_fun_wrong_col_nm_num", SqlValidatorException.class, "not found");
+        assertThrows("SELECT * FROM wrongColumnNamesNumber", SqlValidatorException.class, "not found");
 
-        assertThrows("SELECT * FROM tbl_fun_wrong_ret_type_1", SqlValidatorException.class, "not found");
-        assertThrows("SELECT * FROM tbl_fun_wrong_ret_type_2", SqlValidatorException.class, "not found");
+        assertThrows("SELECT * FROM wrongReturnType1", SqlValidatorException.class, "not found");
+        assertThrows("SELECT * FROM noReturnType", SqlValidatorException.class, "not found");
 
-        assertThrows("SELECT * FROM tbl_fun_wrong_col_types", SqlValidatorException.class, "not found");
+        assertThrows("SELECT * FROM noColumnTypes", SqlValidatorException.class, "not found");
     }
 
     /** */
@@ -254,15 +260,15 @@ private void assertThrows(String sql) {
     }
 
     /** */
-    public static final class TableFunctions {
+    public static final class TableFunctionsLibrary {
         /** */
-        private TableFunctions() {
+        private TableFunctionsLibrary() {
             // No-op.
         }
 
         /** Trivial test. Returts collections as row holders. */
         @QuerySqlFunction(tableColumnTypes = {int.class, int.class, int.class})
-        public static Iterable<Collection<?>> tbl_fun_it(int x) {
+        public static Iterable<Collection<?>> iteratorRow(int x) {
             return Arrays.asList(
                 Arrays.asList(x + 1, x + 2, x + 3),
                 Arrays.asList(x + 4, x + 5, x + 6),
@@ -272,7 +278,7 @@ public static Iterable<Collection<?>> tbl_fun_it(int x) {
 
         /** Overrides. */
         @QuerySqlFunction(tableColumnTypes = {int.class, int.class, int.class})
-        public static Collection<Collection<?>> tbl_fun_it(int x, int y, int z) {
+        public static Collection<Collection<?>> iteratorRow(int x, int y, int z) {
             return Arrays.asList(
                 Arrays.asList(x + 10, y + 20, z + 30),
                 Arrays.asList(x + 40, y + 50, z + 60),
@@ -282,7 +288,7 @@ public static Collection<Collection<?>> tbl_fun_it(int x, int y, int z) {
 
         /** Returns arrays as row holders. */
         @QuerySqlFunction(tableColumnTypes = {int.class, int.class, int.class})
-        public static Iterable<Object[]> tbl_fun_arr(int x) {
+        public static Iterable<Object[]> arrayRow(int x) {
             return Arrays.asList(
                 new Object[] {x + 1, x + 2, x + 3},
                 new Object[] {x + 4, x + 5, x + 6},
@@ -292,7 +298,7 @@ public static Iterable<Object[]> tbl_fun_arr(int x) {
 
         /** Returns mixed row holders. */
         @QuerySqlFunction(tableColumnTypes = {int.class, int.class, int.class})
-        public static Collection<?> tbl_fun_arr_and_it(int x) {
+        public static Collection<?> arrayRow_and_it(int x) {
             return Arrays.asList(
                 new Object[] {x + 1, x + 2, x + 3},
                 Arrays.asList(x + 4, x + 5, x + 6),
@@ -302,13 +308,13 @@ public static Collection<?> tbl_fun_arr_and_it(int x) {
 
         /** Boxed/unboxed test. */
         @QuerySqlFunction(tableColumnTypes = {Integer.class, int.class, Double.class, double.class})
-        public static Collection<List<?>> tbl_fun_boxing(int i1, Integer i2, double d1, Double d2) {
+        public static Collection<List<?>> boxingUnboxing(int i1, Integer i2, double d1, Double d2) {
             return List.of(Arrays.asList(i1, i2, d1, d2));
         }
 
         /** Defined column names test. */
         @QuerySqlFunction(tableColumnTypes = {Integer.class, String.class}, tableColumnNames = {"INT_COL", "STR_COL"})
-        public static Iterable<?> tbl_fun_col_names(int i) {
+        public static Iterable<?> withColumnNames(int i) {
             return Arrays.asList(
                 Arrays.asList(i, "" + i),
                 Arrays.asList(i * 10, "empty")
@@ -316,8 +322,8 @@ public static Iterable<?> tbl_fun_col_names(int i) {
         }
 
         /** Alias test. */
-        @QuerySqlFunction(tableColumnTypes = {int.class, int.class, int.class}, alias = "aliased_fun_name")
-        public static Iterable<Collection<?>> tbl_fun_alias(int x) {
+        @QuerySqlFunction(tableColumnTypes = {int.class, int.class, int.class}, alias = "aliasedName")
+        public static Iterable<Collection<?>> alias(int x) {
             return Arrays.asList(
                 Arrays.asList(x + 1, x + 2, x + 3),
                 Arrays.asList(x + 4, x + 5, x + 6)
@@ -326,7 +332,7 @@ public static Iterable<Collection<?>> tbl_fun_alias(int x) {
 
         /** User exception test. */
         @QuerySqlFunction(tableColumnTypes = {int.class, String.class})
-        public static Iterable<Collection<?>> tbl_fun_exception(int i, String str, boolean doThrow) {
+        public static Iterable<Collection<?>> raiseException(int i, String str, boolean doThrow) {
             if (doThrow)
                 throw new RuntimeException("Test exception.");
 
@@ -335,18 +341,27 @@ public static Iterable<Collection<?>> tbl_fun_exception(int i, String str, boole
                 Arrays.asList(i + 2, str + (i + 2))
             );
         }
+
+        /** User exception test. */
+        @QuerySqlFunction(tableColumnTypes = {int.class, Employer.class}, tableColumnNames = {"ID", "EMP"})
+        public static Iterable<Collection<?>> withClassType(int i) {
+            return Arrays.asList(
+                Arrays.asList(i, new Employer("emp" + i, i * 1000d)),
+                Arrays.asList(i * 10, new Employer("emp" + i * 10, i * 10000d))
+            );
+        }
     }
 
     /** */
-    public static final class IncorrectTableFunctions {
+    public static final class IncorrectTableFunctionsLibrary {
         /** */
-        private IncorrectTableFunctions() {
+        private IncorrectTableFunctionsLibrary() {
             // No-op.
         }
 
         /** Duplicated column names. */
         @QuerySqlFunction(tableColumnTypes = {Integer.class, String.class}, tableColumnNames = {"INT_COL", "INT_COL"})
-        public static Iterable<?> tbl_fun_dupl_col_nm(int i, String s) {
+        public static Iterable<?> duplicateColumnName(int i, String s) {
             return Arrays.asList(
                 Arrays.asList(i, s + i),
                 Arrays.asList(i * 10, s + (i * 10))
@@ -355,7 +370,7 @@ public static Iterable<?> tbl_fun_dupl_col_nm(int i, String s) {
 
         /** Non-matching number of the column names. */
         @QuerySqlFunction(tableColumnTypes = {Integer.class, String.class}, tableColumnNames = {"INT_COL"})
-        public static Iterable<?> tbl_fun_wrong_col_nm_num(int i, String s) {
+        public static Iterable<?> wrongColumnNamesNumber(int i, String s) {
             return Arrays.asList(
                 Arrays.asList(i, s + i),
                 Arrays.asList(i * 10, s + (i * 10))
@@ -364,7 +379,7 @@ public static Iterable<?> tbl_fun_wrong_col_nm_num(int i, String s) {
 
         /** Wrong return type 1. */
         @QuerySqlFunction(tableColumnTypes = {Integer.class, Integer.class})
-        public static Object[] tbl_fun_wrong_ret_type_1(int i) {
+        public static Object[] wrongReturnType1(int i) {
             return new Object[] {
                 Arrays.asList(i * 2, i * 2 + 1),
                 Arrays.asList(i * 3, i * 3 + 1)
@@ -373,13 +388,13 @@ public static Object[] tbl_fun_wrong_ret_type_1(int i) {
 
         /** Wrong return type 2. */
         @QuerySqlFunction(tableColumnTypes = {Integer.class})
-        public static void tbl_fun_wrong_ret_type_2(int i) {
+        public static void noReturnType(int i) {
             System.err.println("Test value: " + i);
         }
 
         /** Empty column types. */
         @QuerySqlFunction(tableColumnNames = {"INT_COL", "STR_COL"})
-        public static Collection<?> tbl_fun_wrong_col_types(int i, String s) {
+        public static Collection<?> noColumnTypes(int i, String s) {
             return Arrays.asList(
                 Arrays.asList(i, s + i),
                 Arrays.asList(i * 10, s + (i * 10))