Skip to content

Commit 2c492b2

Browse files
anoopjcloud-fan
andcommitted
[SPARK-51372][SQL] Introduce TableInfo for table creations
### What changes were proposed in this pull request? Introduces a new `TableInfo` for operations in `TableCatalog`. A followup PR will be sent for the table replace methods in `StagingTableCatalog`. ### Why are the changes needed? The current `TableCatalog` interface has overloaded `createTable()` implementations. As we are adding new parameters, we have to deprecate the old ones. This PR introduces the `TableInfo` POJO so that the future code changes will be cleaner. ### Does this PR introduce _any_ user-facing change? Yes. But the PR is backwards compatible by providing a default implementation. ### How was this patch tested? * New unit tests. * Existing tests in `DataSourceV2Suite`. ### Was this patch authored or co-authored using generative AI tooling? No Closes #50137 from anoopj/anoopj-builder. Lead-authored-by: Anoop Johnson <[email protected]> Co-authored-by: Anoop Johnson <[email protected]> Co-authored-by: Wenchen Fan <[email protected]> Signed-off-by: Wenchen Fan <[email protected]>
1 parent ced7f6b commit 2c492b2

File tree

6 files changed

+379
-76
lines changed

6 files changed

+379
-76
lines changed

sql/catalyst/src/main/java/org/apache/spark/sql/connector/catalog/TableCatalog.java

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@
3333
/**
3434
* Catalog methods for working with Tables.
3535
* <p>
36-
* TableCatalog implementations may be case sensitive or case insensitive. Spark will pass
36+
* TableCatalog implementations may be case-sensitive or case-insensitive. Spark will pass
3737
* {@link Identifier table identifiers} without modification. Field names passed to
3838
* {@link #alterTable(Identifier, TableChange...)} will be normalized to match the case used in the
39-
* table schema when updating, renaming, or dropping existing columns when catalyst analysis is case
40-
* insensitive.
39+
* table schema when updating, renaming, or dropping existing columns when catalyst analysis is
40+
* case-insensitive.
4141
*
4242
* @since 3.0.0
4343
*/
@@ -208,26 +208,37 @@ default Table createTable(
208208
throw QueryCompilationErrors.mustOverrideOneMethodError("createTable");
209209
}
210210

211+
/**
212+
* Create a table in the catalog.
213+
* <p>
214+
* @deprecated This is deprecated. Please override
215+
* {@link #createTable(Identifier, TableInfo)} instead.
216+
*/
217+
@Deprecated(since = "4.1.0")
218+
default Table createTable(
219+
Identifier ident,
220+
Column[] columns,
221+
Transform[] partitions,
222+
Map<String, String> properties) throws TableAlreadyExistsException, NoSuchNamespaceException {
223+
return createTable(ident, CatalogV2Util.v2ColumnsToStructType(columns), partitions, properties);
224+
}
225+
211226
/**
212227
* Create a table in the catalog.
213228
*
214229
* @param ident a table identifier
215-
* @param columns the columns of the new table.
216-
* @param partitions transforms to use for partitioning data in the table
217-
* @param properties a string map of table properties
230+
* @param tableInfo information about the table.
218231
* @return metadata for the new table. This can be null if getting the metadata for the new table
219232
* is expensive. Spark will call {@link #loadTable(Identifier)} if needed (e.g. CTAS).
220233
*
221234
* @throws TableAlreadyExistsException If a table or view already exists for the identifier
222235
* @throws UnsupportedOperationException If a requested partition transform is not supported
223236
* @throws NoSuchNamespaceException If the identifier namespace does not exist (optional)
237+
* @since 4.1.0
224238
*/
225-
default Table createTable(
226-
Identifier ident,
227-
Column[] columns,
228-
Transform[] partitions,
229-
Map<String, String> properties) throws TableAlreadyExistsException, NoSuchNamespaceException {
230-
return createTable(ident, CatalogV2Util.v2ColumnsToStructType(columns), partitions, properties);
239+
default Table createTable(Identifier ident, TableInfo tableInfo)
240+
throws TableAlreadyExistsException, NoSuchNamespaceException {
241+
return createTable(ident, tableInfo.columns(), tableInfo.partitions(), tableInfo.properties());
231242
}
232243

233244
/**
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.spark.sql.connector.catalog;
18+
19+
import static com.google.common.base.Preconditions.checkNotNull;
20+
import com.google.common.collect.Maps;
21+
import org.apache.spark.sql.connector.expressions.Transform;
22+
import org.apache.spark.sql.types.StructType;
23+
24+
import java.util.Collections;
25+
import java.util.Map;
26+
27+
public class TableInfo {
28+
29+
private final Column[] columns;
30+
private final Map<String, String> properties;
31+
private final Transform[] partitions;
32+
33+
/**
34+
* Constructor for TableInfo used by the builder.
35+
* @param builder Builder.
36+
*/
37+
private TableInfo(Builder builder) {
38+
this.columns = builder.columns;
39+
this.properties = Collections.unmodifiableMap(builder.properties);
40+
this.partitions = builder.partitions;
41+
}
42+
43+
public Column[] columns() {
44+
return columns;
45+
}
46+
47+
public StructType schema() {
48+
return CatalogV2Util.v2ColumnsToStructType(columns);
49+
}
50+
51+
public Map<String, String> properties() {
52+
return properties;
53+
}
54+
55+
public Transform[] partitions() {
56+
return partitions;
57+
}
58+
59+
public static class Builder {
60+
private Column[] columns;
61+
private Map<String, String> properties;
62+
private Transform[] partitions;
63+
64+
public Builder withColumns(Column[] columns) {
65+
this.columns = columns;
66+
return this;
67+
}
68+
69+
public Builder withProperties(Map<String, String> properties) {
70+
this.properties = Maps.newHashMap();
71+
this.properties.putAll(properties);
72+
return this;
73+
}
74+
75+
public Builder withPartitions(Transform[] partitions) {
76+
this.partitions = partitions;
77+
return this;
78+
}
79+
80+
public TableInfo build() {
81+
checkNotNull(columns, "columns should not be null");
82+
return new TableInfo(this);
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)