phpmyadmin / phpmyadmin/sql-parser

Parser fails for updates that include subqueries for source tables

Open
#269 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
PHP
Stars
485
Forks
119
PR merge metrics
No merged PRs in 30d

Description

The SQL Parser fails to parse the following query:

update `table1` as e, (select * from table2) e2
set
e.`id` = e2.id,
where e.id=e2.id

It returns the output below. This appears to always be an issue when using subqueries in the table references.

[
    {
        "tables": [
            {
                "database": null,
                "table": "table1",
                "column": null,
                "expr": "`table1`",
                "alias": "e",
                "function": null,
                "subquery": null
            }
        ],
        "set": null,
        "where": null,
        "order": null,
        "limit": null,
        "options": {
            "options": []
        },
        "first": 0,
        "last": 10
    },
    {
        "expr": [
            {
                "database": null,
                "table": null,
                "column": null,
                "expr": "*",
                "alias": null,
                "function": null,
                "subquery": null
            }
        ],
        "from": [
            {
                "database": null,
                "table": "table2",
                "column": null,
                "expr": "table2",
                "alias": null,
                "function": null,
                "subquery": null
            }
        ],
        "index_hints": null,
        "partition": null,
        "where": null,
        "group": null,
        "having": null,
        "order": null,
        "limit": null,
        "procedure": null,
        "into": null,
        "join": null,
        "union": [],
        "end_options": null,
        "options": {
            "options": []
        },
        "first": 11,
        "last": 21
    },
    {
        "options": {
            "options": []
        },
        "end_options": null,
        "set": [
            {
                "column": "e.`id`",
                "value": "e2.id"
            }
        ],
        "first": 22,
        "last": 44,
        "where": [
            {
                "identifiers": [
                    "e",
                    "id",
                    "e2"
                ],
                "isOperator": false,
                "expr": "e.id=e2.id"
            }
        ]
    }
]

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied UPDATE query through the SQL parser and compare its output with the expected table, subquery, SET, and WHERE structure shown in the issue. Trace how UPDATE table references containing a subquery are tokenized and parsed, then add or update a regression test for this query and verify the parsed structure.

Written by the indexing model from the issue text.

Assessment

Tech stack
php, sql
Domain
compilers, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.