# Regression: Painless script fails for BinaryOperator lamdba

**URL:** <https://discuss.elastic.co/t/regression-painless-script-fails-for-binaryoperator-lamdba/291714>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [December 13, 2021, 8:40pm UTC](https://discuss.elastic.co/t/regression-painless-script-fails-for-binaryoperator-lamdba/291714 "2021-12-13T20:40:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sebastian\_Bathke](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sebastian_bathke/32/98859_2.png) [@Sebastian\_Bathke](https://discuss.elastic.co/u/Sebastian_Bathke)\
**Post date:** [December 13, 2021, 8:40pm UTC](https://discuss.elastic.co/t/regression-painless-script-fails-for-binaryoperator-lamdba/291714/1 "2021-12-13T20:40:32Z")

</div>

Hey there,

we noticed previously working painless scripts that make use of the Java Stream API passing lamdbas for BinaryOperator parameters to fail with Elasticsearch 7.16.0 onwards.

E.g. the following sample works fine on Elasticsearch 7.15.2

```auto
curl --location --request POST 'localhost:9200/_scripts/painless/_execute' \
--header 'Content-Type: application/json' \
--data-raw '{
  "script": {
    "source": "def list = new ArrayList(); list.add(1); list.add(2); list.stream().reduce((i1, i2) -> i1).orElse(0)"
  }
}'

```

returning:

```auto
{
    "result": "1"
}

```

but fails on 7.16.0 onwards with:

```auto
{
    "error": {
        "root_cause": [
            {
                "type": "script_exception",
                "reason": "runtime error",
                "script_stack": [
                    "(i1, i2) -> i1).orElse(0)",
                    "^---- HERE"
                ],
                "script": "def list = new ArrayList(); list.add(1); list.add(2); list.stream().reduce((i1, i2) -> i1).orElse(0)",
                "lang": "painless",
                "position": {
                    "offset": 75,
                    "start": 75,
                    "end": 100
                }
            }
        ],
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
            "(i1, i2) -> i1).orElse(0)",
            "^---- HERE"
        ],
        "script": "def list = new ArrayList(); list.add(1); list.add(2); list.stream().reduce((i1, i2) -> i1).orElse(0)",
        "lang": "painless",
        "position": {
            "offset": 75,
            "start": 75,
            "end": 100
        },
        "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "cannot convert function reference [this::lambda$synthetic$0] to a non-functional interface [java.util.function.BinaryOperator]"
        }
    },
    "status": 400
}

```

I haven't tracked down the particular change in 7.16 that causes this.  
The issue is currently blocking us from adding support for Elasticsearch 7.16.

Any hints on syntax workarounds or confimration of this being a regression so I can raise a bug are appreciated!

Best  
Sebastian

---

<div class="post-metadata">

**Author:** ![stu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stu/32/75063_2.png) [@stu](https://discuss.elastic.co/u/stu)\
**Post date:** [December 13, 2021, 11:14pm UTC](https://discuss.elastic.co/t/regression-painless-script-fails-for-binaryoperator-lamdba/291714/2 "2021-12-13T23:14:43Z")

</div>

Hi @Sebastian_Bathke, this is indeed a regression. It would normally fail at runtime, so thankfully it failed at compile time for you.

Unfortunately, the only mitigation is to avoid the functional interface by rewriting in imperative style.

Thank you for the reproduction, it was very helpful in diagnosing the issue.

We are working on a fix. You can track this in [Painless: Failed resolution of subclasses of functional interfaces · Issue #81696 · elastic/elasticsearch · GitHub](https://github.com/elastic/elasticsearch/issues/81696)

---

<div class="post-metadata">

**Author:** ![Sebastian\_Bathke](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sebastian_bathke/32/98859_2.png) [@Sebastian\_Bathke](https://discuss.elastic.co/u/Sebastian_Bathke)\
**Post date:** [December 13, 2021, 11:24pm UTC](https://discuss.elastic.co/t/regression-painless-script-fails-for-binaryoperator-lamdba/291714/3 "2021-12-13T23:24:56Z")

</div>

Many thanks for the fast verification!

I investigated and created a draft fix PR, see [Fix use functional method from superInterface if exists by megglos · Pull Request #81697 · elastic/elasticsearch · GitHub](https://github.com/elastic/elasticsearch/pull/81697) .

Hope this may be helpful.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 10, 2022, 11:25pm UTC](https://discuss.elastic.co/t/regression-painless-script-fails-for-binaryoperator-lamdba/291714/4 "2022-01-10T23:25:06Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
