Skip to content

Commit 0ce5e90

Browse files
docs: update SQL Macro usecase (#3289)
In PR#3276, we found a MACRO usecase which wasn't listed before. Therefore adding this usecase and explainations of it in the doc. Basically, it would be ok to use `MACRO` for a constant if it would be referenced massive times and it would be bad to use `FUNCTION` for it which will bring massive performance overhead since SQL `FUNCTION` can't do inlining.
1 parent ecbe55d commit 0ce5e90

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

docs/analysis/perfetto-sql-syntax.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,18 @@ is inspired by the macros in Rust.
199199

200200
The following are recommended uses of macros:
201201
- Passing tables as arguments to a "function-like" snippet of SQL.
202+
- Defining simple constants for performance-sensitive queries.
202203

203204
Macros are powerful but also dangerous if used incorrectly, making debugging
204205
extremely difficult. For this reason, it's recommended that they are used
205206
sparingly when they are needed and only for the recommended uses described
206-
above. If only passing around scalar SQL values, use functions as discussed
207207
above.
208208

209+
If only passing around scalar SQL values, functions are generally preferred
210+
for their clarity. However, for simple constants used many times in a
211+
performance-sensitive query, a macro can be more efficient as it avoids the
212+
potential overhead of function calls in a large number.
213+
209214
NOTE: Macros are expanded with a pre-processing step *before* any execution
210215
happens. Expansion is a purely syntatic operation involves replacing the macro
211216
invocation with the SQL tokens in the macro definition.
@@ -264,4 +269,4 @@ RETURNS TableOrSubquery AS
264269
SELECT input_tab.input_col + $y
265270
FROM $x AS input_tab;
266271
)
267-
```
272+
```

0 commit comments

Comments
 (0)