Skip to content

Commit

Permalink
[Hudi] Support list/map data type conversion (#3320)
Browse files Browse the repository at this point in the history
<!--
Thanks for sending a pull request!  Here are some tips for you:
1. If this is your first time, please read our contributor guidelines:
https://github.com/delta-io/delta/blob/master/CONTRIBUTING.md
2. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP]
Your PR title ...'.
  3. Be sure to keep the PR description updated to reflect all changes.
  4. Please write your PR title to summarize what this PR proposes.
5. If possible, provide a concise example to reproduce the issue for a
faster review.
6. If applicable, include the corresponding issue number in the PR title
and link it in the body.
-->

#### Which Delta project/connector is this regarding?
<!--
Please add the component selected below to the beginning of the pull
request title
For example: [Spark] Title of my pull request
-->

- [ ] Spark
- [ ] Standalone
- [ ] Flink
- [ ] Kernel
- [x] Other (Hudi)

## Description

<!--
- Describe what this PR changes.
- Describe why we need the change.
 
If this PR resolves an issue be sure to include "Resolves #XXX" to
correctly link and close the issue upon merge.
-->
This PR adds functionality to convert Delta tables with list or map type
columns to be Hudi-readable.

## How was this patch tested?

<!--
If tests were added, say they were added here. Please make sure to test
the changes thoroughly including negative and positive cases if
possible.
If the changes were tested in any way other than unit tests, please
clarify how you tested step by step (ideally copy and paste-able, so
that other reviewers can test and check, and descendants can verify in
the future).
If the changes were not tested, please explain why.
-->

Added unit tests in ConvertToHudiSuite and tested manually with external
Hudi Spark reader.

## Does this PR introduce _any_ user-facing changes?

<!--
If yes, please clarify the previous behavior and the change this PR
proposes - provide the console output, description and/or an example to
show the behavior difference if possible.
If possible, please also clarify if this is a user-facing change
compared to the released Delta Lake versions or within the unreleased
branches such as master.
If no, write 'No'.
-->
Yes. Previously users could not enable the Delta table property for Hudi
conversion on tables containing list/map columns and would receive an
unsupportedType error but now they can.
  • Loading branch information
anniewang-db authored Jul 1, 2024
1 parent 207d8d2 commit dd39415
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@ object HudiSchemaUtils extends DeltaLogging {
finalizeSchema(
Schema.createRecord(currentPath, null, null, false, avroFields),
isNullable)
// TODO: Add List and Map support: https://github.com/delta-io/delta/issues/2738

case ArrayType(elementType, containsNull) =>
throw new UnsupportedOperationException("UniForm Hudi doesn't support Array columns")
finalizeSchema(
Schema.createArray(transform(elementType, containsNull, currentPath)),
isNullable)

case MapType(keyType, valueType, valueContainsNull) =>
throw new UnsupportedOperationException("UniForm Hudi doesn't support Map columns")
finalizeSchema(
Schema.createMap(transform(valueType, valueContainsNull, currentPath)),
isNullable)

case atomicType: AtomicType => convertAtomic(atomicType, isNullable)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,18 +150,55 @@ class ConvertToHudiSuite extends QueryTest with Eventually {
}
}

