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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

//! Transformations of SQL ASTs.
//!
//! Most query optimizations are performed by the dataflow layer, but some
//! are much easier to perform in SQL. Someday, we'll want our own SQL IR,
//! but for now we just use the parser's AST directly.

use mz_ore::stack::{CheckedRecursion, RecursionGuard};
use mz_repr::namespaces::{MZ_CATALOG_SCHEMA, MZ_UNSAFE_SCHEMA, PG_CATALOG_SCHEMA};
use mz_sql_parser::ast::visit_mut::{self, VisitMut, VisitMutNode};
use mz_sql_parser::ast::{
    Expr, Function, FunctionArgs, HomogenizingFunction, Ident, IsExprConstruct, Op, OrderByExpr,
    Query, Select, SelectItem, TableAlias, TableFactor, TableWithJoins, Value, WindowSpec,
};
use mz_sql_parser::ident;
use uuid::Uuid;

use crate::names::{Aug, PartialItemName, ResolvedDataType, ResolvedItemName};
use crate::normalize;
use crate::plan::{PlanError, StatementContext};

pub(crate) fn transform<N>(scx: &StatementContext, node: &mut N) -> Result<(), PlanError>
where
    N: for<'a> VisitMutNode<'a, Aug>,
{
    let mut func_rewriter = FuncRewriter::new(scx);
    node.visit_mut(&mut func_rewriter);
    func_rewriter.status?;

    let mut desugarer = Desugarer::new(scx);
    node.visit_mut(&mut desugarer);
    desugarer.status
}

// Transforms various functions to forms that are more easily handled by the
// planner.
//
// Specifically:
//
//   * Rewrites the `mod` function to the `%` binary operator, so the modulus
//     code only needs to handle the operator form.
//
//   * Rewrites the `nullif` function to a `CASE` statement, to reuse the code
//     for planning equality of datums.
//
//   * Rewrites `avg(col)` to `sum(col) / count(col)`, so that we can pretend
//     the `avg` aggregate function doesn't exist from here on out. This also
//     has the nice side effect of reusing the division planning logic, which
//     is not trivial for some types, like decimals.
//
//   * Rewrites the suite of standard deviation and variance functions in a
//     manner similar to `avg`.
//
// TODO(sploiselle): rewrite these in terms of func::sql_op!
struct FuncRewriter<'a> {
    scx: &'a StatementContext<'a>,
    status: Result<(), PlanError>,
    rewriting_table_factor: bool,
}

