alex-plekhanov commented on code in PR #11832:
URL: https://github.com/apache/ignite/pull/11832#discussion_r1934141471
##########
modules/core/src/main/java/org/apache/ignite/internal/processors/query/schema/SchemaChangeListener.java:
##########
@@ -140,6 +141,23 @@ public void onColumnsDropped(
*/
public void onFunctionCreated(String schemaName, String name, boolean
deterministic, Method method);
+ /**
+ * Callback on table function creation.
+ *
+ * @param schemaName Schema name.
+ * @param name Function name.
+ * @param colTypes Column types of the table representation.
+ * @param colNames Column names if the table representation. If {@code
null} or empty, the default names are used instead.
+ * @param method Public static method, implementing this function.
+ */
+ public void onTableFunctionCreated(
+ String schemaName,
+ String name,
+ Method method,
+ Class<?>[] colTypes,
+ @Nullable String[] colNames
Review Comment:
`colNames` is not nullable
##########
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/TableFunctionScan.java:
##########
@@ -41,6 +42,17 @@ public TableFunctionScan(
/** {@inheritDoc} */
@Override public Iterator<Row> iterator() {
- return F.iterator(dataSupplier.get(),
rowFactory::createByRowContainer, true);
+ return F.iterator(dataSupplier.get(), this::wrapToObjectArray, true);
+ }
+
+ /** */
+ private Row wrapToObjectArray(Object rowContainer) {
Review Comment:
Return type of this method is `Row`, so better name is `wrapToRow`
##########
modules/core/src/main/java/org/apache/ignite/internal/processors/query/schema/AbstractSchemaChangeListener.java:
##########
@@ -107,6 +108,17 @@ public abstract class AbstractSchemaChangeListener
implements SchemaChangeListen
// No-op.
}
+ /** {@inheritDoc} */
+ @Override public void onTableFunctionCreated(
+ String schemaName,
+ String name,
+ Method method,
+ Class<?>[] colTypes,
+ @Nullable String[] colNames
Review Comment:
`colNames` is not nullable
##########
modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/H2SchemaManager.java:
##########
@@ -237,6 +237,17 @@ private void dropTable(H2TableDescriptor tbl) {
}
}
+ /** {@inheritDoc} */
+ @Override public void onTableFunctionCreated(
+ String schemaName,
+ String name,
+ Method method,
+ Class<?>[] colTypes,
+ @Nullable String[] colNames
Review Comment:
`colNames` is not nullable
##########
modules/core/src/main/java/org/apache/ignite/internal/processors/query/schema/SchemaChangeListener.java:
##########
@@ -140,6 +141,23 @@ public void onColumnsDropped(
*/
public void onFunctionCreated(String schemaName, String name, boolean
deterministic, Method method);
+ /**
+ * Callback on table function creation.
+ *
+ * @param schemaName Schema name.
+ * @param name Function name.
+ * @param colTypes Column types of the table representation.
+ * @param colNames Column names if the table representation. If {@code
null} or empty, the default names are used instead.
+ * @param method Public static method, implementing this function.
Review Comment:
Non static methods are supperted now (for onFunctionCreated we can also fix
the javadoc)
##########
modules/core/src/main/java/org/apache/ignite/internal/processors/query/schema/management/SchemaManager.java:
##########
@@ -1415,6 +1427,17 @@ private CompoundSchemaChangeListener(GridKernalContext
ctx, List<SchemaChangeLis
lsnrs.forEach(lsnr -> executeSafe(() ->
lsnr.onFunctionCreated(schemaName, name, deterministic, method)));
}
+ /** {@inheritDoc} */
+ @Override public void onTableFunctionCreated(
+ String schemaName,
+ String name,
+ Method method,
+ Class<?>[] colTypes,
+ @Nullable String[] colNames
Review Comment:
`colNames` is not nullable
##########
docs/_docs/code-snippets/java/src/main/java/org/apache/ignite/snippets/SqlAPI.java:
##########
@@ -165,6 +165,18 @@ public static int sqr(int x) {
// end::sql-function-example[]
+ // tag::sql-table-function-example[]
+ static class SqlTableFunctions {
+ @QuerySqlTableFunction(columnTypes = {Integer.class, String.class},
columnNames = {"INT_COL", "STR_COL"})
+ public static Iterable<Object[]> table_function(int i) {
+ return Arrays.asList(
+ new Object[]{i, "" + i},
+ new Object[]{i * 10, "empty"}
Review Comment:
Space after `new Object[]`
##########
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/schema/SchemaHolderImpl.java:
##########
@@ -357,6 +358,21 @@ private static Object
affinityIdentity(CacheConfiguration<?, ?> ccfg) {
rebuild();
}
+ /** {@inheritDoc} */
+ @Override public void onTableFunctionCreated(
+ String schemaName,
+ String name,
+ Method method,
+ Class<?>[] colTypes,
+ @Nullable String[] colNames
Review Comment:
`colNames` is not nullable
##########
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/jdbc/JdbcSetClientInfoTest.java:
##########
@@ -287,5 +299,13 @@ public String sessionId() {
return sesCtx == null ? null : sesCtx.getAttribute(SESSION_ID);
}
+
+ /** */
+ @QuerySqlTableFunction(columnTypes = {String.class}, columnNames =
{"SID"})
+ public Collection<Object[]> sessionIdTbl() {
+ SessionContext sesCtx = sesCtxProv.getSessionContext();
+
+ return Collections.singletonList(new Object[]{sesCtx == null ?
null : sesCtx.getAttribute(SESSION_ID)});
Review Comment:
Spae after `new Object[]`
##########
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/UserDefinedFunctionsIntegrationTest.java:
##########
@@ -105,12 +127,320 @@ public void testFunctions() throws Exception {
assertQuery("SELECT sq(4)").returns(16d).check();
}
+ /** */
+ @Test
+ public void testTableFunctions() throws Exception {
+ IgniteCache<Integer, Employer> emp = client.getOrCreateCache(new
CacheConfiguration<Integer, Employer>("emp")
+ .setSqlSchema("PUBLIC")
+ .setSqlFunctionClasses(TableFunctionsLibrary.class)
+ .setQueryEntities(F.asList(new QueryEntity(Integer.class,
Employer.class).setTableName("emp")))
+ );
+
+ emp.put(1, new Employer("Igor1", 1d));
+ emp.put(2, new Employer("Roman1", 2d));
+
+ awaitPartitionMapExchange();
+
+ assertQuery("SELECT * from iteratorRow(?)").withParams(1)
+ .returns(2, 3, 4)
+ .returns(5, 6, 7)
+ .returns(8, 9, 10)
+ .check();
+
+ assertQuery("SELECT * from iteratorRow(?) WHERE COL_2=4").withParams(2)
+ .returns(3, 4, 5)
+ .check();
+
+ assertQuery("SELECT COL_1, COL_3 from iteratorRow(?) WHERE
COL_2=3").withParams(1)
+ .returns(2, 4)
+ .check();
+
+ // Overrides.
+ assertQuery("SELECT * from iteratorRow(?, 2, ?)").withParams(1, 3)
+ .returns(11, 22, 33)
+ .returns(41, 52, 63)
+ .returns(71, 82, 93)
+ .check();
+
+ assertQuery("SELECT * from arrayRow(?)").withParams(1)
+ .returns(2, 3, 4)
+ .returns(5, 6, 7)
+ .returns(8, 9, 10)
+ .check();
+
+ assertQuery("SELECT COL_1, COL_3 from arrayRow_and_it(1) WHERE COL_1>4
AND COL_2>? AND COL_3>6").withParams(5)
+ .returns(5, 7)
+ .returns(8, 10)
+ .check();
+
+ assertQuery("SELECT * from boxingUnboxing(1, ?, ?,
4.0::FLOAT)").withParams(1, 4.0d)
+ .returns(1, 1, 4.0d, 4.0d)
+ .check();
+
+ assertQuery("SELECT * from boxingUnboxing(1, 1, 2, 2)")
+ .returns(1, 1, 2.0d, 2.0d)
+ .check();
+
+ assertQuery("SELECT * from boxingUnboxing(?, ?, ?, ?)").withParams(1,
1, 2.0d, 2.0d)
+ .returns(1, 1, 2.0d, 2.0d)
+ .check();
+
+ assertQuery("SELECT * from emp WHERE SALARY >= (SELECT COL_1 from
iteratorRow(1) WHERE COL_2=3)")
+ .returns("Roman1", 2d)
+ .check();
+
+ assertQuery("SELECT * from aliasedName(?)").withParams(1)
+ .returns(2, 3, 4)
+ .returns(5, 6, 7)
+ .check();
+
+ assertQuery("SELECT * from raiseException(?, ?, ?)").withParams(1,
"test", false)
+ .returns(2, "test2")
+ .returns(3, "test3")
+ .check();
+
+ assertThrows("SELECT * from raiseException(?, ?, ?)",
IgniteSQLException.class, "An error occurred while query executing",
+ 1, "test", true);
+ assertThrows("SELECT * from raiseException(?, ?, ?)",
RuntimeException.class, "Test exception",
+ 1, "test", true);
+
+ // Object type.
+ assertQuery("SELECT * from withObjectType(1)")
+ .returns(1, new Employer("emp1", 1000d))
+ .returns(10, new Employer("emp10", 10000d))
+ .check();
+ assertQuery("SELECT * from withObjectType(1) where EMP=?")
+ .withParams(new Employer("emp10", 10000d))
+ .returns(10, new Employer("emp10", 10000d))
+ .check();
+ }
+
+ /** */
+ @Test
+ public void testIncorrectTableFunctions() throws Exception {
+ LogListener logChecker0 = LogListener.matches("One or more column
names is not unique")
+ .andMatches("must match the number of column types")
+ .andMatches("The method is expected to return a collection
(iterable)")
+ .andMatches("Column types cannot be empty")
+ .andMatches("Column names cannot be empty")
+ .build();
+
+ listeningLog.registerListener(logChecker0);
+
+ IgniteCache<Integer, Employer> emp = client.getOrCreateCache(new
CacheConfiguration<Integer, Employer>("emp")
+ .setSqlSchema("PUBLIC")
+ .setSqlFunctionClasses(IncorrectTableFunctionsLibrary.class)
+ .setQueryEntities(F.asList(new QueryEntity(Integer.class,
Employer.class).setTableName("emp")))
+ );
+
+ emp.put(1, new Employer("Igor1", 1d));
+ emp.put(2, new Employer("Roman1", 2d));
+
+ awaitPartitionMapExchange();
+
+ // Ensure the cache SQL is OK.
+ assertQuery("SELECT * from emp WHERE SALARY >= 2")
+ .returns("Roman1", 2d)
+ .check();
+
+ logChecker0.check(getTestTimeout());
+
+ String errTxt = "No match found for function signature";
+
+ assertThrows("SELECT * FROM duplicateColumnName(1, 'a')",
SqlValidatorException.class, errTxt);
+
+ assertThrows("SELECT * FROM wrongColumnNamesNumber(1, 'a')",
SqlValidatorException.class, errTxt);
+
+ assertThrows("SELECT * FROM wrongReturnType(1, 'a')",
SqlValidatorException.class, errTxt);
+
+ assertThrows("SELECT * FROM noReturnType(1, 'a')",
SqlValidatorException.class, errTxt);
+
+ assertThrows("SELECT * FROM noColumnTypes(1, 'a')",
SqlValidatorException.class, errTxt);
+
+ assertThrows("SELECT * FROM noColumnNames(1, 'a')",
SqlValidatorException.class, errTxt);
+ }
+
+ /** */
+ @Test
+ public void testUnregistrableTableFunctions() {
+ GridTestUtils.assertThrowsAnyCause(
+ null,
+ () -> client.getOrCreateCache(new CacheConfiguration<Integer,
Employer>("emp")
+
.setSqlFunctionClasses(UnregistrableTableFunctionsLibrary.class)),
+ IgniteCheckedException.class,
+ "oth table and non-table function variants are defined"
Review Comment:
oth -> Both
##########
docs/_docs/SQL/custom-sql-func.adoc:
##########
@@ -46,4 +46,21 @@ Once you have deployed a cache with the above configuration,
you can call the cu
include::{javaFile}[tags=sql-function-query, indent=0]
----
+
+Custom SQL function can be a table function. Result of table function is
treated as a row set (a table) and can be used
+by other SQL operators. Custom SQL function is also a `public static` method
marked by annotation `@QuerySqlTableFunction`.
Review Comment:
Can be non-static
##########
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/UserDefinedFunctionsIntegrationTest.java:
##########
@@ -105,12 +127,320 @@ public void testFunctions() throws Exception {
assertQuery("SELECT sq(4)").returns(16d).check();
}
+ /** */
+ @Test
+ public void testTableFunctions() throws Exception {
+ IgniteCache<Integer, Employer> emp = client.getOrCreateCache(new
CacheConfiguration<Integer, Employer>("emp")
+ .setSqlSchema("PUBLIC")
+ .setSqlFunctionClasses(TableFunctionsLibrary.class)
+ .setQueryEntities(F.asList(new QueryEntity(Integer.class,
Employer.class).setTableName("emp")))
+ );
+
+ emp.put(1, new Employer("Igor1", 1d));
+ emp.put(2, new Employer("Roman1", 2d));
+
+ awaitPartitionMapExchange();
+
+ assertQuery("SELECT * from iteratorRow(?)").withParams(1)
+ .returns(2, 3, 4)
+ .returns(5, 6, 7)
+ .returns(8, 9, 10)
+ .check();
+
+ assertQuery("SELECT * from iteratorRow(?) WHERE COL_2=4").withParams(2)
+ .returns(3, 4, 5)
+ .check();
+
+ assertQuery("SELECT COL_1, COL_3 from iteratorRow(?) WHERE
COL_2=3").withParams(1)
+ .returns(2, 4)
+ .check();
+
+ // Overrides.
+ assertQuery("SELECT * from iteratorRow(?, 2, ?)").withParams(1, 3)
+ .returns(11, 22, 33)
+ .returns(41, 52, 63)
+ .returns(71, 82, 93)
+ .check();
+
+ assertQuery("SELECT * from arrayRow(?)").withParams(1)
+ .returns(2, 3, 4)
+ .returns(5, 6, 7)
+ .returns(8, 9, 10)
+ .check();
+
+ assertQuery("SELECT COL_1, COL_3 from arrayRow_and_it(1) WHERE COL_1>4
AND COL_2>? AND COL_3>6").withParams(5)
+ .returns(5, 7)
+ .returns(8, 10)
+ .check();
+
+ assertQuery("SELECT * from boxingUnboxing(1, ?, ?,
4.0::FLOAT)").withParams(1, 4.0d)
+ .returns(1, 1, 4.0d, 4.0d)
+ .check();
+
+ assertQuery("SELECT * from boxingUnboxing(1, 1, 2, 2)")
+ .returns(1, 1, 2.0d, 2.0d)
+ .check();
+
+ assertQuery("SELECT * from boxingUnboxing(?, ?, ?, ?)").withParams(1,
1, 2.0d, 2.0d)
+ .returns(1, 1, 2.0d, 2.0d)
+ .check();
+
+ assertQuery("SELECT * from emp WHERE SALARY >= (SELECT COL_1 from
iteratorRow(1) WHERE COL_2=3)")
+ .returns("Roman1", 2d)
+ .check();
+
+ assertQuery("SELECT * from aliasedName(?)").withParams(1)
+ .returns(2, 3, 4)
+ .returns(5, 6, 7)
+ .check();
+
+ assertQuery("SELECT * from raiseException(?, ?, ?)").withParams(1,
"test", false)
+ .returns(2, "test2")
+ .returns(3, "test3")
+ .check();
+
+ assertThrows("SELECT * from raiseException(?, ?, ?)",
IgniteSQLException.class, "An error occurred while query executing",
+ 1, "test", true);
+ assertThrows("SELECT * from raiseException(?, ?, ?)",
RuntimeException.class, "Test exception",
+ 1, "test", true);
+
+ // Object type.
+ assertQuery("SELECT * from withObjectType(1)")
+ .returns(1, new Employer("emp1", 1000d))
+ .returns(10, new Employer("emp10", 10000d))
+ .check();
+ assertQuery("SELECT * from withObjectType(1) where EMP=?")
+ .withParams(new Employer("emp10", 10000d))
+ .returns(10, new Employer("emp10", 10000d))
+ .check();
+ }
+
+ /** */
+ @Test
+ public void testIncorrectTableFunctions() throws Exception {
+ LogListener logChecker0 = LogListener.matches("One or more column
names is not unique")
+ .andMatches("must match the number of column types")
+ .andMatches("The method is expected to return a collection
(iterable)")
+ .andMatches("Column types cannot be empty")
+ .andMatches("Column names cannot be empty")
+ .build();
+
+ listeningLog.registerListener(logChecker0);
+
+ IgniteCache<Integer, Employer> emp = client.getOrCreateCache(new
CacheConfiguration<Integer, Employer>("emp")
+ .setSqlSchema("PUBLIC")
+ .setSqlFunctionClasses(IncorrectTableFunctionsLibrary.class)
+ .setQueryEntities(F.asList(new QueryEntity(Integer.class,
Employer.class).setTableName("emp")))
+ );
+
+ emp.put(1, new Employer("Igor1", 1d));
+ emp.put(2, new Employer("Roman1", 2d));
+
+ awaitPartitionMapExchange();
+
+ // Ensure the cache SQL is OK.
+ assertQuery("SELECT * from emp WHERE SALARY >= 2")
+ .returns("Roman1", 2d)
+ .check();
+
+ logChecker0.check(getTestTimeout());
+
+ String errTxt = "No match found for function signature";
+
+ assertThrows("SELECT * FROM duplicateColumnName(1, 'a')",
SqlValidatorException.class, errTxt);
+
+ assertThrows("SELECT * FROM wrongColumnNamesNumber(1, 'a')",
SqlValidatorException.class, errTxt);
+
+ assertThrows("SELECT * FROM wrongReturnType(1, 'a')",
SqlValidatorException.class, errTxt);
+
+ assertThrows("SELECT * FROM noReturnType(1, 'a')",
SqlValidatorException.class, errTxt);
+
+ assertThrows("SELECT * FROM noColumnTypes(1, 'a')",
SqlValidatorException.class, errTxt);
+
+ assertThrows("SELECT * FROM noColumnNames(1, 'a')",
SqlValidatorException.class, errTxt);
+ }
+
+ /** */
+ @Test
+ public void testUnregistrableTableFunctions() {
+ GridTestUtils.assertThrowsAnyCause(
+ null,
+ () -> client.getOrCreateCache(new CacheConfiguration<Integer,
Employer>("emp")
+
.setSqlFunctionClasses(UnregistrableTableFunctionsLibrary.class)),
+ IgniteCheckedException.class,
+ "oth table and non-table function variants are defined"
+ );
+ }
+
/** */
@SuppressWarnings("ThrowableNotThrown")
private void assertThrows(String sql) {
GridTestUtils.assertThrowsWithCause(() -> assertQuery(sql).check(),
IgniteSQLException.class);
}
+ /** */
+ public static final class TableFunctionsLibrary {
+ /** */
+ private TableFunctionsLibrary() {
+ // No-op.
+ }
+
+ /** Trivial test. Returts collections as row holders. */
+ @QuerySqlTableFunction(columnTypes = {int.class, int.class,
int.class}, columnNames = {"COL_1", "COL_2", "COL_3"})
+ 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),
+ Arrays.asList(x + 7, x + 8, x + 9)
+ );
+ }
+
+ /** Overrides. */
+ @QuerySqlTableFunction(columnTypes = {int.class, int.class,
int.class}, columnNames = {"COL_1", "COL_2", "COL_3"})
+ 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),
+ Arrays.asList(x + 70, y + 80, z + 90)
+ );
+ }
+
+ /** Returns arrays as row holders. */
+ @QuerySqlTableFunction(columnTypes = {int.class, int.class,
int.class}, columnNames = {"COL_1", "COL_2", "COL_3"})
+ 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},
+ new Object[] {x + 7, x + 8, x + 9}
+ );
+ }
+
+ /** Returns mixed row holders. */
+ @QuerySqlTableFunction(columnTypes = {int.class, int.class,
int.class}, columnNames = {"COL_1", "COL_2", "COL_3"})
+ 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),
+ new Object[] {x + 7, x + 8, x + 9}
+ );
+ }
+
+ /** Boxed/unboxed test. */
+ @QuerySqlTableFunction(columnTypes = {Integer.class, int.class,
Double.class, double.class},
+ columnNames = {"COL_1", "COL_2", "COL_3", "COL_4"})
+ public static Collection<List<?>> boxingUnboxing(int i1, Integer i2,
double d1, Double d2) {
+ return List.of(Arrays.asList(i1, i2, d1, d2));
+ }
+
+ /** Alias test. */
+ @QuerySqlTableFunction(columnTypes = {int.class, int.class,
int.class}, columnNames = {"COL_1", "COL_2", "COL_3"},
+ 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)
+ );
+ }
+
+ /** User exception test. */
+ @QuerySqlTableFunction(columnTypes = {int.class, String.class},
columnNames = {"COL_1", "COL_2"})
+ public static Iterable<Collection<?>> raiseException(int i, String
str, boolean doThrow) {
+ if (doThrow)
+ throw new RuntimeException("Test exception.");
+
+ return Arrays.asList(
+ Arrays.asList(i + 1, str + (i + 1)),
+ Arrays.asList(i + 2, str + (i + 2))
+ );
+ }
+
+ /** User exception test. */
Review Comment:
Incorrect javadoc
##########
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/UserDefinedFunctionsIntegrationTest.java:
##########
@@ -105,12 +127,320 @@ public void testFunctions() throws Exception {
assertQuery("SELECT sq(4)").returns(16d).check();
}
+ /** */
+ @Test
+ public void testTableFunctions() throws Exception {
+ IgniteCache<Integer, Employer> emp = client.getOrCreateCache(new
CacheConfiguration<Integer, Employer>("emp")
+ .setSqlSchema("PUBLIC")
+ .setSqlFunctionClasses(TableFunctionsLibrary.class)
+ .setQueryEntities(F.asList(new QueryEntity(Integer.class,
Employer.class).setTableName("emp")))
+ );
+
+ emp.put(1, new Employer("Igor1", 1d));
+ emp.put(2, new Employer("Roman1", 2d));
+
+ awaitPartitionMapExchange();
+
+ assertQuery("SELECT * from iteratorRow(?)").withParams(1)
+ .returns(2, 3, 4)
+ .returns(5, 6, 7)
+ .returns(8, 9, 10)
+ .check();
+
+ assertQuery("SELECT * from iteratorRow(?) WHERE COL_2=4").withParams(2)
+ .returns(3, 4, 5)
+ .check();
+
+ assertQuery("SELECT COL_1, COL_3 from iteratorRow(?) WHERE
COL_2=3").withParams(1)
+ .returns(2, 4)
+ .check();
+
+ // Overrides.
+ assertQuery("SELECT * from iteratorRow(?, 2, ?)").withParams(1, 3)
+ .returns(11, 22, 33)
+ .returns(41, 52, 63)
+ .returns(71, 82, 93)
+ .check();
+
+ assertQuery("SELECT * from arrayRow(?)").withParams(1)
+ .returns(2, 3, 4)
+ .returns(5, 6, 7)
+ .returns(8, 9, 10)
+ .check();
+
+ assertQuery("SELECT COL_1, COL_3 from arrayRow_and_it(1) WHERE COL_1>4
AND COL_2>? AND COL_3>6").withParams(5)
+ .returns(5, 7)
+ .returns(8, 10)
+ .check();
+
+ assertQuery("SELECT * from boxingUnboxing(1, ?, ?,
4.0::FLOAT)").withParams(1, 4.0d)
+ .returns(1, 1, 4.0d, 4.0d)
+ .check();
+
+ assertQuery("SELECT * from boxingUnboxing(1, 1, 2, 2)")
+ .returns(1, 1, 2.0d, 2.0d)
+ .check();
+
+ assertQuery("SELECT * from boxingUnboxing(?, ?, ?, ?)").withParams(1,
1, 2.0d, 2.0d)
+ .returns(1, 1, 2.0d, 2.0d)
+ .check();
+
+ assertQuery("SELECT * from emp WHERE SALARY >= (SELECT COL_1 from
iteratorRow(1) WHERE COL_2=3)")
+ .returns("Roman1", 2d)
+ .check();
+
+ assertQuery("SELECT * from aliasedName(?)").withParams(1)
+ .returns(2, 3, 4)
+ .returns(5, 6, 7)
+ .check();
+
+ assertQuery("SELECT * from raiseException(?, ?, ?)").withParams(1,
"test", false)
+ .returns(2, "test2")
+ .returns(3, "test3")
+ .check();
+
+ assertThrows("SELECT * from raiseException(?, ?, ?)",
IgniteSQLException.class, "An error occurred while query executing",
+ 1, "test", true);
+ assertThrows("SELECT * from raiseException(?, ?, ?)",
RuntimeException.class, "Test exception",
+ 1, "test", true);
+
+ // Object type.
+ assertQuery("SELECT * from withObjectType(1)")
+ .returns(1, new Employer("emp1", 1000d))
+ .returns(10, new Employer("emp10", 10000d))
+ .check();
+ assertQuery("SELECT * from withObjectType(1) where EMP=?")
+ .withParams(new Employer("emp10", 10000d))
+ .returns(10, new Employer("emp10", 10000d))
+ .check();
+ }
+
+ /** */
+ @Test
+ public void testIncorrectTableFunctions() throws Exception {
+ LogListener logChecker0 = LogListener.matches("One or more column
names is not unique")
+ .andMatches("must match the number of column types")
+ .andMatches("The method is expected to return a collection
(iterable)")
+ .andMatches("Column types cannot be empty")
+ .andMatches("Column names cannot be empty")
+ .build();
+
+ listeningLog.registerListener(logChecker0);
+
+ IgniteCache<Integer, Employer> emp = client.getOrCreateCache(new
CacheConfiguration<Integer, Employer>("emp")
+ .setSqlSchema("PUBLIC")
+ .setSqlFunctionClasses(IncorrectTableFunctionsLibrary.class)
+ .setQueryEntities(F.asList(new QueryEntity(Integer.class,
Employer.class).setTableName("emp")))
+ );
+
+ emp.put(1, new Employer("Igor1", 1d));
+ emp.put(2, new Employer("Roman1", 2d));
+
+ awaitPartitionMapExchange();
+
+ // Ensure the cache SQL is OK.
+ assertQuery("SELECT * from emp WHERE SALARY >= 2")
+ .returns("Roman1", 2d)
+ .check();
+
+ logChecker0.check(getTestTimeout());
+
+ String errTxt = "No match found for function signature";
+
+ assertThrows("SELECT * FROM duplicateColumnName(1, 'a')",
SqlValidatorException.class, errTxt);
+
+ assertThrows("SELECT * FROM wrongColumnNamesNumber(1, 'a')",
SqlValidatorException.class, errTxt);
+
+ assertThrows("SELECT * FROM wrongReturnType(1, 'a')",
SqlValidatorException.class, errTxt);
+
+ assertThrows("SELECT * FROM noReturnType(1, 'a')",
SqlValidatorException.class, errTxt);
+
+ assertThrows("SELECT * FROM noColumnTypes(1, 'a')",
SqlValidatorException.class, errTxt);
+
+ assertThrows("SELECT * FROM noColumnNames(1, 'a')",
SqlValidatorException.class, errTxt);
+ }
+
+ /** */
+ @Test
+ public void testUnregistrableTableFunctions() {
+ GridTestUtils.assertThrowsAnyCause(
+ null,
+ () -> client.getOrCreateCache(new CacheConfiguration<Integer,
Employer>("emp")
+
.setSqlFunctionClasses(UnregistrableTableFunctionsLibrary.class)),
+ IgniteCheckedException.class,
+ "oth table and non-table function variants are defined"
+ );
+ }
+
/** */
@SuppressWarnings("ThrowableNotThrown")
private void assertThrows(String sql) {
GridTestUtils.assertThrowsWithCause(() -> assertQuery(sql).check(),
IgniteSQLException.class);
}
+ /** */
+ public static final class TableFunctionsLibrary {
+ /** */
+ private TableFunctionsLibrary() {
+ // No-op.
+ }
+
+ /** Trivial test. Returts collections as row holders. */
Review Comment:
Returts -> Returns
##########
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/UserDefinedFunctionsIntegrationTest.java:
##########
@@ -105,12 +127,320 @@ public void testFunctions() throws Exception {
assertQuery("SELECT sq(4)").returns(16d).check();
}
+ /** */
+ @Test
+ public void testTableFunctions() throws Exception {
+ IgniteCache<Integer, Employer> emp = client.getOrCreateCache(new
CacheConfiguration<Integer, Employer>("emp")
+ .setSqlSchema("PUBLIC")
+ .setSqlFunctionClasses(TableFunctionsLibrary.class)
+ .setQueryEntities(F.asList(new QueryEntity(Integer.class,
Employer.class).setTableName("emp")))
+ );
+
+ emp.put(1, new Employer("Igor1", 1d));
+ emp.put(2, new Employer("Roman1", 2d));
+
+ awaitPartitionMapExchange();
+
+ assertQuery("SELECT * from iteratorRow(?)").withParams(1)
+ .returns(2, 3, 4)
+ .returns(5, 6, 7)
+ .returns(8, 9, 10)
+ .check();
+
+ assertQuery("SELECT * from iteratorRow(?) WHERE COL_2=4").withParams(2)
+ .returns(3, 4, 5)
+ .check();
+
+ assertQuery("SELECT COL_1, COL_3 from iteratorRow(?) WHERE
COL_2=3").withParams(1)
+ .returns(2, 4)
+ .check();
+
+ // Overrides.
+ assertQuery("SELECT * from iteratorRow(?, 2, ?)").withParams(1, 3)
+ .returns(11, 22, 33)
+ .returns(41, 52, 63)
+ .returns(71, 82, 93)
+ .check();
+
+ assertQuery("SELECT * from arrayRow(?)").withParams(1)
+ .returns(2, 3, 4)
+ .returns(5, 6, 7)
+ .returns(8, 9, 10)
+ .check();
+
+ assertQuery("SELECT COL_1, COL_3 from arrayRow_and_it(1) WHERE COL_1>4
AND COL_2>? AND COL_3>6").withParams(5)
+ .returns(5, 7)
+ .returns(8, 10)
+ .check();
+
+ assertQuery("SELECT * from boxingUnboxing(1, ?, ?,
4.0::FLOAT)").withParams(1, 4.0d)
+ .returns(1, 1, 4.0d, 4.0d)
+ .check();
+
+ assertQuery("SELECT * from boxingUnboxing(1, 1, 2, 2)")
+ .returns(1, 1, 2.0d, 2.0d)
+ .check();
+
+ assertQuery("SELECT * from boxingUnboxing(?, ?, ?, ?)").withParams(1,
1, 2.0d, 2.0d)
+ .returns(1, 1, 2.0d, 2.0d)
+ .check();
+
+ assertQuery("SELECT * from emp WHERE SALARY >= (SELECT COL_1 from
iteratorRow(1) WHERE COL_2=3)")
+ .returns("Roman1", 2d)
+ .check();
+
+ assertQuery("SELECT * from aliasedName(?)").withParams(1)
+ .returns(2, 3, 4)
+ .returns(5, 6, 7)
+ .check();
+
+ assertQuery("SELECT * from raiseException(?, ?, ?)").withParams(1,
"test", false)
+ .returns(2, "test2")
+ .returns(3, "test3")
+ .check();
+
+ assertThrows("SELECT * from raiseException(?, ?, ?)",
IgniteSQLException.class, "An error occurred while query executing",
+ 1, "test", true);
+ assertThrows("SELECT * from raiseException(?, ?, ?)",
RuntimeException.class, "Test exception",
+ 1, "test", true);
+
+ // Object type.
+ assertQuery("SELECT * from withObjectType(1)")
+ .returns(1, new Employer("emp1", 1000d))
+ .returns(10, new Employer("emp10", 10000d))
+ .check();
+ assertQuery("SELECT * from withObjectType(1) where EMP=?")
Review Comment:
NL
##########
modules/core/src/main/java/org/apache/ignite/internal/processors/query/schema/SchemaChangeListener.java:
##########
@@ -140,6 +141,23 @@ public void onColumnsDropped(
*/
public void onFunctionCreated(String schemaName, String name, boolean
deterministic, Method method);
+ /**
+ * Callback on table function creation.
+ *
+ * @param schemaName Schema name.
+ * @param name Function name.
+ * @param colTypes Column types of the table representation.
+ * @param colNames Column names if the table representation. If {@code
null} or empty, the default names are used instead.
Review Comment:
`if the table` -> `of the table`
`colNames` is not nullable
##########
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/IgniteTableFunction.java:
##########
@@ -0,0 +1,133 @@
+/*
+ * 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.ignite.internal.processors.query.calcite.exec.exp;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Type;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import org.apache.calcite.adapter.enumerable.NullPolicy;
+import org.apache.calcite.adapter.java.JavaTypeFactory;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.schema.TableFunction;
+import org.apache.ignite.cache.query.annotations.QuerySqlTableFunction;
+import org.apache.ignite.internal.processors.query.IgniteSQLException;
+import org.apache.ignite.internal.util.typedef.F;
+
+/**
+ * Holder of user-defined table function.
+ *
+ * @see QuerySqlTableFunction
+ */
+public class IgniteTableFunction extends IgniteReflectiveFunctionBase
implements TableFunction {
+ /** Column types of the returned table representation. */
+ private final Class<?>[] colTypes;
+
+ /** Column names of the returned table representation. */
+ private final List<String> colNames;
+
+ /**
+ * Creates user-defined table function holder.
+ *
+ * @param method The implementatng method.
+ * @param colTypes Column types of the returned table representation.
+ * @param colNames Column names of the returned table representation.
+ * @param implementor Call implementor.
+ */
+ private IgniteTableFunction(Method method, Class<?>[] colTypes, String[]
colNames, CallImplementor implementor) {
+ super(method, implementor);
+
+ validate(method, colTypes, colNames);
+
+ this.colTypes = colTypes;
+ this.colNames = Arrays.asList(colNames);
+ }
+
+ /**
+ * Creates user-defined table function implementor and holder.
+ *
+ * @param method The implementating method.
+ * @param colTypes Column types of the returned table representation.
+ * @param colNames Column names of the returned table representation.
+ */
+ public static IgniteTableFunction create(Method method, Class<?>[]
colTypes, String[] colNames) {
+ CallImplementor impl = RexImpTable.createImplementor(new
ReflectiveCallNotNullImplementor(method), NullPolicy.NONE, false);
Review Comment:
Line is too long
##########
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/UserDefinedFunctionsIntegrationTest.java:
##########
@@ -105,12 +127,320 @@ public void testFunctions() throws Exception {
assertQuery("SELECT sq(4)").returns(16d).check();
}
+ /** */
+ @Test
+ public void testTableFunctions() throws Exception {
+ IgniteCache<Integer, Employer> emp = client.getOrCreateCache(new
CacheConfiguration<Integer, Employer>("emp")
+ .setSqlSchema("PUBLIC")
+ .setSqlFunctionClasses(TableFunctionsLibrary.class)
+ .setQueryEntities(F.asList(new QueryEntity(Integer.class,
Employer.class).setTableName("emp")))
+ );
+
+ emp.put(1, new Employer("Igor1", 1d));
+ emp.put(2, new Employer("Roman1", 2d));
+
+ awaitPartitionMapExchange();
+
+ assertQuery("SELECT * from iteratorRow(?)").withParams(1)
+ .returns(2, 3, 4)
+ .returns(5, 6, 7)
+ .returns(8, 9, 10)
+ .check();
+
+ assertQuery("SELECT * from iteratorRow(?) WHERE COL_2=4").withParams(2)
+ .returns(3, 4, 5)
+ .check();
+
+ assertQuery("SELECT COL_1, COL_3 from iteratorRow(?) WHERE
COL_2=3").withParams(1)
+ .returns(2, 4)
+ .check();
+
+ // Overrides.
+ assertQuery("SELECT * from iteratorRow(?, 2, ?)").withParams(1, 3)
+ .returns(11, 22, 33)
+ .returns(41, 52, 63)
+ .returns(71, 82, 93)
+ .check();
+
+ assertQuery("SELECT * from arrayRow(?)").withParams(1)
+ .returns(2, 3, 4)
+ .returns(5, 6, 7)
+ .returns(8, 9, 10)
+ .check();
+
+ assertQuery("SELECT COL_1, COL_3 from arrayRow_and_it(1) WHERE COL_1>4
AND COL_2>? AND COL_3>6").withParams(5)
+ .returns(5, 7)
+ .returns(8, 10)
+ .check();
+
+ assertQuery("SELECT * from boxingUnboxing(1, ?, ?,
4.0::FLOAT)").withParams(1, 4.0d)
+ .returns(1, 1, 4.0d, 4.0d)
+ .check();
+
+ assertQuery("SELECT * from boxingUnboxing(1, 1, 2, 2)")
+ .returns(1, 1, 2.0d, 2.0d)
+ .check();
+
+ assertQuery("SELECT * from boxingUnboxing(?, ?, ?, ?)").withParams(1,
1, 2.0d, 2.0d)
+ .returns(1, 1, 2.0d, 2.0d)
+ .check();
+
+ assertQuery("SELECT * from emp WHERE SALARY >= (SELECT COL_1 from
iteratorRow(1) WHERE COL_2=3)")
+ .returns("Roman1", 2d)
+ .check();
+
+ assertQuery("SELECT * from aliasedName(?)").withParams(1)
+ .returns(2, 3, 4)
+ .returns(5, 6, 7)
+ .check();
+
+ assertQuery("SELECT * from raiseException(?, ?, ?)").withParams(1,
"test", false)
+ .returns(2, "test2")
+ .returns(3, "test3")
+ .check();
+
+ assertThrows("SELECT * from raiseException(?, ?, ?)",
IgniteSQLException.class, "An error occurred while query executing",
+ 1, "test", true);
+ assertThrows("SELECT * from raiseException(?, ?, ?)",
RuntimeException.class, "Test exception",
+ 1, "test", true);
+
+ // Object type.
+ assertQuery("SELECT * from withObjectType(1)")
+ .returns(1, new Employer("emp1", 1000d))
+ .returns(10, new Employer("emp10", 10000d))
+ .check();
+ assertQuery("SELECT * from withObjectType(1) where EMP=?")
+ .withParams(new Employer("emp10", 10000d))
+ .returns(10, new Employer("emp10", 10000d))
+ .check();
+ }
+
+ /** */
+ @Test
+ public void testIncorrectTableFunctions() throws Exception {
+ LogListener logChecker0 = LogListener.matches("One or more column
names is not unique")
+ .andMatches("must match the number of column types")
+ .andMatches("The method is expected to return a collection
(iterable)")
+ .andMatches("Column types cannot be empty")
+ .andMatches("Column names cannot be empty")
+ .build();
+
+ listeningLog.registerListener(logChecker0);
+
+ IgniteCache<Integer, Employer> emp = client.getOrCreateCache(new
CacheConfiguration<Integer, Employer>("emp")
+ .setSqlSchema("PUBLIC")
+ .setSqlFunctionClasses(IncorrectTableFunctionsLibrary.class)
+ .setQueryEntities(F.asList(new QueryEntity(Integer.class,
Employer.class).setTableName("emp")))
+ );
+
+ emp.put(1, new Employer("Igor1", 1d));
+ emp.put(2, new Employer("Roman1", 2d));
+
+ awaitPartitionMapExchange();
+
+ // Ensure the cache SQL is OK.
+ assertQuery("SELECT * from emp WHERE SALARY >= 2")
+ .returns("Roman1", 2d)
+ .check();
+
+ logChecker0.check(getTestTimeout());
+
+ String errTxt = "No match found for function signature";
+
+ assertThrows("SELECT * FROM duplicateColumnName(1, 'a')",
SqlValidatorException.class, errTxt);
+
+ assertThrows("SELECT * FROM wrongColumnNamesNumber(1, 'a')",
SqlValidatorException.class, errTxt);
+
+ assertThrows("SELECT * FROM wrongReturnType(1, 'a')",
SqlValidatorException.class, errTxt);
+
+ assertThrows("SELECT * FROM noReturnType(1, 'a')",
SqlValidatorException.class, errTxt);
+
+ assertThrows("SELECT * FROM noColumnTypes(1, 'a')",
SqlValidatorException.class, errTxt);
+
+ assertThrows("SELECT * FROM noColumnNames(1, 'a')",
SqlValidatorException.class, errTxt);
+ }
+
+ /** */
+ @Test
+ public void testUnregistrableTableFunctions() {
+ GridTestUtils.assertThrowsAnyCause(
+ null,
+ () -> client.getOrCreateCache(new CacheConfiguration<Integer,
Employer>("emp")
+
.setSqlFunctionClasses(UnregistrableTableFunctionsLibrary.class)),
+ IgniteCheckedException.class,
+ "oth table and non-table function variants are defined"
+ );
+ }
+
/** */
@SuppressWarnings("ThrowableNotThrown")
private void assertThrows(String sql) {
GridTestUtils.assertThrowsWithCause(() -> assertQuery(sql).check(),
IgniteSQLException.class);
}
+ /** */
+ public static final class TableFunctionsLibrary {
+ /** */
+ private TableFunctionsLibrary() {
+ // No-op.
+ }
+
+ /** Trivial test. Returts collections as row holders. */
+ @QuerySqlTableFunction(columnTypes = {int.class, int.class,
int.class}, columnNames = {"COL_1", "COL_2", "COL_3"})
+ public static Iterable<Collection<?>> iteratorRow(int x) {
Review Comment:
iteratorRow -> collectionRow?
##########
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/UserDefinedFunctionsIntegrationTest.java:
##########
@@ -105,12 +127,320 @@ public void testFunctions() throws Exception {
assertQuery("SELECT sq(4)").returns(16d).check();
}
+ /** */
+ @Test
+ public void testTableFunctions() throws Exception {
+ IgniteCache<Integer, Employer> emp = client.getOrCreateCache(new
CacheConfiguration<Integer, Employer>("emp")
+ .setSqlSchema("PUBLIC")
+ .setSqlFunctionClasses(TableFunctionsLibrary.class)
+ .setQueryEntities(F.asList(new QueryEntity(Integer.class,
Employer.class).setTableName("emp")))
+ );
+
+ emp.put(1, new Employer("Igor1", 1d));
+ emp.put(2, new Employer("Roman1", 2d));
+
+ awaitPartitionMapExchange();
+
+ assertQuery("SELECT * from iteratorRow(?)").withParams(1)
+ .returns(2, 3, 4)
+ .returns(5, 6, 7)
+ .returns(8, 9, 10)
+ .check();
+
+ assertQuery("SELECT * from iteratorRow(?) WHERE COL_2=4").withParams(2)
+ .returns(3, 4, 5)
+ .check();
+
+ assertQuery("SELECT COL_1, COL_3 from iteratorRow(?) WHERE
COL_2=3").withParams(1)
+ .returns(2, 4)
+ .check();
+
+ // Overrides.
+ assertQuery("SELECT * from iteratorRow(?, 2, ?)").withParams(1, 3)
+ .returns(11, 22, 33)
+ .returns(41, 52, 63)
+ .returns(71, 82, 93)
+ .check();
+
+ assertQuery("SELECT * from arrayRow(?)").withParams(1)
+ .returns(2, 3, 4)
+ .returns(5, 6, 7)
+ .returns(8, 9, 10)
+ .check();
+
+ assertQuery("SELECT COL_1, COL_3 from arrayRow_and_it(1) WHERE COL_1>4
AND COL_2>? AND COL_3>6").withParams(5)
+ .returns(5, 7)
+ .returns(8, 10)
+ .check();
+
+ assertQuery("SELECT * from boxingUnboxing(1, ?, ?,
4.0::FLOAT)").withParams(1, 4.0d)
+ .returns(1, 1, 4.0d, 4.0d)
+ .check();
+
+ assertQuery("SELECT * from boxingUnboxing(1, 1, 2, 2)")
+ .returns(1, 1, 2.0d, 2.0d)
+ .check();
+
+ assertQuery("SELECT * from boxingUnboxing(?, ?, ?, ?)").withParams(1,
1, 2.0d, 2.0d)
+ .returns(1, 1, 2.0d, 2.0d)
+ .check();
+
+ assertQuery("SELECT * from emp WHERE SALARY >= (SELECT COL_1 from
iteratorRow(1) WHERE COL_2=3)")
+ .returns("Roman1", 2d)
+ .check();
+
+ assertQuery("SELECT * from aliasedName(?)").withParams(1)
+ .returns(2, 3, 4)
+ .returns(5, 6, 7)
+ .check();
+
+ assertQuery("SELECT * from raiseException(?, ?, ?)").withParams(1,
"test", false)
+ .returns(2, "test2")
+ .returns(3, "test3")
+ .check();
+
+ assertThrows("SELECT * from raiseException(?, ?, ?)",
IgniteSQLException.class, "An error occurred while query executing",
+ 1, "test", true);
+ assertThrows("SELECT * from raiseException(?, ?, ?)",
RuntimeException.class, "Test exception",
Review Comment:
NL
--
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]