Debian Codegraph @

lib/mrb/scripts/expression_tree/binary_operation.rb

raw symbols SCIP
1 module Groonga
2 module ExpressionTree
3 class BinaryOperation
4 attr_reader :operator
5 attr_reader :left
6 attr_reader :right
7 attr_reader :parameter
8 attr_reader :value
9 def initialize(operator, left, right, options={})
10 @operator = operator
11 @left = left
12 @right = right
13 @parameter = options[:parameter]
14 @value = options[:value]
15 end
16
17 def build(expression)
18 @left.build(expression)
19 @right.build(expression)
20 n_args = 2
21 if @parameter
22 @parameter.build(expression)
23 n_args += 1
24 end
25 if @value
26 expression.append_constant(@value, @operator, n_args)
27 else
28 expression.append_operator(@operator, n_args)
29 end
30 end
31
32 def estimatable?
33 true
34 end
35
36 RANGE_OPERATORS = [
37 Operator::LESS,
38 Operator::GREATER,
39 Operator::LESS_EQUAL,
40 Operator::GREATER_EQUAL,
41 ]
42 QUERY_OPERATIONS = [
43 Operator::MATCH,
44 Operator::NEAR,
45 Operator::NEAR_NO_OFFSET,
46 Operator::NEAR_PHRASE,
47 Operator::ORDERED_NEAR_PHRASE,
48 Operator::NEAR_PHRASE_PRODUCT,
49 Operator::ORDERED_NEAR_PHRASE_PRODUCT,
50 Operator::SIMILAR,
51 Operator::EXACT,
52 Operator::REGEXP,
53 Operator::FUZZY,
54 Operator::QUORUM,
55 ]
56 def estimate_size(table)
57 case @operator
58 when Operator::EQUAL
59 estimate_size_equal(table)
60 when *RANGE_OPERATORS
61 estimate_size_range(table)
62 when *QUERY_OPERATIONS
63 estimate_size_query(table)
64 when Operator::PREFIX
65 estimate_size_prefix(table)
66 else
67 table.size
68 end
69 end
70
71 private
72 def estimate_size_equal(table)
73 return table.size unless @left.is_a?(Variable)
74 return table.size unless @right.is_a?(Constant)
75
76 column = @left.column
77 value = @right.value
78 case column
79 when Groonga::Accessor
80 accessor = column
81 term = value.value
82 return column.estimate_size(query: term,
83 mode: Operator::EQUAL)
84 when Groonga::IndexColumn
85 index_column = column
86 else
87 index_info = column.find_index(@operator)
88 return table.size if index_info.nil?
89 index_column = index_info.index
90 end
91 return table.size unless index_column.respond_to?(:lexicon)
92
93 case value
94 when Groonga::Vector, Groonga::UVector
95 return table.size
96 end
97
98 lexicon = index_column.lexicon
99 term = value.value
100 if value.domain_id == lexicon.id
101 term_id = term.id
102 else
103 term_id = lexicon[term]
104 end
105 if term_id.nil?
106 0
107 else
108 index_column.estimate_size(term_id: term_id)
109 end
110 end
111
112 def estimate_size_range(table)
113 return table.size unless @left.is_a?(Variable)
114 return table.size unless @right.is_a?(Constant)
115
116 is_table_search = false
117 index_column_need_unref = false
118 column = @left.column
119 value = @right.value
120 case column
121 when Table
122 is_table_search = true
123 lexicon = column
124 when Groonga::Accessor
125 is_table_search = true
126 lexicon = column.object
127 case lexicon
128 when PatriciaTrie, DoubleArrayTrie
129 # range searchable
130 else
131 return table.size
132 end
133 when Groonga::IndexColumn
134 index_column = column
135 lexicon = column.lexicon
136 else
137 index_info = column.find_index(@operator)
138 return table.size if index_info.nil?
139
140 index_column = index_info.index
141 index_column_need_unref = true
142 lexicon = index_column.lexicon
143 end
144
145 begin
146 n_terms = lexicon.size
147 return 0 if n_terms.zero?
148
149 options = {
150 limit: sampling_cursor_limit(n_terms),
151 }
152 case @operator
153 when Operator::LESS
154 options[:max] = value
155 options[:flags] = TableCursorFlags::LT
156 when Operator::LESS_EQUAL
157 options[:max] = value
158 options[:flags] = TableCursorFlags::LE
159 when Operator::GREATER
160 options[:min] = value
161 options[:flags] = TableCursorFlags::GT
162 when Operator::GREATER_EQUAL
163 options[:min] = value
164 options[:flags] = TableCursorFlags::GE
165 end
166 TableCursor.open(lexicon, options) do |cursor|
167 if is_table_search
168 size = 1
169 else
170 size = index_column.estimate_size(:lexicon_cursor => cursor)
171 end
172 size += 1 if cursor.next != ID::NIL
173 size
174 end
175 ensure
176 index_column.unref if index_column_need_unref
177 end
178 end
179
180 def estimate_size_query(table)
181 return table.size unless @left.is_a?(Variable)
182 return table.size unless @right.is_a?(Constant)
183
184 column = @left.column
185 indexes = []
186 case column
187 when Expression
188 column.codes.each do |code|
189 case code.op
190 when Operator::GET_VALUE
191 value = code.value
192 case value
193 when IndexColumn
194 indexes << index
195 when Column
196 index_info = value.find_index(@operator)
197 return table.size if index_info.nil?
198 indexes << index_info.index
199 end
200 end
201 end
202 when Groonga::IndexColumn
203 indexes << column
204 when Groonga::Accessor
205 indexes << column
206 else
207 index_info = column.find_index(@operator)
208 return table.size if index_info.nil?
209 indexes << index_info.index
210 end
211
212 return table.size if indexes.empty?
213
214 value = @right.value
215 sizes = indexes.collect do |index|
216 index.estimate_size(:query => value,
217 :mode => @operator)
218 end
219 sizes.max
220 end
221
222 def estimate_size_prefix(table)
223 return table.size unless @left.is_a?(Variable)
224 return table.size unless @right.is_a?(Constant)
225
226 is_table_search = false
227 column = @left.column
228 case column
229 when Groonga::Accessor
230 return table.size unless column.key?
231 is_table_search = true
232 lexicon = column.object
233 when Groonga::IndexColumn
234 lexicon = column.lexicon
235 else
236 return table.size
237 end
238 n_terms = lexicon.size
239 return 0 if n_terms.zero?
240
241 value = @right.value
242 options = {
243 min: value,
244 limit: sampling_cursor_limit(n_terms),
245 flags: TableCursorFlags::PREFIX,
246 }
247 TableCursor.open(lexicon, options) do |cursor|
248 if is_table_search
249 size = 1
250 else
251 size = column.estimate_size(:lexicon_cursor => cursor)
252 end
253 size += 1 if cursor.next != ID::NIL
254 size
255 end
256 end
257
258 def sampling_cursor_limit(n_terms)
259 limit = n_terms * 0.01
260 if limit < 10
261 10
262 elsif limit > 1000
263 1000
264 else
265 limit.to_i
266 end
267 end
268 end
269 end
270 end