mz_now_disjunction_alternatives.yml 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. columns:
  2. - column: "Valid/Invalid"
  3. - column: "mz_now() SQL"
  4. rows:
  5. - "Valid/Invalid": |
  6. <blue>Materialize SQL</blue> ✅
  7. "mz_now() SQL": |
  8. **Rewrite as UNION ALL with possible duplicates**
  9. <span class="copyableCode">
  10. ```mzsql
  11. CREATE MATERIALIZED VIEW forecast_completed_orders_duplicates_possible AS
  12. SELECT item, quantity, status from orders
  13. WHERE status = 'Shipped'
  14. UNION ALL
  15. SELECT item, quantity, status from orders
  16. WHERE order_date + interval '30' minutes >= mz_now()
  17. ;
  18. ```
  19. </span>
  20. **Rewrite as UNION ALL that avoids duplicates across queries**
  21. <span class="copyableCode">
  22. ```mzsql
  23. CREATE MATERIALIZED VIEW forecast_completed_orders_deduplicated_union_all AS
  24. SELECT item, quantity, status from orders
  25. WHERE status = 'Shipped'
  26. UNION ALL
  27. SELECT item, quantity, status from orders
  28. WHERE order_date + interval '30' minutes >= mz_now()
  29. AND status != 'Shipped' -- Deduplicate by excluding those with status 'Shipped'
  30. ;
  31. ```
  32. </span>
  33. **Rewrite as UNION to deduplicate any and all duplicated results**
  34. <span class="copyableCode">
  35. ```mzsql
  36. CREATE MATERIALIZED VIEW forecast_completed_orders_deduplicated_results AS
  37. SELECT item, quantity, status from orders
  38. WHERE status = 'Shipped'
  39. UNION
  40. SELECT item, quantity, status from orders
  41. WHERE order_date + interval '30' minutes >= mz_now()
  42. ;
  43. ```
  44. </span>
  45. - "Valid/Invalid": |
  46. <red>Anti-pattern</red> ❌
  47. "mz_now() SQL": |
  48. <red>Not supported</red>
  49. <div style="background-color: var(--code-block)">
  50. ```none
  51. -- Unsupported
  52. CREATE MATERIALIZED VIEW forecast_completed_orders_unsupported AS
  53. SELECT item, quantity, status from orders
  54. WHERE status = 'Shipped'
  55. OR order_date + interval '30' minutes >= mz_now();
  56. ```
  57. </div>