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
31 changes: 27 additions & 4 deletions partiql-ast-passes/src/name_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use indexmap::{IndexMap, IndexSet};
use partiql_ast::ast;
use partiql_ast::ast::{GroupByExpr, GroupKey};
use partiql_ast::visit::{Traverse, Visit, Visitor};
use partiql_catalog::Catalog;
use std::sync::atomic::{AtomicU32, Ordering};

type FnvIndexSet<T> = IndexSet<T, FnvBuildHasher>;
Expand Down Expand Up @@ -82,8 +83,8 @@ enum EnclosingClause {
/// Resolves which clauses in a query produce & consume variable references by walking the
/// AST and collecting variable references. Also partially infers alias if no `AS` alias
/// was provided in the query.
#[derive(Default, Debug)]
pub struct NameResolver {
#[derive(Debug)]
pub struct NameResolver<'c> {
// environment stack tracking
id_path_to_root: Vec<ast::NodeId>,
id_child_stack: Vec<Vec<ast::NodeId>>,
Expand All @@ -99,9 +100,31 @@ pub struct NameResolver {

// errors that occur during name resolution
errors: Vec<AstTransformError>,
catalog: &'c dyn Catalog,
}

impl NameResolver {
impl<'c> NameResolver<'c> {
pub fn new(catalog: &'c dyn Catalog) -> Self {
NameResolver {
// environment stack tracking
id_path_to_root: Default::default(),
id_child_stack: Default::default(),
keyref_stack: Default::default(),
lateral_stack: Default::default(),
id_gen: Default::default(),

// data flow tracking
enclosing_clause: Default::default(),
in_scope: Default::default(),
schema: Default::default(),
aliases: Default::default(),

// errors that occur during name resolution
errors: Default::default(),
catalog,
}
}

pub fn resolve(
&mut self,
query: &ast::AstNode<ast::TopLevelQuery>,
Expand Down Expand Up @@ -188,7 +211,7 @@ impl NameResolver {
}
}

impl<'ast> Visitor<'ast> for NameResolver {
impl<'ast, 'c> Visitor<'ast> for NameResolver<'c> {
fn enter_ast_node(&mut self, id: ast::NodeId) -> Traverse {
self.id_path_to_root.push(id);
if let Some(children) = self.id_child_stack.last_mut() {
Expand Down
5 changes: 3 additions & 2 deletions partiql-logical-planner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use partiql_ast_passes::name_resolver::NameResolver;
use partiql_logical as logical;
use partiql_parser::Parsed;

use partiql_catalog::Catalog;
use partiql_catalog::{Catalog, PartiqlCatalog};

mod builtins;
mod lower;
Expand All @@ -25,7 +25,8 @@ impl<'c> LogicalPlanner<'c> {
parsed: &Parsed,
) -> Result<logical::LogicalPlan<logical::BindingsOp>, AstTransformationError> {
let q = &parsed.ast;
let mut resolver = NameResolver::default();
let catalog = PartiqlCatalog::default();
let mut resolver = NameResolver::new(&catalog);
let registry = resolver.resolve(q)?;
let planner = AstToLogical::new(self.catalog, registry);
planner.lower_query(q)
Expand Down