Get casts directly between two [`SqlScalarType`]s, with control over the allowed [`CastContext`]. Returns `Err` when the cast is not supported: either no cast exists ([`CastError::InvalidCast`]) or the cast is disallowed ([`CastError::UnsupportedRangeElementType`], e.g. range over float/uint).
(
ecx: &ExprContext,
ccx: CastContext,
from: &SqlScalarType,
to: &SqlScalarType,
)
| 1113 | /// ([`CastError::InvalidCast`]) or the cast is disallowed |
| 1114 | /// ([`CastError::UnsupportedRangeElementType`], e.g. range over float/uint). |
| 1115 | fn get_cast( |
| 1116 | ecx: &ExprContext, |
| 1117 | ccx: CastContext, |
| 1118 | from: &SqlScalarType, |
| 1119 | to: &SqlScalarType, |
| 1120 | ) -> Result<Cast, CastError> { |
| 1121 | use CastContext::*; |
| 1122 | |
| 1123 | if from == to || (ccx == Implicit && from.base_eq(to)) { |
| 1124 | return Ok(Box::new(|expr| expr)); |
| 1125 | } |
| 1126 | |
| 1127 | // Reject casts to range types with unsupported element types at plan time. |
| 1128 | if let SqlScalarType::Range { element_type } = to { |
| 1129 | validate_range_element_type(ecx, element_type)?; |
| 1130 | } |
| 1131 | |
| 1132 | let imp = match VALID_CASTS.get(&(from.into(), to.into())) { |
| 1133 | Some(imp) => imp, |
| 1134 | None => { |
| 1135 | return Err(CastError::InvalidCast { |
| 1136 | ccx, |
| 1137 | from: ecx.humanize_sql_scalar_type(from, false), |
| 1138 | to: ecx.humanize_sql_scalar_type(to, false), |
| 1139 | }); |
| 1140 | } |
| 1141 | }; |
| 1142 | let template = if ccx >= imp.context { |
| 1143 | Some(&imp.template) |
| 1144 | } else { |
| 1145 | None |
| 1146 | }; |
| 1147 | match template.and_then(|template| (template.0)(ecx, ccx, from, to)) { |
| 1148 | Some(cast) => Ok(cast), |
| 1149 | None => Err(CastError::InvalidCast { |
| 1150 | ccx, |
| 1151 | from: ecx.humanize_sql_scalar_type(from, false), |
| 1152 | to: ecx.humanize_sql_scalar_type(to, false), |
| 1153 | }), |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | /// Converts an expression to `SqlScalarType::String`. |
| 1158 | /// |
no test coverage detected