Skip to content

Commit 1aad573

Browse files
committed
feat: _local
1 parent 21aa6aa commit 1aad573

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+904
-670
lines changed

prqlc/prqlc-ast/src/expr/ident.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,11 @@ impl<'de> Deserialize<'de> for Ident {
144144
}
145145

146146
pub fn display_ident(f: &mut std::fmt::Formatter, ident: &Ident) -> Result<(), std::fmt::Error> {
147-
for part in &ident.path {
147+
let mut path = &ident.path[..];
148+
if path.first().map_or(false, |f| f == "_local") {
149+
path = &path[1..];
150+
}
151+
for part in path {
148152
display_ident_part(f, part)?;
149153
f.write_char('.')?;
150154
}

prqlc/prqlc/src/cli/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ impl Command {
291291
let mut root_module_def = prql_to_pl_tree(sources)?;
292292

293293
drop_module_def(&mut root_module_def.stmts, "std");
294+
drop_module_def(&mut root_module_def.stmts, "_local");
294295

295296
pl_to_prql(&root_module_def)?.into_bytes()
296297
}
@@ -302,6 +303,7 @@ impl Command {
302303
let mut restricted = prqlc::semantic::ast_expand::restrict_module_def(expanded);
303304

304305
drop_module_def(&mut restricted.stmts, "std");
306+
drop_module_def(&mut restricted.stmts, "_local");
305307

306308
pl_to_prql(&restricted)?.into_bytes()
307309
}
@@ -323,6 +325,7 @@ impl Command {
323325
// resolved PL, restricted back into AST
324326
let mut root_module = semantic::ast_expand::restrict_module(root_module.module);
325327
drop_module_def(&mut root_module.stmts, "std");
328+
drop_module_def(&mut root_module.stmts, "_local");
326329
out.extend(pl_to_prql(&root_module)?.into_bytes());
327330

328331
out

prqlc/prqlc/src/codegen/ast.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,12 @@ impl WriteSource for Ident {
275275
opt.consume_width(width as u16)?;
276276

277277
let mut r = String::new();
278-
for part in &self.path {
278+
279+
let mut path = &self.path[..];
280+
if path.first().map_or(false, |f| f == "_local") {
281+
path = &path[1..];
282+
}
283+
for part in path {
279284
r += &write_ident_part(part);
280285
r += ".";
281286
}

prqlc/prqlc/src/ir/pl/fold.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,9 @@ pub fn fold_func_param<T: ?Sized + PlFold>(
300300
.into_iter()
301301
.map(|param| {
302302
Ok(FuncParam {
303+
name: param.name,
304+
ty: fold_type_opt(fold, param.ty)?,
303305
default_value: fold_optional_box(fold, param.default_value)?,
304-
..param
305306
})
306307
})
307308
.try_collect()

prqlc/prqlc/src/semantic/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ pub const NS_PARAM: &str = "_param";
103103
pub const NS_DEFAULT_DB: &str = "db";
104104
pub const NS_QUERY_DEF: &str = "prql";
105105
pub const NS_MAIN: &str = "main";
106+
pub const NS_LOCAL: &str = "_local";
106107

107108
// refers to the containing module (direct parent)
108109
pub const NS_SELF: &str = "_self";

prqlc/prqlc/src/semantic/module.rs

Lines changed: 83 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use crate::ir::pl::{Expr, Ident, Lineage, LineageColumn};
88
use crate::Error;
99

1010
use super::{
11-
NS_DEFAULT_DB, NS_GENERIC, NS_INFER, NS_INFER_MODULE, NS_MAIN, NS_PARAM, NS_QUERY_DEF, NS_SELF,
12-
NS_STD, NS_THAT, NS_THIS,
11+
NS_DEFAULT_DB, NS_GENERIC, NS_INFER, NS_INFER_MODULE, NS_LOCAL, NS_MAIN, NS_PARAM,
12+
NS_QUERY_DEF, NS_SELF, NS_STD, NS_THAT, NS_THIS,
1313
};
1414
use crate::ir::decl::{Decl, DeclKind, Module, RootModule, TableDecl, TableExpr};
1515

@@ -31,15 +31,75 @@ impl Module {
3131
Decl::from(DeclKind::Module(Module::new_database())),
3232
),
3333
(NS_STD.to_string(), Decl::from(DeclKind::default())),
34+
(
35+
NS_LOCAL.to_string(),
36+
Decl::from(DeclKind::Module(Module {
37+
names: HashMap::from_iter(
38+
[
39+
"array",
40+
"scalar",
41+
"tuple",
42+
"range",
43+
"relation",
44+
"transform",
45+
// transforms
46+
"from",
47+
"select",
48+
"filter",
49+
"derive",
50+
"aggregate",
51+
"sort",
52+
"take",
53+
"join",
54+
"group",
55+
"window",
56+
"append",
57+
"intersect",
58+
"remove",
59+
"loop",
60+
// agg
61+
"min",
62+
"max",
63+
"sum",
64+
"average",
65+
"stddev",
66+
"all",
67+
"any",
68+
"concat_array",
69+
"count",
70+
"count_distinct",
71+
"lag",
72+
"lead",
73+
"first",
74+
"last",
75+
"rank",
76+
"rank_dense",
77+
"row_number",
78+
// utils
79+
"in",
80+
"as",
81+
]
82+
.into_iter()
83+
.map(|s| {
84+
(
85+
s.to_string(),
86+
Decl::from(DeclKind::Import(Ident::from_path(vec!["std", s]))),
87+
)
88+
}),
89+
),
90+
shadowed: None,
91+
redirects: vec![
92+
Ident::from_name(NS_THIS),
93+
Ident::from_name(NS_THAT),
94+
Ident::from_name(NS_PARAM),
95+
Ident::from_name(NS_GENERIC),
96+
],
97+
infer_decl: None,
98+
})),
99+
),
34100
]),
35101
shadowed: None,
36-
redirects: vec![
37-
Ident::from_name(NS_THIS),
38-
Ident::from_name(NS_THAT),
39-
Ident::from_name(NS_PARAM),
40-
Ident::from_name(NS_STD),
41-
Ident::from_name(NS_GENERIC),
42-
],
102+
redirects: vec![],
43103
infer_decl: None,
44104
}
45105
}
@@ -236,7 +296,10 @@ impl Module {
236296
LineageColumn::All { input_id, .. } => {
237297
lineage.find_input(*input_id).map(|i| &i.name)
238298
}
239-
LineageColumn::Single { name, .. } => name.as_ref().and_then(|n| n.path.first()),
299+
LineageColumn::Single { name, .. } => name
300+
.as_ref()
301+
.and_then(|n| n.path.first())
302+
.filter(|x| x.as_str() != NS_THIS),
240303
};
241304

242305
// get or create input namespace
@@ -455,6 +518,16 @@ fn decl_has_annotation(decl: &Decl, annotation_name: &Ident) -> bool {
455518
type HintAndSpan = (Option<String>, Option<Span>);
456519

457520
impl RootModule {
521+
pub fn local(&self) -> &Module {
522+
let decl = self.module.names.get(NS_LOCAL).unwrap();
523+
decl.kind.as_module().unwrap()
524+
}
525+
526+
pub fn local_mut(&mut self) -> &mut Module {
527+
let decl = self.module.names.get_mut(NS_LOCAL).unwrap();
528+
decl.kind.as_module_mut().unwrap()
529+
}
530+
458531
/// Finds that main pipeline given a path to either main itself or its parent module.
459532
/// Returns main expr and fq ident of the decl.
460533
pub fn find_main_rel(&self, path: &[String]) -> Result<(&TableExpr, Ident), HintAndSpan> {

prqlc/prqlc/src/semantic/resolve_decls/names.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use itertools::Itertools;
22

33
use crate::ir::decl;
44
use crate::ir::pl::{self, ImportDef, PlFold};
5+
use crate::semantic::{NS_DEFAULT_DB, NS_LOCAL, NS_STD, NS_THIS};
56
use crate::{ast, utils, Error};
67
use crate::{Result, WithErrorInfo};
78

@@ -181,6 +182,7 @@ impl pl::PlFold for NameResolver<'_> {
181182
let (ident, indirections) = self.resolve_ident(ident).with_span(ty.span)?;
182183

183184
if !indirections.is_empty() {
185+
log::debug!("resolved type ident to : {ident} + {indirections:?}");
184186
return Err(
185187
Error::new_simple("types are not allowed indirections").with_span(ty.span)
186188
);
@@ -204,8 +206,16 @@ impl NameResolver<'_> {
204206
let mod_path = match first.as_str() {
205207
"project" => Some(vec![]),
206208
"module" => Some(self.decl_module_path.to_vec()),
207-
"std" => Some(vec!["std".to_string()]),
208-
"db" => Some(vec!["db".to_string()]),
209+
"super" => {
210+
let mut path = self.decl_module_path.to_vec();
211+
path.pop();
212+
Some(path)
213+
}
214+
215+
NS_STD => Some(vec![NS_STD.to_string()]),
216+
NS_DEFAULT_DB => Some(vec![NS_DEFAULT_DB.to_string()]),
217+
NS_THIS => Some(vec![NS_LOCAL.to_string(), NS_THIS.to_string()]),
218+
"prql" => Some(vec![NS_STD.to_string(), "prql".to_string()]),
209219
_ => None,
210220
};
211221
let mod_decl = mod_path
@@ -236,7 +246,10 @@ impl NameResolver<'_> {
236246
let mut steps = ident.into_iter();
237247
let first = steps.next().unwrap();
238248
let indirections = steps.collect_vec();
239-
(ast::Ident::from_name(first), indirections)
249+
(
250+
ast::Ident::from_path(vec![NS_LOCAL.to_string(), first]),
251+
indirections,
252+
)
240253
};
241254
Ok((ident, indirections))
242255
}

prqlc/prqlc/src/semantic/resolver/expr.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::ast::{Ty, TyKind, TyTupleField};
55
use crate::ir::decl::{DeclKind, Module};
66
use crate::ir::pl::*;
77
use crate::semantic::resolver::{flatten, types, Resolver};
8-
use crate::semantic::{NS_INFER, NS_SELF, NS_THAT, NS_THIS};
8+
use crate::semantic::{NS_INFER, NS_LOCAL, NS_SELF, NS_THAT, NS_THIS};
99
use crate::utils::IdGenerator;
1010
use crate::{Error, Reason, Span, WithErrorInfo};
1111

@@ -17,8 +17,8 @@ impl PlFold for Resolver<'_> {
1717
fn fold_type(&mut self, ty: Ty) -> Result<Ty> {
1818
Ok(match ty.kind {
1919
TyKind::Ident(ident) => {
20-
self.root_mod.module.shadow(NS_THIS);
21-
self.root_mod.module.shadow(NS_THAT);
20+
self.root_mod.local_mut().shadow(NS_THIS);
21+
self.root_mod.local_mut().shadow(NS_THAT);
2222

2323
let fq_ident = self.resolve_ident(&ident)?;
2424

@@ -41,8 +41,8 @@ impl PlFold for Resolver<'_> {
4141
let mut ty = decl_ty.clone();
4242
ty.name = ty.name.or(Some(fq_ident.name));
4343

44-
self.root_mod.module.unshadow(NS_THIS);
45-
self.root_mod.module.unshadow(NS_THAT);
44+
self.root_mod.local_mut().unshadow(NS_THIS);
45+
self.root_mod.local_mut().unshadow(NS_THAT);
4646

4747
ty
4848
}
@@ -266,7 +266,7 @@ impl Resolver<'_> {
266266
let except = self.coerce_into_tuple(expr)?;
267267

268268
self.fold_expr(Expr::new(ExprKind::All {
269-
within: Box::new(Expr::new(Ident::from_name(NS_THIS))),
269+
within: Box::new(Expr::new(Ident::from_path(vec![NS_LOCAL, NS_THIS]))),
270270
except: Box::new(except),
271271
}))
272272
}

prqlc/prqlc/src/semantic/resolver/functions.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::ast::{Ty, TyFunc, TyKind};
88
use crate::ir::decl::{Decl, DeclKind, Module};
99
use crate::ir::pl::*;
1010
use crate::semantic::resolver::types;
11-
use crate::semantic::{NS_GENERIC, NS_PARAM, NS_THAT, NS_THIS};
11+
use crate::semantic::{NS_GENERIC, NS_LOCAL, NS_PARAM, NS_THAT, NS_THIS};
1212
use crate::{Error, Span, WithErrorInfo};
1313

1414
use super::Resolver;
@@ -51,7 +51,7 @@ impl Resolver<'_> {
5151

5252
// push the env
5353
let closure_env = Module::from_exprs(closure.env);
54-
self.root_mod.module.stack_push(NS_PARAM, closure_env);
54+
self.root_mod.local_mut().stack_push(NS_PARAM, closure_env);
5555
let closure = Box::new(Func {
5656
env: HashMap::new(),
5757
..*closure
@@ -103,7 +103,7 @@ impl Resolver<'_> {
103103
};
104104

105105
// pop the env
106-
self.root_mod.module.stack_pop(NS_PARAM).unwrap();
106+
self.root_mod.local_mut().stack_pop(NS_PARAM).unwrap();
107107

108108
Ok(Expr { span, ..res })
109109
}
@@ -114,14 +114,14 @@ impl Resolver<'_> {
114114

115115
let (func_env, body, return_ty) = env_of_closure(*closure);
116116

117-
self.root_mod.module.stack_push(NS_PARAM, func_env);
117+
self.root_mod.local_mut().stack_push(NS_PARAM, func_env);
118118

119119
// fold again, to resolve inner variables & functions
120120
let body = self.fold_expr(body)?;
121121

122122
// remove param decls
123123
log::debug!("stack_pop: {:?}", body.id);
124-
let func_env = self.root_mod.module.stack_pop(NS_PARAM).unwrap();
124+
let func_env = self.root_mod.local_mut().stack_pop(NS_PARAM).unwrap();
125125

126126
Ok(if let ExprKind::Func(mut inner_closure) = body.kind {
127127
// body couldn't been resolved - construct a closure to be evaluated later
@@ -176,7 +176,7 @@ impl Resolver<'_> {
176176
// insert _generic.name declaration
177177
let ident = Ident::from_path(vec![NS_GENERIC, generic_param.name.as_str()]);
178178
let decl = Decl::from(DeclKind::Ty(Ty::new(TyKind::GenericArg(generic_id))));
179-
self.root_mod.module.insert(ident, decl).unwrap();
179+
self.root_mod.local_mut().insert(ident, decl).unwrap();
180180
}
181181

182182
func.params = func
@@ -191,7 +191,7 @@ impl Resolver<'_> {
191191
.try_collect()?;
192192
func.return_ty = fold_type_opt(self, func.return_ty)?;
193193

194-
self.root_mod.module.names.remove(NS_GENERIC);
194+
self.root_mod.local_mut().names.remove(NS_GENERIC);
195195
Ok(func)
196196
}
197197

@@ -255,8 +255,8 @@ impl Resolver<'_> {
255255

256256
// resolve relational args
257257
if has_relations {
258-
self.root_mod.module.shadow(NS_THIS);
259-
self.root_mod.module.shadow(NS_THAT);
258+
self.root_mod.local_mut().shadow(NS_THIS);
259+
self.root_mod.local_mut().shadow(NS_THAT);
260260

261261
for (pos, (index, (param, mut arg))) in relations.into_iter().with_position() {
262262
let is_last = matches!(pos, Position::Last | Position::Only);
@@ -276,9 +276,9 @@ impl Resolver<'_> {
276276
if partial_application_position.is_none() {
277277
let frame = arg.lineage.as_ref().unwrap();
278278
if is_last {
279-
self.root_mod.module.insert_frame(frame, NS_THIS);
279+
self.root_mod.local_mut().insert_frame(frame, NS_THIS);
280280
} else {
281-
self.root_mod.module.insert_frame(frame, NS_THAT);
281+
self.root_mod.local_mut().insert_frame(frame, NS_THAT);
282282
}
283283
}
284284

@@ -300,7 +300,9 @@ impl Resolver<'_> {
300300
// add aliased columns into scope
301301
if let Some(alias) = field.alias.clone() {
302302
let id = field.id.unwrap();
303-
self.root_mod.module.insert_frame_col(NS_THIS, alias, id);
303+
self.root_mod
304+
.local_mut()
305+
.insert_frame_col(NS_THIS, alias, id);
304306
}
305307
fields_new.push(field);
306308
}
@@ -323,8 +325,8 @@ impl Resolver<'_> {
323325
}
324326

325327
if has_relations {
326-
self.root_mod.module.unshadow(NS_THIS);
327-
self.root_mod.module.unshadow(NS_THAT);
328+
self.root_mod.local_mut().unshadow(NS_THIS);
329+
self.root_mod.local_mut().unshadow(NS_THAT);
328330
}
329331

330332
Ok(if let Some(position) = partial_application_position {
@@ -425,6 +427,7 @@ fn extract_partial_application(mut func: Box<Func>, position: usize) -> Box<Func
425427

426428
let param_name = format!("_partial_{}", arg.id.unwrap());
427429
let substitute_arg = Expr::new(Ident::from_path(vec![
430+
NS_LOCAL.to_string(),
428431
NS_PARAM.to_string(),
429432
param_name.clone(),
430433
]));

0 commit comments

Comments
 (0)