Java Code Examples for org.apache.calcite.materialize.MaterializationService#setThreadLocal()

The following examples show how to use org.apache.calcite.materialize.MaterializationService#setThreadLocal() . 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: MaterializationTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testViewMaterialization() {
  try (TryThreadLocal.Memo ignored = Prepare.THREAD_TRIM.push(true)) {
    MaterializationService.setThreadLocal();
    String materialize = "select \"depts\".\"name\"\n"
        + "from \"depts\"\n"
        + "join \"emps\" on (\"emps\".\"deptno\" = \"depts\".\"deptno\")";
    String query = "select \"depts\".\"name\"\n"
        + "from \"depts\"\n"
        + "join \"emps\" on (\"emps\".\"deptno\" = \"depts\".\"deptno\")";

    CalciteAssert.that()
        .withMaterializations(HR_FKUK_MODEL, true, "matview", materialize)
        .query(query)
        .enableMaterializations(true)
        .explainMatches(
            "", CalciteAssert.checkResultContains(
            "EnumerableValues(tuples=[[{ 'noname' }]])")).returnsValue("noname");
  }
}
 
Example 2
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 3
Source File: MaterializationTest.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-761">[CALCITE-761]
 * Pre-populated materializations</a>. */
@Test void testPrePopulated() {
  String q = "select distinct \"deptno\" from \"emps\"";
  try (TryThreadLocal.Memo ignored = Prepare.THREAD_TRIM.push(true)) {
    MaterializationService.setThreadLocal();
    CalciteAssert.that()
        .withMaterializations(
            HR_FKUK_MODEL, builder -> {
              final Map<String, Object> map = builder.map();
              map.put("table", "locations");
              String sql = "select distinct `deptno` as `empid`, '' as `name`\n"
                  + "from `emps`";
              final String sql2 = sql.replace("`", "\"");
              map.put("sql", sql2);
              return ImmutableList.of(map);
            })
        .query(q)
        .enableMaterializations(true)
        .sameResultWithMaterializationsDisabled();
  }
}
 
Example 4
Source File: MaterializationTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testMultiMaterializationMultiUsage() {
  String q = "select *\n"
      + "from (select * from \"emps\" where \"empid\" < 300)\n"
      + "join (select \"deptno\", count(*) as c from \"emps\" group by \"deptno\") using (\"deptno\")";
  try (TryThreadLocal.Memo ignored = Prepare.THREAD_TRIM.push(true)) {
    MaterializationService.setThreadLocal();
    CalciteAssert.that()
        .withMaterializations(HR_FKUK_MODEL,
            "m0", "select \"deptno\", count(*) as c, sum(\"empid\") as s from \"emps\" group by \"deptno\"",
            "m1", "select * from \"emps\" where \"empid\" < 500")
        .query(q)
        .enableMaterializations(true)
        .explainContains("EnumerableTableScan(table=[[hr, m0]])")
        .explainContains("EnumerableTableScan(table=[[hr, m1]])")
        .sameResultWithMaterializationsDisabled();
  }
}
 
Example 5
Source File: MaterializationTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Disabled("Creating mv for depts considering all its column throws exception")
@Test void testMultiMaterializationOnJoinQuery() {
  final String q = "select *\n"
      + "from \"emps\"\n"
      + "join \"depts\" using (\"deptno\") where \"empid\" < 300 "
      + "and \"depts\".\"deptno\" > 200";
  try (TryThreadLocal.Memo ignored = Prepare.THREAD_TRIM.push(true)) {
    MaterializationService.setThreadLocal();
    CalciteAssert.that()
        .withMaterializations(HR_FKUK_MODEL,
            "m0", "select * from \"emps\" where \"empid\" < 500",
            "m1", "select * from \"depts\" where \"deptno\" > 100")
        .query(q)
        .enableMaterializations(true)
        .explainContains("EnumerableTableScan(table=[[hr, m0]])")
        .explainContains("EnumerableTableScan(table=[[hr, m1]])")
        .sameResultWithMaterializationsDisabled();
  }
}
 
