pub fn cast_literal(
literal: Literal<'_>,
target: BuiltinType,
) -> Result<CastValue, CastError>Expand description
The value PostgreSQL 18’s explicit cast literal::target produces, or the error it raises,
with a database encoding of UTF-8.
A string constant is read by the target’s input function, an interval type’s with the fields
and precision of the target, which coerce_type gives interval_in alone. So
'1.75'::interval minute reads 1.75 as minutes, where a text value reads as seconds.
§Errors
The CastError PostgreSQL raises, or CastError::Unsupported for a literal or cast this
crate does not model.
use sql_values::postgres::{
CastError, BuiltinType, CharLength, IntervalFields, IntervalTypmod, Literal, NumericTypmod,
cast_literal,
};
let cast = |literal, target| cast_literal(literal, target).map(|value| value.to_string());
let length = |n| CharLength::new(n);
assert_eq!(cast(Literal::Number("2.5"), BuiltinType::Integer).unwrap(), "3");
assert_eq!(cast(Literal::Number("-2.5"), BuiltinType::Integer).unwrap(), "-3");
assert_eq!(cast(Literal::Number("1e2"), BuiltinType::Integer).unwrap(), "100");
assert_eq!(cast(Literal::Number("2.5"), BuiltinType::Smallint).unwrap(), "3");
assert_eq!(
cast(Literal::Number("32768"), BuiltinType::Smallint),
Err(CastError::OutOfRange(BuiltinType::Smallint)),
);
assert_eq!(
cast(Literal::Quoted("2.5"), BuiltinType::Integer),
Err(CastError::InvalidInputSyntax(BuiltinType::Integer)),
);
assert_eq!(cast(Literal::Quoted("abc"), BuiltinType::Bpchar(length(2))).unwrap(), "ab");
assert_eq!(cast(Literal::Quoted("ab"), BuiltinType::Bpchar(length(4))).unwrap(), "ab ");
assert_eq!(cast(Literal::Quoted("ab"), BuiltinType::Varchar(length(1))).unwrap(), "a");
assert_eq!(cast(Literal::Quoted("abc "), BuiltinType::Varchar(length(3))).unwrap(), "abc");
let numeric_4_1 = BuiltinType::Numeric(NumericTypmod::new(4, 1));
assert_eq!(cast(Literal::Quoted("12.345"), numeric_4_1).unwrap(), "12.3");
assert_eq!(cast(Literal::Number("12.345"), numeric_4_1).unwrap(), "12.3");
assert_eq!(cast(Literal::Quoted("yes"), BuiltinType::Boolean).unwrap(), "t");
assert_eq!(cast(Literal::Quoted("2026-01-01 23:00"), BuiltinType::Date).unwrap(), "2026-01-01");
assert_eq!(
cast(Literal::Quoted("2026-01-01T23:00:00+05"), BuiltinType::Date).unwrap(),
"2026-01-01",
);
assert_eq!(
cast(Literal::Quoted("2026-01-01 23:00:00.6"), BuiltinType::Timestamp(Some(sql_values::postgres::TimePrecision::new(0)))).unwrap(),
"2026-01-01 23:00:01",
);
assert_eq!(cast(Literal::Quoted("today"), BuiltinType::Date), Err(CastError::Unsupported));
let instant = BuiltinType::TimestampTz(None);
assert_eq!(
cast(Literal::Quoted("2030-01-01 00:00:00+02"), instant).unwrap(),
"2029-12-31 22:00:00+00",
);
assert_eq!(
cast(Literal::Quoted("2030-01-01 00:00:00"), instant),
Err(CastError::Unsupported),
);
assert_eq!(
cast(Literal::Quoted(r#"{"b": 1, "a": 2.50}"#), BuiltinType::Jsonb).unwrap(),
r#"{"a": 2.50, "b": 1}"#,
);
assert_eq!(cast(Literal::Number("1"), BuiltinType::Jsonb), Err(CastError::CannotCast {
from: BuiltinType::Integer,
to: BuiltinType::Jsonb,
}));
assert_eq!(
cast(Literal::Quoted("{A0EEBC999C0B4EF8BB6D6BB9BD380A11}"), BuiltinType::Uuid).unwrap(),
"a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11",
);
let minute = IntervalTypmod::new(Some(IntervalFields::Minute), None).unwrap();
assert_eq!(cast(Literal::Quoted("1.75"), BuiltinType::Interval(minute)).unwrap(), "00:01:00");
assert_eq!(
cast(Literal::Quoted("178956971 years"), BuiltinType::Interval(minute)),
Err(CastError::IntervalOutOfRange),
);