|
| 1 | +/* |
| 2 | + * Copyright (2025) The Delta Lake Project Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.delta.kernel.internal.clustering |
| 17 | + |
| 18 | +import scala.collection.JavaConverters._ |
| 19 | + |
| 20 | +import io.delta.kernel.expressions.Column |
| 21 | +import io.delta.kernel.internal.util.{ColumnMapping, ColumnMappingSuiteBase} |
| 22 | +import io.delta.kernel.types._ |
| 23 | + |
| 24 | +import org.scalatest.funsuite.AnyFunSuite |
| 25 | + |
| 26 | +class ClusteringMetadataDomainSuite |
| 27 | + extends AnyFunSuite |
| 28 | + with ColumnMappingSuiteBase { |
| 29 | + |
| 30 | + private def convertToPhysicalColumn( |
| 31 | + logicalColumns: List[Column], |
| 32 | + schema: StructType): List[Column] = { |
| 33 | + logicalColumns.map { column => |
| 34 | + ColumnMapping.getPhysicalColumnNameAndDataType(schema, column)._1 |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + test("ClusteringDomainMetadata can be serialized") { |
| 39 | + val clusteringColumns = |
| 40 | + List(new Column(Array("col1", "`col2,col3`", "`col4.col5`,col6"))) |
| 41 | + val clusteringMetadataDomain = ClusteringMetadataDomain.fromClusteringColumns( |
| 42 | + clusteringColumns.asJava) |
| 43 | + val serializedString = clusteringMetadataDomain.toDomainMetadata.toString |
| 44 | + assert(serializedString === |
| 45 | + """|DomainMetadata{domain='delta.clustering', configuration= |
| 46 | + |'{"clusteringColumns":[["col1","`col2,col3`","`col4.col5`,col6"]]}', |
| 47 | + | removed='false'}""".stripMargin.replace("\n", "")) |
| 48 | + } |
| 49 | + |
| 50 | + test("ClusteringDomainMetadata can be deserialized") { |
| 51 | + val configJson = """{"clusteringColumns":[["col1","`col2,col3`","`col4.col5`,col6"]]}""" |
| 52 | + val clusteringMD = ClusteringMetadataDomain.fromJsonConfiguration(configJson) |
| 53 | + |
| 54 | + assert(clusteringMD.getClusteringColumns === List(new Column(Array( |
| 55 | + "col1", |
| 56 | + "`col2,col3`", |
| 57 | + "`col4.col5`,col6"))).asJava) |
| 58 | + } |
| 59 | + |
| 60 | + test("Successfully get DomainMetadata for non-nested columns") { |
| 61 | + val schema = new StructType() |
| 62 | + .add("id", IntegerType.INTEGER, true) |
| 63 | + .add("name", IntegerType.INTEGER, true) |
| 64 | + .add("age", IntegerType.INTEGER, true) |
| 65 | + |
| 66 | + val clusterColumns = List(new Column("name"), new Column("age")) |
| 67 | + val physicalColumns = convertToPhysicalColumn(clusterColumns, schema) |
| 68 | + |
| 69 | + val clusteringMetadataDomain = |
| 70 | + ClusteringMetadataDomain.fromClusteringColumns( |
| 71 | + physicalColumns.asJava) |
| 72 | + |
| 73 | + val clusteringDomainMetadata = clusteringMetadataDomain.toDomainMetadata |
| 74 | + assert(clusteringMetadataDomain.getClusteringColumns == clusterColumns.asJava) |
| 75 | + assert(clusteringDomainMetadata.getDomain == "delta.clustering") |
| 76 | + assert(clusteringDomainMetadata.getConfiguration == |
| 77 | + """{"clusteringColumns":[["name"],["age"]]}""") |
| 78 | + } |
| 79 | + |
| 80 | + test("Successfully get DomainMetadata for nested columns") { |
| 81 | + val schema = new StructType() |
| 82 | + .add("id", IntegerType.INTEGER, true) |
| 83 | + .add( |
| 84 | + "user", |
| 85 | + new StructType() |
| 86 | + .add( |
| 87 | + "address", |
| 88 | + new StructType() |
| 89 | + .add("city", StringType.STRING, true))) |
| 90 | + |
| 91 | + val clusterColumns = List(new Column(Array("user", "address", "city"))) |
| 92 | + val physicalColumns = convertToPhysicalColumn(clusterColumns, schema) |
| 93 | + |
| 94 | + val clusteringMetadataDomain = ClusteringMetadataDomain.fromClusteringColumns( |
| 95 | + physicalColumns.asJava) |
| 96 | + |
| 97 | + val clusteringDomainMetadata = clusteringMetadataDomain.toDomainMetadata |
| 98 | + assert(clusteringMetadataDomain.getClusteringColumns == |
| 99 | + List(new Column(Array("user", "address", "city"))).asJava) |
| 100 | + assert(clusteringDomainMetadata.getDomain == "delta.clustering") |
| 101 | + assert(clusteringDomainMetadata.getConfiguration == |
| 102 | + """{"clusteringColumns":[["user","address","city"]]}""") |
| 103 | + } |
| 104 | +} |
0 commit comments