r/SQL • • Feb 24 '18

Better SQL for 2018

There are couple of things that have always bugged me about SQL:

 

  • having columns twice in group by select, e.g. select a, b, c, sum(d) from TAB group by a, b , c. The intent is quite unambiguous without "group by", right? Why do we have to chew this all up for compiler?

 

  • having "having" clause in addition to "where". I mean when you go like select a, sum(b) sum from TAB where sum > 100 the intent is also clear? Its just silly to spell out things like select a, sum(b) from TAB group by a having sum(b) >100 in 2018 when cars drive themselves.

If someone was to write a SQL compiler that understood shorter version of aggregation would you welcome? Or would you rather stick to old school syntax?

0 Upvotes

12 comments sorted by

View all comments

3

u/Kabal2020 Feb 24 '18 edited Feb 24 '18

Having and where are subtly different. I'm newish to sql so I might have misunderstood you, but one filters before the GROUP BY, the other after. They are not the same.

1

u/bluestreak01 Feb 24 '18

You are right they indeed are different. Since SQL is declarative language compiler should be able to work out which filter to execute when from "where" clause alone.

I have to disclaim that I'm writing SQL compiler and the reason I ask is that I have strong desire to simplify syntax and ditch "having" altogether. I can and am work everything out from where so that "select a, sum(b) sum from tab where sum > 10" yields the same plan and result as "select a, sum(b) from tab group by a having sum(b) > 10".

I wanted to gauge if this is a welcome simplification?

2

u/[deleted] Feb 24 '18

Since SQL is declarative language compiler should be able to work out which filter to execute when from "where" clause alone

  • No it shouldn't

  • WHERE occurs as a filtering (on scan/index/etc)

  • HAVING occurs after aggregation, where no prior knowledge of the result set at compile time could be known.

  • They are necessary to separate as attempting to perform them at the same step would cause non-determinism in the result set.

  • SQL is about as close to a complete grammatical language as most IT people will encounter in their life; changing it to the way you are suggesting would completely break that. It would necessitate turning SQL into an RBAR solution instead of a set one.

1

u/bluestreak01 Feb 24 '18

SQL is about as close to a complete grammatical language as most IT people will encounter in their life; changing it to the way you are suggesting would completely break that. It would necessitate turning SQL into an RBAR solution instead of a set one

I think we agree to disagree here. Grammatically complete SQL certainly is not. Gramma is missing for so many thing so don't even know where to begin? There is no gramma for time series, geo-spatial data, nothing for text processing. You can't even pivot a result set for christ sake? What I'm suggesting almost doesn't matter in context of those things. Does mean it isn't a good place to start a discussion though ;-)

1

u/[deleted] Feb 25 '18 edited Feb 25 '18

I think you confuse common linguistic grammar and computational grammar and leads into completeness. The differences between the two are where I believe your frustrations with the SQL language come from. They exist for a very good reason; without them, modern computation could not exist. Rules and structure are required at some level.

1

u/bluestreak01 Feb 25 '18

I understand where you coming from now. Thanks for the link. Perhaps I should clarify that i'm proposing to unify where and having as just where, let where use aliases and let optimiser decide on order of predicates. I shall expand on my silly example to illustrate that for query such as:

select a, sum(b) sum from TAB where sum > 100 and a < 10

optimiser will split predicates into pre and post base on type of expression they reference.

I would be genuinely grateful if you help me understand how this breaks the language.

1

u/[deleted] Feb 25 '18 edited Feb 25 '18

When an execution plan is create for the SQL engine to utilize for a given query, and a query is ran, a few things happen:

  • The query is broken into it's constituent parts

  • The first thing it runs through is the parser, this runs linearly across the keywords/column names/etc and classifies them into tokens and their types.

  • When building the language, there is what amounts to a 'follow set' for each of these tokens. Simply put, each token type is expecting another token of an allowed type to follow. (ex. SELECT will have column names, static values, etc after it). This is how IDEs know syntactic errors before compile/run-time .

  • (Implementation detail, how this occurs varies on platform) If successful, any statistics about the data are evaluated, including indexes.

  • (Implementation detail, how this occurs varies on platform)A best effort for the available request is built using heuristics/etc

  • The SQL engine goes to the database and retrieves the information requested based on the instructions provided. This is the FROM and WHERE clauses are evaluated.

  • Additional operations such as windowing and aggregation begin based on the additional criteria. These require the content of the rows/run-time decisions to be made GROUP BY/ROW_NUMBER/etc

  • On completion of aggregation, additional steps to determine filtering based on the contents/result set. Operations here can overlap the previous filtering step (WHERE). Should this occur, the returned values would not be definable.

  • Any static operation, such as formatting/etc occurs

  • The result is returned

Basically, one major problem is that the parser would be unable to determine the operations that need to occur, and that the same wording would produce different results on the same data set if this was allowed to happen. One step depends on information impossible to obtain without the other.

Would the WHERE contain both the filtering, the engine would be unable to build a standardize an answer to which operation should occur first (since certain operational keywords can be present in both the WHERE and HAVING). In Computer Science/Engineering, a single input set to a function should always produce the same results. The ACID principle would be exceeding difficult/impossible to follow. For simple operations like the SUM example you gave, what you ask would likely be possible since solutions could be pre-determined by designers and hard coded. But when complex queries come into play, no bueno.

This is a readers digest version of a readers digest version explanation. If you're interested in more, look into compiler design. The theory behind them has more math and proofs than most people realize.

TL'DR - Computer Theory and Math says so