Java Code Examples for org.apache.calcite.plan.RelOptTable#ViewExpander

The following examples show how to use org.apache.calcite.plan.RelOptTable#ViewExpander . 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: SqlToRelTestBase.java    From calcite with Apache License 2.0 6 votes vote down vote up
protected SqlToRelConverter createSqlToRelConverter(
    final SqlValidator validator,
    final Prepare.CatalogReader catalogReader,
    final RelDataTypeFactory typeFactory,
    final SqlToRelConverter.Config config) {
  final RexBuilder rexBuilder = new RexBuilder(typeFactory);
  RelOptCluster cluster =
      RelOptCluster.create(getPlanner(), rexBuilder);
  if (clusterFactory != null) {
    cluster = clusterFactory.apply(cluster);
  }
  RelOptTable.ViewExpander viewExpander =
      new MockViewExpander(validator, catalogReader, cluster, config);
  return new SqlToRelConverter(viewExpander, validator, catalogReader, cluster,
      StandardConvertletTable.INSTANCE, config);
}
 
Example 2
Source File: RelBuilderTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Ensures that relational algebra ({@link RelBuilder}) works with SQL views.
 *
 * <p>This test currently fails (thus ignored).
 */
@Test void testExpandViewInRelBuilder() throws SQLException {
  try (Connection connection = DriverManager.getConnection("jdbc:calcite:")) {
    final Frameworks.ConfigBuilder configBuilder =
        expandingConfig(connection);
    final RelOptTable.ViewExpander viewExpander =
        (RelOptTable.ViewExpander) Frameworks.getPlanner(configBuilder.build());
    configBuilder.context(Contexts.of(viewExpander));
    final RelBuilder builder = RelBuilder.create(configBuilder.build());
    RelNode node = builder.scan("MYVIEW").build();

    int count = 0;
    try (PreparedStatement statement =
             connection.unwrap(RelRunner.class).prepare(node);
         ResultSet resultSet = statement.executeQuery()) {
      while (resultSet.next()) {
        count++;
      }
    }

    assertTrue(count > 1);
  }
}
 
Example 3
Source File: RelBuilderTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testExpandViewShouldKeepAlias() throws SQLException {
  try (Connection connection = DriverManager.getConnection("jdbc:calcite:")) {
    final Frameworks.ConfigBuilder configBuilder =
        expandingConfig(connection);
    final RelOptTable.ViewExpander viewExpander =
        (RelOptTable.ViewExpander) Frameworks.getPlanner(configBuilder.build());
    configBuilder.context(Contexts.of(viewExpander));
    final RelBuilder builder = RelBuilder.create(configBuilder.build());
    RelNode node =
        builder.scan("MYVIEW")
            .project(
                builder.field(1, "MYVIEW", "EMPNO"),
                builder.field(1, "MYVIEW", "ENAME"))
            .build();
    String expected =
        "LogicalProject(EMPNO=[$0], ENAME=[$1])\n"
            + "  LogicalFilter(condition=[=(1, 1)])\n"
                + "    LogicalTableScan(table=[[scott, EMP]])\n";
    assertThat(node, hasTree(expected));
  }
}
 
Example 4
Source File: BatsOptimizerTest.java    From Bats with Apache License 2.0 5 votes vote down vote up
static RelNode testSqlToRelConverter(RelOptPlanner planner) throws Exception {
    RexBuilder rexBuilder = createRexBuilder();
    RelOptCluster cluster = RelOptCluster.create(planner, rexBuilder);
    RelOptTable.ViewExpander viewExpander = ViewExpanders.simpleContext(cluster);

    Pair<SqlNode, SqlValidator> pair = testSqlValidator();
    SqlNode sqlNode = pair.left;
    SqlValidator validator = pair.right;
    CatalogReader catalogReader = createCalciteCatalogReader();
    SqlRexConvertletTable convertletTable = StandardConvertletTable.INSTANCE;
    SqlToRelConverter.Config config = SqlToRelConverter.Config.DEFAULT;
    // 不转换成EnumerableTableScan,而是LogicalTableScan
    config = SqlToRelConverter.configBuilder().withConvertTableAccess(false).build();

    SqlToRelConverter converter = new SqlToRelConverter(viewExpander, validator, catalogReader, cluster,
            convertletTable, config);

    boolean needsValidation = false;
    boolean top = false;
    RelRoot root = converter.convertQuery(sqlNode, needsValidation, top);
    RelNode relNode = root.rel;

    String plan = RelOptUtil.toString(relNode);
    System.out.println("Logical Plan:");
    System.out.println("------------------------------------------------------------------");
    System.out.println(plan);
    System.out.println();

    // testPrograms(root.rel);

    return relNode;
}
 
