Search in sources :

Example 61 with RelNode

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode in project calcite by apache.

the class RelMdRowCount method getRowCount.

public Double getRowCount(Union rel, RelMetadataQuery mq) {
    double rowCount = 0.0;
    for (RelNode input : rel.getInputs()) {
        Double partialRowCount = mq.getRowCount(input);
        if (partialRowCount == null) {
            return null;
        }
        rowCount += partialRowCount;
    }
    return rowCount;
}
Also used : RelNode(org.apache.calcite.rel.RelNode)

Example 62 with RelNode

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode in project calcite by apache.

the class ReflectiveRelMetadataProvider method reflectiveSource.

private static RelMetadataProvider reflectiveSource(final MetadataHandler target, final ImmutableList<Method> methods) {
    final Space2 space = Space2.create(target, methods);
    // This needs to be a concurrent map since RelMetadataProvider are cached in static
    // fields, thus the map is subject to concurrent modifications later.
    // See map.put in org.apache.calcite.rel.metadata.ReflectiveRelMetadataProvider.apply(
    // java.lang.Class<? extends org.apache.calcite.rel.RelNode>)
    final ConcurrentMap<Class<RelNode>, UnboundMetadata> methodsMap = new ConcurrentHashMap<>();
    for (Class<RelNode> key : space.classes) {
        ImmutableNullableList.Builder<Method> builder = ImmutableNullableList.builder();
        for (final Method method : methods) {
            builder.add(space.find(key, method));
        }
        final List<Method> handlerMethods = builder.build();
        final UnboundMetadata function = new UnboundMetadata() {

            public Metadata bind(final RelNode rel, final RelMetadataQuery mq) {
                return (Metadata) Proxy.newProxyInstance(space.metadataClass0.getClassLoader(), new Class[] { space.metadataClass0 }, new InvocationHandler() {

                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        // new SelectivityImpl().selectivity(filter, rex)
                        if (method.equals(BuiltInMethod.METADATA_REL.method)) {
                            return rel;
                        }
                        if (method.equals(BuiltInMethod.OBJECT_TO_STRING.method)) {
                            return space.metadataClass0.getSimpleName() + "(" + rel + ")";
                        }
                        int i = methods.indexOf(method);
                        if (i < 0) {
                            throw new AssertionError("not handled: " + method + " for " + rel);
                        }
                        final Method handlerMethod = handlerMethods.get(i);
                        if (handlerMethod == null) {
                            throw new AssertionError("not handled: " + method + " for " + rel);
                        }
                        final Object[] args1;
                        final List key;
                        if (args == null) {
                            args1 = new Object[] { rel, mq };
                            key = FlatLists.of(rel, method);
                        } else {
                            args1 = new Object[args.length + 2];
                            args1[0] = rel;
                            args1[1] = mq;
                            System.arraycopy(args, 0, args1, 2, args.length);
                            final Object[] args2 = args1.clone();
                            // replace RelMetadataQuery with method
                            args2[1] = method;
                            for (int j = 0; j < args2.length; j++) {
                                if (args2[j] == null) {
                                    args2[j] = NullSentinel.INSTANCE;
                                } else if (args2[j] instanceof RexNode) {
                                    // Can't use RexNode.equals - it is not deep
                                    args2[j] = args2[j].toString();
                                }
                            }
                            key = FlatLists.copyOf(args2);
                        }
                        if (mq.map.put(key, NullSentinel.INSTANCE) != null) {
                            throw CyclicMetadataException.INSTANCE;
                        }
                        try {
                            return handlerMethod.invoke(target, args1);
                        } catch (InvocationTargetException | UndeclaredThrowableException e) {
                            Util.throwIfUnchecked(e.getCause());
                            throw new RuntimeException(e.getCause());
                        } finally {
                            mq.map.remove(key);
                        }
                    }
                });
            }
        };
        methodsMap.put(key, function);
    }
    return new ReflectiveRelMetadataProvider(methodsMap, space.metadataClass0, space.providerMap);
}
Also used : ImmutableNullableList(org.apache.calcite.util.ImmutableNullableList) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) ImmutableNullableList(org.apache.calcite.util.ImmutableNullableList) List(java.util.List) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BuiltInMethod(org.apache.calcite.util.BuiltInMethod) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) InvocationTargetException(java.lang.reflect.InvocationTargetException) RelNode(org.apache.calcite.rel.RelNode) RexNode(org.apache.calcite.rex.RexNode)

