-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6606231
Refactor JsonUtils
findepi 5fe47f8
Fail on trailing content in Accumulo object metadata JSON
findepi 7fbffe6
Fix ignoring trailing content in client's JsonCodec
findepi 23b350f
Fail on trailing content in Delta Lake metadata JSON
findepi a4ebed3
Fail on trailing content in Kudu table properties JSON
findepi 5f5b914
Fail on trailing content in TPC-DS / TPC-H stats resource JSON
findepi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
client/trino-client/src/test/java/io/trino/client/TestJsonCodec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* 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.client; | ||
|
||
import com.fasterxml.jackson.core.JsonParseException; | ||
import org.testng.annotations.Test; | ||
|
||
import java.io.ByteArrayInputStream; | ||
|
||
import static io.trino.client.JsonCodec.jsonCodec; | ||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.testng.Assert.assertEquals; | ||
|
||
public class TestJsonCodec | ||
{ | ||
@Test | ||
public void testTrailingContent() | ||
throws Exception | ||
{ | ||
JsonCodec<ClientTypeSignature> codec = jsonCodec(ClientTypeSignature.class); | ||
|
||
String json = "{\"rawType\":\"bigint\",\"arguments\":[]}"; | ||
assertEquals(codec.fromJson(json).getRawType(), "bigint"); | ||
|
||
String jsonWithTrailingContent = json + " trailer"; | ||
assertThatThrownBy(() -> codec.fromJson(jsonWithTrailingContent)) | ||
.isInstanceOf(JsonParseException.class) | ||
.hasMessageStartingWith("Unrecognized token 'trailer': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')"); | ||
|
||
assertThatThrownBy(() -> codec.fromJson(new ByteArrayInputStream(jsonWithTrailingContent.getBytes(UTF_8)))) | ||
.isInstanceOf(JsonParseException.class) | ||
.hasMessageStartingWith("Unrecognized token 'trailer': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')"); | ||
|
||
String jsonWithTrailingJsonContent = json + " \"valid json value\""; | ||
assertThatThrownBy(() -> codec.fromJson(jsonWithTrailingJsonContent)) | ||
.isInstanceOf(IllegalArgumentException.class) | ||
.hasMessage("Found characters after the expected end of input"); | ||
|
||
assertThatThrownBy(() -> codec.fromJson(new ByteArrayInputStream(jsonWithTrailingJsonContent.getBytes(UTF_8)))) | ||
.isInstanceOf(IllegalArgumentException.class) | ||
.hasMessage("Found characters after the expected end of input"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: static import There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is within local |
||
} | ||
|
||
private static Object parseDecimal(DecimalType type, String valueString) | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar change in Airlift: airlift/airlift#995