auphong2707/dl4se-code-review-dataset huggingface dataset:
Source:
public boolean matches(DataSourceMetadata other) { if (getClass() != other.getClass()) { return false; } DerivativeDataSourceMetadata that = (DerivativeDataSourceMetadata) other; if (!baseDataSource.equals(that.getBaseDataSource()) || !metrics.equals(that.getMetrics())) { return false; } return dimensions.equals(that.getDimensions()); START } END nit: please break a line.

Target:
public boolean matches(DataSourceMetadata other) { return equals(other); }
Source:
private boolean hasEnoughLag(Interval target, Interval maxInterval) { START if ((target.getStartMillis() + minDataLagMs) > maxInterval.getStartMillis()) { END return false; } else { return true; } } Could just return the boolean expression

Target:
private boolean hasEnoughLag(Interval target, Interval maxInterval) { return minDataLagMs <= (maxInterval.getStartMillis() - target.getStartMillis()); }
Source:
public QueryRunner<T> getRunner(QuerySegmentWalker walker) { START return getQuerySegmentSpecForLookUp().lookup(this, walker); END } nit: if we keep this, should be "ForLookup" (U is not capital)

Target:
public QueryRunner<T> getRunner(QuerySegmentWalker walker) { return getQuerySegmentSpecForLookUp(this).lookup(this, walker); }
Source:
private NumericExprEval(@Nullable Number value) { START super(value == null ? NullHandling.defaultDoubleValue() : value); END } I think it should be specific for Double/Long

Target:
private NumericExprEval(@Nullable Number value) { super(value); }
Source:
public Object finalizeComputation(@Nullable Object object) { START return object == null ? object : ((SerializablePair<Long, Float>) object).rhs; END } Double* variant of this class uses `object == null ? null` that is better

Target:
public Object finalizeComputation(@Nullable Object object) { return object == null ? null : ((SerializablePair<Long, Float>) object).rhs; }
Source:
public boolean eval(Row row) { Object metricVal = row.getRaw(aggregationName); if (metricVal == null || value == null) { return metricVal == null && value == null; } START return HavingSpecMetricComparator.compare(row, aggregationName, value, aggregators) == 0; END } Could it optimize to not perform row.getRaw() twice?

Target:
public boolean eval(Row row) { Object metricVal = row.getRaw(aggregationName); if (metricVal == null || value == null) { return metricVal == null && value == null; } return HavingSpecMetricComparator.compare(aggregationName, value, aggregators, metricVal) == 0; }
Source:
public boolean hasNext() { init(); if (jp.isClosed()) { return false; } START if (jp.getCurrentToken() == JsonToken.END_ARRAY) { END CloseQuietly.close(jp); return false; } return true; } Looks like this iterator is supposed to read only an array of JSONs. Then, I think it should be checked that the first token is a `JsonToken.START_ARRAY`.

Target:
public boolean hasNext() { if (jp.isClosed()) { return false; } if (jp.getCurrentToken() == JsonToken.END_ARRAY) { CloseQuietly.close(jp); return false; } return true; }
Source:
protected long download(T object, File outFile) throws IOException { try { openObjectFunction.open(object, outFile); } START catch (Exception e) { END throw new RuntimeException(e); } return outFile.length(); } Same here. The only possible checked exception here is `IOException` and can be propagated. Please don't catch all types of exceptions. They will be propagated anyway.

