-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[fix](serde) match STRUCT sub-fields by name when loading JSON #64011
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
csun5285
wants to merge
1
commit into
apache:master
Choose a base branch
from
csun5285:fix/OPENSOURCE-374-struct-field-align
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.
+263
−32
Open
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions
7
regression-test/data/datatype_p0/complex_types/test_struct_field_align_ordered.json
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 |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| [ | ||
| {"id": 1, "c_struct": {"f1": 10, "f2": 3.14, "f3": "Emily"}}, | ||
| {"id": 2, "c_struct": {"f1": 4, "f2": 1.5, "f3": null}}, | ||
| {"id": 3, "c_struct": {"f1": 7, "f2": null, "f3": "Benjamin"}}, | ||
| {"id": 4, "c_struct": {}}, | ||
| {"id": 5, "c_struct": null} | ||
| ] |
7 changes: 7 additions & 0 deletions
7
regression-test/data/datatype_p0/complex_types/test_struct_field_align_swapped.json
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 |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| [ | ||
| {"id": 20, "c_struct": {"f2": 1.5, "f1": 4, "f3": null}}, | ||
| {"id": 21, "c_struct": {"f3": "Tom", "f2": 2.5, "f1": 9}}, | ||
| {"id": 22, "c_struct": {"f2": 8.5, "f1": 1}}, | ||
| {"id": 23, "c_struct": {"f1": 7, "f2": 3.5, "f3": "Z", "f4": 999}}, | ||
| {"id": 24, "c_struct": {"F2": 6.5, "F1": 5, "F3": "U"}} | ||
| ] |
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
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 |
|---|---|---|
|
|
@@ -42,5 +42,5 @@ | |
| {"f1":1, "f2":"2022-10-10 00:00:00"} | ||
|
|
||
| -- !sql15 -- | ||
| \N | ||
| {"a":1, "b":1} | ||
|
|
||
91 changes: 91 additions & 0 deletions
91
regression-test/suites/datatype_p0/complex_types/test_struct_field_align.groovy
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 |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you 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. | ||
|
|
||
| // Regression test for OPENSOURCE-374: when loading JSON into a STRUCT column, sub-fields must | ||
| // be matched by field name. Out-of-order JSON keys in Stream Load used to turn the whole struct | ||
| // column into NULL; a missing field is now filled with NULL and an unknown field is ignored | ||
| // (consistent with PostgreSQL / Spark / Trino). Struct-to-struct CAST stays positional, which | ||
| // also matches those engines. | ||
| suite("test_struct_field_align") { | ||
| def tableName = "test_struct_field_align" | ||
|
|
||
| sql "DROP TABLE IF EXISTS ${tableName}" | ||
| sql """ | ||
| CREATE TABLE ${tableName} ( | ||
| id INT NOT NULL, | ||
| c_struct STRUCT<f1:INT,f2:FLOAT,f3:STRING> NULL | ||
| ) | ||
| UNIQUE KEY(id) | ||
| DISTRIBUTED BY HASH(id) BUCKETS 1 | ||
| PROPERTIES ("replication_allocation" = "tag.location.default: 1") | ||
| """ | ||
|
|
||
| def doStreamLoad = { fileName -> | ||
| streamLoad { | ||
| table tableName | ||
| set 'format', 'json' | ||
| set 'strip_outer_array', 'true' | ||
| set 'columns', 'id, c_struct' | ||
| file fileName | ||
| time 10000 | ||
| check { result, exception, startTime, endTime -> | ||
| if (exception != null) { | ||
| throw exception | ||
| } | ||
| def json = parseJson(result) | ||
| assertEquals("success", json.Status.toLowerCase()) | ||
| assertEquals(0, json.NumberFilteredRows) | ||
| assertTrue(json.NumberLoadedRows > 0) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // 1) ordered keys (baseline) | ||
| doStreamLoad "test_struct_field_align_ordered.json" | ||
| // 2) keys whose order differs from the DDL (the core bug) and a row that omits f3 | ||
| doStreamLoad "test_struct_field_align_swapped.json" | ||
|
|
||
| sql "sync" | ||
|
|
||
| def rows = sql "SELECT id, c_struct FROM ${tableName} ORDER BY id" | ||
| def actual = [:] | ||
| for (row in rows) { | ||
| actual[row[0] as int] = (row[1] == null ? "NULL" : row[1].toString()) | ||
| } | ||
|
|
||
| def expected = [ | ||
| 1 : '{"f1":10, "f2":3.14, "f3":"Emily"}', | ||
| 2 : '{"f1":4, "f2":1.5, "f3":null}', | ||
| 3 : '{"f1":7, "f2":null, "f3":"Benjamin"}', | ||
| 4 : '{"f1":null, "f2":null, "f3":null}', | ||
| 5 : 'NULL', | ||
| // swapped JSON keys must align by name instead of producing NULL | ||
| 20 : '{"f1":4, "f2":1.5, "f3":null}', | ||
| 21 : '{"f1":9, "f2":2.5, "f3":"Tom"}', | ||
| // f3 omitted -> filled with NULL | ||
| 22 : '{"f1":1, "f2":8.5, "f3":null}', | ||
| // an unknown field (f4) is ignored, the matched fields still align by name | ||
| 23 : '{"f1":7, "f2":3.5, "f3":"Z"}', | ||
| // upper-case JSON keys are matched case-insensitively to the lower-cased schema names | ||
| 24 : '{"f1":5, "f2":6.5, "f3":"U"}', | ||
| ] | ||
|
|
||
| assertEquals(expected.size(), actual.size()) | ||
| for (e in expected) { | ||
| assertEquals(e.value, actual[e.key], "row id=${e.key} mismatch".toString()) | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.