|
| 1 | +//! Python's `_polyglot_expression_type` for one function argument expression. |
| 2 | +
|
| 3 | +use std::collections::HashMap; |
| 4 | + |
| 5 | +use polyglot_sql::{DataType, Expression}; |
| 6 | +use serde_json::Value; |
| 7 | + |
| 8 | +use crate::semantic_checks::_helpers::sql_text::text::ascii; |
| 9 | +use crate::semantic_checks::constants::{ |
| 10 | + BOOLEAN_RESULT_KINDS, BOOLEAN_TYPE_NAME, COLUMN_KIND, CUSTOM_TYPE_NAME, DECIMAL_TYPE_NAME, |
| 11 | + KNOWN_CAST_TYPE_NAMES, TIMESTAMP_KIND, TIMESTAMP_WITH_TIME_ZONE_TYPE_NAME, VARCHAR_DATA_TYPES, |
| 12 | +}; |
| 13 | +use crate::semantic_checks::models::SemanticDeferral; |
| 14 | + |
| 15 | +/// The type Python infers for `expression`, or None. |
| 16 | +pub(crate) fn expression_type( |
| 17 | + expression: &Expression, |
| 18 | + return_types: &HashMap<String, String>, |
| 19 | +) -> Result<Option<String>, SemanticDeferral> { |
| 20 | + let kind: &str = expression.variant_name(); |
| 21 | + let name: &str = expression.get_name(); |
| 22 | + let function_name: &str = if name.is_empty() { kind } else { name }; |
| 23 | + if kind != COLUMN_KIND |
| 24 | + && let Some(declared) = return_types.get(&ascii(function_name)?.to_ascii_uppercase()) |
| 25 | + { |
| 26 | + return Ok(Some(declared.clone())); |
| 27 | + } |
| 28 | + if BOOLEAN_RESULT_KINDS.contains(&kind) { |
| 29 | + return Ok(Some(BOOLEAN_TYPE_NAME.to_owned())); |
| 30 | + } |
| 31 | + match expression { |
| 32 | + Expression::Cast(cast) | Expression::TryCast(cast) => cast_type(&cast.to), |
| 33 | + _ => Ok(None), |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +/// Python's reading of a cast's serialised `to` payload. |
| 38 | +fn cast_type(data_type: &DataType) -> Result<Option<String>, SemanticDeferral> { |
| 39 | + let target: Value = |
| 40 | + serde_json::to_value(data_type).map_err(|_| SemanticDeferral::NativeFailure)?; |
| 41 | + let Some(raw_type) = target |
| 42 | + .get("data_type") |
| 43 | + .and_then(Value::as_str) |
| 44 | + .filter(|raw| !raw.is_empty()) |
| 45 | + else { |
| 46 | + return Ok(None); |
| 47 | + }; |
| 48 | + let raw_type: &str = ascii(raw_type)?; |
| 49 | + let custom_name: Option<&str> = target |
| 50 | + .get("name") |
| 51 | + .and_then(Value::as_str) |
| 52 | + .filter(|name| !name.is_empty()); |
| 53 | + if raw_type.eq_ignore_ascii_case(CUSTOM_TYPE_NAME) |
| 54 | + && let Some(name) = custom_name |
| 55 | + { |
| 56 | + return Ok(Some(ascii(name)?.to_ascii_uppercase())); |
| 57 | + } |
| 58 | + let lowered: String = raw_type.to_ascii_lowercase(); |
| 59 | + if lowered == TIMESTAMP_KIND && target.get("timezone") == Some(&Value::Bool(true)) { |
| 60 | + return Ok(Some(TIMESTAMP_WITH_TIME_ZONE_TYPE_NAME.to_owned())); |
| 61 | + } |
| 62 | + let type_name: String = KNOWN_CAST_TYPE_NAMES |
| 63 | + .iter() |
| 64 | + .find(|(raw, _)| *raw == raw_type) |
| 65 | + .map_or_else( |
| 66 | + || raw_type.replace('_', " ").to_ascii_uppercase(), |
| 67 | + |(_, known)| (*known).to_owned(), |
| 68 | + ); |
| 69 | + if VARCHAR_DATA_TYPES.contains(&lowered.as_str()) |
| 70 | + && let Some(length) = integer(&target, "length") |
| 71 | + { |
| 72 | + return Ok(Some(format!("VARCHAR({length})"))); |
| 73 | + } |
| 74 | + if type_name == DECIMAL_TYPE_NAME |
| 75 | + && let Some(precision) = integer(&target, "precision") |
| 76 | + { |
| 77 | + return Ok(Some(match integer(&target, "scale") { |
| 78 | + Some(scale) => format!("DECIMAL({precision}, {scale})"), |
| 79 | + None => format!("DECIMAL({precision})"), |
| 80 | + })); |
| 81 | + } |
| 82 | + Ok(Some(type_name)) |
| 83 | +} |
| 84 | + |
| 85 | +/// Python's `isinstance(target.get(key), int)` value. |
| 86 | +fn integer(target: &Value, key: &str) -> Option<i128> { |
| 87 | + let value: &Value = target.get(key)?; |
| 88 | + value |
| 89 | + .as_i64() |
| 90 | + .map(i128::from) |
| 91 | + .or_else(|| value.as_u64().map(i128::from)) |
| 92 | +} |
0 commit comments