Skip to content

Commit

Permalink
Added method to validate field name input and verify there are no dup…
Browse files Browse the repository at this point in the history
…licates. Modified tests to enable testing of new constructor.
  • Loading branch information
Megan Foss committed Nov 5, 2021
1 parent 9d66f91 commit 9b95c45
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public FixedwidthFieldConfig(@JsonProperty("name") String name,
this.dateTimeFormat = dateTimeFormat;



// Need to verify names are different - where can we access all the names of other columns
// if(name != null){
// this.name = name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,25 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import org.apache.drill.common.PlanStringBuilder;
import org.apache.drill.common.exceptions.UserException;
import org.apache.drill.common.logical.FormatPluginConfig;
import org.apache.drill.exec.store.log.LogFormatField;
import org.apache.drill.exec.store.log.LogFormatPlugin;
import org.apache.drill.shaded.guava.com.google.common.collect.ImmutableList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

@JsonTypeName(FixedwidthFormatPlugin.DEFAULT_NAME)
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
public class FixedwidthFormatConfig implements FormatPluginConfig {
private static final Logger logger = LoggerFactory.getLogger(FixedwidthFormatConfig.class);
private final List<String> extensions;
private final List<FixedwidthFieldConfig> fields;

Expand All @@ -44,6 +51,8 @@ public FixedwidthFormatConfig(@JsonProperty("extensions") List<String> extension
@JsonProperty("fields") List<FixedwidthFieldConfig> fields) {
this.extensions = extensions == null ? Collections.singletonList("fwf") : ImmutableList.copyOf(extensions);
this.fields = fields;

validateFieldInput();
}

@JsonInclude(JsonInclude.Include.NON_DEFAULT)
Expand Down Expand Up @@ -101,13 +110,21 @@ public List<String> getFieldNames() {
}

@JsonIgnore
public boolean validateFieldNames(String fieldName){
boolean result = false;
List<String> names = this.getFieldNames();
if (names.contains(fieldName)){
result = true;
public void validateFieldInput(){
Set<String> uniqueNames = new HashSet<>();
for (String name : this.getFieldNames()){
if (name.length() == 0){

}
if (uniqueNames.contains(name)){
throw UserException
.validationError()
.message("Duplicate column name: " + name)
.addContext("Plugin", FixedwidthFormatPlugin.DEFAULT_NAME)
.build(logger);
}
uniqueNames.add(name);
}
return result;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static void setup() throws Exception {

FixedwidthFormatConfig formatConfig = new FixedwidthFormatConfig(Lists.newArrayList("fwf"),
Lists.newArrayList(
new FixedwidthFieldConfig("Number", 1, 5, TypeProtos.MinorType.VARDECIMAL, ""),
new FixedwidthFieldConfig("Number", 1, 5, TypeProtos.MinorType.VARDECIMAL),
new FixedwidthFieldConfig("Letter", 7,4, TypeProtos.MinorType.VARCHAR, ""),
new FixedwidthFieldConfig("Address",12, 3,TypeProtos.MinorType.INT, ""),
new FixedwidthFieldConfig("Date",16, 10,TypeProtos.MinorType.DATE, "MM-dd-yyyy"),
Expand Down

0 comments on commit 9b95c45

Please sign in to comment.