MCPcopy Create free account
hub / github.com/MaterializeInc/materialize / decode_binary

Method decode_binary

src/pgrepr/src/value.rs:865–937  ·  view source on GitHub ↗

Deserializes a value of type `ty` from `raw` using the [binary encoding format](Format::Binary).

(ty: &Type, raw: &[u8])

Source from the content-addressed store, hash-verified

863 /// Deserializes a value of type `ty` from `raw` using the [binary encoding
864 /// format](Format::Binary).
865 pub fn decode_binary(ty: &Type, raw: &[u8]) -> Result<Value, Box<dyn Error + Sync + Send>> {
866 match ty {
867 Type::Array(_) => Err("input of array types is not implemented".into()),
868 Type::Int2Vector => Err("input of int2vector types is not implemented".into()),
869 Type::Bool => bool::from_sql(ty.inner(), raw).map(Value::Bool),
870 Type::Bytea => Vec::<u8>::from_sql(ty.inner(), raw).map(Value::Bytea),
871 Type::Char => {
872 i8::from_sql(ty.inner(), raw).map(|c| Value::Char(u8::reinterpret_cast(c)))
873 }
874 Type::Date => {
875 let days = i32::from_sql(ty.inner(), raw)?;
876 Ok(Value::Date(Date::from_pg_epoch(days)?))
877 }
878 Type::Float4 => f32::from_sql(ty.inner(), raw).map(Value::Float4),
879 Type::Float8 => f64::from_sql(ty.inner(), raw).map(Value::Float8),
880 Type::Int2 => i16::from_sql(ty.inner(), raw).map(Value::Int2),
881 Type::Int4 => i32::from_sql(ty.inner(), raw).map(Value::Int4),
882 Type::Int8 => i64::from_sql(ty.inner(), raw).map(Value::Int8),
883 Type::UInt2 => UInt2::from_sql(ty.inner(), raw).map(Value::UInt2),
884 Type::UInt4 => UInt4::from_sql(ty.inner(), raw).map(Value::UInt4),
885 Type::UInt8 => UInt8::from_sql(ty.inner(), raw).map(Value::UInt8),
886 Type::Interval { .. } => Interval::from_sql(ty.inner(), raw).map(Value::Interval),
887 Type::Json => Err("input of json types is not implemented".into()),
888 Type::Jsonb => Jsonb::from_sql(ty.inner(), raw).map(Value::Jsonb),
889 Type::List(_) => Err("binary decoding of list types is not implemented".into()),
890 Type::Map { .. } => Err("binary decoding of map types is not implemented".into()),
891 Type::Name => {
892 let s = String::from_sql(ty.inner(), raw)?;
893 if s.len() > NAME_MAX_BYTES {
894 return Err("identifier too long".into());
895 }
896 Ok(Value::Name(s))
897 }
898 Type::Numeric { constraints } => {
899 let n = Numeric::from_sql(ty.inner(), raw)?;
900 Ok(Value::Numeric(Numeric(rescale_numeric(
901 n.0,
902 constraints.as_ref(),
903 )?)))
904 }
905 Type::Oid | Type::RegClass | Type::RegProc | Type::RegType => {
906 u32::from_sql(ty.inner(), raw).map(Value::Oid)
907 }
908 Type::Record(_) => Err("input of anonymous composite types is not implemented".into()),
909 Type::Text => String::from_sql(ty.inner(), raw).map(Value::Text),
910 Type::BpChar { .. } => String::from_sql(ty.inner(), raw).map(Value::BpChar),
911 Type::VarChar { .. } => String::from_sql(ty.inner(), raw).map(Value::VarChar),
912 Type::Time { .. } => NaiveTime::from_sql(ty.inner(), raw).map(Value::Time),
913 Type::TimeTz { .. } => Err("input of timetz types is not implemented".into()),
914 Type::Timestamp { .. } => {
915 let ts = NaiveDateTime::from_sql(ty.inner(), raw)?;
916 Ok(Value::Timestamp(CheckedTimestamp::from_timestamplike(ts)?))
917 }
918 Type::TimestampTz { .. } => {
919 let ts = DateTime::<Utc>::from_sql(ty.inner(), raw)?;
920 Ok(Value::TimestampTz(CheckedTimestamp::from_timestamplike(
921 ts,
922 )?))

Callers

nothing calls this directly

Calls 14

rescale_numericFunction · 0.85
TimestampTzClass · 0.85
CharClass · 0.50
DateClass · 0.50
NameClass · 0.50
NumericClass · 0.50
TimestampClass · 0.50
MzTimestampInterface · 0.50
MzAclItemClass · 0.50
mapMethod · 0.45
innerMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected