Skip to content

Commit

Permalink
Allow nested aggregate types
Browse files Browse the repository at this point in the history
These are weird but the type inference and function lookup code needs to
handle them.
  • Loading branch information
emk committed Nov 1, 2023
1 parent c7bce75 commit 607a7ec
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ impl<TV: TypeVarSupport> fmt::Display for Type<TV> {
pub enum ArgumentType<TV: TypeVarSupport = ResolvedTypeVarsOnly> {
/// A value type.
Value(ValueType<TV>),
/// An aggregating value type.
Aggregating(ValueType<TV>),
/// An aggregating value type. Note that we can nest aggregating types.
Aggregating(Box<ArgumentType<TV>>),
}

impl<TV: TypeVarSupport> ArgumentType<TV> {
Expand Down Expand Up @@ -270,7 +270,7 @@ impl<TV: TypeVarSupport> ArgumentType<TV> {
}

(ArgumentType::Aggregating(a), ArgumentType::Aggregating(b)) => {
Some(ArgumentType::Aggregating(a.common_supertype(b)?))
Some(ArgumentType::Aggregating(Box::new(a.common_supertype(b)?)))
}
_ => None,
}
Expand All @@ -290,9 +290,9 @@ impl Unify for ArgumentType<TypeVar> {
(ArgumentType::Value(a), ArgumentType::Value(b)) => {
Ok(ArgumentType::Value(a.unify(b, table, spanned)?))
}
(ArgumentType::Aggregating(a), ArgumentType::Aggregating(b)) => {
Ok(ArgumentType::Aggregating(a.unify(b, table, spanned)?))
}
(ArgumentType::Aggregating(a), ArgumentType::Aggregating(b)) => Ok(
ArgumentType::Aggregating(Box::new(a.unify(b, table, spanned)?)),
),
_ => Err(Error::annotated(
format!("cannot unify {} and {}", self, other),
spanned.span(),
Expand All @@ -304,9 +304,9 @@ impl Unify for ArgumentType<TypeVar> {
fn resolve(&self, table: &UnificationTable, spanned: &dyn Spanned) -> Result<Self::Resolved> {
match self {
ArgumentType::Value(t) => Ok(ArgumentType::Value(t.resolve(table, spanned)?)),
ArgumentType::Aggregating(t) => {
Ok(ArgumentType::Aggregating(t.resolve(table, spanned)?))
}
ArgumentType::Aggregating(t) => Ok(ArgumentType::Aggregating(Box::new(
t.resolve(table, spanned)?,
))),
}
}
}
Expand Down Expand Up @@ -1172,7 +1172,9 @@ peg::parser! {
/ t:function_type() { Type::Function(t) }

rule argument_type<TV: TypeVarSupport>() -> ArgumentType<TV>
= "Agg" _? "<" _? t:value_type() _? ">" { ArgumentType::Aggregating(t) }
= "Agg" _? "<" _? t:argument_type() _? ">" {
ArgumentType::Aggregating(Box::new(t))
}
/ t:value_type() { ArgumentType::Value(t) }

rule value_type<TV: TypeVarSupport>() -> ValueType<TV>
Expand Down Expand Up @@ -1341,4 +1343,16 @@ pub mod tests {
Type::Argument(ArgumentType::Value(ValueType::Simple(SimpleType::Bool)))
);
}

#[test]
fn parse_nested_agg() {
assert_eq!(
ty("Agg<Agg<INT64>>"),
Type::Argument(ArgumentType::Aggregating(Box::new(
ArgumentType::Aggregating(Box::new(ArgumentType::Value(ValueType::Simple(
SimpleType::Int64
))))
)))
);
}
}
29 changes: 29 additions & 0 deletions tests/sql/functions/aggregate/nested_sum.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-- SUM(SUM(x)) is a thing. This affects the design of the type system.

create temp table t1 (
g1 STRING,
g2 STRING,
x INT64
);

insert into t1 values
('a', 'x', 1),
('a', 'y', 2),
('b', 'x', 3),
('b', 'y', 4);

CREATE OR REPLACE TABLE __result1 AS
SELECT g1, g2, SUM(SUM(x)) OVER (PARTITION BY g2) AS `sum`
FROM t1
GROUP BY g1, g2;

CREATE OR REPLACE TABLE __expected1 (
g1 STRING,
g2 STRING,
`sum` INT64
);
INSERT INTO __expected1 VALUES
('a', 'x', 4),
('a', 'y', 6),
('b', 'x', 4),
('b', 'y', 6);

0 comments on commit 607a7ec

Please sign in to comment.