risingwave_frontend/binder/
mod.rs

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
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
// Copyright 2024 RisingWave Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::{HashMap, HashSet};
use std::sync::Arc;

use itertools::Itertools;
use parking_lot::RwLock;
use risingwave_common::catalog::FunctionId;
use risingwave_common::session_config::{SearchPath, SessionConfig};
use risingwave_common::types::DataType;
use risingwave_common::util::iter_util::ZipEqDebug;
use risingwave_sqlparser::ast::{Expr as AstExpr, SelectItem, SetExpr, Statement};

use crate::error::Result;

mod bind_context;
mod bind_param;
mod create;
mod create_view;
mod declare_cursor;
mod delete;
mod expr;
pub mod fetch_cursor;
mod for_system;
mod insert;
mod query;
mod relation;
mod select;
mod set_expr;
mod statement;
mod struct_field;
mod update;
mod values;

pub use bind_context::{BindContext, Clause, LateralBindContext};
pub use create_view::BoundCreateView;
pub use delete::BoundDelete;
pub use expr::{bind_data_type, bind_struct_field};
pub use insert::BoundInsert;
use pgwire::pg_server::{Session, SessionId};
pub use query::BoundQuery;
pub use relation::{
    BoundBackCteRef, BoundBaseTable, BoundJoin, BoundShare, BoundShareInput, BoundSource,
    BoundSystemTable, BoundWatermark, BoundWindowTableFunction, Relation,
    ResolveQualifiedNameError, WindowTableFunctionKind,
};
pub use select::{BoundDistinct, BoundSelect};
pub use set_expr::*;
pub use statement::BoundStatement;
pub use update::{BoundUpdate, UpdateProject};
pub use values::BoundValues;

use crate::catalog::catalog_service::CatalogReadGuard;
use crate::catalog::schema_catalog::SchemaCatalog;
use crate::catalog::{CatalogResult, TableId, ViewId};
use crate::error::ErrorCode;
use crate::expr::ExprImpl;
use crate::session::{AuthContext, SessionImpl, TemporarySourceManager};

pub type ShareId = usize;

/// The type of binding statement.
enum BindFor {
    /// Binding MV/SINK
    Stream,
    /// Binding a batch query
    Batch,
    /// Binding a DDL (e.g. CREATE TABLE/SOURCE)
    Ddl,
    /// Binding a system query (e.g. SHOW)
    System,
}

/// `Binder` binds the identifiers in AST to columns in relations
pub struct Binder {
    // TODO: maybe we can only lock the database, but not the whole catalog.
    catalog: CatalogReadGuard,
    db_name: String,
    session_id: SessionId,
    context: BindContext,
    auth_context: Arc<AuthContext>,
    /// A stack holding contexts of outer queries when binding a subquery.
    /// It also holds all of the lateral contexts for each respective
    /// subquery.
    ///
    /// See [`Binder::bind_subquery_expr`] for details.
    upper_subquery_contexts: Vec<(BindContext, Vec<LateralBindContext>)>,

    /// A stack holding contexts of left-lateral `TableFactor`s.
    ///
    /// We need a separate stack as `CorrelatedInputRef` depth is
    /// determined by the upper subquery context depth, not the lateral context stack depth.
    lateral_contexts: Vec<LateralBindContext>,

    next_subquery_id: usize,
    next_values_id: usize,
    /// The `ShareId` is used to identify the share relation which could be a CTE, a source, a view
    /// and so on.
    next_share_id: ShareId,

    session_config: Arc<RwLock<SessionConfig>>,

    search_path: SearchPath,
    /// The type of binding statement.
    bind_for: BindFor,

    /// `ShareId`s identifying shared views.
    shared_views: HashMap<ViewId, ShareId>,

    /// The included relations while binding a query.
    included_relations: HashSet<TableId>,

    /// The included user-defined functions while binding a query.
    included_udfs: HashSet<FunctionId>,

    param_types: ParameterTypes,

    /// The sql udf context that will be used during binding phase
    udf_context: UdfContext,

