Skip to content

chore(explore): Add format sql and view in SQL Lab option in View Query #33341

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

justinpark
Copy link
Member

@justinpark justinpark commented May 2, 2025

SUMMARY

The "View Query" feature was previously used to display SQL in a formatted manner. However, the prettier formatting was reverted in this pull request due to unintended changes in the original SQL. To minimize the impact while retaining the formatting option, this commit introduces a "Format SQL" feature in View Query modal and an option to show the original SQL for comparison. Additionally, this commit adds a "View in SQL Lab" button, allowing users to conveniently connect to SQL Lab directly from the dashboard, which is particularly useful for users.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

Before:
Screenshot 2025-05-01 at 5 20 53 PM

After:

after--view-query-actions.mov

TESTING INSTRUCTIONS

Click "View Query" in the chart or explore and then check the toolbars above.

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

@dosubot dosubot bot added the explore:control Related to the controls panel of Explore label May 2, 2025
Copy link

@korbit-ai korbit-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review by Korbit AI

Korbit automatically attempts to detect when you fix issues in new commits.
Category Issue Status
Readability Unexplained API Query Parameters ▹ view 🧠 Not in scope
Readability Unexplained Magic String Separator ▹ view 🧠 Incorrect
Error Handling Missing Error Handling in API Calls ▹ view 🧠 Not in scope
Files scanned
File Path Reviewed
superset-frontend/src/explore/components/controls/ViewQueryModal.tsx
superset-frontend/src/explore/components/controls/ViewQuery.tsx

Explore our documentation to understand the languages and file types we support and the files we ignore.

Check out our docs on how you can make Korbit work best for you and your team.

Loving Korbit!? Share us on LinkedIn Reddit and X

Comment on lines 80 to 83
const queryParams = rison.encode({
keys: ['none'],
columns: ['database.backend'],
});

This comment was marked as resolved.

Comment on lines 84 to 98
SupersetClient.get({
endpoint: `/api/v1/dataset/${datasetId}?q=${queryParams}`,
}).then(({ json }) => {
SupersetClient.post({
endpoint: `/api/v1/sqllab/format_sql/`,
body: JSON.stringify({
sql,
engine: json.result.database.backend,
}),
headers: { 'Content-Type': 'application/json' },
}).then(({ json }) => {
setFormattedSQL(json.result);
setShowFormatSQL(true);
});
});

This comment was marked as resolved.

const StyledSyntaxHighlighter = styled(SyntaxHighlighter)`
flex: 1;
`;

const ViewQuery: FC<ViewQueryProps> = props => {
const { sql, language = 'sql' } = props;
const { sql, language = 'sql', datasource } = props;
const datasetId = datasource.split('__')[0];

This comment was marked as resolved.

@michael-s-molina
Copy link
Member

@justinpark The View in SQL Lab button is a great addition!

About the "Format SQL" feature, I think a formatted SQL is better for users when visualizing a query. Given that #30350 states:

When running a query we pass it through sqlglot to prettify the SQL, but sqlglot is doing some unwanted changes when the dialect is set to generic.

and the fact that the dialect will not be generic most of the time, I wonder if we should always format the SQL when visualizing the query. To be clear, I'm not proposing reverting @betodealmeida's changes but to always format the SQL when clicking on View query.

If the dialect is generic, then you could always show the non-formatted version of the query and warning saying that the SQL can't be formatted for that database type.

Pinging @betodealmeida, who probably has more context, to review too.

@justinpark
Copy link
Member Author

and the fact that the dialect will not be generic most of the time, I wonder if we should always format the SQL when visualizing the query. To be clear, I'm not proposing reverting betodealmeida's changes but to always format the SQL when clicking on View query.

I like the idea too. Since the formatted SQL in this section doesn't modify the original SQL, it should be okay.
Additionally, I plan to display a "Show Original" button so that users can revert back to the original SQL if needed, allowing access to both versions.

@justinpark justinpark force-pushed the chore--add-format-and-view-in-sql-on-view-query branch from 3c4dae5 to c79e886 Compare May 2, 2025 20:29
@justinpark
Copy link
Member Author

justinpark commented May 2, 2025

Here is the updated design.

Screenshot 2025-05-02 at 2 09 40 PM

const getFormatSwitch = () =>
screen.getByRole('switch', { name: 'Show original SQL' });

it('renders the component with Formatted SQL and buttons', async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use test instead of it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
explore:control Related to the controls panel of Explore size/L
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants