[MDEV-11988] Some problems with the MariaDB parser Created: 2017-02-03 Updated: 2017-05-25 Resolved: 2017-05-25 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | Parser |
| Fix Version/s: | N/A |
| Type: | Task | Priority: | Minor |
| Reporter: | Olivier Bertrand | Assignee: | Alexander Barkov |
| Resolution: | Won't Fix | Votes: | 0 |
| Labels: | None | ||
| Description |
|
The IN where clause fails when used with a user variable. Example:
Returns 0 lines. The problem is that MariaDB, wrongly thinking the IN clause contains only one item transforms it in an EQUAL clause that fails. Comparison operators modified by ALL, ANY, or SOME are only accepted with sub-select's. According to SQL they should by also accepted with list of values. For instance:
This raises a syntax error; should not. |
| Comments |
| Comment by Elena Stepanova [ 2017-02-06 ] | |||||||||||
|
The first result doesn't look wrong to me – naturally it assumes that IN contains one item, as @Y is not a list, it's just a string containing letters, commas, spaces and single-quote marks. Anyway, assigning the whole thing to bar as a parser expert for further decisions. | |||||||||||
| Comment by Alexander Barkov [ 2017-02-06 ] | |||||||||||
|
The first result doesn't look wrong look for me either. I agree with Elena. | |||||||||||
| Comment by Alexander Barkov [ 2017-02-06 ] | |||||||||||
|
ANY with a list instead of a subquery is not supported yet. | |||||||||||
| Comment by Olivier Bertrand [ 2017-02-07 ] | |||||||||||
|
In my example, replacing the user variable by its value, the query should be:
And this should work. However it is transformed in:
And this fails. It is a parser decision. Regarding variables as one item or replacing them by their value before parsing. The first is logical. The second would be a mean to construct large queries when the number of values is big. But perhaps this could also be done in a procedure. | |||||||||||
| Comment by Alexander Barkov [ 2017-02-07 ] | |||||||||||
|
Olivier, @Y is an expression, it returns the value of the user variable Y! The IN list contains expressions. The IN predicant is compared to values in the list. In case of your example, the predicant is compared to the value of @Y. Everything works as expected. What you're trying to do can be done using dynamic SQL. In pre-10.2 you can use this:
Starting from 10.2, dynamic SQL was extended to understand EXECUTE IMMEDIATE, so now there is a simpler way:
| |||||||||||
| Comment by Olivier Bertrand [ 2017-02-07 ] | |||||||||||
|
Thank you Alexander for these explanations. My request was related to As you explain, this could be done by getting the sub-select result list in a variable:
Unfortunately, this works only for numeric columns. With string columns, the group_concat function should be added a parameter specifying a quoting character in addition to the separator one. Is this feasible? Perhaps this can be also done specifying:
| |||||||||||
| Comment by Alexander Barkov [ 2017-02-08 ] | |||||||||||
|
You can try the QUOTE() function: Something like this: But the problem is that GROUP_CONCAT() is limited to cut the result if it gets longer than group_concat_max_len bytes, which defaults to 1024 bytes. So GROUP_CONCAT() is fine for relatively short lists. You fetch the results of SQL queries from inside the Connect engine plugin, right? Note, there is also a limit on the maximum query size: It seems that, instead of using GROUP_CONCAT(), it should be a manual loop fetching values from this query:
collecting the values into a String variable and sending queries to the remote server in parts: | |||||||||||
| Comment by Alexander Barkov [ 2017-02-08 ] | |||||||||||
|
Olivier, I have a question about quantified comparison operators. You said this is a standard syntax:
I could not find any proves that this is a standard query. According to SQL:2011, ANY, SOME and ALL can accept only a <table subquery> as an argument. So the closest SQL:2011 compliant query would be:
Where VALUES(1,2,3) is a subquery covered by this grammar:
Which standard version do you mean? Can you please quote the relevant grammar? Thanks! |