Skip to content

Add support for booleans in scripts #20950

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 17, 2016
Merged

Conversation

rjernst
Copy link
Member

@rjernst rjernst commented Oct 14, 2016

Since 2.0, booleans have been represented as numeric fields (longs).
However, in scripts, this is odd, since you expect doing a comparison
against a boolean to work. While languages like groovy will auto convert
between booleans and longs, painless does not.

This changes the doc values accessor for boolean fields in scripts to
return Boolean objects instead of Long objects.

closes #20949

Since 2.0, booleans have been represented as numeric fields (longs).
However, in scripts, this is odd, since you expect doing a comparison
against a boolean to work. While languages like groovy will auto convert
between booleans and longs, painless does not.

This changes the doc values accessor for boolean fields in scripts to
return Boolean objects instead of Long objects.

closes elastic#20949
@rjernst
Copy link
Member Author

rjernst commented Oct 14, 2016

This is technically breaking, since anyone who has written a painless script already will need to change from comparing a 0/1 constant to true/false. But we have said that painless will change. The question is if this is considered small enough (and enough of a pain for people to change) that it should be moved into 5.0 (I lean towards just putting in 5.1, and noting it in the release notes). /cc @clintongormley

@jdconrad
Copy link
Contributor

LGTM, thanks!

Copy link
Contributor

@s1monw s1monw left a comment

Choose a reason for hiding this comment

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

Looks gook I left 2 comments

}

@Override
public List<Boolean> getValues() {
Copy link
Contributor

Choose a reason for hiding this comment

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

does it make sense to assign Collections.unmodifiableList(this); to a member then we don't create a new object each time it's pulled?

Copy link
Contributor

Choose a reason for hiding this comment

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

actually we could even skip the unmodifiableList wrapper since the wrapped list is already unmodifiable

@@ -291,4 +291,38 @@ public double geohashDistanceWithDefault(String geohash, double defaultValue) {
return geohashDistance(geohash);
}
}

class Booleans extends AbstractList<Boolean> implements ScriptDocValues<Boolean> {
Copy link
Contributor

Choose a reason for hiding this comment

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

should this class be static and final?

Copy link
Member Author

Choose a reason for hiding this comment

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

static is not necessary since it is inside an interface, but good catch on the final.

Copy link
Contributor

@jpountz jpountz left a comment

Choose a reason for hiding this comment

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

LGTM

}

@Override
public List<Boolean> getValues() {
Copy link
Contributor

Choose a reason for hiding this comment

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

actually we could even skip the unmodifiableList wrapper since the wrapped list is already unmodifiable

@jpountz
Copy link
Contributor

jpountz commented Oct 17, 2016

The question is if this is considered small enough (and enough of a pain for people to change) that it should be moved into 5.0 (I lean towards just putting in 5.1, and noting it in the release notes).

I think I'd be tempted to put this one into 5.0. Since it will be breaking anyway, I'd rather require a change between RC and GA than between two minor releases.

@clintongormley clintongormley changed the title Scripting: Add support for booleans in scripts Add support for booleans in scripts Oct 17, 2016
@clintongormley
Copy link
Contributor

I think I'd be tempted to put this one into 5.0. Since it will be breaking anyway, I'd rather require a change between RC and GA than between two minor releases.

I agree

@s1monw
Copy link
Contributor

s1monw commented Oct 17, 2016

I think I'd be tempted to put this one into 5.0. Since it will be breaking anyway, I'd rather require a change between RC and GA than between two minor releases.

++ this is really a bug IMO that we don't have this

@rjernst rjernst merged commit 3d3dd71 into elastic:master Oct 17, 2016
@rjernst rjernst deleted the scripting_booleans branch October 17, 2016 18:11
rjernst added a commit that referenced this pull request Oct 17, 2016
* Scripting: Add support for booleans in scripts

Since 2.0, booleans have been represented as numeric fields (longs).
However, in scripts, this is odd, since you expect doing a comparison
against a boolean to work. While languages like groovy will auto convert
between booleans and longs, painless does not.

This changes the doc values accessor for boolean fields in scripts to
return Boolean objects instead of Long objects.

closes #20949

* Make Booleans final and remove wrapping of `this` for getValues()
rjernst added a commit that referenced this pull request Oct 17, 2016
* Scripting: Add support for booleans in scripts

Since 2.0, booleans have been represented as numeric fields (longs).
However, in scripts, this is odd, since you expect doing a comparison
against a boolean to work. While languages like groovy will auto convert
between booleans and longs, painless does not.

This changes the doc values accessor for boolean fields in scripts to
return Boolean objects instead of Long objects.

closes #20949

* Make Booleans final and remove wrapping of `this` for getValues()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>bug :Core/Infra/Scripting Scripting abstractions, Painless, and Mustache v5.0.0 v6.0.0-alpha1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a ScriptDocValues specialization for booleans
5 participants