risingwave_common/types/
from_sql.rs1use postgres_types::{FromSql, Type};
16use risingwave_common::types::{
17 Date, Interval, JsonbVal, ScalarImpl, Time, Timestamp, Timestamptz,
18};
19
20impl<'a> FromSql<'a> for ScalarImpl {
21 fn from_sql(
22 ty: &Type,
23 raw: &'a [u8],
24 ) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
25 Ok(match *ty {
26 Type::BOOL => ScalarImpl::from(bool::from_sql(ty, raw)?),
27 Type::INT2 => ScalarImpl::from(i16::from_sql(ty, raw)?),
28 Type::INT4 => ScalarImpl::from(i32::from_sql(ty, raw)?),
29 Type::INT8 => ScalarImpl::from(i64::from_sql(ty, raw)?),
30 Type::FLOAT4 => ScalarImpl::from(f32::from_sql(ty, raw)?),
31 Type::FLOAT8 => ScalarImpl::from(f64::from_sql(ty, raw)?),
32 Type::DATE => ScalarImpl::from(Date::from_sql(ty, raw)?),
33 Type::TIME => ScalarImpl::from(Time::from_sql(ty, raw)?),
34 Type::TIMESTAMP => ScalarImpl::from(Timestamp::from_sql(ty, raw)?),
35 Type::TIMESTAMPTZ => ScalarImpl::from(Timestamptz::from_sql(ty, raw)?),
36 Type::JSON | Type::JSONB => ScalarImpl::from(JsonbVal::from_sql(ty, raw)?),
37 Type::INTERVAL => ScalarImpl::from(Interval::from_sql(ty, raw)?),
38 Type::BYTEA => ScalarImpl::from(Vec::<u8>::from_sql(ty, raw)?.into_boxed_slice()),
39 Type::VARCHAR | Type::TEXT | Type::BPCHAR => {
40 ScalarImpl::from(String::from_sql(ty, raw)?)
41 }
42 ref ty
43 if (ty.name() == "citext"
44 || ty.name() == "ltree"
45 || ty.name() == "lquery"
46 || ty.name() == "ltxtquery") =>
47 {
48 ScalarImpl::from(String::from_sql(ty, raw)?)
49 }
50 ref ty if ty.name() == "geometry" => {
52 ScalarImpl::from(Vec::<u8>::from_sql(ty, raw)?.into_boxed_slice())
53 }
54 _ => {
57 bail_not_implemented!("the postgres decoding for {ty} is unsupported")
58 }
59 })
60 }
61
62 fn accepts(ty: &Type) -> bool {
63 matches!(
64 *ty,
65 Type::BOOL
66 | Type::INT2
67 | Type::INT4
68 | Type::INT8
69 | Type::FLOAT4
70 | Type::FLOAT8
71 | Type::DATE
72 | Type::TIME
73 | Type::TIMESTAMP
74 | Type::TIMESTAMPTZ
75 | Type::JSON
76 | Type::JSONB
77 | Type::INTERVAL
78 | Type::BYTEA
79 | Type::VARCHAR
80 | Type::TEXT
81 | Type::BPCHAR
82 ) || (ty.name() == "citext"
83 || ty.name() == "ltree"
84 || ty.name() == "lquery"
85 || ty.name() == "ltxtquery"
86 || ty.name() == "geometry")
87 }
88}