Search in sources :

Example 26 with Pair

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.Pair in project calcite by apache.

the class SparkRules method main.

// Play area
public static void main(String[] args) {
    final JavaSparkContext sc = new JavaSparkContext("local[1]", "calcite");
    final JavaRDD<String> file = sc.textFile("/usr/share/dict/words");
    System.out.println(file.map(new Function<String, Object>() {

        @Override
        public Object call(String s) throws Exception {
            return s.substring(0, Math.min(s.length(), 1));
        }
    }).distinct().count());
    file.cache();
    String s = file.groupBy(new Function<String, String>() {

        @Override
        public String call(String s) throws Exception {
            return s.substring(0, Math.min(s.length(), 1));
        }
    }).map(new Function<Tuple2<String, Iterable<String>>, Object>() {

        @Override
        public Object call(Tuple2<String, Iterable<String>> pair) {
            return pair._1() + ":" + Iterables.size(pair._2());
        }
    }).collect().toString();
    System.out.print(s);
    final JavaRDD<Integer> rdd = sc.parallelize(new AbstractList<Integer>() {

        final Random random = new Random();

        @Override
        public Integer get(int index) {
            System.out.println("get(" + index + ")");
            return random.nextInt(100);
        }

        @Override
        public int size() {
            System.out.println("size");
            return 10;
        }
    });
    System.out.println(rdd.groupBy(new Function<Integer, Integer>() {

        public Integer call(Integer integer) {
            return integer % 2;
        }
    }).collect().toString());
    System.out.println(file.flatMap(new FlatMapFunction<String, Pair<String, Integer>>() {

        public Iterator<Pair<String, Integer>> call(String x) {
            if (!x.startsWith("a")) {
                return Collections.emptyIterator();
            }
            return Collections.singletonList(Pair.of(x.toUpperCase(Locale.ROOT), x.length())).iterator();
        }
    }).take(5).toString());
}
Also used : FlatMapFunction(org.apache.spark.api.java.function.FlatMapFunction) Function(org.apache.spark.api.java.function.Function) Random(java.util.Random) Tuple2(scala.Tuple2) FlatMapFunction(org.apache.spark.api.java.function.FlatMapFunction) JavaSparkContext(org.apache.spark.api.java.JavaSparkContext) Pair(org.apache.calcite.util.Pair)

Example 27 with Pair

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.Pair 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 28 with Pair

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.Pair in project calcite by apache.

the class VolcanoPlanner method registerMaterializations.

private void registerMaterializations() {
    // Avoid using materializations while populating materializations!
    final CalciteConnectionConfig config = context.unwrap(CalciteConnectionConfig.class);
    if (config == null || !config.materializationsEnabled()) {
        return;
    }
    // Register rels using materialized views.
    final List<Pair<RelNode, List<RelOptMaterialization>>> materializationUses = RelOptMaterializations.useMaterializedViews(originalRoot, materializations);
    for (Pair<RelNode, List<RelOptMaterialization>> use : materializationUses) {
        RelNode rel = use.left;
        Hook.SUB.run(rel);
        registerImpl(rel, root.set);
    }
    // Register table rels of materialized views that cannot find a substitution
    // in root rel transformation but can potentially be useful.
    final Set<RelOptMaterialization> applicableMaterializations = new HashSet<>(RelOptMaterializations.getApplicableMaterializations(originalRoot, materializations));
    for (Pair<RelNode, List<RelOptMaterialization>> use : materializationUses) {
        applicableMaterializations.removeAll(use.right);
    }
    for (RelOptMaterialization materialization : applicableMaterializations) {
        RelSubset subset = registerImpl(materialization.queryRel, null);
        RelNode tableRel2 = RelOptUtil.createCastRel(materialization.tableRel, materialization.queryRel.getRowType(), true);
        registerImpl(tableRel2, subset.set);
    }
    // Register rels using lattices.
    final List<Pair<RelNode, RelOptLattice>> latticeUses = RelOptMaterializations.useLattices(originalRoot, ImmutableList.copyOf(latticeByName.values()));
    if (!latticeUses.isEmpty()) {
        RelNode rel = latticeUses.get(0).left;
        Hook.SUB.run(rel);
        registerImpl(rel, root.set);
    }
}
Also used : RelNode(org.apache.calcite.rel.RelNode) CalciteConnectionConfig(org.apache.calcite.config.CalciteConnectionConfig) RelOptMaterialization(org.apache.calcite.plan.RelOptMaterialization) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Pair(org.apache.calcite.util.Pair) HashSet(java.util.HashSet)

