| 1 |
module Groonga |
| 2 |
module Sharding |
| 3 |
class DynamicColumns |
| 4 |
include Enumerable |
| 5 |
|
| 6 |
class << self |
| 7 |
def parse(tag, input) |
| 8 |
initial_contexts = {} |
| 9 |
filtered_contexts = {} |
| 10 |
output_contexts = {} |
| 11 |
labeled_arguments = LabeledArguments.new(input, /columns?/) |
| 12 |
labeled_arguments.each do |label, arguments| |
| 13 |
contexts = nil |
| 14 |
case arguments["stage"] |
| 15 |
when "initial" |
| 16 |
contexts = initial_contexts |
| 17 |
when "filtered" |
| 18 |
contexts = filtered_contexts |
| 19 |
when "output" |
| 20 |
contexts = output_contexts |
| 21 |
else |
| 22 |
next |
| 23 |
end |
| 24 |
contexts[label] = DynamicColumnExecuteContext.new(tag, |
| 25 |
label, |
| 26 |
arguments) |
| 27 |
end |
| 28 |
|
| 29 |
new(DynamicColumnExecuteContexts.new(initial_contexts), |
| 30 |
DynamicColumnExecuteContexts.new(filtered_contexts), |
| 31 |
DynamicColumnExecuteContexts.new(output_contexts)) |
| 32 |
end |
| 33 |
end |
| 34 |
|
| 35 |
def initialize(initial_contexts, |
| 36 |
filtered_contexts, |
| 37 |
output_contexts) |
| 38 |
@initial_contexts = initial_contexts |
| 39 |
@filtered_contexts = filtered_contexts |
| 40 |
@output_contexts = output_contexts |
| 41 |
end |
| 42 |
|
| 43 |
def have_initial? |
| 44 |
not @initial_contexts.empty? |
| 45 |
end |
| 46 |
|
| 47 |
def have_filtered? |
| 48 |
not @filtered_contexts.empty? |
| 49 |
end |
| 50 |
|
| 51 |
def have_output? |
| 52 |
not @output_contexts.empty? |
| 53 |
end |
| 54 |
|
| 55 |
def have_window_function? |
| 56 |
any? do |context| |
| 57 |
context.window_function? |
| 58 |
end |
| 59 |
end |
| 60 |
|
| 61 |
def each_initial(&block) |
| 62 |
@initial_contexts.tsort_each(&block) |
| 63 |
end |
| 64 |
|
| 65 |
def each_filtered(&block) |
| 66 |
@filtered_contexts.tsort_each(&block) |
| 67 |
end |
| 68 |
|
| 69 |
def each_output(&block) |
| 70 |
@output_contexts.tsort_each(&block) |
| 71 |
end |
| 72 |
|
| 73 |
def each(&block) |
| 74 |
each_initial(&block) |
| 75 |
each_filtered(&block) |
| 76 |
each_output(&block) |
| 77 |
end |
| 78 |
|
| 79 |
def apply_initial(targets, options={}) |
| 80 |
apply(@initial_contexts, targets, options) |
| 81 |
end |
| 82 |
|
| 83 |
def apply_filtered(targets, options={}) |
| 84 |
apply(@filtered_contexts, targets, options) |
| 85 |
end |
| 86 |
|
| 87 |
def apply_output(targets, options={}) |
| 88 |
apply(@output_contexts, targets, options) |
| 89 |
end |
| 90 |
|
| 91 |
def empty? |
| 92 |
@initial_contexts.empty? and |
| 93 |
@filtered_contexts.empty? and |
| 94 |
@output_contexts.empty? |
| 95 |
end |
| 96 |
|
| 97 |
def cache_key |
| 98 |
key = "" |
| 99 |
[ |
| 100 |
@initial_contexts, |
| 101 |
@filtered_contexts, |
| 102 |
@output_contexts, |
| 103 |
].each do |contexts| |
| 104 |
contexts.sort_by(&:label).each do |context| |
| 105 |
key << "#{context.label}\0" |
| 106 |
key << "#{context.stage}\0" |
| 107 |
key << "#{context.type.name}\0" |
| 108 |
key << "#{context.flags}\0" |
| 109 |
key << "#{context.value}\0" |
| 110 |
key << "#{context.window_sort_keys.join(',')}\0" |
| 111 |
key << "#{context.window_group_keys.join(',')}\0" |
| 112 |
end |
| 113 |
end |
| 114 |
key |
| 115 |
end |
| 116 |
|
| 117 |
private |
| 118 |
def apply(contexts, targets, options) |
| 119 |
window_function_contexts = [] |
| 120 |
normal_contexts = [] |
| 121 |
contexts.tsort_each do |context| |
| 122 |
if context.window_function? |
| 123 |
window_function_contexts << context |
| 124 |
else |
| 125 |
normal_contexts << context |
| 126 |
end |
| 127 |
end |
| 128 |
|
| 129 |
if options.fetch(:normal, true) |
| 130 |
targets.each do |result_set, target_options| |
| 131 |
normal_contexts.each do |context| |
| 132 |
global_options = nil |
| 133 |
if options and options[:query_log_prefix] |
| 134 |
global_options = { |
| 135 |
query_log_prefix: options[:query_log_prefix], |
| 136 |
} |
| 137 |
end |
| 138 |
if target_options and global_options |
| 139 |
target_options = global_options.merge(target_options) |
| 140 |
elsif global_options |
| 141 |
target_options = global_options |
| 142 |
end |
| 143 |
context.apply(result_set, target_options) |
| 144 |
end |
| 145 |
end |
| 146 |
end |
| 147 |
if options.fetch(:window_function, true) |
| 148 |
window_function_contexts.each do |context| |
| 149 |
context.apply_window_function(targets, options) |
| 150 |
end |
| 151 |
end |
| 152 |
end |
| 153 |
end |
| 154 |
|
| 155 |
class DynamicColumnExecuteContexts |
| 156 |
include Enumerable |
| 157 |
include TSort |
| 158 |
|
| 159 |
def initialize(contexts) |
| 160 |
@contexts = contexts |
| 161 |
@dependencies = {} |
| 162 |
@contexts.each do |label, context| |
| 163 |
dependencies = [] |
| 164 |
|
| 165 |
context.value.split(/[ \(\),]+/).each do |component| |
| 166 |
depended_context = @contexts[component] |
| 167 |
dependencies << depended_context if depended_context |
| 168 |
end |
| 169 |
context.window_sort_keys.each do |key| |
| 170 |
depended_context = @contexts[key.gsub(/\A-/, "")] |
| 171 |
dependencies << depended_context if depended_context |
| 172 |
end |
| 173 |
context.window_group_keys.each do |key| |
| 174 |
depended_context = @contexts[key] |
| 175 |
dependencies << depended_context if depended_context |
| 176 |
end |
| 177 |
@dependencies[label] = dependencies |
| 178 |
end |
| 179 |
end |
| 180 |
|
| 181 |
def empty? |
| 182 |
@contexts.empty? |
| 183 |
end |
| 184 |
|
| 185 |
def each(&block) |
| 186 |
@contexts.each_value(&block) |
| 187 |
end |
| 188 |
|
| 189 |
def tsort_each_node(&block) |
| 190 |
@contexts.each_value(&block) |
| 191 |
end |
| 192 |
|
| 193 |
def tsort_each_child(context, &block) |
| 194 |
@dependencies[context.label].each(&block) |
| 195 |
end |
| 196 |
end |
| 197 |
|
| 198 |
class DynamicColumnExecuteContext |
| 199 |
include QueryLoggable |
| 200 |
include KeysParsable |
| 201 |
|
| 202 |
attr_reader :label |
| 203 |
attr_reader :stage |
| 204 |
attr_reader :type |
| 205 |
attr_reader :flags |
| 206 |
attr_reader :value |
| 207 |
attr_reader :window_sort_keys |
| 208 |
attr_reader :window_group_keys |
| 209 |
def initialize(tag, label, arguments) |
| 210 |
@tag = tag |
| 211 |
@label = label |
| 212 |
@stage = arguments["stage"] |
| 213 |
@type = parse_type(arguments["type"]) |
| 214 |
@flags = parse_flags(arguments["flags"] || "COLUMN_SCALAR") |
| 215 |
@value = arguments["value"] |
| 216 |
@window_sort_keys = parse_keys(arguments["window.sort_keys"]) |
| 217 |
@window_group_keys = parse_keys(arguments["window.group_keys"]) |
| 218 |
end |
| 219 |
|
| 220 |
def window_function? |
| 221 |
(not @window_sort_keys.empty?) or |
| 222 |
(not @window_group_keys.empty?) |
| 223 |
end |
| 224 |
|
| 225 |
def apply(table, options=nil) |
| 226 |
return if table.find_column(@label) |
| 227 |
column = table.create_column(@label, @flags, @type) |
| 228 |
|
| 229 |
query_log_prefix = nil |
| 230 |
query_log_prefix = options[:query_log_prefix] if options |
| 231 |
unless table.empty? |
| 232 |
condition = nil |
| 233 |
condition = options[:condition] if options |
| 234 |
expression = Expression.create(table) |
| 235 |
begin |
| 236 |
expression.parse(@value) |
| 237 |
expression.condition = condition if condition |
| 238 |
table.apply_expression(column, expression) |
| 239 |
ensure |
| 240 |
expression.close |
| 241 |
end |
| 242 |
end |
| 243 |
query_logger.log(:size, ":", |
| 244 |
"#{query_log_prefix}columns[#{@label}](#{table.size})") |
| 245 |
end |
| 246 |
|
| 247 |
def apply_window_function(targets, options=nil) |
| 248 |
executor = WindowFunctionExecutor.new |
| 249 |
n_records = 0 |
| 250 |
begin |
| 251 |
executor.source = @value |
| 252 |
executor.sort_keys = @window_sort_keys.join(", ") |
| 253 |
executor.group_keys = @window_group_keys.join(", ") |
| 254 |
executor.output_column_name = @label |
| 255 |
targets.each do |table, target_options| |
| 256 |
is_context_table = context_target?(target_options) |
| 257 |
column = table.find_column(@label) |
| 258 |
column ||= table.create_column(@label, @flags, @type) |
| 259 |
next if table.empty? |
| 260 |
if is_context_table |
| 261 |
executor.add_context_table(table) |
| 262 |
else |
| 263 |
executor.add_table(table) |
| 264 |
n_records += table.size |
| 265 |
end |
| 266 |
end |
| 267 |
executor.execute |
| 268 |
ensure |
| 269 |
executor.close |
| 270 |
end |
| 271 |
query_log_prefix = nil |
| 272 |
query_log_prefix = options[:query_log_prefix] if options |
| 273 |
query_logger.log(:size, ":", |
| 274 |
"#{query_log_prefix}columns[#{@label}](#{n_records})") |
| 275 |
end |
| 276 |
|
| 277 |
private |
| 278 |
def parse_type(type_raw) |
| 279 |
return nil if type_raw.nil? |
| 280 |
|
| 281 |
type = Context.instance[type_raw] |
| 282 |
if type.nil? |
| 283 |
message = "#{error_message_tag} unknown type: <#{type_raw}>" |
| 284 |
raise InvalidArgument, message |
| 285 |
end |
| 286 |
|
| 287 |
case type |
| 288 |
when Type, Table |
| 289 |
type |
| 290 |
else |
| 291 |
message = "#{error_message_tag} invalid type: #{type.grn_inspect}" |
| 292 |
raise InvalidArgument, message |
| 293 |
end |
| 294 |
end |
| 295 |
|
| 296 |
def parse_flags(flags_raw) |
| 297 |
Column.parse_flags(error_message_tag, flags_raw) |
| 298 |
end |
| 299 |
|
| 300 |
def context_target?(options) |
| 301 |
options and options[:context] |
| 302 |
end |
| 303 |
|
| 304 |
def error_message_tag |
| 305 |
"#{@tag}[columns][#{@stage}][#{@label}]" |
| 306 |
end |
| 307 |
end |
| 308 |
end |
| 309 |
end |