use super::super::value::PlaceholderPositions;
use crate::idents::{ident_key, object_name_ident};
use crate::statements::SqlBoundItemKind;
use sqlparser::ast::{Function, FunctionArguments, ObjectName, TableFunctionArgs};
mod caller_arguments;
mod inputs;
use caller_arguments::caller_supplied_argument;
pub(super) use caller_arguments::unnest_kind_at;
#[rustfmt::skip]
const AGGREGATES: &[&str] = &[
"count", "sum", "avg", "min", "max", "bool_and", "bool_or", "every", "array_agg",
"string_agg", "json_agg", "jsonb_agg", "json_object_agg", "jsonb_object_agg",
"json_objectagg", "json_arrayagg", "xmlagg", "bit_and", "bit_or", "bit_xor", "stddev",
"stddev_pop", "stddev_samp", "variance", "var_pop", "var_samp", "corr", "covar_pop",
"covar_samp", "regr_avgx", "regr_avgy", "regr_count", "regr_intercept", "regr_r2",
"regr_slope", "regr_sxx", "regr_sxy", "regr_syy", "percentile_cont", "percentile_disc",
"mode", "rank", "dense_rank", "percent_rank", "cume_dist", "any_value", "range_agg",
"range_intersect_agg",
];
mod set_returning;
pub use set_returning::SET_RETURNING;
const FIXED_ONE_ROW: &[&str] = &["pg_stat_get_recovery_prefetch"];
const LATER_SCALAR: &[&str] = &[
"gen_random_uuid",
"regexp_count",
"regexp_instr",
"regexp_substr",
];
const POSTGRES_18_SCALAR: &[&str] = &["uuidv4", "uuidv7"];
mod scalar;
use scalar::is_datetime_keyword;
pub use scalar::SCALAR;
#[rustfmt::skip]
const CALLER_SIZED: &[&str] = &[
"unnest", "generate_series", "generate_subscripts", "json_array_elements",
"json_array_elements_text", "jsonb_array_elements", "jsonb_array_elements_text",
"json_each", "json_each_text", "jsonb_each", "jsonb_each_text", "json_object_keys",
"jsonb_object_keys", "string_to_table", "regexp_split_to_table", "regexp_matches",
"json_populate_recordset", "jsonb_populate_recordset", "json_to_recordset",
"jsonb_to_recordset", "jsonb_path_query", "jsonb_path_query_tz", "aclexplode",
"pg_options_to_table", "pg_mcv_list_items", "pg_snapshot_xip", "txid_snapshot_xip",
"ts_parse", "ts_debug",
];
fn builtin(name: &ObjectName, list: &[&str]) -> bool {
let schema = name
.0
.len()
.checked_sub(2)
.and_then(|index| name.0[index].as_ident());
let in_scope = match name.0.len() {
1 => true,
2 => schema.is_some_and(|schema| ident_key(schema) == "pg_catalog"),
_ => false,
};
in_scope && named(name, list)
}
fn named(name: &ObjectName, list: &[&str]) -> bool {
object_name_ident(name).is_some_and(|ident| list.contains(&ident_key(ident).as_str()))
}
pub(super) fn is_aggregate(name: &ObjectName) -> bool {
builtin(name, AGGREGATES)
}
pub(super) fn is_set_returning(name: &ObjectName) -> bool {
builtin(name, SET_RETURNING) && !builtin(name, FIXED_ONE_ROW)
}
pub(super) fn projection_can_expand(function: &Function) -> bool {
let name = &function.name;
if function.over.is_some()
|| is_aggregate(name)
|| builtin(name, FIXED_ONE_ROW)
|| conditional_form(function)
|| array_subquery(function)
{
return false;
}
is_set_returning(name)
|| !(is_datetime_keyword(name)
|| (builtin(name, SCALAR)
&& (!builtin(name, LATER_SCALAR) || pg_catalog_qualified(name)))
|| (builtin(name, POSTGRES_18_SCALAR) && pg_catalog_qualified(name)))
}
pub(super) fn projection_traversal_boundary(function: &Function) -> bool {
function.over.is_some()
|| is_aggregate(&function.name)
|| builtin(&function.name, FIXED_ONE_ROW)
|| (conditional_form(function)
&& object_name_ident(&function.name)
.is_some_and(|ident| ident_key(ident) == "coalesce"))
}
fn array_subquery(function: &Function) -> bool {
matches!(function.args, FunctionArguments::Subquery(_))
&& function.name.0.len() == 1
&& object_name_ident(&function.name)
.is_some_and(|ident| ident.quote_style.is_none() && ident_key(ident) == "array")
}
pub(super) fn conditional_form(function: &Function) -> bool {
function.name.0.len() == 1
&& object_name_ident(&function.name).is_some_and(|ident| {
ident.quote_style.is_none()
&& ["coalesce", "greatest", "least", "nullif"].contains(&ident_key(ident).as_str())
})
}
fn pg_catalog_qualified(name: &ObjectName) -> bool {
name.0.len() == 2
&& name.0[0]
.as_ident()
.is_some_and(|schema| ident_key(schema) == "pg_catalog")
}
pub(super) fn data_backed_projection(
function: &Function,
positions: PlaceholderPositions<'_>,
) -> bool {
if function.over.is_none()
&& named(&function.name, SET_RETURNING)
&& !builtin(&function.name, SET_RETURNING)
{
return true;
}
if !projection_can_expand(function) {
return false;
}
if !builtin(&function.name, CALLER_SIZED) {
return true;
}
match &function.args {
FunctionArguments::List(list) => !list
.args
.iter()
.all(|arg| caller_supplied_argument(arg, positions)),
_ => true,
}
}
pub(super) fn function_kind_at(
name: &ObjectName,
args: &TableFunctionArgs,
positions: PlaceholderPositions<'_>,
) -> SqlBoundItemKind {
let given = args
.args
.iter()
.all(|arg| caller_supplied_argument(arg, positions));
let fixed_catalog_row =
name.0.len() == 2 && builtin(name, FIXED_ONE_ROW) && args.args.is_empty();
if fixed_catalog_row || (given && builtin(name, CALLER_SIZED)) {
SqlBoundItemKind::Other
} else {
SqlBoundItemKind::Opaque
}
}
#[cfg(all(test, any(nm_shard_all, nm_shard_1)))]
mod tests;