Skip to content

Commit

Permalink
[CALCITE-4215] Ensure org.apache.calcite.schema.Statistic uses null v…
Browse files Browse the repository at this point in the history
…s emptyList appropriately

null means the statistic is not known,
emptyList means the statistic is known, and the value is empty
  • Loading branch information
vlsi committed Oct 1, 2020
1 parent 8a7e1be commit 92ea5a2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 69 deletions.
24 changes: 18 additions & 6 deletions core/src/main/java/org/apache/calcite/schema/Statistic.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,35 @@
*/
public interface Statistic {
/** Returns the approximate number of rows in the table. */
Double getRowCount();
default Double getRowCount() {
return null;
}

/** Returns whether the given set of columns is a unique key, or a superset
* of a unique key, of the table.
*/
boolean isKey(ImmutableBitSet columns);
default boolean isKey(ImmutableBitSet columns) {
return false;
}

/** Returns a list of unique keys, or null if no key exist. */
List<ImmutableBitSet> getKeys();
default List<ImmutableBitSet> getKeys() {
return null;
}

/** Returns the collection of referential constraints (foreign-keys)
* for this table. */
List<RelReferentialConstraint> getReferentialConstraints();
default List<RelReferentialConstraint> getReferentialConstraints() {
return null;
}

/** Returns the collections of columns on which this table is sorted. */
List<RelCollation> getCollations();
default List<RelCollation> getCollations() {
return null;
}

/** Returns the distribution of the data in this table. */
RelDistribution getDistribution();
default RelDistribution getDistribution() {
return null;
}
}
53 changes: 15 additions & 38 deletions core/src/main/java/org/apache/calcite/schema/Statistics.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package org.apache.calcite.schema;

import org.apache.calcite.rel.RelCollation;
import org.apache.calcite.rel.RelDistribution;
import org.apache.calcite.rel.RelDistributionTraitDef;
import org.apache.calcite.rel.RelReferentialConstraint;
import org.apache.calcite.util.ImmutableBitSet;

Expand All @@ -36,50 +34,27 @@ private Statistics() {
/** Returns a {@link Statistic} that knows nothing about a table. */
public static final Statistic UNKNOWN =
new Statistic() {
public Double getRowCount() {
return null;
}

public boolean isKey(ImmutableBitSet columns) {
return false;
}

public List<ImmutableBitSet> getKeys() {
return ImmutableList.of();
}

public List<RelReferentialConstraint> getReferentialConstraints() {
return ImmutableList.of();
}

public List<RelCollation> getCollations() {
return ImmutableList.of();
}

public RelDistribution getDistribution() {
return RelDistributionTraitDef.INSTANCE.getDefault();
}
};

/** Returns a statistic with a given set of referential constraints. */
public static Statistic of(final List<RelReferentialConstraint> referentialConstraints) {
return of(null, ImmutableList.of(),
referentialConstraints, ImmutableList.of());
return of(null, null,
referentialConstraints, null);
}

/** Returns a statistic with a given row count and set of unique keys. */
public static Statistic of(final double rowCount,
final List<ImmutableBitSet> keys) {
return of(rowCount, keys, ImmutableList.of(),
ImmutableList.of());
return of(rowCount, keys, null,
null);
}

/** Returns a statistic with a given row count, set of unique keys,
* and collations. */
public static Statistic of(final double rowCount,
final List<ImmutableBitSet> keys,
final List<RelCollation> collations) {
return of(rowCount, keys, ImmutableList.of(), collations);
return of(rowCount, keys, null, collations);
}

/** Returns a statistic with a given row count, set of unique keys,
Expand All @@ -88,13 +63,19 @@ public static Statistic of(final Double rowCount,
final List<ImmutableBitSet> keys,
final List<RelReferentialConstraint> referentialConstraints,
final List<RelCollation> collations) {
List<ImmutableBitSet> keysCopy = keys == null ? ImmutableList.of() : ImmutableList.copyOf(keys);
List<RelReferentialConstraint> referentialConstraintsCopy =
referentialConstraints == null ? null : ImmutableList.copyOf(referentialConstraints);
List<RelCollation> collationsCopy =
collations == null ? null : ImmutableList.copyOf(collations);

return new Statistic() {
public Double getRowCount() {
return rowCount;
}

public boolean isKey(ImmutableBitSet columns) {
for (ImmutableBitSet key : keys) {
for (ImmutableBitSet key : keysCopy) {
if (columns.contains(key)) {
return true;
}
Expand All @@ -103,19 +84,15 @@ public boolean isKey(ImmutableBitSet columns) {
}

public List<ImmutableBitSet> getKeys() {
return ImmutableList.copyOf(keys);
return keysCopy;
}

public List<RelReferentialConstraint> getReferentialConstraints() {
return referentialConstraints;
return referentialConstraintsCopy;
}

public List<RelCollation> getCollations() {
return collations;
}

public RelDistribution getDistribution() {
return RelDistributionTraitDef.INSTANCE.getDefault();
return collationsCopy;
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
import org.apache.calcite.config.CalciteConnectionConfig;
import org.apache.calcite.jdbc.CalciteSchema;
import org.apache.calcite.linq4j.tree.Expression;
import org.apache.calcite.rel.RelCollation;
import org.apache.calcite.rel.RelDistribution;
import org.apache.calcite.rel.RelReferentialConstraint;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.RelDataTypeFactory;
import org.apache.calcite.rel.type.RelProtoDataType;
Expand All @@ -36,15 +33,13 @@
import org.apache.calcite.sql.dialect.CalciteSqlDialect;
import org.apache.calcite.sql.parser.SqlParser;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.util.ImmutableBitSet;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;

import org.junit.jupiter.api.Test;

import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
Expand Down Expand Up @@ -160,26 +155,6 @@ class RelToSqlConverterStructsTest {
@Override public Double getRowCount() {
return 0D;
}

@Override public boolean isKey(ImmutableBitSet columns) {
return false;
}

@Override public List<ImmutableBitSet> getKeys() {
return ImmutableList.of();
}

@Override public List<RelReferentialConstraint> getReferentialConstraints() {
return ImmutableList.of();
}

@Override public List<RelCollation> getCollations() {
return ImmutableList.of();
}

@Override public RelDistribution getDistribution() {
return null;
}
};

private static final SchemaPlus ROOT_SCHEMA = CalciteSchema
Expand Down

0 comments on commit 92ea5a2

Please sign in to comment.