auphong2707/dl4se-code-review-dataset huggingface dataset:
Source:
START public BlockBuilder nextColumn() END { int currentChannel = channel; channel++; return pageBuilder.getBlockBuilder(currentChannel); } make this `private`

Target:
private BlockBuilder nextColumn() { int currentChannel = channel; channel++; return pageBuilder.getBlockBuilder(currentChannel); }
Source:
START public JoinNode.Type getType() END { return type; } Is this still correct? Can this be ensured only by `LocalExecutionPlanner`?

Target:
public Type getType() { return type; }
Source:
public PlanNode replaceChildren(List<PlanNode> newChildren) { checkArgument(newChildren.size() == 2, "expected newChildren to contain 2 nodes"); PlanNode newLeft = newChildren.get(0); START PlanNode newRight = newChildren.get(1); END return new SpatialJoinNode(getId(), type, newLeft, newRight, outputSymbols, filter); } I would inline them

Target:
public PlanNode replaceChildren(List<PlanNode> newChildren) { checkArgument(newChildren.size() == 2, "expected newChildren to contain 2 nodes"); return new SpatialJoinNode(getId(), type, newChildren.get(0), newChildren.get(1), outputSymbols, filter); }
Source:
public StreamProperties visitSpatialJoin(SpatialJoinNode node, List<StreamProperties> inputProperties) { StreamProperties leftProperties = inputProperties.get(0); switch (node.getType()) { case INNER: case LEFT: return leftProperties.translate(column -> PropertyDerivations.filterIfMissing(node.getOutputSymbols(), column)); default: START throw new UnsupportedOperationException("Unsupported spatial join type: " + node.getType()); END } } `IllegalArgumentException`

Target:
public StreamProperties visitSpatialJoin(SpatialJoinNode node, List<StreamProperties> inputProperties) { StreamProperties leftProperties = inputProperties.get(0); switch (node.getType()) { case INNER: case LEFT: return leftProperties.translate(column -> PropertyDerivations.filterIfMissing(node.getOutputSymbols(), column)); default: throw new IllegalArgumentException("Unsupported spatial join type: " + node.getType()); } }
Source:
public static Type fromJoinNodeType(JoinNode.Type joinNodeType) { switch (joinNodeType) { case INNER: return Type.INNER; case LEFT: return Type.LEFT; default: START throw new UnsupportedOperationException("Unsupported spatial join type: " + joinNodeType); END } } IllegalArgumentException

Target:
public static Type fromJoinNodeType(JoinNode.Type joinNodeType) { switch (joinNodeType) { case INNER: return Type.INNER; case LEFT: return Type.LEFT; default: throw new IllegalArgumentException("Unsupported spatial join type: " + joinNodeType); } }
Source:
START static Long toMillis(Duration duration) END { if (duration == null) { return null; } return duration.toMillis(); } Instead of exporting this, I would simply replace the future usages with Optional.ofNullable(duration).map(Duration::toMillis).orElse(null)

Target:
private static Long toMillis(Duration duration) { if (duration == null) { return null; } return duration.toMillis(); }
Source:
public static MetadataManager createTestMetadataManager(CatalogManager catalogManager, FeaturesConfig featuresConfig) { TypeManager typeManager = new TypeRegistry(ImmutableSet.of(), featuresConfig); return new MetadataManager( featuresConfig, typeManager, new BlockEncodingManager(typeManager), new SessionPropertyManager(), new SchemaPropertyManager(), new TablePropertyManager(), new ColumnPropertyManager(), START InMemoryTransactionManager.createTestTransactionManager(catalogManager)); END } Keep this static imported?

Target:
public static MetadataManager createTestMetadataManager(CatalogManager catalogManager, FeaturesConfig featuresConfig) { TypeManager typeManager = new TypeRegistry(ImmutableSet.of(), featuresConfig); return new MetadataManager( featuresConfig, typeManager, new BlockEncodingManager(typeManager), new SessionPropertyManager(), new SchemaPropertyManager(), new TablePropertyManager(), new ColumnPropertyManager(), createTestTransactionManager(catalogManager)); }
Source:
private static long getGeometryMemorySize(OGCGeometry geometry) { if (geometry == null) { return 0; } START else if (geometry.isEmpty()) { END return OGC_GEOMETRY_BASE_INSTANCE_SIZE; } else { return geometry.estimateMemorySize(); } } style: drop redundant `else`s: ``` if (geometry == null) { return 0; } if (geometry.isEmpty()) { return ... } return geometry.estimateMemorySize(); ``` Could you add a reference to ESRI issue to explain special handling for empty geometries?

