Skip to content

Commit

Permalink
WIP: core
Browse files Browse the repository at this point in the history
  • Loading branch information
vlsi committed Sep 18, 2020
1 parent ae3f950 commit 913634f
Show file tree
Hide file tree
Showing 209 changed files with 1,474 additions and 868 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@
import org.apache.calcite.rex.RexNode;
import org.apache.calcite.util.BuiltInMethod;

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

import java.util.List;

/** Relational expression that applies a limit and/or offset to its input. */
public class EnumerableLimit extends SingleRel implements EnumerableRel {
public final RexNode offset;
public final RexNode fetch;
public final @Nullable RexNode offset;
public final @Nullable RexNode fetch;

/** Creates an EnumerableLimit.
*
Expand All @@ -49,8 +51,8 @@ public EnumerableLimit(
RelOptCluster cluster,
RelTraitSet traitSet,
RelNode input,
RexNode offset,
RexNode fetch) {
@Nullable RexNode offset,
@Nullable RexNode fetch) {
super(cluster, traitSet, input);
this.offset = offset;
this.fetch = fetch;
Expand All @@ -59,8 +61,8 @@ public EnumerableLimit(
}

/** Creates an EnumerableLimit. */
public static EnumerableLimit create(final RelNode input, RexNode offset,
RexNode fetch) {
public static EnumerableLimit create(final RelNode input, @Nullable RexNode offset,
@Nullable RexNode fetch) {
final RelOptCluster cluster = input.getCluster();
final RelMetadataQuery mq = cluster.getMetadataQuery();
final RelTraitSet traitSet =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class EnumerableTableFunctionScan extends TableFunctionScan

public EnumerableTableFunctionScan(RelOptCluster cluster,
RelTraitSet traits, List<RelNode> inputs, @Nullable Type elementType,
@Nullable RelDataType rowType, RexNode call,
RelDataType rowType, RexNode call,
@Nullable Set<RelColumnMapping> columnMappings) {
super(cluster, traits, inputs, call, elementType, rowType,
columnMappings);
Expand All @@ -61,7 +61,7 @@ public EnumerableTableFunctionScan(RelOptCluster cluster,
List<RelNode> inputs,
RexNode rexCall,
@Nullable Type elementType,
@Nullable RelDataType rowType,
RelDataType rowType,
@Nullable Set<RelColumnMapping> columnMappings) {
return new EnumerableTableFunctionScan(getCluster(), traitSet, inputs,
elementType, rowType, rexCall, columnMappings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.apache.calcite.avatica.AvaticaUtils;
import org.apache.calcite.avatica.MetaImpl;
import org.apache.calcite.avatica.SqlType;
import org.apache.calcite.linq4j.Nullness;
import org.apache.calcite.linq4j.function.Experimental;
import org.apache.calcite.linq4j.tree.Expression;
import org.apache.calcite.rel.type.RelDataType;
Expand Down Expand Up @@ -85,7 +84,7 @@ public class JdbcSchema implements Schema {
private final boolean snapshot;

@Experimental
public static final ThreadLocal<Foo> THREAD_METADATA = new ThreadLocal<>();
public static final ThreadLocal<@Nullable Foo> THREAD_METADATA = new ThreadLocal<>();

private static final Ordering<Iterable<Integer>> VERSION_ORDERING =
Ordering.<Integer>natural().lexicographical();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.apache.calcite.adapter.enumerable.PhysTypeImpl;
import org.apache.calcite.adapter.java.JavaTypeFactory;
import org.apache.calcite.config.CalciteSystemProperty;
import org.apache.calcite.linq4j.Nullness;
import org.apache.calcite.linq4j.tree.BlockBuilder;
import org.apache.calcite.linq4j.tree.ConstantExpression;
import org.apache.calcite.linq4j.tree.Expression;
Expand All @@ -41,7 +40,6 @@
import org.apache.calcite.rel.convert.ConverterImpl;
import org.apache.calcite.rel.metadata.RelMetadataQuery;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rex.RexLiteral;
import org.apache.calcite.runtime.Hook;
import org.apache.calcite.runtime.SqlFunctions;
import org.apache.calcite.schema.Schemas;
Expand Down
12 changes: 6 additions & 6 deletions core/src/main/java/org/apache/calcite/jdbc/CalcitePrepare.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
*/
public interface CalcitePrepare {
Function0<CalcitePrepare> DEFAULT_FACTORY = CalcitePrepareImpl::new;
ThreadLocal<Deque<Context>> THREAD_CONTEXT_STACK =
ThreadLocal<@Nullable Deque<Context>> THREAD_CONTEXT_STACK =
ThreadLocal.withInitial(ArrayDeque::new);

ParseResult parse(Context context, String sql);
Expand Down Expand Up @@ -189,7 +189,7 @@ private static SparkHandler createHandler() {
}

public static void push(Context context) {
final Deque<Context> stack = THREAD_CONTEXT_STACK.get();
final Deque<Context> stack = castNonNull(THREAD_CONTEXT_STACK.get());
final List<String> path = context.getObjectPath();
if (path != null) {
for (Context context1 : stack) {
Expand All @@ -203,11 +203,11 @@ public static void push(Context context) {
}

public static Context peek() {
return castNonNull(THREAD_CONTEXT_STACK.get().peek());
return castNonNull(castNonNull(THREAD_CONTEXT_STACK.get()).peek());
}

public static void pop(Context context) {
Context x = THREAD_CONTEXT_STACK.get().pop();
Context x = castNonNull(THREAD_CONTEXT_STACK.get()).pop();
assert x == context;
}

Expand Down Expand Up @@ -333,7 +333,7 @@ public CalciteSignature(String sql, List<AvaticaParameter> parameterList,
long maxRowCount, Bindable<T> bindable) {
this(sql, parameterList, internalParameters, rowType, columns,
cursorFactory, rootSchema, collationList, maxRowCount, bindable,
null);
castNonNull(null));
}

public CalciteSignature(String sql,
Expand All @@ -346,7 +346,7 @@ public CalciteSignature(String sql,
List<RelCollation> collationList,
long maxRowCount,
@Nullable Bindable<T> bindable,
Meta.@Nullable StatementType statementType) {
Meta.StatementType statementType) {
super(columns, sql, parameterList, internalParameters, cursorFactory,
statementType);
this.rowType = rowType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,8 @@ public NavigableSet<String> getFunctionNames() {
}

public Set<String> getSubSchemaNames() {
return CalciteSchema.this.getSubSchemaMap().keySet();
//noinspection RedundantCast
return (Set<String>) CalciteSchema.this.getSubSchemaMap().keySet();
}

public SchemaPlus add(String name, Schema schema) {
Expand Down
58 changes: 38 additions & 20 deletions core/src/main/java/org/apache/calcite/materialize/Lattice.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@
import com.google.common.collect.Multimap;
import com.google.common.collect.Ordering;

import org.checkerframework.checker.initialization.qual.UnknownInitialization;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.checker.nullness.qual.RequiresNonNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
Expand All @@ -82,9 +87,11 @@
import java.util.function.IntFunction;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;

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

/**
* Structure that allows materialized views based upon a star schema to be
* recognized and recommended.
Expand Down Expand Up @@ -128,16 +135,21 @@ private Lattice(CalciteSchema rootSchema, LatticeRootNode rootNode,
}
Preconditions.checkArgument(rowCountEstimate > 0d);
this.rowCountEstimate = rowCountEstimate;
this.statisticProvider =
@SuppressWarnings("argument.type.incompatible")
LatticeStatisticProvider statisticProvider =
Objects.requireNonNull(statisticProviderFactory.apply(this));
this.statisticProvider = statisticProvider;
}

/** Creates a Lattice. */
public static Lattice create(CalciteSchema schema, String sql, boolean auto) {
return builder(schema, sql).auto(auto).build();
}

private boolean isValid(Litmus litmus) {
@RequiresNonNull({"rootNode", "defaultMeasures", "columns"})
private boolean isValid(
@UnknownInitialization Lattice this,
Litmus litmus) {
if (!rootNode.isValid(litmus)) {
return false;
}
Expand All @@ -152,7 +164,7 @@ private boolean isValid(Litmus litmus) {
return litmus.succeed();
}

private static void populateAliases(SqlNode from, List<String> aliases,
private static void populateAliases(SqlNode from, List<@Nullable String> aliases,
@Nullable String current) {
if (from instanceof SqlJoin) {
SqlJoin join = (SqlJoin) from;
Expand Down Expand Up @@ -375,7 +387,10 @@ public String countSql(ImmutableBitSet groupSet) {
public StarTable createStarTable() {
final List<Table> tables = new ArrayList<>();
for (LatticeNode node : rootNode.descendants) {
tables.add(node.table.t.unwrap(Table.class));
tables.add(
assertNonNull(
node.table.t.unwrap(Table.class),
() -> "can't get table for " + node.table.t));
}
return StarTable.of(this, tables);
}
Expand Down Expand Up @@ -524,9 +539,9 @@ Vertex getSource() {
/** Vertex in the temporary graph. */
private static class Vertex {
final LatticeTable table;
final String alias;
final @Nullable String alias;

private Vertex(LatticeTable table, String alias) {
private Vertex(LatticeTable table, @Nullable String alias) {
this.table = table;
this.alias = alias;
}
Expand All @@ -542,7 +557,7 @@ private Vertex(LatticeTable table, String alias) {
public static class Measure implements Comparable<Measure> {
public final SqlAggFunction agg;
public final boolean distinct;
@Nullable public final String name;
public final @Nullable String name;
public final ImmutableList<Column> args;
public final String digest;

Expand Down Expand Up @@ -673,7 +688,7 @@ public int compareTo(Column column) {
public abstract void toSql(SqlWriter writer);

/** The alias that SQL would give to this expression. */
public abstract String defaultAlias();
public abstract @Nullable String defaultAlias();
}

/** Column in a lattice. Columns are identified by table alias and
Expand Down Expand Up @@ -730,7 +745,7 @@ public void toSql(SqlWriter writer) {
writer.write(e);
}

public String defaultAlias() {
public @Nullable String defaultAlias() {
// there is no default alias for an expression
return null;
}
Expand Down Expand Up @@ -779,9 +794,9 @@ public static class Builder {
private boolean algorithm = false;
private long algorithmMaxMillis = -1;
private boolean auto = true;
private Double rowCountEstimate;
private String statisticProvider;
private Map<String, DerivedColumn> derivedColumnsByName =
private @MonotonicNonNull Double rowCountEstimate;
private @Nullable String statisticProvider;
private final Map<String, DerivedColumn> derivedColumnsByName =
new LinkedHashMap<>();

public Builder(LatticeSpace space, CalciteSchema schema, String sql) {
Expand All @@ -797,15 +812,18 @@ public Builder(LatticeSpace space, CalciteSchema schema, String sql) {
populate(relNodes, tempLinks, parsed.root.rel);

// Get aliases.
List<String> aliases = new ArrayList<>();
populateAliases(((SqlSelect) parsed.sqlNode).getFrom(), aliases, null);
List<@Nullable String> aliases = new ArrayList<>();
SqlNode from = ((SqlSelect) parsed.sqlNode).getFrom();
assert from != null : "from must not be null";
populateAliases(from, aliases, null);

// Build a graph.
final DirectedGraph<Vertex, Edge> graph =
DefaultDirectedGraph.create(Edge.FACTORY);
final List<Vertex> vertices = new ArrayList<>();
for (Pair<RelNode, String> p : Pair.zip(relNodes, aliases)) {
final LatticeTable table = space.register(p.left.getTable());
for (Pair<RelNode, @Nullable String> p : Pair.zip(relNodes, aliases)) {
final LatticeTable table = space.register(
assertNonNull(p.left.getTable(), () -> "no table for " + p.left));
final Vertex vertex = new Vertex(table, p.right);
graph.addVertex(vertex);
vertices.add(vertex);
Expand All @@ -815,7 +833,7 @@ public Builder(LatticeSpace space, CalciteSchema schema, String sql) {
final Vertex target = vertices.get(tempLink[1][0]);
Edge edge = graph.getEdge(source, target);
if (edge == null) {
edge = graph.addEdge(source, target);
edge = castNonNull(graph.addEdge(source, target));
}
edge.pairs.add(IntPair.of(tempLink[0][1], tempLink[1][1]));
}
Expand Down Expand Up @@ -907,7 +925,7 @@ public Builder rowCountEstimate(double rowCountEstimate) {
/** Sets the "statisticProvider" attribute.
*
* <p>If not set, the lattice will use {@link Lattices#CACHED_SQL}. */
public Builder statisticProvider(String statisticProvider) {
public Builder statisticProvider(@Nullable String statisticProvider) {
this.statisticProvider = statisticProvider;
return this;
}
Expand Down Expand Up @@ -1108,7 +1126,7 @@ void fixUp(MutableNode node) {
final String alias = SqlValidatorUtil.uniquify(name,
columnAliases, SqlValidatorUtil.ATTEMPT_SUGGESTER);
final BaseColumn column =
new BaseColumn(c++, node.alias, name, alias);
new BaseColumn(c++, castNonNull(node.alias), name, alias);
columnList.add(column);
columnAliasList.put(name, column); // name before it is made unique
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class MaterializationService {
new MaterializationService();

/** For testing. */
private static final ThreadLocal<MaterializationService> THREAD_INSTANCE =
private static final ThreadLocal<@Nullable MaterializationService> THREAD_INSTANCE =
ThreadLocal.withInitial(MaterializationService::new);

private static final Comparator<Pair<CalciteSchema.TableEntry, TileKey>> C =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@

import com.google.common.collect.ImmutableList;

import org.checkerframework.checker.initialization.qual.UnknownInitialization;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.dataflow.qual.Pure;
import org.slf4j.Logger;

import java.text.NumberFormat;
Expand Down Expand Up @@ -66,9 +69,9 @@ public abstract class AbstractRelOptPlanner implements RelOptPlanner {

protected final RelOptCostFactory costFactory;

private @Nullable MulticastRelOptListener listener;
private @MonotonicNonNull MulticastRelOptListener listener;

private @Nullable RuleAttemptsListener ruleAttemptsListener;
private @MonotonicNonNull RuleAttemptsListener ruleAttemptsListener;

private @Nullable Pattern ruleDescExclusionFilter;

Expand Down Expand Up @@ -175,11 +178,11 @@ public boolean removeRule(RelOptRule rule) {
* @param description Description
* @return Rule with given description, or null if not found
*/
protected RelOptRule getRuleByDescription(String description) {
protected @Nullable RelOptRule getRuleByDescription(String description) {
return mapDescToRule.get(description);
}

public void setRuleDescExclusionFilter(Pattern exclusionFilter) {
public void setRuleDescExclusionFilter(@Nullable Pattern exclusionFilter) {
ruleDescExclusionFilter = exclusionFilter;
}

Expand Down Expand Up @@ -255,7 +258,9 @@ public RelTraitSet emptyTraitSet() {
return getCost(rel, mq);
}

public void addListener(RelOptListener newListener) {
public void addListener(
@UnknownInitialization AbstractRelOptPlanner this,
RelOptListener newListener) {
if (listener == null) {
listener = new MulticastRelOptListener();
}
Expand Down Expand Up @@ -427,6 +432,7 @@ protected void notifyDiscard(RelNode rel) {
}
}

@Pure
public @Nullable RelOptListener getListener() {
return listener;
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/apache/calcite/plan/Convention.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public RelTraitDef getTraitDef() {
return ConventionTraitDef.INSTANCE;
}

@Override public RelNode enforce(final RelNode input,
@Override public @Nullable RelNode enforce(final RelNode input,
final RelTraitSet required) {
return null;
}
Expand Down
Loading

0 comments on commit 913634f

Please sign in to comment.