MCPcopy Create free account
hub / github.com/rilldata/rill / RewriteTableRefs

Method RewriteTableRefs

runtime/pkg/duckdbsql/ast.go:85–127  ·  view source on GitHub ↗

RewriteTableRefs replaces table references in a DuckDB SQL query. Only replacing with a base table reference is supported right now.

(fn func(table *TableRef) (*TableRef, bool))

Source from the content-addressed store, hash-verified

83
84// RewriteTableRefs replaces table references in a DuckDB SQL query. Only replacing with a base table reference is supported right now.
85func (a *AST) RewriteTableRefs(fn func(table *TableRef) (*TableRef, bool)) error {
86 if a.ast == nil {
87 return fmt.Errorf("calling rewrite on failed parse")
88 }
89
90 for _, node := range a.fromNodes {
91 if node.ast == nil {
92 continue
93 }
94
95 newRef, shouldReplace := fn(node.ref)
96 if !shouldReplace {
97 continue
98 }
99
100 if newRef.Name != "" {
101 err := node.rewriteToBaseTable(newRef.Name)
102 if err != nil {
103 return err
104 }
105 } else if newRef.Function != "" {
106 switch newRef.Function {
107 case "sqlite_scan":
108 newRef.Params[0] = newRef.Paths[0]
109 err := node.rewriteToSqliteScanFunction(newRef.Params)
110 if err != nil {
111 return err
112 }
113 case "read_csv_auto", "read_csv",
114 "read_parquet",
115 "read_json", "read_json_auto", "read_json_objects", "read_json_objects_auto",
116 "read_ndjson_objects", "read_ndjson", "read_ndjson_auto":
117 err := node.rewriteToReadTableFunction(newRef.Function, newRef.Paths, newRef.Properties)
118 if err != nil {
119 return err
120 }
121 // non read_ functions are not supported right now
122 }
123 }
124 }
125
126 return nil
127}
128
129// RewriteLimit rewrites a DuckDB SQL statement to limit the result size
130func (a *AST) RewriteLimit(limit, offset int) error {

Callers 5

rewriteSQLFunction · 0.80
rewriteLocalPathsFunction · 0.80

Calls 4

rewriteToBaseTableMethod · 0.80
ErrorfMethod · 0.65