Convert a sqlparser `Value` to our `SqlValue`. Number literal routing: - Pure integers → `SqlValue::Int`. - Numbers with `.`, `e`, or `E` → `SqlValue::Decimal` (exact arithmetic). - If decimal parse fails → fallback to `SqlValue::Float`, then `SqlValue::String`.
(val: &Value)
| 12 | /// - Numbers with `.`, `e`, or `E` → `SqlValue::Decimal` (exact arithmetic). |
| 13 | /// - If decimal parse fails → fallback to `SqlValue::Float`, then `SqlValue::String`. |
| 14 | pub fn convert_value(val: &Value) -> Result<SqlValue> { |
| 15 | match val { |
| 16 | Value::Number(n, _) => { |
| 17 | if let Ok(i) = n.parse::<i64>() { |
| 18 | Ok(SqlValue::Int(i)) |
| 19 | } else if n.contains('.') || n.contains('e') || n.contains('E') { |
| 20 | // Fractional or scientific notation: prefer exact Decimal. |
| 21 | if let Ok(d) = rust_decimal::Decimal::from_str_exact(n) { |
| 22 | Ok(SqlValue::Decimal(d)) |
| 23 | } else if let Ok(f) = n.parse::<f64>() { |
| 24 | Ok(SqlValue::Float(f)) |
| 25 | } else { |
| 26 | Ok(SqlValue::String(n.clone())) |
| 27 | } |
| 28 | } else if let Ok(f) = n.parse::<f64>() { |
| 29 | Ok(SqlValue::Float(f)) |
| 30 | } else { |
| 31 | Ok(SqlValue::String(n.clone())) |
| 32 | } |
| 33 | } |
| 34 | Value::SingleQuotedString(s) => Ok(SqlValue::String(s.clone())), |
| 35 | Value::Boolean(b) => Ok(SqlValue::Bool(*b)), |
| 36 | Value::Null => Ok(SqlValue::Null), |
| 37 | _ => Err(SqlError::Unsupported { |
| 38 | detail: format!("value literal: {val}"), |
| 39 | }), |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /// Parse an interval string to microseconds. |
| 44 | /// |
no test coverage detected