Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore(query-span): support JSON_TABLE for MySQL (#13198)
rebelice55a5228320e8
diff --git a/backend/plugin/parser/mysql/query_span_extractor.go b/backend/plugin/parser/mysql/query_span_extractor.goindex 5b9b8a84a4..63db2047eb 100644--- a/backend/plugin/parser/mysql/query_span_extractor.go+++ b/backend/plugin/parser/mysql/query_span_extractor.go@@ -36,6 +36,10 @@ type querySpanExtractor struct {// tableSourceFrom is the table sources from the from clause.tableSourceFrom []base.TableSource++ // priorTableInFrom is the table sources from the from clause before the current table source.+ // It's used to resolve the column name in JSON_TABLE functions.+ priorTableInFrom []base.TableSource}// newQuerySpanExtractor creates a new query span extractor, the databaseMetadata and the ast are in the read guard.@@ -325,6 +329,10 @@ func (q *querySpanExtractor) extractQuerySpecification(ctx mysql.IQuerySpecificadefer func() {q.tableSourceFrom = q.tableSourceFrom[:originalLength]}()+ originalPriorLength := len(q.priorTableInFrom)+ defer func() {+ q.priorTableInFrom = q.priorTableInFrom[:originalPriorLength]+ }()fromSources, err = q.extractTableSourcesFromFromClause(ctx.FromClause())if err != nil {return nil, err@@ -512,6 +520,7 @@ func (q *querySpanExtractor) extractTableReferenceList(ctx mysql.ITableReferenceif err != nil {return nil, err}+ q.priorTableInFrom = append(q.priorTableInFrom, tableResource)result = append(result, tableResource)}@@ -1195,7 +1204,7 @@ func (q *querySpanExtractor) getFieldColumnSource(databaseName, tableName, fieldif databaseName != "" && databaseName != tableSource.GetDatabaseName() {return nil, false}- if databaseName != "" && tableName != tableSource.GetTableName() {+ if tableName != "" && tableName != tableSource.GetTableName() {return nil, false}}@@ -1236,6 +1245,12 @@ func (q *querySpanExtractor) getFieldColumnSource(databaseName, tableName, field}}+ for i := len(q.priorTableInFrom) - 1; i >= 0; i-- {+ if sourceColumnSet, ok := findInTableSource(q.priorTableInFrom[i]); ok {+ return sourceColumnSet, nil+ }+ }+return nil, &parsererror.ResourceNotFoundError{Database: &databaseName,Table: &tableName,diff --git a/backend/plugin/parser/mysql/test-data/query-span/standard.yaml b/backend/plugin/parser/mysql/test-data/query-span/standard.yamlindex a22a34e08b..9faf0c098c 100644--- a/backend/plugin/parser/mysql/test-data/query-span/standard.yaml+++ b/backend/plugin/parser/mysql/test-data/query-span/standard.yaml@@ -1,3 +1,138 @@+- description: Test For JSON TABLE and JSON ARRAYAGG+ statement: |-+ SELECT+ product_info,+ JSON_ARRAYAGG(+ REPLACE(+ JSON_UNQUTED(product_name),+ 'G',+ 'GN'+ )+ )+ FROM products,+ JSON_TABLE(product_info,+ '$' COLUMNS (+ product_id INT PATH '$.id',+ product_name VARCHAR(50) PATH '$.name',+ product_price DECIMAL(10, 2) PATH '$.price'+ )+ ) AS jt;+ connectedDatabase: db+ metadata: |-+ {+ "name": "db",+ "schemas": [+ {+ "name": "",+ "tables": [+ {+ "name": "products",+ "columns": [+ {+ "name": "product_info"+ }+ ]+ }+ ]+ }+ ]+ }+ querySpan:+ results:+ - name: product_info+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: products+ column: product_info+ - name: |-+ JSON_ARRAYAGG(+ REPLACE(+ JSON_UNQUTED(product_name),+ 'G',+ 'GN'+ )+ )+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: products+ column: product_info+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: products+ column: ""+- description: Test For json table+ statement: |-+ SELECT *+ FROM products,+ JSON_TABLE(product_info,+ '$' COLUMNS (+ product_id INT PATH '$.id',+ product_name VARCHAR(50) PATH '$.name',+ product_price DECIMAL(10, 2) PATH '$.price'+ )+ ) AS jt;+ connectedDatabase: db+ metadata: |-+ {+ "name": "db",+ "schemas": [+ {+ "name": "",+ "tables": [+ {+ "name": "products",+ "columns": [+ {+ "name": "product_info"+ }+ ]+ }+ ]+ }+ ]+ }+ querySpan:+ results:+ - name: product_info+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: products+ column: product_info+ - name: product_id+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: products+ column: product_info+ - name: product_name+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: products+ column: product_info+ - name: product_price+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: products+ column: product_info+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: products+ column: ""- description: Test For alias.* with joinstatement: SELECT t1.*, t2.* FROM t t1 JOIN (select * from t) t2 ON t1.a = t2.a;connectedDatabase: db@@ -22,26 +157,26 @@}querySpan:results:- - name: a- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: a- - name: a- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: a+ - name: a+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: a+ - name: a+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: asourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: ""+ - server: ""+ database: db+ schema: ""+ table: t+ column: ""- description: Test For Explain statementsstatement: EXPLAIN SELECT 1;connectedDatabase: db@@ -91,8 +226,8 @@}querySpan:results:- - name: "1"- sourcecolumns: []+ - name: "1"+ sourcecolumns: []sourcecolumns: []- description: Test asterisk and columnstatement: SELECT *, a FROM t;@@ -127,47 +262,47 @@}querySpan:results:- - name: a- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: a- - name: b- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: b- - name: c- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: c- - name: d- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: d- - name: a- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: a+ - name: a+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: a+ - name: b+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: b+ - name: c+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: c+ - name: d+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: d+ - name: a+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: asourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: ""+ - server: ""+ database: db+ schema: ""+ table: t+ column: ""- description: Happy path for a simple select statementstatement: SELECT * FROM tconnectedDatabase: db@@ -201,40 +336,40 @@}querySpan:results:- - name: a- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: a- - name: b- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: b- - name: c- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: c- - name: d- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: d+ - name: a+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: a+ - name: b+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: b+ - name: c+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: c+ - name: d+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: dsourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: ""+ - server: ""+ database: db+ schema: ""+ table: t+ column: ""- description: Referenced table fields by different formatstatement: SELECT a, t.b, t.c, db.t.d FROM t;connectedDatabase: db@@ -268,40 +403,40 @@}querySpan:results:- - name: a- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: a- - name: b- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: b- - name: c- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: c- - name: d- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: d+ - name: a+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: a+ - name: b+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: b+ - name: c+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: c+ - name: d+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: dsourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: ""+ - server: ""+ database: db+ schema: ""+ table: t+ column: ""- description: Scalar subquery in select fieldsstatement: SELECT 1 AS col_1, (SELECT(2)) AS col_2, (SELECT AVG(a + b * c) FROM t) AS avg_a_b_c FROM t;connectedDatabase: db@@ -335,33 +470,33 @@}querySpan:results:- - name: col_1- sourcecolumns: []- - name: col_2- sourcecolumns: []- - name: avg_a_b_c- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: a- - server: ""- database: db- schema: ""- table: t- column: b- - server: ""- database: db- schema: ""- table: t- column: c+ - name: col_1+ sourcecolumns: []+ - name: col_2+ sourcecolumns: []+ - name: avg_a_b_c+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: a+ - server: ""+ database: db+ schema: ""+ table: t+ column: b+ - server: ""+ database: db+ schema: ""+ table: t+ column: csourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: ""+ - server: ""+ database: db+ schema: ""+ table: t+ column: ""- description: Multiple-Row subquery in where clausestatement: SELECT 1 FROM t WHERE a IN (SELECT a FROM t);connectedDatabase: db@@ -395,14 +530,14 @@}querySpan:results:- - name: "1"- sourcecolumns: []+ - name: "1"+ sourcecolumns: []sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: ""+ - server: ""+ database: db+ schema: ""+ table: t+ column: ""- description: Correlated subquery in the target field clause.statement: SELECT city, (SELECT COUNT(*) FROM paintings p WHERE g.id = p.gallery_id) AS total_paintings FROM galleries g;connectedDatabase: db@@ -447,26 +582,26 @@}querySpan:results:- - name: city- sourcecolumns:- - server: ""- database: db- schema: ""- table: galleries- column: city- - name: total_paintings- sourcecolumns: []+ - name: city+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: galleries+ column: city+ - name: total_paintings+ sourcecolumns: []sourcecolumns:- - server: ""- database: db- schema: ""- table: galleries- column: ""- - server: ""- database: db- schema: ""- table: paintings- column: ""+ - server: ""+ database: db+ schema: ""+ table: galleries+ column: ""+ - server: ""+ database: db+ schema: ""+ table: paintings+ column: ""- description: Test for functionsstatement: SELECT max(a), a-b AS c1, a=b AS c2, a>b, b in (a, c, d) from (SELECT * FROM t) result;connectedDatabase: db@@ -500,77 +635,77 @@}querySpan:results:- - name: max(a)- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: a- - name: c1- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: a- - server: ""- database: db- schema: ""- table: t- column: b- - name: c2- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: a- - server: ""- database: db- schema: ""- table: t- column: b- - name: a>b- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: a- - server: ""- database: db- schema: ""- table: t- column: b- - name: b in (a, c, d)- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: a- - server: ""- database: db- schema: ""- table: t- column: b- - server: ""- database: db- schema: ""- table: t- column: c- - server: ""- database: db- schema: ""- table: t- column: d+ - name: max(a)+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: a+ - name: c1+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: a+ - server: ""+ database: db+ schema: ""+ table: t+ column: b+ - name: c2+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: a+ - server: ""+ database: db+ schema: ""+ table: t+ column: b+ - name: a>b+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: a+ - server: ""+ database: db+ schema: ""+ table: t+ column: b+ - name: b in (a, c, d)+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: a+ - server: ""+ database: db+ schema: ""+ table: t+ column: b+ - server: ""+ database: db+ schema: ""+ table: t+ column: c+ - server: ""+ database: db+ schema: ""+ table: t+ column: dsourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: ""+ - server: ""+ database: db+ schema: ""+ table: t+ column: ""- description: Test for SELECT partial FROM JOIN clausestatement: SELECT t1.*, t2.c, 0 FROM t1 JOIN t2 ON 1 = 1;connectedDatabase: db@@ -609,40 +744,40 @@}querySpan:results:- - name: a- sourcecolumns:- - server: ""- database: db- schema: ""- table: t1- column: a- - name: b- sourcecolumns:- - server: ""- database: db- schema: ""- table: t1- column: b- - name: c- sourcecolumns:- - server: ""- database: db- schema: ""- table: t2- column: c- - name: "0"- sourcecolumns: []+ - name: a+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t1+ column: a+ - name: b+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t1+ column: b+ - name: c+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t2+ column: c+ - name: "0"+ sourcecolumns: []sourcecolumns:- - server: ""- database: db- schema: ""- table: t1- column: ""- - server: ""- database: db- schema: ""- table: t2- column: ""+ - server: ""+ database: db+ schema: ""+ table: t1+ column: ""+ - server: ""+ database: db+ schema: ""+ table: t2+ column: ""- description: Test for JOIN with USING clausestatement: SELECT * FROM t AS t1 JOIN t AS t2 USING(a);connectedDatabase: db@@ -676,61 +811,61 @@}querySpan:results:- - name: a- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: a- - name: b- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: b- - name: c- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: c- - name: d- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: d- - name: b- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: b- - name: c- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: c- - name: d- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: d+ - name: a+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: a+ - name: b+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: b+ - name: c+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: c+ - name: d+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: d+ - name: b+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: b+ - name: c+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: c+ - name: d+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: dsourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: ""+ - server: ""+ database: db+ schema: ""+ table: t+ column: ""- description: Simple CTEstatement: WITH t1 AS (SELECT * FROM t) SELECT * FROM t1;connectedDatabase: db@@ -764,45 +899,45 @@}querySpan:results:- - name: a- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: a- - name: b- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: b- - name: c- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: c- - name: d- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: d+ - name: a+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: a+ - name: b+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: b+ - name: c+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: c+ - name: d+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: dsourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: ""- - server: ""- database: db- schema: ""- table: t1- column: ""+ - server: ""+ database: db+ schema: ""+ table: t+ column: ""+ - server: ""+ database: db+ schema: ""+ table: t1+ column: ""- description: Multi-level CTEstatement: WITH tt2 AS (WITH tt2 AS (SELECT * FROM t) SELECT MAX(a) FROM tt2) SELECT * FROM tt2;connectedDatabase: db@@ -836,24 +971,24 @@}querySpan:results:- - name: MAX(a)- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: a+ - name: MAX(a)+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: asourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: ""- - server: ""- database: db- schema: ""- table: tt2- column: ""+ - server: ""+ database: db+ schema: ""+ table: t+ column: ""+ - server: ""+ database: db+ schema: ""+ table: tt2+ column: ""- description: Test for CTE rename fields namestatement: WITH t1(d, c, b, a) AS (SELECT * FROM t) SELECT * FROM t1;connectedDatabase: db@@ -887,45 +1022,45 @@}querySpan:results:- - name: d- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: a- - name: c- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: b- - name: b- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: c- - name: a- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: d+ - name: d+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: a+ - name: c+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: b+ - name: b+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: c+ - name: a+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: dsourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: ""- - server: ""- database: db- schema: ""- table: t1- column: ""+ - server: ""+ database: db+ schema: ""+ table: t+ column: ""+ - server: ""+ database: db+ schema: ""+ table: t1+ column: ""- description: Test for Recursive Common Table Expression dependent closuresstatement: WITH RECURSIVE t1(cc1, cc2, cc3, n) AS (SELECT a AS c1, b AS c2, c AS c3, 1 AS n FROM t UNION SELECT cc1*cc2, cc2 + cc1, cc3 * cc2, n+1 FROM t1 WHERE n < 10) SELECT * FROM t1;connectedDatabase: db@@ -959,60 +1094,60 @@}querySpan:results:- - name: cc1- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: a- - server: ""- database: db- schema: ""- table: t- column: b- - name: cc2- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: a- - server: ""- database: db- schema: ""- table: t- column: b- - name: cc3- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: a- - server: ""- database: db- schema: ""- table: t- column: b- - server: ""- database: db- schema: ""- table: t- column: c- - name: "n"- sourcecolumns: []+ - name: cc1+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: a+ - server: ""+ database: db+ schema: ""+ table: t+ column: b+ - name: cc2+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: a+ - server: ""+ database: db+ schema: ""+ table: t+ column: b+ - name: cc3+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: a+ - server: ""+ database: db+ schema: ""+ table: t+ column: b+ - server: ""+ database: db+ schema: ""+ table: t+ column: c+ - name: "n"+ sourcecolumns: []sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: ""- - server: ""- database: db- schema: ""- table: t1- column: ""+ - server: ""+ database: db+ schema: ""+ table: t+ column: ""+ - server: ""+ database: db+ schema: ""+ table: t1+ column: ""- description: Test for Non-Recursive Common Table Expression with RECURSIVE key wordsstatement: WITH RECURSIVE t1 AS (SELECT 1 AS c1, 2 AS c2, 3 AS c3, 4 AS c4 UNION SELECT a, b, d, c FROM t) SELECT * FROM t1;connectedDatabase: db@@ -1046,42 +1181,42 @@}querySpan:results:- - name: c1- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: a- - name: c2- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: b- - name: c3- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: d- - name: c4- sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: c+ - name: c1+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: a+ - name: c2+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: b+ - name: c3+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: d+ - name: c4+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: csourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: ""- - server: ""- database: db- schema: ""- table: t1- column: ""+ - server: ""+ database: db+ schema: ""+ table: t+ column: ""+ - server: ""+ database: db+ schema: ""+ table: t1+ column: ""