2121import java .util .Arrays ;
2222import java .util .Collection ;
2323import java .util .List ;
24+ import java .util .stream .Collectors ;
2425import org .apache .calcite .schema .SchemaPlus ;
2526import org .apache .calcite .sql .validate .SqlValidatorException ;
2627import org .apache .ignite .IgniteCache ;
@@ -92,7 +93,6 @@ public void testSameSignatureNotRegistered() throws Exception {
9293 assertEquals (1 , schema .getFunctions ("SAMESIGN" ).size ());
9394 }
9495
95-
9696 /** */
9797 @ Test
9898 public void testSystemFunctionOverriding () throws Exception {
@@ -105,18 +105,25 @@ public void testSystemFunctionOverriding() throws Exception {
105105
106106 // Make sure that the new functions didn't affect schema 'PUBLIC'.
107107 assertQuery ("SELECT UPPER(?)" ).withParams ("abc" ).returns ("ABC" ).check ();
108- assertQuery ("select UNIX_SECONDS(TIMESTAMP '2021-01-01 00:00:00')" ).returns (1609459200L ).check ();
109- assertQuery ("select * from table (SYSTEM_RANGE(1, 2))" ).returns (1L ).returns (2L ).check ();
110- assertQuery ("select TYPEOF(?)" ).withParams (1L ).returns ("BIGINT" ).check ();
111- assertQuery ("select ? + ?" ).withParams (1 , 2 ).returns (3 ).check ();
112- assertThrows ("select PLUS(?, ?)" , SqlValidatorException .class , "No match found for function signature" , 1 , 2 );
108+ assertQuery ("SELECT UNIX_SECONDS(TIMESTAMP '2021-01-01 00:00:00')" ).returns (1609459200L ).check ();
109+ assertQuery ("SELECT * FROM TABLE (SYSTEM_RANGE(1, 2))" ).returns (1L ).returns (2L ).check ();
110+ assertQuery ("SELECT TYPEOF(?)" ).withParams (1L ).returns ("BIGINT" ).check ();
111+ assertQuery ("SELECT ? + ?" ).withParams (1 , 2 ).returns (3 ).check ();
112+ assertThrows ("SELECT PLUS(?, ?)" , SqlValidatorException .class , "No match found for function signature" , 1 , 2 );
113113
114114 // Ensure that new functions are successfully created in a custom schema.
115115 assertQuery ("SELECT \" OWN_SCHEMA\" .UPPER(?)" ).withParams ("abc" ).returns (3 ).check ();
116- assertQuery ("select \" OWN_SCHEMA\" .UNIX_SECONDS(TIMESTAMP '2021-01-01 00:00:00')" ).returns (1 ).check ();
117- assertQuery ("select * from table(\" OWN_SCHEMA\" .SYSTEM_RANGE(1, 2))" ).returns (100L ).check ();
118- assertQuery ("select \" OWN_SCHEMA\" .TYPEOF('ABC')" ).returns (1 ).check ();
119- assertQuery ("select \" OWN_SCHEMA\" .PLUS(?, ?)" ).withParams (1 , 2 ).returns (100 ).check ();
116+ assertQuery ("SELECT \" OWN_SCHEMA\" .UNIX_SECONDS(TIMESTAMP '2021-01-01 00:00:00')" ).returns (1 ).check ();
117+ assertQuery ("SELECT * FROM TABLE(\" OWN_SCHEMA\" .SYSTEM_RANGE(1, 2))" ).returns (100L ).check ();
118+ assertQuery ("SELECT \" OWN_SCHEMA\" .TYPEOF('ABC')" ).returns (1 ).check ();
119+ assertQuery ("SELECT \" OWN_SCHEMA\" .PLUS(?, ?)" ).withParams (1 , 2 ).returns (100 ).check ();
120+
121+ assertQuery ("SELECT * FROM TABLE(\" OWN_SCHEMA\" .STR_ARRAY_CONSUME_TABLE(?)) as t" ).withParams (List .of ("row1" , "row2" ))
122+ .returns ("row1" ).returns ( "row2" ).check ();
123+ assertQuery ("SELECT * FROM TABLE(\" OWN_SCHEMA\" .OBJ_ARRAY_CONSUME_TABLE(?)) as t" ).withParams (List .of (new CustomClass (), "row2" ))
124+ .returns ("CustomClass.toString" ).returns ( "row2" ).check ();
125+ assertThrows ("SELECT * FROM TABLE(\" OWN_SCHEMA\" .STR_ARRAY_CONSUME_TABLE(?)) as t" , IgniteSQLException .class ,
126+ "An error occurred while query executing" , List .of (new CustomClass (), "row2" ));
120127
121128 LogListener logChecker0 = LogListener .matches ("Unable to add user-defined SQL function 'upper'" )
122129 .andMatches ("Unable to add user-defined SQL function 'unix_seconds'" )
@@ -713,6 +720,24 @@ public static int typeof(Object o) {
713720 public static int plus (int x , int y ) {
714721 return 100 ;
715722 }
723+
724+ /** Table function with String array as input. */
725+ @ QuerySqlTableFunction (alias = "STR_ARRAY_CONSUME_TABLE" , columnTypes = {String .class }, columnNames = {"RESULT" })
726+ public static Iterable <Object []> strArrConsumeTable (List <String > array ) {
727+ return array .stream ()
728+ .map (Object ::toString )
729+ .map (str -> new Object []{str })
730+ .collect (Collectors .toList ());
731+ }
732+
733+ /** Table function with Object array as input. */
734+ @ QuerySqlTableFunction (alias = "OBJ_ARRAY_CONSUME_TABLE" , columnTypes = {String .class }, columnNames = {"RESULT" })
735+ public static Iterable <Object []> objArrConsumeTable (List <Object > array ) {
736+ return array .stream ()
737+ .map (Object ::toString )
738+ .map (str -> new Object []{str })
739+ .collect (Collectors .toList ());
740+ }
716741 }
717742
718743 /** */
@@ -726,4 +751,12 @@ public static double salary(String ignite, int key) {
726751 .getAll ().get (0 ).get (0 );
727752 }
728753 }
754+
755+ /** */
756+ private static class CustomClass {
757+ /** */
758+ @ Override public String toString () {
759+ return "CustomClass.toString" ;
760+ }
761+ }
729762}
0 commit comments