Skip to content

Commit 4b1da60

Browse files
committed
chore: hms catalog create table should respect default location in from_table_creation
1 parent 21f7e24 commit 4b1da60

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

crates/catalog/hms/src/catalog.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,11 @@ impl Catalog for HmsCatalog {
154154
///
155155
/// This function can return an error in the following situations:
156156
///
157-
/// - If `hive.metastore.database.owner-type` is specified without
157+
/// - If `hive.metastore.database.owner-type` is specified without
158158
/// `hive.metastore.database.owner`,
159159
/// - Errors from `validate_namespace` if the namespace identifier does not
160160
/// meet validation criteria.
161-
/// - Errors from `convert_to_database` if the properties cannot be
161+
/// - Errors from `convert_to_database` if the properties cannot be
162162
/// successfully converted into a database configuration.
163163
/// - Errors from the underlying database creation process, converted using
164164
/// `from_thrift_error`.
@@ -238,7 +238,7 @@ impl Catalog for HmsCatalog {
238238
/// Asynchronously updates properties of an existing namespace.
239239
///
240240
/// Converts the given namespace identifier and properties into a database
241-
/// representation and then attempts to update the corresponding namespace
241+
/// representation and then attempts to update the corresponding namespace
242242
/// in the Hive Metastore.
243243
///
244244
/// # Returns
@@ -276,7 +276,7 @@ impl Catalog for HmsCatalog {
276276
/// # Returns
277277
/// A `Result<()>` indicating the outcome:
278278
/// - `Ok(())` signifies successful namespace deletion.
279-
/// - `Err(...)` signifies failure to drop the namespace due to validation
279+
/// - `Err(...)` signifies failure to drop the namespace due to validation
280280
/// errors, connectivity issues, or Hive Metastore constraints.
281281
async fn drop_namespace(&self, namespace: &NamespaceIdent) -> Result<()> {
282282
let name = validate_namespace(namespace)?;
@@ -297,7 +297,7 @@ impl Catalog for HmsCatalog {
297297
/// A `Result<Vec<TableIdent>>`, which is:
298298
/// - `Ok(vec![...])` containing a vector of `TableIdent` instances, each
299299
/// representing a table within the specified namespace.
300-
/// - `Err(...)` if an error occurs during namespace validation or while
300+
/// - `Err(...)` if an error occurs during namespace validation or while
301301
/// querying the database.
302302
async fn list_tables(&self, namespace: &NamespaceIdent) -> Result<Vec<TableIdent>> {
303303
let name = validate_namespace(namespace)?;
@@ -333,22 +333,25 @@ impl Catalog for HmsCatalog {
333333
async fn create_table(
334334
&self,
335335
namespace: &NamespaceIdent,
336-
creation: TableCreation,
336+
mut creation: TableCreation,
337337
) -> Result<Table> {
338338
let db_name = validate_namespace(namespace)?;
339339
let table_name = creation.name.clone();
340340

341-
let location = match &creation.location {
342-
Some(location) => location.clone(),
343-
None => {
344-
let ns = self.get_namespace(namespace).await?;
345-
get_default_table_location(&ns, &table_name, &self.config.warehouse)
346-
}
347-
};
341+
if creation.location.is_none() {
342+
let ns = self.get_namespace(namespace).await?;
343+
creation.location = Some(get_default_table_location(
344+
&ns,
345+
&table_name,
346+
&self.config.warehouse,
347+
));
348+
}
348349

350+
let location = creation.location.clone().unwrap();
349351
let metadata = TableMetadataBuilder::from_table_creation(creation)?
350352
.build()?
351353
.metadata;
354+
352355
let metadata_location = create_metadata_location(&location, 0)?;
353356

354357
self.file_io

crates/catalog/hms/tests/hms_catalog_test.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ async fn set_test_namespace(catalog: &HmsCatalog, namespace: &NamespaceIdent) ->
101101
Ok(())
102102
}
103103

104-
fn set_table_creation(location: impl ToString, name: impl ToString) -> Result<TableCreation> {
104+
fn set_table_creation(name: impl ToString) -> Result<TableCreation> {
105105
let schema = Schema::builder()
106106
.with_schema_id(0)
107107
.with_fields(vec![
@@ -111,7 +111,6 @@ fn set_table_creation(location: impl ToString, name: impl ToString) -> Result<Ta
111111
.build()?;
112112

113113
let creation = TableCreation::builder()
114-
.location(location.to_string())
115114
.name(name.to_string())
116115
.properties(HashMap::new())
117116
.schema(schema)
@@ -123,7 +122,7 @@ fn set_table_creation(location: impl ToString, name: impl ToString) -> Result<Ta
123122
#[tokio::test]
124123
async fn test_rename_table() -> Result<()> {
125124
let catalog = get_catalog().await;
126-
let creation: TableCreation = set_table_creation("s3a://warehouse/hive", "my_table")?;
125+
let creation: TableCreation = set_table_creation("my_table")?;
127126
let namespace = Namespace::new(NamespaceIdent::new("test_rename_table".into()));
128127
set_test_namespace(&catalog, namespace.name()).await?;
129128

@@ -143,7 +142,7 @@ async fn test_rename_table() -> Result<()> {
143142
#[tokio::test]
144143
async fn test_table_exists() -> Result<()> {
145144
let catalog = get_catalog().await;
146-
let creation = set_table_creation("s3a://warehouse/hive", "my_table")?;
145+
let creation = set_table_creation("my_table")?;
147146
let namespace = Namespace::new(NamespaceIdent::new("test_table_exists".into()));
148147
set_test_namespace(&catalog, namespace.name()).await?;
149148

@@ -159,7 +158,7 @@ async fn test_table_exists() -> Result<()> {
159158
#[tokio::test]
160159
async fn test_drop_table() -> Result<()> {
161160
let catalog = get_catalog().await;
162-
let creation = set_table_creation("s3a://warehouse/hive", "my_table")?;
161+
let creation = set_table_creation("my_table")?;
163162
let namespace = Namespace::new(NamespaceIdent::new("test_drop_table".into()));
164163
set_test_namespace(&catalog, namespace.name()).await?;
165164

@@ -177,7 +176,7 @@ async fn test_drop_table() -> Result<()> {
177176
#[tokio::test]
178177
async fn test_load_table() -> Result<()> {
179178
let catalog = get_catalog().await;
180-
let creation = set_table_creation("s3a://warehouse/hive", "my_table")?;
179+
let creation = set_table_creation("my_table")?;
181180
let namespace = Namespace::new(NamespaceIdent::new("test_load_table".into()));
182181
set_test_namespace(&catalog, namespace.name()).await?;
183182

@@ -200,7 +199,9 @@ async fn test_load_table() -> Result<()> {
200199
#[tokio::test]
201200
async fn test_create_table() -> Result<()> {
202201
let catalog = get_catalog().await;
203-
let creation = set_table_creation("s3a://warehouse/hive", "my_table")?;
202+
let mut creation = set_table_creation("my_table")?;
203+
// inject custom location, ignore the namespace prefix
204+
creation.location = Some("s3a://warehouse/hive".to_string());
204205
let namespace = Namespace::new(NamespaceIdent::new("test_create_table".into()));
205206
set_test_namespace(&catalog, namespace.name()).await?;
206207

@@ -229,7 +230,7 @@ async fn test_list_tables() -> Result<()> {
229230

230231
assert_eq!(result, vec![]);
231232

232-
let creation = set_table_creation("s3a://warehouse/hive", "my_table")?;
233+
let creation = set_table_creation("my_table")?;
233234
catalog.create_table(ns.name(), creation).await?;
234235
let result = catalog.list_tables(ns.name()).await?;
235236

0 commit comments

Comments
 (0)