1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 # Mixin module providing a method to convert filters into a list of SQL
6 # fragments suitable to be fed to ActiveRecord #where.
18 # +filters+ array of conditions, each being [column, operator, operand]
19 # +model_class+ subclass of ActiveRecord being filtered
22 # Hash with the following keys:
23 # :cond_out array of SQL fragments for each filter expression
24 # :param_out array of values for parameter substitution in cond_out
25 # :joins array of joins: either [] or ["JOIN containers ON ..."]
26 def record_filters filters, model_class
31 model_table_name = model_class.table_name
32 filters.each do |filter|
33 attrs_in, operator, operand = filter
35 raise ArgumentError.new("Full text search operator is no longer supported")
38 attrs = model_class.searchable_columns(operator)
39 elsif attrs_in.is_a? Array
44 if !filter.is_a? Array
45 raise ArgumentError.new("Invalid element in filters array: #{filter.inspect} is not an array")
46 elsif !operator.is_a? String
47 raise ArgumentError.new("Invalid operator '#{operator}' (#{operator.class}) in filter")
50 operator = operator.downcase
53 if attrs_in == 'any' && (operator == 'ilike' || operator == 'like') && (operand.is_a? String) && operand.match('^[%].*[%]$')
54 # Trigram index search
55 cond_out << model_class.full_text_trgm + " #{operator} ?"
57 # Skip the generic per-column operator loop below
62 subproperty = attr.split(".", 2)
64 if subproperty.length == 2 && subproperty[0] == 'container' && model_table_name == "container_requests"
65 # attr is "tablename.colname" -- e.g., ["container.state", "=", "Complete"]
66 joins = ["JOIN containers ON container_requests.container_uuid = containers.uuid"]
67 attr_model_class = Container
68 attr_table_name = "containers"
69 subproperty = subproperty[1].split(".", 2)
71 attr_model_class = model_class
72 attr_table_name = model_table_name
76 proppath = subproperty[1]
77 col = attr_model_class.columns.select { |c| c.name == attr }.first
80 if col.nil? or col.type != :jsonb
81 raise ArgumentError.new("Invalid attribute '#{attr}' for subproperty filter")
84 if proppath[0] == "<" and proppath[-1] == ">"
85 proppath = proppath[1..-2]
91 not_in = if operator == "!=" then "NOT " else "" end
92 cond_out << "#{not_in}(#{attr_table_name}.#{attr} @> ?::jsonb)"
93 param_out << SafeJSON.dump({proppath => operand})
95 if operand.is_a? Array
97 cond_out << "#{attr_table_name}.#{attr} @> ?::jsonb"
98 param_out << SafeJSON.dump({proppath => opr})
101 raise ArgumentError.new("Invalid operand type '#{operand.class}' "\
102 "for '#{operator}' operator in filters")
104 when '<', '<=', '>', '>='
105 cond_out << "#{attr_table_name}.#{attr}->? #{operator} ?::jsonb"
106 param_out << proppath
107 param_out << SafeJSON.dump(operand)
109 cond_out << "#{attr_table_name}.#{attr}->>? #{operator} ?"
110 param_out << proppath
113 if operand.is_a? Array
114 cond_out << "#{attr_table_name}.#{attr}->>? NOT IN (?) OR #{attr_table_name}.#{attr}->>? IS NULL"
115 param_out << proppath
117 param_out << proppath
119 raise ArgumentError.new("Invalid operand type '#{operand.class}' "\
120 "for '#{operator}' operator in filters")
124 cond_out << "jsonb_exists_inline_op(#{attr_table_name}.#{attr}, ?)"
125 elsif operand == false
126 cond_out << "(NOT jsonb_exists_inline_op(#{attr_table_name}.#{attr}, ?)) OR #{attr_table_name}.#{attr} is NULL"
128 raise ArgumentError.new("Invalid operand '#{operand}' for '#{operator}' must be true or false")
130 param_out << proppath
132 cond_out << "#{attr_table_name}.#{attr} @> ?::jsonb OR #{attr_table_name}.#{attr} @> ?::jsonb"
133 param_out << SafeJSON.dump({proppath => operand})
134 param_out << SafeJSON.dump({proppath => [operand]})
136 raise ArgumentError.new("Invalid operator for subproperty search '#{operator}'")
138 elsif operator == "exists"
139 if col.nil? or col.type != :jsonb
140 raise ArgumentError.new("Invalid attribute '#{attr}' for operator '#{operator}' in filter")
143 cond_out << "jsonb_exists_inline_op(#{attr_table_name}.#{attr}, ?)"
145 elsif expr = /^ *\( *(\w+) *(<=?|>=?|=) *(\w+) *\) *$/.match(attr)
146 if operator != '=' || ![true,"true"].index(operand)
147 raise ArgumentError.new("Invalid expression filter '#{attr}': subsequent elements must be [\"=\", true]")
150 attr1, attr2 = expr[1], expr[3]
151 allowed = attr_model_class.searchable_columns(operator)
152 [attr1, attr2].each do |tok|
153 if !allowed.index(tok)
154 raise ArgumentError.new("Invalid attribute in expression: '#{tok}'")
156 col = attr_model_class.columns.select { |c| c.name == tok }.first
157 if col.type != :integer
158 raise ArgumentError.new("Non-numeric attribute in expression: '#{tok}'")
161 cond_out << "#{attr1} #{operator} #{attr2}"
163 if !attr_model_class.searchable_columns(operator).index(attr) &&
164 !(col.andand.type == :jsonb && ['contains', '=', '<>', '!='].index(operator))
165 raise ArgumentError.new("Invalid attribute '#{attr}' in filter")
167 attr_type = attr_model_class.attribute_column(attr).type
170 when '=', '<', '<=', '>', '>=', '!=', 'like', 'ilike'
171 operator = '<>' if operator == '!='
172 if operand.is_a? String
173 if attr_type == :boolean
174 if not ['=', '<>'].include?(operator)
175 raise ArgumentError.new("Invalid operator '#{operator}' for " \
176 "boolean attribute '#{attr}'")
178 case operand.downcase
179 when '1', 't', 'true', 'y', 'yes'
181 when '0', 'f', 'false', 'n', 'no'
184 raise ArgumentError.new("Invalid operand '#{operand}' for " \
185 "boolean attribute '#{attr}'")
189 # explicitly allow NULL
190 cond_out << "#{attr_table_name}.#{attr} #{operator} ? OR #{attr_table_name}.#{attr} IS NULL"
192 cond_out << "#{attr_table_name}.#{attr} #{operator} ?"
194 if (# any operator that operates on value rather than
196 operator.match(/[<=>]/) and (attr_type == :datetime))
197 operand = Time.parse operand
200 elsif operand.nil? and operator == '='
201 cond_out << "#{attr_table_name}.#{attr} is null"
202 elsif operand.nil? and operator == '<>'
203 cond_out << "#{attr_table_name}.#{attr} is not null"
204 elsif (attr_type == :boolean) and ['=', '<>'].include?(operator) and
205 [true, false].include?(operand)
206 cond_out << "#{attr_table_name}.#{attr} #{operator} ?"
208 elsif (attr_type == :integer)
209 if !operand.is_a?(Integer) || operand.bit_length > 64
210 raise ArgumentError.new("Invalid operand '#{operand}' "\
211 "for integer attribute '#{attr}'")
213 cond_out << "#{attr_table_name}.#{attr} #{operator} ?"
216 raise ArgumentError.new("Invalid operand type '#{operand.class}' "\
217 "for '#{operator}' operator in filters")
220 if !operand.is_a? Array
221 raise ArgumentError.new("Invalid operand type '#{operand.class}' "\
222 "for '#{operator}' operator in filters")
224 if attr_type == :integer
226 if !el.is_a?(Integer) || el.bit_length > 64
227 raise ArgumentError.new("Invalid element '#{el}' in array "\
228 "for integer attribute '#{attr}'")
232 cond_out << "#{attr_table_name}.#{attr} #{operator} (?)"
234 if operator == 'not in' and not operand.include?(nil)
235 # explicitly allow NULL
236 cond_out[-1] = "(#{cond_out[-1]} OR #{attr_table_name}.#{attr} IS NULL)"
239 operand = [operand] unless operand.is_a? Array
242 cl = ArvadosModel::kind_class op
245 if attr_model_class.uuid_prefix == cl.uuid_prefix
251 # Use a substring query to support remote uuids
252 cond << "substring(#{attr_table_name}.#{attr}, 7, 5) = ?"
253 param_out << cl.uuid_prefix
259 cond_out << cond.join(' OR ')
261 if col.andand.type != :jsonb
262 raise ArgumentError.new("Invalid attribute '#{attr}' for '#{operator}' operator")
265 raise ArgumentError.new("Invalid operand '#{operand.inspect}' for '#{operator}' operator")
267 operand = [operand] unless operand.is_a? Array
270 raise ArgumentError.new("Invalid element #{operand.inspect} in operand for #{operator.inspect} operator (operand must be a string or array of strings)")
273 # We use jsonb_exists_all_inline_op(a,b) instead of "a ?&
274 # b" because the pg gem thinks "?" is a bind var.
276 # See note in migration
277 # 20230815160000_jsonb_exists_functions about _inline_op
280 # We use string interpolation instead of param_out
281 # because the pg gem flattens param_out / doesn't support
282 # passing arrays as bind vars.
283 q = operand.map { |s| ActiveRecord::Base.connection.quote(s) }.join(',')
284 cond_out << "jsonb_exists_all_inline_op(#{attr_table_name}.#{attr}, array[#{q}])"
286 raise ArgumentError.new("Invalid operator '#{operator}'")
290 conds_out << cond_out.join(' OR ') if cond_out.any?
293 {:cond_out => conds_out, :param_out => param_out, :joins => joins}