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

Fix ignoring trailing JSON content in various places #12907

Merged
merged 6 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -22,6 +22,7 @@
import io.airlift.json.ObjectMapperProvider;

import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -78,6 +79,16 @@ public static <T> T parseJson(ObjectMapper mapper, byte[] jsonBytes, Class<T> ja
return parseJson(mapper, ObjectMapper::createParser, jsonBytes, javaType);
}

public static <T> T parseJson(InputStream inputStream, Class<T> javaType)
{
return parseJson(OBJECT_MAPPER, inputStream, javaType);
}

public static <T> T parseJson(ObjectMapper mapper, InputStream inputStream, Class<T> javaType)
{
return parseJson(mapper, ObjectMapper::createParser, inputStream, javaType);
}

private static <I, T> T parseJson(ObjectMapper mapper, ParserConstructor<I> parserConstructor, I input, Class<T> javaType)
{
requireNonNull(mapper, "mapper is null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import io.airlift.json.ObjectMapperProvider;
import io.airlift.log.Logger;
import io.trino.plugin.base.util.JsonUtils;
import io.trino.plugin.deltalake.DeltaLakeColumnHandle;
import io.trino.plugin.deltalake.transactionlog.checkpoint.LastCheckpoint;
import io.trino.spi.TrinoException;
Expand All @@ -35,7 +36,7 @@
import javax.annotation.Nullable;

import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDate;
Expand Down Expand Up @@ -136,7 +137,7 @@ public static DeltaLakeTransactionLogEntry parseJson(String json)
if (json.endsWith("x")) {
json = json.substring(0, json.length() - 1);
}
return OBJECT_MAPPER.readValue(json, DeltaLakeTransactionLogEntry.class);
return JsonUtils.parseJson(OBJECT_MAPPER, json, DeltaLakeTransactionLogEntry.class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: static import

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is within local parseJson method (name conflict)

}

private static Object parseDecimal(DecimalType type, String valueString)
Expand Down Expand Up @@ -230,18 +231,18 @@ static Optional<LastCheckpoint> readLastCheckpoint(FileSystem fileSystem, Path t
}

private static Optional<LastCheckpoint> tryReadLastCheckpoint(FileSystem fileSystem, Path tableLocation)
throws IOException
throws JsonParseException, JsonMappingException
{
Path transactionLogDirectory = getTransactionLogDir(tableLocation);
try (FSDataInputStream lastCheckpointInput = fileSystem.open(new Path(transactionLogDirectory, LAST_CHECKPOINT_FILENAME))) {
// Note: there apparently is 8K buffering applied and _last_checkpoint should be much smaller.
return Optional.of(OBJECT_MAPPER.readValue((InputStream) lastCheckpointInput, LastCheckpoint.class));
return Optional.of(JsonUtils.parseJson(OBJECT_MAPPER, lastCheckpointInput, LastCheckpoint.class));
}
catch (JsonParseException | JsonMappingException e) {
// The _last_checkpoint file is malformed, it's probably in the middle of a rewrite (file rewrites on Azure are NOT atomic)
throw e;
}
catch (IOException e) {
catch (IOException | UncheckedIOException e) {
// _last_checkpoint file was not found, we need to find latest checkpoint manually
// ideally, we'd detect the condition by catching FileNotFoundException, but some file system implementations
// will throw different exceptions if the checkpoint is not found
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import static com.google.common.base.MoreObjects.toStringHelper;
import static io.airlift.slice.SizeOf.estimatedSizeOf;
import static io.trino.plugin.base.util.JsonUtils.parseJson;
import static io.trino.plugin.deltalake.transactionlog.TransactionLogAccess.toCanonicalNameKeyedMap;
import static io.trino.plugin.deltalake.transactionlog.TransactionLogParser.JSON_STATISTICS_TIMESTAMP_FORMATTER;
import static io.trino.plugin.deltalake.transactionlog.TransactionLogParser.START_OF_MODERN_ERA;
Expand All @@ -62,7 +63,7 @@ public class DeltaLakeJsonFileStatistics
public static DeltaLakeJsonFileStatistics create(String jsonStatistics)
throws JsonProcessingException
{
return OBJECT_MAPPER.readValue(jsonStatistics, DeltaLakeJsonFileStatistics.class);
return parseJson(OBJECT_MAPPER, jsonStatistics, DeltaLakeJsonFileStatistics.class);
}

@JsonCreator
Expand Down