|
10 | 10 | import ibis |
11 | 11 | import ibis.expr.operations as ops |
12 | 12 | import ibis.expr.types as ir |
13 | | -from ibis.backends.materialize import operations as mz_ops |
| 13 | +from ibis.backends.materialize.api import mz_now |
14 | 14 | from ibis.backends.postgres import Backend as PostgresBackend |
15 | 15 | from ibis.backends.sql.compilers.materialize import MaterializeCompiler |
16 | 16 |
|
| 17 | +__all__ = ("Backend", "mz_now") |
| 18 | + |
17 | 19 |
|
18 | 20 | class Backend(PostgresBackend): |
19 | 21 | """Materialize backend for Ibis. |
@@ -215,67 +217,6 @@ def set_cluster(self, name: str) -> None: |
215 | 217 | quoted_name = sg.to_identifier(name, quoted=True).sql(self.dialect) |
216 | 218 | cur.execute(f"SET cluster = {quoted_name}") |
217 | 219 |
|
218 | | - def mz_now(self) -> ir.TimestampScalar: |
219 | | - """Return the logical timestamp in Materialize. |
220 | | -
|
221 | | - This returns Materialize's `mz_now()` function, which provides the logical |
222 | | - time at which the query was executed. This is different from `ibis.now()` |
223 | | - (PostgreSQL's `now()`) which returns the system clock time. |
224 | | -
|
225 | | - Key differences from `now()`: |
226 | | - - Returns logical timestamp (for streaming/incremental computation) |
227 | | - - Can be used in temporal filters in materialized views |
228 | | - - Value represents query execution time in Materialize's consistency model |
229 | | -
|
230 | | - Returns |
231 | | - ------- |
232 | | - TimestampScalar |
233 | | - An expression representing Materialize's logical timestamp |
234 | | -
|
235 | | - Examples |
236 | | - -------- |
237 | | - >>> import ibis |
238 | | - >>> con = ibis.materialize.connect() |
239 | | - >>> # Get the current logical timestamp |
240 | | - >>> con.mz_now() |
241 | | -
|
242 | | - Use in temporal filters (e.g., last 30 seconds of data): |
243 | | -
|
244 | | - >>> events = con.table("events") |
245 | | - >>> # Best practice: Isolate mz_now() on one side of comparison |
246 | | - >>> recent = events.filter(con.mz_now() > events.event_ts + ibis.interval(seconds=30)) |
247 | | -
|
248 | | - Compare with regular now(): |
249 | | -
|
250 | | - >>> # System clock time (wall clock) |
251 | | - >>> ibis.now() |
252 | | - >>> # Logical timestamp (streaming time) |
253 | | - >>> con.mz_now() |
254 | | -
|
255 | | - See Also |
256 | | - -------- |
257 | | - ibis.now : PostgreSQL's now() function (system clock time) |
258 | | -
|
259 | | - Notes |
260 | | - ----- |
261 | | - mz_now() is fundamental to Materialize's streaming SQL model and is used |
262 | | - for temporal filters in materialized views to enable incremental computation. |
263 | | -
|
264 | | - **Best Practice**: When using mz_now() in temporal filters, isolate it on one |
265 | | - side of the comparison for optimal incremental computation: |
266 | | -
|
267 | | - - ✅ Good: `mz_now() > created_at + INTERVAL '1 day'` |
268 | | - - ❌ Bad: `mz_now() - created_at > INTERVAL '1 day'` |
269 | | -
|
270 | | - This pattern enables Materialize to efficiently compute incremental updates |
271 | | - without reprocessing the entire dataset. |
272 | | -
|
273 | | - References |
274 | | - ---------- |
275 | | - - Function documentation: https://materialize.com/docs/sql/functions/now_and_mz_now/ |
276 | | - - Idiomatic patterns: https://materialize.com/docs/transform-data/idiomatic-materialize-sql/#temporal-filters |
277 | | - """ |
278 | | - return mz_ops.MzNow().to_expr() |
279 | 220 |
|
280 | 221 | def _register_in_memory_table(self, op: ops.InMemoryTable) -> None: |
281 | 222 | """Register an in-memory table using COPY FROM STDIN. |
|
0 commit comments