From 2a092f8c4d3b897cbd1acf05ccce37ec599ae151 Mon Sep 17 00:00:00 2001 From: Andres Pirela <60560109+andreespirela@users.noreply.github.com> Date: Sat, 3 Oct 2026 21:44:45 -0400 Subject: [PATCH] Add SQL completion and the boom command --- .github/workflows/check.yml | 2 +- .github/workflows/release.yml | 9 +- Cargo.lock | 259 +++++++++++++++++++++++--- Cargo.toml | 17 +- README.md | 179 +++++++++++------- crates/completion/Cargo.toml | 13 ++ crates/completion/src/catalog.rs | 153 +++++++++++++++ crates/completion/src/cursor.rs | 180 ++++++++++++++++++ crates/completion/src/lib.rs | 153 +++++++++++++++ crates/completion/src/query.rs | 169 +++++++++++++++++ crates/completion/tests/completion.rs | 144 ++++++++++++++ install.sh | 13 +- src/cli.rs | 10 +- src/clients/flight_sql.rs | 81 +++++--- src/clients/flight_sql/tables.rs | 50 +++++ src/lib.rs | 16 ++ src/terminal.rs | 5 + src/terminal/completion.rs | 243 ++++++++++++++++++++++++ src/terminal/connection.rs | 1 + src/terminal/menu.rs | 4 + src/terminal/recall.rs | 2 + src/terminal/view.rs | 1 + tests/flight_sql.rs | 159 +++++++++++++++- tests/support/completion.rs | 121 ++++++++++++ tests/support/terminal.rs | 144 ++++++++++++++ ui/query/suggestions.html | 18 ++ ui/terminal.css | 38 ++++ ui/workspace.html | 15 +- 28 files changed, 2056 insertions(+), 143 deletions(-) create mode 100644 crates/completion/Cargo.toml create mode 100644 crates/completion/src/catalog.rs create mode 100644 crates/completion/src/cursor.rs create mode 100644 crates/completion/src/lib.rs create mode 100644 crates/completion/src/query.rs create mode 100644 crates/completion/tests/completion.rs create mode 100644 src/clients/flight_sql/tables.rs create mode 100644 src/terminal/completion.rs create mode 100644 tests/support/completion.rs create mode 100644 tests/support/terminal.rs create mode 100644 ui/query/suggestions.html diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 17b76ff..9b6d930 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -26,4 +26,4 @@ jobs: - name: Install and run the executable run: | cargo install --path . --locked --debug --root "$RUNNER_TEMP/sql-bomb" - "$RUNNER_TEMP/sql-bomb/bin/sql-bomb" --help + "$RUNNER_TEMP/sql-bomb/bin/boom" --help diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 396a9da..9f53a3d 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -30,6 +30,7 @@ jobs: timeout-minutes: 60 env: TARGET: ${{ matrix.target }} + EXECUTABLE: boom steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@stable @@ -57,7 +58,7 @@ jobs: sudo apt-get update sudo apt-get install --yes musl-tools echo "CC=musl-gcc" >> "$GITHUB_ENV" - - run: cargo build --locked --release --target "$TARGET" --bin sql-bomb + - run: cargo build --locked --release --target "$TARGET" --bin "$EXECUTABLE" - name: Package and verify the executable env: VERSION: ${{ steps.version.outputs.version }} @@ -66,14 +67,14 @@ jobs: staging="$RUNNER_TEMP/$name" artifacts="$RUNNER_TEMP/artifacts/v$VERSION" mkdir -p "$staging" "$artifacts" - cp "target/$TARGET/release/sql-bomb" LICENSE README.md "$staging/" + cp "target/$TARGET/release/$EXECUTABLE" LICENSE README.md "$staging/" COPYFILE_DISABLE=1 tar -czf "$artifacts/$name.tar.gz" -C "$RUNNER_TEMP" "$name" cd "$artifacts" shasum -a 256 "$name.tar.gz" > "$name.tar.gz.sha256" export SQL_BOMB_DOWNLOAD_BASE="file://$RUNNER_TEMP/artifacts" export SQL_BOMB_INSTALL="$RUNNER_TEMP/installed sql-bomb" sh "$GITHUB_WORKSPACE/install.sh" "v$VERSION" - "$SQL_BOMB_INSTALL/bin/sql-bomb" --help + "$SQL_BOMB_INSTALL/bin/$EXECUTABLE" --help cp "$name.tar.gz" "$RUNNER_TEMP/intact.tar.gz" printf 'corrupt download' >> "$name.tar.gz" if sh "$GITHUB_WORKSPACE/install.sh" "$VERSION" > "$RUNNER_TEMP/rejected.log" 2>&1; then @@ -83,7 +84,7 @@ jobs: cat "$RUNNER_TEMP/rejected.log" grep -Fx 'error: checksum mismatch; download the release again' \ "$RUNNER_TEMP/rejected.log" - cmp "$staging/sql-bomb" "$SQL_BOMB_INSTALL/bin/sql-bomb" + cmp "$staging/$EXECUTABLE" "$SQL_BOMB_INSTALL/bin/$EXECUTABLE" mv "$RUNNER_TEMP/intact.tar.gz" "$name.tar.gz" - uses: actions/upload-artifact@v4 with: diff --git a/Cargo.lock b/Cargo.lock index d9c1929..f3e3b13 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -455,6 +455,12 @@ version = "0.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + [[package]] name = "bitflags" version = "2.13.2" @@ -530,9 +536,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.6.0" +version = "1.2.67" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f74872d07caf508b30a21f6836e7d7016a2eaf7d9ff4f48deaa58cd8a0407630" +checksum = "e17dd265a7d0f31ef544e1b20e03add05d3b45b491b633b10d67145d2acc1a38" dependencies = [ "find-msvc-tools", "shlex", @@ -544,6 +550,12 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" +[[package]] +name = "cfg_aliases" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" + [[package]] name = "cfg_aliases" version = "0.2.2" @@ -661,7 +673,7 @@ version = "0.28.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "829d955a0bb380ef178a640b91779e3987da38c9aea133b20614cfed8cdea9c6" dependencies = [ - "bitflags", + "bitflags 2.13.2", "crossterm_winapi", "mio", "parking_lot", @@ -783,6 +795,12 @@ dependencies = [ "subtle", ] +[[package]] +name = "downcast-rs" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75b325c5dbd37f80359721ad39aca5a29fb04c89279657cffdda8736d0c0b9d2" + [[package]] name = "either" version = "1.18.0" @@ -829,7 +847,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.61.2", + "windows-sys 0.52.0", ] [[package]] @@ -870,6 +888,17 @@ version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223" +[[package]] +name = "filedescriptor" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e40758ed24c9b2eeb76c35fb0aebc66c626084edd827e07e1552279814c6682d" +dependencies = [ + "libc", + "thiserror 1.0.69", + "winapi", +] + [[package]] name = "find-msvc-tools" version = "0.1.14" @@ -882,7 +911,7 @@ version = "25.12.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "35f6839d7b3b98adde531effaf34f0c2badc6f4735d26fe74709d8e513a96ef3" dependencies = [ - "bitflags", + "bitflags 2.13.2", "rustc_version", ] @@ -1331,7 +1360,7 @@ dependencies = [ [[package]] name = "hypercmd" version = "0.1.0" -source = "git+https://github.com/fusor-rs/hypercmd?rev=5e4cb1781b3e4aeeeabe8e14f8c600f744da1597#5e4cb1781b3e4aeeeabe8e14f8c600f744da1597" +source = "git+https://github.com/fusor-rs/hypercmd?rev=a910ab9e084df3e352e620b422d177fd0f6d75da#a910ab9e084df3e352e620b422d177fd0f6d75da" dependencies = [ "crossterm", "fusor-async", @@ -1352,7 +1381,7 @@ dependencies = [ [[package]] name = "hypercmd-build" version = "0.1.0" -source = "git+https://github.com/fusor-rs/hypercmd?rev=5e4cb1781b3e4aeeeabe8e14f8c600f744da1597#5e4cb1781b3e4aeeeabe8e14f8c600f744da1597" +source = "git+https://github.com/fusor-rs/hypercmd?rev=a910ab9e084df3e352e620b422d177fd0f6d75da#a910ab9e084df3e352e620b422d177fd0f6d75da" dependencies = [ "fusor-build", "proc-macro2", @@ -1484,6 +1513,12 @@ dependencies = [ "zeroize", ] +[[package]] +name = "lazy_static" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20870f649af7073d53e38067b2a84312175d56ea15217e1b15bc83506ec50afb" + [[package]] name = "lexical-core" version = "1.0.6" @@ -1649,15 +1684,27 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "nix" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab2156c4fce2f8df6c499cc1c763e4394b7482525bf2a9701c9d79d215f519e4" +dependencies = [ + "bitflags 2.13.2", + "cfg-if", + "cfg_aliases 0.1.1", + "libc", +] + [[package]] name = "nix" version = "0.29.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46" dependencies = [ - "bitflags", + "bitflags 2.13.2", "cfg-if", - "cfg_aliases", + "cfg_aliases 0.2.2", "libc", "memoffset", ] @@ -1747,7 +1794,7 @@ version = "0.10.81" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77823a27f0babb03091cb9ed9ef80af3b39dbc82f97e8fa530374b7dafd87a45" dependencies = [ - "bitflags", + "bitflags 2.13.2", "cfg-if", "foreign-types", "libc", @@ -1902,6 +1949,27 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "portable-pty" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4a596a2b3d2752d94f51fac2d4a96737b8705dddd311a32b9af47211f08671e" +dependencies = [ + "anyhow", + "bitflags 1.3.2", + "downcast-rs", + "filedescriptor", + "lazy_static", + "libc", + "log", + "nix 0.28.0", + "serial2", + "shared_library", + "shell-words", + "winapi", + "winreg", +] + [[package]] name = "ppv-lite86" version = "0.2.21" @@ -2012,7 +2080,7 @@ version = "0.29.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eabd94c2f37801c20583fc49dd5cd6b0ba68c716787c2dd6ed18571e1e63117b" dependencies = [ - "bitflags", + "bitflags 2.13.2", "cassowary", "compact_str", "crossterm", @@ -2033,7 +2101,7 @@ version = "0.5.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" dependencies = [ - "bitflags", + "bitflags 2.13.2", ] [[package]] @@ -2085,7 +2153,7 @@ version = "0.33.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1c6d5e5acb6f6129fe3f7ba0a7fc77bca1942cb568535e18e7bc40262baf3110" dependencies = [ - "bitflags", + "bitflags 2.13.2", "fallible-iterator", "fallible-streaming-iterator", "hashlink", @@ -2108,11 +2176,11 @@ version = "0.38.44" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fdb5bc1ae2baa591800df16c9ca78619bf65c0488b41b96ccec5d11220d8c154" dependencies = [ - "bitflags", + "bitflags 2.13.2", "errno", "libc", "linux-raw-sys 0.4.15", - "windows-sys 0.59.0", + "windows-sys 0.52.0", ] [[package]] @@ -2121,11 +2189,11 @@ version = "1.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6fe4565b9518b83ef4f91bb47ce29620ca828bd32cb7e408f0062e9930ba190" dependencies = [ - "bitflags", + "bitflags 2.13.2", "errno", "libc", "linux-raw-sys 0.12.1", - "windows-sys 0.61.2", + "windows-sys 0.52.0", ] [[package]] @@ -2227,7 +2295,7 @@ version = "2.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "897b2245f0b511c87893af39b033e5ca9cce68824c4d7e7630b5a1d339658d02" dependencies = [ - "bitflags", + "bitflags 2.13.2", "core-foundation 0.9.4", "core-foundation-sys", "libc", @@ -2240,7 +2308,7 @@ version = "3.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b7f4bc775c73d9a02cde8bf7b2ec4c9d12743edf609006c7facc23998404cd1d" dependencies = [ - "bitflags", + "bitflags 2.13.2", "core-foundation 0.10.1", "core-foundation-sys", "libc", @@ -2299,6 +2367,7 @@ version = "1.0.151" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c841b55ecdae098c80dcae9cf767f6f8a0c2cdb3416bbef72181df4d0fe73f14" dependencies = [ + "indexmap", "itoa", "memchr", "serde", @@ -2326,6 +2395,17 @@ dependencies = [ "serde", ] +[[package]] +name = "serial2" +version = "0.2.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b16809bc35793b19ce4e0c53924bc0dce3937f15487997cfdaed936004180730" +dependencies = [ + "cfg-if", + "libc", + "windows-sys 0.61.2", +] + [[package]] name = "sha1" version = "0.10.7" @@ -2348,6 +2428,22 @@ dependencies = [ "digest", ] +[[package]] +name = "shared_library" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a9e7e0f2bfae24d8a5b5a66c5b257a83c7412304311512a0c054cd5e619da11" +dependencies = [ + "lazy_static", + "libc", +] + +[[package]] +name = "shell-words" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc6fe69c597f9c37bfeeeeeb33da3530379845f10be461a66d16d03eca2ded77" + [[package]] name = "shlex" version = "2.0.1" @@ -2423,6 +2519,7 @@ dependencies = [ "arrow-array", "arrow-cast", "arrow-flight", + "arrow-ipc", "arrow-schema", "async-stream", "fusor-components", @@ -2431,12 +2528,24 @@ dependencies = [ "hypercmd", "hypercmd-build", "keyring", + "portable-pty", "prost", "rusqlite", - "thiserror", + "sql-bomb-completion", + "thiserror 2.0.21", "tokio", "tokio-stream", "tonic", + "vt100", +] + +[[package]] +name = "sql-bomb-completion" +version = "0.1.0" +dependencies = [ + "thiserror 2.0.21", + "tree-sitter", + "tree-sitter-sequel", ] [[package]] @@ -2445,6 +2554,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +[[package]] +name = "streaming-iterator" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b2231b7c3057d5e4ad0156fb3dc807d900806020c5ffa3ee6ff2c8c76fb8520" + [[package]] name = "strsim" version = "0.11.1" @@ -2529,7 +2644,16 @@ dependencies = [ "getrandom 0.3.4", "once_cell", "rustix 1.1.4", - "windows-sys 0.61.2", + "windows-sys 0.52.0", +] + +[[package]] +name = "thiserror" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" +dependencies = [ + "thiserror-impl 1.0.69", ] [[package]] @@ -2538,7 +2662,18 @@ version = "2.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09e52cb86a36cede5cb101bf8908837b3e4c6e5e59fe7fd85c23fb56200d189e" dependencies = [ - "thiserror-impl", + "thiserror-impl 2.0.21", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", ] [[package]] @@ -2797,6 +2932,36 @@ dependencies = [ "once_cell", ] +[[package]] +name = "tree-sitter" +version = "0.25.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78f873475d258561b06f1c595d93308a7ed124d9977cb26b148c2084a4a3cc87" +dependencies = [ + "cc", + "regex", + "regex-syntax", + "serde_json", + "streaming-iterator", + "tree-sitter-language", +] + +[[package]] +name = "tree-sitter-language" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca0d1bf6fdd806e43ae5198f82f527056d359def39e54e67a0f478ac09dac081" + +[[package]] +name = "tree-sitter-sequel" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d198ad3c319c02e43c21efa1ec796b837afcb96ffaef1a40c1978fbdcec7d17" +dependencies = [ + "cc", + "tree-sitter-language", +] + [[package]] name = "try-lock" version = "0.2.5" @@ -2861,6 +3026,12 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "vcpkg" version = "0.2.15" @@ -2873,6 +3044,39 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" +[[package]] +name = "vt100" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84cd863bf0db7e392ba3bd04994be3473491b31e66340672af5d11943c6274de" +dependencies = [ + "itoa", + "log", + "unicode-width 0.1.14", + "vte", +] + +[[package]] +name = "vte" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f5022b5fbf9407086c180e9557be968742d839e68346af7792b8592489732197" +dependencies = [ + "arrayvec", + "utf8parse", + "vte_generate_state_changes", +] + +[[package]] +name = "vte_generate_state_changes" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e369bee1b05d510a7b4ed645f5faa90619e05437111783ea5848f28d97d3c2e" +dependencies = [ + "proc-macro2", + "quote", +] + [[package]] name = "want" version = "0.3.1" @@ -3132,6 +3336,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "winreg" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80d0f4e272c85def139476380b12f9ac60926689dd2e01d4923222f40580869d" +dependencies = [ + "winapi", +] + [[package]] name = "wit-bindgen" version = "0.57.1" @@ -3164,7 +3377,7 @@ dependencies = [ "futures-sink", "futures-util", "hex", - "nix", + "nix 0.29.0", "ordered-stream", "rand", "serde", diff --git a/Cargo.toml b/Cargo.toml index e07ba4b..acf4242 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,9 @@ [workspace] -members = ["."] +members = [".", "crates/completion"] resolver = "3" [workspace.dependencies] +thiserror = "=2.0.21" tokio = { version = "=1.48.0", features = ["rt-multi-thread", "time"] } [package] @@ -12,6 +13,10 @@ edition = "2024" license = "MIT" description = "A terminal workspace for SQL queries and streaming results" +[[bin]] +name = "boom" +path = "src/main.rs" + [package.metadata.hypercmd] entry = "ui/app.html" templates = ["ui"] @@ -21,6 +26,7 @@ styles = ["ui/terminal.css"] arrow-array = "=58.3.0" arrow-cast = "=58.3.0" arrow-flight = { version = "=58.3.0", features = ["flight-sql"] } +arrow-ipc = "=58.3.0" arrow-schema = "=58.3.0" async-stream = "=0.3.6" fusor-components = { version = "=0.1.4", default-features = false } @@ -29,7 +35,8 @@ keyring = { version = "=3.6.3", features = [ "apple-native", "sync-secret-service", "crypto-rust", "vendored", ] } rusqlite = { version = "=0.33.0", features = ["bundled"] } -thiserror = "=2.0.21" +sql-bomb-completion = { path = "crates/completion" } +thiserror.workspace = true tokio.workspace = true tonic = { version = "=0.14.6", features = ["tls-ring", "tls-native-roots"] } @@ -41,16 +48,18 @@ features = ["derive"] [dependencies.hypercmd] git = "https://github.com/fusor-rs/hypercmd" -rev = "5e4cb1781b3e4aeeeabe8e14f8c600f744da1597" +rev = "a910ab9e084df3e352e620b422d177fd0f6d75da" [build-dependencies.hypercmd-build] git = "https://github.com/fusor-rs/hypercmd" -rev = "5e4cb1781b3e4aeeeabe8e14f8c600f744da1597" +rev = "a910ab9e084df3e352e620b422d177fd0f6d75da" [dev-dependencies] +portable-pty = "=0.9.0" prost = "=0.14.3" tokio = { workspace = true, features = ["macros"] } tokio-stream = { version = "=0.1.19", features = ["net"] } +vt100 = "=0.15.2" [lints] workspace = true diff --git a/README.md b/README.md index 7f573d3..e6da8a3 100644 --- a/README.md +++ b/README.md @@ -10,22 +10,25 @@ Write a query, stream the results, and keep the SQL worth running again. -sql-bomb is a SQL terminal built in Rust, with pluggable clients and an interface powered by +sql-bomb is a database-agnostic SQL terminal built in Rust, with pluggable clients +and an interface powered by [hypercmd](https://github.com/fusor-rs/hypercmd) and [fusor](https://github.com/fusor-rs/fusor). Browse results as they arrive, inspect complete values, and return to queries from your local history or starred library. -**In development · v0.1** — Flight SQL is the first supported client. See +Install sql-bomb, then launch it with `boom`. + +**In development · v0.1** — See [available clients](#clients) and [status and known limitations](#status-and-known-limitations). [Get started](#get-started) · [Using sql-bomb](#using-sql-bomb) · -[How it works](#how-it-works) · [Status](#status-and-known-limitations) · +[Clients](#clients) · [How it works](#how-it-works) · [Status](#status-and-known-limitations) · [Contributing](#contributing) ## Get started -You need an interactive terminal on macOS or Linux and a server supported by an -available client. Flight SQL is the first available client. +You need an interactive terminal on macOS or Linux and a connection supported by +an [available client](#clients). The shell installer downloads the latest published release for x86-64 or ARM64, verifies its SHA-256 checksum, and installs it in `~/.sqlbomb/bin` without sudo or a Rust toolchain: @@ -33,7 +36,7 @@ or a Rust toolchain: ```sh curl -fsSL https://raw.githubusercontent.com/fusor-rs/sql-bomb/main/install.sh | sh export PATH="$HOME/.sqlbomb/bin:$PATH" -sql-bomb +boom ``` Add that PATH export to your shell profile to keep it across sessions. Run the @@ -53,7 +56,7 @@ To install the executable from this checkout: ```sh cargo install --path . --locked -sql-bomb +boom ``` Choose a client from the centered cards with a click or Enter, fill in its @@ -68,42 +71,13 @@ Write SQL in the editor and click **Run**, or press Ctrl+R: SELECT 1 AS value; ``` -To skip client selection, use a client flag. Adding a connection address also -skips its form: - -```sh -sql-bomb --flightsql -sql-bomb --flightsql http://localhost:50051 -``` - -A bare address, such as `sql-bomb http://localhost:50051`, prefills the connection -form after you choose a client. **Connection** opens your saved connections, or -the client chooser if you have none. Network connections open when you run a query. +**Connection** opens your saved connections, or the client chooser if you have +none. Opening the workspace loads available table metadata in the background; +running a query opens its query connection. For direct launches, see +the [client-specific flags and connection settings](#clients). ## Using sql-bomb -### Configure a client - -The chooser lists available clients. Choose one to open its connection form. - -**Flight SQL** takes a server URL, including its scheme and port, such as -`http://localhost:50051` or `https://sql.example.com:443`. The form also accepts -optional request headers, one `name: value` per line: - -```text -authorization: Bearer YOUR_TOKEN -x-tenant: YOUR_TENANT -``` - -Server URLs contain only the scheme, host, and port; put authentication in headers, -not URL credentials, paths, or query parameters. Headers stay in memory unless you -explicitly remember them in the OS credential store. They are never saved in query -history. Headers are sent to the configured server and locations with the same scheme, -host, and port. They are not forwarded to other servers. Header names and values -must be ASCII; binary metadata is not supported. See the -[Arrow Flight protocol](https://arrow.apache.org/docs/format/Flight.html) for -endpoint and authentication conventions. - ### Save and reopen connections Check **Save on this device** in the connection form. Give it a name @@ -111,19 +85,14 @@ Check **Save on this device** in the connection form. Give it a name unchecked for a session-only connection. A direct CLI launch remains unsaved; **Menu → Save connection…** saves it without clearing SQL or results. -When saved connections exist, launching `sql-bomb` opens **Connections**. Search +When saved connections exist, launching `boom` opens **Connections**. Search by name, client, or address; recently opened profiles appear first. Click a row, or use Up/Down and Enter, to open it. **New connection**, **Edit**, and **Remove** are visible actions. Editing requires **Save changes**; Escape discards the edit. Removal asks for confirmation and keeps query history, stars, and the active workspace. -Profiles live in `~/.sqlbomb/queries.sqlite3`. Header values are separate: check -**Remember headers securely** to use macOS Keychain or Linux Secret -Service. This is off by default. Otherwise, reopening a profile that used headers -asks for them again. Renaming a session-only profile without entering headers -keeps that prompt. Linux credential storage requires an unlocked Secret Service -provider on the session D-Bus. Credential-store failures are shown in the form; -headers are never written to a plaintext fallback. +Profiles live in `~/.sqlbomb/queries.sqlite3`. Authentication and credential +storage options are described in the [client settings](#clients). ### Find an action @@ -136,10 +105,32 @@ menu. In text fields, `/` remains ordinary text. The menu search accepts names such as `/history`. Escape restores the previous pane, including the editor's cursor and selection. Queries continue streaming while the menu is open. +### SQL suggestions + +Clients that implement table discovery provide suggestions as you type. After +`FROM` or `JOIN`, the editor suggests tables from the active connection, including +catalog and schema qualifiers. In column positions, it suggests columns from +referenced tables and aliases, with their Arrow types and source tables. + +Use Up/Down to choose a suggestion, then Tab or a click to insert it. Insertion +replaces the identifier at the caret and preserves the rest of the query. Escape +closes suggestions; Enter inserts a newline. Suggestions stay out of comments +and string literals. Quoted identifiers preserve their quoting style. +The list shows a bounded set of matches; keep typing to narrow it. + +Schema metadata is held in memory for the active connection. **Refresh schema** +reloads it after database changes or a discovery failure. Loading and failures +appear beneath the editor; typing and executing SQL remain available. Clients +without discovery work without suggestions. + +Completion resolves catalog tables and aliases within the current statement or +subquery. Columns derived from CTEs, subquery projections, and set operations are +not resolved. Ambiguous table references do not produce guessed column lists. + ### Explore results as they arrive The multiline editor preserves pasted SQL, including comments and string -literals. Results appear as Arrow batches arrive. Column headers stay visible, +literals. Results appear as rows arrive. Column headers stay visible, the selected cell is highlighted, and the footer shows your row and column. Use the arrow keys to browse or the mouse wheel to scroll. @@ -214,6 +205,61 @@ Modifier-click gestures belong to the terminal emulator. +## Clients + +Clients provide connection settings and query execution. The editor, result +browser, saved connections, and query library share the same workspace regardless +of the selected client. + +Flight SQL is currently the only implemented client. PostgreSQL and MySQL clients +are not implemented. + +### Flight SQL + +Choose **Flight SQL** in the client chooser, or use its flag to skip selection. +Adding a connection address also skips the form: + +```sh +boom --flightsql +boom --flightsql http://localhost:50051 +``` + +A bare address, such as `boom http://localhost:50051`, prefills the connection +form after you choose a client. + +Flight SQL takes a server URL, including its scheme and port, such as +`http://localhost:50051` or `https://sql.example.com:443`. Use `http://` or +`grpc+tcp://` for plaintext and `https://` or `grpc+tls://` for TLS with system +roots. The form also accepts optional request headers, one `name: value` per line: + +```text +authorization: Bearer YOUR_TOKEN +x-tenant: YOUR_TENANT +``` + +Server URLs contain only the scheme, host, and port; put authentication in headers, +not URL credentials, paths, or query parameters. Headers are sent to the configured +server and locations with the same scheme, host, and port. They are not forwarded +to other servers. Header names and values must be ASCII; binary metadata is not +supported. Authentication uses supplied headers; handshake authentication and +mutual TLS are not implemented. See the +[Arrow Flight protocol](https://arrow.apache.org/docs/format/Flight.html) for +endpoint and authentication conventions. + +Headers stay in memory unless you check **Remember headers securely** when saving +a connection. This uses macOS Keychain or Linux Secret Service and is off by +default. Otherwise, reopening a profile that used headers asks for them again. +Renaming a session-only profile without entering headers keeps that prompt. Linux +credential storage requires an unlocked Secret Service provider on the session +D-Bus. Credential-store failures are shown in the form; headers are never saved +in query history or written to a plaintext fallback. + +Each query opens a gRPC connection, executes a statement, and reads all returned +endpoints in order. For advertised endpoint locations, the first location is used. +The optional `QueryClient::tables()` API uses `CommandGetTables` with schemas +included. It returns each table's catalog, database schema, name, type, and Arrow +column schema. + ## How it works - **Pluggable clients.** The public [QueryClient trait](src/lib.rs) returns a @@ -221,41 +267,38 @@ Modifier-click gestures belong to the terminal emulator. this interface; each client implements its own protocol. The [client registry](src/client.rs) supplies the chooser, CLI flags, and connection setup. Network work runs in a Tokio runtime. -- **Arrow from server to table.** Results remain typed record batches. The UI +- **Typed results.** Clients return Arrow record batches. The UI renders a window of the received rows and columns without discarding older rows. - **HTML views, Rust state.** Fusor's reactive bindings connect the [HTML template](ui/app.html) to [Rust state](src/terminal.rs). Hypercmd handles terminal layout, focus, and input. -- **Flight SQL adapter.** Each query opens a gRPC connection, executes a statement, - and reads all returned endpoints in order. For advertised endpoint locations, - the first location is used. +- **SQL completion.** The independent [completion crate](crates/completion) + uses Tree-sitter's SQL grammar and client-provided metadata. It runs in process + and returns text edits; it does not start a language server or execute SQL. ## Status and known limitations -sql-bomb is in early development. Flight SQL is the first implemented client; -the shared workspace is independent of that protocol. Queries currently target -statements that return rows. +sql-bomb is in early development. Queries currently target statements that return +rows. Client availability and protocol-specific limits are listed under [Clients](#clients). | Capability | Status | | --- | --- | -| Flight SQL | Streaming results, cancellation, and cell inspection | -| Flight SQL connections | Plaintext or TLS with system roots; optional request headers | +| Query workspace | Multiline SQL editor, searchable action menu, keyboard and mouse controls | +| SQL suggestions | Qualified tables and columns from referenced tables and aliases | +| Results | Streaming table, cancellation, and full cell inspection | | Query library | Local history and starred queries in SQLite | | Saved connections | Searchable local profiles; optional OS credential storage | | Terminal platforms | macOS and Linux | -| PostgreSQL and MySQL clients | Not implemented | -| Flight SQL authentication | Supplied authorization headers; no handshake or mutual TLS | -Flight SQL endpoints accept `http://` or `grpc+tcp://` for plaintext and `https://` or -`grpc+tls://` for TLS. Received batches stay in memory until another query starts -or the connection changes. Cancellation closes the client stream; it does not -send a server-side SQL cancellation command. +Received batches stay in memory until another query starts or the connection +changes. Cancellation closes the client stream; it does not send a server-side +SQL cancellation command. ## Contributing Read [AGENTS.md](AGENTS.md) for the repository's coding and review rules. The -client registry lives in `src/client.rs`, the Flight SQL adapter in -`src/clients/flight_sql.rs`, the terminal state in `src/terminal/`, and the views in `ui/`. +client registry lives in `src/client.rs`, client adapters in `src/clients/`, +terminal state in `src/terminal/`, and views in `ui/`. Connection views live together in `ui/connections/`. `ui/app.html` composes them with the workspace, library, inspector, and menu templates. Their shared terminal input is registered in @@ -272,6 +315,8 @@ cargo test --workspace --locked The Flight SQL integration test checks delivery before completion, multiple endpoints, request headers, nullable Unicode values, empty results, and errors. +It also launches `boom` in a terminal to verify metadata discovery, Tab and mouse +completion, caret placement, dismissal, and editing after a metadata failure. UI tests cover client selection, saved connections, credential prompts, history, stars, search, menu navigation, responsive layout, and draft recovery. Storage tests check that profiles exclude secrets and failed saves preserve credentials. @@ -284,7 +329,7 @@ macOS for pushes and pull requests, then installs and launches the executable. Publish a GitHub release with a tag matching the package version in `Cargo.toml`, such as `v0.1.0`. The [Release workflow](.github/workflows/release.yml) runs Check, validates the tag, and builds Linux musl and macOS executables for x86-64 and ARM64. -Each archive contains `sql-bomb`, `LICENSE`, and `README.md` inside a directory +Each archive contains `boom`, `LICENSE`, and `README.md` inside a directory named `sql-bomb-VERSION-TARGET`. Archives use that name with `.tar.gz`, accompanied by a `.tar.gz.sha256` checksum file. Each build uses [install.sh](install.sh) to verify, install, and run the executable before any archives are attached to the diff --git a/crates/completion/Cargo.toml b/crates/completion/Cargo.toml new file mode 100644 index 0000000..0cfab25 --- /dev/null +++ b/crates/completion/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "sql-bomb-completion" +version = "0.1.0" +edition = "2024" +license = "MIT" + +[dependencies] +thiserror.workspace = true +tree-sitter = "=0.25.10" +tree-sitter-sequel = "=0.3.11" + +[lints] +workspace = true diff --git a/crates/completion/src/catalog.rs b/crates/completion/src/catalog.rs new file mode 100644 index 0000000..51be4a4 --- /dev/null +++ b/crates/completion/src/catalog.rs @@ -0,0 +1,153 @@ +use std::borrow::Cow; + +use crate::cursor::{closing_quote, identifier_character, unescape_identifier}; +use crate::{Column, Cursor, Kind, Suggestion, Table}; + +pub(crate) struct Identifier<'sql> { + pub(crate) text: Cow<'sql, str>, + quoted: bool, +} + +impl<'sql> Identifier<'sql> { + pub(crate) fn parse(text: &'sql str) -> Self { + let Some(quote) = text + .chars() + .next() + .filter(|quote| matches!(quote, '"' | '`' | '[')) + else { + return Self { + text: Cow::Borrowed(text), + quoted: false, + }; + }; + let closing = closing_quote(quote); + let text = &text[quote.len_utf8()..text.len() - closing.len_utf8()]; + Self { + text: Cow::Owned(unescape_identifier(text, quote)), + quoted: true, + } + } + + pub(crate) fn matches(&self, name: &str) -> bool { + if self.quoted { + self.text == name + } else { + self.text.eq_ignore_ascii_case(name) + } + } +} + +impl Table { + pub(crate) fn path(&self) -> Vec<&str> { + self.catalog + .as_deref() + .into_iter() + .chain(self.database_schema.as_deref()) + .chain(std::iter::once(self.name.as_str())) + .collect() + } + + pub(crate) fn matches(&self, names: &[Identifier<'_>]) -> bool { + let path = self.path(); + names.len() <= path.len() + && names + .iter() + .zip(&path[path.len() - names.len()..]) + .all(|(name, part)| name.matches(part)) + } + + pub(crate) fn suggestion( + &self, + names: &[Identifier<'_>], + cursor: &Cursor, + ) -> Option { + let path = self.path(); + let remaining = if names.is_empty() { + if !cursor.matches(&self.name) && !cursor.matches(path[0]) { + return None; + } + path.as_slice() + } else { + let start = (0..path.len()).find(|start| { + let suffix = &path[*start..]; + names.len() < suffix.len() + && names + .iter() + .zip(suffix) + .all(|(name, part)| name.matches(part)) + && cursor.matches(suffix[names.len()]) + })?; + &path[start + names.len()..] + }; + Some(Suggestion { + label: path.join("."), + detail: "Table".to_owned(), + insertion: quoted_path(remaining, cursor.quote), + kind: Kind::Table, + replacement: cursor.replacement.clone(), + }) + } +} + +impl Cursor { + pub(crate) fn matches(&self, name: &str) -> bool { + match self.quote { + Some(_) => name.starts_with(&self.prefix), + None => name.to_lowercase().starts_with(&self.prefix), + } + } +} + +pub(crate) fn quoted_path(path: &[&str], quote: Option) -> String { + path.iter() + .map(|part| { + if quote.is_none() && plain_identifier(part) { + return (*part).to_owned(); + } + let quote = quote.unwrap_or('"'); + let closing = closing_quote(quote); + let escaped = part.replace(closing, &closing.to_string().repeat(2)); + format!("{quote}{escaped}{closing}") + }) + .collect::>() + .join(".") +} + +fn plain_identifier(name: &str) -> bool { + let language: tree_sitter::Language = tree_sitter_sequel::LANGUAGE.into(); + name.chars() + .next() + .is_some_and(|character| character.is_ascii_alphabetic() || character == '_') + && name + .chars() + .all(|character| character.is_ascii() && identifier_character(character)) + && language.id_for_node_kind(&format!("keyword_{}", name.to_ascii_lowercase()), true) == 0 +} + +pub(crate) fn column_suggestion( + column: &Column, + table: &Table, + qualifier: &[&str], + cursor: &Cursor, +) -> Suggestion { + let insertion = if qualifier.is_empty() { + quoted_path(&[column.name.as_str()], cursor.quote) + } else { + format!( + "{}.{}", + quoted_path(qualifier, None), + quoted_path(&[column.name.as_str()], cursor.quote) + ) + }; + Suggestion { + label: if qualifier.is_empty() { + column.name.clone() + } else { + insertion.clone() + }, + detail: format!("{} · {}", column.data_type, table.path().join(".")), + insertion, + kind: Kind::Column, + replacement: cursor.replacement.clone(), + } +} diff --git a/crates/completion/src/cursor.rs b/crates/completion/src/cursor.rs new file mode 100644 index 0000000..a43b5d5 --- /dev/null +++ b/crates/completion/src/cursor.rs @@ -0,0 +1,180 @@ +use std::ops::Range; + +const LINE_COMMENT: &str = "--"; +const BLOCK_COMMENT_START: &str = "/*"; +const BLOCK_COMMENT_END: &str = "*/"; + +struct Comment { + end: usize, + closed: bool, +} + +pub(crate) struct Cursor { + pub(crate) replacement: Range, + pub(crate) prefix: String, + pub(crate) quote: Option, +} + +pub(crate) fn at(sql: &str, position: usize) -> Option { + let mut offset = 0; + while offset < sql.len() && offset <= position { + let remaining = &sql[offset..]; + let character = remaining.chars().next()?; + if let Some(comment) = comment(remaining) { + let end = offset + comment.end; + if position < end || position == end && !comment.closed { + return None; + } + offset = end; + continue; + } + if let Some(delimiter) = dollar_delimiter(remaining) { + let end = remaining[delimiter.len()..] + .find(delimiter) + .map_or(sql.len(), |end| offset + delimiter.len() * 2 + end); + if position <= end { + return None; + } + offset = end; + continue; + } + if matches!(character, '\'' | '"' | '`' | '[') { + let closing = quote_ending(remaining, character); + let end = closing.end + offset; + if (offset..=end).contains(&position) { + return quoted( + sql, + offset..end, + position, + character, + offset + closing.start, + ); + } + offset = end; + continue; + } + if identifier_character(character) { + let length = remaining + .find(|character| !identifier_character(character)) + .unwrap_or(remaining.len()); + let end = offset + length; + if (offset..=end).contains(&position) { + return (!character.is_numeric()).then_some(Cursor { + replacement: offset..end, + prefix: sql[offset..position].to_lowercase(), + quote: None, + }); + } + offset = end; + continue; + } + offset += character.len_utf8(); + } + Some(Cursor { + replacement: position..position, + prefix: String::new(), + quote: None, + }) +} + +fn quoted( + sql: &str, + range: Range, + position: usize, + quote: char, + content_end: usize, +) -> Option { + if quote == '\'' || position == range.start { + return None; + } + let prefix = unescape_identifier( + &sql[range.start + quote.len_utf8()..position.min(content_end)], + quote, + ); + Some(Cursor { + replacement: range, + prefix, + quote: Some(quote), + }) +} + +pub(crate) fn unescape_identifier(text: &str, quote: char) -> String { + let closing = closing_quote(quote).to_string(); + text.replace(&closing.repeat(2), &closing) +} + +pub(crate) fn closing_quote(quote: char) -> char { + if quote == '[' { ']' } else { quote } +} + +pub(crate) fn identifier_character(character: char) -> bool { + character.is_alphanumeric() || matches!(character, '_' | '$') +} + +fn quote_ending(sql: &str, quote: char) -> Range { + let closing = closing_quote(quote); + let mut characters = sql.char_indices().skip(1).peekable(); + while let Some((offset, character)) = characters.next() { + if character == '\\' && quote == '\'' { + characters.next(); + continue; + } + if character != closing { + continue; + } + if characters.peek().is_some_and(|(_, next)| *next == closing) { + characters.next(); + continue; + } + return offset..offset + character.len_utf8(); + } + sql.len()..sql.len() +} + +fn comment(sql: &str) -> Option { + if sql.starts_with(LINE_COMMENT) { + return Some(Comment { + end: sql.find('\n').unwrap_or(sql.len()), + closed: false, + }); + } + if !sql.starts_with(BLOCK_COMMENT_START) { + return None; + } + let mut depth = 1; + let mut offset = BLOCK_COMMENT_START.len(); + while offset < sql.len() { + let remaining = &sql[offset..]; + if remaining.starts_with(BLOCK_COMMENT_START) { + depth += 1; + offset += BLOCK_COMMENT_START.len(); + } else if remaining.starts_with(BLOCK_COMMENT_END) { + depth -= 1; + offset += BLOCK_COMMENT_END.len(); + if depth == 0 { + return Some(Comment { + end: offset, + closed: true, + }); + } + } else { + offset += 1; + while !sql.is_char_boundary(offset) { + offset += 1; + } + } + } + Some(Comment { + end: sql.len(), + closed: false, + }) +} + +fn dollar_delimiter(sql: &str) -> Option<&str> { + let remaining = sql.strip_prefix('$')?; + let end = remaining.find('$')?; + remaining[..end] + .chars() + .all(|character| character.is_ascii_alphanumeric() || character == '_') + .then_some(&sql[..end + 2]) +} diff --git a/crates/completion/src/lib.rs b/crates/completion/src/lib.rs new file mode 100644 index 0000000..3e4524e --- /dev/null +++ b/crates/completion/src/lib.rs @@ -0,0 +1,153 @@ +use std::ops::Range; + +#[derive(Debug)] +pub struct Column { + pub name: String, + pub data_type: String, +} + +#[derive(Debug)] +pub struct Table { + pub catalog: Option, + pub database_schema: Option, + pub name: String, + pub columns: Vec, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Kind { + Table, + Column, +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Suggestion { + pub label: String, + pub detail: String, + pub insertion: String, + pub kind: Kind, + pub replacement: Range, +} + +mod catalog; +mod cursor; +mod query; + +use cursor::Cursor; +use tree_sitter::{InputEdit, Parser, Point, Tree}; + +const COMPLETION_IDENTIFIER: &str = "__sql_bomb_completion__"; + +#[derive(Debug, thiserror::Error)] +pub enum Error { + #[error("Could not load the SQL grammar: {0}")] + Grammar(#[from] tree_sitter::LanguageError), + #[error("Cursor {0} is not a UTF-8 boundary in the SQL text; use the editor's byte offset")] + Cursor(usize), + #[error("SQL parsing was interrupted; edit the query and try completion again")] + Interrupted, +} + +pub struct Engine { + parser: Parser, + text: String, + tree: Option, +} + +impl Engine { + pub fn new() -> Result { + let mut parser = Parser::new(); + parser.set_language(&tree_sitter_sequel::LANGUAGE.into())?; + Ok(Self { + parser, + text: String::new(), + tree: None, + }) + } + + /// Cursor and replacement ranges are UTF-8 byte offsets into `sql`. + pub fn complete( + &mut self, + sql: &str, + cursor: usize, + tables: &[Table], + ) -> Result, Error> { + if !sql.is_char_boundary(cursor) { + return Err(Error::Cursor(cursor)); + } + let Some(cursor) = cursor::at(sql, cursor) else { + return Ok(Vec::new()); + }; + let mut text = String::with_capacity(sql.len() + COMPLETION_IDENTIFIER.len()); + text.push_str(&sql[..cursor.replacement.start]); + text.push_str(COMPLETION_IDENTIFIER); + text.push_str(&sql[cursor.replacement.end..]); + self.parse(text)?; + let tree = self + .tree + .as_ref() + .expect("parse stores a syntax tree on success"); + let target = tree.root_node().descendant_for_byte_range( + cursor.replacement.start, + cursor.replacement.start + COMPLETION_IDENTIFIER.len(), + ); + let mut suggestions = target.map_or_else(Vec::new, |target| { + query::suggestions(target, &self.text, tables, &cursor) + }); + suggestions.sort_unstable_by(|left, right| left.label.cmp(&right.label)); + suggestions.dedup(); + Ok(suggestions) + } + + fn parse(&mut self, text: String) -> Result<(), Error> { + if text == self.text && self.tree.is_some() { + return Ok(()); + } + let mut previous = self.tree.take(); + if let Some(tree) = &mut previous { + tree.edit(&difference(&self.text, &text)); + } + self.tree = Some( + self.parser + .parse(&text, previous.as_ref()) + .ok_or(Error::Interrupted)?, + ); + self.text = text; + Ok(()) + } +} + +fn difference(previous: &str, text: &str) -> InputEdit { + let start = previous + .chars() + .zip(text.chars()) + .take_while(|(old, new)| old == new) + .map(|(character, _)| character.len_utf8()) + .sum(); + let suffix: usize = previous[start..] + .chars() + .rev() + .zip(text[start..].chars().rev()) + .take_while(|(old, new)| old == new) + .map(|(character, _)| character.len_utf8()) + .sum(); + let old_end = previous.len() - suffix; + let new_end = text.len() - suffix; + InputEdit { + start_byte: start, + old_end_byte: old_end, + new_end_byte: new_end, + start_position: position(&text[..start]), + old_end_position: position(&previous[..old_end]), + new_end_position: position(&text[..new_end]), + } +} + +fn position(prefix: &str) -> Point { + Point { + row: prefix.bytes().filter(|byte| *byte == b'\n').count(), + column: prefix + .rfind('\n') + .map_or(prefix.len(), |offset| prefix.len() - offset - 1), + } +} diff --git a/crates/completion/src/query.rs b/crates/completion/src/query.rs new file mode 100644 index 0000000..05cf46d --- /dev/null +++ b/crates/completion/src/query.rs @@ -0,0 +1,169 @@ +use tree_sitter::Node; + +use crate::catalog::{Identifier, column_suggestion}; +use crate::{Cursor, Suggestion, Table}; + +const IDENTIFIER: &str = "identifier"; +const REFERENCE: &str = "object_reference"; +const RELATION: &str = "relation"; +const FIELD: &str = "field"; +const STATEMENT: &str = "statement"; +const SUBQUERY: &str = "subquery"; +const CTE: &str = "cte"; +const SET_OPERATION: &str = "set_operation"; +const ALIAS: &str = "alias"; +const RELATION_CLAUSES: &[&str] = &["from", "join", "cross_join"]; + +struct Binding<'catalog, 'sql> { + table: &'catalog Table, + alias: Option>, +} + +pub(crate) fn suggestions( + target: Node<'_>, + sql: &str, + tables: &[Table], + cursor: &Cursor, +) -> Vec { + if target.kind() != IDENTIFIER { + return Vec::new(); + } + let Some(parent) = target.parent() else { + return Vec::new(); + }; + if parent.kind() == REFERENCE { + if parent.named_child(parent.named_child_count() - 1) != Some(target) + || parent.parent().is_none_or(|node| node.kind() != RELATION) + { + return Vec::new(); + } + let mut qualifiers = identifiers(parent, sql); + qualifiers.pop(); + return tables + .iter() + .filter_map(|table| table.suggestion(&qualifiers, cursor)) + .collect(); + } + if parent.kind() != FIELD { + return Vec::new(); + } + let Some(scope) = scope(parent) else { + return Vec::new(); + }; + let qualifiers = parent + .named_child(0) + .filter(|node| node.kind() == REFERENCE) + .map_or_else(Vec::new, |reference| identifiers(reference, sql)); + let mut relations = Vec::new(); + collect_relations(scope, &mut relations); + let bindings = relations + .into_iter() + .map(|relation| resolve(relation, sql, tables)) + .collect::>(); + columns(&bindings, &qualifiers, cursor) +} + +fn scope(mut node: Node<'_>) -> Option> { + loop { + match node.kind() { + STATEMENT | SUBQUERY => return Some(node), + SET_OPERATION => return None, + _ => node = node.parent()?, + } + } +} + +fn identifiers<'sql>(node: Node<'_>, sql: &'sql str) -> Vec> { + node.named_children(&mut node.walk()) + .filter(|child| child.kind() == IDENTIFIER) + .map(|child| Identifier::parse(&sql[child.byte_range()])) + .collect() +} + +fn collect_relations<'tree>(node: Node<'tree>, relations: &mut Vec>) { + for child in node.named_children(&mut node.walk()) { + if child.kind() == RELATION { + relations.push(child); + } else if RELATION_CLAUSES.contains(&child.kind()) { + collect_relations(child, relations); + } + } +} + +fn resolve<'catalog, 'sql>( + relation: Node<'_>, + sql: &'sql str, + tables: &'catalog [Table], +) -> Option> { + let reference = relation + .named_child(0) + .filter(|node| node.kind() == REFERENCE)?; + let names = identifiers(reference, sql); + if names.len() == 1 && shadows_table(relation, &names[0], sql) { + return None; + } + let mut candidates = tables.iter().filter(|table| table.matches(&names)); + let table = candidates.next()?; + if candidates.next().is_some() { + return None; + } + Some(Binding { + table, + alias: relation + .child_by_field_name(ALIAS) + .map(|alias| Identifier::parse(&sql[alias.byte_range()])), + }) +} + +fn shadows_table(mut node: Node<'_>, name: &Identifier<'_>, sql: &str) -> bool { + while let Some(parent) = node.parent() { + for child in parent.named_children(&mut parent.walk()) { + if child.kind() != CTE { + continue; + } + if child.named_child(0).is_some_and(|identifier| { + name.matches(&Identifier::parse(&sql[identifier.byte_range()]).text) + }) { + return true; + } + } + node = parent; + } + false +} + +fn columns( + bindings: &[Option>], + qualifiers: &[Identifier<'_>], + cursor: &Cursor, +) -> Vec { + let mut suggestions = Vec::new(); + for binding in bindings.iter().flatten() { + if !qualifiers.is_empty() && !binding.matches(qualifiers) { + continue; + } + let qualifier = if qualifiers.is_empty() && bindings.len() > 1 { + match &binding.alias { + Some(alias) => vec![alias.text.as_ref()], + None => binding.table.path(), + } + } else { + Vec::new() + }; + for column in &binding.table.columns { + if cursor.matches(&column.name) { + suggestions.push(column_suggestion(column, binding.table, &qualifier, cursor)); + } + } + } + suggestions +} + +impl Binding<'_, '_> { + fn matches(&self, qualifiers: &[Identifier<'_>]) -> bool { + match &self.alias { + Some(alias) => qualifiers.len() == 1 && qualifiers[0].matches(&alias.text), + None => self.table.matches(qualifiers), + } + } +} diff --git a/crates/completion/tests/completion.rs b/crates/completion/tests/completion.rs new file mode 100644 index 0000000..9345950 --- /dev/null +++ b/crates/completion/tests/completion.rs @@ -0,0 +1,144 @@ +use sql_bomb_completion::{Column, Engine, Kind, Suggestion, Table}; + +const CASES: &[(&str, &[&str])] = &[ + ("SELECT * FROM ne|", &["atlas.benzinga.news"]), + ( + "SELECT * FROM /* note */|", + &["archive.public.orders", "atlas.benzinga.news"], + ), + ( + "SELECT ti| FROM news n JOIN unknown u ON n.title = u.title", + &["n.title"], + ), + ("SELECT * FROM atlas.b|", &["benzinga.news"]), + ("SELECT * FROM benzinga.ne|", &["news"]), + ("SELECT * FROM atlas.benzinga.|", &["news"]), + ("SELECT * FROM \"atlas\".\"benzinga\".\"ne|", &["\"news\""]), + ("SELECT * FROM news JOIN ord|", &["archive.public.orders"]), + ("SELECT ti| FROM atlas.benzinga.news n", &["title"]), + ("SELECT n.ti| FROM atlas.benzinga.news n", &["title"]), + ("SELECT N.ti| FROM atlas.benzinga.news n", &["title"]), + ("SELECT * FROM news n WHERE n.ti|", &["title"]), + ("SELECT * FROM news n WHERE n.or|", &["\"order\""]), + ("SELECT * FROM news n WHERE n.\"Ré|", &["\"Résumé\""]), + ("SELECT * FROM news n WHERE n.\"ti|\"", &["\"title\""]), + ( + "SELECT * FROM news n WHERE n.\"quote\"\"|", + &["\"quote\"\"name\""], + ), + ("SELECT * FROM news n WHERE n.`ti|`", &["`title`"]), + ("SELECT * FROM news n WHERE n.[ti|]", &["[title]"]), + ( + "SELECT ti| FROM news n; SELECT total FROM orders", + &["title"], + ), + ("SELECT title FROM news; SELECT to| FROM orders", &["total"]), + ( + "SELECT id| FROM news n JOIN orders o ON n.id = o.id", + &["n.id", "o.id"], + ), + ( + "SELECT * FROM news n WHERE EXISTS (SELECT to| FROM orders)", + &["total"], + ), + ( + "SELECT * FROM news n WHERE EXISTS (SELECT ti| FROM orders)", + &[], + ), + ("SELECT ti| FROM missing", &[]), + ("SELECT title FROM news; SELECT ti|", &[]), + ("SELECT news.ti| FROM news n", &[]), + ("SELECT x.ti| FROM news n", &[]), + ("SELECT * FROM news WHERE 'ti|", &[]), + ("SELECT * FROM news WHERE 'ti|tle'", &[]), + ("SELECT * FROM news WHERE $$ti|", &[]), + ("SELECT * FROM news WHERE $body$ti|$body$", &[]), + ("SELECT * FROM news WHERE -- ti|", &[]), + ("SELECT * FROM news WHERE /* ti|", &[]), + ("SELECT * FROM news WHERE /* nested /* note */ ti| */", &[]), + ( + "WITH news AS (SELECT id FROM orders) SELECT ti| FROM news", + &[], + ), + ("SELECT ti| FROM news UNION SELECT total FROM orders", &[]), +]; + +#[test] +fn completes_catalog_names_in_the_current_query_scope() { + let tables = catalog(); + let mut engine = Engine::new().unwrap(); + for (marked, expected) in CASES { + let cursor = marked.find('|').unwrap(); + let sql = marked.replace('|', ""); + let suggestions = engine.complete(&sql, cursor, &tables).unwrap(); + let insertions = suggestions + .iter() + .map(|suggestion| suggestion.insertion.as_str()) + .collect::>(); + assert_eq!(&insertions, expected, "{marked}"); + } + assert_eq!( + engine + .complete("SELECT \"Ré\" FROM news", 11, &tables) + .unwrap(), + [Suggestion { + label: "Résumé".to_owned(), + detail: "Utf8 · atlas.benzinga.news".to_owned(), + insertion: "\"Résumé\"".to_owned(), + kind: Kind::Column, + replacement: 7..12, + }] + ); + assert_eq!( + engine + .complete("SELECT title FROM news", 9, &tables) + .unwrap()[0] + .replacement, + 7..12 + ); + assert_eq!( + engine + .complete("SELECT \"Ré\" FROM news", 10, &tables) + .unwrap_err() + .to_string(), + "Cursor 10 is not a UTF-8 boundary in the SQL text; use the editor's byte offset" + ); +} + +fn catalog() -> Vec { + [ + ( + "atlas", + "benzinga", + "news", + &[ + ("id", "Int64"), + ("title", "Utf8"), + ("Résumé", "Utf8"), + ("order", "Utf8"), + ("quote\"name", "Utf8"), + ("quote_other", "Utf8"), + ][..], + ), + ( + "archive", + "public", + "orders", + &[("id", "Int64"), ("total", "Float64")][..], + ), + ] + .into_iter() + .map(|(catalog, schema, name, columns)| Table { + catalog: Some(catalog.to_owned()), + database_schema: Some(schema.to_owned()), + name: name.to_owned(), + columns: columns + .iter() + .map(|(name, data_type)| Column { + name: (*name).to_owned(), + data_type: (*data_type).to_owned(), + }) + .collect(), + }) + .collect() +} diff --git a/install.sh b/install.sh index a2cd947..7cceddf 100755 --- a/install.sh +++ b/install.sh @@ -56,8 +56,9 @@ main() { esac download_base=${SQL_BOMB_DOWNLOAD_BASE:-$releases/download} install_directory=${SQL_BOMB_INSTALL:-$HOME/.sqlbomb} - [ ! -d "$install_directory/bin/sql-bomb" ] || - fail "$install_directory/bin/sql-bomb is a directory; choose another SQL_BOMB_INSTALL" + executable=boom + [ ! -d "$install_directory/bin/$executable" ] || + fail "$install_directory/bin/$executable is a directory; choose another SQL_BOMB_INSTALL" name="sql-bomb-$version-$target" archive="$name.tar.gz" url="$download_base/v$version/$archive" @@ -72,10 +73,10 @@ main() { curl -fsSL "$url.sha256" -o "$temporary/$archive.sha256" || fail "cannot download $url.sha256" verify_archive "$temporary/$archive" - tar -xzf "$temporary/$archive" -C "$temporary" "$name/sql-bomb" - chmod 755 "$temporary/$name/sql-bomb" - mv -f "$temporary/$name/sql-bomb" "$install_directory/bin/sql-bomb" - printf 'Installed sql-bomb v%s to %s/bin\n' "$version" "$install_directory" + tar -xzf "$temporary/$archive" -C "$temporary" "$name/$executable" + chmod 755 "$temporary/$name/$executable" + mv -f "$temporary/$name/$executable" "$install_directory/bin/$executable" + printf 'Installed sql-bomb v%s to %s/bin/%s\n' "$version" "$install_directory" "$executable" case ":$PATH:" in *":$install_directory/bin:"*) ;; *) printf 'Add %s/bin to PATH in your shell profile.\n' "$install_directory" ;; diff --git a/src/cli.rs b/src/cli.rs index 0695ee4..7bca7ed 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,5 +1,7 @@ use crate::client::{CLIENTS, Startup}; +const COMMAND: &str = env!("CARGO_BIN_NAME"); + pub(crate) enum Command { Help, Start(Startup), @@ -21,7 +23,9 @@ pub(crate) fn parse( Some(first) }; if arguments.next().is_some() || address.as_ref().is_some_and(|value| value.starts_with('-')) { - return Err("Unknown option or extra argument; run sql-bomb --help for usage".into()); + return Err( + format!("Unknown option or extra argument; run {COMMAND} --help for usage").into(), + ); } let startup = match (client, address) { (Some(client), Some(address)) => Startup::Connected( @@ -37,8 +41,8 @@ pub(crate) fn parse( pub(crate) fn help() { println!( - "Usage: sql-bomb [CLIENT [CONNECTION]] - sql-bomb [CONNECTION] + "Usage: {COMMAND} [CLIENT [CONNECTION]] + {COMMAND} [CONNECTION] Without arguments, open saved connections or choose a client. A client flag opens its connection form; adding a connection opens the workspace. diff --git a/src/clients/flight_sql.rs b/src/clients/flight_sql.rs index 3cb1596..937a6f6 100644 --- a/src/clients/flight_sql.rs +++ b/src/clients/flight_sql.rs @@ -1,7 +1,11 @@ -use crate::{Error, Query, QueryClient}; +mod tables; + +use crate::{Error, Query, QueryClient, Table}; use arrow_flight::{ - FlightEndpoint, decode::FlightRecordBatchStream, flight_service_client::FlightServiceClient, - sql::client::FlightSqlServiceClient, + FlightEndpoint, FlightInfo, + decode::FlightRecordBatchStream, + flight_service_client::FlightServiceClient, + sql::{CommandGetTables, client::FlightSqlServiceClient}, }; use futures_util::{TryStreamExt, future::BoxFuture}; use std::collections::BTreeMap; @@ -44,33 +48,64 @@ impl FlightSql { self.headers = validated; Ok(()) } + + async fn connect(&self) -> Result, Error> { + let mut client = FlightSqlServiceClient::new(self.endpoint.connect().await?); + for (name, value) in &self.headers { + client.set_header(name, value); + } + Ok(client) + } + + fn read_flight( + &self, + mut client: FlightSqlServiceClient, + mut flight: FlightInfo, + ) -> Result { + let endpoints = std::mem::take(&mut flight.endpoint); + let schema = flight.try_decode_schema()?; + let origin = self.endpoint.clone(); + let batches = async_stream::try_stream! { + for endpoint in endpoints { + let mut batches = fetch_endpoint(&mut client, endpoint, &origin).await?; + while let Some(batch) = batches.try_next().await? { + yield batch; + } + } + }; + Ok(Query { + schema, + batches: Box::pin(batches), + }) + } } impl QueryClient for FlightSql { fn query(&self, sql: String) -> BoxFuture<'_, Result> { Box::pin(async move { - let mut client = FlightSqlServiceClient::new(self.endpoint.connect().await?); - for (name, value) in &self.headers { - client.set_header(name, value); - } - let mut flight = client.execute(sql, None).await?; - let endpoints = std::mem::take(&mut flight.endpoint); - let schema = flight.try_decode_schema()?; - let origin = self.endpoint.clone(); - let batches = async_stream::try_stream! { - for endpoint in endpoints { - let mut batches = fetch_endpoint(&mut client, endpoint, &origin).await?; - while let Some(batch) = batches.try_next().await? { - yield batch; - } - } - }; - Ok(Query { - schema, - batches: Box::pin(batches), - }) + let mut client = self.connect().await?; + let flight = client.execute(sql, None).await?; + self.read_flight(client, flight) }) } + + fn tables(&self) -> Option, Error>>> { + Some(Box::pin(async move { + let mut client = self.connect().await?; + let flight = client + .get_tables(CommandGetTables { + include_schema: true, + ..Default::default() + }) + .await?; + let mut query = self.read_flight(client, flight)?; + let mut tables = Vec::new(); + while let Some(batch) = query.batches.try_next().await? { + tables::append(&batch, &mut tables)?; + } + Ok(tables) + })) + } } fn endpoint(address: &str) -> Result { diff --git a/src/clients/flight_sql/tables.rs b/src/clients/flight_sql/tables.rs new file mode 100644 index 0000000..05e6669 --- /dev/null +++ b/src/clients/flight_sql/tables.rs @@ -0,0 +1,50 @@ +use crate::{Error, Table}; +use arrow_array::{Array, BinaryArray, RecordBatch, StringArray}; +use arrow_ipc::convert::try_schema_from_ipc_buffer; + +const CATALOG_NAME: &str = "catalog_name"; +const DATABASE_SCHEMA_NAME: &str = "db_schema_name"; +const TABLE_NAME: &str = "table_name"; +const TABLE_TYPE: &str = "table_type"; +const TABLE_SCHEMA: &str = "table_schema"; + +pub(super) fn append(batch: &RecordBatch, tables: &mut Vec
) -> Result<(), Error> { + let catalogs = column::(batch, CATALOG_NAME)?; + let database_schemas = column::(batch, DATABASE_SCHEMA_NAME)?; + let names = column::(batch, TABLE_NAME)?; + let table_types = column::(batch, TABLE_TYPE)?; + let schemas = column::(batch, TABLE_SCHEMA)?; + for (name, column) in [ + (TABLE_NAME, names as &dyn Array), + (TABLE_TYPE, table_types), + (TABLE_SCHEMA, schemas), + ] { + if column.null_count() != 0 { + return Err(Error::TableMetadata(name)); + } + } + for row in 0..batch.num_rows() { + tables.push(Table { + catalog: catalogs + .is_valid(row) + .then(|| catalogs.value(row).to_owned()), + database_schema: database_schemas + .is_valid(row) + .then(|| database_schemas.value(row).to_owned()), + name: names.value(row).to_owned(), + table_type: table_types.value(row).to_owned(), + schema: try_schema_from_ipc_buffer(schemas.value(row))?, + }); + } + Ok(()) +} + +fn column<'a, Column: Array + 'static>( + batch: &'a RecordBatch, + name: &'static str, +) -> Result<&'a Column, Error> { + batch + .column_by_name(name) + .and_then(|column| column.as_any().downcast_ref()) + .ok_or(Error::TableMetadata(name)) +} diff --git a/src/lib.rs b/src/lib.rs index bdae309..4c2b7c9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,11 @@ pub use clients::FlightSql; pub trait QueryClient { fn query(&self, sql: String) -> BoxFuture<'_, Result>; + + /// `None` means discovery is unsupported; an empty result means no tables were found. + fn tables(&self) -> Option, Error>>> { + None + } } pub struct Query { @@ -15,6 +20,15 @@ pub struct Query { pub batches: BoxStream<'static, Result>, } +#[derive(Debug, PartialEq)] +pub struct Table { + pub catalog: Option, + pub database_schema: Option, + pub name: String, + pub table_type: String, + pub schema: Schema, +} + #[derive(Debug, thiserror::Error)] pub enum Error { #[error("Invalid connection address; use http://host:port or https://host:port")] @@ -31,6 +45,8 @@ pub enum Error { Flight(#[from] arrow_flight::error::FlightError), #[error("Cannot decode the query result; check the server's Arrow response: {0}")] Arrow(#[from] arrow_schema::ArrowError), + #[error("Invalid table metadata column {0}; check the server's table-discovery response")] + TableMetadata(&'static str), #[error("Server returned an endpoint without a ticket; check the Flight SQL server")] MissingTicket, } diff --git a/src/terminal.rs b/src/terminal.rs index e4dfa48..2f916ba 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -1,3 +1,4 @@ +mod completion; mod connection; mod connections; mod inspection; @@ -6,6 +7,7 @@ mod navigation; mod recall; mod view; mod welcome; +use completion::Completion; use connection::ConnectionDraft; use connections::ConnectionCatalog; use inspection::Inspection; @@ -55,6 +57,7 @@ pub(crate) struct Terminal { owner: OwnerHandle, services: Services, sql: Signal, + completion: Rc, execution: Signal, results: Signal, viewport: Signal, @@ -101,6 +104,7 @@ impl FromInputs for Terminal { services: Services::from_owner(&owner)?, owner, sql: signal(String::new()), + completion: Rc::new(Completion::new()?), execution: signal(Execution::Idle), results: signal(Results::default()), viewport: signal(Viewport::default()), @@ -143,6 +147,7 @@ impl Terminal { if !self.can_run() { return Ok(()); } + self.completion.dismiss(); let sql = self.sql.get(); self.save_query(Change::Executed)?; let (abort, registration) = AbortHandle::new_pair(); diff --git a/src/terminal/completion.rs b/src/terminal/completion.rs new file mode 100644 index 0000000..7941af7 --- /dev/null +++ b/src/terminal/completion.rs @@ -0,0 +1,243 @@ +use super::Terminal; +use fusor::{OwnerHandle, Signal, signal}; +use futures_util::future::{AbortHandle, Abortable}; +use hypercmd::{Error, ErrorKind, Event, EventPayload, Input, Key, Node, Services}; +use sql_bomb::QueryClient; +use sql_bomb_completion::{Column, Engine, Suggestion, Table}; +use std::{ + cell::{Cell, RefCell}, + rc::Rc, +}; + +const MAX_SUGGESTIONS: usize = 20; + +enum Metadata { + Unrequested, + Loading(AbortHandle), + Ready(Vec
), + Unavailable, +} + +pub(super) struct Completion { + engine: RefCell, + metadata: RefCell, + editor: RefCell>, + pub(super) requested: Cell, + pub(super) suggestions: Signal>, + pub(super) selected: Signal, + pub(super) status: Signal, +} + +impl Completion { + pub(super) fn new() -> Result { + Ok(Self { + engine: RefCell::new( + Engine::new() + .map_err(|error| Error::new(ErrorKind::Construction, error.to_string()))?, + ), + metadata: RefCell::new(Metadata::Unrequested), + editor: RefCell::new(None), + requested: Cell::new(false), + suggestions: signal(Vec::new()), + selected: signal(0), + status: signal(String::new()), + }) + } + + pub(super) fn dismiss(&self) { + self.requested.set(false); + self.suggestions.set(Vec::new()); + self.selected.set(0); + } + + pub(super) fn reset(&self) { + if let Metadata::Loading(abort) = self.metadata.replace(Metadata::Unrequested) { + abort.abort(); + } + self.status.set(String::new()); + self.dismiss(); + } + + fn load( + self: &Rc, + client: Rc, + services: &Services, + owner: &OwnerHandle, + ) -> Result<(), Error> { + if !matches!(*self.metadata.borrow(), Metadata::Unrequested) { + return Ok(()); + } + let completion = self.clone(); + let (abort, registration) = AbortHandle::new_pair(); + services.spawn(owner, async move { + let discovery = async { + match client.tables() { + Some(tables) => Some(tables.await), + None => None, + } + }; + // Switching connections cancels discovery before it can publish stale metadata. + let Ok(result) = Abortable::new(discovery, registration).await else { + return; + }; + completion.received(result); + })?; + self.metadata.replace(Metadata::Loading(abort)); + self.status.set("Loading schema…".into()); + Ok(()) + } + + fn received(&self, result: Option, sql_bomb::Error>>) { + let (metadata, status) = match result { + Some(Ok(tables)) => { + let status = format!("{} tables available", tables.len()); + ( + Metadata::Ready(tables.into_iter().map(table).collect()), + status, + ) + } + Some(Err(error)) => ( + Metadata::Unavailable, + format!("Suggestions unavailable: {error}"), + ), + None => ( + Metadata::Unavailable, + "This client does not provide schema suggestions".into(), + ), + }; + self.metadata.replace(metadata); + self.status.set(status); + if !self.requested.get() { + return; + } + if let Some(editor) = self.editor.borrow().as_ref() { + self.refresh(editor); + } + } + + pub(super) fn refresh(&self, editor: &Node) { + let cursor = editor.editor(); + self.dismiss(); + if cursor.anchor.is_some_and(|anchor| anchor != cursor.cursor) { + return; + } + self.requested.set(true); + let metadata = self.metadata.borrow(); + let Metadata::Ready(tables) = &*metadata else { + return; + }; + let suggestions = self + .engine + .borrow_mut() + .complete(&editor.value(), cursor.cursor, tables); + match suggestions { + Ok(mut suggestions) => { + suggestions.truncate(MAX_SUGGESTIONS); + self.selected.set(0); + self.suggestions.set(suggestions); + if let Some(list) = editor.component_root().find("query-suggestions") { + list.scroll_to(0, 0); + } + } + Err(error) => self.status.set(format!("Suggestions unavailable: {error}")), + } + } + + pub(super) fn accept(&self, suggestion: &Suggestion) -> Result<(), Error> { + if let Some(editor) = self.editor.borrow().as_ref() { + editor.replace_range(suggestion.replacement.clone(), &suggestion.insertion)?; + editor.request_focus(); + } + self.dismiss(); + Ok(()) + } +} + +fn table(table: sql_bomb::Table) -> Table { + Table { + catalog: table.catalog, + database_schema: table.database_schema, + name: table.name, + columns: table + .schema + .fields() + .iter() + .map(|field| Column { + name: field.name().clone(), + data_type: field.data_type().to_string(), + }) + .collect(), + } +} + +impl Terminal { + pub(super) fn query_focus(&self, event: &Event) -> Result<(), Error> { + self.remember_focus(event); + self.completion.editor.replace(Some(event.target.clone())); + self.discover_schema() + } + + pub(super) fn query_input(&self, event: &Event) -> Result<(), Error> { + self.refresh_star()?; + self.completion.refresh(&event.target); + Ok(()) + } + + pub(super) fn refresh_schema(&self) -> Result<(), Error> { + self.completion.reset(); + self.discover_schema()?; + self.focus("query"); + Ok(()) + } + + fn discover_schema(&self) -> Result<(), Error> { + self.connection.with(|connection| { + if let Some(connection) = connection { + self.completion + .load(connection.client.clone(), &self.services, &self.owner)?; + } + Ok(()) + }) + } + + pub(super) fn completion_key(&self, event: &Event) -> Result<(), Error> { + let EventPayload::Input(Input::Key { key, modifiers, .. }) = event.payload else { + return Ok(()); + }; + if modifiers.control || modifiers.alt || modifiers.super_key || modifiers.shift { + return Ok(()); + } + let count = self.completion.suggestions.with(Vec::len); + if key == Key::Escape { + self.completion.dismiss(); + if count > 0 { + event.prevent_default(); + } + return Ok(()); + } + if count == 0 { + return Ok(()); + } + match key { + Key::Up | Key::Down => { + let direction = if key == Key::Up { -1 } else { 1 }; + self.completion.selected.update(|selected| { + *selected = selected.saturating_add_signed(direction).min(count - 1); + }); + if let Some(list) = self.find_control("query-suggestions") { + list.scroll_to(0, self.completion.selected.get() as u16); + } + } + Key::Tab => { + let suggestion = self + .completion + .suggestions + .with(|suggestions| suggestions[self.completion.selected.get()].clone()); + self.completion.accept(&suggestion)?; + } + _ => return Ok(()), + } + event.prevent_default(); + Ok(()) + } +} diff --git a/src/terminal/connection.rs b/src/terminal/connection.rs index b920588..2a5d573 100644 --- a/src/terminal/connection.rs +++ b/src/terminal/connection.rs @@ -160,6 +160,7 @@ impl Terminal { if let Some(profile) = &connection.saved { self.library.opened_connection(&profile.id)?; } + self.completion.reset(); self.connection.replace(Some(connection)); self.connection_error.set(String::new()); self.results.replace(Results::default()); diff --git a/src/terminal/menu.rs b/src/terminal/menu.rs index bba8641..fc93b8f 100644 --- a/src/terminal/menu.rs +++ b/src/terminal/menu.rs @@ -95,10 +95,14 @@ pub(super) struct Menu { impl Terminal { pub(super) fn remember_focus(&self, event: &Event) { + if event.target.attribute("id").as_deref() != Some("query") { + self.completion.dismiss(); + } self.content_focus.replace(Some(event.target.clone())); } pub(super) fn toggle_menu(&self, event: &Event) { + self.completion.dismiss(); if self.menu.with(Option::is_some) { self.close_menu(); return; diff --git a/src/terminal/recall.rs b/src/terminal/recall.rs index ac7f796..0c8a9c8 100644 --- a/src/terminal/recall.rs +++ b/src/terminal/recall.rs @@ -87,6 +87,7 @@ impl Terminal { if self.connection.with(Option::is_none) { return Ok(()); } + self.completion.dismiss(); self.refresh_library(|_| {})?; self.menu.replace(None); self.screen.set(Screen::Library); @@ -95,6 +96,7 @@ impl Terminal { } pub(super) fn collection(&self, collection: Collection) -> Result<(), Error> { + self.completion.dismiss(); self.refresh_library(|browser| { browser.collection = collection; browser.selected = 0; diff --git a/src/terminal/view.rs b/src/terminal/view.rs index 18bfcbc..f4ecb34 100644 --- a/src/terminal/view.rs +++ b/src/terminal/view.rs @@ -25,6 +25,7 @@ terminal_views! { RemoveConnection => "ui/connections/remove.html", ClientChooser => "ui/connections/clients.html", QueryWorkspace => "ui/workspace.html", + QuerySuggestions => "ui/query/suggestions.html", QueryLibrary => "ui/library.html", ValueInspector => "ui/inspector.html", CommandMenu => "ui/menu.html", diff --git a/tests/flight_sql.rs b/tests/flight_sql.rs index 9e1c325..2ac51b5 100644 --- a/tests/flight_sql.rs +++ b/tests/flight_sql.rs @@ -1,18 +1,18 @@ -use arrow_array::{ArrayRef, RecordBatch, StringArray}; +use arrow_array::{ArrayRef, BinaryArray, RecordBatch, StringArray}; use arrow_flight::{ - FlightDescriptor, FlightEndpoint, FlightInfo, Ticket, + FlightDescriptor, FlightEndpoint, FlightInfo, IpcMessage, SchemaAsIpc, Ticket, encode::FlightDataEncoderBuilder, error::FlightError, flight_service_server::{FlightService, FlightServiceServer}, sql::{ - CommandStatementQuery, ProstMessageExt, SqlInfo, TicketStatementQuery, + CommandGetTables, CommandStatementQuery, ProstMessageExt, SqlInfo, TicketStatementQuery, server::FlightSqlService, }, }; use arrow_schema::{DataType, Field, Schema, SchemaRef}; -use futures_util::{StreamExt, TryStreamExt, stream}; +use futures_util::{StreamExt, TryStreamExt, stream, stream::BoxStream}; use prost::Message; -use sql_bomb::{Error, FlightSql, QueryClient}; +use sql_bomb::{Error, FlightSql, QueryClient, Table}; use std::sync::Arc; use tokio::{ net::TcpListener, @@ -21,6 +21,9 @@ use tokio::{ use tokio_stream::wrappers::TcpListenerStream; use tonic::{Request, Response, Status, transport::Server}; +#[path = "support/completion.rs"] +mod completion; + const QUERY: &str = "SELECT city FROM flights"; const EMPTY_QUERY: &str = "SELECT city @@ -98,11 +101,58 @@ impl FlightSqlService for Flights { PUBLIC_TICKET => Box::pin(stream::empty()), _ => return Err(Status::not_found("Unknown ticket")), }; - let encoded = FlightDataEncoderBuilder::new() - .with_schema(self.schema.clone()) - .build(batches) - .map_err(Status::from); - Ok(Response::new(Box::pin(encoded))) + Ok(encode_batches(self.schema.clone(), batches)) + } + + async fn get_flight_info_tables( + &self, + query: CommandGetTables, + request: Request, + ) -> Result, Status> { + authorize(&request)?; + if query + != (CommandGetTables { + include_schema: true, + ..Default::default() + }) + { + return Err(Status::invalid_argument("Expected all tables with schemas")); + } + let endpoints = [Some("warehouse"), None] + .into_iter() + .map(|catalog| { + let ticket = CommandGetTables { + catalog: catalog.map(str::to_owned), + ..query.clone() + }; + FlightEndpoint::new() + .with_ticket(Ticket::new(ticket.as_any().encode_to_vec())) + .with_location(&self.address) + }) + .collect(); + let flight = FlightInfo::new() + .try_with_schema(&query.into_builder().schema()) + .map_err(FlightError::from)? + .with_endpoints(endpoints); + Ok(Response::new(flight)) + } + + async fn do_get_tables( + &self, + query: CommandGetTables, + request: Request, + ) -> Result::DoGetStream>, Status> { + authorize(&request)?; + let batch = table_batch(&self.schema); + let batches = match query.catalog.as_deref() { + Some("warehouse") => vec![Ok(batch.slice(0, 1)), Ok(batch.slice(1, 1))], + None => vec![Ok(batch.slice(2, 1))], + _ => return Err(Status::not_found("Unknown catalog ticket")), + }; + Ok(encode_batches( + batch.schema(), + Box::pin(stream::iter(batches)), + )) } async fn register_sql_info(&self, _identifier: i32, _result: &SqlInfo) { @@ -110,6 +160,59 @@ impl FlightSqlService for Flights { } } +fn encode_batches( + schema: SchemaRef, + batches: BoxStream<'static, Result>, +) -> Response<::DoGetStream> { + let encoded = FlightDataEncoderBuilder::new() + .with_schema(schema) + .build(batches) + .map_err(Status::from); + Response::new(Box::pin(encoded)) +} + +fn table_batch(schema: &Schema) -> RecordBatch { + let columns = [ + ( + "catalog_name", + vec![Some("warehouse"), Some("warehouse"), None], + true, + ), + ( + "db_schema_name", + vec![Some("travel"), Some("travel"), None], + true, + ), + ( + "table_name", + vec![Some("flights"), Some("arrivées"), Some("airports")], + false, + ), + ( + "table_type", + vec![Some("TABLE"), Some("VIEW"), Some("TABLE")], + false, + ), + ] + .into_iter() + .map(|(name, values, nullable)| { + ( + name, + Arc::new(StringArray::from(values)) as ArrayRef, + nullable, + ) + }); + let serialized = IpcMessage::try_from(SchemaAsIpc::new(schema, &Default::default())) + .expect("the fixture schema has an IPC representation"); + let schemas = BinaryArray::from(vec![serialized.0.as_ref(); 3]); + RecordBatch::try_from_iter_with_nullable(columns.chain(std::iter::once(( + "table_schema", + Arc::new(schemas) as ArrayRef, + false, + )))) + .expect("table metadata columns have three rows with the declared types") +} + fn authorize(request: &Request) -> Result<(), Status> { for (name, value) in [ ("authorization", "Bearer fixture:token"), @@ -191,6 +294,42 @@ async fn streams_before_completion_and_reads_every_endpoint() { }; assert_eq!(status.code(), tonic::Code::InvalidArgument); assert_eq!(status.message(), "Unknown table"); + assert_table_discovery(client, &address).await; + completion::exercise(&address).await; shutdown.send(()).unwrap(); server.await.unwrap(); } + +async fn assert_table_discovery(client: &dyn QueryClient, address: &str) { + let tables = client + .tables() + .expect("Flight SQL supports table discovery") + .await + .expect("table discovery returns the fixture tables"); + let expected = [ + (Some("warehouse"), Some("travel"), "flights", "TABLE"), + (Some("warehouse"), Some("travel"), "arrivées", "VIEW"), + (None, None, "airports", "TABLE"), + ] + .into_iter() + .map(|(catalog, database_schema, name, table_type)| Table { + catalog: catalog.map(str::to_owned), + database_schema: database_schema.map(str::to_owned), + name: name.into(), + table_type: table_type.into(), + schema: Schema::new(vec![Field::new("city", DataType::Utf8, true)]), + }) + .collect::>(); + assert_eq!(tables, expected); + let unauthorized = FlightSql::new(address).expect("the loopback server address is valid"); + let failure = unauthorized + .tables() + .expect("discovery is available before authentication") + .await + .expect_err("the server requires authentication headers"); + let Error::Flight(FlightError::Tonic(status)) = failure else { + panic!("Expected the server's Flight SQL authentication error"); + }; + assert_eq!(status.code(), tonic::Code::Unauthenticated); + assert_eq!(status.message(), "Missing request headers"); +} diff --git a/tests/support/completion.rs b/tests/support/completion.rs new file mode 100644 index 0000000..1c3feb1 --- /dev/null +++ b/tests/support/completion.rs @@ -0,0 +1,121 @@ +#[path = "terminal.rs"] +mod terminal; + +use std::fs; +use terminal::Terminal; + +const TABLE_DRAFT: &str = "SELECT * FROM warehouse.travel.fl WHERE true;"; +const TABLE_SUFFIX: &str = " WHERE true;"; +const COLUMN_DRAFT: &str = "SELECT n.c FROM warehouse.travel.flights n;"; +const COLUMN_SUFFIX: &str = " FROM warehouse.travel.flights n;"; + +pub(super) async fn exercise(address: &str) { + let address = address.replace("grpc+tcp://", "http://"); + tokio::task::spawn_blocking(move || { + let directory = + std::env::temp_dir().join(format!("sql-bomb-completion-{}", std::process::id())); + fs::create_dir(&directory).expect("the test home is isolated"); + let mut terminal = Terminal::start(&directory, &["--flightsql"]); + terminal.wait("connection form", |screen| { + screen.contains("Connection address") + }); + terminal.send("\x01"); + terminal.paste(&address); + terminal.send("\t"); + terminal.paste(crate::HEADERS); + terminal.send("\t\t\r"); + terminal.wait("authenticated schema", |screen| { + screen.contains("3 tables available") + }); + accepts_tables_and_columns(&mut terminal); + dismisses_without_executing(&mut terminal); + terminal.close(); + rejects_metadata_without_blocking_editor(&directory, &address); + fs::remove_dir_all(directory).expect("the test can remove its isolated home"); + }) + .await + .expect("the terminal interaction succeeds"); +} + +fn accepts_tables_and_columns(terminal: &mut Terminal) { + for (draft, suffix, suggestion) in [ + (TABLE_DRAFT, TABLE_SUFFIX, "flights"), + (COLUMN_DRAFT, COLUMN_SUFFIX, "city"), + ] { + terminal.send("\x01"); + terminal.paste(draft); + terminal.wait("SQL draft", |screen| screen.contains(draft)); + terminal.send(&"\x1b[D".repeat(suffix.chars().count())); + terminal.wait("schema suggestion", |screen| screen.contains(suggestion)); + if suggestion == "flights" { + terminal.send("\t"); + } else { + terminal.click(suggestion); + } + terminal.send("!"); + let expected = match suggestion { + "flights" => "SELECT * FROM warehouse.travel.flights! WHERE true;", + _ => "SELECT n.city! FROM warehouse.travel.flights n;", + }; + let screen = terminal.wait("completion and caret", |screen| screen.contains(expected)); + assert_eq!(query_line(&screen), expected); + } +} + +fn dismisses_without_executing(terminal: &mut Terminal) { + const DRAFT: &str = "SELECT * FROM warehouse.travel.fl"; + terminal.send("\x01"); + terminal.paste(DRAFT); + terminal.wait("SQL draft", |screen| screen.contains(DRAFT)); + terminal.wait("table suggestions", |screen| screen.contains("flights")); + terminal.send("\x1b"); + let screen = terminal.wait("dismissed suggestions", |screen| { + !screen.contains("flights") + }); + assert_eq!(query_line(&screen), DRAFT); + terminal.send("\r"); + terminal.paste("-- second line"); + let screen = terminal.wait("editor newline", |screen| screen.contains("-- second line")); + assert_eq!(query_line(&screen), DRAFT); + let second_line = screen + .lines() + .skip_while(|line| !line.contains(DRAFT)) + .nth(1) + .expect("the editor displays a second line"); + assert_eq!( + second_line.trim().trim_matches('│').trim(), + "-- second line" + ); + terminal.send("\x10"); + let screen = terminal.wait("empty query history", |screen| screen.contains("0 queries")); + let count = screen + .lines() + .find(|line| line.contains("0 queries")) + .expect("the history count is visible") + .split('│') + .map(str::trim) + .find(|text| text.ends_with("queries")) + .expect("the history pane displays a query count"); + assert_eq!(count, "0 queries"); +} + +fn rejects_metadata_without_blocking_editor(directory: &std::path::Path, address: &str) { + let mut terminal = Terminal::start(directory, &["--flightsql", address]); + terminal.wait("schema authentication error", |screen| { + screen.contains("Suggestions unavailable:") && screen.contains("Missing request headers") + }); + terminal.paste("SELECT 1;"); + let screen = terminal.wait("editable query", |screen| screen.contains("SELECT 1;")); + assert_eq!(query_line(&screen), "SELECT 1;"); + terminal.close(); +} + +fn query_line(screen: &str) -> &str { + screen + .lines() + .find(|line| line.contains("SELECT ")) + .expect("the SQL editor is visible") + .trim() + .trim_matches('│') + .trim() +} diff --git a/tests/support/terminal.rs b/tests/support/terminal.rs new file mode 100644 index 0000000..785606e --- /dev/null +++ b/tests/support/terminal.rs @@ -0,0 +1,144 @@ +use portable_pty::{Child, CommandBuilder, PtySize}; +use std::{ + io::{self, Read, Write}, + path::Path, + sync::mpsc::{self, Receiver, Sender}, + thread, + time::Duration, +}; + +const ROWS: u16 = 40; +const COLUMNS: u16 = 140; +const OUTPUT_CHUNK: usize = 8192; +const OUTPUT_TIMEOUT: Duration = Duration::from_secs(20); + +pub(super) struct Terminal { + child: Box, + writer: Box, + output: Receiver>>, + parser: vt100::Parser, +} + +impl Terminal { + pub(super) fn start(directory: &Path, arguments: &[&str]) -> Self { + let pair = portable_pty::native_pty_system() + .openpty(PtySize { + rows: ROWS, + cols: COLUMNS, + ..Default::default() + }) + .expect("the test can open a pseudo-terminal"); + let mut command = CommandBuilder::new(env!("CARGO_BIN_EXE_boom")); + command.args(arguments); + command.env("HOME", directory); + command.env("TERM", "xterm-256color"); + let child = pair + .slave + .spawn_command(command) + .expect("boom starts in the PTY"); + let mut reader = pair + .master + .try_clone_reader() + .expect("the PTY exposes output"); + let writer = pair.master.take_writer().expect("the PTY exposes input"); + let (sender, output) = mpsc::channel(); + thread::spawn(move || read_output(reader.as_mut(), &sender)); + Self { + child, + writer, + output, + parser: vt100::Parser::new(ROWS, COLUMNS, 0), + } + } + + pub(super) fn send(&mut self, input: &str) { + self.writer + .write_all(input.as_bytes()) + .expect("boom accepts terminal input"); + self.writer.flush().expect("terminal input flushes"); + } + + pub(super) fn paste(&mut self, text: &str) { + self.send(&format!("\x1b[200~{text}\x1b[201~")); + } + + pub(super) fn wait(&mut self, description: &str, ready: impl Fn(&str) -> bool) -> String { + loop { + let contents = self.parser.screen().contents(); + if ready(&contents) { + return contents; + } + let bytes = self + .output + .recv_timeout(OUTPUT_TIMEOUT) + .unwrap_or_else(|error| panic!("Waiting for {description}: {error}\n{contents}")); + let bytes = bytes.expect("the PTY reader returns output"); + assert_ne!( + bytes.len(), + 0, + "Exited while waiting for {description}\n{contents}" + ); + self.parser.process(&bytes); + } + } + + pub(super) fn click(&mut self, text: &str) { + let (row, line) = self + .parser + .screen() + .rows(0, COLUMNS) + .enumerate() + .find(|(_, line)| line.contains(text)) + .expect("the click target appears in the terminal"); + let prefix = line + .find(text) + .expect("the selected row contains the click target"); + let column = line[..prefix].chars().count() + 1; + let row = row + 1; + self.send(&format!("\x1b[<0;{column};{row}M\x1b[<0;{column};{row}m")); + } + + pub(super) fn close(&mut self) { + self.send("\x03"); + assert_eq!( + self.child + .wait() + .expect("boom exits after Ctrl+C") + .exit_code(), + 0 + ); + } +} + +impl Drop for Terminal { + fn drop(&mut self) { + if self + .child + .try_wait() + .expect("the child status is available") + .is_none() + { + self.child + .kill() + .expect("the test can stop its child process"); + self.child.wait().expect("the stopped child exits"); + } + } +} + +fn read_output(reader: &mut dyn Read, sender: &Sender>>) { + loop { + let mut buffer = [0; OUTPUT_CHUNK]; + let output = reader + .read(&mut buffer) + .map(|length| buffer[..length].to_vec()); + let ended = match &output { + Ok(bytes) => bytes.is_empty(), + Err(_) => true, + }; + // Closing the test session drops its receiver while the reader finishes. + if sender.send(output).is_err() || ended { + return; + } + } +} diff --git a/ui/query/suggestions.html b/ui/query/suggestions.html new file mode 100644 index 0000000..a5cc097 --- /dev/null +++ b/ui/query/suggestions.html @@ -0,0 +1,18 @@ + diff --git a/ui/terminal.css b/ui/terminal.css index dcff262..98b3a73 100644 --- a/ui/terminal.css +++ b/ui/terminal.css @@ -44,6 +44,44 @@ th { color: #DF5036; } .position { height: 1ch; color: #DF5036; padding: 0 1ch; white-space: pre; overflow: hidden; } .help, .muted { color: #AAA6A0; white-space: pre; overflow: hidden; } .help { height: 1ch; padding: 0 1ch; white-space: normal; } +.schema-status { + flex-direction: row; + min-height: 1ch; + gap: 2ch; + padding: 0 1ch; +} +#completion-status { + flex-grow: 1; + flex-shrink: 1; + min-width: 0; + min-height: 1ch; + max-height: 3ch; + overflow: auto; + white-space: normal; +} +.suggestions-panel { + border-style: rounded; + border-color: #DF5036; + flex-shrink: 1; + min-height: 3ch; +} +.suggestions-help { + height: 1ch; + padding: 0 1ch; + color: #AAA6A0; + white-space: pre; + overflow: hidden; +} +#query-suggestions { max-height: 6ch; min-height: 1ch; overflow: auto; flex-shrink: 1; } +.suggestion { flex-direction: row; height: 1ch; gap: 2ch; padding: 0 1ch; } +.suggestion-label { flex-grow: 1; min-width: 0; white-space: pre; overflow: hidden; } +.suggestion-detail { + color: #AAA6A0; + flex-shrink: 1; + min-width: 0; + white-space: pre; + overflow: hidden; +} .connection-screen { height: 100%; min-height: 0; diff --git a/ui/workspace.html b/ui/workspace.html index 6053da8..6b9d295 100644 --- a/ui/workspace.html +++ b/ui/workspace.html @@ -2,9 +2,20 @@
+ +
+

{{ + state.terminal.completion.status.get() + }}

+ +