Target:
protected long download(T object, File outFile) throws IOException { openObjectFunction.open(object, outFile); return outFile.length(); }
Source:
public <T> T retryWithHandle( HandleCallback<T> callback, Predicate<Throwable> myShouldRetry ) { try { return RetryUtils.retry(() -> getDBI().withHandle(callback), myShouldRetry, MAX_RETRIES); } catch (Exception e) { START throw Throwables.propagate(e); END } } Please change to `new RuntimeException(e)` (https://github.com/google/guava/wiki/Why-we-deprecated-Throwables.propagate).

Target:
public <T> T retryWithHandle( HandleCallback<T> callback, Predicate<Throwable> myShouldRetry ) { try { return RetryUtils.retry(() -> getDBI().withHandle(callback), myShouldRetry, MAX_RETRIES); } catch (Exception e) { throw new RuntimeException(e); } }
Source:
START JsonIterator getNextLineIterator() END { if (lineIterator != null) { lineIterator = null; } final JsonIterator iterator = resultIterator.next(); return iterator; } make this method private ?

Target:
private JsonIterator getNextLineIterator() { if (lineIterator != null) { lineIterator = null; } final JsonIterator iterator = resultIterator.next(); return iterator; }
Source:
START public static native int posix_fadvise(int fd, long offset, long len, int flag) throws LastErrorException; END private NativeIO() can this just be private?

Target:
private static native int posix_fadvise(int fd, long offset, long len, int flag) throws LastErrorException; private NativeIO()
Source:
private static Field getFieldByReflection(Class cls, String fieldName) { Field field = null; try { field = cls.getDeclaredField(fieldName); field.setAccessible(true); } catch (Exception e) { START log.error("Unable to read [%s] field from [%s]", fieldName, cls.getName()); END } return field; } Since this is recoverable, can it just be `info`?

Target:
private static Field getFieldByReflection(Class cls, String fieldName) { Field field = null; try { field = cls.getDeclaredField(fieldName); field.setAccessible(true); } catch (Exception e) { log.warn("Unable to read [%s] field from [%s]", fieldName, cls.getName()); } return field; }
Source:
public int getPartition(LongWritable interval, BytesWritable text, int numPartitions) { final String jobTrackerString = config.get("mapred.job.tracker"); START if ((jobTrackerString != null && jobTrackerString.equals("local")) || determineIntervals) { END return 0; } else { return reducerLookup.get(interval); } } `"local".equals(config.get("mapred.job.tracker"))`

Target:
public int getPartition(LongWritable interval, BytesWritable text, int numPartitions) { if ("local".equals(config.get("mapred.job.tracker")) || determineIntervals) { return 0; } else { return reducerLookup.get(interval); } }
Source:
private void initDimensionHandlers() { for (String dim : availableDimensions) { if (Strings.isNullOrEmpty(dim)) { continue; START } END ColumnCapabilities capabilities = getColumn(dim).getCapabilities(); DimensionHandler handler = DimensionHandlerUtils.getHandlerFromCapabilities(dim, capabilities, null); dimensionHandlers.put(dim, handler); } } This is not needed if availableDimensions are filtered before

Target:
private void initDimensionHandlers() { for (String dim : availableDimensions) { ColumnCapabilities capabilities = getColumn(dim).getCapabilities(); DimensionHandler handler = DimensionHandlerUtils.getHandlerFromCapabilities(dim, capabilities, null); dimensionHandlers.put(dim, handler); } }
Source:
public ImmutableBitmap fromByteBuffer(ByteBuffer buffer, int numBytes) { START if (numBytes == 0) { END return null; } buffer.limit(buffer.position() + numBytes); return new WrappedImmutableRoaringBitmap(new ImmutableRoaringBitmap(buffer)); } Nullable

Target:
public ImmutableBitmap fromByteBuffer(ByteBuffer buffer, int numBytes) { buffer.limit(buffer.position() + numBytes); return new WrappedImmutableRoaringBitmap(new ImmutableRoaringBitmap(buffer)); }
Source:
public GenericColumn get() { START if (nullValueBitmap.isEmpty()) { END return new DoublesColumn(column.get()); } else { return new DoublesColumnWithNulls(column.get(), nullValueBitmap); } } I suggest to move this logic from this place to a static factory method in `DoublesColumn`. `DoublesColumn`'s constructor is private, `DoublesColumnWithNulls` class is package-private. Same for Long and Float.

Target:
public GenericColumn get() { return DoublesColumn.create(column.get(), nullValueBitmap); }
Source:
public final int asInt() { START assert NullHandling.replaceWithDefault() || !isNull(); END return value.intValue(); } I don't think this check is needed here, because isNull() is implemented as `value == null`, so without this check, the next line will throw NullPointerException anyway. Assert check is needed where without that assertion check, exception doesn't happen. Same for long/double

Target:
public final int asInt() { return value.intValue(); }
Source:
public final int asInt() { if (value == null) { START assert NullHandling.replaceWithDefault(); END return 0; } final Integer theInt = Ints.tryParse(value); return theInt == null ? 0 : theInt; } Also need check in line 257

Target:
public final int asInt() { if (value == null) { assert NullHandling.replaceWithDefault(); return 0; } final Integer theInt = Ints.tryParse(value); assert NullHandling.replaceWithDefault() || theInt != null; return theInt == null ? 0 : theInt; }
Source:
public final double asDouble() { if (value == null) { assert NullHandling.replaceWithDefault(); START return 0.0; END } final Double theDouble = Doubles.tryParse(value); return theDouble == null ? 0.0 : theDouble; } Please review the rest of `ExprEval` for such things

Target:
public final double asDouble() { if (value == null) { assert NullHandling.replaceWithDefault(); return 0.0; } final Double theDouble = Doubles.tryParse(value); assert NullHandling.replaceWithDefault() || theDouble != null; return theDouble == null ? 0.0 : theDouble; }
Source:
START protected FloatsColumn(final ColumnarFloats column) END { this.column = column; } Please align with Double (no protected)

Target:
FloatsColumn(final ColumnarFloats column) { this.column = column; }
Source:
public boolean isNull() { START return getObject() == null; END } According to the current way how those methods are implemented, it should be `return false`

Target:
public boolean isNull() { return getObject().isNull(); }
Source:
private boolean isConnectionReset(Throwable t) { START return (t instanceof SocketException && (t.getMessage().contains("Connection reset"))) || END (t.getCause() != null && isConnectionReset(t.getCause())); } Message may be `null`, add a guard check

Target:
private boolean isConnectionReset(Throwable t) { return (t instanceof SocketException && (t.getMessage() != null && t.getMessage().contains("Connection reset"))) || (t.getCause() != null && isConnectionReset(t.getCause())); }
Source:
public <T> T retryTransaction(final TransactionCallback<T> callback, final int quietTries, final int maxTries) { try { START return RetryUtils.retry(() -> getDBI().inTransaction(callback), shouldRetry, quietTries, maxTries, null, null); END } catch (Exception e) { throw Throwables.propagate(e); } } Shouldn't pass `null`? There is an overload

Target:
public <T> T retryTransaction(final TransactionCallback<T> callback, final int quietTries, final int maxTries) { try { return RetryUtils.retry(() -> getDBI().inTransaction(callback), shouldRetry, quietTries, maxTries); } catch (Exception e) { throw Throwables.propagate(e); } }
Source:
public ComplexMetricExtractor getExtractor() { return new ComplexMetricExtractor() { @Override public Class<?> extractedClass() { START return ArrayOfDoublesSketch.class; END } @Override public Object extractValue(final InputRow inputRow, final String metricName) { final Object object = inputRow.getRaw(metricName); if (object == null || object instanceof ArrayOfDoublesSketch || object instanceof Memory) { return object; } return ArrayOfDoublesSketchOperations.deserialize(object); } }; } From the code of extractValue(), it could also be Memory.

Target:
public ComplexMetricExtractor getExtractor() { return new ComplexMetricExtractor() { @Override public Class<?> extractedClass() { return ArrayOfDoublesSketch.class; } @Override public Object extractValue(final InputRow inputRow, final String metricName) { final Object object = inputRow.getRaw(metricName); if (object == null || object instanceof ArrayOfDoublesSketch) { return object; } return ArrayOfDoublesSketchOperations.deserialize(object); } }; }
Source:
public boolean equals(final Object o) { if (!super.equals(o)) { return false; } START final ArrayOfDoublesSketchMultiPostAggregator that = (ArrayOfDoublesSketchMultiPostAggregator) o; END return fields.equals(that.fields); } It breaks symmetry of equals() and could be subject to ClassCastException.

Target:
public boolean equals(final Object o) { if (!super.equals(o)) { return false; } // this check is used here instead of instanceof because this is an abstract class // and subclasses not overriding equals should not be equal to each other if (o == null || getClass() != o.getClass()) { return false; } final ArrayOfDoublesSketchMultiPostAggregator that = (ArrayOfDoublesSketchMultiPostAggregator) o; return fields.equals(that.fields); }
Source:
public ArrayOfDoublesSketchBuildAggregator( final DimensionSelector keySelector, final List<BaseDoubleColumnValueSelector> valueSelectors, final int nominalEntries ) { this.keySelector = keySelector; START this.valueSelectors = valueSelectors.toArray(new BaseDoubleColumnValueSelector[valueSelectors.size()]); END values = new double[valueSelectors.size()]; sketch = new ArrayOfDoublesUpdatableSketchBuilder().setNominalEntries(nominalEntries) .setNumberOfValues(valueSelectors.size()).build(); } According to https://shipilev.net/blog/2016/arrays-wisdom-ancients/, better off just `new BaseDoubleColumnValueSelector[0]`

Target:
public ArrayOfDoublesSketchBuildAggregator( final DimensionSelector keySelector, final List<BaseDoubleColumnValueSelector> valueSelectors, final int nominalEntries ) { this.keySelector = keySelector; this.valueSelectors = valueSelectors.toArray(new BaseDoubleColumnValueSelector[0]); values = new double[valueSelectors.size()]; sketch = new ArrayOfDoublesUpdatableSketchBuilder().setNominalEntries(nominalEntries) .setNumberOfValues(valueSelectors.size()).build(); }
Source:
START public synchronized void aggregate() END { final ArrayOfDoublesSketch update = selector.getObject(); if (update == null) { return; } union.update(update); } Are locks needed in Build and Merge Aggregators alike?

Target:
public void aggregate() { final ArrayOfDoublesSketch update = selector.getObject(); if (update == null) { return; } synchronized (this) { union.update(update); } }
Source:
public static BasicAuthenticatorMetadataStorageUpdater createAuthenticatorStorageUpdater(final Injector injector) { if (isCoordinator(injector)) { return injector.getInstance(CoordinatorBasicAuthenticatorMetadataStorageUpdater.class); } else { START return injector.getInstance(NoopBasicAuthenticatorMetadataStorageUpdater.class); END } } Is this needed? I think it's better to not inject anything.

Target:
public static BasicAuthenticatorMetadataStorageUpdater createAuthenticatorStorageUpdater(final Injector injector) { if (isCoordinator(injector)) { return injector.getInstance(CoordinatorBasicAuthenticatorMetadataStorageUpdater.class); } else { return null; } }
Source:
public BasicAuthorizerPermission( ResourceAction resourceAction ) { this.resourceAction = resourceAction; try { this.resourceNamePattern = Pattern.compile(resourceAction.getResource().getName()); } catch (PatternSyntaxException pse) { START throw new BasicSecurityDBResourceException( END "Invalid permission, resource name regex[%s] does not compile.", resourceAction.getResource().getName() ); } } ```pse``` should be included.

Target:
public BasicAuthorizerPermission( ResourceAction resourceAction ) { this.resourceAction = resourceAction; try { this.resourceNamePattern = Pattern.compile(resourceAction.getResource().getName()); } catch (PatternSyntaxException pse) { throw new BasicSecurityDBResourceException( pse, "Invalid permission, resource name regex[%s] does not compile.", resourceAction.getResource().getName() ); } }
Source:
public static byte[] serializeAuthenticatorUserMap( ObjectMapper objectMapper, Map<String, BasicAuthenticatorUser> userMap ) { try { return objectMapper.writeValueAsBytes(userMap); } catch (IOException ioe) { START throw new ISE("WTF? Couldn't serialize userMap!"); END } } Please add ```ioe``` to the exception.

Target:
public static byte[] serializeAuthenticatorUserMap( ObjectMapper objectMapper, Map<String, BasicAuthenticatorUser> userMap ) { try { return objectMapper.writeValueAsBytes(userMap); } catch (IOException ioe) { throw new ISE(ioe, "WTF? Couldn't serialize userMap!"); } }
Source:
START private void writeUserMapToDisk(String prefix, byte[] userMapBytes) throws Exception END { File cacheDir = new File(commonCacheConfig.getCacheDirectory()); cacheDir.mkdirs(); File userMapFile = new File(commonCacheConfig.getCacheDirectory(), getUserMapFilename(prefix)); Files.write(userMapBytes, userMapFile); } ```throws Exception``` can be reduced to ```throws IOException```.

Target:
private void writeUserMapToDisk(String prefix, byte[] userMapBytes) throws IOException { File cacheDir = new File(commonCacheConfig.getCacheDirectory()); cacheDir.mkdirs(); File userMapFile = new File(commonCacheConfig.getCacheDirectory(), getUserMapFilename(prefix)); Files.write(userMapBytes, userMapFile); }
Source:
START private Map<String, BasicAuthenticatorUser> loadUserMapFromDisk(String prefix) throws Exception END { File userMapFile = new File(commonCacheConfig.getCacheDirectory(), getUserMapFilename(prefix)); if (!userMapFile.exists()) { return null; } return objectMapper.readValue( userMapFile, BasicAuthUtils.AUTHENTICATOR_USER_MAP_TYPE_REFERENCE ); } Can be reduced to ```throws IOException```.

Target:
private Map<String, BasicAuthenticatorUser> loadUserMapFromDisk(String prefix) throws IOException { File userMapFile = new File(commonCacheConfig.getCacheDirectory(), getUserMapFilename(prefix)); if (!userMapFile.exists()) { return null; } return objectMapper.readValue( userMapFile, BasicAuthUtils.AUTHENTICATOR_USER_MAP_TYPE_REFERENCE ); }
Source:
START private UserAndRoleMap loadUserAndRoleMapFromDisk(String prefix) throws Exception END { File userAndRoleMapFile = new File(commonCacheConfig.getCacheDirectory(), getUserRoleMapFilename(prefix)); if (!userAndRoleMapFile.exists()) { return null; } return objectMapper.readValue( userAndRoleMapFile, BasicAuthUtils.AUTHORIZER_USER_AND_ROLE_MAP_TYPE_REFERENCE ); } Can be reduced to ```throws IOException```.

Target:
private UserAndRoleMap loadUserAndRoleMapFromDisk(String prefix) throws IOException { File userAndRoleMapFile = new File(commonCacheConfig.getCacheDirectory(), getUserRoleMapFilename(prefix)); if (!userAndRoleMapFile.exists()) { return null; } return objectMapper.readValue( userAndRoleMapFile, BasicAuthUtils.AUTHORIZER_USER_AND_ROLE_MAP_TYPE_REFERENCE ); }
Source:
public Optional<String> getBrokerServiceName(TieredBrokerConfig tierConfig, Query query) { final int priority = QueryContexts.getPriority(query); if (priority < minPriority) { return Optional.of( START tierConfig.getDefaultBrokerServiceName() END ); } else if (priority >= maxPriority) { return Optional.of(tierConfig.getDefaultBrokerServiceName()); } return Optional.absent(); } Logging here might be helpful.

Target:
public Optional<String> getBrokerServiceName(TieredBrokerConfig tierConfig, Query query) { final int priority = QueryContexts.getPriority(query); if (priority < minPriority || priority > maxPriority) { return Optional.of( tierConfig.getDefaultBrokerServiceName() ); } return Optional.absent(); }
Source:
private String extractEtagFromResults( final byte[] cachedResult ) { if (cachedResult == null) { return null; } START log.info("Fetching result level cache identifier for query: %s", query.getId()); END int etagLength = ByteBuffer.wrap(cachedResult, 0, Integer.BYTES).getInt(); return StringUtils.fromUtf8(Arrays.copyOfRange(cachedResult, Integer.BYTES, etagLength + Integer.BYTES)); } this would lead to too many noisy logs, this should be debug

Target:
private String extractEtagFromResults( final byte[] cachedResult ) { if (cachedResult == null) { return null; } log.debug("Fetching result level cache identifier for query: %s", query.getId()); int etagLength = ByteBuffer.wrap(cachedResult, 0, Integer.BYTES).getInt(); return StringUtils.fromUtf8(Arrays.copyOfRange(cachedResult, Integer.BYTES, etagLength + Integer.BYTES)); }
Source:
public AggregatorFactory getMergingFactory(AggregatorFactory other) throws AggregatorFactoryNotMergeableException { if (other.getName().equals(this.getName()) && other instanceof DoublesSketchAggregatorFactory) { START return new DoublesSketchMergeAggregatorFactory(name, k); END } else { throw new AggregatorFactoryNotMergeableException(this, other); } } Would you explain how it supports? Does it return the same result no matter which k is used?

Target:
public AggregatorFactory getMergingFactory(AggregatorFactory other) throws AggregatorFactoryNotMergeableException { if (other.getName().equals(this.getName()) && other instanceof DoublesSketchAggregatorFactory) { // DoublesUnion supports inputs with different k. // The result will have effective k between the specified k and the minimum k from all input sketches // to achieve higher accuracy as much as possible. return new DoublesSketchMergeAggregatorFactory(name, Math.max(k, ((DoublesSketchAggregatorFactory) other).k)); } else { throw new AggregatorFactoryNotMergeableException(this, other); } }
Source:
public void testGroupByOneColumnWithLiterallyFalseFilter() throws Exception { testQuery( "SELECT COUNT(*), MAX(cnt) FROM druid.foo WHERE 1 = 0 GROUP BY dim1", START CalciteTests.REGULAR_USER_AUTH_RESULT, END ImmutableList.of(), ImmutableList.of() ); } To avoid clogging up the majority of tests, the authorization result could be a parameter in the "full" overload (the one where you provide a planner config, context, etc). Then the "basic" overload (sql, expectedQueries, expectedResults) would stay the same.

Target:
public void testGroupByOneColumnWithLiterallyFalseFilter() throws Exception { testQuery( "SELECT COUNT(*), MAX(cnt) FROM druid.foo WHERE 1 = 0 GROUP BY dim1", ImmutableList.of(), ImmutableList.of() ); }
Source:
private static String getAvaticaConnectionId(Map<String, Object> requestMap) throws IOException { Object connectionIdObj = requestMap.get("connectionId"); if (connectionIdObj == null) { throw new IAE("Received an Avatica request without a connectionId."); } if (!(connectionIdObj instanceof String)) { throw new IAE("Received an Avatica request with a non-String connectionId."); } START return (String) requestMap.get("connectionId"); END } nit: return (String) connectionIdObj;

Target:
private static String getAvaticaConnectionId(Map<String, Object> requestMap) throws IOException { Object connectionIdObj = requestMap.get("connectionId"); if (connectionIdObj == null) { throw new IAE("Received an Avatica request without a connectionId."); } if (!(connectionIdObj instanceof String)) { throw new IAE("Received an Avatica request with a non-String connectionId."); } return (String) connectionIdObj; }
Source:
private static String trimLeadingPlusOfLongString(String metricValueString) { if (metricValueString.isEmpty()) { return metricValueString; } if (metricValueString.charAt(0) == '+') { START if (metricValueString.length() > 1) { END char secondChar = metricValueString.charAt(1); if (secondChar >= '0' && secondChar <= '9') { metricValueString = metricValueString.substring(1); } } } return metricValueString; } could do `if (metricValueString.length() > 1 && metricValueString.charAt(0) == '+') {..` and also eliminate the other if condition checking for `metricValueString.isEmpty()`

Target:
private static String trimLeadingPlusOfLongString(String metricValueString) { if (metricValueString.length() > 1 && metricValueString.charAt(0) == '+') { char secondChar = metricValueString.charAt(1); if (secondChar >= '0' && secondChar <= '9') { metricValueString = metricValueString.substring(1); } } return metricValueString; }
Source:
public JavaScriptParseSpec( @JsonProperty("timestampSpec") TimestampSpec timestampSpec, @JsonProperty("dimensionsSpec") DimensionsSpec dimensionsSpec, @JsonProperty("function") String function, @JacksonInject JavaScriptConfig config ) { super(timestampSpec, dimensionsSpec); START Preconditions.checkState(config.isEnabled(), "JavaScript is disabled"); END this.function = function; this.config = config; } The original idea behind delaying these checks until actual usage is that when you disable JavaScript, you might still want your Druid nodes to be able to deserialize JavaScript-based objects. For example, maybe you had used JavaScript aggregators in your ingest tasks, and after disabling JavaScript you still want to be able to view old completed tasks from the task table in the metadata store. So I think it's worth being able to create these objects even if JavaScript is disabled, just make sure the JavaScript is never actually compiled.

Target:
public JavaScriptParseSpec( @JsonProperty("timestampSpec") TimestampSpec timestampSpec, @JsonProperty("dimensionsSpec") DimensionsSpec dimensionsSpec, @JsonProperty("function") String function, @JacksonInject JavaScriptConfig config ) { super(timestampSpec, dimensionsSpec); this.function = function; this.config = config; }
Source:
public void replaceInput(int ordinalInParent, RelNode p) { if (ordinalInParent == 0) { Preconditions.checkArgument( p instanceof DruidRel, "RelNode[%s] is not a DruidRel", p.getClass().getCanonicalName() ); START this.left = (DruidRel<?>) p; END } else if (ordinalInParent == 1) { this.right = p; } else { throw new IndexOutOfBoundsException(StringUtils.format("Invalid ordinalInParent[%s]", ordinalInParent)); } } Left is not really an input (it sort of "becomes" this rel). See getInputs which only returns 1 element as far as Calcite is concerned. So this should only replace `right`.

Target:
public void replaceInput(int ordinalInParent, RelNode p) { if (ordinalInParent != 0) { throw new IndexOutOfBoundsException(StringUtils.format("Invalid ordinalInParent[%s]", ordinalInParent)); } // 'right' is the only one Calcite concerns. See getInputs(). this.right = p; }
Source:
private List<LookupBean> getLookupsListFromLookupConfig() { START List<LookupBean> lookupBeanList = new ArrayList<>(); END if (lookupConfig.getEnableLookupSyncOnStartup()) { String tier = lookupListeningAnnouncerConfig.getLookupTier(); lookupBeanList = getLookupListFromCoordinator(tier); if (lookupBeanList == null) { LOG.info("Coordinator is unavailable. Loading saved snapshot instead"); lookupBeanList = getLookupListFromSnapshot(); } } else { lookupBeanList = getLookupListFromSnapshot(); } return lookupBeanList; } Pointless assignment

Target:
private List<LookupBean> getLookupsListFromLookupConfig() { List<LookupBean> lookupBeanList; if (lookupConfig.getEnableLookupSyncOnStartup()) { String tier = lookupListeningAnnouncerConfig.getLookupTier(); lookupBeanList = getLookupListFromCoordinator(tier); if (lookupBeanList == null) { LOG.info("Coordinator is unavailable. Loading saved snapshot instead"); lookupBeanList = getLookupListFromSnapshot(); } } else { lookupBeanList = getLookupListFromSnapshot(); } return lookupBeanList; }
Source:
START public boolean isNUllRow(int[] row, Indexed<String> encodings) END { if (row == null) { return true; } for (int i : row) { if (encodings.get(i) != null) { // Non-Null value return false; } } return true; } don't think this need to be public so far

Target:
private boolean isNUllRow(int[] row, Indexed<String> encodings) { if (row == null) { return true; } for (int i : row) { if (encodings.get(i) != null) { // Non-Null value return false; } } return true; }
Source:
public static String fromUtf8(final ByteBuffer buffer, final int numBytes) { final byte[] bytes = new byte[numBytes]; buffer.get(bytes); START return StringUtils.fromUtf8(bytes); END } Don't need class qualifier

Target:
public static String fromUtf8(final ByteBuffer buffer, final int numBytes) { final byte[] bytes = new byte[numBytes]; buffer.get(bytes); return fromUtf8(bytes); }
Source:
public static String fromUtf8Nullable(final ByteBuffer buffer, final int numBytes) { final byte[] bytes = new byte[numBytes]; buffer.get(bytes); START return StringUtils.fromUtf8Nullable(bytes); END } bytes are always non null

Target:
public static String fromUtf8Nullable(final ByteBuffer buffer, final int numBytes) { if (numBytes < 0) { return null; } final byte[] bytes = new byte[numBytes]; buffer.get(bytes); return fromUtf8Nullable(bytes); }
Source:
private boolean isNotNull(int index) { if (idForNull == UNKNOWN) { String value = selector.lookupName(index); if (value == null) { idForNull = index; return false; START } END } return index != idForNull; } I think it's clearer to add `else { return true; }` here

Target:
private boolean isNotNull(int index) { if (idForNull == UNKNOWN) { String value = selector.lookupName(index); if (value == null) { idForNull = index; return false; } else { return true; } } return index != idForNull; }
Source:
START final public Aggregator factorize(ColumnSelectorFactory metricFactory) END { Pair<Aggregator, BaseNullableColumnValueSelector> pair = factorize2( metricFactory); return NullHandling.useDefaultValuesForNull() ? pair.lhs : new NullableAggregator(pair.lhs, pair.rhs); } `public` is usually before `final`, same below

Target:
public final Aggregator factorize(ColumnSelectorFactory metricFactory) { Pair<Aggregator, BaseNullableColumnValueSelector> pair = factorize2( metricFactory); return NullHandling.useDefaultValuesForNull() ? pair.lhs : new NullableAggregator(pair.lhs, pair.rhs); }
Source:
private int getCurrValidWorkers(Collection<ImmutableWorkerInfo> workers) { final Predicate<ImmutableWorkerInfo> isValidWorker = ProvisioningUtil.createValidWorkerPredicate(config); final int currValidWorkers = Collections2.filter(workers, isValidWorker).size(); START log.info("Current valid workers: %d", currValidWorkers); END return currValidWorkers; } suggest `debug` level

Target:
private int getCurrValidWorkers(Collection<ImmutableWorkerInfo> workers) { final Predicate<ImmutableWorkerInfo> isValidWorker = ProvisioningUtil.createValidWorkerPredicate(config); final int currValidWorkers = Collections2.filter(workers, isValidWorker).size(); log.debug("Current valid workers: %d", currValidWorkers); return currValidWorkers; }
Source:
public boolean didMove() { START return moved || (copied && deletedOld); END } ```moved``` and ```moveObject()``` are not needed anymore.

Target:
public boolean didMove() { return copied && deletedOld; }
Source:
public void reset() { if (!initialized) { throw new ISE("Grouper is not initialized"); } if (closed) { throw new ISE("Grouper is closed"); } START for (Grouper<KeyType> grouper : groupers) { END grouper.reset(); } } May replace with forEach

Target:
public void reset() { if (!initialized) { throw new ISE("Grouper is not initialized"); } if (closed) { throw new ISE("Grouper is closed"); } groupers.forEach(Grouper::reset); }
Source:
public void close() { try { closer.close(); } catch (IOException e) { START throw Throwables.propagate(e); END } } Could be simply `new RuntimeException(e)`

Target:
public void close() { try { closer.close(); } catch (IOException e) { throw new RuntimeException(e); } }
Source:
private String buildLookupQuery(String table, String filter, String keyColumn, String valueColumn) { START String query; END if (Strings.isNullOrEmpty(filter)) { query = StringUtils.format( "SELECT %s, %s FROM %s", keyColumn, valueColumn, table ); } else { query = StringUtils.format( "SELECT %s, %s FROM %s WHERE %s", keyColumn, valueColumn, table, filter ); } return query; } This variable is not needed, there could be two return statements in `if` and `else` blocks

Target:
private String buildLookupQuery(String table, String filter, String keyColumn, String valueColumn) { if (Strings.isNullOrEmpty(filter)) { return StringUtils.format( "SELECT %s, %s FROM %s", keyColumn, valueColumn, table ); } return StringUtils.format( "SELECT %s, %s FROM %s WHERE %s", keyColumn, valueColumn, table, filter ); }
Source:
public String toString() { START return StringUtils.nonStrictFormat( END "QueryInterruptedException{msg=%s, code=%s, class=%s, host=%s}", getMessage(), errorCode, errorClass, host ); } nonStrictFormat() is designed mostly to be called indirectly, e. g. in a method or constructor which accepts `Object... args` itself. In this case I see no point to use it instead of `StringUtils.format()`.

Target:
public String toString() { return StringUtils.format( "QueryInterruptedException{msg=%s, code=%s, class=%s, host=%s}", getMessage(), errorCode, errorClass, host ); }
Source:
public void addJobProperties(Job job) { Configuration conf = job.getConfiguration(); for (final Map.Entry<String, String> entry : schema.getTuningConfig().getJobProperties().entrySet()) { conf.set(entry.getKey(), entry.getValue()); } START } END can you change this impl to be addJobProperties(job.getConfiguration()) ?

Target:
public void addJobProperties(Job job) { addJobProperties(job.getConfiguration()); }
Source:
public boolean isSegmentLoadedByServer(String serverKey, DataSegment segment) { DruidServerHolder holder = servers.get(serverKey); START if (holder != null) { END return holder.druidServer.getSegment(segment.getIdentifier()) != null; } else { return false; } } This is a nit, but I find inverted logic like this to be hard to following when reading code. I think it would be better as just ``` if (holder == null) { return false; } return holder.druidServer.getSegment(segment.getIdentifier()) != null; ```

Target:
public boolean isSegmentLoadedByServer(String serverKey, DataSegment segment) { DruidServerHolder holder = servers.get(serverKey); return holder != null && holder.druidServer.getSegment(segment.getIdentifier()) != null; }
Source:
public String apply(@Nullable Object value) { START if (Objects.isNull(value)) { END return null; } if (value instanceof Number) { return bucket(((Number) value).doubleValue()); } else if (value instanceof String) { return apply((String) value); } return null; } Objects.isNull method exists for being used as lambda, direct use is just an obscure way to write `value == null`

Target:
public String apply(@Nullable Object value) { if (value == null) { return null; } if (value instanceof Number) { return bucket(((Number) value).doubleValue()); } else if (value instanceof String) { return apply((String) value); } return null; }
Source:
public String apply(@Nullable String value) { START try { END return bucket(Double.parseDouble(value)); } catch (NumberFormatException | NullPointerException ex) { return null; } } Null is "expected" here, so suggested to check for it explicitly, rather than operate via exception handling, that is an antipattern in java

Target:
public String apply(@Nullable String value) { if (value == null) { return null; } try { return bucket(Double.parseDouble(value)); } catch (NumberFormatException | NullPointerException ex) { return null; } }
Source:
static int requiredBufferCapacity( int cardinality, AggregatorFactory[] aggregatorFactories ) { final int cardinalityWithMissingValue = cardinality + 1; final int recordSize = Arrays.stream(aggregatorFactories) .mapToInt(AggregatorFactory::getMaxIntermediateSize) .sum(); START return Integer.BYTES + // grouping key size END getUsedFlagBufferCapacity(cardinalityWithMissingValue) + // total used flags size cardinalityWithMissingValue * recordSize; // total values size } I don't understand where it is actually used, I see only where `getUsedFlagBufferCapacity` (for bitset) and `cardinalityWithMissingValue * recordSize` (for records) space is used

Target:
static int requiredBufferCapacity( int cardinality, AggregatorFactory[] aggregatorFactories ) { final int cardinalityWithMissingValue = cardinality + 1; final int recordSize = Arrays.stream(aggregatorFactories) .mapToInt(AggregatorFactory::getMaxIntermediateSize) .sum(); return getUsedFlagBufferCapacity(cardinalityWithMissingValue) + // total used flags size cardinalityWithMissingValue * recordSize; // total values size }
Source:
private static ImmutableWorkerInfo selectFromEligibleWorkers(final Map<String, ImmutableWorkerInfo> eligibleWorkers) { return eligibleWorkers.values().stream().min( Comparator.comparing(ImmutableWorkerInfo::getAvailableCapacity).reversed() START ).orElse(null); END } It could be simple `.max(Comparator.comparing(ImmutableWorkerInfo::getAvailableCapacity))`

Target:
private static ImmutableWorkerInfo selectFromEligibleWorkers(final Map<String, ImmutableWorkerInfo> eligibleWorkers) { return eligibleWorkers.values().stream().max( Comparator.comparing(ImmutableWorkerInfo::getAvailableCapacity) ).orElse(null); }
Source:
public int getGroupingKeySize() { START return Doubles.BYTES; END } Could use Double.BYTES

Target:
public int getGroupingKeySize() { return Double.BYTES; }
Source:
public boolean checkUnsortedEncodedKeyComponentsEqual(@Nullable Float lhs, @Nullable Float rhs) { START return Objects.equals(DimensionHandlerUtils.nullToZero(lhs), DimensionHandlerUtils.nullToZero(rhs)); END } `DimensionHandlerUtils.nullToZero(lhs)` is non-null, `Objects.equals()` is designed to accepts nullable arguments, so it's less confusing to use `DimensionHandlerUtils.nullToZero(lhs).equals(DimensionHandlerUtils.nullToZero(rhs))` here. + Same for Double, Long.

Target:
public boolean checkUnsortedEncodedKeyComponentsEqual(@Nullable Float lhs, @Nullable Float rhs) { return DimensionHandlerUtils.nullToZero(lhs).equals(DimensionHandlerUtils.nullToZero(rhs)); }
Source:
public int getUnsortedEncodedKeyComponentHashCode(@Nullable Float key) { START return Objects.hashCode(DimensionHandlerUtils.nullToZero(key)); END } Similar to above, suggested `DimensionHandlerUtils.nullToZero(key).hashCode()`. + Same for Double, Long.

Target:
public int getUnsortedEncodedKeyComponentHashCode(@Nullable Float key) { return DimensionHandlerUtils.nullToZero(key).hashCode(); }
Source:
public void queryNative(Blackhole blackhole) throws Exception { START HashMap<String, Object> context = Maps.newHashMap(); END final Sequence<Row> resultSequence = QueryPlus.wrap(groupByQuery).run(walker, context); final ArrayList<Row> resultList = Sequences.toList(resultSequence, Lists.<Row>newArrayList()); for (Row row : resultList) { blackhole.consume(row); } } Could we remove this local var?

Target:
public void queryNative(Blackhole blackhole) throws Exception { final Sequence<Row> resultSequence = QueryPlus.wrap(groupByQuery).run(walker, Maps.newHashMap()); final ArrayList<Row> resultList = Sequences.toList(resultSequence, Lists.<Row>newArrayList()); for (Row row : resultList) { blackhole.consume(row); } }
Source:
protected ExprEval eval(final double x, final double y) { START return ExprEval.of((long) x / (long) y); END } Maybe divide in double?

Target:
protected ExprEval eval(final double x, final double y) { return ExprEval.of((long) (x / y)); }
Source:
START private LogicalType determineTimestampSpecLogicalType(Schema schema, String timestampSpecField) END { for (Schema.Field field : schema.getFields()) { if (field.name().equals(timestampSpecField)) { this.timestampSpecLogicalType = field.schema().getLogicalType(); return this.timestampSpecLogicalType; } } // The timestamp field does not exist in the provided schema return null; } Please annotate `@Nullable` as well.

Target:
private LogicalType determineTimestampSpecLogicalType(Schema schema, String timestampSpecField) { for (Schema.Field field : schema.getFields()) { if (field.name().equals(timestampSpecField)) { return field.schema().getLogicalType(); } } return null; }
Source:
public void open() throws IOException { writer = CompressionFactory.getFloatSerializer( ioPeon, START StringUtils.safeFormat("%s.float_column", filenameBase), END byteOrder, compression ); writer.open(); } Probably should crash if bad format string

Target:
public void open() throws IOException { writer = CompressionFactory.getFloatSerializer( ioPeon, StringUtils.format("%s.float_column", filenameBase), byteOrder, compression ); writer.open(); }
Source:
public static File makeDimFile(File dir, String dimension) { START return new File(dir, StringUtils.safeFormat("dim_%s.drd", dimension)); END } Probably should crash if bad format string

Target:
public static File makeDimFile(File dir, String dimension) { return new File(dir, StringUtils.format("dim_%s.drd", dimension)); }
Source:
public static File makeTimeFile(File dir, ByteOrder order) { START return new File(dir, StringUtils.safeFormat("time_%s.drd", order)); END } Probably should crash if bad format string

Target:
public static File makeTimeFile(File dir, ByteOrder order) { return new File(dir, StringUtils.format("time_%s.drd", order)); }
Source:
public void open() throws IOException { writer = CompressionFactory.getLongSerializer( ioPeon, START StringUtils.safeFormat("%s.long_column", filenameBase), END byteOrder, encoding, compression ); writer.open(); } Probably should crash if bad format string

Target:
public void open() throws IOException { writer = CompressionFactory.getLongSerializer( ioPeon, StringUtils.format("%s.long_column", filenameBase), byteOrder, encoding, compression ); writer.open(); }
Source:
private String makeFilename(String suffix) { START return StringUtils.safeFormat("%s.%s", filenameBase, suffix); END } Probably should crash if bad format string

Target:
private String makeFilename(String suffix) { return StringUtils.format("%s.%s", filenameBase, suffix); }
Source:
public void fill(int index, long[] toFill) { if (totalSize - index < toFill.length) { throw new IndexOutOfBoundsException( START StringUtils.safeFormat( END "Cannot fill array of size[%,d] at index[%,d]. Max size[%,d]", toFill.length, index, totalSize ) ); } for (int i = 0; i < toFill.length; i++) { toFill[i] = get(index + i); } } do you want to just add an `IOBE`?

Target:
public void fill(int index, long[] toFill) { if (totalSize - index < toFill.length) { throw new IndexOutOfBoundsException( StringUtils.format( "Cannot fill array of size[%,d] at index[%,d]. Max size[%,d]", toFill.length, index, totalSize ) ); } for (int i = 0; i < toFill.length; i++) { toFill[i] = get(index + i); } }
Source:
public static String generateValueFileName(String fileNameBase, int fileNum) { START return StringUtils.safeFormat("%s_value_%d", fileNameBase, fileNum); END } Probably should crash if bad format string

Target:
public static String generateValueFileName(String fileNameBase, int fileNum) { return StringUtils.format("%s_value_%d", fileNameBase, fileNum); }
Source:
public static String generateHeaderFileName(String fileNameBase) { START return StringUtils.safeFormat("%s_header", fileNameBase); END } Probably should crash if bad format string

Target:
public static String generateHeaderFileName(String fileNameBase) { return StringUtils.format("%s_header", fileNameBase); }
Source:
public VSizeIndexedIntsWriter( final IOPeon ioPeon, final String filenameBase, final int maxValue ) { this.ioPeon = ioPeon; START this.valueFileName = StringUtils.safeFormat("%s.values", filenameBase); END this.numBytes = VSizeIndexedInts.getNumBytesForMax(maxValue); } Probably should crash if bad format string

Target:
public VSizeIndexedIntsWriter( final IOPeon ioPeon, final String filenameBase, final int maxValue ) { this.ioPeon = ioPeon; this.valueFileName = StringUtils.format("%s.values", filenameBase); this.numBytes = VSizeIndexedInts.getNumBytesForMax(maxValue); }
Source:
public VSizeIndexedWriter( IOPeon ioPeon, String filenameBase, int maxId ) { this.ioPeon = ioPeon; START this.metaFileName = StringUtils.safeFormat("%s.meta", filenameBase); END this.headerFileName = StringUtils.safeFormat("%s.header", filenameBase); this.valuesFileName = StringUtils.safeFormat("%s.values", filenameBase); this.maxId = maxId; } Probably should crash if bad format string

Target:
public VSizeIndexedWriter( IOPeon ioPeon, String filenameBase, int maxId ) { this.ioPeon = ioPeon; this.metaFileName = StringUtils.format("%s.meta", filenameBase); this.headerFileName = StringUtils.format("%s.header", filenameBase); this.valuesFileName = StringUtils.format("%s.values", filenameBase); this.maxId = maxId; }
Source:
START Plumber initPlumber() END { log.info("Someone get us a plumber!"); plumber = fireDepartment.findPlumber(); return plumber; } Seems that this method could be private, and don't need to return Plumber.

Target:
private void initPlumber() { log.info("Someone get us a plumber!"); plumber = fireDepartment.findPlumber(); }
Source:
public int compare(String s, String s2) { // Avoid conversion to bytes for equal references START if(Objects.equals(s, s2)){ END return 0; } return ORDERING.compare(s, s2); } Is this really better? For strings that _are_ equal this is faster but for strings that aren't, it should be slower. I'd guess that most comparisons are between strings that are not equal, so the old code should perform better.

Target:
public int compare(String s, String s2) { // Avoid conversion to bytes for equal references // Assuming we mostly compare different strings, checking s.equals(s2) will only make the comparison slower. if (s == s2) { return 0; } return ORDERING.compare(s, s2); }
Source:
public Object2LongMap<String> getSegmentAvailability() { final Object2LongOpenHashMap<String> retVal = new Object2LongOpenHashMap<>(); if (segmentReplicantLookup == null) { return retVal; } for (DataSegment segment : getAvailableDataSegments()) { START if (segmentReplicantLookup.getTotalReplicants(segment.getIdentifier()) == 0) { END retVal.addTo(segment.getDataSource(), 1); } } return retVal; } This change omits segments whose total replicants is not 0. I think it shouldn't change the behavior.

Target:
public Object2LongMap<String> getSegmentAvailability() { final Object2LongOpenHashMap<String> retVal = new Object2LongOpenHashMap<>(); if (segmentReplicantLookup == null) { return retVal; } for (DataSegment segment : getAvailableDataSegments()) { if (segmentReplicantLookup.getTotalReplicants(segment.getIdentifier()) == 0) { retVal.addTo(segment.getDataSource(), 1); } else { retVal.addTo(segment.getDataSource(), 0); } } return retVal; }
Source:
public Status getStatusHTTP(@Context final HttpServletRequest req) { Access access = authorizationCheck(req, Action.READ); if (!access.isAllowed()) { log.warn("Authorization failure."); START return null; END } return status; } Why is this doing `log.warn` and returning null instead of a code 403? Code 403 would make more sense to me. If it's because you just don't have anything you can return to do that, then you could try either, - switch the return type to Response - or, define a `ForbiddenException` and an `ExceptionMapper<ForbiddenException>` in the manner of `CustomExceptionMapper`; and throw the ForbiddenException.

Target:
public Status getStatusHTTP(@Context final HttpServletRequest req) { authorizationCheck(req, Action.READ); return status; }
Source:
public void destroy() { keytab = null; serverSubject = null; for (LoginContext loginContext : loginContexts) { try { loginContext.logout(); } catch (LoginException ex) { START log.warn(ex.getMessage(), ex); END } } loginContexts.clear(); } This is backwards. It should be `log.warn(ex, ex.getMessage())`.

Target:
public void destroy() { keytab = null; serverSubject = null; for (LoginContext loginContext : loginContexts) { try { loginContext.logout(); } catch (LoginException ex) { log.warn(ex, ex.getMessage()); } } loginContexts.clear(); }
Source:
public SegmentMetadataQuery withFinalizedAnalysisTypes(SegmentMetadataQueryConfig config) { START return Druids.SegmentMetadataQueryBuilder END .copy(this) .analysisTypes(com.google.common.base.Objects.firstNonNull(analysisTypes, config.getDefaultAnalysisTypes())) .build(); } Don't create a new query object if analysisTypes are already non-null, just return `this`

Target:
public SegmentMetadataQuery withFinalizedAnalysisTypes(SegmentMetadataQueryConfig config) { if (analysisTypes != null) { return this; } return Druids.SegmentMetadataQueryBuilder .copy(this) .analysisTypes(config.getDefaultAnalysisTypes()) .build(); }