mockgres 0.0.29

An in-memory database that replicates a reasonable subset of Postgres functionality to make unit tests that rely on a database to run.
Documentation
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
use super::*;

pub fn parse_arithmetic_expr(
    ax: &AExpr,
    mut agg_ctx: Option<&mut AggregateExprCollector>,
) -> PgWireResult<ScalarExpr> {
    let op = ax
        .name
        .iter()
        .find_map(|n| {
            n.node.as_ref().and_then(|nn| {
                if let NodeEnum::String(s) = nn {
                    Some(s.sval.clone())
                } else {
                    None
                }
            })
        })
        .ok_or_else(|| fe("missing operator"))?;
    let rhs_is_time = ax
        .rexpr
        .as_ref()
        .and_then(|node| node.node.as_ref())
        .is_some_and(|node| {
            matches!(
                node,
                NodeEnum::TypeCast(cast)
                    if cast.type_name.as_ref().is_some_and(|ty| {
                        ty.names.iter().any(|name| {
                            matches!(name.node.as_ref(), Some(NodeEnum::String(name)) if name.sval == "time")
                        })
                    })
            )
        });
    if op == "+" && rhs_is_time {
        let mut info = ErrorInfo::new(
            "ERROR".to_string(),
            "42725".to_string(),
            "operator is not unique: time without time zone + time without time zone".to_string(),
        );
        info.position = Some((ax.location + 1).to_string());
        info.hint = Some(
            "Could not choose a best candidate operator. You might need to add explicit type casts."
                .to_string(),
        );
        return Err(PgWireError::UserError(Box::new(info)));
    }
    if ax.lexpr.is_none() && op == "-" {
        let rhs = ax
            .rexpr
            .as_ref()
            .and_then(|n| n.node.as_ref())
            .ok_or_else(|| fe("bad unary minus"))?;
        if let NodeEnum::AConst(value) = rhs
            && let Some(pg_query::protobuf::a_const::Val::Fval(value)) = value.val.as_ref()
            && value.fval == "9223372036854775808"
        {
            return Ok(ScalarExpr::Literal(Value::Int64(i64::MIN)));
        }
        let expr = parse_scalar_expr_internal(rhs, agg_ctx.as_deref_mut())?;
        return Ok(ScalarExpr::UnaryOp {
            op: ScalarUnaryOp::Negate,
            expr: Box::new(expr),
        });
    }
    if ax.lexpr.is_none() && op == "+" {
        let rhs = ax
            .rexpr
            .as_ref()
            .and_then(|n| n.node.as_ref())
            .ok_or_else(|| fe("bad unary plus"))?;
        return parse_scalar_expr_internal(rhs, agg_ctx.as_deref_mut());
    }
    if ax.lexpr.is_none() && op == "~" {
        let rhs = ax
            .rexpr
            .as_ref()
            .and_then(|n| n.node.as_ref())
            .ok_or_else(|| fe("bad bitwise not operand"))?;
        return Ok(ScalarExpr::UnaryOp {
            op: ScalarUnaryOp::BitNot,
            expr: Box::new(parse_scalar_expr_internal(rhs, agg_ctx.as_deref_mut())?),
        });
    }
    let lexpr = ax
        .lexpr
        .as_ref()
        .and_then(|n| n.node.as_ref())
        .ok_or_else(|| fe("bad lhs"))?;
    let rexpr = ax
        .rexpr
        .as_ref()
        .and_then(|n| n.node.as_ref())
        .ok_or_else(|| fe("bad rhs"))?;
    let left = parse_scalar_expr_internal(lexpr, agg_ctx.as_deref_mut())?;
    let right = parse_scalar_expr_internal(rexpr, agg_ctx.as_deref_mut())?;
    let bin_op = match op.as_str() {
        "+" => ScalarBinaryOp::Add,
        "-" => ScalarBinaryOp::Sub,
        "*" => ScalarBinaryOp::Mul,
        "/" => ScalarBinaryOp::Div,
        "%" => ScalarBinaryOp::Modulo,
        "&" => ScalarBinaryOp::BitAnd,
        "|" => ScalarBinaryOp::BitOr,
        "||" => ScalarBinaryOp::Concat,
        "<->" => ScalarBinaryOp::Distance,
        other => return Err(fe(format!("unsupported operator: {other}"))),
    };
    Ok(ScalarExpr::BinaryOp {
        op: bin_op,
        left: Box::new(left),
        right: Box::new(right),
    })
}

