Skip to content
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

[Scripting] BWC Incompatible Change for Bucket Aggregation Context #35351

Closed
jdconrad opened this issue Nov 7, 2018 · 5 comments · Fixed by #35653
Closed

[Scripting] BWC Incompatible Change for Bucket Aggregation Context #35351

jdconrad opened this issue Nov 7, 2018 · 5 comments · Fixed by #35653
Labels
:Core/Infra/Scripting Scripting abstractions, Painless, and Mustache v6.5.1 v6.6.0 v7.0.0-beta1

Comments

@jdconrad
Copy link
Contributor

jdconrad commented Nov 7, 2018

Recent changes to the bucket aggregation context are backwards incompatible for specific return cases in Painless (#32068). The expected return within this context changed from def (Object) to Double. This causes an issue with casting as it is illegal to cast from one boxed type to another. This means that scripts previously were allowed to do the following where value is a def representing double:

value > 0 ? value : 0

This previous script allowed the auto-return mechanism to cast 0 to Integer and return that since def was the expected return type. Now this will cause a ClassCastException because casting from Integer to Double is not allowed in Painless or Java (included as we follow a casting model very similar to Java's).

My recommended fix is to have this context return Object for now until we can come up with a better solution for deprecation. Note that if this context didn't require null, it would be okay to use double since it's legal to cast from int to double as unboxed types.

This started in version 6.5 and was caught by a Kibana script using a similar example to the one given.

ping @original-brownbear @rjernst

@jdconrad jdconrad added :Core/Infra/Scripting Scripting abstractions, Painless, and Mustache v7.0.0 v6.6.0 v6.5.1 labels Nov 7, 2018
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra

@jakelandis
Copy link
Contributor

Here is a reproduction case:

DELETE test
POST test/_doc/_bulk
{ "index" :{}}
{ "foo" : 1}
{ "index" :{}}
{ "foo" : 2 }
{ "index" :{}}
{ "foo" : 3 }

POST /test/_search
{
  "size": 0,
  "aggs": {
    "foo": {
      "date_histogram": {
        "field": "foo",
        "interval": "1ms"
      },
      "aggs": {
        "mymax": {
          "max": {
            "field": "foo"
          }
        },
        "mybucket_script": {
          "bucket_script": {
            "buckets_path": {
              "mymax": "mymax"
            },
            "script": "params.mymax > 0 ? params.mymax : 0"
          }
        }
      }
    }
  }
}

All is good... now introduce a 0

POST test/_doc
{
  "foo": 0
}

Re-run the search and

{
  "error": {
    "root_cause": [],
    "type": "search_phase_execution_exception",
    "reason": "",
    "phase": "fetch",
    "grouped": true,
    "failed_shards": [],
    "caused_by": {
      "type": "script_exception",
      "reason": "runtime error",
      "script_stack": [
        "params.mymax > 0 ? params.mymax : 0",
        "^---- HERE"
      ],
      "script": "params.mymax > 0 ? params.mymax : 0",
      "lang": "painless",
      "caused_by": {
        "type": "class_cast_exception",
        "reason": "java.lang.Integer cannot be cast to java.lang.Double"
      }
    }
  },
  "status": 503
}

@jdconrad
Copy link
Contributor Author

jdconrad commented Nov 7, 2018

Briefly talked to @rjernst about this. I'm thinking over the behavior of return values in Painless. It's possible it makes more sense to make a change to casting model in the singular case because it will be better for the user.

@original-brownbear
Copy link
Member

@jdconrad @rjernst

My recommended fix is to have this context return Object for now until we can come up with a better solution for deprecation.

I'd be in favour of this as a short-term fix. We did exactly this elsewhere too when trying to make the new contexts return a specific type and running into trouble.

jdconrad added a commit that referenced this issue Nov 16, 2018
…35653)

This change fixes #35351. Users were no longer able to return types of numbers other than doubles for bucket aggregation scripts. This change reverts to the previous behavior of being able to return any type of number and having it converted to a double outside of the script.
@jdconrad
Copy link
Contributor Author

Fixed by #35653

jdconrad added a commit that referenced this issue Nov 16, 2018
…35653)

This change fixes #35351. Users were no longer able to return types of numbers other than doubles for bucket aggregation scripts. This change reverts to the previous behavior of being able to return any type of number and having it converted to a double outside of the script.
jdconrad added a commit to jdconrad/elasticsearch that referenced this issue Nov 26, 2018
…lastic#35653)

This change fixes elastic#35351. Users were no longer able to return types of numbers other than doubles for bucket aggregation scripts. This change reverts to the previous behavior of being able to return any type of number and having it converted to a double outside of the script.
jdconrad added a commit that referenced this issue Nov 26, 2018
…35653) (#35919)

This change fixes #35351. Users were no longer able to return types of 
numbers other than doubles for bucket aggregation scripts. This change 
reverts to the previous behavior of being able to return any type of number 
and having it converted to a double outside of the script.
@jimczi jimczi added v7.0.0-beta1 and removed v7.0.0 labels Feb 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Core/Infra/Scripting Scripting abstractions, Painless, and Mustache v6.5.1 v6.6.0 v7.0.0-beta1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants