-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bug fix for using set-type when it's the first directive in the recipie #688
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -134,33 +134,12 @@ public Schema getOutputSchema(SchemaResolutionContext context) { | |
if (field.getName().equals(col)) { | ||
Integer outputScale = scale; | ||
Integer outputPrecision = precision; | ||
Schema fieldSchema = field.getSchema().getNonNullable(); | ||
Pair<Integer, Integer> scaleAndPrecision = getPrecisionAndScale(fieldSchema); | ||
Integer inputSchemaScale = scaleAndPrecision.getSecond(); | ||
Integer inputSchemaPrecision = scaleAndPrecision.getFirst(); | ||
|
||
if (scale == null && precision == null) { | ||
outputScale = inputSchemaScale; | ||
outputPrecision = inputSchemaPrecision; | ||
} else if (scale == null && inputSchemaScale != null) { | ||
if (precision - inputSchemaScale < 1) { | ||
throw new DirectiveParseException(String.format( | ||
"Cannot set scale as '%s' and precision as '%s' when " | ||
+ "given precision - scale is less than 1 ", inputSchemaScale, | ||
precision)); | ||
} | ||
outputScale = inputSchemaScale; | ||
outputPrecision = precision; | ||
|
||
} else if (precision == null && inputSchemaPrecision != null) { | ||
if (inputSchemaPrecision - scale < 1) { | ||
throw new DirectiveParseException(String.format( | ||
"Cannot set scale as '%s' and precision as '%s' when " | ||
+ "given precision - scale is less than 1 ", scale, | ||
inputSchemaPrecision)); | ||
} | ||
outputScale = scale; | ||
outputPrecision = inputSchemaPrecision; | ||
if (type.equalsIgnoreCase("decimal") && field.getSchema().isNullable()) { | ||
Schema fieldSchema = field.getSchema().getNonNullable(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, I think this giant block that calculates precision and scale to use can be moved to a different function. Can refactor the function used in .map() There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
Pair<Integer, Integer> scaleAndPrecision = getValidatedPrecisionAndScale( | ||
fieldSchema, precision, scale); | ||
outputScale = scaleAndPrecision.getSecond(); | ||
outputPrecision = scaleAndPrecision.getFirst(); | ||
} | ||
return Schema.Field.of(col, ColumnConverter.getSchemaForType(type, | ||
outputScale, outputPrecision)); | ||
|
@@ -171,20 +150,50 @@ public Schema getOutputSchema(SchemaResolutionContext context) { | |
} | ||
} | ||
) | ||
.collect(Collectors.toList()) | ||
.collect(Collectors.toList()) | ||
); | ||
} | ||
|
||
/** | ||
* extracts precision and scale from schema string | ||
*/ | ||
public static Pair<Integer, Integer> getPrecisionAndScale(Schema fieldSchema) { | ||
Integer precision = null; | ||
Integer scale = null; | ||
if (fieldSchema.getLogicalType() == LogicalType.DECIMAL) { | ||
precision = fieldSchema.getPrecision(); | ||
scale = fieldSchema.getScale(); | ||
public static Pair<Integer, Integer> getValidatedPrecisionAndScale(Schema fieldSchema, | ||
Integer precision, Integer scale) | ||
throws DirectiveParseException { //check precision and scale | ||
Integer outputPrecision = precision; | ||
Integer outputScale = scale; | ||
Integer inputSchemaPrecision = null; | ||
Integer inputSchemaScale = null; | ||
|
||
if (fieldSchema.getLogicalType() == LogicalType.DECIMAL) { | ||
inputSchemaPrecision = fieldSchema.getPrecision(); | ||
inputSchemaScale = fieldSchema.getScale(); | ||
} | ||
|
||
if (scale == null && precision == null) { | ||
outputScale = inputSchemaScale; | ||
outputPrecision = inputSchemaPrecision; | ||
} else if (scale == null && inputSchemaScale != null) { | ||
if (precision - inputSchemaScale < 1) { | ||
throw new DirectiveParseException(String.format( | ||
"Cannot set scale as '%s' and precision as '%s' when " | ||
+ "given precision - scale is less than 1 ", inputSchemaScale, | ||
precision)); | ||
} | ||
return new Pair<Integer, Integer>(precision, scale); | ||
outputScale = inputSchemaScale; | ||
outputPrecision = precision; | ||
|
||
} else if (precision == null && inputSchemaPrecision != null) { | ||
if (inputSchemaPrecision - scale < 1) { | ||
throw new DirectiveParseException(String.format( | ||
"Cannot set scale as '%s' and precision as '%s' when " | ||
+ "given precision - scale is less than 1 ", scale, | ||
inputSchemaPrecision)); | ||
} | ||
outputScale = scale; | ||
outputPrecision = inputSchemaPrecision; | ||
} | ||
|
||
return new Pair<Integer, Integer>(outputPrecision, outputScale); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens when schema is non nullable but set type directive is applied with 'decimal' as the type?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the schema is non nullable it will skip this if condition and will go to the return statement in line no:167. By default outputScale and outputPrecision are the scale and precision specified by the user. In this case we don't validate if the user specified scale and precision values clash with the original scale and precision values of that column.