Skip to content

Commit 48f9f82

Browse files
Pass catalog name to metastore for lakehouse connectors
1 parent 549101d commit 48f9f82

File tree

54 files changed

+703
-66
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+703
-66
lines changed

presto-docs/src/main/sphinx/connector/deltalake.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Property Name Description
4949
``true``.
5050
``delta.case-sensitive-partitions-enabled`` Allows matching the names of partitioned columns in a ``true``
5151
case-sensitive manner.
52+
``hive.metastore.catalog.name`` Specifies the catalog name to be passed to the metastore.
5253
=============================================== ========================================================= ============
5354

5455
Delta Lake connector reuses many of the modules existing in Hive connector.

presto-docs/src/main/sphinx/connector/hive.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ Property Name Description
206206
error iterating through empty files.
207207

208208
``hive.file-status-cache.max-retained-size`` Maximum size in bytes of the directory listing cache ``0KB``
209+
210+
``hive.metastore.catalog.name`` Specifies the catalog name to be passed to the metastore.
209211
================================================== ============================================================ ============
210212

211213
Metastore Configuration Properties

presto-docs/src/main/sphinx/connector/hudi.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ Property Name Description
3838
======================================= ============================================= ===========
3939
``hudi.metadata-table-enabled`` Fetch the list of file names and sizes from false
4040
Hudi's metadata table rather than storage.
41+
``hive.metastore.catalog.name`` Specifies the catalog name to be passed to
42+
the metastore.
4143
======================================= ============================================= ===========
4244

4345
File-Based Metastore

presto-docs/src/main/sphinx/connector/iceberg.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Property Name Description
9191

9292
``iceberg.hive.table-refresh.backoff-scale-factor`` The multiple used to scale subsequent wait time between 4.0
9393
retries.
94+
``hive.metastore.catalog.name`` Specifies the catalog name to be passed to the metastore.
9495
======================================================== ============================================================= ============
9596

9697
Nessie catalog

presto-hive-common/src/main/java/com/facebook/presto/hive/HiveCommonClientConfig.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public class HiveCommonClientConfig
4646
private boolean readNullMaskedParquetEncryptedValueEnabled;
4747
private boolean useParquetColumnNames;
4848
private boolean zstdJniDecompressionEnabled;
49+
private String catalogName;
4950
private DataSize affinitySchedulingFileSectionSize = new DataSize(256, MEGABYTE);
5051

5152
public NodeSelectionStrategy getNodeSelectionStrategy()
@@ -286,6 +287,19 @@ public HiveCommonClientConfig setZstdJniDecompressionEnabled(boolean zstdJniDeco
286287
return this;
287288
}
288289

290+
public String getCatalogName()
291+
{
292+
return catalogName;
293+
}
294+
295+
@Config("hive.metastore.catalog.name")
296+
@ConfigDescription("Specified property to store the metastore catalog name.")
297+
public HiveCommonClientConfig setCatalogName(String catalogName)
298+
{
299+
this.catalogName = catalogName;
300+
return this;
301+
}
302+
289303
@NotNull
290304
public DataSize getAffinitySchedulingFileSectionSize()
291305
{

presto-hive-common/src/main/java/com/facebook/presto/hive/MetadataUtils.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import com.google.common.collect.ImmutableList;
3030
import com.google.common.collect.Iterables;
3131

32+
import javax.annotation.Nullable;
33+
3234
import java.util.HashSet;
3335
import java.util.List;
3436
import java.util.Map;
@@ -49,6 +51,10 @@
4951

5052
public final class MetadataUtils
5153
{
54+
private static final String CATALOG_DB_SEPARATOR = "#";
55+
private static final String CATALOG_DB_THRIFT_NAME_MARKER = "@";
56+
private static final String DB_EMPTY_MARKER = "!";
57+
private static final String DEFAULT_DATABASE = "default";
5258
private MetadataUtils() {}
5359

5460
public static Optional<DiscretePredicates> getDiscretePredicates(List<ColumnHandle> partitionColumns, List<HivePartition> partitions)
@@ -155,4 +161,26 @@ private static Domain buildColumnDomain(ColumnHandle column, List<HivePartition>
155161

156162
return Domain.onlyNull(type);
157163
}
164+
165+
/**
166+
* Constructs the schema name, including catalog name if applicable.
167+
*
168+
* @param schemaName the original schema name
169+
* @return the formatted schema name (Example - @catalog_name#schema_name)
170+
*/
171+
public static String constructSchemaName(Optional<String> catalogName, @Nullable String schemaName)
172+
{
173+
if (!catalogName.isPresent() || DEFAULT_DATABASE.equals(schemaName) ||
174+
(schemaName != null && schemaName.contains(CATALOG_DB_SEPARATOR))) {
175+
return schemaName;
176+
}
177+
178+
StringBuilder catalogDatabaseName = new StringBuilder()
179+
.append(CATALOG_DB_THRIFT_NAME_MARKER)
180+
.append(catalogName.get()) // Safe since we checked isPresent()
181+
.append(CATALOG_DB_SEPARATOR)
182+
.append(schemaName == null ? "" : schemaName.isEmpty() ? DB_EMPTY_MARKER : schemaName);
183+
184+
return catalogDatabaseName.toString();
185+
}
158186
}

presto-hive-common/src/test/java/com/facebook/presto/hive/TestHiveCommonClientConfig.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public void testDefaults()
4949
.setParquetBatchReaderVerificationEnabled(false)
5050
.setParquetBatchReadOptimizationEnabled(false)
5151
.setReadNullMaskedParquetEncryptedValue(false)
52+
.setCatalogName(null)
5253
.setAffinitySchedulingFileSectionSize(new DataSize(256, MEGABYTE)));
5354
}
5455

