org.apache.calcite.runtime.Hook Java Examples

The following examples show how to use org.apache.calcite.runtime.Hook. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OsAdapterTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testStdin() throws SQLException {
  try (Hook.Closeable ignore = Hook.STANDARD_STREAMS.addThread(
      (Consumer<Holder<Object[]>>) o -> {
        final Object[] values = o.get();
        final InputStream in = (InputStream) values[0];
        final String s = "First line\n"
            + "Second line";
        final ByteArrayInputStream in2 =
            new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8));
        final OutputStream out = (OutputStream) values[1];
        final OutputStream err = (OutputStream) values[2];
        o.set(new Object[] {in2, out, err});
      })) {
    assertThat(foo("select count(*) as c from stdin"), is("2\n"));
  }
}
 
Example #2
Source File: InsertIntegrationTest.java    From calcite-sql-rewriter with Apache License 2.0 6 votes vote down vote up
@Test
public void testRejectsConflictingID() {
	Assume.assumeTrue("Unique ID enforcement not currently supported for TIMESTAMP", versionType != JournalVersionType.TIMESTAMP);
	try {
		CalciteAssert
				.model(TargetDatabase.makeJournalledModel(versionType))
				.query("INSERT INTO \"" + virtualSchemaName + "\".\"emps\" (\"empid\", \"deptno\", \"last_name\") VALUES (2, 1, 'Me')")
				.withHook(Hook.PROGRAM, JournalledJdbcRuleManager.program())
				.updates(0);
		Assert.fail("Expected duplicate key exception");
	} catch(RuntimeException e) {
		// Look for an error about "duplicate key" and consider it a success. For everything else, it's a fail.
		boolean foundDuplicateError = false;
		Throwable ex = e;
		while (ex != null) {
			if (ex.getMessage().contains("duplicate key")) {
				foundDuplicateError = true;
				break;
			}
			ex = ex.getCause();
		}
		if (!foundDuplicateError) {
			throw e;
		}
	}
}
 
Example #3
Source File: MaterializationTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testTableModify() {
  final String m = "select \"deptno\", \"empid\", \"name\""
      + "from \"emps\" where \"deptno\" = 10";
  final String q = "upsert into \"dependents\""
      + "select \"empid\" + 1 as x, \"name\""
      + "from \"emps\" where \"deptno\" = 10";

  final List<List<List<String>>> substitutedNames = new ArrayList<>();
  try (TryThreadLocal.Memo ignored = Prepare.THREAD_TRIM.push(true)) {
    MaterializationService.setThreadLocal();
    CalciteAssert.that()
        .withMaterializations(HR_FKUK_MODEL,
            "m0", m)
        .query(q)
        .withHook(Hook.SUB, (Consumer<RelNode>) r ->
            substitutedNames.add(new TableNameVisitor().run(r)))
        .enableMaterializations(true)
        .explainContains("hr, m0");
  } catch (Exception e) {
    // Table "dependents" not modifiable.
  }
  assertThat(substitutedNames, is(list3(new String[][][]{{{"hr", "m0"}}})));
}
 
Example #4
Source File: InsertIntegrationTest.java    From calcite-sql-rewriter with Apache License 2.0 6 votes vote down vote up
@Test
public void testInsertSelect() {
	CalciteAssert
			.model(TargetDatabase.makeJournalledModel(versionType))
			.query("INSERT INTO \"" + virtualSchemaName + "\".\"emps\" (\"empid\", \"last_name\", \"deptno\") SELECT \"deptno\" + 1000, 'added', \"deptno\" FROM \"" + virtualSchemaName + "\".\"depts\"")
			.withHook(Hook.PROGRAM, JournalledJdbcRuleManager.program())
			.explainContains("PLAN=JdbcToEnumerableConverter\n" +
					"  JdbcTableModify(table=[[" + virtualSchemaName + ", emps_journal]], operation=[INSERT], flattened=[false])\n" +
					"    JdbcProject(empid=[+($0, 1000)], deptno=[$0], last_name=['added'])\n" +
					"      JdbcFilter(condition=[AND(=($1, $3), IS NULL($2))])\n" +
					"        JdbcProject(deptno=[$0], version_number=[$2], subsequent_version_number=[$3], $f4=[MAX($2) OVER (PARTITION BY $0 ORDER BY $2 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)])\n" +
					"          JdbcTableScan(table=[[" + virtualSchemaName + ", depts_journal]])\n")
			.planUpdateHasSql("INSERT INTO \"" + actualSchemaName + "\".\"emps_journal\" (\"empid\", \"deptno\", \"last_name\")\n" +
					"(SELECT \"deptno\" + 1000 AS \"empid\", \"deptno\", 'added' AS \"last_name\"\n" +
					"FROM (SELECT \"deptno\", \"version_number\", \"subsequent_version_number\", MAX(\"version_number\") OVER (PARTITION BY \"deptno\" ORDER BY \"version_number\" ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS \"$f4\"\n" +
					"FROM \"" + actualSchemaName + "\".\"depts_journal\") AS \"t\"\n" +
					"WHERE \"version_number\" = \"$f4\" AND \"subsequent_version_number\" IS NULL)", 4);
}
 