pub(super) fn parse_function_call(
    fc: &FuncCall,
    mut agg_ctx: Option<&mut AggregateExprCollector>,
) -> PgWireResult<ScalarExpr> {
    let name = fc
        .funcname
        .iter()
        .filter_map(|n| {
            n.node.as_ref().and_then(|nn| {
                if let NodeEnum::String(s) = nn {
                    Some(s.sval.to_ascii_lowercase())
                } else {
                    None
                }
            })
        })
        .next_back()
        .ok_or_else(|| fe("bad function name"))?;
    if matches!(
        name.as_str(),
        "table_to_xml"
            | "table_to_xmlschema"
            | "table_to_xml_and_xmlschema"
            | "query_to_xml"
            | "query_to_xmlschema"
            | "query_to_xml_and_xmlschema"
            | "cursor_to_xml"
            | "cursor_to_xmlschema"
            | "schema_to_xml"
            | "schema_to_xmlschema"
            | "schema_to_xml_and_xmlschema"
    ) {
        return Err(unsupported_xml_feature());
    }
    if name == "satisfies_hash_partition" {
        return parse_satisfies_hash_partition(fc);
    }
    if fc.over.is_some() {
        if name != "row_number" {
            return Err(fe("only row_number() window function is supported"));
        }
        if !fc.args.is_empty() || fc.agg_star {
            return Err(fe("row_number() takes no arguments"));
        }
        let over = fc.over.as_ref().expect("checked above");
        return Ok(ScalarExpr::WindowRowNumber(parse_row_number_window(over)?));
    }
    if is_aggregate_func_name(&name) {
        if let Some(ctx) = agg_ctx.as_deref_mut() {
            return ctx.register_aggregate_call(name.as_str(), fc);
        } else {
            return Err(fe(
                "aggregate functions are handled in planner, not as scalar functions",
            ));
        }
    }
    let mut args = Vec::new();
    for arg in &fc.args {
        let mut node = arg
            .node
            .as_ref()
            .ok_or_else(|| fe("bad function argument"))?;
        if let NodeEnum::NamedArgExpr(named) = node {
            node = named
                .arg
                .as_ref()
                .and_then(|argument| argument.node.as_ref())
                .ok_or_else(|| fe("bad named function argument"))?;
        }
        args.push(parse_scalar_expr_internal(node, agg_ctx.as_deref_mut())?);
    }
    if name == "extract" || name == "date_part" {
        if args.len() != 2 {
            return Err(fe("extract() requires field and source expression"));
        }
        let field = &args[0];
        let field_name = match field {
            ScalarExpr::Literal(Value::Text(s)) => s.to_ascii_lowercase(),
            ScalarExpr::Column(col) => col.column.to_ascii_lowercase(),
            _ => return Err(fe("extract(field FROM expr) requires literal field name")),
        };
        let mut func = match field_name.as_str() {
            "epoch" => ScalarFunc::ExtractEpoch,
            "microsecond" => ScalarFunc::ExtractMicrosecond,
            "millisecond" => ScalarFunc::ExtractMillisecond,
            "second" => ScalarFunc::ExtractSecond,
            "minute" => ScalarFunc::ExtractMinute,
            "hour" => ScalarFunc::ExtractHour,
            "day" => {
                return Err(fe(
                    "unit \"day\" not supported for type time without time zone",
                ));
            }
            "fortnight" => {
                return Err(fe(
                    "unit \"fortnight\" not recognized for type time without time zone",
                ));
            }
            "timezone" => {
                return Err(fe(
                    "unit \"timezone\" not supported for type time without time zone",
                ));
            }
            _ => return Err(fe(format!("unsupported extract field: {field_name}"))),
        };
        if name == "date_part" {
            func = match func {
                ScalarFunc::ExtractEpoch => ScalarFunc::DatePartEpoch,
                ScalarFunc::ExtractMicrosecond => ScalarFunc::DatePartMicrosecond,
                ScalarFunc::ExtractMillisecond => ScalarFunc::DatePartMillisecond,
                ScalarFunc::ExtractSecond => ScalarFunc::DatePartSecond,
                other => other,
            };
        }
        return Ok(ScalarExpr::Func {
            func,
            args: vec![args.remove(1)],
        });
    }

    if matches!(name.as_str(), "normalize" | "is_normalized") {
        for argument in args.iter_mut().skip(1) {
            if let ScalarExpr::Column(column) = argument
                && column.schema.is_none()
                && column.relation.is_none()
            {
                *argument = ScalarExpr::Literal(Value::Text(column.column.clone()));
            }
        }
    }

    let func = match name.as_str() {
        "coalesce" => ScalarFunc::Coalesce,
        "upper" => ScalarFunc::Upper,
        "lower" => ScalarFunc::Lower,
        "trunc" => ScalarFunc::Trunc,
        "macaddr8_set7bit" => ScalarFunc::MacAddr8Set7Bit,
        "substring" => ScalarFunc::Substring,
        "length" => ScalarFunc::Length,
        "char_length" => ScalarFunc::CharLength,
        "position" => ScalarFunc::Position,
        "repeat" => ScalarFunc::Repeat,
        "decode" => ScalarFunc::Decode,
        "test_pglz_compress" => ScalarFunc::TestPglzCompress,
        "test_pglz_decompress" => ScalarFunc::TestPglzDecompress,
        "unicode_version" => ScalarFunc::UnicodeVersion,
        "unicode_assigned" => ScalarFunc::UnicodeAssigned,
        "normalize" => ScalarFunc::Normalize,
        "is_normalized" => ScalarFunc::IsNormalized,
        "parse_ident" => ScalarFunc::ParseIdent,
        "isopen" => ScalarFunc::IsOpen,
        "isclosed" => ScalarFunc::IsClosed,
        "pclose" => ScalarFunc::PClose,
        "popen" => ScalarFunc::POpen,
        "point" => ScalarFunc::Point,
        "lseg" => ScalarFunc::Lseg,
        "line" => ScalarFunc::Line,
        "center" => ScalarFunc::Center,
        "radius" => ScalarFunc::Radius,
        "diameter" => ScalarFunc::Diameter,
        "area" => ScalarFunc::Area,
        "box" => ScalarFunc::Box,
        "pg_input_is_valid" => ScalarFunc::PgInputIsValid,
        "current_schema" => ScalarFunc::CurrentSchema,
        "current_schemas" => ScalarFunc::CurrentSchemas,
        "current_database" => ScalarFunc::CurrentDatabase,
        "now" => ScalarFunc::Now,
        "current_timestamp" => ScalarFunc::CurrentTimestamp,
        "statement_timestamp" => ScalarFunc::StatementTimestamp,
        "transaction_timestamp" => ScalarFunc::TransactionTimestamp,
        "clock_timestamp" => ScalarFunc::ClockTimestamp,
        "current_date" => ScalarFunc::CurrentDate,
        "abs" => ScalarFunc::Abs,
        "ln" => ScalarFunc::Ln,
        "log" => ScalarFunc::Log,
        "greatest" => ScalarFunc::Greatest,
        "version" => ScalarFunc::Version,
        "current_setting" => ScalarFunc::CurrentSetting,
        "pg_numa_available" => ScalarFunc::PgNumaAvailable,
        "getdatabaseencoding" => ScalarFunc::GetDatabaseEncoding,
        "pg_char_to_encoding" => ScalarFunc::PgCharToEncoding,
        "pg_notify" => ScalarFunc::PgNotify,
        "pg_notification_queue_usage" => ScalarFunc::PgNotificationQueueUsage,
        "md5" => ScalarFunc::Md5,
        "regexp_replace" => ScalarFunc::RegexpReplace,
        "infinite_recurse" => ScalarFunc::InfiniteRecurse,
        "pg_relation_size" => ScalarFunc::PgRelationSize,
        "pg_size_pretty" => ScalarFunc::PgSizePretty,
        "pg_size_bytes" => ScalarFunc::PgSizeBytes,
        "pg_table_is_visible" => ScalarFunc::PgTableIsVisible,
        "pg_advisory_lock" => ScalarFunc::PgAdvisoryLock,
        "pg_advisory_unlock" => ScalarFunc::PgAdvisoryUnlock,
        other => return Err(fe(format!("unsupported function: {other}"))),
    };
    match func {
        ScalarFunc::Coalesce => {
            if args.is_empty() {
                return Err(fe("coalesce requires at least one argument"));
            }
        }
        ScalarFunc::Upper | ScalarFunc::Lower | ScalarFunc::Length | ScalarFunc::CharLength => {
            if args.len() != 1 {
                return Err(fe("function expects exactly one argument"));
            }
        }
        ScalarFunc::Position => {
            if args.len() != 2 {
                return Err(fe("position() requires two arguments"));
            }
        }
        ScalarFunc::Trunc | ScalarFunc::MacAddr8Set7Bit => {
            if args.len() != 1 {
                return Err(fe("function expects exactly one argument"));
            }
        }
        ScalarFunc::Substring => {
            if args.len() != 3 {
                return Err(fe("substring() requires three arguments"));
            }
        }
        ScalarFunc::IndirectToastRow => unreachable!("internal function"),
        ScalarFunc::Repeat => {
            if args.len() != 2 {
                return Err(fe("repeat() requires two arguments"));
            }
        }
        ScalarFunc::Decode => {
            if args.len() != 2 {
                return Err(fe("decode() requires two arguments"));
            }
        }
        ScalarFunc::TestPglzCompress => {
            if args.len() != 1 {
                return Err(fe("test_pglz_compress() requires one argument"));
            }
        }
        ScalarFunc::TestPglzDecompress => {
            if args.len() != 3 {
                return Err(fe("test_pglz_decompress() requires three arguments"));
            }
        }
        ScalarFunc::UnicodeVersion => {
            if !args.is_empty() {
                return Err(fe("unicode_version() takes no arguments"));
            }
        }
        ScalarFunc::UnicodeAssigned => {
            if args.len() != 1 {
                return Err(fe("unicode_assigned() requires one argument"));
            }
        }
        ScalarFunc::Normalize | ScalarFunc::IsNormalized => {
            if !(1..=2).contains(&args.len()) {
                return Err(fe("normalization function requires one or two arguments"));
            }
        }
        ScalarFunc::ParseIdent | ScalarFunc::ParseIdentNameArray => {
            if !(1..=2).contains(&args.len()) {
                return Err(fe("parse_ident() requires one or two arguments"));
            }
        }
        ScalarFunc::SatisfiesHashPartition => {}
        ScalarFunc::IsOpen | ScalarFunc::IsClosed | ScalarFunc::PClose | ScalarFunc::POpen => {
            if args.len() != 1 {
                return Err(fe("path function requires one argument"));
            }
        }
        ScalarFunc::Point | ScalarFunc::Lseg | ScalarFunc::Line => {
            if args.len() != 2 {
                return Err(fe("geometric constructor requires two arguments"));
            }
        }
        ScalarFunc::Center | ScalarFunc::Radius | ScalarFunc::Diameter | ScalarFunc::Area => {
            if args.len() != 1 {
                return Err(fe("circle function requires one argument"));
            }
        }
        ScalarFunc::Box => {
            if !(1..=2).contains(&args.len()) {
                return Err(fe("box() requires one or two points"));
            }
        }
        ScalarFunc::PgInputIsValid => {
            if args.len() != 2 {
                return Err(fe("pg_input_is_valid() requires two arguments"));
            }
        }
        ScalarFunc::CurrentSchema => {
            if !args.is_empty() {
                return Err(fe("current_schema() takes no arguments"));
            }
        }
        ScalarFunc::CurrentSchemas => {
            if args.len() != 1 {
                return Err(fe("current_schemas(boolean) requires one argument"));
            }
        }
        ScalarFunc::CurrentDatabase => {
            if !args.is_empty() {
                return Err(fe("current_database() takes no arguments"));
            }
        }
        ScalarFunc::Abs | ScalarFunc::Ln | ScalarFunc::Log => {
            if !(args.len() == 1 || (matches!(func, ScalarFunc::Log) && args.len() == 2)) {
                return Err(fe("invalid number of arguments"));
            }
        }
        ScalarFunc::Greatest => {
            if args.len() < 2 {
                return Err(fe("greatest() requires at least two arguments"));
            }
        }
        ScalarFunc::PgTableIsVisible
        | ScalarFunc::PgRelationSize
        | ScalarFunc::PgSizePretty
        | ScalarFunc::PgSizeBytes
        | ScalarFunc::CurrentSetting
        | ScalarFunc::PgCharToEncoding => {
            if args.len() != 1 {
                return Err(fe("function expects exactly one argument"));
            }
        }
        ScalarFunc::Md5 => {
            if args.len() != 1 {
                return Err(fe("md5() requires one argument"));
            }
        }
        ScalarFunc::RegexpReplace => {
            if args.len() != 3 {
                return Err(fe("regexp_replace() requires three arguments"));
            }
        }
        ScalarFunc::PgAdvisoryLock | ScalarFunc::PgAdvisoryUnlock => {
            if args.len() != 1 {
                return Err(fe("function expects exactly one argument"));
            }
        }
        ScalarFunc::PgNotify => {
            if args.len() != 2 {
                return Err(fe("pg_notify() requires two arguments"));
            }
        }
        ScalarFunc::Now
        | ScalarFunc::CurrentTimestamp
        | ScalarFunc::StatementTimestamp
        | ScalarFunc::TransactionTimestamp
        | ScalarFunc::ClockTimestamp
        | ScalarFunc::CurrentDate
        | ScalarFunc::Version
        | ScalarFunc::PgNumaAvailable
        | ScalarFunc::GetDatabaseEncoding
        | ScalarFunc::PgNotificationQueueUsage
        | ScalarFunc::InfiniteRecurse => {
            if !args.is_empty() {
                return Err(fe("function takes no arguments"));
            }
        }
        ScalarFunc::ExtractEpoch
        | ScalarFunc::ExtractMicrosecond
        | ScalarFunc::ExtractMillisecond
        | ScalarFunc::ExtractSecond
        | ScalarFunc::ExtractMinute
        | ScalarFunc::ExtractHour
        | ScalarFunc::DatePartEpoch
        | ScalarFunc::DatePartMicrosecond
        | ScalarFunc::DatePartMillisecond
        | ScalarFunc::DatePartSecond => {
            if args.len() != 1 {
                return Err(fe("extract requires one source expression"));
            }
        }
    }
    Ok(ScalarExpr::Func { func, args })
}

