Search in sources :

Example 11 with TableFunction

use of org.apache.calcite.schema.TableFunction in project calcite by apache.

the class TableFunctionTest method testTableFunctionCursorsInputs.

/**
 * Tests a table function that takes multiple cursor inputs.
 */
@Ignore("CannotPlanException: Node [rel#24:Subset#6.ENUMERABLE.[]] " + "could not be implemented")
@Test
public void testTableFunctionCursorsInputs() throws SQLException, ClassNotFoundException {
    try (Connection connection = getConnectionWithMultiplyFunction()) {
        CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
        SchemaPlus rootSchema = calciteConnection.getRootSchema();
        SchemaPlus schema = rootSchema.getSubSchema("s");
        final TableFunction table = TableFunctionImpl.create(Smalls.GENERATE_STRINGS_METHOD);
        schema.add("GenerateStrings", table);
        final TableFunction add = TableFunctionImpl.create(Smalls.PROCESS_CURSORS_METHOD);
        schema.add("process", add);
        final PreparedStatement ps = connection.prepareStatement("select *\n" + "from table(\"s\".\"process\"(2,\n" + "cursor(select * from table(\"s\".\"multiplication\"(5,5,0))),\n" + "cursor(select * from table(\"s\".\"GenerateStrings\"(?)))\n" + ")) as t(u)\n" + "where u > 3");
        ps.setInt(1, 5);
        ResultSet resultSet = ps.executeQuery();
        // GenerateStrings produce 0..4
        // multiplication produce 1..5
        // process sums and adds 2
        // sum is 2 + 1..9 == 3..9
        assertThat(CalciteAssert.toString(resultSet), equalTo("u=4\n" + "u=5\n" + "u=6\n" + "u=7\n" + "u=8\n" + "u=9\n"));
    }
}
Also used : Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ResultSet(java.sql.ResultSet) TableFunction(org.apache.calcite.schema.TableFunction) PreparedStatement(java.sql.PreparedStatement) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 12 with TableFunction

use of org.apache.calcite.schema.TableFunction in project calcite by apache.

the class CalciteCatalogReader method toOp.

/**
 * Converts a function to a {@link org.apache.calcite.sql.SqlOperator}.
 *
 * <p>The {@code typeFactory} argument is technical debt; see [CALCITE-2082]
 * Remove RelDataTypeFactory argument from SqlUserDefinedAggFunction
 * constructor.
 */
private static SqlOperator toOp(RelDataTypeFactory typeFactory, SqlIdentifier name, final Function function) {
    List<RelDataType> argTypes = new ArrayList<>();
    List<SqlTypeFamily> typeFamilies = new ArrayList<>();
    for (FunctionParameter o : function.getParameters()) {
        final RelDataType type = o.getType(typeFactory);
        argTypes.add(type);
        typeFamilies.add(Util.first(type.getSqlTypeName().getFamily(), SqlTypeFamily.ANY));
    }
    final Predicate<Integer> optional = new PredicateImpl<Integer>() {

        public boolean test(Integer input) {
            return function.getParameters().get(input).isOptional();
        }
    };
    final FamilyOperandTypeChecker typeChecker = OperandTypes.family(typeFamilies, optional);
    final List<RelDataType> paramTypes = toSql(typeFactory, argTypes);
    if (function instanceof ScalarFunction) {
        return new SqlUserDefinedFunction(name, infer((ScalarFunction) function), InferTypes.explicit(argTypes), typeChecker, paramTypes, function);
    } else if (function instanceof AggregateFunction) {
        return new SqlUserDefinedAggFunction(name, infer((AggregateFunction) function), InferTypes.explicit(argTypes), typeChecker, (AggregateFunction) function, false, false, typeFactory);
    } else if (function instanceof TableMacro) {
        return new SqlUserDefinedTableMacro(name, ReturnTypes.CURSOR, InferTypes.explicit(argTypes), typeChecker, paramTypes, (TableMacro) function);
    } else if (function instanceof TableFunction) {
        return new SqlUserDefinedTableFunction(name, ReturnTypes.CURSOR, InferTypes.explicit(argTypes), typeChecker, paramTypes, (TableFunction) function);
    } else {
        throw new AssertionError("unknown function type " + function);
    }
}
Also used : SqlUserDefinedTableMacro(org.apache.calcite.sql.validate.SqlUserDefinedTableMacro) TableMacro(org.apache.calcite.schema.TableMacro) SqlTypeFamily(org.apache.calcite.sql.type.SqlTypeFamily) ArrayList(java.util.ArrayList) PredicateImpl(org.apache.calcite.runtime.PredicateImpl) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlUserDefinedTableMacro(org.apache.calcite.sql.validate.SqlUserDefinedTableMacro) FamilyOperandTypeChecker(org.apache.calcite.sql.type.FamilyOperandTypeChecker) SqlUserDefinedFunction(org.apache.calcite.sql.validate.SqlUserDefinedFunction) ScalarFunction(org.apache.calcite.schema.ScalarFunction) SqlUserDefinedTableFunction(org.apache.calcite.sql.validate.SqlUserDefinedTableFunction) AggregateFunction(org.apache.calcite.schema.AggregateFunction) SqlUserDefinedAggFunction(org.apache.calcite.sql.validate.SqlUserDefinedAggFunction) TableFunction(org.apache.calcite.schema.TableFunction) SqlUserDefinedTableFunction(org.apache.calcite.sql.validate.SqlUserDefinedTableFunction) FunctionParameter(org.apache.calcite.schema.FunctionParameter)

Aggregations

TableFunction (org.apache.calcite.schema.TableFunction)12 Connection (java.sql.Connection)10 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)10 SchemaPlus (org.apache.calcite.schema.SchemaPlus)10 AbstractSchema (org.apache.calcite.schema.impl.AbstractSchema)9 ResultSet (java.sql.ResultSet)8 Test (org.junit.Test)7 PreparedStatement (java.sql.PreparedStatement)5 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 Statement (java.sql.Statement)2 ArrayList (java.util.ArrayList)2 AggregateFunction (org.apache.calcite.schema.AggregateFunction)2 ScalarFunction (org.apache.calcite.schema.ScalarFunction)2 TableMacro (org.apache.calcite.schema.TableMacro)2 SqlUserDefinedTableFunction (org.apache.calcite.sql.validate.SqlUserDefinedTableFunction)2 Ignore (org.junit.Ignore)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Map (java.util.Map)1 Properties (java.util.Properties)1 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)1