From: Tom Clegg Date: Mon, 30 Aug 2021 18:02:46 +0000 (-0400) Subject: 17995: Merge branch 'main' X-Git-Tag: 2.3.0~75^2~3 X-Git-Url: https://git.arvados.org/arvados.git/commitdiff_plain/2115f4609adcc86156e8e0aa59ea38ba5808378e 17995: Merge branch 'main' Arvados-DCO-1.1-Signed-off-by: Tom Clegg --- 2115f4609adcc86156e8e0aa59ea38ba5808378e diff --cc doc/api/methods.html.textile.liquid index 914e9779fc,e051ab66fa..87ff621b8b --- a/doc/api/methods.html.textile.liquid +++ b/doc/api/methods.html.textile.liquid @@@ -105,22 -107,10 +107,24 @@@ table(table table-bordered table-conden |@like@, @ilike@|string|SQL pattern match. Single character match is @_@ and wildcard is @%@. The @ilike@ operator is case-insensitive|@["script_version","like","d00220fb%"]@| |@in@, @not in@|array of strings|Set membership|@["script_version","in",["main","d00220fb38d4b85ca8fc28a8151702a2b9d1dec5"]]@| |@is_a@|string|Arvados object type|@["head_uuid","is_a","arvados#collection"]@| - |@exists@|string|Test if a subproperty is present.|@["properties","exists","my_subproperty"]@| + |@exists@|string|Presence of subproperty|@["properties","exists","my_subproperty"]@| + |@contains@|string, array of strings|Presence of one or more keys or array elements|@["storage_classes_desired", "contains", ["foo", "bar"]]@ (matches both @["foo", "bar"]@ and @["foo", "bar", "baz"]@) + (note @[..., "contains", "foo"]@ is also accepted, and is equivalent to @[..., "contains", ["foo"]]@)| +h4(#filterexpression). Filtering using boolean expressions + +In place of an attribute, the first element of the three-element filter array can be a boolean expression. The following restrictions apply: +* The expression must contain exactly one operator. +* The operator must be @=@, @<@, @<=@, @>@, or @>=@. +* There must be exactly one pair of parentheses, surrounding the entire expression. +* Each operand must be the name of a numeric attribute like @replication_desired@ (literal values like @3@ and non-numeric attributes like @uuid@ are not accepted). +* The expression must not contain whitespace other than an ASCII space (newline and tab characters are not accepted). +* The second and third elements of the filter array must be @"="@ and @true@ respectively. + +Examples: +* @["(replication_desired < replication_confirmed)", "=", true]@ +* @["(replication_desired = replication_confirmed)", "=", true]@ + h4(#substringsearchfilter). Filtering using substring search Resources can also be filtered by searching for a substring in attributes of type @string@, @array of strings@, @text@, and @hash@, which are indexed in the database specifically for search. To use substring search, the filter must: diff --cc services/api/lib/record_filters.rb index 517a7fe284,409e48a6f0..2f5b67074a --- a/services/api/lib/record_filters.rb +++ b/services/api/lib/record_filters.rb @@@ -141,25 -142,9 +142,26 @@@ module RecordFilter cond_out << "jsonb_exists(#{attr_table_name}.#{attr}, ?)" param_out << operand + elsif expr = /^ *\( *(\w+) *(<=?|>=?|=) *(\w+) *\) *$/.match(attr) + if operator != '=' || ![true,"true"].index(operand) + raise ArgumentError.new("Invalid expression filter '#{attr}': subsequent elements must be [\"=\", true]") + end + operator = expr[2] + attr1, attr2 = expr[1], expr[3] + allowed = attr_model_class.searchable_columns(operator) + [attr1, attr2].each do |tok| + if !allowed.index(tok) + raise ArgumentError.new("Invalid attribute in expression: '#{tok}'") + end + col = attr_model_class.columns.select { |c| c.name == tok }.first + if col.type != :integer + raise ArgumentError.new("Non-numeric attribute in expression: '#{tok}'") + end + end + cond_out << "#{attr1} #{operator} #{attr2}" else - if !attr_model_class.searchable_columns(operator).index attr + if !attr_model_class.searchable_columns(operator).index(attr) && + !(col.andand.type == :jsonb && ['contains', '=', '<>', '!='].index(operator)) raise ArgumentError.new("Invalid attribute '#{attr}' in filter") end diff --cc services/api/test/functional/arvados/v1/collections_controller_test.rb index 86a3067311,6c923ff38d..d9e9095755 --- a/services/api/test/functional/arvados/v1/collections_controller_test.rb +++ b/services/api/test/functional/arvados/v1/collections_controller_test.rb @@@ -1456,47 -1404,17 +1404,62 @@@ EO assert_equal col.version, json_response['version'], 'Trashing a collection should not create a new version' end + [['<', :<], + ['<=', :<=], + ['>', :>], + ['>=', :>=], + ['=', :==]].each do |op, rubyop| + test "filter collections by replication_desired #{op} replication_confirmed" do + authorize_with(:active) + get :index, params: { + filters: [["(replication_desired #{op} replication_confirmed)", "=", true]], + } + assert_response :success + json_response["items"].each do |c| + assert_operator(c["replication_desired"], rubyop, c["replication_confirmed"]) + end + end + end + + ["(replication_desired < bogus)", + "replication_desired < replication_confirmed", + "(replication_desired < replication_confirmed", + "(replication_desired ! replication_confirmed)", + "(replication_desired <)", + "(replication_desired < manifest_text)", + "(manifest_text < manifest_text)", # currently only numeric attrs are supported + "(replication_desired < 2)", # currently only attrs are supported, not literals + "(1 < 2)", + ].each do |expr| + test "invalid filter expression #{expr}" do + authorize_with(:active) + get :index, params: { + filters: [[expr, "=", true]], + } + assert_response 422 + end + end + + test "invalid op/arg with filter expression" do + authorize_with(:active) + get :index, params: { + filters: [["replication_desired < replication_confirmed", "!=", false]], + } + assert_response 422 + end ++ + ["storage_classes_desired", "storage_classes_confirmed"].each do |attr| + test "filter collections by #{attr}" do + authorize_with(:active) + get :index, params: { + filters: [[attr, "=", '["default"]']] + } + assert_response :success + assert_not_equal 0, json_response["items"].length + json_response["items"].each do |c| + assert_equal ["default"], c[attr] + end + end + end ++>>>>>>> main end