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
1 change: 1 addition & 0 deletions docs/src/main/sphinx/connector/hive.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ In the case of serializable formats, only specific
more [details about the Trino implementation in the source repository](https://github.com/trinodb/trino/tree/master/lib/trino-hive-formats/src/main/java/io/trino/hive/formats/line/openxjson/README.md).
- TextFile
- ESRI - using `com.esri.hadoop.hive.serde.EsriJsonSerDe`
- ESRI_GEO_JSON - using `com.esri.hadoop.hive.serde.GeoJsonSerDe`

(hive-configuration)=
## General configuration
Expand Down
5 changes: 5 additions & 0 deletions lib/trino-hive-formats/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@
<artifactId>jts-core</artifactId>
</dependency>

<dependency>
<groupId>org.locationtech.jts.io</groupId>
<artifactId>jts-io-common</artifactId>
</dependency>

<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public final class HiveClassNames
public static final String TEXT_INPUT_FORMAT_CLASS = "org.apache.hadoop.mapred.TextInputFormat";
public static final String ESRI_SERDE_CLASS = "com.esri.hadoop.hive.serde.EsriJsonSerDe";
public static final String ESRI_INPUT_FORMAT_CLASS = "com.esri.json.hadoop.EnclosedEsriJsonInputFormat";
public static final String ESRI_GEO_JSON_SERDE_CLASS = "com.esri.hadoop.hive.serde.GeoJsonSerDe";
Comment thread
gertjanal marked this conversation as resolved.
public static final String ESRI_GEO_JSON_INPUT_FORMAT_CLASS = "com.esri.json.hadoop.EnclosedGeoJsonInputFormat";
public static final String TWITTER_ELEPHANTBIRD_PROTOBUF_SERDE_CLASS = "com.twitter.elephantbird.hive.serde.ProtobufDeserializer";

private HiveClassNames() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import io.airlift.slice.Slices;
import io.trino.geospatial.serde.JtsGeometrySerde;
import io.trino.hive.formats.line.Column;
import io.trino.plugin.base.type.DecodedTimestamp;
import io.trino.spi.PageBuilder;
import io.trino.spi.TrinoException;
import io.trino.spi.block.BlockBuilder;
import io.trino.spi.type.CharType;
import io.trino.spi.type.DecimalConversions;
Expand All @@ -32,6 +34,8 @@
import io.trino.spi.type.VarcharType;
import org.joda.time.DateTimeZone;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.geojson.GeoJsonReader;

import java.io.IOException;
import java.math.BigDecimal;
Expand All @@ -53,7 +57,9 @@
import static com.fasterxml.jackson.core.JsonToken.VALUE_NULL;
import static com.fasterxml.jackson.core.JsonToken.VALUE_NUMBER_INT;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static io.trino.hive.formats.esri.EsriDeserializer.Format.GEO_JSON;
import static io.trino.plugin.base.type.TrinoTimestampEncoderFactory.createTimestampEncoder;
import static io.trino.spi.StandardErrorCode.JSON_INPUT_CONVERSION_ERROR;
import static io.trino.spi.type.BigintType.BIGINT;
import static io.trino.spi.type.BooleanType.BOOLEAN;
import static io.trino.spi.type.Chars.truncateToLengthAndTrimSpaces;
Expand Down Expand Up @@ -82,6 +88,7 @@ public final class EsriDeserializer
{
private static final String GEOMETRY_FIELD_NAME = "geometry";
private static final String ATTRIBUTES_FIELD_NAME = "attributes";
private static final String PROPERTIES_FIELD_NAME = "properties";
private static final DateTimeFormatter DATE_FORMATTER =
DateTimeFormatter.ofPattern("yyyy-M-d").withZone(UTC);
private static final List<DateTimeFormatter> TIMESTAMP_FORMATTERS = List.of(
Expand All @@ -95,14 +102,28 @@ public final class EsriDeserializer
private final Map<String, Integer> columnIndex;
private final List<Type> types;
private final boolean[] fieldWritten;
private final Format format;
private GeoJsonReader reader;
private ObjectMapper mapper;

public EsriDeserializer(List<Column> columns)
public enum Format {
ESRI,
GEO_JSON
}

public EsriDeserializer(List<Column> columns, Format format)
{
this.columns = ImmutableList.copyOf(requireNonNull(columns, "columns is null"));
this.types = columns.stream()
.map(Column::type)
.collect(toImmutableList());
this.fieldWritten = new boolean[columns.size()];
this.format = format;

if (format == GEO_JSON) {
reader = new GeoJsonReader();
mapper = new ObjectMapper();
}

for (Column column : columns) {
validateSupportedType(column.type(), column.name());
Expand Down Expand Up @@ -138,6 +159,11 @@ public void deserialize(PageBuilder pageBuilder, JsonParser parser)
throw invalidJson("start of object expected");
}

String attributesFieldName = switch (format) {
case ESRI -> ATTRIBUTES_FIELD_NAME;
case GEO_JSON -> PROPERTIES_FIELD_NAME;
};

Arrays.fill(fieldWritten, false);
while (nextObjectField(parser)) {
String fieldName = parser.currentName();
Expand All @@ -147,7 +173,7 @@ public void deserialize(PageBuilder pageBuilder, JsonParser parser)
if (GEOMETRY_FIELD_NAME.equals(fieldName)) {
parseGeometry(parser, pageBuilder);
}
else if (ATTRIBUTES_FIELD_NAME.equals(fieldName)) {
else if (attributesFieldName.equals(fieldName)) {
parseAttributes(parser, pageBuilder);
}
else {
Expand Down Expand Up @@ -188,7 +214,19 @@ private void parseGeometry(JsonParser parser, PageBuilder pageBuilder)
return;
}

Geometry geometry = EsriJsonParser.parseGeometry(parser);
Geometry geometry = switch (format) {
case ESRI -> EsriJsonParser.parseGeometry(parser);
case GEO_JSON -> {
String json = mapper.writeValueAsString(mapper.readTree(parser));
try {
yield reader.read(json);
}
catch (ParseException e) {
throw new TrinoException(JSON_INPUT_CONVERSION_ERROR, e);
}
}
};

if (geometry == null) {
throw new IllegalArgumentException("Could not parse geometry");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.List;

import static com.fasterxml.jackson.core.JsonToken.START_OBJECT;
import static io.trino.hive.formats.esri.EsriDeserializer.Format.ESRI;
import static io.trino.plugin.base.util.JsonUtils.jsonFactory;
import static io.trino.spi.type.BigintType.BIGINT;
import static io.trino.spi.type.BooleanType.BOOLEAN;
Expand Down Expand Up @@ -812,20 +813,20 @@ private static Page parse(String json, List<Column> columns)
JsonParser jsonParser = JSON_FACTORY.createParser(json);
assertThat(jsonParser.nextToken()).isEqualTo(START_OBJECT);

EsriDeserializer deserializer = new EsriDeserializer(columns);
EsriDeserializer deserializer = new EsriDeserializer(columns, ESRI);
PageBuilder pageBuilder = new PageBuilder(deserializer.getTypes());
deserializer.deserialize(pageBuilder, jsonParser);
Page page = pageBuilder.build();
assertThat(page.getPositionCount()).isEqualTo(1);
return page;
}

private static void assertGeometry(Page page, String expectedWkt)
static void assertGeometry(Page page, String expectedWkt)
{
assertGeometry(page, expectedWkt, 0);
}

private static void assertGeometry(Page page, String expectedWkt, int expectedSrid)
static void assertGeometry(Page page, String expectedWkt, int expectedSrid)
{
if (expectedWkt == null) {
assertThat(page.getBlock(6).isNull(0)).isTrue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.IOException;
import java.util.List;

import static io.trino.hive.formats.esri.EsriDeserializer.Format.ESRI;
import static io.trino.spi.type.BigintType.BIGINT;
import static io.trino.spi.type.VarbinaryType.VARBINARY;
import static io.trino.spi.type.VarcharType.VARCHAR;
Expand Down Expand Up @@ -112,7 +113,7 @@ public void testNumberFeaturesFails()
}
""";

assertThatThrownBy(() -> new EsriReader(new ByteArrayInputStream(json.getBytes(UTF_8)), new EsriDeserializer(TEST_COLUMNS)))
assertThatThrownBy(() -> new EsriReader(new ByteArrayInputStream(json.getBytes(UTF_8)), new EsriDeserializer(TEST_COLUMNS, ESRI)))
.isInstanceOf(IOException.class)
.hasMessage("Invalid JSON: Features field must be an array");
}
Expand All @@ -127,7 +128,7 @@ public void testObjectFeaturesFails()
}
""";

assertThatThrownBy(() -> new EsriReader(new ByteArrayInputStream(json.getBytes(UTF_8)), new EsriDeserializer(TEST_COLUMNS)))
assertThatThrownBy(() -> new EsriReader(new ByteArrayInputStream(json.getBytes(UTF_8)), new EsriDeserializer(TEST_COLUMNS, ESRI)))
.isInstanceOf(IOException.class)
.hasMessage("Invalid JSON: Features field must be an array");
}
Expand Down Expand Up @@ -173,7 +174,7 @@ public void testLargeRead()
}
jsonBuilder.append("]}");

EsriDeserializer deserializer = new EsriDeserializer(TEST_COLUMNS);
EsriDeserializer deserializer = new EsriDeserializer(TEST_COLUMNS, ESRI);
PageBuilder pageBuilder = new PageBuilder(deserializer.getTypes());

try (EsriReader reader = new EsriReader(new ByteArrayInputStream(jsonBuilder.toString().getBytes(UTF_8)), deserializer)) {
Expand Down Expand Up @@ -268,7 +269,7 @@ public void testNestedFeaturesIgnored()
private static Page readAll(String json)
throws IOException
{
EsriDeserializer deserializer = new EsriDeserializer(TEST_COLUMNS);
EsriDeserializer deserializer = new EsriDeserializer(TEST_COLUMNS, ESRI);
EsriReader reader = new EsriReader(new ByteArrayInputStream(json.getBytes(UTF_8)), deserializer);
PageBuilder pageBuilder = new PageBuilder(deserializer.getTypes());
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.trino.hive.formats.esri;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.google.common.collect.ImmutableList;
import io.trino.hive.formats.line.Column;
import io.trino.spi.Page;
import io.trino.spi.PageBuilder;
import io.trino.spi.block.Block;
import io.trino.spi.type.CharType;
import io.trino.spi.type.DecimalType;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.List;

import static com.fasterxml.jackson.core.JsonToken.START_OBJECT;
import static io.trino.hive.formats.esri.EsriDeserializer.Format.GEO_JSON;
import static io.trino.hive.formats.esri.TestGeoJsonReader.assertGeometry;
import static io.trino.plugin.base.util.JsonUtils.jsonFactory;
import static io.trino.spi.type.BigintType.BIGINT;
import static io.trino.spi.type.BooleanType.BOOLEAN;
import static io.trino.spi.type.DateType.DATE;
import static io.trino.spi.type.DoubleType.DOUBLE;
import static io.trino.spi.type.IntegerType.INTEGER;
import static io.trino.spi.type.RealType.REAL;
import static io.trino.spi.type.SmallintType.SMALLINT;
import static io.trino.spi.type.TimestampType.TIMESTAMP_MILLIS;
import static io.trino.spi.type.TinyintType.TINYINT;
import static io.trino.spi.type.VarbinaryType.VARBINARY;
import static io.trino.spi.type.VarcharType.VARCHAR;
import static org.assertj.core.api.Assertions.assertThat;

public class TestGeoJsonDeserializer
{
private static final JsonFactory JSON_FACTORY = jsonFactory();
private static final List<Column> COLUMNS = ImmutableList.of(
new Column("id", BIGINT, 0),
new Column("name", VARCHAR, 1),
new Column("active", BOOLEAN, 2),
new Column("value", DOUBLE, 3),
new Column("date", DATE, 4),
new Column("timestamp", TIMESTAMP_MILLIS, 5),
new Column("geometry", VARBINARY, 6),
new Column("count", INTEGER, 7),
new Column("price", DecimalType.createDecimalType(10, 2), 8),
new Column("small_num", SMALLINT, 9),
new Column("tiny_num", TINYINT, 10),
new Column("real_num", REAL, 11),
new Column("fixed_text", CharType.createCharType(10), 12));

@Test
public void testDeserializeSimpleFeature()
throws IOException
{
String json =
"""
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [13.0, 37.0]
},
"properties": {
"id": 1,
"name": "Test Feature",
"active": true,
"value": 123.45,
"date": 1741034025839,
"timestamp": 1741034025839,
"count": 42,
"price": "1234.56"
}
}
""";

Page page = parse(json);
assertThat(page.getPositionCount()).isEqualTo(1);

for (int i = 0; i < 9; i++) {
assertThat(page.getBlock(i).isNull(0))
.as("Column at index " + i + " should not be null")
.isFalse();
}
assertThat(BIGINT.getLong(page.getBlock(0), 0)).isEqualTo(1L);
assertThat(VARCHAR.getSlice(page.getBlock(1), 0).toStringUtf8()).isEqualTo("Test Feature");
assertThat(BOOLEAN.getBoolean(page.getBlock(2), 0)).isTrue();
assertThat(DOUBLE.getDouble(page.getBlock(3), 0)).isEqualTo(123.45);
assertThat(DATE.getLong(page.getBlock(4), 0)).isEqualTo(20150);
assertThat(TIMESTAMP_MILLIS.getLong(page.getBlock(5), 0)).isEqualTo(1741034025839000L);
assertGeometry(page, "POINT (13 37)", 4326, 6, 0);
assertThat(INTEGER.getLong(page.getBlock(7), 0)).isEqualTo(42);

DecimalType decimalType = DecimalType.createDecimalType(10, 2);
Block decimalBlock = page.getBlock(8);
assertThat(decimalType.getLong(decimalBlock, 0)).isEqualTo(123456L);
}

@Test
public void testDeserializeNullValues()
throws IOException
{
String json =
"""
{
"type": "Feature",
"properties": {
"id": null,
"name": null,
"active": null,
"value": null,
"date": null,
"timestamp": null,
"count": null,
"price": null
}
}
""";

Page page = parse(json);
for (int i = 0; i < 9; i++) {
assertThat(page.getBlock(i).isNull(0))
.as("Column at index " + i + " should be null but was " + page.getBlock(i).getClass().getSimpleName())
.isTrue();
}
}

private static Page parse(String json)
throws IOException
{
return parse(json, COLUMNS);
}

private static Page parse(String json, List<Column> columns)
throws IOException
{
JsonParser jsonParser = JSON_FACTORY.createParser(json);
assertThat(jsonParser.nextToken()).isEqualTo(START_OBJECT);

EsriDeserializer deserializer = new EsriDeserializer(columns, GEO_JSON);
PageBuilder pageBuilder = new PageBuilder(deserializer.getTypes());
deserializer.deserialize(pageBuilder, jsonParser);
Page page = pageBuilder.build();
assertThat(page.getPositionCount()).isEqualTo(1);
return page;
}
}
Loading