Target:
private static long getGeometryMemorySize(OGCGeometry geometry) { if (geometry == null) { return 0; } // Due to the following issue: // https://github.com/Esri/geometry-api-java/issues/192 // We must check if the geometry is empty before calculating its size. Once the issue is resolved // and we bring the fix into our codebase, we can remove this check. if (geometry.isEmpty()) { return OGC_GEOMETRY_BASE_INSTANCE_SIZE; } return geometry.estimateMemorySize(); }
Source:
public void serialize(GeometryState state, BlockBuilder out) { if (state.getGeometry() == null) { out.appendNull(); } else { START GeometryType.GEOMETRY.writeSlice(out, GeometrySerde.serialize(state.getGeometry())); END } } style: static import `GeometryType.GEOMETRY`, `GeometrySerde.serialize` and `GeometrySerde.deserialize`

Target:
public void serialize(GeometryState state, BlockBuilder out) { if (state.getGeometry() == null) { out.appendNull(); } else { GEOMETRY.writeSlice(out, GeometrySerde.serialize(state.getGeometry())); } }
Source:
START public void removePreAllocation(QueryId queryId) END { synchronized (this) { preAllocations.remove(queryId); } MemoryPoolInfo info = pools.get(GENERAL_POOL).getInfo(); for (Consumer<MemoryPoolInfo> listener : changeListeners.get(GENERAL_POOL)) { listenerExecutor.execute(() -> listener.accept(info)); } } What's the reason behind this change? `changeListeners` and `pools` are also `GuardedBy` `this`, and this change removes the synchronization on them.

Target:
public synchronized void removePreAllocation(QueryId queryId) { preAllocations.remove(queryId); MemoryPoolInfo info = pools.get(GENERAL_POOL).getInfo(); for (Consumer<MemoryPoolInfo> listener : changeListeners.get(GENERAL_POOL)) { listenerExecutor.execute(() -> listener.accept(info)); } }
Source:
public Rectangle merge(Rectangle other) { START requireNonNull(other, "other is null"); END return new Rectangle(min(this.xMin, other.xMin), min(this.yMin, other.yMin), max(this.xMax, other.xMax), max(this.yMax, other.yMax)); } This is not really necessary. It will fail with NPE in the line below if it comes to it. In general, it's more important to do these kinds of checks when holding on to the value, which may cause the code to fail much later in an unrelated place.

Target:
public Rectangle merge(Rectangle other) { return new Rectangle(min(this.xMin, other.xMin), min(this.yMin, other.yMin), max(this.xMax, other.xMax), max(this.yMax, other.yMax)); }
Source:
public boolean isFinished() { START return blocked.isDone() && state == State.FINISHED; END } nit: reverse (as it was originally): `state == State.FINISHED && ..` -- do the cheaper thing first

Target:
public boolean isFinished() { return state == State.FINISHED && blocked.isDone(); }
Source:
START public String getTableName() END { return tableName; } all these methods can be private

Target:
private String getTableName() { return tableName; }
Source:
START public String getType() END { return type; } all these methods can be private

Target:
private String getType() { return type; }
Source:
private boolean isS3SelectEnabled(ConnectorSession session) { if (!hiveClientConfig.isS3SelectPushdownEnabled()) { LOG.debug("S3Select is not enabled."); return false; } if (!HiveSessionProperties.isS3SelectPushdownEnabled(session)) { LOG.debug("Session S3SelectPushdown is disabled."); return false; } START LOG.info("S3Select is enabled."); END return true; } This line may get logged a lot of times in a production system when s3 select is enabled, no? (when I trace it, it gets called from `HiveSplitManager.getSplits()`).

Target:
private boolean isS3SelectEnabled(ConnectorSession session) { if (!hiveClientConfig.isS3SelectPushdownEnabled()) { return false; } if (!HiveSessionProperties.isS3SelectPushdownEnabled(session)) { return false; } return true; }
Source:
public void shouldThrowNullPointerExceptionWhenSchemaIsNull() { START updateSplitSchema(null, EMPTY_LIST); END } nit: You can use `ImmutableList.of()` instead of `EMPTY_LIST` with the additional benefit of getting rid of the unchecked assignment warning.

Target:
public void shouldThrowNullPointerExceptionWhenSchemaIsNull() { updateSplitSchema(null, ImmutableList.of()); }
Source:
START private static JoinNode.EquiJoinClause toEquiJoinClause(ComparisonExpression equality, Set<Symbol> leftSymbols) END { Symbol leftSymbol = Symbol.from(equality.getLeft()); Symbol rightSymbol = Symbol.from(equality.getRight()); JoinNode.EquiJoinClause equiJoinClause = new JoinNode.EquiJoinClause(leftSymbol, rightSymbol); return leftSymbols.contains(leftSymbol) ? equiJoinClause : equiJoinClause.flip(); } nit: static import `EquiJoinClause `

Target:
private static EquiJoinClause toEquiJoinClause(ComparisonExpression equality, Set<Symbol> leftSymbols) { Symbol leftSymbol = Symbol.from(equality.getLeft()); Symbol rightSymbol = Symbol.from(equality.getRight()); EquiJoinClause equiJoinClause = new EquiJoinClause(leftSymbol, rightSymbol); return leftSymbols.contains(leftSymbol) ? equiJoinClause : equiJoinClause.flip(); }
Source:
public void testDoesNotAllowOuterJoin() { PlanBuilder p = planBuilder(); Symbol a1 = p.symbol("A1"); Symbol b1 = p.symbol("B1"); JoinNode outerJoin = p.join( START JoinNode.Type.FULL, END p.values(a1), p.values(b1), ImmutableList.of(equiJoinClause(a1, b1)), ImmutableList.of(a1, b1), Optional.empty()); toMultiJoinNode(outerJoin, noLookup(), DEFAULT_JOIN_LIMIT); } Import `FULL`

Target:
public void testDoesNotAllowOuterJoin() { PlanBuilder p = planBuilder(); Symbol a1 = p.symbol("A1"); Symbol b1 = p.symbol("B1"); JoinNode outerJoin = p.join( FULL, p.values(a1), p.values(b1), ImmutableList.of(equiJoinClause(a1, b1)), ImmutableList.of(a1, b1), Optional.empty()); toMultiJoinNode(outerJoin, noLookup(), DEFAULT_JOIN_LIMIT); }
Source:
START private JoinNode.EquiJoinClause equiJoinClause(Symbol symbol1, Symbol symbol2) END { return new JoinNode.EquiJoinClause(symbol1, symbol2); } Import `EquiJoinClause `

Target:
private EquiJoinClause equiJoinClause(Symbol symbol1, Symbol symbol2) { return new EquiJoinClause(symbol1, symbol2); }
Source:
public RuleAssert overrideStats(String nodeId, PlanNodeStatsEstimate nodeStats) { START ((TestingStatsCalculator) statsCalculator).setNodeStats(new PlanNodeId(nodeId), nodeStats); END return this; } The cast is no longer needed

Target:
public RuleAssert overrideStats(String nodeId, PlanNodeStatsEstimate nodeStats) { statsCalculator.setNodeStats(new PlanNodeId(nodeId), nodeStats); return this; }
Source:
public boolean isEnabled(Session session) { // we run this for cost-based reordering also for cases when some of the tables do not have statistics START FeaturesConfig.JoinReorderingStrategy joinReorderingStrategy = getJoinReorderingStrategy(session); END return joinReorderingStrategy == ELIMINATE_CROSS_JOINS || joinReorderingStrategy == COST_BASED; } Import `JoinReorderingStrategy `

Target:
public boolean isEnabled(Session session) { // we run this for cost-based reordering also for cases when some of the tables do not have statistics JoinReorderingStrategy joinReorderingStrategy = getJoinReorderingStrategy(session); return joinReorderingStrategy == ELIMINATE_CROSS_JOINS || joinReorderingStrategy == COST_BASED; }
Source:
public long getRetainedSizeInBytes() { START return INSTANCE_SIZE + SizeOf.sizeOf(nullVector); END } static import `sizeOf` (also in other stream readers)

Target:
public long getRetainedSizeInBytes() { return INSTANCE_SIZE + sizeOf(nullVector); }
Source:
public void close() throws IOException { try (Closer closer = Closer.create()) { closer.register(super::close); START closer.register(s3::shutdown); END if (credentialsProvider instanceof Closeable) { closer.register((Closeable) credentialsProvider); } } } nit: `s3` should be closed before closing credentials providers (because its a dependent resource), so add it last to the closer.

Target:
public void close() throws IOException { try (Closer closer = Closer.create()) { closer.register(super::close); if (credentialsProvider instanceof Closeable) { closer.register((Closeable) credentialsProvider); } closer.register(s3::shutdown); } }
Source:
private static Comparable<?> encodeDecimal(Type type, BigDecimal value) { BigInteger unscaled = Decimals.rescale(value, (DecimalType) type).unscaledValue(); if (Decimals.isShortDecimal(type)) { return unscaled.longValueExact(); } START else { END return Decimals.encodeUnscaledValue(unscaled); } } Nit: else is redundant

Target:
private static Comparable<?> encodeDecimal(Type type, BigDecimal value) { BigInteger unscaled = Decimals.rescale(value, (DecimalType) type).unscaledValue(); if (Decimals.isShortDecimal(type)) { return unscaled.longValueExact(); } return Decimals.encodeUnscaledValue(unscaled); }
Source:
public PartitionMetadata withColumnStatistics(Map<String, HiveColumnStatistics> columnStatistics) { START requireNonNull(columnStatistics, "columnStatistics is null"); END return new PartitionMetadata(columns, parameters, storageFormat, bucketProperty, serdeParameters, externalLocation, columnStatistics); } This is not needed, since the constructor already checks it

Target:
public PartitionMetadata withColumnStatistics(Map<String, HiveColumnStatistics> columnStatistics) { return new PartitionMetadata(columns, parameters, storageFormat, bucketProperty, serdeParameters, externalLocation, columnStatistics); }
Source:
public SpecializedSignature getSpecializedSignature() { SpecializedSignature specializedSignature = new SpecializedSignature( signature, argumentNativeContainerTypes, specializedTypeParameters, returnNativeContainerType); return specializedSignature; START } END nit: You can create the `SpecializedSignature` and return it in one step. ```java return new SpecializedSignature( signature, argumentNativeContainerTypes, specializedTypeParameters, returnNativeContainerType); ```

Target:
SpecializedSignature getSpecializedSignature() { return new SpecializedSignature( signature, argumentNativeContainerTypes, specializedTypeParameters, returnNativeContainerType); }
Source:
static FileBasedSystemAccessControlRules parse(byte[] json) { START ObjectMapper mapper = new ObjectMapperProvider().get() END .enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); Class<FileBasedSystemAccessControlRules> javaType = FileBasedSystemAccessControlRules.class; try { return mapper.readValue(json, javaType); } catch (IOException e) { throw new IllegalArgumentException(format("Invalid JSON string for %s", javaType), e); } } You can use `JsonCodec`: ```java return new JsonCodecFactory().jsonCodec(FileBasedSystemAccessControlRules.class).fromJson(json);

Target:
private static FileBasedSystemAccessControlRules parse(byte[] json) { ObjectMapper mapper = new ObjectMapperProvider().get() .enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); Class<FileBasedSystemAccessControlRules> javaType = FileBasedSystemAccessControlRules.class; try { return mapper.readValue(json, javaType); } catch (IOException e) { throw new IllegalArgumentException(format("Invalid JSON string for %s", javaType), e); } }
Source:
START private Block getNullBlock(Type type, int positionCount) END { Block nullValueBlock = type.createBlockBuilder(null, 1) .appendNull() .build(); return new RunLengthEncodedBlock(nullValueBlock, positionCount); } I think this can be static

Target:
private static Block getNullBlock(Type type, int positionCount) { Block nullValueBlock = type.createBlockBuilder(null, 1) .appendNull() .build(); return new RunLengthEncodedBlock(nullValueBlock, positionCount); }
Source:
private static boolean isDeterminateConstant(RowExpression expression, MethodHandle isIndeterminateFunction) { if (!(expression instanceof ConstantExpression)) { return false; } ConstantExpression constantExpression = (ConstantExpression) expression; Object value = constantExpression.getValue(); boolean isNull = value == null; if (isNull) { return false; } try { START return !(boolean) isIndeterminateFunction.invoke(value, isNull); END } catch (Throwable t) { throwIfUnchecked(t); throw new RuntimeException(t); } } Use `InterpretedFunctionInvoker` instead

Target:
private static boolean isDeterminateConstant(RowExpression expression, MethodHandle isIndeterminateFunction) { if (!(expression instanceof ConstantExpression)) { return false; } ConstantExpression constantExpression = (ConstantExpression) expression; Object value = constantExpression.getValue(); boolean isNull = value == null; if (isNull) { return false; } try { return !(boolean) isIndeterminateFunction.invoke(value, false); } catch (Throwable t) { throwIfUnchecked(t); throw new RuntimeException(t); } }
Source:
public static long xxHash64(@SqlType(StandardTypes.TINYINT) long value) { START return XxHash64.hash((byte) value); END } We do not unpack it for all the inequality operators. Because of that i assume that the assumption is that the higher bits are always zero (or the same). Therefore i don't think the downcast to `byte` is needed here.

Target:
public static long xxHash64(@SqlType(StandardTypes.TINYINT) long value) { return XxHash64.hash(value); }
Source:
public Optional<SystemTable> getSystemTable(ConnectorSession session, SchemaTableName tableName) { return getSourceTable(tableName) START .map(t -> (RaptorTableHandle) getTableHandle(t)) END .map(handle -> new ColumnRangesSystemTable(handle, dbi)); } If we change `getTableHandle` to return `RaptorTableHandle`, we can remove the cast and make this `map(this::getTableHandle)`

Target:
public Optional<SystemTable> getSystemTable(ConnectorSession session, SchemaTableName tableName) { return getSourceTable(tableName) .map(this::getTableHandle) .map(handle -> new ColumnRangesSystemTable(handle, dbi)); }
Source:
public List<BytecodeNode> getChildNodes() { START return null; END } So we decide this class to be a leaf ByteCode Node? (Alternative would be make this returns `ImmutableList.of(block, position)`

Target:
public List<BytecodeNode> getChildNodes() { return ImmutableList.of(); }
Source:
public void testDefaults() { START ConfigAssertions.assertRecordedDefaults(ConfigAssertions.recordDefaults(SqlEnvironmentConfig.class) END .setPath(" ")); } static import the assert methods on this line, so this becomes: ``` assertRecordedDefaults(recordDefaults(SqlEnvironmentConfig.class).setPath(" ")); ```

Target:
public void testDefaults() { assertRecordedDefaults(recordDefaults(SqlEnvironmentConfig.class) .setPath(null)); }
Source:
private static ExpressionRewriter createRewrite() { START return (expression, context) -> ExpressionTreeRewriter.rewriteWith(new com.facebook.presto.sql.tree.ExpressionRewriter<Void>() END { @Override public Expression rewriteCurrentPath(CurrentPath node, Void context, ExpressionTreeRewriter<Void> treeRewriter) { return DesugarCurrentPath.getCall(node); } }, expression); } No need to use the fully qualified name since you import this class already.

Target:
private static ExpressionRewriter createRewrite() { return (expression, context) -> rewriteWith(new com.facebook.presto.sql.tree.ExpressionRewriter<Void>() { @Override public Expression rewriteCurrentPath(CurrentPath node, Void context, ExpressionTreeRewriter<Void> treeRewriter) { return DesugarCurrentPath.getCall(node); } }, expression); }
Source:
START public SetPath(Optional<NodeLocation> location, PathSpecification pathSpecification) END { super(location); this.pathSpecification = requireNonNull(pathSpecification, "path is null"); } this is used only internally, so can be private.

Target:
private SetPath(Optional<NodeLocation> location, PathSpecification pathSpecification) { super(location); this.pathSpecification = requireNonNull(pathSpecification, "path is null"); }
Source:
START public PathElement(Optional<NodeLocation> location, Optional<Identifier> catalog, Identifier schema) END { super(location); this.catalog = requireNonNull(catalog, "catalog is null"); this.schema = requireNonNull(schema, "schema is null"); } can be private.

Target:
private PathElement(Optional<NodeLocation> location, Optional<Identifier> catalog, Identifier schema) { super(location); this.catalog = requireNonNull(catalog, "catalog is null"); this.schema = requireNonNull(schema, "schema is null"); }
Source:
public PhysicalOperation visitRemoteSource(RemoteSourceNode node, LocalExecutionPlanContext context) { if (node.getOrderingScheme().isPresent()) { return createMergeSource(node, context); } START else { END return createRemoteSource(node, context); } } nit: it seems like we prefer to not put `else` after `return` elsewhere in the codebase

Target:
public PhysicalOperation visitRemoteSource(RemoteSourceNode node, LocalExecutionPlanContext context) { if (node.getOrderingScheme().isPresent()) { return createMergeSource(node, context); } return createRemoteSource(node, context); }
Source:
public static List<SqlScalarFunction> parseFunctionDefinitions(Class<?> clazz) { START ImmutableList.Builder<SqlScalarFunction> builder = ImmutableList.builder(); END for (ScalarHeaderAndMethods methods : findScalarsInFunctionSetClass(clazz)) { // Non-static function only makes sense in classes annotated @ScalarFunction. builder.add(parseParametricScalar(methods, ImmutableMap.of())); } return builder.build(); } nit: Maybe more explicit: ``` Only function definition classes (classes annotated with @ScalarFunction) can define SQL functions via non-static java function. Function set classes can only define SQL functions via static java functions. Thus constructor in function set classes is used to define SQL functions. ```

Target:
public static List<SqlScalarFunction> parseFunctionDefinitions(Class<?> clazz) { ImmutableList.Builder<SqlScalarFunction> builder = ImmutableList.builder(); for (ScalarHeaderAndMethods methods : findScalarsInFunctionSetClass(clazz)) { // Non-static function only makes sense in classes annotated @ScalarFunction. builder.add(parseParametricScalar(methods, Optional.empty())); } return builder.build(); }
Source:
private static String getExpectedErrorMessageForInsertExistingBucketedTable(InsertExistingPartitionsBehavior behavior, String partitionName) { if (behavior == InsertExistingPartitionsBehavior.APPEND) { return "Cannot insert into existing partition of bucketed Hive table: " + partitionName; } START else if (behavior == InsertExistingPartitionsBehavior.ERROR) { END return "Cannot insert into an existing partition of Hive table: " + partitionName; } throw new IllegalArgumentException("Unexpected insertExistingPartitionsBehavior: " + behavior.name()); } These `else` is redundant

Target:
private static String getExpectedErrorMessageForInsertExistingBucketedTable(InsertExistingPartitionsBehavior behavior, String partitionName) { if (behavior == InsertExistingPartitionsBehavior.APPEND) { return "Cannot insert into existing partition of bucketed Hive table: " + partitionName; } if (behavior == InsertExistingPartitionsBehavior.ERROR) { return "Cannot insert into an existing partition of Hive table: " + partitionName; } throw new IllegalArgumentException("Unexpected insertExistingPartitionsBehavior: " + behavior); }
Source:
private static List<Type> toTypes(List<Type> groupByTypes, List<Aggregator> aggregates) { ImmutableList.Builder<Type> builder = ImmutableList.builder(); START builder.addAll(groupByTypes); END aggregates.stream().forEach(aggregator -> builder.add(aggregator.getType())); return builder.build(); } Do we want to support partial streaming aggregations now?

Target:
private static List<Type> toTypes(List<Type> groupByTypes, List<Aggregator> aggregates) { ImmutableList.Builder<Type> builder = ImmutableList.builder(); builder.addAll(groupByTypes); aggregates.stream() .map(Aggregator::getType) .forEach(builder::add); return builder.build(); }
Source:
public boolean isStreamable() { START if (!preGroupedSymbols.isEmpty() && groupingSets.size() == 1) { END List<Symbol> groupingSet = Iterables.getOnlyElement(groupingSets); return !groupingSet.isEmpty() && Sets.newHashSet(preGroupedSymbols).equals(Sets.newHashSet(groupingSet)); } return false; } don't you need to check that `aggregations.isEmpty()`?

Target:
public boolean isStreamable() { return !preGroupedSymbols.isEmpty() && groupingSets.size() == 1 && !Iterables.getOnlyElement(groupingSets).isEmpty(); }
Source:
public void checkCanCreateViewWithSelectFromColumns(TransactionId transactionId, Identity identity, QualifiedObjectName tableName, Set<String> columnNames) { if (shouldDenyPrivilege(identity.getUser(), tableName.getObjectName(), CREATE_VIEW_WITH_SELECT_COLUMNS)) { START denyCreateViewWithSelect(tableName.toString()); END } if (denyPrivileges.isEmpty()) { super.checkCanCreateViewWithSelectFromColumns(transactionId, identity, tableName, columnNames); } } Why change this but not the other `checkCanCreateViewXXX` methods?

Target:
public void checkCanCreateViewWithSelectFromColumns(TransactionId transactionId, Identity identity, QualifiedObjectName tableName, Set<String> columnNames) { if (shouldDenyPrivilege(identity.getUser(), tableName.getObjectName(), CREATE_VIEW_WITH_SELECT_COLUMNS)) { denyCreateViewWithSelect(tableName.toString(), identity); } if (denyPrivileges.isEmpty()) { super.checkCanCreateViewWithSelectFromColumns(transactionId, identity, tableName, columnNames); } }
Source:
public void addTableColumnReferences(AccessControl accessControl, Identity identity, Multimap<QualifiedObjectName, String> tableColumnMap) { AccessControlInfo accessControlInfo = new AccessControlInfo(accessControl, identity); START Map<QualifiedObjectName, Set<String>> references = tableColumnReferences.computeIfAbsent(accessControlInfo, (k) -> new LinkedHashMap<>()); END tableColumnMap.asMap() .forEach((key, value) -> references.computeIfAbsent(key, k -> new HashSet<>()).addAll(value)); } Nit: no need for parens around lambda parameter. Also, I think it can be replaced with `LinkedHashMap::new`

Target:
public void addTableColumnReferences(AccessControl accessControl, Identity identity, Multimap<QualifiedObjectName, String> tableColumnMap) { AccessControlInfo accessControlInfo = new AccessControlInfo(accessControl, identity); Map<QualifiedObjectName, Set<String>> references = tableColumnReferences.computeIfAbsent(accessControlInfo, k -> new LinkedHashMap<>()); tableColumnMap.asMap() .forEach((key, value) -> references.computeIfAbsent(key, k -> new HashSet<>()).addAll(value)); }
Source:
private void updateDataSize() { START retainedSizeInBytes = INSTANCE_SIZE + sizeOf(valueIsNull) + sizeOf(values) + NULL_VALUE_BLOCK.getRetainedSizeInBytes(); END if (blockBuilderStatus != null) { retainedSizeInBytes += BlockBuilderStatus.INSTANCE_SIZE; } } This is a constant shared across all instances, so should not be part of the retained size.

Target:
private void updateDataSize() { retainedSizeInBytes = INSTANCE_SIZE + sizeOf(valueIsNull) + sizeOf(values); if (blockBuilderStatus != null) { retainedSizeInBytes += BlockBuilderStatus.INSTANCE_SIZE; } }
Source:
void unexport(MBeanExporter exporter) { for (Class<?> rule : stats.keySet()) { String name = ObjectNames.builder(PlanOptimizer.class) .withProperty("optimizer", rule.getSimpleName()) START .build(); END exporter.unexport(name); } } Consider extracting object name building to a method to emphasize that export and unexport use the same naming strategy.

Target:
void unexport(MBeanExporter exporter) { for (Class<?> rule : stats.keySet()) { exporter.unexport(getName(rule)); } }
Source:
START public final boolean execute(String sql) END throws SQLException { if (connection().shouldStartTransaction()) { internalExecute(connection().getStartTransactionSql()); } return internalExecute(sql); } it needs to be overridden in the subclass

Target:
public boolean execute(String sql) throws SQLException { if (connection().shouldStartTransaction()) { internalExecute(connection().getStartTransactionSql()); } return internalExecute(sql); }
Source:
public static long stNumPoints(@SqlType(GEOMETRY_TYPE_NAME) Slice input) { START return GeometryUtils.getPointCount(deserialize(input)); END } static import

Target:
public static long stNumPoints(@SqlType(GEOMETRY_TYPE_NAME) Slice input) { return getPointCount(deserialize(input)); }
Source:
START default TableStatisticsMetadata getInsertIntoTableStatisticsMetadata(ConnectorSession session, ConnectorTableHandle tableHandle) END { return EMPTY_STATISTICS_METADATA; } Lets name this `getInsertStatisticsMetadata` to be consistent with `getInsertLayout`

Target:
default TableStatisticsMetadata getInsertIntoTableStatisticsMetadata(ConnectorSession session, ConnectorTableHandle tableHandle) { return TableStatisticsMetadata.empty(); }
Source:
private static int getHashPosition(Block keyBlock, int position, MethodHandle keyBlockHashCode, int hashTableSize) { if (keyBlock.isNull(position)) { throw new IllegalArgumentException("map keys cannot be null"); } long hashCode; try { hashCode = (long) keyBlockHashCode.invokeExact(keyBlock, position); } catch (Throwable throwable) { if (throwable instanceof RuntimeException) { throw (RuntimeException) throwable; } throw new RuntimeException(throwable); } return (int) Math.floorMod(hashCode, hashTableSize); START } END ``` long hashCode; try { hashCode = (long) keyBlockHashCode.invokeExact(keyBlock, position); } catch (RuntimeException e) { throw e; } catch (Throwable throwable) { throw new RuntimeException(throwable); }

Target:
private static int getHashPosition(Block keyBlock, int position, MethodHandle keyBlockHashCode, int hashTableSize) { if (keyBlock.isNull(position)) { throw new IllegalArgumentException("map keys cannot be null"); } long hashCode; try { hashCode = (long) keyBlockHashCode.invokeExact(keyBlock, position); } catch (RuntimeException e) { throw e; } catch (Throwable throwable) { throw new RuntimeException(throwable); } return (int) Math.floorMod(hashCode, hashTableSize); }
Source:
private static boolean coordinateInsideBoundary(int coordinate, long maxTileIndex) { START return (coordinate >= 0 && coordinate <= maxTileIndex); END } brackets are extra here

Target:
private static boolean coordinateInsideBoundary(int coordinate, long maxTileIndex) { return coordinate >= 0 && coordinate <= maxTileIndex; }
Source:
public FileBasedSystemAccessControlRules( @JsonProperty("catalogs") Optional<List<CatalogAccessControlRule>> catalogRules, @JsonProperty("principals") Optional<List<PrincipalUserMatchRule>> principalUserMatchRules) { this.catalogRules = catalogRules.map(ImmutableList::copyOf).orElse(ImmutableList.of()); START this.principalUserMatchRules = principalUserMatchRules.map(ImmutableList::copyOf).orElse(ImmutableList.of()); END } `principalUserMatchRules` could remain `Optional<List<..>>`

Target:
public FileBasedSystemAccessControlRules( @JsonProperty("catalogs") Optional<List<CatalogAccessControlRule>> catalogRules, @JsonProperty("principals") Optional<List<PrincipalUserMatchRule>> principalUserMatchRules) { this.catalogRules = catalogRules.map(ImmutableList::copyOf).orElse(ImmutableList.of()); this.principalUserMatchRules = principalUserMatchRules.map(ImmutableList::copyOf); }
Source:
public String toString() { ToStringHelper helper = toStringHelper(this) .add("name", name) .add("ordinalPosition", ordinalPosition) .add("type", type); START return helper.toString(); END } please inline the `helper`

Target:
public String toString() { return toStringHelper(this) .add("name", name) .add("ordinalPosition", ordinalPosition) .add("type", type) .toString(); }
Source:
public ConnectorTableHandle beginDelete(ConnectorSession session, ConnectorTableHandle tableHandle) { START KuduTableHandle kuduTableHandle = fromConnectorTableHandle(session, tableHandle); END return kuduTableHandle; } just cast here

Target:
public ConnectorTableHandle beginDelete(ConnectorSession session, ConnectorTableHandle tableHandle) { return tableHandle; }
Source:
public boolean isFinished() { if (finished) { return true; } START boolean finished = finishing && probe == null && pageBuilder.isEmpty() && outputPage == null; END // if finished drop references so memory is freed early if (finished) { close(); } return finished; } > If you do a join with an empty probe the operator waits until the build finishes, which could take hours. i was aware of this limitation and we've been discussing this with @losipiuk last summer. Question is -- how much of a problem is this? Ideally, we should be quite good at knowing which side of the join is bigger, so empty probe side and hours-worth of the build side is not something we can be proud of.

Target:
public boolean isFinished() { boolean finished = this.finished && probe == null && pageBuilder.isEmpty() && outputPage == null; // if finished drop references so memory is freed early if (finished) { close(); } return finished; }
Source:
private static ListenableFuture<?> firstFinishedFuture(List<ListenableFuture<?>> futures) { START if (futures.size() == 1) { END return futures.get(0); } SettableFuture<?> result = SettableFuture.create(); ExecutorService executor = newDirectExecutorService(); for (ListenableFuture<?> future : futures) { future.addListener(() -> result.set(null), executor); } return result; } This function appears like duplicate of `whenAnyComplete` in airlift `MoreFutures`. What about using that instead?

Target:
private static ListenableFuture<?> firstFinishedFuture(List<ListenableFuture<?>> futures) { SettableFuture<?> result = SettableFuture.create(); ExecutorService executor = newDirectExecutorService(); for (ListenableFuture<?> future : futures) { future.addListener(() -> result.set(null), executor); } return result; }
Source:
public void testDefaults() { START ConfigAssertions.assertRecordedDefaults(ConfigAssertions.recordDefaults(JsonWebTokenConfig.class) END .setKey(null) .setRequiredAudience(null) .setRequiredIssuer(null)); } Static import the assertion methods

Target:
public void testDefaults() { assertRecordedDefaults(ConfigAssertions.recordDefaults(JsonWebTokenConfig.class) .setKey(null) .setRequiredAudience(null) .setRequiredIssuer(null)); }
Source:
private List<String> getSchemaNames(ConnectorSession session, Optional<String> schemaName) { if (!schemaName.isPresent()) { return listSchemaNames(session); } START else if (schemaNameToScaleFactor(schemaName.get()) > 0) { END return ImmutableList.of(schemaName.get()); } return ImmutableList.of(); } `else` is redundant

Target:
private List<String> getSchemaNames(ConnectorSession session, Optional<String> schemaName) { if (!schemaName.isPresent()) { return listSchemaNames(session); } if (schemaNameToScaleFactor(schemaName.get()) > 0) { return ImmutableList.of(schemaName.get()); } return ImmutableList.of(); }
Source:
private static void assertGeometryEquals(Geometry actual, Geometry expected) { START if (expected.isEmpty()) { END assertTrue(actual.isEmpty()); } else { assertEquals(actual.norm(), expected.norm()); } } Since earlier commits enhanced serialization to avoid losing multi-types this test can be simplified to `assertEquals(actual.norm(), expected.norm());` in all cases.

Target:
private static void assertGeometryEquals(Geometry actual, Geometry expected) { assertEquals(actual.norm(), expected.norm()); }
Source:
private static Envelope getEnvelope(BasicSliceInput input, GeometryType type, int length) { switch (type) { case POINT: return getPointEnvelope(input); case MULTI_POINT: case LINE_STRING: case MULTI_LINE_STRING: case POLYGON: case MULTI_POLYGON: return getSimpleGeometryEnvelope(input, length); case GEOMETRY_COLLECTION: return getGeometryCollectionOverallEnvelope(input); default: START throw new UnsupportedOperationException("Unexpected type: " + type); END } } In other switches `IllegalArgumentException` is thrown, which I think is a better fit to here.

Target:
private static Envelope getEnvelope(BasicSliceInput input, GeometryType type, int length) { switch (type) { case POINT: return getPointEnvelope(input); case MULTI_POINT: case LINE_STRING: case MULTI_LINE_STRING: case POLYGON: case MULTI_POLYGON: return getSimpleGeometryEnvelope(input, length); case GEOMETRY_COLLECTION: return getGeometryCollectionOverallEnvelope(input); default: throw new IllegalArgumentException("Unexpected type: " + type); } }
Source:
public List<SchemaTableName> listTables(ConnectorSession session, String schemaNameOrNull) { Stream<SchemaTableName> stream = tables.listSystemTables(session).stream() .map(SystemTable::getTableMetadata) .map(ConnectorTableMetadata::getTable); if (schemaNameOrNull != null) { stream = stream START .filter(table -> table.getSchemaName().equals(schemaNameOrNull)); END } return stream.collect(toImmutableList()); } You could extract this to a method: ``` .stream() ... .filter(tableSchemaMatcher(schemaNameOrNull)) .collect ```

Target:
public List<SchemaTableName> listTables(ConnectorSession session, String schemaNameOrNull) { return tables.listSystemTables(session).stream() .map(SystemTable::getTableMetadata) .map(ConnectorTableMetadata::getTable) .filter(table -> schemaNameOrNull == null || table.getSchemaName().equals(schemaNameOrNull)) .collect(toImmutableList()); }
Source:
public DelegatingSystemTablesProvider(List<SystemTablesProvider> delegates) { requireNonNull(delegates, "delegates is null"); START Preconditions.checkArgument(delegates.size() >= 1, "empty delegates"); END this.delegates = ImmutableList.copyOf(delegates); } - static import - why is having no delegates an error?

Target:
public DelegatingSystemTablesProvider(List<SystemTablesProvider> delegates) { requireNonNull(delegates, "delegates is null"); checkArgument(delegates.size() >= 1, "empty delegates"); this.delegates = ImmutableList.copyOf(delegates); }
Source:
public static boolean isPartitionsSystemTable(SchemaTableName tableName) { START return tableName.getTableName().endsWith(PARTITIONS_TABLE_SUFFIX) && !tableName.getTableName().equals(PARTITIONS_TABLE_SUFFIX); END } `... && tableName.getTableName().length() > PARTITIONS_TABLE_SUFFIX.length()` ?

Target:
public static boolean isPartitionsSystemTable(SchemaTableName tableName) { return tableName.getTableName().endsWith(PARTITIONS_TABLE_SUFFIX) && tableName.getTableName().length() > PARTITIONS_TABLE_SUFFIX.length(); }
Source:
SymbolMapper getCorrelatedSymbolMapper() { return new SymbolMapper(correlatedSymbolsMapping.asMap().entrySet().stream() START .collect(ImmutableMap.toImmutableMap(Map.Entry::getKey, symbols -> Iterables.getLast(symbols.getValue())))); END } static import

Target:
SymbolMapper getCorrelatedSymbolMapper() { return new SymbolMapper(correlatedSymbolsMapping.asMap().entrySet().stream() .collect(toImmutableMap(Map.Entry::getKey, symbols -> Iterables.getLast(symbols.getValue())))); }
Source:
public Optional<DecorrelationResult> visitEnforceSingleRow(EnforceSingleRowNode node, Void context) { Optional<DecorrelationResult> childDecorrelationResultOptional = lookup.resolve(node.getSource()).accept(this, null); START if (!childDecorrelationResultOptional.isPresent() || !childDecorrelationResultOptional.get().atMostSingleRow) { END return Optional.empty(); } return childDecorrelationResultOptional; } use `return childDecorrelationResultOptional.filter(DecorrelationResult::isAtMostSingleRow);`

Target:
public Optional<DecorrelationResult> visitEnforceSingleRow(EnforceSingleRowNode node, Void context) { Optional<DecorrelationResult> childDecorrelationResultOptional = lookup.resolve(node.getSource()).accept(this, null); return childDecorrelationResultOptional.filter(result -> result.atMostSingleRow); }
Source:
public static void main(String[] args) throws IOException, RunnerException { BenchmarkData data = new BenchmarkData(); data.setup(); START data.validate(); END Options options = new OptionsBuilder() .verbosity(VerboseMode.NORMAL) .include(".*" + BenchmarkSTIntersects.class.getSimpleName() + ".*") .build(); new Runner(options).run(); } nit: @martint Martin told me a trick of adding @Test method with validation logic. This ensures that benchmark code stays valid even if benchmark is not run.

Target:
public static void main(String[] args) throws RunnerException { Options options = new OptionsBuilder() .verbosity(VerboseMode.NORMAL) .include(".*" + BenchmarkSTIntersects.class.getSimpleName() + ".*") .build(); new Runner(options).run(); }
Source:
private static Type getInt32Type(RichColumnDescriptor descriptor) { OriginalType originalType = descriptor.getPrimitiveType().getOriginalType(); if (originalType == null) { return IntegerType.INTEGER; } switch (originalType) { case DECIMAL: return createDecimalType(descriptor.getPrimitiveType().getDecimalMetadata()); case DATE: return DateType.DATE; default: START return IntegerType.INTEGER; END } } static import `INTEGER`.

Target:
private static Type getInt32Type(RichColumnDescriptor descriptor) { OriginalType originalType = descriptor.getPrimitiveType().getOriginalType(); if (originalType == null) { return INTEGER; } switch (originalType) { case DECIMAL: return createDecimalType(descriptor.getPrimitiveType().getDecimalMetadata()); case DATE: return DateType.DATE; default: return INTEGER; } }
Source:
public static void combine(HistogramState state, HistogramState otherState) { // NOTE: state = current merged state; otherState = scratchState (new data to be added) // since we have a single instance of a histogram, it will never be non-null. Even asking for a histogram that does not exist returns the histogram object. It doesn't // "exist" until we call add() on it,though START requireNonNull(state.get() != null, "this.typedHistogram should always be non-null"); END requireNonNull(otherState.get() != null, "scratch state should always be non-null"); TypedHistogram typedHistogram = state.get(); long startSize = typedHistogram.getEstimatedSize(); typedHistogram.addAll(otherState.get()); state.addMemoryUsage(typedHistogram.getEstimatedSize() - startSize); } nit: `current merged state should always be non-null`

Target:
public static void combine(HistogramState state, HistogramState otherState) { // NOTE: state = current merged state; otherState = scratchState (new data to be added) // for grouped histograms and single histograms, we have a single histogram object. In neither case, can otherState.get() return null. // Semantically, a histogram object will be returned even if the group is empty. // In that case, the histogram object will represent an empty histogram until we call add() on // it. requireNonNull(otherState.get() != null, "scratch state should always be non-null"); TypedHistogram typedHistogram = state.get(); long startSize = typedHistogram.getEstimatedSize(); typedHistogram.addAll(otherState.get()); state.addMemoryUsage(typedHistogram.getEstimatedSize() - startSize); }
Source:
public void testShowTablesLikeWithEscape() { //Cassandra connector currently does not support create table, so here override START MaterializedResult withEscape = computeActual("SHOW TABLES LIKE '%$_%' ESCAPE '$'"); END assertThat(withEscape.getOnlyColumnAsSet()).allMatch(tableName -> ((String) tableName).contains("_")); } `s/withEscape/result/`

Target:
public void testShowTablesLikeWithEscape() { // Cassandra connector currently does not support create table }
Source:
public void testShowTablesLikeWithEscape() { assertQueryFails("SHOW TABLES IN a LIKE '%$_%' ESCAPE", "line 1:36: no viable alternative at input '<EOF>'"); assertQueryFails("SHOW TABLES LIKE 't$_%' ESCAPE ''", "Escape string must be a single character"); assertQueryFails("SHOW TABLES LIKE 't$_%' ESCAPE '$$'", "Escape string must be a single character"); try { assertUpdate("CREATE TABLE test_escape_1 (a bigint)"); assertUpdate("CREATE TABLE test_escape11 (a bigint)"); } catch (RuntimeException e) { assertTrue(ImmutableSet.of("This connector does not support creating tables", "Unsupported statement type CreateTable") .contains(e.getMessage())); START return; END } assertQuery("SHOW TABLES LIKE 'test_escape_%'", "VALUES 'test_escape_1', 'test_escape11'"); assertQuery("SHOW TABLES LIKE 'test_escape\\_%' ESCAPE '\\'", "VALUES 'test_escape_1'"); assertUpdate("DROP TABLE test_escape_1"); assertUpdate("DROP TABLE test_escape11"); } I am not particularly in favor of this lenient approach, as it may mask other (run-time) problems, not just inability to create table. I would recommend adding overrides to all relevant connectors, just as you did in cassandra case. (Or, you could introduce (in a separate, preparatory commit) a `supportsCreateTable` method (see `com.facebook.presto.tests.AbstractTestDistributedQueries#supportsViews`). )

Target:
public void testShowTablesLikeWithEscape() { assertQueryFails("SHOW TABLES IN a LIKE '%$_%' ESCAPE", "line 1:36: no viable alternative at input '<EOF>'"); assertQueryFails("SHOW TABLES LIKE 't$_%' ESCAPE ''", "Escape string must be a single character"); assertQueryFails("SHOW TABLES LIKE 't$_%' ESCAPE '$$'", "Escape string must be a single character"); assertUpdate("CREATE TABLE test_escape_1 (a bigint)"); assertUpdate("CREATE TABLE test_escape11 (a bigint)"); assertQuery("SHOW TABLES LIKE 'test_escape_%'", "VALUES 'test_escape_1', 'test_escape11'"); assertQuery("SHOW TABLES LIKE 'test_escape\\_%' ESCAPE '\\'", "VALUES 'test_escape_1'"); assertUpdate("DROP TABLE test_escape_1"); assertUpdate("DROP TABLE test_escape11"); }
Source:
START synchronized Collection<ClientBuffer> safeGetBuffersSnapshot() END { return ImmutableList.copyOf(this.buffers.values()); } You don't need this any more.

Target:
private synchronized Collection<ClientBuffer> safeGetBuffersSnapshot() { return ImmutableList.copyOf(this.buffers.values()); }
Source:
private static void testSerialization(String wkt) { OGCGeometry geometry = OGCGeometry.fromText(wkt); START geometry.setSpatialReference(null); END OGCGeometry deserializedGeometry = deserialize(serialize(geometry)); assertEquals(geometry.asText(), deserializedGeometry.asText()); } this code seems unnecessary

Target:
private static void testSerialization(String wkt) { OGCGeometry geometry = OGCGeometry.fromText(wkt); OGCGeometry deserializedGeometry = deserialize(serialize(geometry)); if (geometry.isEmpty()) { assertTrue(deserializedGeometry.isEmpty()); } else { assertEquals(geometry.asText(), deserializedGeometry.asText()); } }
Source:
START protected String getWarningMessage(PrestoWarning warning) END { // If this is a real terminal color the warnings yellow return REAL_TERMINAL ? format("%sWARNING: %s%s", WARNING_BEGIN, warning.getMessage(), WARNING_END) : format("WARNING: %s", warning.getMessage()); } why do you need this flag? This is only used by `printWithInitialSeparator` (why don't other methods need it if this is needed?).

Target:
private String getWarningMessage(PrestoWarning warning) { // If this is a real terminal color the warnings yellow if (REAL_TERMINAL) { return format("%sWARNING: %s%s", WARNING_BEGIN, warning.getMessage(), WARNING_END); } return format("WARNING: %s", warning.getMessage()); }
Source:
private static void assertStartsWithExpectedWarnings(SQLWarning warning, SQLWarning expected) { // Warnings generated with the testing warning collector may have more warnings than expected // due to getWarnings being called multiple times in the StatementClient implementation if (warning == null) { assertNull(expected); return; } assertNotNull(expected); while (true) { assertWarningsEqual(warning, expected); warning = warning.getNextWarning(); expected = expected.getNextWarning(); if (expected == null) { return; } START else { END assertNotNull(warning); } } } unnecessary else

Target:
private static void assertStartsWithExpectedWarnings(SQLWarning warning, SQLWarning expected) { assertNotNull(expected); assertNotNull(warning); while (true) { assertWarningsEqual(warning, expected); warning = warning.getNextWarning(); expected = expected.getNextWarning(); if (expected == null) { return; } assertNotNull(warning); } }
Source:
public ArrayAggregationState createGroupedState() { if (mode == NEW) { return new GroupArrayAggregationState(type); } if (mode == LEGACY) { return new LegacyArrayAggregationGroupState(type); } START throw new PrestoException(FUNCTION_IMPLEMENTATION_ERROR, format("Unexpected group enum type %s", mode)); END } Use `switch` instead of `if`s

Target:
public ArrayAggregationState createGroupedState() { switch (mode) { case NEW: return new GroupArrayAggregationState(type); case LEGACY: return new LegacyArrayAggregationGroupState(type); default: throw new PrestoException(FUNCTION_IMPLEMENTATION_ERROR, format("Unexpected group enum type %s", mode)); } }