impl<'a> FuncRewriter<'a> {
    fn new(scx: &'a StatementContext<'a>) -> FuncRewriter<'a> {
        FuncRewriter {
            scx,
            status: Ok(()),
            rewriting_table_factor: false,
        }
    }

    fn resolve_known_valid_data_type(&self, name: &PartialItemName) -> ResolvedDataType {
        let item = self
            .scx
            .catalog
            .resolve_type(name)
            .expect("data type known to be valid");
        let full_name = self.scx.catalog.resolve_full_name(item.name());
        ResolvedDataType::Named {
            id: item.id(),
            qualifiers: item.name().qualifiers.clone(),
            full_name,
            modifiers: vec![],
            print_id: true,
        }
    }

    fn int32_data_type(&self) -> ResolvedDataType {
        self.resolve_known_valid_data_type(&PartialItemName {
            database: None,
            schema: Some(PG_CATALOG_SCHEMA.into()),
            item: "int4".into(),
        })
    }

    // Divides `lhs` by `rhs` but replaces division-by-zero errors with NULL;
    // note that this is semantically equivalent to `NULLIF(rhs, 0)`.
    fn plan_divide(lhs: Expr<Aug>, rhs: Expr<Aug>) -> Expr<Aug> {
        lhs.divide(Expr::Case {
            operand: None,
            conditions: vec![rhs.clone().equals(Expr::number("0"))],
            results: vec![Expr::null()],
            else_result: Some(Box::new(rhs)),
        })
    }

    fn plan_agg(
        &mut self,
        name: ResolvedItemName,
        expr: Expr<Aug>,
        order_by: Vec<OrderByExpr<Aug>>,
        filter: Option<Box<Expr<Aug>>>,
        distinct: bool,
        over: Option<WindowSpec<Aug>>,
    ) -> Expr<Aug> {
        if self.rewriting_table_factor && self.status.is_ok() {
            self.status = Err(PlanError::Unstructured(
                "aggregate functions are not supported in functions in FROM".to_string(),
            ))
        }
        Expr::Function(Function {
            name,
            args: FunctionArgs::Args {
                args: vec![expr],
                order_by,
            },
            filter,
            over,
            distinct,
        })
    }

    fn plan_avg(
        &mut self,
        expr: Expr<Aug>,
        filter: Option<Box<Expr<Aug>>>,
        distinct: bool,
        over: Option<WindowSpec<Aug>>,
    ) -> Expr<Aug> {
        let sum = self
            .plan_agg(
                self.scx
                    .dangerous_resolve_name(vec![PG_CATALOG_SCHEMA, "sum"]),
                expr.clone(),
                vec![],
                filter.clone(),
                distinct,
                over.clone(),
            )
            .call_unary(
                self.scx
                    .dangerous_resolve_name(vec![MZ_UNSAFE_SCHEMA, "mz_avg_promotion"]),
            );
        let count = self.plan_agg(
            self.scx
                .dangerous_resolve_name(vec![PG_CATALOG_SCHEMA, "count"]),
            expr,
            vec![],
            filter,
            distinct,
            over,
        );
        Self::plan_divide(sum, count)
    }

    /// Same as `plan_avg` but internally uses `mz_avg_promotion_internal_v1`.
    fn plan_avg_internal_v1(
        &mut self,
        expr: Expr<Aug>,
        filter: Option<Box<Expr<Aug>>>,
        distinct: bool,
        over: Option<WindowSpec<Aug>>,
    ) -> Expr<Aug> {
        let sum = self
            .plan_agg(
                self.scx
                    .dangerous_resolve_name(vec![PG_CATALOG_SCHEMA, "sum"]),
                expr.clone(),
                vec![],
                filter.clone(),
                distinct,
                over.clone(),
            )
            .call_unary(
                self.scx
                    .dangerous_resolve_name(vec![MZ_UNSAFE_SCHEMA, "mz_avg_promotion_internal_v1"]),
            );
        let count = self.plan_agg(
            self.scx
                .dangerous_resolve_name(vec![PG_CATALOG_SCHEMA, "count"]),
            expr,
            vec![],
            filter,
            distinct,
            over,
        );
        Self::plan_divide(sum, count)
    }

    fn plan_variance(
        &mut self,
        expr: Expr<Aug>,
        filter: Option<Box<Expr<Aug>>>,
        distinct: bool,
        sample: bool,
        over: Option<WindowSpec<Aug>>,
    ) -> Expr<Aug> {
        // N.B. this variance calculation uses the "textbook" algorithm, which
        // is known to accumulate problematic amounts of error. The numerically
        // stable variants, the most well-known of which is Welford's, are
        // however difficult to implement inside of Differential Dataflow, as
        // they do not obviously support retractions efficiently (#1240).
        //
        // The code below converts var_samp(x) into
        //
        //     (sum(x²) - sum(x)² / count(x)) / (count(x) - 1)
        //
        // and var_pop(x) into:
        //
        //     (sum(x²) - sum(x)² / count(x)) / count(x)
        //
        let expr = expr.call_unary(
            self.scx
                .dangerous_resolve_name(vec![MZ_UNSAFE_SCHEMA, "mz_avg_promotion"]),
        );
        let expr_squared = expr.clone().multiply(expr.clone());
        let sum_squares = self.plan_agg(
            self.scx
                .dangerous_resolve_name(vec![PG_CATALOG_SCHEMA, "sum"]),
            expr_squared,
            vec![],
            filter.clone(),
            distinct,
            over.clone(),
        );
        let sum = self.plan_agg(
            self.scx
                .dangerous_resolve_name(vec![PG_CATALOG_SCHEMA, "sum"]),
            expr.clone(),
            vec![],
            filter.clone(),
            distinct,
            over.clone(),
        );
        let sum_squared = sum.clone().multiply(sum);
        let count = self.plan_agg(
            self.scx
                .dangerous_resolve_name(vec![PG_CATALOG_SCHEMA, "count"]),
            expr,
            vec![],
            filter,
            distinct,
            over,
        );
        let result = Self::plan_divide(
            sum_squares.minus(Self::plan_divide(sum_squared, count.clone())),
            if sample {
                count.minus(Expr::number("1"))
            } else {
                count
            },
        );
        // Result is _basically_ what we want, except
        // that due to numerical inaccuracy, it might be a negative
        // number very close to zero when it should mathematically be zero.
        // This makes it so `stddev` fails as it tries to take the square root
        // of a negative number.
        // So, we need the following logic:
        // If `result` is NULL, return NULL (no surprise here)
        // Otherwise, if `result` is >0, return `result` (no surprise here either)
        // Otherwise, return 0.
        //
        // Unfortunately, we can't use `GREATEST` directly for this,
        // since `greatest(NULL, 0)` is 0, not NULL, so we need to
        // create a `Case` expression that computes `result`
        // twice. Hopefully the optimizer can deal with this!
        let result_is_null = Expr::IsExpr {
            expr: Box::new(result.clone()),
            construct: IsExprConstruct::Null,
            negated: false,
        };
        Expr::Case {
            operand: None,
            conditions: vec![result_is_null],
            results: vec![Expr::Value(Value::Null)],
            else_result: Some(Box::new(Expr::HomogenizingFunction {
                function: HomogenizingFunction::Greatest,
                exprs: vec![result, Expr::number("0")],
            })),
        }
    }

    fn plan_stddev(
        &mut self,
        expr: Expr<Aug>,
        filter: Option<Box<Expr<Aug>>>,
        distinct: bool,
        sample: bool,
        over: Option<WindowSpec<Aug>>,
    ) -> Expr<Aug> {
        self.plan_variance(expr, filter, distinct, sample, over)
            .call_unary(
                self.scx
                    .dangerous_resolve_name(vec![PG_CATALOG_SCHEMA, "sqrt"]),
            )
    }

    fn plan_bool_and(
        &mut self,
        expr: Expr<Aug>,
        filter: Option<Box<Expr<Aug>>>,
        distinct: bool,
        over: Option<WindowSpec<Aug>>,
    ) -> Expr<Aug> {
        // The code below converts `bool_and(x)` into:
        //
        //     sum((NOT x)::int4) = 0
        //
        // It is tempting to use `count` instead, but count does not return NULL
        // when all input values are NULL, as required.
        //
        // The `NOT x` expression has the side effect of implicitly casting `x`
        // to `bool`. We intentionally do not write `NOT x::bool`, because that
        // would perform an explicit cast, and to match PostgreSQL we must
        // perform only an implicit cast.
        let sum = self.plan_agg(
            self.scx
                .dangerous_resolve_name(vec![PG_CATALOG_SCHEMA, "sum"]),
            expr.negate().cast(self.int32_data_type()),
            vec![],
            filter,
            distinct,
            over,
        );
        sum.equals(Expr::Value(Value::Number(0.to_string())))
    }

    fn plan_bool_or(
        &mut self,
        expr: Expr<Aug>,
        filter: Option<Box<Expr<Aug>>>,
        distinct: bool,
        over: Option<WindowSpec<Aug>>,
    ) -> Expr<Aug> {
        // The code below converts `bool_or(x)`z into:
        //
        //     sum((x OR false)::int4) > 0
        //
        // It is tempting to use `count` instead, but count does not return NULL
        // when all input values are NULL, as required.
        //
        // The `(x OR false)` expression implicitly casts `x` to `bool` without
        // changing its logical value. It is tempting to use `x::bool` instead,
        // but that performs an explicit cast, and to match PostgreSQL we must
        // perform only an implicit cast.
        let sum = self.plan_agg(
            self.scx
                .dangerous_resolve_name(vec![PG_CATALOG_SCHEMA, "sum"]),
            expr.or(Expr::Value(Value::Boolean(false)))
                .cast(self.int32_data_type()),
            vec![],
            filter,
            distinct,
            over,
        );
        sum.gt(Expr::Value(Value::Number(0.to_string())))
    }

    fn rewrite_function(&mut self, func: &Function<Aug>) -> Option<(Ident, Expr<Aug>)> {
        if let Function {
            name,
            args: FunctionArgs::Args { args, order_by: _ },
            filter,
            distinct,
            over,
        } = func
        {
            let pg_catalog_id = self
                .scx
                .catalog
                .resolve_schema(None, PG_CATALOG_SCHEMA)
                .expect("pg_catalog schema exists")
                .id();
            let mz_catalog_id = self
                .scx
                .catalog
                .resolve_schema(None, MZ_CATALOG_SCHEMA)
                .expect("mz_catalog schema exists")
                .id();
            let name = match name {
                ResolvedItemName::Item {
                    qualifiers,
                    full_name,
                    ..
                } => {
                    if ![*pg_catalog_id, *mz_catalog_id].contains(&qualifiers.schema_spec) {
                        return None;
                    }
                    full_name.item.clone()
                }
                _ => unreachable!(),
            };

            let filter = filter.clone();
            let distinct = *distinct;
            let over = over.clone();
            let expr = if args.len() == 1 {
                let arg = args[0].clone();
                match name.as_str() {
                    "avg_internal_v1" => self.plan_avg_internal_v1(arg, filter, distinct, over),
                    "avg" => self.plan_avg(arg, filter, distinct, over),
                    "variance" | "var_samp" => {
                        self.plan_variance(arg, filter, distinct, true, over)
                    }
                    "var_pop" => self.plan_variance(arg, filter, distinct, false, over),
                    "stddev" | "stddev_samp" => self.plan_stddev(arg, filter, distinct, true, over),
                    "stddev_pop" => self.plan_stddev(arg, filter, distinct, false, over),
                    "bool_and" => self.plan_bool_and(arg, filter, distinct, over),
                    "bool_or" => self.plan_bool_or(arg, filter, distinct, over),
                    _ => return None,
                }
            } else if args.len() == 2 {
                let (lhs, rhs) = (args[0].clone(), args[1].clone());
                match name.as_str() {
                    "mod" => lhs.modulo(rhs),
                    "pow" => Expr::call(
                        self.scx
                            .dangerous_resolve_name(vec![PG_CATALOG_SCHEMA, "power"]),
                        vec![lhs, rhs],
                    ),
                    _ => return None,
                }
            } else {
                return None;
            };
            Some((Ident::new_unchecked(name), expr))
        } else {
            None
        }
    }

    fn rewrite_expr(&mut self, expr: &Expr<Aug>) -> Option<(Ident, Expr<Aug>)> {
        match expr {
            Expr::Function(function) => self.rewrite_function(function),
            // Rewrites special keywords that SQL considers to be function calls
            // to actual function calls. For example, `SELECT current_timestamp`
            // is rewritten to `SELECT current_timestamp()`.
            Expr::Identifier(ident) if ident.len() == 1 => {
                let ident = normalize::ident(ident[0].clone());
                let fn_ident = match ident.as_str() {
                    "current_role" => Some("current_user"),
                    "current_schema" | "current_timestamp" | "current_user" | "session_user" => {
                        Some(ident.as_str())
                    }
                    _ => None,
                };
                match fn_ident {
                    None => None,
                    Some(fn_ident) => {
                        let expr = Expr::call_nullary(
                            self.scx
                                .dangerous_resolve_name(vec![PG_CATALOG_SCHEMA, fn_ident]),
                        );
                        Some((Ident::new_unchecked(ident), expr))
                    }
                }
            }
            _ => None,
        }
    }
}

