Search in sources :

Example 1 with Function

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Function in project sidewinder by srotya.

the class SqlApi method initCalcite.

public void initCalcite() throws SQLException, ClassNotFoundException {
    Class.forName("org.apache.calcite.jdbc.Driver");
    connection = DriverManager.getConnection("jdbc:calcite:");
    CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
    ModelHandler.create(calciteConnection.getRootSchema(), "now", Arrays.asList(""), NowFunction.class.getName(), "apply");
    ModelHandler.create(calciteConnection.getRootSchema(), "milli", Arrays.asList(""), ToMilliseconds.class.getName(), "apply");
    ModelHandler.create(calciteConnection.getRootSchema(), "ts", Arrays.asList(""), ToTimestamp.class.getName(), "eval");
    ModelHandler.create(calciteConnection.getRootSchema(), "datediff", Arrays.asList(""), DateDiffFunction.class.getName(), "apply");
    for (Function function : calciteConnection.getRootSchema().getFunctions("ts")) {
        System.out.println(function.getParameters().get(0).getName());
    }
}
Also used : NowFunction(com.srotya.sidewinder.core.sql.calcite.functions.NowFunction) DateDiffFunction(com.srotya.sidewinder.core.sql.calcite.functions.DateDiffFunction) Function(org.apache.calcite.schema.Function) NowFunction(com.srotya.sidewinder.core.sql.calcite.functions.NowFunction) ToTimestamp(com.srotya.sidewinder.core.sql.calcite.functions.ToTimestamp) ToMilliseconds(com.srotya.sidewinder.core.sql.calcite.functions.ToMilliseconds) DateDiffFunction(com.srotya.sidewinder.core.sql.calcite.functions.DateDiffFunction) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection)

Example 2 with Function

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Function in project calcite by apache.

the class CachingCalciteSchema method addImplicitFunctionsToBuilder.

protected void addImplicitFunctionsToBuilder(ImmutableList.Builder<Function> builder, String name, boolean caseSensitive) {
    // Add implicit functions, case-insensitive.
    final long now = System.currentTimeMillis();
    final NameSet set = implicitFunctionCache.get(now);
    for (String name2 : set.range(name, caseSensitive)) {
        final Collection<Function> functions = schema.getFunctions(name2);
        if (functions != null) {
            builder.addAll(functions);
        }
    }
}
Also used : Function(org.apache.calcite.schema.Function) NameSet(org.apache.calcite.util.NameSet)

Example 3 with Function

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Function in project calcite by apache.

the class SimpleCalciteSchema method addImplicitTablesBasedOnNullaryFunctionsToBuilder.

protected void addImplicitTablesBasedOnNullaryFunctionsToBuilder(ImmutableSortedMap.Builder<String, Table> builder) {
    ImmutableSortedMap<String, Table> explicitTables = builder.build();
    for (String s : schema.getFunctionNames()) {
        // explicit table wins.
        if (explicitTables.containsKey(s)) {
            continue;
        }
        for (Function function : schema.getFunctions(s)) {
            if (function instanceof TableMacro && function.getParameters().isEmpty()) {
                final Table table = ((TableMacro) function).apply(ImmutableList.of());
                builder.put(s, table);
            }
        }
    }
}
Also used : Function(org.apache.calcite.schema.Function) TableMacro(org.apache.calcite.schema.TableMacro) Table(org.apache.calcite.schema.Table)

Example 4 with Function

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Function in project calcite by apache.

the class CalciteCatalogReader method operatorTable.

/**
 * Creates an operator table that contains functions in the given class.
 *
 * @see ModelHandler#addFunctions
 */
public static SqlOperatorTable operatorTable(String className) {
    // Dummy schema to collect the functions
    final CalciteSchema schema = CalciteSchema.createRootSchema(false, false);
    ModelHandler.addFunctions(schema.plus(), null, ImmutableList.<String>of(), className, "*", true);
    // The following is technical debt; see [CALCITE-2082] Remove
    // RelDataTypeFactory argument from SqlUserDefinedAggFunction constructor
    final SqlTypeFactoryImpl typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
    final ListSqlOperatorTable table = new ListSqlOperatorTable();
    for (String name : schema.getFunctionNames()) {
        for (Function function : schema.getFunctions(name, true)) {
            final SqlIdentifier id = new SqlIdentifier(name, SqlParserPos.ZERO);
            table.add(toOp(typeFactory, id, function));
        }
    }
    return table;
}
Also used : ListSqlOperatorTable(org.apache.calcite.sql.util.ListSqlOperatorTable) SqlUserDefinedFunction(org.apache.calcite.sql.validate.SqlUserDefinedFunction) TableFunction(org.apache.calcite.schema.TableFunction) SqlUserDefinedAggFunction(org.apache.calcite.sql.validate.SqlUserDefinedAggFunction) Function(org.apache.calcite.schema.Function) AggregateFunction(org.apache.calcite.schema.AggregateFunction) SqlUserDefinedTableFunction(org.apache.calcite.sql.validate.SqlUserDefinedTableFunction) ScalarFunction(org.apache.calcite.schema.ScalarFunction) SqlTypeFactoryImpl(org.apache.calcite.sql.type.SqlTypeFactoryImpl) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier)

Example 5 with Function

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Function in project streamline by hortonworks.

the class StreamlineSqlImpl method handleCreateFunction.

private void handleCreateFunction(SqlCreateFunction sqlCreateFunction) throws ClassNotFoundException {
    if (sqlCreateFunction.jarName() != null) {
        throw new UnsupportedOperationException("UDF 'USING JAR' not implemented");
    }
    Method method;
    Function function;
    if ((method = findMethod(sqlCreateFunction.className(), "evaluate")) != null) {
        function = ScalarFunctionImpl.create(method);
    } else if (findMethod(sqlCreateFunction.className(), "add") != null) {
        function = AggregateFunctionImpl.create(Class.forName(sqlCreateFunction.className()));
    } else {
        throw new RuntimeException("Invalid scalar or aggregate function");
    }
    schema.add(sqlCreateFunction.functionName().toUpperCase(), function);
    hasUdf = true;
}
Also used : SqlCreateFunction(com.hortonworks.streamline.streams.sql.parser.SqlCreateFunction) Function(org.apache.calcite.schema.Function) Method(java.lang.reflect.Method)

Aggregations

Function (org.apache.calcite.schema.Function)13 RexNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode)12 Method (java.lang.reflect.Method)11 ArrayList (java.util.ArrayList)11 SqlOperator (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperator)6 RelDataType (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType)4 SchemaPlus (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus)4 TableMacro (org.apache.calcite.schema.TableMacro)4 Test (org.junit.Test)4 ResolvedExpr (com.google.zetasql.resolvedast.ResolvedNodes.ResolvedExpr)3 ResolvedLiteral (com.google.zetasql.resolvedast.ResolvedNodes.ResolvedLiteral)3 JdbcConnection (org.apache.beam.sdk.extensions.sql.impl.JdbcConnection)3 SqlIdentifier (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier)3 Table (org.apache.calcite.schema.Table)3 Function (com.google.zetasql.Function)2 FunctionArgumentType (com.google.zetasql.FunctionArgumentType)2 FunctionSignature (com.google.zetasql.FunctionSignature)2 TableValuedFunction (com.google.zetasql.TableValuedFunction)2 Type (com.google.zetasql.Type)2 ResolvedAggregateFunctionCall (com.google.zetasql.resolvedast.ResolvedNodes.ResolvedAggregateFunctionCall)2