auphong2707/dl4se-code-review-dataset huggingface dataset:
Source:
public QualifiedModuleName convertFromText(String value, Class<?> targetType, String optionContext) { int colonIndex = value.indexOf(':'); if (colonIndex == -1) { logger.warning("Incorrect syntax. Valid syntax is '<ModuleType>:<ModuleName>'."); logger.warning("For example, 'module info source:file' to get 'file' <source> module info."); throw new ShellException(); } START RESTModuleType moduleType = validateAndReturnModuleType(colonIndex, value); END QualifiedModuleName result = new QualifiedModuleName(value.substring(colonIndex + 1), moduleType); return result; } why not just pass a single arg to validateAndReturnModuleType(..), e.g.: ``` validateAndReturnModuleType(value.substring(0, colonIndex)); ```

Target:
public QualifiedModuleName convertFromText(String value, Class<?> targetType, String optionContext) { int colonIndex = value.indexOf(':'); if (colonIndex == -1) { logger.warning("Incorrect syntax. Valid syntax is '<ModuleType>:<ModuleName>'."); logger.warning("For example, 'module info source:file' to get 'file' <source> module info."); throw new ShellException(); } RESTModuleType moduleType = validateAndReturnModuleType(value.substring(0, colonIndex)); QualifiedModuleName result = new QualifiedModuleName(value.substring(colonIndex + 1), moduleType); return result; }
Source:
public RedisTestMessageBus(RedisConnectionFactory connectionFactory, MultiTypeCodec<Object> codec) { RedisMessageBus messageBus = new RedisMessageBus(connectionFactory, codec); START this.setMessageBus(messageBus); END GenericApplicationContext context = new GenericApplicationContext(); context.refresh(); messageBus.setApplicationContext(context); this.setMessageBus(messageBus); template = new StringRedisTemplate(connectionFactory); } Remove, please, this line.

Target:
public RedisTestMessageBus(RedisConnectionFactory connectionFactory, MultiTypeCodec<Object> codec) { RedisMessageBus messageBus = new RedisMessageBus(connectionFactory, codec); GenericApplicationContext context = new GenericApplicationContext(); context.refresh(); messageBus.setApplicationContext(context); setMessageBus(messageBus); template = new StringRedisTemplate(connectionFactory); }
Source:
private void checkForIllegalStreamName(String streamName, String streamDef) { try { StreamNode sn = parse(streamName, streamDef); fail("expected to fail but parsed " + sn.stringify()); } catch (StreamDefinitionException e) { assertEquals(XDDSLMessages.ILLEGAL_STREAM_NAME, e.getMessageCode()); assertEquals(0, e.getPosition()); START e.printStackTrace(); END assertEquals(streamName, e.getInserts()[0]); } } did you intend to leave the printStackTrace() here?