@@ -74,6 +75,7 @@ public void testExplicitPropertyMappings()
7475
.put("hive.enable-parquet-batch-reader-verification", "true")
7576
.put("hive.parquet-batch-read-optimization-enabled", "true")
7677
.put("hive.read-null-masked-parquet-encrypted-value-enabled", "true")
78+
.put("hive.metastore.catalog.name", "catalogName")
7779
.put("hive.affinity-scheduling-file-section-size", "512MB")
7880
.build();
7981

@@ -96,6 +98,7 @@ public void testExplicitPropertyMappings()
9698
.setParquetBatchReaderVerificationEnabled(true)
9799
.setParquetBatchReadOptimizationEnabled(true)
98100
.setReadNullMaskedParquetEncryptedValue(true)
101+
.setCatalogName("catalogName")
99102
.setAffinitySchedulingFileSectionSize(new DataSize(512, MEGABYTE));
100103

101104
ConfigAssertions.assertFullMapping(properties, expected);
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package com.facebook.presto.hive;
15+
16+
import org.testng.annotations.Test;
17+
18+
import java.util.Optional;
19+
20+
import static com.facebook.presto.hive.MetadataUtils.constructSchemaName;
21+
import static org.testng.Assert.assertNull;
22+
import static org.testng.Assert.assertTrue;
23+
24+
public class TestMetadataUtils
25+
{
26+
@Test
27+
public void testWithCatalogAndValidSchema()
28+
{
29+
String result = constructSchemaName(Optional.of("testCatalog"), "testSchema");
30+
assertTrue(result.equals("@testCatalog#testSchema"));
31+
}
32+
33+
@Test
34+
public void testWithCatalogAndDefaultSchema()
35+
{
36+
String result = constructSchemaName(Optional.of("testCatalog"), "default");
37+
assertTrue(result.equals("default"));
38+
}
39+
40+
@Test
41+
public void testWithCatalogAndSchemaContainingSeparator()
42+
{
43+
String result = constructSchemaName(Optional.of("testCatalog"), "schema#with#dot");
44+
assertTrue(result.equals("schema#with#dot"));
45+
}
46+
47+
@Test
48+
public void testWithoutCatalog()
49+
{
50+
String result = constructSchemaName(Optional.empty(), "testSchema");
51+
assertTrue(result.equals("testSchema"));
52+
}
53+
54+
@Test
55+
public void testWithNullSchema()
56+
{
57+
String result = constructSchemaName(Optional.empty(), null);
58+
assertNull(result);
59+
}
60+
61+
@Test
62+
public void testWithoutCatalogNameAndEmptySchemaName()
63+
{
64+
String result = constructSchemaName(Optional.empty(), "");
65+
assertTrue(result.isEmpty());
66+
}
67+
68+
@Test
69+
public void testWithCatalogNameAndEmptySchemaName()
70+
{
71+
String result = constructSchemaName(Optional.of("testCatalog"), "");
72+
assertTrue(result.equals("@testCatalog#!"));
73+
}
74+
}

presto-hive-hadoop2/src/test/java/com/facebook/presto/hive/s3select/S3SelectTestHelper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.facebook.presto.hive.HiveCoercionPolicy;
2929
import com.facebook.presto.hive.HiveColumnConverterProvider;
3030
import com.facebook.presto.hive.HiveColumnHandle;
31+
import com.facebook.presto.hive.HiveCommonClientConfig;
3132
import com.facebook.presto.hive.HiveEncryptionInformationProvider;
3233
import com.facebook.presto.hive.HiveFileRenamer;
3334
import com.facebook.presto.hive.HiveHdfsConfiguration;
@@ -137,7 +138,7 @@ public S3SelectTestHelper(String host,
137138
metastoreClientConfig.setMetastoreSocksProxy(HostAndPort.fromString(proxy));
138139
}
139140

