use of org.apache.calcite.rel.metadata.RelMetadataQuery in project calcite by apache.
the class EnumerableLimit method create.
/**
* Creates an EnumerableLimit.
*/
public static EnumerableLimit create(final RelNode input, RexNode offset, RexNode fetch) {
final RelOptCluster cluster = input.getCluster();
final RelMetadataQuery mq = cluster.getMetadataQuery();
final RelTraitSet traitSet = cluster.traitSetOf(EnumerableConvention.INSTANCE).replaceIfs(RelCollationTraitDef.INSTANCE, new Supplier<List<RelCollation>>() {
public List<RelCollation> get() {
return RelMdCollation.limit(mq, input);
}
}).replaceIf(RelDistributionTraitDef.INSTANCE, new Supplier<RelDistribution>() {
public RelDistribution get() {
return RelMdDistribution.limit(mq, input);
}
});
return new EnumerableLimit(cluster, traitSet, input, offset, fetch);
}
use of org.apache.calcite.rel.metadata.RelMetadataQuery in project calcite by apache.
the class EnumerableMergeJoin method create.
public static EnumerableMergeJoin create(RelNode left, RelNode right, RexLiteral condition, ImmutableIntList leftKeys, ImmutableIntList rightKeys, JoinRelType joinType) throws InvalidRelException {
final RelOptCluster cluster = right.getCluster();
RelTraitSet traitSet = cluster.traitSet();
if (traitSet.isEnabled(RelCollationTraitDef.INSTANCE)) {
final RelMetadataQuery mq = cluster.getMetadataQuery();
final List<RelCollation> collations = RelMdCollation.mergeJoin(mq, left, right, leftKeys, rightKeys);
traitSet = traitSet.replace(collations);
}
return new EnumerableMergeJoin(cluster, traitSet, left, right, condition, leftKeys, rightKeys, ImmutableSet.<CorrelationId>of(), joinType);
}
use of org.apache.calcite.rel.metadata.RelMetadataQuery in project calcite by apache.
the class RelOptUtil method containsNullableFields.
/**
* Determines whether any of the fields in a given relational expression may
* contain null values, taking into account constraints on the field types and
* also deduced predicates.
*
* <p>The method is cautious: It may sometimes return {@code true} when the
* actual answer is {@code false}. In particular, it does this when there
* is no executor, or the executor is not a sub-class of
* {@link RexExecutorImpl}.
*/
private static boolean containsNullableFields(RelNode r) {
final RexBuilder rexBuilder = r.getCluster().getRexBuilder();
final RelDataType rowType = r.getRowType();
final List<RexNode> list = new ArrayList<>();
final RelMetadataQuery mq = r.getCluster().getMetadataQuery();
for (RelDataTypeField field : rowType.getFieldList()) {
if (field.getType().isNullable()) {
list.add(rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL, rexBuilder.makeInputRef(field.getType(), field.getIndex())));
}
}
if (list.isEmpty()) {
// All columns are declared NOT NULL.
return false;
}
final RelOptPredicateList predicates = mq.getPulledUpPredicates(r);
if (predicates.pulledUpPredicates.isEmpty()) {
// declared NULL are really NOT NULL.
return true;
}
final RexExecutor executor = r.getCluster().getPlanner().getExecutor();
if (!(executor instanceof RexExecutorImpl)) {
// Cannot proceed without an executor.
return true;
}
final RexImplicationChecker checker = new RexImplicationChecker(rexBuilder, (RexExecutorImpl) executor, rowType);
final RexNode first = RexUtil.composeConjunction(rexBuilder, predicates.pulledUpPredicates, false);
final RexNode second = RexUtil.composeConjunction(rexBuilder, list, false);
// It does, so we have no nullable columns.
return !checker.implies(first, second);
}
use of org.apache.calcite.rel.metadata.RelMetadataQuery in project calcite by apache.
the class RelMetadataTest method testExpressionLineageInnerJoinRight.
@Test
public void testExpressionLineageInnerJoinRight() {
// ename is column 0 in catalog.sales.bonus
final RelNode rel = convertSql("select bonus.ename from emp join bonus using (ename)");
final RelMetadataQuery mq = RelMetadataQuery.instance();
final RexNode ref = RexInputRef.of(0, rel.getRowType().getFieldList());
final Set<RexNode> r = mq.getExpressionLineage(rel, ref);
assertThat(r.size(), is(1));
final RexTableInputRef result = (RexTableInputRef) r.iterator().next();
assertTrue(result.getQualifiedName().equals(ImmutableList.of("CATALOG", "SALES", "BONUS")));
assertThat(result.getIndex(), is(0));
}
use of org.apache.calcite.rel.metadata.RelMetadataQuery in project calcite by apache.
the class RelMetadataTest method testGroupByEmptyUniqueKeys.
@Test
public void testGroupByEmptyUniqueKeys() {
RelNode rel = convertSql("select count(*) from emp");
final RelMetadataQuery mq = RelMetadataQuery.instance();
Set<ImmutableBitSet> result = mq.getUniqueKeys(rel);
assertThat(result, CoreMatchers.<Set<ImmutableBitSet>>equalTo(ImmutableSet.of(ImmutableBitSet.of())));
assertUniqueConsistent(rel);
}
Aggregations