Example 63 with RelNode

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode in project calcite by apache.

the class RelMdAllPredicates method getAllPredicates.

/**
 * Add the Join condition to the list obtained from the input.
 */
public RelOptPredicateList getAllPredicates(Join join, RelMetadataQuery mq) {
    if (join.getJoinType() != JoinRelType.INNER) {
        // We cannot map origin of this expression.
        return null;
    }
    final RexBuilder rexBuilder = join.getCluster().getRexBuilder();
    final RexNode pred = join.getCondition();
    final Multimap<List<String>, RelTableRef> qualifiedNamesToRefs = HashMultimap.create();
    RelOptPredicateList newPreds = RelOptPredicateList.EMPTY;
    for (RelNode input : join.getInputs()) {
        final RelOptPredicateList inputPreds = mq.getAllPredicates(input);
        if (inputPreds == null) {
            // Bail out
            return null;
        }
        // Gather table references
        final Set<RelTableRef> tableRefs = mq.getTableReferences(input);
        if (input == join.getLeft()) {
            // Left input references remain unchanged
            for (RelTableRef leftRef : tableRefs) {
                qualifiedNamesToRefs.put(leftRef.getQualifiedName(), leftRef);
            }
            newPreds = newPreds.union(rexBuilder, inputPreds);
        } else {
            // Right input references might need to be updated if there are table name
            // clashes with left input
            final Map<RelTableRef, RelTableRef> currentTablesMapping = new HashMap<>();
            for (RelTableRef rightRef : tableRefs) {
                int shift = 0;
                Collection<RelTableRef> lRefs = qualifiedNamesToRefs.get(rightRef.getQualifiedName());
                if (lRefs != null) {
                    shift = lRefs.size();
                }
                currentTablesMapping.put(rightRef, RelTableRef.of(rightRef.getTable(), shift + rightRef.getEntityNumber()));
            }
            final List<RexNode> updatedPreds = Lists.newArrayList(Iterables.transform(inputPreds.pulledUpPredicates, new Function<RexNode, RexNode>() {

                @Override
                public RexNode apply(RexNode e) {
                    return RexUtil.swapTableReferences(rexBuilder, e, currentTablesMapping);
                }
            }));
            newPreds = newPreds.union(rexBuilder, RelOptPredicateList.of(rexBuilder, updatedPreds));
        }
    }
    // Extract input fields referenced by Join condition
    final Set<RelDataTypeField> inputExtraFields = new LinkedHashSet<>();
    final RelOptUtil.InputFinder inputFinder = new RelOptUtil.InputFinder(inputExtraFields);
    pred.accept(inputFinder);
    final ImmutableBitSet inputFieldsUsed = inputFinder.inputBitSet.build();
    // Infer column origin expressions for given references
    final Map<RexInputRef, Set<RexNode>> mapping = new LinkedHashMap<>();
    for (int idx : inputFieldsUsed) {
        final RexInputRef inputRef = RexInputRef.of(idx, join.getRowType().getFieldList());
        final Set<RexNode> originalExprs = mq.getExpressionLineage(join, inputRef);
        if (originalExprs == null) {
            // Bail out
            return null;
        }
        final RexInputRef ref = RexInputRef.of(idx, join.getRowType().getFieldList());
        mapping.put(ref, originalExprs);
    }
    // Replace with new expressions and return union of predicates
    return newPreds.union(rexBuilder, RelOptPredicateList.of(rexBuilder, RelMdExpressionLineage.createAllPossibleExpressions(rexBuilder, pred, mapping)));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) LinkedHashSet(java.util.LinkedHashSet) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) Set(java.util.Set) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) LinkedHashMap(java.util.LinkedHashMap) Function(com.google.common.base.Function) RelOptPredicateList(org.apache.calcite.plan.RelOptPredicateList) RexBuilder(org.apache.calcite.rex.RexBuilder) RelOptPredicateList(org.apache.calcite.plan.RelOptPredicateList) List(java.util.List) RelOptUtil(org.apache.calcite.plan.RelOptUtil) RelTableRef(org.apache.calcite.rex.RexTableInputRef.RelTableRef) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelNode(org.apache.calcite.rel.RelNode) RexInputRef(org.apache.calcite.rex.RexInputRef) RexNode(org.apache.calcite.rex.RexNode)