140-
HiveCluster hiveCluster = new TestingHiveCluster(metastoreClientConfig, thriftHiveMetastoreConfig, host, port);
141+
HiveCluster hiveCluster = new TestingHiveCluster(metastoreClientConfig, thriftHiveMetastoreConfig, host, port, new HiveCommonClientConfig());
141142
executor = newCachedThreadPool(daemonThreadsNamed("hive-%s"));
142143
HivePartitionManager hivePartitionManager = new HivePartitionManager(FUNCTION_AND_TYPE_MANAGER, config);
143144

presto-hive-metastore/src/main/java/com/facebook/presto/hive/metastore/Database.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class Database
3939
private final PrincipalType ownerType;
4040
private final Optional<String> comment;
4141
private final Map<String, String> parameters;
42+
private final Optional<String> catalogName;
4243

4344
@JsonCreator
4445
public Database(
@@ -47,14 +48,16 @@ public Database(
4748
@JsonProperty("ownerName") String ownerName,
4849
@JsonProperty("ownerType") PrincipalType ownerType,
4950
@JsonProperty("comment") Optional<String> comment,
50-
@JsonProperty("parameters") Map<String, String> parameters)
51+
@JsonProperty("parameters") Map<String, String> parameters,
52+
@JsonProperty("catalogName") Optional<String> catalogName)
5153
{
5254
this.databaseName = requireNonNull(databaseName, "databaseName is null");
5355
this.location = requireNonNull(location, "location is null");
5456
this.ownerName = requireNonNull(ownerName, "ownerName is null");
5557
this.ownerType = requireNonNull(ownerType, "ownerType is null");
5658
this.comment = requireNonNull(comment, "comment is null");
5759
this.parameters = ImmutableMap.copyOf(requireNonNull(parameters, "parameters is null"));
60+
this.catalogName = requireNonNull(catalogName, "catalogName is null");
5861
}
5962

6063
@JsonProperty
@@ -103,6 +106,12 @@ public static Builder builder(Database database)
103106
return new Builder(database);
104107
}
105108

109+
@JsonProperty
110+
public Optional<String> getCatalogName()
111+
{
112+
return catalogName;
113+
}
114+
106115
public static class Builder
107116
{
108117
private String databaseName;
@@ -111,6 +120,7 @@ public static class Builder
111120
private PrincipalType ownerType;
112121
private Optional<String> comment = Optional.empty();
113122
private Map<String, String> parameters = new LinkedHashMap<>();
123+
private Optional<String> catalogName = Optional.empty();
114124

115125
public Builder() {}
116126

@@ -122,6 +132,7 @@ public Builder(Database database)
122132
this.ownerType = database.ownerType;
123133
this.comment = database.comment;
124134
this.parameters = database.parameters;
135+
this.catalogName = database.catalogName;
125136
}
126137

127138
public Builder setDatabaseName(String databaseName)
@@ -166,6 +177,12 @@ public Builder setParameters(Map<String, String> parameters)
166177
return this;
167178
}
168179

180+
public Builder setCatalogName(Optional<String> catalogName)
181+
{
182+
this.catalogName = requireNonNull(catalogName, "catalogName is null");
183+
return this;
184+
}
185+
169186
public Database build()
170187
{
171188
return new Database(
@@ -174,7 +191,8 @@ public Database build()
174191
ownerName,
175192
ownerType,
176193
comment,
177-
parameters);
194+
parameters,
195+
catalogName);
178196
}
179197
}
180198

@@ -188,6 +206,7 @@ public String toString()
188206
.add("ownerType", ownerType)
189207
.add("comment", comment)
190208
.add("parameters", parameters)
209+
.add("catalogName", catalogName)
191210
.toString();
192211
}
193212

@@ -207,12 +226,13 @@ public boolean equals(Object o)
207226
Objects.equals(ownerName, database.ownerName) &&
208227
ownerType == database.ownerType &&
209228
Objects.equals(comment, database.comment) &&
210-
Objects.equals(parameters, database.parameters);
229+
Objects.equals(parameters, database.parameters) &&
230+
Objects.equals(catalogName, database.catalogName);
211231
}
212232

213233
@Override
214234
public int hashCode()
215235
{
216-
return Objects.hash(databaseName, location, ownerName, ownerType, comment, parameters);
236+
return Objects.hash(databaseName, location, ownerName, ownerType, comment, parameters, catalogName);
217237
}
218238
}

0 commit comments

Comments
 (0)