Skip to content

Commit 55a4f67

Browse files
authored
Merge pull request #974 from Altinity/backport/antalya-25.6.5/82304
Antalya 25.6: backport of ClickHouse#82304 - Better exception for non existing tables for data lakes
2 parents 0ff5e8a + 1b0c0ae commit 55a4f67

File tree

4 files changed

+122
-10
lines changed

4 files changed

+122
-10
lines changed

src/Databases/DataLake/DatabaseDataLake.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ namespace ErrorCodes
6868
extern const int BAD_ARGUMENTS;
6969
extern const int SUPPORT_IS_DISABLED;
7070
extern const int DATALAKE_DATABASE_ERROR;
71+
extern const int CANNOT_GET_CREATE_TABLE_QUERY;
7172
}
7273

7374
namespace
@@ -528,7 +529,12 @@ ASTPtr DatabaseDataLake::getCreateTableQueryImpl(
528529
auto table_metadata = DataLake::TableMetadata().withLocation().withSchema();
529530

530531
const auto [namespace_name, table_name] = parseTableName(name);
531-
catalog->getTableMetadata(namespace_name, table_name, table_metadata);
532+
533+
if (!catalog->tryGetTableMetadata(namespace_name, table_name, table_metadata))
534+
{
535+
throw Exception(
536+
ErrorCodes::CANNOT_GET_CREATE_TABLE_QUERY, "Table `{}` doesn't exist", name);
537+
}
532538

533539
auto create_table_query = std::make_shared<ASTCreateQuery>();
534540
auto table_storage_define = table_engine_definition->clone();

src/Databases/DataLake/GlueCatalog.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -242,15 +242,6 @@ bool GlueCatalog::tryGetTableMetadata(
242242
const std::string & database_name,
243243
const std::string & table_name,
244244
TableMetadata & result) const
245-
{
246-
getTableMetadata(database_name, table_name, result);
247-
return true;
248-
}
249-
250-
void GlueCatalog::getTableMetadata(
251-
const std::string & database_name,
252-
const std::string & table_name,
253-
TableMetadata & result) const
254245
{
255246
Aws::Glue::Model::GetTableRequest request;
256247
request.SetDatabaseName(database_name);
@@ -320,11 +311,30 @@ void GlueCatalog::getTableMetadata(
320311
}
321312
else
322313
{
314+
if (outcome.GetError().GetErrorType() == Aws::Glue::GlueErrors::ENTITY_NOT_FOUND)
315+
return false; // Table does not exist
316+
323317
throw DB::Exception(
324318
DB::ErrorCodes::DATALAKE_DATABASE_ERROR,
325319
"Exception calling GetTable for table {}: {}",
326320
database_name + "." + table_name, outcome.GetError().GetMessage());
327321
}
322+
323+
return true;
324+
}
325+
326+
void GlueCatalog::getTableMetadata(
327+
const std::string & database_name,
328+
const std::string & table_name,
329+
TableMetadata & result) const
330+
{
331+
if (!tryGetTableMetadata(database_name, table_name, result))
332+
{
333+
throw DB::Exception(
334+
DB::ErrorCodes::DATALAKE_DATABASE_ERROR,
335+
"Table {} does not exist in Glue catalog",
336+
database_name + "." + table_name);
337+
}
328338
}
329339

330340
void GlueCatalog::setCredentials(TableMetadata & metadata) const

tests/integration/test_database_glue/test.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,3 +385,47 @@ def test_select_after_rename(started_cluster):
385385
update.rename_column("bid", "new_bid")
386386

387387
print(node.query(f"SELECT * FROM {CATALOG_NAME}.`{namespace}.{table_name}`"))
388+
389+
390+
def test_non_existing_tables(started_cluster):
391+
node = started_cluster.instances["node1"]
392+
393+
test_ref = f"test_non_existing_tables_{uuid.uuid4()}"
394+
table_name = f"{test_ref}_table"
395+
root_namespace = f"{test_ref}_namespace"
396+
397+
namespaces_to_create = [
398+
root_namespace,
399+
f"{root_namespace}_A",
400+
]
401+
402+
catalog = load_catalog_impl(started_cluster)
403+
404+
for namespace in namespaces_to_create:
405+
catalog.create_namespace(namespace)
406+
407+
for namespace in namespaces_to_create:
408+
table = create_table(catalog, namespace, table_name)
409+
410+
num_rows = 10
411+
df = generate_arrow_data(num_rows)
412+
table.append(df)
413+
414+
create_clickhouse_glue_database(started_cluster, node, CATALOG_NAME)
415+
416+
expected = DEFAULT_CREATE_TABLE.format(CATALOG_NAME, namespace, table_name)
417+
assert expected == node.query(
418+
f"SHOW CREATE TABLE {CATALOG_NAME}.`{namespace}.{table_name}`"
419+
)
420+
421+
try:
422+
node.query(f"SHOW CREATE TABLE {CATALOG_NAME}.`{namespace}.wrong_table_name`")
423+
except Exception as e:
424+
assert "DB::Exception: Table" in str(e)
425+
assert "doesn't exist" in str(e)
426+
427+
try:
428+
node.query(f"SHOW CREATE TABLE {CATALOG_NAME}.`fake_namespace.wrong_table_name`")
429+
except Exception as e:
430+
assert "DB::Exception: Table" in str(e)
431+
assert "doesn't exist" in str(e)

tests/integration/test_database_iceberg/test.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,3 +373,55 @@ def record(key):
373373

374374
assert 'aaa\naaa\naaa' == node.query(f"SELECT symbol FROM {CATALOG_NAME}.`{namespace}.{table_name}`").strip()
375375
assert 'bbb\nbbb\nbbb' == node.query(f"SELECT symbol FROM {CATALOG_NAME}.`{namespace}.{table_name_2}`").strip()
376+
377+
378+
def test_non_existing_tables(started_cluster):
379+
node = started_cluster.instances["node1"]
380+
381+
test_ref = f"test_list_tables_{uuid.uuid4()}"
382+
table_name = f"{test_ref}_table"
383+
root_namespace = f"{test_ref}_namespace"
384+
385+
namespace = f"{root_namespace}.A.B.C"
386+
namespaces_to_create = [
387+
root_namespace,
388+
f"{root_namespace}.A",
389+
f"{root_namespace}.A.B",
390+
f"{root_namespace}.A.B.C",
391+
]
392+
393+
catalog = load_catalog_impl(started_cluster)
394+
395+
for namespace in namespaces_to_create:
396+
catalog.create_namespace(namespace)
397+
assert len(catalog.list_tables(namespace)) == 0
398+
399+
table = create_table(catalog, namespace, table_name)
400+
401+
num_rows = 10
402+
data = [generate_record() for _ in range(num_rows)]
403+
df = pa.Table.from_pylist(data)
404+
table.append(df)
405+
406+
create_clickhouse_iceberg_database(started_cluster, node, CATALOG_NAME)
407+
408+
expected = DEFAULT_CREATE_TABLE.format(CATALOG_NAME, namespace, table_name)
409+
assert expected == node.query(
410+
f"SHOW CREATE TABLE {CATALOG_NAME}.`{namespace}.{table_name}`"
411+
)
412+
413+
try:
414+
node.query(
415+
f"SHOW CREATE TABLE {CATALOG_NAME}.`{namespace}.qweqwe`"
416+
)
417+
except Exception as e:
418+
assert "DB::Exception: Table" in str(e)
419+
assert "doesn't exist" in str(e)
420+
421+
try:
422+
node.query(
423+
f"SHOW CREATE TABLE {CATALOG_NAME}.`qweqwe.qweqwe`"
424+
)
425+
except Exception as e:
426+
assert "DB::Exception: Table" in str(e)
427+
assert "doesn't exist" in str(e)

0 commit comments

Comments
 (0)