coalesce.test 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # 2009 November 10
  2. #
  3. # The author disclaims copyright to this source code. In place of
  4. # a legal notice, here is a blessing:
  5. #
  6. # May you do good and not evil.
  7. # May you find forgiveness for yourself and forgive others.
  8. # May you share freely, never taking more than you give.
  9. #
  10. #***********************************************************************
  11. # Additional test cases for the COALESCE() and IFNULL() functions.
  12. #
  13. set testdir [file dirname $argv0]
  14. source $testdir/tester.tcl
  15. do_test coalesce-1.0 {
  16. db eval {
  17. CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c, d);
  18. INSERT INTO t1 VALUES(1, null, null, null);
  19. INSERT INTO t1 VALUES(2, 2, 99, 99);
  20. INSERT INTO t1 VALUES(3, null, 3, 99);
  21. INSERT INTO t1 VALUES(4, null, null, 4);
  22. INSERT INTO t1 VALUES(5, null, null, null);
  23. INSERT INTO t1 VALUES(6, 22, 99, 99);
  24. INSERT INTO t1 VALUES(7, null, 33, 99);
  25. INSERT INTO t1 VALUES(8, null, null, 44);
  26. SELECT coalesce(b,c,d) FROM t1 ORDER BY a;
  27. }
  28. } {{} 2 3 4 {} 22 33 44}
  29. do_test coalesce-1.1 {
  30. db eval {
  31. SELECT coalesce(d+c+b,d+c,d) FROM t1 ORDER BY a;
  32. }
  33. } {{} 200 102 4 {} 220 132 44}
  34. do_test coalesce-1.2 {
  35. db eval {
  36. SELECT ifnull(d+c+b,ifnull(d+c,d)) FROM t1 ORDER BY a;
  37. }
  38. } {{} 200 102 4 {} 220 132 44}
  39. do_test coalesce-1.3 {
  40. db eval {
  41. SELECT ifnull(ifnull(d+c+b,d+c),d) FROM t1 ORDER BY a;
  42. }
  43. } {{} 200 102 4 {} 220 132 44}
  44. do_test coalesce-1.4 {
  45. db eval {
  46. SELECT ifnull(ifnull(b,c),d) FROM t1 ORDER BY a;
  47. }
  48. } {{} 2 3 4 {} 22 33 44}
  49. do_test coalesce-1.5 {
  50. db eval {
  51. SELECT ifnull(b,ifnull(c,d)) FROM t1 ORDER BY a;
  52. }
  53. } {{} 2 3 4 {} 22 33 44}
  54. do_test coalesce-1.6 {
  55. db eval {
  56. SELECT coalesce(b,NOT b,-b,abs(b),lower(b),length(b),min(b,5),b*123,c)
  57. FROM t1 ORDER BY a;
  58. }
  59. } {{} 2 3 {} {} 22 33 {}}
  60. do_test coalesce-1.7 {
  61. db eval {
  62. SELECT ifnull(nullif(a,4),99)
  63. FROM t1 ORDER BY a;
  64. }
  65. } {1 2 3 99 5 6 7 8}
  66. do_test coalesce-1.8 {
  67. db eval {
  68. pragma vdbe_listing=on;
  69. SELECT coalesce(
  70. CASE WHEN b=2 THEN 123 END,
  71. CASE WHEN b=3 THEN 234 END,
  72. CASE WHEN c=3 THEN 345 WHEN c=33 THEN 456 END,
  73. d
  74. )
  75. FROM t1 ORDER BY a;
  76. }
  77. } {{} 123 345 4 {} 99 456 44}
  78. finish_test