Skip to content

Commit

Permalink
WIP: core nullability
Browse files Browse the repository at this point in the history
  • Loading branch information
vlsi committed Aug 31, 2020
1 parent 696d8f1 commit 4b3b45d
Show file tree
Hide file tree
Showing 120 changed files with 628 additions and 355 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public int size() {
// We have discovered a the first unique key in the table.
sort[0] = pair.i;
final Comparable[] values =
valueSet.values.toArray(new Comparable[list.size()]);
valueSet.values.toArray(new Comparable[0]);
final Kev[] kevs = new Kev[list.size()];
for (int i = 0; i < kevs.length; i++) {
kevs[i] = new Kev(i, values[i]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import com.google.common.collect.Multimap;
import com.google.common.collect.Ordering;

import org.checkerframework.checker.nullness.qual.Nullable;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
Expand Down Expand Up @@ -481,15 +483,15 @@ protected Map<String, RelProtoDataType> getTypes() {
return ImmutableMap.of();
}

@Override public RelProtoDataType getType(String name) {
@Override public @Nullable RelProtoDataType getType(String name) {
return getTypes().get(name);
}

@Override public Set<String> getTypeNames() {
return getTypes().keySet();
}

public Schema getSubSchema(String name) {
public @Nullable Schema getSubSchema(String name) {
// JDBC does not support sub-schemas.
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public static Function1<ResultSet, Function0<Object[]>> factory(
try {
return new ObjectArrayRowBuilder(
resultSet,
Pair.left(list).toArray(new ColumnMetaData.Rep[list.size()]),
Pair.left(list).toArray(new ColumnMetaData.Rep[0]),
Ints.toArray(Pair.right(list)));
} catch (SQLException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.apache.calcite.plan.RelTraitSet;
import org.apache.calcite.rel.RelNode;

import org.checkerframework.checker.nullness.qual.Nullable;

/**
* Calling convention that returns results as an
* {@link org.apache.calcite.linq4j.Enumerable} of object arrays.
Expand Down Expand Up @@ -52,7 +54,7 @@ public String getName() {
return "BINDABLE";
}

@Override public RelNode enforce(RelNode input, RelTraitSet required) {
@Override public @Nullable RelNode enforce(RelNode input, RelTraitSet required) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@

import org.apache.calcite.DataContext;

import org.checkerframework.checker.nullness.qual.Nullable;

/**
* Context for executing a scalar expression in an interpreter.
*/
public class Context {
public final DataContext root;

/** Values of incoming columns from all inputs. */
public Object[] values;
public Object @Nullable [] values;

Context(DataContext root) {
this.root = root;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.ImmutableSortedSet;

import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.Collection;
import java.util.List;
import java.util.Set;
Expand All @@ -50,11 +52,11 @@ class CachingCalciteSchema extends CalciteSchema {
private boolean cache = true;

/** Creates a CachingCalciteSchema. */
CachingCalciteSchema(CalciteSchema parent, Schema schema, String name) {
CachingCalciteSchema(@Nullable CalciteSchema parent, Schema schema, String name) {
this(parent, schema, name, null, null, null, null, null, null, null, null);
}

private CachingCalciteSchema(CalciteSchema parent, Schema schema,
private CachingCalciteSchema(@Nullable CalciteSchema parent, Schema schema,
String name, NameMap<CalciteSchema> subSchemaMap,
NameMap<TableEntry> tableMap, NameMap<LatticeEntry> latticeMap, NameMap<TypeEntry> typeMap,
NameMultimap<FunctionEntry> functionMap, NameSet functionNames,
Expand Down
16 changes: 10 additions & 6 deletions core/src/main/java/org/apache/calcite/jdbc/CalcitePrepare.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;

import org.checkerframework.checker.nullness.qual.Nullable;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
Expand All @@ -58,6 +60,8 @@
import java.util.List;
import java.util.Map;

import static org.apache.calcite.linq4j.Nullness.assertNonNull;

/**
* API for a service that prepares statements for execution.
*/
Expand Down Expand Up @@ -172,7 +176,7 @@ private static SparkHandler createHandler() {
final Class<?> clazz =
Class.forName("org.apache.calcite.adapter.spark.SparkHandlerImpl");
Method method = clazz.getMethod("instance");
return (CalcitePrepare.SparkHandler) method.invoke(null);
return (CalcitePrepare.SparkHandler) assertNonNull(method.invoke(null));
} catch (ClassNotFoundException e) {
return new TrivialSparkHandler();
} catch (IllegalAccessException
Expand Down Expand Up @@ -341,7 +345,7 @@ public CalciteSignature(String sql,
List<RelCollation> collationList,
long maxRowCount,
Bindable<T> bindable,
Meta.StatementType statementType) {
Meta.@Nullable StatementType statementType) {
super(columns, sql, parameterList, internalParameters, cursorFactory,
statementType);
this.rowType = rowType;
Expand Down Expand Up @@ -372,11 +376,11 @@ public List<RelCollation> getCollationList() {
*
* @param <T> element type */
class Query<T> {
public final String sql;
public final Queryable<T> queryable;
public final RelNode rel;
public final @Nullable String sql;
public final @Nullable Queryable<T> queryable;
public final @Nullable RelNode rel;

private Query(String sql, Queryable<T> queryable, RelNode rel) {
private Query(@Nullable String sql, @Nullable Queryable<T> queryable, @Nullable RelNode rel) {
this.sql = sql;
this.queryable = queryable;
this.rel = rel;
Expand Down
28 changes: 17 additions & 11 deletions core/src/main/java/org/apache/calcite/jdbc/CalciteSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Lists;

import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand All @@ -58,7 +60,7 @@
*/
public abstract class CalciteSchema {

private final CalciteSchema parent;
private final @Nullable CalciteSchema parent;
public final Schema schema;
public final String name;
/** Tables explicitly defined in this schema. Does not include tables in
Expand All @@ -70,14 +72,18 @@ public abstract class CalciteSchema {
protected final NameSet functionNames;
protected final NameMap<FunctionEntry> nullaryFunctionMap;
protected final NameMap<CalciteSchema> subSchemaMap;
private List<? extends List<String>> path;

protected CalciteSchema(CalciteSchema parent, Schema schema,
String name, NameMap<CalciteSchema> subSchemaMap,
NameMap<TableEntry> tableMap, NameMap<LatticeEntry> latticeMap, NameMap<TypeEntry> typeMap,
NameMultimap<FunctionEntry> functionMap, NameSet functionNames,
NameMap<FunctionEntry> nullaryFunctionMap,
List<? extends List<String>> path) {
private @Nullable List<? extends List<String>> path;

protected CalciteSchema(@Nullable CalciteSchema parent, Schema schema,
String name,
@Nullable NameMap<CalciteSchema> subSchemaMap,
@Nullable NameMap<TableEntry> tableMap,
@Nullable NameMap<LatticeEntry> latticeMap,
@Nullable NameMap<TypeEntry> typeMap,
@Nullable NameMultimap<FunctionEntry> functionMap,
@Nullable NameSet functionNames,
@Nullable NameMap<FunctionEntry> nullaryFunctionMap,
@Nullable List<? extends List<String>> path) {
this.parent = parent;
this.schema = schema;
this.name = name;
Expand Down Expand Up @@ -662,7 +668,7 @@ public NavigableSet<String> getTableNames() {
return CalciteSchema.this.getTableNames();
}

@Override public RelProtoDataType getType(String name) {
@Override public @Nullable RelProtoDataType getType(String name) {
final TypeEntry entry = CalciteSchema.this.getType(name, true);
return entry == null ? null : entry.getType();
}
Expand All @@ -679,7 +685,7 @@ public NavigableSet<String> getFunctionNames() {
return CalciteSchema.this.getFunctionNames();
}

public SchemaPlus getSubSchema(String name) {
public @Nullable SchemaPlus getSubSchema(String name) {
final CalciteSchema subSchema =
CalciteSchema.this.getSubSchema(name, true);
return subSchema == null ? null : subSchema.plus();
Expand Down
9 changes: 7 additions & 2 deletions core/src/main/java/org/apache/calcite/jdbc/JavaCollation.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

import org.apache.calcite.sql.SqlCollation;

import org.checkerframework.checker.initialization.qual.UnderInitialization;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.nio.charset.Charset;
import java.text.Collator;
import java.util.Locale;
Expand Down Expand Up @@ -55,11 +58,13 @@ private static String getStrengthString(int strengthValue) {
}
}

@Override protected String generateCollationName(Charset charset) {
@Override protected String generateCollationName(
@UnderInitialization(SqlCollation.class) JavaCollation this,
Charset charset) {
return super.generateCollationName(charset) + "$JAVA_COLLATOR";
}

@Override public Collator getCollator() {
@Override public @Nullable Collator getCollator() {
return collator;
}
}
7 changes: 7 additions & 0 deletions core/src/main/java/org/apache/calcite/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,11 @@
/**
* Main package for Calcite, the dynamic data management platform.
*/
@DefaultQualifier(value = NonNull.class, locations = TypeUseLocation.FIELD)
@DefaultQualifier(value = NonNull.class, locations = TypeUseLocation.PARAMETER)
@DefaultQualifier(value = NonNull.class, locations = TypeUseLocation.RETURN)
package org.apache.calcite;

import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.checkerframework.framework.qual.TypeUseLocation;
4 changes: 3 additions & 1 deletion core/src/main/java/org/apache/calcite/plan/Convention.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.core.RelFactories;

import org.checkerframework.checker.nullness.qual.Nullable;

/**
* Calling convention trait.
*/
Expand Down Expand Up @@ -48,7 +50,7 @@ public interface Convention extends RelTrait {
* or {@code null} if trait enforcement is not allowed or the
* required traitSet can't be satisfied.
*/
default RelNode enforce(RelNode input, RelTraitSet required) {
default @Nullable RelNode enforce(RelNode input, RelTraitSet required) {
throw new RuntimeException(getClass().getName()
+ "#enforce() is not implemented.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.calcite.util.CancelFlag;
import org.apache.calcite.util.trace.CalciteTrace;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.slf4j.Logger;

import java.util.List;
Expand Down Expand Up @@ -249,7 +250,7 @@ RelNode register(
* @param equivRel Relational expression it is equivalent to (may be null)
* @return Registered relational expression
*/
RelNode ensureRegistered(RelNode rel, RelNode equivRel);
RelNode ensureRegistered(RelNode rel, @Nullable RelNode equivRel);

/**
* Determines whether a relational expression has been registered.
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/apache/calcite/plan/RelOptRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ public boolean matches(RelOptRuleCall call) {
* @return Convention of the result of firing this rule, null if
* not known
*/
public Convention getOutConvention() {
public @Nullable Convention getOutConvention() {
return null;
}

Expand All @@ -561,7 +561,7 @@ public Convention getOutConvention() {
* @return Trait which will be modified as a result of firing this rule,
* or null if the rule is not a converter rule
*/
public RelTrait getOutTrait() {
public @Nullable RelTrait getOutTrait() {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.slf4j.Logger;

import java.util.HashMap;
Expand Down Expand Up @@ -54,7 +55,7 @@ public abstract class RelOptRuleCall {
public final RelOptRule rule;
public final RelNode[] rels;
private final RelOptPlanner planner;
private final List<RelNode> parents;
private final @Nullable List<RelNode> parents;

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

Expand All @@ -77,7 +78,7 @@ protected RelOptRuleCall(
RelOptRuleOperand operand,
RelNode[] rels,
Map<RelNode, List<RelNode>> nodeInputs,
List<RelNode> parents) {
@Nullable List<RelNode> parents) {
this.id = nextId++;
this.planner = planner;
this.operand0 = operand;
Expand Down Expand Up @@ -171,7 +172,7 @@ public <T extends RelNode> T rel(int ordinal) {
* @param rel Relational expression
* @return Children of relational expression
*/
public List<RelNode> getChildRels(RelNode rel) {
public @Nullable List<RelNode> getChildRels(RelNode rel) {
return nodeInputs.get(rel);
}

Expand Down Expand Up @@ -221,7 +222,7 @@ public RelMetadataQuery getMetadataQuery() {
/**
* Returns a list of parents of the first relational expression.
*/
public List<RelNode> getParents() {
public @Nullable List<RelNode> getParents() {
return parents;
}

Expand Down
Loading

0 comments on commit 4b3b45d

Please sign in to comment.