Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 44 additions & 22 deletions pg/catalog/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -1382,17 +1382,7 @@ func (ac *analyzeCtx) resolveColumnRef(tableName, colName string) (*VarExpr, err
}
for colIdx, cn := range rte.ColNames {
if cn == colName {
var coll uint32
if colIdx < len(rte.ColCollations) {
coll = rte.ColCollations[colIdx]
}
v := &VarExpr{
RangeIdx: rtIdx,
AttNum: int16(colIdx + 1),
TypeOID: rte.ColTypes[colIdx],
TypeMod: rte.ColTypMods[colIdx],
Collation: coll,
}
v := makeRTEColumnVar(rtIdx, rte, colIdx)
if found != nil {
return nil, errAmbiguousColumn(colName)
}
Expand All @@ -1404,6 +1394,17 @@ func (ac *analyzeCtx) resolveColumnRef(tableName, colName string) (*VarExpr, err
}
}

if tableName == "" && found == nil {
for rtIdx, rte := range ac.query.RangeTable {
if v := ac.makeScalarFunctionWholeRowVar(rtIdx, rte, colName); v != nil {
if found != nil {
return nil, errAmbiguousColumn(colName)
}
found = v
}
}
}

if found == nil && ac.parent != nil {
// Try resolving in parent context (correlated subquery).
// pg: src/backend/parser/parse_expr.c — transformColumnRef
Expand Down Expand Up @@ -1490,17 +1491,7 @@ func (ac *analyzeCtx) findVarInRTE(rtIdx int, colName string) (*VarExpr, bool) {
if cn != colName {
continue
}
var coll uint32
if colIdx < len(rte.ColCollations) {
coll = rte.ColCollations[colIdx]
}
v := &VarExpr{
RangeIdx: rtIdx,
AttNum: int16(colIdx + 1),
TypeOID: rte.ColTypes[colIdx],
TypeMod: rte.ColTypMods[colIdx],
Collation: coll,
}
v := makeRTEColumnVar(rtIdx, rte, colIdx)
if found != nil {
return nil, true
}
Expand All @@ -1509,6 +1500,37 @@ func (ac *analyzeCtx) findVarInRTE(rtIdx int, colName string) (*VarExpr, bool) {
return found, false
}

func makeRTEColumnVar(rtIdx int, rte *RangeTableEntry, colIdx int) *VarExpr {
var coll uint32
if colIdx < len(rte.ColCollations) {
coll = rte.ColCollations[colIdx]
}
return &VarExpr{
RangeIdx: rtIdx,
AttNum: int16(colIdx + 1),
TypeOID: rte.ColTypes[colIdx],
TypeMod: rte.ColTypMods[colIdx],
Collation: coll,
}
}

func (ac *analyzeCtx) makeScalarFunctionWholeRowVar(rtIdx int, rte *RangeTableEntry, name string) *VarExpr {
if rte.Kind != RTEFunction || rte.ERef != name || rte.Ordinality || len(rte.FuncExprs) != 1 {
return nil
}
fexpr := rte.FuncExprs[0]
if fexpr == nil || ac.catalog.getTypeFuncClass(fexpr.exprType()) != typeFuncScalar {
return nil
}
return &VarExpr{
RangeIdx: rtIdx,
AttNum: 1,
TypeOID: fexpr.exprType(),
TypeMod: -1,
Collation: fexpr.exprCollation(),
}
}

// transformAConst transforms a constant value.
//
// pg: src/backend/parser/parse_expr.c — make_const
Expand Down
8 changes: 8 additions & 0 deletions pg/catalog/scope_regression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,11 @@ func TestPGAnalyzerScopeRejectsNonLateralSetopSiblingReference(t *testing.T) {
t.Fatal("LoadSQL unexpectedly accepted non-LATERAL set-op reference to sibling alias")
}
}

func TestPGAnalyzerRangeFunctionAliasAsJsonbOperand(t *testing.T) {
c := New()
parseAndAnalyze(t, c, `
SELECT elem ->> 'matchedDateTimeValue'
FROM jsonb_array_elements('[{}]'::jsonb) elem
`)
}
Loading