Skip to content

Fix two improper casts in HavingSpecMetricComparator.#5352

Merged
gianm merged 4 commits intoapache:masterfrom
gianm:fix-havingspec-on-double
Feb 6, 2018
Merged

Fix two improper casts in HavingSpecMetricComparator.#5352
gianm merged 4 commits intoapache:masterfrom
gianm:fix-havingspec-on-double

Conversation

@gianm
Copy link
Copy Markdown
Contributor

@gianm gianm commented Feb 6, 2018

Fixes two things:

  1. An improper double-to-long cast when comparing double metrics to any
    kind of value, which was a regression from greater-than/less-than/equal-to havingSpec to call AggregatorFactory.finalizeComputation(..) #4883.
  2. An improper double-to-long cast when comparing a long/int metric to a
    double/float value: the value was cast to long/int, drawing strange
    conclusions like int 100 matching a havingSpec of equalTo(100.5).

Marked 0.12.0 since one of these was introduced in #4883, new in 0.12.0.

Fixes two things:

1. An improper double-to-long cast when comparing double metrics to any
   kind of value, which was a regression from apache#4883.
2. An improper double-to-long cast when comparing a long/int metric to a
   double/float value: the value was cast to long/int, drawing strange
   conclusions like int 100 matching a havingSpec of equalTo(100.5).
@gianm gianm added the Bug label Feb 6, 2018
@gianm gianm added this to the 0.12.0 milestone Feb 6, 2018
Copy link
Copy Markdown
Member

@nishantmonu51 nishantmonu51 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

if (value instanceof Long || value instanceof Integer) {
return Longs.compare(n, value.longValue());
} else if (value instanceof Double || value instanceof Float) {
return BigDecimal.valueOf(n).compareTo(BigDecimal.valueOf(value.doubleValue()));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add comment about reason to not use Doubles.compare and floats.compare ?

@gianm
Copy link
Copy Markdown
Contributor Author

gianm commented Feb 6, 2018

@nishantmonu51 added comments; thanks for review.

float l = ((Float) metricValueObj).floatValue();
return Floats.compare(l, value.floatValue());
float n = (Float) metricValueObj;
return Floats.compare(n, value.floatValue());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we combine float and double also similar to long and int... also check type of value instance and use BigDecimal if it is not double? ... to keep it similar to long/int case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@himanshug I edited the code to simplify it, and factored out the BigDecimal stuff to a helper method. Now the branches are more similar.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks

@gianm gianm merged commit e255d66 into apache:master Feb 6, 2018
@gianm gianm deleted the fix-havingspec-on-double branch February 6, 2018 21:19
gianm added a commit to gianm/druid that referenced this pull request Feb 6, 2018
* Fix two improper casts in HavingSpecMetricComparator.

Fixes two things:

1. An improper double-to-long cast when comparing double metrics to any
   kind of value, which was a regression from apache#4883.
2. An improper double-to-long cast when comparing a long/int metric to a
   double/float value: the value was cast to long/int, drawing strange
   conclusions like int 100 matching a havingSpec of equalTo(100.5).

* Add comments.

* Remove extraneous comment.

* Simplify code a bit.
jon-wei pushed a commit that referenced this pull request Feb 7, 2018
* Fix two improper casts in HavingSpecMetricComparator.

Fixes two things:

1. An improper double-to-long cast when comparing double metrics to any
   kind of value, which was a regression from #4883.
2. An improper double-to-long cast when comparing a long/int metric to a
   double/float value: the value was cast to long/int, drawing strange
   conclusions like int 100 matching a havingSpec of equalTo(100.5).

* Add comments.

* Remove extraneous comment.

* Simplify code a bit.
gianm added a commit to implydata/druid-public that referenced this pull request Feb 7, 2018
* Fix two improper casts in HavingSpecMetricComparator.

Fixes two things:

1. An improper double-to-long cast when comparing double metrics to any
   kind of value, which was a regression from apache#4883.
2. An improper double-to-long cast when comparing a long/int metric to a
   double/float value: the value was cast to long/int, drawing strange
   conclusions like int 100 matching a havingSpec of equalTo(100.5).

* Add comments.

* Remove extraneous comment.

* Simplify code a bit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants