Skip to content

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

refactor: is mixed query (#13473)

Danny Xu55718762b83f
diff --git a/backend/plugin/parser/pg/query_span_extractor.go b/backend/plugin/parser/pg/query_span_extractor.go
index bd3bd806a8..b67ba2f156 100644
--- a/backend/plugin/parser/pg/query_span_extractor.go
+++ b/backend/plugin/parser/pg/query_span_extractor.go
@@ -103,13 +103,13 @@ func (q *querySpanExtractor) getQuerySpan(ctx context.Context, stmt string) (*ba
// This causes an error (NOT_FOUND) when using querySpanExtractor.findTableSchema.
// As a result, we exclude getting query span results for accessing only the system table.
allSystems, mixed := isMixedQuery(accessTables)
- if mixed != nil {
- return nil, mixed
+ if mixed {
+ return nil, base.MixUserSystemTablesError
}
if allSystems {
return &base.QuerySpan{
- Results: []base.QuerySpanResult{},
SourceColumns: base.SourceColumnSet{},
+ Results: []base.QuerySpanResult{},
}, nil
}
@@ -1722,7 +1722,7 @@ func (q *querySpanExtractor) getRangeVarsFromJSONRecursive(jsonData map[string]a
// figure out whether the table is the table the query actually accesses?
// Bytebase do not sync the system objects, so we skip finding for system objects in the metadata.
- if msg := isSystemResource(resource); msg == "" {
+ if !isSystemResource(resource) {
// Backfill the default database/schema name.
if resource.Database == "" {
resource.Database = currentDatabase
@@ -1774,42 +1774,39 @@ func (q *querySpanExtractor) getRangeVarsFromJSONRecursive(jsonData map[string]a
}
// isMixedQuery checks whether the query accesses the user table and system table at the same time.
-func isMixedQuery(m base.SourceColumnSet) (allSystems bool, mixed error) {
- userMsg, systemMsg := "", ""
+func isMixedQuery(m base.SourceColumnSet) (bool, bool) {
+ hasSystem, hasUser := false, false
for table := range m {
- if msg := isSystemResource(table); msg != "" {
- systemMsg = msg
- continue
- }
- userMsg = fmt.Sprintf("user table %q.%q", table.Schema, table.Table)
- if systemMsg != "" {
- return false, errors.Errorf("cannot access %s and %s at the same time", userMsg, systemMsg)
+ if isSystemResource(table) {
+ hasSystem = true
+ } else {
+ hasUser = true
}
}
- if userMsg != "" && systemMsg != "" {
- return false, errors.Errorf("cannot access %s and %s at the same time", userMsg, systemMsg)
+ if hasSystem && hasUser {
+ return false, true
}
- return userMsg == "" && systemMsg != "", nil
+ return !hasUser && hasSystem, false
}
-func isSystemResource(resource base.ColumnResource) string {
+func isSystemResource(resource base.ColumnResource) bool {
// User can access the system table/view by name directly without database/schema name.
// For example: `SELECT * FROM pg_database`, which will access the system table `pg_database`.
// Additionally, user can create a table/view with the same name with system table/view and access them
// by specify the schema name, for example:
// `CREATE TABLE pg_database(id INT); SELECT * FROM public.pg_database;` which will access the user table `pg_database`.
if IsSystemSchema(resource.Schema) {
- return fmt.Sprintf("system schema %q", resource.Schema)
+ return true
}
if resource.Database == "" && resource.Schema == "" && IsSystemView(resource.Table) {
- return fmt.Sprintf("system view %q", resource.Table)
+ return true
}
if resource.Database == "" && resource.Schema == "" && IsSystemTable(resource.Table) {
- return fmt.Sprintf("system table %q", resource.Table)
+ return true
}
- return ""
+ return false
}
func (q *querySpanExtractor) getColumnsForView(definition string) ([]base.QuerySpanResult, error) {
diff --git a/backend/plugin/parser/snowflake/query_span_extractor.go b/backend/plugin/parser/snowflake/query_span_extractor.go
index f7244f180e..77d5c29757 100644
--- a/backend/plugin/parser/snowflake/query_span_extractor.go
+++ b/backend/plugin/parser/snowflake/query_span_extractor.go
@@ -2,7 +2,6 @@ package snowflake
import (
"context"
- "fmt"
"sort"
"strconv"
@@ -61,13 +60,13 @@ func (q *querySpanExtractor) getQuerySpan(ctx context.Context, statement string)
// This causes an error (NOT_FOUND) when using querySpanExtractor.findTableSchema.
// As a result, we exclude getting query span results for accessing only the system table.
allSystems, mixed := isMixedQuery(accessTables, q.ignoreCaseSensitive)
- if mixed != nil {
- return nil, mixed
+ if mixed {
+ return nil, base.MixUserSystemTablesError
}
if allSystems {
return &base.QuerySpan{
- Results: []base.QuerySpanResult{},
SourceColumns: base.SourceColumnSet{},
+ Results: []base.QuerySpanResult{},
}, nil
}
@@ -1300,29 +1299,26 @@ func getAccessTables(currentNormalizedDatabase string, currentNormalizedSchema s
}
// isMixedQuery checks whether the query accesses the user table and system table at the same time.
-func isMixedQuery(m base.SourceColumnSet, ignoreCaseSensitive bool) (allSystems bool, mixed error) {
- userMsg, systemMsg := "", ""
+func isMixedQuery(m base.SourceColumnSet, ignoreCaseSensitive bool) (bool, bool) {
+ hasSystem, hasUser := false, false
for table := range m {
- if msg := isSystemResource(table, ignoreCaseSensitive); msg != "" {
- systemMsg = msg
- continue
- }
- userMsg = fmt.Sprintf("user table %q.%q", table.Schema, table.Table)
- if systemMsg != "" {
- return false, errors.Errorf("cannot access %s and %s at the same time", userMsg, systemMsg)
+ if isSystemResource(table, ignoreCaseSensitive) {
+ hasSystem = true
+ } else {
+ hasUser = true
}
}
- if userMsg != "" && systemMsg != "" {
- return false, errors.Errorf("cannot access %s and %s at the same time", userMsg, systemMsg)
+ if hasSystem && hasUser {
+ return false, true
}
- return userMsg == "" && systemMsg != "", nil
+ return !hasUser && hasSystem, false
}
-func isSystemResource(base.ColumnResource, bool) string {
+func isSystemResource(base.ColumnResource, bool) bool {
// TODO(zp): fix me.
- return ""
+ return false
}
type accessTablesListener struct {
diff --git a/backend/plugin/parser/tsql/query_span_extractor.go b/backend/plugin/parser/tsql/query_span_extractor.go
index e8deb8424f..7497c54afe 100644
--- a/backend/plugin/parser/tsql/query_span_extractor.go
+++ b/backend/plugin/parser/tsql/query_span_extractor.go
@@ -68,13 +68,13 @@ func (q *querySpanExtractor) getQuerySpan(ctx context.Context, statement string)
// This causes an error (NOT_FOUND) when using querySpanExtractor.findTableSchema.
// As a result, we exclude getting query span results for accessing only the system table.
allSystems, mixed := isMixedQuery(accessTables, q.ignoreCaseSensitive)
- if mixed != nil {
- return nil, mixed
+ if mixed {
+ return nil, base.MixUserSystemTablesError
}
if allSystems {
return &base.QuerySpan{
- Results: []base.QuerySpanResult{},
SourceColumns: base.SourceColumnSet{},
+ Results: []base.QuerySpanResult{},
}, nil
}
@@ -3435,34 +3435,31 @@ func (l *accessTableListener) EnterTable_source_item(ctx *parser.Table_source_it
}
// isMixedQuery checks whether the query accesses the user table and system table at the same time.
-func isMixedQuery(m base.SourceColumnSet, ignoreCaseSensitive bool) (allSystems bool, mixed error) {
- userMsg, systemMsg := "", ""
+func isMixedQuery(m base.SourceColumnSet, ignoreCaseSensitive bool) (bool, bool) {
+ hasSystem, hasUser := false, false
for table := range m {
- if msg := isSystemResource(table, ignoreCaseSensitive); msg != "" {
- systemMsg = msg
- continue
- }
- userMsg = fmt.Sprintf("user table %q.%q", table.Schema, table.Table)
- if systemMsg != "" {
- return false, errors.Errorf("cannot access %s and %s at the same time", userMsg, systemMsg)
+ if isSystemResource(table, ignoreCaseSensitive) {
+ hasSystem = true
+ } else {
+ hasUser = true
}
}
- if userMsg != "" && systemMsg != "" {
- return false, errors.Errorf("cannot access %s and %s at the same time", userMsg, systemMsg)
+ if hasSystem && hasUser {
+ return false, true
}
- return userMsg == "" && systemMsg != "", nil
+ return !hasUser && hasSystem, false
}
-func isSystemResource(resource base.ColumnResource, ignoreCaseSensitive bool) string {
+func isSystemResource(resource base.ColumnResource, ignoreCaseSensitive bool) bool {
if IsSystemDatabase(resource.Database, !ignoreCaseSensitive) {
- return fmt.Sprintf("system database %s", resource.Database)
+ return true
}
if IsSystemSchema(resource.Schema, !ignoreCaseSensitive) {
- return fmt.Sprintf("system schema %s", resource.Schema)
+ return true
}
- return ""
+ return false
}
// splitTableNameIntoNormalizedParts splits the table name into normalized 3 parts: database, schema, table.