-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-30289][SQL][TEST] Partitioned by Nested Column for InMemoryTable
#26929
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,7 +26,7 @@ import org.scalatest.Assertions._ | |
|
|
||
| import org.apache.spark.sql.catalyst.InternalRow | ||
| import org.apache.spark.sql.connector.catalog._ | ||
| import org.apache.spark.sql.connector.expressions.{IdentityTransform, Transform} | ||
| import org.apache.spark.sql.connector.expressions.{IdentityTransform, NamedReference, Transform} | ||
| import org.apache.spark.sql.connector.read._ | ||
| import org.apache.spark.sql.connector.write._ | ||
| import org.apache.spark.sql.sources.{And, EqualTo, Filter, IsNotNull} | ||
|
|
@@ -59,10 +59,30 @@ class InMemoryTable( | |
|
|
||
| def rows: Seq[InternalRow] = dataMap.values.flatMap(_.rows).toSeq | ||
|
|
||
| private val partFieldNames = partitioning.flatMap(_.references).toSeq.flatMap(_.fieldNames) | ||
| private val partIndexes = partFieldNames.map(schema.fieldIndex) | ||
| private val partCols: Array[Array[String]] = partitioning.flatMap(_.references).map { ref => | ||
| schema.findNestedField(ref.fieldNames(), includeCollections = false) match { | ||
| case Some(_) => ref.fieldNames() | ||
| case None => throw new IllegalArgumentException(s"${ref.describe()} does not exist.") | ||
| } | ||
| } | ||
|
|
||
| private def getKey(row: InternalRow): Seq[Any] = partIndexes.map(row.toSeq(schema)(_)) | ||
| private def getKey(row: InternalRow): Seq[Any] = { | ||
| def extractor(fieldNames: Array[String], schema: StructType, row: InternalRow): Any = { | ||
| val index = schema.fieldIndex(fieldNames(0)) | ||
| val value = row.toSeq(schema).apply(index) | ||
| if (fieldNames.length > 1) { | ||
| (value, schema(index).dataType) match { | ||
| case (row: InternalRow, nestedSchema: StructType) => | ||
| extractor(fieldNames.slice(1, fieldNames.length), nestedSchema, row) | ||
|
||
| case (_, dataType) => | ||
| throw new IllegalArgumentException(s"Unsupported type, ${dataType.simpleString}") | ||
| } | ||
| } else { | ||
| value | ||
| } | ||
| } | ||
| partCols.map(filedNames => extractor(filedNames, schema, row)) | ||
|
||
| } | ||
|
|
||
| def withData(data: Array[BufferedRows]): InMemoryTable = dataMap.synchronized { | ||
| data.foreach(_.rows.foreach { row => | ||
|
|
@@ -146,8 +166,10 @@ class InMemoryTable( | |
| } | ||
|
|
||
| private class Overwrite(filters: Array[Filter]) extends TestBatchWrite { | ||
| import org.apache.spark.sql.connector.catalog.CatalogV2Implicits.MultipartIdentifierHelper | ||
| override def commit(messages: Array[WriterCommitMessage]): Unit = dataMap.synchronized { | ||
| val deleteKeys = InMemoryTable.filtersToKeys(dataMap.keys, partFieldNames, filters) | ||
| val deleteKeys = InMemoryTable.filtersToKeys( | ||
| dataMap.keys, partCols.map(_.toSeq.quoted), filters) | ||
| dataMap --= deleteKeys | ||
| withData(messages.map(_.asInstanceOf[BufferedRows])) | ||
| } | ||
|
|
@@ -161,7 +183,8 @@ class InMemoryTable( | |
| } | ||
|
|
||
| override def deleteWhere(filters: Array[Filter]): Unit = dataMap.synchronized { | ||
| dataMap --= InMemoryTable.filtersToKeys(dataMap.keys, partFieldNames, filters) | ||
| import org.apache.spark.sql.connector.catalog.CatalogV2Implicits.MultipartIdentifierHelper | ||
| dataMap --= InMemoryTable.filtersToKeys(dataMap.keys, partCols.map(_.toSeq.quoted), filters) | ||
| } | ||
| } | ||
|
|
||
|
|
||
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.
The nested columns were flatten out here, and then we looked them up against top level columns resulting
IllegalArgumentException.