WITH not available in a WHERE condition

This forum is to report technical problems with Rel.
Post Reply
HughDarwen
Posts: 116
Joined: Sat May 24, 2008 4:49 pm

WITH not available in a WHERE condition

Post by HughDarwen » Mon Nov 19, 2018 4:56 pm

Rel allows the use of WITH in a <bool exp> but not when the <bool exp> is given as a WHERE condition.

I tested this with

Code: Select all

WITH (x := TRUE) : x
and

Code: Select all

r WHERE WITH (x := TRUE) : x
.

Hugh

Dave
Site Admin
Posts: 361
Joined: Sun Nov 27, 2005 7:19 pm

Re: WITH not available in a WHERE condition

Post by Dave » Tue Nov 20, 2018 4:59 pm

Try

Code: Select all

r WHERE (WITH (x := TRUE) : x)
I'll make a note to look at operator precedence around WHERE and WITH.

HughDarwen
Posts: 116
Joined: Sat May 24, 2008 4:49 pm

Re: WITH not available in a WHERE condition

Post by HughDarwen » Tue Nov 27, 2018 2:37 pm

Yes, I did later think of using parens, but our grammar does specify WHERE <bool exp>.

Hugh

Dave
Site Admin
Posts: 361
Joined: Sun Nov 27, 2005 7:19 pm

Re: WITH not available in a WHERE condition

Post by Dave » Tue Nov 27, 2018 5:42 pm

HughDarwen wrote:
Tue Nov 27, 2018 2:37 pm
Yes, I did later think of using parens, but our grammar does specify WHERE <bool exp>.
There's a bit of oddness in the Rel grammar around how WHERE <bool exp> is handled -- particularly as ... WHERE <bool exp> ... can appear as a subexpression within a <bool exp> -- and how WITH is handled. In short, ... WHERE <bool exp> ... can appear within WITH, but WITH can't appear within the <bool exp> of a WHERE <bool exp> unless it's explicitly "promoted" to become a general expression (that must return a BOOLEAN) by being surrounded with parentheses.

I suspect similar issues will exist around TCLOSE and FROM, because they share parsing strategy with WITH.

I can only assume I introduced this bit of oddness to either to get around an ambiguity in the grammar or to get around a limitation of the parser generator I'm using, but I didn't record what in my notes.

I'll look at rearranging the relevant bits of the Rel grammar to address this.

Dave
Site Admin
Posts: 361
Joined: Sun Nov 27, 2005 7:19 pm

Re: WITH not available in a WHERE condition

Post by Dave » Thu Nov 29, 2018 10:07 pm

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.

Post Reply