SELECT 'a' /* this is a comment */ 'b'What should the result be? (You can assume that
FROM onerow
onerow
is an existing table that contains one row)It turns out popular RDBMS-es mostly disagree with one another.
In Oracle XE, we get this:
SELECT 'a' /* comment */ 'b'
*
ERROR at line 1:
ORA-00923: FROM keyword not found where expected
PostgreSQL 8.4 also treats it as a syntax error, and thus seems compatible with Oracle's behavior:
ERROR: syntax error at or near "'b'"
LINE 1: SELECT 'a' /* this is a comment */ 'b'
In Microsoft SQL Server 2008 we get:
bAs you can see, MS SQL treats the query as
-
a
(1 rows affected)
SELECT 'a' AS b FROM onerow
.Finally, in MySQL 5.1, we get:
+----+So in MySQL, its as if the comment isn't there at all, and as if the string literals
| a |
+----+
| ab |
+----+
1 row in set (0.00 sec)
'a'
and 'b'
are actually just one string literal 'ab'
.So what does the SQL standard say? In my copy of the 2003 edition, I find this (ISO/IEC 9075-2:2003 (E) 5.3 <literal>, page 145):
Syntax RulesIf we lookup the definition of
1) In a<character string literal>
or<national character string literal>
, the sequence:<quote> <character representation>... <quote> <separator> <quote> <character representation>... <quote>is equivalent to the sequence<quote> <character representation>... <character representation>... <quote>
<separator>
, it reads: <separator> ::= { <comment> | <white space> }...So in this case, MySQL does the "right" thing, and basically ignores the comment, treating
'a'
and 'b'
as a single string constant 'ab'
.UPDATE 1: As always, the devil is in the details. And trust me, the SQL standard has many of them (details that is - I'll leave it up to the reader to decide for the devils, although I have a suspicion in a particular direction). Read on, and make sure to read Nick's comment on this post - it turns out PostgreSQL seems to behave exactly according to the standard in this case.
UPDATE 2: Serg also posted a comment citing yet another part of the standard that states that all comments implicitly count as a newline. This would mean that there doesn't have to be a literal newline character in or following the comment. In this case, my original remark that MySQL got it right would hold again.
I should state that I think very highly of both Nick and Serg, and as far as I am concerned, they're both right. I can't help but seeing this as yet more support for my statement that the SQL standard is so complex it is almost or perhaps completely impossible to get it right.
Do you find this too bold? If so, I'd really love to hear your thoughts on it. Please help us solve this paradox, I only want to understand what the standard really says.
If you try the same thing with a single line comment, all products mentioned react the same as with the initial query, except for PostgreSQL, which now treats the query according to the standard.UPDATE 2: Serg also posted a comment citing yet another part of the standard that states that all comments implicitly count as a newline. This would mean that there doesn't have to be a literal newline character in or following the comment. In this case, my original remark that MySQL got it right would hold again.
I should state that I think very highly of both Nick and Serg, and as far as I am concerned, they're both right. I can't help but seeing this as yet more support for my statement that the SQL standard is so complex it is almost or perhaps completely impossible to get it right.
Do you find this too bold? If so, I'd really love to hear your thoughts on it. Please help us solve this paradox, I only want to understand what the standard really says.
Now don't get me wrong. This post is not designed to bash or glorify any of the products mentioned. I think all of them are great in their own way. I am quite aware that although MySQL happens to adhere to the standard here, it violates it in other places. Finally, I should point out that I don't have a specific opinion on what the right behavior should be. I just want it to be the same on all platforms.
At the same time, I realize that for SQL it's probably too late - up to an extent, incompatibility is considered normal, and database professionals tend to be specialized in particular products anyway. So I'm not holding my breath for the grand unification of SQL dialects.
When I encountered this issue, I did have to think about that other rathole of incompatibilities I have to deal with professionally, which is web-browsers. An interesting development there is the HTML 5 specification, which actually defines an algorithm for parsing HTML - even invalid HTML. This is quite different from the approach taken by most standards, which typically define only an abstract grammar, but leave the implementation entirely up to the vendors. In theory, providing parsing instructions as detailed as done in HTML 5 should make it easier to create correct parsers, and hopefully this will contribute to a more robust web.
Anyway. That was all. Back to work...
UPDATE: I just heard that Sybase (unsurprisingly) behaves similar to MS SQL for this query (that is, query is valid, and returns
Nick also pointed out that LucidDB also provides a standard compliant implementation, in other words, it behaves exactly like PostgreSQL for this particular query. However, Julian, who was and is closely involved in LucidDB agrees with Serg that the comment should probably count as a newline, and filed a bug for LucidDB.
I checked Firebird 2.1.3, and they are in the Oracle camp: in both cases, the query gives a syntax error.
'a'
in a column called b
). I checked SQLite myself, which is also in that camp.Nick also pointed out that LucidDB also provides a standard compliant implementation, in other words, it behaves exactly like PostgreSQL for this particular query. However, Julian, who was and is closely involved in LucidDB agrees with Serg that the comment should probably count as a newline, and filed a bug for LucidDB.
I checked Firebird 2.1.3, and they are in the Oracle camp: in both cases, the query gives a syntax error.