regex.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 RegexpExtract(Check):
  13. """The regex from regexp_extract has its own ProtoAnalyzedRegex"""
  14. def initialize(self) -> Testdrive:
  15. return Testdrive(
  16. dedent(
  17. """
  18. > CREATE TABLE regexp_extract_table (f1 STRING);
  19. > INSERT INTO regexp_extract_table VALUES ('abc');
  20. """
  21. )
  22. )
  23. def manipulate(self) -> list[Testdrive]:
  24. return [
  25. Testdrive(dedent(s))
  26. for s in [
  27. """
  28. > CREATE MATERIALIZED VIEW regexp_extract_view1 AS SELECT regexp_extract('((a)(.c))|((x)(.z))',f1) AS c1 FROM regexp_extract_table;
  29. > INSERT INTO regexp_extract_table VALUES ('klm');
  30. """,
  31. """
  32. > CREATE MATERIALIZED VIEW regexp_extract_view2 AS SELECT regexp_extract('((a)(.c))|((x)(.z))',f1) AS c1 FROM regexp_extract_table;
  33. > INSERT INTO regexp_extract_table VALUES ('xyz');
  34. """,
  35. ]
  36. ]
  37. def validate(self) -> Testdrive:
  38. return Testdrive(
  39. dedent(
  40. """
  41. > SELECT c1::string FROM regexp_extract_view1;
  42. (,,,xyz,x,yz)
  43. (abc,a,bc,,,)
  44. > SELECT c1::string FROM regexp_extract_view2;
  45. (,,,xyz,x,yz)
  46. (abc,a,bc,,,)
  47. """
  48. )
  49. )
  50. class Regex(Check):
  51. def initialize(self) -> Testdrive:
  52. return Testdrive(
  53. dedent(
  54. """
  55. > CREATE TABLE regex_table (f1 STRING, f2 STRING);
  56. > INSERT INTO regex_table VALUES ('abc', 'abc');
  57. """
  58. )
  59. )
  60. def manipulate(self) -> list[Testdrive]:
  61. return [
  62. Testdrive(dedent(s))
  63. for s in [
  64. """
  65. > CREATE MATERIALIZED VIEW regex_view1 AS SELECT f1 ~ f2 AS c1, f1 ~* f2 AS c2, f1 ~ 'a.c|x.z' AS c3, f1 ~* 'a.c|x.z' AS c4 FROM regex_table;
  66. > INSERT INTO regex_table VALUES ('klm','klm');
  67. """,
  68. """
  69. > CREATE MATERIALIZED VIEW regex_view2 AS SELECT f1 ~ f2 AS c1, f1 ~* f2 AS c2, f1 ~ 'a.c|x.z' AS c3, f1 ~* 'a.c|x.z' AS c4 FROM regex_table;
  70. > INSERT INTO regex_table VALUES ('xyz','xyz');
  71. """,
  72. ]
  73. ]
  74. def validate(self) -> Testdrive:
  75. return Testdrive(
  76. dedent(
  77. """
  78. > SELECT * FROM regex_view1;
  79. true true false false
  80. true true true true
  81. true true true true
  82. > SELECT * FROM regex_view2;
  83. true true false false
  84. true true true true
  85. true true true true
  86. """
  87. )
  88. )