aboutsummaryrefslogtreecommitdiff
path: root/test/indexexpr3.test
blob: 76d3331f75a33311bc40e16ffb9c5ec5cc4086c8 (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
114
115
116
117
118
119
120
121
# 2024-10-05
#
# 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
# focus of this file is testing indexes on expressions.
#

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


do_execsql_test 1.0 {
  CREATE TABLE t1(a, j);
  INSERT INTO t1 VALUES(1, '{x:"one"}');
  INSERT INTO t1 VALUES(2, '{x:"two"}');
  INSERT INTO t1 VALUES(3, '{x:"three"}');

  CREATE INDEX i1 ON t1( json_extract(j, '$.x') );
  CREATE INDEX i2 ON t1( a, json_extract(j, '$.x') );
}

proc do_hasfunction_test {tn sql res} {
  set nFunction 0
  db eval "EXPLAIN $sql" x {
    if {$x(opcode)=="Function"} {
      incr nFunction 
    }
  }

  do_execsql_test $tn "
    SELECT $nFunction;
    $sql
  " $res
}

do_hasfunction_test 1.1 {
  SELECT json_extract(j, '$.x') FROM t1 ORDER BY 1;
} {
  0 one three two
}

do_hasfunction_test 1.2 {
  SELECT json_extract(j, '$.x') FROM t1 WHERE a=2
} {
  0 two
}

do_hasfunction_test 1.3 {
  SELECT coalesce(json_extract(j, '$.x'), 'five') FROM t1 WHERE a=2
} {
  0 two
}

do_hasfunction_test 1.4 {
  SELECT json_extract(j, '$.x') || '.two' FROM t1 WHERE a=2
} {
  0 two.two
}

do_hasfunction_test 1.5 {
  SELECT json_insert( '{}', '$.y', json_extract(j, '$.x') ) FROM t1 WHERE a=2
} {
  2 {{"y":"two"}}
}

do_hasfunction_test 1.6 {
  SELECT json_insert( '{}', '$.y', coalesce( json_extract(j, '$.x'), 'five' ) )
  FROM t1 WHERE a=2
} {
  2 {{"y":"two"}}
}

#-------------------------------------------------------------------------
reset_db
do_execsql_test 2.0 {
  CREATE TABLE t1(a, b, j);
  CREATE INDEX i1 ON t1( a, json_extract(j, '$.x') );
}

do_eqp_test 2.1 {
  SELECT json_extract(j, '$.x') FROM t1 WHERE a=?
} {
  t1 USING COVERING INDEX i1
}

do_eqp_test 2.2 {
  SELECT b, json_extract(j, '$.x') FROM t1 WHERE a=?
} {
  t1 USING INDEX i1
}

do_eqp_test 2.3 {
  SELECT json_insert( '{}', json_extract(j, '$.x') ) FROM t1 WHERE a=?
} {
  t1 USING INDEX i1
}

do_eqp_test 2.4 {
  SELECT sum( json_extract(j, '$.x') ) FROM t1 WHERE a=?
} {
  t1 USING COVERING INDEX i1
}

do_eqp_test 2.5 {
  SELECT json_extract(j, '$.x'), sum( json_extract(j, '$.x') ) FROM t1 WHERE a=?
} {
  t1 USING INDEX i1
}



finish_test