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

Building statement with FORCE INDEX results in wrong SQL

Open SteveTherrien opened this issue 1 year ago • 0 comments

Hello,

I have query that's not parsed or built properly when FORCE INDEX is used:

SELECT a.id, a.name, b.order_id, b.total
FROM customers a
INNER JOIN orders b FORCE INDEX (idx_customer_id)
    ON a.id = b.customer_id
WHERE a.status = 'active'

The query becomes:

SELECT a.id, a.name, b.order_id, b.total
FROM customers AS `a` FORCE INDEX (idx_customer_id)
INNER JOIN orders AS `b`
WHERE a.status = 'active'
  • FORCE INDEX got moved from table orders onto customers.
  • The a.id = b.customer_id join condition was removed.

I see the same linting errors as #497, so this is probably related.

Example code
<?php
require __DIR__ . '/vendor/autoload.php';

$in = <<<SQL
SELECT a.id, a.name, b.order_id, b.total
FROM customers a
INNER JOIN orders b FORCE INDEX (idx_customer_id)
    ON a.id = b.customer_id
WHERE a.status = 'active'
SQL;

$parser = new PhpMyAdmin\SqlParser\Parser($in);
$statement = $parser->statements[0];
echo $statement->build();

SteveTherrien avatar Oct 21 '24 19:10 SteveTherrien