Skip to content

Commit

Permalink
mrb: move estimation for call to tree
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Apr 7, 2016
1 parent f25f356 commit 9fe2ae0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 25 deletions.
36 changes: 11 additions & 25 deletions lib/mrb/scripts/expression_size_estimator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ def estimate_data(data)
Operator::GREATER_EQUAL
size = estimate_range(data, index_column)
when Operator::CALL
procedure = data.args.first
if procedure.is_a?(Procedure) and procedure.name == "between"
size = estimate_between(data, index_column)
end
size = estimate_call(data, index_column)
end
size || @table_size
end
Expand Down Expand Up @@ -137,28 +134,17 @@ def estimate_range(data, index_column)
end
end

def estimate_between(data, index_column)
lexicon = index_column.lexicon
_, _, min, min_border, max, max_border = data.args
options = {
:min => min,
:max => max,
:flags => 0,
}
if min_border == "include"
options[:flags] |= TableCursorFlags::LT
else
options[:flags] |= TableCursorFlags::LE
end
if max_border == "include"
options[:flags] |= TableCursorFlags::GT
else
options[:flags] |= TableCursorFlags::GE
end

TableCursor.open(lexicon, options) do |cursor|
index_column.estimate_size(:lexicon_cursor => cursor)
def estimate_call(data, index_column)
procedure = data.args[0]
arguments = data.args[1..-1].collect do |arg|
if arg.is_a?(::Groonga::Object)
ExpressionTree::Variable.new(arg)
else
ExpressionTree::Constant.new(arg)
end
end
node = ExpressionTree::FunctionCall.new(procedure, arguments)
node.estimate_size(@table)
end
end
end
30 changes: 30 additions & 0 deletions lib/mrb/scripts/expression_tree/function_call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,36 @@ def build(expression)
end
expression.append_operator(Operator::CALL, @arguments.size)
end

def estimate_size(table)
return table.size unless @procedure.name == "between"

column, min, min_border, max, max_border = @arguments
index_info = column.column.find_index(Operator::CALL)
return table.size if index_info.nil?

index_column = index_info.index
lexicon = index_column.lexicon
options = {
:min => min.value,
:max => max.value,
:flags => 0,
}
if min_border.value == "include"
options[:flags] |= TableCursorFlags::LT
else
options[:flags] |= TableCursorFlags::LE
end
if max_border.value == "include"
options[:flags] |= TableCursorFlags::GT
else
options[:flags] |= TableCursorFlags::GE
end

TableCursor.open(lexicon, options) do |cursor|
index_column.estimate_size(:lexicon_cursor => cursor)
end
end
end
end
end

0 comments on commit 9fe2ae0

Please sign in to comment.