260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345 | def run_or_dispatch(executor: Any, node: Any, jenv: Any) -> None:
"""
Incremental materialization for materialized='incremental'.
Method called from BaseExecutor.run_sql(...).
"""
meta = getattr(node, "meta", {}) or {}
materialized = meta.get("materialized")
incr_cfg = meta.get("incremental")
# Determine if incremental is enabled
incr_enabled = False
if isinstance(incr_cfg, bool):
incr_enabled = incr_cfg
elif isinstance(incr_cfg, dict):
# default enabled=True if a dict is present unless explicitly disabled
incr_enabled = incr_cfg.get("enabled", True)
# Decide whether to treat this as an incremental model
is_incremental_model = False
if materialized == "incremental":
# respect enabled flag if present
is_incremental_model = incr_enabled if incr_cfg is not None else True
elif materialized is None and incr_enabled:
# legacy: "incremental: true" without explicit materialized
is_incremental_model = True
relation = relation_for(node.name)
if not is_incremental_model:
rendered_sql = _render_sql_safe(executor, node, jenv)
wrap_and_raise = _wrap_and_raise_factory(node.name, relation, rendered_sql)
try:
_create_table_as_or_replace(executor, relation, rendered_sql)
except Exception as e:
wrap_and_raise(e)
return
exists = _safe_exists(executor, relation)
env = _env_with_incremental(jenv, exists)
base_sql = _render_sql_safe(executor, node, env)
delta_sql = meta.get("delta_sql")
if exists and isinstance(delta_sql, str) and delta_sql.strip():
rendered_sql = delta_sql.strip()
else:
rendered_sql = base_sql
fallback_sql = rendered_sql
if exists:
non_incr_env = _env_with_incremental(jenv, False)
fallback_sql = _render_sql_safe(executor, node, non_incr_env)
wrap_incremental = _wrap_and_raise_factory(node.name, relation, rendered_sql)
wrap_full_refresh = _wrap_and_raise_factory(node.name, relation, fallback_sql)
unique_key = _normalize_unique_key(meta.get("unique_key"))
schema_policy = _get_schema_sync_policy(meta)
if not exists:
try:
_create_table_as_or_replace(executor, relation, fallback_sql)
# Contracts: first incremental run creates the table → verify schema
_apply_runtime_contracts_after_incremental(executor, node, relation)
except Exception as e:
wrap_full_refresh(e)
return
_maybe_schema_sync(executor, relation, rendered_sql, schema_policy)
try:
_merge_or_insert_with_fallback(
executor,
relation,
rendered_sql,
unique_key,
fallback_sql=fallback_sql,
on_full_refresh_error=wrap_full_refresh,
)
# Contracts: after merge/insert/full-refresh fallback, verify schema
_apply_runtime_contracts_after_incremental(executor, node, relation)
except ModelExecutionError:
# already wrapped; propagate
raise
except Exception as e:
wrap_incremental(e)
|