-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathddl.rs
More file actions
executable file
·403 lines (366 loc) · 15.2 KB
/
Copy pathddl.rs
File metadata and controls
executable file
·403 lines (366 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Copyright 2026 ExtendDB contributors
// SPDX-License-Identifier: Apache-2.0
//! DDL helpers for creating and dropping per-DynamoDB-table data tables in `PostgreSQL`.
use extenddb_core::types::{
AttributeDefinition, IndexInfo, IndexType, KeySchemaElement, Projection, StreamSpecification,
TableKeyInfo,
};
use extenddb_storage::error::StorageError;
use extenddb_storage::util::{sk_column, sk_column_n};
use super::{all_sort_key_info, data_table_name, index_table_name};
use crate::PostgresEngine;
/// Row shape returned by the table-info query: (key_schema, attr_defs, status, table_id, stream_spec, has_lsi).
type TableInfoRow = (
serde_json::Value,
serde_json::Value,
String,
String,
Option<serde_json::Value>,
Option<bool>,
);
impl PostgresEngine {
/// Create the per-DynamoDB-table data table in `PostgreSQL`.
///
/// Called within the `create_table` transaction. The DDL is dynamically
/// generated based on the key schema — the primary key constraint uses
/// the sort key column matching the sort key's scalar type.
///
/// # Errors
///
/// Returns [`StorageError::Internal`] if the DDL execution fails.
///
/// # Safety (SQL injection)
///
/// Table names are validated at the engine layer to contain only `[a-zA-Z0-9_.-]`.
/// Column names are compile-time constants. No user input is interpolated
/// into the DDL beyond the validated table name.
pub(crate) async fn create_data_table(
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
table_id: &str,
key_schema: &[KeySchemaElement],
attr_defs: &[AttributeDefinition],
) -> Result<(), StorageError> {
let ddb_table = data_table_name(table_id);
let sk_infos = all_sort_key_info(key_schema, attr_defs);
let ddl = if sk_infos.is_empty() {
format!(
r"CREATE TABLE {ddb_table} (
pk TEXT NOT NULL PRIMARY KEY,
item_data JSONB NOT NULL
)"
)
} else if sk_infos.len() == 1 {
// Backward-compatible single SK path
let sk_col = sk_column(sk_infos[0].1);
format!(
r"CREATE TABLE {ddb_table} (
pk TEXT NOT NULL,
sk_s TEXT,
sk_n NUMERIC,
sk_b BYTEA,
item_data JSONB NOT NULL,
PRIMARY KEY (pk, {sk_col})
)"
)
} else {
// Multi-part RANGE key: one typed column set per RANGE attribute
let mut col_defs = vec!["pk TEXT NOT NULL".to_owned()];
let mut pk_cols = vec!["pk".to_owned()];
for (i, &(_, sk_type)) in sk_infos.iter().enumerate() {
let col = sk_column_n(i, sk_type);
// Add all three type columns for this SK position
if i == 0 {
col_defs.push("sk_s TEXT".to_owned());
col_defs.push("sk_n NUMERIC".to_owned());
col_defs.push("sk_b BYTEA".to_owned());
} else {
let n = i + 1;
col_defs.push(format!("sk{n}_s TEXT"));
col_defs.push(format!("sk{n}_n NUMERIC"));
col_defs.push(format!("sk{n}_b BYTEA"));
}
pk_cols.push(col);
}
col_defs.push("item_data JSONB NOT NULL".to_owned());
format!(
"CREATE TABLE {ddb_table} (\n {},\n PRIMARY KEY ({})\n)",
col_defs.join(",\n "),
pk_cols.join(", ")
)
};
sqlx::query(&ddl)
.execute(&mut **tx)
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
Ok(())
}
/// Drop the per-DynamoDB-table data table.
///
/// Called when a table deletion transition completes.
///
/// # Errors
///
/// Returns [`StorageError::Internal`] if the DDL execution fails.
pub(crate) async fn drop_data_table(
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
table_id: &str,
) -> Result<(), StorageError> {
let ddb_table = data_table_name(table_id);
let ddl = format!("DROP TABLE IF EXISTS {ddb_table}");
sqlx::query(&ddl)
.execute(&mut **tx)
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
Ok(())
}
/// Create a GSI/LSI data table in PostgreSQL.
///
/// GSI tables use the same `(pk, sk_*)` structure as base tables but add
/// `base_pk` and `base_sk_*` columns for uniqueness (GSI keys are not unique).
/// The primary key includes the base table key to ensure one row per base item.
///
/// Multi-part keys: when the index has multiple HASH attributes, they are
/// concatenated into the `pk` column. Multiple RANGE attributes get separate
/// typed column sets (`sk_s/sk_n/sk_b`, `sk2_s/sk2_n/sk2_b`, etc.).
// S2: Parameters mirror the SQL schema dimensions (account, table, index,
// key schemas, attribute defs). A wrapper struct would obscure the call
// site without adding clarity.
#[allow(clippy::too_many_arguments)]
pub(crate) async fn create_index_data_table(
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
index_id: &str,
index_key_schema: &[KeySchemaElement],
attr_defs: &[AttributeDefinition],
base_key_schema: &[KeySchemaElement],
base_attr_defs: &[AttributeDefinition],
) -> Result<(), StorageError> {
let idx_table = index_table_name(index_id);
// Determine base table sort key columns for the uniqueness constraint
let base_sks = all_sort_key_info(base_key_schema, base_attr_defs);
// Determine index sort keys
let idx_sks = all_sort_key_info(index_key_schema, attr_defs);
// Build column definitions
let mut col_defs = vec!["pk TEXT NOT NULL".to_owned()];
// Index SK columns
for (i, &(_, _)) in idx_sks.iter().enumerate() {
if i == 0 {
col_defs.push("sk_s TEXT".to_owned());
col_defs.push("sk_n NUMERIC".to_owned());
col_defs.push("sk_b BYTEA".to_owned());
} else {
let n = i + 1;
col_defs.push(format!("sk{n}_s TEXT"));
col_defs.push(format!("sk{n}_n NUMERIC"));
col_defs.push(format!("sk{n}_b BYTEA"));
}
}
// Base table key columns for uniqueness
col_defs.push("base_pk TEXT NOT NULL".to_owned());
for (i, &(_, _)) in base_sks.iter().enumerate() {
if i == 0 {
col_defs.push("base_sk_s TEXT".to_owned());
col_defs.push("base_sk_n NUMERIC".to_owned());
col_defs.push("base_sk_b BYTEA".to_owned());
} else {
let n = i + 1;
col_defs.push(format!("base_sk{n}_s TEXT"));
col_defs.push(format!("base_sk{n}_n NUMERIC"));
col_defs.push(format!("base_sk{n}_b BYTEA"));
}
}
col_defs.push("item_data JSONB NOT NULL".to_owned());
// Build PK constraint: (pk, base_pk[, base_sk_col...])
let mut pk_cols = vec!["pk".to_owned(), "base_pk".to_owned()];
for (i, &(_, sk_type)) in base_sks.iter().enumerate() {
let col = if i == 0 {
format!("base_{}", sk_column(sk_type))
} else {
format!("base_{}", sk_column_n(i, sk_type))
};
pk_cols.push(col);
}
let ddl = format!(
"CREATE TABLE {idx_table} (\n {},\n PRIMARY KEY ({})\n)",
col_defs.join(",\n "),
pk_cols.join(", ")
);
sqlx::query(&ddl)
.execute(&mut **tx)
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
// Create an index for sort key ordering within a partition
if !idx_sks.is_empty() {
let mut order_cols = vec!["pk".to_owned()];
for (i, &(_, sk_type)) in idx_sks.iter().enumerate() {
order_cols.push(sk_column_n(i, sk_type));
}
order_cols.push("base_pk".to_owned());
for (i, &(_, sk_type)) in base_sks.iter().enumerate() {
let col = if i == 0 {
format!("base_{}", sk_column(sk_type))
} else {
format!("base_{}", sk_column_n(i, sk_type))
};
order_cols.push(col);
}
let order_idx = format!("CREATE INDEX ON {idx_table} ({})", order_cols.join(", "));
sqlx::query(&order_idx)
.execute(&mut **tx)
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
}
// Index on base table key columns for delete_index_row_multi lookups.
{
let mut base_key_cols = vec!["base_pk".to_owned()];
for (i, &(_, sk_type)) in base_sks.iter().enumerate() {
let col = if i == 0 {
format!("base_{}", sk_column(sk_type))
} else {
format!("base_{}", sk_column_n(i, sk_type))
};
base_key_cols.push(col);
}
let idx_name = format!("_ddb_{index_id}_base_key_idx");
let base_key_idx = format!(
"CREATE INDEX \"{idx_name}\" ON {idx_table} ({})",
base_key_cols.join(", ")
);
sqlx::query(&base_key_idx)
.execute(&mut **tx)
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
}
Ok(())
}
/// Drop a GSI/LSI data table.
pub(crate) async fn drop_index_data_table(
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
index_id: &str,
) -> Result<(), StorageError> {
let idx_table = index_table_name(index_id);
let ddl = format!("DROP TABLE IF EXISTS {idx_table}");
sqlx::query(&ddl)
.execute(&mut **tx)
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
Ok(())
}
/// Fetch key schema and attribute definitions for a table from the catalog.
///
/// Uses a single query that combines the table row with an LSI existence
/// subquery, eliminating one catalog roundtrip per call (P118 optimization #1).
///
/// # Errors
///
/// Returns [`StorageError::TableNotFound`] if the table doesn't exist.
/// Returns [`StorageError::TableNotActive`] if the table is not ACTIVE.
/// Returns [`StorageError::Internal`] on query or deserialization failure.
pub(crate) async fn fetch_table_key_info(
&self,
account_id: &str,
table_name: &str,
) -> Result<TableKeyInfo, StorageError> {
let row: Option<TableInfoRow> = sqlx::query_as(
"SELECT key_schema, attribute_definitions, table_status, table_id, \
stream_specification, \
EXISTS(SELECT 1 FROM indexes WHERE table_id = tables.table_id AND index_type = 'LSI') AS has_lsi \
FROM tables \
WHERE account_id = $1 AND table_name = $2",
)
.bind(account_id)
.bind(table_name)
.fetch_optional(&self.pool)
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
let (ks_json, ad_json, status, table_id, stream_spec_json, has_lsi) =
row.ok_or_else(|| StorageError::TableNotFound(table_name.to_owned()))?;
if status != "ACTIVE" {
return Err(StorageError::TableNotActive(table_name.to_owned()));
}
let key_schema: Vec<KeySchemaElement> =
serde_json::from_value(ks_json).map_err(|e| StorageError::Internal(e.to_string()))?;
let attribute_definitions: Vec<AttributeDefinition> =
serde_json::from_value(ad_json).map_err(|e| StorageError::Internal(e.to_string()))?;
let stream_specification: Option<StreamSpecification> = stream_spec_json
.map(serde_json::from_value)
.transpose()
.map_err(|e| StorageError::Internal(e.to_string()))?;
Ok(TableKeyInfo {
table_name: table_name.to_owned(),
account_id: account_id.to_owned(),
table_id,
key_schema,
attribute_definitions,
has_lsi: has_lsi.unwrap_or(false),
stream_specification,
})
}
/// Fetch metadata for a secondary index from the catalog.
///
/// This variant looks up `table_id` from the tables catalog. Prefer
/// `fetch_index_info_by_table_id` when `TableKeyInfo` is already available
/// (P118 optimization #4).
pub(crate) async fn fetch_index_info(
&self,
account_id: &str,
table_name: &str,
index_name: &str,
) -> Result<IndexInfo, StorageError> {
// First get the table_id and verify the table is ACTIVE
let row: Option<(String, String)> = sqlx::query_as(
"SELECT table_id, table_status FROM tables WHERE account_id = $1 AND table_name = $2",
)
.bind(account_id)
.bind(table_name)
.fetch_optional(&self.pool)
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
let (table_id, status) =
row.ok_or_else(|| StorageError::TableNotFound(table_name.to_owned()))?;
if status != "ACTIVE" {
return Err(StorageError::TableNotActive(table_name.to_owned()));
}
self.fetch_index_info_by_table_id(&table_id, index_name)
.await
}
/// Fetch metadata for a secondary index using a known `table_id`.
///
/// Saves one catalog roundtrip vs `fetch_index_info` when the caller
/// already has `TableKeyInfo` (P118 optimization #4).
pub(crate) async fn fetch_index_info_by_table_id(
&self,
table_id: &str,
index_name: &str,
) -> Result<IndexInfo, StorageError> {
let idx_row: Option<(String, String, serde_json::Value, serde_json::Value)> = sqlx::query_as(
"SELECT index_type, index_id, key_schema, projection FROM indexes WHERE table_id = $1 AND index_name = $2",
)
.bind(table_id)
.bind(index_name)
.fetch_optional(&self.pool)
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
let (idx_type_str, idx_id, ks_json, proj_json) =
idx_row.ok_or_else(|| StorageError::IndexNotFound(index_name.to_owned()))?;
let index_type = match idx_type_str.as_str() {
"GSI" => IndexType::Gsi,
"LSI" => IndexType::Lsi,
other => {
return Err(StorageError::Internal(format!(
"unknown index type in database: {other}"
)));
}
};
let key_schema: Vec<KeySchemaElement> =
serde_json::from_value(ks_json).map_err(|e| StorageError::Internal(e.to_string()))?;
let projection: Projection =
serde_json::from_value(proj_json).map_err(|e| StorageError::Internal(e.to_string()))?;
Ok(IndexInfo {
index_name: index_name.to_owned(),
index_id: idx_id,
index_type,
key_schema,
projection,
})
}
}