Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::cmp::Ordering;

use crate::semantic_checks::_helpers::text::{ascii, casefold};
use crate::semantic_checks::_helpers::sql_text::text::{ascii, casefold};
use crate::semantic_checks::constants::{MAX_EDIT_DISTANCE, MIN_ABBREVIATION_LENGTH};
use crate::semantic_checks::models::SemanticDeferral;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ use std::sync::LazyLock;

use regex::Regex;

use crate::semantic_checks::_helpers::bindings::{BindingOwner, binding_positions};
use crate::semantic_checks::_helpers::columns::closest_column;
use crate::semantic_checks::_helpers::explain::{
use crate::semantic_checks::_helpers::explanation::columns::closest_column;
use crate::semantic_checks::_helpers::explanation::explain::{
Binary, ExplainContext, ModelEvidence, Shapes, binary_index, explain_model, is_explained_code,
is_type_code,
};
use crate::semantic_checks::_helpers::messages::{
use crate::semantic_checks::_helpers::explanation::messages::{
compiled, missing_column, pattern, semantic_help, sentence_message,
};
use crate::semantic_checks::_helpers::opt_outs::opt_out_diagnostic;
use crate::semantic_checks::_helpers::parsed_sql::{ParsedModelFacts, parsed_model_facts};
use crate::semantic_checks::_helpers::poison::Poison;
use crate::semantic_checks::_helpers::text::{LineIndex, ascii, plain_text};
use crate::semantic_checks::_helpers::explanation::opt_outs::opt_out_diagnostic;
use crate::semantic_checks::_helpers::recovery::bindings::{BindingOwner, binding_positions};
use crate::semantic_checks::_helpers::recovery::poison::Poison;
use crate::semantic_checks::_helpers::sql_text::parsed_sql::{
ParsedModelFacts, parsed_model_facts,
};
use crate::semantic_checks::_helpers::sql_text::text::{LineIndex, ascii, plain_text};
use crate::semantic_checks::constants::{
MODEL_RESOURCE_TYPE, SEMANTIC_CODE_PREFIX, SQL_TEST_COLUMN_CODE, SQL_TEST_COLUMN_PATTERN,
UNKNOWN_COLUMN_CODE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use std::sync::LazyLock;

use regex::Regex;

use crate::semantic_checks::_helpers::columns::{closest_column, ordered_columns};
use crate::semantic_checks::_helpers::messages::{
use crate::semantic_checks::_helpers::explanation::columns::{closest_column, ordered_columns};
use crate::semantic_checks::_helpers::explanation::messages::{
comparison_help, compiled, missing_column, pattern, semantic_help, sentence_message, type_words,
};
use crate::semantic_checks::_helpers::text::{
use crate::semantic_checks::_helpers::sql_text::text::{
LineIndex, ascii, last_newline_before, newlines_before, prefix,
};
use crate::semantic_checks::constants::{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::LazyLock;

use regex::{Captures, Regex};

use crate::semantic_checks::_helpers::text::ascii;
use crate::semantic_checks::_helpers::sql_text::text::ascii;
use crate::semantic_checks::constants::{
BIGQUERY_DIALECT, COMPARISON_CODE, CONTEXT_SUFFIX_PATTERN, DATE_TYPE, MISSING_PATTERN,
QUOTED_PIECE_PATTERN, SEMANTIC_HELP, TIMESTAMP_TYPE, TYPE_WORDS_PATTERN,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub(crate) mod columns;
pub(crate) mod completion;
pub(crate) mod explain;
pub(crate) mod messages;
pub(crate) mod opt_outs;
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::LazyLock;

use regex::Regex;

use crate::semantic_checks::_helpers::messages::{compiled, pattern};
use crate::semantic_checks::_helpers::explanation::messages::{compiled, pattern};
use crate::semantic_checks::constants::{
ADDITIONAL_HELP_SEPARATOR, FINDING_KINDS, PROJECT_CONFIG_FILENAME, REQUIRE_SQL_ANALYSIS_KEY,
SETTING_SNIPPET_INDENT, SETTINGS_SECTION,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//! Python's `_polyglot_expression_type` for one function argument expression.

use std::collections::HashMap;

use polyglot_sql::{DataType, Expression};
use serde_json::Value;

use crate::semantic_checks::_helpers::sql_text::text::ascii;
use crate::semantic_checks::constants::{
BOOLEAN_RESULT_KINDS, BOOLEAN_TYPE_NAME, COLUMN_KIND, CUSTOM_TYPE_NAME, DECIMAL_TYPE_NAME,
KNOWN_CAST_TYPE_NAMES, TIMESTAMP_KIND, TIMESTAMP_WITH_TIME_ZONE_TYPE_NAME, VARCHAR_DATA_TYPES,
};
use crate::semantic_checks::models::SemanticDeferral;

/// The type Python infers for `expression`, or None.
pub(crate) fn expression_type(
expression: &Expression,
return_types: &HashMap<String, String>,
) -> Result<Option<String>, SemanticDeferral> {
let kind: &str = expression.variant_name();
let name: &str = expression.get_name();
let function_name: &str = if name.is_empty() { kind } else { name };
if kind != COLUMN_KIND
&& let Some(declared) = return_types.get(&ascii(function_name)?.to_ascii_uppercase())
{
return Ok(Some(declared.clone()));
}
if BOOLEAN_RESULT_KINDS.contains(&kind) {
return Ok(Some(BOOLEAN_TYPE_NAME.to_owned()));
}
match expression {
Expression::Cast(cast) | Expression::TryCast(cast) => cast_type(&cast.to),
_ => Ok(None),
}
}

/// Python's reading of a cast's serialised `to` payload.
fn cast_type(data_type: &DataType) -> Result<Option<String>, SemanticDeferral> {
let target: Value =
serde_json::to_value(data_type).map_err(|_| SemanticDeferral::NativeFailure)?;
let Some(raw_type) = target
.get("data_type")
.and_then(Value::as_str)
.filter(|raw| !raw.is_empty())
else {
return Ok(None);
};
let raw_type: &str = ascii(raw_type)?;
let custom_name: Option<&str> = target
.get("name")
.and_then(Value::as_str)
.filter(|name| !name.is_empty());
if raw_type.eq_ignore_ascii_case(CUSTOM_TYPE_NAME)
&& let Some(name) = custom_name
{
return Ok(Some(ascii(name)?.to_ascii_uppercase()));
}
let lowered: String = raw_type.to_ascii_lowercase();
if lowered == TIMESTAMP_KIND && target.get("timezone") == Some(&Value::Bool(true)) {
return Ok(Some(TIMESTAMP_WITH_TIME_ZONE_TYPE_NAME.to_owned()));
}
let type_name: String = KNOWN_CAST_TYPE_NAMES
.iter()
.find(|(raw, _)| *raw == raw_type)
.map_or_else(
|| raw_type.replace('_', " ").to_ascii_uppercase(),
|(_, known)| (*known).to_owned(),
);
if VARCHAR_DATA_TYPES.contains(&lowered.as_str())
&& let Some(length) = integer(&target, "length")
{
return Ok(Some(format!("VARCHAR({length})")));
}
if type_name == DECIMAL_TYPE_NAME
&& let Some(precision) = integer(&target, "precision")
{
return Ok(Some(match integer(&target, "scale") {
Some(scale) => format!("DECIMAL({precision}, {scale})"),
None => format!("DECIMAL({precision})"),
}));
}
Ok(Some(type_name))
}

/// Python's `isinstance(target.get(key), int)` value.
fn integer(target: &Value, key: &str) -> Option<i128> {
let value: &Value = target.get(key)?;
value
.as_i64()
.map(i128::from)
.or_else(|| value.as_u64().map(i128::from))
}
Loading
Loading