-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat(spansql): improve the SQL formatting when printing out SQL #13267
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -171,12 +171,88 @@ func (cp CreateProtoBundle) SQL() string { | |
| } | ||
|
|
||
| func (cv CreateView) SQL() string { | ||
| str := "CREATE" | ||
| var sb strings.Builder | ||
| sb.WriteString("CREATE") | ||
| if cv.OrReplace { | ||
| str += " OR REPLACE" | ||
| sb.WriteString(" OR REPLACE") | ||
| } | ||
| sb.WriteString(" VIEW ") | ||
| sb.WriteString(cv.Name.SQL()) | ||
| sb.WriteString(" SQL SECURITY ") | ||
| sb.WriteString(cv.SecurityType.SQL()) | ||
| sb.WriteString(" AS ") | ||
| writeFormattedQuery(&sb, &cv.Query) | ||
| return sb.String() | ||
| } | ||
|
|
||
| func writeFormattedQuery(sb *strings.Builder, q *Query) { | ||
| sb.WriteString("SELECT\n") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new sb.WriteString("SELECT")
if q.Select.Distinct {
sb.WriteString(" DISTINCT")
}
sb.WriteString("\n") |
||
|
|
||
| // SELECT list with aliases | ||
| for i, expr := range q.Select.List { | ||
| sb.WriteString("\t") | ||
| sb.WriteString(expr.SQL()) | ||
| // Add alias if available (check bounds safely) | ||
| if len(q.Select.ListAliases) > i && q.Select.ListAliases[i] != "" { | ||
| sb.WriteString(" AS ") | ||
| sb.WriteString(q.Select.ListAliases[i].SQL()) | ||
| } | ||
| // Add comma and newline, except after last item | ||
| if i < len(q.Select.List)-1 { | ||
| sb.WriteString(",\n") | ||
| } else { | ||
| sb.WriteString("\n") | ||
| } | ||
| } | ||
|
|
||
| // FROM clause | ||
| if len(q.Select.From) > 0 { | ||
| sb.WriteString("FROM ") | ||
| for i, f := range q.Select.From { | ||
| if i > 0 { | ||
| sb.WriteString(", ") | ||
| } | ||
| sb.WriteString(f.SQL()) | ||
| } | ||
| } | ||
|
|
||
| // WHERE clause | ||
| if q.Select.Where != nil { | ||
| sb.WriteString("\nWHERE ") | ||
| sb.WriteString(q.Select.Where.SQL()) | ||
| } | ||
|
|
||
| // GROUP BY clause | ||
| if len(q.Select.GroupBy) > 0 { | ||
| sb.WriteString("\nGROUP BY ") | ||
| for i, gb := range q.Select.GroupBy { | ||
| if i > 0 { | ||
| sb.WriteString(", ") | ||
| } | ||
| sb.WriteString(gb.SQL()) | ||
| } | ||
| } | ||
|
|
||
| // ORDER BY clause | ||
| if len(q.Order) > 0 { | ||
| sb.WriteString("\nORDER BY ") | ||
| for i, o := range q.Order { | ||
| if i > 0 { | ||
| sb.WriteString(", ") | ||
| } | ||
| sb.WriteString(o.SQL()) | ||
| } | ||
| } | ||
|
anicoll marked this conversation as resolved.
Outdated
|
||
|
|
||
| // LIMIT/OFFSET clauses | ||
| if q.Limit != nil { | ||
| sb.WriteString("\nLIMIT ") | ||
| sb.WriteString(q.Limit.SQL()) | ||
| if q.Offset != nil { | ||
| sb.WriteString(" OFFSET ") | ||
| sb.WriteString(q.Offset.SQL()) | ||
| } | ||
| } | ||
| str += " VIEW " + cv.Name.SQL() + " SQL SECURITY " + cv.SecurityType.SQL() + " AS " + cv.Query.SQL() | ||
| return str | ||
| } | ||
|
|
||
| func (st SecurityType) SQL() string { | ||
|
|
@@ -918,7 +994,7 @@ func (sft SelectFromTable) SQL() string { | |
|
|
||
| func (sfj SelectFromJoin) SQL() string { | ||
| // TODO: The grammar permits arbitrary nesting. Does this need to add parens? | ||
| str := sfj.LHS.SQL() + " " + joinTypes[sfj.Type] + " JOIN " | ||
| str := sfj.LHS.SQL() + "\n" + joinTypes[sfj.Type] + " JOIN " | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While adding the newline here improves readability, this function and the lines that follow still use string concatenation ( |
||
| // TODO: hints go here | ||
| str += sfj.RHS.SQL() | ||
| if sfj.On != nil { | ||
|
|
||
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.
This is a great helper function for formatting queries! Once the missing
DISTINCTandHAVINGclauses are added, you could consider using this function to implementQuery.SQL()to improve its formatting and address the// TODO: improve this.comment. This would make the SQL output for standalone queries consistent with the formatting insideCREATE VIEW.It would look something like this:
Note that this would change the output for existing query tests, so they would need to be updated.