Skip to content

Commit

Permalink
fix: pass catalog when estimating query cost (#28410)
Browse files Browse the repository at this point in the history
  • Loading branch information
betodealmeida authored and SRK committed May 10, 2024
1 parent 6575ffc commit 46418c1
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
3 changes: 2 additions & 1 deletion superset-frontend/src/SqlLab/actions/sqlLab.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,13 @@ export function scheduleQuery(query) {

export function estimateQueryCost(queryEditor) {
return (dispatch, getState) => {
const { dbId, schema, sql, selectedText, templateParams } =
const { dbId, catalog, schema, sql, selectedText, templateParams } =
getUpToDateQuery(getState(), queryEditor);
const requestSql = selectedText || sql;

const postPayload = {
database_id: dbId,
catalog,
schema,
sql: requestSql,
template_params: JSON.parse(templateParams || '{}'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,88 @@ describe('SqlEditor', () => {
).toBeInTheDocument();
});

describe('with EstimateQueryCost enabled', () => {
let isFeatureEnabledMock: jest.MockInstance<
boolean,
[feature: FeatureFlag]
>;
beforeEach(() => {
isFeatureEnabledMock = jest
.spyOn(uiCore, 'isFeatureEnabled')
.mockImplementation(
featureFlag => featureFlag === uiCore.FeatureFlag.EstimateQueryCost,
);
});
afterEach(() => {
isFeatureEnabledMock.mockClear();
});

it('sends the catalog and schema to the endpoint', async () => {
const estimateApi = 'http://localhost/api/v1/sqllab/estimate/';
fetchMock.post(estimateApi, {});

store = createStore({
...initialState,
sqlLab: {
...initialState.sqlLab,
databases: {
2023: {
allow_ctas: false,
allow_cvas: false,
allow_dml: false,
allow_file_upload: false,
allow_run_async: false,
backend: 'postgresql',
database_name: 'examples',
expose_in_sqllab: true,
force_ctas_schema: null,
id: 1,
allows_cost_estimate: true,
},
},
unsavedQueryEditor: {
id: defaultQueryEditor.id,
dbId: 2023,
sql: 'SELECT * FROM t',
schema: 'public',
catalog: 'prod',
},
},
});
const { findByText } = setup(mockedProps, store);
const button = await findByText('Estimate cost');
expect(button).toBeInTheDocument();

// click button
fireEvent.click(button);
await waitFor(() => {
expect(fetchMock.lastUrl()).toEqual(estimateApi);
expect(fetchMock.lastOptions()).toEqual(
expect.objectContaining({
body: JSON.stringify({
database_id: 2023,
catalog: 'prod',
schema: 'public',
sql: 'SELECT * FROM t',
template_params: {},
}),
cache: 'default',
credentials: 'same-origin',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'X-CSRFToken': '1234',
},
method: 'POST',
mode: 'same-origin',
redirect: 'follow',
signal: undefined,
}),
);
});
});
});

describe('with SqllabBackendPersistence enabled', () => {
let isFeatureEnabledMock: jest.MockInstance<
boolean,
Expand Down
3 changes: 3 additions & 0 deletions superset/sqllab/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class EstimateQueryCostSchema(Schema):
template_params = fields.Dict(
keys=fields.String(), metadata={"description": "The SQL query template params"}
)
catalog = fields.String(
allow_none=True, metadata={"description": "The database catalog"}
)
schema = fields.String(
allow_none=True, metadata={"description": "The database schema"}
)
Expand Down

0 comments on commit 46418c1

Please sign in to comment.