Skip to content
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 @@ -20,6 +20,7 @@
import static java.util.stream.Collectors.toList;
import static org.apache.beam.sdk.schemas.Schema.TypeName.BOOLEAN;
import static org.apache.beam.sdk.schemas.Schema.TypeName.BYTE;
import static org.apache.beam.sdk.schemas.Schema.TypeName.DECIMAL;
import static org.apache.beam.sdk.schemas.Schema.TypeName.DOUBLE;
import static org.apache.beam.sdk.schemas.Schema.TypeName.FLOAT;
import static org.apache.beam.sdk.schemas.Schema.TypeName.INT16;
Expand All @@ -28,6 +29,7 @@
import static org.apache.beam.sdk.schemas.Schema.TypeName.STRING;
import static org.apache.beam.sdk.util.RowJsonValueExtractors.booleanValueExtractor;
import static org.apache.beam.sdk.util.RowJsonValueExtractors.byteValueExtractor;
import static org.apache.beam.sdk.util.RowJsonValueExtractors.decimalValueExtractor;
import static org.apache.beam.sdk.util.RowJsonValueExtractors.doubleValueExtractor;
import static org.apache.beam.sdk.util.RowJsonValueExtractors.floatValueExtractor;
import static org.apache.beam.sdk.util.RowJsonValueExtractors.intValueExtractor;
Expand Down Expand Up @@ -83,6 +85,7 @@ public class RowJsonDeserializer extends StdDeserializer<Row> {
.put(DOUBLE, doubleValueExtractor())
.put(BOOLEAN, booleanValueExtractor())
.put(STRING, stringValueExtractor())
.put(DECIMAL, decimalValueExtractor())
.build();

private Schema schema;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import static org.apache.beam.sdk.schemas.Schema.TypeName.BOOLEAN;
import static org.apache.beam.sdk.schemas.Schema.TypeName.BYTE;
import static org.apache.beam.sdk.schemas.Schema.TypeName.DECIMAL;
import static org.apache.beam.sdk.schemas.Schema.TypeName.DOUBLE;
import static org.apache.beam.sdk.schemas.Schema.TypeName.FLOAT;
import static org.apache.beam.sdk.schemas.Schema.TypeName.INT16;
Expand All @@ -37,7 +38,7 @@
class RowJsonValidation {

private static final ImmutableSet<Schema.TypeName> SUPPORTED_TYPES =
ImmutableSet.of(BYTE, INT16, INT32, INT64, FLOAT, DOUBLE, BOOLEAN, STRING);
ImmutableSet.of(BYTE, INT16, INT32, INT64, FLOAT, DOUBLE, BOOLEAN, STRING, DECIMAL);

static void verifyFieldTypeSupported(Schema.Field field) {
Schema.FieldType fieldType = field.getType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,18 @@ static ValueExtractor<String> stringValueExtractor() {
.build();
}

/**
* Extracts BigDecimal from the JsonNode if it is within bounds.
*
* <p>Throws {@link UnsupportedRowJsonException} if value is out of bounds.
*/
static ValueExtractor<BigDecimal> decimalValueExtractor() {
return ValidatingValueExtractor.<BigDecimal>builder()
.setExtractor(JsonNode::decimalValue)
.setValidator(jsonNode -> jsonNode.isNumber())
.build();
}

@AutoValue
public abstract static class ValidatingValueExtractor<W> implements ValueExtractor<W> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import java.math.BigDecimal;
import java.util.Arrays;
import org.apache.beam.sdk.schemas.Schema;
import org.apache.beam.sdk.schemas.Schema.FieldType;
Expand Down Expand Up @@ -66,6 +67,7 @@ public void testParsesFlatRow() throws Exception {
.addDoubleField("f_double")
.addBooleanField("f_boolean")
.addStringField("f_string")
.addDecimalField("f_decimal")
.build();

String rowString =
Expand All @@ -77,7 +79,8 @@ public void testParsesFlatRow() throws Exception {
+ "\"f_float\" : 1.02E5,\n"
+ "\"f_double\" : 62.2,\n"
+ "\"f_boolean\" : true,\n"
+ "\"f_string\" : \"hello\"\n"
+ "\"f_string\" : \"hello\",\n"
+ "\"f_decimal\" : 123.12\n"
+ "}";

RowJsonDeserializer deserializer = RowJsonDeserializer.forSchema(schema);
Expand All @@ -86,7 +89,16 @@ public void testParsesFlatRow() throws Exception {

Row expectedRow =
Row.withSchema(schema)
.addValues((byte) 12, (short) 22, 32, (long) 42, 1.02E5f, 62.2d, true, "hello")
.addValues(
(byte) 12,
(short) 22,
32,
(long) 42,
1.02E5f,
62.2d,
true,
"hello",
new BigDecimal("123.12"))
.build();

assertEquals(expectedRow, parsedRow);
Expand Down