Where do I put the select?
We have a design issue with the RavenDB Query Language. Consider the following queries:
There are two different ways to express the same concept. The first version is what we have now, and it is modeled after SQL. The problem with that is that it makes it very hard to build good intellisense for this.
The second option is much easier, because the format of the query also follow the flow of writing, and by the time you are in the select, you already know what you are querying on. This is the reason why C# & VB.Net has their Linq syntax in this manner.
The problem is that it is pretty common to want to define aliases for fields in the select, and then refer to them afterward. That now become awkward in the second option.
Any thoughts?
Comments
How would you use the aliases? In SQL you wouldn't be able to do:
select u.Name as UserName from Users u where UserName like 'a%' -- it will complain that it doesn't know the column UserName
So you can't use the alias anyway. (I prefer option 2)
I like the second option better. As mentioned, in SQL you would not be able to use aliases in the same query (in the where/order by/group by/etc clauses) either. In nested queries it should not make a difference.
Why isn't putting the 'select' between 'from' and 'where' an option? I would argue that LINQ does this, not just for Intellisense purposes, but because it's the actual order one generally thinks of things. One generally picks tables before one pick fields from said tables... one generally pick fields before constraining a field. More abstractly: one references something before introducing restrictions (i.e. projections/constraints).
pick -> picks
I'm curious. Are you still supporting lucene style queries or is it only SQL style now?
Also, I like the 2nd option better so that you can provide good intellisense support.
Another option is to put the select in between the from and where
That allows you get the intelli-sense that you need while allowing aliases to still be used in the
where
clause.I'd suggest taking a leaf from prior art ...
I like the second option better, and also what several people suggested - to put the select in between the from and where. But -- why not just allow all options? It shouldn't matter to the parser, and each user can use his preferred style.
Why not support both schemes?
Users only benefit from intellisense if they use your tooling. If statements are prepared elsewhere, traditional syntax is probably prefered.
In your tools users of option 2 will have a better experience.
BTW. I prefer option 2.
This is what it would look like in Azure Log Analytics (v.next) / Application Insights Analytics:
I think putting the select between the from and where makes no sense - it doesn't work like than in either SQL or LINQ. In SQL the select clause is evaluated after where but before group by, that's where the aliases can be used (and in having, which comes after group by). Why not do the same? If the query language won't support group by, then leave select at the end.
I used arangodb for a project and I really liked how they designed the aql even is totally javascript driven I found it really natural.....
There is also this driver http://www.arangoclient.net/ that it's great to "linq" arangodb
So I prefer the second one with the
select
at the bottom :)Why not just use Linq syntax allowing users to have just one single query language?
When I used to write simple SELECT statement I always started by just typing the
SELECT
keyword and I then jump directly to theFROM
clause where I sometimes elaborate aJOIN
.Here is what used to do in a pseudo algorithm:
SELECT
FROM
clause where I reason about eventual JOINS, UNIONS...SELECT
statement where I reason about what columns I want in the resultSteve & Dmitry, That is what we ended up doing, actually. If an alias is used, all fields must use aliases. We initially tried to do a lookup and figure it out, but it was easier to require it and be done with it.
Chase, Actually, we decided to put the
select
as the last thing on the query. This is because of the order of events in the pipelinefrom
to select the source,group by
if used, thenwhere
to filter it,order by
to sort the results,load
if you need to get related values, thenselect
to decide what you want to get and finallyinclude
for fetching additional documents.A full query will look something like:
Eric, You can still do lucene queries, but they are now embedded in the parent query, so:
from Customers where luecne(Query, :your_lucene_query)
Milosz,
Linq syntax is imperative, it is too low level for the more complex stuff, and it is really hard to work with from the inside. We are also supporting more clients, so it is important to be more approachable. One of our primary motivations is that we want to be queryable not by devs, but by ops as well.
Comment preview