Skip to content

Commit

Permalink
Add nullability annotations to linq4j
Browse files Browse the repository at this point in the history
  • Loading branch information
vlsi committed Aug 31, 2020
1 parent 8b3f6bd commit 8391c91
Show file tree
Hide file tree
Showing 79 changed files with 978 additions and 496 deletions.
1 change: 1 addition & 0 deletions linq4j/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ dependencies {

implementation("com.google.guava:guava")
implementation("org.apache.calcite.avatica:avatica-core")
implementation("org.checkerframework:checker-qual")
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import org.apache.calcite.linq4j.tree.Expression;

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

import java.lang.reflect.Type;
import java.util.Iterator;

Expand All @@ -34,10 +36,10 @@ public abstract class BaseQueryable<TSource>
extends AbstractQueryable<TSource> {
protected final QueryProvider provider;
protected final Type elementType;
protected final Expression expression;
protected final @Nullable Expression expression;

public BaseQueryable(QueryProvider provider, Type elementType,
Expression expression) {
@Nullable Expression expression) {
this.provider = provider;
this.elementType = elementType;
this.expression = expression;
Expand All @@ -51,7 +53,7 @@ public Type getElementType() {
return elementType;
}

public Expression getExpression() {
public @Nullable Expression getExpression() {
return expression;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
import org.apache.calcite.linq4j.function.Predicate1;
import org.apache.calcite.linq4j.function.Predicate2;

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

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Comparator;
Expand Down Expand Up @@ -69,7 +72,7 @@ protected OrderedEnumerable<T> getThisOrdered() {
return this;
}

public <R> R foreach(Function1<T, R> func) {
public <R> @Nullable R foreach(Function1<T, R> func) {
R result = null;
try (Enumerator<T> enumerator = enumerator()) {
while (enumerator.moveNext()) {
Expand All @@ -89,12 +92,12 @@ protected OrderedQueryable<T> asOrderedQueryable() {
return EnumerableDefaults.asOrderedQueryable(this);
}

public T aggregate(Function2<T, T, T> func) {
public @Nullable T aggregate(Function2<@Nullable T, T, T> func) {
return EnumerableDefaults.aggregate(getThis(), func);
}

public <TAccumulate> TAccumulate aggregate(TAccumulate seed,
Function2<TAccumulate, T, TAccumulate> func) {
public <TAccumulate> @PolyNull TAccumulate aggregate(@PolyNull TAccumulate seed,
Function2<@PolyNull TAccumulate, T, @PolyNull TAccumulate> func) {
return EnumerableDefaults.aggregate(getThis(), seed, func);
}

Expand Down Expand Up @@ -191,11 +194,11 @@ public <TKey> OrderedEnumerable<T> createOrderedEnumerable(
keySelector, comparator, descending);
}

public Enumerable<T> defaultIfEmpty() {
public Enumerable<@Nullable T> defaultIfEmpty() {
return EnumerableDefaults.defaultIfEmpty(getThis());
}

public Enumerable<T> defaultIfEmpty(T value) {
public Enumerable<@PolyNull T> defaultIfEmpty(@PolyNull T value) {
return EnumerableDefaults.defaultIfEmpty(getThis(), value);
}

Expand All @@ -211,7 +214,7 @@ public T elementAt(int index) {
return EnumerableDefaults.elementAt(getThis(), index);
}

public T elementAtOrDefault(int index) {
public @Nullable T elementAtOrDefault(int index) {
return EnumerableDefaults.elementAtOrDefault(getThis(), index);
}

Expand Down Expand Up @@ -241,11 +244,11 @@ public T first(Predicate1<T> predicate) {
return EnumerableDefaults.first(getThis(), predicate);
}

public T firstOrDefault() {
public @Nullable T firstOrDefault() {
return EnumerableDefaults.firstOrDefault(getThis());
}

public T firstOrDefault(Predicate1<T> predicate) {
public @Nullable T firstOrDefault(Predicate1<T> predicate) {
return EnumerableDefaults.firstOrDefault(getThis(), predicate);
}

Expand Down Expand Up @@ -407,7 +410,7 @@ public <TInner, TKey, TResult> Enumerable<TResult> hashJoin(
Function2<T, TInner, TResult> resultSelector,
EqualityComparer<TKey> comparer,
boolean generateNullsOnLeft, boolean generateNullsOnRight,
Predicate2<T, TInner> predicate) {
@Nullable Predicate2<T, TInner> predicate) {
return EnumerableDefaults.hashJoin(getThis(), inner, outerKeySelector,
innerKeySelector, resultSelector, comparer, generateNullsOnLeft,
generateNullsOnRight, predicate);
Expand All @@ -428,11 +431,11 @@ public T last(Predicate1<T> predicate) {
return EnumerableDefaults.last(getThis(), predicate);
}

public T lastOrDefault() {
public @Nullable T lastOrDefault() {
return EnumerableDefaults.lastOrDefault(getThis());
}

public T lastOrDefault(Predicate1<T> predicate) {
public @Nullable T lastOrDefault(Predicate1<T> predicate) {
return EnumerableDefaults.lastOrDefault(getThis(), predicate);
}

Expand Down Expand Up @@ -621,11 +624,11 @@ public T single(Predicate1<T> predicate) {
return EnumerableDefaults.single(getThis(), predicate);
}

public T singleOrDefault() {
public @Nullable T singleOrDefault() {
return EnumerableDefaults.singleOrDefault(getThis());
}

public T singleOrDefault(Predicate1<T> predicate) {
public @Nullable T singleOrDefault(Predicate1<T> predicate) {
return EnumerableDefaults.singleOrDefault(getThis(), predicate);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.apache.calcite.linq4j.function.Predicate2;
import org.apache.calcite.linq4j.tree.FunctionExpression;

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

import java.math.BigDecimal;
import java.util.Comparator;

Expand Down Expand Up @@ -148,7 +150,7 @@ public Enumerator<T> enumerator() {
return factory.ofType(getThis(), clazz);
}

@Override public Queryable<T> defaultIfEmpty() {
@Override public Queryable<@Nullable T> defaultIfEmpty() {
return factory.defaultIfEmpty(getThis());
}

Expand All @@ -162,7 +164,7 @@ public Enumerator<T> enumerator() {

// End disambiguate

public T aggregate(FunctionExpression<Function2<T, T, T>> selector) {
public @Nullable T aggregate(FunctionExpression<Function2<@Nullable T, T, T>> selector) {
return factory.aggregate(getThis(), selector);
}

Expand Down Expand Up @@ -243,7 +245,7 @@ public T first(FunctionExpression<Predicate1<T>> predicate) {
return factory.first(getThis(), predicate);
}

public T firstOrDefault(FunctionExpression<Predicate1<T>> predicate) {
public @Nullable T firstOrDefault(FunctionExpression<Predicate1<T>> predicate) {
return factory.firstOrDefault(getThis(), predicate);
}

Expand Down Expand Up @@ -421,7 +423,7 @@ public T single(FunctionExpression<Predicate1<T>> predicate) {
return factory.single(getThis(), predicate);
}

public T singleOrDefault(FunctionExpression<Predicate1<T>> predicate) {
public @Nullable T singleOrDefault(FunctionExpression<Predicate1<T>> predicate) {
return factory.singleOrDefault(getThis(), predicate);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.calcite.linq4j;

import org.checkerframework.framework.qual.Covariant;

/**
* Exposes the enumerator, which supports a simple iteration over a collection.
*
Expand All @@ -26,6 +28,7 @@
*
* @param <T> Element type
*/
@Covariant(0)
public interface Enumerable<T>
extends RawEnumerable<T>, Iterable<T>, ExtendedEnumerable<T> {
/**
Expand Down
Loading

0 comments on commit 8391c91

Please sign in to comment.