Example #5
Source File: JournalledJdbcSchemaTest.java    From calcite-sql-rewriter with Apache License 2.0 6 votes vote down vote up
@Test
public void testFactoryWillAutomaticallyAddRules() {
	// This test changes the global state of Calcite! It shouldn't cause issues elsewhere since the rules avoid
	// changing unrecognised tables, so will not apply to their own output.
	JournalledJdbcSchema.Factory.INSTANCE.setAutomaticallyAddRules(true);
	JournalledJdbcSchema.Factory.INSTANCE.create(null, "my-parent", options);
	try {
		Program def = Mockito.mock(Program.class);
		Holder<Program> holder = Holder.of(def);
		Hook.PROGRAM.run(holder);
		Assert.assertTrue(holder.get() instanceof SequenceProgram);
		Assert.assertTrue(((SequenceProgram) holder.get()).getPrograms().get(0) instanceof ForcedRulesProgram);
	} finally {
		// ensure no gap where another hook may be added
		JournalledJdbcSchema.Factory.INSTANCE.setAutomaticallyAddRules(false);
		JournalledJdbcRuleManager.removeHook();
	}
}
 
Example #6
Source File: ScannableTableTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-3758">[CALCITE-3758]
 * FilterTableScanRule generate wrong mapping for filter condition
 * when underlying is BindableTableScan</a>. */
@Test public void testPFTableInBindableConvention() {
  final StringBuilder buf = new StringBuilder();
  final Table table = new BeatlesProjectableFilterableTable(buf, true);
  try (Hook.Closeable ignored = Hook.ENABLE_BINDABLE.addThread(Hook.propertyJ(true))) {
    final String explain = "PLAN="
        + "BindableTableScan(table=[[s, beatles]], filters=[[=($1, 'John')]], projects=[[1]])";
    CalciteAssert.that()
        .with(newSchema("s", Pair.of("beatles", table)))
        .query("select \"j\" from \"s\".\"beatles\" where \"j\" = 'John'")
        .explainContains(explain)
        .returnsUnordered("j=John");
    assertThat(buf.toString(),
        is("returnCount=1, filter=<1, John>, projects=[1]"));
  }
}
 
Example #7
Source File: UpdateTimestampIntegrationTest.java    From calcite-sql-rewriter with Apache License 2.0 6 votes vote down vote up
@Test(expected = RuntimeException.class)
public void testRewritingWithIncorrectVersionType() {
	CalciteAssert
			.model(TargetDatabase.makeJournalledModel(versionType).replace("TIMESTAMP", "BIGINT"))
			.query("UPDATE \"" + virtualSchemaName + "\".\"depts\" SET \"department_name\"='First' WHERE \"deptno\" = 3")
			.withHook(Hook.PROGRAM, JournalledJdbcRuleManager.program())
			.explainContains("PLAN=JdbcToEnumerableConverter\n" +
					"  JdbcTableModify(table=[[" + virtualSchemaName + ", depts_journal]], operation=[INSERT], flattened=[false])\n" +
					"    JdbcProject(deptno=[$0], department_name=['First'])\n" +
					"      JdbcFilter(condition=[AND(=($1, $3), IS NULL($2), =($0, 3))])\n" +
					"        JdbcProject(deptno=[$0], version_number=[$2], subsequent_version_number=[$3], $f4=[MAX($2) OVER (PARTITION BY $0 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)])\n" +
					"          JdbcTableScan(table=[[" + virtualSchemaName + ", depts_journal]])\n")
			.planUpdateHasSql("INSERT INTO \"" + actualSchemaName + "\".\"depts_journal\" (\"deptno\", \"department_name\")\n" +
					"(SELECT \"deptno\", 'First' AS \"department_name\"\n" +
					"FROM (SELECT \"deptno\", \"version_number\", \"subsequent_version_number\", MAX(\"version_number\") OVER (PARTITION BY \"deptno\" ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS \"$f4\"\n" +
					"FROM \"" + actualSchemaName + "\".\"depts_journal\") AS \"t\"\n" +
					"WHERE \"version_number\" = \"$f4\" AND \"subsequent_version_number\" IS NULL AND \"deptno\" = 3)", 1);
}
 
