-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expand order by field #1080
base: main
Are you sure you want to change the base?
Expand order by field #1080
Conversation
@@ -81,6 +81,8 @@ import { | |||
|
|||
import {Connection} from '../runtime_types'; | |||
import {AndChain, generateHash, indent} from './utils'; | |||
import { isReduceSegment } from './malloy_types'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extraneous imports
@@ -1055,7 +1057,7 @@ class FieldInstanceResult implements FieldInstance { | |||
getField(name: string): FieldInstanceField { | |||
const fi = this.allFields.get(name); | |||
if (fi === undefined) { | |||
throw new Error(`Internal Error, field Not defined ${name}`); | |||
throw new Error(`Internal Error, field not defined ${name}`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lowercase this not
@@ -1929,7 +1931,18 @@ class QueryQuery extends QueryField { | |||
|
|||
expandFields(resultStruct: FieldInstanceResult) { | |||
let resultIndex = 1; | |||
for (const f of this.expandWildCards(resultStruct.firstSegment.fields)) { | |||
let fields: QueryFieldDef[] = resultStruct.firstSegment.fields; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Core premise of the PR is that we should be processing order by fields explicitly into the resultStruct, otherwise we only get them by chance if they are also included into the query via some other parameter
@christopherswenson you are working in this space can you help? |
Thanks @shivanesabharwal for your contribution! This PR (re?)raises some interesting/difficult questions. I think this is actually a case where the error message about the field being missing from the Currently there isn't perfect agreement between the parser/translator/typechecker and the compiler/SQL generator, and this is one case where that manifests. Today, the translator doesn't actually check that fields referenced in an There is active discussion going on between me, @lloydtabb, and @mtoy-googly-moogly about how we validate Today my understanding is that an According to that understanding, the query in your test case should give an error like "field We're inheriting some of the baggage of SQL here, in that we can't order by a field without including it in the query (though we could theoretically generate an extra query stage to remove those fields after the fact). And it's not clear to me whether it should be possible to automatically add fields to a query by including them in an For example, in this query:
It would certainly be confusing for Some possible avenues that seem reasonable to pursue:
|
Regarding #949, I'm not sure if @lloydtabb had any one of these options in mind (or something else) when he created the issue. From previous discussions with him, I would find it a little surprising if he meant anything other than the second option. |
Thanks so much for the thorough review Chris!
Including it in the query SELECT seems to be a malloy feature in this case. I admit I expected the generated SQL to be something like
It seems this is running into the language limitation you outline:
but the SQL query above is valid, it's just that Malloy doesn't have a way of doing that right now? Similarly for the non-aggregate order_by case with
no grouping, since there's no aggregation. I guess what I am saying is that if a user adds an aggregate to a query, they are implicitly asking us to choose a group by set for them, but I can understand that that carries a lot of baggage :) I know Looker often generates order by fields and puts them in the SELECT stmt, but then simply does not return the row in the server response (it includes it as a My 2c is that any option that allows the user to avoid typing the field twice, i.e. in both a In any case, it's clear that the scope of the problem is beyond this PR. Would you like me to pivot this in any way? It sounds like it's best to just wait until the semantics are settled. |
Hmmm ... another thing we could do, for dialects which allow an expression in the order_by is to simply copy the definition of the order_by field into the order_by expression, SQL doesn't seem to mind expressions which reference columns in the FROM
It is weird parsing these though ...
|
Fix #949
The order by field would throw an internal error as it was missing from the
FieldInstanceResult
field list. Seems that processing of those fields happens inexpandFields
, and should explicitly check whether the query has an order by field.The core issue was that only fields in the segment field list were added to the result's field list, and the order by field is not in that list.
The resulting SQL for the test case provided is: