You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jun 27, 2025. It is now read-only.
Repository navigation
This repository was archived by the owner on Jun 27, 2025. It is now read-only.
Presto functions (COUNT, CARDINALITY, SUBSTR) should be uppercase
I'm happy to hear input on chutten style:
SELECT
DATE_TRUNC('day', DATE_PARSE(field1, '%Y%m%d')) AS pretty_field1,
field2 AS pretty_field2,
FROM table_name
WHERE
field3 IS NOT NULLAND (
field4 IN ('20180301', '20180302')
OR SUBSTR(field5, 1, 1) ='a'
)
GROUP BY1
Notable points I expect contention, and am willing to be swayed, on:
having table_name on the same line as FROM (which I defend because there's only one. If there are multiple table names, I would indent like SELECT and WHERE)
having GROUP BY 1 (this is because I don't want to write GROUP BY DATE_TRUNC('day', DATE_PARSE(field1, '%Y%m%d')) and have to change it every time I want to see what 'minute' or 'hour' might look.)
Putting AND and OR at the beginning of lines (I think they'd get lost at the end)
Where I put my parentheses for nested conditionals (It's essentially the one-true-brace style, but with rounder braces)
Strongly agree: table_name on the same line as FROM. I argue that we should prefer explicit JOIN statements and shouldn't use multiple table names in a FROM statement
I prefer GROUP BY 1, but I can see the argument for explicit column names in reviewed queries. I think we should allow GROUP BY 1
Strongly agree with AND and OR at the beginning of the line. I can't see an argument for end of line. I'm open to allowing rivers in the WHERE statement, but my gut says we should outlaw rivers everywhere E.g.:
@mreid-moz - any argument for indenting table names on the next line? I haven't seen this preference before. Does it add to readability? Make queries easier to write?
Definitely readability. You end up with the most important SQL keywords on their own lines which makes it very easy to scan for the logical part you're interested in:
SELECT
... some stuff
FROM
... some tables
WHERE
... some clauses
GROUP BY
... some groupings
HAVING
... more clauses
Published queries should not have GROUP BY $numeric, opting instead for CTEs:
WITH sample AS (
SELECT
DATE_TRUNC('day', DATE_PARSE(field1, '%Y%m%d')) AS pretty_field1,
field2 AS pretty_field2
FROM
table_name
WHERE
field3 IS NOT NULL
AND (
field4 IN ('20180301', '20180302')
OR SUBSTR(field5, 1, 1) = 'a'
)
)
SELECT
pretty_field1,
aggregate(pretty_field2)
FROM
sample
GROUP BY
pretty_field1
Having to use a CTE just for sensible GROUP BY offends my sense of justice. GROUP BY should "just" freakin' take pretty_field1 as an acceptable identifier.
Also, I contend that CTEs make queries harder to understand, but that might just be me.
Activity
mreid-moz commentedon Apr 5, 2018
UPDATE,SELECT,AS, ...) should be uppercase.*different SQL variants use different quoting mechanisms :(
chutten commentedon Apr 5, 2018
COUNT,CARDINALITY,SUBSTR) should be uppercaseI'm happy to hear input on chutten style:
Notable points I expect contention, and am willing to be swayed, on:
table_nameon the same line asFROM(which I defend because there's only one. If there are multiple table names, I would indent likeSELECTandWHERE)GROUP BY 1(this is because I don't want to writeGROUP BY DATE_TRUNC('day', DATE_PARSE(field1, '%Y%m%d'))and have to change it every time I want to see what'minute'or'hour'might look.)ANDandORat the beginning of lines (I think they'd get lost at the end)harterrt commentedon Apr 5, 2018
+1 to chutten style. Comments:
table_nameon the same line asFROM. I argue that we should prefer explicitJOINstatements and shouldn't use multiple table names in aFROMstatementGROUP BY 1, but I can see the argument for explicit column names in reviewed queries. I think we should allowGROUP BY 1ANDandORat the beginning of the line. I can't see an argument for end of line. I'm open to allowing rivers in theWHEREstatement, but my gut says we should outlaw rivers everywhere E.g.:mreid-moz commentedon Apr 5, 2018
GROUP BY 1: strongly agree (though nit in this case it should beGROUP BY 1, 2to be valid)ANDandORat the beginning of lines: I agree, but I kind of like the "rivers" alignment approach despised by @harterrt for this case.harterrt commentedon Apr 5, 2018
@mreid-moz - any argument for indenting table names on the next line? I haven't seen this preference before. Does it add to readability? Make queries easier to write?
mreid-moz commentedon Apr 5, 2018
Definitely readability. You end up with the most important SQL keywords on their own lines which makes it very easy to scan for the logical part you're interested in:
fbertsch commentedon Apr 5, 2018
JOINrather than multiple table namesOpen question: how to specify the join columns, e.g.
fbertsch commentedon Apr 5, 2018
GROUP BY $numeric, opting instead for CTEs:fbertsch commentedon Apr 6, 2018
Here is a list of reserved keywords which should always be capitalized: https://prestodb.io/docs/current/language/reserved.html
chutten commentedon Apr 6, 2018
Having to use a CTE just for sensible
GROUP BYoffends my sense of justice.GROUP BYshould "just" freakin' takepretty_field1as an acceptable identifier.Also, I contend that CTEs make queries harder to understand, but that might just be me.
mreid-moz commentedon Apr 6, 2018
I don't think CTEs should be mandatory either, and IMO
GROUP BY 1makes maintenance easier in general.chutten commentedon Apr 23, 2018
Seems as though conversation has died out. Shall we omit the controversial subjects (GROUP BY and CTEs) and publish so we don't lose momentum?
I'd like to reference a guide when people contribute SQL examples for docs.tmo
harterrt commentedon Apr 23, 2018
Sounds good to me. @chutten do you have time to take a first pass?
harterrt commentedon May 10, 2018
First pass, open for comments:
mozilla/data-docs#133
harterrt commentedon May 17, 2018
🎉 https://docs.telemetry.mozilla.org/concepts/sql_style.html