Skip to content

Commit

Permalink
[CALCITE-4199] Add nullability annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
vlsi committed Nov 1, 2020
1 parent f1b3b3d commit 4694e4e
Show file tree
Hide file tree
Showing 920 changed files with 11,276 additions and 7,023 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
46 changes: 24 additions & 22 deletions core/src/main/java/org/apache/calcite/adapter/clone/ArrayTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList;

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

import java.lang.reflect.Array;
import java.lang.reflect.Type;
import java.util.AbstractList;
Expand Down Expand Up @@ -82,9 +84,9 @@ class ArrayTable extends AbstractQueryableTable implements ScannableTable {
return Statistics.of(content.size, keys, content.collations);
}

@Override public Enumerable<Object[]> scan(DataContext root) {
return new AbstractEnumerable<Object[]>() {
@Override public Enumerator<Object[]> enumerator() {
@Override public Enumerable<@Nullable Object[]> scan(DataContext root) {
return new AbstractEnumerable<@Nullable Object[]>() {
@Override public Enumerator<@Nullable Object[]> enumerator() {
final Content content = supplier.get();
return content.arrayEnumerator();
}
Expand Down Expand Up @@ -225,7 +227,7 @@ public static List asList(final Representation representation,
// Cache size. It might be expensive to compute.
final int size = representation.size(dataSet);
return new AbstractList() {
@Override public Object get(int index) {
@Override public @Nullable Object get(int index) {
return representation.getObject(dataSet, index);
}

Expand All @@ -243,9 +245,9 @@ public interface Representation {

/** Converts a value set into a compact representation. If
* {@code sources} is not null, permutes. */
Object freeze(ColumnLoader.ValueSet valueSet, int[] sources);
Object freeze(ColumnLoader.ValueSet valueSet, int @Nullable [] sources);

Object getObject(Object dataSet, int ordinal);
@Nullable Object getObject(Object dataSet, int ordinal);
int getInt(Object dataSet, int ordinal);

/** Creates a data set that is the same as a given data set
Expand Down Expand Up @@ -277,7 +279,7 @@ public static class ObjectArray implements Representation {
return RepresentationType.OBJECT_ARRAY;
}

@Override public Object freeze(ColumnLoader.ValueSet valueSet, int[] sources) {
@Override public Object freeze(ColumnLoader.ValueSet valueSet, int @Nullable [] sources) {
// We assume the values have been canonized.
final List<Comparable> list = permuteList(valueSet.values, sources);
return list.toArray(new Comparable[0]);
Expand Down Expand Up @@ -334,7 +336,7 @@ public static class PrimitiveArray implements Representation {
return RepresentationType.PRIMITIVE_ARRAY;
}

@Override public Object freeze(ColumnLoader.ValueSet valueSet, int[] sources) {
@Override public Object freeze(ColumnLoader.ValueSet valueSet, int @Nullable [] sources) {
//noinspection unchecked
return primitive.toArray2(
permuteList((List) valueSet.values, sources));
Expand All @@ -344,7 +346,7 @@ public static class PrimitiveArray implements Representation {
return primitive.permute(dataSet, sources);
}

@Override public Object getObject(Object dataSet, int ordinal) {
@Override public @Nullable Object getObject(Object dataSet, int ordinal) {
return p.arrayItem(dataSet, ordinal);
}

Expand Down Expand Up @@ -375,7 +377,7 @@ public static class PrimitiveDictionary implements Representation {
return RepresentationType.PRIMITIVE_DICTIONARY;
}

@Override public Object freeze(ColumnLoader.ValueSet valueSet, int[] sources) {
@Override public Object freeze(ColumnLoader.ValueSet valueSet, int @Nullable [] sources) {
throw new UnsupportedOperationException(); // TODO:
}

Expand Down Expand Up @@ -423,7 +425,7 @@ public static class ObjectDictionary implements Representation {
return RepresentationType.OBJECT_DICTIONARY;
}

@Override public Object freeze(ColumnLoader.ValueSet valueSet, int[] sources) {
@Override public Object freeze(ColumnLoader.ValueSet valueSet, int @Nullable [] sources) {
final int n = valueSet.map.keySet().size();
int extra = valueSet.containsNull ? 1 : 0;
Comparable[] codeValues =
Expand Down Expand Up @@ -486,7 +488,7 @@ public static class StringDictionary implements Representation {
return RepresentationType.STRING_DICTIONARY;
}

@Override public Object freeze(ColumnLoader.ValueSet valueSet, int[] sources) {
@Override public Object freeze(ColumnLoader.ValueSet valueSet, int @Nullable [] sources) {
throw new UnsupportedOperationException(); // TODO:
}

Expand Down Expand Up @@ -524,7 +526,7 @@ public static class ByteStringDictionary implements Representation {
return RepresentationType.BYTE_STRING_DICTIONARY;
}

@Override public Object freeze(ColumnLoader.ValueSet valueSet, int[] sources) {
@Override public Object freeze(ColumnLoader.ValueSet valueSet, int @Nullable [] sources) {
throw new UnsupportedOperationException(); // TODO:
}

Expand Down Expand Up @@ -565,7 +567,7 @@ public static class Constant implements Representation {
return RepresentationType.CONSTANT;
}

@Override public Object freeze(ColumnLoader.ValueSet valueSet, int[] sources) {
@Override public Object freeze(ColumnLoader.ValueSet valueSet, int @Nullable [] sources) {
final int size = valueSet.values.size();
return Pair.of(size == 0 ? null : valueSet.values.get(0), size);
}
Expand Down Expand Up @@ -626,7 +628,7 @@ public static class BitSlicedPrimitiveArray implements Representation {
return RepresentationType.BIT_SLICED_PRIMITIVE_ARRAY;
}

@Override public Object freeze(ColumnLoader.ValueSet valueSet, int[] sources) {
@Override public Object freeze(ColumnLoader.ValueSet valueSet, int @Nullable [] sources) {
final int chunksPerWord = 64 / bitCount;
final List<Comparable> valueList =
permuteList(valueSet.values, sources);
Expand Down Expand Up @@ -783,7 +785,7 @@ public static void orLong(
}

private static <E> List<E> permuteList(
final List<E> list, final int[] sources) {
final List<E> list, final int @Nullable [] sources) {
if (sources == null) {
return list;
}
Expand Down Expand Up @@ -828,13 +830,13 @@ public <T> Enumerator<T> enumerator() {
}
}

public Enumerator<Object[]> arrayEnumerator() {
public Enumerator<@Nullable Object[]> arrayEnumerator() {
return new ArrayEnumerator(size, columns);
}

/** Enumerator over a table with a single column; each element
* returned is an object. */
private static class ObjectEnumerator implements Enumerator<Object> {
private static class ObjectEnumerator implements Enumerator<@Nullable Object> {
final int rowCount;
final Object dataSet;
final Representation representation;
Expand All @@ -846,7 +848,7 @@ private static class ObjectEnumerator implements Enumerator<Object> {
this.representation = column.representation;
}

@Override public Object current() {
@Override public @Nullable Object current() {
return representation.getObject(dataSet, i);
}

Expand All @@ -864,7 +866,7 @@ private static class ObjectEnumerator implements Enumerator<Object> {

/** Enumerator over a table with more than one column; each element
* returned is an array. */
private static class ArrayEnumerator implements Enumerator<Object[]> {
private static class ArrayEnumerator implements Enumerator<@Nullable Object[]> {
final int rowCount;
final List<Column> columns;
int i = -1;
Expand All @@ -874,8 +876,8 @@ private static class ArrayEnumerator implements Enumerator<Object[]> {
this.columns = columns;
}

@Override public Object[] current() {
Object[] objects = new Object[columns.size()];
@Override public @Nullable Object[] current() {
@Nullable Object[] objects = new Object[columns.size()];
for (int j = 0; j < objects.length; j++) {
final Column pair = columns.get(j);
objects[j] = pair.representation.getObject(pair.dataSet, i);
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 @@ -62,6 +64,7 @@ class ColumnLoader<T> {
* @param sourceTable Source data
* @param protoRowType Logical row type
* @param repList Physical row types, or null if not known */
@SuppressWarnings("method.invocation.invalid")
ColumnLoader(JavaTypeFactory typeFactory,
Enumerable<T> sourceTable,
RelProtoDataType protoRowType,
Expand Down Expand Up @@ -195,7 +198,7 @@ private void load(final RelDataType elementType,
// 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 @@ -230,15 +233,16 @@ private void load(final RelDataType elementType,
* value needs to be converted to a {@link Long}. Similarly
* {@link java.sql.Date} and {@link java.sql.Time} values to
* {@link Integer}. */
private static List wrap(ColumnMetaData.Rep rep, List list,
private static List<? extends @Nullable Object> wrap(ColumnMetaData.Rep rep, List<?> list,
RelDataType type) {
switch (type.getSqlTypeName()) {
case TIMESTAMP:
switch (rep) {
case OBJECT:
case JAVA_SQL_TIMESTAMP:
return Util.transform(list,
final List<@Nullable Long> longs = Util.transform((List<@Nullable Timestamp>) list,
(Timestamp t) -> t == null ? null : t.getTime());
return longs;
default:
break;
}
Expand All @@ -247,7 +251,7 @@ private static List wrap(ColumnMetaData.Rep rep, List list,
switch (rep) {
case OBJECT:
case JAVA_SQL_TIME:
return Util.transform(list, (Time t) -> t == null
return Util.transform((List<@Nullable Time>) list, (Time t) -> t == null
? null
: (int) (t.getTime() % DateTimeUtils.MILLIS_PER_DAY));
default:
Expand All @@ -258,9 +262,10 @@ private static List wrap(ColumnMetaData.Rep rep, List list,
switch (rep) {
case OBJECT:
case JAVA_SQL_DATE:
return Util.transform(list, (Date d) -> d == null
? null
: (int) (d.getTime() / DateTimeUtils.MILLIS_PER_DAY));
return Util.<@Nullable Date, @Nullable Integer>transform(
(List<@Nullable Date>) list, (Date d) -> d == null
? null
: (int) (d.getTime() / DateTimeUtils.MILLIS_PER_DAY));
default:
break;
}
Expand All @@ -279,15 +284,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 @@ -367,7 +372,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 @@ -19,6 +19,8 @@
import org.apache.calcite.linq4j.tree.Expression;
import org.apache.calcite.rel.core.AggregateCall;

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

/**
* Information for a call to
* {@link AggImplementor#implementResult(AggContext, AggResultContext)}
Expand All @@ -32,7 +34,7 @@ public interface AggResultContext extends NestedBlockBuilder, AggResetContext {
* accumulator were aggregated. Most aggregate functions depend on only the
* accumulator, but quasi-aggregate functions such as GROUPING access at the
* key. */
Expression key();
@Nullable Expression key();

/** Returns an expression that references the {@code i}th field of the key,
* cast to the appropriate type. */
Expand Down
Loading

0 comments on commit 4694e4e

Please sign in to comment.