org.apache.calcite.rel.metadata.RelMetadataProvider Java Examples

The following examples show how to use org.apache.calcite.rel.metadata.RelMetadataProvider. 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: Programs.java    From Bats with Apache License 2.0 6 votes vote down vote up
/** Creates a program that executes a {@link HepProgram}. */
public static Program of(final HepProgram hepProgram, final boolean noDag,
    final RelMetadataProvider metadataProvider) {
  return (planner, rel, requiredOutputTraits, materializations, lattices) -> {
    final HepPlanner hepPlanner = new HepPlanner(hepProgram,
        null, noDag, null, RelOptCostImpl.FACTORY);

    List<RelMetadataProvider> list = new ArrayList<>();
    if (metadataProvider != null) {
      list.add(metadataProvider);
    }
    hepPlanner.registerMetadataProviders(list);
    RelMetadataProvider plannerChain =
        ChainedRelMetadataProvider.of(list);
    rel.getCluster().setMetadataProvider(plannerChain);

    hepPlanner.setRoot(rel);
    return hepPlanner.findBestExp();
  };
}
 
Example #2
Source File: Programs.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Creates a program that executes a {@link HepProgram}. */
public static Program of(final HepProgram hepProgram, final boolean noDag,
    final RelMetadataProvider metadataProvider) {
  return (planner, rel, requiredOutputTraits, materializations, lattices) -> {
    final HepPlanner hepPlanner = new HepPlanner(hepProgram,
        null, noDag, null, RelOptCostImpl.FACTORY);

    List<RelMetadataProvider> list = new ArrayList<>();
    if (metadataProvider != null) {
      list.add(metadataProvider);
    }
    hepPlanner.registerMetadataProviders(list);
    for (RelOptMaterialization materialization : materializations) {
      hepPlanner.addMaterialization(materialization);
    }
    for (RelOptLattice lattice : lattices) {
      hepPlanner.addLattice(lattice);
    }
    RelMetadataProvider plannerChain =
        ChainedRelMetadataProvider.of(list);
    rel.getCluster().setMetadataProvider(plannerChain);

    hepPlanner.setRoot(rel);
    return hepPlanner.findBestExp();
  };
}
 
Example #3
Source File: Programs.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a program that executes a list of rules in a HEP planner. */
public static Program hep(Iterable<? extends RelOptRule> rules,
    boolean noDag, RelMetadataProvider metadataProvider) {
  final HepProgramBuilder builder = HepProgram.builder();
  for (RelOptRule rule : rules) {
    builder.addRuleInstance(rule);
  }
  return of(builder.build(), noDag, metadataProvider);
}
 
Example #4
Source File: Programs.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static Program subQuery(RelMetadataProvider metadataProvider) {
  final HepProgramBuilder builder = HepProgram.builder();
  builder.addRuleCollection(
      ImmutableList.of(SubQueryRemoveRule.FILTER,
          SubQueryRemoveRule.PROJECT,
          SubQueryRemoveRule.JOIN));
  return of(builder.build(), true, metadataProvider);
}
 
Example #5
Source File: Programs.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Returns the standard program with user metadata provider. */
public static Program standard(RelMetadataProvider metadataProvider) {
  final Program program1 =
      (planner, rel, requiredOutputTraits, materializations, lattices) -> {
        planner.setRoot(rel);

        for (RelOptMaterialization materialization : materializations) {
          planner.addMaterialization(materialization);
        }
        for (RelOptLattice lattice : lattices) {
          planner.addLattice(lattice);
        }

        final RelNode rootRel2 =
            rel.getTraitSet().equals(requiredOutputTraits)
                ? rel
                : planner.changeTraits(rel, requiredOutputTraits);
        assert rootRel2 != null;

        planner.setRoot(rootRel2);
        final RelOptPlanner planner2 = planner.chooseDelegate();
        final RelNode rootRel3 = planner2.findBestExp();
        assert rootRel3 != null : "could not implement exp";
        return rootRel3;
      };

  return sequence(subQuery(metadataProvider),
      new DecorrelateProgram(),
      new TrimFieldsProgram(),
      program1,

      // Second planner pass to do physical "tweaks". This the first time
      // that EnumerableCalcRel is introduced.
      calc(metadataProvider));
}
 
