-
Notifications
You must be signed in to change notification settings - Fork 14k
[FLINK-40225][table] Reject trailing tokens in JSON functions #28815
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
Open
VasShabu
wants to merge
4
commits into
apache:master
Choose a base branch
from
VasShabu:jsonLeadingTokenBug
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6fd4f4d
[FLINK-40225][table] Fixed the Leading token bug + fixed invalid json…
VasShabu 52455a9
[FLINK-40225][table] Added tests to IS_JSON to ensure correct parseing
VasShabu cc3cc72
[Flink-40225][table] fixed/added more testcases
VasShabu 987f8b4
[FLINK-40225][table] Fix CI test cases issues/InvalidJSON
VasShabu 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 hidden or 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 hidden or 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 | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -101,8 +101,8 @@ Stream<TestSetSpec> getTestSetSpecs() { | |||||||
| private static TestSetSpec jsonExistsSpec() { | ||||||||
| final String jsonValue = getJsonFromResource("/json/json-exists.json"); | ||||||||
| return TestSetSpec.forFunction(BuiltInFunctionDefinitions.JSON_EXISTS) | ||||||||
| .onFieldsWithData(jsonValue) | ||||||||
| .andDataTypes(STRING()) | ||||||||
| .onFieldsWithData(jsonValue, "{\"a\":1} x") | ||||||||
| .andDataTypes(STRING(), STRING()) | ||||||||
|
|
||||||||
| // NULL | ||||||||
| .testResult( | ||||||||
|
|
@@ -175,14 +175,17 @@ private static TestSetSpec jsonExistsSpec() { | |||||||
| .testTableApiRuntimeError( | ||||||||
| $("f0").jsonExists("strict $.invalid", JsonExistsOnError.ERROR), | ||||||||
| TableRuntimeException.class, | ||||||||
| "No results for path: $['invalid']"); | ||||||||
| "No results for path: $['invalid']") | ||||||||
|
|
||||||||
| // Trailing garbage after a valid JSON value | ||||||||
| .testResult($("f1").jsonExists("$.a"), "JSON_EXISTS(f1, '$.a')", false, BOOLEAN()); | ||||||||
| } | ||||||||
|
|
||||||||
| private static TestSetSpec jsonValueSpec() { | ||||||||
| final String jsonValue = getJsonFromResource("/json/json-value.json"); | ||||||||
| return TestSetSpec.forFunction(BuiltInFunctionDefinitions.JSON_VALUE) | ||||||||
| .onFieldsWithData(jsonValue) | ||||||||
| .andDataTypes(STRING()) | ||||||||
| .onFieldsWithData(jsonValue, "{\"a\":1} x") | ||||||||
| .andDataTypes(STRING(), STRING()) | ||||||||
|
|
||||||||
| // NULL and invalid types | ||||||||
| .testResult( | ||||||||
|
|
@@ -295,6 +298,9 @@ private static TestSetSpec jsonValueSpec() { | |||||||
| "right", | ||||||||
| STRING()) | ||||||||
|
|
||||||||
| // Trailing garbage after a valid JSON value | ||||||||
| .testResult($("f1").jsonValue("$.a"), "JSON_VALUE(f1, '$.a')", null, STRING()) | ||||||||
|
|
||||||||
| // Multiple JSON_VALUE calls on the same input should reuse parsed JSON | ||||||||
| .testSqlResult( | ||||||||
| "JSON_VALUE(f0, '$.type'), JSON_VALUE(f0, '$.age')", | ||||||||
|
|
@@ -407,7 +413,12 @@ private static List<TestSetSpec> isJsonSpec() { | |||||||
| $("f0").isJson(JsonType.OBJECT), | ||||||||
| "f0 IS JSON OBJECT", | ||||||||
| true, | ||||||||
| BOOLEAN())); | ||||||||
| BOOLEAN()), | ||||||||
| // Trailing garbage after a valid JSON value | ||||||||
| TestSetSpec.forFunction(BuiltInFunctionDefinitions.IS_JSON) | ||||||||
|
Contributor
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. You have multiple TestSetSpec, but you could group them under one single TestSetSpec as the tests above do
Suggested change
Author
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. sounds good |
||||||||
| .onFieldsWithData("{\"a\":1} x") | ||||||||
| .andDataTypes(STRING()) | ||||||||
| .testResult($("f0").isJson(), "f0 IS JSON", false, BOOLEAN())); | ||||||||
| } | ||||||||
|
|
||||||||
| private static List<TestSetSpec> jsonQuerySpec() { | ||||||||
|
|
@@ -632,7 +643,14 @@ private static List<TestSetSpec> jsonQuerySpec() { | |||||||
| .testTableApiRuntimeError( | ||||||||
| $("f0").jsonQuery("strict $.err10", WITHOUT_ARRAY, NULL, ERROR), | ||||||||
| TableRuntimeException.class, | ||||||||
| "No results for path")); | ||||||||
| "No results for path"), | ||||||||
| // Trailing garbage after a valid JSON value. The path must select an object, | ||||||||
| // because a scalar would return NULL even if the trailing garbage was accepted. | ||||||||
| TestSetSpec.forFunction(BuiltInFunctionDefinitions.JSON_QUERY) | ||||||||
| .onFieldsWithData("{\"a\":{\"b\":1}} x") | ||||||||
| .andDataTypes(STRING()) | ||||||||
| .testResult( | ||||||||
| $("f0").jsonQuery("$.a"), "JSON_QUERY(f0, '$.a')", null, STRING())); | ||||||||
| } | ||||||||
|
|
||||||||
| private static List<TestSetSpec> jsonStringSpec() { | ||||||||
|
|
@@ -758,7 +776,17 @@ private static List<TestSetSpec> jsonStringSpec() { | |||||||
| jsonString($("f0")), | ||||||||
| "JSON_STRING(f0)", | ||||||||
| "{\"field\\ttab\":\"val4\",\"field\\nline\":\"val3\",\"field\\rreturn\":\"val5\",\"field\\\"quote\":\"val1\",\"field\\\\slash\":\"val2\"}", | ||||||||
| STRING().notNull())); | ||||||||
| STRING().notNull()), | ||||||||
| // Trailing garbage after a valid JSON value. JSON_STRING serializes its argument | ||||||||
| // instead of parsing it, so the garbage is kept as part of the string. | ||||||||
| TestSetSpec.forFunction(BuiltInFunctionDefinitions.JSON_STRING) | ||||||||
| .onFieldsWithData("{\"a\":1} x") | ||||||||
| .andDataTypes(STRING()) | ||||||||
| .testResult( | ||||||||
| jsonString($("f0")), | ||||||||
| "JSON_STRING(f0)", | ||||||||
| "\"{\\\"a\\\":1} x\"", | ||||||||
| STRING())); | ||||||||
| } | ||||||||
|
|
||||||||
| private static List<TestSetSpec> parseJsonSpec() { | ||||||||
|
|
@@ -1184,7 +1212,19 @@ private static List<TestSetSpec> jsonObjectSpec() { | |||||||
| jsonObject(JsonOnNull.NULL, "testRow", $("f0")), | ||||||||
| "JSON_OBJECT(KEY 'testRow' VALUE f0 NULL ON NULL)", | ||||||||
| "{\"testRow\":{\"field\\ttab\":\"val4\",\"field\\nline\":\"val3\",\"field\\rreturn\":\"val5\",\"field\\\"quote\":\"val1\",\"field\\\\slash\":\"val2\"}}", | ||||||||
| STRING().notNull())); | ||||||||
| STRING().notNull()), | ||||||||
| // Trailing garbage after a valid JSON value | ||||||||
| TestSetSpec.forFunction(BuiltInFunctionDefinitions.JSON_OBJECT) | ||||||||
| .onFieldsWithData("{\"a\":1} x") | ||||||||
| .andDataTypes(STRING()) | ||||||||
| .testSqlRuntimeError( | ||||||||
| "JSON_OBJECT(KEY 'K' VALUE JSON(f0))", | ||||||||
| TableRuntimeException.class, | ||||||||
| "Invalid JSON string in JSON(value) function") | ||||||||
| .testTableApiRuntimeError( | ||||||||
| jsonObject(JsonOnNull.NULL, "K", json($("f0"))), | ||||||||
| TableRuntimeException.class, | ||||||||
| "Invalid JSON string in JSON(value) function")); | ||||||||
| } | ||||||||
|
|
||||||||
| private static List<TestSetSpec> jsonQuoteSpec() { | ||||||||
|
|
@@ -1256,7 +1296,17 @@ private static List<TestSetSpec> jsonQuoteSpec() { | |||||||
| "\"\\u2260 will be escaped\"", | ||||||||
| STRING().notNull()) | ||||||||
| .testResult( | ||||||||
| $("f8").jsonQuote(), "JSON_QUOTE(f8)", null, STRING().nullable())); | ||||||||
| $("f8").jsonQuote(), "JSON_QUOTE(f8)", null, STRING().nullable()), | ||||||||
| // Trailing garbage after a valid JSON value. JSON_QUOTE only escapes its input, so | ||||||||
| // the garbage is preserved. | ||||||||
| TestSetSpec.forFunction(BuiltInFunctionDefinitions.JSON_QUOTE) | ||||||||
| .onFieldsWithData("{\"a\":1} x") | ||||||||
| .andDataTypes(STRING()) | ||||||||
| .testResult( | ||||||||
| $("f0").jsonQuote(), | ||||||||
| "JSON_QUOTE(f0)", | ||||||||
| "\"{\\\"a\\\":1} x\"", | ||||||||
| STRING())); | ||||||||
| } | ||||||||
|
|
||||||||
| private static List<TestSetSpec> jsonUnquoteSpecWithValidInput() { | ||||||||
|
|
@@ -1272,13 +1322,13 @@ private static List<TestSetSpec> jsonUnquoteSpecWithValidInput() { | |||||||
| TestSetSpec.forFunction(BuiltInFunctionDefinitions.JSON_UNQUOTE) | ||||||||
| .onFieldsWithData( | ||||||||
| "\"abc\"", | ||||||||
| "\"[\"abc\"]\"", | ||||||||
| "\"[\"\\u0041\"]\"", | ||||||||
| "\"[\\\"abc\\\"]\"", | ||||||||
| "\"[\\\"\\u0041\\\"]\"", | ||||||||
| "\"\\u0041\"", | ||||||||
| "\"[\"\\t\\u0032\"]\"", | ||||||||
| "\"[\"This is a \\t test \\n with special characters: \\b \\f \\r \\u0041\"]\"", | ||||||||
| "\"\"\"", | ||||||||
| "\"\"\ufffa\"", | ||||||||
| "\"[\\\"\\t\\u0032\\\"]\"", | ||||||||
| "\"[\\\"This is a \\t test \\n with special characters: \\b \\f \\r \\u0041\\\"]\"", | ||||||||
| "\"\\\"\"", | ||||||||
| "\"\\\"\ufffa\"", | ||||||||
| "\"a unicode \u2260\"", | ||||||||
| "\"valid unicode literal \\uD801\\uDC00\"", | ||||||||
| "[1,2,3]", | ||||||||
|
|
@@ -1478,7 +1528,17 @@ private static List<TestSetSpec> jsonUnquoteSpecWithInvalidInput() { | |||||||
| $("f10").jsonQuote(), | ||||||||
| "JSON_UNQUOTE(f10)", | ||||||||
| null, | ||||||||
| STRING().nullable())); | ||||||||
| STRING().nullable()), | ||||||||
| // Trailing garbage after a valid JSON value. The leading '"abc"' must not be | ||||||||
| // unquoted on its own, the input is passed through unchanged. | ||||||||
| TestSetSpec.forFunction(BuiltInFunctionDefinitions.JSON_UNQUOTE) | ||||||||
| .onFieldsWithData("\"abc\" \"def\"") | ||||||||
| .andDataTypes(STRING()) | ||||||||
| .testResult( | ||||||||
| $("f0").jsonUnquote(), | ||||||||
| "JSON_UNQUOTE(f0)", | ||||||||
| "\"abc\" \"def\"", | ||||||||
| STRING())); | ||||||||
| } | ||||||||
|
|
||||||||
| private static List<TestSetSpec> jsonArraySpec() { | ||||||||
|
|
@@ -1621,7 +1681,19 @@ private static List<TestSetSpec> jsonArraySpec() { | |||||||
| jsonArray(JsonOnNull.NULL, $("f0")), | ||||||||
| "JSON_ARRAY(f0 NULL ON NULL)", | ||||||||
| "[{\"field\\ttab\":\"val4\",\"field\\nline\":\"val3\",\"field\\rreturn\":\"val5\",\"field\\\"quote\":\"val1\",\"field\\\\slash\":\"val2\"}]", | ||||||||
| STRING().notNull())); | ||||||||
| STRING().notNull()), | ||||||||
| // Trailing garbage after a valid JSON value | ||||||||
| TestSetSpec.forFunction(BuiltInFunctionDefinitions.JSON_ARRAY) | ||||||||
| .onFieldsWithData("{\"a\":1} x") | ||||||||
| .andDataTypes(STRING()) | ||||||||
| .testSqlRuntimeError( | ||||||||
| "JSON_ARRAY(JSON(f0))", | ||||||||
| TableRuntimeException.class, | ||||||||
| "Invalid JSON string in JSON(value) function") | ||||||||
| .testTableApiRuntimeError( | ||||||||
| jsonArray(JsonOnNull.NULL, json($("f0"))), | ||||||||
| TableRuntimeException.class, | ||||||||
| "Invalid JSON string in JSON(value) function")); | ||||||||
| } | ||||||||
|
|
||||||||
| /** Pins the local-ref / common-sub-expression handling for JSON construction calls. */ | ||||||||
|
|
||||||||
This file contains hidden or 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 hidden or 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
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.
Since the same mapper now affects every JSON function, could we add a trailing-garbage case for JSON_VALUE/JSON_QUERY/JSON_EXISTS as well (e.g. {"a":1} x)? Right now a future revert of the flag would only fail the IS JSON specs