auphong2707/dl4se-code-review-dataset huggingface dataset:
Source:
public void serializeWithType(PlanNode value, JsonGenerator gen, SerializerProvider serializers, TypeSerializer typeSer) throws IOException { if (rootLevel) { rootLevel = false; JsonSerializer<Object> subTypeSerializer = provider.findTypedValueSerializer(value.getClass(), true, null); subTypeSerializer.serializeWithType(value, gen, serializers, typeSer); START rootLevel = true; END return; } superTypeSerializer.serializeWithType(value, gen, serializers, typeSer); } maybe a finally

Target:
public void serializeWithType(PlanNode value, JsonGenerator gen, SerializerProvider serializers, TypeSerializer typeSer) throws IOException { if (rootLevel) { rootLevel = false; try { JsonSerializer<Object> subTypeSerializer = provider.findTypedValueSerializer(value.getClass(), true, null); subTypeSerializer.serializeWithType(value, gen, serializers, typeSer); } finally { rootLevel = true; } return; } superTypeSerializer.serializeWithType(value, gen, serializers, typeSer); }
Source:
public int hashCode() { START return Objects.hash(name); END } Since you have only one argument you can do `Objects.hashCode(name)` which is more efficient.

Target:
public int hashCode() { return Objects.hashCode(name); }
Source:
START public static Optional<Principal> createPrincipal(Optional<String> principal) END { return principal.map(name -> new InternalPrincipal(name)); } Put this static method after instance methods.

Target:
public static Optional<Principal> createPrincipal(Optional<String> principal) { return principal.map(InternalPrincipal::new); }
Source:
public PipelineStatus getPipelineStatus() { START return new PipelineStatus(ImmutableList.copyOf(this.drivers)); END } Make the `PipelineStatus` constructor as dumb as possible. Put logic here instead.

Target:
public PipelineStatus getPipelineStatus() { return getPipelineStatus(ImmutableList.copyOf(drivers)); }
Source:
public static ColumnIO getArrayElementColumn(ColumnIO columnIO) { START while ((columnIO instanceof GroupColumnIO) && !columnIO.getType().isRepetition(REPEATED)) { END columnIO = ((GroupColumnIO) columnIO).getChild(0); } if (columnIO instanceof GroupColumnIO && columnIO.getType().getOriginalType() == null && ((GroupColumnIO) columnIO).getChildrenCount() == 1) { return ((GroupColumnIO) columnIO).getChild(0); } return columnIO; } parentheses around instanceof check is not necessary.

Target:
public static ColumnIO getArrayElementColumn(ColumnIO columnIO) { while (columnIO instanceof GroupColumnIO && !columnIO.getType().isRepetition(REPEATED)) { columnIO = ((GroupColumnIO) columnIO).getChild(0); } /* If array has a standard 3-level structure with middle level repeated group with a single field: * optional group my_list (LIST) { * repeated group element { * required binary str (UTF8); * }; * } */ if (columnIO instanceof GroupColumnIO && columnIO.getType().getOriginalType() == null && ((GroupColumnIO) columnIO).getChildrenCount() == 1) { return ((GroupColumnIO) columnIO).getChild(0); } /* Backward-compatibility support for 2-level arrays where a repeated field is not a group: * optional group my_list (LIST) { * repeated int32 element; * } */ return columnIO; }
Source:
public void testClientTagsBasedSelection() throws InterruptedException { InternalResourceGroupManager manager = queryRunner.getCoordinator().getResourceGroupManager().get(); START DbResourceGroupConfigurationManager dbConfigurationManager = (DbResourceGroupConfigurationManager) manager.getConfigurationManager(); END dbConfigurationManager.load(); assertResourceGroupWithClientTags(ImmutableSet.of("tag1"), "global.bi-user"); assertResourceGroupWithClientTags(ImmutableSet.of("tag1", "tag2"), "global.user-user.adhoc-user"); } I don't think `load()` call is needed here. I remember adding `load()` to make reloading of the config deterministic when we update config in the tests.

Target:
public void testClientTagsBasedSelection() throws InterruptedException { assertResourceGroupWithClientTags(ImmutableSet.of("tag1"), "global.bi-user"); assertResourceGroupWithClientTags(ImmutableSet.of("tag1", "tag2"), "global.user-user.adhoc-user"); }
Source:
public boolean isNull(int field) { START return Objects.isNull(value); END } `return value == null;` I don't see value of `Objects.isNull` over `== null`. And there's only 1 place in Presto code that this is used.

Target:
public boolean isNull(int field) { return value == null; }
Source:
public ConnectorIndex getIndex( ConnectorTransactionHandle transactionHandle, ConnectorSession session, ConnectorIndexHandle indexHandle, List<ColumnHandle> lookupSchema, List<ColumnHandle> outputSchema) { START ThriftIndexHandle thriftIndexHandle = (ThriftIndexHandle) indexHandle; END return new ThriftConnectorIndex(clientProvider, thriftIndexHandle, lookupSchema, outputSchema, maxBytesPerResponse, lookupRequestsConcurrency); } inline this

Target:
public ConnectorIndex getIndex( ConnectorTransactionHandle transactionHandle, ConnectorSession session, ConnectorIndexHandle indexHandle, List<ColumnHandle> lookupSchema, List<ColumnHandle> outputSchema) { return new ThriftConnectorIndex(clientProvider, (ThriftIndexHandle) indexHandle, lookupSchema, outputSchema, maxBytesPerResponse, lookupRequestsConcurrency); }
Source:
public static Slice toQuadKey(@SqlType(BingTileType.NAME) long input) { START return Slices.utf8Slice(BingTile.decode(input).toQuadKey()); END } static import `utf8Slice`

Target:
public static Slice toQuadKey(@SqlType(BingTileType.NAME) long input) { return utf8Slice(BingTile.decode(input).toQuadKey()); }
Source:
public void close() { try (Closer closer = Closer.create()) { closer.register(userAggregateMemoryContext::close); closer.register(revocableAggregateMemoryContext::close); closer.register(systemAggregateMemoryContext::close); userLocalMemoryContext.setBytes(0); revocableLocalMemoryContext.setBytes(0); START systemLocalMemoryContext.setBytes(0); END } catch (IOException e) { throw new RuntimeException("Exception closing memory tracking context", e); } } Explain why local memory doesn't go in closer, whereas aggregate memory does. In addition, I vaguely remember that you intended to add `close` method to local memory context due to address race issues.

