Commit a3f0765
authored
aitools: parallelize discover-schema across tables and probes (#5097)
## Stack
This PR is part of a 4-PR stack making `aitools` data exploration faster
for ai-dev-kit. Each PR is independently reviewable; merge in order.
1. #5092 — aitools: extract pollStatement helper and pin OnWaitTimeout
*(base: `main`)*
2. #5093 — aitools: run multiple SQL queries in parallel from one query
invocation *(base: #5092)*
3. #5095 — aitools: add 'tools statement' lifecycle commands *(base:
#5093)*
4. **#5097 — aitools: parallelize discover-schema across tables and
probes** *(base: #5095)* — **this PR**
Use `git diff <base>...HEAD` or set the comparison base in the GitHub UI
to see only this PR's changes; the default "Files changed" diff against
`main` includes ancestor PRs.
---
## Why
`discover-schema` walked tables sequentially and ran each table's three
probes (DESCRIBE, sample SELECT, null counts) one after the other. For
ai-dev-kit's data-exploration phase that meant warehouse-bound work was
idle most of the time. Same root cause as the multi-query exploration
latency that #5093 (batch query) fixed; same fix.
This is a pure latency win. No new user-facing API surface, no
output-shape change.
## Changes
**Two layers of parallelism plus a shared statement budget:**
1. **Across tables.** The for-loop in `RunE` becomes an
`errgroup.Group`. A failure on one table never aborts the others; it's
rendered inline as `"Error discovering ..."` exactly as before.
2. **Within a table.** `discoverTable` still runs DESCRIBE first because
the column list feeds the null-counts query. After DESCRIBE returns, the
sample SELECT and null-counts probes run concurrently. Output text is
assembled once both probes finish, preserving the existing `COLUMNS /
SAMPLE DATA / NULL COUNTS` order.
3. **Single warehouse-statement budget.** A new `sqlGate` (chan struct{}
of capacity N + statement_id tracking) wraps every `executeSQL` call.
`--concurrency` (default 8) caps total in-flight statements globally,
regardless of how many tables you pass. So `--concurrency 1` actually
serializes statement load, not just table fan-out.
**Switch `executeSQL` to use `pollStatement`** (the helper extracted in
#5092) instead of the SDK's `ExecuteAndWait`. Pins `OnWaitTimeout:
CONTINUE`. Failed states flow through `checkFailedState`, yielding more
specific error messages (e.g. `"query failed: SYNTAX_ERROR near
'oops'"`) than the previous hand-rolled branch. The user-visible
`"SAMPLE DATA: Error - %v" / "NULL COUNTS: Error - %v"` wrapping is
unchanged. Future polling-helper improvements land here for free.
**Cancellation discipline mirroring batch.go (#5093):** signal handler
cancels a derived `pollCtx`; `sqlGate` records each `statement_id`
post-submission; on cancellation the recorded IDs are swept via
`CancelExecution` before returning `root.ErrAlreadyPrinted`. Without
this, parallelism would orphan up to N×2 statements server-side on
Ctrl+C.
**`--concurrency` validation** mirrors `cmd/fs/cp.go` and #5093:
`PreRunE` rejects values <= 0 with `errInvalidBatchConcurrency`.
Table-name validation also runs in `PreRunE` so malformed identifiers
are rejected before `MustWorkspaceClient` runs (no unnecessary auth
roundtrip on bad input).
**Output unchanged** for any input that previously succeeded. Same
dividers, same header/probe ordering, same per-probe error wrapping.
## Test plan
- [x] `go test ./experimental/aitools/...` passes.
- [x] `make checks` clean.
- [x] `make fmt` no drift.
- [x] `make lint` 0 issues.
- [x] New unit tests in `discover_schema_test.go`:
- `quoteTableName` table-driven (valid, missing parts, too many parts,
injection attempts, empty parts, leading-digit identifiers, backtick in
name)
- `parseDescribeResult` skips metadata rows (`#`-prefixed and empty)
- `sqlGate.run` pins `OnWaitTimeout: CONTINUE`, propagates FAILED state,
wraps transport errors, records IDs, respects cancelled context
- `cancelDiscoverInFlight` calls API per ID; empty list is a no-op
- `discoverTable`: sample and null-count probes run concurrently after
DESCRIBE (deterministic atomic-counter + sync.OnceFunc + channel-close
barrier; sequential execution surfaces a timeout error)
- `discoverTable`: a sample-probe failure does not abort null counts
- `--concurrency 0` and `-1` rejected at PreRunE
- Invalid table name (not `CATALOG.SCHEMA.TABLE`) and injection attempts
rejected at PreRunE before any API call
- [x] Manual smoke against a real warehouse:
```bash
databricks experimental aitools tools discover-schema \
samples.nyctaxi.trips samples.tpch.orders samples.tpch.customer
```1 parent d0d58e8 commit a3f0765
2 files changed
Lines changed: 518 additions & 55 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
| 8 | + | |
7 | 9 | | |
| 10 | + | |
8 | 11 | | |
| 12 | + | |
| 13 | + | |
9 | 14 | | |
10 | 15 | | |
11 | 16 | | |
12 | 17 | | |
13 | 18 | | |
14 | 19 | | |
| 20 | + | |
15 | 21 | | |
16 | 22 | | |
17 | 23 | | |
| 24 | + | |
18 | 25 | | |
19 | 26 | | |
20 | 27 | | |
21 | 28 | | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
22 | 94 | | |
| 95 | + | |
| 96 | + | |
23 | 97 | | |
24 | 98 | | |
25 | 99 | | |
| |||
31 | 105 | | |
32 | 106 | | |
33 | 107 | | |
34 | | - | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
35 | 117 | | |
36 | 118 | | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
44 | 125 | | |
45 | 126 | | |
46 | 127 | | |
47 | 128 | | |
48 | 129 | | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
49 | 135 | | |
50 | 136 | | |
51 | 137 | | |
| |||
57 | 143 | | |
58 | 144 | | |
59 | 145 | | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
65 | 159 | | |
66 | | - | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
67 | 183 | | |
68 | 184 | | |
69 | 185 | | |
| |||
90 | 206 | | |
91 | 207 | | |
92 | 208 | | |
| 209 | + | |
| 210 | + | |
93 | 211 | | |
94 | 212 | | |
95 | 213 | | |
96 | | - | |
97 | | - | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
98 | 233 | | |
| 234 | + | |
99 | 235 | | |
100 | 236 | | |
101 | 237 | | |
102 | 238 | | |
103 | 239 | | |
104 | 240 | | |
105 | | - | |
106 | | - | |
| 241 | + | |
107 | 242 | | |
108 | 243 | | |
109 | 244 | | |
| |||
113 | 248 | | |
114 | 249 | | |
115 | 250 | | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
116 | 286 | | |
117 | 287 | | |
118 | 288 | | |
119 | 289 | | |
120 | 290 | | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
| 291 | + | |
| 292 | + | |
126 | 293 | | |
127 | 294 | | |
128 | 295 | | |
129 | 296 | | |
130 | 297 | | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
| 298 | + | |
| 299 | + | |
142 | 300 | | |
143 | 301 | | |
144 | 302 | | |
| |||
147 | 305 | | |
148 | 306 | | |
149 | 307 | | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | 308 | | |
172 | 309 | | |
173 | 310 | | |
| |||
0 commit comments