Skip to content

Commit

Permalink
Merge pull request #673 from data-integrations/scale-null-bug-fix
Browse files Browse the repository at this point in the history
Fix set-type directive for decimal when scale is null
  • Loading branch information
minurajeeve authored Oct 24, 2023
2 parents ad7736c + ab025fe commit 0707064
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ public static Schema getSchemaForType(String type, Integer scale) throws Directi
type = type.toUpperCase();
if (type.equals(ColumnTypeNames.DECIMAL)) {
// TODO make set-type support setting decimal precision
typeSchema = Schema.nullableOf(Schema.decimalOf(38, scale));
scale = scale != null ? scale : 38;
typeSchema = Schema.nullableOf(Schema.decimalOf(77, scale));
} else {
if (!SCHEMA_TYPE_MAP.containsKey(type)) {
throw new DirectiveParseException(String.format("'%s' is an unsupported type. " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,36 @@ public void testToDecimalInvalidRoundingMode() throws Exception {
TestingRig.execute(directives, rows);
}

@Test
public void testToDecimalScaleIsNull() throws Exception {
List<Row> rows = Collections.singletonList(new Row("scale_2", "125.45"));
String[] directives = new String[] {"set-type scale_2 decimal"};
Schema inputSchema = Schema.recordOf(
"inputSchema",
Schema.Field.of("scale_2", Schema.of(Schema.Type.DOUBLE))
);

Schema expectedSchema = Schema.recordOf(
"expectedSchema",
Schema.Field.of("scale_2", Schema.decimalOf(77, 38))
);

List<Row> results = TestingRig.execute(directives, rows);
Schema outputSchema = TestingRig.executeAndGetSchema(directives, rows, inputSchema);
Row row = results.get(0);
Schema.Field outputSchemaField = expectedSchema.getFields().get(0);

Assert.assertTrue(row.getValue(0) instanceof BigDecimal);
Assert.assertEquals(row.getValue(0), new BigDecimal("125.45"));

Assert.assertEquals(outputSchemaField.getSchema().getType(),
outputSchema.getField(outputSchemaField.getName()).getSchema().getNonNullable().getType());
Assert.assertEquals(outputSchemaField.getSchema().getPrecision(),
outputSchema.getField(outputSchemaField.getName()).getSchema().getNonNullable().getPrecision());
Assert.assertEquals(outputSchemaField.getSchema().getScale(),
outputSchema.getField(outputSchemaField.getName()).getSchema().getNonNullable().getScale());
}

@Test
public void testToBoolean() throws Exception {
List<Row> trueRows = Collections.singletonList(
Expand Down

0 comments on commit 0707064

Please sign in to comment.