Example #6
Source File: RelOptCluster.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Overrides the default metadata provider for this cluster.
 *
 * @param metadataProvider custom provider
 */
public void setMetadataProvider(RelMetadataProvider metadataProvider) {
  this.metadataProvider = metadataProvider;
  this.metadataFactory = new MetadataFactoryImpl(metadataProvider);
  // Wrap the metadata provider as a JaninoRelMetadataProvider
  // and set it to the ThreadLocal,
  // JaninoRelMetadataProvider is required by the RelMetadataQuery.
  RelMetadataQueryBase.THREAD_PROVIDERS
      .set(JaninoRelMetadataProvider.of(metadataProvider));
}
 
Example #7
Source File: Programs.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Returns the standard program with user metadata provider. */
public static Program standard(RelMetadataProvider metadataProvider) {
  final Program program1 =
      (planner, rel, requiredOutputTraits, materializations, lattices) -> {
        planner.setRoot(rel);

        for (RelOptMaterialization materialization : materializations) {
          planner.addMaterialization(materialization);
        }
        for (RelOptLattice lattice : lattices) {
          planner.addLattice(lattice);
        }

        final RelNode rootRel2 =
            rel.getTraitSet().equals(requiredOutputTraits)
                ? rel
                : planner.changeTraits(rel, requiredOutputTraits);
        assert rootRel2 != null;

        planner.setRoot(rootRel2);
        final RelOptPlanner planner2 = planner.chooseDelegate();
        final RelNode rootRel3 = planner2.findBestExp();
        assert rootRel3 != null : "could not implement exp";
        return rootRel3;
      };

  return sequence(subQuery(metadataProvider),
      new DecorrelateProgram(),
      new TrimFieldsProgram(),
      program1,

      // Second planner pass to do physical "tweaks". This the first time
      // that EnumerableCalcRel is introduced.
      calc(metadataProvider));
}
 
Example #8
Source File: Programs.java    From Bats with Apache License 2.0 5 votes vote down vote up
public static Program subQuery(RelMetadataProvider metadataProvider) {
  final HepProgramBuilder builder = HepProgram.builder();
  builder.addRuleCollection(ImmutableList.of((RelOptRule) SubQueryRemoveRule.FILTER,
      SubQueryRemoveRule.PROJECT,
      SubQueryRemoveRule.JOIN));
  return of(builder.build(), true, metadataProvider);
}
 
Example #9
Source File: Programs.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Creates a program that executes a list of rules in a HEP planner. */
public static Program hep(Iterable<? extends RelOptRule> rules,
    boolean noDag, RelMetadataProvider metadataProvider) {
  final HepProgramBuilder builder = HepProgram.builder();
  for (RelOptRule rule : rules) {
    builder.addRuleInstance(rule);
  }
  return of(builder.build(), noDag, metadataProvider);
}
 
