summaryrefslogtreecommitdiffstats
path: root/test/sort3.test
blob: 088ae1642d6d62bde6fbeb3f8a1517e2d39c922e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# 2014 March 25.
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library. 
#
# The tests in this file verify that sorting works when the library is
# configured to use mmap(), but the temporary files generated by the
# sorter are too large to be completely mapped.
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix sort3

#-------------------------------------------------------------------------
# Sort some large ( > 4KiB) records.
#
proc cksum {x} {
  set i1 1
  set i2 2
  binary scan $x c* L
  foreach {a b} $L {
    set i1 [expr (($i2<<3) + $a) & 0x7FFFFFFF]
    set i2 [expr (($i1<<3) + $b) & 0x7FFFFFFF]
  }
  list $i1 $i2
}
db func cksum cksum

do_execsql_test 1.0 {
  PRAGMA cache_size = 5;
  CREATE TABLE t11(a, b);
  INSERT INTO t11 VALUES(randomblob(5000), NULL);
  INSERT INTO t11 SELECT randomblob(5000), NULL FROM t11; --2
  INSERT INTO t11 SELECT randomblob(5000), NULL FROM t11; --3
  INSERT INTO t11 SELECT randomblob(5000), NULL FROM t11; --4
  INSERT INTO t11 SELECT randomblob(5000), NULL FROM t11; --5
  INSERT INTO t11 SELECT randomblob(5000), NULL FROM t11; --6
  INSERT INTO t11 SELECT randomblob(5000), NULL FROM t11; --7
  INSERT INTO t11 SELECT randomblob(5000), NULL FROM t11; --8
  INSERT INTO t11 SELECT randomblob(5000), NULL FROM t11; --9
  UPDATE t11 SET b = cksum(a);
}

foreach {tn mmap_limit} {
  1 0
  2 1000000
} {
  do_test 1.$tn {
    sqlite3_test_control SQLITE_TESTCTRL_SORTER_MMAP db $mmap_limit
    set prev ""
    db eval { SELECT * FROM t11 ORDER BY b } {
      if {$b != [cksum $a]} {error "checksum failed"}
      if {[string compare $b $prev] < 0} {error "sort failed"}
      set prev $b
    }
    set {} {}
  } {}
}


# Sort roughly 20MB of data. Once with a mmap limit of 5MB and once without.
#
foreach {itest limit} {
  1 5000000
  2 0x7FFFFFFF
} {
  sqlite3_test_control SQLITE_TESTCTRL_SORTER_MMAP db $limit
  do_execsql_test 2.$itest {
    WITH r(x,y) AS (
        SELECT 1, randomblob(1000)
        UNION ALL
        SELECT x+1, randomblob(1000) FROM r
        LIMIT 20000
    )
    SELECT count(*), sum(length(y)) FROM r GROUP BY (x%5);
  } {
    4000 4000000 
    4000 4000000 
    4000 4000000 
    4000 4000000 
    4000 4000000
  }
}

# Sort more than 2GB of data. At one point this was causing a problem.
# This test might take one minute or more to run.
#
do_execsql_test 3 {
  PRAGMA cache_size = 20000;
  WITH r(x,y) AS (
    SELECT 1, randomblob(1000)
    UNION ALL
    SELECT x+1, randomblob(1000) FROM r
    LIMIT 2200000
  )
  SELECT count(*), sum(length(y)) FROM r GROUP BY (x%5);
} {
  440000 440000000 
  440000 440000000 
  440000 440000000 
  440000 440000000 
  440000 440000000
}

finish_test