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

Function pgPathArrayToJSONPath

transpiler/transform/operators.go:902–941  ·  view source on GitHub ↗

pgPathArrayToJSONPath converts a literal Postgres text[] path (e.g. '{a,b}' or '{a,0,b}') into a DuckDB JSONPath ('$."a"."b"', '$."a"[0]."b"'). Keys are double-quoted so dotted keys are handled; all-digit elements become array indices. Returns "" for non-literal operands (left untransformed). Diver

(node *pg_query.Node)

Source from the content-addressed store, hash-verified

900// but this static conversion always treats all-digit elements as array indices —
901// an object with a literal numeric key returns NULL instead of its value.
902func pgPathArrayToJSONPath(node *pg_query.Node) string {
903 if node == nil {
904 return ""
905 }
906 // Unwrap an explicit cast like '{a,b}'::text[].
907 if tc := node.GetTypeCast(); tc != nil {
908 node = tc.Arg
909 }
910 ac := node.GetAConst()
911 if ac == nil {
912 return ""
913 }
914 sval := ac.GetSval()
915 if sval == nil {
916 return ""
917 }
918 raw := strings.TrimSpace(sval.Sval)
919 if len(raw) < 2 || raw[0] != '{' || raw[len(raw)-1] != '}' {
920 return ""
921 }
922 inner := raw[1 : len(raw)-1]
923 if strings.TrimSpace(inner) == "" {
924 return ""
925 }
926 var b strings.Builder
927 b.WriteString("$")
928 for _, part := range strings.Split(inner, ",") {
929 part = strings.TrimSpace(part)
930 if part != "" && isAllDigits(part) {
931 b.WriteString("[")
932 b.WriteString(part)
933 b.WriteString("]")
934 continue
935 }
936 b.WriteString(`."`)
937 b.WriteString(strings.ReplaceAll(part, `"`, `""`))
938 b.WriteString(`"`)
939 }
940 return b.String()
941}
942
943func isAllDigits(s string) bool {
944 for _, r := range s {

Callers 1

transformExpressionMethod · 0.85

Calls 2

isAllDigitsFunction · 0.85
StringMethod · 0.45

Tested by

no test coverage detected