Target:
private void checkForIllegalStreamName(String streamName, String streamDef) { try { StreamNode sn = parse(streamName, streamDef); fail("expected to fail but parsed " + sn.stringify()); } catch (StreamDefinitionException e) { assertEquals(XDDSLMessages.ILLEGAL_STREAM_NAME, e.getMessageCode()); assertEquals(0, e.getPosition()); assertEquals(streamName, e.getInserts()[0]); } }
Source:
public void initialize(ConfigurableApplicationContext applicationContext) { String adminPort = applicationContext.getEnvironment().resolvePlaceholders(ADMIN_PORT_PLACEHOLDER); ServerSocket socket = null; try { socket = new ServerSocket(Integer.parseInt(adminPort)); } catch (IOException e) { throw new AdminPortNotAvailableException(adminPort); } finally { if (socket != null) { try { socket.close(); } catch (IOException e) { // It is very unlikely to get an exception here. START e.printStackTrace(); END } } } } I would not even bother printing the stack trace here. It won't do us any good to see it.

Target:
public void initialize(ConfigurableApplicationContext applicationContext) { String adminPort = applicationContext.getEnvironment().resolvePlaceholders(ADMIN_PORT_PLACEHOLDER); ServerSocket socket = null; try { socket = new ServerSocket(Integer.parseInt(adminPort)); } catch (IOException e) { throw new AdminPortNotAvailableException(adminPort); } finally { if (socket != null) { try { socket.close(); } catch (IOException e) { // It is very unlikely to get an exception here. } } } }
Source:
protected ContainerAttributesResource instantiateResource(ContainerAttributes entity) { START return new ContainerAttributesResource(entity.getId(), entity.getPid(), entity.getHost(), entity.getIp()); END } It looks like we need to modify ContainerAttributesResource to simply take a Map in its constructor. That way it's not only simpler, but it would also mean that all attribute values (including user-provided ones beyond those available OOTB) would be represented by the resource representation.

Target:
protected ContainerAttributesResource instantiateResource(ContainerAttributes entity) { return new ContainerAttributesResource(entity); }
Source:
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { if (applicationContext instanceof AbstractApplicationContext) { START ((AbstractApplicationContext) applicationContext).setId(this.containerMetadata.getId()); END } this.context = applicationContext; } This is one of the reasons I didn't create a PR yet; not sure what the answer is yet but was going to explore it (possibly using ApplicationContextInitializer and creating ContainerMetadata earlier). Earlier (current master) we had a static method being called to publish the container started event from a context (static so SingleNodeApplication could also call it), but that definitely needs to change. We need a bean, with a managed lifecycle.

Target:
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.context = applicationContext; }
Source:
public void close() { if (containerContext != null) { containerContext.close(); if (moduleDeployer != null) { ConfigurableApplicationContext moduleCommon = moduleDeployer.getCommonContext(); if (moduleCommon != null) { START moduleCommon.close(); END } } } if (adminContext != null) { adminContext.close(); ApplicationContext parent = adminContext.getParent(); if (parent instanceof ConfigurableApplicationContext) { ((ConfigurableApplicationContext) parent).close(); } } } Seems to me that this is only handled in the SingleNode case, not in the distributed case, am I correct? Moreover, having the launcher know about all those contexts seems a bit ugly to me. I guess I would expect it to be more transparent (maybe by listening for some kind of close event or whatever?)

Target:
public void close() { if (containerContext != null) { containerContext.close(); } if (adminContext != null) { adminContext.close(); ApplicationContext parent = adminContext.getParent(); if (parent instanceof ConfigurableApplicationContext) { ((ConfigurableApplicationContext) parent).close(); } } }
Source:
START protected MessageBus getMessageBus() { END testMessageBus = new RabbitTestMessageBus(rabbitAvailableRule.getResource(), getCodec()); return testMessageBus; } should this only initialize the testMessageBus instance if it's null? (could be synchronized at method-level or in a synchronized block)

Target:
protected MessageBus getMessageBus() { if (testMessageBus == null) { testMessageBus = new RabbitTestMessageBus(rabbitAvailableRule.getResource(), getCodec()); } return testMessageBus; }
Source:
public void testLaunchJobTwiceWhereMakeUniqueIsFalse() { logger.info("Launch batch job (makeUnique=false) twice"); executeJobCreate(MY_JOB, JOB_WITH_PARAMETERS_DESCRIPTOR + " --makeUnique=false"); checkForJobInList(MY_JOB, JOB_WITH_PARAMETERS_DESCRIPTOR + " --makeUnique=false", true); executeJobLaunch(MY_JOB); CommandResult result = executeCommandExpectingFailure("job launch --name " + MY_JOB); START System.out.println("*************************" + result.getException().getMessage()); END assertThat( result.getException().getMessage(), containsString("A job instance already exists and is complete for parameters={}. If you want to run this job again, change the parameters.")); } I assume this was meant to be removed?

Target:
public void testLaunchJobTwiceWhereMakeUniqueIsFalse() { logger.info("Launch batch job (makeUnique=false) twice"); executeJobCreate(MY_JOB, JOB_WITH_PARAMETERS_DESCRIPTOR + " --makeUnique=false"); checkForJobInList(MY_JOB, JOB_WITH_PARAMETERS_DESCRIPTOR + " --makeUnique=false", true); executeJobLaunch(MY_JOB); CommandResult result = executeCommandExpectingFailure("job launch --name " + MY_JOB); assertThat( result.getException().getMessage(), containsString("A job instance already exists and is complete for parameters={}. If you want to run this job again, change the parameters.")); }
Source:
public StepExecutionProgressInfoResource(StepExecution stepExecution, StepExecutionHistory stepExecutionHistory, double percentageComplete, boolean isFinished, double duration) { Assert.notNull(stepExecution, "stepExecution must not be null."); Assert.notNull(stepExecutionHistory, "stepExecution must not be null."); this.stepExecution = stepExecution; this.stepExecutionHistory = stepExecutionHistory; this.percentageComplete = percentageComplete; START this.finished = isFinished ? "true" : "false"; END this.duration = duration; } Why use a String in the Resource. The client should be responsible for formatting.

Target:
public StepExecutionProgressInfoResource(StepExecution stepExecution, StepExecutionHistory stepExecutionHistory, double percentageComplete, boolean isFinished, double duration) { Assert.notNull(stepExecution, "stepExecution must not be null."); Assert.notNull(stepExecutionHistory, "stepExecution must not be null."); this.stepExecution = stepExecution; this.stepExecutionHistory = stepExecutionHistory; this.percentageComplete = percentageComplete; this.finished = isFinished; this.duration = duration; }
Source:
START public synchronized byte[] serialize(Message<?> message) throws SerializationException { END Kryo kryo = kryoInstance(); Output output = new Output(2048, -1); kryo.writeObjectOrNull(output, message, GenericMessage.class); return output.getBuffer(); } Looking at these 3 methods, I don't see why they need to be synchronized. All the state appears to be in method-scope.

Target:
public byte[] serialize(Message<?> message) throws SerializationException { Kryo kryo = kryoInstance(); Output output = new Output(2048, -1); kryo.writeObjectOrNull(output, message, GenericMessage.class); return output.getBuffer(); }
Source:
START protected void closeOutputStreams() throws IOException { END if (outputHolder != null) { outputHolder.getStream().close(); if (outputHolder.getWrappedStream() != null) { outputHolder.getWrappedStream().close(); } outputHolder = null; } } is it necessary to wrap these in try/catch (but still propagate the exception at the end) so that if one fails the other close() is not bypassed?

Target:
protected void closeOutputStreams() throws IOException { if (outputHolder != null) { outputHolder.close(); outputHolder = null; } }
Source:
private ModuleType verifyModuleOfTypeExists(ModuleDeploymentRequest request, String moduleName, ModuleType type) { ModuleDefinition def = moduleDefinitionRepository.findByNameAndType(moduleName, type); if (def == null || def.getResource() == null) { List<ModuleDefinition> definitions = moduleDefinitionRepository.findByName(moduleName); START if (definitions == null || definitions.size() == 0) { END throw new NoSuchModuleException(moduleName); } // TODO: revisit this method altogether; it shouldn't apply to composite modules but at this stage we don't // know whether the 'stream' is actually a composite module (and we do want to verify complete streams) // the following method was removed from MDR: (leaving here as a reminder for the TODO) // The module is known but this doesn't seem to be a standard stream, // assume it is a composite module stream that isn't deployable by itself // request.tagAsUndeployable(); return definitions.get(0).getType(); } return def.getType(); } Code smell: clarify contract of repository regarding emptyness

Target:
private ModuleType verifyModuleOfTypeExists(ModuleDeploymentRequest request, String moduleName, ModuleType type) { ModuleDefinition def = moduleDefinitionRepository.findByNameAndType(moduleName, type); if (def == null || def.getResource() == null) { List<ModuleDefinition> definitions = moduleDefinitionRepository.findByName(moduleName); if (definitions.isEmpty()) { throw new NoSuchModuleException(moduleName); } // TODO: revisit this method altogether; it shouldn't apply to composite modules but at this stage we don't // know whether the 'stream' is actually a composite module (and we do want to verify complete streams) // the following method was removed from MDR: (leaving here as a reminder for the TODO) // The module is known but this doesn't seem to be a standard stream, // assume it is a composite module stream that isn't deployable by itself // request.tagAsUndeployable(); return definitions.get(0).getType(); } return def.getType(); }
Source:
protected synchronized void handleMessageInternal(Message<?> message) throws Exception { String payloadString = message.getPayload().toString(); START Class<?> type = (payloadString.contains("\"composite\":true")) ? CompositeModuleDeploymentRequest.class END : ModuleDeploymentRequest.class; Object deserialized = this.mapper.readValue(payloadString, type); if (deserialized instanceof CompositeModuleDeploymentRequest) { handleCompositeModuleDeployment((CompositeModuleDeploymentRequest) deserialized, message); } else { handleDeploymentRequest((ModuleDeploymentRequest) deserialized, message); } } I guess this is on your laundry list, right ;)

Target:
protected synchronized void handleMessageInternal(Message<?> message) throws Exception { String payloadString = message.getPayload().toString(); ModuleDeploymentRequest deserialized = this.mapper.readValue(payloadString, ModuleDeploymentRequest.class); if (deserialized instanceof CompositeModuleDeploymentRequest) { handleCompositeModuleDeployment((CompositeModuleDeploymentRequest) deserialized, message); } else { handleDeploymentRequest(deserialized, message); } }
Source:
public void onReadError(Exception exception) { if (logger.isDebugEnabled()) { logger.debug("Executing onReadError: " + exception.getMessage(), exception); } final Message<Exception> message = MessageBuilder.withPayload(exception) .setHeader(BatchJobHeaders.BATCH_LISTENER_EVENT_TYPE, START BatchListenerEventType.ITEM_LISTENER_ON_READ_ERROR) END .build(); notificationsChannel.send(message); } Add the .name()

Target:
public void onReadError(Exception exception) { if (logger.isDebugEnabled()) { logger.debug("Executing onReadError: " + exception.getMessage(), exception); } final Message<Exception> message = MessageBuilder.withPayload(exception) .setHeader(BatchJobHeaders.BATCH_LISTENER_EVENT_TYPE, BatchListenerEventType.ITEM_LISTENER_ON_READ_ERROR.name()) .build(); notificationsChannel.send(message); }
Source:
public TarArchiveEntry(final Path file) { START this(file, file.toString()); END } What will happen if `file` is absolute?

Target:
public TarArchiveEntry(final Path file) throws IOException { this(file, file.toString()); }
Source:
public void findImplementations() { ResolverUtil<VFS> resolverUtil = new ResolverUtil<>(); resolverUtil.findImplementations(VFS.class, "org.apache.ibatis.io"); START Set<Class<? extends VFS>> classSets = resolverUtil.getClasses(); END classSets.forEach(c -> Assert.assertTrue(VFS.class.isAssignableFrom(c))); } Should verify size of resolved classes.

Target:
void findImplementations() { ResolverUtil<VFS> resolverUtil = new ResolverUtil<>(); resolverUtil.findImplementations(VFS.class, "org.apache.ibatis.io"); Set<Class<? extends VFS>> classSets = resolverUtil.getClasses(); //org.apache.ibatis.io.VFS //org.apache.ibatis.io.DefaultVFS //org.apache.ibatis.io.JBoss6VFS assertEquals(classSets.size(), 3); //fail if add a new VFS implementation in this package!!! classSets.forEach(c -> assertTrue(VFS.class.isAssignableFrom(c))); }
Source:
START public void setUp() throws Exception { END currentContextClassLoader = Thread.currentThread().getContextClassLoader(); } Can omit `public` on JUnit 5.

Target:
static void setUp() { currentContextClassLoader = Thread.currentThread().getContextClassLoader(); }
Source:
public Object get(Object key) { String strKey = (String) key; if (super.containsKey(strKey)) { return super.get(strKey); } if (parameterMetaObject == null) { return null; } if (fallbackParameterObject) { return parameterMetaObject.hasGetter(strKey) ? START parameterMetaObject.getValue(strKey) : parameterMetaObject.getOriginalObject(); END } else { // issue #61 do not modify the context when reading return parameterMetaObject.getValue(strKey); } } This part can be slightly simpler. ```java if (fallbackParameterObject && !parameterMetaObject.hasGetter(strKey)) { return parameterMetaObject.getOriginalObject(); ```

Target:
public Object get(Object key) { String strKey = (String) key; if (super.containsKey(strKey)) { return super.get(strKey); } if (parameterMetaObject == null) { return null; } if (fallbackParameterObject && !parameterMetaObject.hasGetter(strKey)) { return parameterMetaObject.getOriginalObject(); } else { // issue #61 do not modify the context when reading return parameterMetaObject.getValue(strKey); } }
Source:
public int update(String sql, Object... args) throws SQLException { START try (PreparedStatement ps = connection.prepareStatement(sql)) { END setParameters(ps, args); return ps.executeUpdate(); } } Probably, this changes will broken a current behavior that ignore exception if the `Statement.close` throw a `SQLException`.

Target:
public int update(String sql, Object... args) throws SQLException { PreparedStatement ps = connection.prepareStatement(sql); try { setParameters(ps, args); return ps.executeUpdate(); } finally { try { ps.close(); } catch (SQLException e) { //ignore } } }
Source:
private ReentrantLock getLockForKey(Object key) { START return locks.computeIfAbsent(key, (k) -> new ReentrantLock()); END } `(k)` -> `k` ```suggestion return locks.computeIfAbsent(key, k -> new ReentrantLock()); ```

Target:
private ReentrantLock getLockForKey(Object key) { return locks.computeIfAbsent(key, k -> new ReentrantLock()); }
Source:
public Iterator<T> iterator() { if (isClosed()) { throw new IllegalStateException("A Cursor is already closed."); } if (iteratorRetrieved) { throw new IllegalStateException("Cannot open more than one iterator on a Cursor"); START } END iteratorRetrieved = true; return cursorIterator; } It would be better to check `isClosed()` after checking `iteratorRetrieved` because the error message is more specific.

Target:
public Iterator<T> iterator() { if (iteratorRetrieved) { throw new IllegalStateException("Cannot open more than one iterator on a Cursor"); } if (isClosed()) { throw new IllegalStateException("A Cursor is already closed."); } iteratorRetrieved = true; return cursorIterator; }
Source:
public Object getNullableResult(ResultSet rs, String columnName) throws SQLException { Array array = rs.getArray(columnName); START return (array == null || rs.wasNull()) ? null : array.getArray(); END } A lot of your changes are because of this logic. I'm curious what driver returns a non null for an object type but returns true for rs.wasNull() I've only see it happen for primitive types but never for object types

Target:
public Object getNullableResult(ResultSet rs, String columnName) throws SQLException { Array array = rs.getArray(columnName); return array == null ? null : array.getArray(); }
Source:
protected String sqlxmlToString(SQLXML sqlxml) throws SQLException { START String result = sqlxml.getString(); END sqlxml.free(); return result; } I think it needs the null check.

Target:
protected String sqlxmlToString(SQLXML sqlxml) throws SQLException { if (sqlxml == null) { return null; } try { String result = sqlxml.getString(); return result; } finally { sqlxml.free(); } }
Source:
public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException { SQLXML sqlxml = ps.getConnection().createSQLXML(); sqlxml.setString(parameter); ps.setSQLXML(i, sqlxml); START sqlxml.free(); END } If you need to call `free()`, it may be better to call it in try-finally.

Target:
public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException { SQLXML sqlxml = ps.getConnection().createSQLXML(); try { sqlxml.setString(parameter); ps.setSQLXML(i, sqlxml); } finally { sqlxml.free(); } }
Source:
public void testInsertAnnotated() throws Exception { START SqlSession sqlSession = sqlSessionFactory.openSession(); END try { InsertMapper mapper = sqlSession.getMapper(InsertMapper.class); Name name = new Name(); name.setFirstName("Fred"); name.setLastName("Flintstone"); int rows = mapper.insertNameAnnotated(name); assertNotNull(name.getId()); assertEquals(1, rows); } finally { sqlSession.close(); } } I prefer use try-with-resources as follow: ```java try(SqlSession sqlSession = sqlSessionFactory.openSession()) { InsertMapper mapper = sqlSession.getMapper(InsertMapper.class); Name name = new Name(); name.setFirstName("Fred"); name.setLastName("Flintstone"); int rows = mapper.insertNameAnnotated(name); assertNotNull(name.getId()); assertEquals(1, rows); } ```

Target:
public void testInsertAnnotated() throws Exception { try (SqlSession sqlSession = sqlSessionFactory.openSession()) { InsertMapper mapper = sqlSession.getMapper(InsertMapper.class); Name name = new Name(); name.setFirstName("Fred"); name.setLastName("Flintstone"); int rows = mapper.insertNameAnnotated(name); assertNotNull(name.getId()); assertEquals(1, rows); } }
Source:
private void populateWriterArray() { columnWriters = new ArrayList<>(); // Case for empty result set START if (firstRow == null || firstRow.length == 0) { END return; } int colPosition = 0; for (String value : firstRow) { columnWriters.add(new StringColumnWriter(value, rowWriter, colPosition)); colPosition++; } } Can `columnWriters = new ArrayList<>();` go here (after `firstRow` checks)?

Target:
private void populateWriterArray() { // Case for empty result set if (firstRow == null || firstRow.length == 0) { return; } columnWriters = new ArrayList<>(); int colPosition = 0; for (String value : firstRow) { columnWriters.add(new StringColumnWriter(value, rowWriter, colPosition)); colPosition++; } }
Source:
private TupleMetadata buildSchema() { this.firstRow = csvReader.parseNext(); // Case for empty dataset START if (firstRow == null) { END return builder.buildSchema(); } // Parse the first row for (String value : firstRow) { builder.addNullable(value, TypeProtos.MinorType.VARCHAR); } return builder.buildSchema(); } Does it make sense to refactor complete function to this: ```java this.firstRow = csvReader.parseNext(); if (firstRow != null) { for (String value : firstRow) { builder.addNullable(value, TypeProtos.MinorType.VARCHAR); } } return builder.buildSchema(); ```

Target:
private TupleMetadata buildSchema() { firstRow = csvReader.parseNext(); if (firstRow != null) { for (String value : firstRow) { builder.addNullable(value, TypeProtos.MinorType.VARCHAR); } } return builder.buildSchema(); }
Source:
private IterOutcome handleRightIncoming() { outer: START for (;;) { END IterOutcome outcome = next(0, right); switch (outcome) { case NONE: state = State.LEFT; break outer; case NOT_YET: return outcome; case OK_NEW_SCHEMA: case OK: appendStatistics(statisticsCollector); break; default: throw new UnsupportedOperationException("Unsupported upstream state " + outcome); } } return null; } I use IntelliJ IDEA, the warning was the following: `'for' loop may be replaced with 'while' loop`. Its description is ``` Reports for loops which contain neither initialization or update components, and can thus be replaced by simpler while statements. Example: for(; exitCondition(); ) { process(); } This loop can be replaced with while(exitCondition()) { process(); } A fix action is also available for other for loops, so you can replace any for loop with while. Use the checkbox below if you wish this inspection to ignore for loops with trivial or non-existent conditions. ```

Target:
private IterOutcome handleRightIncoming() { outer: while (true) { IterOutcome outcome = next(0, right); switch (outcome) { case NONE: state = State.LEFT; break outer; case NOT_YET: return outcome; case OK_NEW_SCHEMA: case OK: appendStatistics(statisticsCollector); break; default: throw new UnsupportedOperationException("Unsupported upstream state " + outcome); } } return null; }
Source:
protected ElementParser resolve(TokenIterator tokenizer) { tupleParser.loader().removeNullMarker(this); if (tokenizer.peek() == JsonToken.START_ARRAY) { // The value is [], [ foo, so resolve directly return tupleParser.resolveField(key, tokenizer); START } else { END } // The value is [], foo, so must artificially create the // surrounding array to get a repeated type, which will // then parse the item as one-item array. return tupleParser.resolveArray(key, tokenizer); } Could you please remove the empty block?

Target:
protected ElementParser resolve(TokenIterator tokenizer) { tupleParser.loader().removeNullMarker(this); if (tokenizer.peek() == JsonToken.START_ARRAY) { // The value is [], [ foo, so resolve directly return tupleParser.resolveField(key, tokenizer); } else { // The value is [], foo, so must artificially create the // surrounding array to get a repeated type, which will // then parse the item as one-item array. return tupleParser.resolveArray(key, tokenizer); } }
Source:
private boolean processNextRow() { try { // Stop reading when you run out of data if (!spssReader.readNextCase()) { return false; } rowWriter.start(); for (SpssColumnWriter spssColumnWriter : writerList) { spssColumnWriter.load(spssReader); } rowWriter.save(); } catch (IOException e) { throw UserException .dataReadError(e) .message("Error reading SPSS File.") START .addContext(e.getMessage()) END .addContext(errorContext) .build(logger); } return true; } Not necessary. Better is: ``` .context("Error reading SPSS File.") ``` And let the message be the underlying error.

Target:
private boolean processNextRow() { try { // Stop reading when you run out of data if (!spssReader.readNextCase()) { return false; } rowWriter.start(); for (SpssColumnWriter spssColumnWriter : writerList) { spssColumnWriter.load(spssReader); } rowWriter.save(); } catch (IOException e) { throw UserException .dataReadError(e) .message("Error reading SPSS File.") .addContext(errorContext) .build(logger); } return true; }
Source:
public void allocateNew() throws OutOfMemoryException { if (!allocateNewSafe()) { START throw new OutOfMemoryException("" END + "Failed to allocate memory for " + getClass().getSimpleName()); } } ```suggestion throw new OutOfMemoryException( ```

Target:
public void allocateNew() throws OutOfMemoryException { if (!allocateNewSafe()) { throw new OutOfMemoryException( "Failed to allocate memory for " + getClass().getSimpleName()); } }
Source:
public static List<SchemaPath> expandMetadata(int dirCount) { START List<String> selected = Lists.newArrayList( END FULLY_QUALIFIED_NAME_COL, FILE_PATH_COL, FILE_NAME_COL, SUFFIX_COL); for (int i = 0; i < dirCount; i++) { selected.add(PARTITION_COL + Integer.toString(i)); } return RowSetTestUtils.projectList(selected); } Arrays.asList

Target:
public static List<SchemaPath> expandMetadata(int dirCount) { List<String> selected = Arrays.asList( FULLY_QUALIFIED_NAME_COL, FILE_PATH_COL, FILE_NAME_COL, SUFFIX_COL); for (int i = 0; i < dirCount; i++) { selected.add(PARTITION_COL + Integer.toString(i)); } return RowSetTestUtils.projectList(selected); }
Source:
public void testNullUserAgent() throws Exception { String query = "SELECT parse_user_agent(CAST(null as VARCHAR)) AS agent FROM (values(1))"; START Map<?, ?> emptyMap = new HashMap<>(); END testBuilder() .sqlQuery(query) .ordered() .baselineColumns("agent") .baselineValues(emptyMap) .go(); } ```suggestion Map<?, ?> emptyMap = Collections.emptyMap(); ```

Target:
public void testNullUserAgent() throws Exception { String query = "SELECT parse_user_agent(CAST(null as VARCHAR)) AS agent FROM (values(1))"; Map<?, ?> emptyMap = Collections.emptyMap(); testBuilder() .sqlQuery(query) .ordered() .baselineColumns("agent") .baselineValues(emptyMap) .go(); }
Source:
public void eval() { // Get the timestamp in milliseconds long timestamp = inputDate.value; // Get the interval in milliseconds long groupByInterval = interval.value; START java.time.Instant instant = java.time.Instant.ofEpochMilli(timestamp - (timestamp % groupByInterval)); java.time.LocalDateTime localDate = instant.atZone(java.time.ZoneId.of("UTC")).toLocalDateTime(); out.value = localDate.atZone(java.time.ZoneId.of("UTC")).toInstant().toEpochMilli(); END } @cgivre, could you please explain, what happens here? Initially, you calculate the required milliseconds, after that creates `Instant` instance based on that, converts it to `LocalDateTime` at `UTC` timezone, converts it to `ZonedDateTime`, converts it to `Instant` and after that converts back to milliseconds. Are all these transformations required? Usually, UDF shouldn't apply timezone to the values they handle.

Target:
public void eval() { // Get the timestamp in milliseconds long timestamp = inputDate.value; // Get the interval in milliseconds long groupByInterval = interval.value; out.value = (timestamp - (timestamp % groupByInterval)); }
Source:
public void eval() { // Get the timestamp in milliseconds long timestamp = inputDate.value; // Get the interval in milliseconds long intervalToAdd = interval.value; START out.value = (long) java.lang.Math.floor(timestamp / intervalToAdd) * intervalToAdd; END } isn't simpler formula ```timestamp - (timestamp % interval)``` ?

Target:
public void eval() { // Get the timestamp in milliseconds long timestamp = inputDate.value; // Get the interval in milliseconds long intervalToAdd = interval.value; out.value = timestamp - (timestamp % intervalToAdd); }
Source:
public String toString() { String result = type.name(); for (int i = 0; i < arrayDims; i++) { START result += "[]"; END } return result; } Could you please replace strings concatenation with `StringBuilder` usage.

Target:
public String toString() { StringBuilder buf = new StringBuilder() .append(type.name()); for (int i = 0; i < arrayDims; i++) { buf.append("[]"); } return buf.toString(); }
Source:
public void sendOk(int credit) { START everLargestAdviceCredit = credit > everLargestAdviceCredit ? credit : everLargestAdviceCredit; END if (0 == count.decrementAndGet()) { BitData.AckWithCredit ackWithCredit = BitData.AckWithCredit.newBuilder().setAllowedCredit(everLargestAdviceCredit).build(); Response ackResponse = new Response(BitData.RpcType.DATA_ACK_WITH_CREDIT, ackWithCredit); sender.send(ackResponse); } } This might be a bit clearer: ``` everLargestAdviceCredit = Math.max(everLargestAdviceCredit, credit); ```

Target:
public void sendOk(int credit) { everLargestAdviceCredit = Math.max(everLargestAdviceCredit, credit); if (0 == count.decrementAndGet()) { BitData.AckWithCredit ackWithCredit = BitData.AckWithCredit.newBuilder().setAllowedCredit(everLargestAdviceCredit).build(); Response ackResponse = new Response(BitData.RpcType.DATA_ACK_WITH_CREDIT, ackWithCredit); sender.send(ackResponse); } }
Source:
public boolean equals(final Object other) { if (other == this) { return true; } if (other == null || !(other instanceof Map.Entry)) { return false; } START final Map.Entry<?, ?> entry = (Map.Entry<?, ?>) other; END return Objects.equal(key, entry.getKey()) && Objects.equal(value, entry.getValue()); } ```suggestion Map.Entry<?, ?> entry = (Map.Entry<?, ?>) other; ```

Target:
public boolean equals(final Object other) { if (other == this) { return true; } if (other == null || !(other instanceof Map.Entry)) { return false; } Map.Entry<?, ?> entry = (Map.Entry<?, ?>) other; return Objects.equal(key, entry.getKey()) && Objects.equal(value, entry.getValue()); }
Source:
public SystemPluginLocator(PluginRegistryContext context) { START this.context = checkNotNull(context); END } I think we can skip not null check, let's assume caller knows what he is doing and not going to pass null context.

Target:
public SystemPluginLocator(PluginRegistryContext context) { this.context = context; }
Source:
protected FileScanFramework.FileScanBuilder frameworkBuilder(OptionManager options, EasySubScan scan) { FileScanFramework.FileScanBuilder builder = new FileScanFramework.FileScanBuilder(); builder.setReaderFactory(new AvroReaderFactory(new AvroBatchReader.AvroReaderConfig(this))); initScanBuilder(builder, scan); builder.setNullType(Types.optional(TypeProtos.MinorType.VARCHAR)); START return builder; END } No harm in setting this. But, I wonder if this is the right choice for Avro? Avro has a schema which, presumably, all files should follow. If schema evolution occurred (new field added), we'd really want to use the proper data type when reading old files without that column. This is of course a long-standing limitation of Drill's "pure" schema-on-read approach: while creating the reader is the wrong time to be guessing the schema of columns we are not going to see.

Target:
protected FileScanFramework.FileScanBuilder frameworkBuilder(OptionManager options, EasySubScan scan) { FileScanFramework.FileScanBuilder builder = new FileScanFramework.FileScanBuilder(); builder.setReaderFactory(new AvroReaderFactory()); initScanBuilder(builder, scan); builder.setNullType(Types.optional(TypeProtos.MinorType.VARCHAR)); return builder; }
Source:
protected int processIpV6Packet() { try { return super.processIpV6Packet(); } catch (IllegalStateException ise) { return -1; START } catch (ArrayIndexOutOfBoundsException e) { END return -1; } } Use multi-catch

Target:
protected int processIpV6Packet() { try { return super.processIpV6Packet(); } catch (IllegalStateException | ArrayIndexOutOfBoundsException e) { return -1; } }
Source:
public ConstantFilter(Set<LogicalExpression> constantBoundaries) { START super(); END this.constantBoundaries = constantBoundaries; } ```suggestion ```

Target:
public ConstantFilter(Set<LogicalExpression> constantBoundaries) { this.constantBoundaries = constantBoundaries; }
Source:
public FunctionHolderExpression(String nameUsed, ExpressionPosition pos, List<LogicalExpression> args) { super(pos); this.nameUsed = nameUsed; if (args == null) { this.args = ImmutableList.of(); START } else if (args instanceof ImmutableList) { END this.args = (ImmutableList<LogicalExpression>) args; } else { this.args = ImmutableList.copyOf(args); } } Could you please replace these if blocks with `ImmutableList.copyOf`, it produces these checks internally to avoid copying is possible.

Target:
public FunctionHolderExpression(String nameUsed, ExpressionPosition pos, List<LogicalExpression> args) { super(pos); this.nameUsed = nameUsed; if (args == null) { this.args = ImmutableList.of(); } else { this.args = ImmutableList.copyOf(args); } }
Source:
START public ValueVector getOutgoingValueVector(TypedFieldId fieldId) { END Class<?> clazz = fieldId.getIntermediateClass(); int[] fieldIds = fieldId.getFieldIds(); return outgoingBatch.getValueAccessorById(clazz, fieldIds).getValueVector(); } ```suggestion private ValueVector getOutgoingValueVector(TypedFieldId fieldId) { ```

Target:
private ValueVector getOutgoingValueVector(TypedFieldId fieldId) { Class<?> clazz = fieldId.getIntermediateClass(); int[] fieldIds = fieldId.getFieldIds(); return outgoingBatch.getValueAccessorById(clazz, fieldIds).getValueVector(); }
Source:
START void addField(ValueVector vv, LogicalExpression logicalExpression, OutputColumnType outputColumnType, END String inputColumnName, String outputColumnName) { if(isFixedWidth(vv)) { addFixedWidthField(vv); } else { addVariableWidthField(vv, logicalExpression, outputColumnType, inputColumnName, outputColumnName); } } ```suggestion private void addField(ValueVector vv, LogicalExpression logicalExpression, OutputColumnType outputColumnType, ```

Target:
private void addField(ValueVector vv, LogicalExpression logicalExpression, OutputColumnType outputColumnType, String inputColumnName, String outputColumnName) { if(isFixedWidth(vv)) { addFixedWidthField(vv); } else { addVariableWidthField(vv, logicalExpression, outputColumnType, inputColumnName, outputColumnName); } }
Source:
private CapitalizingJdbcSchema determineDefaultSchema(String connectionSchemaName) { CapitalizingJdbcSchema connSchema; if (connectionSchemaName == null || (connSchema = schemaMap.get(connectionSchemaName.toLowerCase())) == null) { // todo: this selection of default schema isn't correct, users are getting implicit random schema as default connSchema = schemaMap.values().iterator().next(); } START return (CapitalizingJdbcSchema) connSchema.getDefaultSchema(); END } Is casting without instance of check safe here?

Target:
private CapitalizingJdbcSchema determineDefaultSchema(String connectionSchemaName) { CapitalizingJdbcSchema connSchema; if (connectionSchemaName == null || (connSchema = schemaMap.get(connectionSchemaName.toLowerCase())) == null) { connSchema = schemaMap.values().iterator().next(); } return connSchema.getDefaultSchema(); }
Source:
public Table getTable(String name) { if (defaultSchema != null) { try { return defaultSchema.getTable(name); } catch (RuntimeException e) { JdbcStoragePlugin.logger.warn("Failure while attempting to read table '{}' from JDBC source.", name, e); } } // no table was found. return null; START } END Why just runtime exception? I assume you want to ignore all exceptions...

Target:
public Table getTable(String name) { if (defaultSchema != null) { try { return defaultSchema.getTable(name); } catch (RuntimeException e) { logger.warn("Failure while attempting to read table '{}' from JDBC source.", name, e); } } // no table was found. return null; }
Source:
START JdbcIntermediatePrelConverterRule() { END super(JdbcDrel.class, (Predicate<RelNode>) input -> true, DrillRel.DRILL_LOGICAL, Prel.DRILL_PHYSICAL, DrillRelFactories.LOGICAL_BUILDER, "JDBC_PREL_Converter"); } Since this rule does not preserve any state, can we use it as a singleton, i.e. reuse the same instance?

Target:
private JdbcIntermediatePrelConverterRule() { super(JdbcDrel.class, (Predicate<RelNode>) input -> true, DrillRel.DRILL_LOGICAL, Prel.DRILL_PHYSICAL, DrillRelFactories.LOGICAL_BUILDER, "JDBC_PREL_Converter"); }
Source:
private static SqlNode expandAvg(final SqlNode arg) { START final SqlParserPos pos = SqlParserPos.ZERO; END final SqlNode sum = DrillCalciteSqlAggFunctionWrapper.SUM.createCall(pos, arg); final SqlNode count = SqlStdOperatorTable.COUNT.createCall(pos, arg); final SqlNode sumAsDouble = CastHighOp.createCall(pos, sum); return SqlStdOperatorTable.DIVIDE.createCall( pos, sumAsDouble, count); } final

Target:
private static SqlNode expandAvg(final SqlNode arg) { SqlNode sum = DrillCalciteSqlAggFunctionWrapper.SUM.createCall(SqlParserPos.ZERO, arg); SqlNode count = SqlStdOperatorTable.COUNT.createCall(SqlParserPos.ZERO, arg); SqlNode sumAsDouble = CastHighOp.createCall(SqlParserPos.ZERO, sum); return SqlStdOperatorTable.DIVIDE.createCall(SqlParserPos.ZERO, sumAsDouble, count); }
Source:
START private static String readAFileIntoString(String filePath) END { String content = ""; try { content = new String ( Files.readAllBytes( Paths.get(filePath) ) ); } catch (IOException e) { e.printStackTrace(); } return content; } This method has numerous code style issues: - `{` are on the new lines - extra spaces before `(`, ``)` - `e.printStackTrace();`

Target:
private static String readAFileIntoString(String filePath) throws IOException { return new String(Files.readAllBytes(Paths.get(filePath))); }
Source:
public static String getPrincipalFromParts(final String primary, final String instance, final String realm) { checkNotNull(primary); checkNotNull(realm); START return checkNotNull(primary) + END ((instance != "")? "/" + instance: "") + "@" + checkNotNull(realm); } You have already checked that primary and realm are not null

Target:
public static String getPrincipalFromParts(final String primary, final String instance, final String realm) { checkNotNull(primary); checkNotNull(realm); return primary + ((instance != "") ? "/" + instance : "") + "@" + realm; }
Source:
public static RowSetReaderImpl build(BatchAccessor batch) { VectorContainer container = batch.container(); try { return build(container, new HyperSchemaInference().infer(container), batch.selectionVector4()); } catch (SchemaChangeException e) { START throw new UnsupportedOperationException(e); END } } Why is we capture schema change exception, unsupported exception is thrown?

Target:
public static RowSetReaderImpl build(BatchAccessor batch) throws SchemaChangeException { VectorContainer container = batch.container(); return build(container, new HyperSchemaInference().infer(container), batch.selectionVector4()); }
Source:
public <T extends BaseSubScan> CloseableRecordBatch createScan(ExecutorFragmentContext context, BaseSubScan subScan) throws ExecutionSetupException { try { START final ScanFrameworkBuilder builder = END options.scanFactory.scanBuilderShim(this, context.getOptions(), subScan); return builder.buildScanOperator(context, subScan); } catch (final UserException e) { // Rethrow user exceptions directly throw e; } catch (final Throwable e) { // Wrap all others throw new ExecutionSetupException(e); } } Maybe we can remove final here and in exceptions?

Target:
public CloseableRecordBatch createScan(ExecutorFragmentContext context, BaseSubScan subScan) throws ExecutionSetupException { try { ScanFrameworkBuilder builder = options.scanFactory.scanBuilderShim(this, context.getOptions(), subScan); return builder.buildScanOperator(context, subScan); } catch (UserException e) { // Rethrow user exceptions directly throw e; } catch (Throwable e) { // Wrap all others throw new ExecutionSetupException(e); } }
Source:
public BaseSubScan(BaseGroupScan groupScan) { super(groupScan.getUserName()); START storagePlugin = groupScan.storagePlugin(); END this.columns = groupScan.getColumns(); } ```suggestion this.storagePlugin = groupScan.storagePlugin(); ```

Target:
public BaseSubScan(BaseGroupScan groupScan) { super(groupScan.getUserName()); this.storagePlugin = groupScan.storagePlugin(); this.columns = groupScan.getColumns(); }
Source:
public void copy(ColumnReader from) { START throw new UnsupportedOperationException("Cannot copy values through a type converter"); END } If type converters shouldn't support copy then maybe makes sense to mark the method as final here.

Target:
public final void copy(ColumnReader from) { throw new UnsupportedOperationException("Cannot copy values through a type converter"); }
Source:
public void setValueCount(int topLevelValueCount) { if (topLevelValueCount == 0) { return; } int childValueCount; START if (topLevelValueCount == 0) { END childValueCount = 0; offsets.getMutator().setValueCount(0); } else { emptyPopulator.populate(topLevelValueCount); childValueCount = offsets.getAccessor().get(topLevelValueCount); } for (ValueVector v : getChildren()) { v.getMutator().setValueCount(childValueCount); } } Looks like this code wouldn't be triggered because of the return above.

Target:
public void setValueCount(int topLevelValueCount) { int childValueCount; if (topLevelValueCount == 0) { childValueCount = 0; offsets.getMutator().setValueCount(0); } else { emptyPopulator.populate(topLevelValueCount); childValueCount = offsets.getAccessor().get(topLevelValueCount); } for (ValueVector v : getChildren()) { v.getMutator().setValueCount(childValueCount); } }
Source:
public HttpGroupScan ( HttpStoragePluginConfig config, HttpScanSpec scanSpec, List<SchemaPath> columns ) { super("no-user"); START this.httpStoragePluginConfig = config; END this.httpScanSpec = scanSpec; this.columns = columns == null || columns.size() == 0 ? ALL_COLUMNS : columns; } Group scans are wildly complicate (too much so.) One of the odd things is that they are serialized to JSON. (As part of the "logical plan.") This means that this class must be Jackson-serializable. Here, your plugin takes the config (which is serializable). If you find you need the plugin itself, then there is a hokey-pokey you must do. See [this wiki](https://github.com/paul-rogers/drill/wiki/Create-a-Storage-Plugin) for more info.

Target:
public HttpGroupScan ( HttpStoragePluginConfig config, HttpScanSpec scanSpec, List<SchemaPath> columns) { super("no-user"); this.config = config; this.httpScanSpec = scanSpec; this.columns = columns; }
Source:
public SubScan getSpecificScan(int minorFragmentId) { logger.debug("HttpGroupScan getSpecificScan"); START return new HttpSubScan(httpStoragePluginConfig, httpScanSpec, columns); END } This will be called once because `getMaxParallelizationWidth()` returns 1. Should this plugin allow sharding requests? If I'm requesting a time range, say, should the plugin try to split the time ranges into multiple small requests that Drill can spread across Drillbits? That is a VERY complex topic that I'm still trying to sort out...

Target:
public SubScan getSpecificScan(int minorFragmentId) { return new HttpSubScan(config, httpScanSpec, columns); }
Source:
public Table getTable(String tableName) { DynamicDrillTable table = activeTables.get(name); if (table != null) { return table; } if (!activeTables.containsKey(name)) { return registerTable(name, new DynamicDrillTable(plugin, pluginName, new HttpScanSpec(pluginName, name, tableName, plugin.getConfig()))); } START return null; END } Can never get here given the logic flow above.

Target:
public Table getTable(String tableName) { DynamicDrillTable table = activeTables.get(name); if (table != null) { // Return the found table return table; } else { // Register a new table return registerTable(name, new DynamicDrillTable(plugin, pluginName, new HttpScanSpec(pluginName, name, tableName, plugin.getConfig()))); } }
Source:
public int getMaxParallelizationWidth() { START return 0; END } I suspect this must be at least 1. We can't read much data from 0 worker threads.

Target:
public int getMaxParallelizationWidth() { return 1; }
Source:
public void registerSchemas(SchemaConfig schemaConfig, SchemaPlus parent) throws IOException { HttpSchema schema = new HttpSchema(getName()); logger.debug("Registering {}", schema.getName(), schema.toString()); START SchemaPlus schemaPlus = parent.add(getName(), schema); END schema.setHolder(schemaPlus); } Isn't this redundant with the `HttpAPIConnectionSchema` class above? Do we need two? Which one should we review?

Target:
public void registerSchemas(SchemaConfig schemaConfig, SchemaPlus parent) { HttpSchema schema = new HttpSchema(getName()); logger.debug("Registering {}", schema.getName(), schema.toString()); SchemaPlus schemaPlus = parent.add(getName(), schema); schema.setHolder(schemaPlus); }
Source:
public SubScan getSpecificScan(int minorFragmentId) { START logger.debug("HttpGroupScan getSpecificScan"); END return new HttpSubScan(config, httpScanSpec, columns); } Unless you debug by reading logs, no need for these debug statements. These methods are called when needed.

Target:
public SubScan getSpecificScan(int minorFragmentId) { return new HttpSubScan(config, httpScanSpec, columns); }
Source:
public AbstractSchema getSubSchema(String name) { try { if (!plugin.getConfig().connections().containsKey(name)) { throw UserException .connectionError() .message("API '{}' does not exist in HTTP Storage plugin '{}'", name, getName()) .build(logger); } return getSubSchemaKnownExists(name); } catch (Exception e) { START throw new DrillRuntimeException(e); END } } Was this a copy/paste? If we go to the trouble of throwing a `UserException`, we should throw it, not catch it and wrap it in another kind of exception (which will, later be wrapped in a `UserException`.) Just let the `UserException` be thrown. Also, nit, reverse polarity: `if (...containsKey...) { return get... } else { throw ... }`

Target:
public AbstractSchema getSubSchema(String name) { if (plugin.getConfig().connections().containsKey(name)) { return getSubSchemaKnownExists(name); } else { throw UserException .connectionError() .message("API '{}' does not exist in HTTP Storage plugin '{}'", name, getName()) .build(logger); } }
Source:
public BasicAuthInterceptor(String user, String password) { credentials = Credentials.basic(user, password); START logger.debug("Intercepting request adding creds: {}", credentials); END } Please do not log credentials. Security guys (such as yourself) don't like that.

Target:
public BasicAuthInterceptor(String user, String password) { credentials = Credentials.basic(user, password); }