Skip to content

Commit

Permalink
Fixes for Calcite 1.38.0
Browse files Browse the repository at this point in the history
  • Loading branch information
kramerul committed Oct 21, 2024
1 parent 51d8cad commit 1da1f93
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 221 deletions.
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# limitations under the License.
#
set -e
VERSION=1.37.0
VERSION=1.38.0

# Java 21 doesn't suppport Java 8
if [ -d /Library/Java/JavaVirtualMachines/sapmachine-jdk-17.0.11.jdk/Contents/Home ]; then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ Enumerable<MetaTable> tables(String catalog) {

Enumerable<MetaTable> tables(final MetaSchema schema_, LikePattern tableNamePattern) {
final CalciteMetaSchema schema = (CalciteMetaSchema) schema_;
return Linq4j.asEnumerable(schema.calciteSchema.getTableNames())
return Linq4j.asEnumerable(schema.calciteSchema.getTableNames(tableNamePattern))
.select(name -> {
final Table table =
requireNonNull(schema.calciteSchema.getTable(name, true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ public static MySchemaPlus create(Path path) {
}

@Deprecated @Override public @Nullable Table getTable(String name) {
return schema.getTable(name);
return tables().get(name);
}

@Deprecated @Override public Set<String> getTableNames() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,24 +158,6 @@ Boolean areColumnsUnique(RelNode r, RelMetadataQuery mq,
}
}
/** Metadata about whether fields are trimmable within a relation. */
public interface FieldsTrimmable extends Metadata {
MetadataDef<FieldsTrimmable> DEF =
MetadataDef.of(FieldsTrimmable.class, FieldsTrimmable.Handler.class,
BuiltInMethod.FIELDS_TRIMMABLE.method);

Boolean areFieldsTrimmable(RelNode parent);

/** Handler API. */
@FunctionalInterface
interface Handler extends MetadataHandler<FieldsTrimmable> {
Boolean areFieldsTrimmable(RelNode rel, RelMetadataQuery mq, RelNode parent);

@Override default MetadataDef<FieldsTrimmable> getDef() {
return DEF;
}
}
}


/** Metadata about which columns are sorted. */
public interface Collation extends Metadata {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ protected DefaultRelMetadataProvider() {
RelMdMinRowCount.SOURCE,
RelMdUniqueKeys.SOURCE,
RelMdColumnUniqueness.SOURCE,
RelMdFieldsTrimmable.SOURCE,
RelMdPopulationSize.SOURCE,
RelMdSize.SOURCE,
RelMdParallelism.SOURCE,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ public class RelMetadataQuery extends RelMetadataQueryBase {
private BuiltInMetadata.ExpressionLineage.Handler expressionLineageHandler;
private BuiltInMetadata.TableReferences.Handler tableReferencesHandler;
private BuiltInMetadata.ColumnUniqueness.Handler columnUniquenessHandler;
private BuiltInMetadata.FieldsTrimmable.Handler fieldsTrimmableHandler;
private BuiltInMetadata.CumulativeCost.Handler cumulativeCostHandler;
private BuiltInMetadata.DistinctRowCount.Handler distinctRowCountHandler;
private BuiltInMetadata.Distribution.Handler distributionHandler;
Expand Down Expand Up @@ -131,7 +130,6 @@ public RelMetadataQuery(MetadataHandlerProvider provider) {
provider.handler(BuiltInMetadata.ExpressionLineage.Handler.class);
this.tableReferencesHandler = provider.handler(BuiltInMetadata.TableReferences.Handler.class);
this.columnUniquenessHandler = provider.handler(BuiltInMetadata.ColumnUniqueness.Handler.class);
this.fieldsTrimmableHandler = provider.handler(BuiltInMetadata.FieldsTrimmable.Handler.class);
this.cumulativeCostHandler = provider.handler(BuiltInMetadata.CumulativeCost.Handler.class);
this.distinctRowCountHandler = provider.handler(BuiltInMetadata.DistinctRowCount.Handler.class);
this.distributionHandler = provider.handler(BuiltInMetadata.Distribution.Handler.class);
Expand Down Expand Up @@ -168,7 +166,6 @@ private RelMetadataQuery(@SuppressWarnings("unused") boolean dummy) {
this.expressionLineageHandler = initialHandler(BuiltInMetadata.ExpressionLineage.Handler.class);
this.tableReferencesHandler = initialHandler(BuiltInMetadata.TableReferences.Handler.class);
this.columnUniquenessHandler = initialHandler(BuiltInMetadata.ColumnUniqueness.Handler.class);
this.fieldsTrimmableHandler = initialHandler(BuiltInMetadata.FieldsTrimmable.Handler.class);
this.cumulativeCostHandler = initialHandler(BuiltInMetadata.CumulativeCost.Handler.class);
this.distinctRowCountHandler = initialHandler(BuiltInMetadata.DistinctRowCount.Handler.class);
this.distributionHandler = initialHandler(BuiltInMetadata.Distribution.Handler.class);
Expand Down Expand Up @@ -201,7 +198,6 @@ private RelMetadataQuery(
this.expressionLineageHandler = prototype.expressionLineageHandler;
this.tableReferencesHandler = prototype.tableReferencesHandler;
this.columnUniquenessHandler = prototype.columnUniquenessHandler;
this.fieldsTrimmableHandler = prototype.fieldsTrimmableHandler;
this.cumulativeCostHandler = prototype.cumulativeCostHandler;
this.distinctRowCountHandler = prototype.distinctRowCountHandler;
this.distributionHandler = prototype.distributionHandler;
Expand Down Expand Up @@ -597,25 +593,6 @@ public static RelMetadataQuery instance() {
}
}

/**
* Returns the
* {@link BuiltInMetadata.FieldsTrimmable#areFieldsTrimmable(RelNode parent)}
* statistic.
*
* @param rel the relational expression
* @return true or false depending on whether the columns are unique, or
* null if not enough information is available to make that determination
*/
public @Nullable Boolean areFieldsTrimmable(RelNode rel, RelNode parent) {
for (;;) {
try {
return fieldsTrimmableHandler.areFieldsTrimmable(rel, this, parent);
} catch (MetadataHandlerProvider.NoHandler e) {
fieldsTrimmableHandler = revise(BuiltInMetadata.FieldsTrimmable.Handler.class);
}
}
}

/**
* Returns the
* {@link BuiltInMetadata.Collation#collations()}
Expand Down
65 changes: 34 additions & 31 deletions core/src/main/java/org/apache/calcite/sql2rel/RelFieldTrimmer.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public class RelFieldTrimmer implements ReflectiveVisitor {
private final ReflectUtil.MethodDispatcher<TrimResult> trimFieldsDispatcher;
private final RelBuilder relBuilder;
private boolean withinDistinctAggregation = false;
private boolean withinCountStarAggregation = false;

//~ Constructors -----------------------------------------------------------

Expand Down Expand Up @@ -201,42 +202,39 @@ protected TrimResult trimChild(
final ImmutableBitSet fieldsUsed,
Set<RelDataTypeField> extraFields) {

ImmutableBitSet childFieldsUsed;
final RelMetadataQuery mq = input.getCluster().getMetadataQuery();
if ( !mq.areFieldsTrimmable(input, rel)) {
childFieldsUsed = ImmutableBitSet.range(input.getRowType().getFieldCount());
} else {
final ImmutableBitSet.Builder fieldsUsedBuilder = fieldsUsed.rebuild();

// Fields that define the collation cannot be discarded.
final ImmutableList<RelCollation> collations = mq.collations(input);
if (collations != null) {
for (RelCollation collation : collations) {
for (RelFieldCollation fieldCollation : collation.getFieldCollations()) {
fieldsUsedBuilder.set(fieldCollation.getFieldIndex());
}
if ( withinCountStarAggregation && rel instanceof Aggregate && RelOptUtil.hasCalcViewHint(rel)) {
return new TrimResult(input,Mappings.createIdentity(input.getRowType().getFieldCount()));
}
final ImmutableBitSet.Builder fieldsUsedBuilder = fieldsUsed.rebuild();

// Fields that define the collation cannot be discarded.
final RelMetadataQuery mq = rel.getCluster().getMetadataQuery();
final ImmutableList<RelCollation> collations = mq.collations(input);
if (collations != null) {
for (RelCollation collation : collations) {
for (RelFieldCollation fieldCollation : collation.getFieldCollations()) {
fieldsUsedBuilder.set(fieldCollation.getFieldIndex());
}
}
}

// Correlating variables are a means for other relational expressions to use
// fields.
for (final CorrelationId correlation : rel.getVariablesSet()) {
rel.accept(
new CorrelationReferenceFinder() {
@Override
protected RexNode handle(RexFieldAccess fieldAccess) {
final RexCorrelVariable v =
(RexCorrelVariable) fieldAccess.getReferenceExpr();
if (v.id.equals(correlation)) {
fieldsUsedBuilder.set(fieldAccess.getField().getIndex());
}
return fieldAccess;
// Correlating variables are a means for other relational expressions to use
// fields.
for (final CorrelationId correlation : rel.getVariablesSet()) {
rel.accept(
new CorrelationReferenceFinder() {
@Override protected RexNode handle(RexFieldAccess fieldAccess) {
final RexCorrelVariable v =
(RexCorrelVariable) fieldAccess.getReferenceExpr();
if (v.id.equals(correlation)) {
fieldsUsedBuilder.set(fieldAccess.getField().getIndex());
}
});
}
childFieldsUsed = fieldsUsedBuilder.build();
return fieldAccess;
}
});
}
return dispatchTrimFields(input, childFieldsUsed, extraFields);

return dispatchTrimFields(input, fieldsUsedBuilder.build(), extraFields);
}

/**
Expand Down Expand Up @@ -1072,6 +1070,7 @@ public TrimResult trimFields(

final RelDataType rowType = aggregate.getRowType();
boolean distinctAggregation = true;
boolean countStarAggregation = false;

// Compute which input fields are used.
// 1. group fields are always used
Expand All @@ -1086,19 +1085,23 @@ public TrimResult trimFields(
if (aggCall.distinctKeys != null) {
inputFieldsUsed.addAll(aggCall.distinctKeys);
}
countStarAggregation = countStarAggregation || aggCall.getAggregation().kind == SqlKind.COUNT;
distinctAggregation = distinctAggregation && aggCall.isDistinct();
inputFieldsUsed.addAll(RelCollations.ordinals(aggCall.collation));
}
final TrimResult trimResult;
final RelNode input = aggregate.getInput();
boolean savedDistinctAggregation = withinDistinctAggregation;
boolean savedCountStarAggregation = withinCountStarAggregation;
try {
withinDistinctAggregation = distinctAggregation;
withinCountStarAggregation = countStarAggregation;
// Create input with trimmed columns.
final Set<RelDataTypeField> inputExtraFields = Collections.emptySet();
trimResult = trimChild(aggregate, input, inputFieldsUsed.build(), inputExtraFields);
} finally {
withinDistinctAggregation = savedDistinctAggregation;
withinCountStarAggregation = savedCountStarAggregation;
}
final RelNode newInput = trimResult.left;
final Mapping inputMapping = trimResult.right;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3595,52 +3595,43 @@ private void createAggImpl(Blackboard bb,
assert !aggConverter.inOver;
}

// compute inputs to the aggregator
final PairList<RexNode, @Nullable String> preExprs;
if (aggConverter.convertedInputExprs.isEmpty()) {
// Special case for COUNT(*), where we can end up with no inputs
// at all. The rest of the system doesn't like 0-tuples, so we
// select a dummy constant here.
final RexNode zero = rexBuilder.makeExactLiteral(BigDecimal.ZERO);
preExprs = PairList.of(zero, null);
} else {
if (bb.root != null && RelOptUtil.hasCalcViewHint(bb.root)) {
preExprs = aggConverter.convertedInputExprs;
final RelNode inputRel = bb.root();
bb.setRoot(
relBuilder.push(inputRel)
.projectNamed(preExprs.leftList(), preExprs.rightList(), false)
.build(),
false);
if (!( aggConverter.convertedInputExprs.isEmpty() && RelOptUtil.hasCalcViewHint(bb.root()))) {
// compute inputs to the aggregator
final PairList<RexNode, @Nullable String> preExprs;
if (aggConverter.convertedInputExprs.isEmpty()) {
// Special case for COUNT(*), where we can end up with no inputs
// at all. The rest of the system doesn't like 0-tuples, so we
// select a dummy constant here.
final RexNode zero = rexBuilder.makeExactLiteral(BigDecimal.ZERO);
preExprs = PairList.of(zero, null);
} else {
preExprs = aggConverter.convertedInputExprs;
}
}

final RelNode inputRel = bb.root();

// Project the expressions required by agg and having.
RelNode intermediateProject = relBuilder.push(inputRel)
.projectNamed(preExprs.leftList(), preExprs.rightList(), false)
.build();
final RelNode r2;
// deal with correlation
final CorrelationUse p = getCorrelationUse(bb, intermediateProject);
if (p != null) {
assert p.r instanceof Project;
// correlation variables have been normalized in p.r, we should use expressions
// in p.r instead of the original exprs
Project project1 = (Project) p.r;
r2 = relBuilder.push(bb.root())
.projectNamed(project1.getProjects(), project1.getRowType().getFieldNames(),
true, ImmutableSet.of(p.id))
// Project the expressions required by agg and having.
RelNode intermediateProject = relBuilder.push(inputRel)
.projectNamed(preExprs.leftList(), preExprs.rightList(), false)
.build();
} else {
r2 = intermediateProject;
final RelNode r2;
// deal with correlation
final CorrelationUse p = getCorrelationUse(bb, intermediateProject);
if (p != null) {
assert p.r instanceof Project;
// correlation variables have been normalized in p.r, we should use expressions
// in p.r instead of the original exprs
Project project1 = (Project) p.r;
r2 = relBuilder.push(bb.root())
.projectNamed(project1.getProjects(), project1.getRowType().getFieldNames(),
true, ImmutableSet.of(p.id))
.build();
} else {
r2 = intermediateProject;
}
bb.setRoot(r2, false);
bb.mapRootRelToFieldProjection.put(bb.root(), r.groupExprProjection);
}
bb.setRoot(r2, false);
bb.mapRootRelToFieldProjection.put(bb.root(), r.groupExprProjection);

// REVIEW jvs 31-Oct-2007: doesn't the declaration of
// monotonicity here assume sort-based aggregation at
// the physical level?
Expand Down
Loading

0 comments on commit 1da1f93

Please sign in to comment.