Skip to content
Open
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
1 change: 1 addition & 0 deletions sqlglot/typing/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ def _annotate_array(self: TypeAnnotator, expression: exp.Array) -> exp.Array:
exp.LaxInt64,
exp.Length,
exp.Ntile,
exp.Null,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this affect coercion? e.g.

select whatever_expr union all select null

I think the type should always coerce to whatever_expr here.

Copy link
Collaborator Author

@geooo109 geooo109 Dec 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you wrap it in a CTE, it works fine.

The UNION ALL expression isn't annotated as is (the expression has unknown type).

We handle the coercion of Set Operations in the annotate_scope, when we have sources (it was added mostly for CTES).

In queries like this select whatever_expr union all select null, we don't apply any coercion between the LHS/RHS. There are cases that make this tricky:

D with tbl as (select 1::INT as a, a+1 as b union all select 1::float as a, NULL as b) select typeof(a), typeof(b) from tbl;
┌───────────┬───────────┐
│ typeof(a) │ typeof(b) │
│  varchar  │  varchar  │
├───────────┼───────────┤
│ FLOAT     │ INTEGER   │
│ FLOAT     │ INTEGER   │
└───────────┴───────────┘

exp.Rank,
exp.RangeBucket,
exp.RegexpInstr,
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/optimizer/annotate_types.sql
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,7 @@ DATETIME;
# dialect: bigquery
CASE WHEN TRUE THEN TIMESTAMP '2020-02-02 00:00:00' ELSE '2010-01-01' END;
TIMESTAMP;

# dialect: bigquery
NULL;
INT64;