diff --git a/src/servers/src/postgres/types.rs b/src/servers/src/postgres/types.rs index 7e85410ed55..689fe9047e4 100644 --- a/src/servers/src/postgres/types.rs +++ b/src/servers/src/postgres/types.rs @@ -559,10 +559,7 @@ fn timestamp_parameter( Some(ConcreteDataType::Timestamp(unit)) => { timestamp_scalar(ts, unit.unit().as_arrow_time_unit()) } - Some(server_type) => Err(invalid_parameter_error( - "invalid_parameter_type", - Some(format!("Expected: {}, found: {}", server_type, client_type)), - )), + Some(server_type) => Err(parameter_type_mismatch(server_type, client_type)), None => timestamp_scalar(ts, TimeUnit::Millisecond), } } @@ -575,12 +572,7 @@ fn timestamp_array_parameter( let unit = match server_type { Some(ConcreteDataType::List(list_type)) => match list_type.item_type() { ConcreteDataType::Timestamp(unit) => unit.unit().as_arrow_time_unit(), - item_type => { - return Err(invalid_parameter_error( - "invalid_parameter_type", - Some(format!("Expected: {}, found: {}", item_type, client_type)), - )); - } + item_type => return Err(parameter_type_mismatch(item_type, client_type)), }, _ => TimeUnit::Millisecond, }; @@ -637,6 +629,113 @@ fn string_parameter_to_scalar_value( } } +fn parameter_type_mismatch(server_type: impl std::fmt::Display, client_type: &Type) -> PgWireError { + invalid_parameter_error( + "invalid_parameter_type", + Some(format!("Expected: {}, found: {}", server_type, client_type)), + ) +} + +/// Converts a numeric value, rejecting one the target type cannot hold. +fn checked_cast(data: Option, convert: impl Fn(&T) -> Option) -> PgWireResult> +where + T: std::fmt::Display, +{ + data.map(|n| convert(&n).ok_or_else(|| numeric_out_of_range_error(n))) + .transpose() +} + +/// Converts a numeric parameter to an integer server type, or returns `None` for other types. +fn to_integer_scalar( + data: Option, + server_type: &ConcreteDataType, +) -> PgWireResult> +where + T: ToPrimitive + std::fmt::Display, +{ + Ok(Some(match server_type { + ConcreteDataType::Int8(_) => ScalarValue::Int8(checked_cast(data, T::to_i8)?), + ConcreteDataType::Int16(_) => ScalarValue::Int16(checked_cast(data, T::to_i16)?), + ConcreteDataType::Int32(_) => ScalarValue::Int32(checked_cast(data, T::to_i32)?), + ConcreteDataType::Int64(_) => ScalarValue::Int64(checked_cast(data, T::to_i64)?), + ConcreteDataType::UInt8(_) => ScalarValue::UInt8(checked_cast(data, T::to_u8)?), + ConcreteDataType::UInt16(_) => ScalarValue::UInt16(checked_cast(data, T::to_u16)?), + ConcreteDataType::UInt32(_) => ScalarValue::UInt32(checked_cast(data, T::to_u32)?), + ConcreteDataType::UInt64(_) => ScalarValue::UInt64(checked_cast(data, T::to_u64)?), + _ => return Ok(None), + })) +} + +/// Converts an integer parameter to an integer or timestamp server type. Without a server type, +/// the value keeps the client's width. +fn int_parameter( + data: Option, + server_type: Option<&ConcreteDataType>, + client_type: &Type, + unconverted: fn(Option) -> ScalarValue, +) -> PgWireResult +where + T: ToPrimitive + std::fmt::Display + Into, +{ + match server_type { + None => Ok(unconverted(data)), + Some(server_type @ ConcreteDataType::Timestamp(unit)) => { + to_timestamp_scalar_value(data, unit, server_type) + } + Some(server_type) => to_integer_scalar(data, server_type)? + .ok_or_else(|| parameter_type_mismatch(server_type, client_type)), + } +} + +/// Converts a float parameter to an integer or float server type. Without a server type, the +/// value keeps the client's width. +fn float_parameter( + data: Option, + server_type: Option<&ConcreteDataType>, + client_type: &Type, + unconverted: fn(Option) -> ScalarValue, +) -> PgWireResult +where + T: ToPrimitive + std::fmt::Display, +{ + match server_type { + None => Ok(unconverted(data)), + Some(ConcreteDataType::Float32(_)) => { + Ok(ScalarValue::Float32(checked_cast(data, T::to_f32)?)) + } + Some(ConcreteDataType::Float64(_)) => { + Ok(ScalarValue::Float64(checked_cast(data, T::to_f64)?)) + } + Some(server_type) => to_integer_scalar(data, server_type)? + .ok_or_else(|| parameter_type_mismatch(server_type, client_type)), + } +} + +fn list_parameter>( + data: Option>, + item_type: &ArrowDataType, +) -> ScalarValue { + match data { + Some(data) => { + let values = data.into_iter().map(Into::into).collect::>(); + ScalarValue::List(ScalarValue::new_list(&values, item_type, true)) + } + None => ScalarValue::Null, + } +} + +fn uint64_list_parameter(data: Vec>) -> PgWireResult { + let values = data + .into_iter() + .map(|n| Ok(ScalarValue::UInt64(checked_cast(n, Decimal::to_u64)?))) + .collect::>>()?; + Ok(ScalarValue::List(ScalarValue::new_list( + &values, + &ArrowDataType::UInt64, + true, + ))) +} + pub(super) fn parameters_to_scalar_values( plan: &LogicalPlan, portal: &Portal, @@ -670,291 +769,69 @@ pub(super) fn parameters_to_scalar_values( )), )); }; + let mismatch = |server_type: &dyn std::fmt::Display| { + parameter_type_mismatch(server_type, &client_type) + }; let value = match &client_type { &Type::VARCHAR | &Type::TEXT | &Type::CHAR => { let data = portal.parameter::(idx, &client_type)?; - if let Some(server_type) = &server_type { - string_parameter_to_scalar_value(data, server_type).ok_or_else(|| { - invalid_parameter_error( - "invalid_parameter_type", - Some(format!("Expected: {}, found: {}", server_type, client_type)), - ) - })? - } else { - ScalarValue::Utf8(data) + match server_type { + None => ScalarValue::Utf8(data), + Some(server_type) => string_parameter_to_scalar_value(data, server_type) + .ok_or_else(|| mismatch(server_type))?, } } &Type::BOOL => { let data = portal.parameter::(idx, &client_type)?; - if let Some(server_type) = &server_type { - match server_type { - ConcreteDataType::Boolean(_) => ScalarValue::Boolean(data), - _ => { - return Err(invalid_parameter_error( - "invalid_parameter_type", - Some(format!("Expected: {}, found: {}", server_type, client_type)), - )); - } - } - } else { - ScalarValue::Boolean(data) - } - } - &Type::INT2 => { - let data = portal.parameter::(idx, &client_type)?; - if let Some(server_type) = &server_type { - match server_type { - ConcreteDataType::Int8(_) => ScalarValue::Int8( - data.map(|n| n.to_i8().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::Int16(_) => ScalarValue::Int16(data), - ConcreteDataType::Int32(_) => ScalarValue::Int32(data.map(|n| n as i32)), - ConcreteDataType::Int64(_) => ScalarValue::Int64(data.map(|n| n as i64)), - ConcreteDataType::UInt8(_) => ScalarValue::UInt8( - data.map(|n| n.to_u8().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::UInt16(_) => ScalarValue::UInt16( - data.map(|n| n.to_u16().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::UInt32(_) => ScalarValue::UInt32( - data.map(|n| n.to_u32().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::UInt64(_) => ScalarValue::UInt64( - data.map(|n| n.to_u64().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::Timestamp(unit) => { - to_timestamp_scalar_value(data, unit, server_type)? - } - _ => { - return Err(invalid_parameter_error( - "invalid_parameter_type", - Some(format!("Expected: {}, found: {}", server_type, client_type)), - )); - } - } - } else { - ScalarValue::Int16(data) - } - } - &Type::INT4 => { - let data = portal.parameter::(idx, &client_type)?; - if let Some(server_type) = &server_type { - match server_type { - ConcreteDataType::Int8(_) => ScalarValue::Int8( - data.map(|n| n.to_i8().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::Int16(_) => ScalarValue::Int16( - data.map(|n| n.to_i16().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::Int32(_) => ScalarValue::Int32(data), - ConcreteDataType::Int64(_) => ScalarValue::Int64(data.map(|n| n as i64)), - ConcreteDataType::UInt8(_) => ScalarValue::UInt8( - data.map(|n| n.to_u8().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::UInt16(_) => ScalarValue::UInt16( - data.map(|n| n.to_u16().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::UInt32(_) => ScalarValue::UInt32( - data.map(|n| n.to_u32().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::UInt64(_) => ScalarValue::UInt64( - data.map(|n| n.to_u64().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::Timestamp(unit) => { - to_timestamp_scalar_value(data, unit, server_type)? - } - _ => { - return Err(invalid_parameter_error( - "invalid_parameter_type", - Some(format!("Expected: {}, found: {}", server_type, client_type)), - )); - } - } - } else { - ScalarValue::Int32(data) - } - } - &Type::INT8 => { - let data = portal.parameter::(idx, &client_type)?; - if let Some(server_type) = &server_type { - match server_type { - ConcreteDataType::Int8(_) => ScalarValue::Int8( - data.map(|n| n.to_i8().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::Int16(_) => ScalarValue::Int16( - data.map(|n| n.to_i16().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::Int32(_) => ScalarValue::Int32( - data.map(|n| n.to_i32().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::Int64(_) => ScalarValue::Int64(data), - ConcreteDataType::UInt8(_) => ScalarValue::UInt8( - data.map(|n| n.to_u8().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::UInt16(_) => ScalarValue::UInt16( - data.map(|n| n.to_u16().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::UInt32(_) => ScalarValue::UInt32( - data.map(|n| n.to_u32().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::UInt64(_) => ScalarValue::UInt64( - data.map(|n| n.to_u64().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::Timestamp(unit) => { - to_timestamp_scalar_value(data, unit, server_type)? - } - _ => { - return Err(invalid_parameter_error( - "invalid_parameter_type", - Some(format!("Expected: {}, found: {}", server_type, client_type)), - )); - } - } - } else { - ScalarValue::Int64(data) + match server_type { + None | Some(ConcreteDataType::Boolean(_)) => ScalarValue::Boolean(data), + Some(server_type) => return Err(mismatch(server_type)), } } + &Type::INT2 => int_parameter( + portal.parameter::(idx, &client_type)?, + server_type, + &client_type, + ScalarValue::Int16, + )?, + &Type::INT4 => int_parameter( + portal.parameter::(idx, &client_type)?, + server_type, + &client_type, + ScalarValue::Int32, + )?, + &Type::INT8 => int_parameter( + portal.parameter::(idx, &client_type)?, + server_type, + &client_type, + ScalarValue::Int64, + )?, &Type::NUMERIC => { let data = portal.parameter::(idx, &client_type)?; - match &server_type { + match server_type { Some(ConcreteDataType::Decimal128(dt)) => to_decimal_scalar_value(data, dt), Some(st @ ConcreteDataType::Timestamp(unit)) => { to_timestamp_scalar_value(data.and_then(|n| n.to_i64()), unit, st)? } - Some(ConcreteDataType::UInt64(_)) | None => ScalarValue::UInt64( - data.map(|n| n.to_u64().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - Some(st) => { - return Err(invalid_parameter_error( - "invalid_parameter_type", - Some(format!("Expected: {}, found: {}", st, client_type)), - )); + Some(ConcreteDataType::UInt64(_)) | None => { + ScalarValue::UInt64(checked_cast(data, Decimal::to_u64)?) } + Some(server_type) => return Err(mismatch(server_type)), } } - &Type::FLOAT4 => { - let data = portal.parameter::(idx, &client_type)?; - if let Some(server_type) = &server_type { - match server_type { - ConcreteDataType::Int8(_) => ScalarValue::Int8( - data.map(|n| n.to_i8().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::Int16(_) => ScalarValue::Int16( - data.map(|n| n.to_i16().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::Int32(_) => ScalarValue::Int32( - data.map(|n| n.to_i32().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::Int64(_) => ScalarValue::Int64( - data.map(|n| n.to_i64().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::UInt8(_) => ScalarValue::UInt8( - data.map(|n| n.to_u8().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::UInt16(_) => ScalarValue::UInt16( - data.map(|n| n.to_u16().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::UInt32(_) => ScalarValue::UInt32( - data.map(|n| n.to_u32().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::UInt64(_) => ScalarValue::UInt64( - data.map(|n| n.to_u64().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::Float32(_) => ScalarValue::Float32(data), - ConcreteDataType::Float64(_) => { - ScalarValue::Float64(data.map(|n| n as f64)) - } - _ => { - return Err(invalid_parameter_error( - "invalid_parameter_type", - Some(format!("Expected: {}, found: {}", server_type, client_type)), - )); - } - } - } else { - ScalarValue::Float32(data) - } - } - &Type::FLOAT8 => { - let data = portal.parameter::(idx, &client_type)?; - if let Some(server_type) = &server_type { - match server_type { - ConcreteDataType::Int8(_) => ScalarValue::Int8( - data.map(|n| n.to_i8().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::Int16(_) => ScalarValue::Int16( - data.map(|n| n.to_i16().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::Int32(_) => ScalarValue::Int32( - data.map(|n| n.to_i32().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::Int64(_) => ScalarValue::Int64( - data.map(|n| n.to_i64().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::UInt8(_) => ScalarValue::UInt8( - data.map(|n| n.to_u8().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::UInt16(_) => ScalarValue::UInt16( - data.map(|n| n.to_u16().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::UInt32(_) => ScalarValue::UInt32( - data.map(|n| n.to_u32().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::UInt64(_) => ScalarValue::UInt64( - data.map(|n| n.to_u64().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::Float32(_) => ScalarValue::Float32( - data.map(|n| n.to_f32().ok_or_else(|| numeric_out_of_range_error(n))) - .transpose()?, - ), - ConcreteDataType::Float64(_) => ScalarValue::Float64(data), - _ => { - return Err(invalid_parameter_error( - "invalid_parameter_type", - Some(format!("Expected: {}, found: {}", server_type, client_type)), - )); - } - } - } else { - ScalarValue::Float64(data) - } - } + &Type::FLOAT4 => float_parameter( + portal.parameter::(idx, &client_type)?, + server_type, + &client_type, + ScalarValue::Float32, + )?, + &Type::FLOAT8 => float_parameter( + portal.parameter::(idx, &client_type)?, + server_type, + &client_type, + ScalarValue::Float64, + )?, &Type::TIMESTAMP => { let data = portal.parameter::(idx, &client_type)?; timestamp_parameter(data.map(|ts| ts.and_utc()), server_type, &client_type)? @@ -965,237 +842,114 @@ pub(super) fn parameters_to_scalar_values( } &Type::DATE => { let data = portal.parameter::(idx, &client_type)?; - if let Some(server_type) = &server_type { - match server_type { - ConcreteDataType::Date(_) => ScalarValue::Date32( - data.map(|d| (d - DateTime::UNIX_EPOCH.date_naive()).num_days() as i32), - ), - _ => { - return Err(invalid_parameter_error( - "invalid_parameter_type", - Some(format!("Expected: {}, found: {}", server_type, client_type)), - )); - } - } - } else { - ScalarValue::Date32( + match server_type { + None | Some(ConcreteDataType::Date(_)) => ScalarValue::Date32( data.map(|d| (d - DateTime::UNIX_EPOCH.date_naive()).num_days() as i32), - ) + ), + Some(server_type) => return Err(mismatch(server_type)), } } &Type::INTERVAL => { let data = portal.parameter::(idx, &client_type)?; - if let Some(server_type) = &server_type { - match server_type { - ConcreteDataType::Interval(IntervalType::YearMonth(_)) => { - ScalarValue::IntervalYearMonth( - data.map(|i| { - if i.days != 0 || i.microseconds != 0 { - Err(invalid_parameter_error( - "invalid_parameter_type", - Some(format!( - "Expected: {}, found: {}", - server_type, client_type - )), - )) - } else { - Ok(IntervalYearMonth::new(i.months).to_i32()) - } - }) - .transpose()?, - ) - } - ConcreteDataType::Interval(IntervalType::DayTime(_)) => { - ScalarValue::IntervalDayTime( - data.map(|i| { - if i.months != 0 || i.microseconds % 1000 != 0 { - Err(invalid_parameter_error( - "invalid_parameter_type", - Some(format!( - "Expected: {}, found: {}", - server_type, client_type - )), - )) - } else { - Ok(IntervalDayTime::new( - i.days, - (i.microseconds / 1000) as i32, - ) - .into()) - } - }) - .transpose()?, - ) - } - ConcreteDataType::Interval(IntervalType::MonthDayNano(_)) => { - ScalarValue::IntervalMonthDayNano(data.map(|i| { - IntervalMonthDayNano::new( - i.months, - i.days, - i.microseconds * 1_000i64, - ) + match server_type { + None | Some(ConcreteDataType::Interval(IntervalType::MonthDayNano(_))) => { + ScalarValue::IntervalMonthDayNano(data.map(|i| { + IntervalMonthDayNano::new(i.months, i.days, i.microseconds * 1_000i64) .into() - })) - } - _ => { - return Err(invalid_parameter_error( - "invalid_parameter_type", - Some(format!("Expected: {}, found: {}", server_type, client_type)), - )); - } + })) } - } else { - ScalarValue::IntervalMonthDayNano(data.map(|i| { - IntervalMonthDayNano::new(i.months, i.days, i.microseconds * 1_000i64) - .into() - })) + Some(st @ ConcreteDataType::Interval(IntervalType::YearMonth(_))) => { + ScalarValue::IntervalYearMonth( + data.map(|i| { + if i.days != 0 || i.microseconds != 0 { + Err(mismatch(st)) + } else { + Ok(IntervalYearMonth::new(i.months).to_i32()) + } + }) + .transpose()?, + ) + } + Some(st @ ConcreteDataType::Interval(IntervalType::DayTime(_))) => { + ScalarValue::IntervalDayTime( + data.map(|i| { + if i.months != 0 || i.microseconds % 1000 != 0 { + Err(mismatch(st)) + } else { + Ok(IntervalDayTime::new(i.days, (i.microseconds / 1000) as i32) + .into()) + } + }) + .transpose()?, + ) + } + Some(server_type) => return Err(mismatch(server_type)), } } &Type::BYTEA => { let data = portal.parameter::>(idx, &client_type)?; - if let Some(server_type) = &server_type { - match server_type { - ConcreteDataType::String(t) => { - let s = data.map(|d| String::from_utf8_lossy(&d).to_string()); - if t.is_large() { - ScalarValue::LargeUtf8(s) - } else { - ScalarValue::Utf8(s) - } - } - ConcreteDataType::Binary(_) => ScalarValue::Binary(data), - _ => { - return Err(invalid_parameter_error( - "invalid_parameter_type", - Some(format!("Expected: {}, found: {}", server_type, client_type)), - )); + match server_type { + None | Some(ConcreteDataType::Binary(_)) => ScalarValue::Binary(data), + Some(ConcreteDataType::String(t)) => { + let s = data.map(|d| String::from_utf8_lossy(&d).to_string()); + if t.is_large() { + ScalarValue::LargeUtf8(s) + } else { + ScalarValue::Utf8(s) } } - } else { - ScalarValue::Binary(data) + Some(server_type) => return Err(mismatch(server_type)), } } &Type::JSONB => { let data = portal.parameter::(idx, &client_type)?; - if let Some(server_type) = &server_type { - match server_type { - ConcreteDataType::Binary(_) => { - ScalarValue::Binary(data.map(|d| d.to_string().into_bytes())) - } - _ => { - return Err(invalid_parameter_error( - "invalid_parameter_type", - Some(format!("Expected: {}, found: {}", server_type, client_type)), - )); - } + match server_type { + None | Some(ConcreteDataType::Binary(_)) => { + ScalarValue::Binary(data.map(|d| d.to_string().into_bytes())) } - } else { - ScalarValue::Binary(data.map(|d| d.to_string().into_bytes())) - } - } - &Type::INT2_ARRAY => { - let data = portal.parameter::>>(idx, &client_type)?; - if let Some(data) = data { - let values = data.into_iter().map(|i| i.into()).collect::>(); - ScalarValue::List(ScalarValue::new_list(&values, &ArrowDataType::Int16, true)) - } else { - ScalarValue::Null - } - } - &Type::INT4_ARRAY => { - let data = portal.parameter::>>(idx, &client_type)?; - if let Some(data) = data { - let values = data.into_iter().map(|i| i.into()).collect::>(); - ScalarValue::List(ScalarValue::new_list(&values, &ArrowDataType::Int32, true)) - } else { - ScalarValue::Null - } - } - &Type::INT8_ARRAY => { - let data = portal.parameter::>>(idx, &client_type)?; - if let Some(data) = data { - let values = data.into_iter().map(|i| i.into()).collect::>(); - ScalarValue::List(ScalarValue::new_list(&values, &ArrowDataType::Int64, true)) - } else { - ScalarValue::Null + Some(server_type) => return Err(mismatch(server_type)), } } + &Type::INT2_ARRAY => list_parameter( + portal.parameter::>>(idx, &client_type)?, + &ArrowDataType::Int16, + ), + &Type::INT4_ARRAY => list_parameter( + portal.parameter::>>(idx, &client_type)?, + &ArrowDataType::Int32, + ), + &Type::INT8_ARRAY => list_parameter( + portal.parameter::>>(idx, &client_type)?, + &ArrowDataType::Int64, + ), &Type::NUMERIC_ARRAY => { - let data = portal.parameter::>>(idx, &client_type)?; - if let Some(data) = data { - let build_u64_list = |data: Vec>| -> PgWireResult { - let values = data - .into_iter() - .map(|n| { - Ok(ScalarValue::UInt64( - n.map(|n| { - n.to_u64().ok_or_else(|| numeric_out_of_range_error(n)) - }) - .transpose()?, + match portal.parameter::>>(idx, &client_type)? { + None => ScalarValue::Null, + Some(data) => match server_type { + None => uint64_list_parameter(data)?, + Some(ConcreteDataType::List(list_type)) => match list_type.item_type() { + ConcreteDataType::UInt64(_) => uint64_list_parameter(data)?, + ConcreteDataType::Decimal128(dt) => { + let values = data + .into_iter() + .map(|n| to_decimal_scalar_value(n, dt)) + .collect::>(); + ScalarValue::List(ScalarValue::new_list( + &values, + &ArrowDataType::Decimal128(dt.precision(), dt.scale()), + true, )) - }) - .collect::>>()?; - Ok(ScalarValue::List(ScalarValue::new_list( - &values, - &ArrowDataType::UInt64, - true, - ))) - }; - if let Some(server_type) = &server_type { - match server_type { - ConcreteDataType::List(list_type) => match list_type.item_type() { - ConcreteDataType::UInt64(_) => build_u64_list(data)?, - ConcreteDataType::Decimal128(dt) => { - let values = data - .into_iter() - .map(|n| to_decimal_scalar_value(n, dt)) - .collect::>(); - ScalarValue::List(ScalarValue::new_list( - &values, - &ArrowDataType::Decimal128(dt.precision(), dt.scale()), - true, - )) - } - _ => { - // the server type is not a list of decimal or uint64 - return Err(invalid_parameter_error( - "invalid_parameter_type", - Some(format!( - "Expected: {}, found: {}", - list_type.item_type(), - client_type - )), - )); - } - }, - _ => { - // the server type is not a list - return Err(invalid_parameter_error( - "invalid_parameter_type", - Some(format!( - "Expected: {}, found: {}", - server_type, client_type - )), - )); } - } - } else { - // server type not provided - build_u64_list(data)? - } - } else { - ScalarValue::Null - } - } - &Type::VARCHAR_ARRAY | &Type::TEXT_ARRAY | &Type::CHAR_ARRAY => { - let data = portal.parameter::>>(idx, &client_type)?; - if let Some(data) = data { - let values = data.into_iter().map(|i| i.into()).collect::>(); - ScalarValue::List(ScalarValue::new_list(&values, &ArrowDataType::Utf8, true)) - } else { - ScalarValue::Null + item_type => return Err(mismatch(item_type)), + }, + Some(server_type) => return Err(mismatch(server_type)), + }, } } + &Type::VARCHAR_ARRAY | &Type::TEXT_ARRAY | &Type::CHAR_ARRAY => list_parameter( + portal.parameter::>>(idx, &client_type)?, + &ArrowDataType::Utf8, + ), &Type::TIMESTAMP_ARRAY => { let data = portal.parameter::>>(idx, &client_type)?; match data { @@ -2333,290 +2087,158 @@ mod test { ); } + /// Pins the conversion of each client parameter type, with and without an inferred server + /// type, including the server types it rejects and the values that do not fit them. #[test] - fn test_int2_coerce_in_range() { - let plan = build_plan_with_params(vec![ - ("$1", DataType::Int8), - ("$2", DataType::Int16), - ("$3", DataType::Int32), - ("$4", DataType::Int64), - ("$5", DataType::UInt8), - ("$6", DataType::UInt16), - ("$7", DataType::UInt32), - ("$8", DataType::UInt64), - ]); - let portal = make_portal( - vec![ - Some(Type::INT2), - Some(Type::INT2), - Some(Type::INT2), - Some(Type::INT2), - Some(Type::INT2), - Some(Type::INT2), - Some(Type::INT2), - Some(Type::INT2), - ], - vec![ - s("100"), - s("100"), - s("100"), - s("100"), - s("100"), - s("100"), - s("100"), - s("100"), - ], - ); + fn test_parameter_conversion_by_type() { + const MISMATCH: &str = "invalid_parameter_type"; + const OUT_OF_RANGE: &str = "numeric_value_out_of_range"; + let list = |item: D| D::List(Arc::new(Field::new("item", item, true))); + let ts = |unit| Some(D::Timestamp(unit, None)); + let ns_list = || Some(list(D::Timestamp(TimeUnit::Nanosecond, None))); + let interval = |unit| Some(D::Interval(unit)); + use {DataType as D, ScalarValue as V, Type as T}; + type Case = (T, Option, &'static str, Result); + #[rustfmt::skip] + let cases: Vec = vec![ + // Integers keep the client's width without a server type, convert to every integer + // type that holds the value, and convert to timestamps in the server's unit. + (T::INT2, None, "7", Ok(V::Int16(Some(7)))), + (T::INT4, None, "7", Ok(V::Int32(Some(7)))), + (T::INT8, None, "7", Ok(V::Int64(Some(7)))), + (T::INT2, Some(D::Int8), "100", Ok(V::Int8(Some(100)))), + (T::INT2, Some(D::Int16), "100", Ok(V::Int16(Some(100)))), + (T::INT2, Some(D::Int32), "100", Ok(V::Int32(Some(100)))), + (T::INT2, Some(D::Int64), "100", Ok(V::Int64(Some(100)))), + (T::INT2, Some(D::UInt8), "100", Ok(V::UInt8(Some(100)))), + (T::INT2, Some(D::UInt16), "100", Ok(V::UInt16(Some(100)))), + (T::INT2, Some(D::UInt32), "100", Ok(V::UInt32(Some(100)))), + (T::INT2, Some(D::UInt64), "100", Ok(V::UInt64(Some(100)))), + (T::INT4, Some(D::Int16), "1000", Ok(V::Int16(Some(1000)))), + (T::INT4, Some(D::UInt8), "200", Ok(V::UInt8(Some(200)))), + (T::INT4, Some(D::Int64), "100000", Ok(V::Int64(Some(100000)))), + (T::INT8, Some(D::Int32), "100000", Ok(V::Int32(Some(100000)))), + (T::INT8, Some(D::UInt32), "3000000000", Ok(V::UInt32(Some(3000000000)))), + (T::INT2, Some(D::Int8), "200", Err(OUT_OF_RANGE)), + (T::INT2, Some(D::UInt64), "-1", Err(OUT_OF_RANGE)), + (T::INT4, Some(D::Int16), "2147483647", Err(OUT_OF_RANGE)), + (T::INT8, Some(D::Int32), "2147483648", Err(OUT_OF_RANGE)), + (T::INT8, Some(D::UInt64), "-1", Err(OUT_OF_RANGE)), + (T::INT8, ts(TimeUnit::Millisecond), "1000", Ok(V::TimestampMillisecond(Some(1000), None))), + (T::INT2, ts(TimeUnit::Second), "7", Ok(V::TimestampSecond(Some(7), None))), + (T::INT4, Some(D::Float64), "7", Err(MISMATCH)), + (T::INT4, Some(D::Utf8), "7", Err(MISMATCH)), + // Floats convert to both float types and to integers that hold the truncated value. + (T::FLOAT4, None, "1.5", Ok(V::Float32(Some(1.5)))), + (T::FLOAT8, None, "1.5", Ok(V::Float64(Some(1.5)))), + (T::FLOAT4, Some(D::Float64), "2.5", Ok(V::Float64(Some(2.5)))), + (T::FLOAT8, Some(D::Float32), "1.5", Ok(V::Float32(Some(1.5)))), + (T::FLOAT4, Some(D::Int8), "100", Ok(V::Int8(Some(100)))), + (T::FLOAT8, Some(D::Int32), "2.7", Ok(V::Int32(Some(2)))), + (T::FLOAT8, Some(D::UInt64), "200", Ok(V::UInt64(Some(200)))), + (T::FLOAT4, Some(D::Int8), "200", Err(OUT_OF_RANGE)), + (T::FLOAT8, Some(D::UInt64), "-1", Err(OUT_OF_RANGE)), + (T::FLOAT8, ts(TimeUnit::Millisecond), "1", Err(MISMATCH)), + (T::FLOAT4, Some(D::Utf8), "1", Err(MISMATCH)), + // Numerics are unsigned integers unless the server asks for a decimal. + (T::NUMERIC, None, "42", Ok(V::UInt64(Some(42)))), + (T::NUMERIC, Some(D::UInt64), "18446744073709551615", Ok(V::UInt64(Some(u64::MAX)))), + (T::NUMERIC, Some(D::UInt64), "18446744073709551616", Err(OUT_OF_RANGE)), + (T::NUMERIC, Some(D::UInt64), "-1", Err(OUT_OF_RANGE)), + (T::NUMERIC_ARRAY, Some(list(D::UInt64)), "{42,NULL,-1}", Err(OUT_OF_RANGE)), + (T::NUMERIC_ARRAY, Some(list(D::UInt64)), "{18446744073709551616}", Err(OUT_OF_RANGE)), + (T::NUMERIC_ARRAY, None, "{-1}", Err(OUT_OF_RANGE)), + // Timestamps convert to the server's unit and reject what nanoseconds cannot hold. + (T::TIMESTAMPTZ, ts(TimeUnit::Nanosecond), "2024-01-01 08:00:00+08", Ok(V::TimestampNanosecond(Some(1_704_067_200 * 1_000_000_000), None))), + (T::TIMESTAMP, ts(TimeUnit::Nanosecond), "3000-01-01 00:00:00", Err(OUT_OF_RANGE)), + (T::TIMESTAMPTZ, ts(TimeUnit::Nanosecond), "3000-01-01 00:00:00+00", Err(OUT_OF_RANGE)), + (T::TIMESTAMP_ARRAY, ns_list(), r#"{"3000-01-01 00:00:00"}"#, Err(OUT_OF_RANGE)), + (T::TIMESTAMPTZ_ARRAY, ns_list(), r#"{"3000-01-01 00:00:00+00"}"#, Err(OUT_OF_RANGE)), + // The remaining scalar types accept their own server type, or none. + (T::BOOL, None, "t", Ok(V::Boolean(Some(true)))), + (T::BOOL, Some(D::Boolean), "f", Ok(V::Boolean(Some(false)))), + (T::BOOL, Some(D::Int32), "t", Err(MISMATCH)), + (T::TEXT, None, "a", Ok(V::Utf8(s("a")))), + (T::VARCHAR, Some(D::LargeUtf8), "a", Ok(V::LargeUtf8(s("a")))), + (T::TEXT, Some(D::Int32), "1", Err(MISMATCH)), + (T::DATE, None, "1970-01-11", Ok(V::Date32(Some(10)))), + (T::DATE, Some(D::Date32), "1970-01-11", Ok(V::Date32(Some(10)))), + (T::DATE, Some(D::Int32), "1970-01-11", Err(MISMATCH)), + (T::BYTEA, None, "\\x6869", Ok(V::Binary(Some(b"hi".to_vec())))), + (T::BYTEA, Some(D::Utf8), "\\x6869", Ok(V::Utf8(s("hi")))), + (T::BYTEA, Some(D::LargeUtf8), "\\x6869", Ok(V::LargeUtf8(s("hi")))), + (T::BYTEA, Some(D::Int32), "\\x6869", Err(MISMATCH)), + (T::JSONB, None, r#"{"a":1}"#, Ok(V::Binary(Some(br#"{"a":1}"#.to_vec())))), + (T::JSONB, Some(D::Binary), r#"{"a":1}"#, Ok(V::Binary(Some(br#"{"a":1}"#.to_vec())))), + (T::JSONB, Some(D::Utf8), r#"{"a":1}"#, Err(MISMATCH)), + (T::INTERVAL, None, "1 mon 2 days 00:00:00.003", Ok(V::IntervalMonthDayNano(Some(IntervalMonthDayNano::new(1, 2, 3_000_000).into())))), + (T::INTERVAL, interval(IntervalUnit::YearMonth), "1 year 2 mons", Ok(V::IntervalYearMonth(Some(14)))), + (T::INTERVAL, interval(IntervalUnit::YearMonth), "1 mon 1 day", Err(MISMATCH)), + (T::INTERVAL, interval(IntervalUnit::DayTime), "2 days 00:00:00.003", Ok(V::IntervalDayTime(Some(IntervalDayTime::new(2, 3).into())))), + (T::INTERVAL, interval(IntervalUnit::DayTime), "00:00:00.000001", Err(MISMATCH)), + (T::INTERVAL, Some(D::Int32), "1 day", Err(MISMATCH)), + // Integer and string arrays. + (T::INT4_ARRAY, Some(list(D::Int32)), "{1,NULL}", Ok(V::List(V::new_list(&[V::Int32(Some(1)), V::Int32(None)], &ArrowDataType::Int32, true)))), + (T::INT8_ARRAY, None, "{1}", Ok(V::List(V::new_list(&[V::Int64(Some(1))], &ArrowDataType::Int64, true)))), + (T::TEXT_ARRAY, None, "{a,b}", Ok(V::List(V::new_list(&[V::Utf8(s("a")), V::Utf8(s("b"))], &ArrowDataType::Utf8, true)))), + (T::UUID, None, "00000000-0000-0000-0000-000000000000", Err("unsupported_parameter_value")), + ]; - let values = parameters_to_scalar_values(&plan, &portal).unwrap(); - assert_eq!(values[0], ScalarValue::Int8(Some(100))); - assert_eq!(values[1], ScalarValue::Int16(Some(100))); - assert_eq!(values[2], ScalarValue::Int32(Some(100))); - assert_eq!(values[3], ScalarValue::Int64(Some(100))); - assert_eq!(values[4], ScalarValue::UInt8(Some(100))); - assert_eq!(values[5], ScalarValue::UInt16(Some(100))); - assert_eq!(values[6], ScalarValue::UInt32(Some(100))); - assert_eq!(values[7], ScalarValue::UInt64(Some(100))); - } + for (client_type, server_type, value, expected) in cases { + let plan = match &server_type { + Some(server_type) => build_plan_with_params(vec![("$1", server_type.clone())]), + None => LogicalPlanBuilder::empty(true).build().unwrap(), + }; + let portal = make_portal(vec![Some(client_type.clone())], vec![s(value)]); + let result = parameters_to_scalar_values(&plan, &portal); + let case = format!("{client_type} -> {server_type:?}: {value}"); + match (result, expected) { + (Ok(values), Ok(expected)) => assert_eq!(vec![expected], values, "{case}"), + (Err(PgWireError::UserError(error)), Err(message)) => { + assert_eq!( + ("22023", message), + (error.code.as_str(), error.message.as_str()), + "{case}" + ) + } + (result, expected) => panic!("{case}: expected {expected:?}, got {result:?}"), + } + } - #[test] - fn test_int2_coerce_out_of_range() { - let plan = build_plan_with_params(vec![("$1", DataType::Int8)]); - let portal = make_portal(vec![Some(Type::INT2)], vec![s("200")]); - let result = parameters_to_scalar_values(&plan, &portal); - assert!(result.is_err()); - } - - #[test] - fn test_int2_coerce_negative_to_unsigned_out_of_range() { - let plan = build_plan_with_params(vec![("$1", DataType::UInt64)]); - let portal = make_portal(vec![Some(Type::INT2)], vec![s("-1")]); - let result = parameters_to_scalar_values(&plan, &portal); - assert!(result.is_err()); - } - - #[test] - fn test_int4_coerce_in_range() { - let plan = build_plan_with_params(vec![ - ("$1", DataType::Int8), - ("$2", DataType::Int16), - ("$3", DataType::Int32), - ("$4", DataType::Int64), - ("$5", DataType::UInt8), - ("$6", DataType::UInt16), - ("$7", DataType::UInt32), - ("$8", DataType::UInt64), - ]); - let portal = make_portal( - vec![ - Some(Type::INT4), - Some(Type::INT4), - Some(Type::INT4), - Some(Type::INT4), - Some(Type::INT4), - Some(Type::INT4), - Some(Type::INT4), - Some(Type::INT4), - ], - vec![ - s("100"), - s("1000"), - s("100000"), - s("100000"), - s("200"), - s("1000"), - s("100000"), - s("100000"), - ], - ); - - let values = parameters_to_scalar_values(&plan, &portal).unwrap(); - assert_eq!(values[0], ScalarValue::Int8(Some(100))); - assert_eq!(values[1], ScalarValue::Int16(Some(1000))); - assert_eq!(values[2], ScalarValue::Int32(Some(100000))); - assert_eq!(values[3], ScalarValue::Int64(Some(100000))); - assert_eq!(values[4], ScalarValue::UInt8(Some(200))); - assert_eq!(values[5], ScalarValue::UInt16(Some(1000))); - assert_eq!(values[6], ScalarValue::UInt32(Some(100000))); - assert_eq!(values[7], ScalarValue::UInt64(Some(100000))); - } - - #[test] - fn test_int4_coerce_out_of_range() { - let plan = build_plan_with_params(vec![("$1", DataType::Int8)]); - let portal = make_portal(vec![Some(Type::INT4)], vec![s("200")]); - let result = parameters_to_scalar_values(&plan, &portal); - assert!(result.is_err()); - } - - #[test] - fn test_int4_coerce_i32_max_to_i16_out_of_range() { - let plan = build_plan_with_params(vec![("$1", DataType::Int16)]); - let portal = make_portal(vec![Some(Type::INT4)], vec![Some(i32::MAX.to_string())]); - let result = parameters_to_scalar_values(&plan, &portal); - assert!(result.is_err()); - } - - #[test] - fn test_int8_coerce_in_range() { - let plan = build_plan_with_params(vec![ - ("$1", DataType::Int8), - ("$2", DataType::Int16), - ("$3", DataType::Int32), - ("$4", DataType::Int64), - ("$5", DataType::UInt8), - ("$6", DataType::UInt16), - ("$7", DataType::UInt32), - ("$8", DataType::UInt64), - ]); - let portal = make_portal( - vec![ - Some(Type::INT8), - Some(Type::INT8), - Some(Type::INT8), - Some(Type::INT8), - Some(Type::INT8), - Some(Type::INT8), - Some(Type::INT8), - Some(Type::INT8), - ], - vec![ - s("100"), - s("1000"), - s("100000"), - s("100000"), - s("200"), - s("1000"), - s("3000000000"), - s("3000000000"), - ], - ); - - let values = parameters_to_scalar_values(&plan, &portal).unwrap(); - assert_eq!(values[0], ScalarValue::Int8(Some(100))); - assert_eq!(values[1], ScalarValue::Int16(Some(1000))); - assert_eq!(values[2], ScalarValue::Int32(Some(100000))); - assert_eq!(values[3], ScalarValue::Int64(Some(100000))); - assert_eq!(values[4], ScalarValue::UInt8(Some(200))); - assert_eq!(values[5], ScalarValue::UInt16(Some(1000))); - assert_eq!(values[6], ScalarValue::UInt32(Some(3000000000))); - assert_eq!(values[7], ScalarValue::UInt64(Some(3000000000))); - } - - #[test] - fn test_int8_coerce_out_of_range() { - let plan = build_plan_with_params(vec![("$1", DataType::Int32)]); - let portal = make_portal( - vec![Some(Type::INT8)], - vec![Some((i32::MAX as i64 + 1).to_string())], - ); - let result = parameters_to_scalar_values(&plan, &portal); - assert!(result.is_err()); - } - - #[test] - fn test_int8_coerce_negative_to_unsigned_out_of_range() { - let plan = build_plan_with_params(vec![("$1", DataType::UInt64)]); - let portal = make_portal(vec![Some(Type::INT8)], vec![s("-1")]); - let result = parameters_to_scalar_values(&plan, &portal); - assert!(result.is_err()); - } - - #[test] - fn test_float4_coerce_in_range() { - let plan = - build_plan_with_params(vec![("$1", DataType::Float32), ("$2", DataType::Float64)]); - let portal = make_portal( - vec![Some(Type::FLOAT4), Some(Type::FLOAT4)], - vec![s("1.5"), s("2.5")], - ); - - let values = parameters_to_scalar_values(&plan, &portal).unwrap(); - assert_eq!(values[0], ScalarValue::Float32(Some(1.5))); - assert_eq!(values[1], ScalarValue::Float64(Some(2.5))); - } - - #[test] - fn test_float4_coerce_to_int_in_range() { - let plan = build_plan_with_params(vec![ - ("$1", DataType::Int8), - ("$2", DataType::Int32), - ("$3", DataType::UInt64), - ]); - let portal = make_portal( - vec![Some(Type::FLOAT4), Some(Type::FLOAT4), Some(Type::FLOAT4)], - vec![s("100"), s("1000"), s("200")], - ); - - let values = parameters_to_scalar_values(&plan, &portal).unwrap(); - assert_eq!(values[0], ScalarValue::Int8(Some(100))); - assert_eq!(values[1], ScalarValue::Int32(Some(1000))); - assert_eq!(values[2], ScalarValue::UInt64(Some(200))); - } - - #[test] - fn test_float4_coerce_to_int_out_of_range() { - let plan = build_plan_with_params(vec![("$1", DataType::Int8)]); - let portal = make_portal(vec![Some(Type::FLOAT4)], vec![s("200")]); - let result = parameters_to_scalar_values(&plan, &portal); - assert!(result.is_err()); - } - - #[test] - fn test_float8_coerce_in_range() { - let plan = - build_plan_with_params(vec![("$1", DataType::Float32), ("$2", DataType::Float64)]); - let portal = make_portal( - vec![Some(Type::FLOAT8), Some(Type::FLOAT8)], - vec![s("1.5"), s("2.5")], - ); - - let values = parameters_to_scalar_values(&plan, &portal).unwrap(); - assert_eq!(values[0], ScalarValue::Float32(Some(1.5))); - assert_eq!(values[1], ScalarValue::Float64(Some(2.5))); - } - - #[test] - fn test_float8_coerce_to_int_in_range() { - let plan = build_plan_with_params(vec![ - ("$1", DataType::Int8), - ("$2", DataType::Int64), - ("$3", DataType::UInt64), - ]); - let portal = make_portal( - vec![Some(Type::FLOAT8), Some(Type::FLOAT8), Some(Type::FLOAT8)], - vec![s("100"), s("1000000"), s("200")], - ); - - let values = parameters_to_scalar_values(&plan, &portal).unwrap(); - assert_eq!(values[0], ScalarValue::Int8(Some(100))); - assert_eq!(values[1], ScalarValue::Int64(Some(1000000))); - assert_eq!(values[2], ScalarValue::UInt64(Some(200))); - } - - #[test] - fn test_float8_coerce_to_int_out_of_range() { - let plan = build_plan_with_params(vec![("$1", DataType::Int8)]); - let portal = make_portal(vec![Some(Type::FLOAT8)], vec![s("200")]); - let result = parameters_to_scalar_values(&plan, &portal); - assert!(result.is_err()); - } - - #[test] - fn test_float8_coerce_negative_to_unsigned_out_of_range() { - let plan = build_plan_with_params(vec![("$1", DataType::UInt64)]); - let portal = make_portal(vec![Some(Type::FLOAT8)], vec![s("-1")]); - let result = parameters_to_scalar_values(&plan, &portal); - assert!(result.is_err()); + // A mismatch inside a list names the item type, not the list type. + let plan = build_plan_with_params(vec![("$1", list(DataType::Int32))]); + let portal = make_portal(vec![Some(Type::NUMERIC_ARRAY)], vec![s("{1}")]); + match parameters_to_scalar_values(&plan, &portal) { + Err(PgWireError::UserError(error)) => assert_eq!( + Some(format!( + "Expected: {}, found: {}", + ConcreteDataType::int32_datatype(), + Type::NUMERIC_ARRAY + )), + error.detail + ), + result => panic!("expected a type mismatch, got {result:?}"), + } } #[test] fn test_null_parameter() { - let plan = build_plan_with_params(vec![("$1", DataType::Int8)]); - let portal = make_portal(vec![Some(Type::INT2)], vec![None]); - - let values = parameters_to_scalar_values(&plan, &portal).unwrap(); - assert_eq!(values[0], ScalarValue::Int8(None)); + for (client_type, server_type, expected) in [ + (Type::INT2, DataType::Int8, ScalarValue::Int8(None)), + (Type::NUMERIC, DataType::UInt64, ScalarValue::UInt64(None)), + ( + Type::NUMERIC_ARRAY, + DataType::List(Arc::new(Field::new("item", DataType::UInt64, true))), + ScalarValue::Null, + ), + ] { + let plan = build_plan_with_params(vec![("$1", server_type)]); + let portal = make_portal(vec![Some(client_type)], vec![None]); + assert_eq!( + vec![expected], + parameters_to_scalar_values(&plan, &portal).unwrap() + ); + } } fn numeric_uint64_array_plan() -> LogicalPlan { @@ -2626,16 +2248,6 @@ mod test { )]) } - fn assert_numeric_out_of_range(result: PgWireResult>) { - match result.unwrap_err() { - PgWireError::UserError(error) => { - assert_eq!("22023", error.code); - assert_eq!("numeric_value_out_of_range", error.message); - } - error => panic!("expected numeric out-of-range error, got {error:?}"), - } - } - fn qbs_timestamp_nanosecond_array_plan() -> LogicalPlan { build_plan_with_params(vec![( "$1", @@ -2670,92 +2282,6 @@ mod test { assert_eq!(expected, values[0]); } - #[test] - fn test_timestamptz_parameter_converts_offset_to_utc() { - let plan = build_plan_with_params(vec![( - "$1", - DataType::Timestamp(TimeUnit::Nanosecond, None), - )]); - let portal = make_portal( - vec![Some(Type::TIMESTAMPTZ)], - vec![s("2024-01-01 08:00:00+08")], - ); - - let values = parameters_to_scalar_values(&plan, &portal).unwrap(); - assert_eq!( - ScalarValue::TimestampNanosecond(Some(1_704_067_200_000_000_000), None), - values[0] - ); - } - - #[test] - fn test_qbs_pg_timestamp_nanosecond_out_of_range_rejected() { - let scalar_plan = build_plan_with_params(vec![( - "$1", - DataType::Timestamp(TimeUnit::Nanosecond, None), - )]); - let array_plan = qbs_timestamp_nanosecond_array_plan(); - for (client_type, value, plan) in [ - (Type::TIMESTAMP, "3000-01-01 00:00:00", &scalar_plan), - (Type::TIMESTAMPTZ, "3000-01-01 00:00:00+00", &scalar_plan), - ( - Type::TIMESTAMP_ARRAY, - r#"{"3000-01-01 00:00:00"}"#, - &array_plan, - ), - ( - Type::TIMESTAMPTZ_ARRAY, - r#"{"3000-01-01 00:00:00+00"}"#, - &array_plan, - ), - ] { - let portal = make_portal(vec![Some(client_type)], vec![s(value)]); - assert_numeric_out_of_range(parameters_to_scalar_values(plan, &portal)); - } - } - - #[test] - fn test_numeric_uint64_scalar_negative_rejected() { - let plan = build_plan_with_params(vec![("$1", DataType::UInt64)]); - let portal = make_portal(vec![Some(Type::NUMERIC)], vec![s("-1")]); - - assert_numeric_out_of_range(parameters_to_scalar_values(&plan, &portal)); - } - - #[test] - fn test_numeric_uint64_scalar_above_u64_max_rejected() { - let plan = build_plan_with_params(vec![("$1", DataType::UInt64)]); - let portal = make_portal(vec![Some(Type::NUMERIC)], vec![s("18446744073709551616")]); - - assert_numeric_out_of_range(parameters_to_scalar_values(&plan, &portal)); - } - - #[test] - fn test_numeric_uint64_scalar_null_preserved() { - let plan = build_plan_with_params(vec![("$1", DataType::UInt64)]); - let portal = make_portal(vec![Some(Type::NUMERIC)], vec![None]); - - let values = parameters_to_scalar_values(&plan, &portal).unwrap(); - assert_eq!(ScalarValue::UInt64(None), values[0]); - } - - #[test] - fn test_numeric_uint64_scalar_u64_max_preserved() { - let plan = build_plan_with_params(vec![("$1", DataType::UInt64)]); - let portal = make_portal(vec![Some(Type::NUMERIC)], vec![s(&u64::MAX.to_string())]); - - let values = parameters_to_scalar_values(&plan, &portal).unwrap(); - assert_eq!(ScalarValue::UInt64(Some(u64::MAX)), values[0]); - } - - #[test] - fn test_numeric_uint64_array_outer_null_preserved() { - let portal = make_portal(vec![Some(Type::NUMERIC_ARRAY)], vec![None]); - - let values = parameters_to_scalar_values(&numeric_uint64_array_plan(), &portal).unwrap(); - assert_eq!(ScalarValue::Null, values[0]); - } - #[test] fn test_numeric_uint64_array_preserves_values_and_null_slots() { let portal = make_portal( @@ -2775,35 +2301,4 @@ mod test { )); assert_eq!(expected, values[0]); } - - #[test] - fn test_numeric_uint64_array_invalid_after_valid_and_null_prefix_rejected() { - let portal = make_portal(vec![Some(Type::NUMERIC_ARRAY)], vec![s("{42,NULL,-1}")]); - - assert_numeric_out_of_range(parameters_to_scalar_values( - &numeric_uint64_array_plan(), - &portal, - )); - } - - #[test] - fn test_numeric_uint64_array_above_u64_max_rejected() { - let portal = make_portal( - vec![Some(Type::NUMERIC_ARRAY)], - vec![s("{18446744073709551616}")], - ); - - assert_numeric_out_of_range(parameters_to_scalar_values( - &numeric_uint64_array_plan(), - &portal, - )); - } - - #[test] - fn test_numeric_uint64_uninferred_array_invalid_value_rejected() { - let plan = LogicalPlanBuilder::empty(true).build().unwrap(); - let portal = make_portal(vec![Some(Type::NUMERIC_ARRAY)], vec![s("{-1}")]); - - assert_numeric_out_of_range(parameters_to_scalar_values(&plan, &portal)); - } }