Skip to content

[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

Closed
wants to merge 5 commits into from
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 @@ -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}
Expand Down Expand Up @@ -59,10 +59,30 @@ class InMemoryTable(

def rows: Seq[InternalRow] = dataMap.values.flatMap(_.rows).toSeq

private val partFieldNames = partitioning.flatMap(_.references).toSeq.flatMap(_.fieldNames)
Copy link
Member Author

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.

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.drop(1), nestedSchema, row)
case (_, dataType) =>
throw new IllegalArgumentException(s"Unsupported type, ${dataType.simpleString}")
}
} else {
value
}
}
partCols.map(fieldNames => extractor(fieldNames, schema, row))
}

def withData(data: Array[BufferedRows]): InMemoryTable = dataMap.synchronized {
data.foreach(_.rows.foreach { row =>
Expand Down Expand Up @@ -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]))
}
Expand All @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,24 @@

package org.apache.spark.sql

import java.sql.Timestamp

import scala.collection.JavaConverters._

import org.scalatest.BeforeAndAfter

import org.apache.spark.sql.catalyst.analysis.{CannotReplaceMissingTableException, NoSuchTableException, TableAlreadyExistsException}
import org.apache.spark.sql.catalyst.plans.logical.{AppendData, LogicalPlan, OverwriteByExpression, OverwritePartitionsDynamic}
import org.apache.spark.sql.connector.InMemoryTableCatalog
import org.apache.spark.sql.connector.{InMemoryTable, InMemoryTableCatalog}
import org.apache.spark.sql.connector.catalog.{Identifier, TableCatalog}
import org.apache.spark.sql.connector.expressions.{BucketTransform, DaysTransform, FieldReference, HoursTransform, IdentityTransform, LiteralValue, MonthsTransform, YearsTransform}
import org.apache.spark.sql.execution.QueryExecution
import org.apache.spark.sql.execution.datasources.v2.DataSourceV2Relation
import org.apache.spark.sql.test.SharedSparkSession
import org.apache.spark.sql.types.{IntegerType, LongType, StringType, StructType}
import org.apache.spark.sql.types.TimestampType
import org.apache.spark.sql.util.QueryExecutionListener
import org.apache.spark.unsafe.types.UTF8String
import org.apache.spark.util.Utils

class DataFrameWriterV2Suite extends QueryTest with SharedSparkSession with BeforeAndAfter {
Expand Down Expand Up @@ -550,4 +554,84 @@ class DataFrameWriterV2Suite extends QueryTest with SharedSparkSession with Befo
assert(replaced.partitioning.isEmpty)
assert(replaced.properties === defaultOwnership.asJava)
}

test("SPARK-30289 Create: partitioned by nested column") {
val schema = new StructType().add("ts", new StructType()
.add("created", TimestampType)
.add("modified", TimestampType)
.add("timezone", StringType))

val data = Seq(
Row(Row(Timestamp.valueOf("2019-06-01 10:00:00"), Timestamp.valueOf("2019-09-02 07:00:00"),
"America/Los_Angeles")),
Row(Row(Timestamp.valueOf("2019-08-26 18:00:00"), Timestamp.valueOf("2019-09-26 18:00:00"),
"America/Los_Angeles")),
Row(Row(Timestamp.valueOf("2018-11-23 18:00:00"), Timestamp.valueOf("2018-12-22 18:00:00"),
"America/New_York")))
val df = spark.createDataFrame(spark.sparkContext.parallelize(data, 1), schema)

df.writeTo("testcat.table_name")
.partitionedBy($"ts.timezone")
.create()

val table = catalog("testcat").loadTable(Identifier.of(Array(), "table_name"))
.asInstanceOf[InMemoryTable]

assert(table.name === "testcat.table_name")
assert(table.partitioning === Seq(IdentityTransform(FieldReference(Array("ts", "timezone")))))
checkAnswer(spark.table(table.name), data)
assert(table.dataMap.toArray.length == 2)
assert(table.dataMap(Seq(UTF8String.fromString("America/Los_Angeles"))).rows.size == 2)
assert(table.dataMap(Seq(UTF8String.fromString("America/New_York"))).rows.size == 1)

// TODO: `DataSourceV2Strategy` can not translate nested fields into source filter yet
// so the following sql will fail.
// sql("DELETE FROM testcat.table_name WHERE ts.timezone = \"America/Los_Angeles\"")
}

test("SPARK-30289 Create: partitioned by multiple transforms on nested columns") {
spark.table("source")
.withColumn("ts", struct(
lit("2019-06-01 10:00:00.000000").cast("timestamp") as "created",
lit("2019-09-02 07:00:00.000000").cast("timestamp") as "modified",
lit("America/Los_Angeles") as "timezone"))
.writeTo("testcat.table_name")
.tableProperty("allow-unsupported-transforms", "true")
.partitionedBy(
years($"ts.created"), months($"ts.created"), days($"ts.created"), hours($"ts.created"),
years($"ts.modified"), months($"ts.modified"), days($"ts.modified"), hours($"ts.modified")
)
.create()

val table = catalog("testcat").loadTable(Identifier.of(Array(), "table_name"))

assert(table.name === "testcat.table_name")
assert(table.partitioning === Seq(
YearsTransform(FieldReference(Array("ts", "created"))),
MonthsTransform(FieldReference(Array("ts", "created"))),
DaysTransform(FieldReference(Array("ts", "created"))),
HoursTransform(FieldReference(Array("ts", "created"))),
YearsTransform(FieldReference(Array("ts", "modified"))),
MonthsTransform(FieldReference(Array("ts", "modified"))),
DaysTransform(FieldReference(Array("ts", "modified"))),
HoursTransform(FieldReference(Array("ts", "modified")))))
}

test("SPARK-30289 Create: partitioned by bucket(4, ts.timezone)") {
spark.table("source")
.withColumn("ts", struct(
lit("2019-06-01 10:00:00.000000").cast("timestamp") as "created",
lit("2019-09-02 07:00:00.000000").cast("timestamp") as "modified",
lit("America/Los_Angeles") as "timezone"))
.writeTo("testcat.table_name")
.tableProperty("allow-unsupported-transforms", "true")
.partitionedBy(bucket(4, $"ts.timezone"))
.create()

val table = catalog("testcat").loadTable(Identifier.of(Array(), "table_name"))

assert(table.name === "testcat.table_name")
assert(table.partitioning === Seq(BucketTransform(LiteralValue(4, IntegerType),
Seq(FieldReference(Seq("ts", "timezone"))))))
}
}