Skip to content

Commit

Permalink
initial change to alter AuxiliaryConverter API to support dynamic loa…
Browse files Browse the repository at this point in the history
…ding of the implementation in SqlToRelConverter construction
  • Loading branch information
Rong Rong committed Apr 19, 2019
1 parent 395aa85 commit 9cce169
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2451,8 +2451,19 @@ public static SqlCall convertAuxiliaryToGroupCall(SqlCall call) {
*
* <p>For example, converts {@code TUMBLE_START(rowtime, INTERVAL '1' HOUR))}
* to {@code TUMBLE(rowtime, INTERVAL '1' HOUR))}. */
@Deprecated // use convertGroupToAuxiliaryCalls(SqlCall, AuxiliaryConverter)
public static List<Pair<SqlNode, AuxiliaryConverter>> convertGroupToAuxiliaryCalls(
SqlCall call) {
return convertGroupToAuxiliaryCalls(call, new AuxiliaryConverter.Impl());
}

/** Converts a call to a grouped window function to a call to its auxiliary
* window function(s). For other calls returns null.
*
* <p>For example, converts {@code TUMBLE_START(rowtime, INTERVAL '1' HOUR))}
* to {@code TUMBLE(rowtime, INTERVAL '1' HOUR))}. */
public static List<Pair<SqlNode, AuxiliaryConverter>> convertGroupToAuxiliaryCalls(
SqlCall call, AuxiliaryConverter auxiliaryConverter) {
final SqlOperator op = call.getOperator();
if (op instanceof SqlGroupedWindowFunction
&& op.isGroup()) {
Expand All @@ -2461,8 +2472,7 @@ public static List<Pair<SqlNode, AuxiliaryConverter>> convertGroupToAuxiliaryCal
for (final SqlGroupedWindowFunction f
: ((SqlGroupedWindowFunction) op).getAuxiliaryFunctions()) {
builder.add(
Pair.of(copy(call, f),
new AuxiliaryConverter.Impl(f)));
Pair.of(copy(call, f), auxiliaryConverter));
}
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,19 @@ public interface AuxiliaryConverter {
*
* @param rexBuilder Rex builder
* @param groupCall Call to the group function, e.g. "TUMBLE($2, 36000)"
* @param f The actual auxiliary SqlFunction
* @param e Expression holding result of the group function, e.g. "$0"
*
* @return Expression for auxiliary function, e.g. "$0 + 36000" converts
* the result of TUMBLE to the result of TUMBLE_END
*/
RexNode convert(RexBuilder rexBuilder, RexNode groupCall, RexNode e);
RexNode convert(RexBuilder rexBuilder, RexNode groupCall, SqlFunction f, RexNode e);

/** Simple implementation of {@link AuxiliaryConverter}. */
class Impl implements AuxiliaryConverter {
private final SqlFunction f;

public Impl(SqlFunction f) {
this.f = f;
}

public RexNode convert(RexBuilder rexBuilder, RexNode groupCall,
RexNode e) {
SqlFunction f, RexNode e) {
switch (f.getKind()) {
case TUMBLE_START:
case HOP_START:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
import org.apache.calcite.sql.SqlExplainFormat;
import org.apache.calcite.sql.SqlExplainLevel;
import org.apache.calcite.sql.SqlFunction;
import org.apache.calcite.sql.SqlGroupedWindowFunction;
import org.apache.calcite.sql.SqlIdentifier;
import org.apache.calcite.sql.SqlInsert;
import org.apache.calcite.sql.SqlIntervalQualifier;
Expand Down Expand Up @@ -243,6 +244,7 @@ public class SqlToRelConverter {
private final SqlOperatorTable opTab;
protected final RelDataTypeFactory typeFactory;
private final SqlNodeToRexConverter exprConverter;
private final AuxiliaryConverter auxiliaryConverter;
private int explainParamCount;
public final SqlToRelConverter.Config config;
private final RelBuilder relBuilder;
Expand Down Expand Up @@ -304,11 +306,25 @@ public SqlToRelConverter(
Config.DEFAULT);
}

@Deprecated // to be removed before 2.0
public SqlToRelConverter(
RelOptTable.ViewExpander viewExpander,
SqlValidator validator,
Prepare.CatalogReader catalogReader,
RelOptCluster cluster,
SqlRexConvertletTable convertletTable,
Config config) {
this(viewExpander, validator, catalogReader,
new AuxiliaryConverter.Impl(), cluster, convertletTable,
config);
}

/* Creates a converter. */
public SqlToRelConverter(
RelOptTable.ViewExpander viewExpander,
SqlValidator validator,
Prepare.CatalogReader catalogReader,
AuxiliaryConverter auxiliaryConverter,
RelOptCluster cluster,
SqlRexConvertletTable convertletTable,
Config config) {
Expand All @@ -322,6 +338,10 @@ public SqlToRelConverter(
this.subQueryConverter = new NoOpSubQueryConverter();
this.rexBuilder = cluster.getRexBuilder();
this.typeFactory = rexBuilder.getTypeFactory();
this.auxiliaryConverter =
(auxiliaryConverter
== null) ? new AuxiliaryConverter.Impl()
: auxiliaryConverter;
this.cluster = Objects.requireNonNull(cluster);
this.exprConverter = new SqlNodeToRexConverterImpl(convertletTable);
this.explainParamCount = 0;
Expand Down Expand Up @@ -4935,7 +4955,7 @@ public int addGroupExpr(SqlNode expr) {
if (expr instanceof SqlCall) {
SqlCall call = (SqlCall) expr;
for (Pair<SqlNode, AuxiliaryConverter> p
: SqlStdOperatorTable.convertGroupToAuxiliaryCalls(call)) {
: SqlStdOperatorTable.convertGroupToAuxiliaryCalls(call, auxiliaryConverter)) {
addAuxiliaryGroupExpr(p.left, index, p.right);
}
}
Expand Down Expand Up @@ -5218,6 +5238,7 @@ public RexNode lookupAggregates(SqlCall call) {
final int groupOrdinal = e.getValue().i;
return converter.convert(rexBuilder,
convertedInputExprs.get(groupOrdinal).left,
(SqlGroupedWindowFunction) call.getOperator(),
rexBuilder.makeInputRef(bb.root, groupOrdinal));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,7 @@ private void basePushFilterPastAggWithGroupingSets(boolean unchanged)
t.createSqlToRelConverter(
validator,
catalogReader,
null,
typeFactory,
SqlToRelConverter.Config.DEFAULT);

Expand All @@ -883,7 +884,11 @@ private void basePushFilterPastAggWithGroupingSets(boolean unchanged)

String planBefore = NL + RelOptUtil.toString(root.rel);
diffRepos.assertEquals("planBefore", "${planBefore}", planBefore);
converter = t.createSqlToRelConverter(validator, catalogReader, typeFactory,
converter = t.createSqlToRelConverter(
validator,
catalogReader,
null,
typeFactory,
SqlToRelConverter.configBuilder().withTrimUnusedFields(true).build());
root = root.withRel(converter.trimUnusedFields(false, root.rel));
String planAfter = NL + RelOptUtil.toString(root.rel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.apache.calcite.sql.validate.SqlValidatorCatalogReader;
import org.apache.calcite.sql.validate.SqlValidatorImpl;
import org.apache.calcite.sql.validate.SqlValidatorTable;
import org.apache.calcite.sql2rel.AuxiliaryConverter;
import org.apache.calcite.sql2rel.RelFieldTrimmer;
import org.apache.calcite.sql2rel.SqlToRelConverter;
import org.apache.calcite.sql2rel.StandardConvertletTable;
Expand Down Expand Up @@ -602,6 +603,7 @@ public RelRoot convertSqlToRel(String sql) {
createSqlToRelConverter(
validator,
catalogReader,
null,
typeFactory,
localConfig);

Expand All @@ -624,6 +626,7 @@ public RelRoot convertSqlToRel(String sql) {
protected SqlToRelConverter createSqlToRelConverter(
final SqlValidator validator,
final Prepare.CatalogReader catalogReader,
final AuxiliaryConverter auxiliaryConverter,
final RelDataTypeFactory typeFactory,
final SqlToRelConverter.Config config) {
final RexBuilder rexBuilder = new RexBuilder(typeFactory);
Expand All @@ -632,7 +635,8 @@ protected SqlToRelConverter createSqlToRelConverter(
if (clusterFactory != null) {
cluster = clusterFactory.apply(cluster);
}
return new SqlToRelConverter(null, validator, catalogReader, cluster,
return new SqlToRelConverter(null, validator, catalogReader,
auxiliaryConverter, cluster,
StandardConvertletTable.INSTANCE, config);
}

Expand Down

0 comments on commit 9cce169

Please sign in to comment.