17995: Merge branch 'main'
authorTom Clegg <tom@curii.com>
Mon, 30 Aug 2021 18:02:46 +0000 (14:02 -0400)
committerTom Clegg <tom@curii.com>
Mon, 30 Aug 2021 18:02:46 +0000 (14:02 -0400)
Arvados-DCO-1.1-Signed-off-by: Tom Clegg <tom@curii.com>

1  2 
doc/api/methods.html.textile.liquid
services/api/lib/record_filters.rb
services/api/test/functional/arvados/v1/collections_controller_test.rb

index 914e9779fc9ab778f62593271fb823b4809972fc,e051ab66fa7afa18d8e52b09741c292f5e1faa9c..87ff621b8bd15db349d29c58930591f4d2b11c84
@@@ -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:
index 517a7fe28496d46d4352d13f9bb0463ba184cdec,409e48a6f090a3b348cd5d551bf35a91427e42a9..2f5b67074a9bdf5b24d3689333d17ee6e98e0745
@@@ -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
  
index 86a3067311e4835f51f5d252b9acf508397aae0f,6c923ff38d96b8b64e4b8fa8ad83b13b3b29eefc..d9e90957553ee6c94774abc0702b0a7b216cb5f0
      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