Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(parser): migrate Redshift from PostgreSQL parser to dedicated parser (#18828)
h3n4la25aebe46ace
diff --git a/backend/api/v1/release_service_check.go b/backend/api/v1/release_service_check.goindex f1e052b558..ff0d14f591 100644--- a/backend/api/v1/release_service_check.go+++ b/backend/api/v1/release_service_check.go@@ -741,7 +741,7 @@ type statementTypeWithPosition struct {// Currently only PostgreSQL is supported.func getStatementTypesWithPositionsForEngine(engine storepb.Engine, asts []base.AST) ([]statementTypeWithPosition, error) {switch engine {- case storepb.Engine_POSTGRES, storepb.Engine_COCKROACHDB, storepb.Engine_REDSHIFT:+ case storepb.Engine_POSTGRES:pgStmts, err := pg.GetStatementTypes(asts)if err != nil {return nil, errdiff --git a/backend/component/export/resources.go b/backend/component/export/resources.goindex 29098db337..7e46291658 100644--- a/backend/component/export/resources.go+++ b/backend/component/export/resources.go@@ -30,8 +30,10 @@ func GetResources(switch engine {case storepb.Engine_MYSQL, storepb.Engine_MARIADB, storepb.Engine_OCEANBASE:return getResourcesForMySQL(ctx, storeInstance, engine, databaseName, statement, instance, getDatabaseMetadataFunc, listDatabaseNamesFunc, getLinkedDatabaseMetadataFunc)- case storepb.Engine_POSTGRES, storepb.Engine_REDSHIFT:+ case storepb.Engine_POSTGRES:return getResourcesForPostgres(ctx, storeInstance, engine, databaseName, statement, instance, "public", getDatabaseMetadataFunc, listDatabaseNamesFunc, getLinkedDatabaseMetadataFunc)+ case storepb.Engine_REDSHIFT:+ return getResourcesForRedshift(ctx, storeInstance, engine, databaseName, statement, instance, "public", getDatabaseMetadataFunc, listDatabaseNamesFunc, getLinkedDatabaseMetadataFunc)case storepb.Engine_ORACLE:return getResourcesForOracle(ctx, storeInstance, engine, databaseName, statement, instance, getDatabaseMetadataFunc, listDatabaseNamesFunc, getLinkedDatabaseMetadataFunc)case storepb.Engine_SNOWFLAKE:@@ -210,6 +212,72 @@ func getResourcesForPostgres(return result, nil}+func getResourcesForRedshift(+ ctx context.Context,+ storeInstance *store.Store,+ engine storepb.Engine,+ databaseName string,+ statement string,+ instance *store.InstanceMessage,+ defaultSchema string,+ getDatabaseMetadataFunc base.GetDatabaseMetadataFunc,+ listDatabaseNamesFunc base.ListDatabaseNamesFunc,+ getLinkedDatabaseMetadataFunc base.GetLinkedDatabaseMetadataFunc,+) ([]base.SchemaResource, error) {+ spans, err := base.GetQuerySpan(ctx, base.GetQuerySpanContext{+ InstanceID: instance.ResourceID,+ GetDatabaseMetadataFunc: getDatabaseMetadataFunc,+ ListDatabaseNamesFunc: listDatabaseNamesFunc,+ GetLinkedDatabaseMetadataFunc: getLinkedDatabaseMetadataFunc,+ }, engine, statement, databaseName, defaultSchema, !store.IsObjectCaseSensitive(instance))+ if err != nil {+ return nil, err+ }++ database, err := storeInstance.GetDatabase(ctx, &store.FindDatabaseMessage{+ InstanceID: &instance.ResourceID,+ DatabaseName: &databaseName,+ })+ if err != nil {+ return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to fetch database"))+ }+ if database == nil {+ return nil, nil+ }++ dbMetadata, err := storeInstance.GetDBSchema(ctx, &store.FindDBSchemaMessage{+ InstanceID: database.InstanceID,+ DatabaseName: database.DatabaseName,+ })+ if err != nil {+ return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to fetch database schema"))+ }++ var result []base.SchemaResource+ for _, span := range spans {+ for sourceColumn := range span.SourceColumns {+ sr := base.SchemaResource{+ Database: sourceColumn.Database,+ Schema: sourceColumn.Schema,+ Table: sourceColumn.Table,+ LinkedServer: sourceColumn.Server,+ }++ if sourceColumn.Database != dbMetadata.GetProto().Name {+ continue+ }++ if !resourceExists(dbMetadata, sr) {+ continue+ }++ result = append(result, sr)+ }+ }++ return result, nil+}+func getResourcesForOracle(ctx context.Context,storeInstance *store.Store,diff --git a/backend/plugin/db/redshift/redshift.go b/backend/plugin/db/redshift/redshift.goindex a5f96cfbbe..1ae95b65eb 100644--- a/backend/plugin/db/redshift/redshift.go+++ b/backend/plugin/db/redshift/redshift.go@@ -24,7 +24,7 @@ import ("github.com/bytebase/bytebase/backend/plugin/db""github.com/bytebase/bytebase/backend/plugin/db/util""github.com/bytebase/bytebase/backend/plugin/parser/base"- pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"+ redshiftparser "github.com/bytebase/bytebase/backend/plugin/parser/redshift")var (@@ -179,7 +179,7 @@ func (d *Driver) Execute(ctx context.Context, statement string, opts db.ExecuteOvar commands []base.Statementoneshot := trueif len(statement) <= common.MaxSheetCheckSize {- singleSQLs, err := pgparser.SplitSQL(statement)+ singleSQLs, err := redshiftparser.SplitSQL(statement)if err != nil {return 0, err}@@ -361,7 +361,7 @@ func (d *Driver) GetCurrentDatabaseOwner() (string, error) {// QueryConn queries a SQL statement in a given connection.func (d *Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement string, queryContext db.QueryContext) ([]*v1pb.QueryResult, error) {- singleSQLs, err := pgparser.SplitSQL(statement)+ singleSQLs, err := redshiftparser.SplitSQL(statement)if err != nil {return nil, err}diff --git a/backend/plugin/parser/pg/query_span.go b/backend/plugin/parser/pg/query_span.goindex f262751cc3..c0ca78af9a 100644--- a/backend/plugin/parser/pg/query_span.go+++ b/backend/plugin/parser/pg/query_span.go@@ -11,7 +11,6 @@ import (func init() {base.RegisterGetQuerySpan(storepb.Engine_POSTGRES, GetQuerySpan)- base.RegisterGetQuerySpan(storepb.Engine_REDSHIFT, GetQuerySpan)base.RegisterGetQuerySpan(storepb.Engine_COCKROACHDB, GetQuerySpan)}diff --git a/backend/plugin/parser/pg/query_span_extractor.go b/backend/plugin/parser/pg/query_span_extractor.goindex cba8fdd5c8..26749a0e3d 100644--- a/backend/plugin/parser/pg/query_span_extractor.go+++ b/backend/plugin/parser/pg/query_span_extractor.go@@ -2318,7 +2318,7 @@ func (q *querySpanExtractor) extractTableSourceFromRelationExpr(relationExpr posreturn tableSource, nil}-func (q *querySpanExtractor) extractTableSourceFromUDF2(funcExprWindowless postgresql.IFunc_expr_windowlessContext, funcAlias postgresql.IFunc_alias_clauseContext) (base.TableSource, error) {+func (q *querySpanExtractor) extractTableSourceFromUDF(funcExprWindowless postgresql.IFunc_expr_windowlessContext, funcAlias postgresql.IFunc_alias_clauseContext) (base.TableSource, error) {if funcExprWindowless == nil {return nil, errors.New("nil func_expr_windowless")}@@ -2364,7 +2364,7 @@ func (q *querySpanExtractor) extractTableSourceFromFuncTable(funcTable postgresq// If the schemaName is empty, we try to match the system function first.return q.extractTableSourceFromSystemFunctionNew(funcName, args, funcAlias)}- return q.extractTableSourceFromUDF2(funcTable.Func_expr_windowless(), funcAlias)+ return q.extractTableSourceFromUDF(funcTable.Func_expr_windowless(), funcAlias)}// The actual column count and names will be determined by the alias@@ -2397,7 +2397,7 @@ func (q *querySpanExtractor) extractTableSourceFromFuncTable(funcTable postgresqreturn nil, errors.Wrapf(err, "failed to extract table source from system function %s", funcName)}} else {- t, err = q.extractTableSourceFromUDF2(item.Func_expr_windowless(), nil)+ 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)}diff --git a/backend/plugin/parser/redshift/access_tables.go b/backend/plugin/parser/redshift/access_tables.gonew file mode 100644index 0000000000..03513a27c9--- /dev/null+++ b/backend/plugin/parser/redshift/access_tables.go@@ -0,0 +1,184 @@+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.gonew file mode 100644index 0000000000..0fd0c6d636--- /dev/null+++ b/backend/plugin/parser/redshift/query_span.go@@ -0,0 +1,37 @@+package redshift++import (+ "context"++ "github.com/pkg/errors"++ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/plugin/parser/base"+)++func init() {+ base.RegisterGetQuerySpan(storepb.Engine_REDSHIFT, GetQuerySpan)+}++// GetQuerySpan returns the query span for the given statement.+func GetQuerySpan(ctx context.Context, gCtx base.GetQuerySpanContext, statement, database, schema string, _ bool) (*base.QuerySpan, error) {+ if gCtx.GetDatabaseMetadataFunc == nil {+ return nil, errors.New("GetDatabaseMetadataFunc is not set in GetQuerySpanContext")+ }+ _, meta, err := gCtx.GetDatabaseMetadataFunc(ctx, gCtx.InstanceID, database)+ if err != nil {+ return nil, errors.Wrapf(err, "failed to get database metadata for instance %q and database %q", gCtx.InstanceID, database)+ }+ searchPath := meta.GetSearchPath()+ if schema != "" {+ searchPath = []string{schema}+ }+ extractor := newQuerySpanExtractor(database, searchPath, gCtx)++ // Use the new ANTLR-based implementation+ querySpan, err := extractor.getQuerySpan(ctx, statement)+ if err != nil {+ return nil, errors.Wrapf(err, "failed to get query span from statement: %s", statement)+ }+ return querySpan, nil+}diff --git a/backend/plugin/parser/redshift/query_span_extractor.go b/backend/plugin/parser/redshift/query_span_extractor.gonew file mode 100644index 0000000000..3f0e0ffd10--- /dev/null+++ b/backend/plugin/parser/redshift/query_span_extractor.go@@ -0,0 +1,3647 @@+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+ }++ // 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 = ""+ tableName = columnName+ schemaName = ""+ case 2:+ // schema.table.* case+ columnName = ""+ schemaName = columnName+ tableName = parts[0]+ 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_type.go b/backend/plugin/parser/redshift/query_type.gonew file mode 100644index 0000000000..e0b6491a78--- /dev/null+++ b/backend/plugin/parser/redshift/query_type.go@@ -0,0 +1,309 @@+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+ }++ // 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.goindex 842c97f8e5..2c69efc0ae 100644--- a/backend/plugin/parser/redshift/redshift.go+++ b/backend/plugin/parser/redshift/redshift.go@@ -134,3 +134,139 @@ func parseSingleRedshift(statement string, baseLine int) (*base.ANTLRAST, error)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/statement_ranges.go b/backend/plugin/parser/redshift/statement_ranges.gonew file mode 100644index 0000000000..354cfdaa0b--- /dev/null+++ b/backend/plugin/parser/redshift/statement_ranges.go@@ -0,0 +1,93 @@+package redshift++import (+ "context"+ "strings"+ "unicode"++ lsp "github.com/bytebase/lsp-protocol"++ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/plugin/parser/base"+)++func init() {+ base.RegisterStatementRangesFunc(storepb.Engine_REDSHIFT, GetStatementRanges)+}++func GetStatementRanges(_ context.Context, _ base.StatementRangeContext, statement string) ([]base.Range, error) {+ ranges := getSQLStatementRangesUTF16Position([]byte(statement))+ return ranges, nil+}++func getSQLStatementRangesUTF16Position(content []byte) []lsp.Range {+ s := strings.TrimRightFunc(string(content), unicode.IsSpace)+ // Assuming the content is UTF-8 encoded.+ statements := strings.Split(s, ";")++ var ranges []lsp.Range+ // 0-based UTF-16 encoded line and character.+ line, character := 0, 0++ for i, statement := range statements {+ // Trim left space to provide more accurate range.+ statement = strings.TrimLeftFunc(statement, func(r rune) bool {+ if !unicode.IsSpace(r) {+ return false+ }+ if r == '\n' {+ line+++ character = 0+ } else {+ // Check rune utf16 length by BMP.+ if r <= 0xFFFF {+ character+++ } else {+ character += 2+ }+ }+ return true+ })++ // If the statement is empty, skip it.+ if statement == "" {+ continue+ }++ begin := lsp.Position{Line: uint32(line), Character: uint32(character)}+ for _, r := range statement {+ if r == '\n' {+ line+++ character = 0+ } else {+ // Check rune utf16 length by BMP.+ if r <= 0xFFFF {+ character+++ } else {+ character += 2+ }+ }+ }++ endLine, endCharacter := line, character+ // End is exclusive, so we check the next byte.+ if i == len(statements)-1 {+ // End of the content.+ endLine+++ endCharacter = 0+ } else {+ // Next byte is ';', include it.+ character+++ endCharacter+++ if nextStatement := statements[i+1]; len(nextStatement) > 0 && nextStatement[0] == '\n' {+ endLine+++ endCharacter = 0+ } else {+ endCharacter+++ }+ }+ end := lsp.Position{Line: uint32(endLine), Character: uint32(endCharacter)}+ ranges = append(ranges, lsp.Range{Start: begin, End: end})+ }+ return ranges+}diff --git a/backend/plugin/parser/redshift/statement_type.go b/backend/plugin/parser/redshift/statement_type.goindex 463bfa747f..43cf64c1d6 100644--- a/backend/plugin/parser/redshift/statement_type.go+++ b/backend/plugin/parser/redshift/statement_type.go@@ -1,8 +1,6 @@package redshiftimport (- "strings"-"github.com/antlr4-go/antlr/v4"parser "github.com/bytebase/parser/redshift""github.com/pkg/errors"@@ -10,111 +8,359 @@ import ("github.com/bytebase/bytebase/backend/plugin/parser/base")-// GetStatementTypes returns the statement types from the given AST.-func GetStatementTypes(asts []base.AST) ([]string, error) {- listener := &statementTypeListener{- types: make(map[string]bool),+// StatementTypeWithPosition contains statement type and its position information.+type StatementTypeWithPosition struct {+ Type string+ // Line is the one-based line number where the statement ends.+ Line int+ Text string+}++// GetStatementTypesWithPosition returns statement types with position information.+// The line numbers are one-based.+func GetStatementTypesWithPosition(asts []base.AST) ([]StatementTypeWithPosition, error) {+ if len(asts) == 0 {+ return []StatementTypeWithPosition{}, nil}- for _, ast := range asts {- antlrAST, ok := base.GetANTLRAST(ast)+ var allResults []StatementTypeWithPosition+ for _, unifiedAST := range asts {+ antlrAST, ok := base.GetANTLRAST(unifiedAST)if !ok {return nil, errors.New("expected ANTLR AST for Redshift")}- antlr.ParseTreeWalkerDefault.Walk(listener, antlrAST.Tree)+ if antlrAST.Tree == nil {+ return nil, errors.New("ANTLR tree is nil")+ }++ collector := &statementTypeCollectorWithPosition{+ tokens: antlrAST.Tokens,+ baseLine: base.GetLineOffset(antlrAST.StartPosition),+ }++ antlr.ParseTreeWalkerDefault.Walk(collector, antlrAST.Tree)+ allResults = append(allResults, collector.results...)}- var sqlTypes []string- for sqlType := range listener.types {- sqlTypes = append(sqlTypes, sqlType)+ return allResults, nil+}++// GetStatementTypes returns only the statement types as strings.+// This is used for registration with base.RegisterGetStatementTypes.+func GetStatementTypes(asts []base.AST) ([]string, error) {+ results, err := GetStatementTypesWithPosition(asts)+ if err != nil {+ return nil, err}- return sqlTypes, nil+ types := make([]string, len(results))+ for i, r := range results {+ types[i] = r.Type+ }+ return types, nil}-type statementTypeListener struct {+// Statement type constants.+const (+ // CREATE statements.+ stmtTypeCreateTable = "CREATE_TABLE"+ stmtTypeCreateView = "CREATE_VIEW"+ stmtTypeCreateIndex = "CREATE_INDEX"+ stmtTypeCreateSequence = "CREATE_SEQUENCE"+ stmtTypeCreateSchema = "CREATE_SCHEMA"+ stmtTypeCreateFunction = "CREATE_FUNCTION"+ stmtTypeCreateDatabase = "CREATE_DATABASE"++ // DROP statements.+ stmtTypeDropTable = "DROP_TABLE"+ stmtTypeDropIndex = "DROP_INDEX"+ stmtTypeDropSchema = "DROP_SCHEMA"+ stmtTypeDropSequence = "DROP_SEQUENCE"+ stmtTypeDropDatabase = "DROP_DATABASE"+ stmtTypeDropFunction = "DROP_FUNCTION"++ // ALTER statements.+ stmtTypeAlterTable = "ALTER_TABLE"+ stmtTypeAlterView = "ALTER_VIEW"+ stmtTypeAlterSequence = "ALTER_SEQUENCE"++ // RENAME statements.+ stmtTypeRenameIndex = "RENAME_INDEX"+ stmtTypeRenameSchema = "RENAME_SCHEMA"+ stmtTypeRenameSequence = "RENAME_SEQUENCE"++ // DML statements.+ stmtTypeInsert = "INSERT"+ stmtTypeUpdate = "UPDATE"+ stmtTypeDelete = "DELETE"++ // Other statements.+ stmtTypeComment = "COMMENT"++ // Special value for filtering.+ stmtTypeUnknown = "UNKNOWN"+)++// statementTypeCollectorWithPosition collects statement types with positions.+type statementTypeCollectorWithPosition struct {*parser.BaseRedshiftParserListener- types map[string]bool+ tokens *antlr.CommonTokenStream+ baseLine int+ results []StatementTypeWithPosition}-func (l *statementTypeListener) EnterStmt(ctx *parser.StmtContext) {+// Helper function to add statement with position.+func (c *statementTypeCollectorWithPosition) addStatement(stmtType string, ctx antlr.ParserRuleContext) {+ if stmtType == "" || stmtType == stmtTypeUnknown {+ return+ }++ // Get line number from the stop token+ line := 0+ if ctx.GetStop() != nil {+ line = ctx.GetStop().GetLine() + c.baseLine+ }++ // Get statement text+ text := ""+ if c.tokens != nil && ctx.GetStart() != nil && ctx.GetStop() != nil {+ text = c.tokens.GetTextFromInterval(antlr.NewInterval(+ ctx.GetStart().GetTokenIndex(),+ ctx.GetStop().GetTokenIndex(),+ ))+ }++ c.results = append(c.results, StatementTypeWithPosition{+ Type: stmtType,+ Line: line,+ Text: text,+ })+}++// isTopLevel checks if the context is at top level.+func isTopLevel(ctx antlr.Tree) bool {if ctx == nil {+ return true+ }++ switch ctx := ctx.(type) {+ case *parser.RootContext, *parser.StmtblockContext:+ return true+ case *parser.StmtmultiContext, *parser.StmtContext:+ return isTopLevel(ctx.GetParent())+ default:+ return false+ }+}++// CREATE TABLE statements+func (c *statementTypeCollectorWithPosition) EnterCreatestmt(ctx *parser.CreatestmtContext) {+ if !isTopLevel(ctx.GetParent()) {return}+ c.addStatement(stmtTypeCreateTable, ctx)+}- // Determine statement type based on context- switch {- case ctx.Selectstmt() != nil:- l.types["SELECT"] = true- case ctx.Insertstmt() != nil:- l.types["INSERT"] = true- case ctx.Updatestmt() != nil:- l.types["UPDATE"] = true- case ctx.Deletestmt() != nil:- l.types["DELETE"] = true- case ctx.Createstmt() != nil:- l.types["CREATE_TABLE"] = true- case ctx.Dropstmt() != nil:- l.types["DROP"] = true- case ctx.Altertablestmt() != nil:- l.types["ALTER"] = true- case ctx.Indexstmt() != nil:- l.types["CREATE_INDEX"] = true- case ctx.Createdbstmt() != nil:- l.types["CREATE_DATABASE"] = true- case ctx.Dropdbstmt() != nil:- l.types["DROP_DATABASE"] = true- case ctx.Grantstmt() != nil:- l.types["GRANT"] = true- case ctx.Revokestmt() != nil:- l.types["REVOKE"] = true- case ctx.Analyzestmt() != nil:- l.types["ANALYZE"] = true- case ctx.Vacuumstmt() != nil:- l.types["VACUUM"] = true- case ctx.Copystmt() != nil:- l.types["COPY"] = true- case ctx.Explainstmt() != nil:- l.types["EXPLAIN"] = true- case ctx.Createexternalfunctionstmt() != nil || ctx.Createfunctionstmt() != nil:- l.types["CREATE_FUNCTION"] = true- case ctx.Createprocedurestmt() != nil:- l.types["CREATE_PROCEDURE"] = true- case ctx.Creatematviewstmt() != nil:- l.types["CREATE_VIEW"] = true- case ctx.Createexternalviewstmt() != nil:- l.types["CREATE_VIEW"] = true- case ctx.Createschemastmt() != nil:- l.types["CREATE_SCHEMA"] = true- case ctx.Createuserstmt() != nil || ctx.Createrolestmt() != nil:- l.types["CREATE_USER"] = true- case ctx.Alteruserstmt() != nil || ctx.Alterrolestmt() != nil:- l.types["ALTER_USER"] = true- case ctx.Dropuserstmt() != nil || ctx.Droprolestmt() != nil:- l.types["DROP_USER"] = true- case ctx.Truncatestmt() != nil:- l.types["TRUNCATE"] = true- case ctx.Commentstmt() != nil:- l.types["COMMENT"] = true- case ctx.Variablesetstmt() != nil:- l.types["SET"] = true- case ctx.Variableshowstmt() != nil:- l.types["SHOW"] = true- case ctx.Callstmt() != nil:- l.types["CALL"] = true- case ctx.Transactionstmt() != nil:- // Check transaction type- if tStmt := ctx.Transactionstmt(); tStmt != nil {- text := strings.ToUpper(tStmt.GetText())- if strings.Contains(text, "BEGIN") || strings.Contains(text, "START") {- l.types["BEGIN"] = true- } else if strings.Contains(text, "COMMIT") {- l.types["COMMIT"] = true- } else if strings.Contains(text, "ROLLBACK") {- l.types["ROLLBACK"] = true- }+// CREATE VIEW statements+func (c *statementTypeCollectorWithPosition) EnterViewstmt(ctx *parser.ViewstmtContext) {+ if !isTopLevel(ctx.GetParent()) {+ return+ }+ c.addStatement(stmtTypeCreateView, ctx)+}++// CREATE MATERIALIZED VIEW statements+func (c *statementTypeCollectorWithPosition) EnterCreatematviewstmt(ctx *parser.CreatematviewstmtContext) {+ if !isTopLevel(ctx.GetParent()) {+ return+ }+ c.addStatement(stmtTypeCreateView, ctx)+}++// CREATE INDEX statements+func (c *statementTypeCollectorWithPosition) EnterIndexstmt(ctx *parser.IndexstmtContext) {+ if !isTopLevel(ctx.GetParent()) {+ return+ }+ c.addStatement(stmtTypeCreateIndex, ctx)+}++// CREATE SEQUENCE statements+func (c *statementTypeCollectorWithPosition) EnterCreateseqstmt(ctx *parser.CreateseqstmtContext) {+ if !isTopLevel(ctx.GetParent()) {+ return+ }+ c.addStatement(stmtTypeCreateSequence, ctx)+}++// CREATE SCHEMA statements+func (c *statementTypeCollectorWithPosition) EnterCreateschemastmt(ctx *parser.CreateschemastmtContext) {+ if !isTopLevel(ctx.GetParent()) {+ return+ }+ c.addStatement(stmtTypeCreateSchema, ctx)+}++// CREATE FUNCTION statements+func (c *statementTypeCollectorWithPosition) EnterCreatefunctionstmt(ctx *parser.CreatefunctionstmtContext) {+ if !isTopLevel(ctx.GetParent()) {+ return+ }+ c.addStatement(stmtTypeCreateFunction, ctx)+}++// CREATE DATABASE statements+func (c *statementTypeCollectorWithPosition) EnterCreatedbstmt(ctx *parser.CreatedbstmtContext) {+ if !isTopLevel(ctx.GetParent()) {+ return+ }+ c.addStatement(stmtTypeCreateDatabase, ctx)+}++// DROP statements+func (c *statementTypeCollectorWithPosition) EnterDropstmt(ctx *parser.DropstmtContext) {+ if !isTopLevel(ctx.GetParent()) {+ return+ }+ c.addStatement(getDropStatementType(ctx), ctx)+}++// ALTER statements+func (c *statementTypeCollectorWithPosition) EnterAltertablestmt(ctx *parser.AltertablestmtContext) {+ if !isTopLevel(ctx.GetParent()) {+ return+ }+ c.addStatement(stmtTypeAlterTable, ctx)+}++// ALTER MATERIALIZED VIEW statements+func (c *statementTypeCollectorWithPosition) EnterAltermaterializedviewstmt(ctx *parser.AltermaterializedviewstmtContext) {+ if !isTopLevel(ctx.GetParent()) {+ return+ }+ c.addStatement(stmtTypeAlterView, ctx)+}++// ALTER EXTERNAL VIEW statements+func (c *statementTypeCollectorWithPosition) EnterAlterexternalviewstmt(ctx *parser.AlterexternalviewstmtContext) {+ if !isTopLevel(ctx.GetParent()) {+ return+ }+ c.addStatement(stmtTypeAlterView, ctx)+}++func (c *statementTypeCollectorWithPosition) EnterAlterseqstmt(ctx *parser.AlterseqstmtContext) {+ if !isTopLevel(ctx.GetParent()) {+ return+ }+ c.addStatement(stmtTypeAlterSequence, ctx)+}++// RENAME statements+func (c *statementTypeCollectorWithPosition) EnterRenamestmt(ctx *parser.RenamestmtContext) {+ if !isTopLevel(ctx.GetParent()) {+ return+ }++ // Check for top-level RENAME operations+ if ctx.INDEX() != nil {+ c.addStatement(stmtTypeRenameIndex, ctx)+ return+ }+ if ctx.SCHEMA() != nil {+ c.addStatement(stmtTypeRenameSchema, ctx)+ return+ }+ if ctx.SEQUENCE() != nil {+ c.addStatement(stmtTypeRenameSequence, ctx)+ return+ }++ // All other RENAME operations+ if ctx.VIEW() != nil {+ c.addStatement(stmtTypeAlterView, ctx)+ } else if ctx.TABLE() != nil {+ c.addStatement(stmtTypeAlterTable, ctx)+ }+}++// COMMENT statements+func (c *statementTypeCollectorWithPosition) EnterCommentstmt(ctx *parser.CommentstmtContext) {+ if !isTopLevel(ctx.GetParent()) {+ return+ }+ c.addStatement(stmtTypeComment, ctx)+}++// DROP FUNCTION statements+func (c *statementTypeCollectorWithPosition) EnterRemovefuncstmt(ctx *parser.RemovefuncstmtContext) {+ if !isTopLevel(ctx.GetParent()) {+ return+ }+ c.addStatement(stmtTypeDropFunction, ctx)+}++// DROP DATABASE statements+func (c *statementTypeCollectorWithPosition) EnterDropdbstmt(ctx *parser.DropdbstmtContext) {+ if !isTopLevel(ctx.GetParent()) {+ return+ }+ c.addStatement(stmtTypeDropDatabase, ctx)+}++// DROP SCHEMA statements+func (c *statementTypeCollectorWithPosition) EnterDropschemastmt(ctx *parser.DropschemastmtContext) {+ if !isTopLevel(ctx.GetParent()) {+ return+ }+ c.addStatement(stmtTypeDropSchema, ctx)+}++// DML statements+func (c *statementTypeCollectorWithPosition) EnterInsertstmt(ctx *parser.InsertstmtContext) {+ if !isTopLevel(ctx.GetParent()) {+ return+ }+ c.addStatement(stmtTypeInsert, ctx)+}++func (c *statementTypeCollectorWithPosition) EnterUpdatestmt(ctx *parser.UpdatestmtContext) {+ if !isTopLevel(ctx.GetParent()) {+ return+ }+ c.addStatement(stmtTypeUpdate, ctx)+}++func (c *statementTypeCollectorWithPosition) EnterDeletestmt(ctx *parser.DeletestmtContext) {+ if !isTopLevel(ctx.GetParent()) {+ return+ }+ c.addStatement(stmtTypeDelete, ctx)+}++// getDropStatementType determines the specific DROP statement type.+func getDropStatementType(ctx *parser.DropstmtContext) string {+ if ctx == nil {+ return ""+ }++ // Check object_type_any_name (TABLE, SEQUENCE, VIEW, INDEX, etc.)+ if ctx.Object_type_any_name() != nil {+ objType := ctx.Object_type_any_name()+ if objType.TABLE() != nil {+ return stmtTypeDropTable}- default:- // For any unrecognized statement type- l.types["UNKNOWN"] = true+ if objType.VIEW() != nil {+ return stmtTypeDropTable+ }+ if objType.INDEX() != nil {+ return stmtTypeDropIndex+ }+ if objType.SEQUENCE() != nil {+ return stmtTypeDropSequence+ }+ return stmtTypeUnknown}++ // Note: DROP SCHEMA is handled by DropschemastmtContext, not DropstmtContext+ return stmtTypeUnknown}diff --git a/backend/plugin/parser/redshift/system_objects.go b/backend/plugin/parser/redshift/system_objects.gonew file mode 100644index 0000000000..595f847b9b--- /dev/null+++ b/backend/plugin/parser/redshift/system_objects.go@@ -0,0 +1,193 @@+package redshift++import "strings"++var (+ // systemSchemas is the list of system schemas that we will exclude.+ systemSchemas = map[string]bool{+ "information_schema": true,+ "pg_catalog": true,+ "pg_internal": true,+ "pg_toast": true,+ }++ // systemViews is the list of system views.+ systemViews = map[string]bool{+ "pg_available_extensions": true,+ "pg_available_extension_versions": true,+ "pg_backend_memory_contexts": true,+ "pg_config": true,+ "pg_cursors": true,+ "pg_file_settings": true,+ "pg_group": true,+ "pg_hba_file_rules": true,+ "pg_ident_file_mappings": true,+ "pg_indexes": true,+ "pg_locks": true,+ "pg_matviews": true,+ "pg_policies": true,+ "pg_prepared_statements": true,+ "pg_prepared_xacts": true,+ "pg_publication_tables": true,+ "pg_replication_origin_status": true,+ "pg_replication_slots": true,+ "pg_roles": true,+ "pg_rules": true,+ "pg_seclabels": true,+ "pg_sequences": true,+ "pg_settings": true,+ "pg_shadow": true,+ "pg_shmem_allocations": true,+ "pg_stats": true,+ "pg_stats_ext": true,+ "pg_stats_ext_exprs": true,+ "pg_tables": true,+ "pg_timezone_abbrevs": true,+ "pg_timezone_names": true,+ "pg_user": true,+ "pg_user_mappings": true,+ "pg_views": true,+ "pg_stat_statements": true,+ // Redshift-specific system views+ "stl_query": true,+ "stl_querytext": true,+ "stl_wlm_query": true,+ "stl_ddltext": true,+ "stl_utilitytext": true,+ "stl_load_commits": true,+ "stl_load_errors": true,+ "svv_tables": true,+ "svv_table_info": true,+ "svv_columns": true,+ "svv_external_tables": true,+ "svv_external_columns": true,+ "svv_external_schemas": true,+ "svv_redshift_tables": true,+ "svv_redshift_columns": true,+ "svv_all_tables": true,+ "svv_all_columns": true,+ "svl_statementtext": true,+ "svl_user_info": true,+ "svl_stored_proc_call": true,+ "stv_recents": true,+ "stv_inflight": true,+ "stv_sessions": true,+ "stv_wlm_query_state": true,+ "stv_wlm_service_class_state": true,+ }++ // systemTables is the list of system tables.+ systemTables = map[string]bool{+ "pg_aggregate": true,+ "pg_am": true,+ "pg_amop": true,+ "pg_amproc": true,+ "pg_attrdef": true,+ "pg_attribute": true,+ "pg_authid": true,+ "pg_auth_members": true,+ "pg_cast": true,+ "pg_class": true,+ "pg_collation": true,+ "pg_constraint": true,+ "pg_conversion": true,+ "pg_database": true,+ "pg_db_role_setting": true,+ "pg_default_acl": true,+ "pg_depend": true,+ "pg_description": true,+ "pg_enum": true,+ "pg_event_trigger": true,+ "pg_extension": true,+ "pg_foreign_data_wrapper": true,+ "pg_foreign_server": true,+ "pg_foreign_table": true,+ "pg_index": true,+ "pg_inherits": true,+ "pg_init_privs": true,+ "pg_language": true,+ "pg_largeobject": true,+ "pg_largeobject_metadata": true,+ "pg_namespace": true,+ "pg_opclass": true,+ "pg_operator": true,+ "pg_opfamily": true,+ "pg_parameter_acl": true,+ "pg_partitioned_table": true,+ "pg_policy": true,+ "pg_proc": true,+ "pg_publication": true,+ "pg_publication_namespace": true,+ "pg_publication_rel": true,+ "pg_range": true,+ "pg_replication_origin": true,+ "pg_rewrite": true,+ "pg_seclabel": true,+ "pg_sequence": true,+ "pg_shdepend": true,+ "pg_shdescription": true,+ "pg_shseclabel": true,+ "pg_statistic": true,+ "pg_statistic_ext": true,+ "pg_statistic_ext_data": true,+ "pg_subscription": true,+ "pg_subscription_rel": true,+ "pg_tablespace": true,+ "pg_transform": true,+ "pg_trigger": true,+ "pg_ts_config": true,+ "pg_ts_config_map": true,+ "pg_ts_dict": true,+ "pg_ts_parser": true,+ "pg_ts_template": true,+ "pg_type": true,+ "pg_user_mapping": true,+ }+)++func isSystemSchema(schema string) bool {+ _, ok := systemSchemas[schema]+ if ok {+ return true+ }+ if strings.HasPrefix(schema, "pg_temp") {+ return true+ }+ if strings.HasPrefix(schema, "pg_toast") {+ return true+ }+ return false+}++func isSystemTable(table string) bool {+ _, ok := systemTables[table]+ return ok+}++func isSystemView(view string) bool {+ _, ok := systemViews[view]+ return ok+}++// IsSystemFunction returns true if the function is a system/built-in function.+// Note: This is a stub for Redshift - a complete list would need to be added.+func IsSystemFunction(function string, _ string) bool {+ // Common built-in functions+ builtinFunctions := map[string]bool{+ "count": true, "sum": true, "avg": true, "min": true, "max": true,+ "coalesce": true, "nullif": true, "greatest": true, "least": true,+ "now": true, "current_date": true, "current_timestamp": true,+ "upper": true, "lower": true, "length": true, "trim": true,+ "substring": true, "replace": true, "concat": true,+ "abs": true, "round": true, "ceil": true, "floor": true,+ "cast": true, "convert": true,+ "json_extract_path_text": true, "json_extract_array_element_text": true,+ "getdate": true, "dateadd": true, "datediff": true, "date_trunc": true,+ "to_char": true, "to_date": true, "to_timestamp": true,+ "row_number": true, "rank": true, "dense_rank": true, "ntile": true,+ "lead": true, "lag": true, "first_value": true, "last_value": true,+ "listagg": true, "median": true, "percentile_cont": true, "percentile_disc": true,+ }+ _, ok := builtinFunctions[strings.ToLower(function)]+ return ok+}