Skip to content
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

[CDAP-15361] Fix set-type directive for decimal when scale is null #673

Merged
merged 3 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
vanathi-g marked this conversation as resolved.
Show resolved Hide resolved
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
Loading