Search in sources :

Example 6 with Function1

use of org.apache.calcite.linq4j.function.Function1 in project calcite by apache.

the class EnumerableTableScan method toRows.

private Expression toRows(PhysType physType, Expression expression) {
    if (physType.getFormat() == JavaRowFormat.SCALAR && Object[].class.isAssignableFrom(elementType) && getRowType().getFieldCount() == 1 && (table.unwrap(ScannableTable.class) != null || table.unwrap(FilterableTable.class) != null || table.unwrap(ProjectableFilterableTable.class) != null)) {
        return Expressions.call(BuiltInMethod.SLICE0.method, expression);
    }
    JavaRowFormat oldFormat = format();
    if (physType.getFormat() == oldFormat && !hasCollectionField(rowType)) {
        return expression;
    }
    final ParameterExpression row_ = Expressions.parameter(elementType, "row");
    final int fieldCount = table.getRowType().getFieldCount();
    List<Expression> expressionList = new ArrayList<>(fieldCount);
    for (int i = 0; i < fieldCount; i++) {
        expressionList.add(fieldExpression(row_, i, physType, oldFormat));
    }
    return Expressions.call(expression, BuiltInMethod.SELECT.method, Expressions.lambda(Function1.class, physType.record(expressionList), row_));
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) MethodCallExpression(org.apache.calcite.linq4j.tree.MethodCallExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) FilterableTable(org.apache.calcite.schema.FilterableTable) ArrayList(java.util.ArrayList) Function1(org.apache.calcite.linq4j.function.Function1) ScannableTable(org.apache.calcite.schema.ScannableTable)

Example 7 with Function1

use of org.apache.calcite.linq4j.function.Function1 in project calcite by apache.

the class CalcitePrepareImpl method prepare_.

<T> CalciteSignature<T> prepare_(Context context, Query<T> query, Type elementType, long maxRowCount) {
    if (SIMPLE_SQLS.contains(query.sql)) {
        return simplePrepare(context, query.sql);
    }
    final JavaTypeFactory typeFactory = context.getTypeFactory();
    CalciteCatalogReader catalogReader = new CalciteCatalogReader(context.getRootSchema(), context.getDefaultSchemaPath(), typeFactory, context.config());
    final List<Function1<Context, RelOptPlanner>> plannerFactories = createPlannerFactories();
    if (plannerFactories.isEmpty()) {
        throw new AssertionError("no planner factories");
    }
    RuntimeException exception = Util.FoundOne.NULL;
    for (Function1<Context, RelOptPlanner> plannerFactory : plannerFactories) {
        final RelOptPlanner planner = plannerFactory.apply(context);
        if (planner == null) {
            throw new AssertionError("factory returned null planner");
        }
        try {
            return prepare2_(context, query, elementType, maxRowCount, catalogReader, planner);
        } catch (RelOptPlanner.CannotPlanException e) {
            exception = e;
        }
    }
    throw exception;
}
Also used : DataContext(org.apache.calcite.DataContext) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) Function1(org.apache.calcite.linq4j.function.Function1) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner)

Example 8 with Function1

use of org.apache.calcite.linq4j.function.Function1 in project calcite by apache.

the class SqlUtil method filterRoutinesByParameterType.

/**
 * @see Glossary#SQL99 SQL:1999 Part 2 Section 10.4 Syntax Rule 6.b.iii.2.B
 */
private static Iterator<SqlOperator> filterRoutinesByParameterType(SqlSyntax syntax, final Iterator<SqlOperator> routines, final List<RelDataType> argTypes, final List<String> argNames) {
    if (syntax != SqlSyntax.FUNCTION) {
        return routines;
    }
    // noinspection unchecked
    return (Iterator) Iterators.filter(Iterators.filter(routines, SqlFunction.class), new PredicateImpl<SqlFunction>() {

        public boolean test(SqlFunction function) {
            List<RelDataType> paramTypes = function.getParamTypes();
            if (paramTypes == null) {
                // no parameter information for builtins; keep for now
                return true;
            }
            final List<RelDataType> permutedArgTypes;
            if (argNames != null) {
                // Arguments passed by name. Make sure that the function has
                // parameters of all of these names.
                final Map<Integer, Integer> map = new HashMap<>();
                for (Ord<String> argName : Ord.zip(argNames)) {
                    final int i = function.getParamNames().indexOf(argName.e);
                    if (i < 0) {
                        return false;
                    }
                    map.put(i, argName.i);
                }
                permutedArgTypes = Functions.generate(paramTypes.size(), new Function1<Integer, RelDataType>() {

                    public RelDataType apply(Integer a0) {
                        if (map.containsKey(a0)) {
                            return argTypes.get(map.get(a0));
                        } else {
                            return null;
                        }
                    }
                });
            } else {
                permutedArgTypes = Lists.newArrayList(argTypes);
                while (permutedArgTypes.size() < argTypes.size()) {
                    paramTypes.add(null);
                }
            }
            for (Pair<RelDataType, RelDataType> p : Pair.zip(paramTypes, permutedArgTypes)) {
                final RelDataType argType = p.right;
                final RelDataType paramType = p.left;
                if (argType != null && !SqlTypeUtil.canCastFrom(paramType, argType, false)) {
                    return false;
                }
            }
            return true;
        }
    });
}
Also used : HashMap(java.util.HashMap) Function1(org.apache.calcite.linq4j.function.Function1) PredicateImpl(org.apache.calcite.runtime.PredicateImpl) RelDataType(org.apache.calcite.rel.type.RelDataType) NlsString(org.apache.calcite.util.NlsString) Iterator(java.util.Iterator)

