use of org.apache.calcite.rel.metadata.RelMetadataQuery in project calcite by apache.
the class RelMetadataTest method assertUniqueConsistent.
/**
* Asserts that {@link RelMetadataQuery#getUniqueKeys(RelNode)}
* and {@link RelMetadataQuery#areColumnsUnique(RelNode, ImmutableBitSet)}
* return consistent results.
*/
private void assertUniqueConsistent(RelNode rel) {
final RelMetadataQuery mq = RelMetadataQuery.instance();
final Set<ImmutableBitSet> uniqueKeys = mq.getUniqueKeys(rel);
final ImmutableBitSet allCols = ImmutableBitSet.range(0, rel.getRowType().getFieldCount());
for (ImmutableBitSet key : allCols.powerSet()) {
Boolean result2 = mq.areColumnsUnique(rel, key);
assertTrue(result2 == null || result2 == isUnique(uniqueKeys, key));
}
}
use of org.apache.calcite.rel.metadata.RelMetadataQuery in project calcite by apache.
the class RelMetadataTest method checkFilterSelectivity.
private void checkFilterSelectivity(String sql, double expected) {
RelNode rel = convertSql(sql);
final RelMetadataQuery mq = RelMetadataQuery.instance();
Double result = mq.getSelectivity(rel, null);
assertTrue(result != null);
assertEquals(expected, result, EPSILON);
}
use of org.apache.calcite.rel.metadata.RelMetadataQuery in project calcite by apache.
the class RelMetadataTest method testPullUpPredicatesOnNullableConstant.
@Test
public void testPullUpPredicatesOnNullableConstant() {
final String sql = "select nullif(1, 1) as c\n" + " from emp\n" + " where mgr is null and deptno < 10";
final RelNode rel = convertSql(sql);
final RelMetadataQuery mq = RelMetadataQuery.instance();
RelOptPredicateList list = mq.getPulledUpPredicates(rel);
// Uses "IS NOT DISTINCT FROM" rather than "=" because cannot guarantee not null.
assertThat(list.pulledUpPredicates, sortsAs("[IS NOT DISTINCT FROM($0, CASE(=(1, 1), null, 1))]"));
}
use of org.apache.calcite.rel.metadata.RelMetadataQuery in project calcite by apache.
the class RelMetadataTest method testExpressionLineageTwoColumnsSwapped.
@Test
public void testExpressionLineageTwoColumnsSwapped() {
// deptno is column 7 in catalog.sales.emp
// mgr is column 3 in catalog.sales.emp
final RelNode rel = convertSql("select deptno, mgr from emp");
final RelMetadataQuery mq = RelMetadataQuery.instance();
final RexNode ref1 = RexInputRef.of(0, rel.getRowType().getFieldList());
final Set<RexNode> r1 = mq.getExpressionLineage(rel, ref1);
assertThat(r1.size(), is(1));
final RexTableInputRef result1 = (RexTableInputRef) r1.iterator().next();
assertTrue(result1.getQualifiedName().equals(EMP_QNAME));
assertThat(result1.getIndex(), is(7));
final RexNode ref2 = RexInputRef.of(1, rel.getRowType().getFieldList());
final Set<RexNode> r2 = mq.getExpressionLineage(rel, ref2);
assertThat(r2.size(), is(1));
final RexTableInputRef result2 = (RexTableInputRef) r2.iterator().next();
assertTrue(result2.getQualifiedName().equals(EMP_QNAME));
assertThat(result2.getIndex(), is(3));
assertThat(result1.getIdentifier(), is(result2.getIdentifier()));
}
use of org.apache.calcite.rel.metadata.RelMetadataQuery in project calcite by apache.
the class RelMetadataTest method testUnion.
@Test
public void testUnion() {
RelNode rel = convertSql("select deptno from emp\n" + "union\n" + "select deptno from dept");
final RelMetadataQuery mq = RelMetadataQuery.instance();
final Set<ImmutableBitSet> result = mq.getUniqueKeys(rel);
assertThat(result, CoreMatchers.<Set<ImmutableBitSet>>equalTo(ImmutableSet.of(ImmutableBitSet.of(0))));
assertUniqueConsistent(rel);
}
Aggregations