Skip to content

Commit

Permalink
[CALCITE-3798] Make RelBuilder view expander pluggable
Browse files Browse the repository at this point in the history
User can config a view expander directly when constructing the
RelBuilder.

Deprecate RelFactories#expandingScanFactory, we already called
TranslatableTable#toRel in RelOptTable#toRel.
  • Loading branch information
danny0405 committed Feb 16, 2020
1 parent bd92764 commit da1a2dd
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private JdbcRules() {
};

public static final RelFactories.TableScanFactory TABLE_SCAN_FACTORY =
(cluster, table, hints) -> {
(toRelContext, table) -> {
throw new UnsupportedOperationException();
};

Expand Down
25 changes: 16 additions & 9 deletions core/src/main/java/org/apache/calcite/rel/core/RelFactories.java
Original file line number Diff line number Diff line change
Expand Up @@ -516,11 +516,11 @@ public interface TableScanFactory {
/**
* Creates a {@link TableScan}.
*/
RelNode createScan(RelOptCluster cluster, RelOptTable table, List<RelHint> hints);
RelNode createScan(RelOptTable.ToRelContext toRelContext, RelOptTable table);

@Deprecated // to be removed before 1.23
default RelNode createScan(RelOptCluster cluster, RelOptTable table) {
return createScan(cluster, table, ImmutableList.of());
return createScan(ViewExpanders.simpleContext(cluster), table);
}
}

Expand All @@ -529,8 +529,8 @@ default RelNode createScan(RelOptCluster cluster, RelOptTable table) {
* {@link LogicalTableScan}.
*/
private static class TableScanFactoryImpl implements TableScanFactory {
public RelNode createScan(RelOptCluster cluster, RelOptTable table, List<RelHint> hints) {
return table.toRel(ViewExpanders.simpleContext(cluster, hints));
public RelNode createScan(RelOptTable.ToRelContext toRelContext, RelOptTable table) {
return table.toRel(toRelContext);
}
}

Expand All @@ -543,19 +543,26 @@ public RelNode createScan(RelOptCluster cluster, RelOptTable table, List<RelHint
* @param viewExpander View expander
* @param tableScanFactory Factory for non-translatable tables
* @return Table scan factory
*
* @deprecated Use the custom context {@code Contexts.of(viewExpander) } for RelBuilder.
*
*/
@Deprecated // to be removed before 1.23
@Nonnull public static TableScanFactory expandingScanFactory(
@Nonnull RelOptTable.ViewExpander viewExpander,
@Nonnull TableScanFactory tableScanFactory) {
return (cluster, table, hints) -> {
return (toRelContext, table) -> {
final TranslatableTable translatableTable =
table.unwrap(TranslatableTable.class);
final RelOptTable.ToRelContext newToRelContext =
ViewExpanders.toRelContext(
viewExpander,
toRelContext.getCluster(),
toRelContext.getTableHints());
if (translatableTable != null) {
final RelOptTable.ToRelContext toRelContext =
ViewExpanders.toRelContext(viewExpander, cluster, hints);
return translatableTable.toRel(toRelContext, table);
return translatableTable.toRel(newToRelContext, table);
}
return tableScanFactory.createScan(cluster, table, hints);
return tableScanFactory.createScan(newToRelContext, table);
};
}

Expand Down
30 changes: 26 additions & 4 deletions core/src/main/java/org/apache/calcite/tools/RelBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.calcite.plan.RelOptSchema;
import org.apache.calcite.plan.RelOptTable;
import org.apache.calcite.plan.RelOptUtil;
import org.apache.calcite.plan.ViewExpanders;
import org.apache.calcite.prepare.RelOptTableImpl;
import org.apache.calcite.rel.RelCollation;
import org.apache.calcite.rel.RelCollations;
Expand Down Expand Up @@ -148,6 +149,7 @@ public class RelBuilder {
private final Deque<Frame> stack = new ArrayDeque<>();
private final RexSimplify simplifier;
private final Config config;
private final RelOptTable.ViewExpander viewExpander;
private final RelFactories.Struct struct;

protected RelBuilder(Context context, RelOptCluster cluster,
Expand All @@ -158,6 +160,7 @@ protected RelBuilder(Context context, RelOptCluster cluster,
context = Contexts.EMPTY_CONTEXT;
}
this.config = getConfig(context);
this.viewExpander = getViewExpander(cluster, context);
this.struct =
Objects.requireNonNull(RelFactories.Struct.fromContext(context));
final RexExecutor executor =
Expand All @@ -168,6 +171,23 @@ protected RelBuilder(Context context, RelOptCluster cluster,
new RexSimplify(cluster.getRexBuilder(), predicates, executor);
}

/**
* 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));
}

/** Derives the Config to be used for this RelBuilder.
*
* <p>Overrides {@link RelBuilder.Config#simplify} if
Expand Down Expand Up @@ -1021,8 +1041,9 @@ public RelBuilder scan(Iterable<String> tableNames) {
throw RESOURCE.tableNotFound(String.join(".", names)).ex();
}
final RelNode scan =
struct.scanFactory.createScan(cluster, relOptTable,
ImmutableList.of());
struct.scanFactory.createScan(
ViewExpanders.toRelContext(viewExpander, cluster),
relOptTable);
push(scan);
rename(relOptTable.getRowType().getFieldNames());

Expand Down Expand Up @@ -1920,8 +1941,9 @@ public RelBuilder transientScan(String tableName, RelDataType rowType) {
transientTable,
ImmutableList.of(tableName));
RelNode scan =
struct.scanFactory.createScan(cluster, relOptTable,
ImmutableList.of());
struct.scanFactory.createScan(
ViewExpanders.toRelContext(viewExpander, cluster),
relOptTable);
push(scan);
rename(rowType.getFieldNames());
return this;
Expand Down
11 changes: 2 additions & 9 deletions core/src/test/java/org/apache/calcite/test/RelBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.apache.calcite.rel.core.Exchange;
import org.apache.calcite.rel.core.JoinRelType;
import org.apache.calcite.rel.core.Project;
import org.apache.calcite.rel.core.RelFactories;
import org.apache.calcite.rel.core.TableFunctionScan;
import org.apache.calcite.rel.core.TableModify;
import org.apache.calcite.rel.core.Window;
Expand Down Expand Up @@ -3104,10 +3103,7 @@ private RelNode buildRelWithDuplicateAggregates(
expandingConfig(connection);
final RelOptTable.ViewExpander viewExpander =
(RelOptTable.ViewExpander) Frameworks.getPlanner(configBuilder.build());
final RelFactories.TableScanFactory tableScanFactory =
RelFactories.expandingScanFactory(viewExpander,
RelFactories.DEFAULT_TABLE_SCAN_FACTORY);
configBuilder.context(Contexts.of(tableScanFactory));
configBuilder.context(Contexts.of(viewExpander));
final RelBuilder builder = RelBuilder.create(configBuilder.build());
RelNode node = builder.scan("MYVIEW").build();

Expand All @@ -3130,10 +3126,7 @@ private RelNode buildRelWithDuplicateAggregates(
expandingConfig(connection);
final RelOptTable.ViewExpander viewExpander =
(RelOptTable.ViewExpander) Frameworks.getPlanner(configBuilder.build());
final RelFactories.TableScanFactory tableScanFactory =
RelFactories.expandingScanFactory(viewExpander,
RelFactories.DEFAULT_TABLE_SCAN_FACTORY);
configBuilder.context(Contexts.of(tableScanFactory));
configBuilder.context(Contexts.of(viewExpander));
final RelBuilder builder = RelBuilder.create(configBuilder.build());
RelNode node =
builder.scan("MYVIEW")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public static class PigTableScanFactory implements RelFactories.TableScanFactory

public static final PigTableScanFactory INSTANCE = new PigTableScanFactory();

@Override public RelNode createScan(RelOptCluster cluster,
RelOptTable table, List<RelHint> hints) {
Util.discard(hints);
@Override public RelNode createScan(RelOptTable.ToRelContext toRelContext,
RelOptTable table) {
final RelOptCluster cluster = toRelContext.getCluster();
return new PigTableScan(cluster, cluster.traitSetOf(PigRel.CONVENTION), table);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.calcite.plan.RelOptCluster;
import org.apache.calcite.plan.RelOptSchema;
import org.apache.calcite.plan.RelOptTable;
import org.apache.calcite.plan.ViewExpanders;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.SingleRel;
import org.apache.calcite.rel.core.CorrelationId;
Expand Down Expand Up @@ -260,7 +261,8 @@ public RelBuilder scan(RelOptTable userSchema, String... tableNames) {
* @return This builder
*/
private RelBuilder scan(RelOptTable tableSchema) {
final RelNode scan = getScanFactory().createScan(cluster, tableSchema, ImmutableList.of());
final RelNode scan = getScanFactory().createScan(
ViewExpanders.simpleContext(cluster), tableSchema);
push(scan);
return this;
}
Expand Down

0 comments on commit da1a2dd

Please sign in to comment.