Search in sources :

Example 1 with EnumerableRelImplementor

use of org.apache.calcite.adapter.enumerable.EnumerableRelImplementor in project calcite by apache.

the class MongoToEnumerableConverter method implement.

public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
    // Generates a call to "find" or "aggregate", depending upon whether
    // an aggregate is present.
    // 
    // ((MongoTable) schema.getTable("zips")).find(
    // "{state: 'CA'}",
    // "{city: 1, zipcode: 1}")
    // 
    // ((MongoTable) schema.getTable("zips")).aggregate(
    // "{$filter: {state: 'CA'}}",
    // "{$group: {_id: '$city', c: {$sum: 1}, p: {$sum: "$pop"}}")
    final BlockBuilder list = new BlockBuilder();
    final MongoRel.Implementor mongoImplementor = new MongoRel.Implementor();
    mongoImplementor.visitChild(0, getInput());
    int aggCount = 0;
    int findCount = 0;
    String project = null;
    String filter = null;
    for (Pair<String, String> op : mongoImplementor.list) {
        if (op.left == null) {
            ++aggCount;
        }
        if (op.right.startsWith("{$match:")) {
            filter = op.left;
            ++findCount;
        }
        if (op.right.startsWith("{$project:")) {
            project = op.left;
            ++findCount;
        }
    }
    final RelDataType rowType = getRowType();
    final PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), rowType, pref.prefer(JavaRowFormat.ARRAY));
    final Expression fields = list.append("fields", constantArrayList(Pair.zip(MongoRules.mongoFieldNames(rowType), new AbstractList<Class>() {

        @Override
        public Class get(int index) {
            return physType.fieldClass(index);
        }

        @Override
        public int size() {
            return rowType.getFieldCount();
        }
    }), Pair.class));
    final Expression table = list.append("table", mongoImplementor.table.getExpression(MongoTable.MongoQueryable.class));
    List<String> opList = Pair.right(mongoImplementor.list);
    final Expression ops = list.append("ops", constantArrayList(opList, String.class));
    Expression enumerable = list.append("enumerable", Expressions.call(table, MongoMethod.MONGO_QUERYABLE_AGGREGATE.method, fields, ops));
    if (CalcitePrepareImpl.DEBUG) {
        System.out.println("Mongo: " + opList);
    }
    Hook.QUERY_PLAN.run(opList);
    list.add(Expressions.return_(null, enumerable));
    return implementor.result(physType, list.toBlock());
}
Also used : EnumerableRelImplementor(org.apache.calcite.adapter.enumerable.EnumerableRelImplementor) RelDataType(org.apache.calcite.rel.type.RelDataType) PhysType(org.apache.calcite.adapter.enumerable.PhysType) Expression(org.apache.calcite.linq4j.tree.Expression) MethodCallExpression(org.apache.calcite.linq4j.tree.MethodCallExpression) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder) Pair(org.apache.calcite.util.Pair)

Example 2 with EnumerableRelImplementor

use of org.apache.calcite.adapter.enumerable.EnumerableRelImplementor in project drill by apache.

the class ElasticSearchEnumerablePrelContext method generateCode.

@Override
public String generateCode(RelOptCluster cluster, RelNode elasticNode) {
    RelNode enumerableRel = CalciteUtils.getElasticsearchToEnumerableConverterRule().convert(elasticNode);
    ClassDeclaration classDeclaration = new EnumerableRelImplementor(cluster.getRexBuilder(), Collections.emptyMap()).implementRoot((EnumerableRel) enumerableRel, EnumerableRel.Prefer.ARRAY);
    return Expressions.toString(Collections.singletonList(classDeclaration), "\n", false);
}
Also used : ClassDeclaration(org.apache.calcite.linq4j.tree.ClassDeclaration) RelNode(org.apache.calcite.rel.RelNode) EnumerableRelImplementor(org.apache.calcite.adapter.enumerable.EnumerableRelImplementor)

Example 3 with EnumerableRelImplementor

use of org.apache.calcite.adapter.enumerable.EnumerableRelImplementor in project calcite by apache.

the class CassandraToEnumerableConverter method implement.

public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
    // Generates a call to "query" with the appropriate fields and predicates
    final BlockBuilder list = new BlockBuilder();
    final CassandraRel.Implementor cassandraImplementor = new CassandraRel.Implementor();
    cassandraImplementor.visitChild(0, getInput());
    final RelDataType rowType = getRowType();
    final PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), rowType, pref.prefer(JavaRowFormat.ARRAY));
    final Expression fields = list.append("fields", constantArrayList(Pair.zip(CassandraRules.cassandraFieldNames(rowType), new AbstractList<Class>() {

        @Override
        public Class get(int index) {
            return physType.fieldClass(index);
        }

        @Override
        public int size() {
            return rowType.getFieldCount();
        }
    }), Pair.class));
    List<Map.Entry<String, String>> selectList = new ArrayList<Map.Entry<String, String>>();
    for (Map.Entry<String, String> entry : Pair.zip(cassandraImplementor.selectFields.keySet(), cassandraImplementor.selectFields.values())) {
        selectList.add(entry);
    }
    final Expression selectFields = list.append("selectFields", constantArrayList(selectList, Pair.class));
    final Expression table = list.append("table", cassandraImplementor.table.getExpression(CassandraTable.CassandraQueryable.class));
    final Expression predicates = list.append("predicates", constantArrayList(cassandraImplementor.whereClause, String.class));
    final Expression order = list.append("order", constantArrayList(cassandraImplementor.order, String.class));
    final Expression offset = list.append("offset", Expressions.constant(cassandraImplementor.offset));
    final Expression fetch = list.append("fetch", Expressions.constant(cassandraImplementor.fetch));
    Expression enumerable = list.append("enumerable", Expressions.call(table, CassandraMethod.CASSANDRA_QUERYABLE_QUERY.method, fields, selectFields, predicates, order, offset, fetch));
    if (CalcitePrepareImpl.DEBUG) {
        System.out.println("Cassandra: " + predicates);
    }
    Hook.QUERY_PLAN.run(predicates);
    list.add(Expressions.return_(null, enumerable));
    return implementor.result(physType, list.toBlock());
}
Also used : EnumerableRelImplementor(org.apache.calcite.adapter.enumerable.EnumerableRelImplementor) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) PhysType(org.apache.calcite.adapter.enumerable.PhysType) Expression(org.apache.calcite.linq4j.tree.Expression) MethodCallExpression(org.apache.calcite.linq4j.tree.MethodCallExpression) Map(java.util.Map) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder) Pair(org.apache.calcite.util.Pair)

