You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/concepts/macros/sqlmesh_macros.md
+44-1Lines changed: 44 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -855,7 +855,9 @@ FROM foo
855
855
856
856
`@UNION` returns a `UNION` query that selects all columns with matching names and data types from the tables.
857
857
858
-
Its first argument is the `UNION` "type", `'DISTINCT'` (removing duplicated rows) or `'ALL'` (returning all rows). Subsequent arguments are the tables to be combined.
858
+
Its first argument can be either a condition or the `UNION` "type". If the first argument evaluates to a boolean (`TRUE` or `FALSE`), it's treated as a condition. If the condition is `FALSE`, only the first table is returned. If it's `TRUE`, the union operation is performed.
859
+
860
+
If the first argument is not a boolean condition, it's treated as the `UNION` "type": either `'DISTINCT'` (removing duplicated rows) or `'ALL'` (returning all rows). Subsequent arguments are the tables to be combined.
859
861
860
862
Let's assume that:
861
863
@@ -882,6 +884,47 @@ SELECT
882
884
FROM bar
883
885
```
884
886
887
+
If the union type is omitted, `'ALL'` is used as the default. So the following expression:
888
+
889
+
```sql linenums="1"
890
+
@UNION(foo, bar)
891
+
```
892
+
893
+
would be rendered as:
894
+
895
+
```sql linenums="1"
896
+
SELECT
897
+
CAST(a ASINT) AS a,
898
+
CAST(c ASTEXT) AS c
899
+
FROM foo
900
+
UNION ALL
901
+
SELECT
902
+
CAST(a ASINT) AS a,
903
+
CAST(c ASTEXT) AS c
904
+
FROM bar
905
+
```
906
+
907
+
You can also use a condition to control whether the union happens:
908
+
909
+
```sql linenums="1"
910
+
@UNION(1>0, 'all', foo, bar)
911
+
```
912
+
913
+
This would render the same as above. However, if the condition is `FALSE`:
914
+
915
+
```sql linenums="1"
916
+
@UNION(1>2, 'all', foo, bar)
917
+
```
918
+
919
+
Only the first table would be selected:
920
+
921
+
```sql linenums="1"
922
+
SELECT
923
+
CAST(a ASINT) AS a,
924
+
CAST(c ASTEXT) AS c
925
+
FROM foo
926
+
```
927
+
885
928
### @HAVERSINE_DISTANCE
886
929
887
930
`@HAVERSINE_DISTANCE` returns the [haversine distance](https://en.wikipedia.org/wiki/Haversine_formula) between two geographic points.
0 commit comments