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

Function parseNumericTypmod

server/types.go:229–253  ·  view source on GitHub ↗

parseNumericTypmod parses precision and scale from a type name like "DECIMAL(10,2)" and encodes them as a PostgreSQL typmod: ((precision << 16) | scale) + 4. Returns -1 if precision/scale cannot be extracted.

(typeName string)

Source from the content-addressed store, hash-verified

227// and encodes them as a PostgreSQL typmod: ((precision << 16) | scale) + 4.
228// Returns -1 if precision/scale cannot be extracted.
229func parseNumericTypmod(typeName string) int32 {
230 lparen := strings.IndexByte(typeName, '(')
231 rparen := strings.IndexByte(typeName, ')')
232 if lparen < 0 || rparen < 0 || rparen <= lparen {
233 return -1
234 }
235 inner := typeName[lparen+1 : rparen]
236 parts := strings.SplitN(inner, ",", 2)
237 if len(parts) != 2 {
238 return -1
239 }
240 precision := strings.TrimSpace(parts[0])
241 scale := strings.TrimSpace(parts[1])
242 var p, s int
243 if _, err := fmt.Sscanf(precision, "%d", &p); err != nil {
244 return -1
245 }
246 if _, err := fmt.Sscanf(scale, "%d", &s); err != nil {
247 return -1
248 }
249 if p <= 0 || s < 0 || p > 38 {
250 return -1
251 }
252 return int32((p << 16) | s) + 4
253}
254
255// encodeBinary encodes a value in PostgreSQL binary format
256// Returns the encoded bytes, or nil if the value should be sent as NULL

Callers 2

TestParseNumericTypmodFunction · 0.85
mapDuckDBTypeFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestParseNumericTypmodFunction · 0.68