From/Select and Select/From

9/24/2005 8:38:05 PM

From/Select and Select/From

Paul Vick writes about the debate over using FROM/SELECT (the FLWOR notation, which C# and XQuery use) versus SELECT/FROM (which VB and SQL use) for querying data. It seems that are a number of technical reasons why the FLWOR notation is preferable (intellisense, locality of sequence, order of execution), but SQL notation is much more familiar and has been around for decades.

I began asking myself: “Wes, what prevents the compiler from using these two notations interchangeably?”

In C# 3.0 and XQuery, many of the other clauses such as the FROM, LET (FROM assignment) and WHERE clauses can be alternated. In C#, such reorderings can actually optimize queries by performing filtering before a subsequent FROM clause. It’s not clear if the same optimization is possible with VB-style queries, since the VB team has not released a formal specification of the new language features.

The problem with permitted alternative orderings is the ambiguity that would result from having nested (or embedded) query expressions that use a different ordering.

With the FROM/SELECT notation of C#, query expressions are implicitly parenthesized, where the FROM clause acts as the opening clause of a new query expression and a GROUPBY or SELECT clause acts as the closing clause of the innermost query expression. (Note that multiple, adjacent FROM clauses act as a single opener, where all but the initial FROM clause are translated to SelectMany operations.)

Unlike the FROM/SELECT notation, the SELECT/FROM notation appears to require explicit parenthesization of nested query expressions (or query comprehensions as VB prefers to call them), because there is no clear, unambiguous way to determine the closing clause of a nested expression.

If C# were to support both notations, that all nested query expressions would need to be parenthesized as it may become unclear which nested query a clause belongs to.

To resolve this problem, I thought of two possible solutions:

  • Require that the notation of nested query expressions conform to the ordering of top-level query expressions.
  • Replace the FROM keyword with the reserved FOR keyword in the FROM/SELECT notation. This notation would also better match XQuery’s FLWOR syntax.

Comments

 

Navigation

Categories

About

Net Undocumented is a blog about the internals of .NET including Xamarin implementations. Other topics include managed and web languages (C#, C++, Javascript), computer science theory, software engineering and software entrepreneurship.

Social Media