greenlion / PHP-SQL-Parser

A pure PHP SQL (non validating) parser w/ focus on MySQL dialect of SQL
BSD 3-Clause "New" or "Revised" License
609 stars 159 forks source link

UNION with function causes array index error. #279

Open EatonEmmerich opened 6 years ago

EatonEmmerich commented 6 years ago

query is:

SELECT table1.field1, CONCAT(table1.field1, table1.field2) as newfield
FROM table1
UNION ALL
SELECT table2.field3, CONCAT(table2.field3, table2.field2) as newfield
FROM table2

error output:

PHP Notice: Undefined offset: 0 in /mnt/c/Users/eemet/Subversion/Charity/PHP-SQL-Parser/src/PHPSQLParser/processors/SQLProcessor.php on line 67 PHP Notice: Undefined offset: 1 in /mnt/c/Users/eemet/Subversion/Charity/PHP-SQL-Parser/src/PHPSQLParser/processors/SQLProcessor.php on line 67 PHP Notice: Undefined offset: 2 in /mnt/c/Users/eemet/Subversion/Charity/PHP-SQL-Parser/src/PHPSQLParser/processors/SQLProcessor.php on line 67 PHP Notice: Undefined offset: 3 in /mnt/c/Users/eemet/Subversion/Charity/PHP-SQL-Parser/src/PHPSQLParser/processors/SQLProcessor.php on line 67 PHP Notice: Undefined offset: 4 in /mnt/c/Users/eemet/Subversion/Charity/PHP-SQL-Parser/src/PHPSQLParser/processors/SQLProcessor.php on line 67 PHP Notice: Undefined offset: 5 in /mnt/c/Users/eemet/Subversion/Charity/PHP-SQL-Parser/src/PHPSQLParser/processors/SQLProcessor.php on line 67 PHP Notice: Undefined offset: 6 in /mnt/c/Users/eemet/Subversion/Charity/PHP-SQL-Parser/src/PHPSQLParser/processors/SQLProcessor.php on line 67

Using head on master.

krappi01 commented 6 years ago

Hi, we're facing the same problem. Possible solution: Add the following code in line 81 of BracketProcessor.php

if (!is_array($subtree)) { return array(); }

sinri commented 6 years ago

Yes, facing the same problem.

Another solution not so good is use @new to stop all notices.

EatonEmmerich commented 6 years ago

@sinri The resulting object also has the incorrect data, so just suppressing the error output doesn't help with the problem. The resulting SQL query created is not valid SQL syntax. I have not given the solution for @krappi01 a try, at the moment I mitigated the problem by not using function in my union query.

sinri commented 6 years ago

@EatonEmmerich Yes, that is a bad situation, hope this would be fixed soon. Suppressing works for me because I just use parser to determine the type of SQL and filter modification out, so it actually could not be called as a solution :-(


By my further test, union would act correctly only for

(
SELECT table1.field1, CONCAT(table1.field1, table1.field2) as newfield
FROM table1
) UNION ALL (
SELECT table2.field3, CONCAT(table2.field3, table2.field2) as newfield
FROM table2
)
sinri commented 6 years ago

Made a fork to try to fix this...

https://github.com/sinri/PHP-SQL-Parser/tree/sinri

amadoalmex commented 6 years ago

Also having the same issue here.