Skip to content

Commit ae4a83f

Browse files
authored
[Kernel][Clustering #3] add ClusteringMetadataDomain (delta-io#4321)
<!-- 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 - [x] Kernel - [ ] Other (fill in here) ## 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. --> Split the main PR delta-io#4265 for faster review add `ClusteringMetadataDomain` which could generate the clustering domain metadata like ``` { "domainMetadata": { "domain": "delta.clustering", "configuration": "{\"clusteringColumns\":[\"col-daadafd7-7c20-4697-98f8-bff70199b1f9\", \"col-5abe0e80-cf57-47ac-9ffc-a861a3d1077e\"]}", "removed": false } } ``` ## 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. --> unit tests ## 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'. -->
1 parent 930ff26 commit ae4a83f

File tree

2 files changed

+194
-0
lines changed

2 files changed

+194
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 com.fasterxml.jackson.annotation.JsonCreator;
19+
import com.fasterxml.jackson.annotation.JsonIgnore;
20+
import com.fasterxml.jackson.annotation.JsonProperty;
21+
import io.delta.kernel.expressions.Column;
22+
import io.delta.kernel.internal.SnapshotImpl;
23+
import io.delta.kernel.internal.metadatadomain.JsonMetadataDomain;
24+
import java.util.*;
25+
import java.util.stream.Collectors;
26+
27+
/** Represents the metadata domain for clustering. */
28+
public final class ClusteringMetadataDomain extends JsonMetadataDomain {
29+
30+
public static final String DOMAIN_NAME = "delta.clustering";
31+
32+
/**
33+
* Constructs a ClusteringMetadataDomain with the clustering columns.
34+
*
35+
* @param clusteringColumns the columns used for clustering. If column mapping is enabled, use the
36+
* physical name assigned; otherwise, use the logical column name.
37+
*/
38+
public static ClusteringMetadataDomain fromClusteringColumns(List<Column> clusteringColumns) {
39+
return new ClusteringMetadataDomain(
40+
clusteringColumns.stream()
41+
.map(column -> Arrays.asList(column.getNames()))
42+
.collect(Collectors.toList()));
43+
}
44+
45+
/**
46+
* Creates an instance of {@link ClusteringMetadataDomain} from a JSON configuration string.
47+
*
48+
* @param json the JSON configuration string
49+
*/
50+
public static ClusteringMetadataDomain fromJsonConfiguration(String json) {
51+
return JsonMetadataDomain.fromJsonConfiguration(json, ClusteringMetadataDomain.class);
52+
}
53+
54+
/**
55+
* Creates an optional instance of {@link ClusteringMetadataDomain} from a {@link SnapshotImpl} if
56+
* present.
57+
*
58+
* @param snapshot the snapshot instance
59+
*/
60+
// TODO: Add the test coverage for this function in the integration test.
61+
public static Optional<ClusteringMetadataDomain> fromSnapshot(SnapshotImpl snapshot) {
62+
return JsonMetadataDomain.fromSnapshot(snapshot, ClusteringMetadataDomain.class, DOMAIN_NAME);
63+
}
64+
65+
/**
66+
* The column names used for clustering. If column mapping is enabled, we use physical column
67+
* names, otherwise we would store its logical column names. Stored as a List of Lists to avoid
68+
* customized serialization and deserialization logic.
69+
*/
70+
@JsonProperty("clusteringColumns")
71+
private final List<List<String>> clusteringColumns;
72+
73+
@JsonCreator
74+
private ClusteringMetadataDomain(
75+
@JsonProperty("clusteringColumns") List<List<String>> physicalClusteringColumns) {
76+
this.clusteringColumns = physicalClusteringColumns;
77+
}
78+
79+
@Override
80+
public String getDomainName() {
81+
return DOMAIN_NAME;
82+
}
83+
84+
@JsonIgnore
85+
public List<Column> getClusteringColumns() {
86+
return clusteringColumns.stream()
87+
.map(list -> new Column(list.toArray(new String[0])))
88+
.collect(Collectors.toList());
89+
}
90+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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

Comments
 (0)