Example #10
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Test void testCustomProviderWithRelMetadataFactory() {
  final List<String> buf = new ArrayList<>();
  ColTypeImpl.THREAD_LIST.set(buf);

  final String sql = "select deptno, count(*) from emp where deptno > 10 "
      + "group by deptno having count(*) = 0";
  final RelRoot root = tester
      .withClusterFactory(cluster -> {
        // Create a custom provider that includes ColType.
        // Include the same provider twice just to be devious.
        final ImmutableList<RelMetadataProvider> list =
            ImmutableList.of(ColTypeImpl.SOURCE, ColTypeImpl.SOURCE,
                cluster.getMetadataProvider());
        cluster.setMetadataProvider(
            ChainedRelMetadataProvider.of(list));
        return cluster;
      })
      .convertSqlToRel(sql);
  final RelNode rel = root.rel;

  // Top node is a filter. Its metadata uses getColType(RelNode, int).
  assertThat(rel, instanceOf(LogicalFilter.class));
  final RelMetadataQuery mq = rel.getCluster().getMetadataQuery();
  assertThat(colType(mq, rel, 0), equalTo("DEPTNO-rel"));
  assertThat(colType(mq, rel, 1), equalTo("EXPR$1-rel"));

  // Next node is an aggregate. Its metadata uses
  // getColType(LogicalAggregate, int).
  final RelNode input = rel.getInput(0);
  assertThat(input, instanceOf(LogicalAggregate.class));
  assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));

  // There is no caching. Another request causes another call to the provider.
  assertThat(buf.toString(), equalTo("[DEPTNO-rel, EXPR$1-rel, DEPTNO-agg]"));
  assertThat(buf.size(), equalTo(3));
  assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));
  assertThat(buf.size(), equalTo(4));

  // Now add a cache. Only the first request for each piece of metadata
  // generates a new call to the provider.
  final RelOptPlanner planner = rel.getCluster().getPlanner();
  rel.getCluster().setMetadataProvider(
      new CachingRelMetadataProvider(
          rel.getCluster().getMetadataProvider(), planner));
  assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));
  assertThat(buf.size(), equalTo(5));
  assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));
  assertThat(buf.size(), equalTo(5));
  assertThat(colType(mq, input, 1), equalTo("EXPR$1-agg"));
  assertThat(buf.size(), equalTo(6));
  assertThat(colType(mq, input, 1), equalTo("EXPR$1-agg"));
  assertThat(buf.size(), equalTo(6));
  assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));
  assertThat(buf.size(), equalTo(6));

  // With a different timestamp, a metadata item is re-computed on first call.
  long timestamp = planner.getRelMetadataTimestamp(rel);
  assertThat(timestamp, equalTo(0L));
  ((MockRelOptPlanner) planner).setRelMetadataTimestamp(timestamp + 1);
  assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));
  assertThat(buf.size(), equalTo(7));
  assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));
  assertThat(buf.size(), equalTo(7));
}
 
Example #11
Source File: AbstractRelOptPlanner.java    From calcite with Apache License 2.0 4 votes vote down vote up
public void registerMetadataProviders(List<RelMetadataProvider> list) {
}
 
Example #12
Source File: RelOptCluster.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RelMetadataProvider getMetadataProvider() {
  return metadataProvider;
}
 
Example #13
Source File: HepPlanner.java    From calcite with Apache License 2.0 4 votes vote down vote up
public void registerMetadataProviders(List<RelMetadataProvider> list) {
  list.add(0, new HepRelMetadataProvider());
}
 
Example #14
Source File: VolcanoPlanner.java    From calcite with Apache License 2.0 4 votes vote down vote up
public void registerMetadataProviders(List<RelMetadataProvider> list) {
  list.add(0, new VolcanoRelMetadataProvider());
}
 
