mod canonical;
mod outputs;
mod sources;
use super::super::super::value::{is_placeholder_ident_at, PlaceholderPositions};
use super::correlated::{reads_outer_rows, Reads};
use crate::idents::{ident_key, unwrap_expr};
use crate::sql_names::decoded_parts;
use crate::statements::{
SqlBareRead, SqlBoundItem, SqlBoundItemKind, SqlPinSource, SqlQualifiedRead,
};
use outputs::caller_outputs;
use sqlparser::ast::{Expr, Ident, Query};
use std::collections::{BTreeMap, BTreeSet};
pub(in super::super) struct Resolver {
names: Vec<(Option<String>, Option<String>)>,
tables: Vec<bool>,
caller_outputs: Vec<BTreeSet<String>>,
array_outputs: Vec<BTreeMap<String, crate::SqlBoundQuery>>,
outer: BTreeSet<Vec<String>>,
ctes: BTreeMap<String, Option<BTreeSet<String>>>,
positions: Option<Vec<(u32, u32)>>,
}
#[derive(Clone)]
pub(super) struct Sourced {
pub(super) source: SqlPinSource,
pub(super) reads: Vec<SqlBareRead>,
pub(super) qualified_reads: Vec<SqlQualifiedRead>,
}
impl Resolver {
pub(in super::super) fn new(
items: &[SqlBoundItem],
ctes: BTreeMap<String, Option<BTreeSet<String>>>,
positions: PlaceholderPositions<'_>,
) -> Self {
let names: Vec<(Option<String>, Option<String>)> = items
.iter()
.map(|item| {
let table = match &item.kind {
SqlBoundItemKind::Table(name) => decoded_parts(name).pop(),
_ => None,
};
(item.alias.clone(), table)
})
.collect();
let tables = items
.iter()
.map(|item| matches!(item.kind, SqlBoundItemKind::Table(_)))
.collect();
let mut outer: BTreeSet<Vec<String>> = names
.iter()
.filter_map(|(alias, table)| alias.clone().or_else(|| table.clone()))
.map(|name| vec![name])
.collect();
for item in items {
if !item.alias_explicit {
if let SqlBoundItemKind::Table(name) = &item.kind {
outer.insert(decoded_parts(name));
}
}
}
Self {
caller_outputs: items.iter().map(caller_outputs).collect(),
array_outputs: items.iter().map(outputs::array_outputs).collect(),
names,
tables,
outer,
ctes,
positions: positions.map(<[(u32, u32)]>::to_vec),
}
}
pub(in super::super) fn is_table(&self, item: usize) -> bool {
self.tables[item]
}
pub(in super::super) fn reads(&self, query: &Query) -> Reads {
reads_outer_rows(query, &self.outer, &self.ctes, self.positions.as_deref())
}
pub(in super::super) fn reads_with_outer(
&self,
query: &Query,
enclosing: &BTreeSet<Vec<String>>,
) -> Reads {
let mut outer = self.outer.clone();
outer.extend(enclosing.iter().cloned());
reads_outer_rows(query, &outer, &self.ctes, self.positions.as_deref())
}
pub(in super::super) fn column(&self, expr: &Expr) -> Option<(usize, String)> {
match unwrap_expr(expr) {
Expr::Identifier(ident)
if is_placeholder_ident_at(ident, self.positions.as_deref()) =>
{
None
}
Expr::Identifier(ident) => (self.names.len() == 1).then(|| (0, ident_key(ident))),
Expr::CompoundIdentifier(parts) => {
let (qualifier, column) = qualified(parts)?;
Some((self.item_named(&qualifier)?, column))
}
_ => None,
}
}
pub(super) fn candidate_column(&self, expr: &Expr) -> Option<String> {
match unwrap_expr(expr) {
Expr::Identifier(ident)
if self.names.len() > 1
&& !is_placeholder_ident_at(ident, self.positions.as_deref()) =>
{
Some(ident_key(ident))
}
_ => None,
}
}
pub(super) fn projected_array(&self, expr: &Expr) -> Option<Sourced> {
let (item, column) = self.column(expr)?;
Some(Sourced {
source: SqlPinSource::ArrayQuery(self.array_outputs[item].get(&column)?.clone()),
reads: Vec::new(),
qualified_reads: Vec::new(),
})
}
fn item_named(&self, qualifier: &str) -> Option<usize> {
let matches: Vec<usize> = self
.names
.iter()
.enumerate()
.filter(|(_, (alias, table))| match alias {
Some(alias) => alias == qualifier,
None => table.as_deref() == Some(qualifier),
})
.map(|(index, _)| index)
.collect();
(matches.len() == 1).then(|| matches[0])
}
}
fn qualified(parts: &[Ident]) -> Option<(String, String)> {
parts
.iter()
.rev()
.nth(1)
.zip(parts.last())
.map(|(qualifier, column)| (ident_key(qualifier), ident_key(column)))
}
#[cfg(all(test, any(nm_shard_all, nm_shard_2)))]
mod tests;