Skip to content

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

feat: migrate Redshift query span to Omni (#20557)

rebelice4c7bb48117db
diff --git a/backend/plugin/parser/redshift/access_tables.go b/backend/plugin/parser/redshift/access_tables.go
deleted file mode 100644
index 03513a27c9..0000000000
--- a/backend/plugin/parser/redshift/access_tables.go
+++ /dev/null
@@ -1,184 +0,0 @@
-package redshift
-
-import (
- "context"
-
- "github.com/antlr4-go/antlr/v4"
- parser "github.com/bytebase/parser/redshift"
- "github.com/pkg/errors"
-
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
-)
-
-// ExtractAccessTablesOption configures access table extraction behavior.
-type ExtractAccessTablesOption struct {
- // DefaultDatabase is the default database name to use when table reference doesn't specify one.
- DefaultDatabase string
- // DefaultSchema is the default schema name to use when table reference doesn't specify one.
- DefaultSchema string
- // SkipMetadataValidation skips metadata lookup and validation.
- // When true, returns all table references found in SQL without checking if they exist.
- SkipMetadataValidation bool
- // GetDatabaseMetadata is the function to get database metadata (optional, only used when SkipMetadataValidation is false).
- GetDatabaseMetadata base.GetDatabaseMetadataFunc
- // Ctx is the context for metadata lookup (optional, only used when SkipMetadataValidation is false).
- Ctx context.Context
- // InstanceID is the instance ID for metadata lookup (optional, only used when SkipMetadataValidation is false).
- InstanceID string
-}
-
-// ExtractAccessTables extracts all table/view references from a SQL statement.
-// This is a lightweight version that doesn't perform full query span analysis.
-func ExtractAccessTables(statement string, option ExtractAccessTablesOption) ([]base.ColumnResource, error) {
- parseResults, err := ParseRedshift(statement)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to parse PostgreSQL statement")
- }
-
- if len(parseResults) != 1 {
- return nil, errors.Errorf("expected exactly 1 statement, got %d", len(parseResults))
- }
-
- searchPath := []string{option.DefaultSchema}
- if option.DefaultSchema == "" {
- searchPath = []string{"public"}
- }
-
- extractor := &accessTableExtractor{
- defaultDatabase: option.DefaultDatabase,
- searchPath: searchPath,
- getDatabaseMetadata: option.GetDatabaseMetadata,
- ctx: option.Ctx,
- instanceID: option.InstanceID,
- skipMetadataValidation: option.SkipMetadataValidation,
- }
-
- antlr.ParseTreeWalkerDefault.Walk(extractor, parseResults[0].Tree)
-
- if extractor.err != nil {
- return nil, errors.Wrapf(extractor.err, "failed to extract access tables")
- }
-
- return extractor.accessTables, nil
-}
-
-type accessTableExtractor struct {
- *parser.BaseRedshiftParserListener
- err error
- defaultDatabase string
- searchPath []string
- accessTables []base.ColumnResource
- getDatabaseMetadata base.GetDatabaseMetadataFunc
- ctx context.Context
- instanceID string
- skipMetadataValidation bool
-}
-
-func (a *accessTableExtractor) EnterQualified_name(qn *parser.Qualified_nameContext) {
- if a.err != nil {
- return
- }
- resource := base.ColumnResource{
- Database: a.defaultDatabase,
- }
- var directions []string
- qnLength := 1
- directions = append(directions, normalizeRedshiftColid(qn.Colid()))
- if qn.Indirection() != nil {
- allIndirectionElements := qn.Indirection().AllIndirection_el()
- for _, el := range allIndirectionElements {
- if el.Attr_name() != nil {
- directions = append(directions, normalizeRedshiftCollabel(el.Attr_name().Collabel()))
- qnLength++
- break
- }
- continue
- }
- }
-
- switch qnLength {
- case 1:
- resource.Table = directions[0]
- case 2:
- resource.Schema = directions[0]
- resource.Table = directions[1]
- case 3:
- resource.Database = directions[0]
- resource.Schema = directions[1]
- resource.Table = directions[2]
- default:
- a.err = errors.Errorf("improper qualified name (too many dotted names): %s", qn.GetParser().GetTokenStream().GetTextFromInterval(qn.GetSourceInterval()))
- return
- }
-
- if a.skipMetadataValidation {
- // If schema is not specified, use the first schema in search path as default
- if resource.Schema == "" && !isSystemResource(resource) {
- if len(a.searchPath) > 0 {
- resource.Schema = a.searchPath[0]
- }
- }
- a.accessTables = append(a.accessTables, resource)
- return
- }
-
- if !isSystemResource(resource) {
- searchPath := a.searchPath
- if resource.Schema != "" {
- searchPath = []string{resource.Schema}
- }
-
- _, databaseMetadata, err := a.getDatabaseMetadata(a.ctx, a.instanceID, a.defaultDatabase)
- if err != nil {
- a.err = errors.Wrapf(err, "failed to get database metadata for database: %s", a.defaultDatabase)
- return
- }
- // Access pseudo table or table/view we do not sync, return directly.
- if databaseMetadata == nil {
- return
- }
- schemaName, name := databaseMetadata.SearchObject(searchPath, resource.Table)
- if schemaName == "" && name == "" {
- return
- }
- resource.Schema = schemaName
- }
-
- a.accessTables = append(a.accessTables, resource)
-}
-
-// isMixedQuery checks whether the query accesses the user table and system table at the same time.
-func isMixedQuery(m base.SourceColumnSet) (bool, bool) {
- hasSystem, hasUser := false, false
- for table := range m {
- if isSystemResource(table) {
- hasSystem = true
- } else {
- hasUser = true
- }
- }
-
- if hasSystem && hasUser {
- return false, true
- }
-
- return !hasUser && hasSystem, false
-}
-
-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 true
- }
- if resource.Schema == "" && isSystemView(resource.Table) {
- return true
- }
- if resource.Schema == "" && isSystemTable(resource.Table) {
- return true
- }
- return false
-}
diff --git a/backend/plugin/parser/redshift/query_span.go b/backend/plugin/parser/redshift/query_span.go
index 740ccc8cc4..e507db934b 100644
--- a/backend/plugin/parser/redshift/query_span.go
+++ b/backend/plugin/parser/redshift/query_span.go
@@ -26,11 +26,11 @@ func GetQuerySpan(ctx context.Context, gCtx base.GetQuerySpanContext, stmt base.
if schema != "" {
searchPath = []string{schema}
}
- extractor := newQuerySpanExtractor(database, searchPath, gCtx)
+ extractor := newOmniQuerySpanExtractor(database, searchPath, gCtx)
- // Use the new ANTLR-based implementation
- querySpan, err := extractor.getQuerySpan(ctx, stmt.Text)
+ querySpan, err := extractor.getOmniQuerySpan(ctx, stmt.Text)
if err != nil {
+ err = convertOmniError(err, stmt)
return nil, errors.Wrapf(err, "failed to get query span from statement: %s", stmt.Text)
}
return querySpan, nil
diff --git a/backend/plugin/parser/redshift/query_span_extractor.go b/backend/plugin/parser/redshift/query_span_extractor.go
deleted file mode 100644
index c0c90d5793..0000000000
--- a/backend/plugin/parser/redshift/query_span_extractor.go
+++ /dev/null
@@ -1,3648 +0,0 @@
-package redshift
-
-import (
- "context"
- "fmt"
- "slices"
- "strings"
-
- "github.com/antlr4-go/antlr/v4"
- "github.com/pkg/errors"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
-
- parser "github.com/bytebase/parser/redshift"
-
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
- "github.com/bytebase/bytebase/backend/store/model"
-)
-
-const (
- rsUnknownFieldName = "?column?"
- generateSeries = "generate_series"
- generateSubscripts = "generate_subscripts"
- unnest = "unnest"
- jsonbEach = "jsonb_each"
- jsonEach = "json_each"
- jsonbEachText = "jsonb_each_text"
- jsonEachText = "json_each_text"
- jsonPopulateRecord = "json_populate_record"
- jsonbPopulateRecord = "jsonb_populate_record"
- jsonPopulateRecordset = "json_populate_recordset"
- jsonbPopulateRecordset = "jsonb_populate_recordset"
- jsonToRecord = "json_to_record"
- jsonbToRecord = "jsonb_to_record"
- jsonToRecordset = "json_to_recordset"
- jsonbToRecordset = "jsonb_to_recordset"
-)
-
-// querySpanExtractor is the extractor to extract the query span from the given pgquery.RawStmt.
-type querySpanExtractor struct {
- *parser.BaseRedshiftParserListener
- ctx context.Context
- defaultDatabase string
- searchPath []string
- // The metaCache serves as a lazy-load cache for the database metadata and should not be accessed directly.
- // Instead, use querySpanExtractor.getDatabaseMetadata to access it.
- metaCache map[string]*model.DatabaseMetadata
-
- gCtx base.GetQuerySpanContext
-
- // Private fields.
-
- ctes []*base.PseudoTable
-
- // outerTableSource is the list of table sources from the outer query,
- // it's used to resolve the column name in the correlated subquery.
- outerTableSources []base.TableSource
-
- // tableSourcesFrom is the list of table sources from the FROM clause.
- tableSourcesFrom []base.TableSource
-
- // sourceColumnsInFunction is the source columns in the function.
- // It's used to resolve defined functions body as a table source.
- sourceColumnsInFunction base.SourceColumnSet
-
- // variables are variables declared in the function.
- variables map[string]*base.QuerySpanResult
-
- err error
-
- // resultTableSource is used to store the extracted table source from ANTLR parsing
- resultTableSource base.TableSource
-}
-
-// newQuerySpanExtractor creates a new query span extractor, the databaseMetadata and the ast are in the read guard.
-func newQuerySpanExtractor(defaultDatabase string, searchPath []string, gCtx base.GetQuerySpanContext) *querySpanExtractor {
- if len(searchPath) == 0 {
- searchPath = []string{"public"}
- }
- return &querySpanExtractor{
- defaultDatabase: defaultDatabase,
- searchPath: searchPath,
- metaCache: make(map[string]*model.DatabaseMetadata),
- gCtx: gCtx,
- sourceColumnsInFunction: make(base.SourceColumnSet),
- }
-}
-
-func (q *querySpanExtractor) getDatabaseMetadata(database string) (*model.DatabaseMetadata, error) {
- if meta, ok := q.metaCache[database]; ok {
- return meta, nil
- }
- _, meta, err := q.gCtx.GetDatabaseMetadataFunc(q.ctx, q.gCtx.InstanceID, database)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to get database metadata for database: %s", database)
- }
- q.metaCache[database] = meta
- return meta, nil
-}
-
-// Add this field to querySpanExtractor struct (defined in query_span_extractor.go):
-// resultTableSource base.TableSource // Stores the extracted table source from EnterStmtmulti
-
-// getQuerySpan is the ANTLR-based implementation of query span extraction.
-func (q *querySpanExtractor) getQuerySpan(ctx context.Context, stmt string) (*base.QuerySpan, error) {
- q.ctx = ctx
-
- // Parse the statement using ANTLR
- parseResults, err := ParseRedshift(stmt)
- if err != nil {
- return nil, err
- }
-
- if len(parseResults) != 1 {
- return nil, errors.Errorf("expected exactly 1 statement, got %d", len(parseResults))
- }
-
- parseResult := parseResults[0]
-
- // Walk the tree to extract access tables
-
- accessTableExtractor := &accessTableExtractor{
- defaultDatabase: q.defaultDatabase,
- searchPath: q.searchPath,
- getDatabaseMetadata: q.gCtx.GetDatabaseMetadataFunc,
- ctx: ctx,
- instanceID: q.gCtx.InstanceID,
- }
- antlr.ParseTreeWalkerDefault.Walk(accessTableExtractor, parseResult.Tree)
- if accessTableExtractor.err != nil {
- return nil, errors.Wrapf(accessTableExtractor.err, "failed to extract query span from statement: %s", stmt)
- }
-
- // Build access map
- accessesMap := make(base.SourceColumnSet)
- for _, resource := range accessTableExtractor.accessTables {
- accessesMap[resource] = true
- }
-
- // Check for mixed system/user tables
- allSystems, mixed := isMixedQuery(accessesMap)
- if mixed {
- return nil, base.MixUserSystemTablesError
- }
-
- // Get query type using ANTLR-based detection
- queryTypeListener := &queryTypeListener{
- result: base.QueryTypeUnknown,
- allSystems: allSystems,
- }
- antlr.ParseTreeWalkerDefault.Walk(queryTypeListener, parseResult.Tree)
- if queryTypeListener.err != nil {
- return nil, errors.Wrapf(queryTypeListener.err, "failed to get query type from statement: %s", stmt)
- }
-
- queryType, isExplainAnalyze := queryTypeListener.result, queryTypeListener.isExplainAnalyze
-
- // For non-SELECT queries, return early
- if queryType != base.Select {
- return &base.QuerySpan{
- Type: queryType,
- SourceColumns: base.SourceColumnSet{},
- Results: []base.QuerySpanResult{},
- }, nil
- }
-
- // For EXPLAIN ANALYZE SELECT
- if isExplainAnalyze {
- return &base.QuerySpan{
- Type: queryType,
- SourceColumns: accessesMap,
- Results: []base.QuerySpanResult{},
- }, nil
- }
-
- // Extract table source using ANTLR (this was done in EnterStmtmulti)
- // The resultTableSource should have been populated by EnterStmtmulti
- if q.resultTableSource == nil {
- // If not populated, try to extract it now
- root, ok := parseResult.Tree.(*parser.RootContext)
- if !ok {
- return nil, errors.Errorf("failed to assert parse tree to RootContext")
- }
- if root.Stmtblock() != nil && root.Stmtblock().Stmtmulti() != nil {
- stmtmulti := root.Stmtblock().Stmtmulti()
- if len(stmtmulti.AllStmt()) == 1 {
- tableSource, err := q.extractTableSourceFromStmt(stmtmulti.AllStmt()[0])
- if err != nil {
- var functionNotSupported *base.FunctionNotSupportedError
- if errors.As(err, &functionNotSupported) {
- if len(accessesMap) == 0 {
- accessesMap[base.ColumnResource{
- Database: q.defaultDatabase,
- }] = true
- }
- return &base.QuerySpan{
- Type: base.Select,
- SourceColumns: accessesMap,
- Results: []base.QuerySpanResult{},
- NotFoundError: functionNotSupported,
- }, nil
- }
- var resourceNotFound *base.ResourceNotFoundError
- if errors.As(err, &resourceNotFound) {
- if len(accessesMap) == 0 {
- accessesMap[base.ColumnResource{
- Database: q.defaultDatabase,
- }] = true
- }
- return &base.QuerySpan{
- Type: base.Select,
- SourceColumns: accessesMap,
- Results: []base.QuerySpanResult{},
- NotFoundError: resourceNotFound,
- }, nil
- }
- return nil, err
- }
- q.resultTableSource = tableSource
- }
- }
- }
-
- // Merge the source columns in functions to the access tables
- for source := range q.sourceColumnsInFunction {
- accessesMap[source] = true
- }
-
- // Build the final query span result
- var results []base.QuerySpanResult
- if q.resultTableSource != nil {
- results = q.resultTableSource.GetQuerySpanResult()
- }
-
- return &base.QuerySpan{
- Type: base.Select,
- SourceColumns: accessesMap,
- Results: results,
- }, nil
-}
-
-type functionDefinition struct {
- schemaName string
- metadata *storepb.FunctionMetadata
-}
-
-func (q *querySpanExtractor) findFunctionDefine(schemaName, funcName string, nArgs int) (base.TableSource, error) {
- dbMetadata, err := q.getDatabaseMetadata(q.defaultDatabase)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to get database metadata for database: %s", q.defaultDatabase)
- }
- if dbMetadata == nil {
- return nil, &base.ResourceNotFoundError{
- Database: &q.defaultDatabase,
- }
- }
- var funcs []*functionDefinition
- searchPath := q.searchPath
- if schemaName != "" {
- searchPath = []string{schemaName}
- }
- schemas, functions := dbMetadata.SearchFunctions(searchPath, funcName)
- for i, fun := range functions {
- funcs = append(funcs, &functionDefinition{
- schemaName: schemas[i],
- metadata: fun,
- })
- }
-
- candidates, err := getFunctionCandidates(funcs, nArgs, funcName)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to get function candidates")
- }
- if len(candidates) == 0 {
- return nil, &base.ResourceNotFoundError{
- Database: &q.defaultDatabase,
- Schema: &schemaName,
- Function: &funcName,
- }
- }
-
- if len(candidates) > 1 && candidates[0].schemaName == candidates[1].schemaName {
- return nil, errors.Errorf("ambiguous function call: %s", funcName)
- }
-
- function := candidates[0]
- functionName := fmt.Sprintf("%s.%s", function.schemaName, funcName)
- columns, err := q.getColumnsFromFunction(functionName, function.metadata.Definition)
- if err != nil {
- return nil, &base.FunctionNotSupportedError{
- Err: err,
- Function: functionName,
- }
- }
- return &base.PseudoTable{
- Columns: columns,
- }, nil
-}
-
-type functionDefinitionDetail struct {
- nDefaultParam int
- nVariadicParam int
- nParam int
- function *functionDefinition
-}
-
-func buildFunctionDefinitionDetail(funcDef *functionDefinition) (*functionDefinitionDetail, error) {
- function := funcDef.metadata
- definition := function.GetDefinition()
- parseResults, err := ParseRedshift(definition)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to parse function definition: %s", definition)
- }
-
- if len(parseResults) != 1 {
- return nil, errors.Errorf("expected exactly 1 statement in function definition, got %d", len(parseResults))
- }
-
- // Navigate: root -> stmtblock -> stmt -> createfunctionstmt
- root, ok := parseResults[0].Tree.(*parser.RootContext)
- if !ok {
- return nil, errors.Errorf("expecting RootContext but got %T", parseResults[0].Tree)
- }
-
- stmtblock := root.Stmtblock()
- if stmtblock == nil {
- return nil, errors.Errorf("expecting stmtblock but got nil")
- }
-
- stmtmulti := stmtblock.Stmtmulti()
- if stmtmulti == nil {
- return nil, errors.Errorf("expecting stmtmulti but got nil")
- }
-
- stmts := stmtmulti.AllStmt()
- if len(stmts) != 1 {
- return nil, errors.Errorf("expecting 1 statement, but got %d", len(stmts))
- }
-
- stmt := stmts[0]
- createFuncStmt := stmt.Createfunctionstmt()
- if createFuncStmt == nil {
- return nil, errors.Errorf("expecting Createfunctionstmt but got nil")
- }
-
- // Redshift uses Func_py_args_or_sql_args for both SQL and Python UDFs.
- // Unlike PostgreSQL, Redshift function parameters don't support default values or VARIADIC.
- // The parameter syntax is the same regardless of language (SQL or plpythonu).
- funcArgs := createFuncStmt.Func_py_args_or_sql_args()
- var nParam int
- if funcArgs != nil {
- if argList := funcArgs.Func_py_args_or_sql_args_list(); argList != nil {
- nParam = len(argList.AllFunc_type())
- }
- }
-
- return &functionDefinitionDetail{
- nDefaultParam: 0,
- nVariadicParam: 0,
- nParam: nParam,
- function: funcDef,
- }, nil
-}
-
-// getFunctionCandidates returns the function candidates for the function call.
-func getFunctionCandidates(functions []*functionDefinition, nArgs int, funcName string) ([]*functionDefinition, error) {
- // Filter by name only.
- var nameFiltered []*functionDefinition
- for _, function := range functions {
- if function.metadata.GetName() != funcName {
- continue
- }
- nameFiltered = append(nameFiltered, function)
- }
-
- if len(nameFiltered) == 0 {
- return nil, nil
- }
- // If there is only one function with the same name, we return it directly,
- // PostgreSQL would throw an error if the argument does not match the function signature.
- if len(nameFiltered) == 1 {
- return nameFiltered, nil
- }
-
- detail := make([]*functionDefinitionDetail, 0, len(nameFiltered))
- for _, function := range nameFiltered {
- d, err := buildFunctionDefinitionDetail(function)
- if err != nil {
- return nil, err
- }
- if d == nil {
- continue
- }
- detail = append(detail, d)
- }
-
- var candidates []*functionDefinition
- for _, d := range detail {
- // If there are no default and variadic parameters, the number of arguments must match the number of parameters.
- if d.nDefaultParam == 0 && d.nVariadicParam == 0 {
- if nArgs == d.nParam {
- candidates = append(candidates, d.function)
- }
- continue
- }
-
- // Default parameter matches 0 or 1 argument, and variadic parameter matches 0 or more arguments.
- lbound := d.nParam - d.nDefaultParam - d.nVariadicParam
- ubound := d.nParam
- if d.nVariadicParam > 0 && ubound < nArgs {
- // Hack to make variadic parameter match 0 or more arguments.
- ubound = nArgs
- }
- if nArgs >= lbound && nArgs <= ubound {
- candidates = append(candidates, d.function)
- }
- }
-
- return candidates, nil
-}
-
-type languageType int
-
-const (
- languageTypeSQL languageType = iota
- languageTypePLPGSQL
-)
-
-func (q *querySpanExtractor) getColumnsFromFunction(name, definition string) ([]base.QuerySpanResult, error) {
- parseResults, err := ParseRedshift(definition)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to parse function definition: %s", definition)
- }
-
- if len(parseResults) != 1 {
- return nil, errors.Errorf("expected exactly 1 statement in function definition, got %d", len(parseResults))
- }
-
- // Navigate: root -> stmtblock -> stmt -> createfunctionstmt
- root, ok := parseResults[0].Tree.(*parser.RootContext)
- if !ok {
- return nil, errors.Errorf("expecting RootContext but got %T", parseResults[0].Tree)
- }
-
- stmtblock := root.Stmtblock()
- if stmtblock == nil {
- return nil, errors.Errorf("expecting stmtblock but got nil")
- }
-
- stmtmulti := stmtblock.Stmtmulti()
- if stmtmulti == nil {
- return nil, errors.Errorf("expecting stmtmulti but got nil")
- }
-
- stmts := stmtmulti.AllStmt()
- if len(stmts) != 1 {
- return nil, errors.Errorf("expecting 1 statement, but got %d", len(stmts))
- }
-
- stmt := stmts[0]
- createFuncStmt := stmt.Createfunctionstmt()
- if createFuncStmt == nil {
- return nil, errors.Errorf("expecting Createfunctionstmt but got nil")
- }
-
- // Extract language from function options
- language, err := q.extractLanguageFromCreateFunction(createFuncStmt, name)
- if err != nil {
- return nil, err
- }
-
- // Extract AS body from function options
- _, err = q.extractAsBodyFromCreateFunction(createFuncStmt, name)
- if err != nil {
- return nil, err
- }
-
- switch language {
- case languageTypeSQL:
- // SQL functions will be handled below
- return q.getColumnsFromSQLFunction(createFuncStmt, name)
- case languageTypePLPGSQL:
- return q.getColumnsFromPLPGSQLFunction(createFuncStmt, name, definition)
- default:
- return nil, errors.Errorf("unsupported language type: %v", language)
- }
-}
-
-func (q *querySpanExtractor) getColumnsFromSQLFunction(createFuncStmt parser.ICreatefunctionstmtContext, name string) ([]base.QuerySpanResult, error) {
- asBody, err := q.extractAsBodyFromCreateFunction(createFuncStmt, name)
- if err != nil {
- return nil, err
- }
-
- columnNames := q.extractParameterNamesFromCreateFunction(createFuncStmt)
-
- newQ := newQuerySpanExtractor(q.defaultDatabase, q.searchPath, q.gCtx)
- span, err := newQ.getQuerySpan(q.ctx, asBody)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to get query span for function: %s", name)
- }
- if span.NotFoundError != nil {
- return nil, errors.Wrapf(span.NotFoundError, "failed to get query span for function: %s", name)
- }
- for source := range span.SourceColumns {
- q.sourceColumnsInFunction[source] = true
- }
-
- if len(columnNames) != len(span.Results) {
- return nil, errors.Errorf("expecting %d columns but got %d for function: %s", len(columnNames), len(span.Results), name)
- }
- for i, columnName := range columnNames {
- span.Results[i].Name = columnName
- }
- return span.Results, nil
-}
-
-func (q *querySpanExtractor) getColumnsFromPLPGSQLFunction(createFuncStmt parser.ICreatefunctionstmtContext, name, definition string) ([]base.QuerySpanResult, error) {
- // Extract OUT/TABLE parameter names
- columnNames := q.extractParameterNamesFromCreateFunction(createFuncStmt)
-
- // Extract AS body
- asBody, err := q.extractAsBodyFromCreateFunction(createFuncStmt, name)
- if err != nil {
- return nil, err
- }
-
- // Try simple extraction first (extract RETURN QUERY statements)
- simpleResult, err := q.getColumnsFromSimplePLPGSQL(name, asBody, columnNames)
- if err == nil {
- return simpleResult, nil
- }
-
- // Fall back to complex extraction
- return q.getColumnsFromComplexPLPGSQL(name, columnNames, definition)
-}
-
-// getColumnsFromSimplePLPGSQL extracts query spans from simple PL/pgSQL functions with RETURN QUERY.
-func (q *querySpanExtractor) getColumnsFromSimplePLPGSQL(name, asBody string, columnNames []string) ([]base.QuerySpanResult, error) {
- // Parse the PL/pgSQL body to extract RETURN QUERY statements
- sqlList, err := extractReturnQueryStatements(asBody)
- if err != nil {
- return nil, err
- }
-
- if len(sqlList) == 0 {
- return nil, errors.Errorf("no RETURN QUERY statements found in function: %s", name)
- }
-
- // Initialize result with column names and empty source columns
- var leftQuerySpanResult []base.QuerySpanResult
- for _, columnName := range columnNames {
- leftQuerySpanResult = append(leftQuerySpanResult, base.QuerySpanResult{
- Name: columnName,
- SourceColumns: base.SourceColumnSet{},
- })
- }
-
- // Process each RETURN QUERY statement and merge source columns
- for _, sql := range sqlList {
- newQ := newQuerySpanExtractor(q.defaultDatabase, q.searchPath, q.gCtx)
- span, err := newQ.getQuerySpan(q.ctx, sql)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to get query span for function: %s", name)
- }
- if span.NotFoundError != nil {
- return nil, errors.Wrapf(span.NotFoundError, "failed to get query span for function: %s", name)
- }
-
- rightQuerySpanResult := span.Results
- if len(leftQuerySpanResult) != len(rightQuerySpanResult) {
- return nil, errors.Errorf("expecting %d columns but got %d for function: %s", len(leftQuerySpanResult), len(rightQuerySpanResult), name)
- }
- for source := range span.SourceColumns {
- q.sourceColumnsInFunction[source] = true
- }
-
- // Merge source columns
- var result []base.QuerySpanResult
- for i, leftSpanResult := range leftQuerySpanResult {
- rightSpanResult := rightQuerySpanResult[i]
- newResourceColumns, _ := base.MergeSourceColumnSet(leftSpanResult.SourceColumns, rightSpanResult.SourceColumns)
- result = append(result, base.QuerySpanResult{
- Name: leftSpanResult.Name,
- SourceColumns: newResourceColumns,
- })
- }
- leftQuerySpanResult = result
- }
-
- return leftQuerySpanResult, nil
-}
-
-// extractReturnQueryStatements extracts all RETURN QUERY SELECT statements from PL/pgSQL body.
-func extractReturnQueryStatements(asBody string) ([]string, error) {
- // Parse the PL/pgSQL body as a pl_block (BEGIN...END block)
- res, err := ParseRedshiftPLBlock(asBody)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to parse PL/pgSQL body")
- }
-
- listener := &returnQueryListener{
- tokenStream: res.Tokens,
- sqlList: []string{},
- }
- antlr.ParseTreeWalkerDefault.Walk(listener, res.Tree)
-
- return listener.sqlList, nil
-}
-
-// returnQueryListener walks the AST to find all RETURN QUERY statements.
-type returnQueryListener struct {
- *parser.BaseRedshiftParserListener
- tokenStream antlr.TokenStream
- sqlList []string
-}
-
-// EnterStmt_return is called when entering a RETURN statement.
-func (l *returnQueryListener) EnterStmt_return(ctx *parser.Stmt_returnContext) {
- // Check if this is RETURN QUERY (not RETURN NEXT or plain RETURN)
- if ctx.QUERY() == nil {
- return
- }
-
- // Check if it's RETURN QUERY SELECT (not RETURN QUERY EXECUTE)
- if ctx.Selectstmt() == nil {
- return
- }
-
- // Extract the SELECT statement text using GetTextFromInterval
- selectStmt := ctx.Selectstmt()
- start := selectStmt.GetStart().GetTokenIndex()
- stop := selectStmt.GetStop().GetTokenIndex()
- interval := antlr.NewInterval(start, stop)
- sqlText := l.tokenStream.GetTextFromInterval(interval)
-
- l.sqlList = append(l.sqlList, sqlText)
-}
-
-func (q *querySpanExtractor) getColumnsFromComplexPLPGSQL(name string, columnNames []string, definition string) ([]base.QuerySpanResult, error) {
- parseResults, err := ParseRedshift(definition)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to parse PLpgSQL function body for function %s", name)
- }
-
- if len(parseResults) != 1 {
- return nil, errors.Errorf("expected exactly 1 statement in function definition, got %d", len(parseResults))
- }
-
- listener := &plpgSQLListener{
- q: q,
- variables: make(map[string]*base.QuerySpanResult),
- }
- antlr.ParseTreeWalkerDefault.Walk(listener, parseResults[0].Tree)
- if listener.err != nil {
- return nil, errors.Wrapf(listener.err, "failed to extract table source from PLpgSQL function body for function %s", name)
- }
- if listener.span == nil {
- return nil, errors.Errorf("failed to extract table source from PLpgSQL function body for function %s", name)
- }
- if listener.span.NotFoundError != nil {
- return nil, errors.Wrapf(listener.span.NotFoundError, "failed to extract table source from PLpgSQL function body for function %s", name)
- }
- if len(columnNames) != len(listener.span.Results) {
- return nil, errors.Errorf("expecting %d columns but got %d for function: %s", len(columnNames), len(listener.span.Results), name)
- }
- var result []base.QuerySpanResult
- for i, columnName := range columnNames {
- result = append(result, base.QuerySpanResult{
- Name: columnName,
- SourceColumns: listener.span.Results[i].SourceColumns,
- })
- }
- return result, nil
-}
-
-// extractLanguageFromCreateFunction extracts the LANGUAGE clause from a CREATE FUNCTION statement.
-// Returns languageTypeSQL (default) or languageTypePLPGSQL, or error if the language is unsupported.
-func (*querySpanExtractor) extractLanguageFromCreateFunction(createFuncStmt parser.ICreatefunctionstmtContext, funcName string) (languageType, error) {
- // Default language is SQL if not specified
- language := languageTypeSQL
-
- // Parse function options to find LANGUAGE
- createOptList := createFuncStmt.Createfunc_opt_list()
- if createOptList == nil {
- // No options specified, use default
- return language, nil
- }
-
- for _, item := range createOptList.AllCreatefunc_opt_item() {
- if item == nil {
- continue
- }
-
- // Check if this option is LANGUAGE
- if item.LANGUAGE() == nil {
- continue
- }
-
- // Extract language value
- langNode := item.Nonreservedword_or_sconst()
- if langNode == nil {
- continue
- }
-
- // Get the language text and normalize it
- langText := langNode.GetText()
- // Remove quotes if present (e.g., 'sql' -> sql)
-
- langText = strings.ToLower(langText)
-
- switch langText {
- case "sql":
- language = languageTypeSQL
- case "plpgsql":
- language = languageTypePLPGSQL
- default:
- return language, &base.TypeNotSupportedError{
- Type: "function language",
- Name: langText,
- Extra: fmt.Sprintf("function: %s", funcName),
- }
- }
-
- // Found the language, no need to continue
- break
- }
-
- return language, nil
-}
-
-// extractAsBodyFromCreateFunction extracts the AS body (function definition) from a CREATE FUNCTION statement.
-// Returns the function body SQL/PL/pgSQL code with quotes removed and escaped quotes unescaped.
-func (*querySpanExtractor) extractAsBodyFromCreateFunction(createFuncStmt parser.ICreatefunctionstmtContext, funcName string) (string, error) {
- createOptList := createFuncStmt.Createfunc_opt_list()
- if createOptList == nil {
- return "", errors.Errorf("no function options found for function: %s", funcName)
- }
-
- for _, item := range createOptList.AllCreatefunc_opt_item() {
- if item == nil {
- continue
- }
-
- // Check if this option is AS
- if item.AS() == nil {
- continue
- }
-
- // Extract AS body
- funcAs := item.Func_as()
- if funcAs == nil {
- continue
- }
-
- // Get the function body from the first string constant
- allSconst := funcAs.AllSconst()
- if len(allSconst) == 0 {
- continue
- }
-
- // Check for C-style function definition: AS 'obj_file', 'link_symbol'
- // This is a legacy format for C language functions that we don't support
- if len(allSconst) > 1 || funcAs.COMMA() != nil {
- return "", &base.TypeNotSupportedError{
- Type: "function",
- Name: funcName,
- Extra: "C-style function with object file and link symbol is not supported",
- }
- }
-
- // Get the raw text preserving all whitespace using token stream interval
- sconstCtx := allSconst[0]
- parser := sconstCtx.GetParser()
- if parser == nil {
- return "", errors.Errorf("parser is nil for function: %s", funcName)
- }
- tokenStream := parser.GetTokenStream()
-
- // Use GetTextFromInterval to preserve exact whitespace
- start := sconstCtx.GetStart().GetTokenIndex()
- stop := sconstCtx.GetStop().GetTokenIndex()
- interval := antlr.NewInterval(start, stop)
- sconst := tokenStream.GetTextFromInterval(interval)
-
- if len(sconst) < 2 {
- return "", errors.Errorf("invalid AS body for function: %s", funcName)
- }
-
- // Unescape PostgreSQL string constant based on its type
- asBody, err := unescapePostgreSQLString(sconst)
- if err != nil {
- return "", errors.Wrapf(err, "failed to unescape AS body for function: %s", funcName)
- }
-
- return asBody, nil
- }
-
- return "", errors.Errorf("AS body not found for function: %s", funcName)
-}
-
-// unescapePostgreSQLString unescapes a PostgreSQL string constant based on its type.
-// Handles: StringConstant, EscapeStringConstant, UnicodeEscapeStringConstant, DollarStringConstant.
-func unescapePostgreSQLString(s string) (string, error) {
- if len(s) < 2 {
- return "", errors.Errorf("string too short: %s", s)
- }
-
- // Dollar-quoted string: $$...$$, $tag$...$tag$
- // No escaping, just remove delimiters
- if strings.HasPrefix(s, "$") {
- firstDollar := strings.Index(s[1:], "$")
- if firstDollar == -1 {
- return "", errors.Errorf("invalid dollar-quoted string")
- }
- delimiter := s[:firstDollar+2] // e.g., "$$" or "$tag$"
- body := strings.TrimPrefix(s, delimiter)
- body = strings.TrimSuffix(body, delimiter)
- return body, nil
- }
-
- // Escape string constant: E'...'
- // Supports backslash escapes: \n, \t, \r, \\, \', etc.
- if strings.HasPrefix(s, "E'") || strings.HasPrefix(s, "e'") {
- if !strings.HasSuffix(s, "'") {
- return "", errors.Errorf("unterminated escape string constant")
- }
- body := s[2 : len(s)-1]
- return unescapeEscapeString(body), nil
- }
-
- // Unicode escape string constant: U&'...'
- // Supports unicode escapes: \XXXX or \+XXXXXX
- if strings.HasPrefix(s, "U&'") || strings.HasPrefix(s, "u&'") {
- if !strings.HasSuffix(s, "'") {
- return "", errors.Errorf("unterminated unicode escape string constant")
- }
- body := s[3 : len(s)-1]
- return unescapeUnicodeEscapeString(body), nil
- }
-
- // Standard string constant: '...'
- // Only '' is escaped as '
- if strings.HasPrefix(s, "'") && strings.HasSuffix(s, "'") {
- body := s[1 : len(s)-1]
- // Unescape doubled single quotes
- return strings.ReplaceAll(body, "''", "'"), nil
- }
-
- return "", errors.Errorf("unknown string constant type: %s", s)
-}
-
-// unescapeEscapeString unescapes E'...' style strings with backslash escapes.
-func unescapeEscapeString(s string) string {
- var result strings.Builder
- i := 0
- for i < len(s) {
- if s[i] == '\\' && i+1 < len(s) {
- switch s[i+1] {
- case 'n':
- result.WriteByte('\n')
- case 't':
- result.WriteByte('\t')
- case 'r':
- result.WriteByte('\r')
- case 'b':
- result.WriteByte('\b')
- case 'f':
- result.WriteByte('\f')
- case '\\':
- result.WriteByte('\\')
- case '\'':
- result.WriteByte('\'')
- case '"':
- result.WriteByte('"')
- default:
- // Unknown escape, keep as-is
- result.WriteByte(s[i])
- result.WriteByte(s[i+1])
- }
- i += 2
- } else if s[i] == '\'' && i+1 < len(s) && s[i+1] == '\'' {
- // Handle '' as escaped '
- result.WriteByte('\'')
- i += 2
- } else {
- result.WriteByte(s[i])
- i++
- }
- }
- return result.String()
-}
-
-// unescapeUnicodeEscapeString unescapes U&'...' style strings with unicode escapes.
-func unescapeUnicodeEscapeString(s string) string {
- // For now, implement basic unicode escape support
- // Full implementation would need to handle \XXXX and \+XXXXXX patterns
- // This is a simplified version
- result := s
- // Unescape doubled single quotes first
- result = strings.ReplaceAll(result, "''", "'")
- // TODO: Add proper unicode escape handling if needed
- return result
-}
-
-// extractParameterNamesFromCreateFunction extracts column names from RETURNS TABLE clause.
-// Redshift doesn't support PostgreSQL-style OUT parameters, but does support RETURNS TABLE.
-func (q *querySpanExtractor) extractParameterNamesFromCreateFunction(createFuncStmt parser.ICreatefunctionstmtContext) []string {
- var columnNames []string
-
- // Extract TABLE columns from RETURNS TABLE clause if present
- if createFuncStmt.TABLE() != nil && createFuncStmt.Table_func_column_list() != nil {
- for _, tableCol := range createFuncStmt.Table_func_column_list().AllTable_func_column() {
- if tableCol == nil || tableCol.Param_name() == nil {
- continue
- }
- paramName := q.extractParamName(tableCol.Param_name())
- if paramName != "" {
- columnNames = append(columnNames, paramName)
- }
- }
- }
-
- return columnNames
-}
-
-// extractParamName extracts the parameter name from param_name context.
-// param_name can be: type_function_name | builtin_function_name | LEFT | RIGHT
-func (*querySpanExtractor) extractParamName(paramNameCtx parser.IParam_nameContext) string {
- if paramNameCtx == nil {
- return ""
- }
-
- // Handle type_function_name (most common case)
- if paramNameCtx.Type_function_name() != nil {
- return normalizeRedshiftTypeFunctionName(paramNameCtx.Type_function_name())
- }
-
- // Handle builtin_function_name, LEFT, RIGHT
- return strings.ToLower(paramNameCtx.GetText())
-}
-
-type plpgSQLListener struct {
- *parser.BaseRedshiftParserListener
- q *querySpanExtractor
-
- variables map[string]*base.QuerySpanResult
-
- span *base.QuerySpan
- err error
-}
-
-func (l *plpgSQLListener) EnterFunc_as(ctx *parser.Func_asContext) {
- antlr.ParseTreeWalkerDefault.Walk(l, ctx.Definition)
-}
-
-func (l *plpgSQLListener) EnterDecl_statement(ctx *parser.Decl_statementContext) {
- vName := normalizeRedshiftAnyIdentifier(ctx.Decl_varname().Any_identifier())
- l.variables[vName] = &base.QuerySpanResult{
- SourceColumns: base.SourceColumnSet{},
- }
-}
-
-func (l *plpgSQLListener) EnterStmt_assign(ctx *parser.Stmt_assignContext) {
- names := NormalizeRedshiftAnyName(ctx.Assign_var().Any_name())
- if len(names) != 1 {
- return
- }
- assignVar := names[0]
-
- newQ := newQuerySpanExtractor(l.q.defaultDatabase, l.q.searchPath, l.q.gCtx)
- span, err := newQ.getQuerySpan(l.q.ctx, fmt.Sprintf("SELECT %s", ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx.Sql_expression())))
- if err != nil {
- return
- }
- if span.NotFoundError != nil {
- l.err = span.NotFoundError
- return
- }
- for source := range span.SourceColumns {
- l.q.sourceColumnsInFunction[source] = true
- }
- leftVarQuerySpan, exists := l.variables[assignVar]
- if !exists {
- leftVarQuerySpan = &base.QuerySpanResult{
- SourceColumns: base.SourceColumnSet{},
- }
- l.variables[assignVar] = leftVarQuerySpan
- }
- if len(span.Results) != 1 {
- return
- }
- newResourceColumns, _ := base.MergeSourceColumnSet(leftVarQuerySpan.SourceColumns, span.Results[0].SourceColumns)
- l.variables[assignVar] = &base.QuerySpanResult{
- SourceColumns: newResourceColumns,
- }
-}
-
-func (l *plpgSQLListener) EnterStmt_return(ctx *parser.Stmt_returnContext) {
- if ctx.QUERY() == nil {
- return
- }
-
- if ctx.Selectstmt() == nil {
- return
- }
-
- newQ := newQuerySpanExtractor(l.q.defaultDatabase, l.q.searchPath, l.q.gCtx)
- newQ.variables = l.variables
- l.span, l.err = newQ.getQuerySpan(l.q.ctx, ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx.Selectstmt()))
-}
-
-func (q *querySpanExtractor) getAllTableColumnSources(schemaName, tableName string) ([]base.QuerySpanResult, bool) {
- findInTableSource := func(tableSource base.TableSource) ([]base.QuerySpanResult, bool) {
- if schemaName != "" && schemaName != tableSource.GetSchemaName() {
- return nil, false
- }
- if tableName != "" && tableName != tableSource.GetTableName() {
- return nil, false
- }
- // If the table name is empty, we should check if there are ambiguous fields,
- // but we delegate this responsibility to the db-server, we do the fail-open strategy here.
-
- return tableSource.GetQuerySpanResult(), true
- }
-
- // One sub-query may have multi-outer schemas and the multi-outer schemas can use the same name, such as:
- //
- // select (
- // select (
- // select max(a) > x1.a from t
- // )
- // from t1 as x1
- // limit 1
- // )
- // from t as x1;
- //
- // This query has two tables can be called `x1`, and the expression x1.a uses the closer x1 table.
- // This is the reason we loop the slice in reversed order.
-
- for i := len(q.outerTableSources) - 1; i >= 0; i-- {
- if sourceColumnSet, ok := findInTableSource(q.outerTableSources[i]); ok {
- return sourceColumnSet, true
- }
- }
-
- for _, tableSource := range q.tableSourcesFrom {
- if sourceColumnSet, ok := findInTableSource(tableSource); ok {
- return sourceColumnSet, true
- }
- }
-
- return []base.QuerySpanResult{}, false
-}
-
-func (q *querySpanExtractor) getFieldColumnSource(schemaName, tableName, fieldName string) (base.SourceColumnSet, bool) {
- findInTableSource := func(tableSource base.TableSource) (base.SourceColumnSet, bool) {
- if tableSource == nil {
- return nil, false
- }
- if schemaName != "" && schemaName != tableSource.GetSchemaName() {
- return nil, false
- }
- if tableName != "" && tableName != tableSource.GetTableName() {
- return nil, false
- }
- // If the table name is empty, we should check if there are ambiguous fields,
- // but we delegate this responsibility to the db-server, we do the fail-open strategy here.
-
- querySpanResult := tableSource.GetQuerySpanResult()
- for _, field := range querySpanResult {
- if field.Name == fieldName {
- return field.SourceColumns, true
- }
- }
- return nil, false
- }
-
- // One sub-query may have multi-outer schemas and the multi-outer schemas can use the same name, such as:
- //
- // select (
- // select (
- // select max(a) > x1.a from t
- // )
- // from t1 as x1
- // limit 1
- // )
- // from t as x1;
- //
- // This query has two tables can be called `x1`, and the expression x1.a uses the closer x1 table.
- // This is the reason we loop the slice in reversed order.
-
- for i := len(q.outerTableSources) - 1; i >= 0; i-- {
- if sourceColumnSet, ok := findInTableSource(q.outerTableSources[i]); ok {
- return sourceColumnSet, true
- }
- }
-
- for _, tableSource := range q.tableSourcesFrom {
- if sourceColumnSet, ok := findInTableSource(tableSource); ok {
- return sourceColumnSet, true
- }
- }
-
- if schemaName == "" && tableName == "" && q.variables != nil {
- if v, exists := q.variables[fieldName]; exists {
- return v.SourceColumns, true
- }
- }
-
- return base.SourceColumnSet{}, false
-}
-
-func (q *querySpanExtractor) findTableInFrom(schemaName string, tableName string) base.TableSource {
- // Each CTE name in one WITH clause must be unique, but we can use the same name in the different level CTE, such as:
- //
- // with tt2 as (
- // with tt2 as (select * from t)
- // select max(a) from tt2)
- // select * from tt2
- //
- // This query has two CTE can be called `tt2`, and the FROM clause 'from tt2' uses the closer tt2 CTE.
- // This is the reason we loop the slice in reversed order.
- if schemaName == "" {
- for i := len(q.ctes) - 1; i >= 0; i-- {
- table := q.ctes[i]
- if table.Name == tableName {
- return table
- }
- }
- }
-
- for i := len(q.tableSourcesFrom) - 1; i >= 0; i-- {
- tableSource := q.tableSourcesFrom[i]
- if tableSource == nil {
- continue
- }
- emptySchemaNameMatch := schemaName == "" && (tableSource.GetSchemaName() == "" || slices.Contains(q.searchPath, tableSource.GetSchemaName())) && tableName == tableSource.GetTableName()
- nonEmptySchemaNameMatch := schemaName != "" && tableSource.GetSchemaName() == schemaName && tableName == tableSource.GetTableName()
- if emptySchemaNameMatch || nonEmptySchemaNameMatch {
- return tableSource
- }
- }
-
- return nil
-}
-
-func (q *querySpanExtractor) findTableSchema(schemaName string, 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:
- //
- // with tt2 as (
- // with tt2 as (select * from t)
- // select max(a) from tt2)
- // select * from tt2
- //
- // This query has two CTE can be called `tt2`, and the FROM clause 'from tt2' uses the closer tt2 CTE.
- // This is the reason we loop the slice in reversed order.
- if schemaName == "" {
- for i := len(q.ctes) - 1; i >= 0; i-- {
- table := q.ctes[i]
- if table.Name == tableName {
- return table, nil
- }
- }
- }
-
- // FIXME: consider cross database query which is supported in Redshift.
- dbMetadata, err := q.getDatabaseMetadata(q.defaultDatabase)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to get database metadata for database: %s", q.defaultDatabase)
- }
- if dbMetadata == nil {
- return nil, &base.ResourceNotFoundError{
- Database: &q.defaultDatabase,
- }
- }
- searcher := dbMetadata.NewSearcher(schemaName, q.searchPath)
- tableSchemaName, table := searcher.SearchTable(tableName)
- viewSchemaName, view := searcher.SearchView(tableName)
- materializedViewSchemaName, materializedView := searcher.SearchMaterializedView(tableName)
- foreignTableSchemaName, foreignTable := searcher.SearchExternalTable(tableName)
- sequenceSchemaName, sequence := searcher.SearchSequence(tableName)
-
- if table == nil && view == nil && foreignTable == nil && materializedView == nil && sequence == nil {
- return nil, &base.ResourceNotFoundError{
- Database: &q.defaultDatabase,
- Schema: &schemaName,
- Table: &tableName,
- }
- }
-
- if table != nil {
- var columns []string
- for _, column := range table.GetProto().GetColumns() {
- columns = append(columns, column.Name)
- }
- return &base.PhysicalTable{
- Server: "",
- Database: q.defaultDatabase,
- Schema: tableSchemaName,
- Name: table.GetProto().Name,
- Columns: columns,
- }, nil
- }
-
- if foreignTable != nil {
- var columns []string
- for _, column := range foreignTable.GetProto().GetColumns() {
- columns = append(columns, column.Name)
- }
- return &base.PhysicalTable{
- Server: "",
- Database: q.defaultDatabase,
- Schema: foreignTableSchemaName,
- Name: foreignTable.GetProto().Name,
- Columns: columns,
- }, nil
- }
-
- if view != nil && view.Definition != "" {
- columns, err := q.getColumnsForView(view.Definition)
- if err != nil {
- return nil, err
- }
- return &base.PhysicalView{
- Server: "",
- Database: q.defaultDatabase,
- Schema: viewSchemaName,
- Name: view.Name,
- Columns: columns,
- }, nil
- }
-
- if materializedView != nil && materializedView.Definition != "" {
- columns, err := q.getColumnsForMaterializedView(materializedView.Definition)
- if err != nil {
- return nil, err
- }
- return &base.PhysicalView{
- Server: "",
- Database: q.defaultDatabase,
- Schema: materializedViewSchemaName,
- Name: materializedView.Name,
- Columns: columns,
- }, nil
- }
-
- if sequence != nil {
- // The default columns for sequence in PostgreSQL.
- columns := []string{"last_value", "log_cnt", "is_called"}
- return &base.Sequence{
- Server: "",
- Database: q.defaultDatabase,
- Schema: sequenceSchemaName,
- Name: sequence.Name,
- Columns: columns,
- }, nil
- }
- return nil, nil
-}
-
-func (q *querySpanExtractor) getColumnsForView(definition string) ([]base.QuerySpanResult, error) {
- newQ := newQuerySpanExtractor(q.defaultDatabase, q.searchPath, q.gCtx)
- span, err := newQ.getQuerySpan(q.ctx, definition)
- if err != nil {
- return nil, err
- }
- if span.NotFoundError != nil {
- return nil, span.NotFoundError
- }
- return span.Results, nil
-}
-
-func (q *querySpanExtractor) getColumnsForMaterializedView(definition string) ([]base.QuerySpanResult, error) {
- newQ := newQuerySpanExtractor(q.defaultDatabase, q.searchPath, q.gCtx)
- span, err := newQ.getQuerySpan(q.ctx, definition)
- if err != nil {
- return nil, err
- }
- if span.NotFoundError != nil {
- return nil, span.NotFoundError
- }
- return span.Results, nil
-}
-
-func (q *querySpanExtractor) EnterStmtmulti(sm *parser.StmtmultiContext) {
- if len(sm.AllStmt()) != 1 {
- q.err = errors.Errorf("malformed query, expected 1 query but got %d", len(sm.AllStmt()))
- return
- }
-
- // Process the statement and store the result
- stmt := sm.AllStmt()[0]
- tableSource, err := q.extractTableSourceFromStmt(stmt)
- if err != nil {
- q.err = err
- return
- }
-
- // Store the extracted table source for later retrieval
- q.resultTableSource = tableSource
-}
-
-func (q *querySpanExtractor) extractTableSourceFromStmt(stmt parser.IStmtContext) (base.TableSource, error) {
- switch {
- case stmt.Explainstmt() != nil:
- // Handle EXPLAIN statement
- explainStmt := stmt.Explainstmt()
- if explainStmt.Explainablestmt() != nil {
- // Extract from the explained statement
- if explainableStmt := explainStmt.Explainablestmt(); explainableStmt != nil {
- if explainableStmt.Selectstmt() != nil {
- return q.extractTableSourceFromSelectstmt(explainableStmt.Selectstmt())
- }
- if explainableStmt.Insertstmt() != nil ||
- explainableStmt.Updatestmt() != nil ||
- explainableStmt.Deletestmt() != nil {
- // For DML in EXPLAIN, return empty pseudo table
- return &base.PseudoTable{
- Name: "",
- Columns: []base.QuerySpanResult{},
- }, nil
- }
- }
- }
- return &base.PseudoTable{
- Name: "",
- Columns: []base.QuerySpanResult{},
- }, nil
- case stmt.Selectstmt() != nil:
- return q.extractTableSourceFromSelectstmt(stmt.Selectstmt())
- default:
- // For non-SELECT statements, return empty pseudo table
- return &base.PseudoTable{
- Name: "",
- Columns: []base.QuerySpanResult{},
- }, nil
- }
-}
-
-func (q *querySpanExtractor) extractTableSourceFromSelectstmt(selectstmt parser.ISelectstmtContext) (base.TableSource, error) {
- selectNoParens := getSelectNoParensFromSelectStmt(selectstmt)
- return q.extractTableSourceFromSelectNoParens(selectNoParens)
-}
-
-func (q *querySpanExtractor) extractTableSourceFromSelectNoParens(selectNoParens parser.ISelect_no_parensContext) (base.TableSource, error) {
- // Reset the table sources from the FROM clause after exit the SELECT statement.
- previousTableSourcesFromLength := len(q.tableSourcesFrom)
- defer func() {
- q.tableSourcesFrom = q.tableSourcesFrom[:previousTableSourcesFromLength]
- }()
-
- // Analyze the CTE first.
- withClause := selectNoParens.With_clause()
- if withClause != nil {
- cteLength, err := q.recordCTEs(withClause)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to record CTEs")
- }
- defer func() {
- q.ctes = q.ctes[:cteLength]
- }()
- }
-
- // Process the SELECT clause
- selectClause := selectNoParens.Select_clause()
- if selectClause == nil {
- return nil, errors.New("select_no_parens has no select_clause")
- }
-
- // Handle UNION/INTERSECT/EXCEPT at the top level
- allSimpleSelects := selectClause.AllSimple_select_intersect()
- if len(allSimpleSelects) == 0 {
- return nil, errors.New("select_clause has no simple_select_intersect")
- }
-
- // If there's only one simple_select_intersect, process it directly
- if len(allSimpleSelects) == 1 {
- return q.extractTableSourceFromSimpleSelectIntersect(allSimpleSelects[0])
- }
-
- // Multiple simple_select_intersect with operators between them
- // Extract operators from the select_clause
- operators, err := q.extractOperatorsFromSelectClause(selectClause)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to extract operators from select_clause")
- }
-
- return q.mergeSimpleSelectIntersectsWithOperators(allSimpleSelects, operators)
-}
-
-func (q *querySpanExtractor) recordCTEs(with parser.IWith_clauseContext) (restoreCTELength int, err error) {
- previousCTELength := len(q.ctes)
- recursive := with.RECURSIVE() != nil
- for _, cte := range with.Cte_list().AllCommon_table_expr() {
- cteName := normalizeRedshiftName(cte.Name())
- var colAliasNames []string
- if cte.Opt_name_list() != nil {
- for _, name := range cte.Opt_name_list().Name_list().AllName() {
- colAliasNames = append(colAliasNames, normalizeRedshiftName(name))
- }
- }
-
- var cteTableSource *base.PseudoTable
- if !recursive {
- // Non-recursive WITH clause - all CTEs are non-recursive
- cteTableSource, err = q.extractTableSourceFromNonRecursiveCTE(cte, cteName, colAliasNames)
- if err != nil {
- return previousCTELength, err
- }
- } else {
- // WITH RECURSIVE clause - try to extract as recursive
- // If it's not actually recursive, extractTableSourceFromRecursiveCTE will fall back to non-recursive
- cteTableSource, err = q.extractTableSourceFromRecursiveCTE(cte, cteName, colAliasNames)
- if err != nil {
- return previousCTELength, err
- }
- }
-
- q.ctes = append(q.ctes, cteTableSource)
- }
-
- return previousCTELength, nil
-}
-
-// extractTableSourceFromRecursiveCTE processes a recursive CTE by separating it into base and recursive parts,
-// then performs iterative closure computation to resolve all column dependencies.
-// A recursive CTE has the structure: base_case UNION [ALL] recursive_case
-// We use a simple strategy: the last part after UNION is the recursive part.
-func (q *querySpanExtractor) extractTableSourceFromRecursiveCTE(cte parser.ICommon_table_exprContext, cteName string, colAliasNames []string) (*base.PseudoTable, error) {
- selectStmt := cte.Preparablestmt().Selectstmt()
- if selectStmt == nil {
- return nil, errors.Errorf("malformed recursive CTE, expected SELECT statement in CTE but got others")
- }
- selectNoParens := getSelectNoParensFromSelectStmt(selectStmt)
-
- // Split the CTE into base and recursive parts using the simple strategy
- baseParts, baseOperators, recursivePart, recursiveOperator, err := splitRecursiveCTEParts(selectNoParens)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to split recursive CTE %q", cteName)
- }
-
- if len(baseParts) == 0 || recursivePart == nil || recursiveOperator == nil {
- // Not a valid recursive structure, treat as non-recursive
- return q.extractTableSourceFromNonRecursiveCTE(cte, cteName, colAliasNames)
- }
-
- // Process the base parts (can be multiple parts combined with various operators)
- var baseTableSource base.TableSource
- if len(baseParts) == 1 {
- // Single base part
- baseTableSource, err = q.extractTableSourceFromSimpleSelectIntersect(baseParts[0])
- if err != nil {
- return nil, errors.Wrapf(err, "failed to extract base part for recursive CTE %q", cteName)
- }
- } else {
- // Multiple base parts - need to merge them with their respective operators
- baseTableSource, err = q.mergeSimpleSelectIntersectsWithOperators(baseParts, baseOperators)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to merge base parts for recursive CTE %q", cteName)
- }
- }
-
- // Apply column aliases
- querySpanResult := baseTableSource.GetQuerySpanResult()
- if len(colAliasNames) > 0 {
- if len(colAliasNames) != len(querySpanResult) {
- return nil, errors.Errorf("CTE %q has %d columns but alias has %d columns", cteName, len(querySpanResult), len(colAliasNames))
- }
- for i, colAlias := range colAliasNames {
- querySpanResult[i].Name = colAlias
- }
- }
-
- cteTableSource := &base.PseudoTable{
- Name: cteName,
- Columns: querySpanResult,
- }
-
- // Add to CTEs for recursive part to reference
- q.ctes = append(q.ctes, cteTableSource)
- defer func() {
- q.ctes = q.ctes[:len(q.ctes)-1]
- }()
-
- // Iterative closure computation
- for {
- recursiveTableSource, err := q.extractTableSourceFromSimpleSelectIntersect(recursivePart)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to extract recursive part for CTE %q", cteName)
- }
-
- recursiveQuerySpanResult := recursiveTableSource.GetQuerySpanResult()
- if len(recursiveQuerySpanResult) != len(querySpanResult) {
- return nil, errors.Errorf("CTE %q base has %d columns but recursive has %d", cteName, len(querySpanResult), len(recursiveQuerySpanResult))
- }
-
- changed := false
- for i, spanResult := range recursiveQuerySpanResult {
- newSourceColumns, hasDiff := base.MergeSourceColumnSet(querySpanResult[i].SourceColumns, spanResult.SourceColumns)
- if hasDiff {
- changed = true
- querySpanResult[i].SourceColumns = newSourceColumns
- }
- }
-
- if !changed {
- break
- }
- q.ctes[len(q.ctes)-1].Columns = querySpanResult
- }
-
- return cteTableSource, nil
-}
-
-func (q *querySpanExtractor) extractTableSourceFromNonRecursiveCTE(cte parser.ICommon_table_exprContext, cteName string, colAliasNames []string) (*base.PseudoTable, error) {
- selectStmt := cte.Preparablestmt().Selectstmt()
- if selectStmt == nil {
- return nil, errors.Errorf("malformed CTE, expected SELECT statement in CTE but got others")
- }
- // Non-recursive CTE.
- tableSource, err := q.extractTableSourceFromSelectstmt(selectStmt)
- if err != nil {
- return nil, err
- }
- querySpanResult := tableSource.GetQuerySpanResult()
- if len(colAliasNames) > 0 && len(colAliasNames) != len(querySpanResult) {
- return nil, errors.Errorf("the column alias number %d does not match the actual column number %d in CTE %q", len(colAliasNames), len(querySpanResult), cteName)
- }
- for i, colAlias := range colAliasNames {
- querySpanResult[i].Name = colAlias
- }
- cteTableSource := &base.PseudoTable{
- Name: cteName,
- Columns: querySpanResult,
- }
- return cteTableSource, nil
-}
-
-// extractOperatorsFromSelectClause extracts set operators from a select_clause
-func (*querySpanExtractor) extractOperatorsFromSelectClause(selectClause parser.ISelect_clauseContext) ([]SetOperator, error) {
- if selectClause == nil {
- return nil, errors.New("nil select_clause")
- }
-
- allSimpleSelects := selectClause.AllSimple_select_intersect()
- if len(allSimpleSelects) <= 1 {
- return nil, nil // No operators needed for 0 or 1 select
- }
-
- var operators []SetOperator
- children := selectClause.GetChildren()
-
- // Parse children to find operators between simple_select_intersect nodes
- for i := 0; i < len(children); i++ {
- if token, ok := children[i].(antlr.TerminalNode); ok {
- tokenType := token.GetSymbol().GetTokenType()
- var op SetOperator
-
- switch tokenType {
- case parser.RedshiftLexerUNION:
- op.Type = "UNION"
- op.IsDistinct = true // Default to DISTINCT
- // Check for ALL/DISTINCT modifier
- if i+1 < len(children) {
- if nextToken, ok := children[i+1].(antlr.TerminalNode); ok {
- if nextToken.GetSymbol().GetTokenType() == parser.RedshiftLexerALL {
- op.IsDistinct = false
- i++ // Skip ALL token
- } else if nextToken.GetSymbol().GetTokenType() == parser.RedshiftLexerDISTINCT {
- op.IsDistinct = true
- i++ // Skip DISTINCT token
- }
- }
- }
- operators = append(operators, op)
-
- case parser.RedshiftLexerEXCEPT:
- op.Type = "EXCEPT"
- op.IsDistinct = true // Default to DISTINCT
- // Check for ALL/DISTINCT modifier
- if i+1 < len(children) {
- if nextToken, ok := children[i+1].(antlr.TerminalNode); ok {
- if nextToken.GetSymbol().GetTokenType() == parser.RedshiftLexerALL {
- op.IsDistinct = false
- i++ // Skip ALL token
- } else if nextToken.GetSymbol().GetTokenType() == parser.RedshiftLexerDISTINCT {
- op.IsDistinct = true
- i++ // Skip DISTINCT token
- }
- }
- }
- operators = append(operators, op)
-
- case parser.RedshiftLexerINTERSECT:
- op.Type = "INTERSECT"
- op.IsDistinct = true // Default to DISTINCT
- // Check for ALL/DISTINCT modifier
- if i+1 < len(children) {
- if nextToken, ok := children[i+1].(antlr.TerminalNode); ok {
- if nextToken.GetSymbol().GetTokenType() == parser.RedshiftLexerALL {
- op.IsDistinct = false
- i++ // Skip ALL token
- } else if nextToken.GetSymbol().GetTokenType() == parser.RedshiftLexerDISTINCT {
- op.IsDistinct = true
- i++ // Skip DISTINCT token
- }
- }
- }
- operators = append(operators, op)
- default:
- // Ignore other tokens
- continue
- }
- }
- }
-
- if len(operators) != len(allSimpleSelects)-1 {
- return nil, errors.Errorf("expected %d operators but found %d", len(allSimpleSelects)-1, len(operators))
- }
-
- return operators, nil
-}
-
-// SetOperator represents a set operation between SELECT statements
-type SetOperator struct {
- Type string // "UNION", "EXCEPT", "INTERSECT"
- IsDistinct bool // false means ALL, true means DISTINCT (or implicit distinct)
-}
-
-// splitRecursiveCTEParts analyzes a recursive CTE's SELECT structure and separates it into:
-// - baseParts: Everything before the last UNION/UNION ALL (the non-recursive anchor queries)
-// - baseOperators: The operators between base parts (UNION, EXCEPT, etc.)
-// - recursivePart: The part after the last UNION/UNION ALL (the recursive query)
-// - recursiveOperator: The operator connecting the base to the recursive part (must be UNION [ALL])
-// Note: The recursive part must be connected with UNION [ALL], not EXCEPT or INTERSECT.
-func splitRecursiveCTEParts(selectNoParens parser.ISelect_no_parensContext) (
- baseParts []parser.ISimple_select_intersectContext,
- baseOperators []SetOperator,
- recursivePart parser.ISimple_select_intersectContext,
- recursiveOperator *SetOperator,
- err error) {
- if selectNoParens == nil || selectNoParens.Select_clause() == nil {
- return nil, nil, nil, nil, errors.New("invalid select_no_parens")
- }
-
- selectClause := selectNoParens.Select_clause()
- allParts := selectClause.AllSimple_select_intersect()
-
- if len(allParts) < 2 {
- // No UNION, cannot be a recursive CTE
- return nil, nil, nil, nil, nil
- }
-
- // Track all operators between parts
- var allOperators []SetOperator
- lastUnionIndex := -1
-
- // Parse the children to extract operators
- // Pattern: part0 [OPERATOR [ALL|DISTINCT]] part1 [OPERATOR [ALL|DISTINCT]] part2 ...
- children := selectClause.GetChildren()
- partIndex := 0
- i := 0
-
- for i < len(children) {
- child := children[i]
- if token, ok := child.(antlr.TerminalNode); ok {
- tokenType := token.GetSymbol().GetTokenType()
- var op SetOperator
-
- switch tokenType {
- case parser.RedshiftLexerUNION:
- op.Type = "UNION"
- // Check if next token is ALL or DISTINCT
- op.IsDistinct = true // Default to DISTINCT
- if i+1 < len(children) {
- if nextToken, ok := children[i+1].(antlr.TerminalNode); ok {
- nextType := nextToken.GetSymbol().GetTokenType()
- switch nextType {
- case parser.RedshiftLexerALL:
- op.IsDistinct = false
- i++
- case parser.RedshiftLexerDISTINCT:
- op.IsDistinct = true
- i++
- default:
- }
- }
- }
- allOperators = append(allOperators, op)
- lastUnionIndex = partIndex
- partIndex++
-
- case parser.RedshiftLexerEXCEPT:
- op.Type = "EXCEPT"
- // Check if next token is ALL or DISTINCT
- op.IsDistinct = true // Default to DISTINCT
- if i+1 < len(children) {
- if nextToken, ok := children[i+1].(antlr.TerminalNode); ok {
- nextType := nextToken.GetSymbol().GetTokenType()
- switch nextType {
- case parser.RedshiftLexerALL:
- op.IsDistinct = false
- i++
- case parser.RedshiftLexerDISTINCT:
- op.IsDistinct = true
- i++
- default:
- }
- }
- }
- allOperators = append(allOperators, op)
- partIndex++
-
- case parser.RedshiftLexerINTERSECT:
- op.Type = "INTERSECT"
- // Check if next token is ALL or DISTINCT
- op.IsDistinct = true // Default to DISTINCT
- if i+1 < len(children) {
- if nextToken, ok := children[i+1].(antlr.TerminalNode); ok {
- nextType := nextToken.GetSymbol().GetTokenType()
- switch nextType {
- case parser.RedshiftLexerALL:
- op.IsDistinct = false
- i++
- case parser.RedshiftLexerDISTINCT:
- op.IsDistinct = true
- i++
- default:
- }
- }
- }
- allOperators = append(allOperators, op)
- partIndex++
- default:
- // Ignore other tokens
- }
- }
- i++
- }
-
- if lastUnionIndex < 0 {
- // No UNION found, not a recursive CTE
- return nil, nil, nil, nil, nil
- }
-
- // Split at the last UNION position
- // Everything before the last UNION is the base part(s)
- // The part after the last UNION is the recursive part
- baseParts = allParts[:lastUnionIndex+1]
- recursivePart = allParts[lastUnionIndex+1]
-
- // Split operators too
- if lastUnionIndex > 0 {
- baseOperators = allOperators[:lastUnionIndex]
- }
- recursiveOperator = &allOperators[lastUnionIndex]
-
- return baseParts, baseOperators, recursivePart, recursiveOperator, nil
-}
-
-// extractTableSourceFromSimpleSelectIntersect processes a simple_select_intersect node and returns its table source.
-// This handles the INTERSECT operations between multiple simple_select_pramary nodes.
-func (q *querySpanExtractor) extractTableSourceFromSimpleSelectIntersect(simpleSelect parser.ISimple_select_intersectContext) (base.TableSource, error) {
- if simpleSelect == nil {
- return nil, errors.New("nil simple_select_intersect")
- }
-
- allPrimaries := simpleSelect.AllSimple_select_pramary()
- if len(allPrimaries) == 0 {
- return nil, errors.New("no primary selects in simple_select_intersect")
- }
-
- // If there's only one primary, just process it directly
- if len(allPrimaries) == 1 {
- return q.extractTableSourceFromSimpleSelectPrimary(allPrimaries[0])
- }
-
- // Multiple primaries - need to handle INTERSECT operations
- // Start with the first primary
- result, err := q.extractTableSourceFromSimpleSelectPrimary(allPrimaries[0])
- if err != nil {
- return nil, errors.Wrap(err, "failed to extract first primary")
- }
-
- // Process remaining primaries with INTERSECT
- for i := 1; i < len(allPrimaries); i++ {
- nextResult, err := q.extractTableSourceFromSimpleSelectPrimary(allPrimaries[i])
- if err != nil {
- return nil, errors.Wrapf(err, "failed to extract primary %d", i)
- }
-
- // Merge with INTERSECT semantics
- leftQuerySpanResult := result.GetQuerySpanResult()
- rightQuerySpanResult := nextResult.GetQuerySpanResult()
-
- if len(leftQuerySpanResult) != len(rightQuerySpanResult) {
- return nil, errors.Errorf("INTERSECT requires same number of columns: %d vs %d",
- len(leftQuerySpanResult), len(rightQuerySpanResult))
- }
-
- var mergedColumns []base.QuerySpanResult
- for j, leftCol := range leftQuerySpanResult {
- rightCol := rightQuerySpanResult[j]
- // For INTERSECT, we take columns that exist in both
- mergedSourceColumns, _ := base.MergeSourceColumnSet(leftCol.SourceColumns, rightCol.SourceColumns)
- mergedColumns = append(mergedColumns, base.QuerySpanResult{
- Name: leftCol.Name,
- SourceColumns: mergedSourceColumns,
- })
- }
-
- result = &base.PseudoTable{
- Name: "",
- Columns: mergedColumns,
- }
- }
-
- return result, nil
-}
-
-// extractTableSourceFromSimpleSelectPrimary processes a simple_select_pramary node and returns its table source.
-// This is the basic building block of SELECT statements, containing the SELECT list, FROM clause, etc.
-func (q *querySpanExtractor) extractTableSourceFromSimpleSelectPrimary(primary parser.ISimple_select_pramaryContext) (base.TableSource, error) {
- if primary == nil {
- return nil, errors.New("nil simple_select_pramary")
- }
-
- // Handle VALUES clause
- if v := primary.Values_clause(); v != nil {
- columnNumber := len(v.AllExpr_list()[0].AllA_expr())
- // Check other rows have the same number of columns
- for _, exprList := range v.AllExpr_list()[1:] {
- if len(exprList.AllA_expr()) != columnNumber {
- return nil, errors.New("VALUES clause rows have different column counts")
- }
- }
- columnSourceSets := make([]base.SourceColumnSet, columnNumber)
- for i, exprList := range v.AllExpr_list() {
- for j, expr := range exprList.AllA_expr() {
- cols, err := q.extractSourceColumnSetFromAExpr(expr)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to extract columns from VALUES clause row %d, column %d", i, j)
- }
- columnSourceSets[j], _ = base.MergeSourceColumnSet(columnSourceSets[j], cols)
- }
- }
- tableSource := &base.PseudoTable{
- Name: "",
- Columns: func() []base.QuerySpanResult {
- var results []base.QuerySpanResult
- for i, colSet := range columnSourceSets {
- results = append(results, base.QuerySpanResult{
- Name: fmt.Sprintf("column%d", i+1),
- SourceColumns: colSet,
- })
- }
- return results
- }(),
- }
-
- return tableSource, nil
- }
-
- // Handle TABLE clause (TABLE tablename)
- // TODO: Implement TABLE clause handling when needed
-
- // Handle regular SELECT
- if primary.SELECT() != nil {
- // Save current FROM table sources
- previousTableSourcesFromLength := len(q.tableSourcesFrom)
- defer func() {
- q.tableSourcesFrom = q.tableSourcesFrom[:previousTableSourcesFromLength]
- }()
-
- // Process FROM clause first
- fromFieldList, err := q.extractTableSourceFromFromClause(primary.From_clause())
- if err != nil {
- return nil, errors.Wrapf(err, "failed to extract table sources from FROM clause")
- }
-
- // Process target list (SELECT items)
- var targetListContext parser.ITarget_listContext
- if primary.Opt_target_list() != nil {
- targetListContext = primary.Opt_target_list().Target_list()
- } else if primary.Target_list() != nil {
- targetListContext = primary.Target_list()
- }
-
- // PostgreSQL allow SELECT without target list (e.g., SELECT FROM table), it returns no columns.
- if targetListContext == nil {
- return &base.PseudoTable{
- Name: "",
- Columns: []base.QuerySpanResult{},
- }, nil
- }
-
- columns, err := q.extractColumnsFromTargetList(targetListContext, fromFieldList)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to extract columns from target list")
- }
-
- result := &base.PseudoTable{
- Name: "",
- Columns: columns,
- }
-
- return result, nil
- }
-
- // Handle parenthesized SELECT
- if s := primary.Select_with_parens(); s != nil {
- selectNoParens := getSelectNoParensFromSelectWithParens(s)
- return q.extractTableSourceFromSelectNoParens(selectNoParens)
- }
-
- // Default case
- return &base.PseudoTable{
- Name: "",
- Columns: []base.QuerySpanResult{},
- }, nil
-}
-
-// extractTableSourceFromSelectWithParens handles SELECT statements wrapped in parentheses
-func (q *querySpanExtractor) extractTableSourceFromSelectWithParens(selectWithParens parser.ISelect_with_parensContext) (base.TableSource, error) {
- if selectWithParens == nil {
- return nil, errors.New("nil select_with_parens")
- }
-
- if selectWithParens.Select_no_parens() != nil {
- return q.extractTableSourceFromSelectNoParens(selectWithParens.Select_no_parens())
- }
-
- if selectWithParens.Select_with_parens() != nil {
- // Nested parentheses
- return q.extractTableSourceFromSelectWithParens(selectWithParens.Select_with_parens())
- }
-
- return nil, errors.New("select_with_parens has no valid content")
-}
-
-// mergeSimpleSelectIntersectsWithOperators combines multiple simple_select_intersect nodes with their respective operators.
-// This properly handles UNION, EXCEPT, INTERSECT with ALL/DISTINCT modifiers.
-func (q *querySpanExtractor) mergeSimpleSelectIntersectsWithOperators(parts []parser.ISimple_select_intersectContext, operators []SetOperator) (base.TableSource, error) {
- if len(parts) == 0 {
- return nil, errors.New("no parts to merge")
- }
-
- if len(parts) == 1 {
- // Single part, no operators needed
- return q.extractTableSourceFromSimpleSelectIntersect(parts[0])
- }
-
- if len(operators) != len(parts)-1 {
- return nil, errors.Errorf("invalid operators count: %d operators for %d parts", len(operators), len(parts))
- }
-
- // Start with the first part
- result, err := q.extractTableSourceFromSimpleSelectIntersect(parts[0])
- if err != nil {
- return nil, errors.Wrap(err, "failed to extract first simple_select_intersect")
- }
-
- // Apply each operator with the next part
- for i, op := range operators {
- nextResult, err := q.extractTableSourceFromSimpleSelectIntersect(parts[i+1])
- if err != nil {
- return nil, errors.Wrapf(err, "failed to extract simple_select_intersect %d", i+1)
- }
-
- // Apply the specific operator semantics
- result, err = q.applySetOperator(result, nextResult, op)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to apply %s at position %d", op.Type, i)
- }
- }
-
- return result, nil
-}
-
-// applySetOperator applies a set operation (UNION, EXCEPT, INTERSECT) between two table sources
-func (*querySpanExtractor) applySetOperator(left, right base.TableSource, op SetOperator) (base.TableSource, error) {
- leftQuerySpanResult := left.GetQuerySpanResult()
- rightQuerySpanResult := right.GetQuerySpanResult()
-
- if len(leftQuerySpanResult) != len(rightQuerySpanResult) {
- return nil, errors.Errorf("%s requires same number of columns: %d vs %d",
- op.Type, len(leftQuerySpanResult), len(rightQuerySpanResult))
- }
-
- var mergedColumns []base.QuerySpanResult
-
- switch op.Type {
- case "UNION":
- // UNION combines rows from both queries
- for j, leftCol := range leftQuerySpanResult {
- rightCol := rightQuerySpanResult[j]
- mergedSourceColumns, _ := base.MergeSourceColumnSet(leftCol.SourceColumns, rightCol.SourceColumns)
- mergedColumns = append(mergedColumns, base.QuerySpanResult{
- Name: leftCol.Name, // Use the name from the first query
- SourceColumns: mergedSourceColumns,
- })
- }
-
- case "EXCEPT":
- // EXCEPT returns rows from left that don't appear in right
- // For column tracking, we only track columns from the left side
- for _, leftCol := range leftQuerySpanResult {
- mergedColumns = append(mergedColumns, base.QuerySpanResult{
- Name: leftCol.Name,
- SourceColumns: leftCol.SourceColumns, // Only left side columns matter
- })
- }
-
- case "INTERSECT":
- // INTERSECT returns rows that appear in both
- // We need to track columns from both sides
- for j, leftCol := range leftQuerySpanResult {
- rightCol := rightQuerySpanResult[j]
- mergedSourceColumns, _ := base.MergeSourceColumnSet(leftCol.SourceColumns, rightCol.SourceColumns)
- mergedColumns = append(mergedColumns, base.QuerySpanResult{
- Name: leftCol.Name,
- SourceColumns: mergedSourceColumns,
- })
- }
-
- default:
- return nil, errors.Errorf("unsupported set operator: %s", op.Type)
- }
-
- return &base.PseudoTable{
- Name: "",
- Columns: mergedColumns,
- }, nil
-}
-
-// extractTableSourceFromFromClause processes the FROM clause and returns all table sources
-func (q *querySpanExtractor) extractTableSourceFromFromClause(fromClause parser.IFrom_clauseContext) ([]base.TableSource, error) {
- if fromClause == nil || fromClause.From_list() == nil {
- return nil, nil
- }
-
- var fromFieldList []base.TableSource
- for _, fromItem := range fromClause.From_list().AllTable_ref() {
- tableSource, err := q.extractTableSourceFromTableRef(fromItem)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to extract table source from FROM item")
- }
- q.tableSourcesFrom = append(q.tableSourcesFrom, tableSource)
- fromFieldList = append(fromFieldList, tableSource)
- }
-
- return fromFieldList, nil
-}
-
-// extractTableSourceFromTableRef extracts table source from a table reference in FROM clause
-func (q *querySpanExtractor) extractTableSourceFromTableRef(tableRef parser.ITable_refContext) (base.TableSource, error) {
- if tableRef == nil {
- return nil, errors.New("nil table_ref")
- }
-
- var anchor base.TableSource
- var err error
-
- // Handle simple table reference (e.g., schema.table)
- if tableRef.Relation_expr() != nil {
- anchor, err = q.extractTableSourceFromRelationExpr(tableRef.Relation_expr(), tableRef.Opt_alias_clause())
- if err != nil {
- return nil, errors.Wrapf(err, "failed to extract table source from relation_expr")
- }
- if tableRef.Opt_alias_clause() != nil {
- anchor, err = applyOptAliasClauseToTableSource(anchor, tableRef.Opt_alias_clause())
- if err != nil {
- return nil, errors.Wrapf(err, "failed to apply alias to relation_expr")
- }
- }
- }
-
- // Handle function tables (e.g., generate_series, unnest)
- if tableRef.Func_table() != nil {
- anchor, err = q.extractTableSourceFromFuncTable(tableRef.Func_table(), tableRef.Func_alias_clause())
- if err != nil {
- return nil, errors.Wrapf(err, "failed to extract table source from func_table")
- }
- if tableRef.Func_alias_clause() != nil {
- anchor, err = applyFuncAliasClauseToTableSource(anchor, tableRef.Func_alias_clause())
- if err != nil {
- return nil, errors.Wrapf(err, "failed to apply alias to func_table")
- }
- }
- }
-
- // TODO(zp): Handle xmltable candidates.
-
- // Handle subquery (SELECT in parentheses)
- if tableRef.Select_with_parens() != nil {
- anchor, err = q.extractTableSourceFromSelectWithParens(tableRef.Select_with_parens())
- if err != nil {
- return nil, errors.Wrapf(err, "failed to extract subquery")
- }
-
- // Apply alias if present
- if tableRef.Opt_alias_clause() != nil {
- anchor, err = applyOptAliasClauseToTableSource(anchor, tableRef.Opt_alias_clause())
- if err != nil {
- return nil, errors.Wrapf(err, "failed to apply alias to subquery")
- }
- }
- }
-
- if len(tableRef.AllTable_ref()) > 0 {
- anchor, err = q.extractTableSourceFromTableRef(tableRef.Table_ref(0))
- if err != nil {
- return nil, errors.Wrapf(err, "failed to extract nested table_ref")
- }
- if len(tableRef.AllJoined_table()) == 0 && tableRef.Opt_alias_clause() != nil {
- anchor, err = applyOptAliasClauseToTableSource(anchor, tableRef.Opt_alias_clause())
- if err != nil {
- return nil, errors.Wrapf(err, "failed to apply alias to nested table_ref")
- }
- }
- }
-
- q.tableSourcesFrom = append(q.tableSourcesFrom, anchor)
-
- // Handle JOIN expressions
- if len(tableRef.AllJoined_table()) != 0 {
- joinedTables := tableRef.AllJoined_table()
- for i, join := range joinedTables {
- tableSource, err := q.extractTableSourceFromTableRef(join.Table_ref())
- if err != nil {
- return nil, errors.Wrapf(err, "failed to extract joined table in JOIN")
- }
- // According to the grammar:
- // OPEN_PAREN table_ref joined_table? CLOSE_PAREN opt_alias_clause?
- // ) joined_table*
- // The alias appears before the second joined_table.
- // Determine join type and conditions
- naturalJoin := join.NATURAL() != nil
- var usingColumns []string
- if join.Join_qual() != nil && join.Join_qual().USING() != nil {
- usingColumns = normalizeRedshiftNameList(join.Join_qual().Name_list())
- }
- anchor = q.joinTableSources(anchor, tableSource, naturalJoin, usingColumns)
- if i == 0 && tableRef.Opt_alias_clause() != nil && len(tableRef.AllTable_ref()) > 0 {
- anchor, err = applyOptAliasClauseToTableSource(anchor, tableRef.Opt_alias_clause())
- if err != nil {
- return nil, errors.Wrapf(err, "failed to apply alias to table source")
- }
- }
- }
- }
-
- return anchor, nil
-}
-
-func (*querySpanExtractor) joinTableSources(left, right base.TableSource, naturalJoin bool, usingColumn []string) base.TableSource {
- leftSpanResult, rightSpanResult := left.GetQuerySpanResult(), right.GetQuerySpanResult()
-
- result := new(base.PseudoTable)
-
- if naturalJoin {
- leftSpanResultIdx, rightSpanResultIdx := make(map[string]int, len(leftSpanResult)), make(map[string]int, len(rightSpanResult))
- for i, spanResult := range leftSpanResult {
- leftSpanResultIdx[spanResult.Name] = i
- }
- for i, spanResult := range rightSpanResult {
- rightSpanResultIdx[spanResult.Name] = i
- }
-
- // NaturalJoin will merge the same column name field.
- for idx, spanResult := range leftSpanResult {
- if _, ok := rightSpanResultIdx[spanResult.Name]; ok {
- spanResult.SourceColumns, _ = base.MergeSourceColumnSet(spanResult.SourceColumns, rightSpanResult[idx].SourceColumns)
- }
- result.Columns = append(result.Columns, spanResult)
- }
- for _, spanResult := range rightSpanResult {
- if _, ok := leftSpanResultIdx[spanResult.Name]; !ok {
- result.Columns = append(result.Columns, spanResult)
- }
- }
- } else {
- if len(usingColumn) > 0 {
- // ... JOIN ... USING (...) will merge the column in USING.
- usingMap := make(map[string]bool, len(usingColumn))
- for _, using := range usingColumn {
- usingMap[using] = true
- }
-
- result.Columns = append(result.Columns, leftSpanResult...)
- for _, spanResult := range rightSpanResult {
- if _, ok := usingMap[spanResult.Name]; !ok {
- result.Columns = append(result.Columns, spanResult)
- }
- }
- } else {
- result.Columns = append(result.Columns, leftSpanResult...)
- result.Columns = append(result.Columns, rightSpanResult...)
- }
- }
-
- return result
-}
-
-// extractTableSourceFromRelationExpr extracts table source from a relation expression (simple table)
-func (q *querySpanExtractor) extractTableSourceFromRelationExpr(relationExpr parser.IRelation_exprContext, aliasClause parser.IOpt_alias_clauseContext) (base.TableSource, error) {
- if relationExpr == nil {
- return nil, errors.New("nil relation_expr")
- }
-
- qualifiedName := relationExpr.Qualified_name()
- if qualifiedName == nil {
- return nil, errors.New("relation_expr has no qualified_name")
- }
-
- // Extract the table name parts
- nameParts := NormalizeRedshiftQualifiedName(qualifiedName)
-
- var schemaName, tableName string
- switch len(nameParts) {
- case 1:
- tableName = nameParts[0]
- schemaName = "" // Will be resolved using search path
- case 2:
- schemaName = nameParts[0]
- tableName = nameParts[1]
- case 3:
- // Cross-database query: database.schema.table
- // For now, we ignore the database part and use schema.table
- schemaName = nameParts[1]
- tableName = nameParts[2]
- default:
- return nil, errors.Errorf("invalid qualified name with %d parts", len(nameParts))
- }
-
- // Extract alias and column names from alias clause
- aliasName := tableName
- var columnAliases []string
- if aliasClause != nil && aliasClause.Table_alias_clause() != nil {
- tableAlias := aliasClause.Table_alias_clause()
- if tableAlias.Table_alias() != nil {
- aliasName = normalizeRedshiftTableAlias(tableAlias.Table_alias())
- }
- if tableAlias.Name_list() != nil {
- columnAliases = normalizeRedshiftNameList(tableAlias.Name_list())
- }
- }
-
- // Find the actual table schema with columns
- tableSource, err := q.findTableSchema(schemaName, tableName)
- if err != nil {
- return nil, err
- }
-
- // Apply alias if specified
- if aliasName != tableName || len(columnAliases) > 0 {
- // If we have column aliases, use them; otherwise use the original columns
- var columns []base.QuerySpanResult
- if len(columnAliases) > 0 {
- for _, alias := range columnAliases {
- columns = append(columns, base.QuerySpanResult{Name: alias})
- }
- } else {
- columns = tableSource.GetQuerySpanResult()
- }
- return &base.PseudoTable{
- Name: aliasName,
- Columns: columns,
- }, nil
- }
-
- return tableSource, nil
-}
-
-func (q *querySpanExtractor) extractTableSourceFromUDF(funcExprWindowless parser.IFunc_expr_windowlessContext, funcAlias parser.IFunc_alias_clauseContext) (base.TableSource, error) {
- if funcExprWindowless == nil {
- return nil, errors.New("nil func_expr_windowless")
- }
-
- schemaName, funcName, args, err := q.extractFunctionElementFromFuncExprWindowless(funcExprWindowless)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to extract function element from func_expr_windowless")
- }
-
- tableSource, err := q.findFunctionDefine(schemaName, funcName, len(args))
- if err != nil {
- return nil, errors.Wrapf(err, "failed to find function definition for %s.%s with %d args", schemaName, funcName, len(args))
- }
- if funcAlias != nil {
- tableSource, err = applyFuncAliasClauseToTableSource(tableSource, funcAlias)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to apply function alias clause")
- }
- }
- return tableSource, nil
-}
-
-// extractTableSourceFromFuncTable extracts table source from a function table
-func (q *querySpanExtractor) extractTableSourceFromFuncTable(funcTable parser.IFunc_tableContext, funcAlias parser.IFunc_alias_clauseContext) (base.TableSource, error) {
- result := &base.PseudoTable{
- Name: "",
- Columns: []base.QuerySpanResult{},
- }
-
- if funcTable == nil {
- return result, nil
- }
-
- if funcTable.Func_expr_windowless() != nil {
- schemaName, funcName, args, err := q.extractFunctionElementFromFuncExprWindowless(funcTable.Func_expr_windowless())
- if err != nil {
- return nil, errors.Wrapf(err, "failed to extract function name from func_expr_windowless")
- }
- if funcName == "" {
- return nil, errors.Wrapf(err, "empty function name in func_expr_windowless")
- }
- if schemaName == "" && IsSystemFunction(funcName, "") {
- // If the schemaName is empty, we try to match the system function first.
- return q.extractTableSourceFromSystemFunctionNew(funcName, args, funcAlias)
- }
- return q.extractTableSourceFromUDF(funcTable.Func_expr_windowless(), funcAlias)
- }
-
- // The actual column count and names will be determined by the alias
- // For now, just return an empty pseudo table
- // The applyAliasToTableSource function will handle the column names
-
- // Check if this is a ROWS FROM expression
- if funcTable.ROWS() != nil && funcTable.Rowsfrom_list() != nil {
- // Handle ROWS FROM (...)
- // Each function in ROWS FROM produces columns
- // ROWS FROM expands multiple functions in parallel, creating a single result set where the first row contains
- // the first result from each function, the second row contains the second result from each function, and so on.
- tableSource := &base.PseudoTable{
- Columns: []base.QuerySpanResult{},
- }
- for _, item := range funcTable.Rowsfrom_list().AllRowsfrom_item() {
- if item.Func_expr_windowless() != nil {
- schemaName, funcName, args, err := q.extractFunctionElementFromFuncExprWindowless(funcTable.Func_expr_windowless())
- if err != nil {
- return nil, errors.Wrapf(err, "failed to extract function name from func_expr_windowless")
- }
- if funcName == "" {
- return nil, errors.Wrapf(err, "empty function name in func_expr_windowless")
- }
- var t base.TableSource
- if schemaName == "" && IsSystemFunction(funcName, "") {
- // If the schemaName is empty, we try to match the system function first.
- t, err = q.extractTableSourceFromSystemFunctionNew(funcName, args, nil)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to extract table source from system function %s", funcName)
- }
- } else {
- t, err = q.extractTableSourceFromUDF(item.Func_expr_windowless(), nil)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to extract table source from UDF %s", funcName)
- }
- }
- if t != nil {
- // Append columns from this function to the overall table source
- tableSource.Columns = append(tableSource.Columns, t.GetQuerySpanResult()...)
- }
- }
- }
- if funcAlias != nil {
- tableSource, err := applyFuncAliasClauseToTableSource(tableSource, funcAlias)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to apply function alias clause")
- }
- return tableSource, nil
- }
- return tableSource, nil
- }
-
- // For function tables, the columns are typically determined by:
- // 1. The function's return type (which we don't have access to without metadata)
- // 2. The alias provided (which will be applied later)
- // So we return an empty pseudo table that will be populated by the alias
-
- return result, nil
-}
-
-func applyAliasToTableSource(source base.TableSource, tableAliasName string, colAliases []string) (base.TableSource, error) {
- // Create a pseudo table with the alias
- pseudoTable := &base.PseudoTable{
- Name: source.GetTableName(),
- Columns: source.GetQuerySpanResult(),
- }
- if tableAliasName != "" {
- pseudoTable.Name = tableAliasName
- }
-
- // Apply column aliases if provided
- if len(colAliases) > 0 {
- // Special handling for function tables that return no columns initially
- if len(pseudoTable.Columns) == 0 {
- // Create columns based on aliases
- for _, alias := range colAliases {
- pseudoTable.Columns = append(pseudoTable.Columns, base.QuerySpanResult{
- Name: alias,
- SourceColumns: base.SourceColumnSet{},
- })
- }
- } else if len(colAliases) != len(pseudoTable.Columns) {
- // For non-empty tables, the alias count must match
- return nil, errors.Errorf("alias has %d columns but table has %d",
- len(colAliases), len(pseudoTable.Columns))
- } else {
- // Apply aliases to existing columns
- for i, alias := range colAliases {
- pseudoTable.Columns[i].Name = alias
- }
- }
- }
- return pseudoTable, nil
-}
-
-func applyTableAliasClauseToTableSource(source base.TableSource, tableAlias parser.ITable_alias_clauseContext) (base.TableSource, error) {
- aliasName := ""
- var colAliases []string
-
- // Get table alias name
- if tableAlias.Table_alias() != nil {
- aliasName = normalizeRedshiftTableAlias(tableAlias.Table_alias())
- }
-
- // Get column aliases if present
- if tableAlias.Name_list() != nil {
- for _, name := range tableAlias.Name_list().AllName() {
- colAliases = append(colAliases, normalizeRedshiftName(name))
- }
- }
-
- return applyAliasToTableSource(source, aliasName, colAliases)
-}
-
-func applyAliasClauseToTableSource(source base.TableSource, aliasClause parser.IAlias_clauseContext) (base.TableSource, error) {
- if aliasClause == nil {
- return source, nil
- }
-
- aliasName := ""
- var colAliases []string
-
- // Get table alias name
- if aliasClause.Colid() != nil {
- aliasName = normalizeRedshiftColid(aliasClause.Colid())
- }
-
- // Get column aliases if present
- if aliasClause.Name_list() != nil {
- for _, name := range aliasClause.Name_list().AllName() {
- colAliases = append(colAliases, normalizeRedshiftName(name))
- }
- }
-
- return applyAliasToTableSource(source, aliasName, colAliases)
-}
-
-// applyAliasToTableSource applies an alias to a table source
-func applyOptAliasClauseToTableSource(source base.TableSource, aliasClause parser.IOpt_alias_clauseContext) (base.TableSource, error) {
- if aliasClause == nil || aliasClause.Table_alias_clause() == nil {
- return source, nil
- }
-
- return applyTableAliasClauseToTableSource(source, aliasClause.Table_alias_clause())
-}
-
-func applyFuncAliasClauseToTableSource(source base.TableSource, funcAlias parser.IFunc_alias_clauseContext) (base.TableSource, error) {
- if funcAlias == nil {
- return source, nil
- }
-
- if funcAlias.Alias_clause() != nil {
- return applyAliasClauseToTableSource(source, funcAlias.Alias_clause())
- }
-
- aliasName := ""
- if funcAlias.Colid() != nil {
- aliasName = normalizeRedshiftColid(funcAlias.Colid())
- }
-
- var colAliases []string
- if funcAlias.Tablefuncelementlist() != nil {
- for _, name := range funcAlias.Tablefuncelementlist().AllTablefuncelement() {
- if name.Colid() != nil {
- colAliases = append(colAliases, normalizeRedshiftColid(name.Colid()))
- }
- }
- }
-
- return applyAliasToTableSource(source, aliasName, colAliases)
-}
-
-// extractColumnsFromTargetList processes the SELECT target list and returns the result columns.
-// It handles *, table.*, column references, expressions, and aliases.
-func (q *querySpanExtractor) extractColumnsFromTargetList(targetList parser.ITarget_listContext, fromFieldList []base.TableSource) ([]base.QuerySpanResult, error) {
- if targetList == nil {
- return nil, nil
- }
-
- var result []base.QuerySpanResult
-
- for _, targetEl := range targetList.AllTarget_el() {
- // Check if this is a star expansion
- if _, ok := targetEl.(*parser.Target_starContext); ok {
- // Handle SELECT * or SELECT table.*
- starColumns := q.handleStarExpansion(fromFieldList)
- result = append(result, starColumns...)
- continue
- }
-
- // Check if this is a labeled target (expression with optional alias)
- if labelCtx, ok := targetEl.(*parser.Target_labelContext); ok {
- column, err := q.handleTargetLabel(labelCtx)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to process target element")
- }
- result = append(result, column)
- continue
- }
- }
-
- return result, nil
-}
-
-// handleStarExpansion handles SELECT * expansion
-func (*querySpanExtractor) handleStarExpansion(fromFieldList []base.TableSource) []base.QuerySpanResult {
- // Simple * - expand all columns from all tables in FROM clause
- var result []base.QuerySpanResult
- for _, tableSource := range fromFieldList {
- result = append(result, tableSource.GetQuerySpanResult()...)
- }
- return result
-}
-
-// handleTargetLabel handles a labeled target element (expression with optional alias)
-func (q *querySpanExtractor) handleTargetLabel(labelCtx *parser.Target_labelContext) (base.QuerySpanResult, error) {
- // Get the expression
- expr := labelCtx.A_expr()
- if expr == nil {
- return base.QuerySpanResult{}, errors.New("target_label without expression")
- }
-
- // Extract source columns from the expression
- sourceColumns, err := q.extractSourceColumnSetFromAExpr(expr)
- if err != nil {
- return base.QuerySpanResult{}, errors.Wrapf(err, "failed to extract source columns from expression")
- }
-
- // Get the column name or alias
- columnName := ""
- if labelCtx.Target_alias() != nil {
- // Has an explicit alias
- targetAlias := labelCtx.Target_alias()
- if targetAlias.Collabel() != nil {
- columnName = normalizeRedshiftCollabel(targetAlias.Collabel())
- } else if targetAlias.Identifier() != nil {
- columnName = normalizeRedshiftIdentifier(targetAlias.Identifier())
- }
- }
-
- if columnName == "" {
- // No alias, try to extract name from expression
- columnName = q.getFieldNameFromAExpr(expr)
- if columnName == "" {
- // Use default unknown column name
- columnName = rsUnknownFieldName
- }
- }
-
- return base.QuerySpanResult{
- Name: columnName,
- SourceColumns: sourceColumns,
- }, nil
-}
-
-// getFieldNameFromAExpr attempts to extract a meaningful column name from an expression.
-// Returns "?column?" if no meaningful name can be derived.
-func (q *querySpanExtractor) getFieldNameFromAExpr(expr parser.IA_exprContext) string {
- if expr == nil {
- return rsUnknownFieldName
- }
-
- // Try to find a column reference in the expression
- columnName := q.findColumnNameInExpression(expr)
- if columnName != "" {
- return columnName
- }
-
- // Default to unknown column name for other expression types
- return rsUnknownFieldName
-}
-
-// findColumnNameInExpression recursively searches for a column name in an expression
-func (q *querySpanExtractor) findColumnNameInExpression(node antlr.ParseTree) string {
- if node == nil {
- return ""
- }
-
- // Check if this is a column reference
- if columnRef, ok := node.(parser.IColumnrefContext); ok {
- // Get the column name from the columnref
- if columnRef.Colid() != nil {
- colName := normalizeRedshiftColid(columnRef.Colid())
-
- // Check if there's indirection (qualified name)
- if columnRef.Indirection() != nil {
- parts := normalizeRedshiftIndirection(columnRef.Indirection())
- if len(parts) > 0 && parts[len(parts)-1] != "*" {
- // Return the last part as the column name
- return parts[len(parts)-1]
- }
- }
- // Simple column name
- return colName
- }
- }
-
- switch ctx := node.(type) {
- case parser.IColumnrefContext:
- if ctx.Colid() != nil {
- return normalizeRedshiftColid(ctx.Colid())
- }
- case parser.IFunc_exprContext:
- // return the function name as the column name
- _, funcName, _, err := q.extractFunctionElementFromFuncExpr(ctx)
- if err == nil && funcName != "" {
- return funcName
- }
- return rsUnknownFieldName
- case parser.IFunc_expr_windowlessContext:
- _, funcName, _, err := q.extractFunctionElementFromFuncExprWindowless(ctx)
- if err == nil && funcName != "" {
- return funcName
- }
- return rsUnknownFieldName
- default:
- }
-
- // Recursively search children
- for i := 0; i < node.GetChildCount(); i++ {
- if child, ok := node.GetChild(i).(antlr.ParseTree); ok {
- // Skip terminal nodes
- if _, isTerminal := child.(antlr.TerminalNode); isTerminal {
- continue
- }
- if name := q.findColumnNameInExpression(child); name != "" {
- return name
- }
- }
- }
-
- return ""
-}
-
-// extractSourceColumnSetFromAExpr extracts source columns from an a_expr node
-func (q *querySpanExtractor) extractSourceColumnSetFromAExpr(expr parser.IA_exprContext) (base.SourceColumnSet, error) {
- if expr == nil {
- return base.SourceColumnSet{}, nil
- }
-
- // Use the generic node traversal
- return q.extractSourceColumnSetFromNode(expr)
-}
-
-// extractSourceColumnSetFromNode recursively extracts source columns from any parse tree node.
-// This follows the pg_query_go pattern but adapted for ANTLR's tree structure.
-func (q *querySpanExtractor) extractSourceColumnSetFromNode(node antlr.ParseTree) (base.SourceColumnSet, error) {
- if node == nil {
- return base.SourceColumnSet{}, nil
- }
-
- result := make(base.SourceColumnSet)
-
- // Handle specific node types (similar to pg_query_go's switch statement)
- switch ctx := node.(type) {
- case parser.IColumnrefContext:
- // Handle column reference directly
- return q.getSourceColumnSetFromColumnRef(ctx)
- case parser.IFunc_exprContext:
- // Handle function calls
- // Process function arguments recursively
- argsResult, err := q.extractSourceColumnSetFromFuncExpr(ctx)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to extract from function expression")
- }
- if udfResult, err := q.extractSourceColumnSetFromUDF2(ctx); err == nil {
- argsResult, _ = base.MergeSourceColumnSet(argsResult, udfResult)
- }
- return argsResult, nil
- case parser.ISelect_with_parensContext:
- sourceColumnSet := make(base.SourceColumnSet)
- // Subquery in SELECT fields is special.
- // It can be the non-associated or associated subquery.
- // For associated subquery, we should set the fromFieldList as the outerSchemaInfo.
- // So that the subquery can access the outer schema.
- // The reason for new extractor is that we still need the current fromFieldList, overriding it is not expected.
- subqueryExtractor := &querySpanExtractor{
- ctx: q.ctx,
- defaultDatabase: q.defaultDatabase,
- searchPath: q.searchPath,
- metaCache: q.metaCache,
- gCtx: q.gCtx,
- ctes: q.ctes,
- outerTableSources: append(q.outerTableSources, q.tableSourcesFrom...),
- tableSourcesFrom: []base.TableSource{},
- sourceColumnsInFunction: make(base.SourceColumnSet),
- }
- tableSource, err := subqueryExtractor.extractTableSourceFromSelectWithParens(ctx)
- if err != nil {
- return base.SourceColumnSet{}, errors.Wrapf(err, "failed to extract span result from subquery %s", ctx.GetText())
- }
- spanResult := tableSource.GetQuerySpanResult()
-
- for _, field := range spanResult {
- sourceColumnSet, _ = base.MergeSourceColumnSet(sourceColumnSet, field.SourceColumns)
- }
- return sourceColumnSet, nil
- // Add more specific cases as we discover we need them...
-
- default:
- // For any unhandled node type, recursively visit all children
- // This is the generic fallback that ensures we don't miss any column references
- for i := 0; i < node.GetChildCount(); i++ {
- if child, ok := node.GetChild(i).(antlr.ParseTree); ok {
- childSources, err := q.extractSourceColumnSetFromNode(child)
- if err != nil {
- // For now, propagate errors. Could also log and continue.
- return nil, err
- }
- result, _ = base.MergeSourceColumnSet(result, childSources)
- }
- }
- }
-
- return result, nil
-}
-
-func (q *querySpanExtractor) getColumnsFromColumnRef(ctx parser.IColumnrefContext) ([]base.QuerySpanResult, error) {
- if ctx == nil {
- return nil, nil
- }
-
- // Extract schema, table, column from the columnref
- var schemaName, tableName, columnName string
-
- // Get the base column name
- if ctx.Colid() != nil {
- columnName = normalizeRedshiftColid(ctx.Colid())
- }
-
- // Handle indirection (qualified names like table.column or schema.table.column)
- if ctx.Indirection() != nil {
- parts := normalizeRedshiftIndirection(ctx.Indirection())
-
- // Check for star expansion (should have been handled elsewhere, but check anyway)
- if len(parts) > 0 && parts[len(parts)-1] == "*" {
- // This is table.* or schema.table.* - should not happen in regular column refs
- // Return empty set as this should be handled by star expansion logic
- switch len(parts) {
- case 1:
- // table.* case - columnName contains the table name from Colid()
- tableName = columnName
- columnName = ""
- schemaName = ""
- case 2:
- // schema.table.* case - columnName contains the schema name from Colid()
- schemaName = columnName
- tableName = parts[0]
- columnName = ""
- default:
- // More complex indirection with star, ignore
- return []base.QuerySpanResult{}, nil
- }
- sources, ok := q.getAllTableColumnSources(schemaName, tableName)
- if !ok {
- return []base.QuerySpanResult{}, &base.ResourceNotFoundError{
- Err: errors.New("cannot find the table for star expansion"),
- Database: &q.defaultDatabase,
- Schema: &schemaName,
- Table: &tableName,
- }
- }
- return sources, nil
- }
-
- switch len(parts) {
- case 1:
- // table.column case
- tableName = columnName
- columnName = parts[0]
- case 2:
- // schema.table.column case
- schemaName = columnName
- tableName = parts[0]
- columnName = parts[1]
- default:
- // More complex indirection, just take the last part as column
- if len(parts) > 0 {
- columnName = parts[len(parts)-1]
- }
- }
- }
-
- // Special handling for record/composite types
- // If we have a table name but no column name, it might be a whole-row reference
- if tableName != "" && columnName == "" {
- // This is a whole-row reference like "table" in row_to_json(table)
- // We need to return all columns from that table
- tableSource := q.findTableInFromNew(tableName)
- return tableSource.GetQuerySpanResult(), nil
- }
-
- // Look up the column source
- sources, columnSourceOk := q.getFieldColumnSource(schemaName, tableName, columnName)
- // The column ref in function call can be record type, such as row_to_json.
- if !columnSourceOk {
- if schemaName == "" {
- tableSource := q.findTableInFrom(tableName, columnName)
- if tableSource == nil {
- return []base.QuerySpanResult{}, &base.ResourceNotFoundError{
- Err: errors.New("cannot find the column ref"),
- Database: &q.defaultDatabase,
- Schema: &schemaName,
- Table: &tableName,
- Column: &columnName,
- }
- }
- querySpanResult := tableSource.GetQuerySpanResult()
- var result []base.QuerySpanResult
- result = append(result, querySpanResult...)
- return result, nil
- }
- return []base.QuerySpanResult{}, &base.ResourceNotFoundError{
- Err: errors.New("cannot find the column ref"),
- Database: &q.defaultDatabase,
- Schema: &schemaName,
- Table: &tableName,
- Column: &columnName,
- }
- }
- return []base.QuerySpanResult{
- {
- Name: columnName,
- SourceColumns: sources,
- },
- }, nil
-}
-
-// getSourceColumnSetFromColumnRef extracts source columns from a column reference.
-func (q *querySpanExtractor) getSourceColumnSetFromColumnRef(ctx parser.IColumnrefContext) (base.SourceColumnSet, error) {
- if ctx == nil {
- return base.SourceColumnSet{}, nil
- }
-
- columns, err := q.getColumnsFromColumnRef(ctx)
- if err != nil {
- return nil, err
- }
-
- result := make(base.SourceColumnSet)
- for _, col := range columns {
- result, _ = base.MergeSourceColumnSet(result, col.SourceColumns)
- }
- return result, nil
-}
-
-// findTableInFromNew finds a table source by name in the FROM clause or CTEs.
-func (q *querySpanExtractor) findTableInFromNew(tableName string) base.TableSource {
- // Search in FROM clause tables
- for _, tableSource := range q.tableSourcesFrom {
- if tableSource.GetTableName() == tableName {
- return tableSource
- }
- }
-
- // Search in CTEs
- for i := len(q.ctes) - 1; i >= 0; i-- {
- if q.ctes[i].Name == tableName {
- return q.ctes[i]
- }
- }
-
- // Search in outer tables (for correlated subqueries)
- for _, tableSource := range q.outerTableSources {
- if tableSource.GetTableName() == tableName {
- return tableSource
- }
- }
-
- return nil
-}
-
-// extractSourceColumnSetFromFuncExpr extracts source columns from a function expression
-func (q *querySpanExtractor) extractSourceColumnSetFromFuncExpr(funcExpr parser.IFunc_exprContext) (base.SourceColumnSet, error) {
- result := make(base.SourceColumnSet)
-
- // Handle function application
- if funcExpr.Func_application() != nil {
- funcApp := funcExpr.Func_application()
- // Process function arguments
- if funcApp.Func_arg_list() != nil {
- for _, arg := range funcApp.Func_arg_list().AllFunc_arg_expr() {
- if arg.A_expr() != nil {
- argSources, err := q.extractSourceColumnSetFromAExpr(arg.A_expr())
- if err != nil {
- return nil, err
- }
- result, _ = base.MergeSourceColumnSet(result, argSources)
- }
- }
- }
- }
-
- // Handle common subexpressions (COALESCE, GREATEST, LEAST, etc.)
- if funcExpr.Func_expr_common_subexpr() != nil {
- // Recursive extract all nodes in common subexpr
- for i := 0; i < funcExpr.Func_expr_common_subexpr().GetChildCount(); i++ {
- if child, ok := funcExpr.Func_expr_common_subexpr().GetChild(i).(antlr.ParseTree); ok {
- // Skip terminal nodes
- if _, isTerminal := child.(antlr.TerminalNode); isTerminal {
- continue
- }
- childSources, err := q.extractSourceColumnSetFromNode(child)
- if err != nil {
- return nil, err
- }
- result, _ = base.MergeSourceColumnSet(result, childSources)
- }
- }
- // Handle XML functions that may have additional expressions
- // TODO: Add more specific handling for XML functions if needed
- }
-
- // TODO: Handle window functions if needed
-
- filterClause := funcExpr.Filter_clause()
- if filterClause != nil {
- r, err := q.extractSourceColumnSetFromNode(filterClause)
- if err != nil {
- return nil, err
- }
- result, _ = base.MergeSourceColumnSet(result, r)
- }
- return result, nil
-}
-
-func getSelectNoParensFromSelectStmt(selectstmt parser.ISelectstmtContext) parser.ISelect_no_parensContext {
- if selectstmt == nil {
- return nil
- }
-
- // Direct check
- if selectstmt.Select_no_parens() != nil {
- return selectstmt.Select_no_parens()
- }
-
- // Check nested select_with_parens
- if selectstmt.Select_with_parens() != nil {
- return getSelectNoParensFromSelectWithParens(selectstmt.Select_with_parens())
- }
-
- return nil
-}
-
-func getSelectNoParensFromSelectWithParens(selectWithParens parser.ISelect_with_parensContext) parser.ISelect_no_parensContext {
- if selectWithParens == nil {
- return nil
- }
-
- // Direct check
- if selectWithParens.Select_no_parens() != nil {
- return selectWithParens.Select_no_parens()
- }
-
- // Recurse for nested parentheses
- if selectWithParens.Select_with_parens() != nil {
- return getSelectNoParensFromSelectWithParens(selectWithParens.Select_with_parens())
- }
-
- return nil
-}
-
-func (q *querySpanExtractor) extractSourceColumnSetFromUDF2(funcExpr parser.IFunc_exprContext) (base.SourceColumnSet, error) {
- schemaName, funcName, err := q.extractFunctionNameFromFuncExpr(funcExpr)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to extract function name")
- }
- if schemaName == "" && IsSystemFunction(funcName, "") {
- return base.SourceColumnSet{}, nil
- }
- nArgs := q.extractNFunctionArgsFromFuncExpr(funcExpr)
-
- result := make(base.SourceColumnSet)
- tableSource, err := q.findFunctionDefine(schemaName, funcName, nArgs)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to find function definition for %s.%s/%d", schemaName, funcName, nArgs)
- }
- for _, span := range tableSource.GetQuerySpanResult() {
- result, _ = base.MergeSourceColumnSet(result, span.SourceColumns)
- }
- return result, nil
-}
-
-func (*querySpanExtractor) extractFunctionElementFromFuncExpr(funcExpr parser.IFunc_exprContext) (string, string, []antlr.ParseTree, error) {
- args := getArgumentsFromFunctionExpr(funcExpr)
- switch {
- case funcExpr.Func_application() != nil:
- funcName := funcExpr.Func_application().Func_name()
- names := NormalizeRedshiftFuncName(funcName)
- switch len(names) {
- case 2:
- return names[0], names[1], args, nil
- case 1:
- return "", names[0], args, nil
- default:
- return "", "", nil, errors.New("invalid function name")
- }
- case funcExpr.Func_expr_common_subexpr() != nil:
- // Builtin function, return the first token text as function name
- if funcExpr.Func_expr_common_subexpr().GetStart() != nil {
- return "", strings.ToLower(funcExpr.Func_expr_common_subexpr().GetStart().GetText()), args, nil
- }
- default:
- }
- return "", "", nil, errors.New("unable to extract function name")
-}
-
-func (*querySpanExtractor) extractFunctionElementFromFuncExprWindowless(funcExprWindowless parser.IFunc_expr_windowlessContext) (string, string, []antlr.ParseTree, error) {
- args := getArgumentsFromFunctionExprWindowless(funcExprWindowless)
- switch {
- case funcExprWindowless.Func_application() != nil:
- funcName := funcExprWindowless.Func_application().Func_name()
- names := NormalizeRedshiftFuncName(funcName)
- switch len(names) {
- case 2:
- return names[0], names[1], args, nil
- case 1:
- return "", names[0], args, nil
- default:
- return "", "", nil, errors.New("invalid function name")
- }
- case funcExprWindowless.Func_expr_common_subexpr() != nil:
- // Builtin function, return the first token text as function name
- if funcExprWindowless.Func_expr_common_subexpr().GetStart() != nil {
- return "", strings.ToLower(funcExprWindowless.Func_expr_common_subexpr().GetStart().GetText()), args, nil
- }
- default:
- }
- return "", "", nil, errors.New("unable to extract function name")
-}
-
-func (*querySpanExtractor) extractFunctionNameFromFuncExpr(funcExpr parser.IFunc_exprContext) (string, string, error) {
- switch {
- case funcExpr.Func_application() != nil:
- funcName := funcExpr.Func_application().Func_name()
- if funcName.Colid() != nil {
- names := NormalizeRedshiftFuncName(funcName)
- switch len(names) {
- case 2:
- return names[0], names[1], nil
- case 1:
- return "", names[0], nil
- default:
- return "", "", errors.New("invalid function name")
- }
- } else if t := funcName.Type_function_name(); t != nil {
- // No indirection, just a simple function name
- s := t.GetText()
- if t.Identifier() != nil {
- s = normalizeRedshiftIdentifier(t.Identifier())
- }
- return "", s, nil
- } else {
- // Builtin function without schema
- return "", strings.ToLower(funcName.GetText()), nil
- }
- case funcExpr.Func_expr_common_subexpr() != nil:
- // Builtin function, return the first token text as function name
- if funcExpr.Func_expr_common_subexpr().GetStart() != nil {
- return "", strings.ToLower(funcExpr.Func_expr_common_subexpr().GetStart().GetText()), nil
- }
- default:
- }
- return "", "", errors.New("unable to extract function name")
-}
-
-func (*querySpanExtractor) extractNFunctionArgsFromFuncExpr(funcExpr parser.IFunc_exprContext) int {
- if funcExpr == nil {
- return 0
- }
-
- // This handles generic function calls like my_func(a, b).
- if fa := funcExpr.Func_application(); fa != nil {
- if fa.Func_arg_list() != nil {
- return len(fa.Func_arg_list().AllFunc_arg_expr())
- }
- // This handles cases like my_func(a) without a list rule.
- if fa.Func_arg_expr() != nil {
- return 1
- }
- return 0
- }
-
- // This handles a large set of built-in functions with special syntax.
- if f := funcExpr.Func_expr_common_subexpr(); f != nil {
- // Functions with no parentheses have zero arguments (e.g., CURRENT_DATE, CURRENT_USER).
- if f.OPEN_PAREN() == nil {
- return 0
- }
-
- // --- List-based Functions ---
- // These functions take a variable number of arguments in a simple list.
- if f.COALESCE() != nil || f.GREATEST() != nil || f.LEAST() != nil || f.XMLCONCAT() != nil {
- if list := f.Expr_list(); list != nil {
- return len(list.AllA_expr())
- }
- return 0
- }
-
- // --- Functions with Specific Keyword-based Syntax ---
-
- if f.COLLATION() != nil { // COLLATION FOR (a_expr)
- return 1
- }
-
- if f.CURRENT_TIME() != nil || f.CURRENT_TIMESTAMP() != nil || f.LOCALTIME() != nil || f.LOCALTIMESTAMP() != nil {
- // e.g., CURRENT_TIME(precision)
- if f.Iconst() != nil {
- return 1
- }
- return 0
- }
-
- if f.CAST() != nil || f.TREAT() != nil { // CAST(expr AS type), TREAT(expr AS type)
- return 2
- }
-
- if f.EXTRACT() != nil { // EXTRACT(field FROM source)
- if f.Extract_list() != nil {
- // extract_list contains 'extract_arg' and 'a_expr'.
- return 2
- }
- return 0
- }
-
- if f.NORMALIZE() != nil { // NORMALIZE(string [, form])
- count := 1 // 'string' argument is mandatory
- if f.Unicode_normal_form() != nil {
- count++
- }
- return count
- }
-
- if f.OVERLAY() != nil { // OVERLAY(string PLACING new_substring FROM start [FOR count])
- if list := f.Overlay_list(); list != nil {
- return len(list.AllA_expr()) // Counts all expressions
- }
- return 0
- }
-
- if f.POSITION() != nil { // POSITION(substring IN string)
- if f.Position_list() != nil {
- return 2
- }
- return 0
- }
-
- if f.SUBSTRING() != nil { // SUBSTRING(string [FROM start] [FOR count])
- if list := f.Substr_list(); list != nil {
- return len(list.AllA_expr())
- }
- return 0
- }
-
- if f.TRIM() != nil { // TRIM([LEADING|TRAILING|BOTH] [characters] FROM string)
- if list := f.Trim_list(); list != nil {
- count := 0
- if list.A_expr() != nil { // The optional 'characters' argument
- count++
- }
- if list.Expr_list() != nil { // The mandatory 'string' argument
- count += len(list.Expr_list().AllA_expr())
- }
- return count
- }
- return 0
- }
-
- if f.NULLIF() != nil { // NULLIF(value1, value2)
- return len(f.AllA_expr()) // Expects 2
- }
-
- // --- XML Functions ---
- if f.XMLELEMENT() != nil {
- count := 0
- if f.Collabel() != nil {
- count++
- } // The element name
- if f.Xml_attributes() != nil {
- count += len(f.Xml_attributes().Xml_attribute_list().AllXml_attribute_el())
- }
- if f.Expr_list() != nil {
- count += len(f.Expr_list().AllA_expr())
- }
- return count
- }
- if f.XMLEXISTS() != nil {
- return 2
- } // XMLEXISTS(xpath PASSING xml)
- if f.XMLFOREST() != nil {
- if list := f.Xml_attribute_list(); list != nil {
- return len(list.AllXml_attribute_el())
- }
- return 0
- }
- if f.XMLPARSE() != nil { // XMLPARSE(DOCUMENT|CONTENT string_value [WHITESPACE option])
- count := 1 // string_value
- if f.Xml_whitespace_option() != nil {
- count++
- }
- return count
- }
- if f.XMLPI() != nil { // XMLPI(NAME target [, content])
- count := 1 // target
- if len(f.AllA_expr()) > 0 {
- count++
- }
- return count
- }
- if f.XMLROOT() != nil {
- count := 2 // xml and version
- if f.Opt_xml_root_standalone() != nil {
- count++
- }
- return count
- }
- if f.XMLSERIALIZE() != nil {
- return 2
- } // XMLSERIALIZE(CONTENT value AS type)
- }
-
- return 0
-}
-
-func getArgumentsFromFunctionExpr(funcExpr parser.IFunc_exprContext) []antlr.ParseTree {
- var result []antlr.ParseTree
- if funcExpr == nil {
- return result
- }
- if fa := funcExpr.Func_application(); fa != nil {
- if fa.Func_arg_list() != nil {
- result = append(result, getArgumentsFromFuncArgList(fa.Func_arg_list())...)
- }
- if fa.Func_arg_expr() != nil {
- result = append(result, fa.Func_arg_expr())
- }
- // TODO(zp): Handle *, ALL, DISTINCT if needed
- return result
- }
- if f := funcExpr.Func_expr_common_subexpr(); f != nil {
- if f.COLLATION() != nil {
- result = append(result, f.A_expr(0))
- } else if f.CURRENT_TIME() != nil || f.CURRENT_TIMESTAMP() != nil || f.LOCALTIME() != nil || f.LOCALTIMESTAMP() != nil {
- if f.Iconst() != nil {
- result = append(result, f.Iconst())
- }
- } else if f.CAST() != nil || f.TREAT() != nil {
- result = append(result, f.A_expr(0))
- } else if f.EXTRACT() != nil {
- if f.Extract_list() != nil {
- result = append(result, f.Extract_list().A_expr())
- }
- } else if f.NORMALIZE() != nil {
- result = append(result, f.A_expr(0))
- } else if f.OVERLAY() != nil {
- if list := f.Overlay_list(); list != nil {
- for _, expr := range list.AllA_expr() {
- result = append(result, expr)
- }
- }
- } else if f.POSITION() != nil {
- if list := f.Position_list(); list != nil {
- for _, child := range list.GetChildren() {
- if expr, ok := child.(parser.IA_exprContext); ok {
- result = append(result, expr)
- }
- }
- }
- } else if f.SUBSTRING() != nil {
- if list := f.Substr_list(); list != nil {
- for _, expr := range list.AllA_expr() {
- result = append(result, expr)
- }
- }
- } else if f.TRIM() != nil {
- if list := f.Trim_list(); list != nil {
- if list.A_expr() != nil {
- result = append(result, list.A_expr())
- }
- if list.Expr_list() != nil {
- for _, expr := range list.Expr_list().AllA_expr() {
- result = append(result, expr)
- }
- }
- }
- } else if f.NULLIF() != nil {
- for _, expr := range f.AllA_expr() {
- result = append(result, expr)
- }
- } else if f.COALESCE() != nil || f.GREATEST() != nil || f.LEAST() != nil {
- if f.Expr_list() != nil {
- for _, expr := range f.Expr_list().AllA_expr() {
- result = append(result, expr)
- }
- }
- } else if f.XMLELEMENT() != nil {
- if f.Expr_list() != nil {
- for _, expr := range f.Expr_list().AllA_expr() {
- result = append(result, expr)
- }
- }
- } else if f.XMLEXISTS() != nil {
- for _, expr := range f.AllA_expr() {
- result = append(result, expr)
- }
- } else if f.XMLFOREST() != nil {
- if list := f.Xml_attribute_list(); list != nil {
- for _, attr := range list.AllXml_attribute_el() {
- result = append(result, attr.A_expr())
- }
- }
- } else if f.XMLPARSE() != nil {
- result = append(result, f.A_expr(0))
- } else if f.XMLPI() != nil {
- if len(f.AllA_expr()) > 0 {
- result = append(result, f.A_expr(0))
- }
- } else if f.XMLROOT() != nil {
- result = append(result, f.A_expr(0))
- } else if f.XMLSERIALIZE() != nil {
- result = append(result, f.A_expr(0))
- }
- // Note: Redshift doesn't support JSON_OBJECT, JSON_ARRAY, JSON_SCALAR,
- // JSON_SERIALIZE, JSON_QUERY, JSON_EXISTS, JSON_VALUE SQL constructs
- }
- return result
-}
-
-func getArgumentsFromFunctionExprWindowless(funcExprWindowless parser.IFunc_expr_windowlessContext) []antlr.ParseTree {
- var result []antlr.ParseTree
- if funcExprWindowless == nil {
- return result
- }
- if fa := funcExprWindowless.Func_application(); fa != nil {
- if fa.Func_arg_list() != nil {
- result = append(result, getArgumentsFromFuncArgList(fa.Func_arg_list())...)
- }
- if fa.Func_arg_expr() != nil {
- result = append(result, fa.Func_arg_expr())
- }
- // TODO(zp): Handle *, ALL, DISTINCT if needed
- return result
- }
- if f := funcExprWindowless.Func_expr_common_subexpr(); f != nil {
- if f.COLLATION() != nil {
- result = append(result, f.A_expr(0))
- } else if f.CURRENT_TIME() != nil || f.CURRENT_TIMESTAMP() != nil || f.LOCALTIME() != nil || f.LOCALTIMESTAMP() != nil {
- if f.Iconst() != nil {
- result = append(result, f.Iconst())
- }
- } else if f.CAST() != nil || f.TREAT() != nil {
- result = append(result, f.A_expr(0))
- } else if f.EXTRACT() != nil {
- if f.Extract_list() != nil {
- result = append(result, f.Extract_list().A_expr())
- }
- } else if f.NORMALIZE() != nil {
- result = append(result, f.A_expr(0))
- } else if f.OVERLAY() != nil {
- if list := f.Overlay_list(); list != nil {
- for _, expr := range list.AllA_expr() {
- result = append(result, expr)
- }
- }
- } else if f.POSITION() != nil {
- if list := f.Position_list(); list != nil {
- for _, child := range list.GetChildren() {
- if expr, ok := child.(parser.IA_exprContext); ok {
- result = append(result, expr)
- }
- }
- }
- } else if f.SUBSTRING() != nil {
- if list := f.Substr_list(); list != nil {
- for _, expr := range list.AllA_expr() {
- result = append(result, expr)
- }
- }
- } else if f.TRIM() != nil {
- if list := f.Trim_list(); list != nil {
- if list.A_expr() != nil {
- result = append(result, list.A_expr())
- }
- if list.Expr_list() != nil {
- for _, expr := range list.Expr_list().AllA_expr() {
- result = append(result, expr)
- }
- }
- }
- } else if f.NULLIF() != nil {
- for _, expr := range f.AllA_expr() {
- result = append(result, expr)
- }
- } else if f.COALESCE() != nil || f.GREATEST() != nil || f.LEAST() != nil {
- if f.Expr_list() != nil {
- for _, expr := range f.Expr_list().AllA_expr() {
- result = append(result, expr)
- }
- }
- } else if f.XMLELEMENT() != nil {
- if f.Expr_list() != nil {
- for _, expr := range f.Expr_list().AllA_expr() {
- result = append(result, expr)
- }
- }
- } else if f.XMLEXISTS() != nil {
- for _, expr := range f.AllA_expr() {
- result = append(result, expr)
- }
- } else if f.XMLFOREST() != nil {
- if list := f.Xml_attribute_list(); list != nil {
- for _, attr := range list.AllXml_attribute_el() {
- result = append(result, attr.A_expr())
- }
- }
- } else if f.XMLPARSE() != nil {
- result = append(result, f.A_expr(0))
- } else if f.XMLPI() != nil {
- if len(f.AllA_expr()) > 0 {
- result = append(result, f.A_expr(0))
- }
- } else if f.XMLROOT() != nil {
- result = append(result, f.A_expr(0))
- } else if f.XMLSERIALIZE() != nil {
- result = append(result, f.A_expr(0))
- }
- // Note: Redshift doesn't support JSON_OBJECT, JSON_ARRAY, JSON_SCALAR,
- // JSON_SERIALIZE, JSON_QUERY, JSON_EXISTS, JSON_VALUE SQL constructs
- }
- return result
-}
-
-func getArgumentsFromFuncArgList(funcArgList parser.IFunc_arg_listContext) []antlr.ParseTree {
- var result []antlr.ParseTree
- if funcArgList == nil {
- return result
- }
- for _, argExpr := range funcArgList.AllFunc_arg_expr() {
- result = append(result, argExpr)
- }
- return result
-}
-
-func (q *querySpanExtractor) extractTableSourceFromSystemFunctionNew(funcName string, args []antlr.ParseTree, alias parser.IFunc_alias_clauseContext) (base.TableSource, error) {
- switch strings.ToLower(funcName) {
- case generateSeries:
- // https://neon.com/postgresql/postgresql-tutorial/postgresql-generate_series
- tableSource := &base.PseudoTable{
- Name: generateSeries,
- Columns: []base.QuerySpanResult{
- {
- Name: generateSeries,
- SourceColumns: make(base.SourceColumnSet, 0),
- },
- },
- }
- return applyFuncAliasClauseToTableSource(tableSource, alias)
- case generateSubscripts:
- tableSource := &base.PseudoTable{
- Name: generateSubscripts,
- Columns: []base.QuerySpanResult{
- {
- Name: generateSubscripts,
- SourceColumns: make(base.SourceColumnSet, 0),
- },
- },
- }
- return applyFuncAliasClauseToTableSource(tableSource, alias)
- case unnest:
- tableSource := &base.PseudoTable{
- Name: unnest,
- Columns: []base.QuerySpanResult{},
- }
- if alias == nil || (alias.Alias_clause() != nil && alias.Alias_clause().Name_list() == nil) {
- for range args {
- tableSource.Columns = append(tableSource.Columns, base.QuerySpanResult{
- Name: unnest,
- SourceColumns: make(base.SourceColumnSet, 0),
- })
- }
- }
- return applyFuncAliasClauseToTableSource(tableSource, alias)
- case jsonbEach, jsonEach, jsonbEachText, jsonEachText:
- // Should be only called while jsonb_each act as table source.
- // SELECT * FROM json_test, jsonb_each(jb) AS hh(key, value) WHERE id = 1;
- var tableSource base.TableSource = &base.PseudoTable{
- Name: "",
- Columns: []base.QuerySpanResult{
- {
- Name: "key",
- SourceColumns: make(base.SourceColumnSet, 0),
- },
- {
- Name: "value",
- SourceColumns: make(base.SourceColumnSet, 0),
- },
- },
- }
- tableSource, err := applyFuncAliasClauseToTableSource(tableSource, alias)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to apply alias clause to %s", funcName)
- }
-
- if len(args) == 0 {
- return nil, errors.Errorf("unexpected empty args for function %s", funcName)
- }
- fieldArg := args[0]
- set, err := q.extractSourceColumnSetFromNode(fieldArg)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to extract source columns from argument of %s", funcName)
- }
- for i := range tableSource.GetQuerySpanResult() {
- tableSource.GetQuerySpanResult()[i].SourceColumns, _ = base.MergeSourceColumnSet(tableSource.GetQuerySpanResult()[i].SourceColumns, set)
- }
- return tableSource, nil
- case jsonToRecordset, jsonbToRecordset:
- if alias == nil {
- return nil, &base.TypeNotSupportedError{
- Extra: fmt.Sprintf("function %s must explicitly define the structure of the record with an AS clause", funcName),
- Type: "function",
- Name: funcName,
- }
- }
-
- sourceColumn := make(base.SourceColumnSet)
- for _, arg := range args {
- argSources, err := q.extractSourceColumnSetFromNode(arg)
- if err != nil {
- return nil, err
- }
- sourceColumn, _ = base.MergeSourceColumnSet(sourceColumn, argSources)
- }
-
- tableSource := &base.PseudoTable{
- Name: "",
- Columns: []base.QuerySpanResult{},
- }
-
- tableName := ""
- var columnNames []string
- if alias.Colid() != nil {
- tableName = normalizeRedshiftColid(alias.Colid())
- if alias.Tablefuncelementlist() != nil {
- for _, name := range alias.Tablefuncelementlist().AllTablefuncelement() {
- if name.Colid() != nil {
- columnNames = append(columnNames, normalizeRedshiftColid(name.Colid()))
- }
- }
- }
- } else if a := alias.Alias_clause(); a != nil {
- // Get table alias name
- if a.Colid() != nil {
- tableName = normalizeRedshiftColid(a.Colid())
- }
-
- // Get column aliases if present
- if a.Name_list() != nil {
- for _, name := range a.Name_list().AllName() {
- columnNames = append(columnNames, normalizeRedshiftName(name))
- }
- }
- }
-
- for _, colName := range columnNames {
- tableSource.Columns = append(tableSource.Columns, base.QuerySpanResult{
- Name: colName,
- SourceColumns: sourceColumn,
- })
- }
- tableSource.Name = tableName
- return tableSource, nil
- case jsonPopulateRecord, jsonbPopulateRecord, jsonPopulateRecordset, jsonbPopulateRecordset,
- jsonToRecord, jsonbToRecord:
- return nil, &base.TypeNotSupportedError{
- Extra: fmt.Sprintf("Unsupport function %s", funcName),
- Type: "function",
- Name: funcName,
- }
- default:
- // For unknown functions, continue with the generic handling below
- if alias == nil {
- return nil, &base.TypeNotSupportedError{
- Extra: "Use system function result as the table source must have the alias clause to specify table and columns name",
- Type: "function",
- Name: funcName,
- }
- }
-
- tableName := ""
- var columnNames []string
- if alias.Colid() != nil {
- tableName = normalizeRedshiftColid(alias.Colid())
- if alias.Tablefuncelementlist() != nil {
- for _, name := range alias.Tablefuncelementlist().AllTablefuncelement() {
- if name.Colid() != nil {
- columnNames = append(columnNames, normalizeRedshiftColid(name.Colid()))
- }
- }
- }
- } else if a := alias.Alias_clause(); a != nil {
- // Get table alias name
- if a.Colid() != nil {
- tableName = normalizeRedshiftColid(a.Colid())
- }
-
- // Get column aliases if present
- if a.Name_list() != nil {
- for _, name := range a.Name_list().AllName() {
- columnNames = append(columnNames, normalizeRedshiftName(name))
- }
- }
- }
- tableSource := &base.PseudoTable{
- Name: tableName,
- Columns: []base.QuerySpanResult{},
- }
- for _, colName := range columnNames {
- tableSource.Columns = append(tableSource.Columns, base.QuerySpanResult{
- Name: colName,
- SourceColumns: make(base.SourceColumnSet, 0),
- })
- }
- return tableSource, nil
- }
-}
diff --git a/backend/plugin/parser/redshift/query_span_extractor_omni.go b/backend/plugin/parser/redshift/query_span_extractor_omni.go
new file mode 100644
index 0000000000..e14f210ca8
--- /dev/null
+++ b/backend/plugin/parser/redshift/query_span_extractor_omni.go
@@ -0,0 +1,1427 @@
+package redshift
+
+import (
+ "context"
+ "slices"
+ "strings"
+
+ redshiftanalysis "github.com/bytebase/omni/redshift/analysis"
+ redshiftast "github.com/bytebase/omni/redshift/ast"
+ redshiftcatalog "github.com/bytebase/omni/redshift/catalog"
+ "github.com/pkg/errors"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/parser/base"
+ "github.com/bytebase/bytebase/backend/store/model"
+)
+
+const redshiftGetDatabaseMetadataError = "failed to get database metadata for database: %s"
+
+type omniQuerySpanExtractor struct {
+ defaultDatabase string
+ searchPath []string
+ gCtx base.GetQuerySpanContext
+}
+
+func newOmniQuerySpanExtractor(defaultDatabase string, searchPath []string, gCtx base.GetQuerySpanContext) *omniQuerySpanExtractor {
+ return &omniQuerySpanExtractor{
+ defaultDatabase: defaultDatabase,
+ searchPath: normalizeOmniSearchPath(searchPath),
+ gCtx: gCtx,
+ }
+}
+
+func normalizeOmniSearchPath(searchPath []string) []string {
+ result := make([]string, 0, len(searchPath))
+ for _, schema := range searchPath {
+ if schema != "" {
+ result = append(result, schema)
+ }
+ }
+ if len(result) == 0 {
+ return []string{"public"}
+ }
+ return result
+}
+
+func (q *omniQuerySpanExtractor) getOmniQuerySpan(ctx context.Context, statement string) (*base.QuerySpan, error) {
+ return q.getOmniQuerySpanWithFunctionStack(ctx, statement, map[string]bool{})
+}
+
+func (q *omniQuerySpanExtractor) getOmniQuerySpanWithFunctionStack(ctx context.Context, statement string, functionStack map[string]bool) (*base.QuerySpan, error) {
+ stmts, err := ParseRedshiftOmni(statement)
+ if err != nil {
+ return nil, err
+ }
+ if len(stmts) == 0 || stmts[0].Empty() {
+ return emptyOmniQuerySpan(base.Select), nil
+ }
+ if len(stmts) != 1 {
+ return nil, errors.Errorf("expected exactly 1 statement, got %d", len(stmts))
+ }
+
+ node := stmts[0].AST
+ queryType := omniQuerySpanType(node, false /* allSystems */)
+ if explain, ok := node.(*redshiftast.ExplainStmt); ok && hasOmniExplainAnalyze(explain) {
+ return q.getOmniExplainAnalyzeQuerySpan(ctx, explain.Query, queryType)
+ }
+ if unload, ok := node.(*redshiftast.UnloadStmt); ok {
+ return q.getOmniUnloadQuerySpan(ctx, unload, functionStack)
+ }
+ if _, ok := node.(*redshiftast.VariableSetStmt); ok {
+ return emptyOmniQuerySpan(queryType), nil
+ }
+ if queryType != base.Select {
+ return emptyOmniQuerySpan(queryType), nil
+ }
+
+ accessTables, earlySpan, err := q.collectOmniSelectAccessTables(ctx, node)
+ if err != nil {
+ return nil, err
+ }
+ if earlySpan != nil {
+ return earlySpan, nil
+ }
+
+ cat, err := q.newOmniQuerySpanCatalog(ctx)
+ if err != nil {
+ return nil, err
+ }
+ if err := q.installOmniQuerySpanFunctions(ctx, cat); err != nil {
+ return nil, err
+ }
+ omniSpan, err := redshiftanalysis.GetQuerySpan(cat, statement)
+ if err != nil {
+ if isOmniQuerySpanNotFound(err) {
+ return q.notFoundOmniQuerySpan(err), nil
+ }
+ return nil, err
+ }
+
+ results := q.convertOmniResults(omniSpan.Results)
+ functionAccessTables, functionResultSources, functionIssues, err := q.collectOmniFunctionBodySources(ctx, node, functionStack, make(map[string]omniFunctionBodySources))
+ if err != nil {
+ return nil, err
+ }
+ for source := range functionAccessTables {
+ accessTables[source] = true
+ }
+ if functionIssues.functionNotSupportedError != nil && len(accessTables) == 0 {
+ accessTables[base.ColumnResource{Database: q.defaultDatabase}] = true
+ }
+ mergeOmniFunctionResultSources(results, functionResultSources)
+ if hasOmniRangeVar(node) && hasResultWithoutSource(results) && !q.allOmniAccessTablesExist(ctx, accessTables) {
+ return q.notFoundOmniQuerySpan(&base.ResourceNotFoundError{Database: &q.defaultDatabase}), nil
+ }
+
+ return &base.QuerySpan{
+ Type: base.Select,
+ SourceColumns: accessTables,
+ PredicateColumns: q.convertOmniColumnList(omniSpan.PredicateColumns),
+ Results: results,
+ NotFoundError: functionIssues.notFoundError,
+ FunctionNotSupportedError: functionIssues.functionNotSupportedError,
+ }, nil
+}
+
+func (q *omniQuerySpanExtractor) getOmniExplainAnalyzeQuerySpan(ctx context.Context, query redshiftast.Node, queryType base.QueryType) (*base.QuerySpan, error) {
+ if queryType != base.Select {
+ return emptyOmniQuerySpan(queryType), nil
+ }
+
+ accessTables, earlySpan, err := q.collectOmniSelectAccessTables(ctx, query)
+ if err != nil {
+ return nil, err
+ }
+ if earlySpan != nil {
+ return earlySpan, nil
+ }
+ return &base.QuerySpan{
+ Type: base.Select,
+ SourceColumns: accessTables,
+ PredicateColumns: base.SourceColumnSet{},
+ Results: []base.QuerySpanResult{},
+ }, nil
+}
+
+func (q *omniQuerySpanExtractor) getOmniUnloadQuerySpan(ctx context.Context, unload *redshiftast.UnloadStmt, functionStack map[string]bool) (*base.QuerySpan, error) {
+ span, err := q.getOmniQuerySpanWithFunctionStack(ctx, unload.Query, functionStack)
+ if err != nil {
+ return nil, err
+ }
+ return &base.QuerySpan{
+ Type: base.DML,
+ SourceColumns: span.SourceColumns,
+ PredicateColumns: span.PredicateColumns,
+ Results: []base.QuerySpanResult{},
+ NotFoundError: span.NotFoundError,
+ FunctionNotSupportedError: span.FunctionNotSupportedError,
+ }, nil
+}
+
+func (q *omniQuerySpanExtractor) collectOmniSelectAccessTables(ctx context.Context, node redshiftast.Node) (base.SourceColumnSet, *base.QuerySpan, error) {
+ accessTables, err := q.collectOmniAccessTables(ctx, node)
+ if err != nil {
+ var resourceNotFound *base.ResourceNotFoundError
+ if errors.As(err, &resourceNotFound) {
+ return nil, q.notFoundOmniQuerySpan(resourceNotFound), nil
+ }
+ return nil, nil, err
+ }
+ allSystems, mixed := isMixedQuery(accessTables)
+ if mixed {
+ return nil, nil, base.MixUserSystemTablesError
+ }
+ if allSystems {
+ return nil, emptyOmniQuerySpan(base.SelectInfoSchema), nil
+ }
+ return accessTables, nil, nil
+}
+
+func (q *omniQuerySpanExtractor) notFoundOmniQuerySpan(err error) *base.QuerySpan {
+ return &base.QuerySpan{
+ Type: base.Select,
+ SourceColumns: base.SourceColumnSet{
+ {Database: q.defaultDatabase}: true,
+ },
+ PredicateColumns: base.SourceColumnSet{},
+ Results: []base.QuerySpanResult{},
+ NotFoundError: err,
+ }
+}
+
+func emptyOmniQuerySpan(queryType base.QueryType) *base.QuerySpan {
+ return &base.QuerySpan{
+ Type: queryType,
+ SourceColumns: base.SourceColumnSet{},
+ Results: []base.QuerySpanResult{},
+ }
+}
+
+func (q *omniQuerySpanExtractor) newOmniQuerySpanCatalog(ctx context.Context) (*redshiftcatalog.Catalog, error) {
+ if q.gCtx.GetDatabaseMetadataFunc == nil {
+ return nil, errors.New("GetDatabaseMetadataFunc is not set in GetQuerySpanContext")
+ }
+ _, metadata, err := q.gCtx.GetDatabaseMetadataFunc(ctx, q.gCtx.InstanceID, q.defaultDatabase)
+ if err != nil {
+ return nil, errors.Wrapf(err, redshiftGetDatabaseMetadataError, q.defaultDatabase)
+ }
+ if metadata == nil {
+ return nil, &base.ResourceNotFoundError{Database: &q.defaultDatabase}
+ }
+ cat := redshiftcatalog.New()
+ cat.SetSearchPath(q.searchPath)
+ cat.SetRelationResolver(&redshiftQuerySpanRelationResolver{
+ metadata: metadata,
+ })
+ return cat, nil
+}
+
+func (q *omniQuerySpanExtractor) installOmniQuerySpanFunctions(ctx context.Context, cat *redshiftcatalog.Catalog) error {
+ if q.gCtx.GetDatabaseMetadataFunc == nil {
+ return nil
+ }
+ _, metadata, err := q.gCtx.GetDatabaseMetadataFunc(ctx, q.gCtx.InstanceID, q.defaultDatabase)
+ if err != nil {
+ return errors.Wrapf(err, redshiftGetDatabaseMetadataError, q.defaultDatabase)
+ }
+ if metadata == nil {
+ return nil
+ }
+ for _, schema := range metadata.GetProto().GetSchemas() {
+ for _, function := range schema.GetFunctions() {
+ definition := strings.TrimSpace(function.GetDefinition())
+ if definition == "" {
+ continue
+ }
+ if _, err := cat.Exec(definition, nil); err != nil {
+ continue
+ }
+ }
+ }
+ return nil
+}
+
+type omniFunctionBodySources struct {
+ accessTables base.SourceColumnSet
+ results []base.QuerySpanResult
+ issues omniFunctionBodyIssues
+}
+
+type omniFunctionBodyIssues struct {
+ notFoundError error
+ functionNotSupportedError error
+}
+
+func (i *omniFunctionBodyIssues) merge(other omniFunctionBodyIssues) {
+ if i.notFoundError == nil {
+ i.notFoundError = other.notFoundError
+ }
+ if i.functionNotSupportedError == nil {
+ i.functionNotSupportedError = other.functionNotSupportedError
+ }
+}
+
+type omniFunctionSourceProjection struct {
+ names []string
+ sources []base.SourceColumnSet
+}
+
+type omniFunctionSourceScope map[string]omniFunctionSourceProjection
+
+type redshiftQuerySpanRelationResolver struct {
+ metadata *model.DatabaseMetadata
+}
+
+func (r *redshiftQuerySpanRelationResolver) ResolveRelation(schemaName, relationName string, searchPath []string) (*redshiftcatalog.RelationSpec, error) {
+ if r.metadata == nil {
+ return nil, nil
+ }
+
+ resolvedSchemaName, resolvedRelationName := schemaName, relationName
+ if resolvedSchemaName == "" {
+ resolvedSchemaName, resolvedRelationName = r.metadata.SearchObject(searchPath, relationName)
+ if resolvedSchemaName == "" && resolvedRelationName == "" {
+ return nil, nil
+ }
+ }
+
+ schema := r.metadata.GetSchemaMetadata(resolvedSchemaName)
+ if schema == nil {
+ return nil, nil
+ }
+ return redshiftRelationSpecFromMetadata(r.metadata, schema, resolvedSchemaName, resolvedRelationName), nil
+}
+
+func redshiftRelationSpecFromMetadata(metadata *model.DatabaseMetadata, schema *model.SchemaMetadata, schemaName, relationName string) *redshiftcatalog.RelationSpec {
+ if table := schema.GetTable(relationName); table != nil {
+ return redshiftTableRelationSpec(schemaName, table.GetProto().GetName(), table.GetProto().GetColumns())
+ }
+ if table := schema.GetExternalTable(relationName); table != nil {
+ return redshiftTableRelationSpec(schemaName, table.GetProto().GetName(), table.GetProto().GetColumns())
+ }
+ if view := schema.GetView(relationName); view != nil {
+ if definition := strings.TrimSpace(view.GetDefinition()); definition != "" {
+ return &redshiftcatalog.RelationSpec{
+ SchemaName: schemaName,
+ Name: view.GetName(),
+ Kind: redshiftcatalog.RelationKindView,
+ Definition: qualifyRedshiftViewDefinition(definition, schemaName, metadata),
+ }
+ }
+ return redshiftTableRelationSpec(schemaName, view.GetName(), view.GetColumns())
+ }
+ if view := schema.GetMaterializedView(relationName); view != nil {
+ if definition := strings.TrimSpace(view.GetDefinition()); definition != "" {
+ return &redshiftcatalog.RelationSpec{
+ SchemaName: schemaName,
+ Name: view.GetName(),
+ Kind: redshiftcatalog.RelationKindMaterializedView,
+ Definition: qualifyRedshiftViewDefinition(definition, schemaName, metadata),
+ }
+ }
+ return redshiftTableRelationSpec(schemaName, view.GetName(), nil)
+ }
+ if sequence := schema.GetSequence(relationName); sequence != nil {
+ return redshiftSequenceRelationSpec(schemaName, sequence.GetName())
+ }
+ return nil
+}
+
+func redshiftTableRelationSpec(schemaName, tableName string, columns []*storepb.ColumnMetadata) *redshiftcatalog.RelationSpec {
+ return &redshiftcatalog.RelationSpec{
+ SchemaName: schemaName,
+ Name: tableName,
+ Kind: redshiftcatalog.RelationKindTable,
+ Columns: redshiftRelationColumnSpecs(columns),
+ }
+}
+
+func redshiftRelationColumnSpecs(columns []*storepb.ColumnMetadata) []redshiftcatalog.RelationColumnSpec {
+ result := make([]redshiftcatalog.RelationColumnSpec, 0, len(columns))
+ for _, column := range columns {
+ if column == nil || column.GetName() == "" {
+ continue
+ }
+ result = append(result, redshiftcatalog.RelationColumnSpec{
+ Name: column.GetName(),
+ Type: normalizeCompletionType(column.GetType()),
+ })
+ }
+ if len(result) == 0 {
+ result = append(result, redshiftcatalog.RelationColumnSpec{
+ Name: "__bytebase_query_span_placeholder",
+ Type: "text",
+ })
+ }
+ return result
+}
+
+func redshiftSequenceRelationSpec(schemaName, sequenceName string) *redshiftcatalog.RelationSpec {
+ return &redshiftcatalog.RelationSpec{
+ SchemaName: schemaName,
+ Name: sequenceName,
+ Kind: redshiftcatalog.RelationKindTable,
+ Columns: []redshiftcatalog.RelationColumnSpec{
+ {Name: "last_value", Type: "bigint"},
+ {Name: "log_cnt", Type: "bigint"},
+ {Name: "is_called", Type: "boolean"},
+ },
+ }
+}
+
+type redshiftSchemaQualificationEdit struct {
+ offset int
+ schema string
+}
+
+func qualifyRedshiftViewDefinition(definition, schemaName string, metadata *model.DatabaseMetadata) string {
+ stmts, err := ParseRedshiftOmni(definition)
+ if err != nil || len(stmts) != 1 {
+ return definition
+ }
+
+ var query redshiftast.Node
+ switch stmt := stmts[0].AST.(type) {
+ case *redshiftast.ViewStmt:
+ query = stmt.Query
+ case *redshiftast.CreateTableAsStmt:
+ query = stmt.Query
+ case *redshiftast.SelectStmt:
+ query = stmt
+ default:
+ return definition
+ }
+ if query == nil {
+ return definition
+ }
+
+ searchPath := normalizeOmniSearchPath([]string{schemaName, "public"})
+ var edits []redshiftSchemaQualificationEdit
+ collectRedshiftSchemaQualificationEdits(metadata, searchPath, query, map[string]bool{}, &edits)
+ if len(edits) == 0 {
+ return definition
+ }
+
+ slices.SortFunc(edits, func(a, b redshiftSchemaQualificationEdit) int {
+ return b.offset - a.offset
+ })
+ result := definition
+ for _, edit := range edits {
+ if edit.offset < 0 || edit.offset > len(result) {
+ continue
+ }
+ result = result[:edit.offset] + quoteIdent(edit.schema) + "." + result[edit.offset:]
+ }
+ return result
+}
+
+func collectRedshiftSchemaQualificationEdits(metadata *model.DatabaseMetadata, searchPath []string, node redshiftast.Node, ctes map[string]bool, edits *[]redshiftSchemaQualificationEdit) {
+ if node == nil {
+ return
+ }
+ if selectStmt, ok := node.(*redshiftast.SelectStmt); ok {
+ collectRedshiftSchemaQualificationEditsFromSelect(metadata, searchPath, selectStmt, ctes, edits)
+ return
+ }
+
+ redshiftast.Inspect(node, func(n redshiftast.Node) bool {
+ if n != node {
+ if selectStmt, ok := n.(*redshiftast.SelectStmt); ok {
+ collectRedshiftSchemaQualificationEditsFromSelect(metadata, searchPath, selectStmt, ctes, edits)
+ return false
+ }
+ }
+ rangeVar, ok := n.(*redshiftast.RangeVar)
+ if !ok || rangeVar == nil || rangeVar.Relname == "" || rangeVar.Schemaname != "" || rangeVar.Catalogname != "" {
+ return true
+ }
+ if isOmniCTEReference(rangeVar, ctes) || rangeVar.Loc.Start < 0 {
+ return true
+ }
+ schemaName, _ := metadata.SearchObject(searchPath, rangeVar.Relname)
+ if schemaName == "" {
+ return true
+ }
+ *edits = append(*edits, redshiftSchemaQualificationEdit{
+ offset: rangeVar.Loc.Start,
+ schema: schemaName,
+ })
+ return true
+ })
+}
+
+func collectRedshiftSchemaQualificationEditsFromSelect(metadata *model.DatabaseMetadata, searchPath []string, selectStmt *redshiftast.SelectStmt, ctes map[string]bool, edits *[]redshiftSchemaQualificationEdit) {
+ localCTEs := cloneOmniCTENameSet(ctes)
+ if selectStmt.WithClause != nil && selectStmt.WithClause.Ctes != nil {
+ cteScope := cloneOmniCTENameSet(ctes)
+ if selectStmt.WithClause.Recursive {
+ for _, item := range selectStmt.WithClause.Ctes.Items {
+ cte, ok := item.(*redshiftast.CommonTableExpr)
+ if ok && cte.Ctename != "" {
+ cteScope[strings.ToLower(cte.Ctename)] = true
+ }
+ }
+ }
+ for _, item := range selectStmt.WithClause.Ctes.Items {
+ cte, ok := item.(*redshiftast.CommonTableExpr)
+ if !ok || cte.Ctename == "" {
+ continue
+ }
+ collectRedshiftSchemaQualificationEdits(metadata, searchPath, cte.Ctequery, cteScope, edits)
+ name := strings.ToLower(cte.Ctename)
+ cteScope[name] = true
+ localCTEs[name] = true
+ }
+ }
+
+ for _, node := range []redshiftast.Node{
+ selectStmt.WhereClause,
+ selectStmt.HavingClause,
+ selectStmt.QualifyClause,
+ selectStmt.LimitOffset,
+ selectStmt.LimitCount,
+ } {
+ if node != nil {
+ collectRedshiftSchemaQualificationEdits(metadata, searchPath, node, localCTEs, edits)
+ }
+ }
+ if selectStmt.IntoClause != nil {
+ collectRedshiftSchemaQualificationEdits(metadata, searchPath, selectStmt.IntoClause, localCTEs, edits)
+ }
+ if selectStmt.Larg != nil {
+ collectRedshiftSchemaQualificationEdits(metadata, searchPath, selectStmt.Larg, localCTEs, edits)
+ }
+ if selectStmt.Rarg != nil {
+ collectRedshiftSchemaQualificationEdits(metadata, searchPath, selectStmt.Rarg, localCTEs, edits)
+ }
+ for _, list := range []*redshiftast.List{
+ selectStmt.DistinctClause,
+ selectStmt.TargetList,
+ selectStmt.FromClause,
+ selectStmt.GroupClause,
+ selectStmt.WindowClause,
+ selectStmt.ExcludeList,
+ selectStmt.ValuesLists,
+ selectStmt.SortClause,
+ selectStmt.LockingClause,
+ } {
+ collectRedshiftSchemaQualificationEditsFromList(metadata, searchPath, list, localCTEs, edits)
+ }
+}
+
+func collectRedshiftSchemaQualificationEditsFromList(metadata *model.DatabaseMetadata, searchPath []string, list *redshiftast.List, ctes map[string]bool, edits *[]redshiftSchemaQualificationEdit) {
+ if list == nil {
+ return
+ }
+ for _, item := range list.Items {
+ collectRedshiftSchemaQualificationEdits(metadata, searchPath, item, ctes, edits)
+ }
+}
+
+func (q *omniQuerySpanExtractor) collectOmniAccessTables(ctx context.Context, node redshiftast.Node) (base.SourceColumnSet, error) {
+ result := make(base.SourceColumnSet)
+ if err := q.collectOmniAccessTablesFromNode(ctx, node, map[string]bool{}, result); err != nil {
+ return nil, err
+ }
+ return result, nil
+}
+
+func (q *omniQuerySpanExtractor) collectOmniAccessTablesFromNode(ctx context.Context, node redshiftast.Node, ctes map[string]bool, result base.SourceColumnSet) error {
+ if node == nil {
+ return nil
+ }
+ if selectStmt, ok := node.(*redshiftast.SelectStmt); ok {
+ return q.collectOmniAccessTablesFromSelect(ctx, selectStmt, ctes, result)
+ }
+
+ var err error
+ redshiftast.Inspect(node, func(n redshiftast.Node) bool {
+ if err != nil {
+ return false
+ }
+ if n != node {
+ if selectStmt, ok := n.(*redshiftast.SelectStmt); ok {
+ err = q.collectOmniAccessTablesFromSelect(ctx, selectStmt, ctes, result)
+ return false
+ }
+ }
+ rangeVar, ok := n.(*redshiftast.RangeVar)
+ if !ok || rangeVar == nil || rangeVar.Relname == "" {
+ return true
+ }
+ if isOmniCTEReference(rangeVar, ctes) {
+ return true
+ }
+
+ resource, resolveErr := q.resolveOmniRangeVar(ctx, rangeVar)
+ if resolveErr != nil {
+ err = resolveErr
+ return false
+ }
+ if resource.Table != "" {
+ result[resource] = true
+ }
+ return true
+ })
+ if err != nil {
+ return err
+ }
+ return nil
+}
+
+func (q *omniQuerySpanExtractor) collectOmniAccessTablesFromSelect(ctx context.Context, selectStmt *redshiftast.SelectStmt, ctes map[string]bool, result base.SourceColumnSet) error {
+ localCTEs := cloneOmniCTENameSet(ctes)
+ if selectStmt.WithClause != nil && selectStmt.WithClause.Ctes != nil {
+ cteScope := cloneOmniCTENameSet(ctes)
+ if selectStmt.WithClause.Recursive {
+ for _, item := range selectStmt.WithClause.Ctes.Items {
+ cte, ok := item.(*redshiftast.CommonTableExpr)
+ if ok && cte.Ctename != "" {
+ cteScope[strings.ToLower(cte.Ctename)] = true
+ }
+ }
+ }
+ for _, item := range selectStmt.WithClause.Ctes.Items {
+ cte, ok := item.(*redshiftast.CommonTableExpr)
+ if !ok || cte.Ctename == "" {
+ continue
+ }
+ if err := q.collectOmniAccessTablesFromNode(ctx, cte.Ctequery, cteScope, result); err != nil {
+ return err
+ }
+ name := strings.ToLower(cte.Ctename)
+ cteScope[name] = true
+ localCTEs[name] = true
+ }
+ }
+
+ for _, node := range []redshiftast.Node{
+ selectStmt.WhereClause,
+ selectStmt.HavingClause,
+ selectStmt.QualifyClause,
+ selectStmt.LimitOffset,
+ selectStmt.LimitCount,
+ } {
+ if err := q.collectOmniAccessTablesFromNode(ctx, node, localCTEs, result); err != nil {
+ return err
+ }
+ }
+ if selectStmt.IntoClause != nil {
+ if err := q.collectOmniAccessTablesFromNode(ctx, selectStmt.IntoClause, localCTEs, result); err != nil {
+ return err
+ }
+ }
+ if selectStmt.Larg != nil {
+ if err := q.collectOmniAccessTablesFromNode(ctx, selectStmt.Larg, localCTEs, result); err != nil {
+ return err
+ }
+ }
+ if selectStmt.Rarg != nil {
+ if err := q.collectOmniAccessTablesFromNode(ctx, selectStmt.Rarg, localCTEs, result); err != nil {
+ return err
+ }
+ }
+ for _, list := range []*redshiftast.List{
+ selectStmt.DistinctClause,
+ selectStmt.TargetList,
+ selectStmt.FromClause,
+ selectStmt.GroupClause,
+ selectStmt.WindowClause,
+ selectStmt.ExcludeList,
+ selectStmt.ValuesLists,
+ selectStmt.SortClause,
+ selectStmt.LockingClause,
+ } {
+ if err := q.collectOmniAccessTablesFromList(ctx, list, localCTEs, result); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
+func (q *omniQuerySpanExtractor) collectOmniAccessTablesFromList(ctx context.Context, list *redshiftast.List, ctes map[string]bool, result base.SourceColumnSet) error {
+ if list == nil {
+ return nil
+ }
+ for _, item := range list.Items {
+ if err := q.collectOmniAccessTablesFromNode(ctx, item, ctes, result); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
+func (q *omniQuerySpanExtractor) collectOmniFunctionBodySources(ctx context.Context, node redshiftast.Node, functionStack map[string]bool, cache map[string]omniFunctionBodySources) (base.SourceColumnSet, []base.SourceColumnSet, omniFunctionBodyIssues, error) {
+ accessTables, issues, err := q.collectOmniFunctionBodyAccessTables(ctx, node, functionStack, cache)
+ if err != nil {
+ return nil, nil, omniFunctionBodyIssues{}, err
+ }
+
+ var resultSources []base.SourceColumnSet
+ if selectStmt, ok := node.(*redshiftast.SelectStmt); ok {
+ var sourceIssues omniFunctionBodyIssues
+ resultSources, sourceIssues, err = q.collectOmniSelectFunctionResultSources(ctx, selectStmt, functionStack, cache, nil)
+ if err != nil {
+ return nil, nil, omniFunctionBodyIssues{}, err
+ }
+ issues.merge(sourceIssues)
+ }
+ return accessTables, resultSources, issues, nil
+}
+
+func (q *omniQuerySpanExtractor) collectOmniFunctionBodyAccessTables(ctx context.Context, node redshiftast.Node, functionStack map[string]bool, cache map[string]omniFunctionBodySources) (base.SourceColumnSet, omniFunctionBodyIssues, error) {
+ result := make(base.SourceColumnSet)
+ if node == nil {
+ return result, omniFunctionBodyIssues{}, nil
+ }
+
+ var err error
+ issues := omniFunctionBodyIssues{}
+ redshiftast.Inspect(node, func(n redshiftast.Node) bool {
+ if err != nil {
+ return false
+ }
+ call, ok := n.(*redshiftast.FuncCall)
+ if !ok {
+ return true
+ }
+ sources, sourceErr := q.omniFunctionBodySourcesForCall(ctx, call, functionStack, cache)
+ if sourceErr != nil {
+ err = sourceErr
+ return false
+ }
+ issues.merge(sources.issues)
+ for source := range sources.accessTables {
+ result[source] = true
+ }
+ return true
+ })
+ return result, issues, err
+}
+
+func (q *omniQuerySpanExtractor) collectOmniSelectFunctionResultSources(ctx context.Context, selectStmt *redshiftast.SelectStmt, functionStack map[string]bool, cache map[string]omniFunctionBodySources, outerScope omniFunctionSourceScope) ([]base.SourceColumnSet, omniFunctionBodyIssues, error) {
+ if selectStmt == nil || selectStmt.TargetList == nil {
+ return nil, omniFunctionBodyIssues{}, nil
+ }
+
+ scope, issues, err := q.omniFunctionSourceScopeForSelect(ctx, selectStmt, functionStack, cache, outerScope)
+ if err != nil {
+ return nil, omniFunctionBodyIssues{}, err
+ }
+
+ result := make([]base.SourceColumnSet, 0, selectStmt.TargetList.Len())
+ for _, item := range selectStmt.TargetList.Items {
+ target, ok := item.(*redshiftast.ResTarget)
+ if !ok {
+ result = append(result, nil)
+ continue
+ }
+ sources, sourceIssues, err := q.collectOmniFunctionBodyResultSources(ctx, target.Val, functionStack, cache, scope)
+ if err != nil {
+ return nil, omniFunctionBodyIssues{}, err
+ }
+ issues.merge(sourceIssues)
+ result = append(result, sources)
+ }
+ return result, issues, nil
+}
+
+func (q *omniQuerySpanExtractor) omniFunctionSourceScopeForSelect(ctx context.Context, selectStmt *redshiftast.SelectStmt, functionStack map[string]bool, cache map[string]omniFunctionBodySources, outerScope omniFunctionSourceScope) (omniFunctionSourceScope, omniFunctionBodyIssues, error) {
+ scope := cloneOmniFunctionSourceScope(outerScope)
+ issues := omniFunctionBodyIssues{}
+
+ if selectStmt.WithClause != nil && selectStmt.WithClause.Ctes != nil {
+ cteScope := cloneOmniFunctionSourceScope(scope)
+ for _, item := range selectStmt.WithClause.Ctes.Items {
+ cte, ok := item.(*redshiftast.CommonTableExpr)
+ if !ok || cte.Ctename == "" {
+ continue
+ }
+ cteSelect, ok := cte.Ctequery.(*redshiftast.SelectStmt)
+ if !ok {
+ continue
+ }
+ sources, sourceIssues, err := q.collectOmniSelectFunctionResultSources(ctx, cteSelect, functionStack, cache, cteScope)
+ if err != nil {
+ return nil, omniFunctionBodyIssues{}, err
+ }
+ issues.merge(sourceIssues)
+ projection := omniFunctionSourceProjection{
+ names: redshiftSelectOutputNames(cteSelect),
+ sources: sources,
+ }
+ if aliasNames := redshiftStringList(cte.Aliascolnames); len(aliasNames) > 0 {
+ projection.names = aliasNames
+ }
+ name := strings.ToLower(cte.Ctename)
+ scope[name] = projection
+ cteScope[name] = projection
+ }
+ }
+
+ sourceIssues, err := q.collectOmniFunctionSourceScopeFromList(ctx, selectStmt.FromClause, functionStack, cache, scope)
+ if err != nil {
+ return nil, omniFunctionBodyIssues{}, err
+ }
+ issues.merge(sourceIssues)
+ return scope, issues, nil
+}
+
+func (q *omniQuerySpanExtractor) collectOmniFunctionSourceScopeFromList(ctx context.Context, list *redshiftast.List, functionStack map[string]bool, cache map[string]omniFunctionBodySources, scope omniFunctionSourceScope) (omniFunctionBodyIssues, error) {
+ if list == nil {
+ return omniFunctionBodyIssues{}, nil
+ }
+ issues := omniFunctionBodyIssues{}
+ for _, item := range list.Items {
+ sourceIssues, err := q.collectOmniFunctionSourceScopeFromNode(ctx, item, functionStack, cache, scope)
+ if err != nil {
+ return omniFunctionBodyIssues{}, err
+ }
+ issues.merge(sourceIssues)
+ }
+ return issues, nil
+}
+
+func (q *omniQuerySpanExtractor) collectOmniFunctionSourceScopeFromNode(ctx context.Context, node redshiftast.Node, functionStack map[string]bool, cache map[string]omniFunctionBodySources, scope omniFunctionSourceScope) (omniFunctionBodyIssues, error) {
+ switch n := node.(type) {
+ case *redshiftast.RangeVar:
+ if n == nil || n.Alias == nil || n.Alias.Aliasname == "" {
+ return omniFunctionBodyIssues{}, nil
+ }
+ projection, ok := scope[strings.ToLower(n.Relname)]
+ if !ok {
+ return omniFunctionBodyIssues{}, nil
+ }
+ if aliasNames := redshiftStringList(n.Alias.Colnames); len(aliasNames) > 0 {
+ projection.names = aliasNames
+ }
+ scope[strings.ToLower(n.Alias.Aliasname)] = projection
+ return omniFunctionBodyIssues{}, nil
+ case *redshiftast.RangeSubselect:
+ if n == nil || n.Alias == nil || n.Alias.Aliasname == "" {
+ return omniFunctionBodyIssues{}, nil
+ }
+ selectStmt, ok := n.Subquery.(*redshiftast.SelectStmt)
+ if !ok {
+ return omniFunctionBodyIssues{}, nil
+ }
+ sources, issues, err := q.collectOmniSelectFunctionResultSources(ctx, selectStmt, functionStack, cache, scope)
+ if err != nil {
+ return omniFunctionBodyIssues{}, err
+ }
+ projection := omniFunctionSourceProjection{
+ names: redshiftSelectOutputNames(selectStmt),
+ sources: sources,
+ }
+ if aliasNames := redshiftStringList(n.Alias.Colnames); len(aliasNames) > 0 {
+ projection.names = aliasNames
+ }
+ scope[strings.ToLower(n.Alias.Aliasname)] = projection
+ return issues, nil
+ case *redshiftast.JoinExpr:
+ if n == nil {
+ return omniFunctionBodyIssues{}, nil
+ }
+ leftIssues, err := q.collectOmniFunctionSourceScopeFromNode(ctx, n.Larg, functionStack, cache, scope)
+ if err != nil {
+ return omniFunctionBodyIssues{}, err
+ }
+ rightIssues, err := q.collectOmniFunctionSourceScopeFromNode(ctx, n.Rarg, functionStack, cache, scope)
+ if err != nil {
+ return omniFunctionBodyIssues{}, err
+ }
+ leftIssues.merge(rightIssues)
+ return leftIssues, nil
+ default:
+ return omniFunctionBodyIssues{}, nil
+ }
+}
+
+func (q *omniQuerySpanExtractor) collectOmniFunctionBodyResultSources(ctx context.Context, node redshiftast.Node, functionStack map[string]bool, cache map[string]omniFunctionBodySources, scope omniFunctionSourceScope) (base.SourceColumnSet, omniFunctionBodyIssues, error) {
+ result := make(base.SourceColumnSet)
+ if node == nil {
+ return result, omniFunctionBodyIssues{}, nil
+ }
+
+ var err error
+ issues := omniFunctionBodyIssues{}
+ redshiftast.Inspect(node, func(n redshiftast.Node) bool {
+ if err != nil {
+ return false
+ }
+ if columnRef, ok := n.(*redshiftast.ColumnRef); ok {
+ mergeOmniSourceColumnSet(result, resolveOmniFunctionColumnRef(scope, columnRef))
+ return true
+ }
+ call, ok := n.(*redshiftast.FuncCall)
+ if !ok {
+ return true
+ }
+ sources, sourceErr := q.omniFunctionBodySourcesForCall(ctx, call, functionStack, cache)
+ if sourceErr != nil {
+ err = sourceErr
+ return false
+ }
+ issues.merge(sources.issues)
+ for _, resultColumn := range sources.results {
+ for source := range resultColumn.SourceColumns {
+ result[source] = true
+ }
+ }
+ return true
+ })
+ return result, issues, err
+}
+
+func (q *omniQuerySpanExtractor) omniFunctionBodySourcesForCall(ctx context.Context, call *redshiftast.FuncCall, functionStack map[string]bool, cache map[string]omniFunctionBodySources) (omniFunctionBodySources, error) {
+ schemaName, functionName := omniFunctionCallName(call.Funcname)
+ if functionName == "" || IsSystemFunction(functionName, "") {
+ return omniFunctionBodySources{accessTables: base.SourceColumnSet{}}, nil
+ }
+
+ functionSchema, function, findErr := q.findOmniFunctionMetadata(ctx, schemaName, functionName, call.Args.Len())
+ if findErr != nil || function == nil {
+ return omniFunctionBodySources{accessTables: base.SourceColumnSet{}}, findErr
+ }
+
+ cacheKey := strings.ToLower(functionSchema + "." + function.GetName() + "/" + function.GetSignature())
+ if sources, ok := cache[cacheKey]; ok {
+ return sources, nil
+ }
+ if functionStack[cacheKey] {
+ return omniFunctionBodySources{accessTables: base.SourceColumnSet{}}, nil
+ }
+
+ body, ok, bodyErr := extractOmniSQLFunctionBody(function.GetDefinition())
+ if bodyErr != nil {
+ // Body extraction failures should fail closed through QuerySpan.FunctionNotSupportedError.
+ //nolint:nilerr
+ return omniFunctionBodySources{
+ accessTables: base.SourceColumnSet{},
+ issues: omniFunctionBodyIssues{
+ functionNotSupportedError: &base.FunctionNotSupportedError{
+ Err: bodyErr,
+ Function: functionSchema + "." + function.GetName(),
+ Definition: function.GetDefinition(),
+ },
+ },
+ }, nil
+ }
+ if !ok {
+ return omniFunctionBodySources{
+ accessTables: base.SourceColumnSet{},
+ issues: omniFunctionBodyIssues{
+ functionNotSupportedError: &base.FunctionNotSupportedError{
+ Err: errors.New("unsupported or empty SQL function body"),
+ Function: functionSchema + "." + function.GetName(),
+ Definition: function.GetDefinition(),
+ },
+ },
+ }, nil
+ }
+
+ functionStack[cacheKey] = true
+ span, querySpanErr := q.getOmniQuerySpanWithFunctionStack(ctx, body, functionStack)
+ delete(functionStack, cacheKey)
+ if querySpanErr != nil {
+ return omniFunctionBodySources{}, errors.Wrapf(querySpanErr, "failed to get query span for function: %s.%s", functionSchema, functionName)
+ }
+ sources := omniFunctionBodySources{
+ accessTables: span.SourceColumns,
+ results: span.Results,
+ issues: omniFunctionBodyIssues{
+ notFoundError: span.NotFoundError,
+ functionNotSupportedError: span.FunctionNotSupportedError,
+ },
+ }
+ if sources.issues.notFoundError == nil && sources.issues.functionNotSupportedError == nil {
+ cache[cacheKey] = sources
+ }
+ return sources, nil
+}
+
+func (q *omniQuerySpanExtractor) findOmniFunctionMetadata(ctx context.Context, schemaName, functionName string, argCount int) (string, *storepb.FunctionMetadata, error) {
+ if q.gCtx.GetDatabaseMetadataFunc == nil {
+ return "", nil, nil
+ }
+ _, metadata, err := q.gCtx.GetDatabaseMetadataFunc(ctx, q.gCtx.InstanceID, q.defaultDatabase)
+ if err != nil {
+ return "", nil, errors.Wrapf(err, redshiftGetDatabaseMetadataError, q.defaultDatabase)
+ }
+ if metadata == nil {
+ return "", nil, nil
+ }
+
+ searchPath := q.searchPath
+ if schemaName != "" {
+ searchPath = []string{schemaName}
+ }
+ schemas, functions := metadata.SearchFunctions(searchPath, functionName)
+ if len(functions) == 0 {
+ return "", nil, nil
+ }
+ if len(functions) == 1 {
+ return schemas[0], functions[0], nil
+ }
+ for i, function := range functions {
+ count, ok := omniFunctionParameterCount(function.GetDefinition())
+ if ok && count == argCount {
+ return schemas[i], function, nil
+ }
+ }
+ return "", nil, nil
+}
+
+func omniFunctionCallName(list *redshiftast.List) (string, string) {
+ if list == nil {
+ return "", ""
+ }
+ parts := make([]string, 0, list.Len())
+ for _, item := range list.Items {
+ if value, ok := item.(*redshiftast.String); ok {
+ parts = append(parts, value.Str)
+ }
+ }
+ switch len(parts) {
+ case 1:
+ return "", parts[0]
+ case 2:
+ return parts[0], parts[1]
+ case 3:
+ return parts[1], parts[2]
+ default:
+ return "", ""
+ }
+}
+
+func omniFunctionParameterCount(definition string) (int, bool) {
+ stmts, err := ParseRedshiftOmni(definition)
+ if err != nil || len(stmts) != 1 {
+ return 0, false
+ }
+ stmt, ok := stmts[0].AST.(*redshiftast.CreateFunctionStmt)
+ if !ok || stmt.Parameters == nil {
+ return 0, ok
+ }
+ return stmt.Parameters.Len(), true
+}
+
+func extractOmniSQLFunctionBody(definition string) (string, bool, error) {
+ stmts, err := ParseRedshiftOmni(definition)
+ if err != nil {
+ return "", false, errors.Wrapf(err, "failed to parse function definition")
+ }
+ if len(stmts) != 1 {
+ return "", false, nil
+ }
+ stmt, ok := stmts[0].AST.(*redshiftast.CreateFunctionStmt)
+ if !ok {
+ return "", false, nil
+ }
+
+ language := ""
+ body := ""
+ if stmt.Options != nil {
+ for _, item := range stmt.Options.Items {
+ elem, ok := item.(*redshiftast.DefElem)
+ if !ok {
+ continue
+ }
+ switch strings.ToLower(elem.Defname) {
+ case "language":
+ language = omniDefElemString(elem.Arg)
+ case "as":
+ body = omniDefElemString(elem.Arg)
+ default:
+ }
+ }
+ }
+ if language != "" && !strings.EqualFold(language, "sql") {
+ return "", false, nil
+ }
+ body = strings.TrimSpace(body)
+ if body == "" {
+ return "", false, nil
+ }
+ return body, true, nil
+}
+
+func omniDefElemString(node redshiftast.Node) string {
+ switch value := node.(type) {
+ case *redshiftast.String:
+ return value.Str
+ case *redshiftast.List:
+ if len(value.Items) == 0 {
+ return ""
+ }
+ return omniDefElemString(value.Items[0])
+ default:
+ return ""
+ }
+}
+
+func mergeOmniFunctionResultSources(results []base.QuerySpanResult, functionResultSources []base.SourceColumnSet) {
+ if len(functionResultSources) == len(results) {
+ for i, sources := range functionResultSources {
+ for source := range sources {
+ results[i].SourceColumns[source] = true
+ }
+ }
+ return
+ }
+ for _, sources := range functionResultSources {
+ if len(sources) == 0 {
+ continue
+ }
+ for i := range results {
+ if len(results[i].SourceColumns) != 0 {
+ continue
+ }
+ for source := range sources {
+ results[i].SourceColumns[source] = true
+ }
+ }
+ }
+}
+
+func cloneOmniFunctionSourceScope(scope omniFunctionSourceScope) omniFunctionSourceScope {
+ result := make(omniFunctionSourceScope, len(scope))
+ for name, projection := range scope {
+ result[name] = projection
+ }
+ return result
+}
+
+func mergeOmniSourceColumnSet(dst, src base.SourceColumnSet) {
+ for source := range src {
+ dst[source] = true
+ }
+}
+
+func resolveOmniFunctionColumnRef(scope omniFunctionSourceScope, columnRef *redshiftast.ColumnRef) base.SourceColumnSet {
+ parts := redshiftColumnRefParts(columnRef)
+ switch len(parts) {
+ case 1:
+ return resolveUnqualifiedOmniFunctionColumn(scope, parts[0])
+ case 2:
+ if projection, ok := scope[strings.ToLower(parts[0])]; ok {
+ return projection.omniFunctionSourcesForColumn(parts[1])
+ }
+ return base.SourceColumnSet{}
+ default:
+ return base.SourceColumnSet{}
+ }
+}
+
+func resolveUnqualifiedOmniFunctionColumn(scope omniFunctionSourceScope, columnName string) base.SourceColumnSet {
+ var result base.SourceColumnSet
+ for _, projection := range scope {
+ sources := projection.omniFunctionSourcesForColumn(columnName)
+ if len(sources) == 0 {
+ continue
+ }
+ if result != nil {
+ return base.SourceColumnSet{}
+ }
+ result = sources
+ }
+ if result == nil {
+ return base.SourceColumnSet{}
+ }
+ return result
+}
+
+func (p omniFunctionSourceProjection) omniFunctionSourcesForColumn(columnName string) base.SourceColumnSet {
+ for i, name := range p.names {
+ if !strings.EqualFold(name, columnName) || i >= len(p.sources) {
+ continue
+ }
+ return p.sources[i]
+ }
+ return base.SourceColumnSet{}
+}
+
+func redshiftSelectOutputNames(selectStmt *redshiftast.SelectStmt) []string {
+ if selectStmt == nil || selectStmt.TargetList == nil {
+ return nil
+ }
+ result := make([]string, 0, selectStmt.TargetList.Len())
+ for _, item := range selectStmt.TargetList.Items {
+ target, ok := item.(*redshiftast.ResTarget)
+ if !ok {
+ result = append(result, "")
+ continue
+ }
+ result = append(result, redshiftTargetOutputName(target))
+ }
+ return result
+}
+
+func redshiftTargetOutputName(target *redshiftast.ResTarget) string {
+ if target == nil {
+ return ""
+ }
+ if target.Name != "" {
+ return target.Name
+ }
+ if parts := redshiftColumnRefParts(asOmniColumnRef(target.Val)); len(parts) > 0 {
+ return parts[len(parts)-1]
+ }
+ if call, ok := target.Val.(*redshiftast.FuncCall); ok {
+ _, functionName := omniFunctionCallName(call.Funcname)
+ return functionName
+ }
+ return ""
+}
+
+func asOmniColumnRef(node redshiftast.Node) *redshiftast.ColumnRef {
+ columnRef, ok := node.(*redshiftast.ColumnRef)
+ if !ok {
+ return nil
+ }
+ return columnRef
+}
+
+func redshiftColumnRefParts(columnRef *redshiftast.ColumnRef) []string {
+ if columnRef == nil || columnRef.Fields == nil {
+ return nil
+ }
+ result := make([]string, 0, columnRef.Fields.Len())
+ for _, item := range columnRef.Fields.Items {
+ value, ok := item.(*redshiftast.String)
+ if !ok {
+ return nil
+ }
+ result = append(result, value.Str)
+ }
+ return result
+}
+
+func redshiftStringList(list *redshiftast.List) []string {
+ if list == nil {
+ return nil
+ }
+ result := make([]string, 0, list.Len())
+ for _, item := range list.Items {
+ value, ok := item.(*redshiftast.String)
+ if !ok {
+ continue
+ }
+ result = append(result, value.Str)
+ }
+ return result
+}
+
+func cloneOmniCTENameSet(ctes map[string]bool) map[string]bool {
+ result := make(map[string]bool, len(ctes))
+ for name, ok := range ctes {
+ result[name] = ok
+ }
+ return result
+}
+
+func isOmniCTEReference(rangeVar *redshiftast.RangeVar, ctes map[string]bool) bool {
+ if rangeVar == nil || rangeVar.Schemaname != "" || rangeVar.Catalogname != "" {
+ return false
+ }
+ return ctes[strings.ToLower(rangeVar.Relname)]
+}
+
+func hasResultWithoutSource(results []base.QuerySpanResult) bool {
+ for _, result := range results {
+ if len(result.SourceColumns) == 0 {
+ return true
+ }
+ }
+ return false
+}
+
+func hasOmniRangeVar(node redshiftast.Node) bool {
+ found := false
+ redshiftast.Inspect(node, func(n redshiftast.Node) bool {
+ if _, ok := n.(*redshiftast.RangeVar); ok {
+ found = true
+ return false
+ }
+ return true
+ })
+ return found
+}
+
+func (q *omniQuerySpanExtractor) allOmniAccessTablesExist(ctx context.Context, accessTables base.SourceColumnSet) bool {
+ if len(accessTables) == 0 || q.gCtx.GetDatabaseMetadataFunc == nil {
+ return false
+ }
+ for resource := range accessTables {
+ if !q.omniAccessTableExists(ctx, resource) {
+ return false
+ }
+ }
+ return true
+}
+
+func (q *omniQuerySpanExtractor) omniAccessTableExists(ctx context.Context, resource base.ColumnResource) bool {
+ if isSystemResource(resource) {
+ return true
+ }
+ database := resource.Database
+ if database == "" {
+ database = q.defaultDatabase
+ }
+ _, metadata, err := q.gCtx.GetDatabaseMetadataFunc(ctx, q.gCtx.InstanceID, database)
+ if err != nil || metadata == nil {
+ return false
+ }
+ if resource.Schema == "" {
+ schemaName, objectName := metadata.SearchObject(q.searchPath, resource.Table)
+ return schemaName != "" || objectName != ""
+ }
+ schema := metadata.GetSchemaMetadata(resource.Schema)
+ if schema == nil {
+ return false
+ }
+ return schema.GetTable(resource.Table) != nil ||
+ schema.GetView(resource.Table) != nil ||
+ schema.GetMaterializedView(resource.Table) != nil ||
+ schema.GetExternalTable(resource.Table) != nil ||
+ schema.GetFunction(resource.Table) != nil ||
+ schema.GetSequence(resource.Table) != nil
+}
+
+func (q *omniQuerySpanExtractor) resolveOmniRangeVar(ctx context.Context, rangeVar *redshiftast.RangeVar) (base.ColumnResource, error) {
+ resource := base.ColumnResource{
+ Database: q.defaultDatabase,
+ Schema: rangeVar.Schemaname,
+ Table: rangeVar.Relname,
+ }
+ if rangeVar.Catalogname != "" {
+ resource.Database = rangeVar.Catalogname
+ }
+ if resource.Schema != "" || isSystemResource(resource) || q.gCtx.GetDatabaseMetadataFunc == nil {
+ return resource, nil
+ }
+
+ _, metadata, err := q.gCtx.GetDatabaseMetadataFunc(ctx, q.gCtx.InstanceID, resource.Database)
+ if err != nil {
+ return base.ColumnResource{}, errors.Wrapf(err, redshiftGetDatabaseMetadataError, resource.Database)
+ }
+ if metadata == nil {
+ return resource, nil
+ }
+ schemaName, objectName := metadata.SearchObject(q.searchPath, resource.Table)
+ if schemaName == "" && objectName == "" {
+ return base.ColumnResource{}, &base.ResourceNotFoundError{
+ Database: &resource.Database,
+ Table: &resource.Table,
+ }
+ }
+ resource.Schema = schemaName
+ resource.Table = objectName
+ return resource, nil
+}
+
+func (q *omniQuerySpanExtractor) convertOmniResults(results []redshiftanalysis.QuerySpanResult) []base.QuerySpanResult {
+ converted := make([]base.QuerySpanResult, 0, len(results))
+ for _, result := range results {
+ converted = append(converted, base.QuerySpanResult{
+ Name: result.Name,
+ SourceColumns: q.convertOmniColumnList(result.SourceColumns),
+ IsPlainField: result.IsPlainField,
+ })
+ }
+ return converted
+}
+
+func (q *omniQuerySpanExtractor) convertOmniColumnList(columns []redshiftanalysis.ColumnResource) base.SourceColumnSet {
+ result := make(base.SourceColumnSet)
+ for _, column := range columns {
+ result[base.ColumnResource{
+ Database: q.defaultDatabase,
+ Schema: column.Schema,
+ Table: column.Table,
+ Column: column.Column,
+ }] = true
+ }
+ return result
+}
+
+func omniQuerySpanType(node redshiftast.Node, allSystems bool) base.QueryType {
+ switch n := node.(type) {
+ case *redshiftast.SelectStmt:
+ if hasOmniSelectIntoClause(n) {
+ return base.DDL
+ }
+ if allSystems {
+ return base.SelectInfoSchema
+ }
+ return base.Select
+ case *redshiftast.ExplainStmt:
+ if hasOmniExplainAnalyze(n) {
+ return omniQuerySpanType(n.Query, allSystems)
+ }
+ return base.Explain
+ case *redshiftast.RedshiftShowStmt, *redshiftast.RedshiftDescStmt, *redshiftast.VariableShowStmt:
+ return base.SelectInfoSchema
+ case *redshiftast.VariableSetStmt:
+ return base.Select
+ case *redshiftast.InsertStmt, *redshiftast.UpdateStmt, *redshiftast.DeleteStmt, *redshiftast.MergeStmt,
+ *redshiftast.CopyStmt, *redshiftast.UnloadStmt, *redshiftast.RefreshMatViewStmt, *redshiftast.CallStmt:
+ return base.DML
+ case *redshiftast.CreateStmt, *redshiftast.CreateTableAsStmt, *redshiftast.ViewStmt, *redshiftast.IndexStmt, *redshiftast.CreateSeqStmt,
+ *redshiftast.CreateSchemaStmt, *redshiftast.CreateFunctionStmt, *redshiftast.CreatedbStmt, *redshiftast.CreateTrigStmt,
+ *redshiftast.CreateEnumStmt, *redshiftast.CreateDomainStmt, *redshiftast.CreateEventTrigStmt, *redshiftast.CreatePLangStmt,
+ *redshiftast.CreateFdwStmt, *redshiftast.CreateForeignServerStmt, *redshiftast.CreateForeignTableStmt,
+ *redshiftast.CreateUserMappingStmt, *redshiftast.CreateExtensionStmt, *redshiftast.CreateTableSpaceStmt,
+ *redshiftast.CreateAmStmt, *redshiftast.CreatePolicyStmt, *redshiftast.CreatePublicationStmt,
+ *redshiftast.CreateSubscriptionStmt, *redshiftast.CreateStatsStmt, *redshiftast.CreateOpClassStmt,
+ *redshiftast.CreateOpFamilyStmt, *redshiftast.CreateCastStmt, *redshiftast.CreateTransformStmt,
+ *redshiftast.CreateConversionStmt, *redshiftast.CreateRangeStmt, *redshiftast.CreateRoleStmt,
+ *redshiftast.DropStmt, *redshiftast.DropdbStmt, *redshiftast.DropRoleStmt, *redshiftast.DropUserMappingStmt,
+ *redshiftast.DropSubscriptionStmt, *redshiftast.DropTableSpaceStmt, *redshiftast.DropOwnedStmt,
+ *redshiftast.AlterTableStmt, *redshiftast.AlterTableMoveAllStmt, *redshiftast.AlterSeqStmt,
+ *redshiftast.AlterEnumStmt, *redshiftast.AlterDomainStmt, *redshiftast.AlterObjectSchemaStmt,
+ *redshiftast.AlterOwnerStmt, *redshiftast.AlterDatabaseStmt, *redshiftast.AlterDatabaseSetStmt,
+ *redshiftast.AlterDatabaseRefreshCollStmt, *redshiftast.AlterSystemStmt, *redshiftast.AlterCollationStmt,
+ *redshiftast.AlterFunctionStmt, *redshiftast.AlterEventTrigStmt, *redshiftast.AlterFdwStmt,
+ *redshiftast.AlterForeignServerStmt, *redshiftast.AlterUserMappingStmt, *redshiftast.AlterExtensionStmt,
+ *redshiftast.AlterExtensionContentsStmt, *redshiftast.AlterTableSpaceOptionsStmt, *redshiftast.AlterPolicyStmt,
+ *redshiftast.AlterPublicationStmt, *redshiftast.AlterSubscriptionStmt, *redshiftast.AlterObjectDependsStmt,
+ *redshiftast.AlterOperatorStmt, *redshiftast.AlterTypeStmt, *redshiftast.AlterDefaultPrivilegesStmt,
+ *redshiftast.AlterTSDictionaryStmt, *redshiftast.AlterTSConfigurationStmt, *redshiftast.AlterStatsStmt,
+ *redshiftast.AlterOpFamilyStmt, *redshiftast.AlterRoleStmt, *redshiftast.AlterRoleSetStmt,
+ *redshiftast.RenameStmt, *redshiftast.TruncateStmt, *redshiftast.VacuumStmt, *redshiftast.GrantStmt,
+ *redshiftast.GrantRoleStmt, *redshiftast.CommentStmt, *redshiftast.LockStmt, *redshiftast.ClusterStmt,
+ *redshiftast.ReindexStmt, *redshiftast.RuleStmt, *redshiftast.CheckPointStmt, *redshiftast.DiscardStmt,
+ *redshiftast.LoadStmt, *redshiftast.ConstraintsSetStmt, *redshiftast.FetchStmt, *redshiftast.SecLabelStmt,
+ *redshiftast.DoStmt, *redshiftast.ImportForeignSchemaStmt, *redshiftast.ReassignOwnedStmt,
+ *redshiftast.RedshiftObjectStmt:
+ return base.DDL
+ default:
+ return base.QueryTypeUnknown
+ }
+}
+
+func hasOmniSelectIntoClause(n *redshiftast.SelectStmt) bool {
+ if n == nil {
+ return false
+ }
+ if n.IntoClause != nil {
+ return true
+ }
+ if hasOmniSelectIntoClause(n.Larg) {
+ return true
+ }
+ if hasOmniSelectIntoClause(n.Rarg) {
+ return true
+ }
+ return false
+}
+
+func isOmniQuerySpanNotFound(err error) bool {
+ if err == nil {
+ return false
+ }
+ msg := strings.ToLower(err.Error())
+ return strings.Contains(msg, "does not exist") || strings.Contains(msg, "not found")
+}
diff --git a/backend/plugin/parser/redshift/query_span_omni_test.go b/backend/plugin/parser/redshift/query_span_omni_test.go
new file mode 100644
index 0000000000..84932840e7
--- /dev/null
+++ b/backend/plugin/parser/redshift/query_span_omni_test.go
@@ -0,0 +1,887 @@
+package redshift
+
+import (
+ "context"
+ "os"
+ "testing"
+
+ "github.com/pkg/errors"
+ "github.com/stretchr/testify/require"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/parser/base"
+ "github.com/bytebase/bytebase/backend/store/model"
+)
+
+func TestRedshiftOmniQuerySpanSupportedShapes(t *testing.T) {
+ q := newOmniQuerySpanExtractor("db", []string{"analytics", "public"}, redshiftOmniQuerySpanContext(t))
+
+ tests := []struct {
+ name string
+ statement string
+ wantResults []base.QuerySpanResult
+ wantSource []base.ColumnResource
+ wantPredicate []base.ColumnResource
+ wantNotFoundText string
+ }{
+ {
+ name: "plain columns and expression",
+ statement: "SELECT id, amount AS total, amount + tax AS gross FROM orders WHERE status = 'open'",
+ wantResults: []base.QuerySpanResult{
+ {Name: "id", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "id"}}), IsPlainField: true},
+ {Name: "total", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "amount"}}), IsPlainField: true},
+ {Name: "gross", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{
+ {Database: "db", Schema: "public", Table: "orders", Column: "amount"},
+ {Database: "db", Schema: "public", Table: "orders", Column: "tax"},
+ })},
+ },
+ wantSource: []base.ColumnResource{
+ {Database: "db", Schema: "public", Table: "orders"},
+ },
+ wantPredicate: []base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "status"}},
+ },
+ {
+ name: "schema-qualified external table",
+ statement: "SELECT external_id FROM spectrum.spectrum_orders",
+ wantResults: []base.QuerySpanResult{
+ {Name: "external_id", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "spectrum", Table: "spectrum_orders", Column: "external_id"}}), IsPlainField: true},
+ },
+ wantSource: []base.ColumnResource{{Database: "db", Schema: "spectrum", Table: "spectrum_orders"}},
+ },
+ {
+ name: "cte lineage",
+ statement: "WITH q(x) AS (SELECT id FROM orders) SELECT x FROM q",
+ wantResults: []base.QuerySpanResult{
+ {Name: "x", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "id"}}), IsPlainField: true},
+ },
+ wantSource: []base.ColumnResource{
+ {Database: "db", Schema: "public", Table: "orders"},
+ },
+ },
+ {
+ name: "view definition lineage",
+ statement: "SELECT id, amount FROM active_orders",
+ wantResults: []base.QuerySpanResult{
+ {Name: "id", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "id"}}), IsPlainField: true},
+ {Name: "amount", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "amount"}}), IsPlainField: true},
+ },
+ wantSource: []base.ColumnResource{
+ {Database: "db", Schema: "public", Table: "active_orders"},
+ },
+ },
+ {
+ name: "missing relation returns NotFoundError span",
+ statement: "SELECT id FROM missing_orders",
+ wantResults: []base.QuerySpanResult{},
+ wantSource: []base.ColumnResource{{Database: "db"}},
+ wantNotFoundText: "missing_orders",
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ span, err := q.getOmniQuerySpan(context.Background(), test.statement)
+ require.NoError(t, err)
+ require.NotNil(t, span)
+ require.Equal(t, base.Select, span.Type)
+ require.Equal(t, test.wantResults, span.Results)
+ require.Equal(t, redshiftSourceColumnSetFromList(test.wantSource), span.SourceColumns)
+ require.Equal(t, redshiftSourceColumnSetFromList(test.wantPredicate), span.PredicateColumns)
+ if test.wantNotFoundText != "" {
+ require.Error(t, span.NotFoundError)
+ require.ErrorContains(t, span.NotFoundError, test.wantNotFoundText)
+ } else {
+ require.NoError(t, span.NotFoundError)
+ }
+ })
+ }
+}
+
+func TestRedshiftQuerySpanEntrypointDoesNotDependOnANTLR(t *testing.T) {
+ for _, path := range []string{"query_span.go", "query_span_extractor_omni.go"} {
+ content, err := os.ReadFile(path)
+ require.NoError(t, err)
+ source := string(content)
+ require.NotContains(t, source, "github.com/antlr4-go/antlr/v4", path)
+ require.NotContains(t, source, "github.com/bytebase/parser/redshift", path)
+ require.NotContains(t, source, "ParseRedshift(", path)
+ }
+}
+
+func TestRedshiftOmniQuerySpanUsesLazyRelationResolver(t *testing.T) {
+ content, err := os.ReadFile("query_span_extractor_omni.go")
+ require.NoError(t, err)
+ source := string(content)
+ require.Contains(t, source, "SetRelationResolver")
+ require.NotContains(t, source, "pendingOmniCatalogView")
+ require.NotContains(t, source, "buildOmniQuerySpanCatalog")
+ require.NotContains(t, source, "orderPendingOmniCatalogViews")
+ require.NotContains(t, source, "createQuerySpanViewDDL")
+}
+
+func TestRedshiftOmniQuerySpanNonSelectTypes(t *testing.T) {
+ q := newOmniQuerySpanExtractor("db", []string{"public"}, redshiftOmniQuerySpanContext(t))
+
+ tests := []struct {
+ name string
+ statement string
+ want base.QueryType
+ }{
+ {
+ name: "copy is dml",
+ statement: "COPY orders FROM 's3://bucket/orders.csv' IAM_ROLE 'arn:aws:iam::123456789012:role/redshift';",
+ want: base.DML,
+ },
+ {
+ name: "call is dml",
+ statement: "CALL refresh_orders();",
+ want: base.DML,
+ },
+ {
+ name: "create table is ddl",
+ statement: "CREATE TABLE created_orders(id int);",
+ want: base.DDL,
+ },
+ {
+ name: "comment is ddl",
+ statement: "COMMENT ON TABLE orders IS 'hello';",
+ want: base.DDL,
+ },
+ {
+ name: "create user is ddl",
+ statement: "CREATE USER report_user PASSWORD 'Password1';",
+ want: base.DDL,
+ },
+ {
+ name: "alter user is ddl",
+ statement: "ALTER USER report_user RENAME TO report_user2;",
+ want: base.DDL,
+ },
+ {
+ name: "alter user options is ddl",
+ statement: "ALTER USER report_user PASSWORD 'Password2';",
+ want: base.DDL,
+ },
+ {
+ name: "alter user set is ddl",
+ statement: "ALTER USER report_user SET search_path TO public;",
+ want: base.DDL,
+ },
+ {
+ name: "drop user is ddl",
+ statement: "DROP USER report_user;",
+ want: base.DDL,
+ },
+ {
+ name: "grant role is ddl",
+ statement: "GRANT report_role TO report_user;",
+ want: base.DDL,
+ },
+ {
+ name: "show tables is information schema",
+ statement: "SHOW TABLES;",
+ want: base.SelectInfoSchema,
+ },
+ {
+ name: "desc table is information schema",
+ statement: "DESC TABLE orders;",
+ want: base.SelectInfoSchema,
+ },
+ {
+ name: "system table query is information schema",
+ statement: "SELECT oid FROM pg_class;",
+ want: base.SelectInfoSchema,
+ },
+ {
+ name: "explain select is explain",
+ statement: "EXPLAIN SELECT id FROM orders;",
+ want: base.Explain,
+ },
+ {
+ name: "explain analyze select has sources but no result columns",
+ statement: "EXPLAIN ANALYZE SELECT id FROM orders;",
+ want: base.Select,
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ span, err := q.getOmniQuerySpan(context.Background(), test.statement)
+ require.NoError(t, err)
+ require.NotNil(t, span)
+ require.Equal(t, test.want, span.Type)
+ require.Empty(t, span.Results)
+ if test.name == "explain analyze select has sources but no result columns" {
+ require.Equal(t, redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders"}}), span.SourceColumns)
+ } else {
+ require.Empty(t, span.SourceColumns)
+ }
+ })
+ }
+
+ t.Run("unload is dml export with inner sources", func(t *testing.T) {
+ span, err := q.getOmniQuerySpan(context.Background(), "UNLOAD ('SELECT id FROM orders WHERE status = ''open''') TO 's3://bucket/out' IAM_ROLE DEFAULT;")
+ require.NoError(t, err)
+ require.NotNil(t, span)
+ require.Equal(t, base.DML, span.Type)
+ require.Empty(t, span.Results)
+ require.Equal(t, redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders"}}), span.SourceColumns)
+ require.Equal(t, redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "status"}}), span.PredicateColumns)
+ })
+}
+
+func TestRedshiftQuerySpanUsesOmniPath(t *testing.T) {
+ span, err := GetQuerySpan(context.Background(), redshiftOmniQuerySpanContext(t), base.Statement{Text: "SELECT id FROM orders"}, "db", "public", false)
+ require.NoError(t, err)
+ require.NotNil(t, span)
+ require.Equal(t, []base.QuerySpanResult{
+ {Name: "id", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "id"}}), IsPlainField: true},
+ }, span.Results)
+}
+
+func TestRedshiftOmniQuerySpanDefaultSearchPath(t *testing.T) {
+ span, err := newOmniQuerySpanExtractor("db", nil, redshiftOmniQuerySpanContext(t)).getOmniQuerySpan(context.Background(), "SELECT id FROM orders")
+ require.NoError(t, err)
+ require.Equal(t, []base.QuerySpanResult{
+ {Name: "id", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "id"}}), IsPlainField: true},
+ }, span.Results)
+ require.Equal(t, redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders"}}), span.SourceColumns)
+}
+
+func TestRedshiftOmniQuerySpanRootAndQueryTypeCoverage(t *testing.T) {
+ q := newOmniQuerySpanExtractor("db", []string{"public"}, redshiftOmniQuerySpanContext(t))
+
+ t.Run("empty statement returns empty select span", func(t *testing.T) {
+ span, err := q.getOmniQuerySpan(context.Background(), "")
+ require.NoError(t, err)
+ require.Equal(t, base.Select, span.Type)
+ require.Empty(t, span.Results)
+ require.Empty(t, span.SourceColumns)
+ })
+
+ t.Run("multiple statements are rejected", func(t *testing.T) {
+ _, err := q.getOmniQuerySpan(context.Background(), "SELECT 1; SELECT 2;")
+ require.ErrorContains(t, err, "expected exactly 1 statement")
+ })
+
+ tests := []struct {
+ name string
+ statement string
+ want base.QueryType
+ }{
+ {name: "set is select", statement: "SET search_path TO public;", want: base.Select},
+ {name: "select into is ddl", statement: "SELECT id INTO new_orders FROM orders;", want: base.DDL},
+ {name: "set operation select into is ddl", statement: "SELECT id INTO new_orders FROM orders UNION SELECT id FROM orders;", want: base.DDL},
+ {name: "create table as is ddl", statement: "CREATE TABLE copied_orders AS SELECT id FROM orders;", want: base.DDL},
+ {name: "drop table is ddl", statement: "DROP TABLE orders;", want: base.DDL},
+ {name: "alter table is ddl", statement: "ALTER TABLE orders ADD COLUMN note varchar(100);", want: base.DDL},
+ {name: "truncate is ddl", statement: "TRUNCATE TABLE orders;", want: base.DDL},
+ {name: "insert is dml", statement: "INSERT INTO orders(id) SELECT event_id FROM analytics.events;", want: base.DML},
+ {name: "update is dml", statement: "UPDATE orders SET amount = amount + 1 WHERE id = 1;", want: base.DML},
+ {name: "delete is dml", statement: "DELETE FROM orders WHERE id = 1;", want: base.DML},
+ {name: "refresh materialized view is dml", statement: "REFRESH MATERIALIZED VIEW order_totals_mv;", want: base.DML},
+ }
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ span, err := q.getOmniQuerySpan(context.Background(), test.statement)
+ require.NoError(t, err)
+ require.Equal(t, test.want, span.Type)
+ require.Empty(t, span.Results)
+ })
+ }
+}
+
+func TestRedshiftOmniQuerySpanExpressionCoverage(t *testing.T) {
+ q := newOmniQuerySpanExtractor("db", []string{"public"}, redshiftOmniQuerySpanContext(t))
+
+ tests := []struct {
+ name string
+ statement string
+ want []base.QuerySpanResult
+ }{
+ {
+ name: "star expands table columns",
+ statement: "SELECT * FROM orders",
+ want: []base.QuerySpanResult{
+ {Name: "id", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "id"}}), IsPlainField: true},
+ {Name: "amount", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "amount"}}), IsPlainField: true},
+ {Name: "tax", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "tax"}}), IsPlainField: true},
+ {Name: "status", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "status"}}), IsPlainField: true},
+ {Name: "customer_id", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "customer_id"}}), IsPlainField: true},
+ },
+ },
+ {
+ name: "table alias star expands aliased table",
+ statement: "SELECT o.* FROM orders AS o",
+ want: []base.QuerySpanResult{
+ {Name: "id", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "id"}}), IsPlainField: true},
+ {Name: "amount", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "amount"}}), IsPlainField: true},
+ {Name: "tax", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "tax"}}), IsPlainField: true},
+ {Name: "status", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "status"}}), IsPlainField: true},
+ {Name: "customer_id", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "customer_id"}}), IsPlainField: true},
+ },
+ },
+ {
+ name: "duplicate output names are preserved",
+ statement: "SELECT amount AS value, tax AS value FROM orders",
+ want: []base.QuerySpanResult{
+ {Name: "value", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "amount"}}), IsPlainField: true},
+ {Name: "value", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "tax"}}), IsPlainField: true},
+ },
+ },
+ {
+ name: "quoted identifiers preserve exact output names",
+ statement: `SELECT "select", "Camel" AS "ExactCase" FROM keyword_table`,
+ want: []base.QuerySpanResult{
+ {Name: "select", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "keyword_table", Column: "select"}}), IsPlainField: true},
+ {Name: "ExactCase", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "keyword_table", Column: "Camel"}}), IsPlainField: true},
+ },
+ },
+ {
+ name: "case cast and coalesce merge expression sources",
+ statement: "SELECT CASE WHEN amount > 0 THEN tax ELSE customer_id END AS case_value, CAST(amount AS decimal(10,2)) AS cast_value, COALESCE(amount, tax) AS coalesced FROM orders",
+ want: []base.QuerySpanResult{
+ {Name: "case_value", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{
+ {Database: "db", Schema: "public", Table: "orders", Column: "amount"},
+ {Database: "db", Schema: "public", Table: "orders", Column: "tax"},
+ {Database: "db", Schema: "public", Table: "orders", Column: "customer_id"},
+ })},
+ {Name: "cast_value", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "amount"}})},
+ {Name: "coalesced", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{
+ {Database: "db", Schema: "public", Table: "orders", Column: "amount"},
+ {Database: "db", Schema: "public", Table: "orders", Column: "tax"},
+ })},
+ },
+ },
+ {
+ name: "aggregate expressions keep argument sources",
+ statement: "SELECT SUM(amount) AS total, AVG(tax) AS avg_tax FROM orders",
+ want: []base.QuerySpanResult{
+ {Name: "total", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "amount"}})},
+ {Name: "avg_tax", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "tax"}})},
+ },
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ span, err := q.getOmniQuerySpan(context.Background(), test.statement)
+ require.NoError(t, err)
+ require.Equal(t, test.want, span.Results)
+ })
+ }
+}
+
+func TestRedshiftOmniQuerySpanJoinAndScopeCoverage(t *testing.T) {
+ q := newOmniQuerySpanExtractor("db", []string{"analytics", "public"}, redshiftOmniQuerySpanContext(t))
+
+ tests := []struct {
+ name string
+ statement string
+ wantResults []base.QuerySpanResult
+ wantSource []base.ColumnResource
+ wantPredicate []base.ColumnResource
+ }{
+ {
+ name: "join aliases resolve both sides",
+ statement: "SELECT o.id, c.name FROM orders AS o JOIN customers AS c ON o.customer_id = c.id WHERE c.region = 'apac'",
+ wantResults: []base.QuerySpanResult{
+ {Name: "id", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "id"}}), IsPlainField: true},
+ {Name: "name", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "customers", Column: "name"}}), IsPlainField: true},
+ },
+ wantSource: []base.ColumnResource{
+ {Database: "db", Schema: "public", Table: "orders"},
+ {Database: "db", Schema: "public", Table: "customers"},
+ },
+ wantPredicate: []base.ColumnResource{
+ {Database: "db", Schema: "public", Table: "orders", Column: "customer_id"},
+ {Database: "db", Schema: "public", Table: "customers", Column: "id"},
+ {Database: "db", Schema: "public", Table: "customers", Column: "region"},
+ },
+ },
+ {
+ name: "comma separated tables preserve access tables",
+ statement: "SELECT o.id, c.name FROM orders o, customers c WHERE o.customer_id = c.id",
+ wantResults: []base.QuerySpanResult{
+ {Name: "id", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "id"}}), IsPlainField: true},
+ {Name: "name", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "customers", Column: "name"}}), IsPlainField: true},
+ },
+ wantSource: []base.ColumnResource{
+ {Database: "db", Schema: "public", Table: "orders"},
+ {Database: "db", Schema: "public", Table: "customers"},
+ },
+ wantPredicate: []base.ColumnResource{
+ {Database: "db", Schema: "public", Table: "orders", Column: "customer_id"},
+ {Database: "db", Schema: "public", Table: "customers", Column: "id"},
+ },
+ },
+ {
+ name: "derived table aliases flow source columns",
+ statement: "SELECT d.order_id, d.gross FROM (SELECT id AS order_id, amount + tax AS gross FROM orders) AS d",
+ wantResults: []base.QuerySpanResult{
+ {Name: "order_id", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "id"}}), IsPlainField: true},
+ {Name: "gross", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{
+ {Database: "db", Schema: "public", Table: "orders", Column: "amount"},
+ {Database: "db", Schema: "public", Table: "orders", Column: "tax"},
+ }), IsPlainField: true},
+ },
+ wantSource: []base.ColumnResource{{Database: "db", Schema: "public", Table: "orders"}},
+ },
+ {
+ name: "search path resolves analytics before public",
+ statement: "SELECT event_id FROM events",
+ wantResults: []base.QuerySpanResult{
+ {Name: "event_id", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "analytics", Table: "events", Column: "event_id"}}), IsPlainField: true},
+ },
+ wantSource: []base.ColumnResource{{Database: "db", Schema: "analytics", Table: "events"}},
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ span, err := q.getOmniQuerySpan(context.Background(), test.statement)
+ require.NoError(t, err)
+ require.Equal(t, test.wantResults, span.Results)
+ require.Equal(t, redshiftSourceColumnSetFromList(test.wantSource), span.SourceColumns)
+ require.Equal(t, redshiftSourceColumnSetFromList(test.wantPredicate), span.PredicateColumns)
+ })
+ }
+}
+
+func TestRedshiftOmniQuerySpanSubqueryAndCTECoverage(t *testing.T) {
+ q := newOmniQuerySpanExtractor("db", []string{"public"}, redshiftOmniQuerySpanContext(t))
+
+ tests := []struct {
+ name string
+ statement string
+ want []base.QuerySpanResult
+ wantSource []base.ColumnResource
+ }{
+ {
+ name: "target subquery carries inner lineage",
+ statement: "SELECT (SELECT name FROM customers WHERE customers.id = orders.customer_id) AS customer_name FROM orders",
+ want: []base.QuerySpanResult{
+ {Name: "customer_name", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "customers", Column: "name"}})},
+ },
+ wantSource: []base.ColumnResource{
+ {Database: "db", Schema: "public", Table: "orders"},
+ {Database: "db", Schema: "public", Table: "customers"},
+ },
+ },
+ {
+ name: "where exists contributes access tables",
+ statement: "SELECT id FROM orders WHERE EXISTS (SELECT 1 FROM line_items WHERE line_items.order_id = orders.id)",
+ want: []base.QuerySpanResult{
+ {Name: "id", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "id"}}), IsPlainField: true},
+ },
+ wantSource: []base.ColumnResource{
+ {Database: "db", Schema: "public", Table: "orders"},
+ {Database: "db", Schema: "public", Table: "line_items"},
+ },
+ },
+ {
+ name: "cte name shadows physical table",
+ statement: "WITH orders AS (SELECT id FROM line_items) SELECT id FROM orders",
+ want: []base.QuerySpanResult{
+ {Name: "id", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "line_items", Column: "id"}}), IsPlainField: true},
+ },
+ wantSource: []base.ColumnResource{{Database: "db", Schema: "public", Table: "line_items"}},
+ },
+ {
+ name: "multiple independent ctes join",
+ statement: "WITH base AS (SELECT id FROM orders), named AS (SELECT name FROM customers) SELECT base.id, named.name FROM base JOIN named ON true",
+ want: []base.QuerySpanResult{
+ {Name: "id", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "id"}}), IsPlainField: true},
+ {Name: "name", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "customers", Column: "name"}}), IsPlainField: true},
+ },
+ wantSource: []base.ColumnResource{
+ {Database: "db", Schema: "public", Table: "orders"},
+ {Database: "db", Schema: "public", Table: "customers"},
+ },
+ },
+ {
+ name: "nested cte shadow does not hide outer physical table",
+ statement: "SELECT id FROM orders WHERE EXISTS (WITH orders AS (SELECT 1) SELECT 1 FROM orders)",
+ want: []base.QuerySpanResult{
+ {Name: "id", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "id"}}), IsPlainField: true},
+ },
+ wantSource: []base.ColumnResource{{Database: "db", Schema: "public", Table: "orders"}},
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ span, err := q.getOmniQuerySpan(context.Background(), test.statement)
+ require.NoError(t, err)
+ require.Equal(t, test.want, span.Results)
+ require.Equal(t, redshiftSourceColumnSetFromList(test.wantSource), span.SourceColumns)
+ })
+ }
+}
+
+func TestRedshiftOmniQuerySpanMetadataAndErrorCoverage(t *testing.T) {
+ q := newOmniQuerySpanExtractor("db", []string{"public"}, redshiftOmniQuerySpanContext(t))
+
+ t.Run("materialized view definition lineage", func(t *testing.T) {
+ span, err := q.getOmniQuerySpan(context.Background(), "SELECT customer_id, total_amount FROM order_totals_mv")
+ require.NoError(t, err)
+ require.Equal(t, []base.QuerySpanResult{
+ {Name: "customer_id", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "customer_id"}}), IsPlainField: true},
+ {Name: "total_amount", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "amount"}}), IsPlainField: true},
+ }, span.Results)
+ require.Equal(t, redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "order_totals_mv"}}), span.SourceColumns)
+ })
+
+ t.Run("view definition waits for later dependency", func(t *testing.T) {
+ span, err := q.getOmniQuerySpan(context.Background(), "SELECT id FROM a_view")
+ require.NoError(t, err)
+ require.Equal(t, []base.QuerySpanResult{
+ {Name: "id", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "id"}}), IsPlainField: true},
+ }, span.Results)
+ require.Equal(t, redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "a_view"}}), span.SourceColumns)
+ })
+
+ t.Run("view definition resolves unqualified dependencies from view schema", func(t *testing.T) {
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Name: "db",
+ SearchPath: "analytics, public",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "orders",
+ Columns: []*storepb.ColumnMetadata{{Name: "id", Type: "int"}},
+ },
+ },
+ Views: []*storepb.ViewMetadata{
+ {
+ Name: "unqualified_orders_view",
+ Definition: "CREATE VIEW public.unqualified_orders_view AS SELECT id FROM orders;",
+ Columns: []*storepb.ColumnMetadata{{Name: "id", Type: "int"}},
+ },
+ },
+ },
+ {
+ Name: "analytics",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "orders",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "int"},
+ {Name: "event_id", Type: "int"},
+ },
+ },
+ },
+ },
+ },
+ }
+ getter, lister := redshiftMockDatabaseMetadataGetter([]*storepb.DatabaseSchemaMetadata{metadata})
+ localQ := newOmniQuerySpanExtractor("db", []string{"analytics", "public"}, base.GetQuerySpanContext{
+ GetDatabaseMetadataFunc: getter,
+ ListDatabaseNamesFunc: lister,
+ })
+ span, err := localQ.getOmniQuerySpan(context.Background(), "SELECT id FROM unqualified_orders_view")
+ require.NoError(t, err)
+ require.Equal(t, []base.QuerySpanResult{
+ {Name: "id", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "id"}}), IsPlainField: true},
+ }, span.Results)
+ require.Equal(t, redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "unqualified_orders_view"}}), span.SourceColumns)
+ })
+
+ t.Run("full create view definition applies column aliases", func(t *testing.T) {
+ span, err := q.getOmniQuerySpan(context.Background(), "SELECT order_id FROM aliased_orders")
+ require.NoError(t, err)
+ require.Equal(t, []base.QuerySpanResult{
+ {Name: "order_id", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "id"}}), IsPlainField: true},
+ }, span.Results)
+ require.Equal(t, redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "aliased_orders"}}), span.SourceColumns)
+ })
+
+ t.Run("full create materialized view definition applies column aliases", func(t *testing.T) {
+ span, err := q.getOmniQuerySpan(context.Background(), "SELECT order_id FROM aliased_orders_mv")
+ require.NoError(t, err)
+ require.Equal(t, []base.QuerySpanResult{
+ {Name: "order_id", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "id"}}), IsPlainField: true},
+ }, span.Results)
+ require.Equal(t, redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "aliased_orders_mv"}}), span.SourceColumns)
+ })
+
+ t.Run("sql udf body contributes source columns", func(t *testing.T) {
+ span, err := q.getOmniQuerySpan(context.Background(), "SELECT reporting_fn()")
+ require.NoError(t, err)
+ require.Equal(t, redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders"}}), span.SourceColumns)
+ require.Equal(t, []base.QuerySpanResult{
+ {Name: "reporting_fn", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "id"}})},
+ }, span.Results)
+ })
+
+ t.Run("schema qualified sql udf body contributes source columns", func(t *testing.T) {
+ span, err := q.getOmniQuerySpan(context.Background(), "SELECT analytics.reporting_fn()")
+ require.NoError(t, err)
+ require.Equal(t, redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "analytics", Table: "events"}}), span.SourceColumns)
+ require.Equal(t, []base.QuerySpanResult{
+ {Name: "reporting_fn", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "analytics", Table: "events", Column: "event_id"}})},
+ }, span.Results)
+ })
+
+ t.Run("sql udf body result sources propagate through cte", func(t *testing.T) {
+ span, err := q.getOmniQuerySpan(context.Background(), "WITH c AS (SELECT reporting_fn() AS x) SELECT x FROM c")
+ require.NoError(t, err)
+ require.Equal(t, []base.QuerySpanResult{
+ {Name: "x", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "id"}}), IsPlainField: true},
+ }, span.Results)
+ })
+
+ t.Run("sql udf body result sources propagate through subquery", func(t *testing.T) {
+ span, err := q.getOmniQuerySpan(context.Background(), "SELECT c.x FROM (SELECT reporting_fn() AS x) AS c")
+ require.NoError(t, err)
+ require.Equal(t, []base.QuerySpanResult{
+ {Name: "x", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "orders", Column: "id"}}), IsPlainField: true},
+ }, span.Results)
+ })
+
+ t.Run("sql udf body missing relation returns not found span", func(t *testing.T) {
+ span, err := q.getOmniQuerySpan(context.Background(), "SELECT stale_reporting_fn()")
+ require.NoError(t, err)
+ require.Error(t, span.NotFoundError)
+ require.ErrorContains(t, span.NotFoundError, "missing_orders")
+ require.Equal(t, base.Select, span.Type)
+ require.Equal(t, redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db"}}), span.SourceColumns)
+ })
+
+ t.Run("unsupported sql udf body returns function not supported span", func(t *testing.T) {
+ span, err := q.getOmniQuerySpan(context.Background(), "SELECT unsupported_reporting_fn()")
+ require.NoError(t, err)
+ require.Error(t, span.FunctionNotSupportedError)
+ require.ErrorContains(t, span.FunctionNotSupportedError, "public.unsupported_reporting_fn")
+ require.Equal(t, base.Select, span.Type)
+ require.Equal(t, redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db"}}), span.SourceColumns)
+ })
+
+ t.Run("sequence read uses default sequence columns", func(t *testing.T) {
+ span, err := q.getOmniQuerySpan(context.Background(), "SELECT last_value, log_cnt, is_called FROM order_id_seq")
+ require.NoError(t, err)
+ require.Equal(t, redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "order_id_seq"}}), span.SourceColumns)
+ require.Equal(t, []base.QuerySpanResult{
+ {Name: "last_value", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "order_id_seq", Column: "last_value"}}), IsPlainField: true},
+ {Name: "log_cnt", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "order_id_seq", Column: "log_cnt"}}), IsPlainField: true},
+ {Name: "is_called", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "order_id_seq", Column: "is_called"}}), IsPlainField: true},
+ }, span.Results)
+ })
+
+ t.Run("view fallback uses declared columns when definition is missing", func(t *testing.T) {
+ span, err := q.getOmniQuerySpan(context.Background(), "SELECT id, amount FROM fallback_view")
+ require.NoError(t, err)
+ require.Equal(t, []base.QuerySpanResult{
+ {Name: "id", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "fallback_view", Column: "id"}}), IsPlainField: true},
+ {Name: "amount", SourceColumns: redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db", Schema: "public", Table: "fallback_view", Column: "amount"}}), IsPlainField: true},
+ }, span.Results)
+ })
+
+ t.Run("missing relation inside subquery returns not found span", func(t *testing.T) {
+ span, err := q.getOmniQuerySpan(context.Background(), "SELECT id FROM orders WHERE EXISTS (SELECT 1 FROM missing_orders)")
+ require.NoError(t, err)
+ require.Error(t, span.NotFoundError)
+ require.ErrorContains(t, span.NotFoundError, "missing_orders")
+ require.Empty(t, span.Results)
+ require.Equal(t, redshiftSourceColumnSetFromList([]base.ColumnResource{{Database: "db"}}), span.SourceColumns)
+ })
+
+ t.Run("mixed system and user tables are rejected", func(t *testing.T) {
+ _, err := q.getOmniQuerySpan(context.Background(), "SELECT orders.id FROM orders, pg_class")
+ require.ErrorIs(t, err, base.MixUserSystemTablesError)
+ })
+
+ t.Run("syntax error propagates through public entrypoint", func(t *testing.T) {
+ _, err := GetQuerySpan(context.Background(), redshiftOmniQuerySpanContext(t), base.Statement{
+ Text: "SELECT * FORM orders;",
+ Start: &storepb.Position{Line: 7, Column: 1},
+ }, "db", "public", false)
+ require.Error(t, err)
+ var syntaxErr *base.SyntaxError
+ require.ErrorAs(t, err, &syntaxErr)
+ require.Equal(t, int32(7), syntaxErr.Position.Line)
+ })
+}
+
+func redshiftOmniQuerySpanContext(t *testing.T) base.GetQuerySpanContext {
+ t.Helper()
+ getter, lister := redshiftMockDatabaseMetadataGetter([]*storepb.DatabaseSchemaMetadata{redshiftOmniQuerySpanMetadata()})
+ return base.GetQuerySpanContext{
+ GetDatabaseMetadataFunc: getter,
+ ListDatabaseNamesFunc: lister,
+ }
+}
+
+func redshiftOmniQuerySpanMetadata() *storepb.DatabaseSchemaMetadata {
+ return &storepb.DatabaseSchemaMetadata{
+ Name: "db",
+ SearchPath: "analytics, public",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "orders",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "int"},
+ {Name: "amount", Type: "numeric"},
+ {Name: "tax", Type: "numeric"},
+ {Name: "status", Type: "varchar"},
+ {Name: "customer_id", Type: "int"},
+ },
+ },
+ {
+ Name: "customers",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "int"},
+ {Name: "name", Type: "varchar"},
+ {Name: "region", Type: "varchar"},
+ },
+ },
+ {
+ Name: "line_items",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "int"},
+ {Name: "order_id", Type: "int"},
+ {Name: "sku", Type: "varchar"},
+ },
+ },
+ {
+ Name: "keyword_table",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "select", Type: "int"},
+ {Name: "Camel", Type: "int"},
+ },
+ },
+ },
+ Views: []*storepb.ViewMetadata{
+ {
+ Name: "active_orders",
+ Definition: "CREATE VIEW public.active_orders AS SELECT id, amount FROM public.orders WHERE status = 'open';",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "int"},
+ {Name: "amount", Type: "numeric"},
+ },
+ },
+ {
+ Name: "a_view",
+ Definition: "CREATE VIEW public.a_view AS SELECT id FROM public.z_view;",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "int"},
+ },
+ },
+ {
+ Name: "fallback_view",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "int"},
+ {Name: "amount", Type: "numeric"},
+ },
+ },
+ {
+ Name: "z_view",
+ Definition: "CREATE VIEW public.z_view AS SELECT id FROM public.orders;",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "int"},
+ },
+ },
+ {
+ Name: "aliased_orders",
+ Definition: "CREATE VIEW public.aliased_orders(order_id) AS SELECT id FROM public.orders;",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "order_id", Type: "int"},
+ },
+ },
+ },
+ MaterializedViews: []*storepb.MaterializedViewMetadata{
+ {
+ Name: "order_totals_mv",
+ Definition: "CREATE MATERIALIZED VIEW public.order_totals_mv AS SELECT customer_id, SUM(amount) AS total_amount FROM public.orders GROUP BY customer_id;",
+ },
+ {
+ Name: "aliased_orders_mv",
+ Definition: "CREATE MATERIALIZED VIEW public.aliased_orders_mv(order_id) AS SELECT id FROM public.orders;",
+ },
+ },
+ Functions: []*storepb.FunctionMetadata{
+ {
+ Name: "reporting_fn",
+ Definition: "CREATE FUNCTION public.reporting_fn() RETURNS int STABLE AS $$ SELECT id FROM public.orders $$ LANGUAGE SQL;",
+ Signature: "reporting_fn()",
+ },
+ {
+ Name: "stale_reporting_fn",
+ Definition: "CREATE FUNCTION public.stale_reporting_fn() RETURNS int STABLE AS $$ SELECT id FROM public.missing_orders $$ LANGUAGE SQL;",
+ Signature: "stale_reporting_fn()",
+ },
+ {
+ Name: "unsupported_reporting_fn",
+ Definition: "CREATE FUNCTION public.unsupported_reporting_fn() RETURNS int STABLE AS $$ BEGIN RETURN 1; END; $$ LANGUAGE plpgsql;",
+ Signature: "unsupported_reporting_fn()",
+ },
+ },
+ Sequences: []*storepb.SequenceMetadata{
+ {
+ Name: "order_id_seq",
+ },
+ },
+ },
+ {
+ Name: "analytics",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "events",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "event_id", Type: "int"},
+ {Name: "order_id", Type: "int"},
+ },
+ },
+ },
+ Functions: []*storepb.FunctionMetadata{
+ {
+ Name: "reporting_fn",
+ Definition: "CREATE FUNCTION analytics.reporting_fn() RETURNS int STABLE AS $$ SELECT event_id FROM analytics.events $$ LANGUAGE SQL;",
+ Signature: "reporting_fn()",
+ },
+ },
+ },
+ {
+ Name: "spectrum",
+ ExternalTables: []*storepb.ExternalTableMetadata{
+ {
+ Name: "spectrum_orders",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "external_id", Type: "varchar"},
+ },
+ },
+ },
+ },
+ },
+ }
+}
+
+func redshiftMockDatabaseMetadataGetter(databaseMetadata []*storepb.DatabaseSchemaMetadata) (base.GetDatabaseMetadataFunc, base.ListDatabaseNamesFunc) {
+ return func(_ context.Context, _, databaseName string) (string, *model.DatabaseMetadata, error) {
+ for _, metadata := range databaseMetadata {
+ if metadata.GetName() == databaseName {
+ return "", model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_REDSHIFT, true /* isObjectCaseSensitive */), nil
+ }
+ }
+ return "", nil, errors.Errorf("database %q not found", databaseName)
+ }, func(_ context.Context, _ string) ([]string, error) {
+ names := make([]string, 0, len(databaseMetadata))
+ for _, metadata := range databaseMetadata {
+ names = append(names, metadata.GetName())
+ }
+ return names, nil
+ }
+}
+
+func redshiftSourceColumnSetFromList(columns []base.ColumnResource) base.SourceColumnSet {
+ result := make(base.SourceColumnSet)
+ for _, column := range columns {
+ result[column] = true
+ }
+ return result
+}
diff --git a/backend/plugin/parser/redshift/query_type.go b/backend/plugin/parser/redshift/query_type.go
deleted file mode 100644
index dc6d31c85f..0000000000
--- a/backend/plugin/parser/redshift/query_type.go
+++ /dev/null
@@ -1,314 +0,0 @@
-package redshift
-
-import (
- "strings"
-
- "github.com/pkg/errors"
-
- parser "github.com/bytebase/parser/redshift"
-
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
-)
-
-type queryTypeListener struct {
- *parser.BaseRedshiftParserListener
- result base.QueryType
- allSystems bool
- isExplainAnalyze bool
- err error
-}
-
-func (q *queryTypeListener) EnterStmtblock(ctx *parser.StmtblockContext) {
- multi := ctx.Stmtmulti()
- if multi == nil || len(multi.AllStmt()) == 0 {
- q.result = base.QueryTypeUnknown
- return
- }
- if len(multi.AllStmt()) > 1 {
- q.err = errors.Errorf("only single statement is allowed, but got %d", len(multi.AllStmt()))
- return
- }
-
- stmt := multi.AllStmt()[0]
- q.result = q.getStatementType(stmt)
-}
-
-func (q *queryTypeListener) getStatementType(stmt parser.IStmtContext) base.QueryType {
- if stmt == nil {
- return base.QueryTypeUnknown
- }
-
- // DML statements
- if stmt.Insertstmt() != nil {
- return base.DML
- }
- if stmt.Updatestmt() != nil {
- return base.DML
- }
- if stmt.Deletestmt() != nil {
- return base.DML
- }
- if stmt.Mergestmt() != nil {
- return base.DML
- }
- if stmt.Copystmt() != nil {
- return base.DML
- }
-
- // Special case for SELECT with INTO clause (becomes DDL)
- if selectStmt := stmt.Selectstmt(); selectStmt != nil {
- // Check if it's a SELECT INTO statement
- if q.hasIntoClause(selectStmt) {
- return base.DDL
- }
- if q.allSystems {
- return base.SelectInfoSchema
- }
- return base.Select
- }
-
- // Variable SET/SHOW statements
- if stmt.Variablesetstmt() != nil {
- // Treat SET as select (safe operation)
- return base.Select
- }
- if stmt.Variableshowstmt() != nil {
- return base.SelectInfoSchema
- }
- // Note: SHOW is handled via Variableshowstmt
-
- // EXPLAIN statement
- if explainStmt := stmt.Explainstmt(); explainStmt != nil {
- // Check if it has ANALYZE option
- if q.hasAnalyzeOption(explainStmt) {
- q.isExplainAnalyze = true
- // For EXPLAIN ANALYZE, return the type of the explained statement
- if explainedStmt := explainStmt.Explainablestmt(); explainedStmt != nil {
- // Check what kind of statement is being explained
- if explainedStmt.Selectstmt() != nil {
- // Check for SELECT INTO which is DDL
- if q.hasIntoClause(explainedStmt.Selectstmt()) {
- return base.DDL
- }
- return base.Select
- }
- if explainedStmt.Insertstmt() != nil {
- return base.DML
- }
- if explainedStmt.Updatestmt() != nil {
- return base.DML
- }
- if explainedStmt.Deletestmt() != nil {
- return base.DML
- }
- if explainedStmt.Declarecursorstmt() != nil {
- return base.Select
- }
- if explainedStmt.Createasstmt() != nil {
- return base.DDL
- }
- if explainedStmt.Refreshmatviewstmt() != nil {
- return base.DML
- }
- if explainedStmt.Executestmt() != nil {
- // EXECUTE statements are harder to classify without context
- return base.Select
- }
- }
- // Default to Select if we can't determine the explained statement type
- return base.Select
- }
- return base.Explain
- }
-
- // DDL statements - Direct CREATE/DROP statements
- if stmt.Createstmt() != nil || stmt.Dropstmt() != nil {
- return base.DDL
- }
-
- // CREATE statements
- if stmt.Createasstmt() != nil || stmt.Createseqstmt() != nil ||
- stmt.Createschemastmt() != nil || stmt.Createdbstmt() != nil ||
- stmt.Createfunctionstmt() != nil || stmt.Createrolestmt() != nil ||
- stmt.Indexstmt() != nil || stmt.Createextensionstmt() != nil ||
- stmt.Createtrigstmt() != nil || stmt.Createeventtrigstmt() != nil ||
- stmt.Createdomainstmt() != nil || stmt.Createconversionstmt() != nil ||
- stmt.Createcaststmt() != nil || stmt.Createopclassstmt() != nil ||
- stmt.Createopfamilystmt() != nil || stmt.Createpolicystmt() != nil ||
- stmt.Createamstmt() != nil || stmt.Createtransformstmt() != nil ||
- stmt.Createstatsstmt() != nil || stmt.Createtablespacestmt() != nil ||
- stmt.Createfdwstmt() != nil || stmt.Createforeignserverstmt() != nil ||
- stmt.Createforeigntablestmt() != nil || stmt.Createplangstmt() != nil ||
- stmt.Createpublicationstmt() != nil || stmt.Createsubscriptionstmt() != nil ||
- stmt.Createusermappingstmt() != nil {
- return base.DDL
- }
-
- // VIEW statements (DDL)
- if stmt.Viewstmt() != nil {
- return base.DDL
- }
-
- // ALTER statements
- if stmt.Altertablestmt() != nil || stmt.Alterseqstmt() != nil ||
- stmt.Alterdatabasestmt() != nil ||
- stmt.Alterdatabasesetstmt() != nil ||
- stmt.Alterfunctionstmt() != nil || stmt.Alterrolestmt() != nil ||
- stmt.Alterrolesetstmt() != nil || stmt.Altercollationstmt() != nil ||
- stmt.Alterdomainstmt() != nil || stmt.Alterextensionstmt() != nil ||
- stmt.Alterextensioncontentsstmt() != nil || stmt.Alterfdwstmt() != nil ||
- stmt.Alterforeignserverstmt() != nil || stmt.Alteropfamilystmt() != nil ||
- stmt.Alterpolicystmt() != nil || stmt.Altereventtrigstmt() != nil ||
- stmt.Alterobjectdependsstmt() != nil || stmt.Alterobjectschemastmt() != nil ||
- stmt.Alterownerstmt() != nil || stmt.Alteroperatorstmt() != nil ||
- stmt.Altertypestmt() != nil || stmt.Alterenumstmt() != nil ||
- stmt.Alterstatsstmt() != nil || stmt.Altertblspcstmt() != nil ||
- stmt.Altersystemstmt() != nil ||
- stmt.Alterpublicationstmt() != nil || stmt.Altersubscriptionstmt() != nil ||
- stmt.Alterusermappingstmt() != nil || stmt.Altercompositetypestmt() != nil ||
- stmt.Alterdefaultprivilegesstmt() != nil || stmt.Altergroupstmt() != nil ||
- stmt.Altertsconfigurationstmt() != nil || stmt.Altertsdictionarystmt() != nil {
- return base.DDL
- }
-
- // DROP statements (in addition to generic Dropstmt)
- if stmt.Dropdbstmt() != nil || stmt.Droprolestmt() != nil ||
- stmt.Dropownedstmt() != nil || stmt.Dropsubscriptionstmt() != nil ||
- stmt.Dropusermappingstmt() != nil {
- return base.DDL
- }
-
- // Other DDL statements
- if stmt.Truncatestmt() != nil || stmt.Commentstmt() != nil ||
- stmt.Grantstmt() != nil || stmt.Revokestmt() != nil ||
- stmt.Clusterstmt() != nil || stmt.Vacuumstmt() != nil ||
- stmt.Analyzestmt() != nil || stmt.Lockstmt() != nil ||
- stmt.Reindexstmt() != nil || stmt.Rulestmt() != nil ||
- stmt.Renamestmt() != nil || stmt.Reassignownedstmt() != nil ||
- stmt.Loadstmt() != nil ||
- stmt.Importforeignschemastmt() != nil || stmt.Seclabelstmt() != nil ||
- stmt.Dostmt() != nil ||
- stmt.Creatematviewstmt() != nil || stmt.Discardstmt() != nil ||
- stmt.Fetchstmt() != nil || stmt.Constraintssetstmt() != nil ||
- stmt.Checkpointstmt() != nil || stmt.Createtablespacestmt() != nil {
- return base.DDL
- }
-
- // REFRESH MATERIALIZED VIEW is DML
- if stmt.Refreshmatviewstmt() != nil {
- return base.DML
- }
-
- // CALL stored procedure is DML
- if stmt.Callstmt() != nil {
- return base.DML
- }
-
- // Default to unknown for any unhandled statement types
- return base.QueryTypeUnknown
-}
-
-func (q *queryTypeListener) hasIntoClause(selectStmt parser.ISelectstmtContext) bool {
- // Check if SELECT has INTO clause by traversing the AST
- if selectStmt == nil {
- return false
- }
-
- // Check select_no_parens path
- if selectNoParens := selectStmt.Select_no_parens(); selectNoParens != nil {
- if selectClause := selectNoParens.Select_clause(); selectClause != nil {
- // Traverse through select_clause -> simple_select_intersect -> simple_select_pramary
- for _, intersect := range selectClause.AllSimple_select_intersect() {
- if intersect != nil {
- for _, primary := range intersect.AllSimple_select_pramary() {
- if primary != nil {
- // Check if this simple_select_pramary has INTO clause
- if len(primary.AllInto_clause()) > 0 {
- return true
- }
- }
- }
- }
- }
- }
- }
-
- // Check select_with_parens path
- if selectWithParens := selectStmt.Select_with_parens(); selectWithParens != nil {
- // Recursively check nested select statements
- if selectWithParens.Select_with_parens() != nil {
- // For nested parentheses, we need to check recursively
- // This is a simplified approach - a full implementation would need
- // to handle all nesting cases
- if selectWithParens.Select_no_parens() != nil {
- return q.hasIntoClauseInSelectNoParens(selectWithParens.Select_no_parens())
- }
- }
- }
-
- return false
-}
-
-func (*queryTypeListener) hasIntoClauseInSelectNoParens(selectNoParens parser.ISelect_no_parensContext) bool {
- if selectNoParens == nil {
- return false
- }
-
- if selectClause := selectNoParens.Select_clause(); selectClause != nil {
- // Traverse through select_clause -> simple_select_intersect -> simple_select_pramary
- for _, intersect := range selectClause.AllSimple_select_intersect() {
- if intersect != nil {
- for _, primary := range intersect.AllSimple_select_pramary() {
- if primary != nil {
- // Check if this simple_select_pramary has INTO clause
- if len(primary.AllInto_clause()) > 0 {
- return true
- }
- }
- }
- }
- }
- }
-
- return false
-}
-
-func (*queryTypeListener) hasAnalyzeOption(explainStmt parser.IExplainstmtContext) bool {
- if explainStmt == nil {
- return false
- }
-
- // Check if EXPLAIN has ANALYZE keyword via AST
- // Method 1: Check if Analyze_keyword() exists
- if explainStmt.Analyze_keyword() != nil {
- return true
- }
-
- // Method 2: Check in the explain option list
- if optionList := explainStmt.Explain_option_list(); optionList != nil {
- // Check each option element
- for _, optionElem := range optionList.AllExplain_option_elem() {
- if optionElem != nil {
- // Check if the option name is "analyze"
- if optionName := optionElem.Explain_option_name(); optionName != nil {
- // Check if it's the ANALYZE keyword
- if optionName.Analyze_keyword() != nil {
- return true
- }
- // Also check if it's specified as a nonreserved word
- // Note: We need to check the text here because ANALYZE can be specified
- // as a non-reserved word in PostgreSQL, and we need to verify it's actually
- // the ANALYZE option and not some other non-reserved word
- if optionName.Nonreservedword() != nil {
- optionText := optionName.Nonreservedword().GetText()
- if strings.EqualFold(optionText, "analyze") {
- return true
- }
- }
- }
- }
- }
- }
-
- return false
-}
diff --git a/backend/plugin/parser/redshift/redshift.go b/backend/plugin/parser/redshift/redshift.go
index 6bac0415a1..edc6488e28 100644
--- a/backend/plugin/parser/redshift/redshift.go
+++ b/backend/plugin/parser/redshift/redshift.go
@@ -1,15 +1,8 @@
package redshift
import (
- "strings"
-
- "github.com/antlr4-go/antlr/v4"
- parser "github.com/bytebase/parser/redshift"
- "github.com/pkg/errors"
-
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
- "github.com/bytebase/bytebase/backend/utils"
)
func init() {
@@ -53,210 +46,3 @@ func parseRedshiftStatements(statement string) ([]base.ParsedStatement, error) {
return result, nil
}
-
-// ParseRedshift parses the given SQL and returns a list of ANTLRAST (one per statement).
-// Use the Redshift parser based on antlr4.
-func ParseRedshift(sql string) ([]*base.ANTLRAST, error) {
- stmts, err := SplitSQL(sql)
- if err != nil {
- return nil, errors.Wrap(err, "failed to split SQL")
- }
-
- var results []*base.ANTLRAST
- for _, stmt := range stmts {
- if stmt.Empty {
- continue
- }
-
- parseResult, err := parseSingleRedshift(stmt.Text, stmt.BaseLine())
- if err != nil {
- return nil, err
- }
- results = append(results, parseResult)
- }
-
- return results, nil
-}
-
-// parseSingleRedshift parses a single Redshift statement and returns the ANTLRAST.
-func parseSingleRedshift(statement string, baseLine int) (*base.ANTLRAST, error) {
- statement = strings.TrimRightFunc(statement, utils.IsSpaceOrSemicolon) + ";"
- inputStream := antlr.NewInputStream(statement)
- lexer := parser.NewRedshiftLexer(inputStream)
- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
- p := parser.NewRedshiftParser(stream)
-
- // Remove default error listener and add our own error listener.
- startPosition := &storepb.Position{Line: int32(baseLine) + 1}
- lexer.RemoveErrorListeners()
- lexerErrorListener := &base.ParseErrorListener{
- Statement: statement,
- StartPosition: startPosition,
- }
- lexer.AddErrorListener(lexerErrorListener)
-
- parserErrorListener := &base.ParseErrorListener{
- Statement: statement,
- StartPosition: startPosition,
- }
- p.RemoveErrorListeners()
- p.AddErrorListener(parserErrorListener)
-
- p.BuildParseTrees = true
-
- tree := p.Root()
-
- // Return early if there are any lexer errors
- if lexerErrorListener.Err != nil {
- return nil, lexerErrorListener.Err
- }
-
- // Return early if there are any parser errors
- if parserErrorListener.Err != nil {
- return nil, parserErrorListener.Err
- }
-
- result := &base.ANTLRAST{
- StartPosition: &storepb.Position{Line: int32(baseLine) + 1},
- Tree: tree,
- Tokens: stream,
- }
-
- return result, nil
-}
-
-// ParseRedshiftPLBlock parses a PL/pgSQL block (BEGIN...END) and returns the ANTLRAST.
-func ParseRedshiftPLBlock(plBlock string) (*base.ANTLRAST, error) {
- lexer := parser.NewRedshiftLexer(antlr.NewInputStream(plBlock))
- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
- p := parser.NewRedshiftParser(stream)
- lexerErrorListener := &base.ParseErrorListener{
- Statement: plBlock,
- StartPosition: &storepb.Position{Line: 1},
- }
- lexer.RemoveErrorListeners()
- lexer.AddErrorListener(lexerErrorListener)
-
- parserErrorListener := &base.ParseErrorListener{
- Statement: plBlock,
- StartPosition: &storepb.Position{Line: 1},
- }
- p.RemoveErrorListeners()
- p.AddErrorListener(parserErrorListener)
-
- p.BuildParseTrees = true
-
- // Parse starting from pl_block rule instead of root
- tree := p.Pl_block()
- if lexerErrorListener.Err != nil {
- return nil, lexerErrorListener.Err
- }
- if parserErrorListener.Err != nil {
- return nil, parserErrorListener.Err
- }
-
- return &base.ANTLRAST{
- StartPosition: &storepb.Position{Line: 1},
- Tree: tree,
- Tokens: stream,
- }, nil
-}
-
-// normalizeRedshiftTypeFunctionName normalizes a type_function_name context.
-func normalizeRedshiftTypeFunctionName(ctx parser.IType_function_nameContext) string {
- if ctx == nil {
- return ""
- }
-
- text := ctx.GetText()
- if len(text) >= 2 && text[0] == '"' && text[len(text)-1] == '"' {
- return text[1 : len(text)-1]
- }
- return strings.ToLower(text)
-}
-
-// normalizeRedshiftAnyIdentifier normalizes an any_identifier context.
-func normalizeRedshiftAnyIdentifier(ctx parser.IAny_identifierContext) string {
- if ctx == nil {
- return ""
- }
-
- if ctx.Colid() != nil {
- return normalizeRedshiftColid(ctx.Colid())
- }
- if ctx.Plsql_unreserved_keyword() != nil {
- return strings.ToLower(ctx.Plsql_unreserved_keyword().GetText())
- }
- return ""
-}
-
-// NormalizeRedshiftAnyName normalizes an any_name context.
-func NormalizeRedshiftAnyName(ctx parser.IAny_nameContext) []string {
- if ctx == nil {
- return nil
- }
-
- var result []string
- result = append(result, normalizeRedshiftColid(ctx.Colid()))
- if ctx.Attrs() != nil {
- for _, item := range ctx.Attrs().AllAttr_name() {
- result = append(result, normalizeRedshiftAttrName(item))
- }
- }
-
- return result
-}
-
-// normalizeRedshiftIndirection normalizes an indirection context.
-func normalizeRedshiftIndirection(ctx parser.IIndirectionContext) []string {
- if ctx == nil {
- return []string{}
- }
-
- var res []string
- for _, child := range ctx.AllIndirection_el() {
- res = append(res, normalizeRedshiftIndirectionEl(child))
- }
- return res
-}
-
-// normalizeRedshiftIndirectionEl normalizes an indirection element.
-func normalizeRedshiftIndirectionEl(ctx parser.IIndirection_elContext) string {
- if ctx == nil {
- return ""
- }
-
- if ctx.DOT() != nil {
- if ctx.STAR() != nil {
- return "*"
- }
- return normalizeRedshiftAttrName(ctx.Attr_name())
- }
- return ctx.GetText()
-}
-
-// NormalizeRedshiftFuncName normalizes a func_name context and returns the name parts.
-func NormalizeRedshiftFuncName(ctx parser.IFunc_nameContext) []string {
- if ctx == nil {
- return nil
- }
-
- var result []string
- if ctx.Type_function_name() != nil {
- result = append(result, normalizeRedshiftTypeFunctionName(ctx.Type_function_name()))
- }
- if ctx.Colid() != nil {
- result = append(result, normalizeRedshiftColid(ctx.Colid()))
- }
- if ctx.Indirection() != nil {
- for _, el := range ctx.Indirection().AllIndirection_el() {
- result = append(result, normalizeRedshiftIndirectionEl(el))
- }
- }
-
- if len(result) == 0 && ctx.GetText() != "" {
- result = append(result, strings.ToLower(ctx.GetText()))
- }
-
- return result
-}
diff --git a/backend/plugin/parser/redshift/redshift_antlr_helpers.go b/backend/plugin/parser/redshift/redshift_antlr_helpers.go
deleted file mode 100644
index 618ac08d26..0000000000
--- a/backend/plugin/parser/redshift/redshift_antlr_helpers.go
+++ /dev/null
@@ -1,93 +0,0 @@
-package redshift
-
-import (
- "strings"
-
- parser "github.com/bytebase/parser/redshift"
-)
-
-// Normalization functions for legacy Redshift ANTLR query-span paths.
-func normalizeRedshiftName(ctx parser.INameContext) string {
- if ctx == nil {
- return ""
- }
- return normalizeRedshiftIdentifierText(ctx.GetText())
-}
-
-func normalizeRedshiftColid(ctx parser.IColidContext) string {
- if ctx == nil {
- return ""
- }
- return normalizeRedshiftIdentifierText(ctx.GetText())
-}
-
-func normalizeRedshiftIdentifier(ctx parser.IIdentifierContext) string {
- if ctx == nil {
- return ""
- }
- return normalizeRedshiftIdentifierText(ctx.GetText())
-}
-
-func normalizeRedshiftCollabel(ctx parser.ICollabelContext) string {
- if ctx == nil {
- return ""
- }
- return normalizeRedshiftIdentifierText(ctx.GetText())
-}
-
-func normalizeRedshiftAttrName(ctx parser.IAttr_nameContext) string {
- if ctx == nil {
- return ""
- }
- return normalizeRedshiftIdentifierText(ctx.GetText())
-}
-
-func normalizeRedshiftTableAlias(ctx parser.ITable_aliasContext) string {
- if ctx == nil {
- return ""
- }
- return normalizeRedshiftIdentifierText(ctx.GetText())
-}
-
-func normalizeRedshiftNameList(ctx parser.IName_listContext) []string {
- if ctx == nil {
- return nil
- }
- var result []string
- for _, name := range ctx.AllName() {
- result = append(result, normalizeRedshiftName(name))
- }
- return result
-}
-
-func normalizeRedshiftQuotedIdentifier(text string) string {
- if len(text) < 2 {
- return text
- }
- return strings.ReplaceAll(text[1:len(text)-1], `""`, `"`)
-}
-
-func normalizeRedshiftIdentifierText(text string) string {
- if len(text) >= 2 && text[0] == '"' && text[len(text)-1] == '"' {
- return normalizeRedshiftQuotedIdentifier(text)
- }
- return strings.ToLower(text)
-}
-
-func NormalizeRedshiftQualifiedName(ctx parser.IQualified_nameContext) []string {
- if ctx == nil {
- return nil
- }
- var result []string
- if ctx.Colid() != nil {
- result = append(result, normalizeRedshiftColid(ctx.Colid()))
- }
- if ctx.Indirection() != nil {
- for _, el := range ctx.Indirection().AllIndirection_el() {
- if el.Attr_name() != nil {
- result = append(result, normalizeRedshiftAttrName(el.Attr_name()))
- }
- }
- }
- return result
-}
diff --git a/backend/plugin/parser/redshift/system_resource.go b/backend/plugin/parser/redshift/system_resource.go
new file mode 100644
index 0000000000..5118083c22
--- /dev/null
+++ b/backend/plugin/parser/redshift/system_resource.go
@@ -0,0 +1,39 @@
+package redshift
+
+import "github.com/bytebase/bytebase/backend/plugin/parser/base"
+
+// isMixedQuery checks whether the query accesses the user table and system table at the same time.
+func isMixedQuery(m base.SourceColumnSet) (bool, bool) {
+ hasSystem, hasUser := false, false
+ for table := range m {
+ if isSystemResource(table) {
+ hasSystem = true
+ } else {
+ hasUser = true
+ }
+ }
+
+ if hasSystem && hasUser {
+ return false, true
+ }
+
+ return !hasUser && hasSystem, false
+}
+
+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 true
+ }
+ if resource.Schema == "" && isSystemView(resource.Table) {
+ return true
+ }
+ if resource.Schema == "" && isSystemTable(resource.Table) {
+ return true
+ }
+ return false
+}