for (invalidFieldDef <- Seq("col3 ARRAY<STRING>", "col3 MAP<STRING, STRING>")) {
test(s"Table Throws Exception for Unsupported Type ($invalidFieldDef)") {
intercept[DeltaUnsupportedOperationException] {
_sparkSession.sql(
s"""CREATE TABLE `$testTableName` (col1 INT, col2 STRING, $invalidFieldDef) USING DELTA
|LOCATION '$testTablePath'
|TBLPROPERTIES (
| 'delta.universalFormat.enabledFormats' = 'hudi',
| 'delta.enableDeletionVectors' = false
|)""".stripMargin)
}
}
test(s"Conversion behavior for lists") {
_sparkSession.sql(
s"""CREATE TABLE `$testTableName` (col1 ARRAY<INT>) USING DELTA
|LOCATION '$testTablePath'
|TBLPROPERTIES (
| 'delta.universalFormat.enabledFormats' = 'hudi'
|)""".stripMargin)
_sparkSession.sql(s"INSERT INTO `$testTableName` VALUES (array(1, 2, 3))")
verifyFilesAndSchemaMatch()
}

test(s"Conversion behavior for lists of structs") {
_sparkSession.sql(
s"""CREATE TABLE `$testTableName`
|(col1 ARRAY<STRUCT<field1: INT, field2: STRING>>) USING DELTA
|LOCATION '$testTablePath'
|TBLPROPERTIES (
| 'delta.universalFormat.enabledFormats' = 'hudi'
|)""".stripMargin)
_sparkSession.sql(s"INSERT INTO `$testTableName` " +
s"VALUES (array(named_struct('field1', 1, 'field2', 'hello'), " +
s"named_struct('field1', 2, 'field2', 'world')))")
verifyFilesAndSchemaMatch()
}

test(s"Conversion behavior for lists of lists") {
_sparkSession.sql(
s"""CREATE TABLE `$testTableName`
|(col1 ARRAY<ARRAY<INT>>) USING DELTA
|LOCATION '$testTablePath'
|TBLPROPERTIES (
| 'delta.universalFormat.enabledFormats' = 'hudi'
|)""".stripMargin)
_sparkSession.sql(s"INSERT INTO `$testTableName` " +
s"VALUES (array(array(1, 2, 3), array(4, 5, 6)))")
verifyFilesAndSchemaMatch()
}

test(s"Conversion behavior for maps") {
_sparkSession.sql(
s"""CREATE TABLE `$testTableName` (col1 MAP<STRING, INT>) USING DELTA
|LOCATION '$testTablePath'
|TBLPROPERTIES (
| 'delta.universalFormat.enabledFormats' = 'hudi'
|)""".stripMargin)
_sparkSession.sql(
s"INSERT INTO `$testTableName` VALUES (map('a', 1, 'b', 2, 'c', 3))"
)
verifyFilesAndSchemaMatch()
}

test("validate Hudi timeline archival and cleaning") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@ import org.apache.spark.sql.delta.schema.SchemaUtils
import org.apache.spark.internal.MDC
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.catalyst.catalog.CatalogTable
import org.apache.spark.sql.types.{ArrayType, MapType, NullType}
import org.apache.spark.sql.types.NullType

/**
* Utils to validate the Universal Format (UniForm) Delta feature (NOT a table feature).
*
* The UniForm Delta feature governs and implements the actual conversion of Delta metadata into
* other formats.
*
* Currently, UniForm only supports Iceberg. When `delta.universalFormat.enabledFormats` contains
* "iceberg", we say that Universal Format (Iceberg) is enabled.
* UniForm supports both Iceberg and Hudi. When `delta.universalFormat.enabledFormats` contains
* "iceberg", we say that Universal Format (Iceberg) is enabled. When it contains "hudi", we say
* that Universal Format (Hudi) is enabled.
*
* [[enforceInvariantsAndDependencies]] ensures that all of UniForm's requirements for the
* specified format are met (e.g. for 'iceberg' that IcebergCompatV1 or V2 is enabled).
Expand Down Expand Up @@ -101,9 +102,8 @@ object UniversalFormat extends DeltaLogging {
if (DeltaConfigs.ENABLE_DELETION_VECTORS_CREATION.fromMetaData(newestMetadata)) {
throw DeltaErrors.uniFormHudiDeleteVectorCompat()
}
// TODO: remove once map/list support is added https://github.com/delta-io/delta/issues/2738
SchemaUtils.findAnyTypeRecursively(newestMetadata.schema) { f =>
f.isInstanceOf[MapType] || f.isInstanceOf[ArrayType] || f.isInstanceOf[NullType]
f.isInstanceOf[NullType]
} match {
case Some(unsupportedType) =>
throw DeltaErrors.uniFormHudiSchemaCompat(unsupportedType)
Expand Down

0 comments on commit dd39415

Please # to comment.