auphong2707/dl4se-code-review-dataset huggingface dataset:
Source:
protected InputSourceReader formattableReader( InputRowSchema inputRowSchema, InputFormat inputFormat, @Nullable File temporaryDirectory ) throws IOException { return new InputEntityIteratingReader( inputRowSchema, inputFormat, // formattableReader() is supposed to be called in each task that actually creates segments. // The task should already have only one split in parallel indexing, // while there's no need to make splits using splitHintSpec in sequential indexing. createSplits(inputFormat, null).map(split -> new S3Entity(s3Client, split.get())), temporaryDirectory ); }

Target:
Unit test coverage is missing for this method
Source:
public Stream<InputSplit> createSplits(InputFormat inputFormat, @Nullable SplitHintSpec splitHintSpec) throws IOException { if (firehoseFactory.isSplittable()) { return ((FiniteFirehoseFactory) firehoseFactory).getSplits(splitHintSpec); } else { throw new UnsupportedOperationException(); } }

Target:
Is supporting unsplittable `Firehose`s future work?
Source:
public List<InputRow> parseInputRows(String line) throws IOException, ParseException { final Map<String, Object> zipped = parseLine(line); return Collections.singletonList( MapInputRowParser.parse( getInputRowSchema().getTimestampSpec(), getInputRowSchema().getDimensionsSpec(), zipped ) ); }

Target:
Can you add a version of `MapInputRowParser.parse` that takes an `InputRowSchema` since providing timestamp and dimension specs from it seems like it will be common
Source:
default PeekableIntIterator peekableIterator() { return new PeekableIteratorAdapter(iterator()); }

Target:
nit: IntelliJ is complaining about raw types here `return new PeekableIteratorAdapter<>(iterator());`
Source:
public KafkaRecordSupplier( Map<String, Object> consumerProperties, ObjectMapper sortingMapper ) { this.consumerProperties = consumerProperties; this.sortingMapper = sortingMapper; this.consumer = getKafkaConsumer(); }

Target:
The original constructor can delegate to your new one, i.e.: ```java public KafkaRecordSupplier( Map<String, Object> consumerProperties, ObjectMapper sortingMapper ) { this(consumerProperties, sortingMapper, getKafkaConsumer()); } ```
Source:
public InputRow nextRow() throws IOException { if (!hasMore()) { throw new NoSuchElementException(); } if (!parsedInputRows.isEmpty()) { return parsedInputRows.remove(0); } return getNextRow(); }

Target:
`ArrayList.remove()` internally moves the positions of remaining elements and this will happen for every row in this case which could be expensive. Since `parsedInputRows` won't be large in general, I suggest to use an iterator on the list instead of modifying the list. ```java public E remove(int index) { rangeCheck(index); modCount++; E oldValue = elementData(index); int numMoved = size - index - 1; if (numMoved > 0) System.arraycopy(elementData, index+1, elementData, index, numMoved); elementData[--size] = null; // clear to let GC do its work return oldValue; } ```
Source:
public Map<String, ImmutableWorkerInfo> getWorkersEligibleToRunTasks() { return Maps.transformEntries( Maps.filterEntries( workers, input -> !lazyWorkers.containsKey(input.getKey()) && !workersWithUnacknowledgedTask.containsKey(input.getKey()) && !blackListedWorkers.containsKey(input.getKey()) && input.getValue().isInitialized() && input.getValue().isEnabled() ), (String key, WorkerHolder value) -> value.toImmutable() ); }

Target:
Looks better to be package-private if it's not supposed to be used in general.
Source:
protected Long computeResult() { long result = 0; for (int i = 0; i < numBuckets; i += cycleSize) { if (buckets[(i + startFrom) % numBuckets] != null) { result += (buckets[(i + startFrom) % numBuckets]).longValue(); } else { result += 0.0; } } startFrom++; return result; }

Target:
LGTM.com has a warning about this line: https://lgtm.com/projects/g/apache/incubator-druid/snapshot/9d76bbc14139a88dc90787309253de7c438a1e9c/files/extensions-contrib/moving-average-query/src/main/java/org/apache/druid/query/movingaverage/averagers/LongSumAverager.java?sort=name&dir=ASC&mode=heatmap#L38
Source:
public byte[] getCacheKey() { return new CacheKeyBuilder(PostAggregatorIds.HLL_SKETCH_TO_ESTIMATE_CACHE_TYPE_ID) .appendString(name) .appendCacheable(field) .build(); }

Target:
I think `name` shouldn't be in the cache key?
Source:
public int getAndIncrementCounter() { int oldVal = counter; counter++; return oldVal; }

Target:
nit: can be simplified to `return counter++;`
Source:
private void shutdown(Lifecycle lifecycle) { //noinspection finally (not completing the 'finally' block normally is intentional) try { lifecycle.stop(); } catch (Throwable t) { log.warn(t, "Exception when stopping druid lifecycle"); } finally { System.exit(1); } }

Target:
This feels like it should be error log i think
Source:
public void addAppenderator(String taskId, Appenderator appenderator) { taskAppenderatorMap.computeIfAbsent( taskId, (myTaskId) -> { return new ArrayList<>(); } ); taskAppenderatorMap.get(taskId).add(appenderator); }

Target:
I think the below is better to avoid an extra lookup on the map. ``` taskAppenderatorMap.computeIfAbsent(taskId, myTaskId -> new ArrayList<>()).add(appenderator); ```
Source:
public HashSet<String> getRequiredColumns() { return Sets.newHashSet(dimensions.stream() .map(DimensionSpec::getDimension) .collect(Collectors.toSet()) ); }

Target:
Please fix `Sets.newHashSet(...collect(...))`. I've also opened #8241 about fixing this generically.
Source:
public boolean shouldUseBitmapIndex(BitmapIndexSelector selector) { return Filters.shouldUseBitmapIndex(this, selector, filterTuning); }

Target:
It's confusing that this method doesn't just return `false` while `supportsBitmapIndex()` does.
Source:
public void shutdown() { if (mergeExecutor != null) { mergeExecutor.shutdownNow(); } }

Target:
After shutting down the `Executor`, we should explicitly set it to `null` which is more friendly to the GC.
Source:
public Comparator getComparator() { if (errorBoundsStdDev == null) { return Ordering.natural(); } else { return (o1, o2) -> Doubles.compare( ((SketchEstimateWithErrorBounds) o1).getEstimate(), ((SketchEstimateWithErrorBounds) o2).getEstimate() ); } }

Target:
It's `Comparator.comparingDouble(SketchEstimateWithErrorBounds::getEstimate)`
Source:
public void stop() throws InterruptedException { if (supervisorTaskChecker != null) { supervisorTaskChecker.shutdownNow(); supervisorTaskChecker.awaitTermination(10, TimeUnit.SECONDS); supervisorTaskChecker = null; } supervisorTaskCheckTimes.clear(); }

Target:
what is the use of setting it null here ?
Source:
public Set<Expr> getScalarInputs(List<Expr> args) { return ImmutableSet.of(args.get(1), args.get(2)); }

Target:
this should probably check if args length is 2 or 3?
Source:
public void setDisableAcl(boolean disableAcl) { this.disableAcl = disableAcl; }

Target:
This (and `setS3Bucket()`) don't strictly need to be public, but could be package-private (remove the modifier). Since they're currently only being used by tests, annotating them with `@VisibleForTesting` would also be a good practice.
Source:
protected static String sanitize(String namespace) { Pattern DOT_OR_WHITESPACE = Pattern.compile("[\\s]+|[.]+"); return DOT_OR_WHITESPACE.matcher(namespace).replaceAll("_"); }

Target:
```suggestion private static String sanitize(String namespace) ```
Source:
public boolean isValid() { return interval == null ^ segmentIds == null; }

Target:
This could just be a preconditions check in the constructor, and I would use a different error message that just tells the user that `intervals` and `segmentIds` cannot both be set. The current error message I don't think is accurate ("Request payload contains invalid interval[%s] or segmentIds[%s], atmost one valid value must be provided"), since the check is not really for validity of the supplied values but for the presence of both.
Source:
public byte[] getCacheKey() { return new CacheKeyBuilder(HavingSpecUtil.CACHE_TYPE_ID_GREATER_THAN) .appendString(aggregationName) .appendByteArray(StringUtils.toUtf8(String.valueOf(value))) .appendStrings(aggregators.keySet()) .appendCacheables(aggregators.values()) .build(); }

Target:
You don't need to add them to the cache key -- only the `aggregation` and `value` matter. The idea is that anything that changes the meaning or effect of the operator should be part of the cache key, but not other stuff. `aggregators` is just some extra information that the HavingSpec uses to do its job.
Source:
protected ExprEval eval(ExprEval param) { return eval(param, 0); }

Target:
Could probably be private instead of protected, which if so, would be preferred.
Source:
private Segment getAdapter(final DataSegment segment) throws SegmentLoadingException { final Segment adapter; synchronized (segmentLoader) { try { adapter = segmentLoader.getSegment(segment); } catch (SegmentLoadingException e) { segmentLoader.cleanup(segment); throw e; } } if (adapter == null) { throw new SegmentLoadingException("Null adapter from loadSpec[%s]", segment.getLoadSpec()); } return adapter; }

Target:
This is too much synchronization - it makes parallel segment loading impossible.
Source:
static <T> T getRandomElementFromList(List<T> list) { if (list.size() == 0) { return null; } return list.get(ThreadLocalRandom.current().nextInt(list.size())); }

Target:
Highly suggest `isEmpty`
Source:
public static QueryRunner makeMMappedQueryRunner( SegmentId segmentId, boolean rollup, boolean bitmaps, QueryRunnerFactory factory ) { QueryableIndex index = bitmaps ? rollup ? TestIndex.getMMappedTestIndex() : TestIndex.getNoRollupMMappedTestIndex() : TestIndex.getNoBitmapMMappedTestIndex(); return QueryRunnerTestHelper.makeQueryRunner( factory, segmentId, new QueryableIndexSegment(index, segmentId), null ); }

Target:
Nested ternaries = 😭. Please consider using normal `if`s.
Source:
public static IncrementalIndex getNoBitmapIncrementalTestIndex() { synchronized (log) { if (noBitmapRealtimeIndex != null) { return noBitmapRealtimeIndex; } } return noBitmapRealtimeIndex = makeRealtimeIndex("druid.sample.numeric.tsv", false, false); }

Target:
I know this synchronization pattern was before the change, I'm just confused what it tries to accomplish? ie, if `noBitmapRealtimeIndex` is not created then all threads can initialize `noBitmapRealtimeIndex` simultaneously, doesn't it look strange?
Source:
public Map<String, Object> baseline(final Blackhole blackhole) { Map<String, Object> parsed = flatParser.parseToMap(flatInputs.get(flatCounter)); for (Map.Entry<String, Object> entry : parsed.entrySet()) { String s = entry.getKey(); blackhole.consume(parsed.get(s)); } flatCounter = (flatCounter + 1) % numEvents; return parsed; }

Target:
This can be just an iteration over `parsed.values()`. Similar in many other places in this PR.
Source:
public void printStuff() { System.out.println(); for (Map<Object, Integer> valueMap : dimensionMap.values()) { System.out.println("DIMENSION " + valueMap + "\n============"); new TreeMap<>(valueMap).forEach((val, cnt) -> System.out.println(" VAL: " + val + " CNT: " + cnt)); } }

Target:
Now it prints `valueMap` twice. It should be `entrySet()` iteration (or better `dimensionMap.forEach()` call for readability).
Source:
private static @Nullable String findVersion(Map<Interval, String> versions, Interval interval) { return versions.entrySet().stream() .filter(entry -> entry.getKey().contains(interval)) .map(Entry::getValue) .findFirst() .orElse(null); }

Target:
nit: we usually add the annotation above the method.
Source:
public synchronized void close() { if (closed) { return; } closed = true; log.info("Firehose closing."); // Critical to add the poison pill to the queue, don't allow interruption. Uninterruptibles.putUninterruptibly(buffer, FIREHOSE_CLOSED); eventReceiverFirehoseRegister.unregister(serviceName); if (chatHandlerProvider != null) { chatHandlerProvider.unregister(serviceName); } Thread delayedCloseExecutor = this.delayedCloseExecutor; if (delayedCloseExecutor != null && !delayedCloseExecutor.equals(Thread.currentThread())) { // Interrupt delayedCloseExecutor to let it discover that closed flag is already set and exit. delayedCloseExecutor.interrupt(); } }

Target:
Should we do `closed = true` right before the synchronized block end (in case of exception)?
Source:
public RelDataType rowType() { synchronized (lock) { Preconditions.checkState(prepareResult != null || plannerResult != null, "must be called after sql has been prepared"); return plannerResult != null ? plannerResult.rowType() : prepareResult.getRowType(); } }

Target:
The message is not consistent with the condition, since if the SQL has been planned, then the precondition is met. It looks like the only place where `SqlLifecycle#rowType()` is called without a preceding call to `SqlLifecycle#plan()` is in `DruidStatement#prepare()`. However, in that method, it is possible to directly call `PrepareResult#getRowType()` on the return value of the earlier call to `SqlLifecycle#prepare()`. If that is done then the changes here are not needed.
Source:
public CoordinatorBasicAuthenticatorCacheNotifier( AuthenticatorMapper authenticatorMapper, DruidNodeDiscoveryProvider discoveryProvider, @EscalatedClient HttpClient httpClient ) { userCacheNotifier = new CommonCacheNotifier( initAuthenticatorConfigMap(authenticatorMapper), discoveryProvider, httpClient, "/druid-ext/basic-security/authentication/listen/%s", "CoordinatorBasicAuthenticatorCacheNotifier" ); configCacheNotifier = new CommonCacheNotifier( initAuthenticatorConfigMap(authenticatorMapper), discoveryProvider, httpClient, "/druid-ext/basic-security/authentication/listen/config/%s", "CoordinatorBasicAuthenticatorCacheNotifier" ); }

Target:
should this be initAuthenticatorUserMap instead of configMap ?
Source:
public static DateTime of(String instant) { try { Integer seconds = Integer.valueOf(instant); return new DateTime(seconds * 1000L, ISOChronology.getInstanceUTC()); } catch (NumberFormatException ex) { try { return new DateTime(Long.valueOf(instant), ISOChronology.getInstanceUTC()); } catch (IllegalArgumentException ex2) { try { return new DateTime(instant, ISOChronology.getInstanceUTC()); } catch (Exception ex3) { throw ex; } } } }

Target:
This code won't work for timestamps that are near the epoch. I think it's a bit too clever and we should stick to only working on milliseconds timestamps and ISO8601 strings.
Source:
protected void setSegmentMetadataHolder(final DataSegment segment, final SegmentMetadataHolder segmentMetadataHolder) { synchronized (lock) { TreeMap<DataSegment, SegmentMetadataHolder> dataSourceSegments = segmentMetadataInfo.computeIfAbsent( segment.getDataSource(), x -> new TreeMap<>(SEGMENT_ORDER) ); if (dataSourceSegments.put(segment, segmentMetadataHolder) == null) { totalSegments++; } } }

Target:
Could this also be package-private?
Source:
public void setTerminateDruidProcessOnConnectFail(Boolean terminateDruidProcessOnConnectFail) { this.terminateDruidProcessOnConnectFail = terminateDruidProcessOnConnectFail == null ? false : terminateDruidProcessOnConnectFail; }

Target:
Line longer than 120 cols. Please use if-else.
Source:
private void emitThreadAllocationMetrics(ServiceEmitter emitter) { final ServiceMetricEvent.Builder builder = builder(); MonitorUtils.addDimensionsToBuilder(builder, dimensions); Optional<Long> delta = collector.calculateDelta(); delta.ifPresent((value) -> emitter.emit(builder.build("jvm/heapAlloc/bytes", value))); }

Target:
Maybe `jvm/heapAlloc/bytes/delta`
Source:
public int available() { final ReentrantLock lock = this.lock; lock.lock(); try { return objects.size(); } finally { lock.unlock(); } }

Target:
Why bother locking if the size returned is immediately going to be invalid? aka, some other thread may have already changed the size.
Source:
public synchronized void close() { }

Target:
Doesn't need to be synchronized
Source:
public ComplexMetricExtractor getExtractor() { return new ComplexMetricExtractor() { @Override public Class<?> extractedClass() { return MomentSketchWrapper.class; } @Override public Object extractValue(final InputRow inputRow, final String metricName) { Object rawValue = inputRow.getRaw(metricName); if (rawValue instanceof MomentSketchWrapper) { return (MomentSketchWrapper) rawValue; } else { throw new IAE("Not a momentSketch"); } } }; }

Target:
This explicitness doesn't add a lot of value, just cast and let it throw ClassCastException. It will also log the wrong class, unlike the current code.
Source:
public void configure(Binder binder) { String typeName = MomentSketchAggregatorFactory.TYPE_NAME; if (ComplexMetrics.getSerdeForType(typeName) == null) { ComplexMetrics.registerSerde(typeName, new MomentSketchComplexMetricSerde()); } }

Target:
Could you please replace this boilerplate pattern with a single method `registerSerde(String, Supplier<ComplexMetricSerde>)` thoughout the code?
Source:
public MomentSketchWrapper fromByteBuffer(ByteBuffer buffer, int numBytes) { if (numBytes == 0) { return EMPTY_SKETCH; } buffer.limit(buffer.position() + numBytes); return MomentSketchWrapper.fromBytes(buffer); }

Target:
Does `EMPTY_SKETCH.toByteArray()` result in an empty array? Currently there is a discrepancy between `fromByteBuffer()` and `toBytes()` that looks suspicious when just reading the code.
Source:
public List<Task> getActiveTasks() { giant.lock(); try { final ImmutableList.Builder<Task> listBuilder = ImmutableList.builder(); for (final TaskStuff taskStuff : tasks.values()) { if (taskStuff.getStatus().isRunnable() && taskStuff.getTask() != null) { listBuilder.add(taskStuff.getTask()); } } return listBuilder.build(); } finally { giant.unlock(); } }

Target:
I guess `task` is never null in HeapMemoryTaskStorage?
Source:
public Set<StreamPartition<Integer>> getAssignment() { Set<TopicPartition> topicPartitions = consumer.assignment(); return topicPartitions .stream() .map((TopicPartition e) -> new StreamPartition<>(e.topic(), e.partition())) .collect(Collectors.toSet()); }

Target:
Don't need (TopicPartition e) here, just 'e' should be sufficient
Source:
public void close() { if (this.closed) { return; } assign(ImmutableSet.of()); scheduledExec.shutdown(); try { if (!scheduledExec.awaitTermination(EXCEPTION_RETRY_DELAY_MS, TimeUnit.MILLISECONDS)) { scheduledExec.shutdownNow(); } } catch (InterruptedException e) { log.info(e, "InterruptedException while shutting down"); } this.closed = true; }

Target:
Should be warn at least.
Source:
private KinesisSequenceNumber(@NotNull String sequenceNumber, boolean isExclusive) { super(sequenceNumber, isExclusive); if (OrderedPartitionableRecord.END_OF_SHARD_MARKER.equals(sequenceNumber) || SeekableStreamPartitions.NO_END_SEQUENCE_NUMBER.equals(sequenceNumber)) { isMaxSequenceNumber = true; this.intSequence = null; } else { isMaxSequenceNumber = false; this.intSequence = new BigInteger(sequenceNumber); } }

Target:
Is it safe to assume that all sequenceNumbers are in the string format BigInteger allows? If so, why don't we use it instead of String?
Source:
public void close() { if (this.closed) { return; } assign(ImmutableSet.of()); scheduledExec.shutdown(); try { if (!scheduledExec.awaitTermination(EXCEPTION_RETRY_DELAY_MS, TimeUnit.MILLISECONDS)) { scheduledExec.shutdownNow(); } } catch (InterruptedException e) { log.warn(e, "InterruptedException while shutting down"); } this.closed = true; }

Target:
On `InterruptedException`, you should rethrow the exception or interrupt the thread properly.
Source:
private void rescheduleRunnable(long delayMillis) { if (started && !stopRequested) { try { scheduledExec.schedule(getRecordRunnable(), delayMillis, TimeUnit.MILLISECONDS); } catch (RejectedExecutionException e) { log.warn( "Caught RejectedExecutionException, KinesisRecordSupplier for partition[%s] has likely temporarily shutdown the ExecutorService." + "This is expected behavior after calling seek(), seekToEarliest() and seekToLatest()", streamPartition.getPartitionId() ); } } else { log.info("Worker for partition[%s] has been stopped", streamPartition.getPartitionId()); } }

Target:
Please add the exception like this. ``` log.warn( e, "Caught RejectedExecutionException, KinesisRecordSupplier for partition[%s] has likely temporarily shutdown the ExecutorService." + "This is expected behavior after calling seek(), seekToEarliest() and seekToLatest()", streamPartition.getPartitionId() ); ```
Source:
public String getStream() { return id; }

Target:
Missing @JsonProperty ?
Source:
public void testGetDependentFields() throws Exception { List<String> dependentFields = fac.getDependentFields(); assertEquals(1, dependentFields.size()); assertEquals("field", dependentFields.get(0)); }

Target:
I think teamcity is complaining about the `Exception` which is never thrown.
Source:
protected I[] getBuckets() { return (I[]) buckets; }

Target:
Unnecessary cast.
Source:
public boolean equals(Object o) { if (this == o) { return true; } if (o == null || o.getClass() != getClass()) { return false; } PeriodBroadcastDistributionRule that = (PeriodBroadcastDistributionRule) o; if (!Objects.equals(period, that.period)) { return false; } if (!Objects.equals(includeFuture, that.includeFuture)) { return false; } return Objects.equals(colocatedDataSources, that.colocatedDataSources); }

Target:
`includeFuture` can't be null. `if (includeFuture != that.includeFuture)`?
Source:
public Object finalizeComputation(Object object) { try { if (object instanceof ByteBuffer) { return BloomKFilter.deserialize((ByteBuffer) object); } else if (object instanceof byte[]) { return BloomKFilter.deserialize(ByteBuffer.wrap((byte[]) object)); } else { return object; } } catch (IOException ioe) { throw new RuntimeException("Failed to deserialize BloomKFilter"); } }

Target:
`, ioe`
Source:
public void testSerDeser() throws IOException { ObjectMapper mapper = new DefaultObjectMapper(); Assert.assertEquals(loadingCache, mapper.readerFor(LoadingCache.class).readValue(mapper.writeValueAsString(loadingCache))); Assert.assertTrue(loadingCache.hashCode() == mapper.readerFor(LoadingCache.class).readValue(mapper.writeValueAsString(loadingCache)).hashCode()); }

Target:
Could use assertEquals?
Source:
public Parser getFlatParser() { JSONParseSpec spec = new JSONParseSpec( new TimestampSpec("ts", "iso", null, null), new DimensionsSpec(null, null, null), null, null ); return spec.makeParser(); }

Target:
It might have been worth considering adding an additional constructor to `TimestampSpec` with `@VisibleForTesting` annotation so that this PR could have a bit lighter of a footprint and not have to modify all of these tests since they are all just passing in `null` anyway.
Source:
public boolean isArray(final Object o) { if (o instanceof List) { return true; } return false; }

Target:
this can just be `return (o instanceof List)`
Source:
public boolean isMap(final Object o) { if (o instanceof Map) { return true; } else { return o instanceof Group; } }

Target:
could be `(o instanceof Map) || (o instanceof Group)`
Source:
public String toString() { return "PrefixFilteredDimensionSpec{" + "Prefix='" + NullHandling.nullToEmptyIfNeeded(prefix) + '\'' + '}'; }

Target:
"prefix" has a null check in the constructor, so the nullToEmpty call is not needed here
Source:
public DerivativeDataSourceMetadata( @JsonProperty("baseDataSource") String baseDataSource, @JsonProperty("dimensions") Set<String> dimensions, @JsonProperty("metrics") Set<String> metrics ) { this.dimensions = Preconditions.checkNotNull(dimensions, "dimensions cannot be null. This is not a valid DerivativeDataSourceMetadata."); this.metrics = Preconditions.checkNotNull(metrics, "metrics cannot be null. This is not a valid DerivativeDataSourceMetadata."); Preconditions.checkArgument(!Strings.isNullOrEmpty(baseDataSource), "baseDataSource cannot be null or empty. Please provide a baseDataSource."); this.baseDataSource = baseDataSource; }

Target:
nit: Please don't change the initialization order. It would be good if they're aligned with their definition order.
Source:
public void limit(TimeseriesQuery query) { setDimension("limit", toString().valueOf(query.getLimit())); }

Target:
`toString().valueOf(query.getLimit())` is just a weird way of writing `String.valueOf(query.getLimit())`, but using a strange Java feature that lets you call static methods (like valueOf) as if they were methods of specific objects. So please replace it with the latter.
Source:
public AnonymousAuthenticator( @JsonProperty("name") String name, @JsonProperty("authorizerName") String authorizerName, @JsonProperty("identity") String identity ) { this.name = name; this.authorizerName = authorizerName; this.identity = identity == null ? DEFAULT_IDENTITY : identity; this.anonymousResult = new AuthenticationResult( this.identity, this.authorizerName, this.name, null ); }

Target:
nit : It seems only the anonymousResult needs to be a field in the class, rest can be removed ? In case you meant this class to be used in JSON serde, please add @JsonProperty annotations to the fields and create their getter methods.
Source:
public AppenderatorDriverAddResult add( InputRow row, String sequenceName ) throws IOException { return append(row, sequenceName, null, true, true); }

Target:
Why is the BatchAppenderatorDriver skipping the lineage check now? I thought it could still make more than one segment per interval if it's running in non-incremental-publishing mode.
Source:
public static long getMaxBytesInMemoryOrDefault(final long maxBytesInMemory) { // In the main tuningConfig class constructor, we set the maxBytes to 0 if null to avoid setting // maxBytes to max jvm memory of the process that starts first. Instead we set the default based on // the actual task node's jvm memory. long newMaxBytesInMemory = maxBytesInMemory; if (maxBytesInMemory == 0) { newMaxBytesInMemory = TuningConfig.DEFAULT_MAX_BYTES_IN_MEMORY; } else if (maxBytesInMemory == -1) { newMaxBytesInMemory = Long.MAX_VALUE; } return newMaxBytesInMemory; }

Target:
`else if maxBytesInMemory < 0`?
Source:
public Map<String, Object> getStats() { try { return getCurrentTotalStats(); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); log.error("getStats() interrupted."); throw new RuntimeException(ie); } catch (ExecutionException | TimeoutException eete) { throw new RuntimeException(eete); } }

Target:
Please add `ie` to the log.
Source:
public HllSketch combine(final Object objectA, final Object objectB) { final Union union = new Union(lgK); union.update((HllSketch) objectA); union.update((HllSketch) objectA); return union.getResult(tgtHllType); }

Target:
Why duplicate line?
Source:
public AuthenticationResult createEscalatedAuthenticationResult() { return new AuthenticationResult(internalClientUsername, authorizerName, BASIC, null); }

Target:
> If i am getting this right there is one to one mapping between escalator type and authenticator name/type. Authenticator name/type are not necessarily correlated (I can name my kerberos authenticator "George" if I wanted). There's also no restriction on having two Authenticators of the same type (maybe that makes sense for some hypothetical implementation where running two instances with a different "namespace" or similar concept makes sense). So there is only a mapping between a specific escalator instance and an authenticator instance, which is why I suggest adding the property.
Source:
public void close() { Throwable thrown = null; for (MappedByteBuffer mappedByteBuffer : buffersList) { if (mappedByteBuffer == null) { continue; } try { ByteBufferUtils.unmap(mappedByteBuffer); } catch (Throwable t) { if (thrown == null) { thrown = t; } else { thrown.addSuppressed(t); } } } buffersList.clear(); Throwables.propagateIfPossible(thrown); }

Target:
This doesn't seem right. `Throwables.propagateIfPossible` will suppress any exception that is not a RuntimeException or Error. I think there's no reason here to only throw specific error types and suppress others, so I assume it's a bug. Instead, it should probably be, ```java Throwables.propagateIfPossible(thrown); throw new RuntimeException(thrown); ``` I know this is unrelated to your patch, but it's something that looks wrong in nearby code and we could fix that too.
Source:
public List<ReferenceCountingResourceHolder<T>> takeBatch(final int elementNum) { checkInitialized(); try { return takeObjects(elementNum).stream().map(this::wrapObject).collect(Collectors.toList()); } catch (InterruptedException e) { throw Throwables.propagate(e); } }

Target:
I think this has a buffer leak (and it looks like the old code had the leak too, and so does `pollObjects`). If either `pollObjects` or `takeObjects` is interrupted while it's waiting for more objects to become available, then the objects popped from `objects` are not returned to the pool - they are lost.
Source:
public static boolean eligibleForLoad(Period period, Interval interval, DateTime referenceTimestamp) { final Interval currInterval = new Interval(period, referenceTimestamp); return currInterval.overlaps(interval); }

Target:
nit: better to call `eligibleForLoad(currInterval, interval)` for easier code maintenance.
Source:
OnheapIncrementalIndex( IncrementalIndexSchema incrementalIndexSchema, boolean deserializeComplexMetrics, boolean reportParseExceptions, boolean concurrentEventAdd, boolean sortFacts, int maxRowCount, long maxBytesInMemory ) { super(incrementalIndexSchema, deserializeComplexMetrics, reportParseExceptions, concurrentEventAdd); this.maxRowCount = maxRowCount; this.maxBytesInMemory = maxBytesInMemory == 0 ? defaultMaxBytesInMemory : maxBytesInMemory; this.facts = incrementalIndexSchema.isRollup() ? new RollupFactsHolder(sortFacts, dimsComparator(), getDimensions()) : new PlainFactsHolder(sortFacts); if (maxBytesInMemory != -1) { maxBytesPerRowForAggregators = getMaxBytesPerRowForAggregators(incrementalIndexSchema); } }

Target:
Looks like this cannot be 0 in the current implementation. Suggest to throw an exception if it's 0 and remove `defaultMaxBytesInMemory`.
Source:
OnheapIncrementalIndex( IncrementalIndexSchema incrementalIndexSchema, boolean deserializeComplexMetrics, boolean reportParseExceptions, boolean concurrentEventAdd, boolean sortFacts, int maxRowCount, long maxBytesInMemory ) { super(incrementalIndexSchema, deserializeComplexMetrics, reportParseExceptions, concurrentEventAdd); this.maxRowCount = maxRowCount; this.maxBytesInMemory = maxBytesInMemory == 0 ? defaultMaxBytesInMemory : maxBytesInMemory; this.facts = incrementalIndexSchema.isRollup() ? new RollupFactsHolder(sortFacts, dimsComparator(), getDimensions()) : new PlainFactsHolder(sortFacts); maxBytesPerRowForAggregators = getMaxBytesPerRowForAggregators(incrementalIndexSchema); }

Target:
Conventionally, we include "this", like `this.maxBytesPerRowForAggregators =` in constructors when initializing fields. It doesn't matter for correctness, but it's conventional style in the codebase.
Source:
public DerivativeDataSourceMetadata( @JsonProperty("baseDataSource") String baseDataSource, @JsonProperty("dimensions") Set<String> dimensions, @JsonProperty("metrics") Set<String> metrics ) { Preconditions.checkNotNull(baseDataSource, "baseDataSource cannot be null. This is not a valid DerivativeDataSourceMetadata."); Preconditions.checkNotNull(dimensions, "dimensions cannot be null. This is not a valid DerivativeDataSourceMetadata."); Preconditions.checkNotNull(metrics, "metrics cannot be null. This is not a valid DerivativeDataSourceMetadata."); this.baseDataSource = baseDataSource; this.dimensions = dimensions; this.metrics = metrics; }

Target:
nit: Can be simplified to `this.baseDataSource = Preconditions.checkNotNull(baseDataSource, "baseDataSource cannot be null. This is not a valid DerivativeDataSourceMetadata.");`
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()); }

Target:
nit: please break a line.
Source:
private boolean hasEnoughLag(Interval target, Interval maxInterval) { if ((target.getStartMillis() + minDataLagMs) > maxInterval.getStartMillis()) { return false; } else { return true; } }

Target:
Could just return the boolean expression
Source:
public QueryRunner<T> getRunner(QuerySegmentWalker walker) { return getQuerySegmentSpecForLookUp().lookup(this, walker); }

Target:
nit: if we keep this, should be "ForLookup" (U is not capital)
Source:
private NumericExprEval(@Nullable Number value) { super(value == null ? NullHandling.defaultDoubleValue() : value); }

Target:
I think it should be specific for Double/Long
Source:
public Object finalizeComputation(@Nullable Object object) { return object == null ? object : ((SerializablePair<Long, Float>) object).rhs; }

Target:
Double* variant of this class uses `object == null ? null` that is better
Source:
public boolean eval(Row row) { Object metricVal = row.getRaw(aggregationName); if (metricVal == null || value == null) { return metricVal == null && value == null; } return HavingSpecMetricComparator.compare(row, aggregationName, value, aggregators) == 0; }

Target:
Could it optimize to not perform row.getRaw() twice?
Source:
public boolean hasNext() { init(); if (jp.isClosed()) { return false; } if (jp.getCurrentToken() == JsonToken.END_ARRAY) { CloseQuietly.close(jp); return false; } return true; }

Target:
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`.
Source:
protected long download(T object, File outFile) throws IOException { try { openObjectFunction.open(object, outFile); } catch (Exception e) { throw new RuntimeException(e); } return outFile.length(); }

Target:
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.
Source:
public <T> T retryWithHandle( HandleCallback<T> callback, Predicate<Throwable> myShouldRetry ) { try { return RetryUtils.retry(() -> getDBI().withHandle(callback), myShouldRetry, MAX_RETRIES); } catch (Exception e) { throw Throwables.propagate(e); } }

Target:
Please change to `new RuntimeException(e)` (https://github.com/google/guava/wiki/Why-we-deprecated-Throwables.propagate).
Source:
JsonIterator getNextLineIterator() { if (lineIterator != null) { lineIterator = null; } final JsonIterator iterator = resultIterator.next(); return iterator; }

Target:
make this method private ?
Source:
public static native int posix_fadvise(int fd, long offset, long len, int flag) throws LastErrorException; private NativeIO()

Target:
can this just be private?
Source:
private static Field getFieldByReflection(Class cls, String fieldName) { Field field = null; try { field = cls.getDeclaredField(fieldName); field.setAccessible(true); } catch (Exception e) { log.error("Unable to read [%s] field from [%s]", fieldName, cls.getName()); } return field; }

Target:
Since this is recoverable, can it just be `info`?
Source:
public int getPartition(LongWritable interval, BytesWritable text, int numPartitions) { final String jobTrackerString = config.get("mapred.job.tracker"); if ((jobTrackerString != null && jobTrackerString.equals("local")) || determineIntervals) { return 0; } else { return reducerLookup.get(interval); } }

Target:
`"local".equals(config.get("mapred.job.tracker"))`
Source:
private void initDimensionHandlers() { for (String dim : availableDimensions) { if (Strings.isNullOrEmpty(dim)) { continue; } ColumnCapabilities capabilities = getColumn(dim).getCapabilities(); DimensionHandler handler = DimensionHandlerUtils.getHandlerFromCapabilities(dim, capabilities, null); dimensionHandlers.put(dim, handler); } }

Target:
This is not needed if availableDimensions are filtered before
Source:
public ImmutableBitmap fromByteBuffer(ByteBuffer buffer, int numBytes) { if (numBytes == 0) { return null; } buffer.limit(buffer.position() + numBytes); return new WrappedImmutableRoaringBitmap(new ImmutableRoaringBitmap(buffer)); }

Target:
Nullable
Source:
public GenericColumn get() { if (nullValueBitmap.isEmpty()) { return new DoublesColumn(column.get()); } else { return new DoublesColumnWithNulls(column.get(), nullValueBitmap); } }

Target:
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.
Source:
public final int asInt() { assert NullHandling.replaceWithDefault() || !isNull(); return value.intValue(); }

Target:
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
Source:
public final int asInt() { if (value == null) { assert NullHandling.replaceWithDefault(); return 0; } final Integer theInt = Ints.tryParse(value); return theInt == null ? 0 : theInt; }

Target:
Also need check in line 257
Source:
public final double asDouble() { if (value == null) { assert NullHandling.replaceWithDefault(); return 0.0; } final Double theDouble = Doubles.tryParse(value); return theDouble == null ? 0.0 : theDouble; }

Target:
Please review the rest of `ExprEval` for such things
Source:
protected FloatsColumn(final ColumnarFloats column) { this.column = column; }

Target:
Please align with Double (no protected)
Source:
public boolean isNull() { return getObject() == null; }

Target:
According to the current way how those methods are implemented, it should be `return false`
Source:
private boolean isConnectionReset(Throwable t) { return (t instanceof SocketException && (t.getMessage().contains("Connection reset"))) || (t.getCause() != null && isConnectionReset(t.getCause())); }

Target:
Message may be `null`, add a guard check
Source:
public <T> T retryTransaction(final TransactionCallback<T> callback, final int quietTries, final int maxTries) { try { return RetryUtils.retry(() -> getDBI().inTransaction(callback), shouldRetry, quietTries, maxTries, null, null); } catch (Exception e) { throw Throwables.propagate(e); } }

Target:
Shouldn't pass `null`? There is an overload
Source:
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 || object instanceof Memory) { return object; } return ArrayOfDoublesSketchOperations.deserialize(object); } }; }

Target:
From the code of extractValue(), it could also be Memory.
Source:
public boolean equals(final Object o) { if (!super.equals(o)) { return false; } final ArrayOfDoublesSketchMultiPostAggregator that = (ArrayOfDoublesSketchMultiPostAggregator) o; return fields.equals(that.fields); }

Target:
It breaks symmetry of equals() and could be subject to ClassCastException.
Source:
public ArrayOfDoublesSketchBuildAggregator( final DimensionSelector keySelector, final List<BaseDoubleColumnValueSelector> valueSelectors, final int nominalEntries ) { this.keySelector = keySelector; this.valueSelectors = valueSelectors.toArray(new BaseDoubleColumnValueSelector[valueSelectors.size()]); values = new double[valueSelectors.size()]; sketch = new ArrayOfDoublesUpdatableSketchBuilder().setNominalEntries(nominalEntries) .setNumberOfValues(valueSelectors.size()).build(); }

Target:
According to https://shipilev.net/blog/2016/arrays-wisdom-ancients/, better off just `new BaseDoubleColumnValueSelector[0]`
Source:
public synchronized void aggregate() { final ArrayOfDoublesSketch update = selector.getObject(); if (update == null) { return; } union.update(update); }

Target:
Are locks needed in Build and Merge Aggregators alike?
Source:
public static BasicAuthenticatorMetadataStorageUpdater createAuthenticatorStorageUpdater(final Injector injector) { if (isCoordinator(injector)) { return injector.getInstance(CoordinatorBasicAuthenticatorMetadataStorageUpdater.class); } else { return injector.getInstance(NoopBasicAuthenticatorMetadataStorageUpdater.class); } }

Target:
Is this needed? I think it's better to not inject anything.
Source:
public BasicAuthorizerPermission( ResourceAction resourceAction ) { this.resourceAction = resourceAction; try { this.resourceNamePattern = Pattern.compile(resourceAction.getResource().getName()); } catch (PatternSyntaxException pse) { throw new BasicSecurityDBResourceException( "Invalid permission, resource name regex[%s] does not compile.", resourceAction.getResource().getName() ); } }

Target:
```pse``` should be included.
Source:
public static byte[] serializeAuthenticatorUserMap( ObjectMapper objectMapper, Map<String, BasicAuthenticatorUser> userMap ) { try { return objectMapper.writeValueAsBytes(userMap); } catch (IOException ioe) { throw new ISE("WTF? Couldn't serialize userMap!"); } }

Target:
Please add ```ioe``` to the exception.
Source:
private void writeUserMapToDisk(String prefix, byte[] userMapBytes) throws Exception { File cacheDir = new File(commonCacheConfig.getCacheDirectory()); cacheDir.mkdirs(); File userMapFile = new File(commonCacheConfig.getCacheDirectory(), getUserMapFilename(prefix)); Files.write(userMapBytes, userMapFile); }

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

Target:
Can be reduced to ```throws IOException```.
Source:
private UserAndRoleMap loadUserAndRoleMapFromDisk(String prefix) throws Exception { 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 ); }

Target:
Can be reduced to ```throws IOException```.
Source:
public Optional<String> getBrokerServiceName(TieredBrokerConfig tierConfig, Query query) { final int priority = QueryContexts.getPriority(query); if (priority < minPriority) { return Optional.of( tierConfig.getDefaultBrokerServiceName() ); } else if (priority >= maxPriority) { return Optional.of(tierConfig.getDefaultBrokerServiceName()); } return Optional.absent(); }

Target:
Logging here might be helpful.
Source:
private String extractEtagFromResults( final byte[] cachedResult ) { if (cachedResult == null) { return null; } log.info("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)); }

Target:
this would lead to too many noisy logs, this should be debug
Source:
public AggregatorFactory getMergingFactory(AggregatorFactory other) throws AggregatorFactoryNotMergeableException { if (other.getName().equals(this.getName()) && other instanceof DoublesSketchAggregatorFactory) { return new DoublesSketchMergeAggregatorFactory(name, k); } else { throw new AggregatorFactoryNotMergeableException(this, other); } }

Target:
Would you explain how it supports? Does it return the same result no matter which k is used?
Source:
public void testGroupByOneColumnWithLiterallyFalseFilter() throws Exception { testQuery( "SELECT COUNT(*), MAX(cnt) FROM druid.foo WHERE 1 = 0 GROUP BY dim1", CalciteTests.REGULAR_USER_AUTH_RESULT, ImmutableList.of(), ImmutableList.of() ); }

Target:
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.
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."); } return (String) requestMap.get("connectionId"); }

Target:
nit: return (String) connectionIdObj;
Source:
private static String trimLeadingPlusOfLongString(String metricValueString) { if (metricValueString.isEmpty()) { return metricValueString; } if (metricValueString.charAt(0) == '+') { if (metricValueString.length() > 1) { char secondChar = metricValueString.charAt(1); if (secondChar >= '0' && secondChar <= '9') { metricValueString = metricValueString.substring(1); } } } return metricValueString; }

Target:
could do `if (metricValueString.length() > 1 && metricValueString.charAt(0) == '+') {..` and also eliminate the other if condition checking for `metricValueString.isEmpty()`
Source:
public JavaScriptParseSpec( @JsonProperty("timestampSpec") TimestampSpec timestampSpec, @JsonProperty("dimensionsSpec") DimensionsSpec dimensionsSpec, @JsonProperty("function") String function, @JacksonInject JavaScriptConfig config ) { super(timestampSpec, dimensionsSpec); Preconditions.checkState(config.isEnabled(), "JavaScript is disabled"); this.function = function; this.config = config; }

Target:
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.
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() ); this.left = (DruidRel<?>) p; } else if (ordinalInParent == 1) { this.right = p; } else { throw new IndexOutOfBoundsException(StringUtils.format("Invalid ordinalInParent[%s]", ordinalInParent)); } }

Target:
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`.
Source:
private List<LookupBean> getLookupsListFromLookupConfig() { List<LookupBean> lookupBeanList = new ArrayList<>(); 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; }

Target:
Pointless assignment
Source:
public 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; }

Target:
don't think this need to be public so far
Source:
public static String fromUtf8(final ByteBuffer buffer, final int numBytes) { final byte[] bytes = new byte[numBytes]; buffer.get(bytes); return StringUtils.fromUtf8(bytes); }

Target:
Don't need class qualifier
Source:
public static String fromUtf8Nullable(final ByteBuffer buffer, final int numBytes) { final byte[] bytes = new byte[numBytes]; buffer.get(bytes); return StringUtils.fromUtf8Nullable(bytes); }

Target:
bytes are always non null
Source:
private boolean isNotNull(int index) { if (idForNull == UNKNOWN) { String value = selector.lookupName(index); if (value == null) { idForNull = index; return false; } } return index != idForNull; }

Target:
I think it's clearer to add `else { return true; }` here
Source:
final public Aggregator factorize(ColumnSelectorFactory metricFactory) { Pair<Aggregator, BaseNullableColumnValueSelector> pair = factorize2( metricFactory); return NullHandling.useDefaultValuesForNull() ? pair.lhs : new NullableAggregator(pair.lhs, pair.rhs); }

Target:
`public` is usually before `final`, same below