Skip to content

Commit 5d4792e

Browse files
authored
fix(compiler): Fix scope of same column name from different table (#908)
This is very poor quality code, but it does seem to fix the issue. I will write better quality code if it stays around beyond the rewrite of semantic. There may be other issues, in which case I'll work on those this weekend. Thanks to @aljazeren for the guidance.
1 parent 9b91c8b commit 5d4792e

File tree

3 files changed

+70
-7
lines changed

3 files changed

+70
-7
lines changed

prql-compiler/src/lib.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,4 +1464,49 @@ take 20
14641464
"###
14651465
);
14661466
}
1467+
1468+
#[test]
1469+
fn test_same_column_names() -> Result<()> {
1470+
// #820
1471+
let query = r###"
1472+
table x = (
1473+
from x_table
1474+
select only_in_x = foo
1475+
)
1476+
1477+
table y = (
1478+
from y_table
1479+
select foo
1480+
)
1481+
1482+
from x
1483+
join y [id]
1484+
"###;
1485+
1486+
assert_display_snapshot!(compile(query)?,
1487+
@r###"
1488+
WITH x AS (
1489+
SELECT
1490+
foo AS only_in_x
1491+
FROM
1492+
x_table
1493+
),
1494+
y AS (
1495+
SELECT
1496+
foo
1497+
FROM
1498+
y_table
1499+
)
1500+
SELECT
1501+
x.*,
1502+
y.*,
1503+
id
1504+
FROM
1505+
x
1506+
JOIN y USING(id)
1507+
"###
1508+
);
1509+
1510+
Ok(())
1511+
}
14671512
}

prql-compiler/src/semantic/context.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::ast::*;
77
use crate::error::Span;
88

99
/// Context of the pipeline.
10-
#[derive(Default, Serialize, Deserialize, Clone)]
10+
#[derive(Default, Debug, Serialize, Deserialize, Clone)]
1111
pub struct Context {
1212
/// Map of all accessible names (for each namespace)
1313
pub(crate) scope: Scope,
@@ -68,9 +68,3 @@ impl From<Declaration> for anyhow::Error {
6868
anyhow::anyhow!("Unexpected declaration type: {dec:?}")
6969
}
7070
}
71-
72-
impl Debug for Context {
73-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
74-
writeln!(f, "{:?}", self.declarations)
75-
}
76-
}

prql-compiler/src/semantic/name_resolver.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,30 @@ impl AstFold for NameResolver {
8989
self.context.declare_func(func_def);
9090
None
9191
}
92+
Item::Table(_) => {
93+
// This is *extremely* hacky code to solve #820, and will
94+
// be removed soon™, given we are rewriting semantic.
95+
let extern_refs = self
96+
.context
97+
.declarations
98+
.0
99+
.iter()
100+
.filter_map(|(dec, _)| match dec {
101+
Declaration::ExternRef {
102+
table: _,
103+
variable: var,
104+
} => Some(var),
105+
_ => None,
106+
})
107+
.collect::<Vec<_>>();
108+
109+
self.context
110+
.scope
111+
.variables
112+
.retain(|k, _| !extern_refs.contains(&k));
113+
114+
Some(self.fold_node(node)?)
115+
}
92116
_ => Some(self.fold_node(node)?),
93117
})
94118
})

0 commit comments

Comments
 (0)