Skip to content

Commit

Permalink
prevent wildcard aliasing
Browse files Browse the repository at this point in the history
  • Loading branch information
aljazerzen committed Nov 4, 2022
1 parent 7c57b88 commit 4aae055
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 136 deletions.
9 changes: 7 additions & 2 deletions prql-compiler/src/ir/ir_fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,13 @@ pub trait IrFold {
fn fold_column_def(&mut self, cd: ColumnDef) -> Result<ColumnDef> {
Ok(ColumnDef {
id: cd.id,
name: cd.name,
expr: self.fold_ir_expr(cd.expr)?,
kind: match cd.kind {
ColumnDefKind::Wildcard(tid) => ColumnDefKind::Wildcard(tid),
ColumnDefKind::Column { name, expr } => ColumnDefKind::Column {
name,
expr: self.fold_ir_expr(expr)?,
},
},
})
}
fn fold_cid(&mut self, cid: CId) -> Result<CId> {
Expand Down
18 changes: 16 additions & 2 deletions prql-compiler/src/ir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,22 @@ pub struct Window {
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct ColumnDef {
pub id: CId,
pub name: Option<String>,
pub expr: Expr,
pub kind: ColumnDefKind,
}

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub enum ColumnDefKind {
Wildcard(TId),
Column { name: Option<String>, expr: Expr },
}

impl ColumnDef {
pub fn get_name(&self) -> Option<&String> {
match &self.kind {
ColumnDefKind::Column { name, .. } => name.as_ref(),
_ => None,
}
}
}

/// Column id
Expand Down
15 changes: 3 additions & 12 deletions prql-compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ mod test {
}

#[test]
#[ignore]
fn test_precedence() {
assert_display_snapshot!((compile(r###"
from x
Expand Down Expand Up @@ -587,7 +586,6 @@ select `first name`
}

#[test]
#[ignore]
fn test_name_resolving() {
let query = r###"
from numbers
Expand Down Expand Up @@ -729,7 +727,6 @@ select `first name`
}

#[test]
#[ignore]
fn test_range() {
assert_display_snapshot!((compile(r###"
from employees
Expand Down Expand Up @@ -800,14 +797,14 @@ select `first name`
"###).unwrap()), @r###"
WITH table_0 AS (
SELECT
employees.*
*
FROM
employees
LIMIT
10 OFFSET 10
)
SELECT
table_0.*
*
FROM
table_0
ORDER BY
Expand Down Expand Up @@ -924,7 +921,6 @@ select `first name`
}

#[test]
#[ignore]
fn test_dbt_query() {
assert_display_snapshot!((compile(r###"
from {{ ref('stg_orders') }}
Expand Down Expand Up @@ -990,7 +986,6 @@ select [mng_name, managers.gender, salary_avg, salary_sd]"#;
}

#[test]
#[ignore]
fn test_f_string() {
let query = r###"
from employees
Expand Down Expand Up @@ -1055,7 +1050,6 @@ select [mng_name, managers.gender, salary_avg, salary_sd]"#;
}

#[test]
#[ignore]
fn test_sql_of_ast_2() {
let query = r###"
from employees
Expand All @@ -1075,7 +1069,6 @@ select [mng_name, managers.gender, salary_avg, salary_sd]"#;
}

#[test]
#[ignore]
fn test_prql_to_sql_1() {
let query = r#"
from employees
Expand Down Expand Up @@ -1238,7 +1231,6 @@ take 20
}

#[test]
#[ignore]
/// Confirm a nonatomic table works.
fn test_nonatomic_table() {
// A take, then two aggregates
Expand All @@ -1256,7 +1248,7 @@ take 20
assert_display_snapshot!((compile(query).unwrap()), @r###"
WITH table_0 AS (
SELECT
employees.*
*
FROM
employees
LIMIT
Expand Down Expand Up @@ -1362,7 +1354,6 @@ take 20
}

#[test]
#[ignore]
fn test_dialects() {
// Generic
let query = r###"
Expand Down
16 changes: 5 additions & 11 deletions prql-compiler/src/semantic/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use itertools::Itertools;

use crate::ast::{Expr, InterpolateItem, Range};
use crate::error::{Error, Reason};
use crate::ir::{CId, ColumnDef, IdGenerator, Query, TId, Table, TableExpr, Transform};
use crate::ir::{
CId, ColumnDef, ColumnDefKind, IdGenerator, Query, TId, Table, TableExpr, Transform,
};
use crate::{ast, ir};

use super::Context;
Expand Down Expand Up @@ -122,14 +124,7 @@ impl Lowerer {

let star_col = ColumnDef {
id: self.ids.gen_cid(),
expr: ir::Expr {
kind: ir::ExprKind::ExternRef {
variable: "*".to_string(),
table: Some(self.table_mapping[&expr.declared_at.unwrap()]),
},
span: None,
},
name: None,
kind: ColumnDefKind::Wildcard(self.table_mapping[&expr.declared_at.unwrap()]),
};

TableExpr::Ref(ast::TableRef::LocalTable(name.to_string()), vec![star_col])
Expand Down Expand Up @@ -308,8 +303,7 @@ impl Lowerer {
let cid = self.ids.gen_cid();
let def = ColumnDef {
id: cid,
expr,
name,
kind: ColumnDefKind::Column { name, expr },
};

if let Some(id) = id {
Expand Down
18 changes: 4 additions & 14 deletions prql-compiler/src/semantic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,8 @@ mod test {
Ref:
- LocalTable: employees
- - id: 0
name: ~
expr:
kind:
ExternRef:
variable: "*"
table: 0
span: ~
kind:
Wildcard: 0
expr:
Pipeline:
- From: 0
Expand All @@ -126,13 +121,8 @@ mod test {
Ref:
- LocalTable: employees
- - id: 0
name: ~
expr:
kind:
ExternRef:
variable: "*"
table: 0
span: ~
kind:
Wildcard: 0
expr:
Pipeline:
- From: 0
Expand Down
1 change: 0 additions & 1 deletion prql-compiler/src/semantic/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,6 @@ mod test {
}

#[test]
#[ignore]
fn test_func_call_resolve() {
assert_display_snapshot!(compile(r#"
from employees
Expand Down
Loading

0 comments on commit 4aae055

Please sign in to comment.