Example #8
Source File: UpdateTimestampIntegrationTest.java    From calcite-sql-rewriter with Apache License 2.0 6 votes vote down vote up
@Test
public void testRewriting() {
	CalciteAssert
			.model(TargetDatabase.makeJournalledModel(versionType))
			.query("UPDATE \"" + virtualSchemaName + "\".\"depts\" SET \"department_name\"='First' WHERE \"deptno\" = 3")
			.withHook(Hook.PROGRAM, JournalledJdbcRuleManager.program())
			.explainContains("PLAN=JdbcToEnumerableConverter\n" +
					"  JdbcTableModify(table=[[" + virtualSchemaName + ", depts_journal]], operation=[INSERT], flattened=[false])\n" +
					"    JdbcProject(deptno=[$0], department_name=['First'])\n" +
					"      JdbcFilter(condition=[AND(=($1, $3), IS NULL($2), =($0, 3))])\n" +
					"        JdbcProject(deptno=[$0], version_number=[$2], subsequent_version_number=[$3], $f4=[MAX($2) OVER (PARTITION BY $0 ORDER BY $2 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)])\n" +
					"          JdbcTableScan(table=[[" + virtualSchemaName + ", depts_journal]])\n")
			.planUpdateHasSql("INSERT INTO \"" + actualSchemaName + "\".\"depts_journal\" (\"deptno\", \"department_name\")\n" +
					"(SELECT \"deptno\", 'First' AS \"department_name\"\n" +
					"FROM (SELECT \"deptno\", \"version_number\", \"subsequent_version_number\", MAX(\"version_number\") OVER (PARTITION BY \"deptno\" ORDER BY \"version_number\" ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS \"$f4\"\n" +
					"FROM \"" + actualSchemaName + "\".\"depts_journal\") AS \"t\"\n" +
					"WHERE \"version_number\" = \"$f4\" AND \"subsequent_version_number\" IS NULL AND \"deptno\" = 3)", 1);
}
 
Example #9
Source File: StreamlineDataContext.java    From streamline with Apache License 2.0 6 votes vote down vote up
public StreamlineDataContext() {
    // Store the time at which the query started executing. The SQL
    // standard says that functions such as CURRENT_TIMESTAMP return the
    // same value throughout the query.

    final Holder<Long> timeHolder = Holder.of(System.currentTimeMillis());

    // Give a hook chance to alter the clock.
    Hook.CURRENT_TIME.run(timeHolder);
    final long time = timeHolder.get();
    final TimeZone timeZone = Calendar.getInstance().getTimeZone();
    final long localOffset = timeZone.getOffset(time);
    final long currentOffset = localOffset;

    ImmutableMap.Builder<Object, Object> builder = ImmutableMap.builder();
    builder.put(Variable.UTC_TIMESTAMP.camelName, time)
            .put(Variable.CURRENT_TIMESTAMP.camelName, time + currentOffset)
            .put(Variable.LOCAL_TIMESTAMP.camelName, time + localOffset)
            .put(Variable.TIME_ZONE.camelName, timeZone);
    map = builder.build();
}
 
Example #10
Source File: SelectIntegrationTest.java    From calcite-sql-rewriter with Apache License 2.0 6 votes vote down vote up
@Test
public void testRewriting() {
	CalciteAssert
			.model(TargetDatabase.makeJournalledModel(versionType))
			.query("SELECT \"empid\" FROM \"" + virtualSchemaName + "\".\"emps\"")
			.withHook(Hook.PROGRAM, JournalledJdbcRuleManager.program())
			.explainContains("PLAN=JdbcToEnumerableConverter\n" +
					"  JdbcProject(empid=[$0])\n" +
					"    JdbcFilter(condition=[AND(=($1, $3), IS NULL($2))])\n" +
					"      JdbcProject(empid=[$0], version_number=[$2], subsequent_version_number=[$3], $f6=[MAX($2) OVER (PARTITION BY $0 ORDER BY $2 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)])\n" +
					"        JdbcTableScan(table=[[" + virtualSchemaName + ", emps_journal]])\n")
			.planHasSql("SELECT \"empid\"\n" +
					"FROM (SELECT \"empid\", \"version_number\", \"subsequent_version_number\", MAX(\"version_number\") OVER (PARTITION BY \"empid\" ORDER BY \"version_number\" ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS \"$f6\"\n" +
					"FROM \"" + actualSchemaName + "\".\"emps_journal\") AS \"t\"\n" +
					"WHERE \"version_number\" = \"$f6\" AND \"subsequent_version_number\" IS NULL")
			.returns("empid=1\n" +
					"empid=2\n" +
					"empid=3\n" +
					"empid=4\n" +
					"empid=6\n");
}
 
