| 1 |
module Groonga |
| 2 |
module Sharding |
| 3 |
class LogicalRangeFilterCommand < Command |
| 4 |
register("logical_range_filter", |
| 5 |
[ |
| 6 |
"logical_table", |
| 7 |
"shard_key", |
| 8 |
"min", |
| 9 |
"min_border", |
| 10 |
"max", |
| 11 |
"max_border", |
| 12 |
"order", |
| 13 |
"filter", |
| 14 |
"offset", |
| 15 |
"limit", |
| 16 |
"output_columns", |
| 17 |
"use_range_index", |
| 18 |
"post_filter", |
| 19 |
"sort_keys", |
| 20 |
]) |
| 21 |
|
| 22 |
def run_body(input) |
| 23 |
output_columns = input[:output_columns] || "_key, *" |
| 24 |
is_stream_output = stream_output?(input) |
| 25 |
|
| 26 |
context = ExecuteContext.new(input) |
| 27 |
begin |
| 28 |
limit = context.limit |
| 29 |
if limit < 0 and limit != -1 |
| 30 |
message = |
| 31 |
"[logical_range_filter] negative limit must be -1: #{limit}" |
| 32 |
raise InvalidArgument, message |
| 33 |
end |
| 34 |
|
| 35 |
n_elements = 0 |
| 36 |
is_first = true |
| 37 |
begin |
| 38 |
executor = Executor.new(context) |
| 39 |
executor.execute do |result_set| |
| 40 |
n_elements += result_set.size |
| 41 |
|
| 42 |
if is_first |
| 43 |
writer.open_result_set_metadata(result_set, output_columns, -1, 1) |
| 44 |
writer.open_table_records(-1) |
| 45 |
is_first = false |
| 46 |
end |
| 47 |
|
| 48 |
options = {} |
| 49 |
options[:limit] = limit |
| 50 |
options[:auto_flush] = true if is_stream_output |
| 51 |
writer.write_table_records_content(result_set, |
| 52 |
output_columns, |
| 53 |
options) |
| 54 |
writer.flush if is_stream_output |
| 55 |
if limit != -1 |
| 56 |
limit -= result_set.size |
| 57 |
break if limit <= 0 |
| 58 |
end |
| 59 |
end |
| 60 |
query_logger.log(:size, ":", "output(#{n_elements})") |
| 61 |
ensure |
| 62 |
unless is_first |
| 63 |
writer.close_table_records |
| 64 |
writer.close_result_set |
| 65 |
end |
| 66 |
end |
| 67 |
ensure |
| 68 |
context.close |
| 69 |
end |
| 70 |
end |
| 71 |
|
| 72 |
private |
| 73 |
def stream_output?(input) |
| 74 |
context.command_version >= 3 |
| 75 |
end |
| 76 |
|
| 77 |
def cache_key(input) |
| 78 |
return nil if stream_output?(input) |
| 79 |
|
| 80 |
key = "logical_range_filter\0" |
| 81 |
key << "#{input[:logical_table]}\0" |
| 82 |
key << "#{input[:shard_key]}\0" |
| 83 |
key << "#{input[:min]}\0" |
| 84 |
key << "#{input[:min_border]}\0" |
| 85 |
key << "#{input[:max]}\0" |
| 86 |
key << "#{input[:max_border]}\0" |
| 87 |
key << "#{input[:order]}\0" |
| 88 |
key << "#{input[:filter]}\0" |
| 89 |
key << "#{input[:offset]}\0" |
| 90 |
key << "#{input[:limit]}\0" |
| 91 |
key << "#{input[:output_columns]}\0" |
| 92 |
key << "#{input[:use_range_index]}\0" |
| 93 |
key << "#{input[:post_filter]}\0" |
| 94 |
key << "#{input[:sort_keys]}\0" |
| 95 |
key << "#{context.command_version}\0" |
| 96 |
key << "#{context.output_type}\0" |
| 97 |
dynamic_columns = DynamicColumns.parse("[logical_range_filter]", |
| 98 |
input) |
| 99 |
key << dynamic_columns.cache_key |
| 100 |
key |
| 101 |
end |
| 102 |
|
| 103 |
class ExecuteContext < StreamExecuteContext |
| 104 |
include KeysParsable |
| 105 |
|
| 106 |
attr_reader :use_range_index |
| 107 |
attr_reader :offset |
| 108 |
attr_reader :limit |
| 109 |
attr_reader :sort_keys |
| 110 |
attr_accessor :current_offset |
| 111 |
attr_accessor :current_limit |
| 112 |
attr_reader :threshold |
| 113 |
attr_reader :large_shard_threshold |
| 114 |
attr_reader :result_sets |
| 115 |
def initialize(input) |
| 116 |
super("logical_range_filter", input) |
| 117 |
@use_range_index = parse_use_range_index(@input[:use_range_index]) |
| 118 |
@offset = (@input[:offset] || 0).to_i |
| 119 |
@limit = (@input[:limit] || 10).to_i |
| 120 |
@sort_keys = parse_keys(@input[:sort_keys]) |
| 121 |
|
| 122 |
@current_offset = @offset |
| 123 |
@current_limit = @limit |
| 124 |
|
| 125 |
@result_sets = [] |
| 126 |
|
| 127 |
@threshold = compute_threshold |
| 128 |
@large_shard_threshold = compute_large_shard_threshold |
| 129 |
end |
| 130 |
|
| 131 |
def close |
| 132 |
super |
| 133 |
@result_sets.clear |
| 134 |
end |
| 135 |
|
| 136 |
def consume_result_sets |
| 137 |
while (result_set = @result_sets.shift) |
| 138 |
yield(result_set) |
| 139 |
end |
| 140 |
end |
| 141 |
|
| 142 |
private |
| 143 |
def parse_use_range_index(use_range_index) |
| 144 |
case use_range_index |
| 145 |
when "yes" |
| 146 |
true |
| 147 |
when "no" |
| 148 |
false |
| 149 |
else |
| 150 |
nil |
| 151 |
end |
| 152 |
end |
| 153 |
|
| 154 |
def compute_threshold |
| 155 |
threshold_env = ENV["GRN_LOGICAL_RANGE_FILTER_THRESHOLD"] |
| 156 |
default_threshold = 0.3 |
| 157 |
(threshold_env || default_threshold).to_f |
| 158 |
end |
| 159 |
|
| 160 |
def compute_large_shard_threshold |
| 161 |
threshold_env = ENV["GRN_LOGICAL_RANGE_FILTER_LARGE_SHARD_THRESHOLD"] |
| 162 |
default_threshold = "10_000" |
| 163 |
(threshold_env || default_threshold).to_i(10) |
| 164 |
end |
| 165 |
end |
| 166 |
|
| 167 |
class Executor < StreamExecutor |
| 168 |
def initialize(context) |
| 169 |
super(context, ShardExecutor) |
| 170 |
end |
| 171 |
|
| 172 |
def execute |
| 173 |
have_shard = false |
| 174 |
have_result_set = false |
| 175 |
each_shard_executor do |shard_executor| |
| 176 |
have_shard = true |
| 177 |
shard_executor.execute |
| 178 |
@context.consume_result_sets do |result_set| |
| 179 |
have_result_set = true |
| 180 |
yield(result_set) |
| 181 |
end |
| 182 |
break if @context.current_limit.zero? |
| 183 |
end |
| 184 |
unless have_shard |
| 185 |
enumerator = @context.enumerator |
| 186 |
message = |
| 187 |
"[logical_range_filter] no shard exists: " + |
| 188 |
"logical_table: <#{enumerator.logical_table}>: " + |
| 189 |
"shard_key: <#{enumerator.shard_key_name}>" |
| 190 |
raise InvalidArgument, message |
| 191 |
end |
| 192 |
return if have_result_set |
| 193 |
|
| 194 |
each_shard_executor do |shard_executor| |
| 195 |
shard = shard_executor.shard |
| 196 |
result_set = HashTable.create(:flags => ObjectFlags::WITH_SUBREC, |
| 197 |
:key_type => shard.table) |
| 198 |
@context.push |
| 199 |
@context.temporary_tables << result_set |
| 200 |
targets = [[result_set]] |
| 201 |
@context.dynamic_columns.apply_initial(targets) |
| 202 |
@context.dynamic_columns.apply_filtered(targets) |
| 203 |
yield(result_set) |
| 204 |
@context.shift |
| 205 |
break |
| 206 |
end |
| 207 |
end |
| 208 |
end |
| 209 |
|
| 210 |
class ShardExecutor < StreamShardExecutor |
| 211 |
include QueryLoggable |
| 212 |
|
| 213 |
def initialize(context, shard, shard_range) |
| 214 |
super("logical_range_filter", context, shard, shard_range) |
| 215 |
@result_sets = context.result_sets |
| 216 |
end |
| 217 |
|
| 218 |
def execute |
| 219 |
ensure_filtered |
| 220 |
|
| 221 |
return if @filtered_result_sets.empty? |
| 222 |
|
| 223 |
each_result_set do |result_set| |
| 224 |
sort_result_set(result_set) |
| 225 |
return if @context.current_limit.zero? |
| 226 |
end |
| 227 |
end |
| 228 |
|
| 229 |
def compute_limit(n_source_records) |
| 230 |
current_limit = @context.current_limit |
| 231 |
if current_limit < 0 |
| 232 |
n_source_records |
| 233 |
else |
| 234 |
current_limit |
| 235 |
end |
| 236 |
end |
| 237 |
|
| 238 |
def find_range_index |
| 239 |
use_range_index_parameter_message = |
| 240 |
"force by use_range_index parameter" |
| 241 |
case @context.use_range_index |
| 242 |
when true |
| 243 |
return find_range_index_raw(use_range_index_parameter_message, |
| 244 |
__LINE__, __method__) |
| 245 |
when false |
| 246 |
log_use_range_index(false, |
| 247 |
use_range_index_parameter_message, |
| 248 |
__LINE__, __method__) |
| 249 |
return nil |
| 250 |
end |
| 251 |
|
| 252 |
range_index_logical_parameter_message = |
| 253 |
"force by range_index logical parameter" |
| 254 |
case Parameters.range_index |
| 255 |
when :always |
| 256 |
return find_range_index_raw(range_index_logical_parameter_message, |
| 257 |
__LINE__, __method__) |
| 258 |
when :never |
| 259 |
log_use_range_index(false, |
| 260 |
range_index_logical_parameter_message, |
| 261 |
__LINE__, __method__) |
| 262 |
return nil |
| 263 |
end |
| 264 |
|
| 265 |
unless @context.dynamic_columns.empty? |
| 266 |
reason = "dynamic columns are used" |
| 267 |
log_use_range_index(false, reason, __LINE__, __method__) |
| 268 |
return nil |
| 269 |
end |
| 270 |
|
| 271 |
unless @context.post_filter.nil? |
| 272 |
reason = "post_filter is used" |
| 273 |
log_use_range_index(false, reason, __LINE__, __method__) |
| 274 |
return nil |
| 275 |
end |
| 276 |
|
| 277 |
unless @context.sort_keys.empty? |
| 278 |
reason = "sort_keys is used" |
| 279 |
log_use_range_index(false, reason, __LINE__, __method__) |
| 280 |
return nil |
| 281 |
end |
| 282 |
|
| 283 |
current_limit = @context.current_limit |
| 284 |
if current_limit < 0 |
| 285 |
reason = "limit is negative: <#{current_limit}>" |
| 286 |
log_use_range_index(false, reason, __LINE__, __method__) |
| 287 |
return nil |
| 288 |
end |
| 289 |
|
| 290 |
required_n_records = @context.current_offset + current_limit |
| 291 |
max_n_records = @shard.table.size |
| 292 |
is_large_shard = (max_n_records > @context.large_shard_threshold) |
| 293 |
if is_large_shard and max_n_records <= required_n_records |
| 294 |
reason = "the number of required records (#{required_n_records}) " |
| 295 |
reason << ">= " |
| 296 |
reason << "the number of records in shard (#{max_n_records})" |
| 297 |
log_use_range_index(false, reason,__LINE__, __method__) |
| 298 |
return nil |
| 299 |
end |
| 300 |
|
| 301 |
threshold = @context.threshold |
| 302 |
if threshold <= 0.0 |
| 303 |
reason = "threshold is negative: <#{threshold}>" |
| 304 |
return find_range_index_raw(reason, __LINE__, __method__) |
| 305 |
end |
| 306 |
if threshold >= 1.0 |
| 307 |
reason = "threshold (#{threshold}) >= 1.0" |
| 308 |
log_use_range_index(false, reason, __LINE__, __method__) |
| 309 |
return nil |
| 310 |
end |
| 311 |
|
| 312 |
table = @shard.table |
| 313 |
estimated_n_records = 0 |
| 314 |
case @cover_type |
| 315 |
when :all |
| 316 |
if @filter |
| 317 |
create_expression(table) do |expression| |
| 318 |
@expression_builder.build_all(expression) |
| 319 |
unless range_index_available_expression?(expression, |
| 320 |
__LINE__, __method__) |
| 321 |
return nil |
| 322 |
end |
| 323 |
estimated_n_records = expression.estimate_size(table) |
| 324 |
end |
| 325 |
else |
| 326 |
estimated_n_records = max_n_records |
| 327 |
end |
| 328 |
when :partial_min |
| 329 |
create_expression(table) do |expression| |
| 330 |
@expression_builder.build_partial_min(expression) |
| 331 |
unless range_index_available_expression?(expression, |
| 332 |
__LINE__, __method__) |
| 333 |
return nil |
| 334 |
end |
| 335 |
estimated_n_records = expression.estimate_size(table) |
| 336 |
end |
| 337 |
when :partial_max |
| 338 |
create_expression(table) do |expression| |
| 339 |
@expression_builder.build_partial_max(expression) |
| 340 |
unless range_index_available_expression?(expression, |
| 341 |
__LINE__, __method__) |
| 342 |
return nil |
| 343 |
end |
| 344 |
estimated_n_records = expression.estimate_size(table) |
| 345 |
end |
| 346 |
when :partial_min_and_max |
| 347 |
create_expression(table) do |expression| |
| 348 |
@expression_builder.build_partial_min_and_max(expression) |
| 349 |
unless range_index_available_expression?(expression, |
| 350 |
__LINE__, __method__) |
| 351 |
return nil |
| 352 |
end |
| 353 |
estimated_n_records = expression.estimate_size(table) |
| 354 |
end |
| 355 |
end |
| 356 |
|
| 357 |
if is_large_shard and estimated_n_records <= required_n_records |
| 358 |
reason = "the number of required records (#{required_n_records}) " |
| 359 |
reason << ">= " |
| 360 |
reason << "the number of estimated records (#{estimated_n_records})" |
| 361 |
log_use_range_index(false, reason, __LINE__, __method__) |
| 362 |
return nil |
| 363 |
end |
| 364 |
|
| 365 |
max_n_unmatched_records = |
| 366 |
compute_max_n_unmatched_records(max_n_records, |
| 367 |
required_n_records) |
| 368 |
if estimated_n_records >= max_n_unmatched_records |
| 369 |
reason = "the max number of unmatched records (#{max_n_unmatched_records}) " |
| 370 |
reason << "<= " |
| 371 |
reason << "the number of estimated records (#{estimated_n_records})" |
| 372 |
return find_range_index_raw(reason, __LINE__, __method__) |
| 373 |
end |
| 374 |
|
| 375 |
hit_ratio = estimated_n_records / max_n_records.to_f |
| 376 |
use_range_index_by_hit_ratio = (hit_ratio >= threshold) |
| 377 |
if use_range_index_by_hit_ratio |
| 378 |
relation = ">=" |
| 379 |
else |
| 380 |
relation = "<" |
| 381 |
end |
| 382 |
reason = "hit ratio " |
| 383 |
reason << "(#{hit_ratio}=#{estimated_n_records}/#{max_n_records}) " |
| 384 |
reason << "#{relation} threshold (#{threshold})" |
| 385 |
if use_range_index_by_hit_ratio |
| 386 |
find_range_index_raw(reason, __LINE__, __method__) |
| 387 |
else |
| 388 |
log_use_range_index(false, reason, __LINE__, __method__) |
| 389 |
nil |
| 390 |
end |
| 391 |
end |
| 392 |
|
| 393 |
def range_index_available_expression?(expression, line, method_name) |
| 394 |
nested_reference_vector_column_accessor = |
| 395 |
find_nested_reference_vector_column_accessor(expression) |
| 396 |
if nested_reference_vector_column_accessor |
| 397 |
reason = "nested reference vector column accessor can't be used: " |
| 398 |
reason << "<#{nested_reference_vector_column_accessor.name}>" |
| 399 |
log_use_range_index(false, reason, line, method_name) |
| 400 |
return false |
| 401 |
end |
| 402 |
|
| 403 |
selector_only_procedure = find_selector_only_procedure(expression) |
| 404 |
if selector_only_procedure |
| 405 |
reason = "selector only procedure can't be used: " |
| 406 |
reason << "<#{selector_only_procedure.name}>" |
| 407 |
log_use_range_index(false, reason, line, method_name) |
| 408 |
return false |
| 409 |
end |
| 410 |
|
| 411 |
true |
| 412 |
end |
| 413 |
|
| 414 |
def find_nested_reference_vector_column_accessor(expression) |
| 415 |
expression.codes.each do |code| |
| 416 |
value = code.value |
| 417 |
next unless value.is_a?(Accessor) |
| 418 |
|
| 419 |
sub_accessor = value |
| 420 |
while sub_accessor.have_next? |
| 421 |
object = sub_accessor.object |
| 422 |
return value if object.is_a?(Column) and object.vector? |
| 423 |
sub_accessor = sub_accessor.next |
| 424 |
end |
| 425 |
end |
| 426 |
nil |
| 427 |
end |
| 428 |
|
| 429 |
def find_selector_only_procedure(expression) |
| 430 |
expression.codes.each do |code| |
| 431 |
value = code.value |
| 432 |
return value if value.is_a?(Procedure) and value.selector_only? |
| 433 |
end |
| 434 |
nil |
| 435 |
end |
| 436 |
|
| 437 |
def execute_filter(range_index) |
| 438 |
case @cover_type |
| 439 |
when :all |
| 440 |
filter_shard_all(range_index) |
| 441 |
when :partial_min |
| 442 |
if range_index |
| 443 |
filter_by_range(range_index, |
| 444 |
@target_range.min, @target_range.min_border, |
| 445 |
nil, nil) |
| 446 |
else |
| 447 |
filter_table do |expression| |
| 448 |
@expression_builder.build_partial_min(expression) |
| 449 |
end |
| 450 |
end |
| 451 |
when :partial_max |
| 452 |
if range_index |
| 453 |
filter_by_range(range_index, |
| 454 |
nil, nil, |
| 455 |
@target_range.max, @target_range.max_border) |
| 456 |
else |
| 457 |
filter_table do |expression| |
| 458 |
@expression_builder.build_partial_max(expression) |
| 459 |
end |
| 460 |
end |
| 461 |
when :partial_min_and_max |
| 462 |
if range_index |
| 463 |
filter_by_range(range_index, |
| 464 |
@target_range.min, @target_range.min_border, |
| 465 |
@target_range.max, @target_range.max_border) |
| 466 |
else |
| 467 |
filter_table do |expression| |
| 468 |
@expression_builder.build_partial_min_and_max(expression) |
| 469 |
end |
| 470 |
end |
| 471 |
end |
| 472 |
end |
| 473 |
|
| 474 |
def filter_shard_all(range_index) |
| 475 |
table = @target_table |
| 476 |
if @filter.nil? |
| 477 |
if @post_filter.nil? and table.size <= @context.current_offset |
| 478 |
@context.current_offset -= table.size |
| 479 |
return |
| 480 |
end |
| 481 |
if range_index |
| 482 |
filter_by_range(range_index, |
| 483 |
nil, nil, |
| 484 |
nil, nil) |
| 485 |
else |
| 486 |
add_filtered_result_set(table) |
| 487 |
end |
| 488 |
else |
| 489 |
if range_index |
| 490 |
filter_by_range(range_index, |
| 491 |
nil, nil, |
| 492 |
nil, nil) |
| 493 |
else |
| 494 |
filter_table do |expression| |
| 495 |
@expression_builder.build_all(expression) |
| 496 |
end |
| 497 |
end |
| 498 |
end |
| 499 |
end |
| 500 |
|
| 501 |
def filter_by_range(range_index, min, min_border, max, max_border) |
| 502 |
lexicon = range_index.domain |
| 503 |
@context.referred_objects << lexicon |
| 504 |
data_table = range_index.range |
| 505 |
@context.referred_objects << data_table |
| 506 |
flags = build_range_search_flags(min_border, max_border) |
| 507 |
|
| 508 |
result_set = HashTable.create(:flags => ObjectFlags::WITH_SUBREC, |
| 509 |
:key_type => data_table) |
| 510 |
@temporary_tables << result_set |
| 511 |
n_matched_records = 0 |
| 512 |
TableCursor.open(lexicon, |
| 513 |
:min => min, |
| 514 |
:max => max, |
| 515 |
:flags => flags) do |table_cursor| |
| 516 |
options = { |
| 517 |
:offset => @context.current_offset, |
| 518 |
:limit => compute_limit(data_table.size), |
| 519 |
} |
| 520 |
max_n_unmatched_records = |
| 521 |
compute_max_n_unmatched_records(data_table.size, |
| 522 |
options[:limit]) |
| 523 |
options[:max_n_unmatched_records] = max_n_unmatched_records |
| 524 |
if @filter |
| 525 |
create_expression(data_table) do |expression| |
| 526 |
expression.parse(@filter) |
| 527 |
options[:expression] = expression |
| 528 |
IndexCursor.open(table_cursor, range_index) do |index_cursor| |
| 529 |
n_matched_records = index_cursor.select(result_set, options) |
| 530 |
end |
| 531 |
end |
| 532 |
|
| 533 |
query_logger.log(:size, ":", |
| 534 |
"filter(#{n_matched_records})" + |
| 535 |
"[#{@shard.table_name}]: #{@filter}") |
| 536 |
else |
| 537 |
IndexCursor.open(table_cursor, range_index) do |index_cursor| |
| 538 |
n_matched_records = index_cursor.select(result_set, options) |
| 539 |
end |
| 540 |
|
| 541 |
query_logger.log(:size, ":", |
| 542 |
"filter(#{n_matched_records})" + |
| 543 |
"[#{@shard.table_name}]") |
| 544 |
end |
| 545 |
if n_matched_records == -1 |
| 546 |
fallback_message = |
| 547 |
"fallback because there are too much unmatched records: " |
| 548 |
fallback_message << "<#{max_n_unmatched_records}>" |
| 549 |
log_use_range_index(false, |
| 550 |
fallback_message, |
| 551 |
__LINE__, __method__) |
| 552 |
execute_filter(nil) |
| 553 |
return |
| 554 |
end |
| 555 |
end |
| 556 |
|
| 557 |
if n_matched_records <= @context.current_offset |
| 558 |
@context.current_offset -= n_matched_records |
| 559 |
return |
| 560 |
end |
| 561 |
|
| 562 |
if @context.current_offset > 0 |
| 563 |
@context.current_offset = 0 |
| 564 |
end |
| 565 |
if @context.current_limit > 0 |
| 566 |
@context.current_limit -= result_set.size |
| 567 |
end |
| 568 |
@result_sets << result_set |
| 569 |
end |
| 570 |
|
| 571 |
def build_range_search_flags(min_border, max_border) |
| 572 |
flags = TableCursorFlags::BY_KEY |
| 573 |
case @context.order |
| 574 |
when :ascending |
| 575 |
flags |= TableCursorFlags::ASCENDING |
| 576 |
when :descending |
| 577 |
flags |= TableCursorFlags::DESCENDING |
| 578 |
end |
| 579 |
case min_border |
| 580 |
when :include |
| 581 |
flags |= TableCursorFlags::GE |
| 582 |
when :exclude |
| 583 |
flags |= TableCursorFlags::GT |
| 584 |
end |
| 585 |
case max_border |
| 586 |
when :include |
| 587 |
flags |= TableCursorFlags::LE |
| 588 |
when :exclude |
| 589 |
flags |= TableCursorFlags::LT |
| 590 |
end |
| 591 |
flags |
| 592 |
end |
| 593 |
|
| 594 |
def compute_max_n_unmatched_records(max_n_records, limit) |
| 595 |
return limit if max_n_records.zero? |
| 596 |
|
| 597 |
scale = Math.log(max_n_records) ** 2 |
| 598 |
max_n_unmatched_records = limit * 2 * scale |
| 599 |
max_n_sample_records = max_n_records |
| 600 |
if max_n_sample_records > 10000 |
| 601 |
max_n_sample_records = max_n_sample_records / scale |
| 602 |
end |
| 603 |
if max_n_unmatched_records > max_n_sample_records |
| 604 |
max_n_unmatched_records = max_n_sample_records |
| 605 |
end |
| 606 |
max_n_unmatched_records.ceil |
| 607 |
end |
| 608 |
|
| 609 |
def sort_result_set(result_set) |
| 610 |
if result_set.size <= @context.current_offset |
| 611 |
@context.current_offset -= result_set.size |
| 612 |
return |
| 613 |
end |
| 614 |
|
| 615 |
if @context.sort_keys.empty? |
| 616 |
sort_keys = [ |
| 617 |
{ |
| 618 |
:key => @context.enumerator.shard_key_name, |
| 619 |
:order => @context.order, |
| 620 |
}, |
| 621 |
] |
| 622 |
else |
| 623 |
sort_keys = @context.sort_keys.collect do |sort_key| |
| 624 |
if sort_key.start_with?("-") |
| 625 |
key = sort_key[1..-1] |
| 626 |
order = :descending |
| 627 |
else |
| 628 |
key = sort_key |
| 629 |
order = :ascending |
| 630 |
end |
| 631 |
{:key => key, :order => order} |
| 632 |
end |
| 633 |
end |
| 634 |
if @context.current_limit > 0 |
| 635 |
limit = @context.current_limit |
| 636 |
else |
| 637 |
limit = result_set.size |
| 638 |
end |
| 639 |
sorted_result_set = result_set.sort(sort_keys, |
| 640 |
:offset => @context.current_offset, |
| 641 |
:limit => limit) |
| 642 |
@temporary_tables << sorted_result_set |
| 643 |
@result_sets << sorted_result_set |
| 644 |
if @context.current_offset > 0 |
| 645 |
@context.current_offset = 0 |
| 646 |
end |
| 647 |
if @context.current_limit > 0 |
| 648 |
@context.current_limit -= sorted_result_set.size |
| 649 |
end |
| 650 |
unparsed_sort_keys = sort_keys.collect do |sort_key| |
| 651 |
key = sort_key[:key] |
| 652 |
key = "-#{key}" if sort_key[:order] == :descending |
| 653 |
key |
| 654 |
end |
| 655 |
query_logger.log(:size, |
| 656 |
":", |
| 657 |
"sort(#{sorted_result_set.size})" + |
| 658 |
"[#{@shard.table_name}]: " + |
| 659 |
unparsed_sort_keys.join(",")) |
| 660 |
end |
| 661 |
end |
| 662 |
end |
| 663 |
end |
| 664 |
end |