Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,18 @@ object V2ScanPartitioningAndOrdering extends Rule[LogicalPlan] with SQLConfHelpe
private def partitioning(plan: LogicalPlan) = plan.transformDown {
case d @ DataSourceV2ScanRelation(relation, scan: SupportsReportPartitioning, _, None, _) =>
val catalystPartitioning = scan.outputPartitioning() match {
case kgp: KeyGroupedPartitioning => sequenceToOption(kgp.keys().map(
V2ExpressionUtils.toCatalystOpt(_, relation, relation.funCatalog)))
case kgp: KeyGroupedPartitioning =>
val partitioning = sequenceToOption(
kgp.keys().map(V2ExpressionUtils.toCatalystOpt(_, relation, relation.funCatalog)))
if (partitioning.isEmpty) {
None
} else {
if (partitioning.get.forall(p => p.references.subsetOf(d.outputSet))) {
partitioning
} else {
None
}
}
case _: UnknownPartitioning => None
case p => throw new IllegalArgumentException("Unsupported data source V2 partitioning " +
"type: " + p.getClass.getSimpleName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,20 @@ class MetadataColumnSuite extends DatasourceV2SQLBase {
.withColumn("right_all", struct($"right.*"))
checkAnswer(dfQuery, Row(1, "a", "b", Row(1, "a"), Row(1, "b")))
}

test("SPARK-40429: Only set KeyGroupedPartitioning when the referenced column is in the output") {
withTable(tbl) {
sql(s"CREATE TABLE $tbl (id bigint, data string) PARTITIONED BY (id)")
sql(s"INSERT INTO $tbl VALUES (1, 'a'), (2, 'b'), (3, 'c')")
checkAnswer(
spark.table(tbl).select("index", "_partition"),
Seq(Row(0, "3"), Row(0, "2"), Row(0, "1"))
)

checkAnswer(
spark.table(tbl).select("id", "index", "_partition"),
Seq(Row(3, 0, "3"), Row(2, 0, "2"), Row(1, 0, "1"))
)
}
}
}