Search in sources :

Example 1 with Function1

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

the class CassandraTable method query.

/**
 * Executes a CQL query on the underlying table.
 *
 * @param session Cassandra session
 * @param fields List of fields to project
 * @param predicates A list of predicates which should be used in the query
 * @return Enumerator of results
 */
public Enumerable<Object> query(final Session session, List<Map.Entry<String, Class>> fields, final List<Map.Entry<String, String>> selectFields, List<String> predicates, List<String> order, final Integer offset, final Integer fetch) {
    // Build the type of the resulting row based on the provided fields
    final RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
    final RelDataTypeFactory.Builder fieldInfo = typeFactory.builder();
    final RelDataType rowType = getRowType(typeFactory);
    Function1<String, Void> addField = new Function1<String, Void>() {

        public Void apply(String fieldName) {
            SqlTypeName typeName = rowType.getField(fieldName, true, false).getType().getSqlTypeName();
            fieldInfo.add(fieldName, typeFactory.createSqlType(typeName)).nullable(true);
            return null;
        }
    };
    if (selectFields.isEmpty()) {
        for (Map.Entry<String, Class> field : fields) {
            addField.apply(field.getKey());
        }
    } else {
        for (Map.Entry<String, String> field : selectFields) {
            addField.apply(field.getKey());
        }
    }
    final RelProtoDataType resultRowType = RelDataTypeImpl.proto(fieldInfo.build());
    // Construct the list of fields to project
    final String selectString;
    if (selectFields.isEmpty()) {
        selectString = "*";
    } else {
        selectString = Util.toString(new Iterable<String>() {

            public Iterator<String> iterator() {
                final Iterator<Map.Entry<String, String>> selectIterator = selectFields.iterator();
                return new Iterator<String>() {

                    @Override
                    public boolean hasNext() {
                        return selectIterator.hasNext();
                    }

                    @Override
                    public String next() {
                        Map.Entry<String, String> entry = selectIterator.next();
                        return entry.getKey() + " AS " + entry.getValue();
                    }

                    @Override
                    public void remove() {
                        throw new UnsupportedOperationException();
                    }
                };
            }
        }, "", ", ", "");
    }
    // Combine all predicates conjunctively
    String whereClause = "";
    if (!predicates.isEmpty()) {
        whereClause = " WHERE ";
        whereClause += Util.toString(predicates, "", " AND ", "");
    }
    // Build and issue the query and return an Enumerator over the results
    StringBuilder queryBuilder = new StringBuilder("SELECT ");
    queryBuilder.append(selectString);
    queryBuilder.append(" FROM \"" + columnFamily + "\"");
    queryBuilder.append(whereClause);
    if (!order.isEmpty()) {
        queryBuilder.append(Util.toString(order, " ORDER BY ", ", ", ""));
    }
    int limit = offset;
    if (fetch >= 0) {
        limit += fetch;
    }
    if (limit > 0) {
        queryBuilder.append(" LIMIT " + limit);
    }
    queryBuilder.append(" ALLOW FILTERING");
    final String query = queryBuilder.toString();
    return new AbstractEnumerable<Object>() {

        public Enumerator<Object> enumerator() {
            final ResultSet results = session.execute(query);
            // Skip results until we get to the right offset
            int skip = 0;
            Enumerator<Object> enumerator = new CassandraEnumerator(results, resultRowType);
            while (skip < offset && enumerator.moveNext()) {
                skip++;
            }
            return enumerator;
        }
    };
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) RelDataType(org.apache.calcite.rel.type.RelDataType) RelProtoDataType(org.apache.calcite.rel.type.RelProtoDataType) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) Iterator(java.util.Iterator) AbstractEnumerable(org.apache.calcite.linq4j.AbstractEnumerable) ResultSet(com.datastax.driver.core.ResultSet) Function1(org.apache.calcite.linq4j.function.Function1) SqlTypeFactoryImpl(org.apache.calcite.sql.type.SqlTypeFactoryImpl) Map(java.util.Map)

Example 2 with Function1

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

the class TableScanNode method createEnumerable.

