Skip to content

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

refactor(pg): migrate PL/pgSQL query span to omni parser (#19716)

h3n4l80aede34e4d0
diff --git a/backend/plugin/parser/base/span.go b/backend/plugin/parser/base/span.go
index f5cb10790c..bc223c8dfb 100644
--- a/backend/plugin/parser/base/span.go
+++ b/backend/plugin/parser/base/span.go
@@ -432,7 +432,7 @@ type YamlQuerySpan struct {
Results []YamlQuerySpanResult
SourceColumns []ColumnResource
PredicateColumns []ColumnResource
- PredicatePaths []string
+ PredicatePaths []string `yaml:"predicatepaths,omitempty"`
}
type YamlQuerySpanResult struct {
diff --git a/backend/plugin/parser/pg/access_tables_antlr.go b/backend/plugin/parser/pg/access_tables_antlr.go
deleted file mode 100644
index 2cbeb8395d..0000000000
--- a/backend/plugin/parser/pg/access_tables_antlr.go
+++ /dev/null
@@ -1,95 +0,0 @@
-package pg
-
-import (
- "context"
-
- "github.com/bytebase/parser/postgresql"
- "github.com/pkg/errors"
-
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
-)
-
-// antlrAccessTableExtractor is the ANTLR-based access table extractor.
-// Used by query_span_extractor.go which still relies on ANTLR parse trees.
-type antlrAccessTableExtractor struct {
- *postgresql.BasePostgreSQLParserListener
- err error
- defaultDatabase string
- searchPath []string
- accessTables []base.ColumnResource
- getDatabaseMetadata base.GetDatabaseMetadataFunc
- ctx context.Context
- instanceID string
- skipMetadataValidation bool
-}
-
-func (a *antlrAccessTableExtractor) EnterQualified_name(qn *postgresql.Qualified_nameContext) {
- if a.err != nil {
- return
- }
- resource := base.ColumnResource{
- Database: a.defaultDatabase,
- }
- var directions []string
- qnLength := 1
- directions = append(directions, NormalizePostgreSQLColid(qn.Colid()))
- if qn.Indirection() != nil {
- allIndirectionElements := qn.Indirection().AllIndirection_el()
- for _, el := range allIndirectionElements {
- if el.Attr_name() != nil {
- directions = append(directions, normalizePostgreSQLCollabel(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 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
- }
- if databaseMetadata == nil {
- return
- }
- schemaName, name := databaseMetadata.SearchObject(searchPath, resource.Table)
- if schemaName == "" && name == "" {
- return
- }
- resource.Schema = schemaName
- }
-
- a.accessTables = append(a.accessTables, resource)
-}
diff --git a/backend/plugin/parser/pg/extract_params_test.go b/backend/plugin/parser/pg/extract_params_test.go
index 370336a886..c04bf190cc 100644
--- a/backend/plugin/parser/pg/extract_params_test.go
+++ b/backend/plugin/parser/pg/extract_params_test.go
@@ -3,7 +3,7 @@ package pg
import (
"testing"
- "github.com/bytebase/parser/postgresql"
+ "github.com/bytebase/omni/pg/ast"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -73,24 +73,28 @@ $$ LANGUAGE SQL;`,
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- parseResults, err := ParsePostgreSQL(tt.definition)
- require.NoError(t, err, "failed to parse function definition")
- require.Len(t, parseResults, 1, "expected exactly 1 statement")
-
- root, ok := parseResults[0].Tree.(*postgresql.RootContext)
- require.True(t, ok, "expected RootContext")
- stmtblock := root.Stmtblock()
- stmtmulti := stmtblock.Stmtmulti()
- stmts := stmtmulti.AllStmt()
+ // Parse with omni.
+ stmts, err := ParsePg(tt.definition)
+ require.NoError(t, err)
require.Len(t, stmts, 1)
- createFuncStmt := stmts[0].Createfunctionstmt()
- require.NotNil(t, createFuncStmt)
+ createFuncStmt, ok := stmts[0].AST.(*ast.CreateFunctionStmt)
+ require.True(t, ok, "expected CreateFunctionStmt")
- q := &querySpanExtractor{}
- got := q.extractParameterNamesFromCreateFunction(createFuncStmt)
+ // Extract OUT/TABLE parameter names from omni AST.
+ var got []string
+ if createFuncStmt.Parameters != nil {
+ for _, item := range createFuncStmt.Parameters.Items {
+ fp, ok := item.(*ast.FunctionParameter)
+ if !ok {
+ continue
+ }
+ if fp.Mode == ast.FUNC_PARAM_OUT || fp.Mode == ast.FUNC_PARAM_TABLE || fp.Mode == ast.FUNC_PARAM_INOUT {
+ got = append(got, fp.Name)
+ }
+ }
+ }
- require.NoError(t, err)
assert.Equal(t, tt.want, got, "parameter names mismatch")
})
}
diff --git a/backend/plugin/parser/pg/extract_return_query_test.go b/backend/plugin/parser/pg/extract_return_query_test.go
deleted file mode 100644
index 97c2aba5fd..0000000000
--- a/backend/plugin/parser/pg/extract_return_query_test.go
+++ /dev/null
@@ -1,105 +0,0 @@
-package pg
-
-import (
- "testing"
-
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
-)
-
-func TestExtractReturnQueryStatements(t *testing.T) {
- tests := []struct {
- name string
- asBody string
- want []string
- wantErr bool
- }{
- {
- name: "single RETURN QUERY",
- asBody: `BEGIN
- RETURN QUERY SELECT id, name FROM users WHERE active = true;
-END`,
- want: []string{
- "SELECT id, name FROM users WHERE active = true",
- },
- },
- {
- name: "multiple RETURN QUERY",
- asBody: `BEGIN
- RETURN QUERY SELECT id, name FROM users WHERE active = true;
- RETURN QUERY SELECT id, name FROM users WHERE admin = true;
-END`,
- want: []string{
- "SELECT id, name FROM users WHERE active = true",
- "SELECT id, name FROM users WHERE admin = true",
- },
- },
- {
- name: "RETURN QUERY with complex SELECT",
- asBody: `BEGIN
- RETURN QUERY
- SELECT u.id, u.name, p.phone
- FROM users u
- JOIN profiles p ON u.id = p.user_id
- WHERE u.active = true;
-END`,
- want: []string{
- "SELECT u.id, u.name, p.phone\n FROM users u\n JOIN profiles p ON u.id = p.user_id\n WHERE u.active = true",
- },
- },
- {
- name: "RETURN QUERY in IF block",
- asBody: `BEGIN
- IF some_condition THEN
- RETURN QUERY SELECT id, name FROM users WHERE active = true;
- ELSE
- RETURN QUERY SELECT id, name FROM users WHERE admin = true;
- END IF;
-END`,
- want: []string{
- "SELECT id, name FROM users WHERE active = true",
- "SELECT id, name FROM users WHERE admin = true",
- },
- },
- {
- name: "RETURN without QUERY (should be ignored)",
- asBody: `BEGIN
- RETURN 1;
-END`,
- want: []string{},
- },
- {
- name: "RETURN NEXT (should be ignored)",
- asBody: `BEGIN
- FOR r IN SELECT id, name FROM users LOOP
- RETURN NEXT r;
- END LOOP;
-END`,
- want: []string{},
- },
- {
- name: "mixed RETURN QUERY and RETURN NEXT",
- asBody: `BEGIN
- RETURN QUERY SELECT id, name FROM users WHERE active = true;
- FOR r IN SELECT id, name FROM admins LOOP
- RETURN NEXT r;
- END LOOP;
-END`,
- want: []string{
- "SELECT id, name FROM users WHERE active = true",
- },
- },
- }
-
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- got, err := extractReturnQueryStatements(tt.asBody)
- if tt.wantErr {
- assert.Error(t, err)
- return
- }
- require.NoError(t, err)
- assert.Equal(t, tt.want, got, "extracted SQL mismatch")
- })
- }
-}
diff --git a/backend/plugin/parser/pg/pg.go b/backend/plugin/parser/pg/pg.go
index 84af08edaf..853ed33e61 100644
--- a/backend/plugin/parser/pg/pg.go
+++ b/backend/plugin/parser/pg/pg.go
@@ -170,47 +170,6 @@ func parseSinglePostgreSQL(sql string, baseLine int) (*base.ANTLRAST, error) {
return result, nil
}
-// ParsePostgreSQLPLBlock parses the given PL/pgSQL block (BEGIN...END) and returns the ANTLRAST.
-// Use the PostgreSQL parser based on antlr4, starting from pl_block rule.
-func ParsePostgreSQLPLBlock(plBlock string) (*base.ANTLRAST, error) {
- lexer := parser.NewPostgreSQLLexer(antlr.NewInputStream(plBlock))
- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
- p := parser.NewPostgreSQLParser(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
- }
-
- result := &base.ANTLRAST{
- StartPosition: &storepb.Position{Line: 1},
- Tree: tree,
- Tokens: stream,
- }
-
- return result, nil
-}
-
func normalizePostgreSQLTableAlias(ctx parser.ITable_aliasContext) string {
if ctx == nil {
return ""
@@ -349,21 +308,6 @@ func NormalizePostgreSQLColid(ctx parser.IColidContext) string {
return strings.ToLower(ctx.GetText())
}
-func normalizePostgreSQLAnyIdentifier(ctx parser.IAny_identifierContext) string {
- if ctx == nil {
- return ""
- }
-
- switch {
- case ctx.Colid() != nil:
- return NormalizePostgreSQLColid(ctx.Colid())
- case ctx.Plsql_unreserved_keyword() != nil:
- return strings.ToLower(ctx.Plsql_unreserved_keyword().GetText())
- default:
- return ""
- }
-}
-
func normalizePostgreSQLIdentifier(ctx parser.IIdentifierContext) string {
if ctx == nil {
return ""
diff --git a/backend/plugin/parser/pg/query_span_extractor.go b/backend/plugin/parser/pg/query_span_extractor.go
deleted file mode 100644
index 248bafcd80..0000000000
--- a/backend/plugin/parser/pg/query_span_extractor.go
+++ /dev/null
@@ -1,3868 +0,0 @@
-package pg
-
-import (
- "context"
- "fmt"
- "slices"
- "strings"
-
- "github.com/antlr4-go/antlr/v4"
- "github.com/pkg/errors"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
-
- "github.com/bytebase/parser/postgresql"
-
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
- "github.com/bytebase/bytebase/backend/store/model"
-)
-
-const (
- pgUnknownFieldName = "?column?"
- generateSeries = "generate_series"
- generateSubscripts = "generate_subscripts"
- unnest = "unnest"
- jsonbEach = "jsonb_each"
- jsonEach = "json_each"
- jsonbEachText = "jsonb_each_text"
- jsonEachText = "json_each_text"
- jsonbArrayElements = "jsonb_array_elements"
- jsonArrayElements = "json_array_elements"
- jsonbArrayElementsText = "jsonb_array_elements_text"
- jsonArrayElementsText = "json_array_elements_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 {
- *postgresql.BasePostgreSQLParserListener
- 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 := ParsePostgreSQL(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
-
- antlrExtractor := &antlrAccessTableExtractor{
- defaultDatabase: q.defaultDatabase,
- searchPath: q.searchPath,
- getDatabaseMetadata: q.gCtx.GetDatabaseMetadataFunc,
- ctx: ctx,
- instanceID: q.gCtx.InstanceID,
- }
- antlr.ParseTreeWalkerDefault.Walk(antlrExtractor, parseResult.Tree)
- if antlrExtractor.err != nil {
- return nil, errors.Wrapf(antlrExtractor.err, "failed to extract query span from statement: %s", stmt)
- }
-
- // Build access map
- accessesMap := make(base.SourceColumnSet)
- for _, resource := range antlrExtractor.accessTables {
- accessesMap[resource] = true
- }
-
- // Check for mixed system/user tables
- allSystems, mixed := isMixedQuery(accessesMap)
- if mixed {
- return nil, base.MixUserSystemTablesError
- }
-
- // Get query type using omni AST.
- // Fall back to Select if omni cannot parse the statement (e.g. non-standard
- // syntax like 4-part column references that ANTLR accepts but libpg_query rejects).
- queryType, isExplainAnalyze := base.Select, false
- if omniStmts, parseErr := ParsePg(stmt); parseErr == nil && len(omniStmts) == 1 {
- queryType, isExplainAnalyze = classifyQueryType(omniStmts[0].AST, allSystems)
- }
-
- // 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.(*postgresql.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 := ParsePostgreSQL(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.(*postgresql.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")
- }
-
- funcArgsWithDefaults := createFuncStmt.Func_args_with_defaults()
- var params []postgresql.IFunc_arg_with_defaultContext
- if l := funcArgsWithDefaults.Func_args_with_defaults_list(); l != nil {
- params = append(params, l.AllFunc_arg_with_default()...)
- }
-
- var nDefaultPram, nVariadicParam int
- for _, param := range params {
- if param.A_expr() != nil {
- nDefaultPram++
- }
-
- if c := param.Func_arg().Arg_class(); c != nil {
- if c.VARIADIC() != nil {
- nVariadicParam++
- }
- }
- }
-
- return &functionDefinitionDetail{
- nDefaultParam: nDefaultPram,
- nVariadicParam: nVariadicParam,
- nParam: len(params),
- 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 := ParsePostgreSQL(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.(*postgresql.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 postgresql.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 postgresql.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 := ParsePostgreSQLPLBlock(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 {
- *postgresql.BasePostgreSQLParserListener
- tokenStream antlr.TokenStream
- sqlList []string
-}
-
-// EnterStmt_return is called when entering a RETURN statement.
-func (l *returnQueryListener) EnterStmt_return(ctx *postgresql.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 := ParsePostgreSQL(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 postgresql.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 postgresql.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 OUT/TABLE parameter names from CREATE FUNCTION.
-// Returns column names from OUT parameters and RETURNS TABLE columns.
-func (q *querySpanExtractor) extractParameterNamesFromCreateFunction(createFuncStmt postgresql.ICreatefunctionstmtContext) []string {
- var columnNames []string
-
- // Extract OUT parameters from func_args_with_defaults
- funcArgs := createFuncStmt.Func_args_with_defaults()
- if funcArgs != nil && funcArgs.Func_args_with_defaults_list() != nil {
- for _, argWithDefault := range funcArgs.Func_args_with_defaults_list().AllFunc_arg_with_default() {
- if argWithDefault == nil {
- continue
- }
- funcArg := argWithDefault.Func_arg()
- if funcArg == nil {
- continue
- }
-
- // Check if this is an OUT or INOUT parameter
- argClass := funcArg.Arg_class()
- if argClass == nil {
- continue
- }
-
- // Check for OUT_P (OUT mode) or INOUT
- if argClass.OUT_P() == nil && argClass.INOUT() == nil {
- continue
- }
-
- // Extract parameter name
- // func_arg can be: arg_class param_name? func_type | param_name arg_class? func_type | func_type
- var paramName string
- if funcArg.Param_name() != nil {
- paramName = q.extractParamName(funcArg.Param_name())
- }
-
- // If OUT parameter has a name, add it to column names
- if paramName != "" {
- columnNames = append(columnNames, paramName)
- }
- }
- }
-
- // Extract TABLE columns from RETURNS TABLE clause
- 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 postgresql.IParam_nameContext) string {
- if paramNameCtx == nil {
- return ""
- }
-
- // Handle type_function_name (most common case)
- if paramNameCtx.Type_function_name() != nil {
- return normalizePostgreSQLTypeFunctionName(paramNameCtx.Type_function_name())
- }
-
- // Handle builtin_function_name, LEFT, RIGHT
- return strings.ToLower(paramNameCtx.GetText())
-}
-
-type plpgSQLListener struct {
- *postgresql.BasePostgreSQLParserListener
- q *querySpanExtractor
-
- variables map[string]*base.QuerySpanResult
-
- span *base.QuerySpan
- err error
-}
-
-func (l *plpgSQLListener) EnterFunc_as(ctx *postgresql.Func_asContext) {
- antlr.ParseTreeWalkerDefault.Walk(l, ctx.Definition)
-}
-
-func (l *plpgSQLListener) EnterDecl_statement(ctx *postgresql.Decl_statementContext) {
- vName := normalizePostgreSQLAnyIdentifier(ctx.Decl_varname().Any_identifier())
- l.variables[vName] = &base.QuerySpanResult{
- SourceColumns: base.SourceColumnSet{},
- }
-}
-
-func (l *plpgSQLListener) EnterStmt_assign(ctx *postgresql.Stmt_assignContext) {
- names := NormalizePostgreSQLAnyName(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 *postgresql.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 *postgresql.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 postgresql.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 postgresql.ISelectstmtContext) (base.TableSource, error) {
- selectNoParens := getSelectNoParensFromSelectStmt(selectstmt)
- return q.extractTableSourceFromSelectNoParens(selectNoParens)
-}
-
-func (q *querySpanExtractor) extractTableSourceFromSelectNoParens(selectNoParens postgresql.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 postgresql.IWith_clauseContext) (restoreCTELength int, err error) {
- previousCTELength := len(q.ctes)
- recursive := with.RECURSIVE() != nil
- for _, cte := range with.Cte_list().AllCommon_table_expr() {
- cteName := normalizePostgreSQLName(cte.Name())
- var colAliasNames []string
- if cte.Opt_name_list() != nil {
- for _, name := range cte.Opt_name_list().Name_list().AllName() {
- colAliasNames = append(colAliasNames, normalizePostgreSQLName(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 postgresql.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 postgresql.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 postgresql.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 postgresql.PostgreSQLParserUNION:
- 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() == postgresql.PostgreSQLParserALL {
- op.IsDistinct = false
- i++ // Skip ALL token
- } else if nextToken.GetSymbol().GetTokenType() == postgresql.PostgreSQLParserDISTINCT {
- op.IsDistinct = true
- i++ // Skip DISTINCT token
- }
- }
- }
- operators = append(operators, op)
-
- case postgresql.PostgreSQLParserEXCEPT:
- 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() == postgresql.PostgreSQLParserALL {
- op.IsDistinct = false
- i++ // Skip ALL token
- } else if nextToken.GetSymbol().GetTokenType() == postgresql.PostgreSQLParserDISTINCT {
- op.IsDistinct = true
- i++ // Skip DISTINCT token
- }
- }
- }
- operators = append(operators, op)
-
- case postgresql.PostgreSQLParserINTERSECT:
- 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() == postgresql.PostgreSQLParserALL {
- op.IsDistinct = false
- i++ // Skip ALL token
- } else if nextToken.GetSymbol().GetTokenType() == postgresql.PostgreSQLParserDISTINCT {
- 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 postgresql.ISelect_no_parensContext) (
- baseParts []postgresql.ISimple_select_intersectContext,
- baseOperators []SetOperator,
- recursivePart postgresql.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 postgresql.PostgreSQLParserUNION:
- 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 postgresql.PostgreSQLParserALL:
- op.IsDistinct = false
- i++
- case postgresql.PostgreSQLParserDISTINCT:
- op.IsDistinct = true
- i++
- default:
- }
- }
- }
- allOperators = append(allOperators, op)
- lastUnionIndex = partIndex
- partIndex++
-
- case postgresql.PostgreSQLParserEXCEPT:
- 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 postgresql.PostgreSQLParserALL:
- op.IsDistinct = false
- i++
- case postgresql.PostgreSQLParserDISTINCT:
- op.IsDistinct = true
- i++
- default:
- }
- }
- }
- allOperators = append(allOperators, op)
- partIndex++
-
- case postgresql.PostgreSQLParserINTERSECT:
- 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 postgresql.PostgreSQLParserALL:
- op.IsDistinct = false
- i++
- case postgresql.PostgreSQLParserDISTINCT:
- 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 postgresql.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 postgresql.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 postgresql.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 postgresql.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 []postgresql.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 postgresql.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 postgresql.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 tableRef.Table_ref() != nil {
- anchor, err = q.extractTableSourceFromTableRef(tableRef.Table_ref())
- 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 = normalizePostgreSQLNameList(join.Join_qual().Name_list())
- }
- anchor = q.joinTableSources(anchor, tableSource, naturalJoin, usingColumns)
- if i == 0 && tableRef.Opt_alias_clause() != nil && tableRef.Table_ref() != nil {
- 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 postgresql.IRelation_exprContext, aliasClause postgresql.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 := NormalizePostgreSQLQualifiedName(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 = normalizePostgreSQLTableAlias(tableAlias.Table_alias())
- }
- if tableAlias.Name_list() != nil {
- columnAliases = normalizePostgreSQLNameList(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 postgresql.IFunc_expr_windowlessContext, funcAlias postgresql.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 postgresql.IFunc_tableContext, funcAlias postgresql.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 postgresql.ITable_alias_clauseContext) (base.TableSource, error) {
- aliasName := ""
- var colAliases []string
-
- // Get table alias name
- if tableAlias.Table_alias() != nil {
- aliasName = normalizePostgreSQLTableAlias(tableAlias.Table_alias())
- }
-
- // Get column aliases if present
- if tableAlias.Name_list() != nil {
- for _, name := range tableAlias.Name_list().AllName() {
- colAliases = append(colAliases, normalizePostgreSQLName(name))
- }
- }
-
- return applyAliasToTableSource(source, aliasName, colAliases)
-}
-
-func applyAliasClauseToTableSource(source base.TableSource, aliasClause postgresql.IAlias_clauseContext) (base.TableSource, error) {
- if aliasClause == nil {
- return source, nil
- }
-
- aliasName := ""
- var colAliases []string
-
- // Get table alias name
- if aliasClause.Colid() != nil {
- aliasName = NormalizePostgreSQLColid(aliasClause.Colid())
- }
-
- // Get column aliases if present
- if aliasClause.Name_list() != nil {
- for _, name := range aliasClause.Name_list().AllName() {
- colAliases = append(colAliases, normalizePostgreSQLName(name))
- }
- }
-
- return applyAliasToTableSource(source, aliasName, colAliases)
-}
-
-// applyAliasToTableSource applies an alias to a table source
-func applyOptAliasClauseToTableSource(source base.TableSource, aliasClause postgresql.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 postgresql.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 = NormalizePostgreSQLColid(funcAlias.Colid())
- }
-
- var colAliases []string
- if funcAlias.Tablefuncelementlist() != nil {
- for _, name := range funcAlias.Tablefuncelementlist().AllTablefuncelement() {
- if name.Colid() != nil {
- colAliases = append(colAliases, NormalizePostgreSQLColid(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 postgresql.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.(*postgresql.Target_starContext); ok {
- // Handle SELECT * or SELECT table.*
- starColumns := q.handleStarExpansion(fromFieldList)
- result = append(result, starColumns...)
- continue
- }
-
- if _, ok := targetEl.(*postgresql.Target_columnrefContext); ok {
- columns, err := q.getColumnsFromColumnRef(targetEl.(*postgresql.Target_columnrefContext).Columnref())
- if err != nil {
- return nil, errors.Wrapf(err, "failed to process column reference in target element")
- }
- result = append(result, columns...)
- continue
- }
-
- // Check if this is a labeled target (expression with optional alias)
- if labelCtx, ok := targetEl.(*postgresql.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 *postgresql.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 = normalizePostgreSQLCollabel(targetAlias.Collabel())
- } else if targetAlias.Bare_col_label() != nil {
- columnName = normalizePostgreSQLBareColLabel(targetAlias.Bare_col_label())
- }
- }
-
- if columnName == "" {
- // No alias, try to extract name from expression
- columnName = q.getFieldNameFromAExpr(expr)
- if columnName == "" {
- // Use default unknown column name
- columnName = pgUnknownFieldName
- }
- }
-
- 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 postgresql.IA_exprContext) string {
- if expr == nil {
- return pgUnknownFieldName
- }
-
- // 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 pgUnknownFieldName
-}
-
-// 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.(postgresql.IColumnrefContext); ok {
- // Get the column name from the columnref
- if columnRef.Colid() != nil {
- colName := NormalizePostgreSQLColid(columnRef.Colid())
-
- // Check if there's indirection (qualified name)
- if columnRef.Indirection() != nil {
- parts := normalizePostgreSQLIndirection(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 postgresql.IColumnrefContext:
- if ctx.Colid() != nil {
- return NormalizePostgreSQLColid(ctx.Colid())
- }
- case postgresql.IFunc_exprContext:
- // return the function name as the column name
- _, funcName, _, err := q.extractFunctionElementFromFuncExpr(ctx)
- if err == nil && funcName != "" {
- return funcName
- }
- return pgUnknownFieldName
- case postgresql.IFunc_expr_windowlessContext:
- _, funcName, _, err := q.extractFunctionElementFromFuncExprWindowless(ctx)
- if err == nil && funcName != "" {
- return funcName
- }
- return pgUnknownFieldName
- default:
- }
-
- // Recursively search children
- for i := 0; i < node.GetChildCount(); i++ {
- if child, ok := node.GetChild(i).(antlr.ParseTree); ok {
- // Skip terminal nodes
- if _, isTerminal := child.(antlr.TerminalNode); isTerminal {
- continue
- }
- if name := q.findColumnNameInExpression(child); name != "" {
- return name
- }
- }
- }
-
- return ""
-}
-
-// extractSourceColumnSetFromAExpr extracts source columns from an a_expr node
-func (q *querySpanExtractor) extractSourceColumnSetFromAExpr(expr postgresql.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 postgresql.IColumnrefContext:
- // Handle column reference directly
- return q.getSourceColumnSetFromColumnRef(ctx)
- case postgresql.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 postgresql.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 postgresql.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 = NormalizePostgreSQLColid(ctx.Colid())
- }
-
- // Handle indirection (qualified names like table.column or schema.table.column)
- if ctx.Indirection() != nil {
- parts := normalizePostgreSQLIndirection(ctx.Indirection())
-
- // Check for star expansion (should have been handled elsewhere, but check anyway)
- if len(parts) > 0 && parts[len(parts)-1] == "*" {
- // This is table.* or schema.table.* - should not happen in regular column refs
- // Return empty set as this should be handled by star expansion logic
- switch len(parts) {
- case 1:
- // table.* case - columnName contains the table name from Colid()
- tableName = columnName
- columnName = ""
- schemaName = ""
- case 2:
- // schema.table.* case - columnName contains the schema name from Colid()
- schemaName = columnName
- tableName = parts[0]
- columnName = ""
- default:
- // More complex indirection with star, ignore
- return []base.QuerySpanResult{}, nil
- }
- sources, ok := q.getAllTableColumnSources(schemaName, tableName)
- if !ok {
- return []base.QuerySpanResult{}, &base.ResourceNotFoundError{
- Err: errors.New("cannot find the table for star expansion"),
- Database: &q.defaultDatabase,
- Schema: &schemaName,
- Table: &tableName,
- }
- }
- return sources, nil
- }
-
- switch len(parts) {
- case 1:
- // table.column case
- tableName = columnName
- columnName = parts[0]
- case 2:
- // schema.table.column case
- schemaName = columnName
- tableName = parts[0]
- columnName = parts[1]
- default:
- // More complex indirection, just take the last part as column
- if len(parts) > 0 {
- columnName = parts[len(parts)-1]
- }
- }
- }
-
- // Special handling for record/composite types
- // If we have a table name but no column name, it might be a whole-row reference
- if tableName != "" && columnName == "" {
- // This is a whole-row reference like "table" in row_to_json(table)
- // We need to return all columns from that table
- tableSource := q.findTableInFromNew(tableName)
- return tableSource.GetQuerySpanResult(), nil
- }
-
- // Look up the column source
- sources, columnSourceOk := q.getFieldColumnSource(schemaName, tableName, columnName)
- // The column ref in function call can be record type, such as row_to_json.
- if !columnSourceOk {
- if schemaName == "" {
- tableSource := q.findTableInFrom(tableName, columnName)
- if tableSource == nil {
- return []base.QuerySpanResult{}, &base.ResourceNotFoundError{
- Err: errors.New("cannot find the column ref"),
- Database: &q.defaultDatabase,
- Schema: &schemaName,
- Table: &tableName,
- Column: &columnName,
- }
- }
- querySpanResult := tableSource.GetQuerySpanResult()
- var result []base.QuerySpanResult
- result = append(result, querySpanResult...)
- return result, nil
- }
- return []base.QuerySpanResult{}, &base.ResourceNotFoundError{
- Err: errors.New("cannot find the column ref"),
- Database: &q.defaultDatabase,
- Schema: &schemaName,
- Table: &tableName,
- Column: &columnName,
- }
- }
- return []base.QuerySpanResult{
- {
- Name: columnName,
- SourceColumns: sources,
- },
- }, nil
-}
-
-// getSourceColumnSetFromColumnRef extracts source columns from a column reference.
-func (q *querySpanExtractor) getSourceColumnSetFromColumnRef(ctx postgresql.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 postgresql.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 postgresql.ISelectstmtContext) postgresql.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 postgresql.ISelect_with_parensContext) postgresql.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 postgresql.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 postgresql.IFunc_exprContext) (string, string, []antlr.ParseTree, error) {
- args := getArgumentsFromFunctionExpr(funcExpr)
- switch {
- case funcExpr.Func_application() != nil:
- funcName := funcExpr.Func_application().Func_name()
- names := NormalizePostgreSQLFuncName(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.Json_aggregate_func() != nil:
- if funcExpr.Json_aggregate_func().JSON_OBJECTAGG() != nil {
- return "", "json_objectagg", args, nil
- } else if funcExpr.Json_aggregate_func().JSON_ARRAYAGG() != nil {
- return "", "json_arrayagg", args, 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()), args, nil
- }
- default:
- }
- return "", "", nil, errors.New("unable to extract function name")
-}
-
-func (*querySpanExtractor) extractFunctionElementFromFuncExprWindowless(funcExprWindowless postgresql.IFunc_expr_windowlessContext) (string, string, []antlr.ParseTree, error) {
- args := getArgumentsFromFunctionExprWindowless(funcExprWindowless)
- switch {
- case funcExprWindowless.Func_application() != nil:
- funcName := funcExprWindowless.Func_application().Func_name()
- names := NormalizePostgreSQLFuncName(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.Json_aggregate_func() != nil:
- if funcExprWindowless.Json_aggregate_func().JSON_OBJECTAGG() != nil {
- return "", "json_objectagg", args, nil
- } else if funcExprWindowless.Json_aggregate_func().JSON_ARRAYAGG() != nil {
- return "", "json_arrayagg", args, nil
- }
- 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 postgresql.IFunc_exprContext) (string, string, error) {
- switch {
- case funcExpr.Func_application() != nil:
- funcName := funcExpr.Func_application().Func_name()
- if funcName.Colid() != nil {
- names := NormalizePostgreSQLFuncName(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 = normalizePostgreSQLIdentifier(t.Identifier())
- }
- return "", s, nil
- } else {
- // Builtin function without schema
- return "", strings.ToLower(funcName.GetText()), nil
- }
- case funcExpr.Json_aggregate_func() != nil:
- if funcExpr.Json_aggregate_func().JSON_OBJECTAGG() != nil {
- return "", "json_objectagg", nil
- } else if funcExpr.Json_aggregate_func().JSON_ARRAYAGG() != nil {
- return "", "json_arrayagg", 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 postgresql.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 JSON aggregate functions like JSON_AGG(expr).
- if f := funcExpr.Json_aggregate_func(); f != nil {
- // Based on Postgres grammar, these functions (JSON_AGG, JSONB_AGG, JSON_OBJECT_AGG, JSONB_OBJECT_AGG)
- // typically take one or two main arguments. Returning 1 is a reasonable assumption for the primary expression argument.
- return 1
- }
-
- // 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)
-
- // --- JSON Functions ---
- if f.JSON_OBJECT() != nil {
- if list := f.Func_arg_list(); list != nil {
- return len(list.AllFunc_arg_expr())
- }
- if list := f.Json_name_and_value_list(); list != nil {
- return len(list.AllJson_name_and_value())
- }
- return 0
- }
- if f.JSON_ARRAY() != nil {
- if list := f.Json_value_expr_list(); list != nil {
- return len(list.AllJson_value_expr())
- }
- if f.Select_no_parens() != nil {
- return 1
- } // subquery
- return 0
- }
- if f.JSON() != nil || f.JSON_SCALAR() != nil || f.JSON_SERIALIZE() != nil {
- return 1
- }
- if f.MERGE_ACTION() != nil {
- return 0
- }
- if f.JSON_QUERY() != nil || f.JSON_EXISTS() != nil || f.JSON_VALUE() != nil {
- count := 2 // json_value_expr, a_expr
- return count
- }
- }
-
- return 0
-}
-
-func getArgumentsFromFunctionExpr(funcExpr postgresql.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.(postgresql.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))
- } else if f.JSON_OBJECT() != nil {
- if list := f.Func_arg_list(); list != nil {
- result = append(result, getArgumentsFromFuncArgList(list)...)
- }
- if list := f.Json_name_and_value_list(); list != nil {
- result = append(result, getArgumentsFromJSONNameAndValueList(list)...)
- }
- if list := f.Json_value_expr_list(); list != nil {
- result = append(result, getArgumentsFromJSONValueExprList(list)...)
- }
- } else if f.JSON_ARRAY() != nil {
- if list := f.Json_value_expr_list(); list != nil {
- result = append(result, getArgumentsFromJSONValueExprList(list)...)
- }
- if list := f.Select_no_parens(); list != nil {
- result = append(result, list)
- }
- } else if f.JSON() != nil {
- result = append(result, f.Json_value_expr())
- } else if f.JSON_SCALAR() != nil {
- result = append(result, f.A_expr(0))
- } else if f.JSON_SERIALIZE() != nil {
- result = append(result, f.Json_value_expr())
- } else if f.JSON_QUERY() != nil {
- result = append(result, f.Json_value_expr())
- result = append(result, f.A_expr(0))
- } else if f.JSON_EXISTS() != nil {
- result = append(result, f.Json_value_expr())
- result = append(result, f.A_expr(0))
- } else if f.JSON_VALUE() != nil {
- result = append(result, f.Json_value_expr())
- result = append(result, f.A_expr(0))
- }
- }
- return result
-}
-
-func getArgumentsFromFunctionExprWindowless(funcExprWindowless postgresql.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.(postgresql.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))
- } else if f.JSON_OBJECT() != nil {
- if list := f.Func_arg_list(); list != nil {
- result = append(result, getArgumentsFromFuncArgList(list)...)
- }
- if list := f.Json_name_and_value_list(); list != nil {
- result = append(result, getArgumentsFromJSONNameAndValueList(list)...)
- }
- if list := f.Json_value_expr_list(); list != nil {
- result = append(result, getArgumentsFromJSONValueExprList(list)...)
- }
- } else if f.JSON_ARRAY() != nil {
- if list := f.Json_value_expr_list(); list != nil {
- result = append(result, getArgumentsFromJSONValueExprList(list)...)
- }
- if list := f.Select_no_parens(); list != nil {
- result = append(result, list)
- }
- } else if f.JSON() != nil {
- result = append(result, f.Json_value_expr())
- } else if f.JSON_SCALAR() != nil {
- result = append(result, f.A_expr(0))
- } else if f.JSON_SERIALIZE() != nil {
- result = append(result, f.Json_value_expr())
- } else if f.JSON_QUERY() != nil {
- result = append(result, f.Json_value_expr())
- result = append(result, f.A_expr(0))
- } else if f.JSON_EXISTS() != nil {
- result = append(result, f.Json_value_expr())
- result = append(result, f.A_expr(0))
- } else if f.JSON_VALUE() != nil {
- result = append(result, f.Json_value_expr())
- result = append(result, f.A_expr(0))
- }
- }
- return result
-}
-
-func getArgumentsFromFuncArgList(funcArgList postgresql.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 getArgumentsFromJSONNameAndValueList(jsonNameAndValueList postgresql.IJson_name_and_value_listContext) []antlr.ParseTree {
- var result []antlr.ParseTree
- if jsonNameAndValueList == nil {
- return result
- }
- for _, nameAndValue := range jsonNameAndValueList.AllJson_name_and_value() {
- result = append(result, nameAndValue)
- }
- return result
-}
-
-func getArgumentsFromJSONValueExprList(jsonValueExprList postgresql.IJson_value_expr_listContext) []antlr.ParseTree {
- var result []antlr.ParseTree
- if jsonValueExprList == nil {
- return result
- }
- for _, valueExpr := range jsonValueExprList.AllJson_value_expr() {
- result = append(result, valueExpr)
- }
- return result
-}
-
-func (q *querySpanExtractor) extractTableSourceFromSystemFunctionNew(funcName string, args []antlr.ParseTree, alias postgresql.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 = NormalizePostgreSQLColid(alias.Colid())
- if alias.Tablefuncelementlist() != nil {
- for _, name := range alias.Tablefuncelementlist().AllTablefuncelement() {
- if name.Colid() != nil {
- columnNames = append(columnNames, NormalizePostgreSQLColid(name.Colid()))
- }
- }
- }
- } else if a := alias.Alias_clause(); a != nil {
- // Get table alias name
- if a.Colid() != nil {
- tableName = NormalizePostgreSQLColid(a.Colid())
- }
-
- // Get column aliases if present
- if a.Name_list() != nil {
- for _, name := range a.Name_list().AllName() {
- columnNames = append(columnNames, normalizePostgreSQLName(name))
- }
- }
- }
-
- for _, colName := range columnNames {
- tableSource.Columns = append(tableSource.Columns, base.QuerySpanResult{
- Name: colName,
- SourceColumns: sourceColumn,
- })
- }
- tableSource.Name = tableName
- return tableSource, nil
- case jsonbArrayElements, jsonArrayElements, jsonbArrayElementsText, jsonArrayElementsText:
- // jsonb_array_elements returns a single "value" column (unlike jsonb_each which returns key + value).
- // SELECT * FROM t, jsonb_array_elements(a) AS elems;
- var tableSource base.TableSource = &base.PseudoTable{
- Name: "",
- Columns: []base.QuerySpanResult{
- {
- 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 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 = NormalizePostgreSQLColid(alias.Colid())
- if alias.Tablefuncelementlist() != nil {
- for _, name := range alias.Tablefuncelementlist().AllTablefuncelement() {
- if name.Colid() != nil {
- columnNames = append(columnNames, NormalizePostgreSQLColid(name.Colid()))
- }
- }
- }
- } else if a := alias.Alias_clause(); a != nil {
- // Get table alias name
- if a.Colid() != nil {
- tableName = NormalizePostgreSQLColid(a.Colid())
- }
-
- // Get column aliases if present
- if a.Name_list() != nil {
- for _, name := range a.Name_list().AllName() {
- columnNames = append(columnNames, normalizePostgreSQLName(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/pg/query_span_omni.go b/backend/plugin/parser/pg/query_span_omni.go
index ef2c8fcf5c..6d02ad16df 100644
--- a/backend/plugin/parser/pg/query_span_omni.go
+++ b/backend/plugin/parser/pg/query_span_omni.go
@@ -26,8 +26,26 @@ type omniQuerySpanExtractor struct {
searchPath []string
// metaCache is a lazy-load cache for database metadata.
// Use getDatabaseMetadata() instead of accessing directly.
- metaCache map[string]*model.DatabaseMetadata
- cat *catalog.Catalog
+ metaCache map[string]*model.DatabaseMetadata
+ cat *catalog.Catalog
+ funcBodyCache map[uint32][]base.SourceColumnSet
+ // funcOrigDefs stores the original (non-stubbed) function definitions keyed
+ // by lowercase function name. Used by analyzeFunctionBody to get the real
+ // body when it was stubbed during catalog loading.
+ funcOrigDefs map[string]string
+ // funcSourceColumns accumulates table-level access (column="") discovered inside
+ // function bodies. Merged into the top-level QuerySpan.SourceColumns.
+ funcSourceColumns base.SourceColumnSet
+ // funcPredicateColumns accumulates columns used in WHERE/JOIN conditions
+ // inside function bodies. Merged into the top-level QuerySpan.PredicateColumns.
+ funcPredicateColumns base.SourceColumnSet
+ // fallbackCTEMap holds CTE definitions during fallback column extraction.
+ // Set by extractFallbackColumns and used by extractColumnsFromRangeVar
+ // to resolve CTE references.
+ fallbackCTEMap map[string]*ast.SelectStmt
+ // fallbackCTEAliases holds CTE column aliases (e.g., WITH t1(cc1, cc2) AS (...)).
+ // Keyed by lowercase CTE name.
+ fallbackCTEAliases map[string][]string
}
// newOmniQuerySpanExtractor creates a new omni-based query span extractor.
@@ -36,10 +54,14 @@ func newOmniQuerySpanExtractor(defaultDatabase string, searchPath []string, gCtx
searchPath = []string{"public"}
}
return &omniQuerySpanExtractor{
- defaultDatabase: defaultDatabase,
- searchPath: searchPath,
- gCtx: gCtx,
- metaCache: make(map[string]*model.DatabaseMetadata),
+ defaultDatabase: defaultDatabase,
+ searchPath: searchPath,
+ gCtx: gCtx,
+ metaCache: make(map[string]*model.DatabaseMetadata),
+ funcBodyCache: make(map[uint32][]base.SourceColumnSet),
+ funcOrigDefs: make(map[string]string),
+ funcSourceColumns: make(base.SourceColumnSet),
+ funcPredicateColumns: make(base.SourceColumnSet),
}
}
@@ -80,6 +102,18 @@ func (e *omniQuerySpanExtractor) initCatalog() error {
}
}
+ // Store original (non-stubbed) function definitions for body analysis.
+ for _, s := range meta.GetProto().GetSchemas() {
+ for _, f := range s.GetFunctions() {
+ if f.Definition != "" {
+ origBody := extractFuncBodyFromDef(f.Definition)
+ if origBody != "" {
+ e.funcOrigDefs[strings.ToLower(f.Name)] = origBody
+ }
+ }
+ }
+ }
+
return nil
}
@@ -106,7 +140,14 @@ func buildMinimalDDL(meta *storepb.DatabaseSchemaMetadata) string {
}
for _, f := range s.Functions {
if f.Definition != "" {
- fmt.Fprintf(&b, "%s;\n", strings.TrimSuffix(strings.TrimSpace(f.Definition), ";"))
+ // Load the function with a stubbed body to avoid type validation
+ // failures. The catalog needs the function signature (name, params,
+ // return type) to resolve SELECT * FROM func(), but the actual body
+ // is analyzed separately via analyzeFunctionBody. Stubbing the body
+ // avoids errors when table column types default to text but the
+ // function declares int parameters.
+ stubbed := stubFunctionBody(f.Definition)
+ fmt.Fprintf(&b, "%s;\n", strings.TrimSuffix(strings.TrimSpace(stubbed), ";"))
}
}
}
@@ -149,6 +190,77 @@ func quoteIdentifier(name string) string {
return `"` + strings.ReplaceAll(name, `"`, `""`) + `"`
}
+// extractFuncBodyFromDef extracts the dollar-quoted function body from a
+// CREATE FUNCTION definition string.
+func extractFuncBodyFromDef(definition string) string {
+ // Find dollar-quoted body.
+ tag, bodyStart, bodyEnd := findDollarQuotedBody(definition)
+ if tag == "" {
+ return ""
+ }
+ return definition[bodyStart : bodyEnd-len(tag)]
+}
+
+// findDollarQuotedBody locates the dollar-quoted body in a function definition.
+// Returns the tag, the start of body content (after opening tag), and the end
+// position (after closing tag).
+func findDollarQuotedBody(definition string) (tag string, bodyStart int, bodyEnd int) {
+ for i := 0; i < len(definition); i++ {
+ if definition[i] != '$' {
+ continue
+ }
+ // Find end of tag.
+ tagEnd := i + 1
+ for tagEnd < len(definition) && definition[tagEnd] != '$' {
+ tagEnd++
+ }
+ if tagEnd >= len(definition) {
+ continue
+ }
+ tag := definition[i : tagEnd+1]
+ contentStart := tagEnd + 1
+ // Find closing tag.
+ closeIdx := strings.Index(definition[contentStart:], tag)
+ if closeIdx >= 0 {
+ return tag, contentStart, contentStart + closeIdx + len(tag)
+ }
+ }
+ return "", 0, 0
+}
+
+// stubFunctionBody replaces the body of a CREATE FUNCTION statement with a
+// minimal stub. This avoids catalog type validation failures when table column
+// types are unknown (defaulting to text) while the function declares specific
+// return types. The function signature is preserved so the catalog can resolve
+// function calls.
+func stubFunctionBody(definition string) string {
+ tag, bodyStart, bodyEnd := findDollarQuotedBody(definition)
+ if tag == "" {
+ return definition
+ }
+
+ // Determine language for appropriate stub body.
+ upper := strings.ToUpper(definition)
+ lang := ""
+ if langIdx := strings.Index(upper, "LANGUAGE"); langIdx >= 0 {
+ rest := strings.TrimSpace(definition[langIdx+8:])
+ parts := strings.Fields(rest)
+ if len(parts) > 0 {
+ lang = strings.ToLower(strings.TrimRight(parts[0], ";"))
+ }
+ }
+
+ var stubBody string
+ switch lang {
+ case "plpgsql":
+ stubBody = " BEGIN RETURN; END; "
+ default:
+ stubBody = " SELECT NULL "
+ }
+
+ return definition[:bodyStart] + stubBody + definition[bodyEnd-len(tag):]
+}
+
// getQuerySpan extracts the query span for the given SQL statement.
func (e *omniQuerySpanExtractor) getQuerySpan(ctx context.Context, stmt string) (*base.QuerySpan, error) {
e.ctx = ctx
@@ -227,27 +339,30 @@ func (e *omniQuerySpanExtractor) getQuerySpan(ctx context.Context, stmt string)
}, nil
}
- // Pre-analysis check: if the parse tree contains constructs that omni
- // doesn't fully handle (JSON constructors, function calls that may be UDFs),
- // fall back to ANTLR immediately.
- if selectNeedsFallback(selStmt) {
- return e.fallbackToANTLR(ctx, stmt)
- }
-
if err := e.initCatalog(); err != nil {
return nil, errors.Wrapf(err, "failed to initialize catalog")
}
query, err := e.cat.AnalyzeSelectStmt(selStmt)
if err != nil {
- // Fallback: use legacy ANTLR-based extractor when omni analysis fails.
- return e.fallbackToANTLR(ctx, stmt)
- }
-
- // Post-analysis check: if the analyzed query has patterns that need
- // special handling not yet in omni (function RTEs, UDFs).
- if queryNeedsFallback(query) {
- return e.fallbackToANTLR(ctx, stmt)
+ // Before falling back, try to handle user-defined table-returning functions
+ // that omni's AnalyzeSelectStmt can't resolve (e.g., RETURNS TABLE functions
+ // used as table sources: SELECT * FROM func()).
+ if results := e.tryUserFuncTableSource(selStmt, accessesMap); results != nil {
+ return &base.QuerySpan{
+ Type: base.Select,
+ SourceColumns: accessesMap,
+ Results: results,
+ }, nil
+ }
+ // Fail-open: return access tables with best-effort column names and lineage
+ // when analysis fails (e.g., unsupported built-in functions).
+ return &base.QuerySpan{
+ Type: base.Select,
+ SourceColumns: accessesMap,
+ Results: e.extractFallbackColumns(selStmt),
+ PredicateColumns: e.funcPredicateColumns,
+ }, nil
}
// Step 7: Extract lineage from analyzed query.
@@ -255,10 +370,16 @@ func (e *omniQuerySpanExtractor) getQuerySpan(ctx context.Context, stmt string)
// from ExtractAccessTables above — no need to re-extract from the query.
results := e.extractLineage(query, selStmt)
+ // Merge columns discovered inside function bodies into the top-level sets.
+ for col := range e.funcSourceColumns {
+ accessesMap[col] = true
+ }
+
return &base.QuerySpan{
- Type: base.Select,
- SourceColumns: accessesMap,
- Results: results,
+ Type: base.Select,
+ SourceColumns: accessesMap,
+ PredicateColumns: e.funcPredicateColumns,
+ Results: results,
}, nil
}
@@ -275,6 +396,24 @@ func (e *omniQuerySpanExtractor) extractLineage(q *catalog.Query, selStmt *ast.S
// IsPlainField = true only for columns expanded from SELECT * or SELECT t.*.
plainMask := buildPlainFieldMask(selStmt, q)
+ // Collect parse tree ResTargets for name fixups.
+ var parseTargets []*ast.ResTarget
+ if selStmt != nil && selStmt.TargetList != nil {
+ for _, item := range selStmt.TargetList.Items {
+ if rt, ok := item.(*ast.ResTarget); ok {
+ parseTargets = append(parseTargets, rt)
+ }
+ }
+ }
+
+ // Build a helper analyzer for parse-tree fallback extraction.
+ var analyzer *plpgsqlAnalyzer
+ var fromTables map[string]base.ColumnResource
+ if selStmt != nil {
+ analyzer = &plpgsqlAnalyzer{extractor: e, scope: newVariableScope(nil)}
+ fromTables = analyzer.collectFromTables(selStmt)
+ }
+
var results []base.QuerySpanResult
idx := 0
for _, te := range q.TargetList {
@@ -289,8 +428,22 @@ func (e *omniQuerySpanExtractor) extractLineage(q *catalog.Query, selStmt *ast.S
if isPlain {
isPlain = isUltimatelyPlainColumn(q, te.Expr)
}
+ name := te.ResName
+ // When the catalog reduces an expression to ConstExpr (losing column refs,
+ // e.g., json_object('id': a)), fall back to the parse tree to extract
+ // column references and derive a better name.
+ if idx < len(parseTargets) {
+ if name == "?column?" {
+ if better := figureResTargetName(parseTargets[idx]); better != "" {
+ name = better
+ }
+ }
+ if len(sourceColSet) == 0 && analyzer != nil {
+ analyzer.extractColumnRefsFromExpr(parseTargets[idx].Val, fromTables, sourceColSet)
+ }
+ }
results = append(results, base.QuerySpanResult{
- Name: te.ResName,
+ Name: name,
SourceColumns: sourceColSet,
IsPlainField: isPlain,
})
@@ -522,9 +675,19 @@ func (e *omniQuerySpanExtractor) walkExpr(q *catalog.Query, expr catalog.Analyze
case *catalog.VarExpr:
e.resolveVar(q, v, result)
case *catalog.FuncCallExpr:
+ // Walk arguments first — they contribute to access tables.
for _, arg := range v.Args {
e.walkExpr(q, arg, result)
}
+ // Trace lineage through user-defined function body.
+ if proc := e.cat.GetUserProcByOID(v.FuncOID); proc != nil {
+ // Expression context: merge all output columns into one set.
+ for _, colSet := range e.analyzeFunctionBody(proc) {
+ for k, val := range colSet {
+ result[k] = val
+ }
+ }
+ }
case *catalog.AggExpr:
for _, arg := range v.Args {
e.walkExpr(q, arg, result)
@@ -647,141 +810,906 @@ func (e *omniQuerySpanExtractor) resolveVar(q *catalog.Query, v *catalog.VarExpr
// After analysis, VarExprs typically point to base RTEs, not join RTEs.
case catalog.RTEFunction:
- // Function results have no further lineage to track.
+ // Trace lineage through user-defined function body.
+ if len(rte.FuncExprs) > 0 {
+ if fc, ok := rte.FuncExprs[0].(*catalog.FuncCallExpr); ok {
+ if proc := e.cat.GetUserProcByOID(fc.FuncOID); proc != nil {
+ bodySets := e.analyzeFunctionBody(proc)
+ if colIdx >= 0 && colIdx < len(bodySets) {
+ for k, val := range bodySets[colIdx] {
+ result[k] = val
+ }
+ }
+ } else {
+ // Built-in function (no user proc) — trace lineage through
+ // the function's arguments. E.g., jsonb_each(a) depends on column a.
+ for _, arg := range fc.Args {
+ e.walkExpr(q, arg, result)
+ }
+ }
+ }
+ }
default:
// Unknown RTE kind — skip.
}
}
-// selectNeedsFallback checks the raw parse tree (before analysis) for constructs
-// that omni doesn't handle well, such as JSON constructors and function calls.
-func selectNeedsFallback(sel *ast.SelectStmt) bool {
- if sel == nil {
- return false
- }
- // Check set operations recursively.
- if sel.Larg != nil && selectNeedsFallback(sel.Larg) {
- return true
+// extractFallbackColumns attempts to extract column names and lineage from the parse tree
+// when AnalyzeSelectStmt fails. It walks the AST to find column references and resolves
+// them against tables in the FROM clause.
+func (e *omniQuerySpanExtractor) extractFallbackColumns(selStmt *ast.SelectStmt) []base.QuerySpanResult {
+ if selStmt == nil {
+ return nil
}
- if sel.Rarg != nil && selectNeedsFallback(sel.Rarg) {
- return true
+
+ // Build a helper analyzer to reuse column reference extraction.
+ analyzer := &plpgsqlAnalyzer{
+ extractor: e,
+ scope: newVariableScope(nil),
}
- // Check target list for unsupported expressions.
- if sel.TargetList != nil {
- for _, item := range sel.TargetList.Items {
- if rt, ok := item.(*ast.ResTarget); ok && rt.Val != nil {
- if astNodeNeedsFallback(rt.Val) {
- return true
+ fromTables := analyzer.collectFromTables(selStmt)
+
+ // Build CTE map so we can trace through CTEs to real tables.
+ cteMap := collectCTEDefinitions(selStmt)
+ analyzer.cteMap = cteMap
+ // Store cteMap on the extractor so extractColumnsFromRangeVar can resolve CTEs.
+ e.fallbackCTEMap = cteMap
+ e.fallbackCTEAliases = collectCTEAliases(selStmt)
+
+ // Check if SELECT has explicit target list (not *).
+ if selStmt.TargetList != nil {
+ var results []base.QuerySpanResult
+ allStar := true
+ for _, item := range selStmt.TargetList.Items {
+ rt, ok := item.(*ast.ResTarget)
+ if !ok {
+ continue
+ }
+ if !isStarTarget(rt) {
+ allStar = false
+ name := rt.Name
+ if name == "" {
+ name = figureResTargetName(rt)
+ }
+ // PostgreSQL uses "?column?" for unnamed result columns.
+ if name == "" {
+ name = "?column?"
+ }
+ // Extract column references from the expression for lineage.
+ colSet := make(base.SourceColumnSet)
+ analyzer.extractColumnRefsFromExpr(rt.Val, fromTables, colSet)
+ results = append(results, base.QuerySpanResult{
+ Name: name,
+ SourceColumns: colSet,
+ })
+ }
+ }
+ if !allStar && len(results) > 0 {
+ // Also extract WHERE clause columns for predicate tracking.
+ if selStmt.WhereClause != nil {
+ whereColSet := make(base.SourceColumnSet)
+ analyzer.extractColumnRefsFromExpr(selStmt.WhereClause, fromTables, whereColSet)
+ for k, v := range whereColSet {
+ e.funcPredicateColumns[k] = v
}
}
+ return results
+ }
+ }
+
+ // For SELECT *, combine columns from ALL FROM clause items.
+ if selStmt.FromClause != nil {
+ var allCols []base.QuerySpanResult
+ for _, item := range selStmt.FromClause.Items {
+ allCols = append(allCols, e.extractColumnsFromFromItem(item, analyzer, fromTables)...)
+ }
+ if len(allCols) > 0 {
+ return allCols
}
}
- // Check FROM clause for function tables.
- if sel.FromClause != nil {
- for _, item := range sel.FromClause.Items {
- if _, ok := item.(*ast.RangeFunction); ok {
- return true
+
+ return nil
+}
+
+// figureResTargetName extracts a column name from a ResTarget's expression.
+func figureResTargetName(rt *ast.ResTarget) string {
+ if rt == nil || rt.Val == nil {
+ return ""
+ }
+ if cr, ok := rt.Val.(*ast.ColumnRef); ok && cr.Fields != nil {
+ // Use the last String field — for "ia.approver_emails", return "approver_emails".
+ // For unqualified "col", return "col". Skip A_Star nodes.
+ name := ""
+ for _, f := range cr.Fields.Items {
+ if s, ok := f.(*ast.String); ok {
+ name = s.Str
}
}
+ return name
}
- return false
+ if fc, ok := rt.Val.(*ast.FuncCall); ok && fc.Funcname != nil {
+ // Use the last name part — for "pg_catalog.func", return "func".
+ name := ""
+ for _, f := range fc.Funcname.Items {
+ if s, ok := f.(*ast.String); ok {
+ name = s.Str
+ }
+ }
+ return name
+ }
+ // SQL/JSON constructors.
+ if _, ok := rt.Val.(*ast.JsonObjectConstructor); ok {
+ return "json_object"
+ }
+ if _, ok := rt.Val.(*ast.JsonArrayConstructor); ok {
+ return "json_array"
+ }
+ return ""
}
-// astNodeNeedsFallback checks if an AST node contains constructs that require
-// fallback to ANTLR (e.g., JSON constructors, function calls that may be UDFs).
-func astNodeNeedsFallback(n ast.Node) bool {
- if n == nil {
- return false
+// extractColumnsFromFromItem extracts column names from a FROM clause item.
+// For RangeVar (tables), it looks up the catalog for column names and lineage.
+// For RangeFunction, it uses function name or alias column names.
+// analyzer and fromTables are optional (may be nil) and used for resolving
+// function argument column refs in the fallback path.
+func (e *omniQuerySpanExtractor) extractColumnsFromFromItem(item ast.Node, analyzer *plpgsqlAnalyzer, fromTables map[string]base.ColumnResource) []base.QuerySpanResult {
+ if item == nil {
+ return nil
}
- switch v := n.(type) {
- case *ast.JsonObjectConstructor, *ast.JsonArrayConstructor, *ast.JsonArrayQueryConstructor,
- *ast.JsonFuncExpr:
- return true
- case *ast.FuncCall:
- // Function calls may be UDFs needing body analysis.
- return true
- case *ast.A_Indirection:
- // Array subscripts and field selections — omni may reduce to ConstExpr.
- return true
- case *ast.SubLink:
- if sel, ok := v.Subselect.(*ast.SelectStmt); ok {
- return selectNeedsFallback(sel)
+
+ switch v := item.(type) {
+ case *ast.RangeVar:
+ return e.extractColumnsFromRangeVar(v)
+ case *ast.RangeFunction:
+ return e.extractColumnsFromRangeFunction(v, analyzer, fromTables)
+ case *ast.RangeSubselect:
+ if v.Alias != nil && v.Alias.Colnames != nil && len(v.Alias.Colnames.Items) > 0 {
+ return extractColnamesFromAlias(v.Alias)
+ }
+ // No explicit column aliases — extract column names from subquery's target list
+ // (only when the target list has explicit columns, not SELECT *).
+ if subSel, ok := v.Subquery.(*ast.SelectStmt); ok && subSel.TargetList != nil {
+ var results []base.QuerySpanResult
+ hasStar := false
+ for _, item := range subSel.TargetList.Items {
+ rt, ok := item.(*ast.ResTarget)
+ if !ok {
+ continue
+ }
+ if isStarTarget(rt) {
+ hasStar = true
+ break
+ }
+ name := rt.Name
+ if name == "" {
+ name = figureResTargetName(rt)
+ }
+ if name == "" {
+ name = "?column?"
+ }
+ results = append(results, base.QuerySpanResult{
+ Name: name,
+ SourceColumns: base.SourceColumnSet{},
+ })
+ }
+ if !hasStar && len(results) > 0 {
+ return results
+ }
+ // For SELECT * in subquery, try to resolve columns from the subquery's FROM tables.
+ if hasStar && subSel.FromClause != nil {
+ var starResults []base.QuerySpanResult
+ for _, fromItem := range subSel.FromClause.Items {
+ starResults = append(starResults, e.extractColumnsFromFromItem(fromItem, analyzer, fromTables)...)
+ }
+ if len(starResults) > 0 {
+ return starResults
+ }
+ }
}
+ return nil
+ case *ast.JoinExpr:
+ var results []base.QuerySpanResult
+ results = append(results, e.extractColumnsFromFromItem(v.Larg, analyzer, fromTables)...)
+ results = append(results, e.extractColumnsFromFromItem(v.Rarg, analyzer, fromTables)...)
+ return results
default:
- // Other node types don't need fallback.
+ return nil
}
- return false
}
-// queryNeedsFallback checks if the analyzed query contains patterns that
-// omni may not handle correctly and should fall back to ANTLR.
-// This includes: function table sources, function calls in expressions
-// (which may be UDFs needing body analysis), and nil expressions
-// (indicating omni didn't fully resolve something).
-func queryNeedsFallback(q *catalog.Query) bool {
- for _, rte := range q.RangeTable {
- if rte.Kind == catalog.RTEFunction {
- return true
+// extractColumnsFromRangeVar looks up a table in the catalog and returns its columns with lineage.
+// If the table name matches a CTE in fallbackCTEMap, it extracts columns from the CTE definition.
+func (e *omniQuerySpanExtractor) extractColumnsFromRangeVar(rv *ast.RangeVar) []base.QuerySpanResult {
+ // Check if this is a CTE reference.
+ if rv.Schemaname == "" && e.fallbackCTEMap != nil {
+ if cteSel, ok := e.fallbackCTEMap[strings.ToLower(rv.Relname)]; ok {
+ return e.extractColumnsFromCTE(rv.Relname, cteSel)
}
}
- for _, te := range q.TargetList {
- if te.ResJunk {
+
+ schema := rv.Schemaname
+ if schema == "" && len(e.searchPath) > 0 {
+ schema = e.searchPath[0]
+ }
+ rel := e.cat.GetRelation(schema, rv.Relname)
+ if rel == nil {
+ return nil
+ }
+ var results []base.QuerySpanResult
+ for _, col := range rel.Columns {
+ colSet := make(base.SourceColumnSet)
+ colSet[base.ColumnResource{
+ Database: e.defaultDatabase,
+ Schema: rel.Schema.Name,
+ Table: rel.Name,
+ Column: col.Name,
+ }] = true
+ results = append(results, base.QuerySpanResult{
+ Name: col.Name,
+ SourceColumns: colSet,
+ IsPlainField: true,
+ })
+ }
+ return results
+}
+
+// extractColumnsFromCTE extracts columns from a CTE definition.
+// For UNION CTEs, it uses the left branch (the non-recursive term) for column names
+// and merges lineage from both branches.
+func (e *omniQuerySpanExtractor) extractColumnsFromCTE(cteName string, cteSel *ast.SelectStmt) []base.QuerySpanResult {
+ if cteSel == nil {
+ return nil
+ }
+
+ // Build a helper analyzer.
+ analyzer := &plpgsqlAnalyzer{
+ extractor: e,
+ scope: newVariableScope(nil),
+ cteMap: e.fallbackCTEMap,
+ }
+
+ // For UNION (recursive or not), use the left branch for column names
+ // and extract lineage from both branches.
+ if cteSel.Larg != nil {
+ leftResults := e.extractCTEBranchColumns(analyzer, cteSel.Larg)
+ rightResults := e.extractCTEBranchColumns(analyzer, cteSel.Rarg)
+
+ // Merge right-branch lineage into left-branch results.
+ for i := range leftResults {
+ if i < len(rightResults) {
+ for k, v := range rightResults[i].SourceColumns {
+ leftResults[i].SourceColumns[k] = v
+ }
+ }
+ }
+
+ // For recursive CTEs, the recursive term references CTE columns
+ // (e.g., cc1*cc2). Track which CTE columns each recursive expression
+ // depends on, then resolve those to source columns from the non-recursive term.
+ if selectReferencesTable(cteSel.Rarg, cteName) {
+ aliases := e.getCTEColumnAliases(cteName)
+ if len(aliases) == 0 {
+ // No explicit aliases — use the column names from leftResults.
+ for _, r := range leftResults {
+ aliases = append(aliases, r.Name)
+ }
+ }
+ e.mergeRecursiveCTELineage(leftResults, cteSel.Rarg, cteName, aliases)
+ }
+
+ // Look up CTE column aliases from the WithClause.
+ if aliases := e.getCTEColumnAliases(cteName); len(aliases) > 0 {
+ for i := range leftResults {
+ if i < len(aliases) {
+ leftResults[i].Name = aliases[i]
+ }
+ }
+ }
+
+ return leftResults
+ }
+
+ // Simple CTE (no UNION) — extract directly.
+ return e.extractCTEBranchColumns(analyzer, cteSel)
+}
+
+// extractCTEBranchColumns extracts column names and lineage from a single CTE branch.
+func (*omniQuerySpanExtractor) extractCTEBranchColumns(analyzer *plpgsqlAnalyzer, sel *ast.SelectStmt) []base.QuerySpanResult {
+ if sel == nil || sel.TargetList == nil {
+ return nil
+ }
+
+ fromTables := analyzer.collectFromTables(sel)
+
+ var results []base.QuerySpanResult
+ for _, item := range sel.TargetList.Items {
+ rt, ok := item.(*ast.ResTarget)
+ if !ok {
continue
}
- if exprNeedsFallback(te.Expr) {
+ name := rt.Name
+ if name == "" {
+ name = figureResTargetName(rt)
+ }
+ colSet := make(base.SourceColumnSet)
+ analyzer.extractColumnRefsFromExpr(rt.Val, fromTables, colSet)
+ results = append(results, base.QuerySpanResult{
+ Name: name,
+ SourceColumns: colSet,
+ })
+ }
+ return results
+}
+
+// getCTEColumnAliases returns the column aliases defined on a CTE (e.g., t1(cc1, cc2, cc3)).
+func (e *omniQuerySpanExtractor) getCTEColumnAliases(cteName string) []string {
+ if e.fallbackCTEAliases == nil {
+ return nil
+ }
+ return e.fallbackCTEAliases[strings.ToLower(cteName)]
+}
+
+// selectReferencesTable checks if a SELECT statement references a table name
+// in its FROM clause. Used to detect recursive CTEs.
+func selectReferencesTable(sel *ast.SelectStmt, tableName string) bool {
+ if sel == nil || sel.FromClause == nil {
+ return false
+ }
+ lower := strings.ToLower(tableName)
+ for _, item := range sel.FromClause.Items {
+ if nodeReferencesTable(item, lower) {
return true
}
}
return false
}
-func exprNeedsFallback(expr catalog.AnalyzedExpr) bool {
- if expr == nil {
- // nil expression means omni didn't resolve it.
- return true
+func nodeReferencesTable(node ast.Node, tableName string) bool {
+ if node == nil {
+ return false
}
- switch v := expr.(type) {
- case *catalog.FuncCallExpr:
- // Any function call may be a UDF needing body analysis.
- return true
- case *catalog.AggExpr:
- // Aggregate calls are handled by omni, but some JSON/custom aggregates
- // may produce incorrect results — skip for now, will revisit.
+ switch v := node.(type) {
+ case *ast.RangeVar:
+ return strings.ToLower(v.Relname) == tableName
+ case *ast.JoinExpr:
+ return nodeReferencesTable(v.Larg, tableName) || nodeReferencesTable(v.Rarg, tableName)
+ default:
return false
- case *catalog.OpExpr:
- return exprNeedsFallback(v.Left) || exprNeedsFallback(v.Right)
- case *catalog.CaseExprQ:
- if exprNeedsFallback(v.Arg) || exprNeedsFallback(v.Default) {
- return true
+ }
+}
+
+// mergeRecursiveCTELineage tracks which CTE columns each expression in the
+// recursive term depends on, then resolves those to source columns from the
+// non-recursive term results.
+func (*omniQuerySpanExtractor) mergeRecursiveCTELineage(results []base.QuerySpanResult, recursiveSel *ast.SelectStmt, _ string, cteColNames []string) {
+ if recursiveSel == nil || recursiveSel.TargetList == nil {
+ return
+ }
+
+ // Build a map of CTE column name → index (for resolving references).
+ cteColIndex := make(map[string]int)
+ for i, name := range cteColNames {
+ cteColIndex[strings.ToLower(name)] = i
+ }
+
+ // For each expression in the recursive term, find which CTE columns it references.
+ for i, item := range recursiveSel.TargetList.Items {
+ if i >= len(results) {
+ break
}
- for _, w := range v.When {
- if exprNeedsFallback(w.Condition) || exprNeedsFallback(w.Result) {
- return true
+ rt, ok := item.(*ast.ResTarget)
+ if !ok {
+ continue
+ }
+
+ // Collect all column references from this expression.
+ refs := collectColumnRefNames(rt.Val)
+
+ // For each referenced column, if it's a CTE column, merge that column's
+ // source set into this result's source set.
+ for _, ref := range refs {
+ if idx, ok := cteColIndex[strings.ToLower(ref)]; ok && idx < len(results) {
+ for k, v := range results[idx].SourceColumns {
+ results[i].SourceColumns[k] = v
+ }
}
}
- case *catalog.CoalesceExprQ:
- for _, arg := range v.Args {
- if exprNeedsFallback(arg) {
- return true
+ }
+}
+
+// collectColumnRefNames extracts all unqualified column reference names from an expression.
+func collectColumnRefNames(node ast.Node) []string {
+ if node == nil {
+ return nil
+ }
+ var names []string
+ switch v := node.(type) {
+ case *ast.ColumnRef:
+ if v.Fields != nil && len(v.Fields.Items) == 1 {
+ if s, ok := v.Fields.Items[0].(*ast.String); ok {
+ names = append(names, s.Str)
}
}
- case *catalog.SubLinkExpr:
- if v.SubQuery != nil {
- return queryNeedsFallback(v.SubQuery)
+ case *ast.FuncCall:
+ if v.Args != nil {
+ for _, arg := range v.Args.Items {
+ names = append(names, collectColumnRefNames(arg)...)
+ }
}
- case *catalog.RelabelExpr:
- return exprNeedsFallback(v.Arg)
- case *catalog.CoerceViaIOExpr:
- return exprNeedsFallback(v.Arg)
+ case *ast.A_Expr:
+ names = append(names, collectColumnRefNames(v.Lexpr)...)
+ names = append(names, collectColumnRefNames(v.Rexpr)...)
+ case *ast.TypeCast:
+ names = append(names, collectColumnRefNames(v.Arg)...)
default:
- // Other expression types don't need fallback.
}
- return false
+ return names
+}
+
+// collectCTEAliases extracts CTE column aliases from the WITH clause.
+func collectCTEAliases(selStmt *ast.SelectStmt) map[string][]string {
+ if selStmt == nil || selStmt.WithClause == nil {
+ return nil
+ }
+
+ aliasMap := make(map[string][]string)
+ for _, item := range selStmt.WithClause.Ctes.Items {
+ cte, ok := item.(*ast.CommonTableExpr)
+ if !ok {
+ continue
+ }
+ if cte.Aliascolnames == nil || len(cte.Aliascolnames.Items) == 0 {
+ continue
+ }
+ var aliases []string
+ for _, nameItem := range cte.Aliascolnames.Items {
+ if s, ok := nameItem.(*ast.String); ok {
+ aliases = append(aliases, s.Str)
+ }
+ }
+ if len(aliases) > 0 {
+ aliasMap[strings.ToLower(cte.Ctename)] = aliases
+ }
+ }
+ return aliasMap
+}
+
+// extractColumnsFromRangeFunction extracts columns from a function in FROM clause.
+// It first tries to look up user-defined functions in the catalog for lineage tracing.
+// analyzer and fromTables are optional and used for resolving function argument column refs.
+func (e *omniQuerySpanExtractor) extractColumnsFromRangeFunction(rf *ast.RangeFunction, analyzer *plpgsqlAnalyzer, fromTables map[string]base.ColumnResource) []base.QuerySpanResult {
+ // Try to resolve user-defined function for lineage tracing.
+ funcName := extractFuncNameFromRange(rf)
+ if funcName != "" && e.cat != nil {
+ // First try catalog lookup.
+ if proc := e.lookupUserProcByName(funcName); proc != nil {
+ outNames := getOutputParamNames(proc)
+ if len(outNames) > 0 {
+ bodySets := e.analyzeFunctionBody(proc)
+ var results []base.QuerySpanResult
+ for i, name := range outNames {
+ colSet := make(base.SourceColumnSet)
+ if i < len(bodySets) {
+ for k, v := range bodySets[i] {
+ colSet[k] = v
+ }
+ }
+ results = append(results, base.QuerySpanResult{
+ Name: name,
+ SourceColumns: colSet,
+ })
+ }
+ return results
+ }
+ }
+ // Catalog lookup failed — try metadata (for RETURNS TABLE functions).
+ dummyAccesses := make(base.SourceColumnSet)
+ if metaResults := e.tryMetadataFuncLookup(funcName, dummyAccesses); metaResults != nil {
+ return metaResults
+ }
+ }
+
+ if rf.Alias != nil && rf.Alias.Colnames != nil && len(rf.Alias.Colnames.Items) > 0 {
+ // Even with alias column names, resolve function arg column refs for lineage.
+ results := extractColnamesFromAlias(rf.Alias)
+ e.enrichFuncResultsWithArgLineage(rf, analyzer, fromTables, results)
+ return results
+ }
+ results := extractFuncColumnNames(rf, rf.Alias)
+ e.enrichFuncResultsWithArgLineage(rf, analyzer, fromTables, results)
+ return results
+}
+
+// enrichFuncResultsWithArgLineage resolves column references from function arguments
+// and adds them as source columns to all function result columns.
+// For built-in functions like jsonb_each(a), the output depends on the input column a.
+func (*omniQuerySpanExtractor) enrichFuncResultsWithArgLineage(rf *ast.RangeFunction, analyzer *plpgsqlAnalyzer, fromTables map[string]base.ColumnResource, results []base.QuerySpanResult) {
+ if analyzer == nil || fromTables == nil || rf.Functions == nil || len(results) == 0 {
+ return
+ }
+
+ // Collect source columns from all function arguments.
+ argSources := make(base.SourceColumnSet)
+ for _, funcItem := range rf.Functions.Items {
+ funcList, ok := funcItem.(*ast.List)
+ if !ok || len(funcList.Items) < 1 {
+ continue
+ }
+ fc, ok := funcList.Items[0].(*ast.FuncCall)
+ if !ok || fc.Args == nil {
+ continue
+ }
+ for _, arg := range fc.Args.Items {
+ analyzer.extractColumnRefsFromExpr(arg, fromTables, argSources)
+ }
+ }
+
+ // Apply argument source columns to all result columns.
+ for i := range results {
+ for k, v := range argSources {
+ if results[i].SourceColumns == nil {
+ results[i].SourceColumns = make(base.SourceColumnSet)
+ }
+ results[i].SourceColumns[k] = v
+ }
+ }
+}
+
+// extractColnamesFromAlias extracts column names from an alias's column name list.
+func extractColnamesFromAlias(alias *ast.Alias) []base.QuerySpanResult {
+ if alias == nil || alias.Colnames == nil {
+ return nil
+ }
+ var results []base.QuerySpanResult
+ for _, item := range alias.Colnames.Items {
+ if s, ok := item.(*ast.String); ok {
+ results = append(results, base.QuerySpanResult{
+ Name: s.Str,
+ SourceColumns: base.SourceColumnSet{},
+ })
+ }
+ }
+ return results
+}
+
+// extractFuncColumnNames extracts column names from a RangeFunction.
+// For functions like unnest(ARRAY, ARRAY), each argument produces a column
+// named after the function.
+func extractFuncColumnNames(rf *ast.RangeFunction, _ *ast.Alias) []base.QuerySpanResult {
+ if rf.Functions == nil {
+ return nil
+ }
+
+ var results []base.QuerySpanResult
+ for _, funcItem := range rf.Functions.Items {
+ funcList, ok := funcItem.(*ast.List)
+ if !ok || len(funcList.Items) < 1 {
+ continue
+ }
+ fc, ok := funcList.Items[0].(*ast.FuncCall)
+ if !ok || fc.Funcname == nil {
+ continue
+ }
+ // Get function name.
+ funcName := ""
+ for _, nameItem := range fc.Funcname.Items {
+ if s, ok := nameItem.(*ast.String); ok {
+ funcName = s.Str
+ }
+ }
+ if funcName == "" {
+ continue
+ }
+ // Determine number of columns: for set-returning functions like unnest,
+ // each argument produces a column.
+ nCols := 1
+ if fc.Args != nil && len(fc.Args.Items) > 1 {
+ nCols = len(fc.Args.Items)
+ }
+ for range nCols {
+ results = append(results, base.QuerySpanResult{
+ Name: funcName,
+ SourceColumns: base.SourceColumnSet{},
+ })
+ }
+ }
+ return results
+}
+
+// tryUserFuncTableSource handles the case where AnalyzeSelectStmt fails because
+// the FROM clause contains a user-defined RETURNS TABLE function. The omni catalog
+// cannot resolve these as table sources, so we look up the function manually,
+// analyze its body for lineage, and construct the result.
+// Returns nil if this case doesn't apply.
+func (e *omniQuerySpanExtractor) tryUserFuncTableSource(selStmt *ast.SelectStmt, accessesMap base.SourceColumnSet) []base.QuerySpanResult {
+ if selStmt == nil || selStmt.FromClause == nil {
+ return nil
+ }
+
+ // Look for a single RangeFunction in the FROM clause.
+ for _, item := range selStmt.FromClause.Items {
+ rf, ok := item.(*ast.RangeFunction)
+ if !ok || rf.Functions == nil {
+ continue
+ }
+
+ // Extract function name from the RangeFunction.
+ funcName := extractFuncNameFromRange(rf)
+ if funcName == "" {
+ continue
+ }
+
+ // Look up the function in the catalog.
+ proc := e.lookupUserProcByName(funcName)
+ if proc != nil {
+ // Found in catalog — use it for lineage analysis.
+ outNames := getOutputParamNames(proc)
+ if len(outNames) == 0 {
+ continue
+ }
+ bodySets := e.analyzeFunctionBody(proc)
+ return e.buildFuncResults(outNames, bodySets, accessesMap)
+ }
+
+ // Catalog lookup failed — try metadata. This handles cases where
+ // the metadata function name differs from the name in the DDL definition.
+ if results := e.tryMetadataFuncLookup(funcName, accessesMap); results != nil {
+ return results
+ }
+ }
+
+ return nil
+}
+
+// extractFuncNameFromRange extracts the function name from a RangeFunction node.
+func extractFuncNameFromRange(rf *ast.RangeFunction) string {
+ if rf.Functions == nil {
+ return ""
+ }
+ for _, funcItem := range rf.Functions.Items {
+ funcList, ok := funcItem.(*ast.List)
+ if !ok || len(funcList.Items) < 1 {
+ continue
+ }
+ fc, ok := funcList.Items[0].(*ast.FuncCall)
+ if !ok || fc.Funcname == nil {
+ continue
+ }
+ // Use the last name part — for "schema.func", return "func".
+ name := ""
+ for _, nameItem := range fc.Funcname.Items {
+ if s, ok := nameItem.(*ast.String); ok {
+ name = s.Str
+ }
+ }
+ return name
+ }
+ return ""
+}
+
+// lookupUserProcByName finds a user-defined function by name in the catalog,
+// searching through schemas in the search path.
+func (e *omniQuerySpanExtractor) lookupUserProcByName(name string) *catalog.UserProc {
+ lowerName := strings.ToLower(name)
+ for _, schemaName := range e.searchPath {
+ for _, row := range e.cat.QueryPgProc(schemaName) {
+ if strings.ToLower(row.ProName) == lowerName {
+ return e.cat.GetUserProcByOID(row.OID)
+ }
+ }
+ }
+ return nil
+}
+
+// getOutputParamNames returns the names of output parameters (TABLE, OUT, or INOUT mode)
+// for a user-defined function. Returns nil if the function has no output params.
+func getOutputParamNames(proc *catalog.UserProc) []string {
+ var names []string
+ for i, mode := range proc.ArgModes {
+ if mode == 't' || mode == 'o' || mode == 'b' {
+ if i < len(proc.ArgNames) {
+ names = append(names, proc.ArgNames[i])
+ }
+ }
+ }
+ return names
+}
+
+// buildFuncResults constructs QuerySpanResults from output parameter names and
+// function body lineage analysis, and populates table-level access in accessesMap.
+func (*omniQuerySpanExtractor) buildFuncResults(outNames []string, bodySets []base.SourceColumnSet, accessesMap base.SourceColumnSet) []base.QuerySpanResult {
+ for _, colSet := range bodySets {
+ for k, v := range colSet {
+ accessesMap[base.ColumnResource{
+ Database: k.Database,
+ Schema: k.Schema,
+ Table: k.Table,
+ }] = v
+ }
+ }
+
+ var results []base.QuerySpanResult
+ for i, name := range outNames {
+ sourceColumns := make(base.SourceColumnSet)
+ if i < len(bodySets) {
+ for k, v := range bodySets[i] {
+ sourceColumns[k] = v
+ }
+ }
+ results = append(results, base.QuerySpanResult{
+ Name: name,
+ SourceColumns: sourceColumns,
+ })
+ }
+ return results
+}
+
+// tryMetadataFuncLookup searches for a function by name in the database metadata
+// (not the catalog). This handles cases where the catalog can't resolve the
+// function — either because the metadata function name differs from the DDL name,
+// or because RETURNS TABLE functions can't be loaded into the catalog.
+func (e *omniQuerySpanExtractor) tryMetadataFuncLookup(funcName string, accessesMap base.SourceColumnSet) []base.QuerySpanResult {
+ meta, err := e.getDatabaseMetadata(e.defaultDatabase)
+ if err != nil || meta == nil {
+ return nil
+ }
+
+ _, funcs := meta.SearchFunctions(e.searchPath, funcName)
+ if len(funcs) == 0 {
+ return nil
+ }
+
+ // Use the first matching function's definition.
+ funcDef := funcs[0].Definition
+ if funcDef == "" {
+ return nil
+ }
+
+ // Parse the function definition to extract output param names and body.
+ // We can't rely on the catalog for RETURNS TABLE functions because the
+ // catalog may reject them due to type validation.
+ outNames, body, lang := parseFuncDefForLineage(funcDef)
+ if len(outNames) == 0 || body == "" {
+ return nil
+ }
+
+ // Construct a synthetic UserProc for body analysis.
+ syntheticProc := &catalog.UserProc{
+ Name: funcName,
+ Language: lang,
+ Body: body,
+ }
+ // Set ArgModes and ArgNames for output params.
+ for _, name := range outNames {
+ syntheticProc.ArgModes = append(syntheticProc.ArgModes, 't')
+ syntheticProc.ArgNames = append(syntheticProc.ArgNames, name)
+ }
+ // Also extract input params for variable substitution.
+ inputNames := parseInputParamNames(funcDef)
+ for _, name := range inputNames {
+ syntheticProc.ArgModes = append([]byte{'i'}, syntheticProc.ArgModes...)
+ syntheticProc.ArgNames = append([]string{name}, syntheticProc.ArgNames...)
+ }
+
+ // Use a temporary OID for caching.
+ syntheticProc.OID = uint32(0xFFFF0000 + len(e.funcBodyCache))
+
+ bodySets := e.analyzeFunctionBody(syntheticProc)
+ return e.buildFuncResults(outNames, bodySets, accessesMap)
}
-// fallbackToANTLR uses the legacy ANTLR-based extractor when omni analysis fails.
-func (e *omniQuerySpanExtractor) fallbackToANTLR(ctx context.Context, stmt string) (*base.QuerySpan, error) {
- legacy := newQuerySpanExtractor(e.defaultDatabase, e.searchPath, e.gCtx)
- return legacy.getQuerySpan(ctx, stmt)
+// parseFuncDefForLineage extracts output parameter names, function body, and
+// language from a CREATE FUNCTION definition string.
+func parseFuncDefForLineage(definition string) (outNames []string, body string, lang string) {
+ upper := strings.ToUpper(definition)
+
+ // Extract language.
+ if langIdx := strings.Index(upper, "LANGUAGE"); langIdx >= 0 {
+ rest := strings.TrimSpace(definition[langIdx+8:])
+ parts := strings.Fields(rest)
+ if len(parts) > 0 {
+ lang = strings.ToLower(strings.TrimRight(parts[0], ";"))
+ }
+ }
+
+ // Extract body.
+ body = extractFuncBodyFromDef(definition)
+
+ // Extract output param names from RETURNS TABLE(name type, ...).
+ tableIdx := strings.Index(upper, "RETURNS TABLE")
+ if tableIdx < 0 {
+ tableIdx = strings.Index(upper, "RETURNS\n TABLE")
+ }
+ if tableIdx >= 0 {
+ rest := definition[tableIdx:]
+ parenStart := strings.Index(rest, "(")
+ if parenStart >= 0 {
+ depth := 0
+ parenEnd := -1
+ for i := parenStart; i < len(rest); i++ {
+ switch rest[i] {
+ case '(':
+ depth++
+ case ')':
+ depth--
+ if depth == 0 {
+ parenEnd = i
+ }
+ default:
+ }
+ if parenEnd >= 0 {
+ break
+ }
+ }
+ if parenEnd > parenStart {
+ paramList := rest[parenStart+1 : parenEnd]
+ for _, param := range strings.Split(paramList, ",") {
+ parts := strings.Fields(strings.TrimSpace(param))
+ if len(parts) >= 2 {
+ outNames = append(outNames, parts[0])
+ }
+ }
+ }
+ }
+ }
+
+ return outNames, body, lang
+}
+
+// parseInputParamNames extracts input parameter names from a CREATE FUNCTION
+// definition. It parses the parameter list before RETURNS.
+func parseInputParamNames(definition string) []string {
+ upper := strings.ToUpper(definition)
+
+ // Find the parameter list: between FUNCTION name( and ) RETURNS.
+ funcIdx := strings.Index(upper, "FUNCTION")
+ if funcIdx < 0 {
+ return nil
+ }
+
+ rest := definition[funcIdx:]
+ parenStart := strings.Index(rest, "(")
+ if parenStart < 0 {
+ return nil
+ }
+
+ // Find the closing paren that ends the input params.
+ returnsIdx := strings.Index(upper[funcIdx+parenStart:], "RETURNS")
+ if returnsIdx < 0 {
+ return nil
+ }
+
+ paramSection := rest[parenStart+1 : parenStart+returnsIdx]
+ // Find the last ) before RETURNS.
+ lastParen := strings.LastIndex(paramSection, ")")
+ if lastParen >= 0 {
+ paramSection = paramSection[:lastParen]
+ }
+
+ modeKeywords := map[string]bool{
+ "IN": true, "OUT": true, "INOUT": true, "VARIADIC": true,
+ }
+ var names []string
+ for _, param := range strings.Split(paramSection, ",") {
+ parts := strings.Fields(strings.TrimSpace(param))
+ if len(parts) < 2 {
+ continue
+ }
+ // If the first word is a mode keyword (IN, OUT, INOUT, VARIADIC),
+ // the parameter name is the second word. Otherwise the first word is the name.
+ nameIdx := 0
+ if modeKeywords[strings.ToUpper(parts[0])] {
+ nameIdx = 1
+ }
+ if nameIdx < len(parts) {
+ // Skip OUT/INOUT params — we only want input params for variable scope.
+ upperFirst := strings.ToUpper(parts[0])
+ if upperFirst == "OUT" {
+ continue
+ }
+ names = append(names, parts[nameIdx])
+ }
+ }
+ return names
}
diff --git a/backend/plugin/parser/pg/query_span_omni_plpgsql.go b/backend/plugin/parser/pg/query_span_omni_plpgsql.go
new file mode 100644
index 0000000000..4411081745
--- /dev/null
+++ b/backend/plugin/parser/pg/query_span_omni_plpgsql.go
@@ -0,0 +1,1055 @@
+package pg
+
+import (
+ "strings"
+
+ "github.com/pkg/errors"
+
+ "github.com/bytebase/omni/pg/ast"
+ "github.com/bytebase/omni/pg/catalog"
+ plpgsqlast "github.com/bytebase/omni/pg/plpgsql/ast"
+ plpgsqlparser "github.com/bytebase/omni/pg/plpgsql/parser"
+
+ "github.com/bytebase/bytebase/backend/plugin/parser/base"
+)
+
+// analyzeFunctionBody analyzes a user-defined function body and returns per-column
+// source column sets. Results are cached by function OID on the extractor.
+// A nil value in the cache means analysis is in progress (recursion sentinel).
+// On parse errors, it degrades gracefully by returning empty sets.
+func (e *omniQuerySpanExtractor) analyzeFunctionBody(proc *catalog.UserProc) []base.SourceColumnSet {
+ // Check cache.
+ if cached, ok := e.funcBodyCache[proc.OID]; ok {
+ if cached == nil {
+ // In-progress sentinel — recursive call. Return empty sets.
+ return makeEmptySets(proc)
+ }
+ return cached
+ }
+
+ // Store nil sentinel to detect recursion.
+ e.funcBodyCache[proc.OID] = nil
+
+ // If the function body was stubbed during catalog loading (to avoid type
+ // validation errors), use the original body from metadata instead.
+ body := proc.Body
+ if origBody, ok := e.funcOrigDefs[strings.ToLower(proc.Name)]; ok && origBody != "" {
+ body = origBody
+ }
+
+ var result []base.SourceColumnSet
+ var err error
+
+ switch strings.ToLower(proc.Language) {
+ case "plpgsql":
+ result, err = e.analyzePLpgSQLBody(proc, body)
+ case "sql":
+ result, err = e.analyzeSQLBody(body)
+ default:
+ // Unsupported language (C, internal, etc.) — unknown lineage.
+ result = makeEmptySets(proc)
+ }
+
+ if err != nil {
+ // On error, cache empty sets so we don't retry.
+ e.funcBodyCache[proc.OID] = makeEmptySets(proc)
+ return e.funcBodyCache[proc.OID]
+ }
+
+ e.funcBodyCache[proc.OID] = result
+ return result
+}
+
+func makeEmptySets(proc *catalog.UserProc) []base.SourceColumnSet {
+ n := countOutputParams(proc)
+ if n == 0 {
+ n = 1 // scalar return
+ }
+ sets := make([]base.SourceColumnSet, n)
+ for i := range sets {
+ sets[i] = make(base.SourceColumnSet)
+ }
+ return sets
+}
+
+func countOutputParams(proc *catalog.UserProc) int {
+ count := 0
+ for _, mode := range proc.ArgModes {
+ if mode == 'o' || mode == 't' || mode == 'b' {
+ count++
+ }
+ }
+ return count
+}
+
+func (e *omniQuerySpanExtractor) analyzeSQLBody(body string) ([]base.SourceColumnSet, error) {
+ if body == "" {
+ return nil, nil
+ }
+
+ stmts, err := ParsePg(body)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to parse SQL function body")
+ }
+
+ // Find the last SELECT statement (SQL functions return the result of the last statement).
+ for i := len(stmts) - 1; i >= 0; i-- {
+ selStmt, ok := stmts[i].AST.(*ast.SelectStmt)
+ if !ok {
+ continue
+ }
+ query, err := e.cat.AnalyzeSelectStmt(selStmt)
+ if err != nil {
+ // Fall back to parse-tree extraction when analysis fails
+ // (e.g., nested function calls, parameter references).
+ fallbackResults := e.extractFallbackColumns(selStmt)
+ if len(fallbackResults) > 0 {
+ result := make([]base.SourceColumnSet, len(fallbackResults))
+ for j, fr := range fallbackResults {
+ result[j] = fr.SourceColumns
+ }
+ return result, nil
+ }
+ return nil, errors.Wrapf(err, "failed to analyze SQL function body")
+ }
+ return e.extractFuncLineage(query), nil
+ }
+
+ return nil, nil
+}
+
+func (e *omniQuerySpanExtractor) extractFuncLineage(q *catalog.Query) []base.SourceColumnSet {
+ var result []base.SourceColumnSet
+ for _, te := range q.TargetList {
+ if te.ResJunk {
+ continue
+ }
+ colSet := make(base.SourceColumnSet)
+ e.walkExpr(q, te.Expr, colSet)
+ result = append(result, colSet)
+ }
+ return result
+}
+
+// variableScope tracks PL/pgSQL variable source columns with nested scoping.
+type variableScope struct {
+ vars map[string]base.SourceColumnSet
+ parent *variableScope
+}
+
+func newVariableScope(parent *variableScope) *variableScope {
+ return &variableScope{
+ vars: make(map[string]base.SourceColumnSet),
+ parent: parent,
+ }
+}
+
+func (s *variableScope) set(name string, sources base.SourceColumnSet) {
+ s.vars[strings.ToLower(name)] = sources
+}
+
+func (s *variableScope) get(name string) (base.SourceColumnSet, bool) {
+ lower := strings.ToLower(name)
+ for scope := s; scope != nil; scope = scope.parent {
+ if v, ok := scope.vars[lower]; ok {
+ return v, true
+ }
+ }
+ return nil, false
+}
+
+func (s *variableScope) merge(name string, sources base.SourceColumnSet) {
+ lower := strings.ToLower(name)
+ existing, ok := s.get(lower)
+ if !ok {
+ existing = make(base.SourceColumnSet)
+ }
+ merged := make(base.SourceColumnSet)
+ for k, v := range existing {
+ merged[k] = v
+ }
+ for k, v := range sources {
+ merged[k] = v
+ }
+ s.vars[lower] = merged
+}
+
+// plpgsqlAnalyzer holds state for analyzing a single PL/pgSQL function body.
+type plpgsqlAnalyzer struct {
+ extractor *omniQuerySpanExtractor
+ scope *variableScope
+ // returnResults collects per-column source sets from all RETURN QUERY paths.
+ returnResults [][]base.SourceColumnSet
+ // cteMap holds CTE definitions for resolving column refs through CTEs.
+ cteMap map[string]*ast.SelectStmt
+}
+
+func (e *omniQuerySpanExtractor) analyzePLpgSQLBody(proc *catalog.UserProc, body string) ([]base.SourceColumnSet, error) {
+ if body == "" {
+ return makeEmptySets(proc), nil
+ }
+
+ block, err := plpgsqlparser.Parse(body)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to parse PL/pgSQL function body")
+ }
+
+ analyzer := &plpgsqlAnalyzer{
+ extractor: e,
+ scope: newVariableScope(nil),
+ }
+
+ // Add function input parameters to scope so they can be substituted in
+ // embedded SQL. Output/table params ('o', 't') are NOT added because their
+ // names often collide with column names in the function body's queries
+ // (e.g., RETURNS TABLE(a int, b int) where a, b are also column names).
+ for i, name := range proc.ArgNames {
+ if name == "" {
+ continue
+ }
+ if i < len(proc.ArgModes) {
+ mode := proc.ArgModes[i]
+ if mode == 'o' || mode == 't' {
+ continue
+ }
+ }
+ analyzer.scope.set(name, make(base.SourceColumnSet))
+ }
+
+ analyzer.walkBlock(block)
+
+ nCols := countOutputParams(proc)
+ if nCols == 0 {
+ nCols = 1
+ }
+
+ return analyzer.mergeReturnResults(nCols), nil
+}
+
+func (a *plpgsqlAnalyzer) walkBlock(block *plpgsqlast.PLBlock) {
+ // Push scope for declarations.
+ a.scope = newVariableScope(a.scope)
+ defer func() { a.scope = a.scope.parent }()
+
+ // Process declarations.
+ for _, decl := range block.Declarations {
+ switch d := decl.(type) {
+ case *plpgsqlast.PLDeclare:
+ a.scope.set(d.Name, make(base.SourceColumnSet))
+ case *plpgsqlast.PLCursorDecl:
+ a.scope.set(d.Name, make(base.SourceColumnSet))
+ case *plpgsqlast.PLAliasDecl:
+ // Alias references another variable — copy its sources.
+ if sources, ok := a.scope.get(d.RefName); ok {
+ a.scope.set(d.Name, sources)
+ } else {
+ a.scope.set(d.Name, make(base.SourceColumnSet))
+ }
+ default:
+ }
+ }
+
+ // Process body statements.
+ for _, stmt := range block.Body {
+ a.walkStmt(stmt)
+ }
+
+ // Process exception handlers.
+ for _, ex := range block.Exceptions {
+ if ew, ok := ex.(*plpgsqlast.PLExceptionWhen); ok {
+ for _, stmt := range ew.Body {
+ a.walkStmt(stmt)
+ }
+ }
+ }
+}
+
+func (a *plpgsqlAnalyzer) walkStmt(node plpgsqlast.Node) {
+ switch stmt := node.(type) {
+ case *plpgsqlast.PLAssign:
+ a.handleAssign(stmt)
+ case *plpgsqlast.PLReturnQuery:
+ a.handleReturnQuery(stmt)
+ case *plpgsqlast.PLReturn:
+ a.handleReturn(stmt)
+ case *plpgsqlast.PLReturnNext:
+ a.handleReturnNext(stmt)
+ case *plpgsqlast.PLExecSQL:
+ a.handleExecSQL(stmt)
+ case *plpgsqlast.PLIf:
+ a.handleIf(stmt)
+ case *plpgsqlast.PLCase:
+ a.handleCase(stmt)
+ case *plpgsqlast.PLLoop:
+ for _, s := range stmt.Body {
+ a.walkStmt(s)
+ }
+ case *plpgsqlast.PLWhile:
+ for _, s := range stmt.Body {
+ a.walkStmt(s)
+ }
+ case *plpgsqlast.PLForI:
+ for _, s := range stmt.Body {
+ a.walkStmt(s)
+ }
+ case *plpgsqlast.PLForS:
+ a.handleForS(stmt)
+ case *plpgsqlast.PLForC:
+ for _, s := range stmt.Body {
+ a.walkStmt(s)
+ }
+ case *plpgsqlast.PLForDynS:
+ for _, s := range stmt.Body {
+ a.walkStmt(s)
+ }
+ case *plpgsqlast.PLForEachA:
+ for _, s := range stmt.Body {
+ a.walkStmt(s)
+ }
+ case *plpgsqlast.PLBlock:
+ a.walkBlock(stmt)
+ // Skip nodes that don't affect lineage:
+ // PLDynExecute, PLRaise, PLNull, PLCommit, PLRollback, PLPerform,
+ // PLCall, PLOpen, PLFetch, PLClose, PLGetDiag, PLAssert, PLExit
+ default:
+ }
+}
+
+func (a *plpgsqlAnalyzer) handleAssign(stmt *plpgsqlast.PLAssign) {
+ varName := extractBaseVarName(stmt.Target)
+ sources := a.analyzeEmbeddedExpr(stmt.Expr)
+ a.scope.merge(varName, sources)
+}
+
+func (a *plpgsqlAnalyzer) handleReturnQuery(stmt *plpgsqlast.PLReturnQuery) {
+ if stmt.Query == "" {
+ return
+ }
+ results := a.analyzeEmbeddedSQL(stmt.Query)
+ if len(results) > 0 {
+ a.returnResults = append(a.returnResults, results)
+ }
+}
+
+func (a *plpgsqlAnalyzer) handleReturn(stmt *plpgsqlast.PLReturn) {
+ if stmt.Expr == "" {
+ return
+ }
+ sources := a.analyzeEmbeddedExpr(stmt.Expr)
+ a.returnResults = append(a.returnResults, []base.SourceColumnSet{sources})
+}
+
+func (a *plpgsqlAnalyzer) handleReturnNext(stmt *plpgsqlast.PLReturnNext) {
+ if stmt.Expr == "" {
+ return
+ }
+ sources := a.analyzeEmbeddedExpr(stmt.Expr)
+ a.returnResults = append(a.returnResults, []base.SourceColumnSet{sources})
+}
+
+func (a *plpgsqlAnalyzer) handleExecSQL(stmt *plpgsqlast.PLExecSQL) {
+ if len(stmt.Into) == 0 {
+ return
+ }
+ results := a.analyzeEmbeddedSQL(stmt.SQLText)
+ for i, varName := range stmt.Into {
+ if i < len(results) {
+ a.scope.merge(varName, results[i])
+ }
+ }
+}
+
+func (a *plpgsqlAnalyzer) handleForS(stmt *plpgsqlast.PLForS) {
+ results := a.analyzeEmbeddedSQL(stmt.Query)
+ merged := make(base.SourceColumnSet)
+ for _, colSet := range results {
+ for k, v := range colSet {
+ merged[k] = v
+ }
+ }
+ a.scope.merge(stmt.Var, merged)
+ for _, s := range stmt.Body {
+ a.walkStmt(s)
+ }
+}
+
+func (a *plpgsqlAnalyzer) handleIf(stmt *plpgsqlast.PLIf) {
+ for _, s := range stmt.ThenBody {
+ a.walkStmt(s)
+ }
+ for _, elsif := range stmt.ElsIfs {
+ if ei, ok := elsif.(*plpgsqlast.PLElsIf); ok {
+ for _, s := range ei.Body {
+ a.walkStmt(s)
+ }
+ }
+ }
+ for _, s := range stmt.ElseBody {
+ a.walkStmt(s)
+ }
+}
+
+func (a *plpgsqlAnalyzer) handleCase(stmt *plpgsqlast.PLCase) {
+ for _, when := range stmt.Whens {
+ if cw, ok := when.(*plpgsqlast.PLCaseWhen); ok {
+ for _, s := range cw.Body {
+ a.walkStmt(s)
+ }
+ }
+ }
+ for _, s := range stmt.ElseBody {
+ a.walkStmt(s)
+ }
+}
+
+func (a *plpgsqlAnalyzer) analyzeEmbeddedSQL(sql string) []base.SourceColumnSet {
+ substituted := a.substituteVariables(sql)
+
+ // Always collect access tables from embedded SQL for the top-level QuerySpan,
+ // regardless of whether full analysis succeeds.
+ a.collectAccessTablesFromSQL(substituted)
+
+ stmts, err := ParsePg(substituted)
+ if err != nil {
+ return nil
+ }
+ if len(stmts) == 0 {
+ return nil
+ }
+
+ selStmt, ok := stmts[0].AST.(*ast.SelectStmt)
+ if !ok {
+ return nil
+ }
+
+ query, err := a.extractor.cat.AnalyzeSelectStmt(selStmt)
+ if err != nil {
+ // When full analysis fails (e.g., type mismatches with aggregate functions),
+ // fall back to extracting column references from the parse tree and resolving
+ // them against known tables in the catalog.
+ return a.fallbackExtractLineage(sql, selStmt)
+ }
+
+ // Also collect from the analyzed query for precise column-level tracking
+ // (WHERE clause → predicate columns).
+ a.collectQueryPredicateColumns(query)
+
+ results := a.extractor.extractFuncLineage(query)
+
+ // Map back variable references.
+ origSelectItems := extractSelectItemNames(sql)
+ for i, colSet := range results {
+ if len(colSet) == 0 && i < len(origSelectItems) {
+ varName := strings.TrimSpace(origSelectItems[i])
+ if sources, ok := a.scope.get(varName); ok {
+ results[i] = sources
+ }
+ }
+ }
+
+ return results
+}
+
+func extractSelectItemNames(sql string) []string {
+ upper := strings.ToUpper(strings.TrimSpace(sql))
+ idx := strings.Index(upper, "SELECT")
+ if idx < 0 {
+ return nil
+ }
+ rest := sql[idx+6:]
+
+ fromIdx := -1
+ depth := 0
+ upperRest := strings.ToUpper(rest)
+ for i := 0; i < len(upperRest); i++ {
+ switch upperRest[i] {
+ case '(':
+ depth++
+ case ')':
+ depth--
+ default:
+ if depth == 0 && i+4 <= len(upperRest) && upperRest[i:i+4] == "FROM" {
+ if (i == 0 || !isWordChar(rest[i-1])) && (i+4 >= len(rest) || !isWordChar(rest[i+4])) {
+ fromIdx = i
+ }
+ }
+ }
+ if fromIdx >= 0 {
+ break
+ }
+ }
+
+ targetList := rest
+ if fromIdx >= 0 {
+ targetList = rest[:fromIdx]
+ }
+
+ var items []string
+ depth = 0
+ start := 0
+ for i := 0; i < len(targetList); i++ {
+ switch targetList[i] {
+ case '(':
+ depth++
+ case ')':
+ depth--
+ case ',':
+ if depth == 0 {
+ items = append(items, strings.TrimSpace(targetList[start:i]))
+ start = i + 1
+ }
+ default:
+ }
+ }
+ items = append(items, strings.TrimSpace(targetList[start:]))
+
+ return items
+}
+
+func (a *plpgsqlAnalyzer) analyzeEmbeddedExpr(expr string) base.SourceColumnSet {
+ results := a.analyzeEmbeddedSQL("SELECT " + expr)
+ merged := make(base.SourceColumnSet)
+ for _, colSet := range results {
+ for k, v := range colSet {
+ merged[k] = v
+ }
+ }
+ if len(merged) == 0 {
+ varName := strings.TrimSpace(expr)
+ if sources, ok := a.scope.get(varName); ok {
+ for k, v := range sources {
+ merged[k] = v
+ }
+ }
+ }
+ return merged
+}
+
+func (a *plpgsqlAnalyzer) substituteVariables(sql string) string {
+ stmts, err := ParsePg(sql)
+ if err == nil && len(stmts) > 0 {
+ if selStmt, ok := stmts[0].AST.(*ast.SelectStmt); ok {
+ if _, err := a.extractor.cat.AnalyzeSelectStmt(selStmt); err == nil {
+ return sql
+ }
+ }
+ }
+
+ result := sql
+ for scope := a.scope; scope != nil; scope = scope.parent {
+ for varName := range scope.vars {
+ result = replaceWholeWord(result, varName, "NULL::text")
+ }
+ }
+
+ return result
+}
+
+func replaceWholeWord(s, old, replacement string) string {
+ lower := strings.ToLower(s)
+ oldLower := strings.ToLower(old)
+ var result strings.Builder
+ i := 0
+ for i < len(lower) {
+ idx := strings.Index(lower[i:], oldLower)
+ if idx < 0 {
+ result.WriteString(s[i:])
+ break
+ }
+ pos := i + idx
+ if pos > 0 && isWordChar(s[pos-1]) {
+ result.WriteString(s[i : pos+len(old)])
+ i = pos + len(old)
+ continue
+ }
+ end := pos + len(old)
+ if end < len(s) && isWordChar(s[end]) {
+ result.WriteString(s[i : pos+len(old)])
+ i = pos + len(old)
+ continue
+ }
+ result.WriteString(s[i:pos])
+ result.WriteString(replacement)
+ i = end
+ }
+ return result.String()
+}
+
+func isWordChar(b byte) bool {
+ return (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || (b >= '0' && b <= '9') || b == '_'
+}
+
+func extractBaseVarName(target string) string {
+ for i, c := range target {
+ if c == '.' || c == '[' {
+ return target[:i]
+ }
+ }
+ return target
+}
+
+// collectAccessTablesFromSQL extracts all table/column references from embedded SQL
+// and adds them to the extractor's funcSourceColumns. This works even when
+// AnalyzeSelectStmt fails, because it uses the AST-level ExtractAccessTables.
+func (a *plpgsqlAnalyzer) collectAccessTablesFromSQL(sql string) {
+ accessTables, err := ExtractAccessTables(sql, ExtractAccessTablesOption{
+ DefaultDatabase: a.extractor.defaultDatabase,
+ DefaultSchema: a.extractor.searchPath[0],
+ GetDatabaseMetadata: a.extractor.gCtx.GetDatabaseMetadataFunc,
+ Ctx: a.extractor.ctx,
+ InstanceID: a.extractor.gCtx.InstanceID,
+ SkipMetadataValidation: false,
+ })
+ if err != nil {
+ return
+ }
+ for _, resource := range accessTables {
+ a.extractor.funcSourceColumns[resource] = true
+ }
+}
+
+// collectQueryPredicateColumns walks WHERE/HAVING clauses in an analyzed query
+// and collects predicate columns (column-level) into funcPredicateColumns.
+// Note: funcSourceColumns only stores table-level access (via collectAccessTablesFromSQL),
+// so we don't add column-level entries to it here.
+func (a *plpgsqlAnalyzer) collectQueryPredicateColumns(q *catalog.Query) {
+ if q == nil {
+ return
+ }
+
+ // Handle set operations recursively.
+ if q.SetOp != catalog.SetOpNone {
+ a.collectQueryPredicateColumns(q.LArg)
+ a.collectQueryPredicateColumns(q.RArg)
+ return
+ }
+
+ // Collect from WHERE clause → predicate columns only.
+ if q.JoinTree != nil && q.JoinTree.Quals != nil {
+ a.extractor.walkExpr(q, q.JoinTree.Quals, a.extractor.funcPredicateColumns)
+ }
+
+ // Collect from HAVING clause → predicate columns only.
+ if q.HavingQual != nil {
+ a.extractor.walkExpr(q, q.HavingQual, a.extractor.funcPredicateColumns)
+ }
+
+ // Recurse into CTEs.
+ for _, cte := range q.CTEList {
+ if cte.Query != nil {
+ a.collectQueryPredicateColumns(cte.Query)
+ }
+ }
+
+ // Recurse into subqueries in FROM clause.
+ for _, rte := range q.RangeTable {
+ if rte.Kind == catalog.RTESubquery && rte.Subquery != nil {
+ a.collectQueryPredicateColumns(rte.Subquery)
+ }
+ }
+}
+
+func (a *plpgsqlAnalyzer) mergeReturnResults(nCols int) []base.SourceColumnSet {
+ result := make([]base.SourceColumnSet, nCols)
+ for i := range result {
+ result[i] = make(base.SourceColumnSet)
+ }
+
+ for _, returnCols := range a.returnResults {
+ for i := 0; i < nCols && i < len(returnCols); i++ {
+ for k, v := range returnCols[i] {
+ result[i][k] = v
+ }
+ }
+ }
+
+ return result
+}
+
+// fallbackExtractLineage is called when AnalyzeSelectStmt fails for embedded SQL
+// (e.g., due to type mismatches with aggregate functions). It uses the parse tree
+// to extract column references and resolves them against known tables and variables.
+func (a *plpgsqlAnalyzer) fallbackExtractLineage(origSQL string, selStmt *ast.SelectStmt) []base.SourceColumnSet {
+ if selStmt == nil || selStmt.TargetList == nil {
+ return nil
+ }
+
+ // Collect table references from the FROM clause.
+ fromTables := a.collectFromTables(selStmt)
+
+ var results []base.SourceColumnSet
+ origItems := extractSelectItemNames(origSQL)
+
+ for i, item := range selStmt.TargetList.Items {
+ rt, ok := item.(*ast.ResTarget)
+ if !ok {
+ results = append(results, make(base.SourceColumnSet))
+ continue
+ }
+
+ colSet := make(base.SourceColumnSet)
+
+ // Try to extract column references from the expression.
+ a.extractColumnRefsFromExpr(rt.Val, fromTables, colSet)
+
+ // If still empty, try the original item name as a variable reference.
+ if len(colSet) == 0 && i < len(origItems) {
+ varName := strings.TrimSpace(origItems[i])
+ // Strip wrapping parentheses for subquery expressions.
+ varName = strings.TrimPrefix(strings.TrimSuffix(varName, ")"), "(")
+ varName = strings.TrimSpace(varName)
+ if sources, ok := a.scope.get(varName); ok {
+ for k, v := range sources {
+ colSet[k] = v
+ }
+ }
+ }
+
+ results = append(results, colSet)
+ }
+
+ // Extract column refs from WHERE clause for predicate tracking.
+ if selStmt.WhereClause != nil {
+ whereColSet := make(base.SourceColumnSet)
+ a.extractColumnRefsFromExpr(selStmt.WhereClause, fromTables, whereColSet)
+ for k, v := range whereColSet {
+ a.extractor.funcPredicateColumns[k] = v
+ }
+ }
+
+ return results
+}
+
+// collectFromTables collects table references from the FROM clause and maps
+// table names/aliases to their schema-qualified names.
+func (a *plpgsqlAnalyzer) collectFromTables(selStmt *ast.SelectStmt) map[string]base.ColumnResource {
+ tables := make(map[string]base.ColumnResource)
+ if selStmt.FromClause == nil {
+ return tables
+ }
+
+ for _, item := range selStmt.FromClause.Items {
+ a.collectTablesFromNode(item, tables)
+ }
+ return tables
+}
+
+func (a *plpgsqlAnalyzer) collectTablesFromNode(node ast.Node, tables map[string]base.ColumnResource) {
+ switch v := node.(type) {
+ case *ast.RangeVar:
+ schema := "public"
+ if v.Schemaname != "" {
+ schema = v.Schemaname
+ }
+ resource := base.ColumnResource{
+ Database: a.extractor.defaultDatabase,
+ Schema: schema,
+ Table: v.Relname,
+ }
+ tables[strings.ToLower(v.Relname)] = resource
+ if v.Alias != nil && v.Alias.Aliasname != "" {
+ tables[strings.ToLower(v.Alias.Aliasname)] = resource
+ }
+ case *ast.RangeSubselect:
+ // For subquery aliases like (SELECT * FROM t) result, collect inner tables
+ // and map them under the alias so column refs can be resolved.
+ if v.Subquery != nil {
+ if subSel, ok := v.Subquery.(*ast.SelectStmt); ok {
+ innerTables := a.collectFromTables(subSel)
+ // Add inner tables to the outer fromTables.
+ for k, res := range innerTables {
+ if _, exists := tables[k]; !exists {
+ tables[k] = res
+ }
+ }
+ // If the subquery has an alias, also map all inner tables'
+ // columns as accessible through the alias.
+ if v.Alias != nil && v.Alias.Aliasname != "" {
+ alias := strings.ToLower(v.Alias.Aliasname)
+ // Use the first inner table as the primary source for column lookup.
+ for _, res := range innerTables {
+ tables[alias] = res
+ break
+ }
+ }
+ }
+ }
+ case *ast.JoinExpr:
+ if v.Larg != nil {
+ a.collectTablesFromNode(v.Larg, tables)
+ }
+ if v.Rarg != nil {
+ a.collectTablesFromNode(v.Rarg, tables)
+ }
+ default:
+ }
+}
+
+// extractColumnRefsFromExpr recursively walks an AST expression and resolves
+// column references against known tables.
+func (a *plpgsqlAnalyzer) extractColumnRefsFromExpr(node ast.Node, fromTables map[string]base.ColumnResource, result base.SourceColumnSet) {
+ if node == nil {
+ return
+ }
+
+ switch v := node.(type) {
+ case *ast.ColumnRef:
+ a.resolveColumnRef(v, fromTables, result)
+ case *ast.FuncCall:
+ if v.Args != nil {
+ for _, arg := range v.Args.Items {
+ a.extractColumnRefsFromExpr(arg, fromTables, result)
+ }
+ }
+ // FILTER (WHERE ...) clause on aggregates → predicate columns.
+ if v.AggFilter != nil {
+ filterColSet := make(base.SourceColumnSet)
+ a.extractColumnRefsFromExpr(v.AggFilter, fromTables, filterColSet)
+ for k, val := range filterColSet {
+ a.extractor.funcPredicateColumns[k] = val
+ }
+ }
+ case *ast.A_Expr:
+ a.extractColumnRefsFromExpr(v.Lexpr, fromTables, result)
+ a.extractColumnRefsFromExpr(v.Rexpr, fromTables, result)
+ case *ast.SubLink:
+ if v.Subselect != nil {
+ if subSel, ok := v.Subselect.(*ast.SelectStmt); ok {
+ subTables := a.collectFromTables(subSel)
+ for k, val := range fromTables {
+ if _, exists := subTables[k]; !exists {
+ subTables[k] = val
+ }
+ }
+ if subSel.TargetList != nil {
+ for _, item := range subSel.TargetList.Items {
+ if rt, ok := item.(*ast.ResTarget); ok {
+ a.extractColumnRefsFromExpr(rt.Val, subTables, result)
+ }
+ }
+ }
+ // Also collect from subquery WHERE clause for predicate tracking.
+ if subSel.WhereClause != nil {
+ whereColSet := make(base.SourceColumnSet)
+ a.extractColumnRefsFromExpr(subSel.WhereClause, subTables, whereColSet)
+ for k, val := range whereColSet {
+ a.extractor.funcPredicateColumns[k] = val
+ }
+ }
+ }
+ }
+ case *ast.TypeCast:
+ a.extractColumnRefsFromExpr(v.Arg, fromTables, result)
+ case *ast.BoolExpr:
+ if v.Args != nil {
+ for _, arg := range v.Args.Items {
+ a.extractColumnRefsFromExpr(arg, fromTables, result)
+ }
+ }
+ case *ast.CaseExpr:
+ a.extractColumnRefsFromExpr(v.Arg, fromTables, result)
+ if v.Args != nil {
+ for _, w := range v.Args.Items {
+ a.extractColumnRefsFromExpr(w, fromTables, result)
+ }
+ }
+ a.extractColumnRefsFromExpr(v.Defresult, fromTables, result)
+ case *ast.CaseWhen:
+ a.extractColumnRefsFromExpr(v.Expr, fromTables, result)
+ a.extractColumnRefsFromExpr(v.Result, fromTables, result)
+ case *ast.CoalesceExpr:
+ if v.Args != nil {
+ for _, arg := range v.Args.Items {
+ a.extractColumnRefsFromExpr(arg, fromTables, result)
+ }
+ }
+ case *ast.NullTest:
+ a.extractColumnRefsFromExpr(v.Arg, fromTables, result)
+ case *ast.BooleanTest:
+ a.extractColumnRefsFromExpr(v.Arg, fromTables, result)
+ case *ast.A_Indirection:
+ // Array subscript or field selection — the source is the base expression.
+ a.extractColumnRefsFromExpr(v.Arg, fromTables, result)
+ if v.Indirection != nil {
+ for _, ind := range v.Indirection.Items {
+ a.extractColumnRefsFromExpr(ind, fromTables, result)
+ }
+ }
+ case *ast.JsonObjectConstructor:
+ if v.Exprs != nil {
+ for _, item := range v.Exprs.Items {
+ if kv, ok := item.(*ast.JsonKeyValue); ok {
+ if kv.Value != nil {
+ a.extractColumnRefsFromExpr(kv.Value.RawExpr, fromTables, result)
+ }
+ }
+ }
+ }
+ case *ast.JsonArrayConstructor:
+ if v.Exprs != nil {
+ for _, item := range v.Exprs.Items {
+ if jve, ok := item.(*ast.JsonValueExpr); ok {
+ a.extractColumnRefsFromExpr(jve.RawExpr, fromTables, result)
+ }
+ }
+ }
+ case *ast.JsonValueExpr:
+ a.extractColumnRefsFromExpr(v.RawExpr, fromTables, result)
+ case *ast.List:
+ for _, item := range v.Items {
+ a.extractColumnRefsFromExpr(item, fromTables, result)
+ }
+ default:
+ }
+}
+
+// resolveColumnRef resolves a ColumnRef to a source column using known tables.
+// If the table is a CTE, it traces through the CTE to find real source columns.
+func (a *plpgsqlAnalyzer) resolveColumnRef(cr *ast.ColumnRef, fromTables map[string]base.ColumnResource, result base.SourceColumnSet) {
+ if cr == nil || cr.Fields == nil || len(cr.Fields.Items) == 0 {
+ return
+ }
+
+ var parts []string
+ for _, f := range cr.Fields.Items {
+ if s, ok := f.(*ast.String); ok {
+ parts = append(parts, s.Str)
+ }
+ }
+
+ switch len(parts) {
+ case 1:
+ colName := parts[0]
+ if _, ok := a.scope.get(colName); ok {
+ return
+ }
+ // Check if it's a column name in any FROM table.
+ for _, resource := range fromTables {
+ rel := a.extractor.cat.GetRelation(resource.Schema, resource.Table)
+ if rel != nil && relationHasColumn(rel, colName) {
+ result[base.ColumnResource{
+ Database: resource.Database,
+ Schema: resource.Schema,
+ Table: resource.Table,
+ Column: colName,
+ }] = true
+ return
+ }
+ }
+ // Check if it's a whole-row reference (table alias used as value, e.g., to_jsonb(t1)).
+ if resource, ok := fromTables[strings.ToLower(colName)]; ok {
+ rel := a.extractor.cat.GetRelation(resource.Schema, resource.Table)
+ if rel != nil {
+ for _, col := range rel.Columns {
+ result[base.ColumnResource{
+ Database: resource.Database,
+ Schema: resource.Schema,
+ Table: resource.Table,
+ Column: col.Name,
+ }] = true
+ }
+ return
+ }
+ }
+ case 2:
+ tableName := strings.ToLower(parts[0])
+ colName := parts[1]
+ if resource, ok := fromTables[tableName]; ok {
+ // Check if this table is actually a CTE — trace through to real sources.
+ // The CTE name is the original table name (resource.Table), not the alias.
+ if a.cteMap != nil {
+ if cteSel, isCTE := a.cteMap[strings.ToLower(resource.Table)]; isCTE {
+ a.resolveThroughCTE(cteSel, colName, fromTables, result)
+ return
+ }
+ }
+ result[base.ColumnResource{
+ Database: resource.Database,
+ Schema: resource.Schema,
+ Table: resource.Table,
+ Column: colName,
+ }] = true
+ }
+ default:
+ }
+}
+
+// resolveThroughCTE traces a column reference through a CTE to find real source columns.
+func (a *plpgsqlAnalyzer) resolveThroughCTE(cteSel *ast.SelectStmt, colName string, _ map[string]base.ColumnResource, result base.SourceColumnSet) {
+ if cteSel == nil || cteSel.TargetList == nil {
+ return
+ }
+
+ // Build FROM tables for the CTE's query.
+ cteTables := a.collectFromTables(cteSel)
+ // Include parent CTEs for nested CTE references.
+ if a.cteMap != nil {
+ for name := range a.cteMap {
+ if _, exists := cteTables[name]; !exists {
+ cteTables[name] = base.ColumnResource{
+ Database: a.extractor.defaultDatabase,
+ Schema: "public",
+ Table: name,
+ }
+ }
+ }
+ }
+
+ // Find the target entry matching the column name.
+ for _, item := range cteSel.TargetList.Items {
+ rt, ok := item.(*ast.ResTarget)
+ if !ok {
+ continue
+ }
+ // Match by alias first, then by expression name.
+ rtName := rt.Name
+ if rtName == "" {
+ rtName = figureResTargetName(rt)
+ }
+ if !strings.EqualFold(rtName, colName) {
+ continue
+ }
+ // Found matching column — extract source refs from its expression.
+ a.extractColumnRefsFromExpr(rt.Val, cteTables, result)
+
+ // Also collect predicate columns from the CTE's WHERE clause.
+ if cteSel.WhereClause != nil {
+ whereColSet := make(base.SourceColumnSet)
+ a.extractColumnRefsFromExpr(cteSel.WhereClause, cteTables, whereColSet)
+ for k, v := range whereColSet {
+ a.extractor.funcPredicateColumns[k] = v
+ }
+ }
+ return
+ }
+}
+
+// collectCTEDefinitions extracts CTE names and their SelectStmt definitions from a query.
+func collectCTEDefinitions(selStmt *ast.SelectStmt) map[string]*ast.SelectStmt {
+ if selStmt == nil || selStmt.WithClause == nil {
+ return nil
+ }
+
+ cteMap := make(map[string]*ast.SelectStmt)
+ for _, item := range selStmt.WithClause.Ctes.Items {
+ cte, ok := item.(*ast.CommonTableExpr)
+ if !ok {
+ continue
+ }
+ if sel, ok := cte.Ctequery.(*ast.SelectStmt); ok {
+ cteMap[strings.ToLower(cte.Ctename)] = sel
+ }
+ }
+ return cteMap
+}
+
+// relationHasColumn checks if a catalog relation has a column with the given name.
+func relationHasColumn(rel *catalog.Relation, colName string) bool {
+ lowerName := strings.ToLower(colName)
+ for _, col := range rel.Columns {
+ if strings.ToLower(col.Name) == lowerName {
+ return true
+ }
+ }
+ return false
+}
diff --git a/backend/plugin/parser/pg/test-data/query_span.yaml b/backend/plugin/parser/pg/test-data/query_span.yaml
index eb5abf3bc9..2ee137f959 100644
--- a/backend/plugin/parser/pg/test-data/query_span.yaml
+++ b/backend/plugin/parser/pg/test-data/query_span.yaml
@@ -1,5 +1,4 @@
-- description:
- statement: |-
+- statement: |-
SELECT
MIN(bb) AS aa
FROM
@@ -23,11 +22,11 @@
querySpan:
type: 1
results:
- - name: aa
- sourcecolumns: []
- isplainfield: false
- sourcefieldpaths: []
- selectasterisk: false
+ - name: aa
+ sourcecolumns: []
+ isplainfield: false
+ sourcefieldpaths: []
+ selectasterisk: false
sourcecolumns: []
predicatecolumns: []
- description: unnest as table source with alias
@@ -99,7 +98,7 @@
querySpan:
type: 1
results:
- - name: unnest
+ - name: t
sourcecolumns: []
isplainfield: false
sourcefieldpaths: []
@@ -285,7 +284,17 @@
schema: public
table: t2
column: ""
- predicatecolumns: []
+ predicatecolumns:
+ - server: ""
+ database: db
+ schema: public
+ table: t
+ column: b
+ - server: ""
+ database: db
+ schema: public
+ table: t2
+ column: d
- description: Function Overloading
statement: SELECT plpgsql_t(1, 2);
defaultDatabase: db
@@ -792,11 +801,6 @@
selectasterisk: false
- name: approver_emails
sourcecolumns:
- - server: ""
- database: db
- schema: public
- table: issue
- column: payload
- server: ""
database: db
schema: public
@@ -812,11 +816,6 @@
schema: public
table: principal
column: email
- - server: ""
- database: db
- schema: public
- table: task_run
- column: creator_id
isplainfield: false
sourcefieldpaths: []
selectasterisk: false
@@ -841,7 +840,22 @@
schema: public
table: task_run
column: ""
- predicatecolumns: []
+ predicatecolumns:
+ - server: ""
+ database: db
+ schema: public
+ table: issue
+ column: status
+ - server: ""
+ database: db
+ schema: public
+ table: principal
+ column: email
+ - server: ""
+ database: db
+ schema: public
+ table: task_run
+ column: creator_id
- description: Test For PLpgSQL User Defined Function in expression
statement: SELECT plpgsql_t(1);
defaultDatabase: db
@@ -897,7 +911,12 @@
schema: public
table: t
column: ""
- predicatecolumns: []
+ predicatecolumns:
+ - server: ""
+ database: db
+ schema: public
+ table: t
+ column: a
- description: json_object as target field
statement: 'SELECT json_object(''id'': a) FROM t'
defaultDatabase: db
@@ -1059,17 +1078,7 @@
isplainfield: true
sourcefieldpaths: []
selectasterisk: false
- - name: key
- sourcecolumns:
- - server: ""
- database: db
- schema: public
- table: t
- column: a
- isplainfield: false
- sourcefieldpaths: []
- selectasterisk: false
- - name: value
+ - name: jsonb_each
sourcecolumns:
- server: ""
database: db
@@ -2069,7 +2078,17 @@
schema: public
table: paintings
column: ""
- predicatecolumns: []
+ predicatecolumns:
+ - server: ""
+ database: db
+ schema: public
+ table: galleries
+ column: id
+ - server: ""
+ database: db
+ schema: public
+ table: paintings
+ column: gallery_id
- description: Test for functions
statement: SELECT max(a), a-b AS c1, a=b AS c2, a>b, b in (a, c, d) from (SELECT * FROM t) result;
defaultDatabase: db
@@ -2144,7 +2163,7 @@
isplainfield: false
sourcefieldpaths: []
selectasterisk: false
- - name: a
+ - name: ?column?
sourcecolumns:
- server: ""
database: db
@@ -2159,7 +2178,7 @@
isplainfield: false
sourcefieldpaths: []
selectasterisk: false
- - name: b
+ - name: ?column?
sourcecolumns:
- server: ""
database: db
@@ -2945,7 +2964,7 @@
isplainfield: true
sourcefieldpaths: []
selectasterisk: false
- - name: value
+ - name: jsonb_array_elements
sourcecolumns:
- server: ""
database: db
@@ -3049,7 +3068,7 @@
isplainfield: true
sourcefieldpaths: []
selectasterisk: false
- - name: value
+ - name: jsonb_array_elements_text
sourcecolumns:
- server: ""
database: db
diff --git a/backend/plugin/parser/pg/test-data/query_type.yaml b/backend/plugin/parser/pg/test-data/query_type.yaml
index fcbc42db66..dd9ac97c1e 100644
--- a/backend/plugin/parser/pg/test-data/query_type.yaml
+++ b/backend/plugin/parser/pg/test-data/query_type.yaml
@@ -468,12 +468,7 @@
querySpan:
type: 1
results: []
- sourcecolumns:
- - server: ""
- database: db
- schema: ""
- table: ""
- column: ""
+ sourcecolumns: []
predicatecolumns: []
- description: SHOW statement
statement: SHOW ALL;
diff --git a/backend/plugin/parser/pg/unescape_test.go b/backend/plugin/parser/pg/unescape_test.go
deleted file mode 100644
index 52ed494516..0000000000
--- a/backend/plugin/parser/pg/unescape_test.go
+++ /dev/null
@@ -1,286 +0,0 @@
-package pg
-
-import (
- "testing"
-
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
-)
-
-func TestUnescapePostgreSQLString(t *testing.T) {
- tests := []struct {
- name string
- input string
- want string
- wantErr bool
- }{
- // Dollar-quoted strings
- {
- name: "simple dollar-quoted",
- input: "$$SELECT 1$$",
- want: "SELECT 1",
- },
- {
- name: "dollar-quoted with tag",
- input: "$tag$SELECT 1$tag$",
- want: "SELECT 1",
- },
- {
- name: "dollar-quoted with newlines",
- input: "$$\nSELECT\n 1\n$$",
- want: "\nSELECT\n 1\n",
- },
- {
- name: "dollar-quoted with single quotes",
- input: "$$SELECT 'hello'$$",
- want: "SELECT 'hello'",
- },
- {
- name: "dollar-quoted empty",
- input: "$$$$",
- want: "",
- },
- {
- name: "dollar-quoted unterminated",
- input: "$incomplete",
- wantErr: true,
- },
-
- // Standard string constants
- {
- name: "simple string constant",
- input: "'hello'",
- want: "hello",
- },
- {
- name: "string constant with escaped quote",
- input: "'hello''world'",
- want: "hello'world",
- },
- {
- name: "string constant multiple escaped quotes",
- input: "''''",
- want: "'",
- },
- {
- name: "string constant with spaces",
- input: "' hello world '",
- want: " hello world ",
- },
- {
- name: "string constant empty",
- input: "''",
- want: "",
- },
- {
- name: "string constant with newline (literal)",
- input: "'hello\nworld'",
- want: "hello\nworld",
- },
-
- // Escape string constants
- {
- name: "escape string with newline",
- input: "E'hello\\nworld'",
- want: "hello\nworld",
- },
- {
- name: "escape string with tab",
- input: "E'hello\\tworld'",
- want: "hello\tworld",
- },
- {
- name: "escape string with backslash",
- input: "E'back\\\\slash'",
- want: "back\\slash",
- },
- {
- name: "escape string with single quote",
- input: "E'hello\\'world'",
- want: "hello'world",
- },
- {
- name: "escape string with doubled single quote",
- input: "E'hello''world'",
- want: "hello'world",
- },
- {
- name: "escape string with carriage return",
- input: "E'hello\\rworld'",
- want: "hello\rworld",
- },
- {
- name: "escape string with backspace",
- input: "E'hello\\bworld'",
- want: "hello\bworld",
- },
- {
- name: "escape string with form feed",
- input: "E'hello\\fworld'",
- want: "hello\fworld",
- },
- {
- name: "escape string with double quote",
- input: "E'hello\\\"world'",
- want: "hello\"world",
- },
- {
- name: "escape string lowercase e",
- input: "e'hello\\nworld'",
- want: "hello\nworld",
- },
- {
- name: "escape string multiple escapes",
- input: "E'line1\\nline2\\tindented\\\\backslash'",
- want: "line1\nline2\tindented\\backslash",
- },
- {
- name: "escape string unterminated",
- input: "E'hello",
- wantErr: true,
- },
-
- // Unicode escape string constants
- {
- name: "unicode escape string simple",
- input: "U&'hello'",
- want: "hello",
- },
- {
- name: "unicode escape string with escaped quote",
- input: "U&'hello''world'",
- want: "hello'world",
- },
- {
- name: "unicode escape string lowercase",
- input: "u&'hello'",
- want: "hello",
- },
- {
- name: "unicode escape string unterminated",
- input: "U&'hello",
- wantErr: true,
- },
-
- // Error cases
- {
- name: "empty string",
- input: "",
- wantErr: true,
- },
- {
- name: "too short",
- input: "x",
- wantErr: true,
- },
- {
- name: "unknown type",
- input: "hello",
- wantErr: true,
- },
- }
-
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- got, err := unescapePostgreSQLString(tt.input)
- if tt.wantErr {
- assert.Error(t, err, "expected error but got none")
- return
- }
- require.NoError(t, err, "unexpected error")
- assert.Equal(t, tt.want, got, "unescaped string mismatch")
- })
- }
-}
-
-func TestUnescapeEscapeString(t *testing.T) {
- tests := []struct {
- name string
- input string
- want string
- }{
- {
- name: "no escapes",
- input: "hello world",
- want: "hello world",
- },
- {
- name: "newline",
- input: "hello\\nworld",
- want: "hello\nworld",
- },
- {
- name: "tab",
- input: "hello\\tworld",
- want: "hello\tworld",
- },
- {
- name: "backslash",
- input: "hello\\\\world",
- want: "hello\\world",
- },
- {
- name: "single quote with backslash",
- input: "hello\\'world",
- want: "hello'world",
- },
- {
- name: "single quote doubled",
- input: "hello''world",
- want: "hello'world",
- },
- {
- name: "all escapes",
- input: "\\n\\t\\r\\b\\f\\\\\\'\\\"",
- want: "\n\t\r\b\f\\'\"",
- },
- {
- name: "unknown escape",
- input: "hello\\xworld",
- want: "hello\\xworld",
- },
- {
- name: "trailing backslash",
- input: "hello\\",
- want: "hello\\",
- },
- }
-
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- got := unescapeEscapeString(tt.input)
- assert.Equal(t, tt.want, got, "unescaped string mismatch")
- })
- }
-}
-
-func TestUnescapeUnicodeEscapeString(t *testing.T) {
- tests := []struct {
- name string
- input string
- want string
- }{
- {
- name: "no escapes",
- input: "hello world",
- want: "hello world",
- },
- {
- name: "escaped quote",
- input: "hello''world",
- want: "hello'world",
- },
- {
- name: "multiple escaped quotes",
- input: "''hello''",
- want: "'hello'",
- },
- }
-
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- got := unescapeUnicodeEscapeString(tt.input)
- assert.Equal(t, tt.want, got, "unescaped string mismatch")
- })
- }
-}