From 1da1f93a0d37bde3ab03eb6316d5bbf7d6c0394a Mon Sep 17 00:00:00 2001 From: Ulrich Kramer Date: Thu, 17 Oct 2024 14:20:30 +0200 Subject: [PATCH] Fixes for Calcite 1.38.0 --- build.sh | 2 +- .../apache/calcite/jdbc/CalciteMetaImpl.java | 2 +- .../calcite/prepare/RelOptTableImpl.java | 2 +- .../calcite/rel/metadata/BuiltInMetadata.java | 18 ---- .../metadata/DefaultRelMetadataProvider.java | 1 - .../rel/metadata/RelMdFieldsTrimmable.java | 84 ------------------- .../rel/metadata/RelMetadataQuery.java | 23 ----- .../calcite/sql2rel/RelFieldTrimmer.java | 65 +++++++------- .../calcite/sql2rel/SqlToRelConverter.java | 67 +++++++-------- .../apache/calcite/util/BuiltInMethod.java | 3 - .../org/apache/calcite/test/JdbcTest.java | 9 +- .../org/apache/calcite/test/LatticeTest.java | 8 +- .../PostgresqlDateTimeFormatterTest.java | 13 ++- .../apache/calcite/test/DruidAdapter2IT.java | 2 +- .../apache/calcite/test/DruidAdapterIT.java | 2 +- .../calcite/server/ServerDdlExecutor.java | 8 +- 16 files changed, 88 insertions(+), 221 deletions(-) delete mode 100644 core/src/main/java/org/apache/calcite/rel/metadata/RelMdFieldsTrimmable.java diff --git a/build.sh b/build.sh index 767d01b12aa..aab9a6c5173 100755 --- a/build.sh +++ b/build.sh @@ -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 diff --git a/core/src/main/java/org/apache/calcite/jdbc/CalciteMetaImpl.java b/core/src/main/java/org/apache/calcite/jdbc/CalciteMetaImpl.java index 1251d44ebdc..230181b912f 100644 --- a/core/src/main/java/org/apache/calcite/jdbc/CalciteMetaImpl.java +++ b/core/src/main/java/org/apache/calcite/jdbc/CalciteMetaImpl.java @@ -422,7 +422,7 @@ Enumerable tables(String catalog) { Enumerable 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), diff --git a/core/src/main/java/org/apache/calcite/prepare/RelOptTableImpl.java b/core/src/main/java/org/apache/calcite/prepare/RelOptTableImpl.java index 1ff06ba261a..de3d1fa968d 100644 --- a/core/src/main/java/org/apache/calcite/prepare/RelOptTableImpl.java +++ b/core/src/main/java/org/apache/calcite/prepare/RelOptTableImpl.java @@ -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 getTableNames() { diff --git a/core/src/main/java/org/apache/calcite/rel/metadata/BuiltInMetadata.java b/core/src/main/java/org/apache/calcite/rel/metadata/BuiltInMetadata.java index e4fcdcf1298..0155a08bb4d 100644 --- a/core/src/main/java/org/apache/calcite/rel/metadata/BuiltInMetadata.java +++ b/core/src/main/java/org/apache/calcite/rel/metadata/BuiltInMetadata.java @@ -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 DEF = - MetadataDef.of(FieldsTrimmable.class, FieldsTrimmable.Handler.class, - BuiltInMethod.FIELDS_TRIMMABLE.method); - - Boolean areFieldsTrimmable(RelNode parent); - - /** Handler API. */ - @FunctionalInterface - interface Handler extends MetadataHandler { - Boolean areFieldsTrimmable(RelNode rel, RelMetadataQuery mq, RelNode parent); - - @Override default MetadataDef getDef() { - return DEF; - } - } - } - /** Metadata about which columns are sorted. */ public interface Collation extends Metadata { diff --git a/core/src/main/java/org/apache/calcite/rel/metadata/DefaultRelMetadataProvider.java b/core/src/main/java/org/apache/calcite/rel/metadata/DefaultRelMetadataProvider.java index 517d573254c..c874d36340a 100644 --- a/core/src/main/java/org/apache/calcite/rel/metadata/DefaultRelMetadataProvider.java +++ b/core/src/main/java/org/apache/calcite/rel/metadata/DefaultRelMetadataProvider.java @@ -51,7 +51,6 @@ protected DefaultRelMetadataProvider() { RelMdMinRowCount.SOURCE, RelMdUniqueKeys.SOURCE, RelMdColumnUniqueness.SOURCE, - RelMdFieldsTrimmable.SOURCE, RelMdPopulationSize.SOURCE, RelMdSize.SOURCE, RelMdParallelism.SOURCE, diff --git a/core/src/main/java/org/apache/calcite/rel/metadata/RelMdFieldsTrimmable.java b/core/src/main/java/org/apache/calcite/rel/metadata/RelMdFieldsTrimmable.java deleted file mode 100644 index e3bec56dd5b..00000000000 --- a/core/src/main/java/org/apache/calcite/rel/metadata/RelMdFieldsTrimmable.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.calcite.rel.metadata; - -import org.apache.calcite.plan.RelOptPredicateList; -import org.apache.calcite.plan.volcano.RelSubset; -import org.apache.calcite.rel.RelNode; -import org.apache.calcite.rel.SingleRel; -import org.apache.calcite.rel.convert.Converter; -import org.apache.calcite.rel.core.Aggregate; -import org.apache.calcite.rel.core.Calc; -import org.apache.calcite.rel.core.Correlate; -import org.apache.calcite.rel.core.Exchange; -import org.apache.calcite.rel.core.Filter; -import org.apache.calcite.rel.core.Intersect; -import org.apache.calcite.rel.core.Join; -import org.apache.calcite.rel.core.JoinInfo; -import org.apache.calcite.rel.core.JoinRelType; -import org.apache.calcite.rel.core.Minus; -import org.apache.calcite.rel.core.Project; -import org.apache.calcite.rel.core.SetOp; -import org.apache.calcite.rel.core.Sort; -import org.apache.calcite.rel.core.TableModify; -import org.apache.calcite.rel.core.TableScan; -import org.apache.calcite.rel.core.Values; -import org.apache.calcite.rel.type.RelDataType; -import org.apache.calcite.rel.type.RelDataTypeFactory; -import org.apache.calcite.rex.RexCall; -import org.apache.calcite.rex.RexInputRef; -import org.apache.calcite.rex.RexLiteral; -import org.apache.calcite.rex.RexNode; -import org.apache.calcite.rex.RexProgram; -import org.apache.calcite.sql.fun.SqlStdOperatorTable; -import org.apache.calcite.util.ImmutableBitSet; -import org.apache.calcite.util.Pair; -import org.apache.calcite.util.Util; - -import com.google.common.collect.ImmutableList; - -import org.checkerframework.checker.nullness.qual.Nullable; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -/** - * RelMdFieldsTrimmable supplies a default implementation of - * {@link RelMetadataQuery#areColumnsUnique} for the standard logical algebra. - */ -public class RelMdFieldsTrimmable - implements MetadataHandler { - public static final RelMetadataProvider SOURCE = - ReflectiveRelMetadataProvider.reflectiveSource( - new RelMdFieldsTrimmable(), BuiltInMetadata.FieldsTrimmable.Handler.class); - - //~ Constructors ----------------------------------------------------------- - - private RelMdFieldsTrimmable() {} - - //~ Methods ---------------------------------------------------------------- - - @Override public MetadataDef getDef() { - return BuiltInMetadata.FieldsTrimmable.DEF; - } - - public Boolean areFieldsTrimmable(RelNode node, RelMetadataQuery mq, RelNode parent) { - return true; - } -} diff --git a/core/src/main/java/org/apache/calcite/rel/metadata/RelMetadataQuery.java b/core/src/main/java/org/apache/calcite/rel/metadata/RelMetadataQuery.java index a1babc5713c..b2ea8c23da5 100644 --- a/core/src/main/java/org/apache/calcite/rel/metadata/RelMetadataQuery.java +++ b/core/src/main/java/org/apache/calcite/rel/metadata/RelMetadataQuery.java @@ -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; @@ -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); @@ -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); @@ -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; @@ -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()} diff --git a/core/src/main/java/org/apache/calcite/sql2rel/RelFieldTrimmer.java b/core/src/main/java/org/apache/calcite/sql2rel/RelFieldTrimmer.java index e495ca9d7f6..4f20ee00b8c 100644 --- a/core/src/main/java/org/apache/calcite/sql2rel/RelFieldTrimmer.java +++ b/core/src/main/java/org/apache/calcite/sql2rel/RelFieldTrimmer.java @@ -120,6 +120,7 @@ public class RelFieldTrimmer implements ReflectiveVisitor { private final ReflectUtil.MethodDispatcher trimFieldsDispatcher; private final RelBuilder relBuilder; private boolean withinDistinctAggregation = false; + private boolean withinCountStarAggregation = false; //~ Constructors ----------------------------------------------------------- @@ -201,42 +202,39 @@ protected TrimResult trimChild( final ImmutableBitSet fieldsUsed, Set 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 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 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); } /** @@ -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 @@ -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 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; diff --git a/core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java b/core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java index 0d4288179b8..9fe5bf0cb7b 100644 --- a/core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java +++ b/core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java @@ -3595,52 +3595,43 @@ private void createAggImpl(Blackboard bb, assert !aggConverter.inOver; } - // compute inputs to the aggregator - final PairList 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 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? diff --git a/core/src/main/java/org/apache/calcite/util/BuiltInMethod.java b/core/src/main/java/org/apache/calcite/util/BuiltInMethod.java index d6793edbf54..39a5da9656b 100644 --- a/core/src/main/java/org/apache/calcite/util/BuiltInMethod.java +++ b/core/src/main/java/org/apache/calcite/util/BuiltInMethod.java @@ -44,12 +44,10 @@ import org.apache.calcite.linq4j.tree.Primitive; import org.apache.calcite.linq4j.tree.Types; import org.apache.calcite.plan.volcano.VolcanoPlanner; -import org.apache.calcite.rel.RelNode; import org.apache.calcite.rel.metadata.BuiltInMetadata.AllPredicates; import org.apache.calcite.rel.metadata.BuiltInMetadata.Collation; import org.apache.calcite.rel.metadata.BuiltInMetadata.ColumnOrigin; import org.apache.calcite.rel.metadata.BuiltInMetadata.ColumnUniqueness; -import org.apache.calcite.rel.metadata.BuiltInMetadata.FieldsTrimmable; import org.apache.calcite.rel.metadata.BuiltInMetadata.CumulativeCost; import org.apache.calcite.rel.metadata.BuiltInMetadata.DistinctRowCount; import org.apache.calcite.rel.metadata.BuiltInMetadata.Distribution; @@ -877,7 +875,6 @@ public enum BuiltInMethod { MEASURE_EXPAND(Measure.class, "expand", int.class, Measure.Context.class), COLUMN_UNIQUENESS(ColumnUniqueness.class, "areColumnsUnique", ImmutableBitSet.class, boolean.class), - FIELDS_TRIMMABLE(FieldsTrimmable.class, "areFieldsTrimmable", RelNode.class), COLLATIONS(Collation.class, "collations"), DISTRIBUTION(Distribution.class, "distribution"), NODE_TYPES(NodeTypes.class, "getNodeTypes"), diff --git a/core/src/test/java/org/apache/calcite/test/JdbcTest.java b/core/src/test/java/org/apache/calcite/test/JdbcTest.java index 44f9113ff5e..e08aadf9535 100644 --- a/core/src/test/java/org/apache/calcite/test/JdbcTest.java +++ b/core/src/test/java/org/apache/calcite/test/JdbcTest.java @@ -1220,7 +1220,6 @@ private void checkResultSetMetaData(Connection connection, String sql) final CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class); final SchemaPlus rootSchema = calciteConnection.getRootSchema(); -<<<<<<< HEAD final SchemaPlus foodmart = rootSchema.subSchemas().get("foodmart"); assertThat(foodmart, notNullValue()); final JdbcTable timeByDay = @@ -8762,14 +8761,8 @@ public FoodmartJdbcSchema(DataSource dataSource, SqlDialect dialect, super(dataSource, dialect, convention, catalog, schema); } -<<<<<<< HEAD public final Table customer = - requireNonNull(getTable("customer")); -||||||| parent of c54b3c271 (EXPB-2551 Speed up schema lookup for huge schemas) - public final Table customer = getTable("customer"); -======= - public final Table customer = tables().get("customer"); ->>>>>>> c54b3c271 (EXPB-2551 Speed up schema lookup for huge schemas) + requireNonNull(tables().get("customer")); } public static class Customer { diff --git a/core/src/test/java/org/apache/calcite/test/LatticeTest.java b/core/src/test/java/org/apache/calcite/test/LatticeTest.java index 49c649bd93e..61bb05e2cd9 100644 --- a/core/src/test/java/org/apache/calcite/test/LatticeTest.java +++ b/core/src/test/java/org/apache/calcite/test/LatticeTest.java @@ -208,7 +208,7 @@ private static CalciteAssert.AssertThat modelWithLattices( final SchemaPlus schema = c.getRootSchema(); final SchemaPlus adhoc = requireNonNull(schema.subSchemas().get("adhoc")); - assertThat(adhoc.getTableNames().contains("EMPLOYEES"), is(true)); + assertThat(adhoc.tables().get("EMPLOYEES") != null, is(true)); final CalciteSchema adhocSchema = requireNonNull(adhoc.unwrap(CalciteSchema.class)); final Map.Entry entry = @@ -239,8 +239,8 @@ private static CalciteAssert.AssertThat modelWithLattices( .doWithConnection(c -> { final SchemaPlus schema = c.getRootSchema(); final SchemaPlus adhoc = - requireNonNull(schema.getSubSchema("adhoc")); - assertThat(adhoc.getTableNames().contains("EMPLOYEES"), is(true)); + requireNonNull(schema.subSchemas().get("adhoc")); + assertThat(adhoc.tables().get("EMPLOYEES") != null, is(true)); final CalciteSchema adhocSchema = requireNonNull(adhoc.unwrap(CalciteSchema.class)); final Map.Entry entry = @@ -271,7 +271,7 @@ private static CalciteAssert.AssertThat modelWithLattices( final SchemaPlus schema = c.getRootSchema(); final SchemaPlus adhoc = requireNonNull(schema.subSchemas().get("adhoc")); - assertThat(adhoc.getTableNames().contains("EMPLOYEES"), is(true)); + assertThat(adhoc.tables().get("EMPLOYEES") != null, is(true)); final CalciteSchema adhocSchema = requireNonNull(adhoc.unwrap(CalciteSchema.class)); final Map.Entry entry = diff --git a/core/src/test/java/org/apache/calcite/util/format/postgresql/PostgresqlDateTimeFormatterTest.java b/core/src/test/java/org/apache/calcite/util/format/postgresql/PostgresqlDateTimeFormatterTest.java index 636d70d78dc..c1ce1a69132 100644 --- a/core/src/test/java/org/apache/calcite/util/format/postgresql/PostgresqlDateTimeFormatterTest.java +++ b/core/src/test/java/org/apache/calcite/util/format/postgresql/PostgresqlDateTimeFormatterTest.java @@ -20,6 +20,7 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; +import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; @@ -100,7 +101,13 @@ void testHH12(String pattern) { assertThat(toCharUs("FM" + pattern + "th", hourTwo), is("2nd")); } - @Test void testHH24() { + @Test void testLiteral() throws Exception { + final ZonedDateTime time = createDateTime(2024, 1, 1, 0, 0, 0, 0); + assertThat(toTimestamp("2024-01-01 00:00:00" , "YYYY-MM-DD HH24:MI:SS"), is(time)); + assertThat(toTimestamp("2024-01-01T00:00:00" , "YYYY-MM-DD\"T\"HH24:MI:SS"), is(time)); + } + + @Test void testHH24() { final ZonedDateTime midnight = createDateTime(2024, 1, 1, 0, 0, 0, 0); final ZonedDateTime morning = createDateTime(2024, 1, 1, 6, 0, 0, 0); final ZonedDateTime noon = createDateTime(2024, 1, 1, 12, 0, 0, 0); @@ -1551,7 +1558,9 @@ void testEraLowerCaseWithDots(String pattern) { @Test void testToTimestampMonthLowerCase() throws Exception { assertThat(toTimestamp("january", "month"), is(DAY_1_CE)); - assertThat(toTimestamp("march", "month"), is(DAY_1_CE.plusMonths(2))); + assertThat(toTimestamp("march", + + "month"), is(DAY_1_CE.plusMonths(2))); assertThat(toTimestamp("november", "month"), is(DAY_1_CE.plusMonths(10))); } diff --git a/druid/src/test/java/org/apache/calcite/test/DruidAdapter2IT.java b/druid/src/test/java/org/apache/calcite/test/DruidAdapter2IT.java index 384d98409b6..b541ba00170 100644 --- a/druid/src/test/java/org/apache/calcite/test/DruidAdapter2IT.java +++ b/druid/src/test/java/org/apache/calcite/test/DruidAdapter2IT.java @@ -2626,7 +2626,7 @@ private void testCountWithApproxDistinct(boolean approx, String sql, @Test void testTableMapReused() { AbstractSchema schema = new DruidSchema("http://localhost:8082", "http://localhost:8081", true); - assertSame(schema.getTable("wikiticker"), schema.getTable("wikiticker")); + assertSame(schema.tables().get("wikiticker"), schema.tables().get("wikiticker")); } @Test void testPushEqualsCastDimension() { diff --git a/druid/src/test/java/org/apache/calcite/test/DruidAdapterIT.java b/druid/src/test/java/org/apache/calcite/test/DruidAdapterIT.java index e47fe4376ec..07da5334e49 100644 --- a/druid/src/test/java/org/apache/calcite/test/DruidAdapterIT.java +++ b/druid/src/test/java/org/apache/calcite/test/DruidAdapterIT.java @@ -3115,7 +3115,7 @@ private void testCountWithApproxDistinct(boolean approx, String sql, String expe * */ @Test void testTableMapReused() { AbstractSchema schema = new DruidSchema("http://localhost:8082", "http://localhost:8081", true); - assertSame(schema.getTable("wikipedia"), schema.getTable("wikipedia")); + assertSame(schema.tables().get("wikipedia"), schema.tables().get("wikipedia")); } @Test void testPushEqualsCastDimension() { diff --git a/server/src/main/java/org/apache/calcite/server/ServerDdlExecutor.java b/server/src/main/java/org/apache/calcite/server/ServerDdlExecutor.java index 3f24ab52e5c..3fb4545643b 100644 --- a/server/src/main/java/org/apache/calcite/server/ServerDdlExecutor.java +++ b/server/src/main/java/org/apache/calcite/server/ServerDdlExecutor.java @@ -330,7 +330,7 @@ public void execute(SqlDropObject drop, Table materializedView = schema != null && drop.getKind() == SqlKind.DROP_MATERIALIZED_VIEW - ? schema.plus().getTable(objectName) + ? schema.plus().tables().get(objectName) : null; existed = schema != null && schema.removeTable(objectName); @@ -382,7 +382,7 @@ public void execute(SqlTruncateTable truncate, final Pair<@Nullable CalciteSchema, String> pair = schema(context, true, truncate.name); if (pair.left == null - || pair.left.plus().getTable(pair.right) == null) { + || pair.left.plus().tables().get(pair.right) == null) { throw SqlUtil.newContextException(truncate.name.getParserPosition(), RESOURCE.tableNotFound(pair.right)); } @@ -401,7 +401,7 @@ public void execute(SqlCreateMaterializedView create, final Pair<@Nullable CalciteSchema, String> pair = schema(context, true, create.name); if (pair.left != null - && pair.left.plus().getTable(pair.right) != null) { + && pair.left.plus().tables().get(pair.right) != null) { // Materialized view exists. if (!create.ifNotExists) { // They did not specify IF NOT EXISTS, so give error. @@ -588,7 +588,7 @@ public void execute(SqlCreateTableLike create, final Pair<@Nullable CalciteSchema, String> pair = schema(context, true, create.name); requireNonNull(pair.left); // TODO: should not assume parent schema exists - if (pair.left.plus().getTable(pair.right) != null) { + if (pair.left.plus().tables().get(pair.right) != null) { // Table exists. if (create.ifNotExists) { return;