Debian Codegraph @

lib/mrb/scripts/expression_tree/function_call.rb

raw symbols SCIP
1 module Groonga
2 module ExpressionTree
3 class FunctionCall
4 attr_reader :procedure
5 attr_reader :arguments
6 def initialize(procedure, arguments)
7 @procedure = procedure
8 @arguments = arguments
9 end
10
11 def build(expression)
12 @procedure.build(expression)
13 @arguments.each do |argument|
14 argument.build(expression)
15 end
16 expression.append_operator(Operator::CALL, @arguments.size)
17 end
18
19 def estimatable?
20 true
21 end
22
23 def estimate_size(table)
24 case @procedure.name
25 when "between"
26 estimate_size_between(table)
27 when "in_values"
28 estimate_size_in_values(table)
29 when "query"
30 estimate_size_query(table)
31 when "query_parallel_or"
32 estimate_size_query_parallel_or(table)
33 else
34 table.size
35 end
36 end
37
38 private
39 def estimate_size_between(table)
40 case @arguments.size
41 when 3, 4
42 column, min, max, _options = @arguments
43 min_border = "include"
44 max_border = "include"
45 when 5, 6
46 column, min, min_border, max, max_border, _options = @arguments
47 min_border = min_border.value
48 max_border = max_border.value
49 end
50
51 if column.is_a?(Groonga::ExpressionTree::IndexColumn)
52 index_column = column.object
53 else
54 index_info = column.column.find_index(Operator::CALL)
55 return table.size if index_info.nil?
56 index_column = index_info.index
57 end
58
59 while index_column.is_a?(Groonga::Accessor)
60 if index_column.have_next?
61 index_column = index_column.next
62 else
63 index_column = index_column.object
64 index_info = index_column.find_index(Operator::CALL)
65 return table.size if index_info.nil?
66 index_column = index_info.index
67 end
68 end
69
70 lexicon = index_column.lexicon
71 options = {
72 :min => min.value,
73 :max => max.value,
74 :flags => 0,
75 }
76 if min_border == "include"
77 options[:flags] |= TableCursorFlags::LT
78 else
79 options[:flags] |= TableCursorFlags::LE
80 end
81 if max_border == "include"
82 options[:flags] |= TableCursorFlags::GT
83 else
84 options[:flags] |= TableCursorFlags::GE
85 end
86
87 TableCursor.open(lexicon, options) do |cursor|
88 index_column.estimate_size(:lexicon_cursor => cursor)
89 end
90 end
91
92 def estimate_size_in_values(table)
93 column, *values = @arguments
94 estimated_sizes = values.collect do |value|
95 node = BinaryOperation.new(Operator::EQUAL, column, value)
96 node.estimate_size(table)
97 end
98 resolve_estimated_sizes(table, estimated_sizes)
99 end
100
101 def estimate_size_query_raw(table, match_columns, query)
102 estimated_sizes = []
103 expression = Expression.create(table)
104 begin
105 expression.parse(match_columns)
106 expression.codes.each do |code|
107 case code.op
108 when Operator::PUSH
109 value = code.value
110 case value
111 when ::Groonga::IndexColumn
112 estimated_size = value.estimate_size(query: query)
113 else
114 next
115 end
116 when Operator::GET_VALUE
117 index_info = code.value.find_index(Operator::MATCH)
118 next unless index_info
119 index = index_info.index
120 estimated_size = index.estimate_size(query: query)
121 index.unref
122 else
123 next
124 end
125 estimated_sizes << estimated_size
126 end
127 ensure
128 expression.close
129 end
130 resolve_estimated_sizes(table, estimated_sizes)
131 end
132
133 def estimate_size_query(table)
134 match_columns, query, = @arguments
135 estimate_size_query_raw(table,
136 match_columns.value.value,
137 query.value)
138 end
139
140 def estimate_size_query_parallel_or(table)
141 match_columns, *queries = @arguments
142 queries.pop if queries.last.is_a?(HashTable)
143 separated_queries = queries.collect do |query|
144 "(#{query.value})"
145 end
146 estimate_size_query_raw(table,
147 match_columns.value.value,
148 separated_queries.join(" OR "))
149 end
150
151 def resolve_estimated_sizes(table, estimated_sizes)
152 case estimated_sizes.size
153 when 0
154 return 0
155 when 1
156 estimated_size = estimated_sizes.first
157 else
158 total = estimated_sizes.inject(0) do |sum, size|
159 sum + size
160 end
161 duplicated_ratio = 0.3 # No reason :p
162 estimated_size = (total * (1 - duplicated_ratio)).round
163 end
164 n_records = table.size
165 if estimated_size < n_records
166 estimated_size
167 else
168 n_records
169 end
170 end
171 end
172 end
173 end