Skip to content
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

PMM-12151-12273 Explain, table improvements. #2349

Merged
merged 35 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
5d675c8
PMM-12151-12273 Explain.
JiriCtvrtka Jul 4, 2023
891adc5
PMM-12151-12273 Fix.
JiriCtvrtka Jul 4, 2023
81e32d5
PMM12151-12273 Helpers.
JiriCtvrtka Jul 4, 2023
13ea222
PMM-12151-12273 Fix comments.
JiriCtvrtka Jul 4, 2023
760d46c
PMM-12151-12273 Remove semicolons.
JiriCtvrtka Jul 4, 2023
bf2e8e4
Merge branch 'main' into PMM-12151-12273-explain
JiriCtvrtka Jul 5, 2023
34e1951
Update api/qanpb/object_details.proto
JiriCtvrtka Jul 5, 2023
86a6988
Update api/qanpb/object_details.proto
JiriCtvrtka Jul 5, 2023
99ce3aa
PMM-12151-12273 Regen.
JiriCtvrtka Jul 5, 2023
6de8f53
Merge branch 'main' into PMM-12151-12273-explain
BupycHuk Jul 7, 2023
438f3c8
Merge branch 'main' into PMM-12151-12273-explain
JiriCtvrtka Jul 10, 2023
cae1c5c
Update qan-api2/models/metrics.go
JiriCtvrtka Jul 10, 2023
ff9ec88
Update qan-api2/models/metrics.go
JiriCtvrtka Jul 10, 2023
819188d
Update agent/runner/actions/query_transform_test.go
JiriCtvrtka Jul 10, 2023
df0dd23
Update agent/runner/actions/query_transform_test.go
JiriCtvrtka Jul 10, 2023
b79f62b
PMM-12151-12273 Required changes.
JiriCtvrtka Jul 10, 2023
407b7fe
PMM-23252-12273 Parallel.
JiriCtvrtka Jul 10, 2023
0d616ae
PMM-12151-12273 Typo.
JiriCtvrtka Jul 10, 2023
64dd9ca
Merge branch 'main' into PMM-12151-12273-explain
JiriCtvrtka Jul 10, 2023
bfb4b1c
PMM-12151-12273 Gen.
JiriCtvrtka Jul 10, 2023
402875d
PMM-12151-12273 Regen with newer mockery.
JiriCtvrtka Jul 10, 2023
9321064
Merge branch 'main' into PMM-12151-12273-explain
JiriCtvrtka Jul 13, 2023
4ba3f06
PMM-12151-12273 Add warning for trimmed queries.
JiriCtvrtka Jul 18, 2023
77ee9ae
PMM-12151-12273 Add test for trimmed queries.
JiriCtvrtka Jul 18, 2023
ab0c3ab
PMM-12151-12273 Use example for explain in case of enabled examples.
JiriCtvrtka Jul 18, 2023
bb295da
Merge branch 'main' into PMM-12151-12273-explain
JiriCtvrtka Jul 18, 2023
32fc0c2
Merge branch 'main' into PMM-12151-12273-explain
JiriCtvrtka Jul 18, 2023
ede2d0d
Merge branch 'main' into PMM-12151-12273-explain
JiriCtvrtka Jul 18, 2023
a4efba8
Merge branch 'main' into PMM-12151-12273-explain
JiriCtvrtka Jul 19, 2023
f070745
Merge branch 'main' into PMM-12151-12273-explain
JiriCtvrtka Jul 19, 2023
7d46a84
Merge branch 'main' into PMM-12151-12273-explain
JiriCtvrtka Jul 24, 2023
5971527
Merge branch 'main' into PMM-12151-12273-explain
JiriCtvrtka Jul 24, 2023
322fed1
Merge branch 'main' into PMM-12151-12273-explain
JiriCtvrtka Jul 25, 2023
2cd4646
PMM-12151-12273 Use same query source table even for Oracle MySQL >=8
JiriCtvrtka Jul 25, 2023
d3afd2f
Merge branch 'main' into PMM-12151-12273-explain
JiriCtvrtka Jul 28, 2023
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
7 changes: 7 additions & 0 deletions agent/runner/actions/mysql_explain_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ func (a *mysqlExplainAction) Run(ctx context.Context) ([]byte, error) {
IsDMLQuery: changedToSelect,
}

