Skip to content

Commit

Permalink
Add support for const functions in CASE expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
arjunlol committed Dec 30, 2024
1 parent 46ac238 commit b94a729
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/query_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,10 @@ func TestHandleQuery(t *testing.T) {
"description": {"id", "name", "is_superuser", "can_create_role"},
"values": {},
},
"SELECT CASE WHEN TRUE THEN pg_catalog.pg_is_in_recovery() END AS CASE": {
"description": {"case"},
"values": {"f"},
},
// WHERE pg functions
"SELECT gss_authenticated, encrypted FROM (SELECT false, false, false, false, false WHERE false) t(pid, gss_authenticated, principal, encrypted, credentials_delegated) WHERE pid = pg_backend_pid()": {
"description": {"gss_authenticated", "encrypted"},
Expand Down
8 changes: 8 additions & 0 deletions src/select_remapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ func (selectRemapper *SelectRemapper) remapCaseExpressions(selectStatement *pgQu
subSelect := subLink.Subselect.GetSelectStmt()
subSelect = selectRemapper.remapSelectStatement(subSelect, indentLevel+1)
}
if funcCall := whenClause.Result.GetFuncCall(); funcCall != nil {
selectRemapper.traceTreeTraversal("CASE THEN function", indentLevel+1)
whenClause.Result = selectRemapper.remapperSelect.remappedToConstant(funcCall)
}
}
}
}
Expand All @@ -180,6 +184,10 @@ func (selectRemapper *SelectRemapper) remapCaseExpressions(selectStatement *pgQu
subSelect := subLink.Subselect.GetSelectStmt()
subSelect = selectRemapper.remapSelectStatement(subSelect, indentLevel+1)
}
if funcCall := caseExpr.Defresult.GetFuncCall(); funcCall != nil {
selectRemapper.traceTreeTraversal("CASE ELSE function", indentLevel+1)
caseExpr.Defresult = selectRemapper.remapperSelect.remappedToConstant(funcCall)
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/select_remapper_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var REMAPPED_CONSTANT_BY_PG_FUNCTION_NAME = map[string]string{
"pg_tablespace_location": "",
"pg_encoding_to_char": "UTF8",
"pg_backend_pid": "0",
"pg_is_in_recovery": "f",
}

type SelectRemapperSelect struct {
Expand Down

0 comments on commit b94a729

Please sign in to comment.