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

Method handleToChar

transpiler/transform/functions.go:444–471  ·  view source on GitHub ↗

handleToChar converts PostgreSQL to_char() to DuckDB equivalents. to_char(timestamp, format) → strftime(timestamp, converted_format) to_char(number, format) → format(converted_format, number)

(fc *pg_query.FuncCall, funcNameIdx int)

Source from the content-addressed store, hash-verified

442// to_char(timestamp, format) → strftime(timestamp, converted_format)
443// to_char(number, format) → format(converted_format, number)
444func (t *FunctionTransform) handleToChar(fc *pg_query.FuncCall, funcNameIdx int) bool {
445 if len(fc.Args) != 2 {
446 return false
447 }
448
449 // Try to get the format string (second argument)
450 formatStr := extractStringConstant(fc.Args[1])
451 if formatStr == "" {
452 // Can't determine format at parse time — default to strftime for timestamps
453 renameFuncAndStripPrefix(fc, funcNameIdx, "strftime")
454 return true
455 }
456
457 if isDateFormat(formatStr) {
458 // Date/time formatting: to_char(ts, fmt) → strftime(ts, converted_fmt)
459 converted := pgDateFormatToStrftime(formatStr)
460 setStringConstant(fc.Args[1], converted)
461 renameFuncAndStripPrefix(fc, funcNameIdx, "strftime")
462 } else {
463 // Number formatting: to_char(num, fmt) → format(converted_fmt, num)
464 converted := pgNumberFormatToDuckDB(formatStr)
465 setStringConstant(fc.Args[1], converted)
466 renameFuncAndStripPrefix(fc, funcNameIdx, "format")
467 // Swap args: PG to_char(value, fmt) → DuckDB format(fmt, value)
468 fc.Args[0], fc.Args[1] = fc.Args[1], fc.Args[0]
469 }
470 return true
471}
472
473// handleToDate converts PostgreSQL to_date(text, format) to strptime with converted format.
474func (t *FunctionTransform) handleToDate(fc *pg_query.FuncCall, funcNameIdx int) bool {

Callers 1

handleSpecialFunctionMethod · 0.95

Calls 6

extractStringConstantFunction · 0.85
renameFuncAndStripPrefixFunction · 0.85
isDateFormatFunction · 0.85
pgDateFormatToStrftimeFunction · 0.85
setStringConstantFunction · 0.85
pgNumberFormatToDuckDBFunction · 0.85

Tested by

no test coverage detected