impl<'ast> VisitMut<'ast, Aug> for FuncRewriter<'_> {
    fn visit_select_item_mut(&mut self, item: &'ast mut SelectItem<Aug>) {
        if let SelectItem::Expr { expr, alias: None } = item {
            visit_mut::visit_expr_mut(self, expr);
            if let Some((alias, expr)) = self.rewrite_expr(expr) {
                *item = SelectItem::Expr {
                    expr,
                    alias: Some(alias),
                };
            }
        } else {
            visit_mut::visit_select_item_mut(self, item);
        }
    }

    fn visit_table_with_joins_mut(&mut self, item: &'ast mut TableWithJoins<Aug>) {
        visit_mut::visit_table_with_joins_mut(self, item);
        match &mut item.relation {
            TableFactor::Function {
                function,
                alias,
                with_ordinality,
            } => {
                self.rewriting_table_factor = true;
                // Functions that get rewritten must be rewritten as exprs
                // because their catalog functions cannot be planned.
                if let Some((ident, expr)) = self.rewrite_function(function) {
                    let mut select = Select::default().project(SelectItem::Expr {
                        expr,
                        alias: Some(match &alias {
                            Some(TableAlias { name, columns, .. }) => {
                                columns.get(0).unwrap_or(name).clone()
                            }
                            None => ident,
                        }),
                    });

                    if *with_ordinality {
                        select = select.project(SelectItem::Expr {
                            expr: Expr::Value(Value::Number("1".into())),
                            alias: Some(ident!("ordinality")),
                        });
                    }

                    item.relation = TableFactor::Derived {
                        lateral: false,
                        subquery: Box::new(Query {
                            ctes: mz_sql_parser::ast::CteBlock::Simple(vec![]),
                            body: mz_sql_parser::ast::SetExpr::Select(Box::new(select)),
                            order_by: vec![],
                            limit: None,
                            offset: None,
                        }),
                        alias: alias.clone(),
                    }
                }
                self.rewriting_table_factor = false;
            }
            _ => {}
        }
    }

    fn visit_expr_mut(&mut self, expr: &'ast mut Expr<Aug>) {
        visit_mut::visit_expr_mut(self, expr);
        if let Some((_name, new_expr)) = self.rewrite_expr(expr) {
            *expr = new_expr;
        }
    }
}