private static TableScanNode createEnumerable(Compiler compiler, TableScan rel, Enumerable<Row> enumerable, final ImmutableIntList acceptedProjects, List<RexNode> rejectedFilters, final ImmutableIntList rejectedProjects) {
    if (!rejectedFilters.isEmpty()) {
        final RexNode filter = RexUtil.composeConjunction(rel.getCluster().getRexBuilder(), rejectedFilters, false);
        assert filter != null;
        // Re-map filter for the projects that have been applied already
        final RexNode filter2;
        final RelDataType inputRowType;
        if (acceptedProjects == null) {
            filter2 = filter;
            inputRowType = rel.getRowType();
        } else {
            final Mapping mapping = Mappings.target(acceptedProjects, rel.getTable().getRowType().getFieldCount());
            filter2 = RexUtil.apply(mapping, filter);
            final RelDataTypeFactory.Builder builder = rel.getCluster().getTypeFactory().builder();
            final List<RelDataTypeField> fieldList = rel.getTable().getRowType().getFieldList();
            for (int acceptedProject : acceptedProjects) {
                builder.add(fieldList.get(acceptedProject));
            }
            inputRowType = builder.build();
        }
        final Scalar condition = compiler.compile(ImmutableList.of(filter2), inputRowType);
        final Context context = compiler.createContext();
        enumerable = enumerable.where(new Predicate1<Row>() {

            @Override
            public boolean apply(Row row) {
                context.values = row.getValues();
                Boolean b = (Boolean) condition.execute(context);
                return b != null && b;
            }
        });
    }
    if (rejectedProjects != null) {
        enumerable = enumerable.select(new Function1<Row, Row>() {

            final Object[] values = new Object[rejectedProjects.size()];

            @Override
            public Row apply(Row row) {
                final Object[] inValues = row.getValues();
                for (int i = 0; i < rejectedProjects.size(); i++) {
                    values[i] = inValues[rejectedProjects.get(i)];
                }
                return Row.asCopy(values);
            }
        });
    }
    return new TableScanNode(compiler, rel, enumerable);
}
Also used : DataContext(org.apache.calcite.DataContext) Function1(org.apache.calcite.linq4j.function.Function1) RelDataType(org.apache.calcite.rel.type.RelDataType) Mapping(org.apache.calcite.util.mapping.Mapping) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) Predicate1(org.apache.calcite.linq4j.function.Predicate1) RexNode(org.apache.calcite.rex.RexNode)

Example 3 with Function1

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

the class JaninoRexCompiler method compile.

public Scalar compile(List<RexNode> nodes, RelDataType inputRowType) {
    final RexProgramBuilder programBuilder = new RexProgramBuilder(inputRowType, rexBuilder);
    for (RexNode node : nodes) {
        programBuilder.addProject(node, null);
    }
    final RexProgram program = programBuilder.getProgram();
    final BlockBuilder builder = new BlockBuilder();
    final ParameterExpression context_ = Expressions.parameter(Context.class, "context");
    final ParameterExpression outputValues_ = Expressions.parameter(Object[].class, "outputValues");
    final JavaTypeFactoryImpl javaTypeFactory = new JavaTypeFactoryImpl(rexBuilder.getTypeFactory().getTypeSystem());
    // public void execute(Context, Object[] outputValues)
    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 baz(context_, outputValues_, builder.toBlock());
}
Also used : RexProgram(org.apache.calcite.rex.RexProgram) Function1(org.apache.calcite.linq4j.function.Function1) PhysType(org.apache.calcite.adapter.enumerable.PhysType) Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) JavaTypeFactoryImpl(org.apache.calcite.jdbc.JavaTypeFactoryImpl) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) RexToLixTranslator(org.apache.calcite.adapter.enumerable.RexToLixTranslator) RexProgramBuilder(org.apache.calcite.rex.RexProgramBuilder) RexNode(org.apache.calcite.rex.RexNode) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 4 with Function1

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

the class ResultSetEnumerable method primitiveRowBuilderFactory.

