window_functions.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. from textwrap import dedent
  10. from materialize.checks.actions import Testdrive
  11. from materialize.checks.checks import Check
  12. class WindowFunctions(Check):
  13. def initialize(self) -> Testdrive:
  14. return Testdrive(
  15. dedent(
  16. """
  17. > CREATE TABLE window_functions_table (f1 INTEGER, f2 INTEGER);
  18. > INSERT INTO window_functions_table VALUES (1,1), (2, 1), (3, 1);
  19. """
  20. )
  21. )
  22. def manipulate(self) -> list[Testdrive]:
  23. return [
  24. Testdrive(dedent(s))
  25. for s in [
  26. """
  27. > CREATE MATERIALIZED VIEW window_functions_view1 AS
  28. SELECT
  29. row_number() OVER (PARTITION BY f1 ORDER BY f2),
  30. dense_rank() OVER (PARTITION BY f2 ORDER BY f1 DESC),
  31. lag(f1, f2, f1) OVER (ORDER BY f2),
  32. lead(f2, f1, f2) OVER (PARTITION BY f1),
  33. row_number() OVER (RANGE UNBOUNDED PRECEDING) AS range1,
  34. row_number() OVER (RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS range2,
  35. row_number() OVER (ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING) AS rows_between,
  36. first_value(f1) OVER (ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING)
  37. FROM window_functions_table;
  38. > INSERT INTO window_functions_table VALUES (1, 2), (2, 2), (3, 2);
  39. """,
  40. """
  41. > CREATE MATERIALIZED VIEW window_functions_view2 AS
  42. SELECT
  43. row_number() OVER (PARTITION BY f1 ORDER BY f2),
  44. dense_rank() OVER (PARTITION BY f2 ORDER BY f1 DESC),
  45. lag(f1, f2, f1) OVER (ORDER BY f2),
  46. lead(f2, f1, f2) OVER (PARTITION BY f1),
  47. row_number() OVER (RANGE UNBOUNDED PRECEDING) AS range1,
  48. row_number() OVER (RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS range2,
  49. row_number() OVER (ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING) AS rows_between,
  50. first_value(f1) OVER (ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING)
  51. FROM window_functions_table;
  52. > INSERT INTO window_functions_table VALUES (1, 2), (2, 2), (3, 2);
  53. """,
  54. ]
  55. ]
  56. def validate(self) -> Testdrive:
  57. return Testdrive(
  58. dedent(
  59. """
  60. > SELECT * FROM window_functions_view1;
  61. 1 1 2 1 7 7 7 2
  62. 1 2 1 2 4 4 4 1
  63. 1 3 1 2 1 1 1 <null>
  64. 2 1 2 2 8 8 8 2
  65. 2 2 1 2 5 5 5 1
  66. 2 3 2 2 2 2 2 1
  67. 3 1 2 2 9 9 9 3
  68. 3 2 1 2 6 6 6 2
  69. 3 3 3 2 3 3 3 1
  70. > SELECT * FROM window_functions_view2;
  71. 1 1 2 1 7 7 7 2
  72. 1 2 1 2 4 4 4 1
  73. 1 3 1 2 1 1 1 <null>
  74. 2 1 2 2 8 8 8 2
  75. 2 2 1 2 5 5 5 1
  76. 2 3 2 2 2 2 2 1
  77. 3 1 2 2 9 9 9 3
  78. 3 2 1 2 6 6 6 2
  79. 3 3 3 2 3 3 3 1
  80. """
  81. )
  82. )