From e6bc8a0b1b554b42d67595b9ca35dfeb013eefba Mon Sep 17 00:00:00 2001 From: BugsGuru Date: Tue, 29 Oct 2024 13:11:49 +0800 Subject: [PATCH] use util.Fingerprint to fix panic ce --- sqle/driver/mysql/util/parser_helper_test.go | 14 ++++++++++++++ sqle/pkg/driver/impl.go | 8 ++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/sqle/driver/mysql/util/parser_helper_test.go b/sqle/driver/mysql/util/parser_helper_test.go index 854668c1a3..d28bc66fe0 100644 --- a/sqle/driver/mysql/util/parser_helper_test.go +++ b/sqle/driver/mysql/util/parser_helper_test.go @@ -189,6 +189,20 @@ func TestFingerprint(t *testing.T) { input: "select * from tb1 where a='my_db' and b='test1'# this is a comment", expect: "SELECT * FROM `tb1` WHERE `a`=? AND `b`=?", }, + // not sql + { + input: "SELECT*FROM (SELECT * FROM tb values(1));", + expect: "SELECT*FROM (SELECT * FROM tb values(1));", + }, + { + input: "not sql", + expect: "not sql", + }, + // https://github.com/actiontech/sqle/issues/2603 + { + input: "insert into tb values(1)", + expect: "INSERT INTO `tb` VALUES (?)", + }, } for _, c := range cases { testFingerprint(t, c.input, c.expect) diff --git a/sqle/pkg/driver/impl.go b/sqle/pkg/driver/impl.go index 08835e21d7..add630348f 100644 --- a/sqle/pkg/driver/impl.go +++ b/sqle/pkg/driver/impl.go @@ -7,12 +7,12 @@ import ( "fmt" "time" + "github.com/actiontech/sqle/sqle/driver/mysql/util" driverV2 "github.com/actiontech/sqle/sqle/driver/v2" "github.com/actiontech/sqle/sqle/pkg/params" "github.com/actiontech/sqle/sqle/utils" hclog "github.com/hashicorp/go-hclog" - "github.com/percona/go-mysql/query" "github.com/pkg/errors" "vitess.io/vitess/go/vt/sqlparser" ) @@ -200,10 +200,14 @@ func (p *DriverImpl) Parse(ctx context.Context, sql string) ([]driverV2.Node, er nodes := make([]driverV2.Node, 0, len(sqls)) for _, sql := range sqls { + fp, err := util.Fingerprint(sql, true) + if err != nil { + return nil, errors.Wrapf(err, "fingerprint of sql: %s", sql) + } n := driverV2.Node{ Text: sql, Type: classifySQL(sql), - Fingerprint: query.Fingerprint(sql), + Fingerprint: fp, } nodes = append(nodes, n) }