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 @@ -47,9 +47,11 @@
import java.nio.ByteBuffer;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.IntUnaryOperator;
Expand All @@ -62,6 +64,7 @@
import static com.fasterxml.jackson.core.JsonToken.START_ARRAY;
import static com.fasterxml.jackson.core.JsonToken.START_OBJECT;
import static com.fasterxml.jackson.core.JsonToken.VALUE_NULL;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static io.trino.hive.formats.HiveFormatUtils.createTimestampParser;
import static io.trino.hive.formats.HiveFormatUtils.parseHiveDate;
Expand All @@ -85,6 +88,7 @@
import static java.lang.StrictMath.toIntExact;
import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Objects.requireNonNull;
import static org.joda.time.DateTimeZone.UTC;

/**
Expand Down Expand Up @@ -135,7 +139,7 @@ public JsonDeserializer(List<Column> columns, List<String> timestampFormats)
columns.stream()
.map(Column::type)
.map(fieldType -> createDecoder(fieldType, timestampParser))
.collect(toImmutableList()),
.toArray(Decoder[]::new),
topLevelOrdinalMap::get);
}

Expand Down Expand Up @@ -212,7 +216,7 @@ private static Decoder createDecoder(Type type, Function<String, DecodedTimestam
rowType.getFields().stream()
.map(Field::getType)
.map(fieldType -> createDecoder(fieldType, timestampParser))
.collect(toImmutableList()),
.toArray(Decoder[]::new),
IntUnaryOperator.identity());
}
throw new UnsupportedOperationException("Unsupported column type: " + type);
Expand All @@ -221,21 +225,24 @@ private static Decoder createDecoder(Type type, Function<String, DecodedTimestam
private abstract static class Decoder
{
private final Type type;
private final boolean isScalarType;

public Decoder(Type type)
{
this.type = type;
this.type = requireNonNull(type, "type is null");
this.isScalarType = isScalarType(type);
}

public final void decode(JsonParser parser, BlockBuilder builder)
throws IOException
{
if (parser.currentToken() == VALUE_NULL) {
JsonToken currentToken = parser.currentToken();
if (currentToken == VALUE_NULL) {
builder.appendNull();
return;
}

if (isScalarType(type) && !parser.currentToken().isScalarValue()) {
if (isScalarType && !currentToken.isScalarValue()) {
throw invalidJson(type + " value must be a scalar json value");
}
decodeValue(parser, builder);
Expand Down Expand Up @@ -340,7 +347,7 @@ private static class DecimalDecoder
public DecimalDecoder(DecimalType decimalType)
{
super(decimalType);
this.decimalType = decimalType;
this.decimalType = requireNonNull(decimalType, "decimalType is null");
}

@Override
Expand Down Expand Up @@ -427,8 +434,8 @@ private static class TimestampDecoder
public TimestampDecoder(TimestampType timestampType, Function<String, DecodedTimestamp> timestampParser)
{
super(timestampType);
this.timestampType = timestampType;
this.timestampParser = timestampParser;
this.timestampType = requireNonNull(timestampType, "timestampType is null");
this.timestampParser = requireNonNull(timestampParser, "timestampParser is null");
}

@Override
Expand Down Expand Up @@ -483,7 +490,7 @@ private static class VarcharDecoder
public VarcharDecoder(VarcharType varcharType)
{
super(varcharType);
this.varcharType = varcharType;
this.varcharType = requireNonNull(varcharType, "varcharType is null");
}

@Override
Expand All @@ -502,7 +509,7 @@ private static class CharDecoder
public CharDecoder(CharType charType)
{
super(charType);
this.charType = charType;
this.charType = requireNonNull(charType, "charType is null");
}

@Override
Expand All @@ -521,7 +528,7 @@ private static class ArrayDecoder
public ArrayDecoder(ArrayType arrayType, Decoder elementDecoder)
{
super(arrayType);
this.elementDecoder = elementDecoder;
this.elementDecoder = requireNonNull(elementDecoder, "elementDecoder is null");
}

@Override
Expand Down Expand Up @@ -558,8 +565,8 @@ public MapDecoder(MapType mapType, Decoder valueDecoder, Function<String, Decode
super(mapType);
this.keyType = mapType.getKeyType();
this.valueType = mapType.getValueType();
this.valueDecoder = valueDecoder;
this.timestampParser = timestampParser;
this.valueDecoder = requireNonNull(valueDecoder, "valueDecoder is null");
this.timestampParser = requireNonNull(timestampParser, "timestampParser is null");

this.distinctMapKeys = new DistinctMapKeys(mapType, true);
this.keyBlockBuilder = mapType.getKeyType().createBlockBuilder(null, 128);
Expand Down Expand Up @@ -656,10 +663,11 @@ private static class RowDecoder
private static final Pattern INTERNAL_PATTERN = Pattern.compile("_col([0-9]+)");

private final Map<String, Integer> fieldPositions;
private final List<Decoder> fieldDecoders;
private final Decoder[] fieldDecoders;
private final boolean[] fieldWritten;
private final IntUnaryOperator ordinalToFieldPosition;

public RowDecoder(RowType rowType, List<Decoder> fieldDecoders, IntUnaryOperator ordinalToFieldPosition)
public RowDecoder(RowType rowType, Decoder[] fieldDecoders, IntUnaryOperator ordinalToFieldPosition)
{
super(rowType);

Expand All @@ -670,8 +678,11 @@ public RowDecoder(RowType rowType, List<Decoder> fieldDecoders, IntUnaryOperator
fieldPositions.put(field.getName().orElseThrow().toLowerCase(Locale.ROOT), i);
}
this.fieldPositions = fieldPositions.buildOrThrow();
this.fieldDecoders = fieldDecoders;
this.ordinalToFieldPosition = ordinalToFieldPosition;
this.fieldDecoders = requireNonNull(fieldDecoders, "fieldDecoders is null");
checkArgument(this.fieldDecoders.length == fields.size(), "fieldDecoders size mismatch: %s <> %s", this.fieldDecoders.length, fields.size());
checkArgument(Arrays.stream(this.fieldDecoders).noneMatch(Objects::isNull), "fieldDecoders contains null element");
this.fieldWritten = new boolean[this.fieldDecoders.length];
this.ordinalToFieldPosition = requireNonNull(ordinalToFieldPosition, "ordinalToFieldPosition is null");
}

public void decode(JsonParser parser, PageBuilder builder)
Expand All @@ -695,7 +706,7 @@ private void decodeValue(JsonParser parser, int currentPosition, IntFunction<Blo
throw invalidJson("start of object expected");
}

boolean[] fieldWritten = new boolean[fieldDecoders.size()];
Arrays.fill(fieldWritten, false);

while (nextObjectField(parser)) {
String fieldName = parser.getText();
Expand All @@ -711,7 +722,7 @@ private void decodeValue(JsonParser parser, int currentPosition, IntFunction<Blo
}

nextTokenRequired(parser);
fieldDecoders.get(rowIndex).decode(parser, fieldBuilder);
fieldDecoders[rowIndex].decode(parser, fieldBuilder);
fieldWritten[rowIndex] = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.IntFunction;
import java.util.function.IntUnaryOperator;
Expand Down Expand Up @@ -128,7 +130,7 @@ public OpenXJsonDeserializer(List<Column> columns, OpenXJsonOptions options)
columns.stream()
.map(Column::type)
.map(fieldType -> createDecoder(fieldType, options, timestampFormatters))
.collect(toImmutableList()),
.toArray(Decoder[]::new),
ordinal -> topLevelOrdinalMap.getOrDefault(ordinal, -1));
}

Expand Down Expand Up @@ -227,7 +229,7 @@ private static Decoder createDecoder(Type type, OpenXJsonOptions options, List<D
rowType.getFields().stream()
.map(Field::getType)
.map(fieldType -> createDecoder(fieldType, options, timestampFormatters))
.collect(toImmutableList()),
.toArray(Decoder[]::new),
ordinal -> ordinal < rowType.getFields().size() ? ordinal : -1);
}
throw new UnsupportedOperationException("Unsupported column type: " + type);
Expand Down Expand Up @@ -753,19 +755,21 @@ private Block readKeys(Collection<?> fieldNames)
private static class RowDecoder
extends Decoder
{
private final List<FieldName> fieldNames;
private final List<Decoder> fieldDecoders;
private final FieldName[] fieldNames;
private final Decoder[] fieldDecoders;
private final boolean dotsInKeyNames;
private final IntUnaryOperator ordinalToFieldPosition;

public RowDecoder(RowType rowType, OpenXJsonOptions options, List<Decoder> fieldDecoders, IntUnaryOperator ordinalToFieldPosition)
public RowDecoder(RowType rowType, OpenXJsonOptions options, Decoder[] fieldDecoders, IntUnaryOperator ordinalToFieldPosition)
{
this.fieldNames = rowType.getFields().stream()
.map(field -> field.getName().orElseThrow())
.map(fieldName -> fieldName.toLowerCase(Locale.ROOT))
.map(originalValue -> new FieldName(originalValue, options))
.collect(toImmutableList());
this.fieldDecoders = fieldDecoders;
.toArray(FieldName[]::new);
this.fieldDecoders = requireNonNull(fieldDecoders, "fieldDecoders is null");
checkArgument(this.fieldDecoders.length == fieldNames.length, "fieldDecoders length mismatch: %s <> %s", this.fieldDecoders.length, fieldNames.length);
checkArgument(Arrays.stream(this.fieldDecoders).noneMatch(Objects::isNull), "fieldDecoders contains null element");
this.dotsInKeyNames = options.isDotsInFieldNames();
this.ordinalToFieldPosition = requireNonNull(ordinalToFieldPosition, "ordinalToFieldPosition is null");
}
Expand Down Expand Up @@ -805,16 +809,16 @@ private void decodeValueFromString(JsonString jsonString, IntFunction<BlockBuild
throw invalidJson("Primitive can not be coerced to a ROW");
}

for (int i = 0; i < fieldDecoders.size(); i++) {
for (int i = 0; i < fieldDecoders.length; i++) {
BlockBuilder blockBuilder = fieldBuilders.apply(i);
blockBuilder.appendNull();
}
}

private void decodeValueFromMap(Map<?, ?> jsonObject, IntFunction<BlockBuilder> fieldBuilders)
{
for (int i = 0; i < fieldDecoders.size(); i++) {
FieldName fieldName = fieldNames.get(i);
for (int i = 0; i < fieldDecoders.length; i++) {
FieldName fieldName = fieldNames[i];
Object fieldValue = null;
if (jsonObject.containsKey(fieldName)) {
fieldValue = jsonObject.get(fieldName);
Expand All @@ -834,14 +838,14 @@ else if (dotsInKeyNames) {
blockBuilder.appendNull();
}
else {
fieldDecoders.get(i).decode(fieldValue, blockBuilder);
fieldDecoders[i].decode(fieldValue, blockBuilder);
}
}
}

private void decodeValueFromList(List<?> jsonArray, IntFunction<BlockBuilder> fieldBuilders)
{
boolean[] fieldWritten = new boolean[fieldDecoders.size()];
boolean[] fieldWritten = new boolean[fieldDecoders.length];
for (int ordinal = 0; ordinal < jsonArray.size(); ordinal++) {
int fieldPosition = ordinalToFieldPosition.applyAsInt(ordinal);
if (fieldPosition < 0) {
Expand All @@ -854,7 +858,7 @@ private void decodeValueFromList(List<?> jsonArray, IntFunction<BlockBuilder> fi
blockBuilder.appendNull();
}
else {
fieldDecoders.get(fieldPosition).decode(fieldValue, blockBuilder);
fieldDecoders[fieldPosition].decode(fieldValue, blockBuilder);
}
fieldWritten[fieldPosition] = true;
}
Expand Down