-
Notifications
You must be signed in to change notification settings - Fork 28.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
[SPARK-12975] [SQL] Throwing Exception when Bucketing Columns are part of Partitioning Columns #10891
Closed
gatorsmile
wants to merge
8
commits into
apache:master
from
gatorsmile:commonKeysInPartitionByBucketBy
Closed
[SPARK-12975] [SQL] Throwing Exception when Bucketing Columns are part of Partitioning Columns #10891
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
14fb29d
remove unnecessary columns from blockBy
gatorsmile e68351b
added more test cases.
gatorsmile e529b7d
Merge remote-tracking branch 'upstream/master' into commonKeysInParti…
gatorsmile 8c718b3
style fix.
gatorsmile d207813
address comments.
gatorsmile 64f5ea0
typo fix.
gatorsmile f9a8bdf
typo fix.
gatorsmile 2ace09f
address comments.
gatorsmile 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
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 |
---|---|---|
|
@@ -745,7 +745,7 @@ class MetastoreDataSourcesSuite extends QueryTest with SQLTestUtils with TestHiv | |
} | ||
} | ||
|
||
test("Saving partition columns information") { | ||
test("Saving partitionBy columns information") { | ||
val df = (1 to 10).map(i => (i, i + 1, s"str$i", s"str${i + 1}")).toDF("a", "b", "c", "d") | ||
val tableName = s"partitionInfo_${System.currentTimeMillis()}" | ||
|
||
|
@@ -776,6 +776,59 @@ class MetastoreDataSourcesSuite extends QueryTest with SQLTestUtils with TestHiv | |
} | ||
} | ||
|
||
test("Saving information for sortBy and bucketBy columns") { | ||
val df = (1 to 10).map(i => (i, i + 1, s"str$i", s"str${i + 1}")).toDF("a", "b", "c", "d") | ||
val tableName = s"partitionInfo_${System.currentTimeMillis()}" | ||
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: use |
||
|
||
withTable(tableName) { | ||
df.write | ||
.format("parquet") | ||
.bucketBy(8, "d", "b") | ||
.sortBy("c") | ||
.saveAsTable(tableName) | ||
invalidateTable(tableName) | ||
val metastoreTable = catalog.client.getTable("default", tableName) | ||
val expectedBucketByColumns = StructType(df.schema("d") :: df.schema("b") :: Nil) | ||
val expectedSortByColumns = StructType(df.schema("c") :: Nil) | ||
|
||
val numBuckets = metastoreTable.properties("spark.sql.sources.schema.numBuckets").toInt | ||
assert(numBuckets == 8) | ||
|
||
val numBucketCols = metastoreTable.properties("spark.sql.sources.schema.numBucketCols").toInt | ||
assert(numBucketCols == 2) | ||
|
||
val numSortCols = metastoreTable.properties("spark.sql.sources.schema.numSortCols").toInt | ||
assert(numSortCols == 1) | ||
|
||
val actualBucketByColumns = | ||
StructType( | ||
(0 until numBucketCols).map { index => | ||
df.schema(metastoreTable.properties(s"spark.sql.sources.schema.bucketCol.$index")) | ||
}) | ||
// Make sure bucketBy columns are correctly stored in metastore. | ||
assert( | ||
expectedBucketByColumns.sameType(actualBucketByColumns), | ||
s"Partitions columns stored in metastore $actualBucketByColumns is not the " + | ||
s"partition columns defined by the saveAsTable operation $expectedBucketByColumns.") | ||
|
||
val actualSortByColumns = | ||
StructType( | ||
(0 until numSortCols).map { index => | ||
df.schema(metastoreTable.properties(s"spark.sql.sources.schema.sortCol.$index")) | ||
}) | ||
// Make sure sortBy columns are correctly stored in metastore. | ||
assert( | ||
expectedSortByColumns.sameType(actualSortByColumns), | ||
s"Partitions columns stored in metastore $actualSortByColumns is not the " + | ||
s"partition columns defined by the saveAsTable operation $expectedSortByColumns.") | ||
|
||
// Check the content of the saved table. | ||
checkAnswer( | ||
table(tableName).select("c", "b", "d", "a"), | ||
df.select("c", "b", "d", "a")) | ||
} | ||
} | ||
|
||
test("insert into a table") { | ||
def createDF(from: Int, to: Int): DataFrame = { | ||
(from to to).map(i => i -> s"str$i").toDF("c1", "c2") | ||
|
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 |
---|---|---|
|
@@ -92,10 +92,13 @@ class BucketedWriteSuite extends QueryTest with SQLTestUtils with TestHiveSingle | |
fail(s"Unable to find the related bucket files.") | ||
} | ||
|
||
// Remove the duplicate columns in bucketCols and sortCols; | ||
// Otherwise, we got analysis errors due to duplicate names | ||
val selectedColumns = (bucketCols ++ sortCols).distinct | ||
// We may lose the type information after write(e.g. json format doesn't keep schema | ||
// information), here we get the types from the original dataframe. | ||
val types = df.select((bucketCols ++ sortCols).map(col): _*).schema.map(_.dataType) | ||
val columns = (bucketCols ++ sortCols).zip(types).map { | ||
val types = df.select(selectedColumns.map(col): _*).schema.map(_.dataType) | ||
val columns = selectedColumns.zip(types).map { | ||
case (colName, dt) => col(colName).cast(dt) | ||
} | ||
|
||
|
@@ -158,6 +161,21 @@ class BucketedWriteSuite extends QueryTest with SQLTestUtils with TestHiveSingle | |
} | ||
} | ||
|
||
test("write bucketed data with the overlapping bucketBy and partitionBy columns") { | ||
intercept[AnalysisException](df.write | ||
.partitionBy("i") | ||
.bucketBy(8, "i", "k") | ||
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. is it better to use |
||
.sortBy("k") | ||
.saveAsTable("bucketed_table")) | ||
} | ||
|
||
test("write bucketed data with the identical bucketBy and partitionBy columns") { | ||
intercept[AnalysisException](df.write | ||
.partitionBy("i") | ||
.bucketBy(8, "i") | ||
.saveAsTable("bucketed_table")) | ||
} | ||
|
||
test("write bucketed data without partitionBy") { | ||
for (source <- Seq("parquet", "json", "orc")) { | ||
withTable("bucketed_table") { | ||
|
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.
no need to do this?