Range conditions on multi-column `composed_of` aggregates

Hey all,

Ranges on single-mapping composed_of aggregates were fixed in

https://github.com/rails/rails/pull/59013,

so a value object that maps to one column now works:

Customer.where(balance: Money.new(150)..Money.new(250))
# => balance BETWEEN 150 AND 250

I left out composite aggregates deliberately and wanted to discuss how they should be handled.

What happens now

With a value object spread over more than one column, scalar and array conditions work but a range raises:

class Ingredient < ActiveRecord::Base
  composed_of :quantity, class_name: "Measurement",
    mapping: { quantity_value: :value, quantity_unit: :unit }
end

Ingredient.where(quantity: Measurement.new(5, "kg"))
# => quantity_value = 5.0 AND quantity_unit = 'kg'

Ingredient.where(quantity: [Measurement.new(5, "kg")])
# => quantity_value = 5.0 AND quantity_unit = 'kg'

Ingredient.where(quantity: Measurement.new(1, "kg")..Measurement.new(10, "kg"))
# => NoMethodError: undefined method 'value' for an instance of Range

The last example raises cause the multi-mapping branch of PredicateBuilder#expand_from_hash calls object.try!(aggregate_attr) for each mapped column, and a Range responds to none of them.

Why address this?

Now that the single-mapping case works, the multi-mapping path fails. I believe fixing it will support a more consistent API, and better Developer Experience.

Also nothing in the error points towards the aggregate so it is tricky to work out backwards that ranges aren’t supported.

How to address this?

I’m thinking let the declaration name the column a range compares against:

composed_of :quantity, class_name: "Measurement",
  mapping: { quantity_value: :value, quantity_unit: :unit },
  compare_with: :quantity_value

Then where(quantity: Measurement.new(1, "kg")..Measurement.new(10, "kg")) would build quantity_value BETWEEN 1.0 AND 10.0, and the remaining mapped columns would simply not take part in that condition.

Things I’d want your input on:

  1. Should compare_with name the column (:quantity_value) or the value object’s reader (:value)? I lean toward the column, since the resulting condition compares a column and the column is what a caller writing where(...) thinks in terms of. Then again the reader is what the endpoints get projected through, so it’s arguable either way.

  2. Is compare_with: the right name for it?

  3. If adding an option isn’t wanted, would a clearer error be acceptable instead — e.g. raising something that names the mapped attributes and says a Range isn’t supported for this aggregate?

What works today without any core change

  1. Keep composed_of and route the query around it: Register a custom attribute type for the value column, declare that column with the Attributes API, and alias the aggregate name to it. This seems like a “clever workaround” rather than a documented solution, and also depends on the internal implementation. I wrote in detail about it here.

  2. Query the underlying column directly: Ingredient.where(quantity_value: 1..10) builds quantity_value BETWEEN 1.0 AND 10.0. No composed_of involved, so you give up the value-object API for that condition.

1 Like

One thing that worries me about compare_with: is what happens to the other mapped columns.

In your Measurement example, a range from 1 kg to 10 kg would generate a condition on quantity_value alone. Wouldn’t that also match a record containing 5 lb?

That seems like a fairly subtle bug, because the query looks correct at the value-object level but loses an important part of its meaning when translated to SQL.

For quantities stored with a unit, I’d expect the generated condition to include quantity_unit = 'kg' as well, assuming both endpoints use the same unit. If the endpoints have different units, the behavior gets even less obvious.

The same issue could come up with something like a Money object backed by an amount and a currency.

I’m not against supporting ranges here, but I wonder whether a generic compare_with: option is enough to define the semantics safely.

Until that’s clearer, a descriptive error might actually be preferable to a query that silently returns the wrong records.