|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "regexp" |
| 8 | + "strconv" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +// The output mirrors the JSON `sqlc analyze` prints so a golden generated |
| 13 | +// here can be diffed against sqlc's own analysis of the same files. |
| 14 | + |
| 15 | +type analyzedQuery struct { |
| 16 | + Name string `json:"name"` |
| 17 | + Cmd string `json:"cmd"` |
| 18 | + Columns []analyzedColumn `json:"columns"` |
| 19 | + Params []analyzedParam `json:"params"` |
| 20 | +} |
| 21 | + |
| 22 | +type analyzedColumn struct { |
| 23 | + Name string `json:"name"` |
| 24 | + DataType string `json:"data_type"` |
| 25 | + NotNull bool `json:"not_null"` |
| 26 | + IsArray bool `json:"is_array"` |
| 27 | + Table string `json:"table,omitempty"` |
| 28 | +} |
| 29 | + |
| 30 | +type analyzedParam struct { |
| 31 | + Number int `json:"number"` |
| 32 | + Column analyzedColumn `json:"column"` |
| 33 | +} |
| 34 | + |
| 35 | +// analyze runs every query against the schema and fixture and records what |
| 36 | +// ClickHouse reports about each. |
| 37 | +func analyze(ctx context.Context, l local, schema, fixture string, queries []query) ([]analyzedQuery, error) { |
| 38 | + out := make([]analyzedQuery, 0, len(queries)) |
| 39 | + for _, q := range queries { |
| 40 | + aq, err := analyzeQuery(ctx, l, schema, fixture, q) |
| 41 | + if err != nil { |
| 42 | + return nil, fmt.Errorf("%s: %w", q.Name, err) |
| 43 | + } |
| 44 | + out = append(out, aq) |
| 45 | + } |
| 46 | + return out, nil |
| 47 | +} |
| 48 | + |
| 49 | +func analyzeQuery(ctx context.Context, l local, schema, fixture string, q query) (analyzedQuery, error) { |
| 50 | + sql, phs := bindPlaceholders(q.SQL) |
| 51 | + explain := returnsRows(sql) |
| 52 | + |
| 53 | + var script strings.Builder |
| 54 | + for _, stmt := range []string{schema, fixture} { |
| 55 | + if s := strings.TrimRight(strings.TrimSpace(stmt), ";"); s != "" { |
| 56 | + script.WriteString(s) |
| 57 | + script.WriteString(";\n") |
| 58 | + } |
| 59 | + } |
| 60 | + if explain { |
| 61 | + script.WriteString("EXPLAIN QUERY TREE " + sql + ";\n") |
| 62 | + } |
| 63 | + script.WriteString(sql + ";\n") |
| 64 | + |
| 65 | + results, err := l.run(ctx, script.String()) |
| 66 | + if err != nil { |
| 67 | + return analyzedQuery{}, err |
| 68 | + } |
| 69 | + |
| 70 | + aq := analyzedQuery{ |
| 71 | + Name: q.Name, |
| 72 | + Cmd: q.Cmd, |
| 73 | + Columns: []analyzedColumn{}, |
| 74 | + Params: []analyzedParam{}, |
| 75 | + } |
| 76 | + if !explain { |
| 77 | + return analyzeExec(ctx, l, script.String(), sql, phs, aq) |
| 78 | + } |
| 79 | + if len(results) != 2 { |
| 80 | + return analyzedQuery{}, fmt.Errorf("expected the query tree and one result set, got %d results", len(results)) |
| 81 | + } |
| 82 | + |
| 83 | + var lines []string |
| 84 | + for _, row := range results[0].Data { |
| 85 | + var line string |
| 86 | + if err := json.Unmarshal(row["explain"], &line); err != nil { |
| 87 | + return analyzedQuery{}, fmt.Errorf("reading query tree: %w", err) |
| 88 | + } |
| 89 | + lines = append(lines, line) |
| 90 | + } |
| 91 | + tree, err := parseQueryTree(lines) |
| 92 | + if err != nil { |
| 93 | + return analyzedQuery{}, err |
| 94 | + } |
| 95 | + |
| 96 | + // Names and types come from the block header of the executed query, the |
| 97 | + // same header a driver sees. The tree only adds where each came from. |
| 98 | + _, nodes := projection(firstNode(tree.root.children)) |
| 99 | + for i, col := range results[1].Meta { |
| 100 | + ac := column(col.Name, col.Type) |
| 101 | + if i < len(nodes) && len(nodes) == len(results[1].Meta) { |
| 102 | + ac.Table = tree.sourceTable(nodes[i]) |
| 103 | + } |
| 104 | + aq.Columns = append(aq.Columns, ac) |
| 105 | + } |
| 106 | + |
| 107 | + sentinels := tree.sentinels() |
| 108 | + for i, ph := range phs { |
| 109 | + ac := analyzedColumn{} |
| 110 | + if sentinel := sentinels[i+1]; sentinel != nil { |
| 111 | + ac = tree.paramColumn(sentinel) |
| 112 | + } |
| 113 | + if ph.Name != "" { |
| 114 | + ac.Name = ph.Name |
| 115 | + } |
| 116 | + aq.Params = append(aq.Params, analyzedParam{Number: ph.Number, Column: ac}) |
| 117 | + } |
| 118 | + return aq, nil |
| 119 | +} |
| 120 | + |
| 121 | +func column(name, typ string) analyzedColumn { |
| 122 | + dataType, isArray, notNull := sqlcType(typ) |
| 123 | + return analyzedColumn{Name: name, DataType: dataType, NotNull: notNull, IsArray: isArray} |
| 124 | +} |
| 125 | + |
| 126 | +// returnsRows reports whether a statement produces a result set and so can |
| 127 | +// be explained as a query tree. |
| 128 | +func returnsRows(sql string) bool { |
| 129 | + head := strings.ToLower(strings.TrimSpace(sql)) |
| 130 | + if strings.HasPrefix(head, "(") { |
| 131 | + return true |
| 132 | + } |
| 133 | + for _, kw := range []string{"select", "with", "show", "describe", "desc", "exists"} { |
| 134 | + if strings.HasPrefix(head, kw) && (len(head) == len(kw) || !isWordByte(head[len(kw)])) { |
| 135 | + return true |
| 136 | + } |
| 137 | + } |
| 138 | + return false |
| 139 | +} |
| 140 | + |
| 141 | +// sentinels finds the constants the placeholders were substituted with, |
| 142 | +// keyed by placeholder ordinal. |
| 143 | +func (t *queryTree) sentinels() map[int]*treeNode { |
| 144 | + found := map[int]*treeNode{} |
| 145 | + var walk func(n *treeNode) |
| 146 | + walk = func(n *treeNode) { |
| 147 | + if n.kind == "CONSTANT" { |
| 148 | + if k, ok := sentinelOrdinal(n); ok { |
| 149 | + found[k] = n |
| 150 | + return |
| 151 | + } |
| 152 | + } |
| 153 | + for _, c := range n.children { |
| 154 | + walk(c) |
| 155 | + } |
| 156 | + } |
| 157 | + walk(t.root) |
| 158 | + return found |
| 159 | +} |
| 160 | + |
| 161 | +// sentinelOrdinal recognises a constant folded directly from `NULL + k` or |
| 162 | +// `4294967295 + k`, the shapes sentinelFor produces, and returns k. A |
| 163 | +// constant folded from a larger expression that merely contains a sentinel |
| 164 | +// does not match; the sentinel is found nested inside it instead. |
| 165 | +func sentinelOrdinal(c *treeNode) (int, bool) { |
| 166 | + fn := firstNode(c.section("EXPRESSION").childrenOrNil()) |
| 167 | + if fn == nil || fn.kind != "FUNCTION" || fn.attrs["function_name"] != "plus" { |
| 168 | + return 0, false |
| 169 | + } |
| 170 | + args := fn.section("ARGUMENTS").list() |
| 171 | + if len(args) != 2 || args[0].kind != "CONSTANT" || args[1].kind != "CONSTANT" { |
| 172 | + return 0, false |
| 173 | + } |
| 174 | + base := args[0].attrs["constant_value"] |
| 175 | + if base != "NULL" && base != "UInt64_"+limitBase { |
| 176 | + return 0, false |
| 177 | + } |
| 178 | + k, err := strconv.Atoi(strings.TrimPrefix(args[1].attrs["constant_value"], "UInt64_")) |
| 179 | + if err != nil { |
| 180 | + return 0, false |
| 181 | + } |
| 182 | + return k, true |
| 183 | +} |
| 184 | + |
| 185 | +// paramColumn describes what a placeholder is compared with or assigned to: |
| 186 | +// the other operand of the function it is an argument of, preferring a |
| 187 | +// column over an expression, or the projected column it stands for. |
| 188 | +func (t *queryTree) paramColumn(sentinel *treeNode) analyzedColumn { |
| 189 | + list := sentinel.parent |
| 190 | + if list != nil && list.kind == "LIST" && list.parent != nil { |
| 191 | + switch owner := list.parent; { |
| 192 | + case owner.kind == "" && owner.text == "ARGUMENTS": |
| 193 | + // Prefer a column operand, then an expression, then a constant. |
| 194 | + rank := map[string]int{"COLUMN": 0, "FUNCTION": 1, "CONSTANT": 2} |
| 195 | + var best *treeNode |
| 196 | + for _, sib := range list.children { |
| 197 | + if sib == sentinel { |
| 198 | + continue |
| 199 | + } |
| 200 | + if r, ok := rank[sib.kind]; ok && (best == nil || r < rank[best.kind]) { |
| 201 | + best = sib |
| 202 | + } |
| 203 | + } |
| 204 | + if best != nil { |
| 205 | + return t.describe(best) |
| 206 | + } |
| 207 | + case owner.kind == "" && owner.text == "PROJECTION": |
| 208 | + names, nodes := projection(owner.parent) |
| 209 | + for i, n := range nodes { |
| 210 | + if n == sentinel && i < len(names) { |
| 211 | + ac := column(names[i], sentinel.attrs["constant_value_type"]) |
| 212 | + return ac |
| 213 | + } |
| 214 | + } |
| 215 | + } |
| 216 | + } |
| 217 | + return column("", sentinel.attrs["constant_value_type"]) |
| 218 | +} |
| 219 | + |
| 220 | +// describe turns a tree expression into a column description. |
| 221 | +func (t *queryTree) describe(n *treeNode) analyzedColumn { |
| 222 | + switch n.kind { |
| 223 | + case "COLUMN": |
| 224 | + ac := column(n.attrs["column_name"], n.attrs["result_type"]) |
| 225 | + ac.Table = t.sourceTable(n) |
| 226 | + return ac |
| 227 | + case "FUNCTION": |
| 228 | + return column(n.attrs["function_name"], n.attrs["result_type"]) |
| 229 | + case "CONSTANT": |
| 230 | + return column("", n.attrs["constant_value_type"]) |
| 231 | + } |
| 232 | + return analyzedColumn{} |
| 233 | +} |
| 234 | + |
| 235 | +var insertValuesRe = regexp.MustCompile(`(?is)^insert\s+into\s+(?:table\s+)?([\w.` + "`" + `"]+)\s*(?:\(([^)]*)\))?\s*(?:format\s+)?values\b`) |
| 236 | + |
| 237 | +// analyzeExec runs a statement that returns no rows. The only parameters it |
| 238 | +// can describe are those of an INSERT ... VALUES, which map positionally |
| 239 | +// onto the target columns reported by DESCRIBE TABLE. |
| 240 | +func analyzeExec(ctx context.Context, l local, script, sql string, phs []placeholder, aq analyzedQuery) (analyzedQuery, error) { |
| 241 | + m := insertValuesRe.FindStringSubmatch(sql) |
| 242 | + if m != nil { |
| 243 | + script += "DESCRIBE TABLE " + m[1] + ";\n" |
| 244 | + } |
| 245 | + results, err := l.run(ctx, script) |
| 246 | + if err != nil { |
| 247 | + return analyzedQuery{}, err |
| 248 | + } |
| 249 | + |
| 250 | + var targets []analyzedColumn |
| 251 | + if m != nil && len(results) == 1 { |
| 252 | + byName := map[string]analyzedColumn{} |
| 253 | + var all []analyzedColumn |
| 254 | + table := strings.Trim(m[1][strings.LastIndexByte(m[1], '.')+1:], "`\"") |
| 255 | + for _, row := range results[0].Data { |
| 256 | + var name, typ string |
| 257 | + json.Unmarshal(row["name"], &name) |
| 258 | + json.Unmarshal(row["type"], &typ) |
| 259 | + ac := column(name, typ) |
| 260 | + ac.Table = table |
| 261 | + byName[name] = ac |
| 262 | + all = append(all, ac) |
| 263 | + } |
| 264 | + if strings.TrimSpace(m[2]) == "" { |
| 265 | + targets = all |
| 266 | + } else { |
| 267 | + for _, name := range strings.Split(m[2], ",") { |
| 268 | + targets = append(targets, byName[strings.Trim(strings.TrimSpace(name), "`\"")]) |
| 269 | + } |
| 270 | + } |
| 271 | + } |
| 272 | + for i, ph := range phs { |
| 273 | + ac := analyzedColumn{} |
| 274 | + if len(targets) > 0 { |
| 275 | + ac = targets[i%len(targets)] |
| 276 | + } |
| 277 | + if ph.Name != "" { |
| 278 | + ac.Name = ph.Name |
| 279 | + } |
| 280 | + aq.Params = append(aq.Params, analyzedParam{Number: ph.Number, Column: ac}) |
| 281 | + } |
| 282 | + return aq, nil |
| 283 | +} |
0 commit comments