Example 4 with EnumerableRelImplementor

use of org.apache.calcite.adapter.enumerable.EnumerableRelImplementor in project calcite by apache.

the class ElasticsearchToEnumerableConverter method implement.

@Override
public Result implement(EnumerableRelImplementor implementor, Prefer prefer) {
    final BlockBuilder list = new BlockBuilder();
    final ElasticsearchRel.Implementor elasticsearchImplementor = new ElasticsearchRel.Implementor();
    elasticsearchImplementor.visitChild(0, getInput());
    final RelDataType rowType = getRowType();
    final PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), rowType, prefer.prefer(JavaRowFormat.ARRAY));
    final Expression fields = list.append("fields", constantArrayList(Pair.zip(ElasticsearchRules.elasticsearchFieldNames(rowType), new AbstractList<Class>() {

        @Override
        public Class get(int index) {
            return physType.fieldClass(index);
        }

        @Override
        public int size() {
            return rowType.getFieldCount();
        }
    }), Pair.class));
    final Expression table = list.append("table", elasticsearchImplementor.table.getExpression(AbstractElasticsearchTable.ElasticsearchQueryable.class));
    List<String> opList = elasticsearchImplementor.list;
    final Expression ops = list.append("ops", constantArrayList(opList, String.class));
    Expression enumerable = list.append("enumerable", Expressions.call(table, ElasticsearchMethod.ELASTICSEARCH_QUERYABLE_FIND.method, ops, fields));
    if (CalcitePrepareImpl.DEBUG) {
        System.out.println("Elasticsearch: " + opList);
    }
    Hook.QUERY_PLAN.run(opList);
    list.add(Expressions.return_(null, enumerable));
    return implementor.result(physType, list.toBlock());
}
Also used : EnumerableRelImplementor(org.apache.calcite.adapter.enumerable.EnumerableRelImplementor) RelDataType(org.apache.calcite.rel.type.RelDataType) PhysType(org.apache.calcite.adapter.enumerable.PhysType) Expression(org.apache.calcite.linq4j.tree.Expression) MethodCallExpression(org.apache.calcite.linq4j.tree.MethodCallExpression) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder) Pair(org.apache.calcite.util.Pair)

Example 5 with EnumerableRelImplementor

use of org.apache.calcite.adapter.enumerable.EnumerableRelImplementor in project calcite by apache.

the class PigToEnumerableConverter method implement.

/**
 * {@inheritDoc}
 *
 * <p>This implementation does not actually execute the associated Pig Latin
 * script and return results. Instead it returns an empty
 * {@link org.apache.calcite.adapter.enumerable.EnumerableRel.Result}
 * in order to allow for testing and verification of every step of query
 * processing up to actual physical execution and result verification.
 *
 * <p>Next step is to invoke Pig from here, likely in local mode, have it
 * store results in a predefined file so they can be read here and returned as
 * a {@code Result} object.
 */
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
    final BlockBuilder list = new BlockBuilder();
    final PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), rowType, pref.prefer(JavaRowFormat.ARRAY));
    PigRel.Implementor impl = new PigRel.Implementor();
    impl.visitChild(0, getInput());
    // for script validation in tests
    Hook.QUERY_PLAN.run(impl.getScript());
    list.add(Expressions.return_(null, Expressions.call(BuiltInMethod.EMPTY_ENUMERABLE.method)));
    return implementor.result(physType, list.toBlock());
}
Also used : PhysType(org.apache.calcite.adapter.enumerable.PhysType) EnumerableRelImplementor(org.apache.calcite.adapter.enumerable.EnumerableRelImplementor) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Aggregations

EnumerableRelImplementor (org.apache.calcite.adapter.enumerable.EnumerableRelImplementor)6 PhysType (org.apache.calcite.adapter.enumerable.PhysType)4 BlockBuilder (org.apache.calcite.linq4j.tree.BlockBuilder)4 Expression (org.apache.calcite.linq4j.tree.Expression)3 MethodCallExpression (org.apache.calcite.linq4j.tree.MethodCallExpression)3 RelDataType (org.apache.calcite.rel.type.RelDataType)3 Pair (org.apache.calcite.util.Pair)3 ClassDeclaration (org.apache.calcite.linq4j.tree.ClassDeclaration)2 RelNode (org.apache.calcite.rel.RelNode)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1