like.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 Like(Check):
  13. """LIKE, ILIKE, with and without a constant pattern are all compiled and evaluated differently"""
  14. def initialize(self) -> Testdrive:
  15. return Testdrive(
  16. dedent(
  17. """
  18. > CREATE TABLE like_regex_table (f1 STRING, f2 STRING, f3 STRING, f4 STRING);
  19. > INSERT INTO like_regex_table VALUES ('abc', 'abc', 'a~%', '~');
  20. """
  21. )
  22. )
  23. def manipulate(self) -> list[Testdrive]:
  24. return [
  25. Testdrive(dedent(s))
  26. for s in [
  27. """
  28. > CREATE MATERIALIZED VIEW like_regex_view1 AS SELECT f1 LIKE f2 AS c1, f1 ILIKE f2 AS c2, f1 LIKE 'x_z' AS c3, f1 ILIKE 'a_c' AS c4, f1 LIKE 'a~%' ESCAPE '~' AS c5, f1 LIKE f3 ESCAPE '~' AS c6, f1 LIKE f3 ESCAPE f4 AS c7 FROM like_regex_table;
  29. > INSERT INTO like_regex_table VALUES ('klm', 'klm', 'k_m', 'k');
  30. """,
  31. """
  32. > CREATE MATERIALIZED VIEW like_regex_view2 AS SELECT f1 LIKE f2 AS c1, f1 ILIKE f2 AS c2, f1 LIKE 'x_z' AS c3, f1 ILIKE 'a_c' AS c4, f1 LIKE 'a~%' ESCAPE '~' AS c5, f1 LIKE f3 ESCAPE '~' AS c6, f1 LIKE f3 ESCAPE f4 AS c7 FROM like_regex_table;
  33. > INSERT INTO like_regex_table VALUES ('xyz', 'xyz', 'x_%', '_');
  34. """,
  35. ]
  36. ]
  37. def validate(self) -> Testdrive:
  38. return Testdrive(
  39. dedent(
  40. """
  41. > SELECT * FROM like_regex_view1;
  42. true true false true false false false
  43. true true false false false true false
  44. true true true false false true false
  45. > SELECT * FROM like_regex_view2;
  46. true true false true false false false
  47. true true false false false true false
  48. true true true false false true false
  49. """
  50. )
  51. )