Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 53 additions & 17 deletions web-console/src/utils/sql.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ describe('sql', () => {
"column": 14,
"row": 1,
},
"index": 0,
"sql": "SELECT *
FROM wikipedia",
"startOffset": 0,
Expand All @@ -99,6 +100,7 @@ describe('sql', () => {
"column": 7,
"row": 5,
},
"index": 1,
"sql": "SELECT *
FROM w2
LIMIT 5",
Expand Down Expand Up @@ -132,6 +134,7 @@ describe('sql', () => {
"column": 15,
"row": 5,
},
"index": 0,
"sql": "SELECT
\\"channel\\",
COUNT(*) AS \\"Count\\"
Expand All @@ -150,6 +153,7 @@ describe('sql', () => {
"column": 31,
"row": 3,
},
"index": 1,
"sql": "SELECT * FROM \\"wikipedia\\"",
"startOffset": 48,
"startRowColumn": Object {
Expand Down Expand Up @@ -184,6 +188,7 @@ describe('sql', () => {
"column": 15,
"row": 8,
},
"index": 0,
"sql": "WITH w1 AS (
SELECT channel, page FROM \\"wikipedia\\"
)
Expand All @@ -205,6 +210,7 @@ describe('sql', () => {
"column": 39,
"row": 1,
},
"index": 1,
"sql": "SELECT channel, page FROM \\"wikipedia\\"",
"startOffset": 15,
"startRowColumn": Object {
Expand All @@ -218,6 +224,7 @@ describe('sql', () => {
"column": 15,
"row": 8,
},
"index": 2,
"sql": "SELECT
page,
COUNT(*) AS \\"cnt\\"
Expand All @@ -234,8 +241,10 @@ describe('sql', () => {
`);
});

it('works with replace query', () => {
it('works with select query followed by a replace query', () => {
const text = sane`
SELECT * FROM "wiki"

REPLACE INTO "wikipedia" OVERWRITE ALL
WITH "ext" AS (
SELECT *
Expand All @@ -259,11 +268,26 @@ describe('sql', () => {
expect(found).toMatchInlineSnapshot(`
Array [
Object {
"endOffset": 379,
"endOffset": 29,
"endRowColumn": Object {
"column": 7,
"row": 2,
},
"index": 0,
"sql": "SELECT * FROM \\"wiki\\"",
"startOffset": 0,
"startRowColumn": Object {
"column": 0,
"row": 0,
},
},
Object {
"endOffset": 401,
"endRowColumn": Object {
"column": 18,
"row": 15,
"row": 17,
},
"index": 1,
"sql": "REPLACE INTO \\"wikipedia\\" OVERWRITE ALL
WITH \\"ext\\" AS (
SELECT *
Expand All @@ -280,18 +304,19 @@ describe('sql', () => {
\\"channel\\"
FROM \\"ext\\"
PARTITIONED BY DAY",
"startOffset": 0,
"startOffset": 22,
"startRowColumn": Object {
"column": 0,
"row": 0,
"row": 2,
},
},
Object {
"endOffset": 360,
"endOffset": 382,
"endRowColumn": Object {
"column": 10,
"row": 14,
"row": 16,
},
"index": 2,
"sql": "WITH \\"ext\\" AS (
SELECT *
FROM TABLE(
Expand All @@ -306,46 +331,48 @@ describe('sql', () => {
\\"isRobot\\",
\\"channel\\"
FROM \\"ext\\"",
"startOffset": 39,
"startOffset": 61,
"startRowColumn": Object {
"column": 0,
"row": 1,
"row": 3,
},
},
Object {
"endOffset": 276,
"endOffset": 298,
"endRowColumn": Object {
"column": 70,
"row": 8,
"row": 10,
},
"index": 3,
"sql": "SELECT *
FROM TABLE(
EXTERN(
'{\\"type\\":\\"http\\",\\"uris\\":[\\"https://druid.apache.org/data/wikipedia.json.gz\\"]}',
'{\\"type\\":\\"json\\"}'
)
) EXTEND (\\"isRobot\\" VARCHAR, \\"channel\\" VARCHAR, \\"timestamp\\" VARCHAR)",
"startOffset": 57,
"startOffset": 79,
"startRowColumn": Object {
"column": 2,
"row": 2,
"row": 4,
},
},
Object {
"endOffset": 360,
"endOffset": 382,
"endRowColumn": Object {
"column": 10,
"row": 14,
"row": 16,
},
"index": 4,
"sql": "SELECT
TIME_PARSE(\\"timestamp\\") AS \\"__time\\",
\\"isRobot\\",
\\"channel\\"
FROM \\"ext\\"",
"startOffset": 279,
"startOffset": 301,
"startRowColumn": Object {
"column": 0,
"row": 10,
"row": 12,
},
},
]
Expand Down Expand Up @@ -384,6 +411,7 @@ describe('sql', () => {
"column": 22,
"row": 17,
},
"index": 0,
"sql": "EXPLAIN PLAN FOR
INSERT INTO \\"wikipedia\\"
WITH \\"ext\\" AS (
Expand Down Expand Up @@ -414,6 +442,7 @@ describe('sql', () => {
"column": 22,
"row": 17,
},
"index": 1,
"sql": "INSERT INTO \\"wikipedia\\"
WITH \\"ext\\" AS (
SELECT *
Expand Down Expand Up @@ -443,6 +472,7 @@ describe('sql', () => {
"column": 10,
"row": 15,
},
"index": 2,
"sql": "WITH \\"ext\\" AS (
SELECT *
FROM TABLE(
Expand All @@ -469,6 +499,7 @@ describe('sql', () => {
"column": 70,
"row": 9,
},
"index": 3,
"sql": "SELECT *
FROM TABLE(
EXTERN(
Expand All @@ -488,6 +519,7 @@ describe('sql', () => {
"column": 10,
"row": 15,
},
"index": 4,
"sql": "SELECT
TIME_PARSE(\\"timestamp\\") AS \\"__time\\",
\\"isRobot\\",
Expand Down Expand Up @@ -526,6 +558,7 @@ describe('sql', () => {
"column": 14,
"row": 2,
},
"index": 0,
"sql": "EXPLAIN PLAN FOR
SELECT *
FROM wikipedia",
Expand All @@ -541,6 +574,7 @@ describe('sql', () => {
"column": 14,
"row": 2,
},
"index": 1,
"sql": "SELECT *
FROM wikipedia",
"startOffset": 17,
Expand All @@ -555,6 +589,7 @@ describe('sql', () => {
"column": 7,
"row": 7,
},
"index": 2,
"sql": "EXPLAIN PLAN FOR
SELECT *
FROM w2
Expand All @@ -571,6 +606,7 @@ describe('sql', () => {
"column": 7,
"row": 7,
},
"index": 3,
"sql": "SELECT *
FROM w2
LIMIT 5",
Expand Down
17 changes: 16 additions & 1 deletion web-console/src/utils/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,21 @@ export function findSqlQueryPrefix(text: string): string | undefined {
}
}

export function cleanSqlQueryPrefix(text: string): string {
const matchReplace = text.match(/\sREPLACE$/i);
if (matchReplace) {
// This query likely grabbed a "REPLACE" (which is not a reserved keyword) from the next query over, see if we can delete it
const textWithoutReplace = text.slice(0, -matchReplace[0].length).trimEnd();
if (SqlQuery.maybeParse(textWithoutReplace)) {
return textWithoutReplace;
}
}

return text;
}

export interface QuerySlice {
index: number;
startOffset: number;
startRowColumn: RowColumn;
endOffset: number;
Expand All @@ -130,11 +144,12 @@ export function findAllSqlQueriesInText(text: string): QuerySlice[] {
if (sql) {
const endIndex = m.index + sql.length;
found.push({
index: found.length,
startOffset: offset + m.index,
startRowColumn: offsetToRowColumn(text, offset + m.index)!,
endOffset: offset + endIndex,
endRowColumn: offsetToRowColumn(text, offset + endIndex)!,
sql,
sql: cleanSqlQueryPrefix(sql),
});
}
remainingText = remainingText.slice(advanceBy);
Expand Down
Loading