    /// The temporary sources that will be used during binding phase
    temporary_source_manager: TemporarySourceManager,
}

#[derive(Clone, Debug, Default)]
pub struct UdfContext {
    /// The mapping from `sql udf parameters` to a bound `ExprImpl` generated from `ast expressions`
    /// Note: The expressions are constructed during runtime, correspond to the actual users' input
    udf_param_context: HashMap<String, ExprImpl>,

    /// The global counter that records the calling stack depth
    /// of the current binding sql udf chain
    udf_global_counter: u32,
}

impl UdfContext {
    pub fn new() -> Self {
        Self {
            udf_param_context: HashMap::new(),
            udf_global_counter: 0,
        }
    }

    pub fn global_count(&self) -> u32 {
        self.udf_global_counter
    }

    pub fn incr_global_count(&mut self) {
        self.udf_global_counter += 1;
    }

    pub fn decr_global_count(&mut self) {
        self.udf_global_counter -= 1;
    }

    pub fn _is_empty(&self) -> bool {
        self.udf_param_context.is_empty()
    }

    pub fn update_context(&mut self, context: HashMap<String, ExprImpl>) {
        self.udf_param_context = context;
    }

    pub fn _clear(&mut self) {
        self.udf_global_counter = 0;
        self.udf_param_context.clear();
    }

    pub fn get_expr(&self, name: &str) -> Option<&ExprImpl> {
        self.udf_param_context.get(name)
    }

    pub fn get_context(&self) -> HashMap<String, ExprImpl> {
        self.udf_param_context.clone()
    }

    /// A common utility function to extract sql udf
    /// expression out from the input `ast`
    pub fn extract_udf_expression(ast: Vec<Statement>) -> Result<AstExpr> {
        if ast.len() != 1 {
            return Err(ErrorCode::InvalidInputSyntax(
                "the query for sql udf should contain only one statement".to_string(),
            )
            .into());
        }

        // Extract the expression out
        let Statement::Query(query) = ast[0].clone() else {
            return Err(ErrorCode::InvalidInputSyntax(
                "invalid function definition, please recheck the syntax".to_string(),
            )
            .into());
        };

        let SetExpr::Select(select) = query.body else {
            return Err(ErrorCode::InvalidInputSyntax(
                "missing `select` body for sql udf expression, please recheck the syntax"
                    .to_string(),
            )
            .into());
        };

        if select.projection.len() != 1 {
            return Err(ErrorCode::InvalidInputSyntax(
                "`projection` should contain only one `SelectItem`".to_string(),
            )
            .into());
        }

        let SelectItem::UnnamedExpr(expr) = select.projection[0].clone() else {
            return Err(ErrorCode::InvalidInputSyntax(
                "expect `UnnamedExpr` for `projection`".to_string(),
            )
            .into());
        };

        Ok(expr)
    }
}

/// `ParameterTypes` is used to record the types of the parameters during binding prepared stataments.
/// It works by following the rules:
/// 1. At the beginning, it contains the user specified parameters type.
/// 2. When the binder encounters a parameter, it will record it as unknown(call `record_new_param`)
///    if it didn't exist in `ParameterTypes`.
/// 3. When the binder encounters a cast on parameter, if it's a unknown type, the cast function
///    will record the target type as infer type for that parameter(call `record_infer_type`). If the
///    parameter has been inferred, the cast function will act as a normal cast.
/// 4. After bind finished:
///     (a) parameter not in `ParameterTypes` means that the user didn't specify it and it didn't
///    occur in the query. `export` will return error if there is a kind of
///    parameter. This rule is compatible with PostgreSQL
///     (b) parameter is None means that it's a unknown type. The user didn't specify it
///    and we can't infer it in the query. We will treat it as VARCHAR type finally. This rule is
///    compatible with PostgreSQL.
///     (c) parameter is Some means that it's a known type.
#[derive(Clone, Debug)]
pub struct ParameterTypes(Arc<RwLock<HashMap<u64, Option<DataType>>>>);

impl ParameterTypes {
    pub fn new(specified_param_types: Vec<Option<DataType>>) -> Self {
        let map = specified_param_types
            .into_iter()
            .enumerate()
            .map(|(index, data_type)| ((index + 1) as u64, data_type))
            .collect::<HashMap<u64, Option<DataType>>>();
        Self(Arc::new(RwLock::new(map)))
    }

    pub fn has_infer(&self, index: u64) -> bool {
        self.0.read().get(&index).unwrap().is_some()
    }

    pub fn read_type(&self, index: u64) -> Option<DataType> {
        self.0.read().get(&index).unwrap().clone()
    }

    pub fn record_new_param(&mut self, index: u64) {
        self.0.write().entry(index).or_insert(None);
    }

    pub fn record_infer_type(&mut self, index: u64, data_type: DataType) {
        assert!(
            !self.has_infer(index),
            "The parameter has been inferred, should not be inferred again."
        );
        self.0.write().get_mut(&index).unwrap().replace(data_type);
    }

    pub fn export(&self) -> Result<Vec<DataType>> {
        let types = self
            .0
            .read()
            .clone()
            .into_iter()
            .sorted_by_key(|(index, _)| *index)
            .collect::<Vec<_>>();

        // Check if all the parameters have been inferred.
        for ((index, _), expect_index) in types.iter().zip_eq_debug(1_u64..=types.len() as u64) {
            if *index != expect_index {
                return Err(ErrorCode::InvalidInputSyntax(format!(
                    "Cannot infer the type of the parameter {}.",
                    expect_index
                ))
                .into());
            }
        }

        Ok(types
            .into_iter()
            .map(|(_, data_type)| data_type.unwrap_or(DataType::Varchar))
            .collect::<Vec<_>>())
    }
}

impl Binder {
    fn new_inner(
        session: &SessionImpl,
        bind_for: BindFor,
        param_types: Vec<Option<DataType>>,
    ) -> Binder {
        Binder {
            catalog: session.env().catalog_reader().read_guard(),
            db_name: session.database().to_string(),
            session_id: session.id(),
            context: BindContext::new(),
            auth_context: session.auth_context(),
            upper_subquery_contexts: vec![],
            lateral_contexts: vec![],
            next_subquery_id: 0,
            next_values_id: 0,
            next_share_id: 0,
            session_config: session.shared_config(),
            search_path: session.config().search_path(),
            bind_for,
            shared_views: HashMap::new(),
            included_relations: HashSet::new(),
            included_udfs: HashSet::new(),
            param_types: ParameterTypes::new(param_types),
            udf_context: UdfContext::new(),
            temporary_source_manager: session.temporary_source_manager(),
        }
    }

    pub fn new(session: &SessionImpl) -> Binder {
        Self::new_inner(session, BindFor::Batch, vec![])
    }

    pub fn new_with_param_types(
        session: &SessionImpl,
        param_types: Vec<Option<DataType>>,
    ) -> Binder {
        Self::new_inner(session, BindFor::Batch, param_types)
    }

    pub fn new_for_stream(session: &SessionImpl) -> Binder {
        Self::new_inner(session, BindFor::Stream, vec![])
    }

    pub fn new_for_ddl(session: &SessionImpl) -> Binder {
        Self::new_inner(session, BindFor::Ddl, vec![])
    }

    pub fn new_for_system(session: &SessionImpl) -> Binder {
        Self::new_inner(session, BindFor::System, vec![])
    }

    pub fn new_for_stream_with_param_types(
        session: &SessionImpl,
        param_types: Vec<Option<DataType>>,
    ) -> Binder {
        Self::new_inner(session, BindFor::Stream, param_types)
    }

    fn is_for_stream(&self) -> bool {
        matches!(self.bind_for, BindFor::Stream)
    }

    #[expect(dead_code)]
    fn is_for_batch(&self) -> bool {
        matches!(self.bind_for, BindFor::Batch)
    }