Example 9 with Function1

use of org.apache.calcite.linq4j.function.Function1 in project streamline by hortonworks.

the class RexNodeToJavaCodeCompiler method compileToBlock.

private BlockBuilder compileToBlock(final RexProgram program, ParameterExpression context_, ParameterExpression outputValues_) {
    RelDataType inputRowType = program.getInputRowType();
    final BlockBuilder builder = new BlockBuilder();
    final JavaTypeFactoryImpl javaTypeFactory = new JavaTypeFactoryImpl(rexBuilder.getTypeFactory().getTypeSystem());
    final RexToLixTranslator.InputGetter inputGetter = new RexToLixTranslator.InputGetterImpl(ImmutableList.of(Pair.<Expression, PhysType>of(Expressions.field(context_, BuiltInMethod.CONTEXT_VALUES.field), PhysTypeImpl.of(javaTypeFactory, inputRowType, JavaRowFormat.ARRAY, false))));
    final Function1<String, RexToLixTranslator.InputGetter> correlates = new Function1<String, RexToLixTranslator.InputGetter>() {

        public RexToLixTranslator.InputGetter apply(String a0) {
            throw new UnsupportedOperationException();
        }
    };
    final Expression root = Expressions.field(context_, BuiltInMethod.CONTEXT_ROOT.field);
    final List<Expression> list = RexToLixTranslator.translateProjects(program, javaTypeFactory, builder, null, root, inputGetter, correlates);
    for (int i = 0; i < list.size(); i++) {
        builder.add(Expressions.statement(Expressions.assign(Expressions.arrayIndex(outputValues_, Expressions.constant(i)), list.get(i))));
    }
    return builder;
}
Also used : Function1(org.apache.calcite.linq4j.function.Function1) RelDataType(org.apache.calcite.rel.type.RelDataType) PhysType(org.apache.calcite.adapter.enumerable.PhysType) Expression(org.apache.calcite.linq4j.tree.Expression) ExecutableExpression(com.hortonworks.streamline.streams.sql.runtime.ExecutableExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) JavaTypeFactoryImpl(org.apache.calcite.jdbc.JavaTypeFactoryImpl) RexToLixTranslator(org.apache.calcite.adapter.enumerable.RexToLixTranslator) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 10 with Function1

use of org.apache.calcite.linq4j.function.Function1 in project storm by apache.

the class RexNodeToJavaCodeCompiler method compileToBlock.

private BlockBuilder compileToBlock(final RexProgram program, ParameterExpression context, ParameterExpression outputValues) {
    RelDataType inputRowType = program.getInputRowType();
    final BlockBuilder builder = new BlockBuilder();
    final JavaTypeFactoryImpl javaTypeFactory = new JavaTypeFactoryImpl(rexBuilder.getTypeFactory().getTypeSystem());
    final RexToLixTranslator.InputGetter inputGetter = new RexToLixTranslator.InputGetterImpl(ImmutableList.of(Pair.<Expression, PhysType>of(Expressions.field(context, BuiltInMethod.CONTEXT_VALUES.field), PhysTypeImpl.of(javaTypeFactory, inputRowType, JavaRowFormat.ARRAY, false))));
    final Function1<String, RexToLixTranslator.InputGetter> correlates = new Function1<String, RexToLixTranslator.InputGetter>() {

        @Override
        public RexToLixTranslator.InputGetter apply(String a0) {
            throw new UnsupportedOperationException();
        }
    };
    final Expression root = Expressions.field(context, BuiltInMethod.CONTEXT_ROOT.field);
    final List<Expression> list = RexToLixTranslator.translateProjects(program, javaTypeFactory, builder, null, root, inputGetter, correlates);
    for (int i = 0; i < list.size(); i++) {
        builder.add(Expressions.statement(Expressions.assign(Expressions.arrayIndex(outputValues, Expressions.constant(i)), list.get(i))));
    }
    return builder;
}
Also used : Function1(org.apache.calcite.linq4j.function.Function1) RelDataType(org.apache.calcite.rel.type.RelDataType) PhysType(org.apache.calcite.adapter.enumerable.PhysType) Expression(org.apache.calcite.linq4j.tree.Expression) ExecutableExpression(org.apache.storm.sql.runtime.calcite.ExecutableExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) JavaTypeFactoryImpl(org.apache.calcite.jdbc.JavaTypeFactoryImpl) RexToLixTranslator(org.apache.calcite.adapter.enumerable.RexToLixTranslator) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Aggregations

Function1 (org.apache.calcite.linq4j.function.Function1)18 ParameterExpression (org.apache.calcite.linq4j.tree.ParameterExpression)8 Expression (org.apache.calcite.linq4j.tree.Expression)6 RelDataType (org.apache.calcite.rel.type.RelDataType)6 BlockBuilder (org.apache.calcite.linq4j.tree.BlockBuilder)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 PhysType (org.apache.calcite.adapter.enumerable.PhysType)4 RexToLixTranslator (org.apache.calcite.adapter.enumerable.RexToLixTranslator)4 ImmutableList (com.google.common.collect.ImmutableList)3 List (java.util.List)3 DataContext (org.apache.calcite.DataContext)3 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)3 Connection (java.sql.Connection)2 Iterator (java.util.Iterator)2 Random (java.util.Random)2 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)2 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)2 Enumerable (org.apache.calcite.linq4j.Enumerable)2 QueryProvider (org.apache.calcite.linq4j.QueryProvider)2