sql-parser icon indicating copy to clipboard operation
sql-parser copied to clipboard

Subquery in expressions support

Open m0003r opened this issue 5 years ago • 2 comments

It seems to subqueries are poorly supported

Some examples first:

They are not parsed at all
SELECT * FROM (SELECT * FROM b) a

results in

...
PhpMyAdmin\SqlParser\Components\Expression Object
(
    [database] =>
    [table] =>
    [column] =>
    [expr] => (SELECT * FROM b)
    [alias] => a
    [function] =>
    [subquery] => SELECT
)
...
not even extracted properly (see #254)
SELECT * FROM a WHERE a.field IN (SELECT * FROM b)

results in

...
...
PhpMyAdmin\SqlParser\Components\Condition Object
(
    [identifiers] => Array
        (
            [0] => a
            [1] => field
            [2] => b
        )

    [isOperator] =>
    [expr] => a.field IN (SELECT * FROM b)
)
...

So I have an idea. May be there must be separate ExpressionParser generating AST for expression? Those (including me) who needed parsed expression can use it (and extract subselects, and parse it with regular parser), and there will be no compatibility break.

Does it sounds as a good plan? If it does, I would like to implement it.

m0003r avatar Apr 07 '20 21:04 m0003r

For me it sound a good plan, I would be happy to see a PR ;) @devenbansod any thoughts on this one ?

williamdes avatar Apr 07 '20 21:04 williamdes

@m0003r Yes. Definitely sounds like a plan. Please feel free to make a PR and I would be happy to help review.

We do have a https://github.com/phpmyadmin/sql-parser/blob/master/src/Components/Expression.php and https://github.com/phpmyadmin/sql-parser/blob/master/src/Components/CaseExpression.php which do parse normal expressions. You can decide on whether you can reuse any functionality (or look for inspiration on how to handle a few things) from these.

devenbansod avatar Apr 10 '20 19:04 devenbansod