mz_now() expressions
Overview
In Materialize, mz_now() function returns
Materialize’s current virtual timestamp (i.e., returns
mz_timestamp). The function can be used in
temporal filters to reduce the
working dataset.
mz_now() expression has the following form:
mz_now() <comparison_operator> <numeric_expr | timestamp_expr>
Idiomatic Materialize SQL
mz_now() expressions to calculate past or future timestamp
Idiomatic Materialize SQL: mz_now() must be used with one of the following comparison operators: =,
<, <=, >, >=, or an operator that desugars to them or to a conjunction
(AND) of them (for example, BETWEEN...AND...). That is, you cannot use
date/time operations directly on mz_now() to calculate a timestamp in the
past or future. Instead, rewrite the query expression to move the operation to
the other side of the comparison.
Examples
|
|
|
|
|
|
Disjunctions (OR)
When used in a materialized view definition, a view definition that is being
indexed (i.e., although you can create the view and perform ad-hoc query on
the view, you cannot create an index on that view), or a SUBSCRIBE
statement:
-
mz_now()clauses can only be combined using anAND, and -
All top-level
WHEREorHAVINGconditions must be combined using anAND, even if themz_now()clause is nested.
For example:
| mz_now() Compound Clause | Valid/Invalid |
|---|---|
|
✅ Valid Ad-hoc queries do not have the same restrictions. |
|
✅ Valid |
|
✅ Valid |
|
❌ Invalid In materialized view definitions, |
|
❌ Invalid In materialized view definitions with |
|
❌ Invalid To index a view whose definitions includes |
Idiomatic Materialize SQL: When mz_now() is included in a materialized
view definition, a view definition that is being indexed, or a SUBSCRIBE
statement, instead of using disjunctions (OR) when using mz_now(), rewrite
the query to use UNION ALL or UNION instead, deduplicating as necessary:
-
In some cases, you may need to modify the conditions to deduplicate results when using
UNION ALL. For example, you might add the negation of one input’s condition to the other as a conjunction. -
In some cases, using
UNIONinstead ofUNION ALLmay suffice if the inputs do not contain other duplicates that need to be retained.
Examples
|
|
Rewrite as UNION ALL with possible duplicates Rewrite as UNION ALL that avoids duplicates across queries Rewrite as UNION to deduplicate any and all duplicated results |
|
|
|