xref: /sqlite-3.40.0/test/selectG.test (revision b058d054)
1a0c01768Sdrh# 2015-01-05
2a0c01768Sdrh#
3a0c01768Sdrh# The author disclaims copyright to this source code.  In place of
4a0c01768Sdrh# a legal notice, here is a blessing:
5a0c01768Sdrh#
6a0c01768Sdrh#    May you do good and not evil.
7a0c01768Sdrh#    May you find forgiveness for yourself and forgive others.
8a0c01768Sdrh#    May you share freely, never taking more than you give.
9a0c01768Sdrh#
10a0c01768Sdrh#***********************************************************************
11a0c01768Sdrh#
12a0c01768Sdrh# This file verifies that INSERT operations with a very large number of
13a0c01768Sdrh# VALUE terms works and does not hit the SQLITE_LIMIT_COMPOUND_SELECT limit.
14a0c01768Sdrh#
15a0c01768Sdrh
16a0c01768Sdrhset testdir [file dirname $argv0]
17a0c01768Sdrhsource $testdir/tester.tcl
18a0c01768Sdrhset testprefix selectG
19a0c01768Sdrh
206fab3d46Sdrh# Do an INSERT with a VALUES clause that contains 100,000 entries.  Verify
216fab3d46Sdrh# that this insert happens quickly (in less than 10 seconds).  Actually, the
226fab3d46Sdrh# insert will normally happen in less than 0.5 seconds on a workstation, but
236fab3d46Sdrh# we allow plenty of overhead for slower machines.  The speed test checks
246fab3d46Sdrh# for an O(N*N) inefficiency that was once in the code and that would make
256fab3d46Sdrh# the insert run for over a minute.
266fab3d46Sdrh#
276fab3d46Sdrhdo_test 100 {
286fab3d46Sdrh  set sql "CREATE TABLE t1(x);\nINSERT INTO t1(x) VALUES"
296fab3d46Sdrh  for {set i 1} {$i<100000} {incr i} {
306fab3d46Sdrh    append sql "($i),"
316fab3d46Sdrh  }
326fab3d46Sdrh  append sql "($i);"
336fab3d46Sdrh  set microsec [lindex [time {db eval $sql}] 0]
346fab3d46Sdrh  db eval {
356fab3d46Sdrh    SELECT count(x), sum(x), avg(x), $microsec<10000000 FROM t1;
366fab3d46Sdrh  }
376fab3d46Sdrh} {100000 5000050000 50000.5 1}
38a0c01768Sdrh
39*b058d054Sdrh# 2018-01-14.  A 100K-entry VALUES clause within a scalar expression does
40*b058d054Sdrh# not cause processor stack overflow.
41*b058d054Sdrh#
42*b058d054Sdrhdo_test 110 {
43*b058d054Sdrh  set sql "SELECT (VALUES"
44*b058d054Sdrh  for {set i 1} {$i<100000} {incr i} {
45*b058d054Sdrh    append sql "($i),"
46*b058d054Sdrh  }
47*b058d054Sdrh  append sql "($i));"
48*b058d054Sdrh  db eval $sql
49*b058d054Sdrh} {1}
50*b058d054Sdrh
51*b058d054Sdrh# Only the left-most term of a multi-valued VALUES within a scalar
52*b058d054Sdrh# expression is evaluated.
53*b058d054Sdrh#
54*b058d054Sdrhdo_test 120 {
55*b058d054Sdrh  set n [llength [split [db eval "explain $sql"] \n]]
56*b058d054Sdrh  expr {$n<10}
57*b058d054Sdrh} {1}
58*b058d054Sdrh
59a0c01768Sdrhfinish_test
60