Example 5
Source File: RelFactories.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a {@link TableScanFactory} that uses a
 * {@link org.apache.calcite.plan.RelOptTable.ViewExpander} to handle
 * {@link TranslatableTable} instances, and falls back to a default
 * factory for other tables.
 *
 * @param viewExpander View expander
 * @param tableScanFactory Factory for non-translatable tables
 * @return Table scan factory
 */
public static TableScanFactory expandingScanFactory(
    RelOptTable.ViewExpander viewExpander,
    TableScanFactory tableScanFactory) {
  return (cluster, table) -> {
    final TranslatableTable translatableTable =
        table.unwrap(TranslatableTable.class);
    if (translatableTable != null) {
      final RelOptTable.ToRelContext toRelContext =
          ViewExpanders.toRelContext(viewExpander, cluster);
      return translatableTable.toRel(toRelContext, table);
    }
    return tableScanFactory.createScan(cluster, table);
  };
}
 
Example 6
Source File: LixToRelTranslator.java    From calcite with Apache License 2.0 5 votes vote down vote up
RelOptTable.ToRelContext toRelContext() {
  if (preparingStmt instanceof RelOptTable.ViewExpander) {
    final RelOptTable.ViewExpander viewExpander =
        (RelOptTable.ViewExpander) this.preparingStmt;
    return ViewExpanders.toRelContext(viewExpander, cluster);
  } else {
    return ViewExpanders.simpleContext(cluster);
  }
}
 
Example 7
Source File: Frameworks.java    From calcite with Apache License 2.0 5 votes vote down vote up
StdFrameworkConfig(Context context,
    SqlRexConvertletTable convertletTable,
    SqlOperatorTable operatorTable,
    ImmutableList<Program> programs,
    ImmutableList<RelTraitDef> traitDefs,
    SqlParser.Config parserConfig,
    SqlValidator.Config sqlValidatorConfig,
    SqlToRelConverter.Config sqlToRelConverterConfig,
    SchemaPlus defaultSchema,
    RelOptCostFactory costFactory,
    RelDataTypeSystem typeSystem,
    RexExecutor executor,
    boolean evolveLattice,
    SqlStatisticProvider statisticProvider,
    RelOptTable.ViewExpander viewExpander) {
  this.context = context;
  this.convertletTable = convertletTable;
  this.operatorTable = operatorTable;
  this.programs = programs;
  this.traitDefs = traitDefs;
  this.parserConfig = parserConfig;
  this.sqlValidatorConfig = sqlValidatorConfig;
  this.sqlToRelConverterConfig = sqlToRelConverterConfig;
  this.defaultSchema = defaultSchema;
  this.costFactory = costFactory;
  this.typeSystem = typeSystem;
  this.executor = executor;
  this.evolveLattice = evolveLattice;
  this.statisticProvider = statisticProvider;
  this.viewExpander = viewExpander;
}
 
Example 8
Source File: MycatCalciteDataContext.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
@Override
public RelOptTable.ViewExpander getViewExpander() {
    return MycatCalciteSupport.INSTANCE.config.getViewExpander();
}
 
Example 9
Source File: Frameworks.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RelOptTable.ViewExpander getViewExpander() {
  return viewExpander;
}
 
Example 10
Source File: FrameworkConfig.java    From Bats with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a view expander.
 */
RelOptTable.ViewExpander getViewExpander();
 
Example 11
Source File: FrameworkConfig.java    From calcite with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a view expander.
 */
RelOptTable.ViewExpander getViewExpander();
 
Example 12
Source File: RelBuilder.java    From calcite with Apache License 2.0 2 votes vote down vote up
/**
 * Derives the view expander
 * {@link org.apache.calcite.plan.RelOptTable.ViewExpander}
 * to be used for this RelBuilder.
 *
 * <p>The ViewExpander instance is used for expanding views in the default
 * table scan factory {@code RelFactories.TableScanFactoryImpl}.
 * You can also define a new table scan factory in the {@code struct}
 * to override the whole table scan creation.
 *
 * <p>The default view expander does not support expanding views.
 */
private RelOptTable.ViewExpander getViewExpander(RelOptCluster cluster, Context context) {
  return Util.first(context.unwrap(RelOptTable.ViewExpander.class),
      ViewExpanders.simpleContext(cluster));
}