This is an automated email from the ASF dual-hosted git repository.
JingsongLi pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/paimon-rust.git
The following commit(s) were added to refs/heads/main by this push:
new a8a875b2 [core] Reject reserved names for new catalog tables (#1533)
a8a875b2 is described below
commit a8a875b29f4f4a79bf7e63e423dc00d12c60ba11
Author: Jingsong Lee <[email protected]>
AuthorDate: Tue Nov 24 16:19:24 2026 +0800
[core] Reject reserved names for new catalog tables (#1533)
---
.../datafusion/tests/table_type_routing.rs | 16 ++++-
crates/paimon/src/catalog/filesystem.rs | 83 ++++++++++++++++++++++
crates/paimon/src/catalog/mod.rs | 18 ++++-
crates/paimon/src/catalog/rest/rest_catalog.rs | 49 +++++++++++++
4 files changed, 163 insertions(+), 3 deletions(-)
diff --git a/crates/integrations/datafusion/tests/table_type_routing.rs
b/crates/integrations/datafusion/tests/table_type_routing.rs
index 45e3c27f..5ac619f5 100644
--- a/crates/integrations/datafusion/tests/table_type_routing.rs
+++ b/crates/integrations/datafusion/tests/table_type_routing.rs
@@ -1577,12 +1577,26 @@ async fn
metadata_replace_view_preserves_physical_name_collisions() {
async fn assert_metadata_ddl_preserves_physical_table(name: &str, ddl: &str) {
let env =
setup_with_engine(Some(Arc::new(MetadataEngineResolver::default()))).await;
- env.ctx
+ let error = env
+ .ctx
.sql(&format!(
"CREATE TABLE {CATALOG}.{DB}.\"{name}\" (id INT NOT NULL)"
))
.await
+ .unwrap_err();
+ assert!(error
+ .to_string()
+ .contains("reserved branch or system table suffix"));
+
+ // An older writer could leave a physical table at a reserved name.
+ env.ctx
+ .sql(&format!(
+ "CREATE TABLE {CATALOG}.{DB}.legacy_table (id INT NOT NULL)"
+ ))
+ .await
.unwrap();
+ let database_path = env._paimon_dir.path().join(format!("{DB}.db"));
+ std::fs::rename(database_path.join("legacy_table"),
database_path.join(name)).unwrap();
let identifier = Identifier::new(DB, name);
let table = env.catalog.get_table(&identifier).await.unwrap();
let batch = RecordBatch::try_new(
diff --git a/crates/paimon/src/catalog/filesystem.rs
b/crates/paimon/src/catalog/filesystem.rs
index 826d341b..2170d4f4 100644
--- a/crates/paimon/src/catalog/filesystem.rs
+++ b/crates/paimon/src/catalog/filesystem.rs
@@ -441,6 +441,7 @@ impl Catalog for FileSystemCatalog {
ignore_if_exists: bool,
) -> Result<()> {
identifier.validate()?;
+ identifier.validate_new_table_name()?;
// Never persist a type nothing can load.
let declared = CoreOptions::new(creation.options()).table_type()?;
@@ -504,6 +505,7 @@ impl Catalog for FileSystemCatalog {
) -> Result<()> {
from.validate()?;
to.validate()?;
+ to.validate_new_table_name()?;
let from_path = self.table_path(from);
let to_path = self.table_path(to);
@@ -2177,6 +2179,87 @@ mod tests {
assert!(catalog.get_table(&source).await.is_ok());
}
+ #[tokio::test]
+ async fn test_create_table_rejects_reserved_table_names() {
+ let (_temp_dir, catalog) = create_test_catalog();
+ catalog
+ .create_database("db1", false, HashMap::new())
+ .await
+ .unwrap();
+
+ for name in [
+ "events$branch_dev",
+ "events$branch_main",
+ "events$snapshots",
+ "events$branch_dev$snapshots",
+ "events$malformed$snapshots",
+ ] {
+ let identifier = Identifier::new("db1", name);
+ let result = catalog
+ .create_table(&identifier, testing_schema(), true)
+ .await;
+ assert!(
+ matches!(result, Err(Error::IdentifierInvalid { .. })),
+ "{result:?}"
+ );
+ assert!(!catalog.table_exists(&identifier).await.unwrap());
+ }
+ }
+
+ #[tokio::test]
+ async fn test_rename_table_rejects_reserved_table_names_at_destination() {
+ let (_temp_dir, catalog) = create_test_catalog();
+ catalog
+ .create_database("db1", false, HashMap::new())
+ .await
+ .unwrap();
+ let source = Identifier::new("db1", "events");
+ catalog
+ .create_table(&source, testing_schema(), false)
+ .await
+ .unwrap();
+
+ for name in ["events$branch_dev", "events$snapshots"] {
+ let destination = Identifier::new("db1", name);
+ let result = catalog.rename_table(&source, &destination,
true).await;
+ assert!(
+ matches!(result, Err(Error::IdentifierInvalid { .. })),
+ "{result:?}"
+ );
+ assert!(!catalog.table_exists(&destination).await.unwrap());
+ assert!(catalog.table_exists(&source).await.unwrap());
+ }
+ }
+
+ #[tokio::test]
+ async fn test_legacy_reserved_table_names_can_be_renamed_or_dropped() {
+ let (_temp_dir, catalog) = create_test_catalog();
+ catalog
+ .create_database("db1", false, HashMap::new())
+ .await
+ .unwrap();
+ let rename_source = Identifier::new("db1", "events$branch_dev");
+ let drop_source = Identifier::new("db1", "events$snapshots");
+ let schema = TableSchema::new(0, &testing_schema());
+ for identifier in [&rename_source, &drop_source] {
+ catalog
+ .save_table_schema(&catalog.table_path(identifier), &schema)
+ .await
+ .unwrap();
+ }
+
+ let destination = Identifier::new("db1", "events_from_legacy");
+ catalog
+ .rename_table(&rename_source, &destination, false)
+ .await
+ .unwrap();
+ catalog.drop_table(&drop_source, false).await.unwrap();
+ assert!(catalog.get_table(&destination).await.is_ok());
+ for identifier in [&rename_source, &drop_source] {
+ assert!(!catalog.table_exists(identifier).await.unwrap());
+ }
+ }
+
#[tokio::test]
async fn test_list_partitions_default_table_not_found_errors() {
let (_temp_dir, catalog) = create_test_catalog();
diff --git a/crates/paimon/src/catalog/mod.rs b/crates/paimon/src/catalog/mod.rs
index 72c936e3..9ac428c2 100644
--- a/crates/paimon/src/catalog/mod.rs
+++ b/crates/paimon/src/catalog/mod.rs
@@ -129,6 +129,20 @@ impl Identifier {
validate_identifier_name("object", name)
}
+ /// A new physical table must be addressable without branch or system
table syntax.
+ pub(crate) fn validate_new_table_name(&self) -> Result<()> {
+ let name = self.parsed_object_name()?;
+ if name.branch().is_some() || name.system_table().is_some() {
+ return Err(Error::IdentifierInvalid {
+ message: format!(
+ "Table name '{}' uses a reserved branch or system table
suffix",
+ self.full_name()
+ ),
+ });
+ }
+ Ok(())
+ }
+
/// Database name.
pub fn database(&self) -> &str {
&self.database
@@ -466,7 +480,7 @@ pub trait Catalog: Send + Sync {
/// * [`crate::Error::DatabaseNotExist`] - database does not exist.
async fn list_tables(&self, database_name: &str) -> Result<Vec<String>>;
- /// Create a table.
+ /// Create a physical table with a name that has no branch or system table
suffix.
///
/// * `ignore_if_exists` - if true, do nothing when the table already
exists;
/// if false, return [`crate::Error::TableAlreadyExist`].
@@ -487,7 +501,7 @@ pub trait Catalog: Send + Sync {
/// * [`crate::Error::TableNotExist`] - table does not exist when
`ignore_if_not_exists` is false.
async fn drop_table(&self, identifier: &Identifier, ignore_if_not_exists:
bool) -> Result<()>;
- /// Rename a table.
+ /// Rename a table to a name that has no branch or system table suffix.
///
/// # Errors
/// * [`crate::Error::TableNotExist`] - source table does not exist when
`ignore_if_not_exists` is false.
diff --git a/crates/paimon/src/catalog/rest/rest_catalog.rs
b/crates/paimon/src/catalog/rest/rest_catalog.rs
index 568a44bc..8ed909fc 100644
--- a/crates/paimon/src/catalog/rest/rest_catalog.rs
+++ b/crates/paimon/src/catalog/rest/rest_catalog.rs
@@ -347,6 +347,7 @@ impl Catalog for RESTCatalog {
creation: Schema,
ignore_if_exists: bool,
) -> Result<()> {
+ identifier.validate_new_table_name()?;
let request = match RESTApi::prepare_create_table(identifier,
creation) {
Ok(request) => request,
Err(error) if ignore_if_exists => {
@@ -412,6 +413,7 @@ impl Catalog for RESTCatalog {
to: &Identifier,
ignore_if_not_exists: bool,
) -> Result<()> {
+ to.validate_new_table_name()?;
let result = self
.api
.rename_table(from, to)
@@ -1657,4 +1659,51 @@ mod tests {
Error::TableNotExist { .. }
));
}
+
+ #[tokio::test]
+ async fn test_rest_rejects_reserved_table_names_before_sending_requests() {
+ use std::sync::atomic::{AtomicUsize, Ordering};
+
+ use crate::spec::{DataType, IntType};
+ use axum::{http::StatusCode, Router};
+
+ let requests = Arc::new(AtomicUsize::new(0));
+ let captured = Arc::clone(&requests);
+ let app = Router::new().fallback(move || async move {
+ captured.fetch_add(1, Ordering::Relaxed);
+ StatusCode::BAD_REQUEST
+ });
+ let listener =
tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let address = listener.local_addr().unwrap();
+ let server = tokio::spawn(async move { axum::serve(listener,
app).await.unwrap() });
+ let mut options = Options::new();
+ options.set(CatalogOptions::URI, format!("http://{address}"));
+ options.set(CatalogOptions::WAREHOUSE, "test-warehouse");
+ options.set(CatalogOptions::TOKEN_PROVIDER, "bear");
+ options.set(CatalogOptions::TOKEN, "test-token");
+ let catalog = RESTCatalog::new(options, false).await.unwrap();
+ let schema = Schema::builder()
+ .column("id", DataType::Int(IntType::new()))
+ .build()
+ .unwrap();
+ let source = Identifier::new("default", "events");
+
+ for name in ["events$branch_dev", "events$snapshots"] {
+ let destination = Identifier::new("default", name);
+ let creation = catalog
+ .create_table(&destination, schema.clone(), true)
+ .await;
+ let rename = catalog.rename_table(&source, &destination,
true).await;
+ assert!(
+ matches!(creation, Err(Error::IdentifierInvalid { .. })),
+ "{creation:?}"
+ );
+ assert!(
+ matches!(rename, Err(Error::IdentifierInvalid { .. })),
+ "{rename:?}"
+ );
+ }
+ assert_eq!(requests.load(Ordering::Relaxed), 0);
+ server.abort();
+ }
}