Search in sources :

Example 91 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 testIntersect.

@Test
public void testIntersect() {
    // Equivalent SQL:
    // SELECT empno FROM emp
    // WHERE deptno = 20
    // INTERSECT
    // SELECT deptno FROM dept
    final RelBuilder builder = RelBuilder.create(config().build());
    RelNode root = builder.scan("DEPT").project(builder.field("DEPTNO")).scan("EMP").filter(builder.call(SqlStdOperatorTable.EQUALS, builder.field("DEPTNO"), builder.literal(20))).project(builder.field("EMPNO")).intersect(false).build();
    final String expected = "" + "LogicalIntersect(all=[false])\n" + "  LogicalProject(DEPTNO=[$0])\n" + "    LogicalTableScan(table=[[scott, DEPT]])\n" + "  LogicalProject(EMPNO=[$0])\n" + "    LogicalFilter(condition=[=($7, 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 92 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 testFilterCastNull.

@Test
public void testFilterCastNull() {
    final RelBuilder builder = RelBuilder.create(config().build());
    final RelDataTypeFactory typeFactory = builder.getTypeFactory();
    final RelNode root = builder.scan("EMP").filter(builder.getRexBuilder().makeCast(typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.BOOLEAN), true), builder.equals(builder.field("DEPTNO"), builder.literal(10)))).build();
    final String expected = "" + "LogicalFilter(condition=[=($7, 10)])\n" + "  LogicalTableScan(table=[[scott, EMP]])\n";
    assertThat(root, hasTree(expected));
}
Also used : RelBuilder(org.apache.calcite.tools.RelBuilder) RelNode(org.apache.calcite.rel.RelNode) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 93 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 testUnionAlias.

@Test
public void testUnionAlias() {
    final RelBuilder builder = RelBuilder.create(config().build());
    RelNode root = builder.scan("EMP").as("e1").project(builder.field("EMPNO"), builder.call(SqlStdOperatorTable.CONCAT, builder.field("ENAME"), builder.literal("-1"))).scan("EMP").as("e2").project(builder.field("EMPNO"), builder.call(SqlStdOperatorTable.CONCAT, builder.field("ENAME"), builder.literal("-2"))).union(// aliases lost here
    false).project(builder.fields(Lists.newArrayList(1, 0))).build();
    final String expected = "" + "LogicalProject($f1=[$1], EMPNO=[$0])\n" + "  LogicalUnion(all=[false])\n" + "    LogicalProject(EMPNO=[$0], $f1=[||($1, '-1')])\n" + "      LogicalTableScan(table=[[scott, EMP]])\n" + "    LogicalProject(EMPNO=[$0], $f1=[||($1, '-2')])\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 94 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 testRename.

@Test
public void testRename() {
    final RelBuilder builder = RelBuilder.create(config().build());
    // No rename necessary (null name is ignored)
    RelNode root = builder.scan("DEPT").rename(Arrays.asList("DEPTNO", null)).build();
    final String expected = "LogicalTableScan(table=[[scott, DEPT]])\n";
    assertThat(root, hasTree(expected));
    // No rename necessary (prefix matches)
    root = builder.scan("DEPT").rename(ImmutableList.of("DEPTNO")).build();
    assertThat(root, hasTree(expected));
    // Add project to rename fields
    root = builder.scan("DEPT").rename(Arrays.asList("NAME", null, "DEPTNO")).build();
    final String expected2 = "" + "LogicalProject(NAME=[$0], DNAME=[$1], DEPTNO=[$2])\n" + "  LogicalTableScan(table=[[scott, DEPT]])\n";
    assertThat(root, hasTree(expected2));
    // If our requested list has non-unique names, we might get the same field
    // names we started with. Don't add a useless project.
    root = builder.scan("DEPT").rename(Arrays.asList("DEPTNO", null, "DEPTNO")).build();
    final String expected3 = "" + "LogicalProject(DEPTNO=[$0], DNAME=[$1], DEPTNO0=[$2])\n" + "  LogicalTableScan(table=[[scott, DEPT]])\n";
    assertThat(root, hasTree(expected3));
    root = builder.scan("DEPT").rename(Arrays.asList("DEPTNO", null, "DEPTNO")).rename(Arrays.asList("DEPTNO", null, "DEPTNO")).build();
    // No extra Project
    assertThat(root, hasTree(expected3));
    // Name list too long
    try {
        root = builder.scan("DEPT").rename(ImmutableList.of("NAME", "DEPTNO", "Y", "Z")).build();
        fail("expected error, got " + root);
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), is("More names than fields"));
    }
}
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 95 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 testRenameValues.

@Test
public void testRenameValues() {
    final RelBuilder builder = RelBuilder.create(config().build());
    RelNode root = builder.values(new String[] { "a", "b" }, true, 1, false, -50).build();
    final String expected = "LogicalValues(tuples=[[{ true, 1 }, { false, -50 }]])\n";
    assertThat(root, hasTree(expected));
    // When you rename Values, you get a Values with a new row type, no Project
    root = builder.push(root).rename(ImmutableList.of("x", "y z")).build();
    assertThat(root, hasTree(expected));
    assertThat(root.getRowType().getFieldNames().toString(), is("[x, y z]"));
}
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