Debian Codegraph @

doc/source/reference/functions/between.rst

raw
1 .. -*- rst -*-
2
3 .. groonga-command
4 .. database: functions_between
5
6 ``between``
7 ===========
8
9 Summary
10 -------
11
12 ``between`` is used for checking the specified value exists in the specific range.
13 It is often used in combination with :ref:`select-filter` option in :doc:`/reference/commands/select`.
14
15 Syntax
16 ------
17
18 ``between`` has some parameters from three to six parameters::
19
20 between(column_or_value, min, max)
21 between(column_or_value, min, max, {"option": "value of option"})
22 between(column_or_value, min, min_border, max, max_border)
23 between(column_or_value, min, min_border, max, max_border, {"option": "value of option"})
24
25 .. _between-usage:
26
27 Usage
28 -----
29
30 Here are a schema definition and sample data to show usage.
31
32 .. groonga-command
33 .. include:: ../../example/reference/functions/between/usage_setup.log
34 .. table_create Users TABLE_HASH_KEY ShortText
35 .. column_create Users age COLUMN_SCALAR Int32
36 .. table_create Ages TABLE_HASH_KEY Int32
37 .. column_create Ages user_age COLUMN_INDEX Users age
38 .. load --table Users
39 .. [
40 .. {"_key": "Alice", "age": 12},
41 .. {"_key": "Bob", "age": 13},
42 .. {"_key": "Calros", "age": 15},
43 .. {"_key": "Dave", "age": 16},
44 .. {"_key": "Eric", "age": 20},
45 .. {"_key": "Frank", "age": 21}
46 .. ]
47
48 Here is a query to show users who match PG-13 rating (MPAA).
49
50 .. groonga-command
51 .. include:: ../../example/reference/functions/between/usage_age.log
52 .. select Users --filter 'between(age, 13, "include", 16, "include")'
53
54 It returns 13, 14, 15 and 16 years old users.
55
56 ``between`` function accepts not only a column but also a value.
57
58 If you specify a value as the 1st parameter, it checks whether the value is included or not. If the value matches the specified range, the ``select`` returns all the records because the ``between`` function returns true.
59 If it doesn't match the specified range, the ``select`` returns no records because the ``between`` function returns false.
60
61 .. groonga-command
62 .. include:: ../../example/reference/functions/between/usage_value.log
63 .. select Users --filter 'between(14, 13, "include", 16, "include")'
64
65 In the above case, it returns all the records because 14 exists in between 13 and 16.
66 This behavior is used for checking the specified value exists or not in the table.
67
68 .. versionadded:: 16.0.6
69
70 ``between`` also accepts a vector as the 1st parameter.
71
72 Vector is supported only in sequential search. ``between`` doesn't use an index for a vector value yet.
73
74 The 1st parameter can be a vector value such as a ``COLUMN_VECTOR`` column or a vector literal. In this case, ``between`` returns true if any of the elements is included in the specified range. So you can use ``between`` to search records that have at least one value in the range.
75
76 The following example uses a ``COLUMN_VECTOR`` column. Each record has multiple prices. ``between`` selects the records that have at least one price in ``[18, 20)``:
77
78 .. groonga-command
79 .. include:: ../../example/reference/functions/between/usage_vector.log
80 .. table_create Products TABLE_HASH_KEY ShortText
81 .. column_create Products prices COLUMN_VECTOR Int32
82 .. load --table Products
83 .. [
84 .. {"_key": "A", "prices": [17, 170, 1700]},
85 .. {"_key": "B", "prices": [18, 180, 1800]},
86 .. {"_key": "C", "prices": [19, 190]},
87 .. {"_key": "D", "prices": [20]},
88 .. {"_key": "E", "prices": [21, 210, 2100]}
89 .. ]
90 .. select Products --filter 'between(prices, 18, "include", 20, "exclude")'
91
92 ``A`` and ``E`` are excluded because none of their prices is in the range.
93 ``D`` is excluded because ``20`` is excluded by ``"exclude"``.
94 ``B`` and ``C`` are selected because each of them has at least one price in the range.
95
96 If the elements of a vector use different types, the type of the first element is used as the target type and the elements that use other types are ignored. For example, in ``[180.0, 18]``, ``18`` is ignored because the first element ``180.0`` is a floating point number while ``18`` isn't.
97
98 Then, you can also specify options of ``between``.
99 Currently, you can only specify ``too_many_index_match_ratio``. The type of this value is ``double``.
100
101 You can change the value of ``GRN_BETWEEN_TOO_MANY_INDEX_MATCH_RATIO`` with ``too_many_index_match_ratio``.
102 The default value of ``GRN_BETWEEN_TOO_MANY_INDEX_MATCH_RATIO`` is ``0.01``.
103 ``GRN_BETWEEN_TOO_MANY_INDEX_MATCH_RATIO`` is used for deciding whether ``between`` use an index or not.
104
105 There is a case that sequential search is faster than index search when the number of narrowed down records is small enough in contrast to the number of expected records to narrow down by ``between`` with AND operation which use indexes.
106
107 For example, suppose you narrow down records by ``--filter`` and you narrow down them by ``between``.
108
109 In the default, ``between`` use sequential search in the following case.
110
111 1. If you narrow down records to 1,000 by ``--filter`` and records of the target of ``between`` are 500,000.
112
113 .. code-block::
114
115 1,000/500,000 = 0.002 < 0.01(GRN_BETWEEN_TOO_MANY_INDEX_MATCH_RATIO) -> between use sequential search.
116
117 On the other hand, ``between`` use index in the following case.
118
119 2. If you narrow down records to 1,000 by ``--filter`` and records of the target of ``between`` are 50,000.
120
121 .. code-block::
122
123 1,000/50,000 = 0.02 > 0.01(GRN_BETWEEN_TOO_MANY_INDEX_MATCH_RATIO) -> between use index.
124
125
126 Here is a query to set options of ``between``:
127
128 .. groonga-command
129 .. include:: ../../example/reference/functions/between/usage_options.log
130 .. select Users --filter 'between(age, 13, "include", 16, "include", {"too_many_index_match_ratio":0.001})'
131
132 Parameters
133 ----------
134
135 Required parameters
136 ^^^^^^^^^^^^^^^^^^^
137
138 There are three required parameters ``column_or_value``, ``min`` and ``max``.
139
140 ``column_or_value``
141 """""""""""""""""""
142
143 Specifies a column or value.
144
145 .. versionadded:: 16.0.6
146
147 A vector is also accepted only with sequential search. ``between`` returns true if any of the elements is included in the specified range. See :ref:`between-usage` for details.
148
149 ``min``
150 """""""
151
152 Specifies the minimal border value of the range.
153 The range is inclusive by default but you can control the behavior that the value of ``min`` is included or excluded with ``min_border`` parameter.
154
155 ``max``
156 """""""
157
158 Specifies the maximum border value of the range.
159 The range is inclusive by default but you can control the behavior that the value of ``max`` is included or excluded with ``max_border`` parameter.
160
161 Optional parameter
162 ^^^^^^^^^^^^^^^^^^
163
164 There are two optional parameters ``min_border`` and ``max_border``.
165
166 ``min_border``
167 """"""""""""""
168
169 Specifies whether the specified range contains the value of ``min`` or not.
170 The value of ``min_border`` must be either "include" or "exclude". If it is "include", ``min`` value is included. If it is "exclude", ``min`` value is not included.
171
172 ``max_border``
173 """"""""""""""
174
175 Specifies whether the specified range contains the value of ``max`` or not.
176 The value of ``max_border`` must be either "include" or "exclude". If it is "include", ``max`` value is included. If it is "exclude", ``max`` value is not included.
177
178 ``{"option": "value of option"}``
179 """""""""""""""""""""""""""""""""
180
181 Specify between's option.
182 Currently, you can only specify ``too_many_index_match_ratio``. The type of this value is ``double``.
183
184 Return value
185 """"""""""""
186
187 ``between`` returns whether the column value exists in the specified range or not. If a record matches the specified range, it returns true. Otherwise, it returns false.