auphong2707/dl4se-code-review-dataset huggingface dataset:
Source:
START public void initTestAggregationFunction() END { typeRegistry = new TypeRegistry(); functionManager = new FunctionManager(typeRegistry, new BlockEncodingManager(typeRegistry), new FeaturesConfig()); session = testSessionBuilder().build(); } Is this a necessary change?

Target:
public final void initTestAggregationFunction() { typeRegistry = new TypeRegistry(); functionManager = new FunctionManager(typeRegistry, new BlockEncodingManager(typeRegistry), new FeaturesConfig()); }
Source:
protected TestHivePushdownFilterQueries() { START super(() -> createQueryRunner()); END } nit: `super(TestHivePushdownFilterQueries::createQueryRunner);`

Target:
protected TestHivePushdownFilterQueries() { super(TestHivePushdownFilterQueries::createQueryRunner); }
Source:
public static CallExpression call(FunctionManager functionManager, String name, Type returnType, List<RowExpression> arguments) { FunctionHandle functionHandle = functionManager.lookupFunction(name, fromTypes(arguments.stream().map(RowExpression::getType).collect(toImmutableList()))); START return new CallExpression(name, functionHandle, returnType, arguments); END } - this should delegate to `call` two functions above. - also, here `name` would be a bit confusing from `displayName`. Maybe @rongrong could provide suggestion.

Target:
public static CallExpression call(FunctionManager functionManager, String name, Type returnType, List<RowExpression> arguments) { FunctionHandle functionHandle = functionManager.lookupFunction(name, fromTypes(arguments.stream().map(RowExpression::getType).collect(toImmutableList()))); return call(name, functionHandle, returnType, arguments); }
Source:
START public static Optional<HashComputation> computeHash(Iterable<VariableReferenceExpression> fields, FunctionManager functionManager) END { requireNonNull(fields, "fields is null"); List<VariableReferenceExpression> variables = ImmutableList.copyOf(fields); if (variables.isEmpty()) { return Optional.empty(); } return Optional.of(new HashComputation(fields, functionManager)); } this could be private

Target:
private static Optional<HashComputation> computeHash(Iterable<VariableReferenceExpression> fields, FunctionManager functionManager) { requireNonNull(fields, "fields is null"); List<VariableReferenceExpression> variables = ImmutableList.copyOf(fields); if (variables.isEmpty()) { return Optional.empty(); } return Optional.of(new HashComputation(fields, functionManager)); }
Source:
private HashComputation(Iterable<VariableReferenceExpression> fields, FunctionManager functionManager) { requireNonNull(fields, "fields is null"); requireNonNull(functionManager, "functionManager is null"); this.fields = ImmutableList.copyOf(fields); START this.functionManager = functionManager; END checkArgument(!this.fields.isEmpty(), "fields can not be empty"); } inline requireNonNull with this.

Target:
private HashComputation(Iterable<VariableReferenceExpression> fields, FunctionManager functionManager) { requireNonNull(fields, "fields is null"); this.fields = ImmutableList.copyOf(fields); this.functionManager = requireNonNull(functionManager, "functionManager is null"); checkArgument(!this.fields.isEmpty(), "fields can not be empty"); }
Source:
private Function<RowExpression, Optional<RowExpression>> translateGroupIdVariables(GroupIdNode node) { return variable -> { checkArgument(variable instanceof VariableReferenceExpression, "Expect VariableReferenceExpression"); if (node.getAggregationArguments().contains(variable)) { return Optional.of(variable); START } END if (node.getCommonGroupingColumns().contains(variable)) { return Optional.of((node.getGroupingColumns().get(variable))); } return Optional.empty(); }; } @highker , @hellium01 , would you mind also taking a look at `AddExchange` ? -- it's basically a template parameter change.

Target:
private Function<VariableReferenceExpression, Optional<VariableReferenceExpression>> translateGroupIdVariables(GroupIdNode node) { return variable -> { if (node.getAggregationArguments().contains(variable)) { return Optional.of(variable); } if (node.getCommonGroupingColumns().contains(variable)) { return Optional.of((node.getGroupingColumns().get(variable))); } return Optional.empty(); }; }
Source:
START public AbstractTupleDomainFilter(boolean nullAllowed) END { this.nullAllowed = nullAllowed; } I guess what we want here is an effectively sealed class, which Java doesn't have native support for. But we can mimic it by reducing the visibility of the constructor by making this and child constructors private, and exposing factory for creation.

Target:
private AbstractTupleDomainFilter(boolean nullAllowed) { this.nullAllowed = nullAllowed; }
Source:
START private boolean verifyAggregationOrderBy(Optional<OrderingScheme> orderingScheme, Optional<OrderBy> expectedSortOrder) END { if (orderingScheme.isPresent() && expectedSortOrder.isPresent()) { return verifyAggregationOrderBy(orderingScheme.get(), expectedSortOrder.get()); } return orderingScheme.isPresent() == expectedSortOrder.isPresent(); } static?

Target:
private static boolean verifyAggregationOrderBy(Optional<OrderingScheme> orderingScheme, Optional<OrderBy> expectedSortOrder) { if (orderingScheme.isPresent() && expectedSortOrder.isPresent()) { return verifyAggregationOrderBy(orderingScheme.get(), expectedSortOrder.get()); } return orderingScheme.isPresent() == expectedSortOrder.isPresent(); }
Source:
private TableStatistics pruneStatistics(TableStatistics tableStatistics, Set<ColumnHandle> desiredColumns) { Map<ColumnHandle, ColumnStatistics> columnStatistics = tableStatistics.getColumnStatistics() START .entrySet().stream() END .filter(entry -> desiredColumns.contains(entry.getKey())) .collect(toImmutableMap(Map.Entry::getKey, Map.Entry::getValue)); return new TableStatistics(tableStatistics.getRowCount(), columnStatistics); } nit: put in on the previous line

Target:
private TableStatistics pruneStatistics(TableStatistics tableStatistics, Set<ColumnHandle> desiredColumns) { Map<ColumnHandle, ColumnStatistics> columnStatistics = tableStatistics.getColumnStatistics().entrySet().stream() .filter(entry -> desiredColumns.contains(entry.getKey())) .collect(toImmutableMap(Entry::getKey, Entry::getValue)); return new TableStatistics(tableStatistics.getRowCount(), columnStatistics); }
Source:
START FixedDoubleHistogram(FixedDoubleHistogram other) END { this(other.bucketCount, other.min, other.max); for (int i = 0; i < bucketCount; ++i) { weights[i] = other.weights[i]; } } I feel that we normally don't do this in Presto but explicitly create a new object with old parameters.