Example 29 with Pair

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.Pair in project calcite by apache.

the class RelOptUtil method createRenameRel.

// to be removed before 2.0
@Deprecated
public static RelNode createRenameRel(RelDataType outputType, RelNode rel) {
    RelDataType inputType = rel.getRowType();
    List<RelDataTypeField> inputFields = inputType.getFieldList();
    int n = inputFields.size();
    List<RelDataTypeField> outputFields = outputType.getFieldList();
    assert outputFields.size() == n : "rename: field count mismatch: in=" + inputType + ", out" + outputType;
    final List<Pair<RexNode, String>> renames = new ArrayList<>();
    for (Pair<RelDataTypeField, RelDataTypeField> pair : Pair.zip(inputFields, outputFields)) {
        final RelDataTypeField inputField = pair.left;
        final RelDataTypeField outputField = pair.right;
        assert inputField.getType().equals(outputField.getType());
        final RexBuilder rexBuilder = rel.getCluster().getRexBuilder();
        renames.add(Pair.<RexNode, String>of(rexBuilder.makeInputRef(inputField.getType(), inputField.getIndex()), outputField.getName()));
    }
    final RelBuilder relBuilder = RelFactories.LOGICAL_BUILDER.create(rel.getCluster(), null);
    return relBuilder.push(rel).project(Pair.left(renames), Pair.right(renames), true).build();
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelBuilder(org.apache.calcite.tools.RelBuilder) ArrayList(java.util.ArrayList) RexBuilder(org.apache.calcite.rex.RexBuilder) RelDataType(org.apache.calcite.rel.type.RelDataType) Pair(org.apache.calcite.util.Pair)

Example 30 with Pair

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.Pair in project calcite by apache.

the class RelOptMaterializations method useLattices.

/**
 * Returns a list of RelNode transformed from all possible lattice uses.
 * @param rel       the original RelNode
 * @param lattices  the lattice list
 * @return the list of transformed RelNode together with their corresponding
 *         lattice used in the transformation.
 */
public static List<Pair<RelNode, RelOptLattice>> useLattices(final RelNode rel, List<RelOptLattice> lattices) {
    final Set<RelOptTable> queryTables = RelOptUtil.findTables(rel);
    // Use a lattice if the query uses at least the central (fact) table of the
    // lattice.
    final List<Pair<RelNode, RelOptLattice>> latticeUses = Lists.newArrayList();
    final Set<List<String>> queryTableNames = Sets.newHashSet(Iterables.transform(queryTables, GET_QUALIFIED_NAME));
    // Remember leaf-join form of root so we convert at most once.
    final Supplier<RelNode> leafJoinRoot = Suppliers.memoize(new Supplier<RelNode>() {

        public RelNode get() {
            return RelOptMaterialization.toLeafJoinForm(rel);
        }
    });
    for (RelOptLattice lattice : lattices) {
        if (queryTableNames.contains(lattice.rootTable().getQualifiedName())) {
            RelNode rel2 = lattice.rewrite(leafJoinRoot.get());
            if (rel2 != null) {
                if (CalcitePrepareImpl.DEBUG) {
                    System.out.println("use lattice:\n" + RelOptUtil.toString(rel2));
                }
                latticeUses.add(Pair.of(rel2, lattice));
            }
        }
    }
    return latticeUses;
}
Also used : RelNode(org.apache.calcite.rel.RelNode) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Pair(org.apache.calcite.util.Pair)

Aggregations

Pair (org.apache.calcite.util.Pair)112 RexNode (org.apache.calcite.rex.RexNode)72 ArrayList (java.util.ArrayList)70 RelNode (org.apache.calcite.rel.RelNode)59 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)55 RexInputRef (org.apache.calcite.rex.RexInputRef)29 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)29 HashMap (java.util.HashMap)26 RexBuilder (org.apache.calcite.rex.RexBuilder)23 Map (java.util.Map)21 AggregateCall (org.apache.calcite.rel.core.AggregateCall)20 List (java.util.List)19 RelDataType (org.apache.calcite.rel.type.RelDataType)19 ImmutableList (com.google.common.collect.ImmutableList)18 JoinRelType (org.apache.calcite.rel.core.JoinRelType)16 TreeMap (java.util.TreeMap)14 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)13 RelBuilder (org.apache.calcite.tools.RelBuilder)13 ImmutableMap (com.google.common.collect.ImmutableMap)12 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)12