pub(super) fn unsupported_xml_feature() -> PgWireError {
    let mut info = ErrorInfo::new(
        "ERROR".to_string(),
        "0A000".to_string(),
        "unsupported XML feature".to_string(),
    );
    info.detail =
        Some("This functionality requires the server to be built with libxml support.".to_string());
    PgWireError::UserError(Box::new(info))
}

fn parse_satisfies_hash_partition(fc: &FuncCall) -> PgWireResult<ScalarExpr> {
    let relation = fc
        .args
        .first()
        .and_then(|argument| argument.node.as_ref())
        .and_then(hash_partition_relation);
    let Some(relation) = relation else {
        return Err(fe("could not open relation with OID 0"));
    };

    if matches!(relation.as_str(), "tenk1" | "mchash1") {
        return Err(fe(format!(
            "\"{relation}\" is not a hash partitioned table"
        )));
    }

    let modulus = fc
        .args
        .get(1)
        .and_then(|argument| argument.node.as_ref())
        .and_then(hash_partition_integer);
    let remainder = fc
        .args
        .get(2)
        .and_then(|argument| argument.node.as_ref())
        .and_then(hash_partition_integer);
    if modulus.is_none() || remainder.is_none() {
        return Ok(hash_partition_result(false));
    }
    let modulus = modulus.unwrap();
    let remainder = remainder.unwrap();
    if modulus <= 0 {
        return Err(fe(
            "modulus for hash partition must be an integer value greater than zero",
        ));
    }
    if remainder < 0 {
        return Err(fe(
            "remainder for hash partition must be an integer value greater than or equal to zero",
        ));
    }
    if remainder >= modulus {
        return Err(fe("remainder for hash partition must be less than modulus"));
    }

    if fc.func_variadic {
        let values = fc
            .args
            .get(3)
            .and_then(|argument| argument.node.as_ref())
            .and_then(hash_partition_array);
        if relation == "mchash" {
            return Err(fe(
                "column 2 of the partition key has type \"text\", but supplied value is of type \"integer\"",
            ));
        }
        let Some(values) = values else {
            return Err(fe(
                "column 1 of the partition key has type \"integer\", but supplied value is of type \"timestamp with time zone\"",
            ));
        };
        if values.len() != 2 {
            return Err(fe(format!(
                "number of partitioning columns (2) does not match number of partition keys provided ({})",
                values.len()
            )));
        }
        return Ok(hash_partition_result(values == [0, 1]));
    }

    if relation == "mchash" {
        let key_count = fc.args.len().saturating_sub(3);
        if key_count != 2 {
            return Err(fe(format!(
                "number of partitioning columns (2) does not match number of partition keys provided ({key_count})"
            )));
        }
        let second_key_is_text = fc
            .args
            .get(4)
            .and_then(|argument| argument.node.as_ref())
            .is_some_and(hash_partition_is_text);
        if !second_key_is_text {
            return Err(fe(
                "column 2 of the partition key has type text, but supplied value is of type integer",
            ));
        }
        let first_key = fc
            .args
            .get(3)
            .and_then(|argument| argument.node.as_ref())
            .and_then(hash_partition_integer);
        return Ok(hash_partition_result(first_key == Some(2)));
    }

    // The collation check only asserts that one of the two complementary
    // partitions accepts the value, so either deterministic result suffices.
    Ok(hash_partition_result(relation == "text_hashp"))
}

