char-varchar-orderby.td 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #
  10. # Make sure the ORDER BY semantics of CHAR/VARCHAR is correct regardless
  11. # of whether the ordering is done at the end or within the dataflow.
  12. > CREATE TABLE char_table (f1 CHAR(20), d TEXT);
  13. > INSERT INTO char_table VALUES ('a', 'only a'), ('a ', 'a space'), ('', 'empty string'), (' ', 'only space'), ('b', 'only b'), (' a', 'space a');
  14. > CREATE TABLE varchar_table (f1 VARCHAR(20));
  15. > INSERT INTO varchar_table VALUES ('a'), ('a '), (''), (' '), ('b'), (' a');
  16. #
  17. # First, when sorting happens outside of the dataflow
  18. #
  19. # We validate the order in this painstaking way in order to prevent testdrive's
  20. # internal automatic sorting from interfering
  21. > SELECT * FROM char_table ORDER BY f1 LIMIT 1 OFFSET 0;
  22. " " "only space"
  23. # empty string is sorted after the single space, see
  24. # https://github.com/MaterializeInc/materialize/pull/7522#issuecomment-895797576
  25. > SELECT * FROM char_table ORDER BY f1 LIMIT 1 OFFSET 1;
  26. " " "empty string"
  27. > SELECT * FROM char_table ORDER BY f1 LIMIT 1 OFFSET 2;
  28. " a " "space a"
  29. > SELECT * FROM char_table ORDER BY f1 LIMIT 1 OFFSET 3;
  30. "a " "only a"
  31. > SELECT * FROM char_table ORDER BY f1 LIMIT 1 OFFSET 4;
  32. "a " "a space"
  33. > SELECT * FROM char_table ORDER BY f1 LIMIT 1 OFFSET 5;
  34. "b " "only b"
  35. > SELECT * FROM varchar_table ORDER BY f1 LIMIT 1 OFFSET 0;
  36. ""
  37. > SELECT * FROM varchar_table ORDER BY f1 LIMIT 1 OFFSET 1;
  38. " "
  39. > SELECT * FROM varchar_table ORDER BY f1 LIMIT 1 OFFSET 2;
  40. " a"
  41. > SELECT * FROM varchar_table ORDER BY f1 LIMIT 1 OFFSET 3;
  42. "a"
  43. > SELECT * FROM varchar_table ORDER BY f1 LIMIT 1 OFFSET 4;
  44. "a "
  45. > SELECT * FROM varchar_table ORDER BY f1 LIMIT 1 OFFSET 5;
  46. "b"
  47. #
  48. # Second, when sorting happens inside the dataflow
  49. #
  50. # Make sure the plan for those queries actually involve the TopK operator
  51. $ set-regex match=u\d+ replacement=UID
  52. ? EXPLAIN OPTIMIZED PLAN AS VERBOSE TEXT FOR SELECT * FROM (SELECT * FROM char_table ORDER BY f1 NULLS FIRST LIMIT 1 OFFSET 0)
  53. Explained Query:
  54. TopK order_by=[#0 asc nulls_first] limit=1
  55. ReadStorage materialize.public.char_table
  56. Source materialize.public.char_table
  57. Target cluster: quickstart
  58. > SELECT * FROM (SELECT * FROM char_table ORDER BY f1 LIMIT 1 OFFSET 0)
  59. " " "empty string"
  60. > SELECT * FROM (SELECT * FROM char_table ORDER BY f1 LIMIT 1 OFFSET 1)
  61. " " "only space"
  62. > SELECT * FROM (SELECT * FROM char_table ORDER BY f1 LIMIT 1 OFFSET 2)
  63. " a " "space a"
  64. # 'a' plus space is sorted before the stand-alone 'a', see
  65. # https://github.com/MaterializeInc/materialize/pull/7522#issuecomment-895797576
  66. > SELECT * FROM (SELECT * FROM char_table ORDER BY f1 LIMIT 1 OFFSET 3)
  67. "a " "a space"
  68. > SELECT * FROM (SELECT * FROM char_table ORDER BY f1 LIMIT 1 OFFSET 4)
  69. "a " "only a"
  70. > SELECT * FROM (SELECT * FROM char_table ORDER BY f1 LIMIT 1 OFFSET 5)
  71. "b " "only b"