Example 6
Source File: SqlWorker.java    From quark with Apache License 2.0 5 votes vote down vote up
public RelNode run(RelOptPlanner planner, RelNode rel,
                   RelTraitSet requiredOutputTraits,
                   List<RelOptMaterialization> materializations,
                   List<RelOptLattice> lattices) {
  planner.clear();

  planner.addRelTraitDef(ConventionTraitDef.INSTANCE);

  planner.addRelTraitDef(RelCollationTraitDef.INSTANCE);
  //((VolcanoPlanner) planner).registerAbstractRelationalRules();

  RelOptUtil.registerAbstractRels(planner);
  for (RelOptRule rule : ruleSet) {
    planner.addRule(rule);
  }

  planner.addRule(Bindables.BINDABLE_TABLE_SCAN_RULE);
  planner.addRule(ProjectTableScanRule.INSTANCE);
  planner.addRule(ProjectTableScanRule.INTERPRETER);
  planner.addRule(EnumerableInterpreterRule.INSTANCE);

  final CalciteSchema rootSchema = CalciteSchema.from(context.getRootSchema());
  planner.setExecutor(new RexExecutorImpl(null));
  planner.setRoot(rel);

  MaterializationService.setThreadLocal(materializationService);
  plannerHolder.setPlanner(planner);
  populateMaterializationsAndLattice(plannerHolder, rootSchema);
  if (!rel.getTraitSet().equals(requiredOutputTraits)) {
    rel = planner.changeTraits(rel, requiredOutputTraits);
    planner.setRoot(rel);
  }

  RelOptPlanner planner2 = planner.chooseDelegate();
  return planner2.findBestExp();
}
 
Example 7
Source File: MaterializationTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testViewSchemaPath() {
  try (TryThreadLocal.Memo ignored = Prepare.THREAD_TRIM.push(true)) {
    MaterializationService.setThreadLocal();
    final String m = "select empno, deptno from emp";
    final String q = "select deptno from scott.emp";
    final List<String> path = ImmutableList.of("SCOTT");
    final JsonBuilder builder = new JsonBuilder();
    final String model = "{\n"
        + "  version: '1.0',\n"
        + "  defaultSchema: 'hr',\n"
        + "  schemas: [\n"
        + JdbcTest.SCOTT_SCHEMA
        + "  ,\n"
        + "    {\n"
        + "      materializations: [\n"
        + "        {\n"
        + "          table: 'm0',\n"
        + "          view: 'm0v',\n"
        + "          sql: " + builder.toJsonString(m) + ",\n"
        + "          viewSchemaPath: " + builder.toJsonString(path)
        + "        }\n"
        + "      ],\n"
        + "      type: 'custom',\n"
        + "      name: 'hr',\n"
        + "      factory: 'org.apache.calcite.adapter.java.ReflectiveSchema$Factory',\n"
        + "      operand: {\n"
        + "        class: 'org.apache.calcite.test.JdbcTest$HrSchema'\n"
        + "      }\n"
        + "    }\n"
        + "  ]\n"
        + "}\n";
    CalciteAssert.that()
        .withModel(model)
        .query(q)
        .enableMaterializations(true)
        .explainMatches("", CONTAINS_M0)
        .sameResultWithMaterializationsDisabled();
  }
}
 
Example 8
Source File: MaterializationTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testMaterializationSubstitution() {
  String q = "select *\n"
      + "from (select * from \"emps\" where \"empid\" < 300)\n"
      + "join (select * from \"emps\" where \"empid\" < 200) using (\"empid\")";

  final String[][][] expectedNames = {
      {{"hr", "emps"}, {"hr", "m0"}},
      {{"hr", "emps"}, {"hr", "m1"}},
      {{"hr", "m0"}, {"hr", "emps"}},
      {{"hr", "m0"}, {"hr", "m0"}},
      {{"hr", "m0"}, {"hr", "m1"}},
      {{"hr", "m1"}, {"hr", "emps"}},
      {{"hr", "m1"}, {"hr", "m0"}},
      {{"hr", "m1"}, {"hr", "m1"}}};

  try (TryThreadLocal.Memo ignored = Prepare.THREAD_TRIM.push(true)) {
    MaterializationService.setThreadLocal();
    final List<List<List<String>>> substitutedNames = new ArrayList<>();
    CalciteAssert.that()
        .withMaterializations(HR_FKUK_MODEL,
            "m0", "select * from \"emps\" where \"empid\" < 300",
            "m1", "select * from \"emps\" where \"empid\" < 600")
        .query(q)
        .withHook(Hook.SUB, (Consumer<RelNode>) r ->
            substitutedNames.add(new TableNameVisitor().run(r)))
        .enableMaterializations(true)
        .sameResultWithMaterializationsDisabled();
    substitutedNames.sort(CASE_INSENSITIVE_LIST_LIST_COMPARATOR);
    assertThat(substitutedNames, is(list3(expectedNames)));
  }
}
 
Example 9
Source File: MaterializationTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testMaterializationSubstitution2() {
  String q = "select *\n"
      + "from (select * from \"emps\" where \"empid\" < 300)\n"
      + "join (select * from \"emps\" where \"empid\" < 200) using (\"empid\")";

  final String[][][] expectedNames = {
      {{"hr", "emps"}, {"hr", "m0"}},
      {{"hr", "emps"}, {"hr", "m1"}},
      {{"hr", "emps"}, {"hr", "m2"}},
      {{"hr", "m0"}, {"hr", "emps"}},
      {{"hr", "m0"}, {"hr", "m0"}},
      {{"hr", "m0"}, {"hr", "m1"}},
      {{"hr", "m0"}, {"hr", "m2"}},
      {{"hr", "m1"}, {"hr", "emps"}},
      {{"hr", "m1"}, {"hr", "m0"}},
      {{"hr", "m1"}, {"hr", "m1"}},
      {{"hr", "m1"}, {"hr", "m2"}},
      {{"hr", "m2"}, {"hr", "emps"}},
      {{"hr", "m2"}, {"hr", "m0"}},
      {{"hr", "m2"}, {"hr", "m1"}},
      {{"hr", "m2"}, {"hr", "m2"}}};

  try (TryThreadLocal.Memo ignored = Prepare.THREAD_TRIM.push(true)) {
    MaterializationService.setThreadLocal();
    final List<List<List<String>>> substitutedNames = new ArrayList<>();
    CalciteAssert.that()
        .withMaterializations(HR_FKUK_MODEL,
            "m0", "select * from \"emps\" where \"empid\" < 300",
            "m1", "select * from \"emps\" where \"empid\" < 600",
            "m2", "select * from \"m1\"")
        .query(q)
        .withHook(Hook.SUB, (Consumer<RelNode>) r ->
            substitutedNames.add(new TableNameVisitor().run(r)))
        .enableMaterializations(true)
        .sameResultWithMaterializationsDisabled();
    substitutedNames.sort(CASE_INSENSITIVE_LIST_LIST_COMPARATOR);
    assertThat(substitutedNames, is(list3(expectedNames)));
  }
}
 
Example 10
Source File: LatticeTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
private void checkTileAlgorithm(String statisticProvider,
    String expectedExplain) {
  final RelOptRule[] rules = {
      MaterializedViewProjectFilterRule.INSTANCE,
      MaterializedViewOnlyFilterRule.INSTANCE,
      MaterializedViewProjectJoinRule.INSTANCE,
      MaterializedViewOnlyJoinRule.INSTANCE,
      MaterializedViewProjectAggregateRule.INSTANCE,
      MaterializedViewOnlyAggregateRule.INSTANCE
  };
  MaterializationService.setThreadLocal();
  MaterializationService.instance().clear();
  foodmartLatticeModel(statisticProvider)
      .query("select distinct t.\"the_year\", t.\"quarter\"\n"
          + "from \"foodmart\".\"sales_fact_1997\" as s\n"
          + "join \"foodmart\".\"time_by_day\" as t using (\"time_id\")\n")
      .enableMaterializations(true)

  // Disable materialization rules from this test. For some reason, there is
  // a weird interaction between these rules and the lattice rewriting that
  // produces non-deterministic rewriting (even when only lattices are present).
  // For more context, see
  // <a href="https://issues.apache.org/jira/browse/CALCITE-2953">[CALCITE-2953]</a>.
      .withHook(Hook.PLANNER, (Consumer<RelOptPlanner>) planner ->
          Arrays.asList(rules).forEach(planner::removeRule))

  // disable for MySQL; times out running star-join query
  // disable for H2; it thinks our generated SQL has invalid syntax
      .enable(CalciteAssert.DB != CalciteAssert.DatabaseInstance.MYSQL
          && CalciteAssert.DB != CalciteAssert.DatabaseInstance.H2)
      .explainContains(expectedExplain)
      .returnsUnordered("the_year=1997; quarter=Q1",
          "the_year=1997; quarter=Q2",
          "the_year=1997; quarter=Q3",
          "the_year=1997; quarter=Q4");
}
 
Example 11
Source File: BabelQuidemTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
@BeforeEach public void setup() {
  MaterializationService.setThreadLocal();
}
 
Example 12
Source File: ServerQuidemTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
@BeforeEach
public void setup() {
  MaterializationService.setThreadLocal();
}