risingwave_frontend/expr/
parameter.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
// 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::fmt::{Debug, Formatter};
use std::hash::{Hash, Hasher};

use risingwave_common::types::DataType;

use super::Expr;
use crate::binder::ParameterTypes;

#[derive(Clone)]
pub struct Parameter {
    pub index: u64,
    param_types: ParameterTypes,
}

impl Debug for Parameter {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Parameter(index: {}, type: {:?})",
            self.index,
            self.param_types.read_type(self.index)
        )
    }
}

impl PartialEq for Parameter {
    fn eq(&self, other: &Self) -> bool {
        self.index == other.index
    }
}

impl Eq for Parameter {}

impl Hash for Parameter {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.index.hash(state);
    }
}

impl Expr for Parameter {
    fn return_type(&self) -> DataType {
        self.param_types
            .read_type(self.index)
            .unwrap_or(DataType::Varchar)
    }

    fn to_expr_proto(&self) -> risingwave_pb::expr::ExprNode {
        unreachable!("Parameter should not be serialized to ExprNode")
    }
}

impl Parameter {
    pub fn new(index: u64, mut param_types: ParameterTypes) -> Self {
        param_types.record_new_param(index);
        Self { index, param_types }
    }

    pub fn has_infer(&self) -> bool {
        self.param_types.has_infer(self.index)
    }

    pub fn cast_infer_type(&mut self, data_type: DataType) {
        self.param_types.record_infer_type(self.index, data_type);
    }
}