Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1454,8 +1454,9 @@ private static Expression scaleValue(
final Pair<RexNode, @Nullable Type> key = Pair.of(inputRef, currentStorageType);
// If the RexInputRef has been visited under current storage type already,
// it is not necessary to visit it again, just return the result.
if (rexWithStorageTypeResultMap.containsKey(key)) {
return rexWithStorageTypeResultMap.get(key);
final Result cached = rexWithStorageTypeResultMap.get(key);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some Java maps allow null keys. Have you checked that this one doesn't?
If it does, the semantics is not the same.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case the values will always be non-null. Should I comment in these cases to make it more clear this is safe?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fortunately we have @Nullable annotations, so if the ksy is not nullable, hopefully the invariant is maintained.

if (cached != null) {
return cached;
}
// Generate one line of code to get the input, e.g.,
// "final Employee current =(Employee) inputEnumerator.current();"
Expand Down Expand Up @@ -1519,8 +1520,9 @@ private static Expression scaleValue(
*/
@Override public Result visitLiteral(RexLiteral literal) {
// If the RexLiteral has been visited already, just return the result
if (rexResultMap.containsKey(literal)) {
return rexResultMap.get(literal);
final Result cached = rexResultMap.get(literal);
if (cached != null) {
return cached;
}
// Generate one line of code for the value of RexLiteral, e.g.,
// "final int literal_value = 10;"
Expand Down Expand Up @@ -1613,8 +1615,9 @@ private ConstantExpression getTypedNullLiteral(RexLiteral literal) {
* need to be implemented separately.
*/
@Override public Result visitCall(RexCall call) {
if (rexResultMap.containsKey(call)) {
return rexResultMap.get(call);
final Result cached = rexResultMap.get(call);
if (cached != null) {
return cached;
}
final SqlOperator operator = call.getOperator();
if (operator == PREV) {
Expand Down Expand Up @@ -1812,8 +1815,9 @@ private Result toInnerStorageType(Result result, Type storageType) {
@Override public Result visitDynamicParam(RexDynamicParam dynamicParam) {
final Pair<RexNode, @Nullable Type> key =
Pair.of(dynamicParam, currentStorageType);
if (rexWithStorageTypeResultMap.containsKey(key)) {
return rexWithStorageTypeResultMap.get(key);
final Result cached = rexWithStorageTypeResultMap.get(key);
if (cached != null) {
return cached;
}
final Type valueType = typeFactory.getJavaClass(dynamicParam.getType());
final Type storageType = currentStorageType != null ? currentStorageType : valueType;
Expand Down Expand Up @@ -1857,8 +1861,9 @@ private Result toInnerStorageType(Result result, Type storageType) {
@Override public Result visitFieldAccess(RexFieldAccess fieldAccess) {
final Pair<RexNode, @Nullable Type> key =
Pair.of(fieldAccess, currentStorageType);
if (rexWithStorageTypeResultMap.containsKey(key)) {
return rexWithStorageTypeResultMap.get(key);
final Result cached = rexWithStorageTypeResultMap.get(key);
if (cached != null) {
return cached;
}
final RexNode target = deref(fieldAccess.getReferenceExpr());
int fieldIndex = fieldAccess.getField().getIndex();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,15 +382,16 @@ public void send(Row row) {
}
Row key = builder.build();

if (!accumulators.containsKey(key)) {
AccumulatorList list = new AccumulatorList();
AccumulatorList list = accumulators.get(key);
if (list == null) {
list = new AccumulatorList();
for (AccumulatorFactory factory : accumulatorFactories) {
list.add(factory.get());
}
accumulators.put(key, list);
}

accumulators.get(key).send(row);
list.send(row);
}

public void end(Sink sink) throws InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,16 @@ public UncollectNode(Compiler compiler, Uncollect uncollect) {
}
}
} else if (value instanceof Map) {
Map map = (Map) value;
Map<?, ?> map = (Map<?, ?>) value;
if (map.isEmpty() && rel.isOuter) {
sink.send(Row.of(new Object[width]));
continue;
}
for (Object key : map.keySet()) {
for (Map.Entry<?, ?> entry : map.entrySet()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Map.forEach(BiConsumer) may be more efficient here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The int i is being modified within this loop, so that would need to be wrapped in order to modify it, if we went that route. entrySet seems like a better fit here

if (rel.withOrdinality) {
sink.send(Row.of(key, map.get(key), i++));
sink.send(Row.of(entry.getKey(), entry.getValue(), i++));
} else {
sink.send(Row.of(key, map.get(key)));
sink.send(Row.of(entry.getKey(), entry.getValue()));
}
}
} else {
Expand Down
5 changes: 3 additions & 2 deletions core/src/main/java/org/apache/calcite/plan/RelOptUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2773,8 +2773,9 @@ public static void inferViewPredicates(Map<Integer, RexNode> projectMap,
}
if (o0 instanceof RexInputRef && o1 instanceof RexLiteral) {
final int index = ((RexInputRef) o0).getIndex();
if (projectMap.get(index) == null) {
projectMap.put(index, o1);
// The first constraint on a column populates projectMap; a later
// constraint on the same column remains in filters.
if (projectMap.putIfAbsent(index, o1) == null) {
continue;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1080,9 +1080,7 @@ private void updateVertex(HepRelVertex vertex, RelNode rel) {
notifyDiscard(vertex.getCurrentRel());
}
RelDigest oldKey = vertex.getCurrentRel().getRelDigest();
if (mapDigestToVertex.get(oldKey) == vertex) {
mapDigestToVertex.remove(oldKey);
}
mapDigestToVertex.remove(oldKey, vertex);
// When a transformation happened in one rule apply, support
// vertex2 replace vertex1, but the current relNode of
// vertex1 and vertex2 is same,
Expand Down
9 changes: 2 additions & 7 deletions core/src/main/java/org/apache/calcite/rel/core/Match.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,8 @@ private static class AggregateFinder extends RexVisitorImpl<Void> {
pv.add(STAR);
}
for (String alpha : pv) {
final NavigableSet<RexMRAggCall> set;
if (aggregateCallsPerVar.containsKey(alpha)) {
set = aggregateCallsPerVar.get(alpha);
} else {
set = new TreeSet<>();
aggregateCallsPerVar.put(alpha, set);
}
final NavigableSet<RexMRAggCall> set =
aggregateCallsPerVar.computeIfAbsent(alpha, k -> new TreeSet<>());
boolean update = true;
for (RexMRAggCall rex : set) {
if (rex.equals(aggCall)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -187,9 +188,10 @@ private void explainInputs(List<? extends @Nullable RelNode> inputs) {
pw.println("digraph {");

// print nodes with styles
for (RelNode rel : nodeStyles.keySet()) {
String style = String.join(",", nodeStyles.get(rel));
pw.println(nodeLabels.get(rel) + " [style=\"" + style + "\"]");
for (Map.Entry<RelNode, Collection<String>> entry
: nodeStyles.asMap().entrySet()) {
String style = String.join(",", entry.getValue());
pw.println(nodeLabels.get(entry.getKey()) + " [style=\"" + style + "\"]");
}

// ordinary arcs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,8 @@ private static RelHomogeneousShuttle getReplacer(
@Override public RelNode visit(RelNode node) {
// Check if this node's digest matches any of our shared components
RelDigest nodeDigest = node.getRelDigest();
if (digestToSpool.containsKey(nodeDigest)) {
LogicalTableSpool spool = digestToSpool.get(nodeDigest);

LogicalTableSpool spool = digestToSpool.get(nodeDigest);
if (spool != null) {
if (producers.contains(nodeDigest)) {
// Subsequent occurrence - replace with table scan (consumer)
return LogicalTableScan.create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,9 @@ private static void findRemovableSelfJoins(RelMetadataQuery mq, LoptMultiJoin mu
// From the candidate self-join pairs, determine if there is
// the appropriate join condition between the two factors that will
// allow the join to be removed.
for (Integer factor1 : selfJoinPairs.keySet()) {
final int factor2 = selfJoinPairs.get(factor1);
for (Map.Entry<Integer, Integer> selfJoinPair : selfJoinPairs.entrySet()) {
final int factor1 = selfJoinPair.getKey();
final int factor2 = selfJoinPair.getValue();
final List<RexNode> selfJoinFilters = new ArrayList<>();
for (RexNode filter : multiJoin.getJoinFilters()) {
ImmutableBitSet joinFactors =
Expand Down
26 changes: 9 additions & 17 deletions core/src/main/java/org/apache/calcite/rex/RexUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3197,14 +3197,12 @@ private void combinePredicatesUsingAnd(
Map<K, RexNode> baseMap,
Map<K, RexNode> forMergeMap) {
for (Map.Entry<K, RexNode> entry : forMergeMap.entrySet()) {
final RexNode baseRex = baseMap.get(entry.getKey());
RexNode mergedRex =
relBuilder.and(
entry.getValue(),
baseMap.getOrDefault(entry.getKey(), relBuilder.literal(true)));
relBuilder.and(entry.getValue(),
baseRex == null ? relBuilder.literal(true) : baseRex);
int originalCount = entry.getValue().nodeCount()
+ (baseMap.containsKey(entry.getKey())
? baseMap.get(entry.getKey()).nodeCount()
: 0);
+ (baseRex == null ? 0 : baseRex.nodeCount());
checkExpandCount(mergedRex.nodeCount() - originalCount);
baseMap.put(entry.getKey(), mergedRex);
}
Expand Down Expand Up @@ -3233,23 +3231,17 @@ private void combinePredicatesUsingOr(
Iterator<Map.Entry<K, RexNode>> iterator =
baseMap.entrySet().iterator();
while (iterator.hasNext()) {
int forMergeNodeCount = 0;

Map.Entry<K, RexNode> entry = iterator.next();
if (!forMergeMap.containsKey(entry.getKey())) {
final RexNode forMergeRex = forMergeMap.get(entry.getKey());
if (forMergeRex == null) {
checkExpandCount(-entry.getValue().nodeCount());
iterator.remove();
continue;
} else {
forMergeNodeCount = forMergeMap.get(entry.getKey()).nodeCount();
}
RexNode mergedRex =
relBuilder.or(
entry.getValue(),
forMergeMap.get(entry.getKey()));
int originalCount = entry.getValue().nodeCount() + forMergeNodeCount;
RexNode mergedRex = relBuilder.or(entry.getValue(), forMergeRex);
int originalCount = entry.getValue().nodeCount() + forMergeRex.nodeCount();
checkExpandCount(mergedRex.nodeCount() - originalCount);
baseMap.put(entry.getKey(), mergedRex);
entry.setValue(mergedRex);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8793,8 +8793,7 @@ private SqlNode expandAliases(SqlIdentifier id, CalciteContextException ex) {
if (expr instanceof SqlIdentifier) {
expr = getScope().fullyQualify((SqlIdentifier) expr).identifier;
}
if (!expansions.containsKey(name)) {
expansions.put(name, expr);
if (expansions.putIfAbsent(name, expr) == null) {
validator.setOriginal(expr, id);
}
return expr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,23 +197,24 @@ private RelNode getCurrentRelOrThrow() {
public void updateRelInMap(
SortedSetMultimap<RelNode, CorrelationId> mapRefRelToCorVar) {
for (RelNode rel : Lists.newArrayList(mapRefRelToCorVar.keySet())) {
if (oldToNewRelMap.containsKey(rel)) {
RelNode newRel = oldToNewRelMap.get(rel);
if (newRel != null) {
SortedSet<CorrelationId> corVarSet =
mapRefRelToCorVar.removeAll(rel);
mapRefRelToCorVar.putAll(oldToNewRelMap.get(rel), corVarSet);
mapRefRelToCorVar.putAll(newRel, corVarSet);
}
}
}

@SuppressWarnings({"JdkObsolete", "ModifyCollectionInEnhancedForLoop"})
@SuppressWarnings("JdkObsolete")
public void updateRelInMap(
SortedMap<CorrelationId, LogicalCorrelate> mapCorVarToCorRel) {
for (CorrelationId corVar : mapCorVarToCorRel.keySet()) {
LogicalCorrelate oldRel = mapCorVarToCorRel.get(corVar);
if (oldToNewRelMap.containsKey(oldRel)) {
RelNode newRel = oldToNewRelMap.get(oldRel);
for (Map.Entry<CorrelationId, LogicalCorrelate> entry
: mapCorVarToCorRel.entrySet()) {
RelNode newRel = oldToNewRelMap.get(entry.getValue());
if (newRel != null) {
assert newRel instanceof LogicalCorrelate;
mapCorVarToCorRel.put(corVar, (LogicalCorrelate) newRel);
entry.setValue((LogicalCorrelate) newRel);
}
}
}
Expand Down
Loading