PageRenderTime 65ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/test/e_expr.test

https://bitbucket.org/eumario/csharp-sqlite
Unknown | 1868 lines | 1694 code | 174 blank | 0 comment | 0 complexity | f806de886a50408725651d21e1a2461d MD5 | raw file
  1. # 2010 July 16
  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. #
  12. # This file implements tests to verify that the "testable statements" in
  13. # the lang_expr.html document are correct.
  14. #
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. source $testdir/malloc_common.tcl
  18. proc do_expr_test {tn expr type value} {
  19. uplevel do_execsql_test $tn [list "SELECT typeof($expr), $expr"] [
  20. list [list $type $value]
  21. ]
  22. }
  23. proc do_qexpr_test {tn expr value} {
  24. uplevel do_execsql_test $tn [list "SELECT quote($expr)"] [list $value]
  25. }
  26. # Set up three global variables:
  27. #
  28. # ::opname An array mapping from SQL operator to an easy to parse
  29. # name. The names are used as part of test case names.
  30. #
  31. # ::opprec An array mapping from SQL operator to a numeric
  32. # precedence value. Operators that group more tightly
  33. # have lower numeric precedences.
  34. #
  35. # ::oplist A list of all SQL operators supported by SQLite.
  36. #
  37. foreach {op opn} {
  38. || cat * mul / div % mod + add
  39. - sub << lshift >> rshift & bitand | bitor
  40. < less <= lesseq > more >= moreeq = eq1
  41. == eq2 <> ne1 != ne2 IS is LIKE like
  42. GLOB glob AND and OR or MATCH match REGEXP regexp
  43. {IS NOT} isnt
  44. } {
  45. set ::opname($op) $opn
  46. }
  47. set oplist [list]
  48. foreach {prec opl} {
  49. 1 ||
  50. 2 {* / %}
  51. 3 {+ -}
  52. 4 {<< >> & |}
  53. 5 {< <= > >=}
  54. 6 {= == != <> IS {IS NOT} LIKE GLOB MATCH REGEXP}
  55. 7 AND
  56. 8 OR
  57. } {
  58. foreach op $opl {
  59. set ::opprec($op) $prec
  60. lappend oplist $op
  61. }
  62. }
  63. # Hook in definitions of MATCH and REGEX. The following implementations
  64. # cause MATCH and REGEX to behave similarly to the == operator.
  65. #
  66. proc matchfunc {a b} { return [expr {$a==$b}] }
  67. proc regexfunc {a b} { return [expr {$a==$b}] }
  68. db func match -argcount 2 matchfunc
  69. db func regexp -argcount 2 regexfunc
  70. #-------------------------------------------------------------------------
  71. # Test cases e_expr-1.* attempt to verify that all binary operators listed
  72. # in the documentation exist and that the relative precedences of the
  73. # operators are also as the documentation suggests.
  74. #
  75. # EVIDENCE-OF: R-15514-65163 SQLite understands the following binary
  76. # operators, in order from highest to lowest precedence: || * / % + -
  77. # << >> & | < <= > >= = == != <> IS IS
  78. # NOT IN LIKE GLOB MATCH REGEXP AND OR
  79. #
  80. # EVIDENCE-OF: R-38759-38789 Operators IS and IS NOT have the same
  81. # precedence as =.
  82. #
  83. unset -nocomplain untested
  84. foreach op1 $oplist {
  85. foreach op2 $oplist {
  86. set untested($op1,$op2) 1
  87. foreach {tn A B C} {
  88. 1 22 45 66
  89. 2 0 0 0
  90. 3 0 0 1
  91. 4 0 1 0
  92. 5 0 1 1
  93. 6 1 0 0
  94. 7 1 0 1
  95. 8 1 1 0
  96. 9 1 1 1
  97. 10 5 6 1
  98. 11 1 5 6
  99. 12 1 5 5
  100. 13 5 5 1
  101. 14 5 2 1
  102. 15 1 4 1
  103. 16 -1 0 1
  104. 17 0 1 -1
  105. } {
  106. set testname "e_expr-1.$opname($op1).$opname($op2).$tn"
  107. # If $op2 groups more tightly than $op1, then the result
  108. # of executing $sql1 whould be the same as executing $sql3.
  109. # If $op1 groups more tightly, or if $op1 and $op2 have
  110. # the same precedence, then executing $sql1 should return
  111. # the same value as $sql2.
  112. #
  113. set sql1 "SELECT $A $op1 $B $op2 $C"
  114. set sql2 "SELECT ($A $op1 $B) $op2 $C"
  115. set sql3 "SELECT $A $op1 ($B $op2 $C)"
  116. set a2 [db one $sql2]
  117. set a3 [db one $sql3]
  118. do_execsql_test $testname $sql1 [list [
  119. if {$opprec($op2) < $opprec($op1)} {set a3} {set a2}
  120. ]]
  121. if {$a2 != $a3} { unset -nocomplain untested($op1,$op2) }
  122. }
  123. }
  124. }
  125. foreach op {* AND OR + || & |} { unset untested($op,$op) }
  126. unset untested(+,-) ;# Since (a+b)-c == a+(b-c)
  127. unset untested(*,<<) ;# Since (a*b)<<c == a*(b<<c)
  128. do_test e_expr-1.1 { array names untested } {}
  129. # At one point, test 1.2.2 was failing. Instead of the correct result, it
  130. # was returning {1 1 0}. This would seem to indicate that LIKE has the
  131. # same precedence as '<'. Which is incorrect. It has lower precedence.
  132. #
  133. do_execsql_test e_expr-1.2.1 {
  134. SELECT 0 < 2 LIKE 1, (0 < 2) LIKE 1, 0 < (2 LIKE 1)
  135. } {1 1 0}
  136. do_execsql_test e_expr-1.2.2 {
  137. SELECT 0 LIKE 0 < 2, (0 LIKE 0) < 2, 0 LIKE (0 < 2)
  138. } {0 1 0}
  139. # Showing that LIKE and == have the same precedence
  140. #
  141. do_execsql_test e_expr-1.2.3 {
  142. SELECT 2 LIKE 2 == 1, (2 LIKE 2) == 1, 2 LIKE (2 == 1)
  143. } {1 1 0}
  144. do_execsql_test e_expr-1.2.4 {
  145. SELECT 2 == 2 LIKE 1, (2 == 2) LIKE 1, 2 == (2 LIKE 1)
  146. } {1 1 0}
  147. # Showing that < groups more tightly than == (< has higher precedence).
  148. #
  149. do_execsql_test e_expr-1.2.5 {
  150. SELECT 0 < 2 == 1, (0 < 2) == 1, 0 < (2 == 1)
  151. } {1 1 0}
  152. do_execsql_test e_expr-1.6 {
  153. SELECT 0 == 0 < 2, (0 == 0) < 2, 0 == (0 < 2)
  154. } {0 1 0}
  155. #-------------------------------------------------------------------------
  156. # Check that the four unary prefix operators mentioned in the
  157. # documentation exist.
  158. #
  159. # EVIDENCE-OF: R-13958-53419 Supported unary prefix operators are these:
  160. # - + ~ NOT
  161. #
  162. do_execsql_test e_expr-2.1 { SELECT - 10 } {-10}
  163. do_execsql_test e_expr-2.2 { SELECT + 10 } {10}
  164. do_execsql_test e_expr-2.3 { SELECT ~ 10 } {-11}
  165. do_execsql_test e_expr-2.4 { SELECT NOT 10 } {0}
  166. #-------------------------------------------------------------------------
  167. # Tests for the two statements made regarding the unary + operator.
  168. #
  169. # EVIDENCE-OF: R-53670-03373 The unary operator + is a no-op.
  170. #
  171. # EVIDENCE-OF: R-19480-30968 It can be applied to strings, numbers,
  172. # blobs or NULL and it always returns a result with the same value as
  173. # the operand.
  174. #
  175. foreach {tn literal type} {
  176. 1 'helloworld' text
  177. 2 45 integer
  178. 3 45.2 real
  179. 4 45.0 real
  180. 5 X'ABCDEF' blob
  181. 6 NULL null
  182. } {
  183. set sql " SELECT quote( + $literal ), typeof( + $literal) "
  184. do_execsql_test e_expr-3.$tn $sql [list $literal $type]
  185. }
  186. #-------------------------------------------------------------------------
  187. # Check that both = and == are both acceptable as the "equals" operator.
  188. # Similarly, either != or <> work as the not-equals operator.
  189. #
  190. # EVIDENCE-OF: R-03679-60639 Equals can be either = or ==.
  191. #
  192. # EVIDENCE-OF: R-30082-38996 The non-equals operator can be either != or
  193. # <>.
  194. #
  195. foreach {tn literal different} {
  196. 1 'helloworld' '12345'
  197. 2 22 23
  198. 3 'xyz' X'78797A'
  199. 4 X'78797A00' 'xyz'
  200. } {
  201. do_execsql_test e_expr-4.$tn "
  202. SELECT $literal = $literal, $literal == $literal,
  203. $literal = $different, $literal == $different,
  204. $literal = NULL, $literal == NULL,
  205. $literal != $literal, $literal <> $literal,
  206. $literal != $different, $literal <> $different,
  207. $literal != NULL, $literal != NULL
  208. " {1 1 0 0 {} {} 0 0 1 1 {} {}}
  209. }
  210. #-------------------------------------------------------------------------
  211. # Test the || operator.
  212. #
  213. # EVIDENCE-OF: R-44409-62641 The || operator is "concatenate" - it joins
  214. # together the two strings of its operands.
  215. #
  216. foreach {tn a b} {
  217. 1 'helloworld' '12345'
  218. 2 22 23
  219. } {
  220. set as [db one "SELECT $a"]
  221. set bs [db one "SELECT $b"]
  222. do_execsql_test e_expr-5.$tn "SELECT $a || $b" [list "${as}${bs}"]
  223. }
  224. #-------------------------------------------------------------------------
  225. # Test the % operator.
  226. #
  227. # EVIDENCE-OF: R-08914-63790 The operator % outputs the value of its
  228. # left operand modulo its right operand.
  229. #
  230. do_execsql_test e_expr-6.1 {SELECT 72%5} {2}
  231. do_execsql_test e_expr-6.2 {SELECT 72%-5} {2}
  232. do_execsql_test e_expr-6.3 {SELECT -72%-5} {-2}
  233. do_execsql_test e_expr-6.4 {SELECT -72%5} {-2}
  234. #-------------------------------------------------------------------------
  235. # Test that the results of all binary operators are either numeric or
  236. # NULL, except for the || operator, which may evaluate to either a text
  237. # value or NULL.
  238. #
  239. # EVIDENCE-OF: R-20665-17792 The result of any binary operator is either
  240. # a numeric value or NULL, except for the || concatenation operator
  241. # which always evaluates to either NULL or a text value.
  242. #
  243. set literals {
  244. 1 'abc' 2 'hexadecimal' 3 ''
  245. 4 123 5 -123 6 0
  246. 7 123.4 8 0.0 9 -123.4
  247. 10 X'ABCDEF' 11 X'' 12 X'0000'
  248. 13 NULL
  249. }
  250. foreach op $oplist {
  251. foreach {n1 rhs} $literals {
  252. foreach {n2 lhs} $literals {
  253. set t [db one " SELECT typeof($lhs $op $rhs) "]
  254. do_test e_expr-7.$opname($op).$n1.$n2 {
  255. expr {
  256. ($op=="||" && ($t == "text" || $t == "null"))
  257. || ($op!="||" && ($t == "integer" || $t == "real" || $t == "null"))
  258. }
  259. } 1
  260. }}
  261. }
  262. #-------------------------------------------------------------------------
  263. # Test the IS and IS NOT operators.
  264. #
  265. # EVIDENCE-OF: R-24731-45773 The IS and IS NOT operators work like = and
  266. # != except when one or both of the operands are NULL.
  267. #
  268. # EVIDENCE-OF: R-06325-15315 In this case, if both operands are NULL,
  269. # then the IS operator evaluates to 1 (true) and the IS NOT operator
  270. # evaluates to 0 (false).
  271. #
  272. # EVIDENCE-OF: R-19812-36779 If one operand is NULL and the other is
  273. # not, then the IS operator evaluates to 0 (false) and the IS NOT
  274. # operator is 1 (true).
  275. #
  276. # EVIDENCE-OF: R-61975-13410 It is not possible for an IS or IS NOT
  277. # expression to evaluate to NULL.
  278. #
  279. do_execsql_test e_expr-8.1.1 { SELECT NULL IS NULL } {1}
  280. do_execsql_test e_expr-8.1.2 { SELECT 'ab' IS NULL } {0}
  281. do_execsql_test e_expr-8.1.3 { SELECT NULL IS 'ab' } {0}
  282. do_execsql_test e_expr-8.1.4 { SELECT 'ab' IS 'ab' } {1}
  283. do_execsql_test e_expr-8.1.5 { SELECT NULL == NULL } {{}}
  284. do_execsql_test e_expr-8.1.6 { SELECT 'ab' == NULL } {{}}
  285. do_execsql_test e_expr-8.1.7 { SELECT NULL == 'ab' } {{}}
  286. do_execsql_test e_expr-8.1.8 { SELECT 'ab' == 'ab' } {1}
  287. do_execsql_test e_expr-8.1.9 { SELECT NULL IS NOT NULL } {0}
  288. do_execsql_test e_expr-8.1.10 { SELECT 'ab' IS NOT NULL } {1}
  289. do_execsql_test e_expr-8.1.11 { SELECT NULL IS NOT 'ab' } {1}
  290. do_execsql_test e_expr-8.1.12 { SELECT 'ab' IS NOT 'ab' } {0}
  291. do_execsql_test e_expr-8.1.13 { SELECT NULL != NULL } {{}}
  292. do_execsql_test e_expr-8.1.14 { SELECT 'ab' != NULL } {{}}
  293. do_execsql_test e_expr-8.1.15 { SELECT NULL != 'ab' } {{}}
  294. do_execsql_test e_expr-8.1.16 { SELECT 'ab' != 'ab' } {0}
  295. foreach {n1 rhs} $literals {
  296. foreach {n2 lhs} $literals {
  297. if {$rhs!="NULL" && $lhs!="NULL"} {
  298. set eq [execsql "SELECT $lhs = $rhs, $lhs != $rhs"]
  299. } else {
  300. set eq [list [expr {$lhs=="NULL" && $rhs=="NULL"}] \
  301. [expr {$lhs!="NULL" || $rhs!="NULL"}]
  302. ]
  303. }
  304. set test e_expr-8.2.$n1.$n2
  305. do_execsql_test $test.1 "SELECT $lhs IS $rhs, $lhs IS NOT $rhs" $eq
  306. do_execsql_test $test.2 "
  307. SELECT ($lhs IS $rhs) IS NULL, ($lhs IS NOT $rhs) IS NULL
  308. " {0 0}
  309. }
  310. }
  311. #-------------------------------------------------------------------------
  312. # Run some tests on the COLLATE "unary postfix operator".
  313. #
  314. # This collation sequence reverses both arguments before using
  315. # [string compare] to compare them. For example, when comparing the
  316. # strings 'one' and 'four', return the result of:
  317. #
  318. # string compare eno ruof
  319. #
  320. proc reverse_str {zStr} {
  321. set out ""
  322. foreach c [split $zStr {}] { set out "${c}${out}" }
  323. set out
  324. }
  325. proc reverse_collate {zLeft zRight} {
  326. string compare [reverse_str $zLeft] [reverse_str $zRight]
  327. }
  328. db collate reverse reverse_collate
  329. # EVIDENCE-OF: R-59577-33471 The COLLATE operator is a unary postfix
  330. # operator that assigns a collating sequence to an expression.
  331. #
  332. # EVIDENCE-OF: R-23441-22541 The COLLATE operator has a higher
  333. # precedence (binds more tightly) than any prefix unary operator or any
  334. # binary operator.
  335. #
  336. do_execsql_test e_expr-9.1 { SELECT 'abcd' < 'bbbb' COLLATE reverse } 0
  337. do_execsql_test e_expr-9.2 { SELECT ('abcd' < 'bbbb') COLLATE reverse } 1
  338. do_execsql_test e_expr-9.3 { SELECT 'abcd' <= 'bbbb' COLLATE reverse } 0
  339. do_execsql_test e_expr-9.4 { SELECT ('abcd' <= 'bbbb') COLLATE reverse } 1
  340. do_execsql_test e_expr-9.5 { SELECT 'abcd' > 'bbbb' COLLATE reverse } 1
  341. do_execsql_test e_expr-9.6 { SELECT ('abcd' > 'bbbb') COLLATE reverse } 0
  342. do_execsql_test e_expr-9.7 { SELECT 'abcd' >= 'bbbb' COLLATE reverse } 1
  343. do_execsql_test e_expr-9.8 { SELECT ('abcd' >= 'bbbb') COLLATE reverse } 0
  344. do_execsql_test e_expr-9.10 { SELECT 'abcd' = 'ABCD' COLLATE nocase } 1
  345. do_execsql_test e_expr-9.11 { SELECT ('abcd' = 'ABCD') COLLATE nocase } 0
  346. do_execsql_test e_expr-9.12 { SELECT 'abcd' == 'ABCD' COLLATE nocase } 1
  347. do_execsql_test e_expr-9.13 { SELECT ('abcd' == 'ABCD') COLLATE nocase } 0
  348. do_execsql_test e_expr-9.14 { SELECT 'abcd' IS 'ABCD' COLLATE nocase } 1
  349. do_execsql_test e_expr-9.15 { SELECT ('abcd' IS 'ABCD') COLLATE nocase } 0
  350. do_execsql_test e_expr-9.16 { SELECT 'abcd' != 'ABCD' COLLATE nocase } 0
  351. do_execsql_test e_expr-9.17 { SELECT ('abcd' != 'ABCD') COLLATE nocase } 1
  352. do_execsql_test e_expr-9.18 { SELECT 'abcd' <> 'ABCD' COLLATE nocase } 0
  353. do_execsql_test e_expr-9.19 { SELECT ('abcd' <> 'ABCD') COLLATE nocase } 1
  354. do_execsql_test e_expr-9.20 { SELECT 'abcd' IS NOT 'ABCD' COLLATE nocase } 0
  355. do_execsql_test e_expr-9.21 { SELECT ('abcd' IS NOT 'ABCD') COLLATE nocase } 1
  356. do_execsql_test e_expr-9.22 {
  357. SELECT 'bbb' BETWEEN 'AAA' AND 'CCC' COLLATE nocase
  358. } 1
  359. do_execsql_test e_expr-9.23 {
  360. SELECT ('bbb' BETWEEN 'AAA' AND 'CCC') COLLATE nocase
  361. } 0
  362. # EVIDENCE-OF: R-58731-25439 The collating sequence set by the COLLATE
  363. # operator overrides the collating sequence determined by the COLLATE
  364. # clause in a table column definition.
  365. #
  366. do_execsql_test e_expr-9.24 {
  367. CREATE TABLE t24(a COLLATE NOCASE, b);
  368. INSERT INTO t24 VALUES('aaa', 1);
  369. INSERT INTO t24 VALUES('bbb', 2);
  370. INSERT INTO t24 VALUES('ccc', 3);
  371. } {}
  372. do_execsql_test e_expr-9.25 { SELECT 'BBB' = a FROM t24 } {0 1 0}
  373. do_execsql_test e_expr-9.25 { SELECT a = 'BBB' FROM t24 } {0 1 0}
  374. do_execsql_test e_expr-9.25 { SELECT 'BBB' = a COLLATE binary FROM t24 } {0 0 0}
  375. do_execsql_test e_expr-9.25 { SELECT a COLLATE binary = 'BBB' FROM t24 } {0 0 0}
  376. #-------------------------------------------------------------------------
  377. # Test statements related to literal values.
  378. #
  379. # EVIDENCE-OF: R-31536-32008 Literal values may be integers, floating
  380. # point numbers, strings, BLOBs, or NULLs.
  381. #
  382. do_execsql_test e_expr-10.1.1 { SELECT typeof(5) } {integer}
  383. do_execsql_test e_expr-10.1.2 { SELECT typeof(5.1) } {real}
  384. do_execsql_test e_expr-10.1.3 { SELECT typeof('5.1') } {text}
  385. do_execsql_test e_expr-10.1.4 { SELECT typeof(X'ABCD') } {blob}
  386. do_execsql_test e_expr-10.1.5 { SELECT typeof(NULL) } {null}
  387. # "Scientific notation is supported for point literal values."
  388. #
  389. do_execsql_test e_expr-10.2.1 { SELECT typeof(3.4e-02) } {real}
  390. do_execsql_test e_expr-10.2.2 { SELECT typeof(3e+5) } {real}
  391. do_execsql_test e_expr-10.2.3 { SELECT 3.4e-02 } {0.034}
  392. do_execsql_test e_expr-10.2.4 { SELECT 3e+4 } {30000.0}
  393. # EVIDENCE-OF: R-35229-17830 A string constant is formed by enclosing
  394. # the string in single quotes (').
  395. #
  396. # EVIDENCE-OF: R-07100-06606 A single quote within the string can be
  397. # encoded by putting two single quotes in a row - as in Pascal.
  398. #
  399. do_execsql_test e_expr-10.3.1 { SELECT 'is not' } {{is not}}
  400. do_execsql_test e_expr-10.3.2 { SELECT typeof('is not') } {text}
  401. do_execsql_test e_expr-10.3.3 { SELECT 'isn''t' } {isn't}
  402. do_execsql_test e_expr-10.3.4 { SELECT typeof('isn''t') } {text}
  403. # EVIDENCE-OF: R-09593-03321 BLOB literals are string literals
  404. # containing hexadecimal data and preceded by a single "x" or "X"
  405. # character.
  406. #
  407. # EVIDENCE-OF: R-39344-59787 For example: X'53514C697465'
  408. #
  409. do_execsql_test e_expr-10.4.1 { SELECT typeof(X'0123456789ABCDEF') } blob
  410. do_execsql_test e_expr-10.4.2 { SELECT typeof(x'0123456789ABCDEF') } blob
  411. do_execsql_test e_expr-10.4.3 { SELECT typeof(X'0123456789abcdef') } blob
  412. do_execsql_test e_expr-10.4.4 { SELECT typeof(x'0123456789abcdef') } blob
  413. do_execsql_test e_expr-10.4.5 { SELECT typeof(X'53514C697465') } blob
  414. # EVIDENCE-OF: R-23914-51476 A literal value can also be the token
  415. # "NULL".
  416. #
  417. do_execsql_test e_expr-10.5.1 { SELECT NULL } {{}}
  418. do_execsql_test e_expr-10.5.2 { SELECT typeof(NULL) } {null}
  419. #-------------------------------------------------------------------------
  420. # Test statements related to bound parameters
  421. #
  422. proc parameter_test {tn sql params result} {
  423. set stmt [sqlite3_prepare_v2 db $sql -1]
  424. foreach {number name} $params {
  425. set nm [sqlite3_bind_parameter_name $stmt $number]
  426. do_test $tn.name.$number [list set {} $nm] $name
  427. sqlite3_bind_int $stmt $number [expr -1 * $number]
  428. }
  429. sqlite3_step $stmt
  430. set res [list]
  431. for {set i 0} {$i < [sqlite3_column_count $stmt]} {incr i} {
  432. lappend res [sqlite3_column_text $stmt $i]
  433. }
  434. set rc [sqlite3_finalize $stmt]
  435. do_test $tn.rc [list set {} $rc] SQLITE_OK
  436. do_test $tn.res [list set {} $res] $result
  437. }
  438. # EVIDENCE-OF: R-33509-39458 A question mark followed by a number NNN
  439. # holds a spot for the NNN-th parameter. NNN must be between 1 and
  440. # SQLITE_MAX_VARIABLE_NUMBER.
  441. #
  442. set mvn $SQLITE_MAX_VARIABLE_NUMBER
  443. parameter_test e_expr-11.1 "
  444. SELECT ?1, ?123, ?$SQLITE_MAX_VARIABLE_NUMBER, ?123, ?4
  445. " "1 ?1 123 ?123 $mvn ?$mvn 4 ?4" "-1 -123 -$mvn -123 -4"
  446. set errmsg "variable number must be between ?1 and ?$SQLITE_MAX_VARIABLE_NUMBER"
  447. foreach {tn param_number} [list \
  448. 2 0 \
  449. 3 [expr $SQLITE_MAX_VARIABLE_NUMBER+1] \
  450. 4 [expr $SQLITE_MAX_VARIABLE_NUMBER+2] \
  451. 5 12345678903456789034567890234567890 \
  452. 6 2147483648 \
  453. 7 2147483649 \
  454. 8 4294967296 \
  455. 9 4294967297 \
  456. 10 9223372036854775808 \
  457. 11 9223372036854775809 \
  458. 12 18446744073709551616 \
  459. 13 18446744073709551617 \
  460. ] {
  461. do_catchsql_test e_expr-11.1.$tn "SELECT ?$param_number" [list 1 $errmsg]
  462. }
  463. # EVIDENCE-OF: R-33670-36097 A question mark that is not followed by a
  464. # number creates a parameter with a number one greater than the largest
  465. # parameter number already assigned.
  466. #
  467. # EVIDENCE-OF: R-42938-07030 If this means the parameter number is
  468. # greater than SQLITE_MAX_VARIABLE_NUMBER, it is an error.
  469. #
  470. parameter_test e_expr-11.2.1 "SELECT ?" {1 {}} -1
  471. parameter_test e_expr-11.2.2 "SELECT ?, ?" {1 {} 2 {}} {-1 -2}
  472. parameter_test e_expr-11.2.3 "SELECT ?5, ?" {5 ?5 6 {}} {-5 -6}
  473. parameter_test e_expr-11.2.4 "SELECT ?, ?5" {1 {} 5 ?5} {-1 -5}
  474. parameter_test e_expr-11.2.5 "SELECT ?, ?456, ?" {
  475. 1 {} 456 ?456 457 {}
  476. } {-1 -456 -457}
  477. parameter_test e_expr-11.2.5 "SELECT ?, ?456, ?4, ?" {
  478. 1 {} 456 ?456 4 ?4 457 {}
  479. } {-1 -456 -4 -457}
  480. foreach {tn sql} [list \
  481. 1 "SELECT ?$mvn, ?" \
  482. 2 "SELECT ?[expr $mvn-5], ?, ?, ?, ?, ?, ?" \
  483. 3 "SELECT ?[expr $mvn], ?5, ?6, ?" \
  484. ] {
  485. do_catchsql_test e_expr-11.3.$tn $sql [list 1 {too many SQL variables}]
  486. }
  487. # EVIDENCE-OF: R-11620-22743 A colon followed by an identifier name
  488. # holds a spot for a named parameter with the name :AAAA.
  489. #
  490. # Identifiers in SQLite consist of alphanumeric, '_' and '$' characters,
  491. # and any UTF characters with codepoints larger than 127 (non-ASCII
  492. # characters).
  493. #
  494. parameter_test e_expr-11.2.1 {SELECT :AAAA} {1 :AAAA} -1
  495. parameter_test e_expr-11.2.2 {SELECT :123} {1 :123} -1
  496. parameter_test e_expr-11.2.3 {SELECT :__} {1 :__} -1
  497. parameter_test e_expr-11.2.4 {SELECT :_$_} {1 :_$_} -1
  498. #Skip Unicode Test
  499. if 0 {
  500. parameter_test e_expr-11.2.5 "
  501. SELECT :\u0e40\u0e2d\u0e28\u0e02\u0e39\u0e40\u0e2d\u0e25
  502. " "1 :\u0e40\u0e2d\u0e28\u0e02\u0e39\u0e40\u0e2d\u0e25" -1
  503. }
  504. parameter_test e_expr-11.2.6 "SELECT :\u0080" "1 :\u0080" -1
  505. # EVIDENCE-OF: R-49783-61279 An "at" sign works exactly like a colon,
  506. # except that the name of the parameter created is @AAAA.
  507. #
  508. parameter_test e_expr-11.3.1 {SELECT @AAAA} {1 @AAAA} -1
  509. parameter_test e_expr-11.3.2 {SELECT @123} {1 @123} -1
  510. parameter_test e_expr-11.3.3 {SELECT @__} {1 @__} -1
  511. parameter_test e_expr-11.3.4 {SELECT @_$_} {1 @_$_} -1
  512. #Skip Unicode Test
  513. if 0 {
  514. parameter_test e_expr-11.3.5 "
  515. SELECT @\u0e40\u0e2d\u0e28\u0e02\u0e39\u0e40\u0e2d\u0e25
  516. " "1 @\u0e40\u0e2d\u0e28\u0e02\u0e39\u0e40\u0e2d\u0e25" -1
  517. }
  518. parameter_test e_expr-11.3.6 "SELECT @\u0080" "1 @\u0080" -1
  519. # EVIDENCE-OF: R-62610-51329 A dollar-sign followed by an identifier
  520. # name also holds a spot for a named parameter with the name $AAAA.
  521. #
  522. # EVIDENCE-OF: R-55025-21042 The identifier name in this case can
  523. # include one or more occurrences of "::" and a suffix enclosed in
  524. # "(...)" containing any text at all.
  525. #
  526. # Note: Looks like an identifier cannot consist entirely of "::"
  527. # characters or just a suffix. Also, the other named variable characters
  528. # (: and @) work the same way internally. Why not just document it that way?
  529. #
  530. parameter_test e_expr-11.4.1 {SELECT $AAAA} {1 $AAAA} -1
  531. parameter_test e_expr-11.4.2 {SELECT $123} {1 $123} -1
  532. parameter_test e_expr-11.4.3 {SELECT $__} {1 $__} -1
  533. parameter_test e_expr-11.4.4 {SELECT $_$_} {1 $_$_} -1
  534. #Skip Unicode Test
  535. if 0 {
  536. parameter_test e_expr-11.4.5 "
  537. SELECT \$\u0e40\u0e2d\u0e28\u0e02\u0e39\u0e40\u0e2d\u0e25
  538. " "1 \$\u0e40\u0e2d\u0e28\u0e02\u0e39\u0e40\u0e2d\u0e25" -1
  539. }
  540. parameter_test e_expr-11.4.6 "SELECT \$\u0080" "1 \$\u0080" -1
  541. parameter_test e_expr-11.5.1 {SELECT $::::a(++--++)} {1 $::::a(++--++)} -1
  542. parameter_test e_expr-11.5.2 {SELECT $::a()} {1 $::a()} -1
  543. parameter_test e_expr-11.5.3 {SELECT $::1(::#$)} {1 $::1(::#$)} -1
  544. # EVIDENCE-OF: R-11370-04520 Named parameters are also numbered. The
  545. # number assigned is one greater than the largest parameter number
  546. # already assigned.
  547. #
  548. # EVIDENCE-OF: R-42620-22184 If this means the parameter would be
  549. # assigned a number greater than SQLITE_MAX_VARIABLE_NUMBER, it is an
  550. # error.
  551. #
  552. parameter_test e_expr-11.6.1 "SELECT ?, @abc" {1 {} 2 @abc} {-1 -2}
  553. parameter_test e_expr-11.6.2 "SELECT ?123, :a1" {123 ?123 124 :a1} {-123 -124}
  554. parameter_test e_expr-11.6.3 {SELECT $a, ?8, ?, $b, ?2, $c} {
  555. 1 $a 8 ?8 9 {} 10 $b 2 ?2 11 $c
  556. } {-1 -8 -9 -10 -2 -11}
  557. foreach {tn sql} [list \
  558. 1 "SELECT ?$mvn, \$::a" \
  559. 2 "SELECT ?$mvn, ?4, @a1" \
  560. 3 "SELECT ?[expr $mvn-2], :bag, @123, \$x" \
  561. ] {
  562. do_catchsql_test e_expr-11.7.$tn $sql [list 1 {too many SQL variables}]
  563. }
  564. # EVIDENCE-OF: R-14068-49671 Parameters that are not assigned values
  565. # using sqlite3_bind() are treated as NULL.
  566. #
  567. do_test e_expr-11.7.1 {
  568. set stmt [sqlite3_prepare_v2 db { SELECT ?, :a, @b, $d } -1]
  569. sqlite3_step $stmt
  570. list [sqlite3_column_type $stmt 0] \
  571. [sqlite3_column_type $stmt 1] \
  572. [sqlite3_column_type $stmt 2] \
  573. [sqlite3_column_type $stmt 3]
  574. } {NULL NULL NULL NULL}
  575. do_test e_expr-11.7.1 { sqlite3_finalize $stmt } SQLITE_OK
  576. #-------------------------------------------------------------------------
  577. # "Test" the syntax diagrams in lang_expr.html.
  578. #
  579. # EVIDENCE-OF: R-62067-43884 -- syntax diagram signed-number
  580. #
  581. do_execsql_test e_expr-12.1.1 { SELECT 0, +0, -0 } {0 0 0}
  582. do_execsql_test e_expr-12.1.2 { SELECT 1, +1, -1 } {1 1 -1}
  583. do_execsql_test e_expr-12.1.3 { SELECT 2, +2, -2 } {2 2 -2}
  584. do_execsql_test e_expr-12.1.4 {
  585. SELECT 1.4, +1.4, -1.4
  586. } {1.4 1.4 -1.4}
  587. do_execsql_test e_expr-12.1.5 {
  588. SELECT 1.5e+5, +1.5e+5, -1.5e+5
  589. } {150000.0 150000.0 -150000.0}
  590. do_execsql_test e_expr-12.1.6 {
  591. SELECT 0.0001, +0.0001, -0.0001
  592. } {0.0001 0.0001 -0.0001}
  593. # EVIDENCE-OF: R-21258-25489 -- syntax diagram literal-value
  594. #
  595. set sqlite_current_time 1
  596. do_execsql_test e_expr-12.2.1 {SELECT 123} {123}
  597. do_execsql_test e_expr-12.2.2 {SELECT 123.4e05} {12340000.0}
  598. do_execsql_test e_expr-12.2.3 {SELECT 'abcde'} {abcde}
  599. do_execsql_test e_expr-12.2.4 {SELECT X'414243'} {ABC}
  600. do_execsql_test e_expr-12.2.5 {SELECT NULL} {{}}
  601. do_execsql_test e_expr-12.2.6 {SELECT CURRENT_TIME} {00:00:01}
  602. do_execsql_test e_expr-12.2.7 {SELECT CURRENT_DATE} {1970-01-01}
  603. do_execsql_test e_expr-12.2.8 {SELECT CURRENT_TIMESTAMP} {{1970-01-01 00:00:01}}
  604. set sqlite_current_time 0
  605. # EVIDENCE-OF: R-57598-59332 -- syntax diagram expr
  606. #
  607. file delete -force test.db2
  608. execsql {
  609. ATTACH 'test.db2' AS dbname;
  610. CREATE TABLE dbname.tblname(cname);
  611. }
  612. proc glob {args} {return 1}
  613. db function glob glob
  614. db function match glob
  615. db function regexp glob
  616. foreach {tn expr} {
  617. 1 123
  618. 2 123.4e05
  619. 3 'abcde'
  620. 4 X'414243'
  621. 5 NULL
  622. 6 CURRENT_TIME
  623. 7 CURRENT_DATE
  624. 8 CURRENT_TIMESTAMP
  625. 9 ?
  626. 10 ?123
  627. 11 @hello
  628. 12 :world
  629. 13 $tcl
  630. 14 $tcl(array)
  631. 15 cname
  632. 16 tblname.cname
  633. 17 dbname.tblname.cname
  634. 18 "+ EXPR"
  635. 19 "- EXPR"
  636. 20 "NOT EXPR"
  637. 21 "~ EXPR"
  638. 22 "EXPR1 || EXPR2"
  639. 23 "EXPR1 * EXPR2"
  640. 24 "EXPR1 / EXPR2"
  641. 25 "EXPR1 % EXPR2"
  642. 26 "EXPR1 + EXPR2"
  643. 27 "EXPR1 - EXPR2"
  644. 28 "EXPR1 << EXPR2"
  645. 29 "EXPR1 >> EXPR2"
  646. 30 "EXPR1 & EXPR2"
  647. 31 "EXPR1 | EXPR2"
  648. 32 "EXPR1 < EXPR2"
  649. 33 "EXPR1 <= EXPR2"
  650. 34 "EXPR1 > EXPR2"
  651. 35 "EXPR1 >= EXPR2"
  652. 36 "EXPR1 = EXPR2"
  653. 37 "EXPR1 == EXPR2"
  654. 38 "EXPR1 != EXPR2"
  655. 39 "EXPR1 <> EXPR2"
  656. 40 "EXPR1 IS EXPR2"
  657. 41 "EXPR1 IS NOT EXPR2"
  658. 42 "EXPR1 AND EXPR2"
  659. 43 "EXPR1 OR EXPR2"
  660. 44 "count(*)"
  661. 45 "count(DISTINCT EXPR)"
  662. 46 "substr(EXPR, 10, 20)"
  663. 47 "changes()"
  664. 48 "( EXPR )"
  665. 49 "CAST ( EXPR AS integer )"
  666. 50 "CAST ( EXPR AS 'abcd' )"
  667. 51 "CAST ( EXPR AS 'ab$ $cd' )"
  668. 52 "EXPR COLLATE nocase"
  669. 53 "EXPR COLLATE binary"
  670. 54 "EXPR1 LIKE EXPR2"
  671. 55 "EXPR1 LIKE EXPR2 ESCAPE EXPR"
  672. 56 "EXPR1 GLOB EXPR2"
  673. 57 "EXPR1 GLOB EXPR2 ESCAPE EXPR"
  674. 58 "EXPR1 REGEXP EXPR2"
  675. 59 "EXPR1 REGEXP EXPR2 ESCAPE EXPR"
  676. 60 "EXPR1 MATCH EXPR2"
  677. 61 "EXPR1 MATCH EXPR2 ESCAPE EXPR"
  678. 62 "EXPR1 NOT LIKE EXPR2"
  679. 63 "EXPR1 NOT LIKE EXPR2 ESCAPE EXPR"
  680. 64 "EXPR1 NOT GLOB EXPR2"
  681. 65 "EXPR1 NOT GLOB EXPR2 ESCAPE EXPR"
  682. 66 "EXPR1 NOT REGEXP EXPR2"
  683. 67 "EXPR1 NOT REGEXP EXPR2 ESCAPE EXPR"
  684. 68 "EXPR1 NOT MATCH EXPR2"
  685. 69 "EXPR1 NOT MATCH EXPR2 ESCAPE EXPR"
  686. 70 "EXPR ISNULL"
  687. 71 "EXPR NOTNULL"
  688. 72 "EXPR NOT NULL"
  689. 73 "EXPR1 IS EXPR2"
  690. 74 "EXPR1 IS NOT EXPR2"
  691. 75 "EXPR NOT BETWEEN EXPR1 AND EXPR2"
  692. 76 "EXPR BETWEEN EXPR1 AND EXPR2"
  693. 77 "EXPR NOT IN (SELECT cname FROM tblname)"
  694. 78 "EXPR NOT IN (1)"
  695. 79 "EXPR NOT IN (1, 2, 3)"
  696. 80 "EXPR NOT IN tblname"
  697. 81 "EXPR NOT IN dbname.tblname"
  698. 82 "EXPR IN (SELECT cname FROM tblname)"
  699. 83 "EXPR IN (1)"
  700. 84 "EXPR IN (1, 2, 3)"
  701. 85 "EXPR IN tblname"
  702. 86 "EXPR IN dbname.tblname"
  703. 87 "EXISTS (SELECT cname FROM tblname)"
  704. 88 "NOT EXISTS (SELECT cname FROM tblname)"
  705. 89 "CASE EXPR WHEN EXPR1 THEN EXPR2 ELSE EXPR END"
  706. 90 "CASE EXPR WHEN EXPR1 THEN EXPR2 END"
  707. 91 "CASE EXPR WHEN EXPR1 THEN EXPR2 WHEN EXPR THEN EXPR1 ELSE EXPR2 END"
  708. 92 "CASE EXPR WHEN EXPR1 THEN EXPR2 WHEN EXPR THEN EXPR1 END"
  709. 93 "CASE WHEN EXPR1 THEN EXPR2 ELSE EXPR END"
  710. 94 "CASE WHEN EXPR1 THEN EXPR2 END"
  711. 95 "CASE WHEN EXPR1 THEN EXPR2 WHEN EXPR THEN EXPR1 ELSE EXPR2 END"
  712. 96 "CASE WHEN EXPR1 THEN EXPR2 WHEN EXPR THEN EXPR1 END"
  713. } {
  714. # If the expression string being parsed contains "EXPR2", then replace
  715. # string "EXPR1" and "EXPR2" with arbitrary SQL expressions. If it
  716. # contains "EXPR", then replace EXPR with an arbitrary SQL expression.
  717. #
  718. set elist [list $expr]
  719. if {[string match *EXPR2* $expr]} {
  720. set elist [list]
  721. foreach {e1 e2} { cname "34+22" } {
  722. lappend elist [string map [list EXPR1 $e1 EXPR2 $e2] $expr]
  723. }
  724. }
  725. if {[string match *EXPR* $expr]} {
  726. set elist2 [list]
  727. foreach el $elist {
  728. foreach e { cname "34+22" } {
  729. lappend elist2 [string map [list EXPR $e] $el]
  730. }
  731. }
  732. set elist $elist2
  733. }
  734. set x 0
  735. foreach e $elist {
  736. incr x
  737. do_test e_expr-12.3.$tn.$x {
  738. set rc [catch { execsql "SELECT $e FROM tblname" } msg]
  739. } {0}
  740. }
  741. }
  742. # EVIDENCE-OF: R-49462-56079 -- syntax diagram raise-function
  743. #
  744. foreach {tn raiseexpr} {
  745. 1 "RAISE(IGNORE)"
  746. 2 "RAISE(ROLLBACK, 'error message')"
  747. 3 "RAISE(ABORT, 'error message')"
  748. 4 "RAISE(FAIL, 'error message')"
  749. } {
  750. do_execsql_test e_expr-12.4.$tn "
  751. CREATE TRIGGER dbname.tr$tn BEFORE DELETE ON tblname BEGIN
  752. SELECT $raiseexpr ;
  753. END;
  754. " {}
  755. }
  756. #-------------------------------------------------------------------------
  757. # Test the statements related to the BETWEEN operator.
  758. #
  759. # EVIDENCE-OF: R-40079-54503 The BETWEEN operator is logically
  760. # equivalent to a pair of comparisons. "x BETWEEN y AND z" is equivalent
  761. # to "x>=y AND x<=z" except that with BETWEEN, the x expression is
  762. # only evaluated once.
  763. #
  764. db func x x
  765. proc x {} { incr ::xcount ; return [expr $::x] }
  766. foreach {tn x expr res nEval} {
  767. 1 10 "x() >= 5 AND x() <= 15" 1 2
  768. 2 10 "x() BETWEEN 5 AND 15" 1 1
  769. 3 5 "x() >= 5 AND x() <= 5" 1 2
  770. 4 5 "x() BETWEEN 5 AND 5" 1 1
  771. } {
  772. do_test e_expr-13.1.$tn {
  773. set ::xcount 0
  774. set a [execsql "SELECT $expr"]
  775. list $::xcount $a
  776. } [list $nEval $res]
  777. }
  778. # EVIDENCE-OF: R-05155-34454 The precedence of the BETWEEN operator is
  779. # the same as the precedence as operators == and != and LIKE and groups
  780. # left to right.
  781. #
  782. # Therefore, BETWEEN groups more tightly than operator "AND", but less
  783. # so than "<".
  784. #
  785. do_execsql_test e_expr-13.2.1 { SELECT 1 == 10 BETWEEN 0 AND 2 } 1
  786. do_execsql_test e_expr-13.2.2 { SELECT (1 == 10) BETWEEN 0 AND 2 } 1
  787. do_execsql_test e_expr-13.2.3 { SELECT 1 == (10 BETWEEN 0 AND 2) } 0
  788. do_execsql_test e_expr-13.2.4 { SELECT 6 BETWEEN 4 AND 8 == 1 } 1
  789. do_execsql_test e_expr-13.2.5 { SELECT (6 BETWEEN 4 AND 8) == 1 } 1
  790. do_execsql_test e_expr-13.2.6 { SELECT 6 BETWEEN 4 AND (8 == 1) } 0
  791. do_execsql_test e_expr-13.2.7 { SELECT 5 BETWEEN 0 AND 0 != 1 } 1
  792. do_execsql_test e_expr-13.2.8 { SELECT (5 BETWEEN 0 AND 0) != 1 } 1
  793. do_execsql_test e_expr-13.2.9 { SELECT 5 BETWEEN 0 AND (0 != 1) } 0
  794. do_execsql_test e_expr-13.2.10 { SELECT 1 != 0 BETWEEN 0 AND 2 } 1
  795. do_execsql_test e_expr-13.2.11 { SELECT (1 != 0) BETWEEN 0 AND 2 } 1
  796. do_execsql_test e_expr-13.2.12 { SELECT 1 != (0 BETWEEN 0 AND 2) } 0
  797. do_execsql_test e_expr-13.2.13 { SELECT 1 LIKE 10 BETWEEN 0 AND 2 } 1
  798. do_execsql_test e_expr-13.2.14 { SELECT (1 LIKE 10) BETWEEN 0 AND 2 } 1
  799. do_execsql_test e_expr-13.2.15 { SELECT 1 LIKE (10 BETWEEN 0 AND 2) } 0
  800. do_execsql_test e_expr-13.2.16 { SELECT 6 BETWEEN 4 AND 8 LIKE 1 } 1
  801. do_execsql_test e_expr-13.2.17 { SELECT (6 BETWEEN 4 AND 8) LIKE 1 } 1
  802. do_execsql_test e_expr-13.2.18 { SELECT 6 BETWEEN 4 AND (8 LIKE 1) } 0
  803. do_execsql_test e_expr-13.2.19 { SELECT 0 AND 0 BETWEEN 0 AND 1 } 0
  804. do_execsql_test e_expr-13.2.20 { SELECT 0 AND (0 BETWEEN 0 AND 1) } 0
  805. do_execsql_test e_expr-13.2.21 { SELECT (0 AND 0) BETWEEN 0 AND 1 } 1
  806. do_execsql_test e_expr-13.2.22 { SELECT 0 BETWEEN -1 AND 1 AND 0 } 0
  807. do_execsql_test e_expr-13.2.23 { SELECT (0 BETWEEN -1 AND 1) AND 0 } 0
  808. do_execsql_test e_expr-13.2.24 { SELECT 0 BETWEEN -1 AND (1 AND 0) } 1
  809. do_execsql_test e_expr-13.2.25 { SELECT 2 < 3 BETWEEN 0 AND 1 } 1
  810. do_execsql_test e_expr-13.2.26 { SELECT (2 < 3) BETWEEN 0 AND 1 } 1
  811. do_execsql_test e_expr-13.2.27 { SELECT 2 < (3 BETWEEN 0 AND 1) } 0
  812. do_execsql_test e_expr-13.2.28 { SELECT 2 BETWEEN 1 AND 2 < 3 } 0
  813. do_execsql_test e_expr-13.2.29 { SELECT 2 BETWEEN 1 AND (2 < 3) } 0
  814. do_execsql_test e_expr-13.2.30 { SELECT (2 BETWEEN 1 AND 2) < 3 } 1
  815. #-------------------------------------------------------------------------
  816. # Test the statements related to the LIKE and GLOB operators.
  817. #
  818. # EVIDENCE-OF: R-16584-60189 The LIKE operator does a pattern matching
  819. # comparison.
  820. #
  821. # EVIDENCE-OF: R-11295-04657 The operand to the right of the LIKE
  822. # operator contains the pattern and the left hand operand contains the
  823. # string to match against the pattern.
  824. #
  825. do_execsql_test e_expr-14.1.1 { SELECT 'abc%' LIKE 'abcde' } 0
  826. do_execsql_test e_expr-14.1.2 { SELECT 'abcde' LIKE 'abc%' } 1
  827. # EVIDENCE-OF: R-55406-38524 A percent symbol ("%") in the LIKE pattern
  828. # matches any sequence of zero or more characters in the string.
  829. #
  830. do_execsql_test e_expr-14.2.1 { SELECT 'abde' LIKE 'ab%de' } 1
  831. do_execsql_test e_expr-14.2.2 { SELECT 'abXde' LIKE 'ab%de' } 1
  832. do_execsql_test e_expr-14.2.3 { SELECT 'abABCde' LIKE 'ab%de' } 1
  833. # EVIDENCE-OF: R-30433-25443 An underscore ("_") in the LIKE pattern
  834. # matches any single character in the string.
  835. #
  836. do_execsql_test e_expr-14.3.1 { SELECT 'abde' LIKE 'ab_de' } 0
  837. do_execsql_test e_expr-14.3.2 { SELECT 'abXde' LIKE 'ab_de' } 1
  838. do_execsql_test e_expr-14.3.3 { SELECT 'abABCde' LIKE 'ab_de' } 0
  839. # EVIDENCE-OF: R-59007-20454 Any other character matches itself or its
  840. # lower/upper case equivalent (i.e. case-insensitive matching).
  841. #
  842. do_execsql_test e_expr-14.4.1 { SELECT 'abc' LIKE 'aBc' } 1
  843. do_execsql_test e_expr-14.4.2 { SELECT 'aBc' LIKE 'aBc' } 1
  844. do_execsql_test e_expr-14.4.3 { SELECT 'ac' LIKE 'aBc' } 0
  845. # EVIDENCE-OF: R-23648-58527 SQLite only understands upper/lower case
  846. # for ASCII characters by default.
  847. #
  848. # EVIDENCE-OF: R-04532-11527 The LIKE operator is case sensitive by
  849. # default for unicode characters that are beyond the ASCII range.
  850. #
  851. # EVIDENCE-OF: R-44381-11669 the expression
  852. # 'a'&nbsp;LIKE&nbsp;'A' is TRUE but
  853. # '&aelig;'&nbsp;LIKE&nbsp;'&AElig;' is FALSE.
  854. #
  855. # The restriction to ASCII characters does not apply if the ICU
  856. # library is compiled in. When ICU is enabled SQLite does not act
  857. # as it does "by default".
  858. #
  859. do_execsql_test e_expr-14.5.1 { SELECT 'A' LIKE 'a' } 1
  860. ifcapable !icu {
  861. do_execsql_test e_expr-14.5.2 "SELECT '\u00c6' LIKE '\u00e6'" 0
  862. }
  863. # EVIDENCE-OF: R-56683-13731 If the optional ESCAPE clause is present,
  864. # then the expression following the ESCAPE keyword must evaluate to a
  865. # string consisting of a single character.
  866. #
  867. do_catchsql_test e_expr-14.6.1 {
  868. SELECT 'A' LIKE 'a' ESCAPE '12'
  869. } {1 {ESCAPE expression must be a single character}}
  870. do_catchsql_test e_expr-14.6.2 {
  871. SELECT 'A' LIKE 'a' ESCAPE ''
  872. } {1 {ESCAPE expression must be a single character}}
  873. do_catchsql_test e_expr-14.6.3 { SELECT 'A' LIKE 'a' ESCAPE 'x' } {0 1}
  874. do_catchsql_test e_expr-14.6.4 "SELECT 'A' LIKE 'a' ESCAPE '\u00e6'" {0 1}
  875. # EVIDENCE-OF: R-02045-23762 This character may be used in the LIKE
  876. # pattern to include literal percent or underscore characters.
  877. #
  878. # EVIDENCE-OF: R-13345-31830 The escape character followed by a percent
  879. # symbol (%), underscore (_), or a second instance of the escape
  880. # character itself matches a literal percent symbol, underscore, or a
  881. # single escape character, respectively.
  882. #
  883. do_execsql_test e_expr-14.7.1 { SELECT 'abc%' LIKE 'abcX%' ESCAPE 'X' } 1
  884. do_execsql_test e_expr-14.7.2 { SELECT 'abc5' LIKE 'abcX%' ESCAPE 'X' } 0
  885. do_execsql_test e_expr-14.7.3 { SELECT 'abc' LIKE 'abcX%' ESCAPE 'X' } 0
  886. do_execsql_test e_expr-14.7.4 { SELECT 'abcX%' LIKE 'abcX%' ESCAPE 'X' } 0
  887. do_execsql_test e_expr-14.7.5 { SELECT 'abc%%' LIKE 'abcX%' ESCAPE 'X' } 0
  888. do_execsql_test e_expr-14.7.6 { SELECT 'abc_' LIKE 'abcX_' ESCAPE 'X' } 1
  889. do_execsql_test e_expr-14.7.7 { SELECT 'abc5' LIKE 'abcX_' ESCAPE 'X' } 0
  890. do_execsql_test e_expr-14.7.8 { SELECT 'abc' LIKE 'abcX_' ESCAPE 'X' } 0
  891. do_execsql_test e_expr-14.7.9 { SELECT 'abcX_' LIKE 'abcX_' ESCAPE 'X' } 0
  892. do_execsql_test e_expr-14.7.10 { SELECT 'abc__' LIKE 'abcX_' ESCAPE 'X' } 0
  893. do_execsql_test e_expr-14.7.11 { SELECT 'abcX' LIKE 'abcXX' ESCAPE 'X' } 1
  894. do_execsql_test e_expr-14.7.12 { SELECT 'abc5' LIKE 'abcXX' ESCAPE 'X' } 0
  895. do_execsql_test e_expr-14.7.13 { SELECT 'abc' LIKE 'abcXX' ESCAPE 'X' } 0
  896. do_execsql_test e_expr-14.7.14 { SELECT 'abcXX' LIKE 'abcXX' ESCAPE 'X' } 0
  897. # EVIDENCE-OF: R-51359-17496 The infix LIKE operator is implemented by
  898. # calling the application-defined SQL functions like(Y,X) or like(Y,X,Z).
  899. #
  900. proc likefunc {args} {
  901. eval lappend ::likeargs $args
  902. return 1
  903. }
  904. db func like -argcount 2 likefunc
  905. db func like -argcount 3 likefunc
  906. set ::likeargs [list]
  907. do_execsql_test e_expr-15.1.1 { SELECT 'abc' LIKE 'def' } 1
  908. do_test e_expr-15.1.2 { set likeargs } {def abc}
  909. set ::likeargs [list]
  910. do_execsql_test e_expr-15.1.3 { SELECT 'abc' LIKE 'def' ESCAPE 'X' } 1
  911. do_test e_expr-15.1.4 { set likeargs } {def abc X}
  912. db close
  913. sqlite3 db test.db
  914. # EVIDENCE-OF: R-22868-25880 The LIKE operator can be made case
  915. # sensitive using the case_sensitive_like pragma.
  916. #
  917. do_execsql_test e_expr-16.1.1 { SELECT 'abcxyz' LIKE 'ABC%' } 1
  918. do_execsql_test e_expr-16.1.2 { PRAGMA case_sensitive_like = 1 } {}
  919. do_execsql_test e_expr-16.1.3 { SELECT 'abcxyz' LIKE 'ABC%' } 0
  920. do_execsql_test e_expr-16.1.4 { SELECT 'ABCxyz' LIKE 'ABC%' } 1
  921. do_execsql_test e_expr-16.1.5 { PRAGMA case_sensitive_like = 0 } {}
  922. do_execsql_test e_expr-16.1.6 { SELECT 'abcxyz' LIKE 'ABC%' } 1
  923. do_execsql_test e_expr-16.1.7 { SELECT 'ABCxyz' LIKE 'ABC%' } 1
  924. # EVIDENCE-OF: R-52087-12043 The GLOB operator is similar to LIKE but
  925. # uses the Unix file globbing syntax for its wildcards.
  926. #
  927. # EVIDENCE-OF: R-09813-17279 Also, GLOB is case sensitive, unlike LIKE.
  928. #
  929. do_execsql_test e_expr-17.1.1 { SELECT 'abcxyz' GLOB 'abc%' } 0
  930. do_execsql_test e_expr-17.1.2 { SELECT 'abcxyz' GLOB 'abc*' } 1
  931. do_execsql_test e_expr-17.1.3 { SELECT 'abcxyz' GLOB 'abc___' } 0
  932. do_execsql_test e_expr-17.1.4 { SELECT 'abcxyz' GLOB 'abc???' } 1
  933. do_execsql_test e_expr-17.1.5 { SELECT 'abcxyz' GLOB 'abc*' } 1
  934. do_execsql_test e_expr-17.1.6 { SELECT 'ABCxyz' GLOB 'abc*' } 0
  935. do_execsql_test e_expr-17.1.7 { SELECT 'abcxyz' GLOB 'ABC*' } 0
  936. # EVIDENCE-OF: R-39616-20555 Both GLOB and LIKE may be preceded by the
  937. # NOT keyword to invert the sense of the test.
  938. #
  939. do_execsql_test e_expr-17.2.1 { SELECT 'abcxyz' NOT GLOB 'ABC*' } 1
  940. do_execsql_test e_expr-17.2.2 { SELECT 'abcxyz' NOT GLOB 'abc*' } 0
  941. do_execsql_test e_expr-17.2.3 { SELECT 'abcxyz' NOT LIKE 'ABC%' } 0
  942. do_execsql_test e_expr-17.2.4 { SELECT 'abcxyz' NOT LIKE 'abc%' } 0
  943. do_execsql_test e_expr-17.2.5 { SELECT 'abdxyz' NOT LIKE 'abc%' } 1
  944. db nullvalue null
  945. do_execsql_test e_expr-17.2.6 { SELECT 'abcxyz' NOT GLOB NULL } null
  946. do_execsql_test e_expr-17.2.7 { SELECT 'abcxyz' NOT LIKE NULL } null
  947. do_execsql_test e_expr-17.2.8 { SELECT NULL NOT GLOB 'abc*' } null
  948. do_execsql_test e_expr-17.2.9 { SELECT NULL NOT LIKE 'ABC%' } null
  949. db nullvalue {}
  950. # EVIDENCE-OF: R-39414-35489 The infix GLOB operator is implemented by
  951. # calling the function glob(Y,X) and can be modified by overriding that
  952. # function.
  953. proc globfunc {args} {
  954. eval lappend ::globargs $args
  955. return 1
  956. }
  957. db func glob -argcount 2 globfunc
  958. set ::globargs [list]
  959. do_execsql_test e_expr-17.3.1 { SELECT 'abc' GLOB 'def' } 1
  960. do_test e_expr-17.3.2 { set globargs } {def abc}
  961. set ::globargs [list]
  962. do_execsql_test e_expr-17.3.3 { SELECT 'X' NOT GLOB 'Y' } 0
  963. do_test e_expr-17.3.4 { set globargs } {Y X}
  964. sqlite3 db test.db
  965. # EVIDENCE-OF: R-41650-20872 No regexp() user function is defined by
  966. # default and so use of the REGEXP operator will normally result in an
  967. # error message.
  968. #
  969. # There is a regexp function if ICU is enabled though.
  970. #
  971. # USE C# version of REGEXP
  972. if 0 {
  973. ifcapable !icu {
  974. do_catchsql_test e_expr-18.1.1 {
  975. SELECT regexp('abc', 'def')
  976. } {1 {no such function: regexp}}
  977. do_catchsql_test e_expr-18.1.2 {
  978. SELECT 'abc' REGEXP 'def'
  979. } {1 {no such function: REGEXP}}
  980. }
  981. } else {
  982. #C# has buildin Regex
  983. do_catchsql_test e_expr-18.1.1 {
  984. SELECT regexp('abc', 'def')
  985. } {0 0}
  986. do_catchsql_test e_expr-18.1.2 {
  987. SELECT 'abc' REGEXP 'def'
  988. } {0 1}
  989. }
  990. # EVIDENCE-OF: R-33693-50180 The REGEXP operator is a special syntax for
  991. # the regexp() user function.
  992. #
  993. # EVIDENCE-OF: R-57289-13578 If a application-defined SQL function named
  994. # "regexp" is added at run-time, that function will be called in order
  995. # to implement the REGEXP operator.
  996. #
  997. proc regexpfunc {args} {
  998. eval lappend ::regexpargs $args
  999. return 1
  1000. }
  1001. db func regexp -argcount 2 regexpfunc
  1002. set ::regexpargs [list]
  1003. do_execsql_test e_expr-18.2.1 { SELECT 'abc' REGEXP 'def' } 1
  1004. do_test e_expr-18.2.2 { set regexpargs } {def abc}
  1005. set ::regexpargs [list]
  1006. do_execsql_test e_expr-18.2.3 { SELECT 'X' NOT REGEXP 'Y' } 0
  1007. do_test e_expr-18.2.4 { set regexpargs } {Y X}
  1008. sqlite3 db test.db
  1009. # EVIDENCE-OF: R-42037-37826 The default match() function implementation
  1010. # raises an exception and is not really useful for anything.
  1011. #
  1012. do_catchsql_test e_expr-19.1.1 {
  1013. SELECT 'abc' MATCH 'def'
  1014. } {1 {unable to use function MATCH in the requested context}}
  1015. do_catchsql_test e_expr-19.1.2 {
  1016. SELECT match('abc', 'def')
  1017. } {1 {unable to use function MATCH in the requested context}}
  1018. # EVIDENCE-OF: R-37916-47407 The MATCH operator is a special syntax for
  1019. # the match() application-defined function.
  1020. #
  1021. # EVIDENCE-OF: R-06021-09373 But extensions can override the match()
  1022. # function with more helpful logic.
  1023. #
  1024. proc matchfunc {args} {
  1025. eval lappend ::matchargs $args
  1026. return 1
  1027. }
  1028. db func match -argcount 2 matchfunc
  1029. set ::matchargs [list]
  1030. do_execsql_test e_expr-19.2.1 { SELECT 'abc' MATCH 'def' } 1
  1031. do_test e_expr-19.2.2 { set matchargs } {def abc}
  1032. set ::matchargs [list]
  1033. do_execsql_test e_expr-19.2.3 { SELECT 'X' NOT MATCH 'Y' } 0
  1034. do_test e_expr-19.2.4 { set matchargs } {Y X}
  1035. sqlite3 db test.db
  1036. #-------------------------------------------------------------------------
  1037. # Test cases for the testable statements related to the CASE expression.
  1038. #
  1039. # EVIDENCE-OF: R-15199-61389 There are two basic forms of the CASE
  1040. # expression: those with a base expression and those without.
  1041. #
  1042. do_execsql_test e_expr-20.1 {
  1043. SELECT CASE WHEN 1 THEN 'true' WHEN 0 THEN 'false' ELSE 'else' END;
  1044. } {true}
  1045. do_execsql_test e_expr-20.2 {
  1046. SELECT CASE 0 WHEN 1 THEN 'true' WHEN 0 THEN 'false' ELSE 'else' END;
  1047. } {false}
  1048. proc var {nm} {
  1049. lappend ::varlist $nm
  1050. return [set "::$nm"]
  1051. }
  1052. db func var var
  1053. # EVIDENCE-OF: R-30638-59954 In a CASE without a base expression, each
  1054. # WHEN expression is evaluated and the result treated as a boolean,
  1055. # starting with the leftmost and continuing to the right.
  1056. #
  1057. foreach {a b c} {0 0 0} break
  1058. set varlist [list]
  1059. do_execsql_test e_expr-21.1.1 {
  1060. SELECT CASE WHEN var('a') THEN 'A'
  1061. WHEN var('b') THEN 'B'
  1062. WHEN var('c') THEN 'C' END
  1063. } {{}}
  1064. do_test e_expr-21.1.2 { set varlist } {a b c}
  1065. set varlist [list]
  1066. do_execsql_test e_expr-21.1.3 {
  1067. SELECT CASE WHEN var('c') THEN 'C'
  1068. WHEN var('b') THEN 'B'
  1069. WHEN var('a') THEN 'A'
  1070. ELSE 'no result'
  1071. END
  1072. } {{no result}}
  1073. do_test e_expr-21.1.4 { set varlist } {c b a}
  1074. # EVIDENCE-OF: R-39009-25596 The result of the CASE expression is the
  1075. # evaluation of the THEN expression that corresponds to the first WHEN
  1076. # expression that evaluates to true.
  1077. #
  1078. foreach {a b c} {0 1 0} break
  1079. do_execsql_test e_expr-21.2.1 {
  1080. SELECT CASE WHEN var('a') THEN 'A'
  1081. WHEN var('b') THEN 'B'
  1082. WHEN var('c') THEN 'C'
  1083. ELSE 'no result'
  1084. END
  1085. } {B}
  1086. foreach {a b c} {0 1 1} break
  1087. do_execsql_test e_expr-21.2.2 {
  1088. SELECT CASE WHEN var('a') THEN 'A'
  1089. WHEN var('b') THEN 'B'
  1090. WHEN var('c') THEN 'C'
  1091. ELSE 'no result'
  1092. END
  1093. } {B}
  1094. foreach {a b c} {0 0 1} break
  1095. do_execsql_test e_expr-21.2.3 {
  1096. SELECT CASE WHEN var('a') THEN 'A'
  1097. WHEN var('b') THEN 'B'
  1098. WHEN var('c') THEN 'C'
  1099. ELSE 'no result'
  1100. END
  1101. } {C}
  1102. # EVIDENCE-OF: R-24227-04807 Or, if none of the WHEN expressions
  1103. # evaluate to true, the result of evaluating the ELSE expression, if
  1104. # any.
  1105. #
  1106. foreach {a b c} {0 0 0} break
  1107. do_execsql_test e_expr-21.3.1 {
  1108. SELECT CASE WHEN var('a') THEN 'A'
  1109. WHEN var('b') THEN 'B'
  1110. WHEN var('c') THEN 'C'
  1111. ELSE 'no result'
  1112. END
  1113. } {{no result}}
  1114. # EVIDENCE-OF: R-14168-07579 If there is no ELSE expression and none of
  1115. # the WHEN expressions are true, then the overall result is NULL.
  1116. #
  1117. db nullvalue null
  1118. do_execsql_test e_expr-21.3.2 {
  1119. SELECT CASE WHEN var('a') THEN 'A'
  1120. WHEN var('b') THEN 'B'
  1121. WHEN var('c') THEN 'C'
  1122. END
  1123. } {null}
  1124. db nullvalue {}
  1125. # EVIDENCE-OF: R-13943-13592 A NULL result is considered untrue when
  1126. # evaluating WHEN terms.
  1127. #
  1128. do_execsql_test e_expr-21.4.1 {
  1129. SELECT CASE WHEN NULL THEN 'A' WHEN 1 THEN 'B' END
  1130. } {B}
  1131. do_execsql_test e_expr-21.4.2 {
  1132. SELECT CASE WHEN 0 THEN 'A' WHEN NULL THEN 'B' ELSE 'C' END
  1133. } {C}
  1134. # EVIDENCE-OF: R-38620-19499 In a CASE with a base expression, the base
  1135. # expression is evaluated just once and the result is compared against
  1136. # the evaluation of each WHEN expression from left to right.
  1137. #
  1138. # Note: This test case tests the "evaluated just once" part of the above
  1139. # statement. Tests associated with the next two statements test that the
  1140. # comparisons take place.
  1141. #
  1142. foreach {a b c} [list [expr 3] [expr 4] [expr 5]] break
  1143. set ::varlist [list]
  1144. do_execsql_test e_expr-22.1.1 {
  1145. SELECT CASE var('a') WHEN 1 THEN 'A' WHEN 2 THEN 'B' WHEN 3 THEN 'C' END
  1146. } {C}
  1147. do_test e_expr-22.1.2 { set ::varlist } {a}
  1148. # EVIDENCE-OF: R-07667-49537 The result of the CASE expression is the
  1149. # evaluation of the THEN expression that corresponds to the first WHEN
  1150. # expression for which the comparison is true.
  1151. #
  1152. do_execsql_test e_expr-22.2.1 {
  1153. SELECT CASE 23 WHEN 1 THEN 'A' WHEN 23 THEN 'B' WHEN 23 THEN 'C' END
  1154. } {B}
  1155. do_execsql_test e_expr-22.2.2 {
  1156. SELECT CASE 1 WHEN 1 THEN 'A' WHEN 23 THEN 'B' WHEN 23 THEN 'C' END
  1157. } {A}
  1158. # EVIDENCE-OF: R-47543-32145 Or, if none of the WHEN expressions
  1159. # evaluate to a value equal to the base expression, the result of
  1160. # evaluating the ELSE expression, if any.
  1161. #
  1162. do_execsql_test e_expr-22.3.1 {
  1163. SELECT CASE 24 WHEN 1 THEN 'A' WHEN 23 THEN 'B' WHEN 23 THEN 'C' ELSE 'D' END
  1164. } {D}
  1165. # EVIDENCE-OF: R-54721-48557 If there is no ELSE expression and none of
  1166. # the WHEN expressions produce a result equal to the base expression,
  1167. # the overall result is NULL.
  1168. #
  1169. do_execsql_test e_expr-22.4.1 {
  1170. SELECT CASE 24 WHEN 1 THEN 'A' WHEN 23 THEN 'B' WHEN 23 THEN 'C' END
  1171. } {{}}
  1172. db nullvalue null
  1173. do_execsql_test e_expr-22.4.2 {
  1174. SELECT CASE 24 WHEN 1 THEN 'A' WHEN 23 THEN 'B' WHEN 23 THEN 'C' END
  1175. } {null}
  1176. db nullvalue {}
  1177. # EVIDENCE-OF: R-11479-62774 When comparing a base expression against a
  1178. # WHEN expression, the same collating sequence, affinity, and
  1179. # NULL-handling rules apply as if the base expression and WHEN
  1180. # expression are respectively the left- and right-hand operands of an =
  1181. # operator.
  1182. #
  1183. proc rev {str} {
  1184. set ret ""
  1185. set chars [split $str]
  1186. for {set i [expr [llength $chars]-1]} {$i>=0} {incr i -1} {
  1187. append ret [lindex $chars $i]
  1188. }
  1189. set ret
  1190. }
  1191. proc reverse {lhs rhs} {
  1192. string compare [rev $lhs] [ref $rhs]
  1193. }
  1194. db collate reverse reverse
  1195. do_execsql_test e_expr-23.1.1 {
  1196. CREATE TABLE t1(
  1197. a TEXT COLLATE NOCASE,
  1198. b COLLATE REVERSE,
  1199. c INTEGER,
  1200. d BLOB
  1201. );
  1202. INSERT INTO t1 VALUES('abc', 'cba', 55, 34.5);
  1203. } {}
  1204. do_execsql_test e_expr-23.1.2 {
  1205. SELECT CASE a WHEN 'xyz' THEN 'A' WHEN 'AbC' THEN 'B' END FROM t1
  1206. } {B}
  1207. do_execsql_test e_expr-23.1.3 {
  1208. SELECT CASE 'AbC' WHEN 'abc' THEN 'A' WHEN a THEN 'B' END FROM t1
  1209. } {B}
  1210. do_execsql_test e_expr-23.1.4 {
  1211. SELECT CASE a WHEN b THEN 'A' ELSE 'B' END FROM t1
  1212. } {B}
  1213. do_execsql_test e_expr-23.1.5 {
  1214. SELECT CASE b WHEN a THEN 'A' ELSE 'B' END FROM t1
  1215. } {A}
  1216. do_execsql_test e_expr-23.1.6 {
  1217. SELECT CASE 55 WHEN '55' THEN 'A' ELSE 'B' END
  1218. } {B}
  1219. do_execsql_test e_expr-23.1.7 {
  1220. SELECT CASE c WHEN '55' THEN 'A' ELSE 'B' END FROM t1
  1221. } {A}
  1222. do_execsql_test e_expr-23.1.8 {
  1223. SELECT CASE '34.5' WHEN d THEN 'A' ELSE 'B' END FROM t1
  1224. } {B}
  1225. do_execsql_test e_expr-23.1.9 {
  1226. SELECT CASE NULL WHEN NULL THEN 'A' ELSE 'B' END
  1227. } {B}
  1228. # EVIDENCE-OF: R-37304-39405 If the base expression is NULL then the
  1229. # result of the CASE is always the result of evaluating the ELSE
  1230. # expression if it exists, or NULL if it does not.
  1231. #
  1232. do_execsql_test e_expr-24.1.1 {
  1233. SELECT CASE NULL WHEN 'abc' THEN 'A' WHEN 'def' THEN 'B' END;
  1234. } {{}}
  1235. do_execsql_test e_expr-24.1.2 {
  1236. SELECT CASE NULL WHEN 'abc' THEN 'A' WHEN 'def' THEN 'B' ELSE 'C' END;
  1237. } {C}
  1238. # EVIDENCE-OF: R-56280-17369 Both forms of the CASE expression use lazy,
  1239. # or short-circuit, evaluation.
  1240. #
  1241. set varlist [list]
  1242. foreach {a b c} {0 1 0} break
  1243. do_execsql_test e_expr-25.1.1 {
  1244. SELECT CASE WHEN var('a') THEN 'A'
  1245. WHEN var('b') THEN 'B'
  1246. WHEN var('c') THEN 'C'
  1247. END
  1248. } {B}
  1249. do_test e_expr-25.1.2 { set ::varlist } {a b}
  1250. set varlist [list]
  1251. do_execsql_test e_expr-25.1.3 {
  1252. SELECT CASE 0 WHEN var('a') THEN 'A'
  1253. WHEN var('b') THEN 'B'
  1254. WHEN var('c') THEN 'C'
  1255. END
  1256. } {A}
  1257. do_test e_expr-25.1.4 { set ::varlist } {a}
  1258. # EVIDENCE-OF: R-34773-62253 The only difference between the following
  1259. # two CASE expressions is that the x expression is evaluated exactly
  1260. # once in the first example but might be evaluated multiple times in the
  1261. # second: CASE x WHEN w1 THEN r1 WHEN w2 THEN r2 ELSE r3 END CASE WHEN
  1262. # x=w1 THEN r1 WHEN x=w2 THEN r2 ELSE r3 END
  1263. #
  1264. proc ceval {x} {
  1265. incr ::evalcount
  1266. return $x
  1267. }
  1268. db func ceval ceval
  1269. set ::evalcount 0
  1270. do_execsql_test e_expr-26.1.1 {
  1271. CREATE TABLE t2(x, w1, r1, w2, r2, r3);
  1272. INSERT INTO t2 VALUES(1, 1, 'R1', 2, 'R2', 'R3');
  1273. INSERT INTO t2 VALUES(2, 1, 'R1', 2, 'R2', 'R3');
  1274. INSERT INTO t2 VALUES(3, 1, 'R1', 2, 'R2', 'R3');
  1275. } {}
  1276. do_execsql_test e_expr-26.1.2 {
  1277. SELECT CASE x WHEN w1 THEN r1 WHEN w2 THEN r2 ELSE r3 END FROM t2
  1278. } {R1 R2 R3}
  1279. do_execsql_test e_expr-26.1.3 {
  1280. SELECT CASE WHEN x=w1 THEN r1 WHEN x=w2 THEN r2 ELSE r3 END FROM t2
  1281. } {R1 R2 R3}
  1282. do_execsql_test e_expr-26.1.4 {
  1283. SELECT CASE ceval(x) WHEN w1 THEN r1 WHEN w2 THEN r2 ELSE r3 END FROM t2
  1284. } {R1 R2 R3}
  1285. do_test e_expr-26.1.5 { set ::evalcount } {3}
  1286. set ::evalcount 0
  1287. do_execsql_test e_expr-26.1.6 {
  1288. SELECT CASE
  1289. WHEN ceval(x)=w1 THEN r1
  1290. WHEN ceval(x)=w2 THEN r2
  1291. ELSE r3 END
  1292. FROM t2
  1293. } {R1 R2 R3}
  1294. do_test e_expr-26.1.6 { set ::evalcount } {5}
  1295. #-------------------------------------------------------------------------
  1296. # Test statements related to CAST expressions.
  1297. #
  1298. # EVIDENCE-OF: R-65079-31758 Application of a CAST expression is
  1299. # different to application of a column affinity, as with a CAST
  1300. # expression the storage class conversion is forced even if it is lossy
  1301. # and irrreversible.
  1302. #
  1303. do_execsql_test e_expr-27.1.1 {
  1304. CREATE TABLE t3(a TEXT, b REAL, c INTEGER);
  1305. INSERT INTO t3 VALUES(X'555655', '1.23abc', 4.5);
  1306. SELECT typeof(a), a, typeof(b), b, typeof(c), c FROM t3;
  1307. } {blob UVU text 1.23abc real 4.5}
  1308. do_execsql_test e_expr-27.1.2 {
  1309. SELECT
  1310. typeof(CAST(X'555655' as TEXT)), CAST(X'555655' as TEXT),
  1311. typeof(CAST('1.23abc' as REAL)), CAST('1.23abc' as REAL),
  1312. typeof(CAST(4.5 as INTEGER)), CAST(4.5 as INTEGER)
  1313. } {text UVU real 1.23 integer 4}
  1314. # EVIDENCE-OF: R-27225-65050 If the value of <expr> is NULL, then
  1315. # the result of the CAST expression is also NULL.
  1316. #
  1317. do_expr_test e_expr-27.2.1 { CAST(NULL AS integer) } null {}
  1318. do_expr_test e_expr-27.2.2 { CAST(NULL AS text) } null {}
  1319. do_expr_test e_expr-27.2.3 { CAST(NULL AS blob) } null {}
  1320. do_expr_test e_expr-27.2.4 { CAST(NULL AS number) } null {}
  1321. # EVIDENCE-OF: R-31076-23575 Casting a value to a <type-name> with
  1322. # no affinity causes the value to be converted into a BLOB.
  1323. #
  1324. do_expr_test e_expr-27.3.1 { CAST('abc' AS blob) } blob abc
  1325. do_expr_test e_expr-27.3.2 { CAST('def' AS shobblob_x) } blob def
  1326. do_expr_test e_expr-27.3.3 { CAST('ghi' AS abbLOb10) } blob ghi
  1327. # EVIDENCE-OF: R-22956-37754 Casting to a BLOB consists of first casting
  1328. # the value to TEXT in the encoding of the database connection, then
  1329. # interpreting the resulting byte sequence as a BLOB instead of as TEXT.
  1330. #
  1331. do_qexpr_test e_expr-27.4.1 { CAST('ghi' AS blob) } X'676869'
  1332. do_qexpr_test e_expr-27.4.2 { CAST(456 AS blob) } X'343536'
  1333. do_qexpr_test e_expr-27.4.3 { CAST(1.78 AS blob) } X'312E3738'
  1334. rename db db2
  1335. sqlite3 db :memory:
  1336. ifcapable {utf16} {
  1337. db eval { PRAGMA encoding = 'utf-16le' }
  1338. do_qexpr_test e_expr-27.4.4 { CAST('ghi' AS blob) } X'670068006900'
  1339. do_qexpr_test e_expr-27.4.5 { CAST(456 AS blob) } X'340035003600'
  1340. do_qexpr_test e_expr-27.4.6 { CAST(1.78 AS blob) } X'31002E0037003800'
  1341. }
  1342. db close
  1343. sqlite3 db :memory:
  1344. db eval { PRAGMA encoding = 'utf-16be' }
  1345. ifcapable {utf16} {
  1346. do_qexpr_test e_expr-27.4.7 { CAST('ghi' AS blob) } X'006700680069'
  1347. do_qexpr_test e_expr-27.4.8 { CAST(456 AS blob) } X'003400350036'
  1348. do_qexpr_test e_expr-27.4.9 { CAST(1.78 AS blob) } X'0031002E00370038'
  1349. }
  1350. db close
  1351. rename db2 db
  1352. # EVIDENCE-OF: R-04207-37981 To cast a BLOB value to TEXT, the sequence
  1353. # of bytes that make up the BLOB is interpreted as text encoded using
  1354. # the database encoding.
  1355. #
  1356. do_expr_test e_expr-28.1.1 { CAST (X'676869' AS text) } text ghi
  1357. do_expr_test e_expr-28.1.2 { CAST (X'670068006900' AS text) } text g
  1358. rename db db2
  1359. sqlite3 db :memory:
  1360. db eval { PRAGMA encoding = 'utf-16le' }
  1361. ifcapable {utf16} {
  1362. do_expr_test e_expr-28.1.3 { CAST (X'676869' AS text) == 'ghi' } integer 0
  1363. do_expr_test e_expr-28.1.4 { CAST (X'670068006900' AS text) } text ghi
  1364. }
  1365. db close
  1366. rename db2 db
  1367. # EVIDENCE-OF: R-22235-47006 Casting an INTEGER or REAL value into TEXT
  1368. # renders the value as if via sqlite3_snprintf() except that the
  1369. # resulting TEXT uses the encoding of the database connection.
  1370. #
  1371. do_expr_test e_expr-28.2.1 { CAST (1 AS text) } text 1
  1372. do_expr_test e_expr-28.2.2 { CAST (45 AS text) } text 45
  1373. do_expr_test e_expr-28.2.3 { CAST (-45 AS text) } text -45
  1374. do_expr_test e_expr-28.2.4 { CAST (8.8 AS text) } text 8.8
  1375. do_expr_test e_expr-28.2.5 { CAST (2.3e+5 AS text) } text 230000.0
  1376. do_expr_test e_expr-28.2.6 { CAST (-2.3e-5 AS text) } text -2.3e-05
  1377. do_expr_test e_expr-28.2.7 { CAST (0.0 AS text) } text 0.0
  1378. do_expr_test e_expr-28.2.7 { CAST (0 AS text) } text 0
  1379. # EVIDENCE-OF: R-26346-36443 When casting a BLOB value to a REAL, the
  1380. # value is first converted to TEXT.
  1381. #
  1382. do_expr_test e_expr-29.1.1 { CAST (X'312E3233' AS REAL) } real 1.23
  1383. do_expr_test e_expr-29.1.2 { CAST (X'3233302E30' AS REAL) } real 230.0
  1384. do_expr_test e_expr-29.1.3 { CAST (X'2D392E3837' AS REAL) } real -9.87
  1385. do_expr_test e_expr-29.1.4 { CAST (X'302E30303031' AS REAL) } real 0.0001
  1386. rename db db2
  1387. sqlite3 db :memory:
  1388. ifcapable {utf16} {
  1389. db eval { PRAGMA encoding = 'utf-16le' }
  1390. do_expr_test e_expr-29.1.5 {
  1391. CAST (X'31002E0032003300' AS REAL) } real 1.23
  1392. do_expr_test e_expr-29.1.6 {
  1393. CAST (X'3200330030002E003000' AS REAL) } real 230.0
  1394. do_expr_test e_expr-29.1.7 {
  1395. CAST (X'2D0039002E0038003700' AS REAL) } real -9.87
  1396. do_expr_test e_expr-29.1.8 {
  1397. CAST (X'30002E003000300030003100' AS REAL) } real 0.0001
  1398. }
  1399. db close
  1400. rename db2 db
  1401. # EVIDENCE-OF: R-54898-34554 When casting a TEXT value to REAL, the
  1402. # longest possible prefix of the value that can be interpreted as a real
  1403. # number is extracted from the TEXT value and the remainder ignored.
  1404. #
  1405. do_expr_test e_expr-29.2.1 { CAST('1.23abcd' AS REAL) } real 1.23
  1406. do_expr_test e_expr-29.2.2 { CAST('1.45.23abcd' AS REAL) } real 1.45
  1407. do_expr_test e_expr-29.2.3 { CAST('-2.12e-01ABC' AS REAL) } real -0.212
  1408. do_expr_test e_expr-29.2.4 { CAST('1 2 3 4' AS REAL) } real 1.0
  1409. # EVIDENCE-OF: R-11321-47427 Any leading spaces in the TEXT value are
  1410. # ignored when converging from TEXT to REAL.
  1411. #
  1412. do_expr_test e_expr-29.3.1 { CAST(' 1.23abcd' AS REAL) } real 1.23
  1413. do_expr_test e_expr-29.3.2 { CAST(' 1.45.23abcd' AS REAL) } real 1.45
  1414. do_expr_test e_expr-29.3.3 { CAST(' -2.12e-01ABC' AS REAL) } real -0.212
  1415. do_expr_test e_expr-29.3.4 { CAST(' 1 2 3 4' AS REAL) } real 1.0
  1416. # EVIDENCE-OF: R-22662-28218 If there is no prefix that can be
  1417. # interpreted as a real number, the result of the conversion is 0.0.
  1418. #
  1419. do_expr_test e_expr-29.4.1 { CAST('' AS REAL) } real 0.0
  1420. do_expr_test e_expr-29.4.2 { CAST('not a number' AS REAL) } real 0.0
  1421. do_expr_test e_expr-29.4.3 { CAST('XXI' AS REAL) } real 0.0
  1422. # EVIDENCE-OF: R-21829-14563 When casting a BLOB value to INTEGER, the
  1423. # value is first converted to TEXT.
  1424. #
  1425. do_expr_test e_expr-30.1.1 { CAST(X'313233' AS INTEGER) } integer 123
  1426. do_expr_test e_expr-30.1.2 { CAST(X'2D363738' AS INTEGER) } integer -678
  1427. do_expr_test e_expr-30.1.3 {
  1428. CAST(X'31303030303030' AS INTEGER)
  1429. } integer 1000000
  1430. do_expr_test e_expr-30.1.4 {
  1431. CAST(X'2D31313235383939393036383432363234' AS INTEGER)
  1432. } integer -1125899906842624
  1433. rename db db2
  1434. sqlite3 db :memory:
  1435. ifcapable {utf16} {
  1436. execsql { PRAGMA encoding = 'utf-16be' }
  1437. do_expr_test e_expr-30.1.5 { CAST(X'003100320033' AS INTEGER) } integer 123
  1438. do_expr_test e_expr-30.1.6 { CAST(X'002D003600370038' AS INTEGER) } integer -678
  1439. do_expr_test e_expr-30.1.7 {
  1440. CAST(X'0031003000300030003000300030' AS INTEGER)
  1441. } integer 1000000
  1442. do_expr_test e_expr-30.1.8 {
  1443. CAST(X'002D0031003100320035003800390039003900300036003800340032003600320034' AS INTEGER)
  1444. } integer -1125899906842624
  1445. }
  1446. db close
  1447. rename db2 db
  1448. # EVIDENCE-OF: R-47612-45842 When casting a TEXT value to INTEGER, the
  1449. # longest possible prefix of the value that can be interpreted as an
  1450. # integer number is extracted from the TEXT value and the remainder
  1451. # ignored.
  1452. #
  1453. do_expr_test e_expr-30.2.1 { CAST('123abcd' AS INT) } integer 123
  1454. do_expr_test e_expr-30.2.2 { CAST('14523abcd' AS INT) } integer 14523
  1455. do_expr_test e_expr-30.2.3 { CAST('-2.12e-01ABC' AS INT) } integer -2
  1456. do_expr_test e_expr-30.2.4 { CAST('1 2 3 4' AS INT) } integer 1
  1457. # EVIDENCE-OF: R-34400-33772 Any leading spaces in the TEXT value when
  1458. # converting from TEXT to INTEGER are ignored.
  1459. #
  1460. do_expr_test e_expr-30.3.1 { CAST(' 123abcd' AS INT) } integer 123
  1461. do_expr_test e_expr-30.3.2 { CAST(' 14523abcd' AS INT) } integer 14523
  1462. do_expr_test e_expr-30.3.3 { CAST(' -2.12e-01ABC' AS INT) } integer -2
  1463. do_expr_test e_expr-30.3.4 { CAST(' 1 2 3 4' AS INT) } integer 1
  1464. # EVIDENCE-OF: R-43164-44276 If there is no prefix that can be
  1465. # interpreted as an integer number, the result of the conversion is 0.
  1466. #
  1467. do_expr_test e_expr-30.4.1 { CAST('' AS INTEGER) } integer 0
  1468. do_expr_test e_expr-30.4.2 { CAST('not a number' AS INTEGER) } integer 0
  1469. do_expr_test e_expr-30.4.3 { CAST('XXI' AS INTEGER) } integer 0
  1470. # EVIDENCE-OF: R-00741-38776 A cast of a REAL value into an INTEGER will
  1471. # truncate the fractional part of the REAL.
  1472. #
  1473. do_expr_test e_expr-31.1.1 { CAST(3.14159 AS INTEGER) } integer 3
  1474. do_expr_test e_expr-31.1.2 { CAST(1.99999 AS INTEGER) } integer 1
  1475. do_expr_test e_expr-31.1.3 { CAST(-1.99999 AS INTEGER) } integer -1
  1476. do_expr_test e_expr-31.1.4 { CAST(-0.99999 AS INTEGER) } integer 0
  1477. # EVIDENCE-OF: R-49503-28105 If a REAL is too large to be represented as
  1478. # an INTEGER then the result of the cast is the largest negative
  1479. # integer: -9223372036854775808.
  1480. #
  1481. do_expr_test e_expr-31.2.1 { CAST(2e+50 AS INT) } integer -9223372036854775808
  1482. do_expr_test e_expr-31.2.2 { CAST(-2e+50 AS INT) } integer -9223372036854775808
  1483. do_expr_test e_expr-31.2.3 {
  1484. CAST(-9223372036854775809.0 AS INT)
  1485. } integer -9223372036854775808
  1486. do_expr_test e_expr-31.2.4 {
  1487. CAST(9223372036854775809.0 AS INT)
  1488. } integer -9223372036854775808
  1489. # EVIDENCE-OF: R-09295-61337 Casting a TEXT or BLOB value into NUMERIC
  1490. # first does a forced conversion into REAL but then further converts the
  1491. # result into INTEGER if and only if the conversion from REAL to INTEGER
  1492. # is lossless and reversible.
  1493. #
  1494. do_expr_test e_expr-32.1.1 { CAST('45' AS NUMERIC) } integer 45
  1495. do_expr_test e_expr-32.1.2 { CAST('45.0' AS NUMERIC) } integer 45
  1496. do_expr_test e_expr-32.1.3 { CAST('45.2' AS NUMERIC) } real 45.2
  1497. do_expr_test e_expr-32.1.4 { CAST('11abc' AS NUMERIC) } integer 11
  1498. do_expr_test e_expr-32.1.5 { CAST('11.1abc' AS NUMERIC) } real 11.1
  1499. # EVIDENCE-OF: R-30347-18702 Casting a REAL or INTEGER value to NUMERIC
  1500. # is a no-op, even if a real value could be losslessly converted to an
  1501. # integer.
  1502. #
  1503. do_expr_test e_expr-32.2.1 { CAST(13.0 AS NUMERIC) } real 13.0
  1504. do_expr_test e_expr-32.2.2 { CAST(13.5 AS NUMERIC) } real 13.5
  1505. do_expr_test e_expr-32.2.3 {
  1506. CAST(-9223372036854775808 AS NUMERIC)
  1507. } integer -9223372036854775808
  1508. do_expr_test e_expr-32.2.4 {
  1509. CAST(9223372036854775807 AS NUMERIC)
  1510. } integer 9223372036854775807
  1511. # EVIDENCE-OF: R-64550-29191 Note that the result from casting any
  1512. # non-BLOB value into a BLOB and the result from casting any BLOB value
  1513. # into a non-BLOB value may be different depending on whether the
  1514. # database encoding is UTF-8, UTF-16be, or UTF-16le.
  1515. #
  1516. ifcapable {utf16} {
  1517. sqlite3 db1 :memory: ; db1 eval { PRAGMA encoding = 'utf-8' }
  1518. sqlite3 db2 :memory: ; db2 eval { PRAGMA encoding = 'utf-16le' }
  1519. sqlite3 db3 :memory: ; db3 eval { PRAGMA encoding = 'utf-16be' }
  1520. foreach {tn castexpr differs} {
  1521. 1 { CAST(123 AS BLOB) } 1
  1522. 2 { CAST('' AS BLOB) } 0
  1523. 3 { CAST('abcd' AS BLOB) } 1
  1524. 4 { CAST(X'abcd' AS TEXT) } 1
  1525. 5 { CAST(X'' AS TEXT) } 0
  1526. } {
  1527. set r1 [db1 eval "SELECT typeof($castexpr), quote($castexpr)"]
  1528. set r2 [db2 eval "SELECT typeof($castexpr), quote($castexpr)"]
  1529. set r3 [db3 eval "SELECT typeof($castexpr), quote($castexpr)"]
  1530. if {$differs} {
  1531. set res [expr {$r1!=$r2 && $r2!=$r3}]
  1532. } else {
  1533. set res [expr {$r1==$r2 && $r2==$r3}]
  1534. }
  1535. do_test e_expr-33.1.$tn {set res} 1
  1536. }
  1537. db1 close
  1538. db2 close
  1539. db3 close
  1540. }
  1541. #-------------------------------------------------------------------------
  1542. # Test statements related to the EXISTS and NOT EXISTS operators.
  1543. #
  1544. catch { db close }
  1545. file delete -force test.db
  1546. sqlite3 db test.db
  1547. do_execsql_test e_expr-34.1 {
  1548. CREATE TABLE t1(a, b);
  1549. INSERT INTO t1 VALUES(1, 2);
  1550. INSERT INTO t1 VALUES(NULL, 2);
  1551. INSERT INTO t1 VALUES(1, NULL);
  1552. INSERT INTO t1 VALUES(NULL, NULL);
  1553. } {}
  1554. # EVIDENCE-OF: R-25588-27181 The EXISTS operator always evaluates to one
  1555. # of the integer values 0 and 1.
  1556. #
  1557. # This statement is not tested by itself. Instead, all e_expr-34.* tests
  1558. # following this point explicitly test that specific invocations of EXISTS
  1559. # return either integer 0 or integer 1.
  1560. #
  1561. # EVIDENCE-OF: R-58553-63740 If executing the SELECT statement specified
  1562. # as the right-hand operand of the EXISTS operator would return one or
  1563. # more rows, then the EXISTS operator evaluates to 1.
  1564. #
  1565. foreach {tn expr} {
  1566. 1 { EXISTS ( SELECT a FROM t1 ) }
  1567. 2 { EXISTS ( SELECT b FROM t1 ) }
  1568. 3 { EXISTS ( SELECT 24 ) }
  1569. 4 { EXISTS ( SELECT NULL ) }
  1570. 5 { EXISTS ( SELECT a FROM t1 WHERE a IS NULL ) }
  1571. } {
  1572. do_expr_test e_expr-34.2.$tn $expr integer 1
  1573. }
  1574. # EVIDENCE-OF: R-19673-40972 If executing the SELECT would return no
  1575. # rows at all, then the EXISTS operator evaluates to 0.
  1576. #
  1577. foreach {tn expr} {
  1578. 1 { EXISTS ( SELECT a FROM t1 WHERE 0) }
  1579. 2 { EXISTS ( SELECT b FROM t1 WHERE a = 5) }
  1580. 3 { EXISTS ( SELECT 24 WHERE 0) }
  1581. 4 { EXISTS ( SELECT NULL WHERE 1=2) }
  1582. } {
  1583. do_expr_test e_expr-34.3.$tn $expr integer 0
  1584. }
  1585. # EVIDENCE-OF: R-35109-49139 The number of columns in each row returned
  1586. # by the SELECT statement (if any) and the specific values returned have
  1587. # no effect on the results of the EXISTS operator.
  1588. #
  1589. foreach {tn expr res} {
  1590. 1 { EXISTS ( SELECT * FROM t1 ) } 1
  1591. 2 { EXISTS ( SELECT *, *, * FROM t1 ) } 1
  1592. 3 { EXISTS ( SELECT 24, 25 ) } 1
  1593. 4 { EXISTS ( SELECT NULL, NULL, NULL ) } 1
  1594. 5 { EXISTS ( SELECT a,b,a||b FROM t1 WHERE a IS NULL ) } 1
  1595. 6 { EXISTS ( SELECT a, a FROM t1 WHERE 0) } 0
  1596. 7 { EXISTS ( SELECT b, b, a FROM t1 WHERE a = 5) } 0
  1597. 8 { EXISTS ( SELECT 24, 46, 89 WHERE 0) } 0
  1598. 9 { EXISTS ( SELECT NULL, NULL WHERE 1=2) } 0
  1599. } {
  1600. do_expr_test e_expr-34.4.$tn $expr integer $res
  1601. }
  1602. # EVIDENCE-OF: R-10645-12439 In particular, rows containing NULL values
  1603. # are not handled any differently from rows without NULL values.
  1604. #
  1605. foreach {tn e1 e2} {
  1606. 1 { EXISTS (SELECT 'not null') } { EXISTS (SELECT NULL) }
  1607. 2 { EXISTS (SELECT NULL FROM t1) } { EXISTS (SELECT 'bread' FROM t1) }
  1608. } {
  1609. set res [db one "SELECT $e1"]
  1610. do_expr_test e_expr-34.5.${tn}a $e1 integer $res
  1611. do_expr_test e_expr-34.5.${tn}b $e2 integer $res
  1612. }
  1613. #-------------------------------------------------------------------------
  1614. # Test statements related to scalar sub-queries.
  1615. #
  1616. catch { db close }
  1617. file delete -force test.db
  1618. sqlite3 db test.db
  1619. do_test e_expr-35.0 {
  1620. execsql {
  1621. CREATE TABLE t2(a, b);
  1622. INSERT INTO t2 VALUES('one', 'two');
  1623. INSERT INTO t2 VALUES('three', NULL);
  1624. INSERT INTO t2 VALUES(4, 5.0);
  1625. }
  1626. } {}
  1627. # EVIDENCE-OF: R-00980-39256 A SELECT statement enclosed in parentheses
  1628. # may appear as a scalar quantity.
  1629. #
  1630. # EVIDENCE-OF: R-56294-03966 All types of SELECT statement, including
  1631. # aggregate and compound SELECT queries (queries with keywords like
  1632. # UNION or EXCEPT) are allowed as scalar subqueries.
  1633. #
  1634. do_expr_test e_expr-35.1.1 { (SELECT 35) } integer 35
  1635. do_expr_test e_expr-35.1.2 { (SELECT NULL) } null {}
  1636. do_expr_test e_expr-35.1.3 { (SELECT count(*) FROM t2) } integer 3
  1637. do_expr_test e_expr-35.1.4 { (SELECT 4 FROM t2) } integer 4
  1638. do_expr_test e_expr-35.1.5 {
  1639. (SELECT b FROM t2 UNION SELECT a+1 FROM t2)
  1640. } null {}
  1641. do_expr_test e_expr-35.1.6 {
  1642. (SELECT a FROM t2 UNION SELECT COALESCE(b, 55) FROM t2 ORDER BY 1)
  1643. } integer 4
  1644. # EVIDENCE-OF: R-46899-53765 A SELECT used as a scalar quantity must
  1645. # return a result set with a single column.
  1646. #
  1647. # The following block tests that errors are returned in a bunch of cases
  1648. # where a subquery returns more than one column.
  1649. #
  1650. set M {only a single result allowed for a SELECT that is part of an expression}
  1651. foreach {tn sql} {
  1652. 1 { SELECT (SELECT * FROM t2 UNION SELECT a+1, b+1 FROM t2) }
  1653. 2 { SELECT (SELECT * FROM t2 UNION SELECT a+1, b+1 FROM t2 ORDER BY 1) }
  1654. 3 { SELECT (SELECT 1, 2) }
  1655. 4 { SELECT (SELECT NULL, NULL, NULL) }
  1656. 5 { SELECT (SELECT * FROM t2) }
  1657. 6 { SELECT (SELECT * FROM (SELECT 1, 2, 3)) }
  1658. } {
  1659. do_catchsql_test e_expr-35.2.$tn $sql [list 1 $M]
  1660. }
  1661. # EVIDENCE-OF: R-35764-28041 The result of the expression is the value
  1662. # of the only column in the first row returned by the SELECT statement.
  1663. #
  1664. # EVIDENCE-OF: R-41898-06686 If the SELECT yields more than one result
  1665. # row, all rows after the first are ignored.
  1666. #
  1667. do_execsql_test e_expr-36.3.1 {
  1668. CREATE TABLE t4(x, y);
  1669. INSERT INTO t4 VALUES(1, 'one');
  1670. INSERT INTO t4 VALUES(2, 'two');
  1671. INSERT INTO t4 VALUES(3, 'three');
  1672. } {}
  1673. foreach {tn expr restype resval} {
  1674. 2 { ( SELECT x FROM t4 ORDER BY x ) } integer 1
  1675. 3 { ( SELECT x FROM t4 ORDER BY y ) } integer 1
  1676. 4 { ( SELECT x FROM t4 ORDER BY x DESC ) } integer 3
  1677. 5 { ( SELECT x FROM t4 ORDER BY y DESC ) } integer 2
  1678. 6 { ( SELECT y FROM t4 ORDER BY y DESC ) } text two
  1679. 7 { ( SELECT sum(x) FROM t4 ) } integer 6
  1680. 8 { ( SELECT group_concat(y,'') FROM t4 ) } text onetwothree
  1681. 9 { ( SELECT max(x) FROM t4 WHERE y LIKE '___') } integer 2
  1682. } {
  1683. do_expr_test e_expr-36.3.$tn $expr $restype $resval
  1684. }
  1685. # EVIDENCE-OF: R-25492-41572 If the SELECT yields no rows, then the
  1686. # value of the expression is NULL.
  1687. #
  1688. foreach {tn expr} {
  1689. 1 { ( SELECT x FROM t4 WHERE x>3 ORDER BY x ) }
  1690. 2 { ( SELECT x FROM t4 WHERE y<'one' ORDER BY y ) }
  1691. } {
  1692. do_expr_test e_expr-36.4.$tn $expr null {}
  1693. }
  1694. finish_test