Skip to content

between broken with decimal columns #411

@ioquatix

Description

@ioquatix

If you try to supply a range made from BigDecimal values (e.g. provided by columns of type decimal), the code breaks.

def between other
if equals_quoted?(other.begin, -Float::INFINITY)
if equals_quoted?(other.end, Float::INFINITY)
not_in([])
elsif other.exclude_end?
lt(other.end)
else
lteq(other.end)
end
elsif equals_quoted?(other.end, Float::INFINITY)
gteq(other.begin)
elsif other.exclude_end?
gteq(other.begin).and(lt(other.end))
else
left = quoted_node(other.begin)
right = quoted_node(other.end)
Nodes::Between.new(self, left.and(right))
end
end

It's because you can't compare BigDecimal with INFINITY

> BigDecimal.new(10) == Float::INFINITY                           
FloatDomainError: Infinity
from (pry):10:in `to_r'

The solution is not to use comparison but to use Float#infinite? and BigDecimal#infinite?

[13] pry(main)> Float::INFINITY.infinite?                                       
=> 1
[14] pry(main)> -Float::INFINITY.infinite?                                      
=> -1
[15] pry(main)> BigDecimal::INFINITY.infinite?                                  
=> 1
[16] pry(main)> -BigDecimal::INFINITY.infinite?                                 
=> -1

The problem this introduces is that it no longer works for Numeric values, e.g. Fixnum and Bignum. However, these can never be infinite.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions