risingwave_common/types/
to_sql.rs

1// Copyright 2025 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::error::Error;
16
17use bytes::BytesMut;
18use postgres_types::{IsNull, ToSql, Type, to_sql_checked};
19use risingwave_common::types::ScalarRefImpl;
20
21use crate::types::ScalarImpl;
22
23impl ToSql for ScalarImpl {
24    to_sql_checked!();
25
26    fn to_sql(&self, ty: &Type, out: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>>
27    where
28        Self: Sized,
29    {
30        match self {
31            ScalarImpl::Int16(v) => v.to_sql(ty, out),
32            ScalarImpl::Int32(v) => v.to_sql(ty, out),
33            ScalarImpl::Int64(v) => v.to_sql(ty, out),
34            ScalarImpl::Serial(v) => v.to_sql(ty, out),
35            ScalarImpl::Float32(v) => v.to_sql(ty, out),
36            ScalarImpl::Float64(v) => v.to_sql(ty, out),
37            ScalarImpl::Utf8(v) => v.to_sql(ty, out),
38            ScalarImpl::Bool(v) => v.to_sql(ty, out),
39            ScalarImpl::Decimal(v) => v.to_sql(ty, out),
40            ScalarImpl::Interval(v) => v.to_sql(ty, out),
41            ScalarImpl::Date(v) => v.to_sql(ty, out),
42            ScalarImpl::Timestamp(v) => v.to_sql(ty, out),
43            ScalarImpl::Timestamptz(v) => v.to_sql(ty, out),
44            ScalarImpl::Time(v) => v.to_sql(ty, out),
45            ScalarImpl::Bytea(v) => (&**v).to_sql(ty, out),
46            ScalarImpl::Jsonb(v) => v.to_sql(ty, out),
47            ScalarImpl::Vector(_) => todo!("VECTOR_PLACEHOLDER"),
48            ScalarImpl::Int256(_) | ScalarImpl::Struct(_) | ScalarImpl::List(_) => {
49                bail_not_implemented!("the postgres encoding for {ty} is unsupported")
50            }
51            ScalarImpl::Map(_) => todo!(),
52        }
53    }
54
55    // return true to accept all types
56    fn accepts(_ty: &Type) -> bool
57    where
58        Self: Sized,
59    {
60        true
61    }
62}
63
64impl ToSql for ScalarRefImpl<'_> {
65    to_sql_checked!();
66
67    fn to_sql(&self, ty: &Type, out: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>>
68    where
69        Self: Sized,
70    {
71        match self {
72            ScalarRefImpl::Int16(v) => v.to_sql(ty, out),
73            ScalarRefImpl::Int32(v) => v.to_sql(ty, out),
74            ScalarRefImpl::Int64(v) => v.to_sql(ty, out),
75            ScalarRefImpl::Serial(v) => v.to_sql(ty, out),
76            ScalarRefImpl::Float32(v) => v.to_sql(ty, out),
77            ScalarRefImpl::Float64(v) => v.to_sql(ty, out),
78            ScalarRefImpl::Utf8(v) => v.to_sql(ty, out),
79            ScalarRefImpl::Bool(v) => v.to_sql(ty, out),
80            ScalarRefImpl::Decimal(v) => v.to_sql(ty, out),
81            ScalarRefImpl::Interval(v) => v.to_sql(ty, out),
82            ScalarRefImpl::Date(v) => v.to_sql(ty, out),
83            ScalarRefImpl::Timestamp(v) => v.to_sql(ty, out),
84            ScalarRefImpl::Timestamptz(v) => v.to_sql(ty, out),
85            ScalarRefImpl::Time(v) => v.to_sql(ty, out),
86            ScalarRefImpl::Bytea(v) => (&**v).to_sql(ty, out),
87            ScalarRefImpl::Jsonb(v) => v.to_sql(ty, out),
88            ScalarRefImpl::Vector(_) => todo!("VECTOR_PLACEHOLDER"),
89            ScalarRefImpl::Int256(_) | ScalarRefImpl::Struct(_) | ScalarRefImpl::List(_) => {
90                bail_not_implemented!("the postgres encoding for {ty} is unsupported")
91            }
92            ScalarRefImpl::Map(_) => todo!(),
93        }
94    }
95
96    // return true to accept all types
97    fn accepts(_ty: &Type) -> bool
98    where
99        Self: Sized,
100    {
101        true
102    }
103}