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 fa480269 fix(datafusion): reject writes to shadowed persistent tables
(#1373)
fa480269 is described below
commit fa480269efb14243140c4edcb1d6eef746a2edfb
Author: slfan1989 <[email protected]>
AuthorDate: Mon Nov 16 13:25:32 2026 +0800
fix(datafusion): reject writes to shadowed persistent tables (#1373)
---
.../datafusion/src/format_partition_analyze.rs | 2 +
.../datafusion/src/format_partition_repair.rs | 2 +
crates/integrations/datafusion/src/sql_context.rs | 24 +++++--
.../datafusion/tests/rest_format_partition_sql.rs | 45 ++++++++++++
.../datafusion/tests/sql_context_tests.rs | 80 ++++++++++++++++++++++
5 files changed, 148 insertions(+), 5 deletions(-)
diff --git a/crates/integrations/datafusion/src/format_partition_analyze.rs
b/crates/integrations/datafusion/src/format_partition_analyze.rs
index a3277d17..01946d70 100644
--- a/crates/integrations/datafusion/src/format_partition_analyze.rs
+++ b/crates/integrations/datafusion/src/format_partition_analyze.rs
@@ -58,6 +58,8 @@ pub(crate) async fn execute_analyze(
));
}
SQLContext::ensure_partition_command_target(table_name, "ANALYZE TABLE")?;
+ ctx.reject_temporary_write_target(table_name, "ANALYZE TABLE")
+ .await?;
let (catalog, _catalog_name, identifier) =
ctx.resolve_catalog_and_table(table_name)?;
let table = catalog
.get_table(&identifier)
diff --git a/crates/integrations/datafusion/src/format_partition_repair.rs
b/crates/integrations/datafusion/src/format_partition_repair.rs
index 7b097670..1f018b38 100644
--- a/crates/integrations/datafusion/src/format_partition_repair.rs
+++ b/crates/integrations/datafusion/src/format_partition_repair.rs
@@ -37,6 +37,8 @@ pub(crate) async fn execute_msck(ctx: &SQLContext, msck:
&Msck) -> DFResult<Data
));
}
SQLContext::ensure_partition_command_target(&msck.table_name, "MSCK REPAIR
TABLE")?;
+ ctx.reject_temporary_write_target(&msck.table_name, "MSCK REPAIR TABLE")
+ .await?;
let (catalog, _catalog_name, identifier) =
ctx.resolve_catalog_and_table(&msck.table_name)?;
let table = catalog
.get_table(&identifier)
diff --git a/crates/integrations/datafusion/src/sql_context.rs
b/crates/integrations/datafusion/src/sql_context.rs
index 96ad10bb..65c7cbcb 100644
--- a/crates/integrations/datafusion/src/sql_context.rs
+++ b/crates/integrations/datafusion/src/sql_context.rs
@@ -1479,6 +1479,21 @@ impl SQLContext {
.map(|temp| (catalog_name, database, table, temp)))
}
+ /// Reject writes that would bypass a temporary object and mutate the
+ /// persistent table it shadows.
+ pub(crate) async fn reject_temporary_write_target(
+ &self,
+ name: &ObjectName,
+ operation: &str,
+ ) -> DFResult<()> {
+ if self.temp_table_for(name).await?.is_some() {
+ return Err(DataFusionError::NotImplemented(format!(
+ "{operation} on temporary tables or views is not supported"
+ )));
+ }
+ Ok(())
+ }
+
fn show_create_result(
&self,
catalog_name: String,
@@ -1568,6 +1583,8 @@ impl SQLContext {
} else {
Self::ensure_main_branch_write_target(name, "ALTER TABLE")?;
}
+ self.reject_temporary_write_target(name, "ALTER TABLE")
+ .await?;
let identifier = self.resolve_table_name(name)?;
if operations.len() > 1
@@ -1658,11 +1675,6 @@ impl SQLContext {
options,
column_position,
} => {
- if self.temp_table_for(name).await?.is_some() {
- return Err(DataFusionError::NotImplemented(
- "MODIFY COLUMN on temporary tables or views is not
supported".into(),
- ));
- }
if column_position.is_some() {
return Err(DataFusionError::Plan(
"MODIFY COLUMN FIRST/AFTER is not
supported".into(),
@@ -1947,6 +1959,8 @@ impl SQLContext {
}
};
Self::ensure_main_branch_write_target(&table_name, "INSERT
OVERWRITE")?;
+ self.reject_temporary_write_target(&table_name, "INSERT OVERWRITE")
+ .await?;
let (catalog, _catalog_name, identifier) =
self.resolve_catalog_and_table(&table_name)?;
let table = catalog
.get_table(&identifier)
diff --git a/crates/integrations/datafusion/tests/rest_format_partition_sql.rs
b/crates/integrations/datafusion/tests/rest_format_partition_sql.rs
index 55a9c380..800d3265 100644
--- a/crates/integrations/datafusion/tests/rest_format_partition_sql.rs
+++ b/crates/integrations/datafusion/tests/rest_format_partition_sql.rs
@@ -226,6 +226,51 @@ async fn
test_partition_commands_update_rest_metadata_and_directories() {
assert!(!temp_dir.path().join("dt=a").exists());
}
+#[cfg(not(windows))]
+#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
+async fn test_partition_maintenance_rejects_shadowed_temporary_targets() {
+ let temp_dir = tempfile::tempdir().unwrap();
+ let (server, context) =
+ setup_rest_table(&temp_dir, format_table_schema(&[("dt",
varchar())])).await;
+ common::exec(
+ &context,
+ &format!("ALTER TABLE {TABLE_NAME} ADD PARTITION (dt = 'a')"),
+ )
+ .await;
+ write_ids_file(&temp_dir.path().join("dt=a/part-0.parquet"), &[1, 2]);
+ std::fs::create_dir_all(temp_dir.path().join("dt=discovered")).unwrap();
+ let before = partition_statistics(&server);
+ common::exec(
+ &context,
+ &format!("CREATE TEMPORARY VIEW {TABLE_NAME} AS SELECT 99 AS id"),
+ )
+ .await;
+
+ for (sql, operation) in [
+ (
+ format!("ALTER TABLE {TABLE_NAME} ADD PARTITION (dt = 'b')"),
+ "ALTER TABLE",
+ ),
+ (
+ format!("MSCK REPAIR TABLE {TABLE_NAME} ADD PARTITIONS"),
+ "MSCK REPAIR TABLE",
+ ),
+ (
+ format!("ANALYZE TABLE {TABLE_NAME} COMPUTE STATISTICS"),
+ "ANALYZE TABLE",
+ ),
+ ] {
+ common::assert_sql_error(
+ &context,
+ &sql,
+ &format!("{operation} on temporary tables or views is not
supported"),
+ )
+ .await;
+ }
+ assert_eq!(partition_statistics(&server), before);
+ assert!(!temp_dir.path().join("dt=b").exists());
+}
+
#[cfg(not(windows))]
#[tokio::test]
async fn test_partition_literals() {
diff --git a/crates/integrations/datafusion/tests/sql_context_tests.rs
b/crates/integrations/datafusion/tests/sql_context_tests.rs
index 48df7b1e..2e3d7fd0 100644
--- a/crates/integrations/datafusion/tests/sql_context_tests.rs
+++ b/crates/integrations/datafusion/tests/sql_context_tests.rs
@@ -1134,6 +1134,86 @@ async fn
test_truncate_rejects_shadowed_temporary_targets() {
.is_empty());
}
+#[tokio::test]
+async fn
test_alter_and_partition_overwrite_reject_shadowed_temporary_targets() {
+ let (_tmp, catalog) = create_test_env();
+ let ctx = create_sql_context(catalog).await;
+ ctx.sql(r#"CREATE TABLE default."PartitionTarget" (id INT, pt INT)
PARTITIONED BY (pt)"#)
+ .await
+ .unwrap();
+ ctx.sql(r#"INSERT INTO default."PartitionTarget" VALUES (1, 1), (2, 2)"#)
+ .await
+ .unwrap()
+ .collect()
+ .await
+ .unwrap();
+
+ for kind in ["TABLE", "VIEW"] {
+ ctx.sql(&format!(
+ r#"CREATE TEMPORARY {kind} default."PartitionTarget" AS SELECT
CAST(999 AS INT) AS id"#
+ ))
+ .await
+ .unwrap();
+ assert_eq!(
+ collect_ids(&ctx, r#"SELECT id FROM "PartitionTarget""#).await,
+ vec![999]
+ );
+ for (sql, operation) in [
+ (
+ r#"INSERT OVERWRITE paimon.default."PartitionTarget" PARTITION
(pt = 1) SELECT 8"#,
+ "INSERT OVERWRITE",
+ ),
+ (
+ r#"ALTER TABLE "PartitionTarget" DROP COLUMN id"#,
+ "ALTER TABLE",
+ ),
+ (
+ r#"ALTER TABLE "PartitionTarget" DROP PARTITION (pt = 1)"#,
+ "ALTER TABLE",
+ ),
+ (
+ r#"ALTER TABLE IF EXISTS default."PartitionTarget" DROP
PARTITION (pt = 1)"#,
+ "ALTER TABLE",
+ ),
+ (
+ r#"ALTER TABLE paimon.default."PartitionTarget" DROP IF EXISTS
PARTITION (pt = 1)"#,
+ "ALTER TABLE",
+ ),
+ ] {
+ assert_sql_error_contains(
+ &ctx,
+ sql,
+ &format!("{operation} on temporary tables or views is not
supported"),
+ )
+ .await;
+ }
+ ctx.sql(&format!(
+ r#"DROP TEMPORARY {kind} default."PartitionTarget""#
+ ))
+ .await
+ .unwrap();
+ assert_eq!(
+ collect_ids(&ctx, r#"SELECT id FROM "PartitionTarget" ORDER BY
id"#).await,
+ vec![1, 2]
+ );
+ }
+
+ ctx.sql(r#"INSERT OVERWRITE "PartitionTarget" PARTITION (pt = 1) SELECT
8"#)
+ .await
+ .unwrap();
+ assert_eq!(
+ collect_ids(&ctx, r#"SELECT id FROM "PartitionTarget" ORDER BY
id"#).await,
+ vec![2, 8]
+ );
+ ctx.sql(r#"ALTER TABLE "PartitionTarget" DROP PARTITION (pt = 1)"#)
+ .await
+ .unwrap();
+ assert_eq!(
+ collect_ids(&ctx, r#"SELECT id FROM "PartitionTarget""#).await,
+ vec![2]
+ );
+}
+
#[tokio::test]
async fn test_scoped_show_accepts_information_schema() {
let (_tmp, catalog) = create_test_env();