if a.params.Schema != "" {
_, err = tx.ExecContext(ctx, fmt.Sprintf("USE %#q", a.params.Schema))
if err != nil {
return nil, err
}
}

switch a.params.OutputFormat {
case agentpb.MysqlExplainOutputFormat_MYSQL_EXPLAIN_OUTPUT_FORMAT_DEFAULT:
response.ExplainResult, err = a.explainDefault(ctx, tx)
Expand Down
13 changes: 11 additions & 2 deletions agent/runner/actions/query_transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
//nolint:lll
var (
dmlVerbs = []string{"select", "insert", "update", "delete", "replace"}
commentsRe = regexp.MustCompile(`(?s)\/\*(.*?)\*\/`)
selectRe = regexp.MustCompile(`(?i)^select\s+(.*?)\bfrom\s+(.*?)$`)
updateRe = regexp.MustCompile(`(?i)^update\s+(?:low_priority|ignore)?\s*(.*?)\s+set\s+(.*?)(?:\s+where\s+(.*?))?(?:\s+limit\s*[0-9]+(?:\s*,\s*[0-9]+)?)?$`)
deleteRe = regexp.MustCompile(`(?i)^delete\s+(.*?)\bfrom\s+(.*?)$`)
Expand All @@ -31,8 +32,16 @@ var (
insertSetRe = regexp.MustCompile(`(?i)(?:insert(?:\s+ignore)?|replace)\s+(?:.*?\binto)\s+(.*?)\s*set\s+(.*?)\s*(?:\blimit\b|on\s+duplicate\s+key.*)?\s*$`)
)

func prepareQuery(query string) string {
query = commentsRe.ReplaceAllString(query, "")
query = strings.ReplaceAll(query, "\t", " ")
query = strings.ReplaceAll(query, "\n", " ")
query = strings.TrimRight(query, ";")
return strings.TrimLeft(query, " ")
}

func isDMLQuery(query string) bool {
query = strings.ToLower(strings.TrimSpace(query))
query = strings.ToLower(prepareQuery(query))
for _, verb := range dmlVerbs {
if strings.HasPrefix(query, verb) {
return true
Expand All @@ -51,7 +60,7 @@ it able to explain DML queries on older MySQL versions and for unprivileged user

// dmlToSelect returns query converted to select and boolean, if conversion were needed.
func dmlToSelect(query string) (string, bool) {
query = strings.ReplaceAll(query, "\n", " ")
query = prepareQuery(query)

m := selectRe.FindStringSubmatch(query)
if len(m) > 1 {
Expand Down
220 changes: 151 additions & 69 deletions agent/runner/actions/query_transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,79 +15,161 @@
package actions

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestDMLToSelect(t *testing.T) {
q, c := dmlToSelect(`SELECT nombre FROM tabla WHERE id = 0`)
assert.False(t, c)
assert.Equal(t, `SELECT nombre FROM tabla WHERE id = 0`, q)

q, c = dmlToSelect(`update ignore tabla set nombre = "carlos" where id = 0 limit 2`)
assert.True(t, c)
assert.Equal(t, `SELECT nombre = "carlos" FROM tabla WHERE id = 0`, q)

q, c = dmlToSelect(`update ignore tabla set nombre = "carlos" where id = 0`)
assert.True(t, c)
assert.Equal(t, `SELECT nombre = "carlos" FROM tabla WHERE id = 0`, q)

q, c = dmlToSelect(`update ignore tabla set nombre = "carlos" limit 1`)
assert.True(t, c)
assert.Equal(t, `SELECT nombre = "carlos" FROM tabla`, q)

q, c = dmlToSelect(`update tabla set nombre = "carlos" where id = 0 limit 2`)
assert.True(t, c)
assert.Equal(t, `SELECT nombre = "carlos" FROM tabla WHERE id = 0`, q)

q, c = dmlToSelect(`update tabla set nombre = "carlos" where id = 0`)
assert.True(t, c)
assert.Equal(t, `SELECT nombre = "carlos" FROM tabla WHERE id = 0`, q)

q, c = dmlToSelect(`update tabla set nombre = "carlos" limit 1`)
assert.True(t, c)
assert.Equal(t, `SELECT nombre = "carlos" FROM tabla`, q)

q, c = dmlToSelect(`delete from tabla`)
assert.True(t, c)
assert.Equal(t, `SELECT * FROM tabla`, q)

q, c = dmlToSelect(`delete from tabla join tabla2 on tabla.id = tabla2.tabla2_id`)
assert.True(t, c)
assert.Equal(t, `SELECT 1 FROM tabla join tabla2 on tabla.id = tabla2.tabla2_id`, q)

q, c = dmlToSelect(`insert into tabla (f1, f2, f3) values (1,2,3)`)
assert.True(t, c)
assert.Equal(t, `SELECT * FROM tabla WHERE f1=1 and f2=2 and f3=3`, q)

q, c = dmlToSelect(`insert into tabla (f1, f2, f3) values (1,2)`)
assert.True(t, c)
assert.Equal(t, `SELECT * FROM tabla LIMIT 1`, q)

q, c = dmlToSelect(`insert into tabla set f1="A1", f2="A2"`)
assert.True(t, c)
assert.Equal(t, `SELECT * FROM tabla WHERE f1="A1" AND f2="A2"`, q)

q, c = dmlToSelect(`replace into tabla set f1="A1", f2="A2"`)
assert.True(t, c)
assert.Equal(t, `SELECT * FROM tabla WHERE f1="A1" AND f2="A2"`, q)

q, c = dmlToSelect("insert into `tabla-1` values(12)")
assert.True(t, c)
assert.Equal(t, "SELECT * FROM `tabla-1` LIMIT 1", q)

q, c = dmlToSelect(`UPDATE
employees2
SET
first_name = 'Joe',
emp_no = 10
WHERE
emp_no = 3`)
assert.True(t, c)
assert.Equal(t, "SELECT first_name = 'Joe', emp_no = 10 FROM employees2 WHERE emp_no = 3", q)
func Test_dmlToSelect(t *testing.T) {
JiriCtvrtka marked this conversation as resolved.
Show resolved Hide resolved
JiriCtvrtka marked this conversation as resolved.
Show resolved Hide resolved
JiriCtvrtka marked this conversation as resolved.
Show resolved Hide resolved
type testCase struct {
Query string
Converted bool
Expected string
}

testCases := []testCase{
{
Query: `SELECT nombre FROM tabla WHERE id = 0`,
Converted: false,
Expected: `SELECT nombre FROM tabla WHERE id = 0`,
},
{
Query: `update ignore tabla set nombre = "carlos" where id = 0 limit 2`,
Converted: true,
Expected: `SELECT nombre = "carlos" FROM tabla WHERE id = 0`,
},
{
Query: `update ignore tabla set nombre = "carlos" where id = 0`,
Converted: true,
Expected: `SELECT nombre = "carlos" FROM tabla WHERE id = 0`,
},
{
Query: `update ignore tabla set nombre = "carlos" limit 1`,
Converted: true,
Expected: `SELECT nombre = "carlos" FROM tabla`,
},
{
Query: `update tabla set nombre = "carlos" where id = 0 limit 2`,
Converted: true,
Expected: `SELECT nombre = "carlos" FROM tabla WHERE id = 0`,
},
{
Query: `update tabla set nombre = "carlos" where id = 0`,
Converted: true,
Expected: `SELECT nombre = "carlos" FROM tabla WHERE id = 0`,
},
{
Query: `update tabla set nombre = "carlos" limit 1`,
Converted: true,
Expected: `SELECT nombre = "carlos" FROM tabla`,
},
{
Query: `delete from tabla`,
Converted: true,
Expected: `SELECT * FROM tabla`,
},
{
Query: `delete from tabla join tabla2 on tabla.id = tabla2.tabla2_id`,
Converted: true,
Expected: `SELECT 1 FROM tabla join tabla2 on tabla.id = tabla2.tabla2_id`,
},
{
Query: `insert into tabla (f1, f2, f3) values (1,2,3)`,
Converted: true,
Expected: `SELECT * FROM tabla WHERE f1=1 and f2=2 and f3=3`,
},
{
Query: `insert into tabla (f1, f2, f3) values (1,2)`,
Converted: true,
Expected: `SELECT * FROM tabla LIMIT 1`,
},
{
Query: `insert into tabla set f1="A1", f2="A2"`,
Converted: true,
Expected: `SELECT * FROM tabla WHERE f1="A1" AND f2="A2"`,
},
{
Query: "insert into `tabla-1` values(12)",
Converted: true,
Expected: "SELECT * FROM `tabla-1` LIMIT 1",
},
{
Query: `UPDATE
employees2
SET
first_name = 'Joe',
emp_no = 10
WHERE
emp_no = 3`,
Converted: true,
Expected: `SELECT first_name = 'Joe', emp_no = 10 FROM employees2 WHERE emp_no = 3`,
},
{
Query: `
/* File:movie.php Line:8 Func:update_info */
SELECT
*
FROM
movie_info
WHERE
movie_id = 68357`,
Converted: false,
Expected: `SELECT * FROM movie_info WHERE movie_id = 68357`,
},
{
Query: `SELECT /*+ NO_RANGE_OPTIMIZATION(t3 PRIMARY, f2_idx) */ f1
FROM t3 WHERE f1 > 30 AND f1 < 33;`,
Converted: false,
Expected: `SELECT f1 FROM t3 WHERE f1 > 30 AND f1 < 33`,
},
{
Query: `SELECT /*+ BKA(t1) NO_BKA(t2) */ * FROM t1 INNER JOIN t2 WHERE ...;`,
Converted: false,
Expected: `SELECT * FROM t1 INNER JOIN t2 WHERE ...`,
},
{
Query: `SELECT /*+ NO_ICP(t1, t2) */ * FROM t1 INNER JOIN t2 WHERE ...;`,
Converted: false,
Expected: `SELECT * FROM t1 INNER JOIN t2 WHERE ...`,
},
{
Query: `SELECT /*+ SEMIJOIN(FIRSTMATCH, LOOSESCAN) */ * FROM t1 ...;`,
Converted: false,
Expected: `SELECT * FROM t1 ...`,
},
{
Query: `EXPLAIN SELECT /*+ NO_ICP(t1) */ * FROM t1 WHERE ...;`,
Converted: false,
Expected: ``,
},
{
Query: `SELECT /*+ MERGE(dt) */ * FROM (SELECT * FROM t1) AS dt;`,
Converted: false,
Expected: `SELECT * FROM (SELECT * FROM t1) AS dt`,
},
{
Query: `INSERT /*+ SET_VAR(foreign_key_checks=OFF) */ INTO t2 VALUES(2);`,
Converted: true,
Expected: `SELECT * FROM t2 LIMIT 1`,
},
}

for i, tc := range testCases {
tc := tc
t.Run(fmt.Sprintf("TestDMLToSelect %d. %s", i, tc.Query), func(t *testing.T) {
t.Parallel()
q, c := dmlToSelect(tc.Query)
assert.Equal(t, tc.Converted, c)
assert.Equal(t, tc.Expected, q)
})
}
}

q, c = dmlToSelect(`UPDATE employees2 SET first_name = 'Joe', emp_no = 10 WHERE emp_no = 3`)
assert.True(t, c)
assert.Equal(t, "SELECT first_name = 'Joe', emp_no = 10 FROM employees2 WHERE emp_no = 3", q)
func Test_isDMLQuery(t *testing.T) {
assert.True(t, isDMLQuery("SELECT * FROM table"))
assert.True(t, isDMLQuery(`update tabla set nombre = "carlos" where id = 0`))
assert.True(t, isDMLQuery("delete from tabla join tabla2 on tabla.id = tabla2.tabla2_id"))
assert.True(t, isDMLQuery("/*+ SET_VAR(foreign_key_checks=OFF) */ INSERT INTO t2 VALUES(2);"))
assert.False(t, isDMLQuery("EXPLAIN SELECT * FROM table"))
}
Loading