Skip to content

Commit

Permalink
WIP: linq4j nullability
Browse files Browse the repository at this point in the history
  • Loading branch information
vlsi committed May 28, 2020
1 parent 4ea00da commit a6678da
Show file tree
Hide file tree
Showing 54 changed files with 277 additions and 170 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 com.google.common.base.Preconditions;

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

/**
* Represents an integer in modular arithmetic.
* Its {@code value} is between 0 and {@code m - 1} for some modulus {@code m}.
Expand All @@ -35,7 +37,7 @@ class ModularInteger {
this.modulus = modulus;
}

@Override public boolean equals(Object obj) {
@Override public boolean equals(@Nullable Object obj) {
return obj == this
|| obj instanceof ModularInteger
&& value == ((ModularInteger) obj).value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.calcite.linq4j.tree;

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

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

Expand Down Expand Up @@ -70,12 +72,12 @@ public Node accept(Shuttle shuttle) {
"visit not supported: " + getClass() + ":" + nodeType);
}

public Object evaluate(Evaluator evaluator) {
public @Nullable Object evaluate(Evaluator evaluator) {
throw new RuntimeException(
"evaluation not supported: " + getClass() + ":" + nodeType);
}

@Override public boolean equals(Object o) {
@Override public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.calcite.linq4j.tree;

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

import java.lang.reflect.Array;
import java.lang.reflect.Type;
import java.util.Objects;
Expand Down Expand Up @@ -58,7 +60,7 @@ public Type getDeclaringClass() {
return clazz;
}

@Override public boolean equals(Object o) {
@Override public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.calcite.linq4j.tree;

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

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

Expand Down Expand Up @@ -44,7 +46,7 @@ public class BinaryExpression extends Expression {
return visitor.visit(this, expression0, expression1);
}

public <R> R accept(Visitor<R> visitor) {
public <@Nullable R> R accept(Visitor<R> visitor) {
return visitor.visit(this);
}

Expand Down Expand Up @@ -88,8 +90,7 @@ public Object evaluate(Evaluator evaluator) {
throw cannotEvaluate();
}
case Equal:
return expression0.evaluate(evaluator)
.equals(expression1.evaluate(evaluator));
return Objects.equals(expression0.evaluate(evaluator), expression1.evaluate(evaluator));
case GreaterThan:
switch (primitive) {
case INT:
Expand Down Expand Up @@ -241,7 +242,7 @@ private double evaluateDouble(Expression expression, Evaluator evaluator) {
return ((Number) expression.evaluate(evaluator)).doubleValue();
}

@Override public boolean equals(Object o) {
@Override public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.calcite.linq4j.tree;

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

import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.ArrayList;
Expand All @@ -41,7 +43,7 @@ public class BlockBuilder {
new HashMap<>();

private final boolean optimizing;
private final BlockBuilder parent;
private final @Nullable BlockBuilder parent;

private static final Shuttle OPTIMIZE_SHUTTLE = new OptimizeShuttle();

Expand All @@ -66,7 +68,7 @@ public BlockBuilder(boolean optimizing) {
*
* @param optimizing Whether to eliminate common sub-expressions
*/
public BlockBuilder(boolean optimizing, BlockBuilder parent) {
public BlockBuilder(boolean optimizing, @Nullable BlockBuilder parent) {
this.optimizing = optimizing;
this.parent = parent;
}
Expand All @@ -85,7 +87,7 @@ public void clear() {
* (possibly a variable) that represents the result of the newly added
* block.
*/
public Expression append(String name, BlockStatement block) {
public @Nullable Expression append(String name, BlockStatement block) {
return append(name, block, true);
}

Expand All @@ -99,7 +101,7 @@ public Expression append(String name, BlockStatement block) {
* a variable. Do not do this if the expression has
* side-effects or a time-dependent value.
*/
public Expression append(String name, BlockStatement block,
public @Nullable Expression append(String name, BlockStatement block,
boolean optimize) {
if (statements.size() > 0) {
Statement lastStatement = statements.get(statements.size() - 1);
Expand Down Expand Up @@ -184,7 +186,7 @@ public Expression append(String name, Expression expression) {
/**
* Appends an expression to a list of statements, if it is not null.
*/
public Expression appendIfNotNull(String name, Expression expression) {
public @Nullable Expression appendIfNotNull(String name, @Nullable Expression expression) {
if (expression == null) {
return null;
}
Expand Down Expand Up @@ -283,7 +285,7 @@ private Expression normalizeDeclaration(DeclarationStatement decl) {
* @param expr expression to test
* @return existing ParameterExpression or null
*/
public DeclarationStatement getComputedExpression(Expression expr) {
public @Nullable DeclarationStatement getComputedExpression(Expression expr) {
if (parent != null) {
DeclarationStatement decl = parent.getComputedExpression(expr);
if (decl != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.calcite.linq4j.tree;

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

import java.lang.reflect.Type;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -61,7 +63,7 @@ private boolean distinctVariables(boolean fail) {
return shuttle.visit(this, newStatements);
}

public <R> R accept(Visitor<R> visitor) {
public <@Nullable R> R accept(Visitor<R> visitor) {
return visitor.visit(this);
}

Expand All @@ -77,15 +79,15 @@ public <R> R accept(Visitor<R> visitor) {
writer.end("}\n");
}

@Override public Object evaluate(Evaluator evaluator) {
@Override public @Nullable Object evaluate(Evaluator evaluator) {
Object o = null;
for (Statement statement : statements) {
o = statement.evaluate(evaluator);
}
return o;
}

@Override public boolean equals(Object o) {
@Override public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.calcite.linq4j.tree;

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

import java.util.Objects;

/**
Expand All @@ -31,7 +33,7 @@ public CatchBlock(ParameterExpression parameter,
this.body = body;
}

@Override public boolean equals(Object o) {
@Override public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.calcite.linq4j.tree;

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 @@ -66,11 +68,11 @@ public ClassDeclaration accept(Shuttle shuttle) {
return shuttle.visit(this, members1);
}

public <R> R accept(Visitor<R> visitor) {
public <@Nullable R> R accept(Visitor<R> visitor) {
return visitor.visit(this);
}

@Override public boolean equals(Object o) {
@Override public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import org.apache.calcite.linq4j.function.Function1;

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

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
Expand All @@ -30,7 +32,7 @@
* created for optimizing a new expression tree.
*/
public class ClassDeclarationFinder extends Shuttle {
protected final ClassDeclarationFinder parent;
protected final @Nullable ClassDeclarationFinder parent;

/**
* The list of new final static fields to be added to the current class.
Expand Down Expand Up @@ -254,7 +256,7 @@ protected boolean isConstant(Iterable<? extends Expression> list) {
* @param expression input expression
* @return always returns null
*/
protected ParameterExpression findDeclaredExpression(Expression expression) {
protected @Nullable ParameterExpression findDeclaredExpression(Expression expression) {
return null;
}

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

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

import java.lang.reflect.Type;
import java.util.List;
import java.util.Objects;
Expand All @@ -41,7 +43,7 @@ public ConditionalExpression(List<Node> expressionList, Type type) {
this.expressionList = expressionList;
}

public <R> R accept(Visitor<R> visitor) {
public <@Nullable R> R accept(Visitor<R> visitor) {
return visitor.visit(this);
}

Expand All @@ -59,7 +61,7 @@ public <R> R accept(Visitor<R> visitor) {
}
}

@Override public boolean equals(Object o) {
@Override public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.calcite.linq4j.tree;

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

import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -46,7 +48,7 @@ public ConditionalStatement(List<Node> expressionList) {
return shuttle.visit(this, list);
}

public <R> R accept(Visitor<R> visitor) {
public <@Nullable R> R accept(Visitor<R> visitor) {
return visitor.visit(this);
}

Expand All @@ -72,7 +74,7 @@ private static <E> E last(List<E> collection) {
return collection.get(collection.size() - 1);
}

@Override public boolean equals(Object o) {
@Override public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.calcite.linq4j.tree;

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

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
Expand All @@ -32,9 +34,9 @@
* Represents an expression that has a constant value.
*/
public class ConstantExpression extends Expression {
public final Object value;
public final @Nullable Object value;

public ConstantExpression(Type type, Object value) {
public ConstantExpression(Type type, @Nullable Object value) {
super(ExpressionType.Constant, type);
this.value = value;
if (value != null) {
Expand All @@ -54,15 +56,15 @@ public ConstantExpression(Type type, Object value) {
}
}

public Object evaluate(Evaluator evaluator) {
public @Nullable Object evaluate(Evaluator evaluator) {
return value;
}

@Override public Expression accept(Shuttle shuttle) {
return shuttle.visit(this);
}

public <R> R accept(Visitor<R> visitor) {
public <@Nullable R> R accept(Visitor<R> visitor) {
return visitor.visit(this);
}

Expand All @@ -77,7 +79,7 @@ public <R> R accept(Visitor<R> visitor) {
}

private static ExpressionWriter write(ExpressionWriter writer,
final Object value, Type type) {
final Object value, @Nullable Type type) {
if (value == null) {
return writer.append("null");
}
Expand Down Expand Up @@ -328,7 +330,7 @@ private static void escapeString(StringBuilder buf, String s) {
buf.append('"');
}

@Override public boolean equals(Object o) {
@Override public boolean equals(@Nullable Object o) {
// REVIEW: Should constants with the same value and different type
// (e.g. 3L and 3) be considered equal.
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.calcite.linq4j.tree;

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

/**
* Represents a constant null of unknown type
* Java allows type inference for such nulls, thus "null" cannot always be
Expand All @@ -36,7 +38,7 @@ private ConstantUntypedNull() {
writer.append("null");
}

@Override public boolean equals(Object o) {
@Override public boolean equals(@Nullable Object o) {
return o == INSTANCE;
}

Expand Down
Loading

0 comments on commit a6678da

Please sign in to comment.