50
50
/// ```
51
51
use partiql_value:: { BindingsName , Value } ;
52
52
use std:: collections:: HashMap ;
53
+ use std:: fmt:: { Debug , Display , Formatter } ;
53
54
54
55
#[ cfg( feature = "serde" ) ]
55
56
use serde:: { Deserialize , Serialize } ;
@@ -162,9 +163,27 @@ impl OpId {
162
163
}
163
164
}
164
165
166
+ impl < T > Display for LogicalPlan < T >
167
+ where
168
+ T : Default + Debug ,
169
+ {
170
+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
171
+ let flows = self . flows ( ) ;
172
+ writeln ! ( f, "LogicalPlan" ) ?;
173
+ writeln ! ( f, "---" ) ?;
174
+ for ( s, d, _w) in flows {
175
+ let src_node = self . operator ( * s) . expect ( "Unable to get the src operator" ) ;
176
+ let dst_node = self . operator ( * d) . expect ( "Unable to get the dst operator" ) ;
177
+ writeln ! ( f, ">>> [{src_node:?}] -> [{dst_node:?}]" ) ?;
178
+ }
179
+ writeln ! ( f)
180
+ }
181
+ }
182
+
165
183
/// Represents PartiQL binding operators; A `BindingOp` is an operator that operates on
166
184
/// binding tuples as specified by [PartiQL Specification 2019](https://partiql.org/assets/PartiQL-Specification.pdf).
167
185
#[ derive( Debug , Clone , Default , Eq , PartialEq ) ]
186
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
168
187
pub enum BindingsOp {
169
188
Scan ( Scan ) ,
170
189
Pivot ( Pivot ) ,
@@ -187,6 +206,7 @@ pub enum BindingsOp {
187
206
188
207
/// [`Scan`] bridges from [`ValueExpr`]s to [`BindingsOp`]s.
189
208
#[ derive( Debug , Clone , Eq , PartialEq ) ]
209
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
190
210
pub struct Scan {
191
211
pub expr : ValueExpr ,
192
212
pub as_key : String ,
@@ -198,13 +218,15 @@ pub struct Scan {
198
218
/// see section `6.2` of
199
219
/// [PartiQL Specification — August 1, 2019](https://partiql.org/assets/PartiQL-Specification.pdf).
200
220
#[ derive( Debug , Clone , Eq , PartialEq ) ]
221
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
201
222
pub struct Pivot {
202
223
pub key : ValueExpr ,
203
224
pub value : ValueExpr ,
204
225
}
205
226
206
227
/// [`Unpivot`] bridges from [`ValueExpr`]s to [`BindingsOp`]s.
207
228
#[ derive( Debug , Clone , Eq , PartialEq ) ]
229
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
208
230
pub struct Unpivot {
209
231
pub expr : ValueExpr ,
210
232
pub as_key : String ,
@@ -213,13 +235,15 @@ pub struct Unpivot {
213
235
214
236
/// [`Filter`] represents a filter operator, e.g. `WHERE a = 10` in `SELECT a FROM t WHERE a = 10`.
215
237
#[ derive( Debug , Clone , Eq , PartialEq ) ]
238
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
216
239
pub struct Filter {
217
240
pub expr : ValueExpr ,
218
241
}
219
242
220
243
/// ['Join`] represents a join operator, e.g. implicit `CROSS JOIN` specified by comma in `FROM`
221
244
/// clause in `SELECT t1.a, t2.b FROM tbl1 AS t1, tbl2 AS t2`.
222
245
#[ derive( Debug , Clone , Eq , PartialEq ) ]
246
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
223
247
pub struct Join {
224
248
pub kind : JoinKind ,
225
249
pub left : Box < BindingsOp > ,
@@ -229,6 +253,7 @@ pub struct Join {
229
253
230
254
/// Represents join types.
231
255
#[ derive( Debug , Clone , Eq , PartialEq ) ]
256
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
232
257
pub enum JoinKind {
233
258
Inner ,
234
259
Left ,
@@ -239,26 +264,30 @@ pub enum JoinKind {
239
264
240
265
/// Represents a projection, e.g. `SELECT a` in `SELECT a FROM t`.
241
266
#[ derive( Debug , Clone , Eq , PartialEq ) ]
267
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
242
268
pub struct Project {
243
269
pub exprs : HashMap < String , ValueExpr > ,
244
270
}
245
271
246
272
/// Represents a value projection (SELECT VALUE) e.g. `SELECT VALUE t.a * 2` in
247
273
///`SELECT VALUE t.a * 2 IN tbl AS t`.
248
274
#[ derive( Debug , Clone , Eq , PartialEq ) ]
275
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
249
276
pub struct ProjectValue {
250
277
pub expr : ValueExpr ,
251
278
}
252
279
253
280
/// Represents an expression query e.g. `a * 2` in `a * 2` or an expression like `2+2`.
254
281
#[ derive( Debug , Clone , Eq , PartialEq ) ]
282
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
255
283
pub struct ExprQuery {
256
284
pub expr : ValueExpr ,
257
285
}
258
286
259
287
/// Represents a PartiQL value expression. Evaluation of a [`ValueExpr`] leads to a PartiQL value as
260
288
/// specified by [PartiQL Specification 2019](https://partiql.org/assets/PartiQL-Specification.pdf).
261
289
#[ derive( Debug , Clone , Eq , PartialEq ) ]
290
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
262
291
pub enum ValueExpr {
263
292
UnExpr ( UnaryOp , Box < ValueExpr > ) ,
264
293
BinaryExpr ( BinaryOp , Box < ValueExpr > , Box < ValueExpr > ) ,
@@ -283,6 +312,7 @@ pub enum ValueExpr {
283
312
// TODO we should replace this enum with some identifier that can be looked up in a symtab/funcregistry?
284
313
/// Represents logical plan's unary operators.
285
314
#[ derive( Debug , Clone , Eq , PartialEq ) ]
315
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
286
316
pub enum UnaryOp {
287
317
Pos ,
288
318
Neg ,
@@ -292,6 +322,7 @@ pub enum UnaryOp {
292
322
// TODO we should replace this enum with some identifier that can be looked up in a symtab/funcregistry?
293
323
/// Represents logical plan's binary operators.
294
324
#[ derive( Debug , Clone , Eq , PartialEq ) ]
325
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
295
326
pub enum BinaryOp {
296
327
And ,
297
328
Or ,
@@ -315,6 +346,7 @@ pub enum BinaryOp {
315
346
}
316
347
317
348
#[ derive( Debug , Clone , Eq , PartialEq ) ]
349
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
318
350
/// Represents a path component in a plan.
319
351
pub enum PathComponent {
320
352
/// E.g. `b` in `a.b`
@@ -327,6 +359,7 @@ pub enum PathComponent {
327
359
328
360
/// Represents a PartiQL tuple expression, e.g: `{ a.b: a.c * 2, 'count': a.c + 10}`.
329
361
#[ derive( Clone , Debug , Default , Eq , PartialEq ) ]
362
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
330
363
pub struct TupleExpr {
331
364
pub attrs : Vec < ValueExpr > ,
332
365
pub values : Vec < ValueExpr > ,
@@ -341,6 +374,7 @@ impl TupleExpr {
341
374
342
375
/// Represents a PartiQL list expression, e.g. `[a.c * 2, 5]`.
343
376
#[ derive( Clone , Debug , Default , Eq , PartialEq ) ]
377
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
344
378
pub struct ListExpr {
345
379
pub elements : Vec < ValueExpr > ,
346
380
}
@@ -354,6 +388,7 @@ impl ListExpr {
354
388
355
389
/// Represents a PartiQL bag expression, e.g. `<<a.c * 2, 5>>`.
356
390
#[ derive( Clone , Debug , Default , Eq , PartialEq ) ]
391
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
357
392
pub struct BagExpr {
358
393
pub elements : Vec < ValueExpr > ,
359
394
}
@@ -367,6 +402,7 @@ impl BagExpr {
367
402
368
403
/// Represents a PartiQL `BETWEEN` expression, e.g. `BETWEEN 500 AND 600`.
369
404
#[ derive( Debug , Clone , Eq , PartialEq ) ]
405
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
370
406
pub struct BetweenExpr {
371
407
pub value : Box < ValueExpr > ,
372
408
pub from : Box < ValueExpr > ,
@@ -375,12 +411,14 @@ pub struct BetweenExpr {
375
411
376
412
/// Represents a PartiQL Pattern Match expression, e.g. `'foo' LIKE 'foo'`.
377
413
#[ derive( Debug , Clone , Eq , PartialEq ) ]
414
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
378
415
pub struct PatternMatchExpr {
379
416
pub value : Box < ValueExpr > ,
380
417
pub pattern : Pattern ,
381
418
}
382
419
383
420
#[ derive( Debug , Clone , Eq , PartialEq ) ]
421
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
384
422
pub enum Pattern {
385
423
Like ( LikeMatch ) , // TODO other e.g., SIMILAR_TO, or regex match
386
424
LikeNonStringNonLiteral ( LikeNonStringNonLiteralMatch ) ,
@@ -389,6 +427,7 @@ pub enum Pattern {
389
427
/// Represents a LIKE expression where both the `pattern` and `escape` are string literals,
390
428
/// e.g. `'foo%' ESCAPE '/'`
391
429
#[ derive( Debug , Clone , Eq , PartialEq ) ]
430
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
392
431
pub struct LikeMatch {
393
432
pub pattern : String ,
394
433
pub escape : String ,
@@ -397,6 +436,7 @@ pub struct LikeMatch {
397
436
/// Represents a LIKE expression where one of `pattern` and `escape` is not a string literal,
398
437
/// e.g. `some_pattern ESCAPE '/'`
399
438
#[ derive( Debug , Clone , Eq , PartialEq ) ]
439
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
400
440
pub struct LikeNonStringNonLiteralMatch {
401
441
pub pattern : Box < ValueExpr > ,
402
442
pub escape : Box < ValueExpr > ,
@@ -405,13 +445,15 @@ pub struct LikeNonStringNonLiteralMatch {
405
445
/// Represents a sub-query expression, e.g. `SELECT v.a*2 AS u FROM t AS v` in
406
446
/// `SELECT t.a, s FROM data AS t, (SELECT v.a*2 AS u FROM t AS v) AS s`
407
447
#[ derive( Debug , Clone , Eq , PartialEq ) ]
448
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
408
449
pub struct SubQueryExpr {
409
450
pub plan : LogicalPlan < BindingsOp > ,
410
451
}
411
452
412
453
/// Represents a PartiQL's simple case expressions,
413
454
/// e.g.`CASE <expr> [ WHEN <expr> THEN <expr> ]... [ ELSE <expr> ] END`.
414
455
#[ derive( Debug , Clone , Eq , PartialEq ) ]
456
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
415
457
pub struct SimpleCase {
416
458
pub expr : Box < ValueExpr > ,
417
459
pub cases : Vec < ( Box < ValueExpr > , Box < ValueExpr > ) > ,
@@ -421,13 +463,15 @@ pub struct SimpleCase {
421
463
/// Represents a PartiQL's searched case expressions,
422
464
/// e.g.`CASE [ WHEN <expr> THEN <expr> ]... [ ELSE <expr> ] END`.
423
465
#[ derive( Debug , Clone , Eq , PartialEq ) ]
466
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
424
467
pub struct SearchedCase {
425
468
pub cases : Vec < ( Box < ValueExpr > , Box < ValueExpr > ) > ,
426
469
pub default : Option < Box < ValueExpr > > ,
427
470
}
428
471
429
472
/// Represents an `IS` expression, e.g. `IS TRUE`.
430
473
#[ derive( Debug , Clone , Eq , PartialEq ) ]
474
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
431
475
pub struct IsTypeExpr {
432
476
pub not : bool ,
433
477
pub expr : Box < ValueExpr > ,
@@ -436,6 +480,7 @@ pub struct IsTypeExpr {
436
480
437
481
/// Represents a PartiQL Type.
438
482
#[ derive( Clone , Debug , PartialEq , Eq ) ]
483
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
439
484
pub enum Type {
440
485
NullType ,
441
486
BooleanType ,
@@ -468,6 +513,7 @@ pub enum Type {
468
513
469
514
/// Represents a `NULLIF` expression, e.g. `NULLIF(v1, v2)` in `SELECT NULLIF(v1, v2) FROM data`.
470
515
#[ derive( Debug , Clone , Eq , PartialEq ) ]
516
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
471
517
pub struct NullIfExpr {
472
518
pub lhs : Box < ValueExpr > ,
473
519
pub rhs : Box < ValueExpr > ,
@@ -476,19 +522,22 @@ pub struct NullIfExpr {
476
522
/// Represents a `COALESCE` expression, e.g.
477
523
/// `COALESCE(NULL, 10)` in `SELECT COALESCE(NULL, 10) FROM data`.
478
524
#[ derive( Debug , Clone , Eq , PartialEq ) ]
525
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
479
526
pub struct CoalesceExpr {
480
527
pub elements : Vec < ValueExpr > ,
481
528
}
482
529
483
530
/// Represents a `CALL` expression (i.e., a function call), e.g. `LOWER("ALL CAPS")`.
484
531
#[ derive( Debug , Clone , Eq , PartialEq ) ]
532
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
485
533
pub struct CallExpr {
486
534
pub name : CallName ,
487
535
pub arguments : Vec < ValueExpr > ,
488
536
}
489
537
490
538
/// Represents a known function.
491
539
#[ derive( Debug , Clone , Eq , PartialEq ) ]
540
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
492
541
pub enum CallName {
493
542
Lower ,
494
543
Upper ,
0 commit comments