Skip to content

Commit

Permalink
Code style: lint
Browse files Browse the repository at this point in the history
The next commit adds various lint rules. This commit fixes
the errors detected by those rules in existing code.
  • Loading branch information
julianhyde committed Oct 14, 2023
1 parent e8af93f commit ce88348
Show file tree
Hide file tree
Showing 65 changed files with 170 additions and 140 deletions.
12 changes: 7 additions & 5 deletions babel/src/test/resources/sql/big-query.iq
Original file line number Diff line number Diff line change
Expand Up @@ -686,8 +686,8 @@ SELECT SAFE_ADD(CAST('NaN' AS DOUBLE), CAST(3 as BIGINT)) as NaN_result;
#
# SAFE_DIVIDE(value1, value2)
#
# Equivalent to the divide operator (/), but returns NULL if overflow/underflow occurs
# or if value2 is zero.
# Equivalent to the divide operator (/), but returns NULL if
# overflow/underflow occurs or if value2 is zero.
SELECT SAFE_DIVIDE(20, 4) as result;
+--------+
| result |
Expand All @@ -709,7 +709,8 @@ SELECT SAFE_DIVIDE(-9223372036854775806, 0) as zero_result;

!ok

SELECT SAFE_DIVIDE(CAST(1.7e308 as DOUBLE), CAST(1.7e-308 as DOUBLE)) as double_overflow;
SELECT SAFE_DIVIDE(CAST(1.7e308 as DOUBLE),
CAST(1.7e-308 as DOUBLE)) as double_overflow;
+-----------------+
| double_overflow |
+-----------------+
Expand All @@ -719,8 +720,8 @@ SELECT SAFE_DIVIDE(CAST(1.7e308 as DOUBLE), CAST(1.7e-308 as DOUBLE)) as double_

!ok

SELECT SAFE_DIVIDE(CAST(-3.5e75 AS DECIMAL(76, 0)), CAST(3.5e-75 AS DECIMAL(76, 0)))
as decimal_overflow;
SELECT SAFE_DIVIDE(CAST(-3.5e75 AS DECIMAL(76, 0)),
CAST(3.5e-75 AS DECIMAL(76, 0))) as decimal_overflow;
+------------------+
| decimal_overflow |
+------------------+
Expand Down Expand Up @@ -751,6 +752,7 @@ SELECT SAFE_DIVIDE(1, CAST(1 AS DOUBLE) / CAST(9223372036854775807 AS DOUBLE)) a
(1 row)

!ok

