Skip to content

Commit 6ffcfaa

Browse files
committed
review comments -0
1 parent 6c686ae commit 6ffcfaa

File tree

1 file changed

+42
-35
lines changed
  • wrangler-core/src/main/java/io/cdap/directives/column

1 file changed

+42
-35
lines changed

wrangler-core/src/main/java/io/cdap/directives/column/SetType.java

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -136,33 +136,10 @@ public Schema getOutputSchema(SchemaResolutionContext context) {
136136
Integer outputPrecision = precision;
137137
if (type.equalsIgnoreCase("decimal") && field.getSchema().isNullable()) {
138138
Schema fieldSchema = field.getSchema().getNonNullable();
139-
Pair<Integer, Integer> scaleAndPrecision = getPrecisionAndScale(fieldSchema);
140-
Integer inputSchemaScale = scaleAndPrecision.getSecond();
141-
Integer inputSchemaPrecision = scaleAndPrecision.getFirst();
142-
143-
if (scale == null && precision == null) {
144-
outputScale = inputSchemaScale;
145-
outputPrecision = inputSchemaPrecision;
146-
} else if (scale == null && inputSchemaScale != null) {
147-
if (precision - inputSchemaScale < 1) {
148-
throw new DirectiveParseException(String.format(
149-
"Cannot set scale as '%s' and precision as '%s' when "
150-
+ "given precision - scale is less than 1 ", inputSchemaScale,
151-
precision));
152-
}
153-
outputScale = inputSchemaScale;
154-
outputPrecision = precision;
155-
156-
} else if (precision == null && inputSchemaPrecision != null) {
157-
if (inputSchemaPrecision - scale < 1) {
158-
throw new DirectiveParseException(String.format(
159-
"Cannot set scale as '%s' and precision as '%s' when "
160-
+ "given precision - scale is less than 1 ", scale,
161-
inputSchemaPrecision));
162-
}
163-
outputScale = scale;
164-
outputPrecision = inputSchemaPrecision;
165-
}
139+
Pair<Integer, Integer> scaleAndPrecision = getValidatedPrecisionAndScale(
140+
fieldSchema, precision, scale);
141+
outputScale = scaleAndPrecision.getSecond();
142+
outputPrecision = scaleAndPrecision.getFirst();
166143
}
167144
return Schema.Field.of(col, ColumnConverter.getSchemaForType(type,
168145
outputScale, outputPrecision));
@@ -173,20 +150,50 @@ public Schema getOutputSchema(SchemaResolutionContext context) {
173150
}
174151
}
175152
)
176-
.collect(Collectors.toList())
153+
.collect(Collectors.toList())
177154
);
178155
}
179156

180157
/**
181158
* extracts precision and scale from schema string
182159
*/
183-
public static Pair<Integer, Integer> getPrecisionAndScale(Schema fieldSchema) {
184-
Integer precision = null;
185-
Integer scale = null;
186-
if (fieldSchema.getLogicalType() == LogicalType.DECIMAL) {
187-
precision = fieldSchema.getPrecision();
188-
scale = fieldSchema.getScale();
160+
public static Pair<Integer, Integer> getValidatedPrecisionAndScale(Schema fieldSchema,
161+
Integer precision, Integer scale)
162+
throws DirectiveParseException { //check precision and scale
163+
Integer outputPrecision = precision;
164+
Integer outputScale = scale;
165+
Integer inputSchemaPrecision = null;
166+
Integer inputSchemaScale = null;
167+
168+
if (fieldSchema.getLogicalType() == LogicalType.DECIMAL) {
169+
inputSchemaPrecision = fieldSchema.getPrecision();
170+
inputSchemaScale = fieldSchema.getScale();
171+
}
172+
173+
if (scale == null && precision == null) {
174+
outputScale = inputSchemaScale;
175+
outputPrecision = inputSchemaPrecision;
176+
} else if (scale == null && inputSchemaScale != null) {
177+
if (precision - inputSchemaScale < 1) {
178+
throw new DirectiveParseException(String.format(
179+
"Cannot set scale as '%s' and precision as '%s' when "
180+
+ "given precision - scale is less than 1 ", inputSchemaScale,
181+
precision));
189182
}
190-
return new Pair<Integer, Integer>(precision, scale);
183+
outputScale = inputSchemaScale;
184+
outputPrecision = precision;
185+
186+
} else if (precision == null && inputSchemaPrecision != null) {
187+
if (inputSchemaPrecision - scale < 1) {
188+
throw new DirectiveParseException(String.format(
189+
"Cannot set scale as '%s' and precision as '%s' when "
190+
+ "given precision - scale is less than 1 ", scale,
191+
inputSchemaPrecision));
192+
}
193+
outputScale = scale;
194+
outputPrecision = inputSchemaPrecision;
191195
}
196+
197+
return new Pair<Integer, Integer>(outputPrecision, outputScale);
198+
}
192199
}

0 commit comments

Comments
 (0)