-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-28178][SQL] DataSourceV2: DataFrameWriter.insertInfo #24980
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
107 changes: 107 additions & 0 deletions
107
sql/core/src/test/scala/org/apache/spark/sql/sources/v2/DataSourceV2DataFrameSuite.scala
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,107 @@ | ||
| /* | ||
| * 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 org.apache.spark.sql.sources.v2 | ||
|
|
||
| import org.scalatest.BeforeAndAfter | ||
|
|
||
| import org.apache.spark.sql.QueryTest | ||
| import org.apache.spark.sql.internal.SQLConf.{PARTITION_OVERWRITE_MODE, PartitionOverwriteMode} | ||
| import org.apache.spark.sql.test.SharedSQLContext | ||
|
|
||
| class DataSourceV2DataFrameSuite extends QueryTest with SharedSQLContext with BeforeAndAfter { | ||
| import testImplicits._ | ||
|
|
||
| before { | ||
| spark.conf.set("spark.sql.catalog.testcat", classOf[TestInMemoryTableCatalog].getName) | ||
| spark.conf.set("spark.sql.catalog.testcat2", classOf[TestInMemoryTableCatalog].getName) | ||
|
|
||
| val df = spark.createDataFrame(Seq((1L, "a"), (2L, "b"), (3L, "c"))).toDF("id", "data") | ||
| df.createOrReplaceTempView("source") | ||
| val df2 = spark.createDataFrame(Seq((4L, "d"), (5L, "e"), (6L, "f"))).toDF("id", "data") | ||
| df2.createOrReplaceTempView("source2") | ||
| } | ||
|
|
||
| after { | ||
| spark.catalog("testcat").asInstanceOf[TestInMemoryTableCatalog].clearTables() | ||
| spark.sql("DROP VIEW source") | ||
| spark.sql("DROP VIEW source2") | ||
| } | ||
|
|
||
| test("insertInto: append") { | ||
| val t1 = "testcat.ns1.ns2.tbl" | ||
| withTable(t1) { | ||
| sql(s"CREATE TABLE $t1 (id bigint, data string) USING foo") | ||
| spark.table("source").select("id", "data").write.insertInto(t1) | ||
| checkAnswer(spark.table(t1), spark.table("source")) | ||
| } | ||
| } | ||
|
|
||
| test("insertInto: append - across catalog") { | ||
| val t1 = "testcat.ns1.ns2.tbl" | ||
| val t2 = "testcat2.db.tbl" | ||
| withTable(t1, t2) { | ||
| sql(s"CREATE TABLE $t1 USING foo AS TABLE source") | ||
| sql(s"CREATE TABLE $t2 (id bigint, data string) USING foo") | ||
| spark.table(t1).write.insertInto(t2) | ||
| checkAnswer(spark.table(t2), spark.table("source")) | ||
| } | ||
| } | ||
|
|
||
| test("insertInto: append partitioned table") { | ||
| val t1 = "testcat.ns1.ns2.tbl" | ||
| withTable(t1) { | ||
| sql(s"CREATE TABLE $t1 (id bigint, data string) USING foo PARTITIONED BY (id)") | ||
| spark.table("source").write.insertInto(t1) | ||
| checkAnswer(spark.table(t1), spark.table("source")) | ||
| } | ||
| } | ||
|
|
||
| test("insertInto: overwrite non-partitioned table") { | ||
| val t1 = "testcat.ns1.ns2.tbl" | ||
| withTable(t1) { | ||
| sql(s"CREATE TABLE $t1 USING foo AS TABLE source") | ||
| spark.table("source2").write.mode("overwrite").insertInto(t1) | ||
| checkAnswer(spark.table(t1), spark.table("source2")) | ||
| } | ||
| } | ||
|
|
||
| test("insertInto: overwrite - static mode") { | ||
| withSQLConf(PARTITION_OVERWRITE_MODE.key -> PartitionOverwriteMode.STATIC.toString) { | ||
| val t1 = "testcat.ns1.ns2.tbl" | ||
| withTable(t1) { | ||
| sql(s"CREATE TABLE $t1 (id bigint, data string) USING foo PARTITIONED BY (id)") | ||
| Seq((2L, "dummy"), (4L, "keep")).toDF("id", "data").write.insertInto(t1) | ||
| spark.table("source").write.mode("overwrite").insertInto(t1) | ||
| checkAnswer(spark.table(t1), spark.table("source")) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| test("insertInto: overwrite - dynamic mode") { | ||
| withSQLConf(PARTITION_OVERWRITE_MODE.key -> PartitionOverwriteMode.DYNAMIC.toString) { | ||
| val t1 = "testcat.ns1.ns2.tbl" | ||
| withTable(t1) { | ||
| sql(s"CREATE TABLE $t1 (id bigint, data string) USING foo PARTITIONED BY (id)") | ||
| Seq((2L, "dummy"), (4L, "keep")).toDF("id", "data").write.insertInto(t1) | ||
| spark.table("source").write.mode("overwrite").insertInto(t1) | ||
| checkAnswer(spark.table(t1), | ||
| spark.table("source").union(sql("SELECT 4L, 'keep'"))) | ||
| } | ||
| } | ||
| } | ||
| } |
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.
I missed it. If you look at the doc of
insertInto, it saysWe should use
byPositionhere.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.
Agreed. This is an oversight and should be by position.
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.
Thanks @cloud-fan @rdblue. I will submit a hotfix.
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.
I can create a follow-up PR to introduce an option
matchByName, default tofalse. If true, insertInto uses byName; otherwise, byPosition.Maybe even included in this PR?
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.
If we are going to create a new dataframe writer API in the future, I'd like to keep it as it is, and always do by-position in this
insertInto.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.
Sounds good to me. I'll submit a PR for the new API.
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.
@rdblue @cloud-fan @dongjoon-hyun #25353 is ready for review.