    fn is_for_ddl(&self) -> bool {
        matches!(self.bind_for, BindFor::Ddl)
    }

    /// Bind a [`Statement`].
    pub fn bind(&mut self, stmt: Statement) -> Result<BoundStatement> {
        self.bind_statement(stmt)
    }

    pub fn export_param_types(&self) -> Result<Vec<DataType>> {
        self.param_types.export()
    }

    /// Get included relations in the query after binding. This is used for resolving relation
    /// dependencies. Note that it only contains referenced relations discovered during binding.
    /// After the plan is built, the referenced relations may be changed. We cannot rely on the
    /// collection result of plan, because we still need to record the dependencies that have been
    /// optimised away.
    pub fn included_relations(&self) -> &HashSet<TableId> {
        &self.included_relations
    }

    /// Get included user-defined functions in the query after binding.
    pub fn included_udfs(&self) -> &HashSet<FunctionId> {
        &self.included_udfs
    }

    fn push_context(&mut self) {
        let new_context = std::mem::take(&mut self.context);
        self.context
            .cte_to_relation
            .clone_from(&new_context.cte_to_relation);
        let new_lateral_contexts = std::mem::take(&mut self.lateral_contexts);
        self.upper_subquery_contexts
            .push((new_context, new_lateral_contexts));
    }

    fn pop_context(&mut self) -> Result<()> {
        let (old_context, old_lateral_contexts) = self
            .upper_subquery_contexts
            .pop()
            .ok_or_else(|| ErrorCode::InternalError("Popping non-existent context".to_string()))?;
        self.context = old_context;
        self.lateral_contexts = old_lateral_contexts;
        Ok(())
    }

    fn push_lateral_context(&mut self) {
        let new_context = std::mem::take(&mut self.context);
        self.context
            .cte_to_relation
            .clone_from(&new_context.cte_to_relation);
        self.lateral_contexts.push(LateralBindContext {
            is_visible: false,
            context: new_context,
        });
    }

    fn pop_and_merge_lateral_context(&mut self) -> Result<()> {
        let mut old_context = self
            .lateral_contexts
            .pop()
            .ok_or_else(|| ErrorCode::InternalError("Popping non-existent context".to_string()))?
            .context;
        old_context.merge_context(self.context.clone())?;
        self.context = old_context;
        Ok(())
    }

    fn try_mark_lateral_as_visible(&mut self) {
        if let Some(mut ctx) = self.lateral_contexts.pop() {
            ctx.is_visible = true;
            self.lateral_contexts.push(ctx);
        }
    }

    fn try_mark_lateral_as_invisible(&mut self) {
        if let Some(mut ctx) = self.lateral_contexts.pop() {
            ctx.is_visible = false;
            self.lateral_contexts.push(ctx);
        }
    }

    fn next_subquery_id(&mut self) -> usize {
        let id = self.next_subquery_id;
        self.next_subquery_id += 1;
        id
    }

    fn next_values_id(&mut self) -> usize {
        let id = self.next_values_id;
        self.next_values_id += 1;
        id
    }

    fn next_share_id(&mut self) -> ShareId {
        let id = self.next_share_id;
        self.next_share_id += 1;
        id
    }

    fn first_valid_schema(&self) -> CatalogResult<&SchemaCatalog> {
        self.catalog.first_valid_schema(
            &self.db_name,
            &self.search_path,
            &self.auth_context.user_name,
        )
    }

    pub fn set_clause(&mut self, clause: Option<Clause>) {
        self.context.clause = clause;
    }

    pub fn udf_context_mut(&mut self) -> &mut UdfContext {
        &mut self.udf_context
    }
}

/// The column name stored in [`BindContext`] for a column without an alias.
pub const UNNAMED_COLUMN: &str = "?column?";
/// The table name stored in [`BindContext`] for a subquery without an alias.
const UNNAMED_SUBQUERY: &str = "?subquery?";
/// The table name stored in [`BindContext`] for a column group.
const COLUMN_GROUP_PREFIX: &str = "?column_group_id?";

