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;
}
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);
}
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)));
}
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)));
}
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;
}
Aggregations