[go: nahoru, domu]

Skip to content

Commit

Permalink
[CALCITE-5546] Code style: Break long assignment expressions after '='
Browse files Browse the repository at this point in the history
Specifically, we disallow lines that contain ' = ' and end
in '(' and ',', and break up the line by moving the
right-hand side of the assignment to the next line. For
example,

  int x = foo(
      bar());
  int y = baz(1,
      buzz());

becomes

  int x =
      foo(bar());
  int y =
      baz(1, buzz());

The rule looks for '(' or ',' at the end of lines that
contain '=' and do not contain '/' or '*' (which would
suggest a comment), '"' (which would suggest a string
literal), or '@' (which would suggest use in annotation
parameters).

Also remove spaces before '),' ');', ')),', etc.
  • Loading branch information
julianhyde committed Mar 1, 2023
1 parent 4f1452e commit 2dba40e
Show file tree
Hide file tree
Showing 429 changed files with 6,023 additions and 5,392 deletions.
6 changes: 4 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -582,9 +582,11 @@ allprojects {
replaceRegex("require message for requireNonNull", """(?<!#)requireNonNull\(\s*(\w+)\s*(?:,\s*"(?!\1")\w+"\s*)?\)""", "requireNonNull($1, \"$1\")")
// (?-m) disables multiline, so $ matches the very end of the file rather than end of line
replaceRegex("Remove '// End file.java' trailer", "(?-m)\n// End [^\n]+\\.\\w+\\s*$", "")
replaceRegex("<p> should not be placed a the end of the line", "(?-m)\\s*+<p> *+\n \\* ", "\n *\n * <p>")
replaceRegex("<p> should not be placed at the end of the line", "(?-m)\\s*+<p> *+\n \\* ", "\n *\n * <p>")
replaceRegex("Method parameter list should not end in whitespace or newline", "(?<!;)\\s+\\) \\{", ") {")
replaceRegex("Method argument list should not end in whitespace or newline", "(?<!;)\\s+\\);", ");")
replaceRegex("Method argument list should not end in whitespace or newline", "(?<!;)\\s+(\\)+[;,])", "$1")
replaceRegex("Long assignment should be broken after '='", "^([^/*\"\\n]*) = ([^@\\n]*)\\(\n( *)", "$1 =\n$3$2(")
replaceRegex("Long assignment should be broken after '='", "^([^/*\"\\n]*) = ([^@\\n]*)\\((.*,)\n( *)", "$1 =\n$4$2($3 ")
// Assume developer copy-pasted the link, and updated text only, so the url is old, and we replace it with the proper one
replaceRegex(">[CALCITE-...] link styles: 1", "<a(?:(?!CALCITE-)[^>])++CALCITE-\\d+[^>]++>\\s*+\\[?(CALCITE-\\d+)\\]?", "<a href=\"https://issues.apache.org/jira/browse/\$1\">[\$1]")
// If the link was crafted manually, ensure it has [CALCITE-...] in the link text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ class CassandraEnumerator implements Enumerator<Object> {
*/
private @Nullable Object currentRowField(int index) {
assert current != null;
final Object o = current.get(index,
CodecRegistry.DEFAULT.codecFor(
current.getColumnDefinitions().get(index).getType()));
final Object o =
current.get(index,
CodecRegistry.DEFAULT.codecFor(
current.getColumnDefinitions().get(index).getType()));

return convertToEnumeratorObject(o);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,16 @@ RelProtoDataType getRelDataType(String columnFamily, boolean view) {
final String columnName = column.getName().asInternal();

if (dataType instanceof ListType) {
SqlTypeName arrayInnerType = CQL_TO_SQL_TYPE.lookup(
((ListType) dataType).getElementType());
SqlTypeName arrayInnerType =
CQL_TO_SQL_TYPE.lookup(((ListType) dataType).getElementType());

fieldInfo.add(columnName,
typeFactory.createArrayType(
typeFactory.createSqlType(arrayInnerType), -1))
.nullable(true);
} else if (dataType instanceof SetType) {
SqlTypeName multiSetInnerType = CQL_TO_SQL_TYPE.lookup(
((SetType) dataType).getElementType());
SqlTypeName multiSetInnerType =
CQL_TO_SQL_TYPE.lookup(((SetType) dataType).getElementType());

fieldInfo.add(columnName,
typeFactory.createMultisetType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public class CassandraSchemaFactory implements SchemaFactory {
private static final int DEFAULT_CASSANDRA_PORT = 9042;
private static final Map<Map<String, Object>, CqlSession> INFO_TO_SESSION =
new ConcurrentHashMap<>();
private static final Set<String> SESSION_DEFINING_KEYS = ImmutableSet.of(
"host", "port", "keyspace", "username", "password");
private static final Set<String> SESSION_DEFINING_KEYS =
ImmutableSet.of("host", "port", "keyspace", "username", "password");
protected static final Logger LOGGER = CalciteTrace.getPlannerTracer();

public CassandraSchemaFactory() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import static java.util.Objects.requireNonNull;

/**
* Table based on a Cassandra column family.
*/
Expand Down Expand Up @@ -138,8 +139,8 @@ public Enumerable<Object> query(final CqlSession session, List<Map.Entry<String,
final RelDataType rowType = getRowType(typeFactory);

Function1<String, Void> addField = fieldName -> {
RelDataType relDataType = Objects.requireNonNull(
rowType.getField(fieldName, true, false)).getType();
RelDataType relDataType =
requireNonNull(rowType.getField(fieldName, true, false)).getType();
fieldInfo.add(fieldName, relDataType).nullable(true);
return null;
};
Expand Down Expand Up @@ -266,7 +267,7 @@ private CassandraTable getTable() {
}

private CqlSession getSession() {
return Objects.requireNonNull(schema.unwrap(CassandraSchema.class)).session;
return requireNonNull(schema.unwrap(CassandraSchema.class)).session;
}

/** Called via code-generation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;

import java.util.Objects;
import static java.util.Objects.requireNonNull;

/**
* Tests for the {@code org.apache.calcite.adapter.cassandra} package related to data types.
Expand Down Expand Up @@ -195,8 +195,9 @@ static void load(CqlSession session) {

@Test void testCollectionsInnerValues() {
// timestamp retrieval depends on the user timezone, we must compute the expected result
long v = Objects.requireNonNull(
TypeCodecs.TIMESTAMP.parse("'2015-05-03 13:30:54.234'")).toEpochMilli();
long v =
requireNonNull(TypeCodecs.TIMESTAMP.parse("'2015-05-03 13:30:54.234'"))
.toEpochMilli();
String expectedTimestamp = DateTimeUtils.unixTimestampToString(v);

CalciteAssert.that()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,9 @@ private void load(final RelDataType elementType,
switch (rep) {
case OBJECT:
case JAVA_SQL_TIMESTAMP:
final List<@Nullable Long> longs = Util.transform((List<@Nullable Timestamp>) list,
(Timestamp t) -> t == null ? null : t.getTime());
final List<@Nullable Long> longs =
Util.transform((List<@Nullable Timestamp>) list,
(Timestamp t) -> t == null ? null : t.getTime());
return longs;
default:
break;
Expand All @@ -261,8 +262,8 @@ private void load(final RelDataType elementType,
case OBJECT:
case JAVA_SQL_TIME:
return Util.<@Nullable Time, @Nullable Integer>transform(
(List<@Nullable Time>) list, (Time t) -> t == null
? null
(List<@Nullable Time>) list,
(Time t) -> t == null ? null
: (int) (t.getTime() % DateTimeUtils.MILLIS_PER_DAY));
default:
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,10 +540,11 @@ public static Expression convert(Expression operand, Type fromType,
// Try to call "toString()" method
// E.g. from "Integer" to "String"
// Generate "x == null ? null : x.toString()"
result = Expressions.condition(
Expressions.equal(operand, RexImpTable.NULL_EXPR),
RexImpTable.NULL_EXPR,
Expressions.call(operand, "toString"));
result =
Expressions.condition(
Expressions.equal(operand, RexImpTable.NULL_EXPR),
RexImpTable.NULL_EXPR,
Expressions.call(operand, "toString"));
} catch (RuntimeException e) {
// For some special cases, e.g., "BuiltInMethod.LESSER",
// its return type is generic ("Comparable"), which contains
Expand Down Expand Up @@ -820,31 +821,24 @@ static Expression tumblingWindowSelector(
// Find the fixed window for a timestamp given a window size and an offset, and return the
// window start.
// wmColExprToLong - (wmColExprToLong + windowSizeMillis - offsetMillis) % windowSizeMillis
Expression windowStartExpr = Expressions.subtract(
wmColExprToLong,
Expressions.modulo(
Expressions.add(
wmColExprToLong,
Expressions.subtract(
windowSizeExpr,
offsetExpr
)),
Expression windowStartExpr =
Expressions.subtract(wmColExprToLong,
Expressions.modulo(
Expressions.add(wmColExprToLong,
Expressions.subtract(windowSizeExpr, offsetExpr)),
windowSizeExpr));

expressions.add(windowStartExpr);

// The window end equals to the window start plus window size.
// windowStartMillis + sizeMillis
Expression windowEndExpr = Expressions.add(
windowStartExpr,
windowSizeExpr);
Expression windowEndExpr =
Expressions.add(windowStartExpr, windowSizeExpr);

expressions.add(windowEndExpr);

return Expressions.lambda(
Function1.class,
outputPhysType.record(expressions),
parameter);
return Expressions.lambda(Function1.class,
outputPhysType.record(expressions), parameter);
}

/**
Expand Down Expand Up @@ -930,10 +924,11 @@ private void initialize() {
for (@Nullable Object[] element : elements) {
SortedMultiMap<Pair<Long, Long>, @Nullable Object[]> session =
sessionKeyMap.computeIfAbsent(element[indexOfKeyColumn], k -> new SortedMultiMap<>());
Object watermark = requireNonNull(element[indexOfWatermarkedColumn],
"element[indexOfWatermarkedColumn]");
Pair<Long, Long> initWindow = computeInitWindow(
SqlFunctions.toLong(watermark), gap);
Object watermark =
requireNonNull(element[indexOfWatermarkedColumn],
"element[indexOfWatermarkedColumn]");
Pair<Long, Long> initWindow =
computeInitWindow(SqlFunctions.toLong(watermark), gap);
session.putMulti(initWindow, element);
}

Expand Down Expand Up @@ -1043,10 +1038,12 @@ private static class HopEnumerator implements Enumerator<@Nullable Object[]> {
return takeOne();
} else {
@Nullable Object[] current = inputEnumerator.current();
Object watermark = requireNonNull(current[indexOfWatermarkedColumn],
"element[indexOfWatermarkedColumn]");
List<Pair<Long, Long>> windows = hopWindows(SqlFunctions.toLong(watermark),
emitFrequency, windowSize, offset);
Object watermark =
requireNonNull(current[indexOfWatermarkedColumn],
"element[indexOfWatermarkedColumn]");
List<Pair<Long, Long>> windows =
hopWindows(SqlFunctions.toLong(watermark), emitFrequency,
windowSize, offset);
for (Pair<Long, Long> window : windows) {
@Nullable Object[] curWithWindow = new Object[current.length + 2];
System.arraycopy(current, 0, curWithWindow, 0, current.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ public EnumerableAggregate(RelOptCluster cluster, RelTraitSet traitSet,
final List<Expression> initExpressions = new ArrayList<>();
final BlockBuilder initBlock = new BlockBuilder();

final List<Type> aggStateTypes = createAggStateTypes(
initExpressions, initBlock, aggs, typeFactory);
final List<Type> aggStateTypes =
createAggStateTypes(initExpressions, initBlock, aggs, typeFactory);

final PhysType accPhysType =
PhysTypeImpl.of(typeFactory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ protected void implementLambdaFactory(BlockBuilder builder,
Expression accumulatorInitializer, boolean hasOrderedCall,
ParameterExpression lambdaFactory) {
if (hasOrderedCall) {
ParameterExpression pe = Expressions.parameter(List.class,
builder.newName("lazyAccumulators"));
ParameterExpression pe =
Expressions.parameter(List.class, builder.newName("lazyAccumulators"));
builder.add(
Expressions.declare(0, pe, Expressions.new_(LinkedList.class)));

Expand Down Expand Up @@ -163,8 +163,8 @@ protected void implementLambdaFactory(BlockBuilder builder,
accumulatorInitializer, pe)));
} else {
// when hasOrderedCall == false
ParameterExpression pe = Expressions.parameter(List.class,
builder.newName("accumulatorAdders"));
ParameterExpression pe =
Expressions.parameter(List.class, builder.newName("accumulatorAdders"));
builder.add(
Expressions.declare(0, pe, Expressions.new_(LinkedList.class)));

Expand Down Expand Up @@ -283,11 +283,13 @@ protected void createAccumulatorAdders(
}
};

agg.implementor.implementAdd(requireNonNull(agg.context, "agg.context"), addContext);
agg.implementor.implementAdd(requireNonNull(agg.context, "agg.context"),
addContext);
builder2.add(accExpr);
agg.accumulatorAdder = builder.append("accumulatorAdder",
Expressions.lambda(Function2.class, builder2.toBlock(), accExpr,
inParameter));
agg.accumulatorAdder =
builder.append("accumulatorAdder",
Expressions.lambda(Function2.class, builder2.toBlock(), accExpr,
inParameter));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,13 @@ public static EnumerableBatchNestedLoopJoin create(
corrArg =
Expressions.parameter(Modifier.FINAL,
corrVarType, corrVar.get(c));
final DeclarationStatement decl = Expressions.declare(
Modifier.FINAL,
corrArg,
Expressions.convert_(
Expressions.call(
corrArgList,
BuiltInMethod.LIST_GET.method,
Expressions.constant(c)),
corrVarType));
final DeclarationStatement decl =
Expressions.declare(Modifier.FINAL, corrArg,
Expressions.convert_(
Expressions.call(corrArgList,
BuiltInMethod.LIST_GET.method,
Expressions.constant(c)),
corrVarType));
corrBlock.add(decl);
implementor.registerCorrelVariable(corrVar.get(c), corrArg,
corrBlock, leftResult.physType);
Expand All @@ -193,13 +191,11 @@ public static EnumerableBatchNestedLoopJoin create(
corrArg =
Expressions.parameter(Modifier.FINAL,
Primitive.box(corrVarType), "$box" + corrVar.get(c));
final DeclarationStatement decl = Expressions.declare(
Modifier.FINAL,
corrArg,
Expressions.call(
corrArgList,
BuiltInMethod.LIST_GET.method,
Expressions.constant(c)));
final DeclarationStatement decl =
Expressions.declare(Modifier.FINAL, corrArg,
Expressions.call(corrArgList,
BuiltInMethod.LIST_GET.method,
Expressions.constant(c)));
corrBlock.add(decl);
final ParameterExpression corrRef =
(ParameterExpression) corrBlock.append(corrVar.get(c),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ protected EnumerableBindable(RelOptCluster cluster, RelNode input) {

@Override public Enumerable<@Nullable Object[]> bind(DataContext dataContext) {
final ImmutableMap<String, Object> map = ImmutableMap.of();
final Bindable bindable = EnumerableInterpretable.toBindable(map, null,
(EnumerableRel) getInput(), EnumerableRel.Prefer.ARRAY);
final Bindable bindable =
EnumerableInterpretable.toBindable(map, null,
(EnumerableRel) getInput(), EnumerableRel.Prefer.ARRAY);
final ArrayBindable arrayBindable = EnumerableInterpretable.box(bindable);
return arrayBindable.bind(dataContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,17 +266,17 @@ public static EnumerableCalc create(final RelNode input,

@Override public @Nullable Pair<RelTraitSet, List<RelTraitSet>> passThroughTraits(
final RelTraitSet required) {
final List<RexNode> exps = Util.transform(program.getProjectList(),
program::expandLocalRef);
final List<RexNode> exps =
Util.transform(program.getProjectList(), program::expandLocalRef);

return EnumerableTraitsUtils.passThroughTraitsForProject(required, exps,
input.getRowType(), input.getCluster().getTypeFactory(), traitSet);
}

@Override public @Nullable Pair<RelTraitSet, List<RelTraitSet>> deriveTraits(
final RelTraitSet childTraits, final int childId) {
final List<RexNode> exps = Util.transform(program.getProjectList(),
program::expandLocalRef);
final List<RexNode> exps =
Util.transform(program.getProjectList(), program::expandLocalRef);

return EnumerableTraitsUtils.deriveTraitsForProject(childTraits, childId, exps,
input.getRowType(), input.getCluster().getTypeFactory(), traitSet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public enum EnumerableConvention implements Convention {
final RelTraitSet required) {
RelNode rel = input;
if (input.getConvention() != INSTANCE) {
rel = ConventionTraitDef.INSTANCE.convert(
input.getCluster().getPlanner(),
input, INSTANCE, true);
rel =
ConventionTraitDef.INSTANCE.convert(input.getCluster().getPlanner(),
input, INSTANCE, true);
requireNonNull(rel,
() -> "Unable to convert input to " + INSTANCE + ", input = " + input);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ public static EnumerableCorrelate create(
corrArg =
Expressions.parameter(Modifier.FINAL,
Primitive.box(corrVarType), "$box" + getCorrelVariable());
corrRef = (ParameterExpression) corrBlock.append(getCorrelVariable(),
Expressions.unbox(corrArg));
corrRef =
(ParameterExpression) corrBlock.append(getCorrelVariable(),
Expressions.unbox(corrArg));
}

implementor.registerCorrelVariable(getCorrelVariable(), corrRef,
Expand Down
Loading

0 comments on commit 2dba40e

Please sign in to comment.