use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode in project calcite by apache.
the class LogicalWindow method toInputRefs.
private static List<RexNode> toInputRefs(final List<? extends RexNode> operands) {
return new AbstractList<RexNode>() {
public int size() {
return operands.size();
}
public RexNode get(int index) {
final RexNode operand = operands.get(index);
if (operand instanceof RexInputRef) {
return operand;
}
assert operand instanceof RexLocalRef;
final RexLocalRef ref = (RexLocalRef) operand;
return new RexInputRef(ref.getIndex(), ref.getType());
}
};
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode 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.rex.RexNode 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.rex.RexNode 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.rex.RexNode in project calcite by apache.
the class Sort method accept.
public RelNode accept(RexShuttle shuttle) {
RexNode offset = shuttle.apply(this.offset);
RexNode fetch = shuttle.apply(this.fetch);
List<RexNode> fieldExps = shuttle.apply(this.fieldExps);
assert fieldExps == this.fieldExps : "Sort node does not support modification of input field expressions." + " Old expressions: " + this.fieldExps + ", new ones: " + fieldExps;
if (offset == this.offset && fetch == this.fetch) {
return this;
}
return copy(traitSet, getInput(), collation, offset, fetch);
}
Aggregations