Skip to content

Commit 523366a

Browse files
author
Megan Foss
committed
Modified validation for field width and field index. Added comments to code.
1 parent fa47a14 commit 523366a

File tree

2 files changed

+12
-25
lines changed

2 files changed

+12
-25
lines changed

contrib/format-fixedwidth/src/main/java/org/apache/drill/exec/store/fixedwidth/FixedwidthFieldConfig.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ public FixedwidthFieldConfig(@JsonProperty("name") String name,
6464

6565
public int getWidth() {return width;}
6666

67-
public void setWidth(int value) {
68-
this.width = value;
69-
}
70-
7167
public TypeProtos.MinorType getType() {return type;}
7268

7369
public void setType() {

contrib/format-fixedwidth/src/main/java/org/apache/drill/exec/store/fixedwidth/FixedwidthFormatConfig.java

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ public class FixedwidthFormatConfig implements FormatPluginConfig {
4545
private static final Logger logger = LoggerFactory.getLogger(FixedwidthFormatConfig.class);
4646
private final List<String> extensions;
4747
private final List<FixedwidthFieldConfig> fields;
48-
private final List<TypeProtos.MinorType> validDataTypes = Arrays.asList(new TypeProtos.MinorType[]{TypeProtos.MinorType.INT, TypeProtos.MinorType.VARCHAR,
48+
private final List<TypeProtos.MinorType> validDataTypes = Arrays.asList(TypeProtos.MinorType.INT, TypeProtos.MinorType.VARCHAR,
4949
TypeProtos.MinorType.DATE, TypeProtos.MinorType.TIME, TypeProtos.MinorType.TIMESTAMP, TypeProtos.MinorType.FLOAT4,
50-
TypeProtos.MinorType.FLOAT8, TypeProtos.MinorType.BIGINT, TypeProtos.MinorType.VARDECIMAL});
50+
TypeProtos.MinorType.FLOAT8, TypeProtos.MinorType.BIGINT, TypeProtos.MinorType.VARDECIMAL);
5151

5252
@JsonCreator
5353
public FixedwidthFormatConfig(@JsonProperty("extensions") List<String> extensions,
@@ -139,15 +139,6 @@ public List<Integer> getFieldWidths() {
139139
return result;
140140
}
141141

142-
@JsonIgnore
143-
public void setFieldWidths(int i, int value) {
144-
for (FixedwidthFieldConfig field : fields) {
145-
if (field.getIndex() == i) {
146-
field.setWidth(value);
147-
}
148-
}
149-
}
150-
151142
@JsonIgnore
152143
public List<TypeProtos.MinorType> getFieldTypes() {
153144
List<TypeProtos.MinorType> result = new ArrayList<>();
@@ -180,7 +171,7 @@ public void validateFieldInput(){
180171
int width = 0;
181172
int prevIndexAndWidth = -1;
182173

183-
// Ensure no two fields have the same name
174+
// Validate Field Name - Ensure field is not empty, no two fields have the same name, and field is valid SQL syntax
184175
for (String name : this.getFieldNames()){
185176
if (name.length() == 0){
186177
throw UserException
@@ -199,7 +190,7 @@ public void validateFieldInput(){
199190
uniqueNames.add(name);
200191
}
201192

202-
//assuming that fieldIndices is the same size as fieldWidths, width is required
193+
// Validate Field Index - Must be greater than 0, and must not overlap with other fields
203194
for (int i = 0; i<fieldIndices.size(); i++) {
204195
if (fieldIndices.get(i) < 1) {
205196
throw UserException
@@ -216,26 +207,26 @@ else if (fieldIndices.get(i) <= prevIndexAndWidth) {
216207
.build(logger);
217208
}
218209

210+
// Validate Field Width - must be greater than 0.
219211
if (fieldWidths.get(i) == null || fieldWidths.get(i) < 1) {
220-
// Come back to this - can we calculate this instead of throwing an error?
221-
if (i == fieldIndices.size()-1) {
222212
throw UserException
223213
.validationError()
224-
.message("Width for field '" + fieldNames.get(i) + "' is empty.")
214+
.message("Width for field '" + fieldNames.get(i) + "' is invalid. Widths must be greater than 0.")
225215
.addContext("Plugin", FixedwidthFormatPlugin.DEFAULT_NAME)
226216
.build(logger);
227-
}
228-
width = fieldIndices.get(i+1) - fieldIndices.get(i) - 1;
229-
setFieldWidths(fieldIndices.get(i), width);
230217
}
231218
prevIndexAndWidth = fieldIndices.get(i) + fieldWidths.get(i);
232219

233-
// Validate Field Type
220+
// Validate Field Type - must not be empty and must be included in list of valid data types for the fixed width plugin
234221
if (fieldTypes.get(i) == null || fieldTypes.get(i).toString().length() == 0) {
235222
setFieldTypes(fieldIndices.get(i));
236223
}
237224
else if (!validDataTypes.contains(fieldTypes.get(i))){
238-
setFieldTypes(fieldIndices.get(i)); //Should we throw an error or default to VARCHAR for data types that are not yet available in this plugin
225+
throw UserException
226+
.validationError()
227+
.message("Field type " + fieldTypes.get(i) + " is not valid. Please check for typos and ensure the required data type is included in the Fixed Width Format Plugin.")
228+
.addContext("Plugin", FixedwidthFormatPlugin.DEFAULT_NAME)
229+
.build(logger);
239230
}
240231
}
241232
}

0 commit comments

Comments
 (0)