Example #11
Source File: EnumerableCorrelateTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-2605">[CALCITE-2605]
 * NullPointerException when left outer join implemented with EnumerableCorrelate</a> */
@Test void leftOuterJoinCorrelate() {
  tester(false, new JdbcTest.HrSchema())
      .query(
          "select e.empid, e.name, d.name as dept from emps e left outer join depts d on e.deptno=d.deptno")
      .withHook(Hook.PLANNER, (Consumer<RelOptPlanner>) planner -> {
        // force the left outer join to run via EnumerableCorrelate
        // instead of EnumerableHashJoin
        planner.addRule(JoinToCorrelateRule.INSTANCE);
        planner.removeRule(EnumerableRules.ENUMERABLE_JOIN_RULE);
      })
      .explainContains(""
          + "EnumerableCalc(expr#0..4=[{inputs}], empid=[$t0], name=[$t2], dept=[$t4])\n"
          + "  EnumerableCorrelate(correlation=[$cor0], joinType=[left], requiredColumns=[{1}])\n"
          + "    EnumerableCalc(expr#0..4=[{inputs}], proj#0..2=[{exprs}])\n"
          + "      EnumerableTableScan(table=[[s, emps]])\n"
          + "    EnumerableCalc(expr#0..3=[{inputs}], expr#4=[$cor0], expr#5=[$t4.deptno], expr#6=[=($t5, $t0)], proj#0..1=[{exprs}], $condition=[$t6])\n"
          + "      EnumerableTableScan(table=[[s, depts]])")
      .returnsUnordered(
          "empid=100; name=Bill; dept=Sales",
          "empid=110; name=Theodore; dept=Sales",
          "empid=150; name=Sebastian; dept=Sales",
          "empid=200; name=Eric; dept=null");
}
 
Example #12
Source File: DeleteTimestampIntegrationTest.java    From calcite-sql-rewriter with Apache License 2.0 6 votes vote down vote up
@Test(expected = RuntimeException.class)
public void testRewritingWrongVersionType() {
	CalciteAssert
			.model(TargetDatabase.makeJournalledModel(versionType).replace("TIMESTAMP", "BIGINT"))
			.query("DELETE FROM \"" + virtualSchemaName + "\".\"depts\" WHERE \"deptno\"=3")
			.withHook(Hook.PROGRAM, JournalledJdbcRuleManager.program())
			.explainContains("PLAN=JdbcToEnumerableConverter\n" +
					"  JdbcTableModify(table=[[" + virtualSchemaName + ", depts_journal]], operation=[INSERT], flattened=[false])\n" +
					"    JdbcProject(deptno=[$0], department_name=[$1], version_number=[CURRENT_TIMESTAMP], subsequent_version_number=[CURRENT_TIMESTAMP])\n" +
					"      JdbcFilter(condition=[AND(=($2, $4), IS NULL($3), =($0, 3))])\n" +
					"        JdbcProject(deptno=[$0], department_name=[$1], version_number=[$2], subsequent_version_number=[$3], $f4=[MAX($2) OVER (PARTITION BY $0 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)])\n" +
					"          JdbcTableScan(table=[[" + virtualSchemaName + ", depts_journal]])\n")
			.planUpdateHasSql("INSERT INTO \"" + actualSchemaName + "\".\"depts_journal\" (\"deptno\", \"department_name\", \"version_number\", \"subsequent_version_number\")\n" +
					"(SELECT \"deptno\", \"department_name\", CURRENT_TIMESTAMP AS \"version_number\", CURRENT_TIMESTAMP AS \"subsequent_version_number\"\n" +
					"FROM (SELECT \"deptno\", \"department_name\", \"version_number\", \"subsequent_version_number\", MAX(\"version_number\") OVER (PARTITION BY \"deptno\" ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS \"$f4\"\n" +
					"FROM \"" + actualSchemaName + "\".\"depts_journal\") AS \"t\"\n" +
					"WHERE \"version_number\" = \"$f4\" AND \"subsequent_version_number\" IS NULL AND \"deptno\" = 3)", 1);
}
 