Target:
protected FixedDoubleHistogram(FixedDoubleHistogram other) { this(other.bucketCount, other.min, other.max); for (int i = 0; i < bucketCount; ++i) { weights[i] = other.weights[i]; } }
Source:
public void teardown() { closeQuietly(handle); START mySqlServer.close(); END } `closeQuietly(mySqlServer, handle)`

Target:
public void teardown() { closeQuietly(mySqlServer, handle); }
Source:
private PrestoException invalidSubfieldPath() { START return new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Invalid subfield path: '%s'", path)); END } maybe have `index` encoded in the error message to denote where it goes wrong, which I assume `toString()` is for that purpose? So it will be ```java format("Invalid subfield path: '%s'", this) ```

Target:
private PrestoException invalidSubfieldPath() { return new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Invalid subfield path: '%s'", this)); }
Source:
private void assertInvalidPath(String path) { SubfieldTokenizer tokenizer = new SubfieldTokenizer(path); try { START ImmutableList<PathElement> pathElements = Streams.stream(tokenizer).collect(toImmutableList()); END fail("Expected failure"); } catch (PrestoException e) { // this is expected assertTrue(e.getMessage().startsWith("Invalid subfield path: ")); } } nit: `Streams.stream(tokenizer).collect(toImmutableList());` is good enough. (Assignment to `pathElements` is not necessary)

Target:
private void assertInvalidPath(String path) { SubfieldTokenizer tokenizer = new SubfieldTokenizer(path); try { Streams.stream(tokenizer).collect(toImmutableList()); fail("Expected failure"); } catch (PrestoException e) { // this is expected assertTrue(e.getMessage().startsWith("Invalid subfield path: ")); } }
Source:
START private boolean isLambda(RowExpression rowExpression) END { if (isExpression(rowExpression)) { return castToExpression(rowExpression) instanceof LambdaExpression; } return rowExpression instanceof LambdaDefinitionExpression; } Can this be a static method? Another similar methods is `MultipleDistinctAggregationToMarkDistinct#toSymbol`. Should they be put in a Util class? @highker

Target:
private static boolean isLambda(RowExpression rowExpression) { return castToExpression(rowExpression) instanceof LambdaExpression; }
Source:
public void testNot() { assertOptimizedEquals("not true", "false"); assertOptimizedEquals("not false", "true"); assertOptimizedEquals("not null", "null"); assertOptimizedEquals("not 1=1", "false"); assertOptimizedEquals("not 1=BIGINT '1'", "false"); assertOptimizedEquals("not 1!=1", "true"); assertOptimizedEquals("not unbound_long=1", "not unbound_long=1"); assertOptimizedEquals("not unbound_long=(1+1)", "not unbound_long=2"); START assertOptimizedEquals("cast(json_parse(unbound_string) as map(varchar, varchar))", "cast(json_parse(unbound_string) as map(varchar, varchar))"); END assertOptimizedEquals("cast(json_parse(unbound_string) as array(varchar))", "cast(json_parse(unbound_string) as array(varchar))"); assertOptimizedEquals("cast(json_parse(unbound_string) as row(bigint, varchar))", "cast(json_parse(unbound_string) as row(bigint, varchar))"); } why these are in `testNot`? -- Also it doesn't seem to perform the optimization?

Target:
public void testNot() { assertOptimizedEquals("not true", "false"); assertOptimizedEquals("not false", "true"); assertOptimizedEquals("not null", "null"); assertOptimizedEquals("not 1=1", "false"); assertOptimizedEquals("not 1=BIGINT '1'", "false"); assertOptimizedEquals("not 1!=1", "true"); assertOptimizedEquals("not unbound_long=1", "not unbound_long=1"); assertOptimizedEquals("not unbound_long=(1+1)", "not unbound_long=2"); }
Source:
public static Slice createHll(@SqlType(StandardTypes.BIGINT) long value) { START HyperLogLog hll = HyperLogLog.newInstance(ApproximateCountDistinctAggregation.standardErrorToBuckets(ApproximateSetAggregation.DEFAULT_STANDARD_ERROR)); END hll.add(value); return hll.serialize(); } I would add some static imports: `HyperLogLog hll = HyperLogLog.newInstance(standardErrorToBuckets(DEFAULT_STANDARD_ERROR));`

Target:
public static Slice createHll(@SqlType(StandardTypes.BIGINT) long value) { HyperLogLog hll = HyperLogLog.newInstance(standardErrorToBuckets(DEFAULT_STANDARD_ERROR)); hll.add(value); return hll.serialize(); }
Source:
private static PlanFragment createTableScanPlanFragment(String name) { Symbol symbol = new Symbol("column"); PlanNode planNode = new TableScanNode( new PlanNodeId(name), new TableHandle( new ConnectorId("test"), new TestingTableHandle(), TestingTransactionHandle.create(), START Optional.of(TestingHandle.INSTANCE)), END ImmutableList.of(symbol), ImmutableMap.of(symbol, new TestingColumnHandle("column"))); return createFragment(planNode); } Same here, should we use `Optional.empty()`?

Target:
private static PlanFragment createTableScanPlanFragment(String name) { Symbol symbol = new Symbol("column"); PlanNode planNode = new TableScanNode( new PlanNodeId(name), new TableHandle( new ConnectorId("test"), new TestingTableHandle(), TestingTransactionHandle.create(), Optional.empty()), ImmutableList.of(symbol), ImmutableMap.of(symbol, new TestingColumnHandle("column"))); return createFragment(planNode); }
Source:
private PagesSerde createPagesSerdeInternal(Optional<SpillCipher> spillCipher) { Optional<Compressor> compressor; Optional<Decompressor> decompressor; if (compressionEnabled) { compressor = Optional.of(new Lz4Compressor()); decompressor = Optional.of(new Lz4Decompressor()); } else { compressor = Optional.empty(); decompressor = Optional.empty(); } START return new PagesSerde(blockEncodingSerde, compressor, decompressor, spillCipher); END } The original code style in this method is better: ``` private PagesSerde createPagesSerdeInternal(Optional<SpillCipher> spillCipher) { if (compressionEnabled) { return new PagesSerde(blockEncodingSerde, Optional.of(new Lz4Compressor()), Optional.of(new Lz4Decompressor()), spillCipher); } return new PagesSerde(blockEncodingSerde, Optional.empty(), Optional.empty(), spillCipher); } ```

Target:
private PagesSerde createPagesSerdeInternal(Optional<SpillCipher> spillCipher) { if (compressionEnabled) { return new PagesSerde(blockEncodingSerde, Optional.of(new Lz4Compressor()), Optional.of(new Lz4Decompressor()), spillCipher); } return new PagesSerde(blockEncodingSerde, Optional.empty(), Optional.empty(), spillCipher); }
Source:
private SecretKey verifyKeyNotDestroyed() { SecretKey key = this.key; if (key == null || key.isDestroyed()) { throw new PrestoException(GENERIC_INTERNAL_ERROR, "Spill cipher already destroyed"); } START else { END return key; } } drop unnecessary `else`. ``` private SecretKey verifyKeyNotDestroyed() { SecretKey key = this.key; if (key == null || key.isDestroyed()) { throw new PrestoException(GENERIC_INTERNAL_ERROR, "Spill cipher already destroyed"); } return key; } ```

Target:
private SecretKey verifyKeyNotDestroyed() { SecretKey key = this.key; if (key == null) { throw new PrestoException(GENERIC_INTERNAL_ERROR, "Spill cipher already destroyed"); } return key; }
Source:
private SecretKey verifyKeyNotDestroyed() { SecretKey key = this.key; START if (key == null || key.isDestroyed()) { END throw new PrestoException(GENERIC_INTERNAL_ERROR, "Spill cipher already destroyed"); } return key; } Are you expecting destroy() to be called concurrently with this method? If so, the value of `key` visible to different threads may not be the same and the value of key.isDestroyed() may change after this check.

Target:
private SecretKey verifyKeyNotDestroyed() { SecretKey key = this.key; if (key == null) { throw new PrestoException(GENERIC_INTERNAL_ERROR, "Spill cipher already destroyed"); } return key; }
Source:
START long adjustValue(int offsetIndex, long value) END { if (inDictionaryStream != null) { return inDictionaryFlags[offsetIndex] ? dictionary[(int) value] : value; } return dictionary[(int) value]; } private

Target:
private long adjustValue(int offsetIndex, long value) { if (inDictionaryStream != null) { return inDictionaryFlags[offsetIndex] ? dictionary[(int) value] : value; } return dictionary[(int) value]; }
Source:
protected void ensureValuesCapacity(int capacity, boolean includeNulls) { START if (values == null) { END values = new int[capacity]; } else if (values == null || values.length < capacity) { values = resize(values, capacity); if (valueIsNull != null) { valueIsNull = resize(valueIsNull, capacity); } } if (includeNulls && valueIsNull == null) { valueIsNull = new boolean[values.length]; } } this check should be removed now

Target:
protected void ensureValuesCapacity(int capacity, boolean includeNulls) { if (values == null || values.length < capacity) { values = resize(values, capacity); if (valueIsNull != null) { valueIsNull = resize(valueIsNull, capacity); } } if (includeNulls && valueIsNull == null) { valueIsNull = new boolean[values.length]; } }
Source:
START int adjustValue(long value, int offsetIndex) END { if (inDictionaryStream != null && !inDictionaryFlags[offsetIndex]) { return toIntExact(value + stripeDictionarySize); } return toIntExact(value); } adjustValue method needs to be marked as `private`

Target:
private int adjustValue(long value, int offsetIndex) { if (inDictionaryStream != null && !inDictionaryFlags[offsetIndex]) { return toIntExact(value + stripeDictionarySize); } return toIntExact(value); }
Source:
public void testLength() { START double length = 4350866.6362; END // Empty linestring returns null assertLength("LINESTRING EMPTY", null); // Linestring with one point has length 0 assertLength("LINESTRING (0 0)", 0.0); // Linestring with only one distinct point has length 0 assertLength("LINESTRING (0 0, 0 0, 0 0)", 0.0); // ST_Length is equivalent to sums of ST_DISTANCE between points in the LineString assertLength("LINESTRING (-71.05 42.36, -87.62 41.87, -122.41 37.77)", length); // Linestring has same length as its reverse assertLength("LINESTRING (-122.41 37.77, -87.62 41.87, -71.05 42.36)", length); // Path north pole -> south pole -> north pole should be roughly the circumference of the Earth assertLength("LINESTRING (0.0 90.0, 0.0 -90.0, 0.0 90.0)", 4.003e7); // Empty multi-linestring returns null assertLength("MULTILINESTRING (EMPTY)", null); // Multi-linestring with one path is equivalent to a single linestring assertLength("MULTILINESTRING ((-71.05 42.36, -87.62 41.87, -122.41 37.77))", length); // Multi-linestring with two disjoint paths has length equal to sum of lengths of lines assertLength("MULTILINESTRING ((-71.05 42.36, -87.62 41.87, -122.41 37.77), (-73.05 42.36, -89.62 41.87, -124.41 37.77))", 2 * length); // Multi-linestring with adjacent paths is equivalent to a single linestring assertLength("MULTILINESTRING ((-71.05 42.36, -87.62 41.87), (-87.62 41.87, -122.41 37.77))", length); } nit: move this variable closer to its first usage

Target:
public void testLength() { // Empty linestring returns null assertLength("LINESTRING EMPTY", null); // Linestring with one point has length 0 assertLength("LINESTRING (0 0)", 0.0); // Linestring with only one distinct point has length 0 assertLength("LINESTRING (0 0, 0 0, 0 0)", 0.0); double length = 4350866.6362; // ST_Length is equivalent to sums of ST_DISTANCE between points in the LineString assertLength("LINESTRING (-71.05 42.36, -87.62 41.87, -122.41 37.77)", length); // Linestring has same length as its reverse assertLength("LINESTRING (-122.41 37.77, -87.62 41.87, -71.05 42.36)", length); // Path north pole -> south pole -> north pole should be roughly the circumference of the Earth assertLength("LINESTRING (0.0 90.0, 0.0 -90.0, 0.0 90.0)", 4.003e7); // Empty multi-linestring returns null assertLength("MULTILINESTRING (EMPTY)", null); // Multi-linestring with one path is equivalent to a single linestring assertLength("MULTILINESTRING ((-71.05 42.36, -87.62 41.87, -122.41 37.77))", length); // Multi-linestring with two disjoint paths has length equal to sum of lengths of lines assertLength("MULTILINESTRING ((-71.05 42.36, -87.62 41.87, -122.41 37.77), (-73.05 42.36, -89.62 41.87, -124.41 37.77))", 2 * length); // Multi-linestring with adjacent paths is equivalent to a single linestring assertLength("MULTILINESTRING ((-71.05 42.36, -87.62 41.87), (-87.62 41.87, -122.41 37.77))", length); }
Source:
public FunctionHandle lookupFunction(QualifiedName name, List<TypeSignatureProvider> parameterTypes) { START return staticFunctionNamespace.resolveFunction(name, parameterTypes); END } Call `lookupFunction` here.

Target:
public FunctionHandle lookupFunction(QualifiedName name, List<TypeSignatureProvider> parameterTypes) { return staticFunctionNamespace.lookupFunction(name, parameterTypes); }
Source:
public SimpleFilterProjectSemiJoinStatsRule(StatsNormalizer normalizer, FilterStatsCalculator filterStatsCalculator, FunctionManager functionManager) { super(normalizer); this.filterStatsCalculator = requireNonNull(filterStatsCalculator, "filterStatsCalculator can not be null"); START this.logicalRowExpressions = new LogicalRowExpressions(requireNonNull(functionManager, "functionManager can not be null")); END this.functionResolution = new StandardFunctionResolution(functionManager); } Nit: Move requireNonNull in out of this statement.

Target:
public SimpleFilterProjectSemiJoinStatsRule(StatsNormalizer normalizer, FilterStatsCalculator filterStatsCalculator, FunctionManager functionManager) { super(normalizer); this.filterStatsCalculator = requireNonNull(filterStatsCalculator, "filterStatsCalculator can not be null"); requireNonNull(functionManager, "functionManager can not be null"); this.logicalRowExpressions = new LogicalRowExpressions(functionManager); this.functionResolution = new StandardFunctionResolution(functionManager); }
Source:
public void notifyAllLifespansFinishedExecution() { checkState(groupedExecution); state = State.FINISHED; splitSource.close(); START whenFinishedOrNewLifespanAdded.set(null); END } What does "finished" mean in `whenFinishedOrNewLifespanAdded`? It seems to mean scheduling finished for ungrouped execution and execution finished for grouped execution?

Target:
public synchronized void notifyAllLifespansFinishedExecution() { checkState(groupedExecution); state = State.FINISHED; splitSource.close(); whenFinishedOrNewLifespanAdded.set(null); }
Source:
public List<Type> getTypeParameters() { List<Type> parameters = new ArrayList<>(argumentTypes.size() + 1); START argumentTypes.stream() END .forEach(parameters::add); parameters.add(returnType); return unmodifiableList(parameters); } same line

Target:
public List<Type> getTypeParameters() { List<Type> parameters = new ArrayList<>(argumentTypes.size() + 1); parameters.addAll(argumentTypes); parameters.add(returnType); return unmodifiableList(parameters); }
Source:
ChecksumResult(long rowCount, Map<String, Object> checksums) { for (Object checksum : checksums.values()) { checkArgument( checksum == null || checksum instanceof Long || checksum instanceof Double || checksum instanceof SqlVarbinary, "checksum can only be one of (null, Long, Double, SqlVarbinary), found %s", checksum == null ? "null" : checksum.getClass().getName()); } this.rowCount = rowCount; START this.checksums = unmodifiableMap(requireNonNull(checksums, "checksums is null")); END } `requireNonNull(checksums)` should go to the top because we would have already accessed it in `checksum.values()`

Target:
ChecksumResult(long rowCount, Map<String, Object> checksums) { this.rowCount = rowCount; this.checksums = unmodifiableMap(requireNonNull(checksums, "checksums is null")); for (Object checksum : checksums.values()) { checkArgument( checksum == null || checksum instanceof Long || checksum instanceof Double || checksum instanceof SqlVarbinary, "checksum can only be one of (null, Long, Double, SqlVarbinary), found %s", checksum == null ? "null" : checksum.getClass().getName()); } }
Source:
public void testCastOperatorsExistForCoercions() { Set<Type> types = getStandardPrimitiveTypes(); for (Type sourceType : types) { for (Type resultType : types) { if (typeRegistry.canCoerce(sourceType, resultType) && sourceType != UNKNOWN && resultType != UNKNOWN) { try { START functionManager.lookupCast(CastType.CAST, sourceType.getTypeSignature(), resultType.getTypeSignature()); END } catch (OperatorNotFoundException e) { fail(format("'%s' -> '%s' coercion exists but there is no cast operator", sourceType, resultType)); } } } } } Probably static import?

Target:
public void testCastOperatorsExistForCoercions() { Set<Type> types = getStandardPrimitiveTypes(); for (Type sourceType : types) { for (Type resultType : types) { if (typeRegistry.canCoerce(sourceType, resultType) && sourceType != UNKNOWN && resultType != UNKNOWN) { try { functionManager.lookupCast(CAST, sourceType.getTypeSignature(), resultType.getTypeSignature()); } catch (OperatorNotFoundException e) { fail(format("'%s' -> '%s' coercion exists but there is no cast operator", sourceType, resultType)); } } } } }
Source:
START static List<HiveSplit> drainSplits(HiveSplitSource source) END throws Exception { ImmutableList.Builder<HiveSplit> splits = ImmutableList.builder(); while (!source.isFinished()) { source.getNextBatch(NOT_PARTITIONED, 100).get() .getSplits().stream() .map(HiveSplit.class::cast) .forEach(splits::add); } return splits.build(); } Usually we just put tests in `TestBackgroundHiveSplitLoaderForBucketedTables.java` into this `TestBackgroundHiveSplitLoader.java` class. In this specific case, I understand the tests and other helper methods in `TestBackgroundHiveSplitLoaderForBucketedTables.java` are very specific and may not suit for general purposed `TestBackgroundHiveSplitLoader.java`. But a preferred approach might just be refactoring these methods into a helper class like `HvieSplitTestHelper` (this name is just made up)

Target:
private static List<HiveSplit> drainSplits(HiveSplitSource source) throws Exception { ImmutableList.Builder<HiveSplit> splits = ImmutableList.builder(); while (!source.isFinished()) { source.getNextBatch(NOT_PARTITIONED, 100).get() .getSplits().stream() .map(HiveSplit.class::cast) .forEach(splits::add); } return splits.build(); }
Source:
START public Plan getLogicalPlan(Session session, Statement statement, List<Expression> parameters, PlanNodeIdAllocator idAllocator, WarningCollector warningCollector) END { // analyze statement Analysis analysis = analyze(session, statement, parameters, warningCollector); // plan statement LogicalPlanner logicalPlanner = new LogicalPlanner(session, planOptimizers, idAllocator, metadata, sqlParser, statsCalculator, costCalculator, warningCollector); return logicalPlanner.plan(analysis); } It might be better to put `idAllocator` as the last parameter since we have a default value from overloaded `getLogicalPlan`.

Target:
public Plan getLogicalPlan(Session session, Statement statement, List<Expression> parameters, PlanNodeIdAllocator idAllocator, WarningCollector warningCollector) { // analyze statement Analysis analysis = analyze(session, statement, parameters, warningCollector); // plan statement LogicalPlanner logicalPlanner = new LogicalPlanner(true, session, planOptimizers, idAllocator, metadata, sqlParser, statsCalculator, costCalculator, warningCollector); return logicalPlanner.plan(analysis); }
Source:
public LocalMemoryManager(NodeMemoryConfig config) { requireNonNull(config, "config is null"); START configureMemoryPools(config, Runtime.getRuntime().maxMemory()); END } What do you think about using a telescoping constructor since one is a prefix of the other? ``` @Inject public LocalMemoryManager(NodeMemoryConfig config) { this(config, Runtime.getRuntime().maxMemory()); } ```

Target:
public LocalMemoryManager(NodeMemoryConfig config) { this(config, Runtime.getRuntime().maxMemory()); }
Source:
public static Boolean equal(@SqlType(GEOMETRY_TYPE_NAME) Slice left, @SqlType(GEOMETRY_TYPE_NAME) Slice right) { START if (left == null) { END return right == null; } if (right == null) { return false; } return deserialize(left).Equals(deserialize(right)); } No need for the explicit null checks here. The parameters are not marked as `@SqlNullable`, so the engine will apply the default semantics when null is found

Target:
public static Boolean equal(@SqlType(GEOMETRY_TYPE_NAME) Slice left, @SqlType(GEOMETRY_TYPE_NAME) Slice right) { return deserialize(left).Equals(deserialize(right)); }
Source:
public void enqueue(Lifespan lifespan, int partitionNumber, List<SerializedPage> pages) { START requireNonNull(lifespan, "lifespan is null"); END checkState(partitionNumber == 0, "Expected partition number to be zero"); checkState(lifespanCompletionCallback != null, "lifespanCompletionCallback has to be set before starting enqueue data"); enqueue(lifespan, pages); } Ditto about checks. I don't think we need to duplicate them.

Target:
public void enqueue(Lifespan lifespan, int partitionNumber, List<SerializedPage> pages) { checkState(partitionNumber == 0, "Expected partition number to be zero"); enqueue(lifespan, pages); }
Source:
public void setNoMorePagesForLifespan(Lifespan lifespan) { START noMorePagesForLifespan.add(requireNonNull(lifespan, "lifespan is null")); END } nit: move the null check to previous line (for readability)

Target:
public void setNoMorePagesForLifespan(Lifespan lifespan) { requireNonNull(lifespan, "lifespan is null"); noMorePagesForLifespan.add(lifespan); }
Source:
static EquatableValueSet of(Type type, Object first, Object... rest) { START LinkedHashSet<ValueEntry> set = new LinkedHashSet<>(rest.length + 1); END set.add(ValueEntry.create(type, first)); for (Object value : rest) { set.add(ValueEntry.create(type, value)); } return new EquatableValueSet(type, true, set); } Keep the declaration as `HashSet`

Target:
static EquatableValueSet of(Type type, Object first, Object... rest) { HashSet<ValueEntry> set = new LinkedHashSet<>(rest.length + 1); set.add(ValueEntry.create(type, first)); for (Object value : rest) { set.add(ValueEntry.create(type, value)); } return new EquatableValueSet(type, true, set); }
Source:
private LocationHandle createLocationHandle(HdfsContext context, ConnectorSession session, Path targetPath, boolean isExistingTable) { if (shouldUseTemporaryDirectory(session, context, targetPath)) { Path writePath = createTemporaryPath(session, context, hdfsEnvironment, targetPath); return new LocationHandle(targetPath, writePath, isExistingTable, STAGE_AND_MOVE_TO_TARGET_DIRECTORY); } START else { END return new LocationHandle(targetPath, targetPath, isExistingTable, isExistingTable ? DIRECT_TO_TARGET_EXISTING_DIRECTORY : DIRECT_TO_TARGET_NEW_DIRECTORY); } } I would do ```java else if (isExistingTable) { return new LocationHandle(targetPath, targetPath, true, DIRECT_TO_TARGET_EXISTING_DIRECTORY); } else { return new LocationHandle(targetPath, targetPath, false, DIRECT_TO_TARGET_NEW_DIRECTORY); } ``` so there are 3 branches, and easier to follow (at least for myself :) )

Target:
private LocationHandle createLocationHandle(HdfsContext context, ConnectorSession session, Path targetPath, boolean isExistingTable) { if (shouldUseTemporaryDirectory(session, context, targetPath)) { Path writePath = createTemporaryPath(session, context, hdfsEnvironment, targetPath); return new LocationHandle(targetPath, writePath, isExistingTable, STAGE_AND_MOVE_TO_TARGET_DIRECTORY); } if (isExistingTable) { return new LocationHandle(targetPath, targetPath, true, DIRECT_TO_TARGET_EXISTING_DIRECTORY); } return new LocationHandle(targetPath, targetPath, false, DIRECT_TO_TARGET_NEW_DIRECTORY); }
Source:
public static BlockBuilder quantileBoundsToArray(double lowerQuantile, double upperQuantile) { START QuantileDigestFunctions.validateQuantileRange(lowerQuantile, upperQuantile); END BlockBuilder blockBuilder = DOUBLE.createBlockBuilder(null, 2); DOUBLE.writeDouble(blockBuilder, lowerQuantile); DOUBLE.writeDouble(blockBuilder, upperQuantile); return blockBuilder; } You can static import `validateQuantileRange`

Target:
public static BlockBuilder quantileBoundsToArray(double lowerQuantile, double upperQuantile) { validateQuantileRange(lowerQuantile, upperQuantile); BlockBuilder blockBuilder = DOUBLE.createBlockBuilder(null, 2); DOUBLE.writeDouble(blockBuilder, lowerQuantile); DOUBLE.writeDouble(blockBuilder, upperQuantile); return blockBuilder; }
Source:
public static Double digestToTruncatedMean(QuantileDigest digest, List<Long> bounds, QuantileDigest.MiddleFunction middleFunction) { if (bounds.get(0).equals(bounds.get(1))) { return null; } START else { END return digest.getHistogram(bounds, middleFunction).get(1).getMean(); } } I would remove the else and just return.

Target:
public static Double digestToTruncatedMean(QuantileDigest digest, List<Long> bounds, QuantileDigest.MiddleFunction middleFunction) { if (bounds.get(0).equals(bounds.get(1))) { return null; } return digest.getHistogram(bounds, middleFunction).get(1).getMean(); }
Source:
public static void input(@AggregationState DigestAndPercentileArrayState state, @SqlType(StandardTypes.DOUBLE) double value, @SqlType(StandardTypes.DOUBLE) double lowerQuantile, @SqlType(StandardTypes.DOUBLE) double upperQuantile) { ApproximateDoublePercentileArrayAggregations.input(state, value, START ApproximateLongTruncatedMeanAggregations.quantileBoundsToArray(lowerQuantile, upperQuantile)); END } I would static import `quantileBoundsToArray`.

Target:
public static void input( @AggregationState DigestAndPercentileArrayState state, @SqlType(StandardTypes.DOUBLE) double value, @SqlType(StandardTypes.DOUBLE) double lowerQuantile, @SqlType(StandardTypes.DOUBLE) double upperQuantile) { ApproximateDoublePercentileArrayAggregations.input(state, value, quantileBoundsToArray(lowerQuantile, upperQuantile)); }
Source:
public Block split(@TypeParameter("map<varchar,varchar>") Type mapType, @SqlType(StandardTypes.VARCHAR) Slice string, @SqlType(StandardTypes.VARCHAR) Slice entryDelimiter, @SqlType(StandardTypes.VARCHAR) Slice keyValueDelimiter) { START return splitToMap(pageBuilder, mapType, string, entryDelimiter, keyValueDelimiter, null); END } Why don't you use `Optional` instead of null here?

Target:
public Block split( @TypeParameter("map<varchar,varchar>") Type mapType, @SqlType(StandardTypes.VARCHAR) Slice string, @SqlType(StandardTypes.VARCHAR) Slice entryDelimiter, @SqlType(StandardTypes.VARCHAR) Slice keyValueDelimiter) { return splitToMap(pageBuilder, mapType, string, entryDelimiter, keyValueDelimiter, Optional.empty()); }
Source:
public Block split( @TypeParameter("map<varchar,varchar>") Type mapType, @SqlType(StandardTypes.VARCHAR) Slice string, @SqlType(StandardTypes.VARCHAR) Slice entryDelimiter, @SqlType(StandardTypes.VARCHAR) Slice keyValueDelimiter, @SqlType("function(varchar, varchar, varchar, varchar)") DuplicateKeyResolutionLambda function) START { END return splitToMap(pageBuilder, mapType, string, entryDelimiter, keyValueDelimiter, function); } ditto about adding `requireNonNull`.

Target:
public Block split( @TypeParameter("map<varchar,varchar>") Type mapType, @SqlType(StandardTypes.VARCHAR) Slice string, @SqlType(StandardTypes.VARCHAR) Slice entryDelimiter, @SqlType(StandardTypes.VARCHAR) Slice keyValueDelimiter, @SqlType("function(varchar, varchar, varchar, varchar)") DuplicateKeyResolutionLambda function) { return splitToMap(pageBuilder, mapType, string, entryDelimiter, keyValueDelimiter, Optional.of(function)); }
Source:
START public static void assertWarnings(QueryRunner queryRunner, Session session, @Language("SQL") String sql, List<WarningCode> expectedWarnings) END { Set<WarningCode> warnings = queryRunner.execute(session, sql).getWarnings().stream() .map(PrestoWarning::getWarningCode) .collect(toImmutableSet()); for (WarningCode warningCode : expectedWarnings) { if (!warnings.contains(warningCode)) { fail("Expected warning: " + warningCode); } } } nit: does this need to be public?

Target:
private static void assertWarnings(QueryRunner queryRunner, Session session, @Language("SQL") String sql, List<WarningCode> expectedWarnings) { Set<WarningCode> warnings = queryRunner.execute(session, sql).getWarnings().stream() .map(PrestoWarning::getWarningCode) .collect(toImmutableSet()); for (WarningCode warningCode : expectedWarnings) { if (!warnings.contains(warningCode)) { fail("Expected warning: " + warningCode); } } }
Source:
public WriteContext init(final Configuration configuration) { START schema = MessageTypeParser.parseMessageType(configuration.get("parquet.hive.schema")); END return new WriteContext(schema, new HashMap<>()); } static import `parseMessageType`

Target:
public WriteContext init(final Configuration configuration) { schema = parseMessageType(configuration.get("parquet.hive.schema")); return new WriteContext(schema, new HashMap<>()); }
Source:
private static void assertTimestampCorrect(String timestampString) { Timestamp timestamp = Timestamp.valueOf(timestampString); NanoTime nanoTime = NanoTimeUtils.getNanoTime(timestamp, false); ByteBuffer buffer = ByteBuffer.wrap(nanoTime.toBinary().getBytes()); START long decodedTimestampMillis = getTimestampMillis(Binary.fromConstantByteBuffer(buffer)); END assertEquals(decodedTimestampMillis, timestamp.getTime()); } static import `fromConstantByteBuffer`.

Target:
private static void assertTimestampCorrect(String timestampString) { Timestamp timestamp = Timestamp.valueOf(timestampString); NanoTime nanoTime = getNanoTime(timestamp, false); ByteBuffer buffer = ByteBuffer.wrap(nanoTime.toBinary().getBytes()); long decodedTimestampMillis = getTimestampMillis(fromConstantByteBuffer(buffer)); assertEquals(decodedTimestampMillis, timestamp.getTime()); }
Source:
ObjectMetadata getS3ObjectMetadata(Path path) throws IOException { String bucketName = getBucketName(uri); String key = keyFromPath(path); ObjectMetadata s3ObjectMetadata = getS3ObjectMetadata(path, bucketName, key); if (s3ObjectMetadata == null && !key.isEmpty()) { // check if key is not a "directory" START verify(!key.endsWith(PATH_SEPARATOR), "Expected key to not end with %s", PATH_SEPARATOR); END return getS3ObjectMetadata(path, bucketName, key + PATH_SEPARATOR); } return s3ObjectMetadata; } We don't need this check, since `keyFromPath()` is guaranteed never to return a key with a trailing slash. `URI` guarantees that paths are normalized (no double slashes) and `keyFromPath()` strips off any trailing slash. Having this check here is confusing because the reader has to puzzle over how this would be possible.

Target:
ObjectMetadata getS3ObjectMetadata(Path path) throws IOException { String bucketName = getBucketName(uri); String key = keyFromPath(path); ObjectMetadata s3ObjectMetadata = getS3ObjectMetadata(path, bucketName, key); if (s3ObjectMetadata == null && !key.isEmpty()) { return getS3ObjectMetadata(path, bucketName, key + PATH_SEPARATOR); } return s3ObjectMetadata; }
Source:
public Object randomValue() { START return (boolean) ThreadLocalRandom.current().nextBoolean(); END } nit: redundant casting

Target:
public Object randomValue() { return ThreadLocalRandom.current().nextBoolean(); }
Source:
public Object[][] inputSequences() START { END return new Object[][] { {true}, {false}, {true, true, true}, {false, false, false}, {true, false, true, false}, }; } add `{},`

Target:
public Object[][] inputSequences() { return new Object[][] { {true}, {false}, {true, false}, {true, true, true}, {false, false, false}, {true, false, true, false}, }; }
Source:
public void testNoInput() { START List<Object> booleanList = new ArrayList<>(); END assertCount(ImmutableList.of(), 0, 0); } unused variable

Target:
public void testNoInput() { assertCount(ImmutableList.of(), 0, 0); }
Source:
public Builder setBaseDataDir(Optional<Path> baseDataDir) { requireNonNull(baseDataDir, "baseDataDir is null"); START this.baseDataDir = baseDataDir; END return this; } @yingsu00 This can be shorted to `this.baseDataDir = requireNonNull(baseDataDir, "baseDataDir is null");`

Target:
public Builder setBaseDataDir(Optional<Path> baseDataDir) { this.baseDataDir = requireNonNull(baseDataDir, "baseDataDir is null"); return this; }
Source:
private RetryDriver retry() { return RetryDriver.retry() .exponentialBackoff( START this.minBackoffDelay, END this.maxBackoffDelay, this.maxRetryTime, this.scaleFactor) .maxAttempts(this.maxRetries + 1) .exceptionMapper(exceptionMapper) .stopOn(PrestoException.class); } Please remove `this` from these field references as they aren't needed. We only use them when required, or sometimes for all assignments in the constructor (to be consistent with the assignments where it is required).

Target:
private RetryDriver retry() { return RetryDriver.retry() .exponentialBackoff( minBackoffDelay, maxBackoffDelay, maxRetryTime, scaleFactor) .maxAttempts(maxRetries + 1) .exceptionMapper(exceptionMapper) .stopOn(PrestoException.class); }
Source:
public void tearDown() { try (Closer closer = Closer.create()) { // Closer is documented to mimic try-with-resource, which implies close will happen in reverse order. // first close the cients, then clear the map closer.register(clients::clear); for (Entry<String, TransportClient> entry : clients.entrySet()) { closer.register(entry.getValue()); } START closer.register(() -> executor.shutdown()); END } catch (IOException e) { throw new UncheckedIOException(e); } } closer.register(executor::shutdown);

Target:
public void tearDown() { // Closer closes the resources in reverse order. // Therefore, we first clear the clients map, then close each client. try (Closer closer = Closer.create()) { closer.register(clients::clear); for (Entry<String, TransportClient> entry : clients.entrySet()) { closer.register(entry.getValue()); } closer.register(executor::shutdown); } catch (IOException e) { throw new UncheckedIOException(e); } }
Source:
START private ConnectorTableMetadata getTableMetadata(SchemaTableName tableName) END { ElasticsearchTableDescription table = client.getTable(tableName.getSchemaName(), tableName.getTableName()); if (table == null) { return null; } return new ConnectorTableMetadata(tableName, client.getColumnMetadata(table)); } Update this method to return `Optional<ConnectorTableMetadata>`

Target:
private Optional<ConnectorTableMetadata> getTableMetadata(SchemaTableName tableName) { ElasticsearchTableDescription table = client.getTable(tableName.getSchemaName(), tableName.getTableName()); if (table == null) { return Optional.empty(); } return Optional.of(new ConnectorTableMetadata(tableName, client.getColumnMetadata(table))); }
Source:
private SearchResponse getSearchResponse(ElasticsearchQueryBuilder queryBuilder) { try { return retry() .maxAttempts(maxAttempts) .exponentialBackoff(maxRetryTime, 2.0) START .run("searchRequest", () -> { END return queryBuilder.buildScrollSearchRequest() .execute() .actionGet(requestTimeout.toMillis()); }); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RuntimeException(e); } catch (Exception e) { throw new RuntimeException(e); } } nit: you can use an expression lambda here and on L214 (remove curly braces).

Target:
private SearchResponse getSearchResponse(ElasticsearchQueryBuilder queryBuilder) { try { return retry() .maxAttempts(maxAttempts) .exponentialBackoff(maxRetryTime) .run("searchRequest", () -> queryBuilder.buildScrollSearchRequest() .execute() .actionGet(requestTimeout.toMillis())); } catch (Exception e) { throw new RuntimeException(e); } }
Source:
private List<ColumnMetadata> loadColumns(ElasticsearchTableDescription table) { if (table.getColumns().isPresent()) { return buildMetadata(table.getColumns().get()); } List<ElasticsearchColumn> columns = buildColumns(table); START return buildMetadata(columns); END } inline columns

Target:
private List<ColumnMetadata> loadColumns(ElasticsearchTableDescription table) { if (table.getColumns().isPresent()) { return buildMetadata(table.getColumns().get()); } return buildMetadata(buildColumns(table)); }
Source:
public PlanNode optimize(PlanNode plan, Session session, TypeProvider types, SymbolAllocator symbolAllocator, PlanNodeIdAllocator idAllocator, WarningCollector warningCollector) { requireNonNull(plan, "plan is null"); requireNonNull(session, "session is null"); START requireNonNull(symbolAllocator, "symbolAllocator is null"); END requireNonNull(types, "types is null"); return SimplePlanRewriter.rewriteWith(new Rewriter(), plan); } nit: Only `plan` parameter is used. I don't think you need to check anything but `plan` with the `requireNonNull`.

Target:
public PlanNode optimize(PlanNode plan, Session session, TypeProvider types, SymbolAllocator symbolAllocator, PlanNodeIdAllocator idAllocator, WarningCollector warningCollector) { requireNonNull(plan, "plan is null"); return SimplePlanRewriter.rewriteWith(new Rewriter(), plan); }
Source:
public static PlanMatchPattern semiJoin(String sourceSymbolAlias, String filteringSymbolAlias, String outputAlias, PlanMatchPattern source, PlanMatchPattern filtering) { START return node(SemiJoinNode.class, source, filtering).with(new SemiJoinMatcher(sourceSymbolAlias, filteringSymbolAlias, outputAlias, Optional.empty())); END } reuse overloaded `semiJoin` method here passing distribution type as `Optional.empty()`

Target:
public static PlanMatchPattern semiJoin(String sourceSymbolAlias, String filteringSymbolAlias, String outputAlias, PlanMatchPattern source, PlanMatchPattern filtering) { return semiJoin(sourceSymbolAlias, filteringSymbolAlias, outputAlias, Optional.empty(), source, filtering); }
Source:
public String toString() { return toStringHelper(this) .add("filteringSymbolAlias", filteringSymbolAlias) .add("sourceSymbolAlias", sourceSymbolAlias) .add("outputAlias", outputAlias) START .add("distributionType", distributionType.orElse(null)) END .toString(); } Just `distributionType`. It is somehow confusing to put `null` here. It would imply that the `distributionType` field is set to `null`, but not to `Optional.empty()`.

Target:
public String toString() { return toStringHelper(this) .add("filteringSymbolAlias", filteringSymbolAlias) .add("sourceSymbolAlias", sourceSymbolAlias) .add("outputAlias", outputAlias) .add("distributionType", distributionType) .toString(); }
Source:
public Result apply(SemiJoinNode semiJoinNode, Captures captures, Context context) { JoinDistributionType joinDistributionType = getJoinDistributionType(context.getSession()); switch (joinDistributionType) { case AUTOMATIC: return Result.ofPlanNode(getCostBasedDistributionType(semiJoinNode, context)); case PARTITIONED: return Result.ofPlanNode(semiJoinNode.withDistributionType(PARTITIONED)); case BROADCAST: return Result.ofPlanNode(semiJoinNode.withDistributionType(REPLICATED)); default: START throw new UnsupportedOperationException("Unknown join_distribution_type: " + joinDistributionType); END } } `UnsupportedOperationException` should be used when the entire operation (method) is not supported. You can use here either `IllegalArgumentException` or `VerifyException`

Target:
public Result apply(SemiJoinNode semiJoinNode, Captures captures, Context context) { JoinDistributionType joinDistributionType = getJoinDistributionType(context.getSession()); switch (joinDistributionType) { case AUTOMATIC: return Result.ofPlanNode(getCostBasedDistributionType(semiJoinNode, context)); case PARTITIONED: return Result.ofPlanNode(semiJoinNode.withDistributionType(PARTITIONED)); case BROADCAST: return Result.ofPlanNode(semiJoinNode.withDistributionType(REPLICATED)); default: throw new IllegalArgumentException("Unknown join_distribution_type: " + joinDistributionType); } }
Source:
private static int megabytes(int size) { START return toIntExact(new DataSize(size, DataSize.Unit.MEGABYTE).toBytes()); END } static import?

Target:
private static int megabytes(int size) { return toIntExact(new DataSize(size, MEGABYTE).toBytes()); }
Source:
public ToIntFunction<ConnectorSplit> getSplitBucketFunction( ConnectorTransactionHandle transactionHandle, ConnectorSession session, ConnectorPartitioningHandle partitioningHandle) { return value -> ((HiveSplit) value).getBucketNumber() START .orElseThrow(() -> new IllegalStateException("Bucket number not set in split")); END } nit: "in a split"

Target:
public ToIntFunction<ConnectorSplit> getSplitBucketFunction( ConnectorTransactionHandle transactionHandle, ConnectorSession session, ConnectorPartitioningHandle partitioningHandle) { return value -> ((HiveSplit) value).getBucketNumber() .orElseThrow(() -> new IllegalArgumentException("Bucket number not set in split")); }
Source:
public void add(Block block, int position) { requireNonNull(block, "block must not be null"); checkArgument(position >= 0, "position must be >= 0"); if (block.isNull(position)) { START if (!containsNullElement) { END containsNullElement = true; } } // null is added to both elementBlock and the blockPositionByHash hash table only once. // Adding it to elementBlock makes getBlock() being able to be called multiple times. // Adding it to hash table avoids changes in rehash() int hashPosition = getHashPositionOfElement(block, position); if (blockPositionByHash.get(hashPosition) == EMPTY_SLOT) { addNewElement(hashPosition, block, position); } } This if-statement looks redundant -- we can just set `containsNullElement` to be true.

Target:
public void add(Block block, int position) { requireNonNull(block, "block must not be null"); checkArgument(position >= 0, "position must be >= 0"); // containsNullElement flag is maintained so contains() method can have shortcut for null value if (block.isNull(position)) { containsNullElement = true; } int hashPosition = getHashPositionOfElement(block, position); if (blockPositionByHash.get(hashPosition) == EMPTY_SLOT) { addNewElement(hashPosition, block, position); } }