#####################################################################
# SAFE_MULTIPLY
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ static List<Expression> convertAssignableTypes(Class<?>[] targetTypes,
}
} else {
int j = 0;
for (Expression argument: arguments) {
for (Expression argument : arguments) {
Class<?> type;
if (!targetTypes[j].isArray()) {
type = targetTypes[j];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,25 @@ public static EnumerableLimitSort create(

final Expression fetchVal;
if (this.fetch == null) {
fetchVal = Expressions.constant(Integer.valueOf(Integer.MAX_VALUE));
fetchVal = Expressions.constant(Integer.MAX_VALUE);
} else {
fetchVal = getExpression(this.fetch);
}

final Expression offsetVal = this.offset == null ? Expressions.constant(Integer.valueOf(0))
: getExpression(this.offset);
final Expression offsetVal;
if (this.offset == null) {
offsetVal = Expressions.constant(0);
} else {
offsetVal = getExpression(this.offset);
}

builder.add(
Expressions.return_(
null, Expressions.call(
BuiltInMethod.ORDER_BY_WITH_FETCH_AND_OFFSET.method, Expressions.list(
childExp,
builder.append("keySelector", pair.left))
.appendIfNotNull(builder.appendIfNotNull("comparator", pair.right))
Expressions.return_(null,
Expressions.call(BuiltInMethod.ORDER_BY_WITH_FETCH_AND_OFFSET.method,
Expressions.list(childExp,
builder.append("keySelector", pair.left))
.appendIfNotNull(
builder.appendIfNotNull("comparator", pair.right))
.appendIfNotNull(
builder.appendIfNotNull("offset",
Expressions.constant(offsetVal)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3762,7 +3762,7 @@ private abstract static class AbstractRexCallImplementor
final List<RexToLixTranslator.Result> arguments) {
final List<Expression> argIsNullList = new ArrayList<>();
final List<Expression> argValueList = new ArrayList<>();
for (RexToLixTranslator.Result result: arguments) {
for (RexToLixTranslator.Result result : arguments) {
argIsNullList.add(result.isNullVariable);
argValueList.add(result.valueVariable);
}
Expand Down Expand Up @@ -3950,7 +3950,7 @@ private static class LogicalAndImplementor extends AbstractRexCallImplementor {
@Override public RexToLixTranslator.Result implement(final RexToLixTranslator translator,
final RexCall call, final List<RexToLixTranslator.Result> arguments) {
final List<Expression> argIsNullList = new ArrayList<>();
for (RexToLixTranslator.Result result: arguments) {
for (RexToLixTranslator.Result result : arguments) {
argIsNullList.add(result.isNullVariable);
}
final List<Expression> nullAsTrue =
Expand Down Expand Up @@ -4004,7 +4004,7 @@ private static class LogicalOrImplementor extends AbstractRexCallImplementor {
@Override public RexToLixTranslator.Result implement(final RexToLixTranslator translator,
final RexCall call, final List<RexToLixTranslator.Result> arguments) {
final List<Expression> argIsNullList = new ArrayList<>();
for (RexToLixTranslator.Result result: arguments) {
for (RexToLixTranslator.Result result : arguments) {
argIsNullList.add(result.isNullVariable);
}
final List<Expression> nullAsFalse =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public JoinNode(Compiler compiler, Join rel) {
// send un-match rows for full join on right source
List<Row> empty = new ArrayList<>();
// TODO: CALCITE-4308, JointNode in Interpreter might fail with NPE for FULL join
for (Row row: requireNonNull(innerRows, "innerRows")) {
for (Row row : requireNonNull(innerRows, "innerRows")) {
if (matchRowSet.contains(row)) {
continue;
}
Expand All @@ -109,7 +109,7 @@ private List<Row> doJoin(Row outerRow, List<Row> innerRows,
boolean outerRowOnLeft = joinRelType != JoinRelType.RIGHT;
copyToContext(outerRow, outerRowOnLeft);
List<Row> matchInnerRows = new ArrayList<>();
for (Row innerRow: innerRows) {
for (Row innerRow : innerRows) {
copyToContext(innerRow, !outerRowOnLeft);
final Boolean execute = (Boolean) condition.execute(context);
if (execute != null && execute) {
Expand All @@ -135,7 +135,7 @@ private void doSend(Row outerRow, List<Row> matchInnerRows,
boolean outerRowOnLeft = joinRelType != JoinRelType.RIGHT;
copyToContext(outerRow, outerRowOnLeft);
requireNonNull(context.values, "context.values");
for (Row row: matchInnerRows) {
for (Row row : matchInnerRows) {
copyToContext(row, !outerRowOnLeft);
sink.send(Row.asCopy(context.values));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public UncollectNode(Compiler compiler, Uncollect uncollect) {
@Override public void run() throws InterruptedException {
Row row = null;
while ((row = source.receive()) != null) {
for (Object value: row.getValues()) {
for (Object value : row.getValues()) {
if (value == null) {
throw new NullPointerException("NULL value for unnest.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private SimpleCalciteSchema(@Nullable CalciteSchema parent,
return lowerCaseName;
}
// Fall through: Set iteration
for (String candidate: candidates) {
for (String candidate : candidates) {
if (candidate.equalsIgnoreCase(name)) {
return candidate;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1746,7 +1746,7 @@ private static boolean sameRelCollectionNoOrderConsidered(
if (list0.size() != list1.size()) {
return false;
}
for (MutableRel rel: list0) {
for (MutableRel rel : list0) {
int index = list1.indexOf(rel);
if (index == -1) {
return false;
Expand All @@ -1766,7 +1766,7 @@ public static Pair<RexNode, List<RexNode>> explainCalc(MutableCalc calc) {
final RexShuttle shuttle = getExpandShuttle(calc.program);
final RexNode condition = shuttle.apply(calc.program.getCondition());
final List<RexNode> projects = new ArrayList<>();
for (RexNode rex: shuttle.apply(calc.program.getProjectList())) {
for (RexNode rex : shuttle.apply(calc.program.getProjectList())) {
projects.add(rex);
}
if (condition == null) {
Expand Down Expand Up @@ -1836,7 +1836,7 @@ private static boolean implies(
private static boolean referenceByMapping(
RexNode joinCondition, List<RexNode>... projectsOfInputs) {
List<RexNode> projects = new ArrayList<>();
for (List<RexNode> projectsOfInput: projectsOfInputs) {
for (List<RexNode> projectsOfInput : projectsOfInputs) {
projects.addAll(projectsOfInput);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,9 @@ public VolcanoPlanner(@Nullable RelOptCostFactory costFactory,
this.zeroCost = this.costFactory.makeZeroCost();
this.infCost = this.costFactory.makeInfiniteCost();
// If LOGGER is debug enabled, enable provenance information to be captured
this.provenanceMap = LOGGER.isDebugEnabled() ? new HashMap<>()
: Util.blackholeMap();
this.provenanceMap =
LOGGER.isDebugEnabled() ? new HashMap<>()
: Util.blackholeMap();
initRuleQueue();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ protected ConverterRule(Config config) {

// Most sub-classes are concerned with converting one convention to
// another, and for them, the "out" field is a convenient short-cut.
this.out = outTrait instanceof Convention ? (Convention) outTrait
: castNonNull(null);
this.out =
outTrait instanceof Convention ? (Convention) outTrait
: castNonNull(null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,9 @@ private String formatNodeLabel(String label, int limit) {
}

List<String> descParts = new ArrayList<>();
for (int idx = 0; idx < label.length(); idx += option.maxNodeLabelPerLine()) {
int endIdx = idx + option.maxNodeLabelPerLine() > label.length() ? label.length()
: idx + option.maxNodeLabelPerLine();
descParts.add(label.substring(idx, endIdx));
for (int i = 0; i < label.length(); i += option.maxNodeLabelPerLine()) {
int endIdx = Math.min(i + option.maxNodeLabelPerLine(), label.length());
descParts.add(label.substring(i, endIdx));
}

return String.join("\\n", descParts) + (trimmed ? "..." : "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ public enum Composition {
private boolean apply(Composition composition, RelHint hint, RelNode rel) {
switch (composition) {
case AND:
for (HintPredicate predicate: predicates) {
for (HintPredicate predicate : predicates) {
if (!predicate.apply(hint, rel)) {
return false;
}
}
return true;
case OR:
default:
for (HintPredicate predicate: predicates) {
for (HintPredicate predicate : predicates) {
if (predicate.apply(hint, rel)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private RelMdColumnOrigins() {}
}
};
final List<RexNode> projects = new ArrayList<>();
for (RexNode rex: rexShuttle.apply(rel.getProgram().getProjectList())) {
for (RexNode rex : rexShuttle.apply(rel.getProgram().getProjectList())) {
projects.add(rex);
}
final RexNode rexNode = projects.get(iOutputColumn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ public Boolean areColumnsUnique(Intersect rel, RelMetadataQuery mq,
return false;
}
final ImmutableBitSet.Builder targetColumns = ImmutableBitSet.builder();
for (int key: commonKeys) {
for (int key : commonKeys) {
targetColumns.set(rel.getGroupSet().nth(key));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ public RelOptPredicateList getPredicates(Intersect intersect, RelMetadataQuery m
continue;
}

for (RexNode pred: info.pulledUpPredicates) {
for (RexNode pred : info.pulledUpPredicates) {
if (finalPredicates.stream().anyMatch(
finalPred -> rexImplicationChecker.implies(finalPred, pred))) {
// There's already a stricter predicate in finalPredicates,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,16 +501,16 @@ public Result visit(Window e) {
final RelNode input = e.getInput();
final int inputFieldCount = input.getRowType().getFieldCount();
final List<SqlNode> rexOvers = new ArrayList<>();
for (Window.Group group: e.groups) {
for (Window.Group group : e.groups) {
rexOvers.addAll(builder.context.toSql(group, e.constants, inputFieldCount));
}
final List<SqlNode> selectList = new ArrayList<>();

for (RelDataTypeField field: input.getRowType().getFieldList()) {
for (RelDataTypeField field : input.getRowType().getFieldList()) {
addSelect(selectList, builder.context.field(field.getIndex()), e.getRowType());
}

for (SqlNode rexOver: rexOvers) {
for (SqlNode rexOver : rexOvers) {
addSelect(selectList, rexOver, e.getRowType());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -943,10 +943,10 @@ public List<SqlNode> toSql(Window.Group group, ImmutableList<RexLiteral> constan
final List<SqlNode> rexOvers = new ArrayList<>();
final List<SqlNode> partitionKeys = new ArrayList<>();
final List<SqlNode> orderByKeys = new ArrayList<>();
for (int partition: group.keys) {
for (int partition : group.keys) {
partitionKeys.add(this.field(partition));
}
for (RelFieldCollation collation: group.orderKeys.getFieldCollations()) {
for (RelFieldCollation collation : group.orderKeys.getFieldCollations()) {
this.addOrderItem(orderByKeys, collation);
}
SqlLiteral isRows = SqlLiteral.createBoolean(group.isRows, POS);
Expand All @@ -955,7 +955,7 @@ public List<SqlNode> toSql(Window.Group group, ImmutableList<RexLiteral> constan

final SqlLiteral allowPartial = null;

for (Window.RexWinAggCall winAggCall: group.aggCalls) {
for (Window.RexWinAggCall winAggCall : group.aggCalls) {
SqlAggFunction aggFunction = (SqlAggFunction) winAggCall.getOperator();
final SqlWindow sqlWindow =
SqlWindow.create(null, null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,9 @@ private static void rewriteUsingGroupingSets(RelOptRuleCall call,
// Get the base ordinal of filter args for different groupSets.
final Map<Pair<ImmutableBitSet, Integer>, Integer> filters = new LinkedHashMap<>();
int z = groupCount + distinctAggCalls.size();
for (ImmutableBitSet groupSet: groupSets) {
for (ImmutableBitSet groupSet : groupSets) {
Set<Integer> filterArgList = distinctFilterArgMap.get(groupSet);
for (Integer filterArg: requireNonNull(filterArgList, "filterArgList")) {
for (Integer filterArg : requireNonNull(filterArgList, "filterArgList")) {
filters.put(Pair.of(groupSet, filterArg), z);
z += 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ public AggregateJoinRemoveRule(
boolean isLeftJoin = join.getJoinType() == JoinRelType.LEFT;
int lower = isLeftJoin
? join.getLeft().getRowType().getFieldCount() : 0;
int upper = isLeftJoin ? join.getRowType().getFieldCount()
: join.getLeft().getRowType().getFieldCount();
int upper =
isLeftJoin ? join.getRowType().getFieldCount()
: join.getLeft().getRowType().getFieldCount();

// Check whether the aggregate uses columns whose index is between
// lower(included) and upper(excluded).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private AggregateProjectConstantToDummyJoinRule(Config config) {
final Aggregate aggregate = call.rel(0);
final Project project = call.rel(1);

for (int groupKey: aggregate.getGroupSet().asList()) {
for (int groupKey : aggregate.getGroupSet().asList()) {
if (groupKey >= aggregate.getRowType().getFieldCount()) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,9 @@ public MinusToDistinctRule(Class<? extends Minus> minusClass,

ImmutableList.Builder<RexNode> filters = ImmutableList.builder();
for (int i = 0; i < branchCount; i++) {
SqlOperator operator = i == 0 ? SqlStdOperatorTable.GREATER_THAN
: SqlStdOperatorTable.EQUALS;
SqlOperator operator =
i == 0 ? SqlStdOperatorTable.GREATER_THAN
: SqlStdOperatorTable.EQUALS;
filters.add(
rexBuilder.makeCall(operator, relBuilder.field(originalFieldCnt + i),
relBuilder.literal(new BigDecimal(0))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public ProjectJoinJoinRemoveRule(
int leftBottomChildSize = bottomJoin.getLeft().getRowType().getFieldCount();

// Check whether the project uses columns in the right input of bottom join.
for (RexNode expr: project.getProjects()) {
for (RexNode expr : project.getProjects()) {
if (RelOptUtil.InputFinder.bits(expr).asList().stream().anyMatch(
i -> i >= leftBottomChildSize
&& i < bottomJoin.getRowType().getFieldCount())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public ProjectJoinRemoveRule(

// Check whether the project uses columns whose index is between
// lower(included) and upper(excluded).
for (RexNode expr: project.getProjects()) {
for (RexNode expr : project.getProjects()) {
if (RelOptUtil.InputFinder.bits(expr).asList().stream().anyMatch(
i -> i >= lower && i < upper)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ private RelDataType createStructType(
final List<RelDataType> types, SqlTypeName sqlTypeName) {
assert sqlTypeName == SqlTypeName.ARRAY || sqlTypeName == SqlTypeName.MULTISET;
boolean isNullable = false;
for (RelDataType type: types) {
for (RelDataType type : types) {
if (type.getComponentType() == null) {
return null;
}
Expand All @@ -286,7 +286,7 @@ private RelDataType createStructType(
final List<RelDataType> types, SqlTypeName sqlTypeName) {
assert sqlTypeName == SqlTypeName.MAP;
boolean isNullable = false;
for (RelDataType type: types) {
for (RelDataType type : types) {
if (!(type instanceof MapSqlType)) {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/apache/calcite/rex/RexProgram.java
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public static RexProgram create(RelInput input) {
final List<RexNode> projectRexNodes =
requireNonNull(input.getExpressionList("projects"), "projects");
final List<RexLocalRef> projects = new ArrayList<>(projectRexNodes.size());
for (RexNode rexNode: projectRexNodes) {
for (RexNode rexNode : projectRexNodes) {
projects.add((RexLocalRef) rexNode);
}
final RelDataType inputType = input.getRowType("inputRowType");
Expand Down
Loading

0 comments on commit ce88348

Please sign in to comment.