Skip to content

[DRAFT] Read variable name from Catalog (TypeEnv) during name resolution #427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
40 changes: 33 additions & 7 deletions partiql-ast-passes/src/name_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::error::{AstTransformError, AstTransformationError};
use fnv::FnvBuildHasher;
use indexmap::{IndexMap, IndexSet};
use partiql_ast::ast;
use partiql_ast::ast::{GroupByExpr, GroupKey};
use partiql_ast::ast::{GroupByExpr, GroupKey, SymbolPrimitive};
use partiql_ast::visit::{Traverse, Visit, Visitor};
use partiql_catalog::Catalog;
use std::sync::atomic::{AtomicU32, Ordering};
Expand Down Expand Up @@ -209,6 +209,19 @@ impl<'c> NameResolver<'c> {
fn push_consume_name(&mut self, name: NameRef) {
self.keyref_stack.last_mut().unwrap().consume.insert(name);
}

#[inline]
fn catalog_resolvable(&mut self, name: &SymbolPrimitive) -> bool {
// TODO fix the name look up based on case sensitivity:
// https://github.com/partiql/partiql-lang-rust/issues/426
dbg!(self.catalog);

if let Some(_) = self.catalog.resolve_type(name.value.as_str()) {
true
} else {
false
}
}
}

impl<'ast, 'c> Visitor<'ast> for NameResolver<'c> {
Expand Down Expand Up @@ -358,19 +371,32 @@ impl<'ast, 'c> Visitor<'ast> for NameResolver<'c> {
// in a From path, a prefix `@` means to look locally before globally Cf. specification section 10
let name = if is_from_path {
match &var_ref.qualifier {
ast::ScopeQualifier::Unqualified => NameRef {
sym: var_ref.name.clone(),
lookup: vec![NameLookup::Global, NameLookup::Local],
},
ast::ScopeQualifier::Unqualified => {
if self.catalog_resolvable(&var_ref.name) {
NameRef {
sym: var_ref.name.clone(),
lookup: vec![NameLookup::Global],
}
} else {
// self.errors.push(AstTransformError::IllegalState(format!(
// "No schema found for [{:?}] in the Catalog",
// &var_ref.name
// )));
NameRef {
sym: var_ref.name.clone(),
lookup: vec![NameLookup::Global],
}
}
}
ast::ScopeQualifier::Qualified => NameRef {
sym: var_ref.name.clone(),
lookup: vec![NameLookup::Local, NameLookup::Global],
lookup: vec![NameLookup::Local],
},
}
} else {
NameRef {
sym: var_ref.name.clone(),
lookup: vec![NameLookup::Local, NameLookup::Global],
lookup: vec![NameLookup::Local],
}
};

Expand Down
1 change: 1 addition & 0 deletions partiql-logical-planner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ thiserror = "1.0"

[dev-dependencies]
partiql-eval = { path = "../partiql-eval", version = "0.5.*" }
partiql-types = { path = "../partiql-types", version = "0.5.*"}
8 changes: 6 additions & 2 deletions partiql-logical-planner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ impl<'c> LogicalPlanner<'c> {
mod tests {
use assert_matches::assert_matches;
use partiql_ast_passes::error::AstTransformationError;
use partiql_catalog::PartiqlCatalog;
use partiql_catalog::Catalog;
use partiql_catalog::{PartiqlCatalog, TypeEnvEntry};
use partiql_types::any;

use partiql_eval::env::basic::MapBindings;

Expand All @@ -59,14 +61,16 @@ mod tests {
fn lower(
parsed: &Parsed,
) -> Result<logical::LogicalPlan<logical::BindingsOp>, AstTransformationError> {
let catalog = PartiqlCatalog::default();
let mut catalog = PartiqlCatalog::default();
let _oid = catalog.add_type_entry(TypeEnvEntry::new("customer", &[], any!()));
let planner = LogicalPlanner::new(&catalog);
planner.lower(parsed)
}

#[track_caller]
fn evaluate(logical: LogicalPlan<BindingsOp>, bindings: MapBindings<Value>) -> Value {
let catalog = PartiqlCatalog::default();

let mut planner = plan::EvaluatorPlanner::new(EvaluationMode::Permissive, &catalog);
let mut plan = planner.compile(&logical).expect("Expect no plan error");
println!("{}", plan.to_dot_graph());
Expand Down
2 changes: 0 additions & 2 deletions partiql-logical-planner/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,6 @@ impl<'a> AstToLogical<'a> {
}
}

// TODO in the presence of schema, error if the variable reference doesn't correspond to a data table

// assume global
ValueExpr::VarRef(symprim_to_binding(&varref.name), VarRefType::Global)
}
Expand Down