Example #13
Source File: EnumerableBatchNestedLoopJoinTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void simpleInnerBatchJoinTestBuilder() {
  tester(false, new JdbcTest.HrSchema())
      .query("?")
      .withHook(Hook.PLANNER, (Consumer<RelOptPlanner>) planner -> {
        planner.removeRule(EnumerableRules.ENUMERABLE_CORRELATE_RULE);
        planner.addRule(EnumerableRules.ENUMERABLE_BATCH_NESTED_LOOP_JOIN_RULE);
      })
      .withRel(
          builder -> builder
              .scan("s", "depts").as("d")
              .scan("s", "emps").as("e")
              .join(JoinRelType.INNER,
                  builder.equals(
                      builder.field(2, "d", "deptno"),
                      builder.field(2, "e", "deptno")))
              .project(
                  builder.field("deptno"))
              .build())
      .returnsUnordered(
          "deptno=10",
          "deptno=10",
          "deptno=10");
}
 
Example #14
Source File: EnumerableBatchNestedLoopJoinTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testSemiJoin() {
  tester(false, new JdbcTest.HrSchemaBig())
      .query("?")
      .withHook(Hook.PLANNER, (Consumer<RelOptPlanner>) planner -> {
        planner.removeRule(EnumerableRules.ENUMERABLE_CORRELATE_RULE);
        planner.removeRule(EnumerableRules.ENUMERABLE_MERGE_JOIN_RULE);
        planner.removeRule(EnumerableRules.ENUMERABLE_JOIN_RULE);
        planner.addRule(EnumerableRules.ENUMERABLE_BATCH_NESTED_LOOP_JOIN_RULE);
      })
      .withRel(
          builder -> builder
              .scan("s", "emps").as("e")
              .scan("s", "depts").as("d")
              .semiJoin(
                  builder.equals(
                      builder.field(2, "e", "empid"),
                      builder.field(2, "d", "deptno")))
              .project(
                  builder.field("name"))
              .build())
      .returnsUnordered(
          "name=Emmanuel",
          "name=Gabriel",
          "name=Michelle",
          "name=Ursula");
}
 
Example #15
Source File: EnumerableSortedAggregateTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void sortedAgg() {
  tester(false, new JdbcTest.HrSchema())
      .query(
          "select deptno, "
          + "max(salary) as max_salary, count(name) as num_employee "
          + "from emps group by deptno")
      .withHook(Hook.PLANNER, (Consumer<RelOptPlanner>) planner -> {
        planner.removeRule(EnumerableRules.ENUMERABLE_AGGREGATE_RULE);
        planner.addRule(EnumerableRules.ENUMERABLE_SORTED_AGGREGATE_RULE);
      })
      .explainContains(
          "EnumerableSortedAggregate(group=[{1}], max_salary=[MAX($3)], num_employee=[COUNT($2)])\n"
          + "  EnumerableSort(sort0=[$1], dir0=[ASC])\n"
          + "    EnumerableTableScan(table=[[s, emps]])")
      .returnsOrdered(
          "deptno=10; max_salary=11500.0; num_employee=3",
          "deptno=20; max_salary=8000.0; num_employee=1");
}
 
Example #16
Source File: EnumerableSortedAggregateTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void sortedAggTwoGroupKeys() {
  tester(false, new JdbcTest.HrSchema())
      .query(
          "select deptno, commission, "
              + "max(salary) as max_salary, count(name) as num_employee "
              + "from emps group by deptno, commission")
      .withHook(Hook.PLANNER, (Consumer<RelOptPlanner>) planner -> {
        planner.removeRule(EnumerableRules.ENUMERABLE_AGGREGATE_RULE);
        planner.addRule(EnumerableRules.ENUMERABLE_SORTED_AGGREGATE_RULE);
      })
      .explainContains(
          "EnumerableSortedAggregate(group=[{1, 4}], max_salary=[MAX($3)], num_employee=[COUNT($2)])\n"
          + "  EnumerableSort(sort0=[$1], sort1=[$4], dir0=[ASC], dir1=[ASC])\n"
          + "    EnumerableTableScan(table=[[s, emps]])")
      .returnsOrdered(
          "deptno=10; commission=250; max_salary=11500.0; num_employee=1",
          "deptno=10; commission=1000; max_salary=10000.0; num_employee=1",
          "deptno=10; commission=null; max_salary=7000.0; num_employee=1",
          "deptno=20; commission=500; max_salary=8000.0; num_employee=1");
}
 