#[cfg(test)]
pub mod test_utils {
    use risingwave_common::types::DataType;

    use super::Binder;
    use crate::session::SessionImpl;

    #[cfg(test)]
    pub fn mock_binder() -> Binder {
        Binder::new(&SessionImpl::mock())
    }

    #[cfg(test)]
    pub fn mock_binder_with_param_types(param_types: Vec<Option<DataType>>) -> Binder {
        Binder::new_with_param_types(&SessionImpl::mock(), param_types)
    }
}

#[cfg(test)]
mod tests {
    use expect_test::expect;

    use super::test_utils::*;

    #[tokio::test]
    async fn test_rcte() {
        let stmt = risingwave_sqlparser::parser::Parser::parse_sql(
            "WITH RECURSIVE t1 AS (SELECT 1 AS a UNION ALL SELECT a + 1 FROM t1 WHERE a < 10) SELECT * FROM t1",
        ).unwrap().into_iter().next().unwrap();
        let mut binder = mock_binder();
        let bound = binder.bind(stmt).unwrap();

        let expected = expect![[r#"
            Query(
                BoundQuery {
                    body: Select(
                        BoundSelect {
                            distinct: All,
                            select_items: [
                                InputRef(
                                    InputRef {
                                        index: 0,
                                        data_type: Int32,
                                    },
                                ),
                            ],
                            aliases: [
                                Some(
                                    "a",
                                ),
                            ],
                            from: Some(
                                Share(
                                    BoundShare {
                                        share_id: 0,
                                        input: Query(
                                            Right(
                                                RecursiveUnion {
                                                    all: true,
                                                    base: Select(
                                                        BoundSelect {
                                                            distinct: All,
                                                            select_items: [
                                                                Literal(
                                                                    Literal {
                                                                        data: Some(
                                                                            Int32(
                                                                                1,
                                                                            ),
                                                                        ),
                                                                        data_type: Some(
                                                                            Int32,
                                                                        ),
                                                                    },
                                                                ),
                                                            ],
                                                            aliases: [
                                                                Some(
                                                                    "a",
                                                                ),
                                                            ],
                                                            from: None,
                                                            where_clause: None,
                                                            group_by: GroupKey(
                                                                [],
                                                            ),
                                                            having: None,
                                                            schema: Schema {
                                                                fields: [
                                                                    a:Int32,
                                                                ],
                                                            },
                                                        },
                                                    ),
                                                    recursive: Select(
                                                        BoundSelect {
                                                            distinct: All,
                                                            select_items: [
                                                                FunctionCall(
                                                                    FunctionCall {
                                                                        func_type: Add,
                                                                        return_type: Int32,
                                                                        inputs: [
                                                                            InputRef(
                                                                                InputRef {
                                                                                    index: 0,
                                                                                    data_type: Int32,
                                                                                },
                                                                            ),
                                                                            Literal(
                                                                                Literal {
                                                                                    data: Some(
                                                                                        Int32(
                                                                                            1,
                                                                                        ),
                                                                                    ),
                                                                                    data_type: Some(
                                                                                        Int32,
                                                                                    ),
                                                                                },
                                                                            ),
                                                                        ],
                                                                    },
                                                                ),
                                                            ],
                                                            aliases: [
                                                                None,
                                                            ],
                                                            from: Some(
                                                                BackCteRef(
                                                                    BoundBackCteRef {
                                                                        share_id: 0,
                                                                        base: Select(
                                                                            BoundSelect {
                                                                                distinct: All,
                                                                                select_items: [
                                                                                    Literal(
                                                                                        Literal {
                                                                                            data: Some(
                                                                                                Int32(
                                                                                                    1,
                                                                                                ),
                                                                                            ),
                                                                                            data_type: Some(
                                                                                                Int32,
                                                                                            ),
                                                                                        },
                                                                                    ),
                                                                                ],
                                                                                aliases: [
                                                                                    Some(
                                                                                        "a",
                                                                                    ),
                                                                                ],
                                                                                from: None,
                                                                                where_clause: None,
                                                                                group_by: GroupKey(
                                                                                    [],
                                                                                ),
                                                                                having: None,
                                                                                schema: Schema {
                                                                                    fields: [
                                                                                        a:Int32,
                                                                                    ],
                                                                                },
                                                                            },
                                                                        ),
                                                                    },
                                                                ),
                                                            ),
                                                            where_clause: Some(
                                                                FunctionCall(
                                                                    FunctionCall {
                                                                        func_type: LessThan,
                                                                        return_type: Boolean,
                                                                        inputs: [
                                                                            InputRef(
                                                                                InputRef {
                                                                                    index: 0,
                                                                                    data_type: Int32,
                                                                                },
                                                                            ),
                                                                            Literal(
                                                                                Literal {
                                                                                    data: Some(
                                                                                        Int32(
                                                                                            10,
                                                                                        ),
                                                                                    ),
                                                                                    data_type: Some(
                                                                                        Int32,
                                                                                    ),
                                                                                },
                                                                            ),
                                                                        ],
                                                                    },
                                                                ),
                                                            ),
                                                            group_by: GroupKey(
                                                                [],
                                                            ),
                                                            having: None,
                                                            schema: Schema {
                                                                fields: [
                                                                    ?column?:Int32,
                                                                ],
                                                            },
                                                        },
                                                    ),
                                                    schema: Schema {
                                                        fields: [
                                                            a:Int32,
                                                        ],
                                                    },
                                                },
                                            ),
                                        ),
                                    },
                                ),
                            ),
                            where_clause: None,
                            group_by: GroupKey(
                                [],
                            ),
                            having: None,
                            schema: Schema {
                                fields: [
                                    a:Int32,
                                ],
                            },
                        },
                    ),
                    order: [],
                    limit: None,
                    offset: None,
                    with_ties: false,
                    extra_order_exprs: [],
                },
            )"#]];

        expected.assert_eq(&format!("{:#?}", bound));
    }

    #[tokio::test]
    async fn test_bind_approx_percentile() {
        let stmt = risingwave_sqlparser::parser::Parser::parse_sql(
            "SELECT approx_percentile(0.5, 0.01) WITHIN GROUP (ORDER BY generate_series) FROM generate_series(1, 100)",
        ).unwrap().into_iter().next().unwrap();
        let parse_expected = expect![[r#"
            Query(
                Query {
                    with: None,
                    body: Select(
                        Select {
                            distinct: All,
                            projection: [
                                UnnamedExpr(
                                    Function(
                                        Function {
                                            scalar_as_agg: false,
                                            name: ObjectName(
                                                [
                                                    Ident {
                                                        value: "approx_percentile",
                                                        quote_style: None,
                                                    },
                                                ],
                                            ),
                                            arg_list: FunctionArgList {
                                                distinct: false,
                                                args: [
                                                    Unnamed(
                                                        Expr(
                                                            Value(
                                                                Number(
                                                                    "0.5",
                                                                ),
                                                            ),
                                                        ),
                                                    ),
                                                    Unnamed(
                                                        Expr(
                                                            Value(
                                                                Number(
                                                                    "0.01",
                                                                ),
                                                            ),
                                                        ),
                                                    ),
                                                ],
                                                variadic: false,
                                                order_by: [],
                                                ignore_nulls: false,
                                            },
                                            within_group: Some(
                                                OrderByExpr {
                                                    expr: Identifier(
                                                        Ident {
                                                            value: "generate_series",
                                                            quote_style: None,
                                                        },
                                                    ),
                                                    asc: None,
                                                    nulls_first: None,
                                                },
                                            ),
                                            filter: None,
                                            over: None,
                                        },
                                    ),
                                ),
                            ],
                            from: [
                                TableWithJoins {
                                    relation: TableFunction {
                                        name: ObjectName(
                                            [
                                                Ident {
                                                    value: "generate_series",
                                                    quote_style: None,
                                                },
                                            ],
                                        ),
                                        alias: None,
                                        args: [
                                            Unnamed(
                                                Expr(
                                                    Value(
                                                        Number(
                                                            "1",
                                                        ),
                                                    ),
                                                ),
                                            ),
                                            Unnamed(
                                                Expr(
                                                    Value(
                                                        Number(
                                                            "100",
                                                        ),
                                                    ),
                                                ),
                                            ),
                                        ],
                                        with_ordinality: false,
                                    },
                                    joins: [],
                                },
                            ],
                            lateral_views: [],
                            selection: None,
                            group_by: [],
                            having: None,
                        },
                    ),
                    order_by: [],
                    limit: None,
                    offset: None,
                    fetch: None,
                },
            )"#]];
        parse_expected.assert_eq(&format!("{:#?}", stmt));

        let mut binder = mock_binder();
        let bound = binder.bind(stmt).unwrap();

        let expected = expect![[r#"
            Query(
                BoundQuery {
                    body: Select(
                        BoundSelect {
                            distinct: All,
                            select_items: [
                                AggCall(
                                    AggCall {
                                        agg_type: Builtin(
                                            ApproxPercentile,
                                        ),
                                        return_type: Float64,
                                        args: [
                                            FunctionCall(
                                                FunctionCall {
                                                    func_type: Cast,
                                                    return_type: Float64,
                                                    inputs: [
                                                        InputRef(
                                                            InputRef {
                                                                index: 0,
                                                                data_type: Int32,
                                                            },
                                                        ),
                                                    ],
                                                },
                                            ),
                                        ],
                                        filter: Condition {
                                            conjunctions: [],
                                        },
                                        distinct: false,
                                        order_by: OrderBy {
                                            sort_exprs: [
                                                OrderByExpr {
                                                    expr: InputRef(
                                                        InputRef {
                                                            index: 0,
                                                            data_type: Int32,
                                                        },
                                                    ),
                                                    order_type: OrderType {
                                                        direction: Ascending,
                                                        nulls_are: Largest,
                                                    },
                                                },
                                            ],
                                        },
                                        direct_args: [
                                            Literal {
                                                data: Some(
                                                    Float64(
                                                        OrderedFloat(
                                                            0.5,
                                                        ),
                                                    ),
                                                ),
                                                data_type: Some(
                                                    Float64,
                                                ),
                                            },
                                            Literal {
                                                data: Some(
                                                    Float64(
                                                        OrderedFloat(
                                                            0.01,
                                                        ),
                                                    ),
                                                ),
                                                data_type: Some(
                                                    Float64,
                                                ),
                                            },
                                        ],
                                    },
                                ),
                            ],
                            aliases: [
                                Some(
                                    "approx_percentile",
                                ),
                            ],
                            from: Some(
                                TableFunction {
                                    expr: TableFunction(
                                        FunctionCall {
                                            function_type: GenerateSeries,
                                            return_type: Int32,
                                            args: [
                                                Literal(
                                                    Literal {
                                                        data: Some(
                                                            Int32(
                                                                1,
                                                            ),
                                                        ),
                                                        data_type: Some(
                                                            Int32,
                                                        ),
                                                    },
                                                ),
                                                Literal(
                                                    Literal {
                                                        data: Some(
                                                            Int32(
                                                                100,
                                                            ),
                                                        ),
                                                        data_type: Some(
                                                            Int32,
                                                        ),
                                                    },
                                                ),
                                            ],
                                        },
                                    ),
                                    with_ordinality: false,
                                },
                            ),
                            where_clause: None,
                            group_by: GroupKey(
                                [],
                            ),
                            having: None,
                            schema: Schema {
                                fields: [
                                    approx_percentile:Float64,
                                ],
                            },
                        },
                    ),
                    order: [],
                    limit: None,
                    offset: None,
                    with_ties: false,
                    extra_order_exprs: [],
                },
            )"#]];

        expected.assert_eq(&format!("{:#?}", bound));
    }
}