Handle `SELECT TRANSFER(collection, source_key, dest_key, field, amount)`
(
state: &SharedState,
identity: &AuthenticatedIdentity,
sql: &str,
)
| 26 | |
| 27 | /// Handle `SELECT TRANSFER(collection, source_key, dest_key, field, amount)` |
| 28 | pub async fn transfer( |
| 29 | state: &SharedState, |
| 30 | identity: &AuthenticatedIdentity, |
| 31 | sql: &str, |
| 32 | ) -> PgWireResult<Vec<Response>> { |
| 33 | let args = super::kv_atomic::parse_function_args(sql, "TRANSFER")?; |
| 34 | if args.len() < 5 { |
| 35 | return Err(sqlstate_error( |
| 36 | "42601", |
| 37 | "TRANSFER requires 5 arguments: (collection, source_key, dest_key, field, amount)", |
| 38 | )); |
| 39 | } |
| 40 | |
| 41 | let collection = unquote(&args[0]).to_lowercase(); |
| 42 | let source_key = unquote(&args[1]); |
| 43 | let dest_key = unquote(&args[2]); |
| 44 | let field = unquote(&args[3]); |
| 45 | let amount_str = args[4].trim().to_string(); |
| 46 | let amount: f64 = amount_str.parse().map_err(|_| { |
| 47 | sqlstate_error( |
| 48 | "42601", |
| 49 | &format!("TRANSFER: amount must be a number, got '{amount_str}'"), |
| 50 | ) |
| 51 | })?; |
| 52 | |
| 53 | if amount <= 0.0 { |
| 54 | return Err(sqlstate_error("42601", "TRANSFER: amount must be positive")); |
| 55 | } |
| 56 | |
| 57 | let tenant_id = identity.tenant_id; |
| 58 | let vshard = VShardId::from_collection_in_database(DatabaseId::DEFAULT, &collection); |
| 59 | |
| 60 | // Dispatch to Data Plane — entire read+validate+write is atomic (single TPC core). |
| 61 | let plan = PhysicalPlan::Kv(KvOp::Transfer { |
| 62 | collection, |
| 63 | source_key: source_key.into_bytes(), |
| 64 | dest_key: dest_key.into_bytes(), |
| 65 | field, |
| 66 | amount, |
| 67 | }); |
| 68 | |
| 69 | match crate::control::server::dispatch_utils::dispatch_to_data_plane( |
| 70 | state, |
| 71 | tenant_id, |
| 72 | vshard, |
| 73 | plan, |
| 74 | TraceId::ZERO, |
| 75 | ) |
| 76 | .await |
| 77 | { |
| 78 | Ok(resp) => { |
| 79 | let payload_text = |
| 80 | crate::data::executor::response_codec::decode_payload_to_json(&resp.payload); |
| 81 | respond_json("transfer", &payload_text) |
| 82 | } |
| 83 | Err(e) => Err(sqlstate_error("XX000", &e.to_string())), |
| 84 | } |
| 85 | } |
no test coverage detected