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

Method rewriteDirectQuery

server/conn_query_exec.go:106–144  ·  view source on GitHub ↗

rewriteDirectQuery expands a bare `USE ducklake` to its reliable two-part `catalog.schema` target. This is NOT logical-name masking — the catalog name is real; the rewrite only works around DuckDB's bare-catalog `USE` resolution (a bare `USE ducklake` issued while the session is in another catalog r

(query string)

Source from the content-addressed store, hash-verified

104// rewriteDirectQuery expands a bare `USE ducklake` to its reliable two-part
105// `catalog.schema` target. This is NOT logical-name masking — the catalog name
106// is real; the rewrite only works around DuckDB's bare-catalog `USE`
107// resolution (a bare `USE ducklake` issued while the session is in another
108// catalog resolves `ducklake` as a *schema* within that catalog).
109//
110// A session connected under its org's logical catalog alias (c.database, the
111// org's Trino catalog name) may name the catalog either way: the alias is the
112// only catalog name such a client ever sees, so `USE <alias>` has to land where
113// `USE ducklake` does. That alias was validated against the session's own org
114// at connect time, so it can only ever name this session's own catalog.
115//
116// Any other `USE <name>` and all other statements are passed through unchanged.
117func (c *clientConn) rewriteDirectQuery(query string) string {
118 if c == nil || c.server == nil || c.passthrough || !c.catalogUseRewrite {
119 return query
120 }
121
122 stripped := strings.TrimSpace(stripLeadingComments(query))
123 if stripped == "" {
124 return query
125 }
126
127 hasSemicolon := strings.HasSuffix(stripped, ";")
128 trimmed := strings.TrimSpace(strings.TrimSuffix(stripped, ";"))
129
130 if len(trimmed) < len("USE") || !strings.EqualFold(trimmed[:len("USE")], "USE") {
131 return query
132 }
133
134 target := strings.TrimSpace(trimmed[len("USE"):])
135 if target == "" {
136 return query
137 }
138
139 unquoted := target
140 if len(target) >= 2 && target[0] == '"' && target[len(target)-1] == '"' {
141 unquoted = strings.ReplaceAll(target[1:len(target)-1], `""`, `"`)
142 }
143
144 if !strings.EqualFold(unquoted, physicalDuckLakeCatalog) && !c.namesDuckLakeCatalog(unquoted) {
145 return query
146 }
147 // `USE ducklake` -> ducklake.main (DuckLake's real schema is `main`).

Calls 1

stripLeadingCommentsFunction · 0.85