Target:
public void close() { try (Closer closer = Closer.create()) { closer.register(userAggregateMemoryContext::close); closer.register(revocableAggregateMemoryContext::close); closer.register(systemAggregateMemoryContext::close); userLocalMemoryContext.close(); revocableLocalMemoryContext.close(); systemLocalMemoryContext.close(); } catch (IOException e) { throw new RuntimeException("Exception closing memory tracking context", e); } }
Source:
START public SliceInput getDelegate() END { if (delegate == null) { delegate = requireNonNull(loader.get(), "loader returned a null stream"); verify(delegate.length() == globalLength, "loader returned stream of length %s, but length %s was expected", delegate.length(), globalLength); delegate.setPosition(initialPosition); } return delegate; } This looks like it should be private

Target:
private SliceInput getDelegate() { if (delegate == null) { delegate = requireNonNull(loader.get(), "loader returned a null stream"); verify(delegate.length() == globalLength, "loader returned stream of length %s, but length %s was expected", delegate.length(), globalLength); delegate.setPosition(initialPosition); } return delegate; }
Source:
public boolean process() { checkState(!finished); finished = prerequisite.process(); if (!finished) { return false; } START finished = true; END result = transform.apply(prerequisite.getResult()); return true; } This line is no-op

Target:
public boolean process() { checkState(!finished); finished = prerequisite.process(); if (!finished) { return false; } result = transform.apply(prerequisite.getResult()); return true; }
Source:
public List<Type> getTypes() { START return (List<Type>) localExchangeFactory.getTypes(); END } Change the getter so you don't need the cast

Target:
public List<Type> getTypes() { return localExchangeFactory.getTypes(); }
Source:
private HashUtil() { START throw new UnsupportedOperationException(); END } No need for this exception. This class cannot be instantiated anyway.

Target:
private HashUtil() { }
Source:
START public GroupedHistogramState( END Type keyType, int expectedEntriesCount) { valuesBlockBuilder = keyType.createBlockBuilder(null, computeBucketCount(expectedEntriesCount, GroupedTypedHistogram.MAX_FILL_RATIO)); typedHistogram = new GroupedTypedHistogram(keyType, expectedEntriesCount, valuesBlockBuilder); } No need to break across multiple lines

Target:
public GroupedHistogramState(Type keyType, int expectedEntriesCount) { typedHistogram = new GroupedTypedHistogram(keyType, expectedEntriesCount); }
Source:
public static Slice stPoint(@SqlType(StandardTypes.DOUBLE) double x, @SqlType(StandardTypes.DOUBLE) double y) { START OGCGeometry geometry = getGeometry(OGCGeometry.createFromEsriGeometry(new Point(x, y), null), "ST_Point", POINT); END return serialize(geometry); } getGeometry call here is redundant, because OGCGeometry.createFromEsriGeometry(new Point(x, y), null) can't return anything other than point. Perhaps, remove it.

Target:
public static Slice stPoint(@SqlType(StandardTypes.DOUBLE) double x, @SqlType(StandardTypes.DOUBLE) double y) { OGCGeometry geometry = createFromEsriGeometry(new Point(x, y), null); return serialize(geometry); }
Source:
public static Slice stPoint(@SqlType(StandardTypes.DOUBLE) double x, @SqlType(StandardTypes.DOUBLE) double y) { START OGCGeometry geometry = OGCGeometry.createFromEsriGeometry(new Point(x, y), null); END return serialize(geometry); } static import `createFromEsriGeometry`

Target:
public static Slice stPoint(@SqlType(StandardTypes.DOUBLE) double x, @SqlType(StandardTypes.DOUBLE) double y) { OGCGeometry geometry = createFromEsriGeometry(new Point(x, y), null); return serialize(geometry); }
Source:
public void addValue(long value) { nonNullValueCount++; minimum = Math.min(value, minimum); maximum = Math.max(value, maximum); START try { END sum = addExact(sum, value); } catch (ArithmeticException e) { overflow = true; } } I would only execute this try if `overflow` is not already set. Given the way this code is currently written, I'm concerned about repeated throws in a tight loop.

Target:
public void addValue(long value) { nonNullValueCount++; minimum = Math.min(value, minimum); maximum = Math.max(value, maximum); if (!overflow) { try { sum = addExact(sum, value); } catch (ArithmeticException e) { overflow = true; } } }
Source:
START protected TestMemorySmoke() END { super(MemoryQueryRunner::createQueryRunner); } This should technically be public since it's a public class for TestNG to execute. (it apparently bypasses privilege checks using reflection

Target:
public TestMemorySmoke() { super(MemoryQueryRunner::createQueryRunner); }
Source:
public synchronized Map<SchemaTableName, ConnectorViewDefinition> getViews(ConnectorSession session, SchemaTablePrefix prefix) { return views.entrySet().stream() .filter(entry -> prefix.matches(entry.getKey())) START .collect(ImmutableMap.toImmutableMap( END Map.Entry::getKey, entry -> new ConnectorViewDefinition(entry.getKey(), Optional.empty(), entry.getValue()))); } Static import

Target:
public synchronized Map<SchemaTableName, ConnectorViewDefinition> getViews(ConnectorSession session, SchemaTablePrefix prefix) { return views.entrySet().stream() .filter(entry -> prefix.matches(entry.getKey())) .collect(toImmutableMap( Map.Entry::getKey, entry -> new ConnectorViewDefinition(entry.getKey(), Optional.empty(), entry.getValue()))); }
Source:
START static Expression rewrite(Expression expression, Rule.Context context) END { return LambdaCaptureDesugaringRewriter.rewrite(expression, context.getSymbolAllocator().getTypes(), context.getSymbolAllocator()); } why not private?