Example 64 with RelNode

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode in project calcite by apache.

the class RelMdAllPredicates method getAllPredicates.

/**
 * Add the Filter condition to the list obtained from the input.
 */
public RelOptPredicateList getAllPredicates(Filter filter, RelMetadataQuery mq) {
    final RelNode input = filter.getInput();
    final RexBuilder rexBuilder = filter.getCluster().getRexBuilder();
    final RexNode pred = filter.getCondition();
    final RelOptPredicateList predsBelow = mq.getAllPredicates(input);
    if (predsBelow == null) {
        // Safety check
        return null;
    }
    // Extract input fields referenced by Filter condition
    final Set<RelDataTypeField> inputExtraFields = new LinkedHashSet<>();
    final RelOptUtil.InputFinder inputFinder = new RelOptUtil.InputFinder(inputExtraFields);
    pred.accept(inputFinder);
    final ImmutableBitSet inputFieldsUsed = inputFinder.inputBitSet.build();
    // Infer column origin expressions for given references
    final Map<RexInputRef, Set<RexNode>> mapping = new LinkedHashMap<>();
    for (int idx : inputFieldsUsed) {
        final RexInputRef ref = RexInputRef.of(idx, filter.getRowType().getFieldList());
        final Set<RexNode> originalExprs = mq.getExpressionLineage(filter, ref);
        if (originalExprs == null) {
            // Bail out
            return null;
        }
        mapping.put(ref, originalExprs);
    }
    // Replace with new expressions and return union of predicates
    return predsBelow.union(rexBuilder, RelOptPredicateList.of(rexBuilder, RelMdExpressionLineage.createAllPossibleExpressions(rexBuilder, pred, mapping)));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) LinkedHashSet(java.util.LinkedHashSet) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) Set(java.util.Set) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) RelOptUtil(org.apache.calcite.plan.RelOptUtil) LinkedHashMap(java.util.LinkedHashMap) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelNode(org.apache.calcite.rel.RelNode) RelOptPredicateList(org.apache.calcite.plan.RelOptPredicateList) RexBuilder(org.apache.calcite.rex.RexBuilder) RexInputRef(org.apache.calcite.rex.RexInputRef) RexNode(org.apache.calcite.rex.RexNode)

Example 65 with RelNode

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode in project calcite by apache.

the class Intersect method estimateRowCount.

@Override
public double estimateRowCount(RelMetadataQuery mq) {
    // REVIEW jvs 30-May-2005:  I just pulled this out of a hat.
    double dRows = Double.MAX_VALUE;
    for (RelNode input : inputs) {
        dRows = Math.min(dRows, mq.getRowCount(input));
    }
    dRows *= 0.25;
    return dRows;
}
Also used : RelNode(org.apache.calcite.rel.RelNode)

Aggregations

RelNode (org.apache.calcite.rel.RelNode)1133 RexNode (org.apache.calcite.rex.RexNode)353 ArrayList (java.util.ArrayList)273 Test (org.junit.Test)239 RelBuilder (org.apache.calcite.tools.RelBuilder)161 RelDataType (org.apache.calcite.rel.type.RelDataType)156 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)142 RexBuilder (org.apache.calcite.rex.RexBuilder)129 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)121 RelTraitSet (org.apache.calcite.plan.RelTraitSet)114 RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)97 RexInputRef (org.apache.calcite.rex.RexInputRef)85 HashMap (java.util.HashMap)82 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)77 Project (org.apache.calcite.rel.core.Project)74 RelOptCluster (org.apache.calcite.plan.RelOptCluster)71 List (java.util.List)67 Pair (org.apache.calcite.util.Pair)67 AggregateCall (org.apache.calcite.rel.core.AggregateCall)63 SqlNode (org.apache.calcite.sql.SqlNode)61