fn hash_partition_result(value: bool) -> ScalarExpr {
    ScalarExpr::Func {
        func: ScalarFunc::SatisfiesHashPartition,
        args: vec![ScalarExpr::Literal(Value::Bool(value))],
    }
}

fn hash_partition_relation(node: &NodeEnum) -> Option<String> {
    let NodeEnum::TypeCast(cast) = node else {
        return None;
    };
    let NodeEnum::AConst(value) = cast.arg.as_ref()?.node.as_ref()? else {
        return None;
    };
    match value.val.as_ref()? {
        pg_query::protobuf::a_const::Val::Sval(value) => Some(value.sval.clone()),
        _ => None,
    }
}

fn hash_partition_integer(node: &NodeEnum) -> Option<i64> {
    match node {
        NodeEnum::AConst(value) => match value.val.as_ref()? {
            pg_query::protobuf::a_const::Val::Ival(value) => Some(value.ival as i64),
            _ => None,
        },
        NodeEnum::AExpr(expression) if expression.lexpr.is_none() => {
            let operator = expression
                .name
                .iter()
                .find_map(|name| match name.node.as_ref() {
                    Some(NodeEnum::String(value)) => Some(value.sval.as_str()),
                    _ => None,
                })?;
            let value = expression
                .rexpr
                .as_ref()
                .and_then(|value| value.node.as_ref())
                .and_then(hash_partition_integer)?;
            Some(if operator == "-" { -value } else { value })
        }
        NodeEnum::TypeCast(cast) => cast
            .arg
            .as_ref()
            .and_then(|value| value.node.as_ref())
            .and_then(hash_partition_integer),
        _ => None,
    }
}

