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
6 changes: 3 additions & 3 deletions prql-compiler/src/ast/pl/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub enum ExprKind {
name: String,
args: Vec<Expr>,
},
Set(SetExpr),
Type(TypeExpr),

/// a placeholder for values provided after query is compiled
Param(String),
Expand Down Expand Up @@ -599,8 +599,8 @@ impl Display for Expr {
ExprKind::BuiltInFunction { .. } => {
f.write_str("<built-in>")?;
}
ExprKind::Set(_) => {
writeln!(f, "<set-expr>")?;
ExprKind::Type(_) => {
writeln!(f, "<type-expr>")?;
}
ExprKind::Param(id) => {
writeln!(f, "${id}")?;
Expand Down
2 changes: 1 addition & 1 deletion prql-compiler/src/ast/pl/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pub fn fold_expr_kind<T: ?Sized + AstFold>(fold: &mut T, expr_kind: ExprKind) ->
Param(id) => Param(id),

// None of these capture variables, so we don't need to fold them.
Literal(_) | Set(_) => expr_kind,
Literal(_) | Type(_) => expr_kind,
})
}

Expand Down
56 changes: 28 additions & 28 deletions prql-compiler/src/ast/pl/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,37 @@ use serde::{Deserialize, Serialize};
use super::{Frame, Literal};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, EnumAsInner)]
pub enum SetExpr {
/// Set of a built-in primitive type
pub enum TypeExpr {
/// Type of a built-in primitive type
Primitive(TyLit),

/// Set that contains only a literal value
/// Type that contains only a literal value
Singleton(Literal),

/// Union of sets (sum)
Union(Vec<(Option<String>, SetExpr)>),
Union(Vec<(Option<String>, TypeExpr)>),

/// Set of tuples (product)
/// Type of tuples (product)
Tuple(Vec<TupleElement>),

/// Set of arrays
Array(Box<SetExpr>),
/// Type of arrays
Array(Box<TypeExpr>),

/// Set of sets.
/// Type of sets.
/// Used for exprs that can be converted to SetExpr and then used as a Ty.
Set,
Type,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum TupleElement {
Single(Option<String>, SetExpr),
Single(Option<String>, TypeExpr),
Wildcard,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, EnumAsInner)]
pub enum Ty {
/// Value is an element of this [SetExpr]
SetExpr(SetExpr),
TypeExpr(TypeExpr),

/// Value is a function described by [TyFunc]
// TODO: convert into [Ty::Domain].
Expand Down Expand Up @@ -93,7 +93,7 @@ impl Ty {
// Not handled here. See type_resolver.
(Ty::Infer, _) | (_, Ty::Infer) => false,

(Ty::SetExpr(left), Ty::SetExpr(right)) => left.is_superset_of(right),
(Ty::TypeExpr(left), Ty::TypeExpr(right)) => left.is_superset_of(right),

(Ty::Table(_), Ty::Table(_)) => true,

Expand All @@ -102,17 +102,17 @@ impl Ty {
}
}

impl SetExpr {
fn is_superset_of(&self, subset: &SetExpr) -> bool {
impl TypeExpr {
fn is_superset_of(&self, subset: &TypeExpr) -> bool {
match (self, subset) {
// TODO: convert these to array
(SetExpr::Primitive(TyLit::Column), SetExpr::Primitive(TyLit::Column)) => true,
(SetExpr::Primitive(TyLit::Column), SetExpr::Primitive(_)) => true,
(SetExpr::Primitive(_), SetExpr::Primitive(TyLit::Column)) => false,
(TypeExpr::Primitive(TyLit::Column), TypeExpr::Primitive(TyLit::Column)) => true,
(TypeExpr::Primitive(TyLit::Column), TypeExpr::Primitive(_)) => true,
(TypeExpr::Primitive(_), TypeExpr::Primitive(TyLit::Column)) => false,

(SetExpr::Primitive(l0), SetExpr::Primitive(r0)) => l0 == r0,
(SetExpr::Union(many), one) => many.iter().any(|(_, any)| any.is_superset_of(one)),
(one, SetExpr::Union(many)) => many.iter().all(|(_, each)| one.is_superset_of(each)),
(TypeExpr::Primitive(l0), TypeExpr::Primitive(r0)) => l0 == r0,
(TypeExpr::Union(many), one) => many.iter().any(|(_, any)| any.is_superset_of(one)),
(one, TypeExpr::Union(many)) => many.iter().all(|(_, each)| one.is_superset_of(each)),

(l, r) => l == r,
}
Expand All @@ -122,7 +122,7 @@ impl SetExpr {
impl Display for Ty {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
match &self {
Ty::SetExpr(lit) => write!(f, "{:}", lit),
Ty::TypeExpr(lit) => write!(f, "{:}", lit),
Ty::Table(frame) => write!(f, "table<{frame}>"),
Ty::Infer => write!(f, "infer"),
Ty::Function(func) => {
Expand All @@ -138,11 +138,11 @@ impl Display for Ty {
}
}

impl Display for SetExpr {
impl Display for TypeExpr {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
match &self {
SetExpr::Primitive(lit) => write!(f, "{:}", lit),
SetExpr::Union(ts) => {
TypeExpr::Primitive(lit) => write!(f, "{:}", lit),
TypeExpr::Union(ts) => {
for (i, (_, e)) in ts.iter().enumerate() {
write!(f, "{e}")?;
if i < ts.len() - 1 {
Expand All @@ -151,8 +151,8 @@ impl Display for SetExpr {
}
Ok(())
}
SetExpr::Singleton(lit) => write!(f, "{:}", lit),
SetExpr::Tuple(elements) => {
TypeExpr::Singleton(lit) => write!(f, "{:}", lit),
TypeExpr::Tuple(elements) => {
write!(f, "[")?;
for e in elements {
match e {
Expand All @@ -170,8 +170,8 @@ impl Display for SetExpr {
}
Ok(())
}
SetExpr::Set => write!(f, "set"),
SetExpr::Array(_) => todo!(),
TypeExpr::Type => write!(f, "set"),
TypeExpr::Array(_) => todo!(),
}
}
}
8 changes: 4 additions & 4 deletions prql-compiler/src/semantic/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,16 +388,16 @@ fn get_stdlib_decl(name: &str) -> Option<ExprKind> {
"timestamp" => TyLit::Timestamp,
"table" => {
// TODO: this is just a dummy that gets intercepted when resolving types
return Some(ExprKind::Set(SetExpr::Array(Box::new(SetExpr::Singleton(
Literal::Null,
)))));
return Some(ExprKind::Type(TypeExpr::Array(Box::new(
TypeExpr::Singleton(Literal::Null),
))));
}
"column" => TyLit::Column,
"list" => TyLit::List,
"scalar" => TyLit::Scalar,
_ => return None,
};
Some(ExprKind::Set(SetExpr::Primitive(ty_lit)))
Some(ExprKind::Type(TypeExpr::Primitive(ty_lit)))
}

impl Default for DeclKind {
Expand Down
2 changes: 1 addition & 1 deletion prql-compiler/src/semantic/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ impl Lowerer {
| pl::ExprKind::List(_)
| pl::ExprKind::Closure(_)
| pl::ExprKind::Pipeline(_)
| pl::ExprKind::Set(_)
| pl::ExprKind::Type(_)
| pl::ExprKind::TransformCall(_) => {
log::debug!("cannot lower {ast:?}");
return Err(Error::new(Reason::Unexpected {
Expand Down
8 changes: 4 additions & 4 deletions prql-compiler/src/semantic/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl AstFold for Resolver {
name: ty_def.name,
value: Box::new(ty_def.value.unwrap_or_else(|| {
let mut e = Expr::null();
e.ty = Some(Ty::SetExpr(SetExpr::Set));
e.ty = Some(Ty::TypeExpr(TypeExpr::Type));
e
})),
};
Expand Down Expand Up @@ -448,7 +448,7 @@ impl Resolver {
// evaluate
let needs_window = (closure.body_ty)
.as_ref()
.map(|ty| ty.is_superset_of(&Ty::SetExpr(SetExpr::Primitive(TyLit::Column))))
.map(|ty| ty.is_superset_of(&Ty::TypeExpr(TypeExpr::Primitive(TyLit::Column))))
.unwrap_or_default();

let mut res = match self.cast_built_in_function(closure)? {
Expand Down Expand Up @@ -780,11 +780,11 @@ impl Resolver {
let set_expr = type_resolver::coerce_to_set(expr, &self.context)?;

// TODO: workaround
if let SetExpr::Array(_) = set_expr {
if let TypeExpr::Array(_) = set_expr {
return Ok(Some(Ty::Table(Frame::default())));
}

Some(Ty::SetExpr(set_expr))
Some(Ty::TypeExpr(set_expr))
}
None => None,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ expression: "resolve_derive(r#\"\n func subtract a b -> a - b\n\n
target_id: 7
ty: Infer
ty:
SetExpr:
TypeExpr:
Primitive: Column
alias: net_salary

Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ expression: "resolve_derive(r#\"\n func lag_day x -> s\"lag_day_todo(
Literal:
Integer: 1
ty:
SetExpr:
TypeExpr:
Primitive: Int
ty: Infer
op: Add
Expand All @@ -53,6 +53,6 @@ expression: "resolve_derive(r#\"\n func lag_day x -> s\"lag_day_todo(
target_id: 8
ty: Infer
ty:
SetExpr:
TypeExpr:
Primitive: Column

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ expression: "resolve_derive(r#\"\n func plus_one x -> x + 1\n
Literal:
Integer: 2
ty:
SetExpr:
TypeExpr:
Primitive: Int
op: Add
right:
Expand All @@ -34,11 +34,11 @@ expression: "resolve_derive(r#\"\n func plus_one x -> x + 1\n
Literal:
Integer: 1
ty:
SetExpr:
TypeExpr:
Primitive: Int
ty: Infer
ty:
SetExpr:
TypeExpr:
Primitive: Column
alias: b

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ expression: "resolve_derive(r#\"\n from a\n derive one = (
target_id: 6
ty: Infer
ty:
SetExpr:
TypeExpr:
Primitive: Column
alias: one

Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ expression: "resolve_derive(r#\"\n func add x to:1 -> x + to\n\n
Literal:
Integer: 3
ty:
SetExpr:
TypeExpr:
Primitive: Int
ty:
SetExpr:
TypeExpr:
Primitive: Column
alias: added
- id: 25
Expand All @@ -40,10 +40,10 @@ expression: "resolve_derive(r#\"\n func add x to:1 -> x + to\n\n
Literal:
Integer: 1
ty:
SetExpr:
TypeExpr:
Primitive: Int
ty:
SetExpr:
TypeExpr:
Primitive: Column
alias: added_default

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ expression: "resolve_derive(r#\"\n from employees\n derive
target_id: 6
ty: Infer
ty:
SetExpr:
TypeExpr:
Primitive: Column
alias: gross_salary
- id: 16
Expand All @@ -44,7 +44,7 @@ expression: "resolve_derive(r#\"\n from employees\n derive
target_id: 6
ty: Infer
ty:
SetExpr:
TypeExpr:
Primitive: Column
alias: gross_cost

2 changes: 1 addition & 1 deletion prql-compiler/src/semantic/transforms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,7 @@ mod tests {
target_id: 6
ty: Infer
ty:
SetExpr:
TypeExpr:
Primitive: Column
partition:
- id: 12
Expand Down
Loading