Example #17
Source File: EnumerableCorrelateTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-2930">[CALCITE-2930]
 * FilterCorrelateRule on a Correlate with SemiJoinType SEMI (or ANTI)
 * throws IllegalStateException</a> */
@Test void semiJoinCorrelateWithFilterCorrelateRule() {
  tester(false, new JdbcTest.HrSchema())
      .query(
          "select empid, name from emps e where e.deptno in (select d.deptno from depts d) and e.empid > 100")
      .withHook(Hook.PLANNER, (Consumer<RelOptPlanner>) planner -> {
        // force the semijoin to run via EnumerableCorrelate
        // instead of EnumerableHashJoin(SEMI),
        // and push the 'empid > 100' filter into the Correlate
        planner.addRule(JoinToCorrelateRule.INSTANCE);
        planner.addRule(FilterCorrelateRule.INSTANCE);
        planner.removeRule(EnumerableRules.ENUMERABLE_JOIN_RULE);
        planner.removeRule(EnumerableRules.ENUMERABLE_MERGE_JOIN_RULE);
      })
      .explainContains(""
          + "EnumerableCalc(expr#0..3=[{inputs}], empid=[$t1], name=[$t3])\n"
          + "  EnumerableCorrelate(correlation=[$cor1], joinType=[inner], requiredColumns=[{0}])\n"
          + "    EnumerableAggregate(group=[{0}])\n"
          + "      EnumerableTableScan(table=[[s, depts]])\n"
          + "    EnumerableCalc(expr#0..4=[{inputs}], expr#5=[$cor1], expr#6=[$t5.deptno], expr#7=[=($t6, $t1)], expr#8=[100], expr#9=[>($t0, $t8)], expr#10=[AND($t7, $t9)], proj#0..2=[{exprs}], $condition=[$t10])\n"
          + "      EnumerableTableScan(table=[[s, emps]])")
      .returnsUnordered(
          "empid=110; name=Theodore",
          "empid=150; name=Sebastian");
}
 
Example #18
Source File: EnumerableBatchNestedLoopJoinTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void simpleInnerBatchJoinTestSQL() {
  tester(false, new JdbcTest.HrSchema())
      .query(
          "select e.name from emps e join depts d on d.deptno = e.deptno")
      .withHook(Hook.PLANNER, (Consumer<RelOptPlanner>) planner -> {
        planner.removeRule(EnumerableRules.ENUMERABLE_CORRELATE_RULE);
        planner.addRule(EnumerableRules.ENUMERABLE_BATCH_NESTED_LOOP_JOIN_RULE);
      })
      .returnsUnordered("name=Bill",
          "name=Sebastian",
          "name=Theodore");
}
 
Example #19
Source File: RelOptTestBase.java    From calcite with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void check(boolean unchanged) {
  try (Closer closer = new Closer()) {
    for (Map.Entry<Hook, Consumer> entry : hooks.entrySet()) {
      closer.add(entry.getKey().addThread(entry.getValue()));
    }
    Tester t = tester;
    for (Function<Tester, Tester> transform : transforms) {
      t = transform.apply(t);
    }
    checkPlanning(t, preProgram, planner, sql, unchanged);
  }
}
 
Example #20
Source File: CalciteAssert.java    From calcite with Apache License 2.0 5 votes vote down vote up
private void ensurePlan(Consumer<Integer> checkUpdate) {
  if (plan != null) {
    return;
  }
  addHook(Hook.JAVA_PLAN, this::setPlan);
  withConnection(connection -> {
    assertQuery(connection, sql, limit, materializationsEnabled,
        hooks, null, checkUpdate, null);
    assertNotNull(plan);
  });
}
 
Example #21
Source File: RelOptTestBase.java    From calcite with Apache License 2.0 5 votes vote down vote up
Sql(Tester tester, String sql, HepProgram preProgram, RelOptPlanner planner,
    ImmutableMap<Hook, Consumer> hooks,
    ImmutableList<Function<Tester, Tester>> transforms) {
  this.tester = tester;
  this.sql = sql;
  this.preProgram = preProgram;
  this.planner = planner;
  this.hooks = hooks;
  this.transforms = transforms;
}
 
