char-varchar-distinct.td 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 distinct operator inside a dataflow operates correctly
  11. # with respect to CHAR/VARCHAR, especially in the presence of trailing
  12. # spaces.
  13. #
  14. # The reason for this being a td and not slt is that td has "" wrapping on the output, which is convenient here.
  15. > SELECT 'a '::char(5) UNION DISTINCT SELECT 'a '::char(5);
  16. "a "
  17. # NOTE(benesch): this does not match PostgreSQL, which will choose to preserve
  18. # the trailing spaces of *one* of the datums arbitrarily. We can't allow such
  19. # nondeterminism in our dataflow layer, so we choose to strip all trailing
  20. # spaces.
  21. > SELECT 'a '::char(5) UNION DISTINCT SELECT 'a '::char(10);
  22. "a"
  23. # Ditto.
  24. > SELECT 'a '::char(10) UNION DISTINCT SELECT 'a '::char(5);
  25. "a"
  26. > SELECT 'a '::varchar(5) UNION DISTINCT SELECT 'a '::varchar(5);
  27. "a "
  28. "a "
  29. > SELECT 'a '::varchar(5) UNION DISTINCT SELECT 'a '::char(5);
  30. a
  31. "a "
  32. > SELECT 'a '::varchar(5) UNION DISTINCT SELECT 'a '::text;
  33. "a "
  34. # This again does not match PostgreSQL for the reason described above.
  35. > SELECT 'a '::char(5) UNION DISTINCT SELECT 'a '::text;
  36. "a"
  37. > CREATE TABLE char_table (f1 CHAR(20));
  38. > INSERT INTO char_table VALUES ('a'), ('a '), ('a '), (''), (' '), (NULL);
  39. > CREATE TABLE varchar_table (f1 VARCHAR(20));
  40. > INSERT INTO varchar_table VALUES ('a'), ('a '), ('a '), (''), (' '), (NULL);
  41. > CREATE TABLE string_table (f1 STRING);
  42. > INSERT INTO string_table VALUES ('a'), ('a '), ('a '), (''), (' '), (NULL);
  43. > SELECT DISTINCT f1 FROM char_table;
  44. <null>
  45. " "
  46. "a "
  47. > SELECT COUNT(DISTINCT f1) FROM char_table;
  48. 2
  49. > SELECT f1 FROM char_table GROUP BY f1;
  50. <null>
  51. " "
  52. "a "
  53. > SELECT f1 FROM char_table UNION DISTINCT SELECT f1 FROM char_table;
  54. <null>
  55. " "
  56. "a "
  57. > SELECT f1, LENGTH(f1) , c FROM (SELECT f1, COUNT(*) AS c FROM char_table GROUP BY f1 ORDER BY f1 LIMIT 3);
  58. <null> <null> 1
  59. " " 0 2
  60. "a " 1 3
  61. > SELECT DISTINCT f1 FROM varchar_table;
  62. ""
  63. " "
  64. "<null>"
  65. "a"
  66. "a "
  67. "a "
  68. > SELECT f1, LENGTH(f1) , c FROM (SELECT f1, COUNT(*) AS c FROM varchar_table GROUP BY f1 ORDER BY f1 LIMIT 3);
  69. "" 0 1
  70. " " 2 1
  71. "a" 1 1
  72. > SELECT DISTINCT f1 FROM string_table;
  73. ""
  74. " "
  75. "<null>"
  76. "a"
  77. "a "
  78. "a "
  79. > SELECT f1, LENGTH(f1) , c FROM (SELECT f1, COUNT(*) AS c FROM string_table GROUP BY f1 ORDER BY f1 LIMIT 3);
  80. "" 0 1
  81. " " 2 1
  82. "a" 1 1