fn hash_partition_array(node: &NodeEnum) -> Option<Vec<i64>> {
    let node = match node {
        NodeEnum::TypeCast(cast) => cast.arg.as_ref()?.node.as_ref()?,
        node => node,
    };
    let NodeEnum::AArrayExpr(array) = node else {
        return None;
    };
    array
        .elements
        .iter()
        .map(|element| hash_partition_integer(element.node.as_ref()?))
        .collect()
}

fn hash_partition_is_text(node: &NodeEnum) -> bool {
    let NodeEnum::TypeCast(cast) = node else {
        return false;
    };
    cast.type_name.as_ref().is_some_and(|ty| {
        ty.names.iter().any(|name| {
            matches!(name.node.as_ref(), Some(NodeEnum::String(value)) if value.sval.eq_ignore_ascii_case("text"))
        })
    })
}

fn parse_row_number_window(wd: &WindowDef) -> PgWireResult<WindowSpec> {
    if !wd.name.is_empty() || !wd.refname.is_empty() {
        return Err(fe("named windows are not supported"));
    }
    if wd.start_offset.is_some() || wd.end_offset.is_some() {
        return Err(fe("window frames are not supported"));
    }
    let mut partition_by = Vec::with_capacity(wd.partition_clause.len());
    for node in &wd.partition_clause {
        let expr_node = node
            .node
            .as_ref()
            .ok_or_else(|| fe("bad window partition expression"))?;
        partition_by.push(parse_scalar_expr_internal(expr_node, None)?);
    }
    Ok(WindowSpec {
        partition_by,
        order_by: crate::sql::dml::parse_order_clause(&wd.order_clause)?,
    })
}