Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore(query-span): ignore cluster name for StarRocks (#14110)

rebelicefd01c1cfe3e9
diff --git a/backend/plugin/parser/base/interface.go b/backend/plugin/parser/base/interface.go
index 622a98cba2..ed9378248c 100644
--- a/backend/plugin/parser/base/interface.go
+++ b/backend/plugin/parser/base/interface.go
@@ -190,6 +190,7 @@ func GetQuerySpan(ctx context.Context, gCtx GetQuerySpanContext, engine storepb.
if !ok {
return nil, nil
}
+ gCtx.Engine = engine
statements, err := SplitMultiSQL(engine, statement)
if err != nil {
return nil, err
diff --git a/backend/plugin/parser/base/span.go b/backend/plugin/parser/base/span.go
index b2fdc93978..e98214f4f9 100644
--- a/backend/plugin/parser/base/span.go
+++ b/backend/plugin/parser/base/span.go
@@ -8,6 +8,7 @@ import (
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/store/model"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
var (
@@ -287,6 +288,14 @@ type GetQuerySpanContext struct {
GetDatabaseMetadataFunc GetDatabaseMetadataFunc
ListDatabaseNamesFunc ListDatabaseNamesFunc
GetLinkedDatabaseMetadataFunc GetLinkedDatabaseMetadataFunc
+
+ // Adding the engine information here is a trade-off between the copy-pasted and shared code.
+ // For engines with more different, we implement the getQuerySpan separately.
+ // For some similar engines, we can share the same getQuerySpan implementation.
+ // But they may have some differences, so we need to pass the engine information here.
+ // No need to set this field when call GetQuerySpan, because the base.GetQuerySpan already has the engine information.
+ // We'll deal this field in the base.GetQuerySpan.
+ Engine storepb.Engine
}
// GetDatabaseMetadataFunc is the function to get database metadata.
diff --git a/backend/plugin/parser/mysql/query_span_extractor.go b/backend/plugin/parser/mysql/query_span_extractor.go
index 58c73aa933..ff20a160d1 100644
--- a/backend/plugin/parser/mysql/query_span_extractor.go
+++ b/backend/plugin/parser/mysql/query_span_extractor.go
@@ -10,6 +10,7 @@ import (
parsererror "github.com/bytebase/bytebase/backend/plugin/parser/errors"
"github.com/bytebase/bytebase/backend/store/model"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
@@ -1197,6 +1198,7 @@ func (q *querySpanExtractor) getAllTableColumnSources(databaseName, tableName st
}
func (q *querySpanExtractor) getFieldColumnSource(databaseName, tableName, fieldName string) (base.SourceColumnSet, error) {
+ databaseName = q.filterClusterName(databaseName)
findInTableSource := func(tableSource base.TableSource) (base.SourceColumnSet, bool) {
if q.ignoreCaseSensitive {
if databaseName != "" && !strings.EqualFold(databaseName, tableSource.GetDatabaseName()) {
@@ -1263,6 +1265,18 @@ func (q *querySpanExtractor) getFieldColumnSource(databaseName, tableName, field
}
}
+func (q *querySpanExtractor) filterClusterName(databaseName string) string {
+ if q.gCtx.Engine == storepb.Engine_STARROCKS {
+ // For StarRocks, user can use `cluster_name:database_name` as the database name.
+ // But for the query span in Bytebase, we only care about the database name.
+ list := strings.Split(databaseName, ":")
+ if len(list) > 1 {
+ databaseName = list[len(list)-1]
+ }
+ }
+ return databaseName
+}
+
func (q *querySpanExtractor) findTableSchema(databaseName, tableName string) (base.TableSource, error) {
// Each CTE name in one WITH clause must be unique, but we can use the same name in the different level CTE, such as:
//
@@ -1286,6 +1300,8 @@ func (q *querySpanExtractor) findTableSchema(databaseName, tableName string) (ba
databaseName = q.defaultDatabase
}
+ databaseName = q.filterClusterName(databaseName)
+
var dbSchema *model.DatabaseMetadata
allDatabaseNames, err := q.gCtx.ListDatabaseNamesFunc(q.ctx, q.gCtx.InstanceID)
if err != nil {
diff --git a/backend/plugin/parser/mysql/query_span_test.go b/backend/plugin/parser/mysql/query_span_test.go
index 06bdc9e698..5bb947d897 100644
--- a/backend/plugin/parser/mysql/query_span_test.go
+++ b/backend/plugin/parser/mysql/query_span_test.go
@@ -4,6 +4,7 @@ import (
"context"
"io"
"os"
+ "strings"
"testing"
"github.com/pkg/errors"
@@ -33,11 +34,17 @@ func TestGetQuerySpan(t *testing.T) {
testDataPaths = []string{
"test-data/query-span/standard.yaml",
"test-data/query-span/case_insensitive.yaml",
+ "test-data/query-span/starrocks.yaml",
}
)
a := require.New(t)
for _, testDataPath := range testDataPaths {
+ engine := storepb.Engine_MYSQL
+ if strings.Contains(testDataPath, "starrocks") {
+ engine = storepb.Engine_STARROCKS
+ }
+
testDataPath := testDataPath
yamlFile, err := os.Open(testDataPath)
@@ -56,6 +63,7 @@ func TestGetQuerySpan(t *testing.T) {
result, err := GetQuerySpan(context.TODO(), base.GetQuerySpanContext{
GetDatabaseMetadataFunc: databaseMetadataGetter,
ListDatabaseNamesFunc: databaseNameLister,
+ Engine: engine,
}, tc.Statement, tc.DefaultDatabase, "", tc.IgnoreCaseSensitve)
a.NoErrorf(err, "statement: %s", tc.Statement)
resultYaml := result.ToYaml()
diff --git a/backend/plugin/parser/mysql/test-data/query-span/starrocks.yaml b/backend/plugin/parser/mysql/test-data/query-span/starrocks.yaml
new file mode 100644
index 0000000000..e135d5bdc8
--- /dev/null
+++ b/backend/plugin/parser/mysql/test-data/query-span/starrocks.yaml
@@ -0,0 +1,90 @@
+- description: Test For cluster name
+ statement: SELECT * FROM v2
+ defaultDatabase: db
+ metadata: |-
+ {
+ "name": "db",
+ "schemas": [
+ {
+ "name": "",
+ "tables": [
+ {
+ "name": "products",
+ "columns": [
+ {
+ "name": "product_info"
+ }
+ ]
+ }
+ ],
+ "views": [
+ {
+ "name": "v1",
+ "definition": "SELECT `cluster_name:db`.`products`.`product_info` FROM `cluster_name:db`.`products`"
+ },
+ {
+ "name": "v2",
+ "definition": "SELECT `cluster_name:db`.`v1`.`product_info` FROM `cluster_name:db`.`v1`"
+ }
+ ]
+ }
+ ]
+ }
+ querySpan:
+ results:
+ - name: product_info
+ sourcecolumns:
+ - server: ""
+ database: db
+ schema: ""
+ table: products
+ column: product_info
+ sourcecolumns:
+ - server: ""
+ database: db
+ schema: ""
+ table: v2
+ column: ""
+- description: Test For cluster name
+ statement: SELECT * FROM v1
+ defaultDatabase: db
+ metadata: |-
+ {
+ "name": "db",
+ "schemas": [
+ {
+ "name": "",
+ "tables": [
+ {
+ "name": "products",
+ "columns": [
+ {
+ "name": "product_info"
+ }
+ ]
+ }
+ ],
+ "views": [
+ {
+ "name": "v1",
+ "definition": "SELECT `cluster_name:db`.`products`.`product_info` FROM `cluster_name:db`.`products`"
+ }
+ ]
+ }
+ ]
+ }
+ querySpan:
+ results:
+ - name: product_info
+ sourcecolumns:
+ - server: ""
+ database: db
+ schema: ""
+ table: products
+ column: product_info
+ sourcecolumns:
+ - server: ""
+ database: db
+ schema: ""
+ table: v1
+ column: ""