-
Notifications
You must be signed in to change notification settings - Fork 14.6k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
feat(sqllab): Format sql #25344
feat(sqllab): Format sql #25344
Conversation
@@ -144,8 +144,10 @@ const AceEditorWrapper = ({ | |||
}; | |||
|
|||
const onChangeText = (text: string) => { | |||
setSql(text); | |||
onChange(text); | |||
if (text !== sql) { |
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 change prevents the overwrite the sql by setQueryEditorAndSaveSqlWithDebounce
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.
Thank you for the PR @justinpark! I left some first-pass comments.
/testenv up |
@michael-s-molina Ephemeral environment spinning up at http://34.220.51.49:8080. Credentials are |
a61a3e0
to
881f020
Compare
@michael-s-molina could you stamp this PR if the updates looks okay? |
@michael-s-molina I also set up |
50ab10a
to
bf9ad0e
Compare
Revert this config option after offline discussion |
/testenv up |
@michael-s-molina Ephemeral environment spinning up at http://18.237.171.147:8080. Credentials are |
func: () => { | ||
formatCurrentQuery(); | ||
}, |
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.
func: () => { | |
formatCurrentQuery(); | |
}, | |
func: formatCurrentQuery, |
superset/sqllab/api.py
Outdated
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}" f".format", | ||
log_to_statsd=False, | ||
) | ||
def format(self) -> FlaskResponse: |
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.
Maybe rename the endpoint to format_sql
?
export function formatQuery(queryEditor) { | ||
return function (dispatch, getState) { | ||
const { sql } = getUpToDateQuery(getState(), queryEditor); | ||
return SupersetClient.post({ |
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.
why couldn't we just us a npm package to do this work vs. creating a whole new endpoint?
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.
why couldn't we just us a npm package to do this work vs. creating a whole new endpoint?
@hughhhh We chose server-side formatting over client-side formatting for several reasons:
- It reduces the need to maintain additional NPM dependencies.
- It reduces the package size.
- It ensures that SQL formatting is consistent across surfaces in "view query", using sqlparse.
If you and other community members believe that an NPM package solution is better, I am happy to make changes.
@betodealmeida @eschutho @michael-s-molina do you have an opinion on the frontend approach?
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.
There seems to be a number of pros with having this on the backend including: custom formatting, dialect specific formatting, and consistency throughout the application.
@villebro has experience working with frontend SQL formatting and ran into a number of issues.
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.
LGTM
superset/sqllab/api.py
Outdated
try: | ||
model = self.format_model_schema.load(request.json) | ||
result = sqlparse.format( | ||
model.get("sql", ""), reindent=True, keyword_case="upper" |
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.
If the sql
field is required (per the schema) is model["sql"]
suffice?
Ephemeral environment shutdown and build artifacts deleted. |
SUMMARY
This commit adds "Format SQL" feature in sql editor which helps to format sql code in the sql editor.
This commit also adds a new shortcut (ctrl + shift + f) for this feature.
BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
after--format-sql.mov
TESTING INSTRUCTIONS
Go to SQL Lab and type a long-run sql
Hit Ctrl + Shift + f or click "Format SQL" in the action dropdown
Check the formatted sql
ADDITIONAL INFORMATION