-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-21402][SQL] Fix java array of structs deserialization #22708
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
Closed
Closed
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
154 changes: 154 additions & 0 deletions
154
sql/core/src/test/java/test/org/apache/spark/sql/JavaBeanWithArraySuite.java
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,154 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| package test.org.apache.spark.sql; | ||
|
||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| import org.junit.After; | ||
| import org.junit.Assert; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| import org.apache.spark.sql.Dataset; | ||
| import org.apache.spark.sql.Encoder; | ||
| import org.apache.spark.sql.Encoders; | ||
| import org.apache.spark.sql.test.TestSparkSession; | ||
|
|
||
| public class JavaBeanWithArraySuite { | ||
|
|
||
| private static final List<Record> RECORDS = new ArrayList<>(); | ||
|
|
||
| static { | ||
| RECORDS.add(new Record(1, Arrays.asList(new Interval(111, 211), new Interval(121, 221)))); | ||
| RECORDS.add(new Record(2, Arrays.asList(new Interval(112, 212), new Interval(122, 222)))); | ||
| RECORDS.add(new Record(3, Arrays.asList(new Interval(113, 213), new Interval(123, 223)))); | ||
| } | ||
|
|
||
| private TestSparkSession spark; | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| spark = new TestSparkSession(); | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() { | ||
| spark.stop(); | ||
| spark = null; | ||
| } | ||
|
|
||
| @Test | ||
| public void testBeanWithArrayFieldDeserialization() { | ||
|
|
||
| Encoder<Record> encoder = Encoders.bean(Record.class); | ||
|
|
||
| Dataset<Record> dataset = spark | ||
| .read() | ||
| .format("json") | ||
| .schema("id int, intervals array<struct<startTime: bigint, endTime: bigint>>") | ||
| .load("src/test/resources/test-data/with-array-fields.json") | ||
| .as(encoder); | ||
|
|
||
| List<Record> records = dataset.collectAsList(); | ||
| Assert.assertEquals(records, RECORDS); | ||
| } | ||
|
|
||
| public static class Record { | ||
|
|
||
| private int id; | ||
| private List<Interval> intervals; | ||
|
|
||
| public Record() { } | ||
|
|
||
| Record(int id, List<Interval> intervals) { | ||
| this.id = id; | ||
| this.intervals = intervals; | ||
| } | ||
|
|
||
| public int getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public void setId(int id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| public List<Interval> getIntervals() { | ||
| return intervals; | ||
| } | ||
|
|
||
| public void setIntervals(List<Interval> intervals) { | ||
| this.intervals = intervals; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| if (!(obj instanceof Record)) return false; | ||
| Record other = (Record) obj; | ||
| return (other.id == this.id) && other.intervals.equals(this.intervals); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return String.format("{ id: %d, intervals: %s }", id, intervals); | ||
| } | ||
| } | ||
|
|
||
| public static class Interval { | ||
|
|
||
| private long startTime; | ||
| private long endTime; | ||
|
|
||
| public Interval() { } | ||
|
|
||
| Interval(long startTime, long endTime) { | ||
| this.startTime = startTime; | ||
| this.endTime = endTime; | ||
| } | ||
|
|
||
| public long getStartTime() { | ||
| return startTime; | ||
| } | ||
|
|
||
| public void setStartTime(long startTime) { | ||
| this.startTime = startTime; | ||
| } | ||
|
|
||
| public long getEndTime() { | ||
| return endTime; | ||
| } | ||
|
|
||
| public void setEndTime(long endTime) { | ||
| this.endTime = endTime; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| if (!(obj instanceof Interval)) return false; | ||
| Interval other = (Interval) obj; | ||
| return (other.startTime == this.startTime) && (other.endTime == this.endTime); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return String.format("[%d,%d]", startTime, endTime); | ||
| } | ||
| } | ||
| } | ||
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,3 @@ | ||
| { "id": 1, "intervals": [{ "startTime": 111, "endTime": 211 }, { "startTime": 121, "endTime": 221 }]} | ||
| { "id": 2, "intervals": [{ "startTime": 112, "endTime": 212 }, { "startTime": 122, "endTime": 222 }]} | ||
| { "id": 3, "intervals": [{ "startTime": 113, "endTime": 213 }, { "startTime": 123, "endTime": 223 }]} |
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.
can we exclude other changes except this one? This one is very easy to reason about. We did the same thing in
ScalaReflection.We need more time to think about the map case, and fix it in
ScalaReflectionas well.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.
Sure. Should I create another pull request with this change only?
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.
yes please, thanks!
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.
Removed other changes from this PR and created a new one with only map case.
As far as I see, everything works fine with scala classes, because StructTypes are generated based on constructor parameters, and they are available in correct order with correct names. Which is hardly achievable with Java beans..