Skip to content

Commit

Permalink
infer: Allow binding to array type
Browse files Browse the repository at this point in the history
  • Loading branch information
emk committed Nov 6, 2023
1 parent 2e482fc commit 985881a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ impl InferTypes for ast::Expression {
ast::Expression::If(if_expr) => if_expr.infer_types(scope),
ast::Expression::Case(case) => case.infer_types(scope),
ast::Expression::Binop(binop) => binop.infer_types(scope),
ast::Expression::Parens { expression, .. } => expression.infer_types(scope),
ast::Expression::Array(array) => array.infer_types(scope),
ast::Expression::Count(count) => count.infer_types(scope),
ast::Expression::ArrayAgg(array_agg) => array_agg.infer_types(scope),
Expand Down
21 changes: 20 additions & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,13 @@ impl Unify for ValueType<TypeVar> {
spanned: &dyn Spanned,
) -> Result<Self::Resolved> {
match (self, other) {
// TODO: Unify an array with with a TypeVar.
// Unify an array with with a TypeVar.
(ValueType::Simple(SimpleType::Parameter(var)), array_ty @ ValueType::Array(_)) => {
table
.update(var.clone(), ArgumentType::Value(array_ty.clone()), spanned)?
.expect_value_type(spanned)
.cloned()
}
(ValueType::Simple(a), ValueType::Simple(b)) => {
Ok(ValueType::Simple(a.unify(b, table, spanned)?))
}
Expand All @@ -479,6 +485,19 @@ impl Unify for ValueType<TypeVar> {

fn resolve(&self, table: &UnificationTable, spanned: &dyn Spanned) -> Result<Self::Resolved> {
match self {
// TODO: This is duplicated from SimpleType, because we may have
// bound a type variable to an array type.
ValueType::Simple(SimpleType::Parameter(var)) => table
.get(var.clone())
.ok_or_else(|| {
Error::annotated(
format!("cannot resolve type variable: {}", var),
spanned.span(),
"unbound type variable",
)
})?
.expect_value_type(spanned)
.cloned(),
ValueType::Simple(t) => Ok(ValueType::Simple(t.resolve(table, spanned)?)),
ValueType::Array(t) => Ok(ValueType::Array(t.resolve(table, spanned)?)),
}
Expand Down
15 changes: 15 additions & 0 deletions tests/sql/operators/case_array.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- pending: sqlite3 ARRAY not supported

CREATE OR REPLACE TABLE __result1 AS
SELECT
CASE
WHEN TRUE THEN ARRAY[1]
ELSE CAST(NULL AS ARRAY<INT64>)
END AS case_when_true;

CREATE OR REPLACE TABLE __expected1 (
case_when_true ARRAY<INT64>,
);
-- Snowflake does not allow array constants in VALUES.
INSERT INTO __expected1
SELECT [1];

0 comments on commit 985881a

Please sign in to comment.