Example #15
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Test void testCustomProviderWithRelMetadataQuery() {
  final List<String> buf = new ArrayList<>();
  ColTypeImpl.THREAD_LIST.set(buf);

  final String sql = "select deptno, count(*) from emp where deptno > 10 "
      + "group by deptno having count(*) = 0";
  final RelRoot root = tester
      .withClusterFactory(cluster -> {
        // Create a custom provider that includes ColType.
        // Include the same provider twice just to be devious.
        final ImmutableList<RelMetadataProvider> list =
            ImmutableList.of(ColTypeImpl.SOURCE, ColTypeImpl.SOURCE,
                cluster.getMetadataProvider());
        cluster.setMetadataProvider(
            ChainedRelMetadataProvider.of(list));
        cluster.setMetadataQuerySupplier(MyRelMetadataQuery::new);
        return cluster;
      })
      .convertSqlToRel(sql);
  final RelNode rel = root.rel;

  // Top node is a filter. Its metadata uses getColType(RelNode, int).
  assertThat(rel, instanceOf(LogicalFilter.class));
  assertThat(rel.getCluster().getMetadataQuery(), instanceOf(MyRelMetadataQuery.class));
  final MyRelMetadataQuery mq = (MyRelMetadataQuery) rel.getCluster().getMetadataQuery();
  assertThat(colType(mq, rel, 0), equalTo("DEPTNO-rel"));
  assertThat(colType(mq, rel, 1), equalTo("EXPR$1-rel"));

  // Next node is an aggregate. Its metadata uses
  // getColType(LogicalAggregate, int).
  final RelNode input = rel.getInput(0);
  assertThat(input, instanceOf(LogicalAggregate.class));
  assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));

  // The metadata query is caching, only the first request for each piece of metadata
  // generates a new call to the provider.
  assertThat(buf.toString(), equalTo("[DEPTNO-rel, EXPR$1-rel, DEPTNO-agg]"));
  assertThat(buf.size(), equalTo(3));
  assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));
  assertThat(buf.size(), equalTo(3));
  assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));
  assertThat(buf.size(), equalTo(3));
  assertThat(colType(mq, input, 1), equalTo("EXPR$1-agg"));
  assertThat(buf.size(), equalTo(4));
  assertThat(colType(mq, input, 1), equalTo("EXPR$1-agg"));
  assertThat(buf.size(), equalTo(4));
  assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));
  assertThat(buf.size(), equalTo(4));

  // Invalidate the metadata query triggers clearing of all the metadata.
  rel.getCluster().invalidateMetadataQuery();
  assertThat(rel.getCluster().getMetadataQuery(), instanceOf(MyRelMetadataQuery.class));
  final MyRelMetadataQuery mq1 = (MyRelMetadataQuery) rel.getCluster().getMetadataQuery();
  assertThat(colType(mq1, input, 0), equalTo("DEPTNO-agg"));
  assertThat(buf.size(), equalTo(5));
  assertThat(colType(mq1, input, 0), equalTo("DEPTNO-agg"));
  assertThat(buf.size(), equalTo(5));
  // Resets the RelMetadataQuery to default.
  rel.getCluster().setMetadataQuerySupplier(RelMetadataQuery::instance);
}
 
Example #16
Source File: Programs.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Deprecated // to be removed before 2.0
public static Program subquery(RelMetadataProvider metadataProvider) {
  return subQuery(metadataProvider);
}
 
Example #17
Source File: Programs.java    From calcite with Apache License 2.0 4 votes vote down vote up
public static Program calc(RelMetadataProvider metadataProvider) {
  return hep(RelOptRules.CALC_RULES, true, metadataProvider);
}
 
Example #18
Source File: RelOptTestBase.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Checks the plan for a SQL statement before/after executing a given rule,
 * with a pre-program to prepare the tree.
 *
 * @param tester     Tester
 * @param preProgram Program to execute before comparing before state
 * @param planner    Planner
 * @param sql        SQL query
 * @param unchanged  Whether the rule is to have no effect
 */
private void checkPlanning(Tester tester, HepProgram preProgram,
    RelOptPlanner planner, String sql, boolean unchanged) {
  final DiffRepository diffRepos = getDiffRepos();
  String sql2 = diffRepos.expand("sql", sql);
  final RelRoot root = tester.convertSqlToRel(sql2);
  final RelNode relInitial = root.rel;

  assertNotNull(relInitial);

  List<RelMetadataProvider> list = new ArrayList<>();
  list.add(DefaultRelMetadataProvider.INSTANCE);
  planner.registerMetadataProviders(list);
  RelMetadataProvider plannerChain =
      ChainedRelMetadataProvider.of(list);
  final RelOptCluster cluster = relInitial.getCluster();
  cluster.setMetadataProvider(plannerChain);

  RelNode relBefore;
  if (preProgram == null) {
    relBefore = relInitial;
  } else {
    HepPlanner prePlanner = new HepPlanner(preProgram);
    prePlanner.setRoot(relInitial);
    relBefore = prePlanner.findBestExp();
  }

  assertThat(relBefore, notNullValue());

  final String planBefore = NL + RelOptUtil.toString(relBefore);
  diffRepos.assertEquals("planBefore", "${planBefore}", planBefore);
  SqlToRelTestBase.assertValid(relBefore);

  if (planner instanceof VolcanoPlanner) {
    relBefore = planner.changeTraits(relBefore,
        relBefore.getTraitSet().replace(EnumerableConvention.INSTANCE));
  }
  planner.setRoot(relBefore);
  RelNode r = planner.findBestExp();
  if (tester.isLateDecorrelate()) {
    final String planMid = NL + RelOptUtil.toString(r);
    diffRepos.assertEquals("planMid", "${planMid}", planMid);
    SqlToRelTestBase.assertValid(r);
    final RelBuilder relBuilder =
        RelFactories.LOGICAL_BUILDER.create(cluster, null);
    r = RelDecorrelator.decorrelateQuery(r, relBuilder);
  }
  final String planAfter = NL + RelOptUtil.toString(r);
  if (unchanged) {
    assertThat(planAfter, is(planBefore));
  } else {
    diffRepos.assertEquals("planAfter", "${planAfter}", planAfter);
    if (planBefore.equals(planAfter)) {
      throw new AssertionError("Expected plan before and after is the same.\n"
          + "You must use unchanged=true or call checkUnchanged");
    }
  }
  SqlToRelTestBase.assertValid(r);
}
 
