diff --git a/prql-compiler/src/ast/pl/expr.rs b/prql-compiler/src/ast/pl/expr.rs index 049495952e8d..ebe2377c0c1c 100644 --- a/prql-compiler/src/ast/pl/expr.rs +++ b/prql-compiler/src/ast/pl/expr.rs @@ -77,7 +77,7 @@ pub enum ExprKind { name: String, args: Vec, }, - Set(SetExpr), + Type(TypeExpr), /// a placeholder for values provided after query is compiled Param(String), @@ -599,8 +599,8 @@ impl Display for Expr { ExprKind::BuiltInFunction { .. } => { f.write_str("")?; } - ExprKind::Set(_) => { - writeln!(f, "")?; + ExprKind::Type(_) => { + writeln!(f, "")?; } ExprKind::Param(id) => { writeln!(f, "${id}")?; diff --git a/prql-compiler/src/ast/pl/fold.rs b/prql-compiler/src/ast/pl/fold.rs index 75bb3c2cf27c..66f38b14fe1e 100644 --- a/prql-compiler/src/ast/pl/fold.rs +++ b/prql-compiler/src/ast/pl/fold.rs @@ -119,7 +119,7 @@ pub fn fold_expr_kind(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, }) } diff --git a/prql-compiler/src/ast/pl/types.rs b/prql-compiler/src/ast/pl/types.rs index 622b3117020a..063189aee770 100644 --- a/prql-compiler/src/ast/pl/types.rs +++ b/prql-compiler/src/ast/pl/types.rs @@ -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, SetExpr)>), + Union(Vec<(Option, TypeExpr)>), - /// Set of tuples (product) + /// Type of tuples (product) Tuple(Vec), - /// Set of arrays - Array(Box), + /// Type of arrays + Array(Box), - /// 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, SetExpr), + Single(Option, 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]. @@ -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, @@ -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, } @@ -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) => { @@ -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 { @@ -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 { @@ -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!(), } } } diff --git a/prql-compiler/src/semantic/context.rs b/prql-compiler/src/semantic/context.rs index 06854632f614..ec75b7ce5d73 100644 --- a/prql-compiler/src/semantic/context.rs +++ b/prql-compiler/src/semantic/context.rs @@ -388,16 +388,16 @@ fn get_stdlib_decl(name: &str) -> Option { "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 { diff --git a/prql-compiler/src/semantic/lowering.rs b/prql-compiler/src/semantic/lowering.rs index 591363f0b5b9..c48fa2c96577 100644 --- a/prql-compiler/src/semantic/lowering.rs +++ b/prql-compiler/src/semantic/lowering.rs @@ -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 { diff --git a/prql-compiler/src/semantic/resolver.rs b/prql-compiler/src/semantic/resolver.rs index e1dd506409c8..1a6c4accc7b8 100644 --- a/prql-compiler/src/semantic/resolver.rs +++ b/prql-compiler/src/semantic/resolver.rs @@ -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 })), }; @@ -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)? { @@ -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, }) diff --git a/prql-compiler/src/semantic/snapshots/prql_compiler__semantic__resolver__test__functions_1.snap b/prql-compiler/src/semantic/snapshots/prql_compiler__semantic__resolver__test__functions_1.snap index c8780eb17a9a..efaa4218bbd2 100644 --- a/prql-compiler/src/semantic/snapshots/prql_compiler__semantic__resolver__test__functions_1.snap +++ b/prql-compiler/src/semantic/snapshots/prql_compiler__semantic__resolver__test__functions_1.snap @@ -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 diff --git a/prql-compiler/src/semantic/snapshots/prql_compiler__semantic__resolver__test__functions_nested.snap b/prql-compiler/src/semantic/snapshots/prql_compiler__semantic__resolver__test__functions_nested.snap index 75477a3642f6..6c352641ad83 100644 --- a/prql-compiler/src/semantic/snapshots/prql_compiler__semantic__resolver__test__functions_nested.snap +++ b/prql-compiler/src/semantic/snapshots/prql_compiler__semantic__resolver__test__functions_nested.snap @@ -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 @@ -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 diff --git a/prql-compiler/src/semantic/snapshots/prql_compiler__semantic__resolver__test__functions_pipeline-2.snap b/prql-compiler/src/semantic/snapshots/prql_compiler__semantic__resolver__test__functions_pipeline-2.snap index ea54921c5edc..c8d206fe5494 100644 --- a/prql-compiler/src/semantic/snapshots/prql_compiler__semantic__resolver__test__functions_pipeline-2.snap +++ b/prql-compiler/src/semantic/snapshots/prql_compiler__semantic__resolver__test__functions_pipeline-2.snap @@ -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: @@ -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 diff --git a/prql-compiler/src/semantic/snapshots/prql_compiler__semantic__resolver__test__functions_pipeline.snap b/prql-compiler/src/semantic/snapshots/prql_compiler__semantic__resolver__test__functions_pipeline.snap index 335d1d89e686..a793f584cc1f 100644 --- a/prql-compiler/src/semantic/snapshots/prql_compiler__semantic__resolver__test__functions_pipeline.snap +++ b/prql-compiler/src/semantic/snapshots/prql_compiler__semantic__resolver__test__functions_pipeline.snap @@ -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 diff --git a/prql-compiler/src/semantic/snapshots/prql_compiler__semantic__resolver__test__named_args.snap b/prql-compiler/src/semantic/snapshots/prql_compiler__semantic__resolver__test__named_args.snap index 5afd102820a2..59ec1d7e4315 100644 --- a/prql-compiler/src/semantic/snapshots/prql_compiler__semantic__resolver__test__named_args.snap +++ b/prql-compiler/src/semantic/snapshots/prql_compiler__semantic__resolver__test__named_args.snap @@ -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 @@ -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 diff --git a/prql-compiler/src/semantic/snapshots/prql_compiler__semantic__resolver__test__variables_1.snap b/prql-compiler/src/semantic/snapshots/prql_compiler__semantic__resolver__test__variables_1.snap index ea39d81c2bbc..c48ae4395985 100644 --- a/prql-compiler/src/semantic/snapshots/prql_compiler__semantic__resolver__test__variables_1.snap +++ b/prql-compiler/src/semantic/snapshots/prql_compiler__semantic__resolver__test__variables_1.snap @@ -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 @@ -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 diff --git a/prql-compiler/src/semantic/transforms.rs b/prql-compiler/src/semantic/transforms.rs index 00fb130d4ca4..41cc14b76180 100644 --- a/prql-compiler/src/semantic/transforms.rs +++ b/prql-compiler/src/semantic/transforms.rs @@ -1132,7 +1132,7 @@ mod tests { target_id: 6 ty: Infer ty: - SetExpr: + TypeExpr: Primitive: Column partition: - id: 12 diff --git a/prql-compiler/src/semantic/type_resolver.rs b/prql-compiler/src/semantic/type_resolver.rs index 229faacfb131..8f695e77879e 100644 --- a/prql-compiler/src/semantic/type_resolver.rs +++ b/prql-compiler/src/semantic/type_resolver.rs @@ -7,27 +7,27 @@ use crate::error::{Error, Reason, WithErrorInfo}; use super::Context; -/// Takes a resolved [Expr] and evaluates it a set expression that can be used to construct a type. -pub fn coerce_to_set(expr: Expr, context: &Context) -> Result { +/// Takes a resolved [Expr] and evaluates it a type expression that can be used to construct a type. +pub fn coerce_to_set(expr: Expr, context: &Context) -> Result { coerce_to_named_set(expr, context).map(|(_, s)| s) } -fn coerce_to_named_set(expr: Expr, context: &Context) -> Result<(Option, SetExpr), Error> { +fn coerce_to_named_set(expr: Expr, context: &Context) -> Result<(Option, TypeExpr), Error> { let name = expr.alias; let expr = coerce_kind_to_set(expr.kind, context).map_err(|e| e.with_span(expr.span))?; Ok((name, expr)) } -fn coerce_kind_to_set(expr: ExprKind, context: &Context) -> Result { +fn coerce_kind_to_set(expr: ExprKind, context: &Context) -> Result { // primitives - if let ExprKind::Set(set_expr) = expr { + if let ExprKind::Type(set_expr) = expr { return Ok(set_expr); } // singletons if let ExprKind::Literal(lit) = expr { - return Ok(SetExpr::Singleton(lit)); + return Ok(TypeExpr::Singleton(lit)); } // tuples @@ -35,12 +35,12 @@ fn coerce_kind_to_set(expr: ExprKind, context: &Context) -> Result Result Result { Ok(match &node.kind { ExprKind::Literal(ref literal) => match literal { Literal::Null => Ty::Infer, - Literal::Integer(_) => Ty::SetExpr(SetExpr::Primitive(TyLit::Int)), - Literal::Float(_) => Ty::SetExpr(SetExpr::Primitive(TyLit::Float)), - Literal::Boolean(_) => Ty::SetExpr(SetExpr::Primitive(TyLit::Bool)), - Literal::String(_) => Ty::SetExpr(SetExpr::Primitive(TyLit::Text)), - Literal::Date(_) => Ty::SetExpr(SetExpr::Primitive(TyLit::Date)), - Literal::Time(_) => Ty::SetExpr(SetExpr::Primitive(TyLit::Time)), - Literal::Timestamp(_) => Ty::SetExpr(SetExpr::Primitive(TyLit::Timestamp)), + Literal::Integer(_) => Ty::TypeExpr(TypeExpr::Primitive(TyLit::Int)), + Literal::Float(_) => Ty::TypeExpr(TypeExpr::Primitive(TyLit::Float)), + Literal::Boolean(_) => Ty::TypeExpr(TypeExpr::Primitive(TyLit::Bool)), + Literal::String(_) => Ty::TypeExpr(TypeExpr::Primitive(TyLit::Text)), + Literal::Date(_) => Ty::TypeExpr(TypeExpr::Primitive(TyLit::Date)), + Literal::Time(_) => Ty::TypeExpr(TypeExpr::Primitive(TyLit::Time)), + Literal::Timestamp(_) => Ty::TypeExpr(TypeExpr::Primitive(TyLit::Timestamp)), Literal::ValueAndUnit(_) => Ty::Infer, // TODO Literal::Relation(_) => unreachable!(), }, @@ -97,11 +97,11 @@ pub fn infer_type(node: &Expr, context: &Context) -> Result { ExprKind::Ident(_) | ExprKind::Pipeline(_) | ExprKind::FuncCall(_) => Ty::Infer, ExprKind::SString(_) => Ty::Infer, - ExprKind::FString(_) => Ty::SetExpr(SetExpr::Primitive(TyLit::Text)), + ExprKind::FString(_) => Ty::TypeExpr(TypeExpr::Primitive(TyLit::Text)), ExprKind::Range(_) => Ty::Infer, // TODO ExprKind::TransformCall(call) => Ty::Table(call.infer_type(context)?), - ExprKind::List(_) => Ty::SetExpr(SetExpr::Primitive(TyLit::List)), + ExprKind::List(_) => Ty::TypeExpr(TypeExpr::Primitive(TyLit::List)), _ => Ty::Infer, })