Skip to content

Commit

Permalink
[CALCITE-4199] Add nullability annotations to core
Browse files Browse the repository at this point in the history
  • Loading branch information
vlsi committed Sep 26, 2020
1 parent 0a51c7f commit 4f558d7
Show file tree
Hide file tree
Showing 577 changed files with 5,977 additions and 3,903 deletions.
10 changes: 6 additions & 4 deletions core/src/main/java/org/apache/calcite/DataContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

import com.google.common.base.CaseFormat;

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

import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Modifier;
Expand All @@ -42,17 +44,17 @@ public interface DataContext {
/**
* Returns a sub-schema with a given name, or null.
*/
SchemaPlus getRootSchema();
@Nullable SchemaPlus getRootSchema();

/**
* Returns the type factory.
*/
JavaTypeFactory getTypeFactory();
@Nullable JavaTypeFactory getTypeFactory();

/**
* Returns the query provider.
*/
QueryProvider getQueryProvider();
@Nullable QueryProvider getQueryProvider();

/**
* Returns a context variable.
Expand All @@ -62,7 +64,7 @@ public interface DataContext {
*
* @param name Name of variable
*/
Object get(String name);
@Nullable Object get(String name);

/** Variable that may be asked for in a call to {@link DataContext#get}. */
enum Variable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.apache.calcite.rel.type.RelProtoDataType;
import org.apache.calcite.util.Util;

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

import java.lang.reflect.Type;
import java.sql.Date;
import java.sql.Time;
Expand Down Expand Up @@ -195,7 +197,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 Expand Up @@ -271,15 +273,15 @@ static class ValueSet {
final Class clazz;
final Map<Comparable, Comparable> map = new HashMap<>();
final List<Comparable> values = new ArrayList<>();
Comparable min;
Comparable max;
@Nullable Comparable min;
@Nullable Comparable max;
boolean containsNull;

ValueSet(Class clazz) {
this.clazz = clazz;
}

void add(Comparable e) {
void add(@Nullable Comparable e) {
if (e != null) {
final Comparable old = e;
e = map.get(e);
Expand Down Expand Up @@ -357,7 +359,7 @@ private long toLong(Object o) {
}
}

private boolean canBeLong(Object o) {
private boolean canBeLong(@Nullable Object o) {
return o instanceof Boolean
|| o instanceof Character
|| o instanceof Number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.apache.calcite.linq4j.tree.Expression;
import org.apache.calcite.rex.RexNode;

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

import java.util.List;

/**
Expand All @@ -41,7 +43,7 @@ public interface AggAddContext extends AggResultContext {
* Returns {@link org.apache.calcite.rex.RexNode} representation of the
* filter, or null.
*/
RexNode rexFilterArgument();
@Nullable RexNode rexFilterArgument();

/**
* Returns Linq4j form of arguments.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.apache.calcite.linq4j.tree.Expression;
import org.apache.calcite.rel.core.AggregateCall;

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

import java.util.List;

/**
Expand All @@ -28,21 +30,21 @@ public class AggImpState {
public final int aggIdx;
public final AggregateCall call;
public final AggImplementor implementor;
public AggContext context;
public Expression result;
public List<Expression> state;
public Expression accumulatorAdder;
public @MonotonicNonNull AggContext context;
public @MonotonicNonNull Expression result;
public @MonotonicNonNull List<Expression> state;
public @MonotonicNonNull Expression accumulatorAdder;

public AggImpState(int aggIdx, AggregateCall call, boolean windowContext) {
this.aggIdx = aggIdx;
this.call = call;
this.implementor =
RexImpTable.INSTANCE.get(call.getAggregation(), windowContext);
AggImplementor implementor = RexImpTable.INSTANCE.get(call.getAggregation(), windowContext);
if (implementor == null) {
throw new IllegalArgumentException(
"Unable to get aggregate implementation for aggregate "
+ call.getAggregation()
+ (windowContext ? " in window context" : ""));
}
this.implementor = implementor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@

import com.google.common.collect.ImmutableList;

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

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
Expand All @@ -68,6 +70,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;

/**
* Utilities for generating programs in the Enumerable (functional)
Expand Down Expand Up @@ -121,15 +124,16 @@ public int size() {

static List<RelDataType> fieldRowTypes(
final RelDataType inputRowType,
final List<? extends RexNode> extraInputs,
final @Nullable List<? extends RexNode> extraInputs,
final List<Integer> argList) {
final List<RelDataTypeField> inputFields = inputRowType.getFieldList();
return new AbstractList<RelDataType>() {
public RelDataType get(int index) {
final int arg = argList.get(index);
return arg < inputFields.size()
? inputFields.get(arg).getType()
: extraInputs.get(arg - inputFields.size()).getType();
: Objects.requireNonNull(extraInputs, "extraInputs")
.get(arg - inputFields.size()).getType();
}
public int size() {
return argList.size();
Expand Down Expand Up @@ -254,7 +258,7 @@ private static Expression fromInternal(Expression operand,
&& Primitive.isBox(targetType)) {
// E.g. operand is "int", target is "Long", generate "(long) operand".
return Expressions.convert_(operand,
Primitive.ofBox(targetType).primitiveClass);
Primitive.unbox(targetType));
}
return operand;
}
Expand Down Expand Up @@ -296,7 +300,7 @@ private static Type toInternal(RelDataType type) {
return toInternal(type, false);
}

static Type toInternal(RelDataType type, boolean forceNotNull) {
static @Nullable Type toInternal(RelDataType type, boolean forceNotNull) {
switch (type.getSqlTypeName()) {
case DATE:
case TIME:
Expand Down Expand Up @@ -640,7 +644,7 @@ public static MethodCallExpression call(Class clazz, String methodName,
* @throws RuntimeException if no suitable method found
*/
public static MethodCallExpression call(Class clazz, String methodName,
List<? extends Expression> arguments, Expression targetExpression) {
List<? extends Expression> arguments, @Nullable Expression targetExpression) {
Class[] argumentTypes = Types.toClassArray(arguments);
try {
Method candidate = clazz.getMethod(methodName, argumentTypes);
Expand All @@ -666,7 +670,7 @@ public static MethodCallExpression call(Class clazz, String methodName,
}
}

private static List<? extends Expression> matchMethodParameterTypes(boolean varArgs,
private static @Nullable List<? extends Expression> matchMethodParameterTypes(boolean varArgs,
Class[] parameterTypes, List<? extends Expression> arguments) {
if ((varArgs && arguments.size() < parameterTypes.length - 1)
|| (!varArgs && arguments.size() != parameterTypes.length)) {
Expand Down Expand Up @@ -696,7 +700,7 @@ private static List<? extends Expression> matchMethodParameterTypes(boolean varA
* @return Converted argument expression that matches the parameter type.
* Returns null if it is impossible to match.
*/
private static Expression matchMethodParameterType(
private static @Nullable Expression matchMethodParameterType(
Expression argument, Class parameter) {
Type argumentType = argument.getType();
if (Types.isAssignableFrom(parameter, argumentType)) {
Expand Down Expand Up @@ -883,7 +887,7 @@ private static class SessionizationEnumerator implements Enumerator<Object[]> {
initialize();
initialized = true;
}
return list.pollFirst();
return Objects.requireNonNull(list.pollFirst(), "list.pollFirst()");
}

@Override public boolean moveNext() {
Expand Down Expand Up @@ -1049,8 +1053,8 @@ public void reset() {
public void close() {
}

private Object[] takeOne() {
return list.pollFirst();
private Object @Nullable [] takeOne() {
return Objects.requireNonNull(list.pollFirst(), "list.pollFirst()");
}
}

Expand Down Expand Up @@ -1098,7 +1102,7 @@ public void close() {
};
}

public static Expression generateCollatorExpression(SqlCollation collation) {
public static @Nullable Expression generateCollatorExpression(@Nullable SqlCollation collation) {
if (collation == null || collation.getCollator() == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

import com.google.common.collect.ImmutableList;

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

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -79,7 +81,7 @@ public EnumerableAggregate(RelOptCluster cluster, RelTraitSet traitSet,

@Override public EnumerableAggregate copy(RelTraitSet traitSet, RelNode input,
ImmutableBitSet groupSet,
List<ImmutableBitSet> groupSets, List<AggregateCall> aggCalls) {
@Nullable List<ImmutableBitSet> groupSets, List<AggregateCall> aggCalls) {
try {
return new EnumerableAggregate(getCluster(), traitSet, input,
groupSet, groupSets, aggCalls);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@

import com.google.common.collect.ImmutableList;

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

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -263,7 +265,7 @@ public List<RexNode> rexArguments() {
return args;
}

public RexNode rexFilterArgument() {
public @Nullable RexNode rexFilterArgument() {
return agg.call.filterArg < 0
? null
: RexInputRef.of(agg.call.filterArg,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@

import com.google.common.collect.ImmutableList;

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

import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.ArrayList;
Expand Down Expand Up @@ -90,13 +92,13 @@ public static EnumerableBatchNestedLoopJoin create(
joinType);
}

@Override public Pair<RelTraitSet, List<RelTraitSet>> passThroughTraits(
@Override public @Nullable Pair<RelTraitSet, List<RelTraitSet>> passThroughTraits(
final RelTraitSet required) {
return EnumerableTraitsUtils.passThroughTraitsForJoin(
required, joinType, getLeft().getRowType().getFieldCount(), traitSet);
}

@Override public Pair<RelTraitSet, List<RelTraitSet>> deriveTraits(
@Override public @Nullable Pair<RelTraitSet, List<RelTraitSet>> deriveTraits(
final RelTraitSet childTraits, final int childId) {
return EnumerableTraitsUtils.deriveTraitsForJoin(
childTraits, childId, joinType, traitSet, right.getTraitSet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@

import com.google.common.collect.ImmutableList;

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

import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.Collections;
Expand Down Expand Up @@ -266,7 +268,7 @@ public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
return implementor.result(physType, builder.toBlock());
}

@Override public Pair<RelTraitSet, List<RelTraitSet>> passThroughTraits(
@Override public @Nullable Pair<RelTraitSet, List<RelTraitSet>> passThroughTraits(
final RelTraitSet required) {
final List<RexNode> exps = Util.transform(program.getProjectList(),
program::expandLocalRef);
Expand All @@ -275,7 +277,7 @@ public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
input.getRowType(), input.getCluster().getTypeFactory(), traitSet);
}

@Override public Pair<RelTraitSet, List<RelTraitSet>> deriveTraits(
@Override public @Nullable Pair<RelTraitSet, List<RelTraitSet>> deriveTraits(
final RelTraitSet childTraits, final int childId) {
final List<RexNode> exps = Util.transform(program.getProjectList(),
program::expandLocalRef);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@

import com.google.common.collect.ImmutableList;

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

import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.List;
Expand Down Expand Up @@ -84,7 +86,7 @@ public static EnumerableCorrelate create(
traitSet, left, right, correlationId, requiredColumns, joinType);
}

@Override public Pair<RelTraitSet, List<RelTraitSet>> passThroughTraits(
@Override public @Nullable Pair<RelTraitSet, List<RelTraitSet>> passThroughTraits(
final RelTraitSet required) {
// EnumerableCorrelate traits passdown shall only pass through collation to left input.
// This is because for EnumerableCorrelate always uses left input as the outer loop,
Expand All @@ -93,7 +95,7 @@ public static EnumerableCorrelate create(
required, joinType, left.getRowType().getFieldCount(), getTraitSet());
}

@Override public Pair<RelTraitSet, List<RelTraitSet>> deriveTraits(
@Override public @Nullable Pair<RelTraitSet, List<RelTraitSet>> deriveTraits(
final RelTraitSet childTraits, final int childId) {
// should only derive traits (limited to collation for now) from left input.
return EnumerableTraitsUtils.deriveTraitsForJoin(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

import com.google.common.collect.ImmutableList;

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

import java.util.List;

/** Implementation of {@link org.apache.calcite.rel.core.Filter} in
Expand Down Expand Up @@ -76,7 +78,7 @@ public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
throw new UnsupportedOperationException();
}

@Override public Pair<RelTraitSet, List<RelTraitSet>> passThroughTraits(
@Override public @Nullable Pair<RelTraitSet, List<RelTraitSet>> passThroughTraits(
RelTraitSet required) {
RelCollation collation = required.getCollation();
if (collation == null || collation == RelCollations.EMPTY) {
Expand All @@ -86,7 +88,7 @@ public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
return Pair.of(traits, ImmutableList.of(traits));
}

@Override public Pair<RelTraitSet, List<RelTraitSet>> deriveTraits(
@Override public @Nullable Pair<RelTraitSet, List<RelTraitSet>> deriveTraits(
final RelTraitSet childTraits, final int childId) {
RelCollation collation = childTraits.getCollation();
if (collation == null || collation == RelCollations.EMPTY) {
Expand Down
Loading

0 comments on commit 4f558d7

Please sign in to comment.