Target:
private static Expression rewrite(Expression expression, Rule.Context context) { return LambdaCaptureDesugaringRewriter.rewrite(expression, context.getSymbolAllocator().getTypes(), context.getSymbolAllocator()); }
Source:
public static Expression rewrite(Expression expression, Map<Symbol, Type> symbolTypes, SymbolAllocator symbolAllocator) { checkState(!(expression instanceof GroupingOperation), "GroupingOperation should have been re-written to a FunctionCall before execution"); START if (expression instanceof SymbolReference) { END return expression; } return ExpressionTreeRewriter.rewriteWith(new Visitor(symbolTypes, symbolAllocator), expression, new Context()); } is this needed? not much will be executed for such expression

Target:
public static Expression rewrite(Expression expression, Map<Symbol, Type> symbolTypes, SymbolAllocator symbolAllocator) { return ExpressionTreeRewriter.rewriteWith(new Visitor(symbolTypes, symbolAllocator), expression, new Context()); }
Source:
public static Expression rewrite(Expression expression, Session session, Metadata metadata, SqlParser sqlParser, SymbolAllocator symbolAllocator) { requireNonNull(metadata, "metadata is null"); requireNonNull(sqlParser, "sqlParser is null"); START checkArgument(!(expression instanceof GroupingOperation), "GroupingOperation should have been re-written to a FunctionCall before execution"); END if (expression instanceof SymbolReference) { return expression; } Map<NodeRef<Expression>, Type> expressionTypes = getExpressionTypes(session, metadata, sqlParser, symbolAllocator.getTypes(), expression, emptyList()); // parameters already replaced return rewrite(expression, expressionTypes); } Why grouping causes issues? I thought that it is irrelevant.

Target:
public static Expression rewrite(Expression expression, Session session, Metadata metadata, SqlParser sqlParser, SymbolAllocator symbolAllocator) { requireNonNull(metadata, "metadata is null"); requireNonNull(sqlParser, "sqlParser is null"); if (expression instanceof SymbolReference) { return expression; } Map<NodeRef<Expression>, Type> expressionTypes = getExpressionTypes(session, metadata, sqlParser, symbolAllocator.getTypes(), expression, emptyList()); // parameters already replaced return rewrite(expression, expressionTypes); }
Source:
public InputStream handleException(Request request, Exception exception) START throws RuntimeException END { throw new RuntimeException("Request to worker failed", exception); } These `throws` shouldn't be necessary for an unchecked exception

Target:
public InputStream handleException(Request request, Exception exception) { throw new RuntimeException("Request to worker failed", exception); }
Source:
public void queryStopped() { START runningQueries.decrementAndGet(); END queuedQueries.decrementAndGet(); } Aren't you decrementing the `queuedQueries` twice? First the query will get queued (`queryQueued` will get called), then the query will start running (`queryStarted ` gets called and decrements), then query will finish (`queryStopped ` will get called and decrements), right?

Target:
public void queryStopped() { runningQueries.decrementAndGet(); }
Source:
START private static final Type entryType(Type keyType, Type valueType) END { return new ArrayType(new RowType(ImmutableList.of(keyType, valueType), Optional.empty())); } I don't think we use `final` for functions in our code base

Target:
private static Type entryType(Type keyType, Type valueType) { return new ArrayType(new RowType(ImmutableList.of(keyType, valueType), Optional.empty())); }
Source:
private boolean tryBuildPage() { if (pageBuilder.isEmpty()) { return false; } START if (finished || pageBuilder.isFull()) { END buildPage(); return true; } return false; } Not sure why you consult `finished` here? BTW this seems to be always `false` when this method is called. However, if you consulted `finishing` it wouldn't be good either (you would probably produce single-row pages for whole last page of the input).

Target:
private boolean tryBuildPage() { if (pageBuilder.isFull()) { buildPage(); return true; } return false; }
Source:
public void close() throws IOException { try { writeRowGroup(); output.close(); } finally { START Closer closer = Closer.create(); END closer.register(keySectionOutput::destroy); for (ColumnEncoder columnEncoder : columnEncoders) { closer.register(columnEncoder::destroy); } closer.close(); } } Consider `try (Closer closer = ...)` if you already use `Closer`. Also, this `Closer` instance could also handle the `try` section, eliminating `try-finally`. (Mind the order in which closer closes though).

Target:
public void close() throws IOException { try (Closer closer = Closer.create()) { writeRowGroup(); closer.register(output); closer.register(keySectionOutput::destroy); for (ColumnEncoder columnEncoder : columnEncoders) { closer.register(columnEncoder::destroy); } } }
Source:
public int hashCode() { START return Objects.hash(filter, leftBlocksSize, sortChannel); END } You could reorder hashing so that if follow order in equals. This helps verifying they agree. Best if they followed order of fields, but let's not change this _now_, to be on the safe side.

Target:
public int hashCode() { return Objects.hash(leftBlocksSize, filter); }
Source:
private static PagesHashStrategy pagesHashStrategy() { return new SimplePagesHashStrategy( ImmutableList.of(BIGINT), ImmutableList.of(), ImmutableList.of(ImmutableList.of(TEST_PAGE.getBlock(0))), ImmutableList.of(), Optional.empty(), START Optional.of(new SortExpressionExtractor.RowSortExpressionContext(0))); END } should the `SortExpressionExtractor` be renamed as well?

Target:
private static PagesHashStrategy pagesHashStrategy() { return new SimplePagesHashStrategy( ImmutableList.of(BIGINT), ImmutableList.of(), ImmutableList.of(ImmutableList.of(TEST_PAGE.getBlock(0))), ImmutableList.of(), Optional.empty(), Optional.of(0)); }
Source:
public String toString() { START String predicate = Joiner.on("AND") END .join(filterExpectedMappings.entrySet().stream() .map(entry -> entry.getKey() + " = " + entry.getValue()) .collect(toImmutableList())); return toStringHelper(this) .add("predicate", filterExpectedMappings) .toString(); } `predicate` is not used.

Target:
public String toString() { String predicate = Joiner.on("AND") .join(filterExpectedMappings.entrySet().stream() .map(entry -> entry.getKey() + " = " + entry.getValue()) .collect(toImmutableList())); return toStringHelper(this) .add("predicate", predicate) .toString(); }
Source:
START void trivialMatchers() END { //any assertMatch(any(), 42); assertMatch(any(), "John Doe"); //class based assertMatch(typeOf(Integer.class), 42); assertMatch(typeOf(Number.class), 42); assertNoMatch(typeOf(Integer.class), "John Doe"); } [DONE] public?

Target:
public void trivialMatchers() { //any assertMatch(any(), 42); assertMatch(any(), "John Doe"); //class based assertMatch(typeOf(Integer.class), 42); assertMatch(typeOf(Number.class), 42); assertNoMatch(typeOf(Integer.class), "John Doe"); }
Source:
public Optional<PlanNode> apply(PlanNode node, Context context) { FilterNode filterNode = (FilterNode) node; Expression rewritten = rewriter.rewrite(filterNode.getPredicate(), context); if (filterNode.getPredicate().equals(rewritten)) { return Optional.empty(); } START else { END return Optional.of(new FilterNode(node.getId(), filterNode.getSource(), rewritten)); } } not needed `else` block. Here in other rewrites as well.

Target:
public Optional<PlanNode> apply(PlanNode node, Context context) { FilterNode filterNode = (FilterNode) node; Expression rewritten = rewriter.rewrite(filterNode.getPredicate(), context); if (filterNode.getPredicate().equals(rewritten)) { return Optional.empty(); } return Optional.of(new FilterNode(node.getId(), filterNode.getSource(), rewritten)); }
Source:
public void testFilterExpressionRewrite() { tester().assertThat(zeroRewriter).on(p -> START p.filter(new LongLiteral("1"), p.values())) END .matches( filter("0", PlanMatchPattern.values())); } use static imports for `PlanMatchPattern` things

Target:
public void testFilterExpressionRewrite() { tester().assertThat(zeroRewriter).on(p -> p.filter(new LongLiteral("1"), p.values())) .matches( filter("0", values())); }
Source:
START protected ExpressionRewriteRuleSet(ExpressionRewriteRule rewrite) END { this.rewriter = rewrite; } make it public

Target:
public ExpressionRewriteRuleSet(ExpressionRewriteRule rewrite) { this.rewriter = rewrite; }
Source:
private static byte[] reverse(final byte[] a) { final int length = a.length; START int i = length / 2; END while(i-- != 0) { final byte t = a[length - i - 1]; a[length - i - 1] = a[i]; a[i] = t; } return a; } This looks to me like maybe you accidentally included an unrelated change?

Target:
private static byte[] reverse(final byte[] a) { final int length = a.length; for (int i = length / 2; i-- != 0; ) { final byte t = a[length - i - 1]; a[length - i - 1] = a[i]; a[i] = t; } return a; }
Source:
START public AggregationBuilder addAggregation(Symbol output, Expression expression, List<Type> inputTypes, Optional<Symbol> mask) END { checkArgument(expression instanceof FunctionCall); FunctionCall aggregation = (FunctionCall) expression; Signature signature = metadata.getFunctionRegistry().resolveFunction(aggregation.getName(), TypeSignatureProvider.fromTypes(inputTypes)); return addAggregation(output, new Aggregation(aggregation, signature, mask)); } Maybe this method could get rid of `Optional`? As in case of `Optional.empty()` the method you just added can called.

Target:
private AggregationBuilder addAggregation(Symbol output, Expression expression, List<Type> inputTypes, Optional<Symbol> mask) { checkArgument(expression instanceof FunctionCall); FunctionCall aggregation = (FunctionCall) expression; Signature signature = metadata.getFunctionRegistry().resolveFunction(aggregation.getName(), TypeSignatureProvider.fromTypes(inputTypes)); return addAggregation(output, new Aggregation(aggregation, signature, mask)); }
Source:
public SimplifyExpressions(Metadata metadata, SqlParser sqlParser) START { END super((expression, context) -> rewrite(expression, context, metadata, sqlParser)); } I thought there were more rewrites.

Target:
public SimplifyExpressions(Metadata metadata, SqlParser sqlParser) { this.metadata = metadata; this.sqlParser = sqlParser; }
Source:
public FailureInfo getLastFailureInfo() { Exception lastFailureException = getLastFailureException(); if (lastFailureException == null) { return null; } START else { END return Failures.toFailure(lastFailureException).toFailureInfo(); } } unnecessary else as previous if returns.

Target:
public FailureInfo getLastFailureInfo() { Exception lastFailureException = getLastFailureException(); if (lastFailureException == null) { return null; } return Failures.toFailure(lastFailureException).toFailureInfo(); }
Source:
public ListeningScheduledExecutorService createRetryDriverScheduledExecutor(ThriftConnectorConfig config) { START return MoreExecutors.listeningDecorator(newScheduledThreadPool(config.getRetryDriverThreads(), threadsNamed("thrift-retry-driver-%s"))); END } use static import

Target:
public ListeningScheduledExecutorService createRetryDriverScheduledExecutor(ThriftConnectorConfig config) { return listeningDecorator(newScheduledThreadPool(config.getRetryDriverThreads(), threadsNamed("thrift-retry-driver-%s"))); }
Source:
START private boolean connectorSupportsTest(TypeElement connector, Class<?> test) END { Set<ConnectorFeature> supportedFeatures = supportedFeatures(connector); Set<ConnectorFeature> requiredFeatures = requiredFeatures(test); return supportedFeatures.containsAll(requiredFeatures); } This can be `static`

Target:
private static boolean connectorSupportsTest(TypeElement connector, Class<?> test) { Set<ConnectorFeature> supportedFeatures = supportedFeatures(connector); Set<ConnectorFeature> requiredFeatures = requiredFeatures(test); return supportedFeatures.containsAll(requiredFeatures); }
Source:
private <T extends PlanNode> void findAllRecursive(PlanNode node, ImmutableList.Builder<T> nodes) { node = lookup.resolve(node); if (where.test(node)) { nodes.add((T) node); } START if (skipOnly.test(node) || where.test(node)) { END for (PlanNode source : node.getSources()) { findAllRecursive(source, nodes); } } } `skipOnly` is named terribly wrong. However as long as it means "continue search only when", you should _not_ modify this `if`.

Target:
private <T extends PlanNode> void findAllRecursive(PlanNode node, ImmutableList.Builder<T> nodes) { node = lookup.resolve(node); if (where.test(node)) { nodes.add((T) node); } if (skipOnly.test(node)) { for (PlanNode source : node.getSources()) { findAllRecursive(source, nodes); } } }
Source:
public long getRegionSizeInBytes(int positionOffset, int length) { int positionCount = getPositionCount(); if (positionOffset < 0 || length < 0 || positionOffset + length > positionCount) { throw new IndexOutOfBoundsException("Invalid position " + positionOffset + " in block with " + positionCount + " positions"); } START return (long) length * (fixedSize + Byte.BYTES); END } It is not obvious whether this is correct: `(long) x * y`. I would rewrite it to either `y * (long) x` or `x * (long) y`.

Target:
public long getRegionSizeInBytes(int positionOffset, int length) { int positionCount = getPositionCount(); if (positionOffset < 0 || length < 0 || positionOffset + length > positionCount) { throw new IndexOutOfBoundsException("Invalid position " + positionOffset + " in block with " + positionCount + " positions"); } return (fixedSize + Byte.BYTES) * (long) length; }
Source:
private String convertDaysToMidnightTimestamp(String columnName) { // A date is stored as an integer of days from 1970-01-01. Multiplying it by milliseconds per day renders the UTC midnight timestamp, which is stored as a bigint. START return format("(CAST (%s AS BIGINT) * %s)", columnName, TimeUnit.DAYS.toMillis(1)); END } use static import for `days`

Target:
private String convertDaysToMidnightTimestamp(String columnName) { // A date is stored as an integer of days from 1970-01-01. Multiplying it by milliseconds per day renders the UTC midnight timestamp, which is stored as a bigint. return format("(CAST (%s AS BIGINT) * %s)", columnName, DAYS.toMillis(1)); }
Source:
private static Iterable<Expression> pullNullableConjunctsThroughOuterJoin(List<Expression> conjuncts, Collection<Symbol> outputSymbols, Predicate<Symbol>... nullSymbolScopes) { // Conjuncts without any symbol dependencies cannot be applied to the effective predicate (e.g. FALSE literal) return conjuncts.stream() .map(expression -> pullExpressionThroughSymbols(expression, outputSymbols)) START .map(expression -> SymbolsExtractor.extractAll(expression).anyMatch(alwaysTrue()) ? TRUE_LITERAL : expression) END .map(expressionOrNullSymbols(nullSymbolScopes)) .collect(toImmutableList()); } This is wrong. `isEmpty()` can be migrated to `count() == 1`

Target:
private static Iterable<Expression> pullNullableConjunctsThroughOuterJoin(List<Expression> conjuncts, Collection<Symbol> outputSymbols, Predicate<Symbol>... nullSymbolScopes) { // Conjuncts without any symbol dependencies cannot be applied to the effective predicate (e.g. FALSE literal) return conjuncts.stream() .map(expression -> pullExpressionThroughSymbols(expression, outputSymbols)) .map(expression -> isEmpty(SymbolsExtractor.extractAll(expression)) ? TRUE_LITERAL : expression) .map(expressionOrNullSymbols(nullSymbolScopes)) .collect(toImmutableList()); }
Source:
public TestTpchDistributedStats() throws Exception { START runner = createQueryRunnerWithoutCatalogs(emptyMap(), emptyMap(), 4); END runner.createCatalog("tpch", "tpch", ImmutableMap.of( "tpch.column-naming", ColumnNaming.STANDARD.name() )); } Can you just add another overloaded method that does not take a worker count (similar to what you did already)?

Target:
public TestTpchDistributedStats() throws Exception { runner = createQueryRunnerWithoutCatalogs(emptyMap(), emptyMap()); runner.createCatalog("tpch", "tpch", ImmutableMap.of( "tpch.column-naming", ColumnNaming.STANDARD.name() )); }
Source:
public Node visitWindowSpecification(SqlBaseParser.WindowSpecificationContext context) { return new WindowSpecification( getLocation(context), START Optional.ofNullable(context.identifier()).map(i -> (Identifier) visit(i)), END visit(context.partition, Expression.class), visit(context.sortItem(), SortItem.class), visitIfPresent(context.windowFrame(), WindowFrame.class)); } ditto, `s/i/identifier/`

Target:
public Node visitWindowSpecification(SqlBaseParser.WindowSpecificationContext context) { return new WindowSpecification( getLocation(context), Optional.ofNullable(context.identifier()).map(identifier -> (Identifier) visit(identifier)), visit(context.partition, Expression.class), visit(context.sortItem(), SortItem.class), visitIfPresent(context.windowFrame(), WindowFrame.class)); }
Source:
public boolean advanceNextPosition() START { END if (this.closed) { return false; } try { boolean result = this.resultSet.next(); if (!result) { this.close(); } return result; } catch (SQLException | RuntimeException e) { throw this.handleSqlException(e); } } The body of this method looks very much like `com.facebook.presto.plugin.jdbc.JdbcRecordCursor#advanceNextPosition`. Perhaps there is a possibility to retain reuse? Same applies to other methods too. Even if you need to copy something (i hope you don't), clearly separate copying and enhancements into two separate commits, otherwise I don't know what to review.

Target:
public boolean advanceNextPosition() { if (closed) { return false; } try { boolean result = resultSet.next(); if (!result) { close(); } return result; } catch (SQLException | RuntimeException e) { throw handleSqlException(e); } }
Source:
START public void setUp() END { tester = new RuleTester(); } Make these methods final so they can't accidentally be overridden

Target:
public final void setUp() { tester = new RuleTester(); }
Source:
private boolean isKnownCost(PlanNode node) { START return !Objects.equals(UNKNOWN_COST, costs.getOrDefault(node.getId(), UNKNOWN_COST)); END } Since this is comparing with a constant object, no need to use `Objects.equals` ```java return !UNKNOWN_COST.equals(costs.getOrDefault(node.getId(), UNKNOWN_COST));

Target:
private boolean isKnownCost(PlanNode node) { return !UNKNOWN_COST.equals(costs.getOrDefault(node.getId(), UNKNOWN_COST)); }
Source:
public ResourceGroupStateInfo getQueryStateInfos(@PathParam("resourceGroupId") String resourceGroupIdString) { if (!isNullOrEmpty(resourceGroupIdString)) { START try { END return resourceGroupManager.getResourceGroupStateInfo(new ResourceGroupId( newArrayList(resourceGroupIdString.split("/")).stream() .map(ResourceGroupStateInfoResource::urlDecode).collect(toImmutableList()))); } catch (NoSuchElementException e) { throw new WebApplicationException(NOT_FOUND); } } throw new WebApplicationException(NOT_FOUND); } Arrays.stream() might be easier, and the collect call should be on a newline. Maybe something like: ```java try { return resourceGroupManager.getResourceGroupStateInfo( new ResourceGroupId( Arrays.stream(resourceGroupIdString.split("/")) .map(ResourceGroupStateInfoResource::urlDecode) .collect(toImmutableList()))); } ```

Target:
public ResourceGroupStateInfo getQueryStateInfos(@PathParam("resourceGroupId") String resourceGroupIdString) { if (!isNullOrEmpty(resourceGroupIdString)) { try { return resourceGroupManager.getResourceGroupStateInfo( new ResourceGroupId( Arrays.stream(resourceGroupIdString.split("/")) .map(ResourceGroupStateInfoResource::urlDecode) .collect(toImmutableList()))); } catch (NoSuchElementException e) { throw new WebApplicationException(NOT_FOUND); } } throw new WebApplicationException(NOT_FOUND); }
Source:
static void waitForQueryCount(DistributedQueryRunner queryRunner, Set<QueryState> countingStates, int expectedCount) throws InterruptedException { QueryManager queryManager = queryRunner.getCoordinator().getQueryManager(); START while (queryManager.getAllQueryInfo().stream().filter(q -> countingStates.contains(q.getState())).count() != expectedCount) { END MILLISECONDS.sleep(500); } } Split into multiple lines

Target:
public static void waitForQueryCount(DistributedQueryRunner queryRunner, Set<QueryState> countingStates, int expectedCount) throws InterruptedException { QueryManager queryManager = queryRunner.getCoordinator().getQueryManager(); while (queryManager.getAllQueryInfo().stream() .filter(q -> countingStates.contains(q.getState())).count() != expectedCount) { MILLISECONDS.sleep(500); } }
Source:
private static Optional<Domain> getPathDomain(TupleDomain<HiveColumnHandle> effectivePredicate) { if (!effectivePredicate.getDomains().isPresent()) { return Optional.empty(); } return effectivePredicate.getDomains().get().entrySet().stream() .filter(entrySet -> isPathColumnHandle(entrySet.getKey())) .findFirst() START .map(entrySet -> entrySet.getValue()); END } It's an `entry`, not `entrySet`. In addition, you can write `Map.Entry::getValue` here.

Target:
private static Optional<Domain> getPathDomain(TupleDomain<HiveColumnHandle> effectivePredicate) { if (!effectivePredicate.getDomains().isPresent()) { return Optional.empty(); } return effectivePredicate.getDomains().get().entrySet().stream() .filter(entry -> isPathColumnHandle(entry.getKey())) .findFirst() .map(Map.Entry::getValue); }
Source:
public void tryEvaluateWithAll(String expression, Type expectedType) { START executeProjectionWithAll(expression, expectedType, session, compiler); END } Have this call the other `tryEvaluateWithAll` method

Target:
public void tryEvaluateWithAll(String expression, Type expectedType) { tryEvaluateWithAll(expression, expectedType, session); }
Source:
public boolean needsInput() { START boolean stateNeedsInput = state == State.CONSUMING_INPUT END || (state == State.SPILLING_INPUT && spillInProgress.isDone()); return stateNeedsInput && !lookupSourceFactoryDestroyed.isDone(); } add parentheses

Target:
public boolean needsInput() { boolean stateNeedsInput = (state == State.CONSUMING_INPUT) || (state == State.SPILLING_INPUT && spillInProgress.isDone()); return stateNeedsInput && !lookupSourceFactoryDestroyed.isDone(); }
Source:
public void testNotAllOutputsReferenced() { tester().assertThat(new PruneIndexSourceColumns()) .on(p -> buildProjectedIndexSource(p, symbol -> symbol.getName().equals("orderkey"))) .matches( strictProject( START ImmutableMap.of("x", PlanMatchPattern.expression("orderkey")), END constrainedIndexSource( "orders", ImmutableMap.of("totalprice", Domain.onlyNull(DOUBLE)), ImmutableMap.of( "orderkey", "orderkey", "totalprice", "totalprice")))); } static imort?

Target:
public void testNotAllOutputsReferenced() { tester().assertThat(new PruneIndexSourceColumns()) .on(p -> buildProjectedIndexSource(p, symbol -> symbol.getName().equals("orderkey"))) .matches( strictProject( ImmutableMap.of("x", expression("orderkey")), constrainedIndexSource( "orders", ImmutableMap.of("totalprice", Domain.onlyNull(DOUBLE)), ImmutableMap.of( "orderkey", "orderkey", "totalprice", "totalprice")))); }
Source:
public void close() { START if (httpClient != null) { END httpClient.close(); } } you don't need to check for null since this gets set in the constructor

Target:
public void close() { httpClient.close(); }
Source:
public static Slice crc32(@SqlType(StandardTypes.VARBINARY) Slice slice) { Slice hash = Slices.allocate(Long.BYTES); CRC32 crc32 = new CRC32(); crc32.update(slice.toByteBuffer()); // The expectation is that CRC32 is unsigned, so this won't fit // correctly in a 32 signed integer, therefore this uses a long. long crc32Value = crc32.getValue(); hash.setLong(0, Long.reverseBytes(crc32Value)); START return hash; END } I did a bunch of investigation and had quite a discussion with @martint. We decide that crc32 will return a number (instead of binary). I'm sorry for the change. Please change accordingly. I looked into PHP, java (java.util.zip), Java (Guava), C++ Boost, Python (binascii), and Ruby (zlib). It turns out there is no general consensus on byte order for representation of the result.

Target:
public static long crc32(@SqlType(StandardTypes.VARBINARY) Slice slice) { CRC32 crc32 = new CRC32(); crc32.update(slice.toByteBuffer()); return crc32.getValue(); }
Source:
public Class<? extends ConnectorTableHandle> getTableHandleClass() { START return TestingMetadata.TestingTableHandle.class; END } static import

Target:
public Class<? extends ConnectorTableHandle> getTableHandleClass() { return TestingTableHandle.class; }
Source:
private static PlanFragment createTableScanPlanFragment(String name) { Symbol symbol = new Symbol("column"); PlanNode planNode = new TableScanNode( new PlanNodeId(name), START new TableHandle(new ConnectorId("test"), new TestingMetadata.TestingTableHandle()), END ImmutableList.of(symbol), ImmutableMap.of(symbol, new TestingColumnHandle("column")), Optional.empty(), TupleDomain.all(), null); return createFragment(planNode); } static import

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()), ImmutableList.of(symbol), ImmutableMap.of(symbol, new TestingColumnHandle("column")), Optional.empty(), TupleDomain.all(), null); return createFragment(planNode); }
Source:
public TableScanNode tableScan(List<Symbol> symbols, Map<Symbol, ColumnHandle> assignments) { START TableHandle tableHandle = new TableHandle(new ConnectorId("testConnector"), new TestingMetadata.TestingTableHandle()); END return tableScan(tableHandle, symbols, assignments); } static import

Target:
public TableScanNode tableScan(List<Symbol> symbols, Map<Symbol, ColumnHandle> assignments) { TableHandle tableHandle = new TableHandle(new ConnectorId("testConnector"), new TestingTableHandle()); return tableScan(tableHandle, symbols, assignments); }
Source:
private void avroAssertColumnTypes(QueryResult queryResult) { Connection connection = defaultQueryExecutor().getConnection(); if (usingPrestoJdbcDriver(connection)) { assertThat(queryResult).hasColumns( BIGINT, DOUBLE, BIGINT ); } START else if (usingTeradataJdbcDriver(connection)) { END assertThat(queryResult).hasColumns( BIGINT, DOUBLE, BIGINT ); } else { throw new IllegalStateException(); } } these conditions are the same. you should combine them

Target:
private void avroAssertColumnTypes(QueryResult queryResult) { Connection connection = defaultQueryExecutor().getConnection(); if (usingPrestoJdbcDriver(connection) || usingTeradataJdbcDriver(connection)) { assertThat(queryResult).hasColumns( BIGINT, DOUBLE, BIGINT ); } else { throw new IllegalStateException(); } }
Source:
public void setNoMorePages() { checkState(!Thread.holdsLock(this), "Can not set no more pages while holding a lock on this"); state.compareAndSet(OPEN, NO_MORE_PAGES); state.compareAndSet(NO_MORE_BUFFERS, FLUSHING); memoryManager.setNoBlockOnFull(); masterBuffer.setNoMorePages(); // process any pending reads from the client buffers START for (ClientBuffer clientBuffer : safeGetBuffersSnapshot()) { END if (masterBuffer.isEmpty()) { clientBuffer.setNoMorePages(); } else { clientBuffer.loadPagesIfNecessary(masterBuffer); } } checkFlushComplete(); } I'm not sure this is correct. The `ClientBuffer` has a push and a pull mode. The push mode uses `enueuPages` and `setNoMorePages` and the pull mode uses `loadPagesIfNecessary`. I'm not confident that using `setNoMorePages` in a pull mode would do what we want. Instead, I suggest we simply call `loadPagesIfNecessary` on every `ClientBuffer` and then modify `loadPagesIfNecessary` to properly handle being called with a `!pagesSupplier.mayHaveMorePages()`.

Target:
public void setNoMorePages() { checkState(!Thread.holdsLock(this), "Can not set no more pages while holding a lock on this"); state.compareAndSet(OPEN, NO_MORE_PAGES); state.compareAndSet(NO_MORE_BUFFERS, FLUSHING); memoryManager.setNoBlockOnFull(); masterBuffer.setNoMorePages(); // process any pending reads from the client buffers for (ClientBuffer clientBuffer : safeGetBuffersSnapshot()) { clientBuffer.loadPagesIfNecessary(masterBuffer); } checkFlushComplete(); }
Source:
private void reset() { probeIndexBuilder.reset(); START buildPageBuilder.reset(); END // Reset source page when probe reached the end. if (reachedProbeEnd) { probe = null; } } Isn't that if `reset` is called we know we reached end of the probe?

Target:
private void reset() { probeIndexBuilder.clear(); buildPageBuilder.reset(); // Reset source page when probe reached the end. if (reachedProbeEnd) { probe = null; } }
Source:
private static ListenableFuture<?> getBlockedFuture(Operator operator) { ListenableFuture<?> blocked = operator.isBlocked(); START if (blocked.isDone()) { END blocked = operator.getOperatorContext().isWaitingForMemory(); } return blocked; } It looks like what you really want is to say, "I'm blocked" until both of these conditions are satisfied. If so, you can simple replace the original method with: ```java return Futures.allAsList(operator.isBlocked(), operator.getOperatorContext().isWaitingForMemory()); ```

Target:
private static Optional<ListenableFuture<?>> getBlockedFuture(Operator operator) { ListenableFuture<?> blocked = operator.isBlocked(); if (!blocked.isDone()) { return Optional.of(blocked); } blocked = operator.getOperatorContext().isWaitingForMemory(); if (!blocked.isDone()) { return Optional.of(blocked); } return Optional.empty(); }
Source:
public void addEventListenerFactory(EventListenerFactory eventListenerFactory) { configuredEventListener.set(Optional.of(eventListenerFactory.create(ImmutableMap.of(), new EventListenerContext() { START private final MBeanServer mBeanServer = getMBeanNamespaceManager().createMBeanServer("TestEventListener"); END @Override public MBeanServer getMBeanServer() { return mBeanServer; } }))); } Declare outside

Target:
public void addEventListenerFactory(EventListenerFactory eventListenerFactory) { configuredEventListener.set(Optional.of(eventListenerFactory.create(ImmutableMap.of(), new EventListenerContext() { @Override public MBeanServer getMBeanServer() { return mBeanServer; } }))); }
Source:
public void init() throws Exception { START queryRunner = queryRunner != null ? queryRunner : queryRunnerSupplier.get(); END h2QueryRunner = new H2QueryRunner(); sqlParser = new SqlParser(); costCalculator = new CoefficientBasedCostCalculator(queryRunner.getMetadata()); } This seems wrong. The `@BeforeClass` method should never be called more than once.

Target:
public void init() throws Exception { queryRunner = queryRunnerSupplier.get(); h2QueryRunner = new H2QueryRunner(); sqlParser = new SqlParser(); costCalculator = new CoefficientBasedCostCalculator(queryRunner.getMetadata()); }
Source:
public StatusPrinter(StatementClient client, PrintStream out) { this.client = client; this.out = out; this.console = new ConsolePrinter(out); this.debug = client.isDebug(); START this.mappingPreviousStageStatuses = new HashMap<>(); END } inline to its declaration.

Target:
public StatusPrinter(StatementClient client, PrintStream out) { this.client = client; this.out = out; this.console = new ConsolePrinter(out); this.debug = client.isDebug(); }
Source:
public double next() throws IOException { START input.readFully(buffer, 0, (int) SIZE_OF_DOUBLE); END return slice.getDouble(0); } This cast is not necessary (same with the others)

Target:
public double next() throws IOException { input.readFully(buffer, 0, SIZE_OF_DOUBLE); return slice.getDouble(0); }
Source:
public synchronized Optional<ConnectorOutputMetadata> finishInsert(ConnectorSession session, ConnectorInsertTableHandle insertHandle, Collection<Slice> fragments) { requireNonNull(insertHandle, "insertHandle is null"); START checkArgument(insertHandle instanceof MemoryInsertTableHandle); END MemoryInsertTableHandle memoryInsertHandle = (MemoryInsertTableHandle) insertHandle; updateRowsOnHosts(memoryInsertHandle.getTable(), fragments); return Optional.empty(); } Remove instanceof checks - doesn't add any value. Look at other implementations of ConnectorMetadata

Target:
public synchronized Optional<ConnectorOutputMetadata> finishInsert(ConnectorSession session, ConnectorInsertTableHandle insertHandle, Collection<Slice> fragments) { requireNonNull(insertHandle, "insertHandle is null"); MemoryInsertTableHandle memoryInsertHandle = (MemoryInsertTableHandle) insertHandle; updateRowsOnHosts(memoryInsertHandle.getTable(), fragments); return Optional.empty(); }
Source:
private static long getSliceArrayRetainedSizeInBytes(Slice[] values) { START long sizeInBytes = SizeOf.sizeOf(values); END Map<Object, Boolean> uniqueRetained = new IdentityHashMap<>(values.length); for (Slice value : values) { if (value != null && value.getBase() != null && uniqueRetained.put(value.getBase(), true) == null) { sizeInBytes += value.getRetainedSize(); } } return sizeInBytes; } Replace `SizeOf.sizeOf`. You statically imported it.

Target:
private static long getSliceArrayRetainedSizeInBytes(Slice[] values) { long sizeInBytes = sizeOf(values); Map<Object, Boolean> uniqueRetained = new IdentityHashMap<>(values.length); for (Slice value : values) { if (value == null) { continue; } if (value.getBase() != null && uniqueRetained.put(value.getBase(), true) == null) { sizeInBytes += value.getRetainedSize(); } } return sizeInBytes; }
Source:
START public Optional<Decorrelated> decorrelate(PlanNode reference) END { checkArgument(reference instanceof GroupReference); return lookup.resolve(reference).accept(this, reference); } If this has to be a GroupReference, just take that as an argument.

Target:
public Optional<Decorrelated> decorrelate(PlanNode reference) { return lookup.resolve(reference).accept(this, reference); }
Source:
public Optional<Decorrelated> visitFilter(FilterNode node, PlanNode reference) { Optional<Decorrelated> result = decorrelate(node.getSource()); START return result.map(decorrelated -> { END return new Decorrelated( ImmutableList.<Expression>builder() .addAll(decorrelated.getCorrelatedPredicates()) // No need to retain uncorrelated conditions, predicate push down will push them back .add(node.getPredicate()) .build(), decorrelated.getDecorrelatedNode() ); }); } No need for `{ ... }` and `return`. It's a single expression.

Target:
public Optional<Decorrelated> visitFilter(FilterNode node, PlanNode reference) { Optional<Decorrelated> result = decorrelate(node.getSource()); return result.map(decorrelated -> new Decorrelated( ImmutableList.<Expression>builder() .addAll(decorrelated.getCorrelatedPredicates()) // No need to retain uncorrelated conditions, predicate push down will push them back .add(node.getPredicate()) .build(), decorrelated.getDecorrelatedNode() )); }
Source:
public int hashCode() { START return Objects.hash(message, sqlState, errorCode, errorName, errorType, errorLocation, failureInfo); END } please order the same as in equals so it's obvious whether all are here (why is equals required?)

Target:
public int hashCode() { return Objects.hash(errorCode, message, sqlState, errorName, errorType, errorLocation, failureInfo); }
Source:
protected Boolean visitBetweenPredicate(BetweenPredicate actual, Expression expectedExpression) { if (expectedExpression instanceof BetweenPredicate) { BetweenPredicate expected = (BetweenPredicate) expectedExpression; START return process(actual.getMin(), expected.getMin()) && process(actual.getMax(), expected.getMax()); END } return false; } It needs to process the "value" from the between predicate, too.

Target:
protected Boolean visitBetweenPredicate(BetweenPredicate actual, Expression expectedExpression) { if (expectedExpression instanceof BetweenPredicate) { BetweenPredicate expected = (BetweenPredicate) expectedExpression; return process(actual.getValue(), expected.getValue()) && process(actual.getMin(), expected.getMin()) && process(actual.getMax(), expected.getMax()); } return false; }
Source:
public String idFromValueAndType(Object value, Class<?> suggestedType) { requireNonNull(value, "value is null"); if(value instanceof TupleExpression) { TupleExpression expression = (TupleExpression)value; if(expression instanceof DomainExpression) { return expression.getName() + "@@" + nameResolver.apply((ColumnHandle) ((DomainExpression) expression).getColumn()) ; } return expression.getName(); } START else END return null; } Missing brackets.

Target:
public String idFromValueAndType(Object value, Class<?> suggestedType) { requireNonNull(value, "value is null"); if (value instanceof TupleExpression) { TupleExpression expression = (TupleExpression) value; if (expression instanceof DomainExpression) { return expression.getName() + "@@" + nameResolver.apply((ColumnHandle) ((DomainExpression) expression).getColumn()); } return expression.getName(); } else { return null; } }