Skip to content

Commit

Permalink
WIP: nullness
Browse files Browse the repository at this point in the history
  • Loading branch information
vlsi committed Oct 23, 2020
1 parent b43ceb4 commit 17efa52
Show file tree
Hide file tree
Showing 25 changed files with 130 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,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 @@ -239,8 +240,9 @@ private void load(final RelDataType elementType,
switch (rep) {
case OBJECT:
case JAVA_SQL_TIMESTAMP:
return Util.transform((List<@Nullable Timestamp>) list,
final List<@Nullable Long> longs = Util.transform((List<@Nullable Timestamp>) list,
(Timestamp t) -> t == null ? null : t.getTime());
return longs;
default:
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ assert canHandle(table)
/** Creates an EnumerableTableScan. */
public static EnumerableTableScan create(RelOptCluster cluster,
RelOptTable relOptTable) {
final Table table = relOptTable.unwrapOrThrow(Table.class);
final Table table = relOptTable.unwrap(Table.class);
Class elementType = EnumerableTableScan.deduceElementType(table);
final RelTraitSet traitSet =
cluster.traitSetOf(EnumerableConvention.INSTANCE)
Expand Down Expand Up @@ -171,7 +171,7 @@ public static boolean canHandle(RelOptTable relOptTable) {
return true;
}

public static Class deduceElementType(Table table) {
public static Class deduceElementType(@Nullable Table table) {
if (table instanceof QueryableTable) {
final QueryableTable queryableTable = (QueryableTable) table;
final Type type = queryableTable.getElementType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@

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

import static java.util.Objects.requireNonNull;

/**
* Subset of an equivalence class where all relational expressions have the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Objects;
import java.util.PriorityQueue;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

import com.google.common.collect.ImmutableMap;

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

/** Registry of {@link Enum} classes that can be serialized to JSON.
*
* <p>Suppose you want to serialize the value
Expand Down Expand Up @@ -76,7 +78,7 @@ private RelEnumTypes() {}

private static void register(ImmutableMap.Builder<String, Enum<?>> builder,
Class<? extends Enum> aClass) {
for (Enum enumConstant : aClass.getEnumConstants()) {
for (Enum enumConstant : castNonNull(aClass.getEnumConstants())) {
builder.put(enumConstant.name(), enumConstant);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public RelNode read(String s) throws IOException {
.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true)
.readValue(s, TYPE_REF);
@SuppressWarnings("unchecked")
final List<Map<String, Object>> rels = (List) o.get("rels");
final List<Map<String, Object>> rels = (List) requireNonNull(o.get("rels"), "rels");
readRels(rels);
return requireNonNull(lastRel, "lastRel");
}
Expand All @@ -97,7 +97,7 @@ private void readRels(List<Map<String, Object>> jsonRels) {
}

private void readRel(final Map<String, Object> jsonRel) {
String id = (String) jsonRel.get("id");
String id = (String) requireNonNull(jsonRel.get("id"), "jsonRel.id");
String type = (String) jsonRel.get("relOp");
Constructor constructor = relJson.getConstructor(type);
RelInput input = new RelInput() {
Expand Down Expand Up @@ -174,7 +174,7 @@ private void readRel(final Map<String, Object> jsonRel) {

@Override public List<AggregateCall> getAggregateCalls(String tag) {
@SuppressWarnings("unchecked")
final List<Map<String, Object>> jsonAggs = (List) jsonRel.get(tag);
final List<Map<String, Object>> jsonAggs = (List) getNonNull(tag);
final List<AggregateCall> inputs = new ArrayList<>();
for (Map<String, Object> jsonAggCall : jsonAggs) {
inputs.add(toAggCall(jsonAggCall));
Expand Down Expand Up @@ -298,10 +298,13 @@ private AggregateCall toAggCall(Map<String, Object> jsonAggCall) {
() -> "relJson.toAggregation output for " + aggMap);
final Boolean distinct = (Boolean) jsonAggCall.get("distinct");
@SuppressWarnings("unchecked")
final List<Integer> operands = (List<Integer>) jsonAggCall.get("operands");
final List<Integer> operands = (List<Integer>) requireNonNull(
jsonAggCall.get("operands"),
"jsonAggCall.operands");
final Integer filterOperand = (Integer) jsonAggCall.get("filter");
final Object jsonAggType = requireNonNull(jsonAggCall.get("type"), "jsonAggCall.type");
final RelDataType type =
relJson.toType(cluster.getTypeFactory(), jsonAggCall.get("type"));
relJson.toType(cluster.getTypeFactory(), jsonAggType);
final String name = (String) jsonAggCall.get("name");
return AggregateCall.create(aggregation, distinct, false, false, operands,
filterOperand == null ? -1 : filterOperand,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* Callback for a relational expression to dump itself as JSON.
Expand All @@ -42,7 +43,7 @@ public class RelJsonWriter implements RelWriter {
protected final JsonBuilder jsonBuilder;
protected final RelJson relJson;
private final Map<RelNode, String> relIdMap = new IdentityHashMap<>();
protected final List<Object> relList;
protected final List<@Nullable Object> relList;
private final List<Pair<String, @Nullable Object>> values = new ArrayList<>();
private @Nullable String previousId;

Expand All @@ -68,8 +69,8 @@ protected void explain_(RelNode rel, List<Pair<String, @Nullable Object>> values
put(map, value.left, value.right);
}
// omit 'inputs: ["3"]' if "3" is the preceding rel
final List<Object> list = explainInputs(rel.getInputs());
if (list.size() != 1 || !list.get(0).equals(previousId)) {
final List<@Nullable Object> list = explainInputs(rel.getInputs());
if (list.size() != 1 || !Objects.equals(list.get(0), previousId)) {
map.put("inputs", list);
}

Expand All @@ -81,12 +82,12 @@ protected void explain_(RelNode rel, List<Pair<String, @Nullable Object>> values
previousId = id;
}

private void put(Map<String, Object> map, String name, Object value) {
private void put(Map<String, @Nullable Object> map, String name, @Nullable Object value) {
map.put(name, relJson.toJson(value));
}

private List<Object> explainInputs(List<RelNode> inputs) {
final List<Object> list = jsonBuilder.list();
private List<@Nullable Object> explainInputs(List<RelNode> inputs) {
final List<@Nullable Object> list = jsonBuilder.list();
for (RelNode input : inputs) {
String id = relIdMap.get(input);
if (id == null) {
Expand Down Expand Up @@ -128,7 +129,7 @@ private List<Object> explainInputs(List<RelNode> inputs) {
* explained.
*/
public String asString() {
final Map<String, Object> map = jsonBuilder.map();
final Map<String, @Nullable Object> map = jsonBuilder.map();
map.put("rels", relList);
return jsonBuilder.toJsonString(map);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ private RelBuilder convertMonopole(RelBuilder relBuilder, Aggregate aggregate,
* be modified @return Relational expression
*/
private void doRewrite(RelBuilder relBuilder, Aggregate aggregate, int n,
List<Integer> argList, int filterArg, List<RexInputRef> refs) {
List<Integer> argList, int filterArg, List<@Nullable RexInputRef> refs) {
final RexBuilder rexBuilder = aggregate.getCluster().getRexBuilder();
final List<RelDataTypeField> leftFields;
if (n == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@

import com.google.common.collect.ImmutableList;

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

import java.util.ArrayList;
import java.util.BitSet;
import java.util.HashMap;
Expand Down Expand Up @@ -449,7 +451,7 @@ private static <E> SqlSplittableAggFunction.Registry<E> registry(
/** Work space for an input to a join. */
private static class Side {
final Map<Integer, Integer> split = new HashMap<>();
RelNode newInput;
@Nullable RelNode newInput;
boolean aggregate;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,8 @@ private static class HighestUsageFinder extends RexVisitorImpl<Void> {
continue;
}
currentLevel = exprLevels[i];
exprs[i].accept(this);
@SuppressWarnings("argument.type.incompatible")
final Void unused = exprs[i].accept(this);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import java.util.List;
import java.util.Map;

import static java.util.Objects.requireNonNull;

/**
* Planner rule to flatten a tree of
* {@link org.apache.calcite.rel.logical.LogicalJoin}s
Expand Down Expand Up @@ -342,7 +344,7 @@ private void combineOuterJoins(
*/
private void copyOuterJoinInfo(
MultiJoin multiJoin,
List<Pair<JoinRelType, RexNode>> destJoinSpecs,
List<Pair<JoinRelType, @Nullable RexNode>> destJoinSpecs,
int adjustmentAmount,
@Nullable List<RelDataTypeField> srcFields,
@Nullable List<RelDataTypeField> destFields) {
Expand Down Expand Up @@ -513,7 +515,9 @@ private ImmutableMap<Integer, ImmutableIntList> addOnJoinFieldRefCounts(
nFields =
multiJoinInputs.get(currInput).getRowType().getFieldCount();
}
int[] refCounts = refCountsMap.get(currInput);
final int key = currInput;
int[] refCounts = requireNonNull(refCountsMap.get(key),
() -> "refCountsMap.get(currInput) for " + key);
refCounts[i - startField] += joinCondRefCounts[i];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.core.Join;

import org.checkerframework.checker.initialization.qual.NotOnlyInitialized;
import org.checkerframework.checker.initialization.qual.UnderInitialization;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
Expand All @@ -36,6 +39,7 @@
public class LoptJoinTree {
//~ Instance fields --------------------------------------------------------

@NotOnlyInitialized
private final BinaryTree factorTree;
private final RelNode joinTree;
private final boolean removableSelfJoin;
Expand Down Expand Up @@ -154,9 +158,10 @@ public boolean isRemovableSelfJoin() {
* track of the parent LoptJoinTree object associated with the binary tree.
*/
protected abstract static class BinaryTree {
@NotOnlyInitialized
private final LoptJoinTree parent;

protected BinaryTree(LoptJoinTree parent) {
protected BinaryTree(@UnderInitialization LoptJoinTree parent) {
this.parent = Objects.requireNonNull(parent);
}

Expand Down Expand Up @@ -191,7 +196,7 @@ protected static class Node extends BinaryTree {
private final BinaryTree left;
private final BinaryTree right;

public Node(BinaryTree left, BinaryTree right, LoptJoinTree parent) {
public Node(BinaryTree left, BinaryTree right, @UnderInitialization LoptJoinTree parent) {
super(parent);
this.left = Objects.requireNonNull(left);
this.right = Objects.requireNonNull(right);
Expand Down
Loading

0 comments on commit 17efa52

Please sign in to comment.