Google-cloud-ruby: Firestore querying time with rails throws ArgumentError: comparison of Rational with NaN failed

Created on 20 Sep 2018  Â·  8Comments  Â·  Source: googleapis/google-cloud-ruby

This following snippet works on ruby 2.3.3, but broke in ruby 2.3.7+ (with rails/ActiveSupport)

firestore_client.col('my_col').where('createdAt', '>', Time.at(0)).get.count
> ArgumentError: comparison of Rational with NaN failed
> .rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/activesupport-5.0.6/lib/active_support/core_ext/date/calculations.rb:138:in `<=>'

I had a look, here's the line that caused the issue; it is trying to compare NAN with an instance of Time.
https://github.com/googleapis/google-cloud-ruby/blob/2394fc371dc4602380c01bb17d14ba69eeffc696/google-cloud-firestore/lib/google/cloud/firestore/query.rb#L940

It broke because ruby 2.3.7 tighten up Date comparison, and rails' ActiveSupport patches Time class with it, so you get these:

Date.new(0) <=> Float::NAN
ArgumentError: comparison of Rational with NaN failed

Time.at(0) <=> Float::NAN
ArgumentError: comparison of Rational with NaN failed

Prior to ruby 2.3.7 the above both returns nil.

firestore investigating cleanup

All 8 comments

@maxhungry Thanks for opening the issue. We will start on this ASAP. Can you post a full backtrace?

Btw great work on google-cloud-ruby :heart:
here's the backtrace:
/Users/maxhung/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/activesupport-5.0.6/lib/active_support/core_ext/date/calculations.rb:138:in `<=>' /Users/maxhung/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/activesupport-5.0.6/lib/active_support/core_ext/date/calculations.rb:138:in `compare_with_coercion' /Users/maxhung/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/activesupport-5.0.6/lib/active_support/core_ext/date_time/calculations.rb:196:in `<=>' /Users/maxhung/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/activesupport-5.0.6/lib/active_support/core_ext/time/calculations.rb:274:in `compare_with_coercion' /Users/maxhung/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/google-cloud-firestore-0.21.1/lib/google/cloud/firestore/query.rb:614:in `==' /Users/maxhung/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/google-cloud-firestore-0.21.1/lib/google/cloud/firestore/query.rb:614:in `==' /Users/maxhung/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/google-cloud-firestore-0.21.1/lib/google/cloud/firestore/query.rb:614:in `include?' /Users/maxhung/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/google-cloud-firestore-0.21.1/lib/google/cloud/firestore/query.rb:614:in `filter' /Users/maxhung/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/google-cloud-firestore-0.21.1/lib/google/cloud/firestore/query.rb:223:in `where' (pry):14:in `<main>' /Users/maxhung/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:355:in `eval' /Users/maxhung/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:355:in `evaluate_ruby' /Users/maxhung/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:323:in `handle_line' /Users/maxhung/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:243:in `block (2 levels) in eval' /Users/maxhung/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:242:in `catch' /Users/maxhung/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:242:in `block in eval' /Users/maxhung/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:241:in `catch' /Users/maxhung/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:241:in `eval' /Users/maxhung/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/pry-0.10.4/lib/pry/repl.rb:77:in `block in repl' /Users/maxhung/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/pry-0.10.4/lib/pry/repl.rb:67:in `loop' /Users/maxhung/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/pry-0.10.4/lib/pry/repl.rb:67:in `repl' /Users/maxhung/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/pry-0.10.4/lib/pry/repl.rb:38:in `block in start' /Users/maxhung/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/pry-0.10.4/lib/pry/input_lock.rb:61:in `__with_ownership' /Users/maxhung/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/pry-0.10.4/lib/pry/input_lock.rb:79:in `with_ownership' /Users/maxhung/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/pry-0.10.4/lib/pry/repl.rb:38:in `start' /Users/maxhung/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/pry-0.10.4/lib/pry/repl.rb:15:in `start' /Users/maxhung/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/pry-0.10.4/lib/pry/pry_class.rb:169:in `start' /Users/maxhung/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/pry-byebug-3.5.0/lib/pry-byebug/pry_ext.rb:11:in `start_with_pry_byebug' /Users/maxhung/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/railties-5.0.6/lib/rails/commands/console.rb:65:in `start' /Users/maxhung/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/railties-5.0.6/lib/rails/commands/console_helper.rb:9:in `start' /Users/maxhung/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/railties-5.0.6/lib/rails/commands/commands_tasks.rb:78:in `console' /Users/maxhung/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/railties-5.0.6/lib/rails/commands/commands_tasks.rb:49:in `run_command!' /Users/maxhung/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/railties-5.0.6/lib/rails/commands.rb:18:in `<top (required)>' bin/rails:4:in `require' bin/rails:4:in `<main>' ArgumentError: comparison of Rational with NaN failed from /Users/maxhung/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/activesupport-5.0.6/lib/active_support/core_ext/date/calculations.rb:138:in `<=>' [10] pry(main)>

@maxhungry I think I have identified a solution for this. Could you check my fix on your project? To do this you should replace your Gemfile's google-cloud-firestore dependency with the following:

gem "google-cloud-firestore",
    github: "blowmage/google-cloud-ruby",
    branch: "firestore-nan-fix"

I'm 98+% sure #2471 will fix this for you. We will be releasing a new gem with this fix included shortly.

:tada: that fixed it.

Great. Expect a release in the next couple hours.

Can you upgrade to google-cloud-firestore 0.24.2 and see if that also fixes the issue for you?

Confirmed, it is fixed in the new release. Thanks, that was fast ✨

Was this page helpful?
0 / 5 - 0 ratings