Example #19
Source File: CachingRelMetadataProvider.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public CachingRelMetadataProvider(
    RelMetadataProvider underlyingProvider,
    RelOptPlanner planner) {
  this.underlyingProvider = underlyingProvider;
  this.planner = planner;
}
 
Example #20
Source File: DremioVolcanoPlanner.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public void registerMetadataProviders(List<RelMetadataProvider> list) {
  // Do nothing - in practice, prevent VolcanoRelMetadataProvider to be registered
}
 
Example #21
Source File: RelOptCluster.java    From flink with Apache License 2.0 4 votes vote down vote up
public RelMetadataProvider getMetadataProvider() {
	return metadataProvider;
}
 
Example #22
Source File: RelOptCluster.java    From flink with Apache License 2.0 4 votes vote down vote up
public RelMetadataProvider getMetadataProvider() {
	return metadataProvider;
}
 
Example #23
Source File: DefaultSqlHandler.java    From Bats with Apache License 2.0 4 votes vote down vote up
public MetaDataProviderModifier(RelMetadataProvider metadataProvider) {
  this.metadataProvider = metadataProvider;
}
 
Example #24
Source File: AbstractRelOptPlanner.java    From Bats with Apache License 2.0 4 votes vote down vote up
public void registerMetadataProviders(List<RelMetadataProvider> list) {
}
 
Example #25
Source File: RelOptCluster.java    From Bats with Apache License 2.0 4 votes vote down vote up
public RelMetadataProvider getMetadataProvider() {
  return metadataProvider;
}
 
Example #26
Source File: HepPlanner.java    From Bats with Apache License 2.0 4 votes vote down vote up
public void registerMetadataProviders(List<RelMetadataProvider> list) {
  list.add(0, new HepRelMetadataProvider());
}
 
Example #27
Source File: VolcanoPlanner.java    From Bats with Apache License 2.0 4 votes vote down vote up
public void registerMetadataProviders(List<RelMetadataProvider> list) {
  list.add(0, new VolcanoRelMetadataProvider());
}
 
Example #28
Source File: Programs.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Deprecated // to be removed before 2.0
public static Program subquery(RelMetadataProvider metadataProvider) {
  return subQuery(metadataProvider);
}
 
Example #29
Source File: Programs.java    From Bats with Apache License 2.0 4 votes vote down vote up
public static Program calc(RelMetadataProvider metadataProvider) {
  return hep(CALC_RULES, true, metadataProvider);
}
 
Example #30
Source File: RelOptCluster.java    From flink with Apache License 2.0 2 votes vote down vote up
/**
 * Overrides the default metadata provider for this cluster.
 *
 * @param metadataProvider custom provider
 */
public void setMetadataProvider(RelMetadataProvider metadataProvider) {
	this.metadataProvider = metadataProvider;
	this.metadataFactory = new MetadataFactoryImpl(metadataProvider);
}