/// Removes syntax sugar to simplify the planner.
///
/// For example, `<expr> NOT IN (<subquery>)` is rewritten to `expr <> ALL
/// (<subquery>)`.
struct Desugarer<'a> {
    scx: &'a StatementContext<'a>,
    status: Result<(), PlanError>,
    recursion_guard: RecursionGuard,
}

impl<'a> CheckedRecursion for Desugarer<'a> {
    fn recursion_guard(&self) -> &RecursionGuard {
        &self.recursion_guard
    }
}

impl<'a, 'ast> VisitMut<'ast, Aug> for Desugarer<'a> {
    fn visit_expr_mut(&mut self, expr: &'ast mut Expr<Aug>) {
        self.visit_internal(Self::visit_expr_mut_internal, expr);
    }
}

impl<'a> Desugarer<'a> {
    fn visit_internal<F, X>(&mut self, f: F, x: X)
    where
        F: Fn(&mut Self, X) -> Result<(), PlanError>,
    {
        if self.status.is_ok() {
            // self.status could have changed from a deeper call, so don't blindly
            // overwrite it with the result of this call.
            let status = self.checked_recur_mut(|d| f(d, x));
            if self.status.is_ok() {
                self.status = status;
            }
        }
    }

    fn new(scx: &'a StatementContext) -> Desugarer<'a> {
        Desugarer {
            scx,
            status: Ok(()),
            recursion_guard: RecursionGuard::with_limit(1024), // chosen arbitrarily
        }
    }

    fn visit_expr_mut_internal(&mut self, expr: &mut Expr<Aug>) -> Result<(), PlanError> {
        // `($expr)` => `$expr`
        while let Expr::Nested(e) = expr {
            *expr = e.take();
        }

        // `$expr BETWEEN $low AND $high` => `$expr >= $low AND $expr <= $low`
        // `$expr NOT BETWEEN $low AND $high` => `$expr < $low OR $expr > $low`
        if let Expr::Between {
            expr: e,
            low,
            high,
            negated,
        } = expr
        {
            if *negated {
                *expr = Expr::lt(*e.clone(), low.take()).or(e.take().gt(high.take()));
            } else {
                *expr = e.clone().gt_eq(low.take()).and(e.take().lt_eq(high.take()));
            }
        }

        // `$expr IN ($subquery)` => `$expr = ANY ($subquery)`
        // `$expr NOT IN ($subquery)` => `$expr <> ALL ($subquery)`
        if let Expr::InSubquery {
            expr: e,
            subquery,
            negated,
        } = expr
        {
            if *negated {
                *expr = Expr::AllSubquery {
                    left: Box::new(e.take()),
                    op: Op::bare("<>"),
                    right: Box::new(subquery.take()),
                };
            } else {
                *expr = Expr::AnySubquery {
                    left: Box::new(e.take()),
                    op: Op::bare("="),
                    right: Box::new(subquery.take()),
                };
            }
        }

        // `$expr = ALL ($array_expr)`
        // =>
        // `$expr = ALL (SELECT elem FROM unnest($array_expr) _ (elem))`
        //
        // and analogously for other operators and ANY.
        if let Expr::AnyExpr { left, op, right } | Expr::AllExpr { left, op, right } = expr {
            let binding = ident!("elem");

            let subquery = Query::select(
                Select::default()
                    .from(TableWithJoins {
                        relation: TableFactor::Function {
                            function: Function {
                                name: self
                                    .scx
                                    .dangerous_resolve_name(vec![MZ_CATALOG_SCHEMA, "unnest"]),
                                args: FunctionArgs::args(vec![right.take()]),
                                filter: None,
                                over: None,
                                distinct: false,
                            },
                            alias: Some(TableAlias {
                                name: ident!("_"),
                                columns: vec![binding.clone()],
                                strict: true,
                            }),
                            with_ordinality: false,
                        },
                        joins: vec![],
                    })
                    .project(SelectItem::Expr {
                        expr: Expr::Identifier(vec![binding]),
                        alias: None,
                    }),
            );

            let left = Box::new(left.take());

            let op = op.clone();

            *expr = match expr {
                Expr::AnyExpr { .. } => Expr::AnySubquery {
                    left,
                    op,
                    right: Box::new(subquery),
                },
                Expr::AllExpr { .. } => Expr::AllSubquery {
                    left,
                    op,
                    right: Box::new(subquery),
                },
                _ => unreachable!(),
            };
        }

        // `$expr = ALL ($subquery)`
        // =>
        // `(SELECT mz_unsafe.mz_all($expr = $binding) FROM ($subquery) AS _ ($binding))
        //
        // and analogously for other operators and ANY.
        if let Expr::AnySubquery { left, op, right } | Expr::AllSubquery { left, op, right } = expr
        {
            let left = match &mut **left {
                Expr::Row { .. } => left.take(),
                _ => Expr::Row {
                    exprs: vec![left.take()],
                },
            };

            let arity = match &left {
                Expr::Row { exprs } => exprs.len(),
                _ => unreachable!(),
            };

            let bindings: Vec<_> = (0..arity)
                // Note: using unchecked is okay here because we know the value will be less than
                // our maximum length.
                .map(|_| Ident::new_unchecked(format!("right_{}", Uuid::new_v4())))
                .collect();

            let select = Select::default()
                .from(TableWithJoins::subquery(
                    right.take(),
                    TableAlias {
                        name: ident!("subquery"),
                        columns: bindings.clone(),
                        strict: true,
                    },
                ))
                .project(SelectItem::Expr {
                    expr: left
                        .binop(
                            op.clone(),
                            Expr::Row {
                                exprs: bindings
                                    .into_iter()
                                    .map(|b| Expr::Identifier(vec![b]))
                                    .collect(),
                            },
                        )
                        .call_unary(self.scx.dangerous_resolve_name(match expr {
                            Expr::AnySubquery { .. } => vec![MZ_UNSAFE_SCHEMA, "mz_any"],
                            Expr::AllSubquery { .. } => vec![MZ_UNSAFE_SCHEMA, "mz_all"],
                            _ => unreachable!(),
                        })),
                    alias: None,
                });

            *expr = Expr::Subquery(Box::new(Query::select(select)));
        }

        // Expands row comparisons.
        //
        // ROW($l1, $l2, ..., $ln) = ROW($r1, $r2, ..., $rn)
        // =>
        // $l1 = $r1 AND $l2 = $r2 AND ... AND $ln = $rn
        //
        // ROW($l1, $l2, ..., $ln) < ROW($r1, $r2, ..., $rn)
        // =>
        // $l1 < $r1 OR ($l1 = $r1 AND ($l2 < $r2 OR ($l2 = $r2 AND ... ($ln < $rn))))
        //
        // ROW($l1, $l2, ..., $ln) <= ROW($r1, $r2, ..., $rn)
        // =>
        // $l1 < $r1 OR ($l1 = $r1 AND ($l2 < $r2 OR ($l2 = $r2 AND ... ($ln <= $rn))))
        //
        // and analogously for the inverse operations !=, >, and >=.
        if let Expr::Op {
            op,
            expr1: left,
            expr2: Some(right),
        } = expr
        {
            if let (Expr::Row { exprs: left }, Expr::Row { exprs: right }) =
                (&mut **left, &mut **right)
            {
                if matches!(normalize::op(op)?, "=" | "<>" | "<" | "<=" | ">" | ">=") {
                    if left.len() != right.len() {
                        sql_bail!("unequal number of entries in row expressions");
                    }
                    if left.is_empty() {
                        assert!(right.is_empty());
                        sql_bail!("cannot compare rows of zero length");
                    }
                }
                match normalize::op(op)? {
                    "=" | "<>" => {
                        let mut pairs = left.iter_mut().zip(right);
                        let mut new = pairs
                            .next()
                            .map(|(l, r)| l.take().equals(r.take()))
                            .expect("cannot compare rows of zero length");
                        for (l, r) in pairs {
                            new = l.take().equals(r.take()).and(new);
                        }
                        if normalize::op(op)? == "<>" {
                            new = new.negate();
                        }
                        *expr = new;
                    }
                    "<" | "<=" | ">" | ">=" => {
                        let strict_op = match normalize::op(op)? {
                            "<" | "<=" => "<",
                            ">" | ">=" => ">",
                            _ => unreachable!(),
                        };
                        let (l, r) = (left.last_mut().unwrap(), right.last_mut().unwrap());
                        let mut new = l.take().binop(op.clone(), r.take());
                        for (l, r) in left.iter_mut().zip(right).rev().skip(1) {
                            new = l
                                .clone()
                                .binop(Op::bare(strict_op), r.clone())
                                .or(l.take().equals(r.take()).and(new));
                        }
                        *expr = new;
                    }
                    _ if left.len() == 1 && right.len() == 1 => {
                        let left = left.remove(0);
                        let right = right.remove(0);
                        *expr = left.binop(op.clone(), right);
                    }
                    _ => (),
                }
            }
        }

        visit_mut::visit_expr_mut(self, expr);
        Ok(())
    }
}