Skip to content

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

chore(query-span): improve error message for query span type not supported (#10397)

h3n4la549c9d31687
diff --git a/backend/plugin/parser/base/interface.go b/backend/plugin/parser/base/interface.go
index cdd387033a..9e0260e922 100644
--- a/backend/plugin/parser/base/interface.go
+++ b/backend/plugin/parser/base/interface.go
@@ -201,9 +201,13 @@ func GetQuerySpan(ctx context.Context, engine storepb.Engine, statement, databas
result, err := f(ctx, stmt.Text, database, getMetadataFunc)
if err != nil {
// Try to unwrap the error to see if it's a ResourceNotFoundError to decrease the error noise.
- var e *parsererror.ResourceNotFoundError
- if errors.As(err, &e) {
- return nil, e
+ var resourceNotFound *parsererror.ResourceNotFoundError
+ if errors.As(err, &resourceNotFound) {
+ return nil, resourceNotFound
+ }
+ var typeNotSupported *parsererror.TypeNotSupportedError
+ if errors.As(err, &typeNotSupported) {
+ return nil, typeNotSupported
}
return nil, err
}
diff --git a/backend/plugin/parser/errors/resources.go b/backend/plugin/parser/errors/resources.go
index 9402d0cabd..9e5e2e526c 100644
--- a/backend/plugin/parser/errors/resources.go
+++ b/backend/plugin/parser/errors/resources.go
@@ -47,3 +47,31 @@ func (e *ResourceNotFoundError) Error() string {
func (e *ResourceNotFoundError) Unwrap() error {
return e.Err
}
+
+// TypeNotSupportedError is returned when a type is not supported in the
+// query span, for example, using a function as table source.
+type TypeNotSupportedError struct {
+ Err error
+ Type string
+ Name string
+}
+
+func (e *TypeNotSupportedError) Error() string {
+ parts := []string{
+ fmt.Sprintf("type not supported: %s", e.Type),
+ }
+
+ if e.Err != nil {
+ parts = append(parts, fmt.Sprintf("err: %s", e.Err.Error()))
+ }
+
+ if e.Name != "" {
+ parts = append(parts, fmt.Sprintf("name: %s", e.Name))
+ }
+
+ return strings.Join(parts, ", ")
+}
+
+func (e *TypeNotSupportedError) Unwrap() error {
+ return e.Err
+}
diff --git a/backend/plugin/parser/pg/query_span_extractor.go b/backend/plugin/parser/pg/query_span_extractor.go
index 57378e819c..1c5177ec04 100644
--- a/backend/plugin/parser/pg/query_span_extractor.go
+++ b/backend/plugin/parser/pg/query_span_extractor.go
@@ -145,7 +145,7 @@ func (q *querySpanExtractor) extractTableSourceFromNode(node *pgquery.Node) (bas
case *pgquery.Node_JoinExpr:
return q.extractTableSourceFromJoin(node)
}
- return nil, nil
+ return nil, newTypeNotSupportedErrorByNode(node)
}
func (q *querySpanExtractor) extractTableSourceFromSelect(node *pgquery.Node_SelectStmt) (base.TableSource, error) {
@@ -1212,3 +1212,36 @@ func (q *querySpanExtractor) getColumnsForMaterializedView(definition string) ([
}
return span.Results, nil
}
+
+func newTypeNotSupportedErrorByNode(node *pgquery.Node) *parsererror.TypeNotSupportedError {
+ switch node := node.Node.(type) {
+ case *pgquery.Node_RangeFunction:
+ name := extractFunctionNameInRangeFunction(node.RangeFunction)
+ if name == "" {
+ return &parsererror.TypeNotSupportedError{
+ Type: "function",
+ Err: errors.Errorf("node: %+v", node),
+ }
+ }
+ return &parsererror.TypeNotSupportedError{
+ Type: "function",
+ Name: name,
+ }
+ default:
+ return &parsererror.TypeNotSupportedError{
+ Err: errors.Errorf("node: %+v", node),
+ }
+ }
+}
+
+func extractFunctionNameInRangeFunction(node *pgquery.RangeFunction) string {
+ // Capture the function name from the range function.
+
+ // regex := regexp.MustCompile(`funcname:{string:{sval:""}}`)
+ regex := regexp.MustCompile(`funcname:{string:{sval:"(?P<funcname>[a-zA-Z0-9_]+)"}}`)
+ match := regex.FindStringSubmatch(fmt.Sprintf("%+v", node))
+ if len(match) == 0 {
+ return ""
+ }
+ return match[1]
+}