1
+ //! An implementation of the memo table using SQLite.
2
+
1
3
use std:: { str:: FromStr , sync:: Arc , time:: Duration } ;
2
4
3
5
use super :: transaction:: Transaction ;
@@ -184,8 +186,9 @@ impl Memoize for SqliteMemo {
184
186
}
185
187
}
186
188
187
- // Memoize helpers
189
+ // Helper functions for implementing the ` Memoize` trait.
188
190
impl SqliteMemo {
191
+ /// Gets the representative group id of a relational group.
189
192
async fn get_representative_group_id (
190
193
& self ,
191
194
db : & mut SqliteConnection ,
@@ -199,6 +202,7 @@ impl SqliteMemo {
199
202
Ok ( representative_group_id)
200
203
}
201
204
205
+ /// Sets the representative group id of a relational group.
202
206
async fn set_representative_group_id (
203
207
& self ,
204
208
db : & mut SqliteConnection ,
@@ -213,6 +217,7 @@ impl SqliteMemo {
213
217
Ok ( ( ) )
214
218
}
215
219
220
+ /// Gets the representative group id of a scalar group.
216
221
async fn get_representative_scalar_group_id (
217
222
& self ,
218
223
db : & mut SqliteConnection ,
@@ -226,6 +231,7 @@ impl SqliteMemo {
226
231
Ok ( representative_group_id)
227
232
}
228
233
234
+ /// Sets the representative group id of a scalar group.
229
235
async fn set_representative_scalar_group_id (
230
236
& self ,
231
237
db : & mut SqliteConnection ,
@@ -240,6 +246,10 @@ impl SqliteMemo {
240
246
Ok ( ( ) )
241
247
}
242
248
249
+ /// Inserts a scalar expression into the database. If the `add_to_group_id` is `Some`,
250
+ /// we will attempt to add the scalar expression to the specified group.
251
+ /// If the scalar expression already exists in the database, the existing group id will be returned.
252
+ /// Otherwise, a new group id will be created.
243
253
async fn add_scalar_expr_to_group_inner (
244
254
& self ,
245
255
scalar_expr : & ScalarExpression ,
@@ -304,9 +314,6 @@ impl SqliteMemo {
304
314
ScalarOperatorKind :: Add ,
305
315
)
306
316
. await ?;
307
- println ! ( "add: {:?}" , add) ;
308
- println ! ( "scalar_expr_id: {:?}" , scalar_expr_id) ;
309
- println ! ( "group_id: {:?}" , group_id) ;
310
317
311
318
sqlx:: query_scalar ( "INSERT INTO scalar_adds (scalar_expression_id, group_id, left_group_id, right_group_id) VALUES ($1, $2, $3, $4) ON CONFLICT DO UPDATE SET group_id = group_id RETURNING group_id" )
312
319
. bind ( scalar_expr_id)
@@ -351,7 +358,7 @@ impl SqliteMemo {
351
358
Ok ( inserted_group_id)
352
359
}
353
360
354
- /// Inserts a scalar expression into the database .
361
+ /// Inserts an entry into the `scalar_expressions` table .
355
362
async fn insert_into_scalar_expressions (
356
363
db : & mut SqliteConnection ,
357
364
scalar_expr_id : ScalarExpressionId ,
@@ -368,6 +375,7 @@ impl SqliteMemo {
368
375
Ok ( ( ) )
369
376
}
370
377
378
+ /// Removes a dangling scalar expression from the `scalar_expressions` table.
371
379
async fn remove_dangling_scalar_expr (
372
380
& self ,
373
381
db : & mut SqliteConnection ,
@@ -380,6 +388,10 @@ impl SqliteMemo {
380
388
Ok ( ( ) )
381
389
}
382
390
391
+ /// Inserts a logical expression into the memo table. If the `add_to_group_id` is `Some`,
392
+ /// we will attempt to add the logical expression to the specified group.
393
+ /// If the logical expression already exists in the database, the existing group id will be returned.
394
+ /// Otherwise, a new group id will be created.
383
395
async fn add_logical_expr_to_group_inner (
384
396
& self ,
385
397
logical_expr : & LogicalExpression ,
@@ -477,6 +489,7 @@ impl SqliteMemo {
477
489
Ok ( inserted_group_id)
478
490
}
479
491
492
+ /// Inserts an entry into the `logical_expressions` table.
480
493
async fn insert_into_logical_expressions (
481
494
txn : & mut SqliteConnection ,
482
495
logical_expr_id : LogicalExpressionId ,
@@ -493,6 +506,7 @@ impl SqliteMemo {
493
506
Ok ( ( ) )
494
507
}
495
508
509
+ /// Removes a dangling logical expression from the `logical_expressions` table.
496
510
async fn remove_dangling_logical_expr (
497
511
& self ,
498
512
db : & mut SqliteConnection ,
@@ -507,6 +521,8 @@ impl SqliteMemo {
507
521
}
508
522
509
523
/// The SQL query to get all logical expressions in a group.
524
+ /// For each of the operators, the logical_expression_id is selected,
525
+ /// as well as the data fields in json form.
510
526
const fn get_all_logical_exprs_in_group_query ( ) -> & ' static str {
511
527
concat ! (
512
528
"SELECT logical_expression_id, json_object('Scan', json_object('table_name', json(table_name), 'predicate', predicate_group_id)) as data FROM scans WHERE group_id = $1" ,
@@ -518,6 +534,8 @@ const fn get_all_logical_exprs_in_group_query() -> &'static str {
518
534
}
519
535
520
536
/// The SQL query to get all scalar expressions in a group.
537
+ /// For each of the operators, the scalar_expression_id is selected,
538
+ /// as well as the data fields in json form.
521
539
const fn get_all_scalar_exprs_in_group_query ( ) -> & ' static str {
522
540
concat ! (
523
541
"SELECT scalar_expression_id, json_object('Constant', json(value)) as data FROM scalar_constants WHERE group_id = $1" ,
0 commit comments