Skip to content

Commit 1da1f93

Browse files
committed
Fixes for Calcite 1.38.0
1 parent 51d8cad commit 1da1f93

File tree

16 files changed

+88
-221
lines changed

16 files changed

+88
-221
lines changed

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# limitations under the License.
1717
#
1818
set -e
19-
VERSION=1.37.0
19+
VERSION=1.38.0
2020

2121
# Java 21 doesn't suppport Java 8
2222
if [ -d /Library/Java/JavaVirtualMachines/sapmachine-jdk-17.0.11.jdk/Contents/Home ]; then

core/src/main/java/org/apache/calcite/jdbc/CalciteMetaImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ Enumerable<MetaTable> tables(String catalog) {
422422

423423
Enumerable<MetaTable> tables(final MetaSchema schema_, LikePattern tableNamePattern) {
424424
final CalciteMetaSchema schema = (CalciteMetaSchema) schema_;
425-
return Linq4j.asEnumerable(schema.calciteSchema.getTableNames())
425+
return Linq4j.asEnumerable(schema.calciteSchema.getTableNames(tableNamePattern))
426426
.select(name -> {
427427
final Table table =
428428
requireNonNull(schema.calciteSchema.getTable(name, true),

core/src/main/java/org/apache/calcite/prepare/RelOptTableImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ public static MySchemaPlus create(Path path) {
506506
}
507507

508508
@Deprecated @Override public @Nullable Table getTable(String name) {
509-
return schema.getTable(name);
509+
return tables().get(name);
510510
}
511511

512512
@Deprecated @Override public Set<String> getTableNames() {

core/src/main/java/org/apache/calcite/rel/metadata/BuiltInMetadata.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -158,24 +158,6 @@ Boolean areColumnsUnique(RelNode r, RelMetadataQuery mq,
158158
}
159159
}
160160
/** Metadata about whether fields are trimmable within a relation. */
161-
public interface FieldsTrimmable extends Metadata {
162-
MetadataDef<FieldsTrimmable> DEF =
163-
MetadataDef.of(FieldsTrimmable.class, FieldsTrimmable.Handler.class,
164-
BuiltInMethod.FIELDS_TRIMMABLE.method);
165-
166-
Boolean areFieldsTrimmable(RelNode parent);
167-
168-
/** Handler API. */
169-
@FunctionalInterface
170-
interface Handler extends MetadataHandler<FieldsTrimmable> {
171-
Boolean areFieldsTrimmable(RelNode rel, RelMetadataQuery mq, RelNode parent);
172-
173-
@Override default MetadataDef<FieldsTrimmable> getDef() {
174-
return DEF;
175-
}
176-
}
177-
}
178-
179161

180162
/** Metadata about which columns are sorted. */
181163
public interface Collation extends Metadata {

core/src/main/java/org/apache/calcite/rel/metadata/DefaultRelMetadataProvider.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ protected DefaultRelMetadataProvider() {
5151
RelMdMinRowCount.SOURCE,
5252
RelMdUniqueKeys.SOURCE,
5353
RelMdColumnUniqueness.SOURCE,
54-
RelMdFieldsTrimmable.SOURCE,
5554
RelMdPopulationSize.SOURCE,
5655
RelMdSize.SOURCE,
5756
RelMdParallelism.SOURCE,

core/src/main/java/org/apache/calcite/rel/metadata/RelMdFieldsTrimmable.java

Lines changed: 0 additions & 84 deletions
This file was deleted.

core/src/main/java/org/apache/calcite/rel/metadata/RelMetadataQuery.java

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ public class RelMetadataQuery extends RelMetadataQueryBase {
8888
private BuiltInMetadata.ExpressionLineage.Handler expressionLineageHandler;
8989
private BuiltInMetadata.TableReferences.Handler tableReferencesHandler;
9090
private BuiltInMetadata.ColumnUniqueness.Handler columnUniquenessHandler;
91-
private BuiltInMetadata.FieldsTrimmable.Handler fieldsTrimmableHandler;
9291
private BuiltInMetadata.CumulativeCost.Handler cumulativeCostHandler;
9392
private BuiltInMetadata.DistinctRowCount.Handler distinctRowCountHandler;
9493
private BuiltInMetadata.Distribution.Handler distributionHandler;
@@ -131,7 +130,6 @@ public RelMetadataQuery(MetadataHandlerProvider provider) {
131130
provider.handler(BuiltInMetadata.ExpressionLineage.Handler.class);
132131
this.tableReferencesHandler = provider.handler(BuiltInMetadata.TableReferences.Handler.class);
133132
this.columnUniquenessHandler = provider.handler(BuiltInMetadata.ColumnUniqueness.Handler.class);
134-
this.fieldsTrimmableHandler = provider.handler(BuiltInMetadata.FieldsTrimmable.Handler.class);
135133
this.cumulativeCostHandler = provider.handler(BuiltInMetadata.CumulativeCost.Handler.class);
136134
this.distinctRowCountHandler = provider.handler(BuiltInMetadata.DistinctRowCount.Handler.class);
137135
this.distributionHandler = provider.handler(BuiltInMetadata.Distribution.Handler.class);
@@ -168,7 +166,6 @@ private RelMetadataQuery(@SuppressWarnings("unused") boolean dummy) {
168166
this.expressionLineageHandler = initialHandler(BuiltInMetadata.ExpressionLineage.Handler.class);
169167
this.tableReferencesHandler = initialHandler(BuiltInMetadata.TableReferences.Handler.class);
170168
this.columnUniquenessHandler = initialHandler(BuiltInMetadata.ColumnUniqueness.Handler.class);
171-
this.fieldsTrimmableHandler = initialHandler(BuiltInMetadata.FieldsTrimmable.Handler.class);
172169
this.cumulativeCostHandler = initialHandler(BuiltInMetadata.CumulativeCost.Handler.class);
173170
this.distinctRowCountHandler = initialHandler(BuiltInMetadata.DistinctRowCount.Handler.class);
174171
this.distributionHandler = initialHandler(BuiltInMetadata.Distribution.Handler.class);
@@ -201,7 +198,6 @@ private RelMetadataQuery(
201198
this.expressionLineageHandler = prototype.expressionLineageHandler;
202199
this.tableReferencesHandler = prototype.tableReferencesHandler;
203200
this.columnUniquenessHandler = prototype.columnUniquenessHandler;
204-
this.fieldsTrimmableHandler = prototype.fieldsTrimmableHandler;
205201
this.cumulativeCostHandler = prototype.cumulativeCostHandler;
206202
this.distinctRowCountHandler = prototype.distinctRowCountHandler;
207203
this.distributionHandler = prototype.distributionHandler;
@@ -597,25 +593,6 @@ public static RelMetadataQuery instance() {
597593
}
598594
}
599595

600-
/**
601-
* Returns the
602-
* {@link BuiltInMetadata.FieldsTrimmable#areFieldsTrimmable(RelNode parent)}
603-
* statistic.
604-
*
605-
* @param rel the relational expression
606-
* @return true or false depending on whether the columns are unique, or
607-
* null if not enough information is available to make that determination
608-
*/
609-
public @Nullable Boolean areFieldsTrimmable(RelNode rel, RelNode parent) {
610-
for (;;) {
611-
try {
612-
return fieldsTrimmableHandler.areFieldsTrimmable(rel, this, parent);
613-
} catch (MetadataHandlerProvider.NoHandler e) {
614-
fieldsTrimmableHandler = revise(BuiltInMetadata.FieldsTrimmable.Handler.class);
615-
}
616-
}
617-
}
618-
619596
/**
620597
* Returns the
621598
* {@link BuiltInMetadata.Collation#collations()}

core/src/main/java/org/apache/calcite/sql2rel/RelFieldTrimmer.java

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ public class RelFieldTrimmer implements ReflectiveVisitor {
120120
private final ReflectUtil.MethodDispatcher<TrimResult> trimFieldsDispatcher;
121121
private final RelBuilder relBuilder;
122122
private boolean withinDistinctAggregation = false;
123+
private boolean withinCountStarAggregation = false;
123124

124125
//~ Constructors -----------------------------------------------------------
125126

@@ -201,42 +202,39 @@ protected TrimResult trimChild(
201202
final ImmutableBitSet fieldsUsed,
202203
Set<RelDataTypeField> extraFields) {
203204

204-
ImmutableBitSet childFieldsUsed;
205-
final RelMetadataQuery mq = input.getCluster().getMetadataQuery();
206-
if ( !mq.areFieldsTrimmable(input, rel)) {
207-
childFieldsUsed = ImmutableBitSet.range(input.getRowType().getFieldCount());
208-
} else {
209-
final ImmutableBitSet.Builder fieldsUsedBuilder = fieldsUsed.rebuild();
210-
211-
// Fields that define the collation cannot be discarded.
212-
final ImmutableList<RelCollation> collations = mq.collations(input);
213-
if (collations != null) {
214-
for (RelCollation collation : collations) {
215-
for (RelFieldCollation fieldCollation : collation.getFieldCollations()) {
216-
fieldsUsedBuilder.set(fieldCollation.getFieldIndex());
217-
}
205+
if ( withinCountStarAggregation && rel instanceof Aggregate && RelOptUtil.hasCalcViewHint(rel)) {
206+
return new TrimResult(input,Mappings.createIdentity(input.getRowType().getFieldCount()));
207+
}
208+
final ImmutableBitSet.Builder fieldsUsedBuilder = fieldsUsed.rebuild();
209+
210+
// Fields that define the collation cannot be discarded.
211+
final RelMetadataQuery mq = rel.getCluster().getMetadataQuery();
212+
final ImmutableList<RelCollation> collations = mq.collations(input);
213+
if (collations != null) {
214+
for (RelCollation collation : collations) {
215+
for (RelFieldCollation fieldCollation : collation.getFieldCollations()) {
216+
fieldsUsedBuilder.set(fieldCollation.getFieldIndex());
218217
}
219218
}
219+
}
220220

221-
// Correlating variables are a means for other relational expressions to use
222-
// fields.
223-
for (final CorrelationId correlation : rel.getVariablesSet()) {
224-
rel.accept(
225-
new CorrelationReferenceFinder() {
226-
@Override
227-
protected RexNode handle(RexFieldAccess fieldAccess) {
228-
final RexCorrelVariable v =
229-
(RexCorrelVariable) fieldAccess.getReferenceExpr();
230-
if (v.id.equals(correlation)) {
231-
fieldsUsedBuilder.set(fieldAccess.getField().getIndex());
232-
}
233-
return fieldAccess;
221+
// Correlating variables are a means for other relational expressions to use
222+
// fields.
223+
for (final CorrelationId correlation : rel.getVariablesSet()) {
224+
rel.accept(
225+
new CorrelationReferenceFinder() {
226+
@Override protected RexNode handle(RexFieldAccess fieldAccess) {
227+
final RexCorrelVariable v =
228+
(RexCorrelVariable) fieldAccess.getReferenceExpr();
229+
if (v.id.equals(correlation)) {
230+
fieldsUsedBuilder.set(fieldAccess.getField().getIndex());
234231
}
235-
});
236-
}
237-
childFieldsUsed = fieldsUsedBuilder.build();
232+
return fieldAccess;
233+
}
234+
});
238235
}
239-
return dispatchTrimFields(input, childFieldsUsed, extraFields);
236+
237+
return dispatchTrimFields(input, fieldsUsedBuilder.build(), extraFields);
240238
}
241239

242240
/**
@@ -1072,6 +1070,7 @@ public TrimResult trimFields(
10721070

10731071
final RelDataType rowType = aggregate.getRowType();
10741072
boolean distinctAggregation = true;
1073+
boolean countStarAggregation = false;
10751074

10761075
// Compute which input fields are used.
10771076
// 1. group fields are always used
@@ -1086,19 +1085,23 @@ public TrimResult trimFields(
10861085
if (aggCall.distinctKeys != null) {
10871086
inputFieldsUsed.addAll(aggCall.distinctKeys);
10881087
}
1088+
countStarAggregation = countStarAggregation || aggCall.getAggregation().kind == SqlKind.COUNT;
10891089
distinctAggregation = distinctAggregation && aggCall.isDistinct();
10901090
inputFieldsUsed.addAll(RelCollations.ordinals(aggCall.collation));
10911091
}
10921092
final TrimResult trimResult;
10931093
final RelNode input = aggregate.getInput();
10941094
boolean savedDistinctAggregation = withinDistinctAggregation;
1095+
boolean savedCountStarAggregation = withinCountStarAggregation;
10951096
try {
10961097
withinDistinctAggregation = distinctAggregation;
1098+
withinCountStarAggregation = countStarAggregation;
10971099
// Create input with trimmed columns.
10981100
final Set<RelDataTypeField> inputExtraFields = Collections.emptySet();
10991101
trimResult = trimChild(aggregate, input, inputFieldsUsed.build(), inputExtraFields);
11001102
} finally {
11011103
withinDistinctAggregation = savedDistinctAggregation;
1104+
withinCountStarAggregation = savedCountStarAggregation;
11021105
}
11031106
final RelNode newInput = trimResult.left;
11041107
final Mapping inputMapping = trimResult.right;

core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3595,52 +3595,43 @@ private void createAggImpl(Blackboard bb,
35953595
assert !aggConverter.inOver;
35963596
}
35973597

3598-
// compute inputs to the aggregator
3599-
final PairList<RexNode, @Nullable String> preExprs;
3600-
if (aggConverter.convertedInputExprs.isEmpty()) {
3601-
// Special case for COUNT(*), where we can end up with no inputs
3602-
// at all. The rest of the system doesn't like 0-tuples, so we
3603-
// select a dummy constant here.
3604-
final RexNode zero = rexBuilder.makeExactLiteral(BigDecimal.ZERO);
3605-
preExprs = PairList.of(zero, null);
3606-
} else {
3607-
if (bb.root != null && RelOptUtil.hasCalcViewHint(bb.root)) {
3608-
preExprs = aggConverter.convertedInputExprs;
3609-
final RelNode inputRel = bb.root();
3610-
bb.setRoot(
3611-
relBuilder.push(inputRel)
3612-
.projectNamed(preExprs.leftList(), preExprs.rightList(), false)
3613-
.build(),
3614-
false);
3598+
if (!( aggConverter.convertedInputExprs.isEmpty() && RelOptUtil.hasCalcViewHint(bb.root()))) {
3599+
// compute inputs to the aggregator
3600+
final PairList<RexNode, @Nullable String> preExprs;
3601+
if (aggConverter.convertedInputExprs.isEmpty()) {
3602+
// Special case for COUNT(*), where we can end up with no inputs
3603+
// at all. The rest of the system doesn't like 0-tuples, so we
3604+
// select a dummy constant here.
3605+
final RexNode zero = rexBuilder.makeExactLiteral(BigDecimal.ZERO);
3606+
preExprs = PairList.of(zero, null);
36153607
} else {
36163608
preExprs = aggConverter.convertedInputExprs;
36173609
}
3618-
}
36193610

36203611
final RelNode inputRel = bb.root();
36213612

3622-
// Project the expressions required by agg and having.
3623-
RelNode intermediateProject = relBuilder.push(inputRel)
3624-
.projectNamed(preExprs.leftList(), preExprs.rightList(), false)
3625-
.build();
3626-
final RelNode r2;
3627-
// deal with correlation
3628-
final CorrelationUse p = getCorrelationUse(bb, intermediateProject);
3629-
if (p != null) {
3630-
assert p.r instanceof Project;
3631-
// correlation variables have been normalized in p.r, we should use expressions
3632-
// in p.r instead of the original exprs
3633-
Project project1 = (Project) p.r;
3634-
r2 = relBuilder.push(bb.root())
3635-
.projectNamed(project1.getProjects(), project1.getRowType().getFieldNames(),
3636-
true, ImmutableSet.of(p.id))
3613+
// Project the expressions required by agg and having.
3614+
RelNode intermediateProject = relBuilder.push(inputRel)
3615+
.projectNamed(preExprs.leftList(), preExprs.rightList(), false)
36373616
.build();
3638-
} else {
3639-
r2 = intermediateProject;
3617+
final RelNode r2;
3618+
// deal with correlation
3619+
final CorrelationUse p = getCorrelationUse(bb, intermediateProject);
3620+
if (p != null) {
3621+
assert p.r instanceof Project;
3622+
// correlation variables have been normalized in p.r, we should use expressions
3623+
// in p.r instead of the original exprs
3624+
Project project1 = (Project) p.r;
3625+
r2 = relBuilder.push(bb.root())
3626+
.projectNamed(project1.getProjects(), project1.getRowType().getFieldNames(),
3627+
true, ImmutableSet.of(p.id))
3628+
.build();
3629+
} else {
3630+
r2 = intermediateProject;
3631+
}
3632+
bb.setRoot(r2, false);
3633+
bb.mapRootRelToFieldProjection.put(bb.root(), r.groupExprProjection);
36403634
}
3641-
bb.setRoot(r2, false);
3642-
bb.mapRootRelToFieldProjection.put(bb.root(), r.groupExprProjection);
3643-
36443635
// REVIEW jvs 31-Oct-2007: doesn't the declaration of
36453636
// monotonicity here assume sort-based aggregation at
36463637
// the physical level?

0 commit comments

Comments
 (0)