JSQLParser / JSqlParser

JSqlParser parses an SQL statement and translate it into a hierarchy of Java classes. The generated hierarchy can be navigated using the Visitor Pattern
https://github.com/JSQLParser/JSqlParser/wiki
Apache License 2.0
5.34k stars 1.34k forks source link

[FEATURE] support for Spark (and others) lambda syntax #2030

Closed oscarbonilla-tid closed 2 months ago

oscarbonilla-tid commented 3 months ago

Grammar or Syntax Description

Spark supports lambda expressions in some functions (e.g. map_filter). It is not possible to parse queries using such functions

SQL Example

SELECT map_filter(my_column, (k,v) -> v.my_inner_column = 'some_value')
FROM my_table

Additional context

manticore-projects commented 3 months ago

Greetings,

its largely supported since 5.0, the following query parses:


SELECT map_filter(my_column, v -> v.my_inner_column = 'some_value')
FROM my_table

However, I will need to look for extending the parameter syntax (x, y).

oscarbonilla-tid commented 3 months ago

You're right. I've just tried parsing with just one parameter and that works.

In any case, I don't think Spark supports the use of that particular function (map_filter) with just one parameter :(

oscarbonilla-tid commented 3 months ago

@manticore-projects do you have plans to fix this in the near future? I can see you had the fix WIP in this commit https://github.com/JSQLParser/JSqlParser/commit/236793aaeabc30f80857b3347ce8e4a6b767e849#diff-d323df58a0300a038ac87b328bf05b8255ff06e6b5d0e9aeae641fa566e4068cR5214

manticore-projects commented 3 months ago

Should be ready by the end of this week.

manticore-projects commented 3 months ago

Sorry, I just checked the LambdaExpressionTest and found:

    @Disabled
    @Test
    // wip, right now the Grammar works but collides with Multi Value Lists
    void testLambdaFunctionMultipleParameter() throws JSQLParserException {
        String sqlStr = "SELECT list_transform(\n" +
                "        [1, 2, 3],\n" +
                "        x -> list_reduce([4, 5, 6], (a, b) -> a + b) + x\n" +
                "    )";
        TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
    }

1) Multi-parameter Lambda expressions have been considered by design and should work in general

2) however, the Grammar collides will Multi Expression lists and needs to be refactored (which is a tedious process with lots of testing)

So I can not promise holding the time line.

manticore-projects commented 2 months ago

Sorry for taking a bit longer. This try-hard example is parsing now:

SELECT list_transform(
        [1, 2, 3],
        x -> list_reduce([4, 5, 6], (a, b) -> a + b) + x
    );
oscarbonilla-tid commented 2 months ago

Thanks for the speed fixing it! Any timeline on a 5.1 release?

manticore-projects commented 2 months ago

Thanks for the speed fixing it! Any timeline on a 5.1 release?

We just releases 5.0 and usual will have 4 releases per year, maybe less since JSQLParser has matured and there is not much stuff to do.

Also, releases have no big meaning here since our development is "test suite driven" and usually the latest snapshot is just the best version of the software.

manticore-projects commented 2 months ago

And you are very welcome of course! Cheers!