MCPcopy Create free account
hub / github.com/PostHog/duckgres / transformFuncCall

Method transformFuncCall

transpiler/transform/functions.go:229–285  ·  view source on GitHub ↗
(fc *pg_query.FuncCall)

Source from the content-addressed store, hash-verified

227}
228
229func (t *FunctionTransform) transformFuncCall(fc *pg_query.FuncCall) bool {
230 if fc == nil || len(fc.Funcname) == 0 {
231 return false
232 }
233
234 // Get function name (last element, ignoring schema prefix)
235 var funcName string
236 var funcNameIdx int
237 for i, name := range fc.Funcname {
238 if str := name.GetString_(); str != nil {
239 funcName = strings.ToLower(str.Sval)
240 funcNameIdx = i
241 }
242 }
243
244 if funcName == "" {
245 return false
246 }
247
248 // Check for simple name mapping
249 if newName, ok := functionNameMapping[funcName]; ok {
250 if str := fc.Funcname[funcNameIdx].GetString_(); str != nil {
251 str.Sval = newName
252 }
253
254 // array_upper(arr, dim) and array_length(arr, dim) take a dimension
255 // parameter that DuckDB's len() doesn't accept. Strip the second arg.
256 if (funcName == "array_upper" || funcName == "array_length") && len(fc.Args) == 2 {
257 fc.Args = fc.Args[:1]
258 }
259
260 // Remove schema prefix if present, BUT preserve pg_catalog for functions
261 // using SQL syntax (COERCE_SQL_SYNTAX) like EXTRACT(field FROM source)
262 // because the deparser only outputs the special syntax with the prefix.
263 // However, only preserve the prefix when the function name stays the same.
264 // If we rename the function (like btrim -> trim), the deparser won't recognize
265 // it as SQL syntax anymore, so we must strip the prefix.
266 preservePrefix := fc.Funcformat == pg_query.CoercionForm_COERCE_SQL_SYNTAX && funcName == newName
267 if len(fc.Funcname) > 1 && !preservePrefix {
268 if first := fc.Funcname[0].GetString_(); first != nil {
269 schema := strings.ToLower(first.Sval)
270 if schema == "pg_catalog" || schema == "public" {
271 fc.Funcname = fc.Funcname[1:]
272 }
273 }
274 }
275
276 return true
277 }
278
279 // Handle special transformations
280 if specialFunctions[funcName] {
281 return t.handleSpecialFunction(fc, funcName, funcNameIdx)
282 }
283
284 return false
285}
286

Callers 1

TransformMethod · 0.95

Calls 1

handleSpecialFunctionMethod · 0.95

Tested by

no test coverage detected