Example #22
Source File: RelOptTestBase.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** @deprecated Use {@link #withHook(Hook, Consumer)}. */
@SuppressWarnings("Guava")
@Deprecated // to be removed before 2.0
public <T> Sql withHook(Hook hook,
    com.google.common.base.Function<T, Void> handler) {
  return withHook(hook, (Consumer<T>) handler::apply);
}
 
Example #23
Source File: EnumerableBatchNestedLoopJoinTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testAntiJoin() {
  tester(false, new JdbcTest.HrSchema())
      .query("?")
      .withHook(Hook.PLANNER, (Consumer<RelOptPlanner>) planner -> {
        planner.removeRule(EnumerableRules.ENUMERABLE_CORRELATE_RULE);
        planner.removeRule(EnumerableRules.ENUMERABLE_MERGE_JOIN_RULE);
        planner.removeRule(EnumerableRules.ENUMERABLE_JOIN_RULE);
        planner.addRule(EnumerableRules.ENUMERABLE_BATCH_NESTED_LOOP_JOIN_RULE);
      })
      .withRel(
          builder -> builder
              .scan("s", "emps").as("e")
              .scan("s", "emps").as("e2")
              .antiJoin(
                  builder.and(
                      builder.equals(
                          builder.field(2, "e", "deptno"),
                          builder.field(2, "e2", "deptno")),
                      builder.call(
                          SqlStdOperatorTable.GREATER_THAN,
                          builder.field(2, "e2", "salary"),
                          builder.field(2, "e", "salary"))))
              .project(
                  builder.field("name"),
                  builder.field("salary"))
              .build())
      .returnsUnordered(
          "name=Theodore; salary=11500.0",
          "name=Eric; salary=8000.0");
}
 
Example #24
Source File: Prepare.java    From calcite with Apache License 2.0 5 votes vote down vote up
protected Program getProgram() {
  // Allow a test to override the default program.
  final Holder<Program> holder = Holder.of(null);
  Hook.PROGRAM.run(holder);
  if (holder.get() != null) {
    return holder.get();
  }

  return Programs.standard();
}
 
Example #25
Source File: PlannerImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
private void ready() {
  switch (state) {
  case STATE_0_CLOSED:
    reset();
  }
  ensure(State.STATE_1_RESET);

  RelDataTypeSystem typeSystem =
      connectionConfig.typeSystem(RelDataTypeSystem.class,
          RelDataTypeSystem.DEFAULT);
  typeFactory = new JavaTypeFactoryImpl(typeSystem);
  planner = new VolcanoPlanner(costFactory, context);
  RelOptUtil.registerDefaultRules(planner,
      connectionConfig.materializationsEnabled(),
      Hook.ENABLE_BINDABLE.get(false));
  planner.setExecutor(executor);

  state = State.STATE_2_READY;

  // If user specify own traitDef, instead of default default trait,
  // register the trait def specified in traitDefs.
  if (this.traitDefs == null) {
    planner.addRelTraitDef(ConventionTraitDef.INSTANCE);
    if (CalciteSystemProperty.ENABLE_COLLATION_TRAIT.value()) {
      planner.addRelTraitDef(RelCollationTraitDef.INSTANCE);
    }
  } else {
    for (RelTraitDef def : this.traitDefs) {
      planner.addRelTraitDef(def);
    }
  }
}
 
Example #26
Source File: EnumerableBatchNestedLoopJoinTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testJoinSubQuery() {
  String sql = "SELECT count(name) FROM emps e WHERE e.deptno NOT IN "
      + "(SELECT d.deptno FROM depts d WHERE d.name = 'Sales')";
  tester(false, new JdbcTest.HrSchemaBig())
      .query(sql)
      .withHook(Hook.PLANNER, (Consumer<RelOptPlanner>) planner -> {
        planner.removeRule(EnumerableRules.ENUMERABLE_CORRELATE_RULE);
        planner.removeRule(EnumerableRules.ENUMERABLE_MERGE_JOIN_RULE);
        planner.removeRule(EnumerableRules.ENUMERABLE_JOIN_RULE);
        planner.addRule(EnumerableRules.ENUMERABLE_BATCH_NESTED_LOOP_JOIN_RULE);
      })
      .returnsUnordered("EXPR$0=23");
}
 
Example #27
Source File: CalciteMetaImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Wraps the SQL string in a
 * {@link org.apache.calcite.jdbc.CalcitePrepare.Query} object, giving the
 * {@link Hook#STRING_TO_QUERY} hook chance to override. */
private CalcitePrepare.Query<Object> toQuery(
    Context context, String sql) {
  final Holder<CalcitePrepare.Query<Object>> queryHolder =
      Holder.of(CalcitePrepare.Query.of(sql));
  final FrameworkConfig config = Frameworks.newConfigBuilder()
      .parserConfig(SqlParser.Config.DEFAULT)
      .defaultSchema(context.getRootSchema().plus())
      .build();
  Hook.STRING_TO_QUERY.run(Pair.of(config, queryHolder));
  return queryHolder.get();
}
 
Example #28
Source File: EnumerableBatchNestedLoopJoinTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testInnerJoinOnString() {
  String sql = "SELECT d.name, e.salary FROM depts d join emps e on d.name = e.name";
  tester(false, new JdbcTest.HrSchemaBig())
      .query(sql)
      .withHook(Hook.PLANNER, (Consumer<RelOptPlanner>) planner -> {
        planner.removeRule(EnumerableRules.ENUMERABLE_CORRELATE_RULE);
        planner.removeRule(EnumerableRules.ENUMERABLE_MERGE_JOIN_RULE);
        planner.removeRule(EnumerableRules.ENUMERABLE_JOIN_RULE);
        planner.addRule(EnumerableRules.ENUMERABLE_BATCH_NESTED_LOOP_JOIN_RULE);
      })
      .returnsUnordered("");
}
 
Example #29
Source File: HiveEnumerableInterpretable.java    From marble with Apache License 2.0 5 votes vote down vote up
public static Bindable toBindable(Map<String, Object> parameters,
    CalcitePrepare.SparkHandler spark, EnumerableRel rel,
    EnumerableRel.Prefer prefer) {
  HiveEnumerableRelImplementor relImplementor =
      new HiveEnumerableRelImplementor(rel.getCluster().getRexBuilder(),
          parameters);

  final ClassDeclaration expr = relImplementor.implementRoot(rel, prefer);
  String s = Expressions.toString(expr.memberDeclarations, "\n", false);

  if (CalcitePrepareImpl.DEBUG) {
    Util.debugCode(System.out, s);
  }

  Hook.JAVA_PLAN.run(s);

  try {
    if (spark != null && spark.enabled()) {
      return spark.compile(expr, s);
    } else {
      return getBindable(expr, s,
          rel.getRowType().getFieldCount());
    }
  } catch (Exception e) {
    throw Helper.INSTANCE.wrap("Error while compiling generated Java code:\n"
        + s, e);
  }
}
 
Example #30
Source File: EnumerableJoinTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-3846">[CALCITE-3846]
 * EnumerableMergeJoin: wrong comparison of composite key with null values</a>. */
@Test void testMergeJoinWithCompositeKeyAndNullValues() {
  tester(false, new JdbcTest.HrSchema())
      .query("?")
      .withHook(Hook.PLANNER, (Consumer<RelOptPlanner>) planner -> {
        planner.addRule(EnumerableRules.ENUMERABLE_MERGE_JOIN_RULE);
        planner.removeRule(EnumerableRules.ENUMERABLE_JOIN_RULE);
      })
      .withRel(builder -> builder
          .scan("s", "emps")
          .sort(builder.field("deptno"), builder.field("commission"))
          .scan("s", "emps")
          .sort(builder.field("deptno"), builder.field("commission"))
          .join(JoinRelType.INNER,
              builder.and(
                  builder.equals(
                      builder.field(2, 0, "deptno"),
                      builder.field(2, 1, "deptno")),
                  builder.equals(
                      builder.field(2, 0, "commission"),
                      builder.field(2, 1, "commission"))))
          .project(
              builder.field("empid"))
          .build())
      .explainHookMatches("" // It is important that we have MergeJoin in the plan
          + "EnumerableCalc(expr#0..4=[{inputs}], empid=[$t0])\n"
          + "  EnumerableMergeJoin(condition=[AND(=($1, $3), =($2, $4))], joinType=[inner])\n"
          + "    EnumerableSort(sort0=[$1], sort1=[$2], dir0=[ASC], dir1=[ASC])\n"
          + "      EnumerableCalc(expr#0..4=[{inputs}], proj#0..1=[{exprs}], commission=[$t4])\n"
          + "        EnumerableTableScan(table=[[s, emps]])\n"
          + "    EnumerableSort(sort0=[$0], sort1=[$1], dir0=[ASC], dir1=[ASC])\n"
          + "      EnumerableCalc(expr#0..4=[{inputs}], deptno=[$t1], commission=[$t4])\n"
          + "        EnumerableTableScan(table=[[s, emps]])\n")
      .returnsUnordered("empid=100\nempid=110\nempid=150\nempid=200");
}