diff --git a/crates/dbx-sql-data/src/data_grid_sql.rs b/crates/dbx-sql-data/src/data_grid_sql.rs index c08f66b573..5fee22ef61 100644 --- a/crates/dbx-sql-data/src/data_grid_sql.rs +++ b/crates/dbx-sql-data/src/data_grid_sql.rs @@ -44,6 +44,11 @@ const DATA_GRID_COLUMN_DISTINCT_VALUES_MAX_LIMIT: usize = 1000; const KEYLESS_GUARD_COUNT_ALIAS: &str = "dbx_keyless_row_matches"; const KEYLESS_AMBIGUOUS_ROW_ERROR: &str = "Cannot safely update or delete this row: the table has no primary key, so the row is identified by matching every column value, and more than one row in the table matches that condition. Add a primary key or unique index, or make the rows distinguishable, before editing."; const KEYLESS_UNIDENTIFIABLE_ROW_ERROR: &str = "Cannot safely update or delete this row: the table has no primary key and none of the result columns map to a table column, so there is no condition that can target a single row. Add a primary key, or edit the table directly, before saving."; +/// Dameng rejects every comparison against its binary LOB types with +/// `Data type mismatch` (SQLSTATE 22000, code -6105), and spatial columns are +/// BLOB-backed and behave the same, so a keyless row predicate — which matches +/// every result column value — cannot carry one of them. +const DAMENG_KEYLESS_BINARY_LOB_ERROR: &str = "Cannot safely update or delete this row: the table has no primary key, and Dameng cannot compare binary LOB values, so Dbx cannot build a condition that targets it. Add a primary key or unique index, or exclude the binary LOB column from the query, before editing."; const MYSQL_DATA_GRID_BATCH_MAX_ROWS: usize = 500; const MYSQL_DATA_GRID_BATCH_TARGET_SQL_BYTES: usize = 256 * 1024; @@ -1215,6 +1220,9 @@ fn validate_data_grid_save(options: &DataGridSaveStatementOptions) -> Option Option { + if options.database_type != Some(DatabaseType::Dameng) + || !options.table_meta.primary_keys.is_empty() + || (options.dirty_rows.is_empty() && options.deleted_rows.is_empty()) + { + return None; + } + let save_columns = effective_columns(options); + let column_info = options.table_meta.columns.as_deref().unwrap_or(&[]); + let touched_row_indexes = + options.dirty_rows.iter().map(|(row_index, _)| *row_index).chain(options.deleted_rows.iter().copied()); + for row_index in touched_row_indexes { + let Some(row) = options.rows.get(row_index) else { + continue; + }; + let compares_binary_lob = save_columns.iter().enumerate().any(|(index, column)| { + let Some(column) = column.as_deref() else { + return false; + }; + if is_synthetic_row_id(options.database_type, Some(column)) || row.get(index).is_none_or(Value::is_null) { + return false; + } + column_info_for(column_info, column).is_some_and(|info| is_dameng_uncomparable_lob_type(&info.data_type)) + }); + if compares_binary_lob { + return Some(DAMENG_KEYLESS_BINARY_LOB_ERROR.to_string()); + } + } + None +} + /// Without a primary key a row can only be addressed by matching every column /// value, and `build_row_where` drops columns that have no source column of /// their own. When nothing is left, the generated predicate is empty and the @@ -3299,6 +3345,19 @@ fn is_oracle_lob_type(data_type: &str) -> bool { || lower.starts_with("character large object") } +/// Dameng binary LOB types have no comparison operator: `=`, `LIKE` and even +/// `col = col` all raise `Data type mismatch`, so they cannot be part of a +/// keyless row predicate. Spatial types are BLOB-backed and behave the same. +fn is_dameng_uncomparable_lob_type(data_type: &str) -> bool { + let lower = data_type.trim().trim_matches('"').to_ascii_lowercase(); + if lower.starts_with("binary large object") { + return true; + } + let head = lower.split(['(', ':', ' ']).next().unwrap_or(""); + let base = head.rsplit('.').next().unwrap_or(head).trim(); + matches!(base, "blob" | "image" | "longvarbinary" | "bfile" | "geometry") || base.ends_with("_geometry") +} + fn oracle_character_lob_constructor(data_type: &str) -> Option<&'static str> { let lower = data_type.trim().trim_matches('"').to_ascii_lowercase(); let base = lower.split(['(', ':', ' ']).next().unwrap_or(""); @@ -7754,6 +7813,149 @@ mod tests { assert!(result.rollback_statements.is_empty()); } + fn dameng_keyless_options( + table_columns: Vec, + columns: Vec, + row: Vec, + ) -> DataGridSaveStatementOptions { + DataGridSaveStatementOptions { + database_type: Some(DatabaseType::Dameng), + identifier_quote: None, + table_meta: DataGridTableMeta { + catalog: None, + database: None, + schema: Some("SYSDBA".to_string()), + table_name: "T8819".to_string(), + primary_keys: vec![], + columns: Some(table_columns), + }, + columns, + source_columns: None, + rows: vec![row], + dirty_rows: vec![(0, vec![(1, json!("b"))])], + deleted_rows: vec![], + new_rows: vec![], + include_database_name: false, + } + } + + #[test] + fn rejects_dameng_keyless_binary_lob_writes() { + // Regression test for https://github.com/t8y2/dbx/issues/8819: Dameng + // rejects `"B" = '0x0102'` with `Data type mismatch` (code -6105), so a + // keyless row predicate cannot carry a non-NULL binary LOB column. + let result = prepare_data_grid_save(dameng_keyless_options( + vec![ + column("GID", "INT", true, None), + column("NAME", "VARCHAR(10)", true, None), + column("B", "BLOB", true, None), + ], + vec!["GID".to_string(), "NAME".to_string(), "B".to_string()], + vec![json!(1), json!("a"), json!("0x0102")], + )); + + assert_eq!(result.validation_error.as_deref(), Some(DAMENG_KEYLESS_BINARY_LOB_ERROR)); + assert!(result.statements.is_empty()); + assert!(result.rollback_statements.is_empty()); + assert!(result.keyless_guards.is_empty()); + } + + #[test] + fn rejects_dameng_keyless_binary_lob_deletes() { + let mut options = dameng_keyless_options( + vec![column("GID", "INT", true, None), column("B", "BLOB", true, None)], + vec!["GID".to_string(), "B".to_string()], + vec![json!(1), json!("0x0102")], + ); + options.dirty_rows = vec![]; + options.deleted_rows = vec![0]; + + let result = prepare_data_grid_save(options); + + assert_eq!(result.validation_error.as_deref(), Some(DAMENG_KEYLESS_BINARY_LOB_ERROR)); + assert!(result.statements.is_empty()); + assert!(result.rollback_statements.is_empty()); + } + + #[test] + fn rejects_dameng_keyless_spatial_lob_writes() { + // The issue's column is a DMGEO2 spatial type, which Dbx receives as + // `SYSGEO2.ST_GEOMETRY(...)` text and which the server also refuses to + // compare. Spatial columns are BLOB-backed, so they are refused too. + let result = prepare_data_grid_save(dameng_keyless_options( + vec![ + column("GID", "INT", true, None), + column("NAME", "VARCHAR(10)", true, None), + column("GEOM", "SYSGEO2.ST_GEOMETRY", true, None), + ], + vec!["GID".to_string(), "NAME".to_string(), "GEOM".to_string()], + vec![json!(1), json!("a"), json!("SYSGEO2.ST_GEOMETRY(dm.jdbc.driver.DmdbBlob@5f0f259e)")], + )); + + assert_eq!(result.validation_error.as_deref(), Some(DAMENG_KEYLESS_BINARY_LOB_ERROR)); + assert!(result.statements.is_empty()); + } + + #[test] + fn allows_dameng_keyless_writes_when_comparable_values_remain() { + // Textual LOBs and NULL binary LOBs are comparable (`= 'text'`, + // `IS NULL`), so keyless editing keeps working and only the columns + // that really cannot be compared are refused. + let result = prepare_data_grid_save(dameng_keyless_options( + vec![ + column("GID", "INT", true, None), + column("NAME", "VARCHAR(10)", true, None), + column("BODY", "CLOB", true, None), + column("B", "BLOB", true, None), + ], + vec!["GID".to_string(), "NAME".to_string(), "BODY".to_string(), "B".to_string()], + vec![json!(1), json!("a"), json!("body"), Value::Null], + )); + + assert_eq!(result.validation_error, None); + assert_eq!( + result.statements, + vec![ + r#"UPDATE "SYSDBA"."T8819" SET "NAME" = 'b' WHERE "GID" = 1 AND "NAME" = 'a' AND "BODY" = 'body' AND "B" IS NULL;"# + ] + ); + assert_guards_cover_statement_predicates(&result); + } + + #[test] + fn allows_dameng_binary_lob_writes_with_a_primary_key() { + let mut options = dameng_keyless_options( + vec![ + column("GID", "INT", false, None), + column("NAME", "VARCHAR(10)", true, None), + column("B", "BLOB", true, None), + ], + vec!["GID".to_string(), "NAME".to_string(), "B".to_string()], + vec![json!(1), json!("a"), json!("0x0102")], + ); + options.table_meta.primary_keys = vec!["GID".to_string()]; + + assert_eq!(prepare_data_grid_save(options).validation_error, None); + } + + #[test] + fn allows_dameng_keyless_writes_that_do_not_load_the_binary_lob_column() { + // A binary LOB column outside the result set never reaches the row + // predicate, so it must not block the save. + let mut options = dameng_keyless_options( + vec![ + column("GID", "INT", true, None), + column("NAME", "VARCHAR(10)", true, None), + column("B", "BLOB", true, None), + ], + vec!["GID".to_string(), "NAME".to_string()], + vec![json!(1), json!("a")], + ); + options.dirty_rows = vec![(0, vec![(1, json!("b"))])]; + + assert_eq!(prepare_data_grid_save(options).validation_error, None); + } + fn daily_stats_keyless_options() -> DataGridSaveStatementOptions { DataGridSaveStatementOptions { database_type: Some(DatabaseType::Sqlite),