I've looked into this and have reminded myself why it works the way it does.

There is a parsing ambiguity when using WITH (and similarly, TUPLE FROM, FROM, and TCLOSE) within WHERE, which I'll illustrate with an example. Given the following expression:

Code: Select all

`WHERE WITH (p := x * 3): p > 2 AND x = 7 `

Does

*AND x = 7* belong to the WITH, i.e.,

*p > 2 AND x = 7* is a complete subexpression?

Or, does

*AND x = 7* belong to WHERE, i.e., equivalent to

*WHERE (WITH (p := x * 3): p > 2) AND x = 7* ?

The user could intend either one, but the syntax doesn't tell us

*which* one. Even if they are semantically equivalent, the parser can't assume they are semantically equivalent. Thus, I'm compelled to force the user to resolve the ambiguity by introducing parentheses.

There may be ways of automatically resolving this -- such as by introducing a rule that any expression terms in a WHERE WITH ... belong to the WITH -- but I'm not sure that would be any better.