private static Function1<ResultSet, Function0<Object>> primitiveRowBuilderFactory(final Primitive[] primitives) {
    return new Function1<ResultSet, Function0<Object>>() {

        public Function0<Object> apply(final ResultSet resultSet) {
            final ResultSetMetaData metaData;
            final int columnCount;
            try {
                metaData = resultSet.getMetaData();
                columnCount = metaData.getColumnCount();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            assert columnCount == primitives.length;
            if (columnCount == 1) {
                return new Function0<Object>() {

                    public Object apply() {
                        try {
                            return resultSet.getObject(1);
                        } catch (SQLException e) {
                            throw new RuntimeException(e);
                        }
                    }
                };
            }
            // noinspection unchecked
            return (Function0) new Function0<Object[]>() {

                public Object[] apply() {
                    try {
                        final List<Object> list = new ArrayList<Object>();
                        for (int i = 0; i < columnCount; i++) {
                            list.add(primitives[i].jdbcGet(resultSet, i + 1));
                        }
                        return list.toArray();
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    }
                }
            };
        }
    };
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException) Function1(org.apache.calcite.linq4j.function.Function1) ResultSet(java.sql.ResultSet) Function0(org.apache.calcite.linq4j.function.Function0) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with Function1

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

the class PhysTypeImpl method generateCollationKey.

public Pair<Expression, Expression> generateCollationKey(final List<RelFieldCollation> collations) {
    final Expression selector;
    if (collations.size() == 1) {
        RelFieldCollation collation = collations.get(0);
        ParameterExpression parameter = Expressions.parameter(javaRowClass, "v");
        selector = Expressions.lambda(Function1.class, fieldReference(parameter, collation.getFieldIndex()), parameter);
        return Pair.<Expression, Expression>of(selector, Expressions.call(BuiltInMethod.NULLS_COMPARATOR.method, Expressions.constant(collation.nullDirection == RelFieldCollation.NullDirection.FIRST), Expressions.constant(collation.getDirection() == RelFieldCollation.Direction.DESCENDING)));
    }
    selector = Expressions.call(BuiltInMethod.IDENTITY_SELECTOR.method);
    // int c;
    // c = Utilities.compare(v0, v1);
    // if (c != 0) return c; // or -c if descending
    // ...
    // return 0;
    BlockBuilder body = new BlockBuilder();
    final ParameterExpression parameterV0 = Expressions.parameter(javaRowClass, "v0");
    final ParameterExpression parameterV1 = Expressions.parameter(javaRowClass, "v1");
    final ParameterExpression parameterC = Expressions.parameter(int.class, "c");
    final int mod = collations.size() == 1 ? Modifier.FINAL : 0;
    body.add(Expressions.declare(mod, parameterC, null));
    for (RelFieldCollation collation : collations) {
        final int index = collation.getFieldIndex();
        Expression arg0 = fieldReference(parameterV0, index);
        Expression arg1 = fieldReference(parameterV1, index);
        switch(Primitive.flavor(fieldClass(index))) {
            case OBJECT:
                arg0 = Types.castIfNecessary(Comparable.class, arg0);
                arg1 = Types.castIfNecessary(Comparable.class, arg1);
        }
        final boolean nullsFirst = collation.nullDirection == RelFieldCollation.NullDirection.FIRST;
        final boolean descending = collation.getDirection() == RelFieldCollation.Direction.DESCENDING;
        final Method method = (fieldNullable(index) ? (nullsFirst ^ descending ? BuiltInMethod.COMPARE_NULLS_FIRST : BuiltInMethod.COMPARE_NULLS_LAST) : BuiltInMethod.COMPARE).method;
        body.add(Expressions.statement(Expressions.assign(parameterC, Expressions.call(method.getDeclaringClass(), method.getName(), arg0, arg1))));
        body.add(Expressions.ifThen(Expressions.notEqual(parameterC, Expressions.constant(0)), Expressions.return_(null, descending ? Expressions.negate(parameterC) : parameterC)));
    }
    body.add(Expressions.return_(null, Expressions.constant(0)));
    final List<MemberDeclaration> memberDeclarations = Expressions.<MemberDeclaration>list(Expressions.methodDecl(Modifier.PUBLIC, int.class, "compare", ImmutableList.of(parameterV0, parameterV1), body.toBlock()));
    if (EnumerableRules.BRIDGE_METHODS) {
        final ParameterExpression parameterO0 = Expressions.parameter(Object.class, "o0");
        final ParameterExpression parameterO1 = Expressions.parameter(Object.class, "o1");
        BlockBuilder bridgeBody = new BlockBuilder();
        bridgeBody.add(Expressions.return_(null, Expressions.call(Expressions.parameter(Comparable.class, "this"), BuiltInMethod.COMPARATOR_COMPARE.method, Expressions.convert_(parameterO0, javaRowClass), Expressions.convert_(parameterO1, javaRowClass))));
        memberDeclarations.add(overridingMethodDecl(BuiltInMethod.COMPARATOR_COMPARE.method, ImmutableList.of(parameterO0, parameterO1), bridgeBody.toBlock()));
    }
    return Pair.<Expression, Expression>of(selector, Expressions.new_(Comparator.class, Collections.<Expression>emptyList(), memberDeclarations));
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) MemberDeclaration(org.apache.calcite.linq4j.tree.MemberDeclaration) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) Function1(org.apache.calcite.linq4j.function.Function1) BuiltInMethod(org.apache.calcite.util.BuiltInMethod) Method(java.lang.reflect.Method) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder) Comparator(java.util.Comparator)

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