Search in sources :

Example 81 with RelNode

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

the class RelBuilderTest method testFilterCastAny.

@Test
public void testFilterCastAny() {
    final RelBuilder builder = RelBuilder.create(config().build());
    final RelDataType anyType = builder.getTypeFactory().createSqlType(SqlTypeName.ANY);
    final RelNode root = builder.scan("EMP").filter(builder.cast(builder.getRexBuilder().makeInputRef(anyType, 0), SqlTypeName.BOOLEAN)).build();
    final String expected = "" + "LogicalFilter(condition=[CAST($0):BOOLEAN NOT NULL])\n" + "  LogicalTableScan(table=[[scott, EMP]])\n";
    assertThat(root, hasTree(expected));
}
Also used : RelBuilder(org.apache.calcite.tools.RelBuilder) RelNode(org.apache.calcite.rel.RelNode) RelDataType(org.apache.calcite.rel.type.RelDataType) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 82 with RelNode

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

the class RelBuilderTest method testJoin.

@Test
public void testJoin() {
    // Equivalent SQL:
    // SELECT *
    // FROM (SELECT * FROM emp WHERE comm IS NULL)
    // JOIN dept ON emp.deptno = dept.deptno
    final RelBuilder builder = RelBuilder.create(config().build());
    RelNode root = builder.scan("EMP").filter(builder.call(SqlStdOperatorTable.IS_NULL, builder.field("COMM"))).scan("DEPT").join(JoinRelType.INNER, builder.call(SqlStdOperatorTable.EQUALS, builder.field(2, 0, "DEPTNO"), builder.field(2, 1, "DEPTNO"))).build();
    final String expected = "" + "LogicalJoin(condition=[=($7, $8)], joinType=[inner])\n" + "  LogicalFilter(condition=[IS NULL($6)])\n" + "    LogicalTableScan(table=[[scott, EMP]])\n" + "  LogicalTableScan(table=[[scott, DEPT]])\n";
    assertThat(root, hasTree(expected));
}
Also used : RelBuilder(org.apache.calcite.tools.RelBuilder) RelNode(org.apache.calcite.rel.RelNode) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 83 with RelNode

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

the class RelBuilderTest method testAggregate3.

/**
 * Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-2192">[CALCITE-2192]
 * RelBuilder wrongly skips creation of Aggregate that prunes columns if input
 * is unique</a>.
 */
@Test
public void testAggregate3() {
    // Equivalent SQL:
    // SELECT DISTINCT deptno FROM (
    // SELECT deptno, COUNT(*)
    // FROM emp
    // GROUP BY deptno)
    final RelBuilder builder = RelBuilder.create(config().build());
    RelNode root = builder.scan("EMP").aggregate(builder.groupKey(builder.field(1)), builder.aggregateCall(SqlStdOperatorTable.COUNT, false, false, null, "C")).aggregate(builder.groupKey(builder.field(0))).build();
    final String expected = "" + "LogicalProject(ENAME=[$0])\n" + "  LogicalAggregate(group=[{1}], C=[COUNT()])\n" + "    LogicalTableScan(table=[[scott, EMP]])\n";
    assertThat(root, hasTree(expected));
}
Also used : RelBuilder(org.apache.calcite.tools.RelBuilder) RelNode(org.apache.calcite.rel.RelNode) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 84 with RelNode

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

the class RelBuilderTest method testAliasLimit.

@Test
public void testAliasLimit() {
    final RelBuilder builder = RelBuilder.create(config().build());
    RelNode root = builder.scan("EMP").as("e").sort(1).sortLimit(10, // aliases were lost here if preceded by sort()
    20).project(builder.field("e", "EMPNO")).build();
    final String expected = "" + "LogicalProject(EMPNO=[$0])\n" + "  LogicalSort(sort0=[$1], dir0=[ASC], offset=[10], fetch=[20])\n" + "    LogicalTableScan(table=[[scott, EMP]])\n";
    assertThat(root, hasTree(expected));
}
Also used : RelBuilder(org.apache.calcite.tools.RelBuilder) RelNode(org.apache.calcite.rel.RelNode) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 85 with RelNode

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

the class RelBuilderTest method testSortThenLimit.

/**
 * Tests that a sort on a field followed by a limit gives the same
 * effect as calling sortLimit.
 *
 * <p>In general a relational operator cannot rely on the order of its input,
 * but it is reasonable to merge sort and limit if they were created by
 * consecutive builder operations. And clients such as Piglet rely on it.
 */
@Test
public void testSortThenLimit() {
    final RelBuilder builder = RelBuilder.create(config().build());
    final RelNode root = builder.scan("EMP").sort(builder.desc(builder.field("DEPTNO"))).limit(-1, 10).build();
    final String expected = "" + "LogicalSort(sort0=[$7], dir0=[DESC], fetch=[10])\n" + "  LogicalTableScan(table=[[scott, EMP]])\n";
    assertThat(root, hasTree(expected));
    final RelNode root2 = builder.scan("EMP").sortLimit(-1, 10, builder.desc(builder.field("DEPTNO"))).build();
    assertThat(root2, hasTree(expected));
}
Also used : RelBuilder(org.apache.calcite.tools.RelBuilder) RelNode(org.apache.calcite.rel.RelNode) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

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