Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

feat(mysql): merge batched grid update keys into in clauses

Abeautifulsnow4a5d1c78c193
diff --git a/crates/dbx-sql-data/src/data_grid_sql.rs b/crates/dbx-sql-data/src/data_grid_sql.rs
index 0eba8a94b4..562df0a5b8 100644
--- a/crates/dbx-sql-data/src/data_grid_sql.rs
+++ b/crates/dbx-sql-data/src/data_grid_sql.rs
@@ -51,8 +51,23 @@ const KEYLESS_UNIDENTIFIABLE_ROW_ERROR: &str = "Cannot safely update or delete t
/// 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.";
+/// Row budget for one statement. It binds only the predicate shapes that cannot
+/// merge into an `IN` run (a compound key stays one OR-ed part per row) and the
+/// deleted-rows rollback INSERT value tuples; merged `IN` runs are bounded by
+/// `MYSQL_DATA_GRID_BATCH_MAX_VALUES` and by the text budget instead, so the
+/// three budgets each cover a different shape.
const MYSQL_DATA_GRID_BATCH_MAX_ROWS: usize = 500;
const MYSQL_DATA_GRID_BATCH_TARGET_SQL_BYTES: usize = 256 * 1024;
+/// Per-statement ceiling on the literal values a batched MySQL UPDATE/DELETE may
+/// carry. MySQL charges the range optimizer ≈230 bytes per `IN` literal (every
+/// `IN` value counts as a predicate combined with `OR`), so 10k values ≈ 2.3 MiB
+/// of the 8 MiB default `range_optimizer_max_mem_size` — about 3.6× headroom.
+/// The 256 KiB text budget alone is not enough: short integer keys are ≈7 bytes
+/// of text each, so it would let a statement reach ≈8.6 MiB and silently abandon
+/// the range scan (warning 3170, or a hard error under `sql_safe_updates`).
+/// Keeping 500 values per flush would instead re-join the runs into the same
+/// oversized statement, which is why the cap counts values rather than rows.
+const MYSQL_DATA_GRID_BATCH_MAX_VALUES: usize = 10_000;
const ORACLE_SQL_LITERAL_MAX_BYTES: usize = 4000;
const ORACLE_LOB_LITERAL_CHUNK_BYTES: usize = 3900;
/// SQL Server types an `N'..' + NCHAR(n) + N'..'` chain of non-`max` operands as
@@ -1747,7 +1762,7 @@ fn build_data_grid_save_statements(
if sets.is_empty() {
continue;
}
- let mut where_clause = build_primary_key_where(
+ let mut predicate = build_primary_key_predicate(
options.database_type,
&options.table_meta.primary_keys,
&save_columns,
@@ -1760,27 +1775,36 @@ fn build_data_grid_save_statements(
&save_columns,
row,
changes.iter().map(|(index, _)| *index),
- &mut where_clause,
+ &mut predicate.sql,
);
- guard_predicate(&where_clause, &mut guarded_predicates);
+ if guarded_lob {
+ // An appended `AND ...` guard makes the clause more than the plain
+ // equality it started as; never merge a guarded predicate.
+ predicate.plain = None;
+ }
+ guard_predicate(&predicate.sql, &mut guarded_predicates);
if batch_mysql_writes {
if update_sets.as_deref().is_some_and(|current| current != sets) {
let current_sets = update_sets.take().unwrap_or_default();
- push_mysql_predicate_batches(
+ push_mysql_key_predicate_batches(
&mut statements,
&format!("UPDATE {table} SET {current_sets} WHERE "),
std::mem::take(&mut update_predicates),
);
}
update_sets = Some(sets);
- update_predicates.push(where_clause);
+ update_predicates.push(predicate);
} else {
- let statement = data_grid_update_sql(options.database_type, &table, &sets, &where_clause);
+ let statement = data_grid_update_sql(options.database_type, &table, &sets, &predicate.sql);
statements.push(data_grid_save_statement(options.database_type, statement, guarded_lob, bound_statements));
}
}
if let Some(sets) = update_sets {
- push_mysql_predicate_batches(&mut statements, &format!("UPDATE {table} SET {sets} WHERE "), update_predicates);
+ push_mysql_key_predicate_batches(
+ &mut statements,
+ &format!("UPDATE {table} SET {sets} WHERE "),
+ update_predicates,
+ );
}
let mut delete_predicates = Vec::new();
@@ -1788,7 +1812,7 @@ fn build_data_grid_save_statements(
let Some(row) = options.rows.get(*row_index) else {
continue;
};
- let mut where_clause = build_primary_key_where(
+ let mut predicate = build_primary_key_predicate(
options.database_type,
&options.table_meta.primary_keys,
&save_columns,
@@ -1797,17 +1821,20 @@ fn build_data_grid_save_statements(
options.identifier_quote.as_deref(),
);
let guarded_lob =
- append_oceanbase_lob_save_guards(options, &save_columns, row, 0..save_columns.len(), &mut where_clause);
- guard_predicate(&where_clause, &mut guarded_predicates);
+ append_oceanbase_lob_save_guards(options, &save_columns, row, 0..save_columns.len(), &mut predicate.sql);
+ if guarded_lob {
+ predicate.plain = None;
+ }
+ guard_predicate(&predicate.sql, &mut guarded_predicates);
if batch_mysql_writes {
- delete_predicates.push(where_clause);
+ delete_predicates.push(predicate);
} else {
- let statement = data_grid_delete_sql(options.database_type, &table, &where_clause);
+ let statement = data_grid_delete_sql(options.database_type, &table, &predicate.sql);
statements.push(data_grid_save_statement(options.database_type, statement, guarded_lob, bound_statements));
}
}
if !delete_predicates.is_empty() {
- push_mysql_delete_batches(&mut statements, &format!("DELETE FROM {table} WHERE "), delete_predicates);
+ push_mysql_key_predicate_batches(&mut statements, &format!("DELETE FROM {table} WHERE "), delete_predicates);
}
for row in &options.new_rows {
@@ -2105,98 +2132,107 @@ fn supports_mysql_data_grid_batch(options: &DataGridSaveStatementOptions) -> boo
options.database_type == Some(DatabaseType::Mysql) && !options.table_meta.primary_keys.is_empty()
}
-fn push_mysql_predicate_batches(statements: &mut Vec<String>, prefix: &str, predicates: Vec<String>) {
- push_mysql_joined_batches(statements, prefix, predicates, " OR ", true);
+/// One row key predicate of a batched MySQL UPDATE/DELETE.
+struct MysqlKeyPredicate {
+ /// Exactly the text the statement carries when this predicate cannot be
+ /// merged — the same OR-form clause an unbatched statement would use.
+ sql: String,
+ /// Present only when `sql` is provably one plain non-NULL equality on a
+ /// single column, so it may be merged into an `IN` list.
+ plain: Option<MysqlPlainEquality>,
+}
+
+/// A provably plain `` `column` = literal `` key equality. Both pieces are
+/// already rendered — the dialect-quoted identifier and the formatted literal —
+/// and are accepted only after the clause they came from was checked to be
+/// exactly `"{column} = {literal}"` (`plain_equality_from_clause`).
+#[derive(Clone)]
+struct MysqlPlainEquality {
+ column: String,
+ literal: String,
}
-/// Render a batch of delete predicates for MySQL. Rows deleted through the grid
-/// usually differ only in a single primary-key equality predicate, so equal-key
-/// runs collapse into `id IN (...)` instead of a chain of ORs (#8857). Anything
-/// that is not a plain `column = literal` equality (compound keys, NULL checks,
-/// BINARY comparisons, ...) falls back to the OR form, which stays correct for
-/// every predicate shape.
-fn push_mysql_delete_batches(statements: &mut Vec<String>, prefix: &str, predicates: Vec<String>) {
+/// A rendered part of a batched MySQL statement, with the weight the
+/// per-statement value budget has to account for it.
+struct MysqlBatchPart {
+ sql: String,
+ /// Literal values this part contributes to the statement's value budget. An
+ /// `IN` run contributes one per literal, a standalone predicate one; an
+ /// INSERT value tuple contributes nothing because that budget exists only to
+ /// bound range-optimizer memory on key predicates.
+ values: usize,
+}
+
+/// Render the batched `WHERE` parts of a MySQL save. Runs of rows that differ
+/// only in a single primary-key value collapse into `` `id` IN (...) `` instead
+/// of a chain of ORs (#8857), which is the form a DBA is willing to run.
+/// Anything that is not provably one plain non-NULL `column = literal` equality
+/// — compound keys, `IS NULL`, BINARY/JSON comparisons, BLOB-guarded keys —
+/// falls back to its own text, which stays correct for every predicate shape.
+///
+/// The merge is streaming: a non-mergeable predicate flushes the open run in
+/// place, so the order the caller built the predicates in is preserved rather
+/// than regrouped (`SET A → SET B → SET A` stays three statements).
+///
+/// Whether a predicate is "plain" is decided by the code that rendered it
+/// (`build_primary_key_predicate`), and only after that rendering was checked to
+/// reproduce the clause character for character. That check is why a future
+/// change in `build_column_predicate` cannot silently widen a batch: a new
+/// wrapper or comparison stops matching and the predicate keeps its OR form.
+fn push_mysql_key_predicate_batches(statements: &mut Vec<String>, prefix: &str, predicates: Vec<MysqlKeyPredicate>) {
const IN_JOINER: &str = ", ";
struct InRun {
- column: Option<String>,
+ column: String,
values: Vec<String>,
bytes: usize,
}
- fn flush_in(run: &mut InRun, batches: &mut Vec<String>) {
- if let Some(column) = run.column.take() {
- if run.values.len() == 1 {
- batches.push(format!("{column} = {}", run.values[0]));
- } else {
- batches.push(format!("{column} IN ({})", run.values.join(IN_JOINER)));
- }
- run.values.clear();
- run.bytes = 0;
+ fn flush_run(run: &mut InRun, parts: &mut Vec<MysqlBatchPart>) {
+ if run.values.len() == 1 {
+ parts.push(MysqlBatchPart { sql: format!("{} = {}", run.column, run.values[0]), values: 1 });
+ } else {
+ parts.push(MysqlBatchPart {
+ sql: format!("{} IN ({})", run.column, run.values.join(IN_JOINER)),
+ values: run.values.len(),
+ });
}
+ run.values.clear();
+ run.bytes = 0;
}
- let mut in_run = InRun { column: None, values: Vec::new(), bytes: 0 };
- let mut in_batches: Vec<String> = Vec::new();
- let mut pending_batches: Vec<String> = Vec::new();
-
- for predicate in &predicates {
- match parse_mysql_equality_predicate(predicate) {
- Some((column, literal)) => {
- let literal_bytes = literal.len() + IN_JOINER.len();
- let column_switch = in_run.column.as_deref().is_some_and(|existing| existing != column);
- let over_budget = in_run.bytes + literal_bytes > MYSQL_DATA_GRID_BATCH_TARGET_SQL_BYTES;
- let over_rows = in_run.values.len() >= MYSQL_DATA_GRID_BATCH_MAX_ROWS;
- if column_switch || over_budget || over_rows {
- flush_in(&mut in_run, &mut in_batches);
- }
- if in_run.column.is_none() {
- in_run.column = Some(column);
- }
- in_run.bytes += literal_bytes;
- in_run.values.push(literal);
+ let mut parts: Vec<MysqlBatchPart> = Vec::new();
+ let mut run: Option<InRun> = None;
+ for predicate in predicates {
+ let Some(plain) = predicate.plain else {
+ if let Some(open) = run.as_mut() {
+ flush_run(open, &mut parts);
}
- None => {
- flush_in(&mut in_run, &mut in_batches);
- pending_batches.push(predicate.clone());
+ run = None;
+ parts.push(MysqlBatchPart { sql: predicate.sql, values: 1 });
+ continue;
+ };
+ let literal_bytes = plain.literal.len() + IN_JOINER.len();
+ let starts_new_run = run.as_ref().is_some_and(|open| {
+ open.column != plain.column
+ || open.bytes + literal_bytes > MYSQL_DATA_GRID_BATCH_TARGET_SQL_BYTES
+ || open.values.len() >= MYSQL_DATA_GRID_BATCH_MAX_VALUES
+ });
+ if starts_new_run {
+ if let Some(open) = run.as_mut() {
+ flush_run(open, &mut parts);
}
+ run = None;
}
+ let open = run.get_or_insert_with(|| InRun { column: plain.column, values: Vec::new(), bytes: 0 });
+ open.bytes += literal_bytes;
+ open.values.push(plain.literal);
}
- flush_in(&mut in_run, &mut in_batches);
-
- let mut batches = pending_batches;
- batches.extend(in_batches);
- if batches.len() == 1 {
- statements.push(format!("{prefix}{};", batches.remove(0)));
- } else if !batches.is_empty() {
- push_mysql_predicate_batches(statements, prefix, batches);
+ if let Some(open) = run.as_mut() {
+ flush_run(open, &mut parts);
}
-}
-/// Recognize predicates shaped exactly like `` `column` = literal `` (with or
-/// without backticks) so they can merge into an IN list. Anything else —
-/// compound-key AND chains, IS NULL, BINARY comparisons — returns None.
-fn parse_mysql_equality_predicate(predicate: &str) -> Option<(String, String)> {
- let mut trimmed = predicate;
- while let Some(inner) = trimmed.strip_prefix('(').and_then(|inner| inner.strip_suffix(')')) {
- trimmed = inner;
- }
- let separator = trimmed.find(" = ")?;
- let column = trimmed.get(..separator)?.trim().to_string();
- if !column.starts_with('`') || !column.ends_with('`') || column.len() < 3 {
- return None;
- }
- let literal = trimmed.get(separator + 3..)?.trim().to_string();
- if literal.is_empty() || literal.eq_ignore_ascii_case("null") {
- return None;
- }
- // Compound-key predicates contain further AND/OR/IS clauses after the first
- // equality; merging those into an IN list would change the row targeting.
- let upper = literal.to_ascii_uppercase();
- if upper.contains(" AND ") || upper.contains(" OR ") || upper.starts_with("IS ") {
- return None;
- }
- Some((column, literal))
+ push_mysql_joined_batches(statements, prefix, parts, " OR ", true, Some(MYSQL_DATA_GRID_BATCH_MAX_VALUES));
}
fn push_mysql_values_insert_batches(
@@ -2208,24 +2244,34 @@ fn push_mysql_values_insert_batches(
push_mysql_joined_batches(
statements,
&format!("INSERT INTO {table} ({columns}) VALUES "),
- value_tuples,
+ value_tuples.into_iter().map(|tuple| MysqlBatchPart { sql: tuple, values: 0 }).collect(),
", ",
false,
+ // Row values are not key predicates; the value budget does not apply.
+ None,
);
}
+/// Joins the parts of a MySQL statement, splitting whenever the open batch
+/// reaches the row budget, the SQL byte budget, or — when `max_values` is set —
+/// the per-statement value budget (see `MYSQL_DATA_GRID_BATCH_MAX_VALUES`).
fn push_mysql_joined_batches(
statements: &mut Vec<String>,
prefix: &str,
- parts: Vec<String>,
+ parts: Vec<MysqlBatchPart>,
separator: &str,
wrap_multiple_parts: bool,
+ max_values: Option<usize>,
) {
- let mut batch = Vec::new();
+ let mut batch: Vec<MysqlBatchPart> = Vec::new();
+ let mut batch_values = 0usize;
for part in parts {
let next_len = joined_batch_sql_len(prefix, &batch, &part, separator, wrap_multiple_parts);
+ let over_values = max_values.is_some_and(|max| batch_values + part.values > max);
if !batch.is_empty()
- && (batch.len() >= MYSQL_DATA_GRID_BATCH_MAX_ROWS || next_len > MYSQL_DATA_GRID_BATCH_TARGET_SQL_BYTES)
+ && (batch.len() >= MYSQL_DATA_GRID_BATCH_MAX_ROWS
+ || next_len > MYSQL_DATA_GRID_BATCH_TARGET_SQL_BYTES
+ || over_values)
{
push_mysql_joined_batch_statement(
statements,
@@ -2234,7 +2280,9 @@ fn push_mysql_joined_batches(
separator,
wrap_multiple_parts,
);
+ batch_values = 0;
}
+ batch_values += part.values;
batch.push(part);
}
if !batch.is_empty() {
@@ -2244,35 +2292,36 @@ fn push_mysql_joined_batches(
fn joined_batch_sql_len(
prefix: &str,
- current: &[String],
- next: &str,
+ current: &[MysqlBatchPart],
+ next: &MysqlBatchPart,
separator: &str,
wrap_multiple_parts: bool,
) -> usize {
let part_bytes = |part: &str| part.len() + usize::from(wrap_multiple_parts) * 2;
if current.is_empty() {
- return prefix.len() + next.len() + 1;
+ return prefix.len() + next.sql.len() + 1;
}
let current_bytes = if current.len() == 1 && wrap_multiple_parts {
- current[0].len()
+ current[0].sql.len()
} else {
- current.iter().map(|part| part_bytes(part)).sum::<usize>() + separator.len() * current.len().saturating_sub(1)
+ current.iter().map(|part| part_bytes(&part.sql)).sum::<usize>()
+ + separator.len() * current.len().saturating_sub(1)
};
let existing_adjustment = if current.len() == 1 && wrap_multiple_parts { 2 } else { 0 };
- prefix.len() + current_bytes + existing_adjustment + separator.len() + part_bytes(next) + 1
+ prefix.len() + current_bytes + existing_adjustment + separator.len() + part_bytes(&next.sql) + 1
}
fn push_mysql_joined_batch_statement(
statements: &mut Vec<String>,
prefix: &str,
- parts: Vec<String>,
+ parts: Vec<MysqlBatchPart>,
separator: &str,
wrap_multiple_parts: bool,
) {
let body = if parts.len() == 1 || !wrap_multiple_parts {
- parts.join(separator)
+ parts.into_iter().map(|part| part.sql).collect::<Vec<_>>().join(separator)
} else {
- parts.into_iter().map(|part| format!("({part})")).collect::<Vec<_>>().join(separator)
+ parts.into_iter().map(|part| format!("({})", part.sql)).collect::<Vec<_>>().join(separator)
};
statements.push(format!("{prefix}{body};"));
}
@@ -3448,26 +3497,81 @@ fn build_primary_key_where(
column_info: &[DataGridColumnInfo],
identifier_quote: Option<&str>,
) -> String {
+ build_primary_key_predicate(database_type, primary_keys, columns, row, column_info, identifier_quote).sql
+}
+
+/// Builds a row's key `WHERE` clause and, for a single-column key, the structured
+/// equality the MySQL batching may merge into an `IN` list. Both come out of the
+/// same rendering call, so the merge decision can never be made against a clause
+/// that differs from the one the statement carries.
+fn build_primary_key_predicate(
+ database_type: Option<DatabaseType>,
+ primary_keys: &[String],
+ columns: &[Option<String>],
+ row: &[Value],
+ column_info: &[DataGridColumnInfo],
+ identifier_quote: Option<&str>,
+) -> MysqlKeyPredicate {
if primary_keys.is_empty() && uses_keyless_row_predicate(database_type) {
- return build_row_where(database_type, columns, row, column_info, identifier_quote);
+ // A keyless row predicate matches every column value: never one plain
+ // equality, and never batched (`supports_mysql_data_grid_batch`).
+ return MysqlKeyPredicate {
+ sql: build_row_where(database_type, columns, row, column_info, identifier_quote),
+ plain: None,
+ };
}
- primary_keys
- .iter()
- .map(|primary_key| {
- let value = row
- .get(find_column_index(database_type, columns, primary_key).unwrap_or(usize::MAX))
- .unwrap_or(&Value::Null);
- build_column_predicate(
- database_type,
- primary_key,
- value,
- column_info_for(column_info, primary_key),
- false,
- identifier_quote,
- )
- })
- .collect::<Vec<_>>()
- .join(" AND ")
+
+ let single_key = primary_keys.len() == 1;
+ let mut plain = None;
+ let mut parts = Vec::with_capacity(primary_keys.len());
+ for primary_key in primary_keys {
+ let value = row
+ .get(find_column_index(database_type, columns, primary_key).unwrap_or(usize::MAX))
+ .unwrap_or(&Value::Null);
+ let (part, part_plain) = build_column_predicate_with_plain(
+ database_type,
+ primary_key,
+ value,
+ column_info_for(column_info, primary_key),
+ false,
+ identifier_quote,
+ );
+ parts.push(part);
+ if single_key {
+ plain = part_plain;
+ }
+ }
+ let sql = parts.join(" AND ");
+ let plain = plain.and_then(|plain| plain_equality_from_clause(&sql, plain));
+ MysqlKeyPredicate { sql, plain }
+}
+
+/// Accepts the structured equality only when re-rendering it as
+/// `{column} = {literal}` reproduces the clause about to be sent, modulo the
+/// outer parentheses a batched part may add. `build_column_predicate` stays the
+/// single source of truth for that text: if a future branch there renders another
+/// form (a new wrapper, a different comparison), this check fails and the
+/// predicate keeps the OR form instead of silently merging into an `IN` list that
+/// targets rows the clause alone would not.
+fn plain_equality_from_clause(clause: &str, plain: MysqlPlainEquality) -> Option<MysqlPlainEquality> {
+ // Keep the retired text parser's explicit rejection of a bare `NULL` literal.
+ // A NULL key value already leaves through the `IS NULL` branch, so this only
+ // guards a formatter change that would emit the keyword for a real value:
+ // `col = NULL` and `col IN (NULL)` both match nothing, but an `IN (NULL, ...)`
+ // list would still read as a bug in the SQL a DBA has to approve.
+ if plain.literal.eq_ignore_ascii_case("null") {
+ return None;
+ }
+ let expected = format!("{} = {}", plain.column, plain.literal);
+ (strip_outer_parentheses(clause) == expected).then_some(plain)
+}
+
+fn strip_outer_parentheses(text: &str) -> &str {
+ let mut trimmed = text.trim();
+ while let Some(inner) = trimmed.strip_prefix('(').and_then(|inner| inner.strip_suffix(')')) {
+ trimmed = inner;
+ }
+ trimmed
}
fn build_row_where(
@@ -3534,25 +3638,54 @@ pub fn build_column_predicate(
use_binary_text_comparison: bool,
identifier_quote: Option<&str>,
) -> String {
+ build_column_predicate_with_plain(
+ database_type,
+ column,
+ value,
+ column_info,
+ use_binary_text_comparison,
+ identifier_quote,
+ )
+ .0
+}
+
+/// Renders one key predicate and, alongside it, the structured
+/// `` (identifier, literal) `` pair when the rendering is the plain
+/// `column = literal` equality. The pair is a *claim* about the text returned
+/// here, not an independent derivation: callers must confirm it still reproduces
+/// that text (`plain_equality_from_clause`) before merging anything.
+fn build_column_predicate_with_plain(
+ database_type: Option<DatabaseType>,
+ column: &str,
+ value: &Value,
+ column_info: Option<&DataGridColumnInfo>,
+ use_binary_text_comparison: bool,
+ identifier_quote: Option<&str>,
+) -> (String, Option<MysqlPlainEquality>) {
let ident = predicate_ident(database_type, column, identifier_quote);
if value.is_null() {
- format!("{ident} IS NULL")
- } else if let Some(predicate) = oceanbase_lob_predicate(database_type, &ident, value, column_info) {
- predicate
- } else if use_binary_text_comparison && uses_mysql_binary_text_predicate(database_type, value, column_info) {
- format!(
- "BINARY {ident} = {}",
- format_grid_assignment_sql_literal(value, database_type, column_info, identifier_quote)
- )
- } else {
- let literal = format_grid_assignment_sql_literal(value, database_type, column_info, identifier_quote);
- if use_binary_text_comparison {
- if let Some(predicate) = postgres_keyless_json_predicate(database_type, &ident, &literal, column_info) {
- return predicate;
- }
+ return (format!("{ident} IS NULL"), None);
+ }
+ if let Some(predicate) = oceanbase_lob_predicate(database_type, &ident, value, column_info) {
+ return (predicate, None);
+ }
+ if use_binary_text_comparison && uses_mysql_binary_text_predicate(database_type, value, column_info) {
+ return (
+ format!(
+ "BINARY {ident} = {}",
+ format_grid_assignment_sql_literal(value, database_type, column_info, identifier_quote)
+ ),
+ None,
+ );
+ }
+ let literal = format_grid_assignment_sql_literal(value, database_type, column_info, identifier_quote);
+ if use_binary_text_comparison {
+ if let Some(predicate) = postgres_keyless_json_predicate(database_type, &ident, &literal, column_info) {
+ return (predicate, None);
}
- format!("{ident} = {}", mysql_json_predicate_literal(literal, database_type, column_info))
}
+ let sql = format!("{ident} = {}", mysql_json_predicate_literal(literal.clone(), database_type, column_info));
+ (sql, Some(MysqlPlainEquality { column: ident, literal }))
}
fn build_save_column_predicate(
@@ -9924,6 +10057,15 @@ mod tests {
);
}
+ /// A predicate the save already proved plain: the same `(column, literal)`
+ /// pair `build_primary_key_predicate` produces, for direct batcher tests.
+ fn mysql_plain_predicate(sql: &str, column: &str, literal: &str) -> MysqlKeyPredicate {
+ MysqlKeyPredicate {
+ sql: sql.to_string(),
+ plain: Some(MysqlPlainEquality { column: column.to_string(), literal: literal.to_string() }),
+ }
+ }
+
#[test]
fn batches_mysql_updates_with_identical_values() {
let mut options = mysql_people_save_options(3);
@@ -9932,12 +10074,336 @@ mod tests {
let result = prepare_data_grid_save(options);
+ assert_eq!(result.validation_error, None);
+ assert_eq!(result.statements, vec!["UPDATE `app`.`people` SET `status` = NULL WHERE `id` IN (1, 2, 3);"]);
+ assert_eq!(result.rollback_statements.len(), 3);
+ }
+
+ #[test]
+ fn keeps_single_dirty_mysql_row_as_plain_equality() {
+ let mut options = mysql_people_save_options(1);
+ options.dirty_rows = vec![(0, vec![(1, Value::Null)])];
+
+ let result = prepare_data_grid_save(options);
+
+ assert_eq!(result.validation_error, None);
+ assert_eq!(result.statements, vec!["UPDATE `app`.`people` SET `status` = NULL WHERE `id` = 1;"]);
+ }
+
+ #[test]
+ fn preserves_mysql_update_order_when_mergeable_and_unmergeable_predicates_mix() {
+ // Renders past the save path: a NULL key row is rejected by
+ // `validate_existing_row_primary_keys`, but the batcher itself must still
+ // flush an open run in place instead of regrouping, so the order the save
+ // built (IN-able, unmergeable, IN-able) survives.
+ let mut statements = Vec::new();
+ push_mysql_key_predicate_batches(
+ &mut statements,
+ "UPDATE `app`.`people` SET `id` = 10 WHERE ",
+ vec![
+ mysql_plain_predicate("`id` = 1", "`id`", "1"),
+ MysqlKeyPredicate { sql: "`id` IS NULL".to_string(), plain: None },
+ mysql_plain_predicate("`id` = 3", "`id`", "3"),
+ ],
+ );
+
+ assert_eq!(
+ statements,
+ vec!["UPDATE `app`.`people` SET `id` = 10 WHERE (`id` = 1) OR (`id` IS NULL) OR (`id` = 3);"]
+ );
+ }
+
+ #[test]
+ fn keeps_null_mysql_key_predicates_out_of_the_in_run() {
+ // A NULL key cannot be merged (`col IN (NULL)` never matches, unlike
+ // `col = NULL`), so it stays its own part and never joins a neighbour run.
+ let mut statements = Vec::new();
+ push_mysql_key_predicate_batches(
+ &mut statements,
+ "UPDATE `app`.`people` SET `id` = 1 WHERE ",
+ vec![
+ mysql_plain_predicate("`id` = 1", "`id`", "1"),
+ mysql_plain_predicate("`id` = 2", "`id`", "2"),
+ MysqlKeyPredicate { sql: "`id` IS NULL".to_string(), plain: None },
+ mysql_plain_predicate("`id` = 3", "`id`", "3"),
+ mysql_plain_predicate("`id` = 4", "`id`", "4"),
+ ],
+ );
+
+ assert_eq!(
+ statements,
+ vec!["UPDATE `app`.`people` SET `id` = 1 WHERE (`id` IN (1, 2)) OR (`id` IS NULL) OR (`id` IN (3, 4));"]
+ );
+ }
+
+ #[test]
+ fn merges_mysql_string_keys_containing_sql_keywords() {
+ // The retired text parser rejected any literal containing " AND " / " OR "
+ // / starting with "IS "; the structured check proves these are plain
+ // equalities, so both paths merge them now.
+ let mut options = mysql_people_save_options(0);
+ options.table_meta.columns = Some(vec![column("id", "varchar(32)", false, None)]);
+ options.columns = vec!["id".to_string()];
+ options.table_meta.primary_keys = vec!["id".to_string()];
+ options.rows = vec![vec![json!("a AND b")], vec![json!("c OR d")], vec![json!("IS x")]];
+ options.dirty_rows =
+ vec![(0, vec![(0, json!("new"))]), (1, vec![(0, json!("new"))]), (2, vec![(0, json!("new"))])];
+ options.deleted_rows = vec![];
+
+ let updates = prepare_data_grid_save(options.clone());
+
+ assert_eq!(updates.validation_error, None);
+ assert_eq!(
+ updates.statements,
+ vec!["UPDATE `app`.`people` SET `id` = 'new' WHERE `id` IN ('a AND b', 'c OR d', 'IS x');"]
+ );
+
+ let deletes = prepare_data_grid_save(DataGridSaveStatementOptions {
+ dirty_rows: vec![],
+ deleted_rows: vec![0, 1, 2],
+ ..options
+ });
+ assert_eq!(deletes.validation_error, None);
+ assert_eq!(deletes.statements, vec!["DELETE FROM `app`.`people` WHERE `id` IN ('a AND b', 'c OR d', 'IS x');"]);
+ }
+
+ #[test]
+ fn merges_mysql_string_keys_with_quotes_and_backslashes() {
+ let mut options = mysql_people_save_options(0);
+ options.table_meta.columns = Some(vec![column("id", "varchar(32)", false, None)]);
+ options.columns = vec!["id".to_string()];
+ options.table_meta.primary_keys = vec!["id".to_string()];
+ options.rows = vec![vec![json!("O'Reilly")], vec![json!("back\\slash")]];
+ options.dirty_rows = vec![(0, vec![(0, json!("new"))]), (1, vec![(0, json!("new"))])];
+
+ let result = prepare_data_grid_save(options);
+
+ // The literals keep the formatter's escaping; only the predicate shape changed.
assert_eq!(result.validation_error, None);
assert_eq!(
result.statements,
- vec!["UPDATE `app`.`people` SET `status` = NULL WHERE (`id` = 1) OR (`id` = 2) OR (`id` = 3);"]
+ vec!["UPDATE `app`.`people` SET `id` = 'new' WHERE `id` IN ('O''Reilly', 'back\\\\slash');"]
);
- assert_eq!(result.rollback_statements.len(), 3);
+ }
+
+ #[test]
+ fn keeps_null_mysql_key_predicates_out_of_the_in_run_after_a_rejected_save() {
+ // A NULL key value never reaches the batcher: the save refuses the write
+ // outright, which is also why `col IN (NULL)` cannot silently over-match.
+ let mut options = mysql_people_save_options(0);
+ options.table_meta.columns = Some(vec![column("id", "bigint", true, None)]);
+ options.columns = vec!["id".to_string()];
+ options.table_meta.primary_keys = vec!["id".to_string()];
+ options.rows = vec![vec![json!(1)], vec![Value::Null], vec![json!(3)]];
+ options.dirty_rows = vec![(0, vec![(0, json!(10))]), (1, vec![(0, json!(11))]), (2, vec![(0, json!(12))])];
+
+ let result = prepare_data_grid_save(options);
+
+ assert!(result.statements.is_empty());
+ assert!(
+ result.validation_error.unwrap_or_default().contains("has no value in the query result"),
+ "a NULL key must be refused instead of merging into an IN list"
+ );
+ }
+
+ #[test]
+ fn keeps_guarded_mysql_predicates_out_of_the_in_run() {
+ // A clause that carries an extra `AND` guard is not a plain equality even
+ // when its first half looks like one; merging it would widen the batch.
+ let mut statements = Vec::new();
+ push_mysql_key_predicate_batches(
+ &mut statements,
+ "DELETE FROM `app`.`people` WHERE ",
+ vec![
+ mysql_plain_predicate("`id` = 1", "`id`", "1"),
+ MysqlKeyPredicate { sql: "`id` = 2 AND `payload` = 'x'".to_string(), plain: None },
+ mysql_plain_predicate("`id` = 3", "`id`", "3"),
+ ],
+ );
+
+ assert_eq!(
+ statements,
+ vec!["DELETE FROM `app`.`people` WHERE (`id` = 1) OR (`id` = 2 AND `payload` = 'x') OR (`id` = 3);"]
+ );
+ }
+
+ #[test]
+ fn keeps_compound_key_mysql_updates_in_or_form() {
+ let mut options = mysql_people_save_options(2);
+ options.table_meta.primary_keys = vec!["id".to_string(), "status".to_string()];
+ options.dirty_rows = vec![(0, vec![(1, json!("x"))]), (1, vec![(1, json!("x"))])];
+
+ let result = prepare_data_grid_save(options);
+
+ assert_eq!(result.validation_error, None);
+ assert_eq!(
+ result.statements,
+ vec![
+ "UPDATE `app`.`people` SET `status` = 'x' WHERE (`id` = 1 AND `status` = 'active') OR (`id` = 2 AND `status` = 'active');"
+ ]
+ );
+ }
+
+ #[test]
+ fn rejects_plain_report_when_the_rendered_clause_differs_from_the_pair() {
+ let mysql = Some(DatabaseType::Mysql);
+ // Sanity: the ordinary MySQL key is plain.
+ let predicate = build_primary_key_predicate(
+ mysql,
+ &["id".to_string()],
+ &[Some("id".to_string())],
+ &[json!(1)],
+ &[column("id", "int", false, None)],
+ None,
+ );
+ assert_eq!(predicate.sql, "`id` = 1");
+ assert!(predicate.plain.is_some());
+
+ // MySQL JSON columns already wrap the literal (`CAST(... AS JSON)`). Any
+ // future branch that renders another form must stop being mergeable,
+ // because the pair no longer reproduces the clause.
+ let json_predicate = build_primary_key_predicate(
+ mysql,
+ &["payload".to_string()],
+ &[Some("payload".to_string())],
+ &[json!(r#"{"a":1}"#)],
+ &[column("payload", "json", false, None)],
+ None,
+ );
+ assert!(json_predicate.sql.contains("CAST("), "{}", json_predicate.sql);
+ assert!(json_predicate.plain.is_none(), "a wrapped literal must stay in the OR form");
+ }
+
+ #[test]
+ fn plain_equality_check_ignores_outer_parentheses_only() {
+ let plain = |column: &str, literal: &str| MysqlPlainEquality {
+ column: column.to_string(),
+ literal: literal.to_string(),
+ };
+
+ assert!(plain_equality_from_clause("`id` = 1", plain("`id`", "1")).is_some());
+ assert!(plain_equality_from_clause("(`id` = 1)", plain("`id`", "1")).is_some());
+ // An appended guard, a compound key or a different comparison must not pass.
+ assert!(plain_equality_from_clause("`id` = 1 AND `status` = 'x'", plain("`id`", "1")).is_none());
+ assert!(plain_equality_from_clause("BINARY `id` = 1", plain("`id`", "1")).is_none());
+ assert!(plain_equality_from_clause("`id` IS NULL", plain("`id`", "1")).is_none());
+ assert!(plain_equality_from_clause("`id` = CAST('1' AS JSON)", plain("`id`", "1")).is_none());
+ // A bare NULL keyword is never merged, matching the retired parser.
+ assert!(plain_equality_from_clause("`id` = NULL", plain("`id`", "NULL")).is_none());
+ assert!(plain_equality_from_clause("`id` = null", plain("`id`", "null")).is_none());
+ }
+
+ #[test]
+ fn splits_mysql_batches_above_the_value_budget() {
+ let row_count = MYSQL_DATA_GRID_BATCH_MAX_VALUES + 1;
+ let mut options = mysql_people_save_options(row_count);
+ options.dirty_rows = (0..row_count).map(|index| (index, vec![(1, json!("archived"))])).collect();
+
+ let updates = prepare_data_grid_save(options.clone());
+
+ assert_eq!(updates.validation_error, None);
+ assert_eq!(updates.statements.len(), 2);
+ // The first statement holds exactly the value budget, so the last value
+ // it carries is the budget itself and the leftover opens the next one.
+ assert!(updates.statements[0].contains("`id` IN ("));
+ assert!(updates.statements[0].ends_with(&format!("{});", MYSQL_DATA_GRID_BATCH_MAX_VALUES)));
+ assert!(!updates.statements[0].contains(&format!(", {row_count})")));
+ assert!(updates.statements[1].ends_with(&format!("WHERE `id` = {row_count};")));
+
+ // The same budget applies to DELETE, which already emits IN lists today.
+ let deletes = prepare_data_grid_save(DataGridSaveStatementOptions {
+ dirty_rows: vec![],
+ deleted_rows: (0..row_count).collect(),
+ ..options
+ });
+
+ assert_eq!(deletes.validation_error, None);
+ assert_eq!(deletes.statements.len(), 2);
+ assert!(deletes.statements[0].ends_with(&format!("{});", MYSQL_DATA_GRID_BATCH_MAX_VALUES)));
+ assert!(deletes.statements[1].ends_with(&format!("WHERE `id` = {row_count};")));
+ }
+
+ #[test]
+ fn mysql_data_grid_update_respects_target_sql_size() {
+ // Keys long enough that two of them fit the text budget but not three, so
+ // the merged run still has to split even though all rows share one SET.
+ let key_length = 101_000;
+ let mut options = mysql_people_save_options(3);
+ options.rows[0][0] = json!("a".repeat(key_length));
+ options.rows[1][0] = json!("b".repeat(key_length));
+ options.rows[2][0] = json!("c".repeat(key_length));
+ options.dirty_rows = vec![
+ (0, vec![(1, json!("archived"))]),
+ (1, vec![(1, json!("archived"))]),
+ (2, vec![(1, json!("archived"))]),
+ ];
+
+ let result = prepare_data_grid_save(options);
+
+ assert_eq!(result.validation_error, None);
+ assert_eq!(result.statements.len(), 2);
+ assert!(result.statements[0].contains("`id` IN ("), "{}", &result.statements[0][..80]);
+ assert!(result.statements.iter().all(|statement| statement.contains("'archived'")));
+ assert!(result.statements.iter().all(|statement| statement.len() <= MYSQL_DATA_GRID_BATCH_TARGET_SQL_BYTES));
+ }
+
+ #[test]
+ fn splits_mysql_in_runs_when_the_plain_column_changes() {
+ // The save path never mixes key columns inside one SET group, so this is
+ // only reachable from a direct caller — but merging across columns would
+ // rewrite `id = 1 OR code = 'a'` into a list that matches the wrong rows,
+ // so the column change must break the run.
+ let mut statements = Vec::new();
+ push_mysql_key_predicate_batches(
+ &mut statements,
+ "UPDATE `app`.`people` SET `status` = 'x' WHERE ",
+ vec![
+ mysql_plain_predicate("`id` = 1", "`id`", "1"),
+ mysql_plain_predicate("`code` = 'a'", "`code`", "'a'"),
+ mysql_plain_predicate("`id` = 2", "`id`", "2"),
+ ],
+ );
+
+ assert_eq!(
+ statements,
+ vec!["UPDATE `app`.`people` SET `status` = 'x' WHERE (`id` = 1) OR (`code` = 'a') OR (`id` = 2);"]
+ );
+ }
+
+ #[test]
+ fn keeps_non_mysql_batched_updates_in_or_form() {
+ for (database_type, quote) in [(DatabaseType::Postgres, '"'), (DatabaseType::StarRocks, '`')] {
+ let mut options = mysql_people_save_options(2);
+ options.database_type = Some(database_type);
+ options.table_meta.primary_keys = vec!["id".to_string()];
+ options.dirty_rows = vec![(0, vec![(1, Value::Null)]), (1, vec![(1, Value::Null)])];
+
+ let result = prepare_data_grid_save_for_driver_profile(options, None);
+
+ assert_eq!(result.validation_error, None);
+ assert_eq!(
+ result.statements,
+ vec![
+ format!("UPDATE {quote}app{quote}.{quote}people{quote} SET {quote}status{quote} = NULL WHERE {quote}id{quote} = 1;"),
+ format!("UPDATE {quote}app{quote}.{quote}people{quote} SET {quote}status{quote} = NULL WHERE {quote}id{quote} = 2;"),
+ ],
+ "{database_type:?} must keep one statement per row"
+ );
+ }
+ }
+
+ #[test]
+ fn keeps_mysql_batching_for_starrocks_driver_profiles() {
+ // The batch gate stays `DatabaseType::Mysql`; a MySQL connection whose
+ // driver profile is "starrocks" must keep the same batching (and the same
+ // IN form the DELETE path already emits there).
+ let mut options = mysql_people_save_options(2);
+ options.dirty_rows = vec![(0, vec![(1, Value::Null)]), (1, vec![(1, Value::Null)])];
+
+ let result = prepare_data_grid_save_for_driver_profile(options, Some("starrocks"));
+
+ assert_eq!(result.validation_error, None);
+ assert_eq!(result.statements, vec!["UPDATE `app`.`people` SET `status` = NULL WHERE `id` IN (1, 2);"]);
}
#[test]