From 03f8cca2c4556b3032dd4f89b123bd5da5108ec2 Mon Sep 17 00:00:00 2001 From: Leo Stewen Date: Wed, 10 Jun 2026 13:21:47 +0200 Subject: [PATCH 1/8] Welcome coln-query This is a so far unintegrated copy of my thesis' code. --- Cargo.lock | 5102 +++++++++++++++-- Cargo.toml | 19 +- packages/coln-query/Cargo.toml | 30 + packages/coln-query/src/api.rs | 230 + packages/coln-query/src/context.rs | 97 + packages/coln-query/src/dbsp.rs | 1110 ++++ packages/coln-query/src/error.rs | 80 + packages/coln-query/src/expr.rs | 529 ++ packages/coln-query/src/function.rs | 67 + packages/coln-query/src/interpreter.rs | 648 +++ packages/coln-query/src/lib.rs | 1217 ++++ packages/coln-query/src/operator.rs | 34 + packages/coln-query/src/operators/coalesce.rs | 33 + packages/coln-query/src/operators/mod.rs | 3 + .../coln-query/src/operators/projection.rs | 108 + packages/coln-query/src/operators/reindex.rs | 76 + packages/coln-query/src/optimizer/mod.rs | 18 + packages/coln-query/src/relation.rs | 619 ++ packages/coln-query/src/resolver.rs | 382 ++ packages/coln-query/src/scalar.rs | 156 + packages/coln-query/src/stmt.rs | 113 + packages/coln-query/src/test_helper.rs | 374 ++ packages/coln-query/src/type_resolver.rs | 286 + packages/coln-query/src/util.rs | 47 + packages/coln-query/src/variable.rs | 180 + packages/coln-store/Cargo.toml | 1 + 26 files changed, 11203 insertions(+), 356 deletions(-) create mode 100644 packages/coln-query/Cargo.toml create mode 100644 packages/coln-query/src/api.rs create mode 100644 packages/coln-query/src/context.rs create mode 100644 packages/coln-query/src/dbsp.rs create mode 100644 packages/coln-query/src/error.rs create mode 100644 packages/coln-query/src/expr.rs create mode 100644 packages/coln-query/src/function.rs create mode 100644 packages/coln-query/src/interpreter.rs create mode 100644 packages/coln-query/src/lib.rs create mode 100644 packages/coln-query/src/operator.rs create mode 100644 packages/coln-query/src/operators/coalesce.rs create mode 100644 packages/coln-query/src/operators/mod.rs create mode 100644 packages/coln-query/src/operators/projection.rs create mode 100644 packages/coln-query/src/operators/reindex.rs create mode 100644 packages/coln-query/src/optimizer/mod.rs create mode 100644 packages/coln-query/src/relation.rs create mode 100644 packages/coln-query/src/resolver.rs create mode 100644 packages/coln-query/src/scalar.rs create mode 100644 packages/coln-query/src/stmt.rs create mode 100644 packages/coln-query/src/test_helper.rs create mode 100644 packages/coln-query/src/type_resolver.rs create mode 100644 packages/coln-query/src/util.rs create mode 100644 packages/coln-query/src/variable.rs diff --git a/Cargo.lock b/Cargo.lock index c1ce75e4..167c38a4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,648 +3,5040 @@ version = 4 [[package]] -name = "aho-corasick" -version = "1.1.4" +name = "actix-codec" +version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" +checksum = "5f7b0a21988c1bf877cf4759ef5ddaac04c1c9fe808c9142ecb78ba97d97a28a" dependencies = [ + "bitflags", + "bytes", + "futures-core", + "futures-sink", "memchr", + "pin-project-lite", + "tokio", + "tokio-util", + "tracing", ] [[package]] -name = "arrayref" -version = "0.3.9" +name = "actix-http" +version = "3.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "76a2e8124351fda1ef8aaaa3bbd7ebbcb486bbcd4225aca0aa0d84bb2db8fecb" +checksum = "93acb4a42f64936f9b8cae4a433b237599dd6eb6ed06124eb67132ef8cc90662" +dependencies = [ + "actix-codec", + "actix-rt", + "actix-service", + "actix-utils", + "base64", + "bitflags", + "brotli", + "bytes", + "bytestring", + "derive_more 2.1.1", + "encoding_rs", + "flate2", + "foldhash", + "futures-core", + "h2 0.3.27", + "http 0.2.12", + "httparse", + "httpdate", + "itoa", + "language-tags", + "local-channel", + "mime", + "percent-encoding", + "pin-project-lite", + "rand 0.10.1", + "sha1 0.11.0", + "smallvec", + "tokio", + "tokio-util", + "tracing", + "zstd", +] [[package]] -name = "arrayvec" -version = "0.7.6" +name = "actix-macros" +version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" +checksum = "e01ed3140b2f8d422c68afa1ed2e85d996ea619c988ac834d255db32138655cb" +dependencies = [ + "quote", + "syn 2.0.117", +] [[package]] -name = "atomic-polyfill" -version = "1.0.3" +name = "actix-router" +version = "0.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8cf2bce30dfe09ef0bfaef228b9d414faaf7e563035494d7fe092dba54b300f4" +checksum = "14f8c75c51892f18d9c46150c5ac7beb81c95f78c8b83a634d49f4ca32551fe7" dependencies = [ - "critical-section", + "bytestring", + "cfg-if", + "http 0.2.12", + "regex", + "regex-lite", + "serde", + "tracing", ] [[package]] -name = "bitflags" -version = "2.12.1" +name = "actix-rt" +version = "2.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84d7ced0ae9557296835c32bf1b1e02b44c746701f898460fb000d7eaa84f00a" +checksum = "92589714878ca59a7626ea19734f0e07a6a875197eec751bb5d3f99e64998c63" +dependencies = [ + "futures-core", + "tokio", +] [[package]] -name = "blake3" -version = "1.8.5" +name = "actix-server" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0aa83c34e62843d924f905e0f5c866eb1dd6545fc4d719e803d9ba6030371fce" +checksum = "a65064ea4a457eaf07f2fba30b4c695bf43b721790e9530d26cb6f9019ff7502" dependencies = [ - "arrayref", - "arrayvec", - "cc", - "cfg-if", - "constant_time_eq", - "cpufeatures", + "actix-rt", + "actix-service", + "actix-utils", + "futures-core", + "futures-util", + "mio", + "socket2 0.5.10", + "tokio", + "tracing", ] [[package]] -name = "byteorder" -version = "1.5.0" +name = "actix-service" +version = "2.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" +checksum = "9e46f36bf0e5af44bdc4bdb36fbbd421aa98c79a9bce724e1edeb3894e10dc7f" +dependencies = [ + "futures-core", + "pin-project-lite", +] [[package]] -name = "cc" -version = "1.2.63" +name = "actix-utils" +version = "3.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "556e016178bb5662a08681bbe0f00f8e17631781a4dfc8c45e466e4b185ec27f" +checksum = "88a1dcdff1466e3c2488e1cb5c36a71822750ad43839937f85d2f4d9f8b705d8" dependencies = [ - "find-msvc-tools", - "shlex", + "local-waker", + "pin-project-lite", ] [[package]] -name = "cfg-if" -version = "1.0.4" +name = "actix-web" +version = "4.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" +checksum = "ff87453bc3b56e9b2b23c1cc0b1be8797184accf51d2abe0f8a33ec275d316bf" +dependencies = [ + "actix-codec", + "actix-http", + "actix-macros", + "actix-router", + "actix-rt", + "actix-server", + "actix-service", + "actix-utils", + "actix-web-codegen", + "bytes", + "bytestring", + "cfg-if", + "cookie", + "derive_more 2.1.1", + "encoding_rs", + "foldhash", + "futures-core", + "futures-util", + "impl-more", + "itoa", + "language-tags", + "log", + "mime", + "once_cell", + "pin-project-lite", + "regex", + "regex-lite", + "serde", + "serde_json", + "serde_urlencoded", + "smallvec", + "socket2 0.6.4", + "time", + "tracing", + "url", +] [[package]] -name = "cfg_aliases" -version = "0.2.1" +name = "actix-web-codegen" +version = "4.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" +checksum = "f591380e2e68490b5dfaf1dd1aa0ebe78d84ba7067078512b4ea6e4492d622b8" +dependencies = [ + "actix-router", + "proc-macro2", + "quote", + "syn 2.0.117", +] [[package]] -name = "clipboard-win" -version = "5.4.1" +name = "adler2" +version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bde03770d3df201d4fb868f2c9c59e66a3e4e2bd06692a0fe701e7103c7e84d4" +checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" + +[[package]] +name = "aes" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b169f7a6d4742236a0a00c541b845991d0ac43e546831af1249753ab4c3aa3a0" dependencies = [ - "error-code", + "cfg-if", + "cipher", + "cpufeatures 0.2.17", ] [[package]] -name = "cobs" -version = "0.3.0" +name = "ahash" +version = "0.7.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fa961b519f0b462e3a3b4a34b64d119eeaca1d59af726fe450bbba07a9fc0a1" +checksum = "891477e0c6a8957309ee5c45a6368af3ae14bb510732d2684ffa19af310920f9" dependencies = [ - "thiserror", + "getrandom 0.2.17", + "once_cell", + "version_check", ] [[package]] -name = "coln-lang-rs" -version = "0.1.0" +name = "ahash" +version = "0.8.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a15f179cd60c4584b8a8c596927aadc462e27f2ca70c04e0071964a73ba7a75" dependencies = [ - "serde", - "serde_json", + "cfg-if", + "getrandom 0.3.4", + "once_cell", + "version_check", + "zerocopy", ] [[package]] -name = "coln-store" -version = "0.1.0" +name = "aho-corasick" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" dependencies = [ - "blake3", - "coln-lang-rs", - "hex", - "hexane", - "leb128", - "postcard", - "rustyline", - "serde", - "serde_json", - "shlex", - "tracing", - "tracing-subscriber", + "memchr", ] [[package]] -name = "constant_time_eq" -version = "0.4.2" +name = "aliasable" +version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d52eff69cd5e647efe296129160853a42795992097e8af39800e1060caeea9b" +checksum = "250f629c0161ad8107cf89319e990051fae62832fd343083bea452d93e2205fd" [[package]] -name = "cpufeatures" -version = "0.3.0" +name = "alloc-no-stdlib" +version = "2.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b2a41393f66f16b0823bb79094d54ac5fbd34ab292ddafb9a0456ac9f87d201" +checksum = "cc7bb162ec39d46ab1ca8c77bf72e890535becd1751bb45f64c597edb4c8c6b3" + +[[package]] +name = "alloc-stdlib" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94fb8275041c72129eb51b7d0322c29b8387a0386127718b096429201a5d6ece" +dependencies = [ + "alloc-no-stdlib", +] + +[[package]] +name = "alloca" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5a7d05ea6aea7e9e64d25b9156ba2fee3fdd659e34e41063cd2fc7cd020d7f4" +dependencies = [ + "cc", +] + +[[package]] +name = "allocator-api2" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" dependencies = [ "libc", ] [[package]] -name = "critical-section" -version = "1.2.0" +name = "anes" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b" +checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" [[package]] -name = "embedded-io" -version = "0.4.0" +name = "anstream" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef1a6892d9eef45c8fa6b9e0086428a2cca8491aca8f787c534a3d6d0bcb3ced" +checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] [[package]] -name = "embedded-io" -version = "0.6.1" +name = "anstyle" +version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "edd0f118536f44f5ccd48bcb8b111bdc3de888b58c74639dfb034a357d0f206d" +checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000" [[package]] -name = "endian-type" -version = "0.2.0" +name = "anstyle-parse" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "869b0adbda23651a9c5c0c3d270aac9fcb52e8622a8f2b17e57802d7791962f2" +checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e" +dependencies = [ + "utf8parse", +] [[package]] -name = "error-code" -version = "3.3.2" +name = "anstyle-query" +version = "1.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dea2df4cf52843e0452895c455a1a2cfbb842a1e7329671acf418fdc53ed4c59" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys 0.61.2", +] [[package]] -name = "find-msvc-tools" -version = "0.1.9" +name = "anstyle-wincon" +version = "3.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys 0.61.2", +] [[package]] -name = "hash32" -version = "0.2.1" +name = "anyhow" +version = "1.0.102" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0c35f58762feb77d74ebe43bdbc3210f09be9fe6742234d573bacc26ed92b67" +checksum = "7f202df86484c868dbad7eaa557ef785d5c66295e41b460ef922eca0723b842c" + +[[package]] +name = "arbitrary" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d036a3c4ab069c7b410a2ce876bd74808d2d0888a82667669f8e783a898bf1" dependencies = [ - "byteorder", + "derive_arbitrary", ] [[package]] -name = "heapless" -version = "0.7.17" +name = "arc-swap" +version = "1.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdc6457c0eb62c71aac4bc17216026d8410337c4126773b9c5daba343f17964f" +checksum = "6a3a1fd6f75306b68087b831f025c712524bcb19aad54e557b1129cfa0a2b207" +dependencies = [ + "rustversion", +] + +[[package]] +name = "arcstr" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03918c3dbd7701a85c6b9887732e2921175f26c350b4563841d0958c21d57e6d" + +[[package]] +name = "array-init" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d62b7694a562cdf5a74227903507c56ab2cc8bdd1f781ed5cb4cf9c9f810bfc" + +[[package]] +name = "arrayref" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76a2e8124351fda1ef8aaaa3bbd7ebbcb486bbcd4225aca0aa0d84bb2db8fecb" + +[[package]] +name = "arrayvec" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" + +[[package]] +name = "arrow-schema" +version = "58.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f633dbfdf39c039ada1bf9e34c694816eb71fbb7dc78f613993b7245e078a1ed" dependencies = [ - "atomic-polyfill", - "hash32", - "rustc_version", "serde", - "spin", - "stable_deref_trait", + "serde_core", ] [[package]] -name = "hex" -version = "0.4.3" +name = "async-stream" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +checksum = "0b5a71a6f37880a80d1d7f19efd781e4b5de42c88f0722cc13bcb6cc2cfe8476" +dependencies = [ + "async-stream-impl", + "futures-core", + "pin-project-lite", +] [[package]] -name = "hexane" -version = "1.0.0-alpha.1" -source = "git+https://github.com/automerge/automerge?branch=hexane_v1#30b83c03dc9669654153c01fe89f3fb931198324" +name = "async-stream-impl" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7c24de15d275a1ecfd47a380fb4d5ec9bfe0933f309ed5e705b775596a3574d" dependencies = [ - "leb128", - "thiserror", + "proc-macro2", + "quote", + "syn 2.0.117", ] [[package]] -name = "home" -version = "0.5.12" +name = "async-trait" +version = "0.1.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc627f471c528ff0c4a49e1d5e60450c8f6461dd6d10ba9dcd3a61d3dff7728d" +checksum = "9035ad2d096bed7955a320ee7e2230574d28fd3c3a0f186cbea1ff3c7eed5dbb" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "atomic-polyfill" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cf2bce30dfe09ef0bfaef228b9d414faaf7e563035494d7fe092dba54b300f4" dependencies = [ - "windows-sys", + "critical-section", ] [[package]] -name = "itoa" -version = "1.0.18" +name = "atomic-waker" +version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" [[package]] -name = "lazy_static" -version = "1.5.0" +name = "autocfg" +version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" +checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" [[package]] -name = "leb128" -version = "0.2.6" +name = "base64" +version = "0.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6cc46bac87ef8093eed6f272babb833b6443374399985ac8ed28471ee0918545" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" [[package]] -name = "libc" -version = "0.2.186" +name = "binrw" +version = "0.13.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66" +checksum = "768230c3e8ce988f15f99af8d0d2c3afd0a656b88b35480fb1f924699e810e57" +dependencies = [ + "array-init", + "binrw_derive", + "bytemuck", +] [[package]] -name = "lock_api" -version = "0.4.14" +name = "binrw_derive" +version = "0.13.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" +checksum = "08118044b6695ea0de85ecdaac53adeb0d5d024770a904f45b9453ad3c12898c" dependencies = [ - "scopeguard", + "either", + "owo-colors", + "proc-macro2", + "quote", + "syn 1.0.109", ] [[package]] -name = "log" -version = "0.4.32" +name = "bitflags" +version = "2.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84d7ced0ae9557296835c32bf1b1e02b44c746701f898460fb000d7eaa84f00a" + +[[package]] +name = "bitvec" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c" +dependencies = [ + "funty", + "radium", + "tap", + "wyz", +] + +[[package]] +name = "blake3" +version = "1.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0aa83c34e62843d924f905e0f5c866eb1dd6545fc4d719e803d9ba6030371fce" +dependencies = [ + "arrayref", + "arrayvec", + "cc", + "cfg-if", + "constant_time_eq 0.4.2", + "cpufeatures 0.3.0", +] + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "block-buffer" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdd35008169921d80bc60d3d0ab416eecb028c4cd653352907921d95084790be" +dependencies = [ + "hybrid-array", +] + +[[package]] +name = "brotli" +version = "8.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8119e4516436f5708bbc474a9d395bf12f1b5395e93a92a56e647ac3388c8610" +dependencies = [ + "alloc-no-stdlib", + "alloc-stdlib", + "brotli-decompressor", +] + +[[package]] +name = "brotli-decompressor" +version = "5.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5962523e1b92ce1b5e793d9169b9943eece10d39f62550bc04bb605d75b94924" +dependencies = [ + "alloc-no-stdlib", + "alloc-stdlib", +] + +[[package]] +name = "bumpalo" +version = "3.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" + +[[package]] +name = "bytecheck" +version = "0.6.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23cdc57ce23ac53c931e88a43d06d070a6fd142f2617be5855eb75efc9beb1c2" +dependencies = [ + "bytecheck_derive", + "ptr_meta 0.1.4", + "simdutf8", + "uuid", +] + +[[package]] +name = "bytecheck_derive" +version = "0.6.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3db406d29fbcd95542e92559bed4d8ad92636d1ca8b3b72ede10b4bcc010e659" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "bytemuck" +version = "1.25.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8efb64bd706a16a1bdde310ae86b351e4d21550d98d056f22f8a7f7a2183fec" +dependencies = [ + "bytemuck_derive", +] + +[[package]] +name = "bytemuck_derive" +version = "1.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9abbd1bc6865053c427f7198e6af43bfdedc55ab791faed4fbd361d789575ff" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "bytes" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e748733b7cbc798e1434b6ac524f0c1ff2ab456fe201501e6497c8417a4fc33" + +[[package]] +name = "bytestring" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86566c496f2f47d9b8147a4c8b02ffdb69c919fe0c2b2e7195d22cbba0e635c9" +dependencies = [ + "bytes", +] + +[[package]] +name = "bzip2" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3a53fac24f34a81bc9954b5d6cfce0c21e18ec6959f44f56e8e90e4bb7c346c" +dependencies = [ + "libbz2-rs-sys", +] + +[[package]] +name = "cast" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" + +[[package]] +name = "cc" +version = "1.2.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "556e016178bb5662a08681bbe0f00f8e17631781a4dfc8c45e466e4b185ec27f" +dependencies = [ + "find-msvc-tools", + "jobserver", + "libc", + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "cfg_aliases" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" + +[[package]] +name = "chacha20" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f8d983286843e49675a4b7a2d174efe136dc93a18d69130dd18198a6c167601" +dependencies = [ + "cfg-if", + "cpufeatures 0.3.0", + "rand_core 0.10.1", +] + +[[package]] +name = "chrono" +version = "0.4.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aa79e62e7697b8e29b513a68abacf485adcd1fe8284a4316c5ae868e6633327" +dependencies = [ + "iana-time-zone", + "num-traits", + "rkyv", + "serde", + "windows-link", +] + +[[package]] +name = "ciborium" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e" +dependencies = [ + "ciborium-io", + "ciborium-ll", + "serde", +] + +[[package]] +name = "ciborium-io" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757" + +[[package]] +name = "ciborium-ll" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9" +dependencies = [ + "ciborium-io", + "half", +] + +[[package]] +name = "cipher" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" +dependencies = [ + "crypto-common 0.1.7", + "inout", +] + +[[package]] +name = "clap" +version = "4.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ddb117e43bbf7dacf0a4190fef4d345b9bad68dfc649cb349e7d17d28428e51" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "714a53001bf66416adb0e2ef5ac857140e7dc3a0c48fb28b2f10762fc4b5069f" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", + "terminal_size", +] + +[[package]] +name = "clap_derive" +version = "4.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2ce8604710f6733aa641a2b3731eaa1e8b3d9973d5e3565da11800813f997a9" +dependencies = [ + "heck 0.5.0", + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "clap_lex" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9" + +[[package]] +name = "cli-table" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b53f9241f288a7b12c56565f04aaeaeeab6b8923d42d99255d4ca428b4d97f89" +dependencies = [ + "cli-table-derive", + "csv", + "termcolor", + "unicode-width 0.1.14", +] + +[[package]] +name = "cli-table-derive" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e83a93253aaae7c74eb7428ce4faa6e219ba94886908048888701819f82fb94" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "clipboard-win" +version = "5.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bde03770d3df201d4fb868f2c9c59e66a3e4e2bd06692a0fe701e7103c7e84d4" +dependencies = [ + "error-code", +] + +[[package]] +name = "cobs" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa961b519f0b462e3a3b4a34b64d119eeaca1d59af726fe450bbba07a9fc0a1" +dependencies = [ + "thiserror 2.0.18", +] + +[[package]] +name = "coln-lang-rs" +version = "0.1.0" +dependencies = [ + "serde", + "serde_json", +] + +[[package]] +name = "coln-query" +version = "0.1.0" +dependencies = [ + "anyhow", + "cli-table", + "criterion", + "dbsp", + "feldera-size-of", + "rkyv", + "thiserror 2.0.18", +] + +[[package]] +name = "coln-store" +version = "0.1.0" +dependencies = [ + "blake3", + "coln-lang-rs", + "coln-query", + "hex", + "hexane", + "leb128", + "postcard", + "rustyline", + "serde", + "serde_json", + "shlex", + "tracing", + "tracing-subscriber", +] + +[[package]] +name = "colorchoice" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570" + +[[package]] +name = "const-oid" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6ef517f0926dd24a1582492c791b6a4818a4d94e789a334894aa15b0d12f55c" + +[[package]] +name = "constant_time_eq" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c74b8349d32d297c9134b8c88677813a227df8f779daa29bfc29c183fe3dca6" + +[[package]] +name = "constant_time_eq" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d52eff69cd5e647efe296129160853a42795992097e8af39800e1060caeea9b" + +[[package]] +name = "convert_case" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "633458d4ef8c78b72454de2d54fd6ab2e60f9e02be22f3c6104cdc8a4e0fceb9" +dependencies = [ + "unicode-segmentation", +] + +[[package]] +name = "cookie" +version = "0.16.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e859cd57d0710d9e06c381b550c06e76992472a8c6d527aecd2fc673dcc231fb" +dependencies = [ + "percent-encoding", + "time", + "version_check", +] + +[[package]] +name = "core-foundation" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2a6cd9ae233e7f62ba4e9353e81a88df7fc8a5987b8d445b4d90c879bd156f6" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + +[[package]] +name = "core_affinity" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a034b3a7b624016c6e13f5df875747cc25f884156aad2abd12b6c46797971342" +dependencies = [ + "libc", + "num_cpus", + "winapi", +] + +[[package]] +name = "cpufeatures" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +dependencies = [ + "libc", +] + +[[package]] +name = "cpufeatures" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b2a41393f66f16b0823bb79094d54ac5fbd34ab292ddafb9a0456ac9f87d201" +dependencies = [ + "libc", +] + +[[package]] +name = "crc" +version = "3.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5eb8a2a1cd12ab0d987a5d5e825195d372001a4094a0376319d5a0ad71c1ba0d" +dependencies = [ + "crc-catalog", +] + +[[package]] +name = "crc-catalog" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "217698eaf96b4a3f0bc4f3662aaa55bdf913cd54d7204591faa790070c6d0853" + +[[package]] +name = "crc32c" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a47af21622d091a8f0fb295b88bc886ac74efcc613efc19f5d0b21de5c89e47" +dependencies = [ + "rustc_version", +] + +[[package]] +name = "crc32fast" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9481c1c90cbf2ac953f07c8d4a58aa3945c425b7185c9154d67a65e4230da511" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "criterion" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "950046b2aa2492f9a536f5f4f9a3de7b9e2476e575e05bd6c333371add4d98f3" +dependencies = [ + "alloca", + "anes", + "cast", + "ciborium", + "clap", + "criterion-plot", + "itertools 0.13.0", + "num-traits", + "oorandom", + "page_size", + "plotters", + "rayon", + "regex", + "serde", + "serde_json", + "tinytemplate", + "walkdir", +] + +[[package]] +name = "criterion-plot" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8d80a2f4f5b554395e47b5d8305bc3d27813bacb73493eb1001e8f76dae29ea" +dependencies = [ + "cast", + "itertools 0.13.0", +] + +[[package]] +name = "critical-section" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b" + +[[package]] +name = "crossbeam" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1137cd7e7fc0fb5d3c5a8678be38ec56e819125d8d7907411fe24ccb943faca8" +dependencies = [ + "crossbeam-channel", + "crossbeam-deque", + "crossbeam-epoch", + "crossbeam-queue", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-channel" +version = "0.5.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82b8f8f868b36967f9606790d1903570de9ceaf870a7bf9fbbd3016d636a2cb2" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-queue" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f58bbc28f91df819d0aa2a2c00cd19754769c2fad90579b3592b1c9ba7a3115" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" + +[[package]] +name = "crunchy" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5" + +[[package]] +name = "crypto-common" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "crypto-common" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce6e4c961d6cd6c9a86db418387425e8bdeaf05b3c8bc1411e6dca4c252f1453" +dependencies = [ + "hybrid-array", +] + +[[package]] +name = "csv" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52cd9d68cf7efc6ddfaaee42e7288d3a99d613d4b50f76ce9827ae0c6e14f938" +dependencies = [ + "csv-core", + "itoa", + "ryu", + "serde_core", +] + +[[package]] +name = "csv-core" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "704a3c26996a80471189265814dbc2c257598b96b8a7feae2d31ace646bb9782" +dependencies = [ + "memchr", +] + +[[package]] +name = "cty" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35" + +[[package]] +name = "dashmap" +version = "5.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "978747c1d849a7d2ee5e8adc0159961c48fb7e5db2f06af6723b80123bb53856" +dependencies = [ + "cfg-if", + "hashbrown 0.14.5", + "lock_api", + "once_cell", + "parking_lot_core", +] + +[[package]] +name = "dbsp" +version = "0.305.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c432c8cd94b8a21900fb79b136a723325ddf8aa5c3a0523d69e84f2daba62278" +dependencies = [ + "anyhow", + "arc-swap", + "async-stream", + "binrw", + "clap", + "core_affinity", + "crc32c", + "crossbeam", + "crossbeam-utils", + "csv", + "derive_more 1.0.0", + "dyn-clone", + "enum-map", + "fastbloom", + "fdlimit", + "feldera-buffer-cache", + "feldera-ir", + "feldera-macros", + "feldera-samply", + "feldera-size-of", + "feldera-storage", + "feldera-types", + "flate2", + "futures", + "futures-util", + "hashbrown 0.14.5", + "impl-trait-for-tuples", + "indexmap", + "inventory", + "itertools 0.14.0", + "libc", + "memory-stats", + "metrics", + "mimalloc-rust-sys", + "nix 0.29.0", + "num", + "num-derive", + "num-format", + "num-traits", + "once_cell", + "ordered-float 4.6.0", + "ouroboros", + "paste", + "petgraph", + "pin-project-lite", + "ptr_meta 0.2.0", + "rand 0.8.6", + "rand_chacha 0.3.1", + "rkyv", + "rmp-serde", + "roaring", + "seq-macro", + "serde", + "serde_json", + "serde_json_path_to_error", + "smallvec", + "snap", + "static_assertions", + "tempfile", + "textwrap", + "thiserror 2.0.18", + "thread-id", + "time", + "tokio", + "tokio-util", + "tracing", + "typedmap", + "uuid", + "xxhash-rust", + "zip", +] + +[[package]] +name = "deflate64" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac6b926516df9c60bfa16e107b21086399f8285a44ca9711344b9e553c5146e2" + +[[package]] +name = "deranged" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7cd812cc2bc1d69d4764bd80df88b4317eaef9e773c75226407d9bc0876b211c" +dependencies = [ + "powerfmt", + "serde_core", +] + +[[package]] +name = "derive_arbitrary" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e567bd82dcff979e4b03460c307b3cdc9e96fde3d73bed1496d2bc75d9dd62a" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "derive_more" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a9b99b9cbbe49445b21764dc0625032a89b145a2642e67603e1c936f5458d05" +dependencies = [ + "derive_more-impl 1.0.0", +] + +[[package]] +name = "derive_more" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d751e9e49156b02b44f9c1815bcb94b984cdcc4396ecc32521c739452808b134" +dependencies = [ + "derive_more-impl 2.1.1", +] + +[[package]] +name = "derive_more-impl" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb7330aeadfbe296029522e6c40f315320aba36fc43a5b3632f3795348f3bd22" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", + "unicode-xid", +] + +[[package]] +name = "derive_more-impl" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "799a97264921d8623a957f6c3b9011f3b5492f557bbb7a5a19b7fa6d06ba8dcb" +dependencies = [ + "convert_case", + "proc-macro2", + "quote", + "rustc_version", + "syn 2.0.117", + "unicode-xid", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer 0.10.4", + "crypto-common 0.1.7", + "subtle", +] + +[[package]] +name = "digest" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1dd6dbb5841937940781866fa1281a1ff7bd3bf827091440879f9994983d5c2" +dependencies = [ + "block-buffer 0.12.0", + "const-oid", + "crypto-common 0.2.2", +] + +[[package]] +name = "displaydoc" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ac70aa55017e108007fbaf5aa0f54b021c98f92ff8af59d42eda9da96e3dd4f" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "dyn-clone" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0881ea181b1df73ff77ffaaf9c7544ecc11e82fba9b5f27b262a3c73a332555" + +[[package]] +name = "either" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91622ff5e7162018101f2fea40d6ebf4a78bbe5a49736a2020649edf9693679e" + +[[package]] +name = "embedded-io" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef1a6892d9eef45c8fa6b9e0086428a2cca8491aca8f787c534a3d6d0bcb3ced" + +[[package]] +name = "embedded-io" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edd0f118536f44f5ccd48bcb8b111bdc3de888b58c74639dfb034a357d0f206d" + +[[package]] +name = "encoding_rs" +version = "0.8.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75030f3c4f45dafd7586dd6780965a8c7e8e285a5ecb86713e63a79c5b2766f3" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "endian-type" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "869b0adbda23651a9c5c0c3d270aac9fcb52e8622a8f2b17e57802d7791962f2" + +[[package]] +name = "enum-map" +version = "2.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6866f3bfdf8207509a033af1a75a7b08abda06bbaaeae6669323fd5a097df2e9" +dependencies = [ + "enum-map-derive", +] + +[[package]] +name = "enum-map-derive" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f282cfdfe92516eb26c2af8589c274c7c17681f5ecc03c18255fe741c6aa64eb" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + +[[package]] +name = "erased-serde" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c138974f9d5e7fe373eb04df7cae98833802ae4b11c24ac7039a21d5af4b26c" +dependencies = [ + "serde", +] + +[[package]] +name = "errno" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "error-code" +version = "3.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dea2df4cf52843e0452895c455a1a2cfbb842a1e7329671acf418fdc53ed4c59" + +[[package]] +name = "fastbloom" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7f34442dbe69c60fe8eaf58a8cafff81a1f278816d8ab4db255b3bef4ac3c4" +dependencies = [ + "getrandom 0.3.4", + "libm", + "rand 0.9.4", + "siphasher", +] + +[[package]] +name = "fastrand" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f1f227452a390804cdb637b74a86990f2a7d7ba4b7d5693aac9b4dd6defd8d6" + +[[package]] +name = "fdlimit" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e182f7dbc2ef73d9ef67351c5fbbea084729c48362d3ce9dd44c28e32e277fe5" +dependencies = [ + "libc", + "thiserror 1.0.69", +] + +[[package]] +name = "feldera-buffer-cache" +version = "0.305.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11e579b03d6f54251f082b9ebf0395812f074311848e83bda545539fca7c29e4" +dependencies = [ + "crossbeam-utils", + "enum-map", + "feldera-types", + "quick_cache", + "serde", + "tracing", +] + +[[package]] +name = "feldera-ir" +version = "0.305.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d499d1e0469f6add2f045370f4f5e7923c4a7437137fa6650f00fb3e19ab591" +dependencies = [ + "serde", + "serde_json", + "utoipa", + "zip", +] + +[[package]] +name = "feldera-macros" +version = "0.305.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d440e5735a527fbffbbd992d827b8cc9a1f46f0f99b6280929ed1d1820b2685a" +dependencies = [ + "prettyplease", + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "feldera-samply" +version = "0.305.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4c8ddbf9ea64bc9e71e40a17d6b55f4d32f3b89afb7931dab15dff521030a6e" +dependencies = [ + "crossbeam", + "feldera-size-of", + "flate2", + "itertools 0.14.0", + "libc", + "mach2", + "memory-stats", + "nix 0.29.0", + "serde", + "serde_json", + "serde_json_path_to_error", + "thiserror 2.0.18", + "thread-id", + "tokio", + "tracing", +] + +[[package]] +name = "feldera-size-of" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f75b89f6b944171e5b7df05e40a399ce6355f7b03a51fdd446d6235a890f5f57" +dependencies = [ + "arcstr", + "chrono", + "hashbrown 0.13.2", + "ordered-float 3.9.2", + "size-of-derive", + "time", + "xxhash-rust", +] + +[[package]] +name = "feldera-storage" +version = "0.305.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61ff6c7974844d92614d26d6f77b8668c1478e8fe41245544d50398279bd92bb" +dependencies = [ + "anyhow", + "crossbeam", + "feldera-size-of", + "feldera-types", + "inventory", + "itertools 0.14.0", + "libc", + "nix 0.27.1", + "object_store", + "once_cell", + "rkyv", + "serde", + "serde_json", + "thiserror 2.0.18", + "tokio", + "tracing", + "uuid", +] + +[[package]] +name = "feldera-types" +version = "0.305.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbe442bb15bc050c0bcdb391ebef272adb3baae3393ca2df38fb1f190adb259c" +dependencies = [ + "actix-web", + "anyhow", + "arrow-schema", + "bytemuck", + "chrono", + "clap", + "enum-map", + "erased-serde", + "feldera-ir", + "libc", + "log", + "regex", + "serde", + "serde_json", + "thiserror 2.0.18", + "time", + "utoipa", + "uuid", +] + +[[package]] +name = "find-msvc-tools" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582" + +[[package]] +name = "fixedbitset" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" + +[[package]] +name = "flate2" +version = "1.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "843fba2746e448b37e26a819579957415c8cef339bf08564fe8b7ddbd959573c" +dependencies = [ + "crc32fast", + "miniz_oxide", + "zlib-rs", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "foldhash" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" + +[[package]] +name = "form_urlencoded" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "funty" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" + +[[package]] +name = "futures" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b147ee9d1f6d097cef9ce628cd2ee62288d963e16fb287bd9286455b241382d" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07bbe89c50d7a535e539b8c17bc0b49bdb77747034daa8087407d655f3f7cc1d" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e3450815272ef58cec6d564423f6e755e25379b217b0bc688e295ba24df6b1d" + +[[package]] +name = "futures-executor" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf29c38818342a3b26b5b923639e7b1f4a61fc5e76102d4b1981c6dc7a7579d" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cecba35d7ad927e23624b22ad55235f2239cfa44fd10428eecbeba6d6a717718" + +[[package]] +name = "futures-macro" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e835b70203e41293343137df5c0664546da5745f82ec9b84d40be8336958447b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "futures-sink" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c39754e157331b013978ec91992bde1ac089843443c49cbc7f46150b0fad0893" + +[[package]] +name = "futures-task" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "037711b3d59c33004d3856fbdc83b99d4ff37a24768fa1be9ce3538a1cde4393" + +[[package]] +name = "futures-util" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "389ca41296e6190b48053de0321d02a77f32f8a5d2461dd38762c0593805c6d6" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "slab", +] + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0" +dependencies = [ + "cfg-if", + "js-sys", + "libc", + "wasi", + "wasm-bindgen", +] + +[[package]] +name = "getrandom" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" +dependencies = [ + "cfg-if", + "js-sys", + "libc", + "r-efi 5.3.0", + "wasip2", + "wasm-bindgen", +] + +[[package]] +name = "getrandom" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0de51e6874e94e7bf76d726fc5d13ba782deca734ff60d5bb2fb2607c7406555" +dependencies = [ + "cfg-if", + "libc", + "r-efi 6.0.0", + "rand_core 0.10.1", + "wasip2", + "wasip3", +] + +[[package]] +name = "h2" +version = "0.3.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0beca50380b1fc32983fc1cb4587bfa4bb9e78fc259aad4a0032d2080309222d" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http 0.2.12", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "h2" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "171fefbc92fe4a4de27e0698d6a5b392d6a0e333506bc49133760b3bcf948733" +dependencies = [ + "atomic-waker", + "bytes", + "fnv", + "futures-core", + "futures-sink", + "http 1.4.2", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "half" +version = "2.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ea2d84b969582b4b1864a92dc5d27cd2b77b622a8d79306834f1be5ba20d84b" +dependencies = [ + "cfg-if", + "crunchy", + "zerocopy", +] + +[[package]] +name = "hash32" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0c35f58762feb77d74ebe43bdbc3210f09be9fe6742234d573bacc26ed92b67" +dependencies = [ + "byteorder", +] + +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +dependencies = [ + "ahash 0.7.8", +] + +[[package]] +name = "hashbrown" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e" +dependencies = [ + "ahash 0.8.12", +] + +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" +dependencies = [ + "ahash 0.8.12", + "allocator-api2", +] + +[[package]] +name = "hashbrown" +version = "0.15.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" +dependencies = [ + "foldhash", +] + +[[package]] +name = "hashbrown" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" + +[[package]] +name = "hashbrown" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" + +[[package]] +name = "heapless" +version = "0.7.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdc6457c0eb62c71aac4bc17216026d8410337c4126773b9c5daba343f17964f" +dependencies = [ + "atomic-polyfill", + "hash32", + "rustc_version", + "serde", + "spin", + "stable_deref_trait", +] + +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "hermit-abi" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc0fef456e4baa96da950455cd02c081ca953b141298e41db3fc7e36b1da849c" + +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "hexane" +version = "1.0.0-alpha.1" +source = "git+https://github.com/automerge/automerge?branch=hexane_v1#30b83c03dc9669654153c01fe89f3fb931198324" +dependencies = [ + "leb128", + "thiserror 2.0.18", +] + +[[package]] +name = "hmac" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" +dependencies = [ + "digest 0.10.7", +] + +[[package]] +name = "home" +version = "0.5.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc627f471c528ff0c4a49e1d5e60450c8f6461dd6d10ba9dcd3a61d3dff7728d" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "http" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "601cbb57e577e2f5ef5be8e7b83f0f63994f25aa94d673e54a92d5c516d101f1" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "http" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6970f50e31d6fc17d3fa27329444bfa74e196cf62e95052a3f6fee181dba6425" +dependencies = [ + "bytes", + "itoa", +] + +[[package]] +name = "http-body" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184" +dependencies = [ + "bytes", + "http 1.4.2", +] + +[[package]] +name = "http-body-util" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b021d93e26becf5dc7e1b75b1bed1fd93124b374ceb73f43d4d4eafec896a64a" +dependencies = [ + "bytes", + "futures-core", + "http 1.4.2", + "http-body", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" + +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + +[[package]] +name = "humantime" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "135b12329e5e3ce057a9f972339ea52bc954fe1e9358ef27f95e89716fbc5424" + +[[package]] +name = "hybrid-array" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9155a582abd142abc056962c29e3ce5ff2ad5469f4246b537ed42c5deba857da" +dependencies = [ + "typenum", +] + +[[package]] +name = "hyper" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55281c53a1894c864990125767da440a4e630446785086f52523b20033b74498" +dependencies = [ + "atomic-waker", + "bytes", + "futures-channel", + "futures-core", + "h2 0.4.14", + "http 1.4.2", + "http-body", + "httparse", + "itoa", + "pin-project-lite", + "smallvec", + "tokio", + "want", +] + +[[package]] +name = "hyper-rustls" +version = "0.27.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ca68d021ef39cf6463ab54c1d0f5daf03377b70561305bb89a8f83aab66e0f" +dependencies = [ + "http 1.4.2", + "hyper", + "hyper-util", + "rustls", + "rustls-native-certs", + "tokio", + "tokio-rustls", + "tower-service", +] + +[[package]] +name = "hyper-util" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96547c2556ec9d12fb1578c4eaf448b04993e7fb79cbaad930a656880a6bdfa0" +dependencies = [ + "base64", + "bytes", + "futures-channel", + "futures-util", + "http 1.4.2", + "http-body", + "hyper", + "ipnet", + "libc", + "percent-encoding", + "pin-project-lite", + "socket2 0.6.4", + "tokio", + "tower-service", + "tracing", +] + +[[package]] +name = "iana-time-zone" +version = "0.1.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "log", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "icu_collections" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2984d1cd16c883d7935b9e07e44071dca8d917fd52ecc02c04d5fa0b5a3f191c" +dependencies = [ + "displaydoc", + "potential_utf", + "utf8_iter", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_locale_core" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92219b62b3e2b4d88ac5119f8904c10f8f61bf7e95b640d25ba3075e6cac2c29" +dependencies = [ + "displaydoc", + "litemap", + "tinystr", + "writeable", + "zerovec", +] + +[[package]] +name = "icu_normalizer" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c56e5ee99d6e3d33bd91c5d85458b6005a22140021cc324cea84dd0e72cff3b4" +dependencies = [ + "icu_collections", + "icu_normalizer_data", + "icu_properties", + "icu_provider", + "smallvec", + "zerovec", +] + +[[package]] +name = "icu_normalizer_data" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da3be0ae77ea334f4da67c12f149704f19f81d1adf7c51cf482943e84a2bad38" + +[[package]] +name = "icu_properties" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bee3b67d0ea5c2cca5003417989af8996f8604e34fb9ddf96208a033901e70de" +dependencies = [ + "icu_collections", + "icu_locale_core", + "icu_properties_data", + "icu_provider", + "zerotrie", + "zerovec", +] + +[[package]] +name = "icu_properties_data" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e2bbb201e0c04f7b4b3e14382af113e17ba4f63e2c9d2ee626b720cbce54a14" + +[[package]] +name = "icu_provider" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "139c4cf31c8b5f33d7e199446eff9c1e02decfc2f0eec2c8d71f65befa45b421" +dependencies = [ + "displaydoc", + "icu_locale_core", + "writeable", + "yoke", + "zerofrom", + "zerotrie", + "zerovec", +] + +[[package]] +name = "id-arena" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d3067d79b975e8844ca9eb072e16b31c3c1c36928edf9c6789548c524d0d954" + +[[package]] +name = "idna" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b0875f23caa03898994f6ddc501886a45c7d3d62d04d2d90788d47be1b1e4de" +dependencies = [ + "idna_adapter", + "smallvec", + "utf8_iter", +] + +[[package]] +name = "idna_adapter" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb68373c0d6620ef8105e855e7745e18b0d00d3bdb07fb532e434244cdb9a714" +dependencies = [ + "icu_normalizer", + "icu_properties", +] + +[[package]] +name = "impl-more" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8a5a9a0ff0086c7a148acb942baaabeadf9504d10400b5a05645853729b9cd2" + +[[package]] +name = "impl-trait-for-tuples" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0eb5a3343abf848c0984fe4604b2b105da9539376e24fc0a3b0007411ae4fd9" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "indexmap" +version = "2.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d466e9454f08e4a911e14806c24e16fba1b4c121d1ea474396f396069cf949d9" +dependencies = [ + "equivalent", + "hashbrown 0.17.1", + "serde", + "serde_core", +] + +[[package]] +name = "inout" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "879f10e63c20629ecabbb64a8010319738c66a5cd0c29b02d63d272b03751d01" +dependencies = [ + "generic-array", +] + +[[package]] +name = "inventory" +version = "0.3.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4f0c30c76f2f4ccee3fe55a2435f691ca00c0e4bd87abe4f4a851b1d4dac39b" +dependencies = [ + "rustversion", +] + +[[package]] +name = "ipnet" +version = "2.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d98f6fed1fde3f8c21bc40a1abb88dd75e67924f9cffc3ef95607bad8017f8e2" + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + +[[package]] +name = "itertools" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" +dependencies = [ + "either", +] + +[[package]] +name = "itertools" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285" +dependencies = [ + "either", +] + +[[package]] +name = "itoa" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" + +[[package]] +name = "jobserver" +version = "0.1.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9afb3de4395d6b3e67a780b6de64b51c978ecf11cb9a462c66be7d4ca9039d33" +dependencies = [ + "getrandom 0.3.4", + "libc", +] + +[[package]] +name = "js-sys" +version = "0.3.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2025f20d7a4fa7785846e7b63d10a76d3f1cee98ee5cb79ea59703f95e42162" +dependencies = [ + "cfg-if", + "futures-util", + "wasm-bindgen", +] + +[[package]] +name = "language-tags" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4345964bb142484797b161f473a503a434de77149dd8c7427788c6e13379388" + +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] +name = "leb128" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6cc46bac87ef8093eed6f272babb833b6443374399985ac8ed28471ee0918545" + +[[package]] +name = "leb128fmt" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09edd9e8b54e49e587e4f6295a7d29c3ea94d469cb40ab8ca70b288248a81db2" + +[[package]] +name = "libbz2-rs-sys" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34b357333733e8260735ba5894eb928c02ecc69c78715f01a8019e7fa7f2db4c" + +[[package]] +name = "libc" +version = "0.2.186" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66" + +[[package]] +name = "libm" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981" + +[[package]] +name = "linux-raw-sys" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53" + +[[package]] +name = "litemap" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92daf443525c4cce67b150400bc2316076100ce0b3686209eb8cf3c31612e6f0" + +[[package]] +name = "local-channel" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6cbc85e69b8df4b8bb8b89ec634e7189099cea8927a276b7384ce5488e53ec8" +dependencies = [ + "futures-core", + "futures-sink", + "local-waker", +] + +[[package]] +name = "local-waker" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d873d7c67ce09b42110d801813efbc9364414e356be9935700d368351657487" + +[[package]] +name = "lock_api" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" +dependencies = [ + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "953f07c43838f8e6f9758cab68bf5bed85465e7587ebe0b823f1bcd81978ad3a" + +[[package]] +name = "lru-slab" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "112b39cec0b298b6c1999fee3e31427f74f676e4cb9879ed1a121b43661a4154" + +[[package]] +name = "lzma-rust2" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c60a23ffb90d527e23192f1246b14746e2f7f071cb84476dd879071696c18a4a" +dependencies = [ + "crc", + "sha2", +] + +[[package]] +name = "mach2" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dae608c151f68243f2b000364e1f7b186d9c29845f7d2d85bd31b9ad77ad552b" + +[[package]] +name = "matchers" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1525a2a28c7f4fa0fc98bb91ae755d1e2d1505079e05539e35bc876b5d65ae9" +dependencies = [ + "regex-automata", +] + +[[package]] +name = "md-5" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf" +dependencies = [ + "cfg-if", + "digest 0.10.7", +] + +[[package]] +name = "memchr" +version = "2.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b947ae49db0d222b1dbc6b113ce7248a3fc3a6ca21b696717bfc000ba4484d8" + +[[package]] +name = "memory-stats" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c73f5c649995a115e1a0220b35e4df0a1294500477f97a91d0660fb5abeb574a" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + +[[package]] +name = "metrics" +version = "0.23.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3045b4193fbdc5b5681f32f11070da9be3609f189a79f3390706d42587f46bb5" +dependencies = [ + "ahash 0.8.12", + "portable-atomic", +] + +[[package]] +name = "mimalloc-rust-sys" +version = "1.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96f7f32dcc7aeb79781116048bbd27cc819b9db55805690b3379d536d40e9590" +dependencies = [ + "cc", + "cty", +] + +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + +[[package]] +name = "miniz_oxide" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316" +dependencies = [ + "adler2", + "simd-adler32", +] + +[[package]] +name = "mio" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02bd0af71c67b473010cbbc60715ee815645a4dc942899111f494b4b737d6fda" +dependencies = [ + "libc", + "log", + "wasi", + "windows-sys 0.61.2", +] + +[[package]] +name = "nibble_vec" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a5d83df9f36fe23f0c3648c6bbb8b0298bb5f1939c8f2704431371f4b84d43" +dependencies = [ + "smallvec", +] + +[[package]] +name = "nix" +version = "0.27.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2eb04e9c688eff1c89d72b407f168cf79bb9e867a9d3323ed6c01519eb9cc053" +dependencies = [ + "bitflags", + "cfg-if", + "libc", +] + +[[package]] +name = "nix" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46" +dependencies = [ + "bitflags", + "cfg-if", + "cfg_aliases", + "libc", +] + +[[package]] +name = "nix" +version = "0.31.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf20d2fde8ff38632c426f1165ed7436270b44f199fc55284c38276f9db47c3d" +dependencies = [ + "bitflags", + "cfg-if", + "cfg_aliases", + "libc", +] + +[[package]] +name = "nu-ansi-term" +version = "0.50.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "num" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35bd024e8b2ff75562e5f34e7f4905839deb4b22955ef5e73d2fea1b9813cb23" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + +[[package]] +name = "num-bigint" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9" +dependencies = [ + "num-integer", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-conv" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "521739c6d2bac4aa25192232afe6841231376b2b26d4d9fae5ecf8ca5772e441" + +[[package]] +name = "num-derive" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "num-format" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a652d9771a63711fd3c3deb670acfbe5c30a4072e664d7a3bf5a9e1056ac72c3" +dependencies = [ + "arrayvec", + "itoa", +] + +[[package]] +name = "num-integer" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" +dependencies = [ + "num-bigint", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "num_cpus" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91df4bbde75afed763b708b7eee1e8e7651e02d97f6d5dd763e89367e957b23b" +dependencies = [ + "hermit-abi", + "libc", +] + +[[package]] +name = "object_store" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbfbfff40aeccab00ec8a910b57ca8ecf4319b335c542f2edcd19dd25a1e2a00" +dependencies = [ + "async-trait", + "base64", + "bytes", + "chrono", + "form_urlencoded", + "futures", + "http 1.4.2", + "http-body-util", + "httparse", + "humantime", + "hyper", + "itertools 0.14.0", + "md-5", + "parking_lot", + "percent-encoding", + "quick-xml", + "rand 0.9.4", + "reqwest", + "ring", + "rustls-pemfile", + "serde", + "serde_json", + "serde_urlencoded", + "thiserror 2.0.18", + "tokio", + "tracing", + "url", + "walkdir", + "wasm-bindgen-futures", + "web-time", +] + +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" + +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + +[[package]] +name = "oorandom" +version = "11.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e" + +[[package]] +name = "openssl-probe" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c87def4c32ab89d880effc9e097653c8da5d6ef28e6b539d313baaacfbafcbe" + +[[package]] +name = "ordered-float" +version = "3.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1e1c390732d15f1d48471625cd92d154e66db2c56645e29a9cd26f4699f72dc" +dependencies = [ + "num-traits", +] + +[[package]] +name = "ordered-float" +version = "4.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7bb71e1b3fa6ca1c61f383464aaf2bb0e2f8e772a1f01d486832464de363b951" +dependencies = [ + "num-traits", + "rand 0.8.6", + "rkyv", + "serde", +] + +[[package]] +name = "ouroboros" +version = "0.18.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e0f050db9c44b97a94723127e6be766ac5c340c48f2c4bb3ffa11713744be59" +dependencies = [ + "aliasable", + "ouroboros_macro", + "static_assertions", +] + +[[package]] +name = "ouroboros_macro" +version = "0.18.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c7028bdd3d43083f6d8d4d5187680d0d3560d54df4cc9d752005268b41e64d0" +dependencies = [ + "heck 0.4.1", + "proc-macro2", + "proc-macro2-diagnostics", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "owo-colors" +version = "3.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f" + +[[package]] +name = "page_size" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30d5b2194ed13191c1999ae0704b7839fb18384fa22e49b57eeaa97d79ce40da" +dependencies = [ + "libc", + "winapi", +] + +[[package]] +name = "parking_lot" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-link", +] + +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + +[[package]] +name = "pbkdf2" +version = "0.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8ed6a7761f76e3b9f92dfb0a60a6a6477c61024b775147ff0973a02653abaf2" +dependencies = [ + "digest 0.10.7", + "hmac", +] + +[[package]] +name = "percent-encoding" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" + +[[package]] +name = "petgraph" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4c5cc86750666a3ed20bdaf5ca2a0344f9c67674cae0515bec2da16fbaa47db" +dependencies = [ + "fixedbitset", + "indexmap", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" + +[[package]] +name = "pkg-config" +version = "0.3.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19f132c84eca552bf34cab8ec81f1c1dcc229b811638f9d283dceabe58c5569e" + +[[package]] +name = "plotters" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aeb6f403d7a4911efb1e33402027fc44f29b5bf6def3effcc22d7bb75f2b747" +dependencies = [ + "num-traits", + "plotters-backend", + "plotters-svg", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "plotters-backend" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df42e13c12958a16b3f7f4386b9ab1f3e7933914ecea48da7139435263a4172a" + +[[package]] +name = "plotters-svg" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51bae2ac328883f7acdfea3d66a7c35751187f870bc81f94563733a154d7a670" +dependencies = [ + "plotters-backend", +] + +[[package]] +name = "portable-atomic" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c33a9471896f1c69cecef8d20cbe2f7accd12527ce60845ff44c153bb2a21b49" + +[[package]] +name = "postcard" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6764c3b5dd454e283a30e6dfe78e9b31096d9e32036b5d1eaac7a6119ccb9a24" +dependencies = [ + "cobs", + "embedded-io 0.4.0", + "embedded-io 0.6.1", + "heapless", + "serde", +] + +[[package]] +name = "potential_utf" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0103b1cef7ec0cf76490e969665504990193874ea05c85ff9bab8b911d0a0564" +dependencies = [ + "zerovec", +] + +[[package]] +name = "powerfmt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" + +[[package]] +name = "ppmd-rust" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "efca4c95a19a79d1c98f791f10aebd5c1363b473244630bb7dbde1dc98455a24" + +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "prettyplease" +version = "0.2.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b" +dependencies = [ + "proc-macro2", + "syn 2.0.117", +] + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn 1.0.109", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro2" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "proc-macro2-diagnostics" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af066a9c399a26e020ada66a034357a868728e72cd426f3adcd35f80d88d88c8" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", + "version_check", + "yansi", +] + +[[package]] +name = "ptr_meta" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0738ccf7ea06b608c10564b31debd4f5bc5e197fc8bfe088f68ae5ce81e7a4f1" +dependencies = [ + "ptr_meta_derive 0.1.4", +] + +[[package]] +name = "ptr_meta" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bcada80daa06c42ed5f48c9a043865edea5dc44cbf9ac009fda3b89526e28607" +dependencies = [ + "ptr_meta_derive 0.2.0", +] + +[[package]] +name = "ptr_meta_derive" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16b845dbfca988fa33db069c0e230574d15a3088f147a87b64c7589eb662c9ac" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "ptr_meta_derive" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bca9224df2e20e7c5548aeb5f110a0f3b77ef05f8585139b7148b59056168ed2" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "quick-xml" +version = "0.38.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b66c2058c55a409d601666cffe35f04333cf1013010882cec174a7467cd4e21c" +dependencies = [ + "memchr", + "serde", +] + +[[package]] +name = "quick_cache" +version = "0.6.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a3db184a8b66cfe87f0263a1de147a6b554c864d1767c6f7fa4eb0e5497b565" +dependencies = [ + "ahash 0.8.12", + "equivalent", + "hashbrown 0.16.1", + "parking_lot", +] + +[[package]] +name = "quinn" +version = "0.11.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e20a958963c291dc322d98411f541009df2ced7b5a4f2bd52337638cfccf20" +dependencies = [ + "bytes", + "cfg_aliases", + "pin-project-lite", + "quinn-proto", + "quinn-udp", + "rustc-hash", + "rustls", + "socket2 0.6.4", + "thiserror 2.0.18", + "tokio", + "tracing", + "web-time", +] + +[[package]] +name = "quinn-proto" +version = "0.11.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "434b42fec591c96ef50e21e886936e66d3cc3f737104fdb9b737c40ffb94c098" +dependencies = [ + "bytes", + "getrandom 0.3.4", + "lru-slab", + "rand 0.9.4", + "ring", + "rustc-hash", + "rustls", + "rustls-pki-types", + "slab", + "thiserror 2.0.18", + "tinyvec", + "tracing", + "web-time", +] + +[[package]] +name = "quinn-udp" +version = "0.5.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "addec6a0dcad8a8d96a771f815f0eaf55f9d1805756410b39f5fa81332574cbd" +dependencies = [ + "cfg_aliases", + "libc", + "once_cell", + "socket2 0.6.4", + "tracing", + "windows-sys 0.60.2", +] + +[[package]] +name = "quote" +version = "1.0.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" + +[[package]] +name = "r-efi" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" + +[[package]] +name = "radium" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" + +[[package]] +name = "radix_trie" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b4431027dcd37fc2a73ef740b5f233aa805897935b8bce0195e41bbf9a3289a" +dependencies = [ + "endian-type", + "nibble_vec", +] + +[[package]] +name = "rand" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca0ecfa931c29007047d1bc58e623ab12e5590e8c7cc53200d5202b69266d8a" +dependencies = [ + "libc", + "rand_chacha 0.3.1", + "rand_core 0.6.4", + "serde", +] + +[[package]] +name = "rand" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44c5af06bb1b7d3216d91932aed5265164bf384dc89cd6ba05cf59a35f5f76ea" +dependencies = [ + "rand_chacha 0.9.0", + "rand_core 0.9.5", +] + +[[package]] +name = "rand" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2e8e8bcc7961af1fdac401278c6a831614941f6164ee3bf4ce61b7edb162207" +dependencies = [ + "chacha20", + "getrandom 0.4.2", + "rand_core 0.10.1", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core 0.6.4", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core 0.9.5", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom 0.2.17", + "serde", +] + +[[package]] +name = "rand_core" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c" +dependencies = [ + "getrandom 0.3.4", +] + +[[package]] +name = "rand_core" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69" + +[[package]] +name = "rayon" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb39b166781f92d482534ef4b4b1b2568f42613b53e5b6c160e24cfbfa30926d" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + +[[package]] +name = "redox_syscall" +version = "0.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" +dependencies = [ + "bitflags", +] + +[[package]] +name = "regex" +version = "1.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1292b7759ae1cb9ec195452d1390a074f0cd8541ab7a5a8c31cd6db45d4a6ba" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e1dd4122fc1595e8162618945476892eefca7b88c52820e74af6262213cae8f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-lite" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cab834c73d247e67f4fae452806d17d3c7501756d98c8808d7c9c7aa7d18f973" + +[[package]] +name = "regex-syntax" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4" + +[[package]] +name = "rend" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71fe3824f5629716b1589be05dacd749f6aa084c87e00e016714a8cdfccc997c" +dependencies = [ + "bytecheck", +] + +[[package]] +name = "reqwest" +version = "0.12.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eddd3ca559203180a307f12d114c268abf583f59b03cb906fd0b3ff8646c1147" +dependencies = [ + "base64", + "bytes", + "futures-core", + "futures-util", + "h2 0.4.14", + "http 1.4.2", + "http-body", + "http-body-util", + "hyper", + "hyper-rustls", + "hyper-util", + "js-sys", + "log", + "percent-encoding", + "pin-project-lite", + "quinn", + "rustls", + "rustls-native-certs", + "rustls-pki-types", + "serde", + "serde_json", + "serde_urlencoded", + "sync_wrapper", + "tokio", + "tokio-rustls", + "tokio-util", + "tower", + "tower-http", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "wasm-streams", + "web-sys", +] + +[[package]] +name = "ring" +version = "0.17.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7" +dependencies = [ + "cc", + "cfg-if", + "getrandom 0.2.17", + "libc", + "untrusted", + "windows-sys 0.52.0", +] + +[[package]] +name = "rkyv" +version = "0.7.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2297bf9c81a3f0dc96bc9521370b88f054168c29826a75e89c55ff196e7ed6a1" +dependencies = [ + "bitvec", + "bytecheck", + "bytes", + "hashbrown 0.12.3", + "ptr_meta 0.1.4", + "rend", + "rkyv_derive", + "seahash", + "tinyvec", + "uuid", +] + +[[package]] +name = "rkyv_derive" +version = "0.7.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84d7b42d4b8d06048d3ac8db0eb31bcb942cbeb709f0b5f2b2ebde398d3038f5" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "rmp" +version = "0.8.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ba8be72d372b2c9b35542551678538b562e7cf86c3315773cae48dfbfe7790c" +dependencies = [ + "num-traits", +] + +[[package]] +name = "rmp-serde" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72f81bee8c8ef9b577d1681a70ebbc962c232461e397b22c208c43c04b67a155" +dependencies = [ + "rmp", + "serde", +] + +[[package]] +name = "roaring" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ba9ce64a8f45d7fc86358410bb1a82e8c987504c0d4900e9141d69a9f26c885" +dependencies = [ + "bytemuck", + "byteorder", +] + +[[package]] +name = "rustc-hash" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94300abf3f1ae2e2b8ffb7b58043de3d399c73fa6f4b73826402a5c457614dbe" + +[[package]] +name = "rustc_version" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" +dependencies = [ + "semver", +] + +[[package]] +name = "rustix" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6fe4565b9518b83ef4f91bb47ce29620ca828bd32cb7e408f0062e9930ba190" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.61.2", +] + +[[package]] +name = "rustls" +version = "0.23.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef86cd5876211988985292b91c96a8f2d298df24e75989a43a3c73f2d4d8168b" +dependencies = [ + "once_cell", + "ring", + "rustls-pki-types", + "rustls-webpki", + "subtle", + "zeroize", +] + +[[package]] +name = "rustls-native-certs" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dab5152771c58876a2146916e53e35057e1a4dfa2b9df0f0305b07f611fdea4d" +dependencies = [ + "openssl-probe", + "rustls-pki-types", + "schannel", + "security-framework", +] + +[[package]] +name = "rustls-pemfile" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dce314e5fee3f39953d46bb63bb8a46d40c2f8fb7cc5a3b6cab2bde9721d6e50" +dependencies = [ + "rustls-pki-types", +] + +[[package]] +name = "rustls-pki-types" +version = "1.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30a7197ae7eb376e574fe940d068c30fe0462554a3ddbe4eca7838e049c937a9" +dependencies = [ + "web-time", + "zeroize", +] + +[[package]] +name = "rustls-webpki" +version = "0.103.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61c429a8649f110dddef65e2a5ad240f747e85f7758a6bccc7e5777bd33f756e" +dependencies = [ + "ring", + "rustls-pki-types", + "untrusted", +] + +[[package]] +name = "rustversion" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" + +[[package]] +name = "rustyline" +version = "18.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a990b25f351b25139ddc7f21ee3f6f56f86d6846b74ac8fad3a719a287cd4a0" +dependencies = [ + "bitflags", + "cfg-if", + "clipboard-win", + "home", + "libc", + "log", + "memchr", + "nix 0.31.3", + "radix_trie", + "unicode-segmentation", + "unicode-width 0.2.2", + "utf8parse", + "windows-sys 0.61.2", +] + +[[package]] +name = "ryu" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f" + +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "schannel" +version = "0.1.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91c1b7e4904c873ef0710c1f407dde2e6287de2bebc1bbbf7d430bb7cbffd939" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "seahash" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c107b6f4780854c8b126e228ea8869f4d7b71260f962fefb57b996b8959ba6b" + +[[package]] +name = "security-framework" +version = "3.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7f4bc775c73d9a02cde8bf7b2ec4c9d12743edf609006c7facc23998404cd1d" +dependencies = [ + "bitflags", + "core-foundation", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "2.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce2691df843ecc5d231c0b14ece2acc3efb62c0a398c7e1d875f3983ce020e3" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "semver" +version = "1.0.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd" + +[[package]] +name = "seq-macro" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bc711410fbe7399f390ca1c3b60ad0f53f80e95c5eb935e52268a0e2cd49acc" + +[[package]] +name = "serde" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "serde_json" +version = "1.0.150" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8014e44b4736ed0538adeecded0fce2a272f22dc9578a7eb6b2d9993c74cfb9" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + +[[package]] +name = "serde_json_path_to_error" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "131f9e72e503904612f46028049e1c873b7b4d725baccedf8a00e3e67904cb1a" +dependencies = [ + "serde", + "serde_json", + "serde_path_to_error", +] + +[[package]] +name = "serde_path_to_error" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10a9ff822e371bb5403e391ecd83e182e0e77ba7f6fe0160b795797109d1b457" +dependencies = [ + "itoa", + "serde", + "serde_core", +] + +[[package]] +name = "serde_urlencoded" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "sha1" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba" +dependencies = [ + "cfg-if", + "cpufeatures 0.2.17", + "digest 0.10.7", +] + +[[package]] +name = "sha1" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aacc4cc499359472b4abe1bf11d0b12e688af9a805fa5e3016f9a386dc2d0214" +dependencies = [ + "cfg-if", + "cpufeatures 0.3.0", + "digest 0.11.3", +] + +[[package]] +name = "sha2" +version = "0.10.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" +dependencies = [ + "cfg-if", + "cpufeatures 0.2.17", + "digest 0.10.7", +] + +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "shlex" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" + +[[package]] +name = "signal-hook-registry" +version = "1.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b" +dependencies = [ + "errno", + "libc", +] + +[[package]] +name = "simd-adler32" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "703d5c7ef118737c72f1af64ad2f6f8c5e1921f818cdcb97b8fe6fc69bf66214" + +[[package]] +name = "simdutf8" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e" + +[[package]] +name = "siphasher" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ee5873ec9cce0195efcb7a4e9507a04cd49aec9c83d0389df45b1ef7ba2e649" + +[[package]] +name = "size-of-derive" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eefff4890f5308d477f3da563af8bdb8fbb6fabaec4c974bd211896fa7945e68" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "slab" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" + +[[package]] +name = "smallvec" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" +dependencies = [ + "serde", +] + +[[package]] +name = "smawk" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7c388c1b5e93756d0c740965c41e8822f866621d41acbdf6336a6a168f8840c" + +[[package]] +name = "snap" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b6b67fb9a61334225b5b790716f609cd58395f895b3fe8b328786812a40bc3b" + +[[package]] +name = "socket2" +version = "0.5.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e22376abed350d73dd1cd119b57ffccad95b4e585a7cda43e286245ce23c0678" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + +[[package]] +name = "socket2" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52d1cfed4120b4d927bf7c0f86d2087a4a7d6027c906d9f9d525a80573b9be51" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "spin" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" +dependencies = [ + "lock_api", +] + +[[package]] +name = "stable_deref_trait" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" + +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "2.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "sync_wrapper" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" +dependencies = [ + "futures-core", +] + +[[package]] +name = "synstructure" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "tap" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" + +[[package]] +name = "tempfile" +version = "3.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" +dependencies = [ + "fastrand", + "getrandom 0.4.2", + "once_cell", + "rustix", + "windows-sys 0.61.2", +] + +[[package]] +name = "termcolor" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "terminal_size" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "230a1b821ccbd75b185820a1f1ff7b14d21da1e442e22c0863ea5f08771a8874" +dependencies = [ + "rustix", + "windows-sys 0.61.2", +] + +[[package]] +name = "textwrap" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7b3e525a49ec206798b40326a44121291b530c963cfb01018f63e135bac543d" +dependencies = [ + "smawk", + "unicode-linebreak", + "unicode-width 0.1.14", +] + +[[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]] +name = "thiserror" +version = "2.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4288b5bcbc7920c07a1149a35cf9590a2aa808e0bc1eafaade0b80947865fbc4" +dependencies = [ + "thiserror-impl 2.0.18", +] + +[[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.117", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc4ee7f67670e9b64d05fa4253e753e016c6c95ff35b89b7941d6b856dec1d5" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "thread-id" +version = "5.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2010d27add3f3240c1fef7959f46c814487b216baee662af53be645ba7831c07" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "thread_local" +version = "1.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f60246a4944f24f6e018aa17cdeffb7818b76356965d03b07d6a9886e8962185" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "time" +version = "0.3.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "743bd48c283afc0388f9b8827b976905fb217ad9e647fae3a379a9283c4def2c" +dependencies = [ + "deranged", + "itoa", + "num-conv", + "powerfmt", + "serde_core", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7694e1cfe791f8d31026952abf09c69ca6f6fa4e1a1229e18988f06a04a12dca" + +[[package]] +name = "time-macros" +version = "0.2.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e70e4c5a0e0a8a4823ad65dfe1a6930e4f4d756dcd9dd7939022b5e8c501215" +dependencies = [ + "num-conv", + "time-core", +] + +[[package]] +name = "tinystr" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8323304221c2a851516f22236c5722a72eaa19749016521d6dff0824447d96d" +dependencies = [ + "displaydoc", + "zerovec", +] + +[[package]] +name = "tinytemplate" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc" +dependencies = [ + "serde", + "serde_json", +] + +[[package]] +name = "tinyvec" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e61e67053d25a4e82c844e8424039d9745781b3fc4f32b8d55ed50f5f667ef3" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + +[[package]] +name = "tokio" +version = "1.52.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fc7f01b389ac15039e4dc9531aa973a135d7a4135281b12d7c1bc79fd57fffe" +dependencies = [ + "bytes", + "libc", + "mio", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "socket2 0.6.4", + "tokio-macros", + "windows-sys 0.61.2", +] + +[[package]] +name = "tokio-macros" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "385a6cb71ab9ab790c5fe8d67f1645e6c450a7ce006a33de03daa956cf70a496" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "tokio-rustls" +version = "0.26.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1729aa945f29d91ba541258c8df89027d5792d85a8841fb65e8bf0f4ede4ef61" +dependencies = [ + "rustls", + "tokio", +] + +[[package]] +name = "tokio-util" +version = "0.7.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ae9cec805b01e8fc3fd2fe289f89149a9b66dd16786abd8b19cfa7b48cb0098" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "tower" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebe5ef63511595f1344e2d5cfa636d973292adc0eec1f0ad45fae9f0851ab1d4" +dependencies = [ + "futures-core", + "futures-util", + "pin-project-lite", + "sync_wrapper", + "tokio", + "tower-layer", + "tower-service", +] + +[[package]] +name = "tower-http" +version = "0.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cfcf7e2740e6fc6d4d688b4ef00650406bb94adf4731e43c096c3a19fe40840" +dependencies = [ + "bitflags", + "bytes", + "futures-util", + "http 1.4.2", + "http-body", + "pin-project-lite", + "tower", + "tower-layer", + "tower-service", + "url", +] + +[[package]] +name = "tower-layer" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" + +[[package]] +name = "tower-service" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" + +[[package]] +name = "tracing" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" +dependencies = [ + "log", + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "tracing-core" +version = "0.1.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb7f578e5945fb242538965c2d0b04418d38ec25c79d160cd279bf0731c8d319" +dependencies = [ + "matchers", + "nu-ansi-term", + "once_cell", + "regex-automata", + "sharded-slab", + "smallvec", + "thread_local", + "tracing", + "tracing-core", + "tracing-log", +] + +[[package]] +name = "try-lock" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" + +[[package]] +name = "typedmap" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb93c688a361f7402d0369d5473bb5ce0802710abcaa75c9811cfcb272ce8c44" +dependencies = [ + "dashmap", +] + +[[package]] +name = "typenum" +version = "1.20.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" + +[[package]] +name = "unicode-ident" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" + +[[package]] +name = "unicode-linebreak" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b09c83c3c29d37506a3e260c08c03743a6bb66a9cd432c6934ab501a190571f" + +[[package]] +name = "unicode-segmentation" +version = "1.13.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6f5d3c3b1bf09027a88a6bc961fc00497d651009560b5463668dc81b0fa87a8" + +[[package]] +name = "unicode-width" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" + +[[package]] +name = "unicode-width" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254" + +[[package]] +name = "unicode-xid" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" + +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + +[[package]] +name = "url" +version = "2.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff67a8a4397373c3ef660812acab3268222035010ab8680ec4215f38ba3d0eed" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", + "serde", +] + +[[package]] +name = "utf8_iter" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "utoipa" +version = "4.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c5afb1a60e207dca502682537fefcfd9921e71d0b83e9576060f09abc6efab23" +dependencies = [ + "indexmap", + "serde", + "serde_json", + "utoipa-gen", +] + +[[package]] +name = "utoipa-gen" +version = "4.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20c24e8ab68ff9ee746aad22d39b5535601e6416d1b0feeabf78be986a5c4392" +dependencies = [ + "proc-macro-error", + "proc-macro2", + "quote", + "syn 2.0.117", + "uuid", +] + +[[package]] +name = "uuid" +version = "1.23.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "144d6b123cef80b301b8f72a9e2ca4370ddec21950d0a103dd22c437006d2db7" +dependencies = [ + "getrandom 0.4.2", + "js-sys", + "serde_core", + "wasm-bindgen", +] + +[[package]] +name = "valuable" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + +[[package]] +name = "want" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa7760aed19e106de2c7c0b581b509f2f25d3dacaf737cb82ac61bc6d760b0e" +dependencies = [ + "try-lock", +] + +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + +[[package]] +name = "wasip2" +version = "1.0.3+wasi-0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20064672db26d7cdc89c7798c48a0fdfac8213434a1186e5ef29fd560ae223d6" +dependencies = [ + "wit-bindgen 0.57.1", +] + +[[package]] +name = "wasip3" +version = "0.4.0+wasi-0.3.0-rc-2026-01-06" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5428f8bf88ea5ddc08faddef2ac4a67e390b88186c703ce6dbd955e1c145aca5" +dependencies = [ + "wit-bindgen 0.51.0", +] + +[[package]] +name = "wasm-bindgen" +version = "0.2.123" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a254a4b10c19a76f09a27640e7ffbf9bc30bf67e16a3bf28aaefa4920fe81563" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.73" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54568702fabf5d4849ce2b90fadfa64168a097eaf4b351ce9df8b687a0086aaf" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.123" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24a40fc75b0ec6f3746ceb10d36f53a93dcd68a93b11b6445983945d79eba0dc" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.123" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "908f34bd9b9ce3d4caf07b72dfab63d61504d156856c6bd3cd87fa350cf3985b" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn 2.0.117", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.123" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7acbf7616c27b194bbb550bf77ed0c2c3e5b7fd1260a93082b95fb7f47959b92" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "wasm-encoder" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "990065f2fe63003fe337b932cfb5e3b80e0b4d0f5ff650e6985b1048f62c8319" +dependencies = [ + "leb128fmt", + "wasmparser", +] + +[[package]] +name = "wasm-metadata" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb0e353e6a2fbdc176932bbaab493762eb1255a7900fe0fea1a2f96c296cc909" +dependencies = [ + "anyhow", + "indexmap", + "wasm-encoder", + "wasmparser", +] + +[[package]] +name = "wasm-streams" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15053d8d85c7eccdbefef60f06769760a563c7f0a9d6902a13d35c7800b0ad65" +dependencies = [ + "futures-util", + "js-sys", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", +] + +[[package]] +name = "wasmparser" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47b807c72e1bac69382b3a6fb3dbe8ea4c0ed87ff5629b8685ae6b9a611028fe" +dependencies = [ + "bitflags", + "hashbrown 0.15.5", + "indexmap", + "semver", +] + +[[package]] +name = "web-sys" +version = "0.3.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e0871acf327f283dc6da28a1696cdc64fb355ba9f935d052021fa77f35cce69" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "web-time" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-core" +version = "0.62.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-link", + "windows-result", + "windows-strings", +] + +[[package]] +name = "windows-implement" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "windows-interface" +version = "0.59.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-result" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-strings" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-sys" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2f500e4d28234f72040990ec9d39e3a6b950f9f22d3dba18416c35882612bcb" +dependencies = [ + "windows-targets 0.53.5", +] + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", + "windows_i686_gnullvm 0.52.6", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", +] + +[[package]] +name = "windows-targets" +version = "0.53.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4945f9f551b88e0d65f3db0bc25c33b8acea4d9e41163edf90dcd0b19f9069f3" +dependencies = [ + "windows-link", + "windows_aarch64_gnullvm 0.53.1", + "windows_aarch64_msvc 0.53.1", + "windows_i686_gnu 0.53.1", + "windows_i686_gnullvm 0.53.1", + "windows_i686_msvc 0.53.1", + "windows_x86_64_gnu 0.53.1", + "windows_x86_64_gnullvm 0.53.1", + "windows_x86_64_msvc 0.53.1", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "953f07c43838f8e6f9758cab68bf5bed85465e7587ebe0b823f1bcd81978ad3a" +checksum = "a9d8416fa8b42f5c947f8482c43e7d89e73a173cead56d044f6a56104a6d1b53" [[package]] -name = "matchers" -version = "0.2.0" +name = "windows_aarch64_msvc" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d1525a2a28c7f4fa0fc98bb91ae755d1e2d1505079e05539e35bc876b5d65ae9" -dependencies = [ - "regex-automata", -] +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" [[package]] -name = "memchr" -version = "2.8.1" +name = "windows_aarch64_msvc" +version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b947ae49db0d222b1dbc6b113ce7248a3fc3a6ca21b696717bfc000ba4484d8" +checksum = "b9d782e804c2f632e395708e99a94275910eb9100b2114651e04744e9b125006" [[package]] -name = "nibble_vec" -version = "0.1.0" +name = "windows_i686_gnu" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77a5d83df9f36fe23f0c3648c6bbb8b0298bb5f1939c8f2704431371f4b84d43" -dependencies = [ - "smallvec", -] +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" [[package]] -name = "nix" -version = "0.31.3" +name = "windows_i686_gnu" +version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf20d2fde8ff38632c426f1165ed7436270b44f199fc55284c38276f9db47c3d" -dependencies = [ - "bitflags", - "cfg-if", - "cfg_aliases", - "libc", -] +checksum = "960e6da069d81e09becb0ca57a65220ddff016ff2d6af6a223cf372a506593a3" [[package]] -name = "nu-ansi-term" -version = "0.50.3" +name = "windows_i686_gnullvm" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" -dependencies = [ - "windows-sys", -] +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" [[package]] -name = "once_cell" -version = "1.21.4" +name = "windows_i686_gnullvm" +version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" +checksum = "fa7359d10048f68ab8b09fa71c3daccfb0e9b559aed648a8f95469c27057180c" [[package]] -name = "pin-project-lite" -version = "0.2.17" +name = "windows_i686_msvc" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" [[package]] -name = "postcard" -version = "1.1.3" +name = "windows_i686_msvc" +version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6764c3b5dd454e283a30e6dfe78e9b31096d9e32036b5d1eaac7a6119ccb9a24" -dependencies = [ - "cobs", - "embedded-io 0.4.0", - "embedded-io 0.6.1", - "heapless", - "serde", -] +checksum = "1e7ac75179f18232fe9c285163565a57ef8d3c89254a30685b57d83a38d326c2" [[package]] -name = "proc-macro2" -version = "1.0.106" +name = "windows_x86_64_gnu" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" -dependencies = [ - "unicode-ident", -] +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" [[package]] -name = "quote" -version = "1.0.45" +name = "windows_x86_64_gnu" +version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924" -dependencies = [ - "proc-macro2", -] +checksum = "9c3842cdd74a865a8066ab39c8a7a473c0778a3f29370b5fd6b4b9aa7df4a499" [[package]] -name = "radix_trie" -version = "0.3.0" +name = "windows_x86_64_gnullvm" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b4431027dcd37fc2a73ef740b5f233aa805897935b8bce0195e41bbf9a3289a" -dependencies = [ - "endian-type", - "nibble_vec", -] +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" [[package]] -name = "regex-automata" -version = "0.4.14" +name = "windows_x86_64_gnullvm" +version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e1dd4122fc1595e8162618945476892eefca7b88c52820e74af6262213cae8f" -dependencies = [ - "aho-corasick", - "memchr", - "regex-syntax", -] +checksum = "0ffa179e2d07eee8ad8f57493436566c7cc30ac536a3379fdf008f47f6bb7ae1" [[package]] -name = "regex-syntax" -version = "0.8.10" +name = "windows_x86_64_msvc" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc897dd8d9e8bd1ed8cdad82b5966c3e0ecae09fb1907d58efaa013543185d0a" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] -name = "rustc_version" -version = "0.4.1" +name = "windows_x86_64_msvc" +version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" -dependencies = [ - "semver", -] +checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650" [[package]] -name = "rustyline" -version = "18.0.0" +name = "wit-bindgen" +version = "0.51.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a990b25f351b25139ddc7f21ee3f6f56f86d6846b74ac8fad3a719a287cd4a0" +checksum = "d7249219f66ced02969388cf2bb044a09756a083d0fab1e566056b04d9fbcaa5" dependencies = [ - "bitflags", - "cfg-if", - "clipboard-win", - "home", - "libc", - "log", - "memchr", - "nix", - "radix_trie", - "unicode-segmentation", - "unicode-width", - "utf8parse", - "windows-sys", + "wit-bindgen-rust-macro", ] [[package]] -name = "scopeguard" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" - -[[package]] -name = "semver" -version = "1.0.28" +name = "wit-bindgen" +version = "0.57.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd" +checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" [[package]] -name = "serde" -version = "1.0.228" +name = "wit-bindgen-core" +version = "0.51.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" +checksum = "ea61de684c3ea68cb082b7a88508a8b27fcc8b797d738bfc99a82facf1d752dc" dependencies = [ - "serde_core", - "serde_derive", + "anyhow", + "heck 0.5.0", + "wit-parser", ] [[package]] -name = "serde_core" -version = "1.0.228" +name = "wit-bindgen-rust" +version = "0.51.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" +checksum = "b7c566e0f4b284dd6561c786d9cb0142da491f46a9fbed79ea69cdad5db17f21" dependencies = [ - "serde_derive", + "anyhow", + "heck 0.5.0", + "indexmap", + "prettyplease", + "syn 2.0.117", + "wasm-metadata", + "wit-bindgen-core", + "wit-component", ] [[package]] -name = "serde_derive" -version = "1.0.228" +name = "wit-bindgen-rust-macro" +version = "0.51.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" +checksum = "0c0f9bfd77e6a48eccf51359e3ae77140a7f50b1e2ebfe62422d8afdaffab17a" dependencies = [ + "anyhow", + "prettyplease", "proc-macro2", "quote", - "syn", + "syn 2.0.117", + "wit-bindgen-core", + "wit-bindgen-rust", ] [[package]] -name = "serde_json" -version = "1.0.150" +name = "wit-component" +version = "0.244.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8014e44b4736ed0538adeecded0fce2a272f22dc9578a7eb6b2d9993c74cfb9" +checksum = "9d66ea20e9553b30172b5e831994e35fbde2d165325bec84fc43dbf6f4eb9cb2" dependencies = [ - "itoa", - "memchr", + "anyhow", + "bitflags", + "indexmap", + "log", "serde", - "serde_core", - "zmij", + "serde_derive", + "serde_json", + "wasm-encoder", + "wasm-metadata", + "wasmparser", + "wit-parser", ] [[package]] -name = "sharded-slab" -version = "0.1.7" +name = "wit-parser" +version = "0.244.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +checksum = "ecc8ac4bc1dc3381b7f59c34f00b67e18f910c2c0f50015669dde7def656a736" dependencies = [ - "lazy_static", + "anyhow", + "id-arena", + "indexmap", + "log", + "semver", + "serde", + "serde_derive", + "serde_json", + "unicode-xid", + "wasmparser", ] [[package]] -name = "shlex" -version = "2.0.1" +name = "writeable" +version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" +checksum = "1ffae5123b2d3fc086436f8834ae3ab053a283cfac8fe0a0b8eaae044768a4c4" [[package]] -name = "smallvec" -version = "1.15.1" +name = "wyz" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" +checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed" +dependencies = [ + "tap", +] [[package]] -name = "spin" -version = "0.9.8" +name = "xxhash-rust" +version = "0.8.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" -dependencies = [ - "lock_api", -] +checksum = "fdd20c5420375476fbd4394763288da7eb0cc0b8c11deed431a91562af7335d3" [[package]] -name = "stable_deref_trait" -version = "1.2.1" +name = "yansi" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" +checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049" [[package]] -name = "syn" -version = "2.0.117" +name = "yoke" +version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99" +checksum = "709fe23a0424b6a435d82152b1bd3fdfb0833487d5fa90d05d42762a9891fef5" +dependencies = [ + "stable_deref_trait", + "yoke-derive", + "zerofrom", +] + +[[package]] +name = "yoke-derive" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de844c262c8848816172cef550288e7dc6c7b7814b4ee56b3e1553f275f1858e" dependencies = [ "proc-macro2", "quote", - "unicode-ident", + "syn 2.0.117", + "synstructure", ] [[package]] -name = "thiserror" -version = "2.0.18" +name = "zerocopy" +version = "0.8.52" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4288b5bcbc7920c07a1149a35cf9590a2aa808e0bc1eafaade0b80947865fbc4" +checksum = "ce1022995ff5ff5d841ad7d994facc23098cd40152f2c1d11cd607c6f530653f" dependencies = [ - "thiserror-impl", + "zerocopy-derive", ] [[package]] -name = "thiserror-impl" -version = "2.0.18" +name = "zerocopy-derive" +version = "0.8.52" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ebc4ee7f67670e9b64d05fa4253e753e016c6c95ff35b89b7941d6b856dec1d5" +checksum = "1ae7f38b72ec2a254e2b87ef277cf2cd4fb97cbebf944faa6f33354da0867930" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.117", ] [[package]] -name = "thread_local" -version = "1.1.9" +name = "zerofrom" +version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f60246a4944f24f6e018aa17cdeffb7818b76356965d03b07d6a9886e8962185" +checksum = "0ec05a11813ea801ff6d75110ad09cd0824ddba17dfe17128ea0d5f68e6c5272" dependencies = [ - "cfg-if", + "zerofrom-derive", ] [[package]] -name = "tracing" -version = "0.1.44" +name = "zerofrom-derive" +version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" +checksum = "11532158c46691caf0f2593ea8358fed6bbf68a0315e80aae9bd41fbade684a1" dependencies = [ - "pin-project-lite", - "tracing-attributes", - "tracing-core", + "proc-macro2", + "quote", + "syn 2.0.117", + "synstructure", ] [[package]] -name = "tracing-attributes" -version = "0.1.31" +name = "zeroize" +version = "1.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da" +checksum = "b97154e67e32c85465826e8bcc1c59429aaaf107c1e4a9e53c8d8ccd5eff88d0" dependencies = [ - "proc-macro2", - "quote", - "syn", + "zeroize_derive", ] [[package]] -name = "tracing-core" -version = "0.1.36" +name = "zeroize_derive" +version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" +checksum = "85a5b4158499876c763cb03bc4e49185d3cccbabb15b33c627f7884f43db852e" dependencies = [ - "once_cell", - "valuable", + "proc-macro2", + "quote", + "syn 2.0.117", ] [[package]] -name = "tracing-log" -version = "0.2.0" +name = "zerotrie" +version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +checksum = "0f9152d31db0792fa83f70fb2f83148effb5c1f5b8c7686c3459e361d9bc20bf" dependencies = [ - "log", - "once_cell", - "tracing-core", + "displaydoc", + "yoke", + "zerofrom", ] [[package]] -name = "tracing-subscriber" -version = "0.3.23" +name = "zerovec" +version = "0.11.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb7f578e5945fb242538965c2d0b04418d38ec25c79d160cd279bf0731c8d319" +checksum = "90f911cbc359ab6af17377d242225f4d75119aec87ea711a880987b18cd7b239" dependencies = [ - "matchers", - "nu-ansi-term", - "once_cell", - "regex-automata", - "sharded-slab", - "smallvec", - "thread_local", - "tracing", - "tracing-core", - "tracing-log", + "yoke", + "zerofrom", + "zerovec-derive", ] [[package]] -name = "unicode-ident" -version = "1.0.24" +name = "zerovec-derive" +version = "0.11.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" +checksum = "625dc425cab0dca6dc3c3319506e6593dcb08a9f387ea3b284dbd52a92c40555" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] [[package]] -name = "unicode-segmentation" -version = "1.13.3" +name = "zip" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c6f5d3c3b1bf09027a88a6bc961fc00497d651009560b5463668dc81b0fa87a8" +checksum = "eb2a05c7c36fde6c09b08576c9f7fb4cda705990f73b58fe011abf7dfb24168b" +dependencies = [ + "aes", + "arbitrary", + "bzip2", + "constant_time_eq 0.3.1", + "crc32fast", + "deflate64", + "flate2", + "getrandom 0.3.4", + "hmac", + "indexmap", + "lzma-rust2", + "memchr", + "pbkdf2", + "ppmd-rust", + "sha1 0.10.6", + "time", + "zeroize", + "zopfli", + "zstd", +] [[package]] -name = "unicode-width" -version = "0.2.2" +name = "zlib-rs" +version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254" +checksum = "3be3d40e40a133f9c916ee3f9f4fa2d9d63435b5fbe1bfc6d9dae0aa0ada1513" [[package]] -name = "utf8parse" -version = "0.2.2" +name = "zmij" +version = "1.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa" [[package]] -name = "valuable" -version = "0.1.1" +name = "zopfli" +version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" +checksum = "f05cd8797d63865425ff89b5c4a48804f35ba0ce8d125800027ad6017d2b5249" +dependencies = [ + "bumpalo", + "crc32fast", + "log", + "simd-adler32", +] [[package]] -name = "windows-link" -version = "0.2.1" +name = "zstd" +version = "0.13.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" +checksum = "e91ee311a569c327171651566e07972200e76fcfe2242a4fa446149a3881c08a" +dependencies = [ + "zstd-safe", +] [[package]] -name = "windows-sys" -version = "0.61.2" +name = "zstd-safe" +version = "7.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +checksum = "8f49c4d5f0abb602a93fb8736af2a4f4dd9512e36f7f570d66e65ff867ed3b9d" dependencies = [ - "windows-link", + "zstd-sys", ] [[package]] -name = "zmij" -version = "1.0.21" +name = "zstd-sys" +version = "2.0.16+zstd.1.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa" +checksum = "91e19ebc2adc8f83e43039e79776e3fda8ca919132d68a1fed6a5faca2683748" +dependencies = [ + "cc", + "pkg-config", +] diff --git a/Cargo.toml b/Cargo.toml index 62ba277d..78d7fcfa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,3 +1,20 @@ [workspace] -members = ["packages/coln-lang-rs", "packages/coln-store"] +members = [ + "packages/coln-lang-rs", + "packages/coln-store", + "packages/coln-query" +] resolver = "3" + +[workspace.package] +version = "0.0.1" +authors = ["The Coln Authors"] +description = "A proof assistant with database characteristics" +homepage = "https://github.com/coln-project/Coln" +repository = "https://github.com/coln-project/Coln" + +# Useful if we want to align on one version of a shared dependency. +[workspace.dependencies] +anyhow = "1.0.102" +thiserror = "2.0.18" +criterion = "0.8.2" diff --git a/packages/coln-query/Cargo.toml b/packages/coln-query/Cargo.toml new file mode 100644 index 00000000..2cd59cd0 --- /dev/null +++ b/packages/coln-query/Cargo.toml @@ -0,0 +1,30 @@ +[package] +name = "coln-query" +version = "0.1.0" +edition = "2024" + +[features] +test = [] + +[dependencies] +anyhow = { workspace = true } +thiserror = { workspace = true } +dbsp = "0.305.0" +rkyv = { version = "0.7.46", default-features = false, features = [ + "std", + "size_64", + "validation", + "uuid", +] } +size-of = { version = "0.1.7", package = "feldera-size-of", features = [ + "hashbrown", + "time-std", + "xxhash-xxh3", + "arcstr", + "chrono", + "ordered-float", +] } +cli-table = "0.4.9" + +[dev-dependencies] +criterion = { workspace = true } diff --git a/packages/coln-query/src/api.rs b/packages/coln-query/src/api.rs new file mode 100644 index 00000000..f06aa3ca --- /dev/null +++ b/packages/coln-query/src/api.rs @@ -0,0 +1,230 @@ +//! This module defines the public API of the query engine. It consists of: +//! +//! 1. An interface for passing deltas of row-oriented data. There is +//! [TableDelta], [StoreDelta], and [DerivedDataDelta]. +//! 2. An interface for [Transactions](Tx). A transactions can be in exactly one +//! state of [Prepare], [Pending], [Committed], [Aborted], [Rejected]. +//! 3. An interface for constructing the query engine's IR (TBD with RFC 03). + +use crate::scalar::ScalarTypedValue; +use std::error::Error; + +pub struct Table { + name: String, + // TODO: How does the schema interface look like? Maybe it doesn't need to + // be included every time as the schema should be implicitly given through + // the initial queries derived from `coln-compiler`'s IR. + schema: (), +} + +pub type ZWeight = i64; +/// An update to a table. It either represents an insertion or a deletion of a +/// row from a table, see [`z_weight`](`Self::z_weight`) documentation. +pub struct TableDelta { + /// A ZWeight value ... + /// - `== 0` is not allowed per convention but it does not do harm, it + /// behaves as if there was no insertion happening at all. + /// - `n if n > 0` represents an insertion. If `n > 1` it is a duplicated + /// insertion, that is, the row is inserted n-times. + /// - `n if n < 0` represents a deletion. If `n < 1` we remove the row + /// n-times. + z_weight: ZWeight, + /// The row-oriented data. + row: Vec, +} + +/// An update of the EDBPs, that is, insertions or deletions of base facts. +pub struct StoreDelta { + inner: Vec<(Table, Vec)>, +} + +/// An update of the IDBPs, that is, insertions or deletions of derived facts. +pub struct DerivedDataDelta { + /// Contains the delta in the IDB after applying a delta in the EDB (the + /// latter is a [`StoreDelta`]). + inner: Vec<(Table, Vec)>, +} + +/// For each query checking a constraint this reports back identified +/// counterexamples. +pub struct Violations { + /// Contains the counter examples for each unmet constraint. Note that + /// [`Table`] refers to a derived view (defined through a query) rather + /// than a physical input table here. + inner: Vec<(Table, Vec)>, +} + +/// We use the Typestate-Pattern for compile-time enforced transaction states +/// and their transitions to provide a hard-to-abuse API. +pub struct Tx { + state: State, +} + +/// This is the initial state of a Transaction and it is open to receive table +/// deltas. +pub struct Prepare { + delta: StoreDelta, +} + +/// The transaction is ready to apply in theory, that is, all constraints are +/// met. Still, it awaits either an approval or an end user abort. Without an +/// explicit approval, any state change caused by the transaction will be +/// undone. +pub struct Pending<'a, Store: TxStore> { + store: &'a mut Store, + derived_data_delta: DerivedDataDelta, +} + +/// The transaction is finalized and applied to both the storage and query +/// engine. Any state caused by the transaction is already committed. +pub struct Committed { + derived_data_delta: DerivedDataDelta, +} + +/// The transaction is committable in theory, that is, it does _not_ violate any +/// constraint but the end user decided to abort regardless. Any state caused by +/// the transaction is already rolled back. +pub struct Aborted {} + +/// The transaction _must be_ rejected because some constraint is violated. Any +/// state caused by the transaction is already rolled back. +pub struct Rejected { + violations: Violations, +} + +// Open Questions: +// +// 1. How pure do we want the query engine to be? In some ways constraint +// violations are just the result of some queries (antijoins). Do we want to +// "higher" (as in the opposite of lowering) them as part of the query engine +// to constraint violations already? Or shall we just pass through the raw +// query results to higher layers and they figure out the intepretation of +// the query results? (For the antijoin approach that would be checking if +// the result is empty (constraint met) or, otherwise, the output serve as +// counterexamples.) Shall that duty be up to the query engine? +// +// 2. In case of chased laws (inside rules): Are they just some inevitable (but +// somewhat irrelevant) computation step to figure out if all constraints are +// met, or do they require back propagation up to the end user (i.e. they +// need to be communicated back from the query engine)? +// +// 3. Assuming constraint violation interpretation (1) is on the query engine +// and the results of chased laws (IDB) need to be visible to users (2), are +// the two exclusive? I.e., are there cases in which constraints are violated +// but derived data (IDB) caused by the (invalid) transaction is still valid? +// If this is _not_ the case, the interface below may be a decent fit. +// Implementation-wise I suggest we do not communicate a constraint violation +// as an Error case (but rather reserve these for harder engine errors) and +// instead treat a constraint violation as a perfectly valid outcome (part of +// Ok case). +pub enum ExecutionOutcome { + DerivedDataDelta(DerivedDataDelta), + Violations(Violations), +} + +pub enum TryCommitOk<'a, Store: TxStore> { + Pending(Tx>), + Rejected(Tx), +} + +pub enum TryCommitErr { + ExecutionError(ExecutionError), + RollbackError(RollbackError), +} + +impl Tx { + pub fn new(store_delta: StoreDelta) -> Self { + Tx { + state: Prepare { delta: store_delta }, + } + } + /// Convenience method to add data beyond initialization. + pub fn insert)>>(&mut self, deltas: I) { + self.state.delta.inner.extend(deltas); + } + pub fn try_commit<'a, Store: TxStore>( + self, + store: &'a mut Store, + ) -> Result, TryCommitErr> + { + match store + .apply(self.state.delta) + .map_err(TryCommitErr::ExecutionError) + .map(Into::::into)? + { + ExecutionOutcome::DerivedDataDelta(delta) => Ok(TryCommitOk::Pending(Tx { + state: Pending { + store, + derived_data_delta: delta, + }, + })), + ExecutionOutcome::Violations(violations) => { + store.rollback().map_err(TryCommitErr::RollbackError)?; + Ok(TryCommitOk::Rejected(Tx { + state: Rejected { violations }, + })) + } + } + } +} + +impl<'a, Store: TxStore> Tx> { + pub fn commit(self) -> Result, Store::CommitError> { + // Prevent the custom Drop implementation from running at this point. + let md = std::mem::ManuallyDrop::new(self.state); + // Move the store ref. This is safe because `md` will never be dropped, + // so we avoid a double-free, *and* because the returned `Committed` + // state inherits the same lifetime as `self`. + let store = unsafe { std::ptr::read(&md.store) }; + // Move the vector. This is safe because `md` will never be dropped, so + // we avoid a double-free. + let derived_data_delta = unsafe { std::ptr::read(&md.derived_data_delta) }; + store.commit()?; + Ok(Tx { + state: Committed { derived_data_delta }, + }) + } + pub fn abort(self) -> Result, Store::RollbackError> { + // Prevent the custom Drop implementation from running at this point. + let md = std::mem::ManuallyDrop::new(self.state); + // Move the store ref. This is safe because `md` will never be dropped, + // so we avoid a double-free, *and* because the returned `Aborted` + // state inherits the same lifetime as `self`. + let store = unsafe { std::ptr::read(&md.store) }; + // Move the vector. This is safe because `md` will never be dropped, so + // we avoid a double-free, but also required to free the heap allocation + // behind the vector. + let derived_data_delta = unsafe { std::ptr::read(&md.derived_data_delta) }; + store.rollback()?; + Ok(Tx { state: Aborted {} }) + } +} + +// This is to make the API foolproof: If the caller does neither commit nor +// abort the transaction, we take the conservative approach and rollback any +// state change caused by it. +impl<'a, Store: TxStore> Drop for Pending<'a, Store> { + fn drop(&mut self) { + // Happens in best-effort manner to avoid panicking in Drop impls. + // Possibly log the event, though. + let _ = self.store.rollback(); + } +} + +/// A generic transactional engine/store. It prescribes little things upon the +/// the store and it may also be a good fit for the storage engine's public API? +pub(crate) trait TxStore { + type ExecutionOk: Into; + type ExecutionError: Error + Clone; + type RollbackError: Error + Clone; + type CommitError: Error + Clone; + + /// Executes and applies the transaction given by the updates in `delta`. + fn apply(&mut self, delta: StoreDelta) -> Result; + /// Undoes the last transaction by rolling back every state change caused + /// by that transaction. Should only fail in exceptional circumstances. + fn rollback(&mut self) -> Result<(), Self::RollbackError>; + /// Commits the last transaction. Possibly, a no-op or do some cleanup. + /// Should only fail in exceptional circumstances. + fn commit(&mut self) -> Result<(), Self::CommitError>; +} diff --git a/packages/coln-query/src/context.rs b/packages/coln-query/src/context.rs new file mode 100644 index 00000000..53071aa3 --- /dev/null +++ b/packages/coln-query/src/context.rs @@ -0,0 +1,97 @@ +use std::collections::HashMap; + +use crate::{ + relation::{SchemaTuple, Tuple, TupleSchema}, + resolver::{ScopeStack, VariableMeta}, + scalar::ScalarTypedValue, + stmt::Program, + variable::Environment, +}; + +pub struct ProgramContext { + /// Stores the program code executed so far by the interpreter. + /// Read-only during resolution and interpretation. + pub program: Program, + /// The environment stores the variables and their values. + /// Writable and _changing_ during interpretation. + pub environment: Environment, + /// Scope stack for the resolver. + /// Writable during resolution and not accessed during interpretation. + pub scopes: ScopeStack, +} + +impl Default for ProgramContext { + fn default() -> Self { + Self::new() + } +} + +impl ProgramContext { + pub fn new() -> Self { + Self { + program: Program::empty(), + environment: Environment::default(), + scopes: ScopeStack::new(), + } + } +} + +#[derive(Debug)] +pub struct InterpreterContext<'a> { + pub environment: &'a mut Environment, + /// If the interpreter runs within a DBSP context, we store the currently + /// processing tuple here for making each of its fields accessible + /// as a variable. + // No need to wrap it in an Option because HashMap::new() does not allocate! + pub tuple_vars: HashMap, + /// Stores the most recent alias for a relation. + alias: Option, +} + +impl InterpreterContext<'_> { + pub fn new(environment: &mut Environment) -> InterpreterContext<'_> { + InterpreterContext { + environment, + tuple_vars: HashMap::new(), + alias: None, + } + } + pub fn set_alias(&mut self, alias: String) { + self.alias = Some(alias); + } + pub fn consume_alias(&mut self) -> Option { + self.alias.take() + } + pub fn extend_tuple_ctx( + &mut self, + alias: &Option, + schema: &TupleSchema, + tuple: &T, + ) { + self.tuple_vars + .extend(SchemaTuple::new(schema, tuple).named_fields(alias)); + } + pub fn clear_tuple_ctx(&mut self) { + self.tuple_vars.clear(); + } +} + +pub struct ResolverContext<'a> { + pub scopes: &'a mut ScopeStack, + pub is_tuple_context: bool, +} + +impl ResolverContext<'_> { + pub fn new(scopes: &mut ScopeStack) -> ResolverContext<'_> { + ResolverContext { + scopes, + is_tuple_context: false, + } + } + pub fn begin_tuple_context(&mut self) { + self.is_tuple_context = true; + } + pub fn end_tuple_context(&mut self) { + self.is_tuple_context = false; + } +} diff --git a/packages/coln-query/src/dbsp.rs b/packages/coln-query/src/dbsp.rs new file mode 100644 index 00000000..0918f6d7 --- /dev/null +++ b/packages/coln-query/src/dbsp.rs @@ -0,0 +1,1110 @@ +use crate::{ + expr::{Literal, LiteralExpr}, + relation::{Relation, RelationSchema, SchemaTuple, TupleKey, TupleValue}, +}; +use cli_table::{Cell, Style, Table, format::Justify}; +pub use dbsp::{ + CircuitHandle, DBData as DbData, DBSPHandle as DbspHandle, Error as DbspError, NestedCircuit, + RootCircuit, Runtime, ZWeight, dynamic::ArchivedDBData as ArchivedDbData, utils::IsNone, zset, +}; +use dbsp::{ + IndexedZSetHandle, IndexedZSetReader, OrdIndexedZSet, OrdZSet, OutputHandle, Stream, + typed_batch::SpineSnapshot, utils::Tup2, +}; +use std::{ + collections::HashMap, + fmt::{Debug, Display}, + iter, +}; + +type OrdStream = Stream>; + +struct OrderedTestStream { + inner: OrdStream, + other: OrdStream, +} + +impl OrderedTestStream { + fn try_out(&self) { + let selected: OrdStream = self.inner.filter(move |tuple| { + // execute filter expression + // return result + true + }); + + // Issue: Apparently, there is no way to select a join key here, + // the entire tuple will be matched, hence, the OrdZSet is not a good + // choice here! + let joined: OrdStream = selected.join_generic(&self.other, |k, _, _| { + // merge left and right tuple + Some((k.clone(), ())) + }); + + let projected: OrdStream = joined.map(|k| { + // project tuple + k.clone() + // (k.clone(), ()) + }); + } +} + +struct IndexedTestStream { + a: Relation, + b: Relation, +} + +impl IndexedTestStream { + fn try_out(&self) { + let copy: usize = 12; + let non_copy = String::from("hi"); + + let selected = self.a.inner.filter(move |tuple| { + // Put attributes into scope. How? + // - Define vars uninitialized beforehand and just assign them here? This would + // allow running the resolver just once before the interpreter starts. + // - Define vars with a var declaration stmt with an initializer? But this + // requires running the resolver for each tuple. + // - Hacky through the interpreter? + // - Through an (anonymous) function call? + // execute filter expression + // return result + let access_copy = copy; + let access_non_copy = &non_copy; + true + }); + + let joined = selected.join_index(&self.b.inner, |k, left, right| { + // merge left and right tuple + Some((k.clone(), right.clone())) + }); + + let projected = joined.map_index(|(k, tuple)| { + // project tuple + (k.clone(), tuple.clone()) + }); + } +} + +pub fn new_ord_indexed_stream( + circuit: &mut RootCircuit, +) -> (OrdIndexedRootStream, OrdIndexedStreamInputHandle) { + circuit.add_input_indexed_zset::() +} + +pub type OrdIndexedStreamInputHandle = IndexedZSetHandle; + +pub type OrdIndexedStreamOutputHandle = + OutputHandle>>; + +pub type OrdIndexedStream = Stream>; + +pub type OrdIndexedRootStream = OrdIndexedStream; +pub type OrdIndexedNestedStream = OrdIndexedStream; + +/// A wrapper of DBSP's streams carrying [`dbsp::OrdIndexedZSet`] but +/// generic-free over the circuit type. This limits the nesting level to one +/// level but this does not matter for practical applications. +#[derive(Clone)] +pub enum StreamWrapper { + Root(OrdIndexedRootStream), + Nested(OrdIndexedNestedStream), +} + +impl StreamWrapper { + pub fn distinct(&self) -> StreamWrapper { + match self { + Self::Root(stream) => Self::Root(stream.distinct()), + Self::Nested(stream) => Self::Nested(stream.distinct()), + } + } + + pub fn sum<'a, I>(&'a self, streams: I) -> StreamWrapper + where + I: IntoIterator, + { + match self { + Self::Root(stream) => { + Self::Root(stream.sum(streams.into_iter().map(|s| s.expect_root()))) + } + Self::Nested(stream) => { + Self::Nested(stream.sum(streams.into_iter().map(|s| s.expect_nested()))) + } + } + } + + pub fn minus(&self, other: &Self) -> Self { + match self { + Self::Root(stream) => Self::Root(stream.minus(other.expect_root())), + Self::Nested(stream) => Self::Nested(stream.minus(other.expect_nested())), + } + } + + pub fn map_index(&self, map_func: F) -> StreamWrapper + where + F: Fn((&TupleKey, &TupleValue)) -> (TupleKey, TupleValue) + 'static, + { + match self { + Self::Root(stream) => Self::Root(stream.map_index(map_func)), + Self::Nested(stream) => Self::Nested(stream.map_index(map_func)), + } + } + + pub fn filter(&self, filter_func: F) -> Self + where + F: Fn((&TupleKey, &TupleValue)) -> bool + 'static, + { + match self { + Self::Root(stream) => Self::Root(stream.filter(filter_func)), + Self::Nested(stream) => Self::Nested(stream.filter(filter_func)), + } + } + + pub fn join_index(&self, other: &Self, join: F) -> Self + where + F: Fn(&TupleKey, &TupleValue, &TupleValue) -> It + Clone + 'static, + It: IntoIterator + 'static, + { + match self { + Self::Root(stream) => Self::Root(stream.join_index(other.expect_root(), join)), + Self::Nested(stream) => Self::Nested(stream.join_index(other.expect_nested(), join)), + } + } + + pub fn anti_join_index(&self, other: &Self) -> Self { + match self { + Self::Root(stream) => Self::Root(stream.antijoin(other.expect_root())), + Self::Nested(stream) => Self::Nested(stream.antijoin(other.expect_nested())), + } + } + + /// The delta0 operator imports a stream from the parent circuit into the + /// child circuit. + pub fn delta0(&self, child_circuit: &NestedCircuit) -> Self { + match self { + // Transitions from RootStream to NestedStream + Self::Root(stream) => Self::Nested(stream.delta0(child_circuit)), + Self::Nested(stream) => panic!("No further nesting for beyond NestedStreams"), + } + } + + pub fn output(&self) -> OrdIndexedStreamOutputHandle { + match self { + Self::Root(stream) => stream.accumulate_output(), + Self::Nested(stream) => panic!("Nested streams do not support output()"), + } + } + + pub fn expect_root(&self) -> &OrdIndexedRootStream { + if let Self::Root(stream) = self { + stream + } else { + panic!("Expected RootStream") + } + } + + pub fn expect_nested(&self) -> &OrdIndexedNestedStream { + if let Self::Nested(stream) = self { + stream + } else { + panic!("Expected NestedStream") + } + } +} + +impl From for StreamWrapper { + fn from(stream: OrdIndexedRootStream) -> Self { + Self::Root(stream) + } +} + +impl From for StreamWrapper { + fn from(stream: OrdIndexedNestedStream) -> Self { + Self::Nested(stream) + } +} + +impl IntoIterator for &'_ StreamWrapper { + type Item = Self; + type IntoIter = std::iter::Once; + + fn into_iter(self) -> Self::IntoIter { + std::iter::once(self) + } +} + +#[derive(Default, Debug, Clone)] +pub struct DbspInputs { + inputs: HashMap, +} + +impl DbspInputs { + fn insert(&mut self, name: String, input: DbspInput) { + self.inputs.insert(name, input); + } + pub fn get(&self, name: &str) -> Option<&DbspInput> { + self.inputs.get(name) + } + pub fn take(&mut self, name: &str) -> Option { + self.inputs.remove(name) + } + pub fn iter(&self) -> impl Iterator { + self.inputs.values() + } +} + +#[derive(Clone)] +pub struct DbspInput { + schema: RelationSchema, + handle: OrdIndexedStreamInputHandle, +} + +impl DbspInput { + pub fn add( + schema: RelationSchema, + circuit: &mut RootCircuit, + inputs: &mut DbspInputs, + ) -> LiteralExpr { + let (stream, handle) = new_ord_indexed_stream(circuit); + let input = Self { + schema: schema.clone(), + handle, + }; + inputs.insert(schema.name.clone(), input); + LiteralExpr { + value: Literal::Relation(Relation::new(schema, stream)), + } + } + pub fn handle(&self) -> &OrdIndexedStreamInputHandle { + &self.handle + } + pub fn insert<'a, T: Into + Into + Clone + 'a>( + &self, + tuples: impl IntoIterator, + ) { + tuples.into_iter().for_each(|(tuple, z_weight)| { + self.handle + .push(tuple.clone().into(), (tuple.clone().into(), z_weight)) + }) + } + pub fn insert_with_same_weight<'a, T: Into + Into + Clone + 'a>( + &self, + tuples: impl IntoIterator, + z_weight: ZWeight, + ) { + self.insert(tuples.into_iter().map(|tuple| (tuple, z_weight))); + } +} + +impl Debug for DbspInput { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("DbspInput") + .field("schema", &self.schema) + .finish() + } +} + +pub struct DbspOutput { + handle: OrdIndexedStreamOutputHandle, + schema: RelationSchema, +} + +impl DbspOutput { + pub fn new(schema: RelationSchema, handle: OrdIndexedStreamOutputHandle) -> Self { + Self { schema, handle } + } + pub fn to_batch(&self) -> DbspOutputBatch<'_> { + let inner = self + .handle + .concat() + .consolidate() + .iter() + .collect::>(); + DbspOutputBatch { + schema: &self.schema, + inner, + } + } +} + +pub struct DbspOutputBatch<'a> { + schema: &'a RelationSchema, + inner: Vec<(TupleKey, TupleValue, ZWeight)>, +} + +impl DbspOutputBatch<'_> { + const JUSTIFICATION: Justify = Justify::Right; + + pub fn as_table(&self) -> impl Display { + self.inner + .iter() + .map(|(key, tuple, weight)| { + iter::once(weight.to_string().cell().justify(Self::JUSTIFICATION)).chain( + SchemaTuple::new(&self.schema.tuple, tuple) + .fields() + .map(|attribute| attribute.to_string().cell().justify(Self::JUSTIFICATION)) + .collect::>(), + ) + }) + .table() + .title( + iter::once("z-weight".cell()) + .chain(self.schema.tuple.field_names(&None).map(|name| name.cell())), + ) + .bold(true) + .display() + .expect("Table error") + } + pub fn as_debug_table(&self) -> impl Display { + self.inner + .iter() + .map(|(key, tuple, weight)| { + // We ensure that the key and tuple data lengths match the + // respective schema field lengths. + debug_assert!(key.data.len() == self.schema.key.full_len()); + debug_assert!(tuple.data.len() == self.schema.tuple.full_len()); + iter::once(weight.to_string().cell().justify(Self::JUSTIFICATION)) + .chain( + SchemaTuple::new(&self.schema.key, key) + .all_fields() + .map(|attribute| { + attribute.to_string().cell().justify(Self::JUSTIFICATION) + }) + .collect::>(), + ) + .chain( + SchemaTuple::new(&self.schema.tuple, tuple) + .all_fields() + .map(|attribute| { + attribute.to_string().cell().justify(Self::JUSTIFICATION) + }) + .collect::>(), + ) + }) + .table() + .title( + iter::once("z-weight".cell()) + .chain( + self.schema + .key + .all_field_names(&None) + .map(|name| format!("[key] {name}").cell()), + ) + .chain( + self.schema + .tuple + .all_field_names(&None) + .map(|name| format!("[value] {name}").cell()), + ), + ) + .bold(true) + .display() + .expect("Table error") + } + pub fn as_data(&self) -> impl Iterator { + self.inner + .iter() + .map(|(_key, tuple, weight)| (*weight, tuple)) + } + pub fn as_zset(&self) -> OrdZSet { + let keys = self + .inner + .iter() + .map(|(_key, tuple, weight)| { + let tuple: TupleValue = SchemaTuple::new(&self.schema.tuple, tuple) + .fields() + .cloned() + .collect(); + Tup2(tuple, *weight) + }) + .collect::>(); + OrdZSet::from_keys((), keys) + } + pub fn as_debug_zset(&self) -> OrdZSet { + let keys = self + .inner + .iter() + .map(|(_key, tuple, weight)| Tup2(tuple.clone(), *weight)) + .collect::>(); + OrdZSet::from_keys((), keys) + } +} + +#[cfg(test)] +mod test { + use super::*; + use dbsp::{ + Circuit, indexed_zset, + operator::{Generator, Z1}, + utils::{Tup2, Tup3, Tup4}, + zset, zset_set, + }; + use std::{cell::RefCell, num::NonZeroUsize, rc::Rc}; + + fn worker_threads() -> NonZeroUsize { + std::thread::available_parallelism().unwrap_or(NonZeroUsize::new(4).unwrap()) + } + + /// Note that this example only works with acyclic graphs. + #[test] + fn test_transitive_closure() -> Result<(), anyhow::Error> { + const STEPS: usize = 2; + + let (mut circuit_handle, (edges_input, output_handle)) = Runtime::init_circuit( + worker_threads(), + move |root_circuit| { + let (edges, edges_input) = root_circuit.add_input_zset(); + + // Create a base relation with all paths of length 1. + let len_1 = edges.map(|Tup3(from, to, weight)| Tup4(*from, *to, *weight, 1)); + + let closure = root_circuit.recursive( + |child_circuit, len_n_minus_1: Stream<_, OrdZSet>>| { + // Import the `edges` and `len_1` relation from the parent circuit. + let edges = edges.delta0(child_circuit); + let len_1 = len_1.delta0(child_circuit); + + // Perform an iterative step (n-1 to n) through joining the + // paths of length n-1 with the edges. + let len_n = len_n_minus_1 + .map_index(|Tup4(start, end, cum_weight, hopcnt)| { + ( + *end, + Tup4(*start, *end, *cum_weight, *hopcnt), + ) + }) + .join( + &edges.map_index(|Tup3(from, to, weight)| { + (*from, Tup3(*from, *to, *weight)) + }), + |_end_from, + Tup4(start, _end, cum_weight, hopcnt), + Tup3(_from, to, weight)| { + Tup4(*start, *to, cum_weight + weight, hopcnt + 1) + }, + ).plus(&len_1); + + Ok(len_n) + }, + )?; + + Ok(((edges_input), closure.accumulate_output())) + }, + )?; + + let mut edges_data = ([ + // The first clock cycle adds a graph of four nodes: + // |0| -1-> |1| -1-> |2| -2-> |3| -2-> |4| + vec![ + Tup2(Tup3(0_usize, 1_usize, 1_usize), 1_i64), + Tup2(Tup3(1, 2, 1), 1), + Tup2(Tup3(2, 3, 2), 1), + Tup2(Tup3(3, 4, 2), 1), + ], + // The second clock cycle removes the edge |1| -1-> |2|. + vec![Tup2(Tup3(1, 2, 1), -1)], + // The third clock cycle would introduce a cycle but that would + // cause the fixed point computation to never terminate. + // In total, we have the following graph: + // |0| -1-> |1| -1-> |2| -2-> |3| -2-> |4| + // ^ | + // | | + // ------------------3------------------ + // zset_set! { Tup3(1,2,1), Tup3(4, 0, 3)} + ] as [_; STEPS]) + .into_iter(); + + let mut expected_outputs = ([ + // We expect the full transitive closure in the first clock cycle. + zset! { + Tup4(0_usize, 1_usize, 1_usize, 1_usize) => 1, + Tup4(0, 2, 2, 2) => 1, + Tup4(0, 3, 4, 3) => 1, + Tup4(0, 4, 6, 4) => 1, + Tup4(1, 2, 1, 1) => 1, + Tup4(1, 3, 3, 2) => 1, + Tup4(1, 4, 5, 3) => 1, + Tup4(2, 3, 2, 1) => 1, + Tup4(2, 4, 4, 2) => 1, + Tup4(3, 4, 2, 1) => 1, + }, + // These paths are removed in the second clock cycle. + zset! { + Tup4(0, 2, 2, 2) => -1, + Tup4(0, 3, 4, 3) => -1, + Tup4(0, 4, 6, 4) => -1, + Tup4(1, 2, 1, 1) => -1, + Tup4(1, 3, 3, 2) => -1, + Tup4(1, 4, 5, 3) => -1, + }, + // This does not matter anymore, as the computation does not + // terminate anymore due to the cycle. + // zset! {}, + ] as [_; STEPS]) + .into_iter(); + + for _ in 0..STEPS { + edges_input.append(&mut edges_data.next().unwrap()); + circuit_handle.transaction()?; + let output = output_handle.concat().consolidate(); + assert_eq!(output, expected_outputs.next().unwrap()); + } + + Ok(()) + } + + // Taken from the [DBSP docs](https://docs.rs/dbsp/latest/dbsp/circuit/circuit_builder/struct.ChildCircuit.html#method.recursive). + #[test] + fn test_recursive() -> Result<(), anyhow::Error> { + const STEPS: usize = 3; + + // Propagate labels along graph edges. + let (mut circuit, output_handle) = + Runtime::init_circuit(worker_threads(), move |root_circuit| { + // Graph topology. + let mut edges = ([ + // Start with four nodes connected in a cycle. + zset_set! { Tup2(1, 2), Tup2(2, 3), Tup2(3, 4), Tup2(4, 1) }, + // Add an edge. + zset_set! { Tup2(4, 5) }, + // Remove an edge, breaking the cycle. + zset! { Tup2(1, 2) => -1 }, + ] as [OrdZSet>; STEPS]) + .into_iter(); + let edges = root_circuit.add_source(Generator::new(move || edges.next().unwrap())); + + // Initial labeling of the graph. + let mut init_labels = ([ + // Start with a single label on node 1. + zset_set! { Tup2(1, "l1".to_string()) }, + // Add a label to node 2. + zset_set! { Tup2(2, "l2".to_string()) }, + zset! {}, + ] as [OrdZSet>; STEPS]) + .into_iter(); + + let init_labels = + root_circuit.add_source(Generator::new(move || init_labels.next().unwrap())); + + let labels = root_circuit.recursive( + |child_circuit, labels: Stream<_, OrdZSet>>| { + // Import `edges` and `init_labels` relations from the parent circuit. + let edges = edges.delta0(child_circuit); + let init_labels = init_labels.delta0(child_circuit); + + // Given an edge `from -> to` where the `from` node is labeled with `l`, + // propagate `l` to node `to`. + let result = labels + .map_index(|Tup2(x, y)| (*x, y.clone())) + .join(&edges.map_index(|Tup2(x, y)| (*x, *y)), |_from, l, to| { + Tup2(*to, l.clone()) + }) + .plus(&init_labels); + Ok(result) + }, + )?; + + Ok(labels.accumulate_output()) + })?; + + // Expected _changes_ to the output graph labeling after each clock cycle. + let mut expected_outputs = ([ + zset! { + Tup2(1, "l1".to_string()) => 1, + Tup2(2, "l1".to_string()) => 1, + Tup2(3, "l1".to_string()) => 1, + Tup2(4, "l1".to_string()) => 1 + }, + zset! { + Tup2(1, "l2".to_string()) => 1, + Tup2(2, "l2".to_string()) => 1, + Tup2(3, "l2".to_string()) => 1, + Tup2(4, "l2".to_string()) => 1, + Tup2(5, "l1".to_string()) => 1, + Tup2(5, "l2".to_string()) => 1 + }, + zset! { + Tup2(2, "l1".to_string()) => -1, + Tup2(3, "l1".to_string()) => -1, + Tup2(4, "l1".to_string()) => -1, + Tup2(5, "l1".to_string()) => -1 + }, + ] as [OrdZSet>; STEPS]) + .into_iter(); + + for _ in 0..STEPS { + circuit.transaction()?; + let output = output_handle.concat().consolidate(); + assert_eq!(output, expected_outputs.next().unwrap()); + let as_vec = output.iter().collect::>(); + println!("Output: {as_vec:?}"); + } + + Ok(()) + } + + // Computes the factorial of the first 10 numbers. + #[test] + fn test_factorial_with_iterate() -> Result<(), anyhow::Error> { + let (mut circuit, output) = Runtime::init_circuit(worker_threads(), |circuit| { + // Generate sequence 0, 1, 2, ... + let mut n: usize = 0; + let source = circuit.add_source(Generator::new(move || { + let result = n; + n += 1; + result + })); + // Compute factorial of each number in the sequence. + let fact = circuit.iterate(|child| { + let counter = Rc::new(RefCell::new(1)); + let counter_clone = Rc::clone(&counter); + let countdown = source.delta0(child).apply(move |parent_val| { + let mut counter_borrow = counter_clone.borrow_mut(); + *counter_borrow += *parent_val; + let res = *counter_borrow; + *counter_borrow -= 1; + res + }); + let (z1_output, z1_feedback) = child.add_feedback_with_export(Z1::new(1)); + let multiplication = + countdown.apply2(&z1_output.local, |n1: &usize, n2: &usize| n1 * n2); + z1_feedback.connect(&multiplication); + // Stop iterating when the counter reaches 0. + Ok((async move || Ok(*counter.borrow() == 0), z1_output.export)) + })?; + Ok(fact.output()) + })?; + + let factorial = |n: usize| (1..=n).product::(); + const ITERATIONS: usize = 10; + for i in 0..ITERATIONS { + circuit.transaction()?; + let result = output.take_from_all(); + let result = result.first().unwrap(); + println!("Iteration {:3}: {:3}! = {}", i + 1, i, result); + assert_eq!(*result, factorial(i)); + } + + Ok(()) + } + + // Computes the sum of the first n natural numbers. + #[test] + fn test_sum_n_natural_numbers_with_add_feedback() -> Result<(), anyhow::Error> { + let (mut circuit, output) = Runtime::init_circuit(worker_threads(), |circuit| { + // Create a data source. + let mut n: usize = 1; + let source = circuit.add_source(Generator::new(move || { + let result = n; + n += 1; + result + })); + // Create z1. `z1_output` will contain the output stream of `z1`; `z1_feedback` + // is a placeholder where we can later plug the input to `z1`. + let (z1_output, z1_feedback) = circuit.add_feedback(Z1::new(0)); + // Connect outputs of `source` and `z1` to the plus operator. + let plus = source.apply2(&z1_output, |n1: &usize, n2: &usize| n1 + n2); + // Connect the output of `+` as input to `z1`. + z1_feedback.connect(&plus); + + Ok(z1_output.output()) + })?; + + let sum_first_natural_numbers = |n: usize| (n * (n + 1)) / 2; + let iterations = 10; + for i in 1..=iterations { + circuit.transaction()?; + let result = output.take_from_all(); + let result = result.first().unwrap(); + println!("Step {:3}: \\sum_{{i=0}}^{} i = {}", i, i - 1, result); + assert_eq!(*result, sum_first_natural_numbers(i - 1)); + } + + Ok(()) + } + + #[test] + fn test_not_operator() -> Result<(), anyhow::Error> { + const STEPS: usize = 2; + + let (mut circuit, ((left_input, right_input), output)) = + Runtime::init_circuit(worker_threads(), |root_circuit| { + let (left, left_input) = + root_circuit.add_input_indexed_zset::, Tup2>(); + + let (right, right_input) = + root_circuit.add_input_indexed_zset::, Tup2>(); + + let set_minus = left.minus(&right); + + Ok(((left_input, right_input), set_minus.accumulate_output())) + })?; + + let mut left_data = ([ + vec![ + Tup2(Tup2(1_usize, 1_usize), Tup2(Tup2(1_usize, 1_usize), 1_i64)), + Tup2(Tup2(1, 2), Tup2(Tup2(1, 2), 1)), + Tup2(Tup2(1, 3), Tup2(Tup2(1, 3), 1)), + Tup2(Tup2(1, 4), Tup2(Tup2(1, 4), 1)), + ], + vec![], + ] as [_; STEPS]) + .into_iter(); + + let mut right_data = ([ + vec![ + Tup2(Tup2(1_usize, 2_usize), Tup2(Tup2(1_usize, 2_usize), 1_i64)), + Tup2(Tup2(1, 3), Tup2(Tup2(1, 3), 1)), + ], + vec![Tup2(Tup2(1, 4), Tup2(Tup2(1, 4), 1))], + ] as [_; STEPS]) + .into_iter(); + + let mut expected_outputs = ([ + indexed_zset! {Tup2 => Tup2: + Tup2(1, 1) => { Tup2(1, 1) => 1 }, + Tup2(1, 4) => { Tup2(1, 4) => 1 }, + }, + indexed_zset! {Tup2 => Tup2: + Tup2(1, 4) => { Tup2(1, 4) => -1 }, + }, + ] as [_; STEPS]) + .into_iter(); + + for i in 1..=STEPS { + left_input.append(&mut left_data.next().unwrap()); + right_input.append(&mut right_data.next().unwrap()); + circuit.transaction()?; + let output = output.concat().consolidate(); + println!("{output:?}"); + assert_eq!(output, expected_outputs.next().unwrap()); + } + + Ok(()) + } + + #[test] + fn test_cartesian_product() -> Result<(), anyhow::Error> { + const STEPS: usize = 2; + + let (mut circuit, ((left_input, right_input), output)) = + Runtime::init_circuit(worker_threads(), |root_circuit| { + let (left, left_input) = + root_circuit.add_input_indexed_zset::, Tup2>(); + let left = left.map_index(|(_k, v)| ((), *v)); + + let (right, right_input) = + root_circuit.add_input_indexed_zset::, Tup2>(); + let right = right.map_index(|(_k, v)| ((), *v)); + + let cartesian_product = + left.join_index(&right, |_k, Tup2(l1, l2), Tup2(r1, r2)| { + // Merge left and right tuples. + Some(((), Tup4(*l1, *l2, *r1, *r2))) + }); + + Ok(( + (left_input, right_input), + cartesian_product.accumulate_output(), + )) + })?; + + let mut left_data = ([ + vec![ + Tup2(Tup2(1_usize, 1_usize), Tup2(Tup2(1_usize, 1_usize), 1_i64)), + Tup2(Tup2(1, 1), Tup2(Tup2(1, 1), 1)), // duplicate of the above! + Tup2(Tup2(1, 2), Tup2(Tup2(1, 2), 1)), + Tup2(Tup2(1, 3), Tup2(Tup2(1, 3), 1)), + ], + vec![], + ] as [_; STEPS]) + .into_iter(); + + let mut right_data = ([ + vec![ + Tup2(Tup2(2_usize, 1_usize), Tup2(Tup2(2_usize, 1_usize), 1_i64)), + Tup2(Tup2(2, 2), Tup2(Tup2(2, 2), 1)), + ], + vec![Tup2(Tup2(2, 3), Tup2(Tup2(2, 3), 1))], + ] as [_; STEPS]) + .into_iter(); + + let mut expected_outputs = ([ + indexed_zset! {() => Tup4: + () => { Tup4(1, 1, 2, 1) => 2 }, + () => { Tup4(1, 1, 2, 2) => 2 }, + () => { Tup4(1, 2, 2, 1) => 1 }, + () => { Tup4(1, 2, 2, 2) => 1 }, + () => { Tup4(1, 3, 2, 1) => 1 }, + () => { Tup4(1, 3, 2, 2) => 1 }, + }, + indexed_zset! {() => Tup4: + () => { Tup4(1, 1, 2, 3) => 2 }, + () => { Tup4(1, 2, 2, 3) => 1 }, + () => { Tup4(1, 3, 2, 3) => 1 }, + }, + ] as [_; STEPS]) + .into_iter(); + + for i in 1..=STEPS { + left_input.append(&mut left_data.next().unwrap()); + right_input.append(&mut right_data.next().unwrap()); + circuit.transaction()?; + let result = output.concat().consolidate(); + println!("Result in {i}: {result:?}"); + assert_eq!(result, expected_outputs.next().unwrap()); + } + + Ok(()) + } + + #[test] + fn negative_zweight_behavior() -> Result<(), anyhow::Error> { + const STEPS: usize = 6; + + let (mut circuit, ((left_input, right_input), output)) = + Runtime::init_circuit(worker_threads(), |root_circuit| { + let (left, left_input) = + root_circuit.add_input_indexed_zset::, Tup2>(); + + let (right, right_input) = + root_circuit.add_input_indexed_zset::, Tup2>(); + + let joined = left.join_index(&right, |k, Tup2(l1, l2), Tup2(r1, r2)| { + // Merge left and right tuples. + Some((*k, Tup4(*l1, *l2, *r1, *r2))) + }); + + Ok(((left_input, right_input), joined.accumulate_output())) + })?; + + let mut left_data = ([ + vec![Tup2( + Tup2(1_usize, 1_usize), + Tup2(Tup2(1_usize, 1_usize), 1_i64), + )], + vec![Tup2(Tup2(1, 1), Tup2(Tup2(1, 1), -3))], + vec![Tup2(Tup2(1, 1), Tup2(Tup2(1, 1), 1))], + vec![Tup2(Tup2(1, 1), Tup2(Tup2(1, 1), 1))], + vec![Tup2(Tup2(1, 1), Tup2(Tup2(1, 1), 1))], + vec![Tup2(Tup2(1, 1), Tup2(Tup2(1, 1), 0))], + ] as [_; STEPS]) + .into_iter(); + + let mut right_data = ([ + vec![Tup2( + Tup2(1_usize, 1_usize), + Tup2(Tup2(2_usize, 2_usize), 1_i64), + )], + vec![], + vec![], + vec![], + vec![], + vec![], + ] as [_; STEPS]) + .into_iter(); + + let mut expected_outputs = ([ + indexed_zset! {Tup2 => Tup4: + Tup2(1, 1) => { Tup4(1, 1, 2, 2) => 1 }, // "Lifetime" Total: +1 + }, + indexed_zset! {Tup2 => Tup4: + Tup2(1, 1) => { Tup4(1, 1, 2, 2) => -3 }, // "Lifetime" Total: -2 + }, + indexed_zset! {Tup2 => Tup4: + Tup2(1, 1) => { Tup4(1, 1, 2, 2) => 1 }, // "Lifetime" Total: -1 + }, + indexed_zset! {Tup2 => Tup4: + Tup2(1, 1) => { Tup4(1, 1, 2, 2) => 1 }, // "Lifetime" Total: +0 + }, + indexed_zset! {Tup2 => Tup4: + Tup2(1, 1) => { Tup4(1, 1, 2, 2) => 1 }, // "Lifetime" Total: +1 + }, + indexed_zset! {Tup2 => Tup4: + // No output delta because the zweight is 0; "Lifetime" Total: +1 + }, + ] as [_; STEPS]) + .into_iter(); + + for i in 1..=STEPS { + left_input.append(&mut left_data.next().unwrap()); + right_input.append(&mut right_data.next().unwrap()); + circuit.transaction()?; + let output = output.concat().consolidate(); + println!("OUTPUT DELTA IN {i}: {output:?}"); + assert_eq!(output, expected_outputs.next().unwrap()); + } + + Ok(()) + } + + #[test] + fn multiple_outputs() -> Result<(), anyhow::Error> { + const STEPS: usize = 2; + + let (mut circuit, ((left_input, right_input), (filter_output, join_output))) = + Runtime::init_circuit(worker_threads(), |root_circuit| { + let (left, left_input) = + root_circuit.add_input_indexed_zset::, Tup2>(); + + let (right, right_input) = + root_circuit.add_input_indexed_zset::, Tup2>(); + + let left_filtered = left.filter(|(k, v)| k.0 == 2); + + let joined = left_filtered.join_index(&right, |k, Tup2(l1, l2), Tup2(r1, r2)| { + // Merge left and right tuples. + Some((*k, Tup4(*l1, *l2, *r1, *r2))) + }); + + // We output both the intermediate filter result and the final join result. + Ok(( + (left_input, right_input), + ( + left_filtered.accumulate_output(), + joined.accumulate_output(), + ), + )) + })?; + + let mut left_data = ([ + vec![ + Tup2(Tup2(1_usize, 1_usize), Tup2(Tup2(1_usize, 1_usize), 1_i64)), + Tup2(Tup2(2, 1), Tup2(Tup2(1, 1), 1)), + Tup2(Tup2(2, 3), Tup2(Tup2(1, 1), 1)), + ], + vec![ + Tup2(Tup2(1, 2), Tup2(Tup2(1, 1), 1)), + Tup2(Tup2(2, 2), Tup2(Tup2(1, 1), 1)), + ], + ] as [_; STEPS]) + .into_iter(); + + let mut right_data = ([ + vec![ + Tup2(Tup2(2_usize, 1_usize), Tup2(Tup2(2_usize, 2_usize), 1_i64)), + Tup2(Tup2(2, 2), Tup2(Tup2(2, 2), 1)), + ], + vec![Tup2(Tup2(2, 3), Tup2(Tup2(2, 2), 1))], + ] as [_; STEPS]) + .into_iter(); + + let mut expected_filter_outputs = ([ + indexed_zset! {Tup2 => Tup2: + Tup2(2, 1) => { Tup2(1, 1) => 1 }, + Tup2(2, 3) => { Tup2(1, 1) => 1 }, + }, + indexed_zset! {Tup2 => Tup2: + Tup2(2, 2) => { Tup2(1, 1) => 1 }, + }, + ] as [_; STEPS]) + .into_iter(); + + let mut expected_join_outputs = ([ + indexed_zset! {Tup2 => Tup4: + Tup2(2, 1) => { Tup4(1, 1, 2, 2) => 1 }, + }, + indexed_zset! {Tup2 => Tup4: + Tup2(2, 2) => { Tup4(1, 1, 2, 2) => 1 }, + Tup2(2, 3) => { Tup4(1, 1, 2, 2) => 1 }, + }, + ] as [_; STEPS]) + .into_iter(); + + for i in 1..=STEPS { + left_input.append(&mut left_data.next().unwrap()); + right_input.append(&mut right_data.next().unwrap()); + circuit.transaction()?; + let filter_result = filter_output.concat().consolidate(); + println!("FILTER {filter_result:?}"); + assert_eq!(filter_result, expected_filter_outputs.next().unwrap()); + let join_result = join_output.concat().consolidate(); + println!("JOIN {join_result:?}"); + assert_eq!(join_result, expected_join_outputs.next().unwrap()); + } + + Ok(()) + } + + #[test] + fn rollback_test() -> Result<(), anyhow::Error> { + const STEPS: usize = 4; + + let (mut circuit, ((left_input, right_input), output)) = + Runtime::init_circuit(worker_threads(), |root_circuit| { + let (left, left_input) = + root_circuit.add_input_indexed_zset::, Tup2>(); + + let (right, right_input) = + root_circuit.add_input_indexed_zset::, Tup2>(); + + let antijoined = left.antijoin(&right); + + Ok(((left_input, right_input), antijoined.accumulate_output())) + })?; + + let mut left_data = ([ + // The initial transaction. + vec![Tup2( + Tup2(1_usize, 1_usize), + // Note: Only the key matters for determining equality. + Tup2(Tup2(1_usize, 1_usize), 1_i64), + )], + // A transaction which causes a constraint violation. + vec![Tup2(Tup2(1, 2), Tup2(Tup2(1, 2), 1))], + // We rollback/undo the previous transaction (by inverting the zweight). + vec![Tup2(Tup2(1, 2), Tup2(Tup2(1, 2), -1))], + // A new transaction which does not violate any constraint. + vec![Tup2(Tup2(1, 2), Tup2(Tup2(1, 2), 1))], + ] as [_; STEPS]) + .into_iter(); + + let mut right_data = ([ + // The initial transaction. + vec![Tup2( + Tup2(1_usize, 1_usize), + // Note: Only the key matters for determining equality. + Tup2(Tup2(2_usize, 2_usize), 1_i64), + )], + // A transaction which causes a constraint violation. + vec![], + // We rollback/undo the previous transaction (by inverting the zweight). + vec![], + // A new transaction which does not violate any constraint. + vec![ + Tup2(Tup2(1, 2), Tup2(Tup2(1, 2), 1)), + // For L ANTIJOIN R it is okay to have R carry "excess" tuples. + Tup2(Tup2(1, 3), Tup2(Tup2(1, 3), 1)), + ], + ] as [_; STEPS]) + .into_iter(); + + let mut expected_outputs = ([ + indexed_zset! {Tup2 => Tup2: + // Empty output, i.e., no constraint violated. + }, + indexed_zset! {Tup2 => Tup2: + // Violation due to non-empty result. + Tup2(1, 2) => { Tup2(1, 2) => 1 }, + }, + indexed_zset! {Tup2 => Tup2: + // Rollback cleans operator state, result does not matter. + Tup2(1, 2) => { Tup2(1, 2) => -1 }, + }, + indexed_zset! {Tup2 => Tup2: + // Empty output, i.e., no constraint violated. + }, + ] as [_; STEPS]) + .into_iter(); + + for i in 1..=STEPS { + left_input.append(&mut left_data.next().unwrap()); + right_input.append(&mut right_data.next().unwrap()); + circuit.transaction()?; + let output = output.concat().consolidate(); + println!("OUTPUT DELTA IN {i}: {output:?}"); + assert_eq!(output, expected_outputs.next().unwrap()); + } + + Ok(()) + } +} diff --git a/packages/coln-query/src/error.rs b/packages/coln-query/src/error.rs new file mode 100644 index 00000000..8c7502d6 --- /dev/null +++ b/packages/coln-query/src/error.rs @@ -0,0 +1,80 @@ +use crate::dbsp::DbspError; +use thiserror::Error; + +#[derive(Error, Debug, Clone, PartialEq, Eq)] +/// Public error type for any Incremental Datalog error. +pub enum IncLogError { + #[error(transparent)] + Syntax(#[from] SyntaxError), + #[error(transparent)] + Optimization(#[from] OptimizationError), + #[error(transparent)] + Runtime(#[from] RuntimeError), + #[error(transparent)] + Engine(#[from] EngineError), +} + +#[derive(Error, Debug, Clone, PartialEq, Eq)] +#[error("{message}")] +/// An error that occurs during parsing or static analysis at compile time. +pub struct SyntaxError { + // TODO: source location + pub message: String, +} + +impl SyntaxError { + pub fn new>(message: T) -> Self { + Self { + message: message.into(), + } + } +} + +#[derive(Error, Debug, Clone, PartialEq, Eq)] +#[error("{message}")] +/// An error that occurs during an optimization pass prior to run time. +pub struct OptimizationError { + pub message: String, +} + +impl OptimizationError { + pub fn new>(message: T) -> Self { + Self { + message: message.into(), + } + } +} + +#[derive(Error, Debug, Clone, PartialEq, Eq)] +#[error("{message}")] +/// An error that occurs during runtime of the tree-walk interpreter. +// TODO: Instead of being generic, we could introduce: +// - a type error +// - a reference error +pub struct RuntimeError { + message: String, +} + +impl RuntimeError { + pub fn new>(message: T) -> Self { + Self { + message: message.into(), + } + } +} + +#[derive(Error, Debug, Clone, PartialEq, Eq)] +#[error("{message}")] +/// An error that occurs during runtime of the underlying engine (currently +/// DBSP). +pub struct EngineError { + message: String, +} + +impl From for IncLogError { + fn from(value: DbspError) -> Self { + IncLogError::Engine(EngineError { + message: value.to_string(), + }) + } +} diff --git a/packages/coln-query/src/expr.rs b/packages/coln-query/src/expr.rs new file mode 100644 index 00000000..ce998cbc --- /dev/null +++ b/packages/coln-query/src/expr.rs @@ -0,0 +1,529 @@ +use super::operator::Operator; +use crate::{ + impl_from_auto_box, + relation::Relation, + stmt::BlockStmt, + util::{MemAddr, Named, Resolvable}, + variable::VariableSlot, +}; +use std::fmt::{self, Debug, Display}; + +#[derive(Clone, Debug, Eq, PartialEq)] +pub enum Expr { + Binary(Box), + Unary(Box), + Grouping(Box), + Var(Box), + Assign(Box), + Literal(Box), + Call(Box), + Function(Box), + Alias(Box), + Distinct(Box), + Union(Box), + Difference(Box), + Selection(Box), + Projection(Box), + CartesianProduct(Box), + EquiJoin(Box), + AntiJoin(Box), + FixedPointIter(Box), +} + +impl_from_auto_box! { + Expr, + (Expr::Binary, BinaryExpr), + (Expr::Unary, UnaryExpr), + (Expr::Grouping, GroupingExpr), + (Expr::Var, VarExpr), + (Expr::Assign, AssignExpr), + (Expr::Literal, LiteralExpr), + (Expr::Call, CallExpr), + (Expr::Function, FunctionExpr), + (Expr::Alias, AliasExpr), + (Expr::Distinct, DistinctExpr), + (Expr::Union, UnionExpr), + (Expr::Difference, DifferenceExpr), + (Expr::Selection, SelectionExpr), + (Expr::Projection, ProjectionExpr), + (Expr::CartesianProduct, CartesianProductExpr), + (Expr::EquiJoin, EquiJoinExpr), + (Expr::AntiJoin, AntiJoinExpr), + (Expr::FixedPointIter, FixedPointIterExpr) +} + +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct BinaryExpr { + pub operator: Operator, + pub left: Expr, + pub right: Expr, +} + +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct UnaryExpr { + pub operator: Operator, + pub operand: Expr, +} + +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct GroupingExpr { + pub expr: Expr, +} + +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct VarExpr { + pub name: String, + pub resolved: Option, +} + +impl VarExpr { + pub fn new>(name: T) -> Self { + Self { + name: name.into(), + resolved: None, + } + } +} + +impl Resolvable for VarExpr { + fn set_resolved(&mut self, info: VariableSlot) { + self.resolved = Some(info); + } +} + +impl Named for VarExpr { + fn name(&self) -> &str { + &self.name + } +} + +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct AssignExpr { + pub name: String, + pub value: Expr, + pub resolved: Option, +} + +impl AssignExpr { + pub fn new>(name: T, value: Expr) -> Self { + Self { + name: name.into(), + value, + resolved: None, + } + } +} + +impl Resolvable for AssignExpr { + fn set_resolved(&mut self, info: VariableSlot) { + self.resolved = Some(info); + } +} + +impl Named for AssignExpr { + fn name(&self) -> &str { + &self.name + } +} + +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct LiteralExpr { + pub value: Literal, +} + +impl> From for LiteralExpr { + fn from(value: T) -> Self { + Self { + value: value.into(), + } + } +} + +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct FunctionExpr { + pub parameters: Vec, + pub body: BlockStmt, +} + +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct CallExpr { + pub callee: Expr, + pub arguments: Vec, +} + +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct AliasExpr { + pub relation: Expr, + pub alias: String, +} + +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct DistinctExpr { + pub relation: Expr, +} + +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct UnionExpr { + /// All `Expr`s must evaluate to a relation and have a compatible schema, + /// that is, the same order and arity of attributes with same types, respectively. + pub relations: Vec, +} + +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct DifferenceExpr { + /// All `Expr`s must evaluate to a relation and have a compatible schema, + /// that is, the same order and arity of attributes with same types, respectively. + pub left: Expr, + pub right: Expr, +} + +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct SelectionExpr { + /// Must evaluate to a relation. + pub relation: Expr, + pub condition: Expr, +} + +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct ProjectionExpr { + /// Must evaluate to a relation. + pub relation: Expr, + /// The attributes to map over. The first element `String` is the name + /// of the attribute. The second element `Expr` is the expression + /// which produces the new value of the attribute. + /// + /// In case the `Expr` is just a `VarExpr` referencing a **tuple** variable, + /// the interpreter is not run to evaluate the expression but instead only + /// the schema is changed. + pub attributes: Vec<(String, Expr)>, +} + +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct CartesianProductExpr { + /// We delegate to an [`EquiJoinExpr`] with an empty `on` clause. + pub inner: EquiJoinExpr, +} + +impl CartesianProductExpr { + pub fn new(left: Expr, right: Expr, attributes: Option>) -> Self { + Self { + inner: EquiJoinExpr { + left, + right, + on: vec![], + attributes, + }, + } + } +} + +/// An equijoin is a join that exclusively uses equality of attribute(s). +/// [More information on join classifications](https://stackoverflow.com/a/7870216). +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct EquiJoinExpr { + /// Must evaluate to a relation. + pub left: Expr, + /// Must evaluate to a relation. + pub right: Expr, + /// The attributes to join on. The first element of any pair belongs to the + /// left relation, and the second element of any pair belongs to right relation. + /// Each attribute pair should produce the same type. + pub on: Vec<(Expr, Expr)>, + /// An optional projection step. See documentation of [`ProjectionExpr`]. + pub attributes: Option>, +} + +/// This is not a commutative operation, that is, swapping the `left` and `right` +/// relations may alter the result. This computes `left` setminus `right` while +/// only considering the columns specified in `on`. +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct AntiJoinExpr { + /// Must evaluate to a relation. + pub left: Expr, + /// Must evaluate to a relation. + pub right: Expr, + /// The attributes to _not_ join on. The first element of any pair belongs to the + /// left relation, and the second element of any pair belongs to right relation. + /// Each attribute pair should produce the same type. + pub on: Vec<(Expr, Expr)>, +} + +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct ThetaJoinExpr { + /// Must evaluate to a relation. + pub left: Expr, + /// Must evaluate to a relation. + pub right: Expr, + /// An arbitrary join condition that is evaluated for each pair of tuples + /// (cartesian product of both relations) in the context containing + /// the attributes of both relations. + /// If the condition evaluates to false, the output does not contain that + /// pair. If the condition evaluates to true, the output contains the pair. + pub on: Expr, + /// An optional projection step. See documentation of [`ProjectionExpr`]. + pub attributes: Option>, +} + +/// Evaluates to a relation/stream again. +#[derive(Clone, Debug)] +pub struct FixedPointIterExpr { + /// The streams from the root circuit to make available in the nested circuit. + /// The relations are available as variables named according to the first respective + /// tuple element in the context of the child circuit, that is, + /// within the the context of the [`step`](FixedPointIterExpr.step) statements. + /// The second tuple elements must evaluate to a relation, respectively. + pub imports: Vec<(String, Expr)>, + /// The accumulator is available as a variable named according to the first + /// tuple element in the context of the child circuit, that is, + /// within the the context of the [`step`](FixedPointIterExpr.step) statements. + /// The second tuple element must evaluate to a relation. + /// The accumulator also defines the schema of the fixed point computation. + pub accumulator: (String, Expr), + /// What to do in each iteration. Runs in the context of the child circuit. + /// The value the last statement evaluates to becomes the accumulator of + /// the next iteration. + pub step: BlockStmt, +} + +impl Eq for FixedPointIterExpr {} + +impl PartialEq for FixedPointIterExpr { + fn eq(&self, other: &Self) -> bool { + self.imports == other.imports + && self.accumulator == other.accumulator + && self.step == other.step + } +} + +#[derive(Clone, Debug)] +pub enum Literal { + /// String. + String(String), + /// Unsigned integer value of 64 bits. + Uint(u64), + /// Signed integer value of 64 bits. + Iint(i64), + /// Boolean. + Bool(bool), + /// Null. + // The `Null` variant carries the unit type to align its field-arity with + // other variants. That eases the definition of macros operating on the enum. + Null(()), + /// Relation (an extensional database predicate). + // With a textual representation of the language, this may be just an + // identifier and the stream with its schema is defined elsewhere. + Relation(Relation), +} + +impl Eq for Literal {} + +impl PartialEq for Literal { + fn eq(&self, other: &Self) -> bool { + match (self, other) { + (Literal::String(a), Literal::String(b)) => a == b, + (Literal::Uint(a), Literal::Uint(b)) => a == b, + (Literal::Iint(a), Literal::Iint(b)) => a == b, + (Literal::Bool(a), Literal::Bool(b)) => a == b, + (Literal::Null(()), Literal::Null(())) => true, + _ => false, + } + } +} + +impl From for Literal { + fn from(value: String) -> Self { + Literal::String(value) + } +} + +impl From<&str> for Literal { + fn from(value: &str) -> Self { + Literal::String(value.to_string()) + } +} + +impl From for Literal { + fn from(value: u64) -> Self { + Literal::Uint(value) + } +} + +impl From for Literal { + fn from(value: i64) -> Self { + Literal::Iint(value) + } +} + +impl From for Literal { + fn from(value: bool) -> Self { + Literal::Bool(value) + } +} + +impl From<()> for Literal { + fn from(_: ()) -> Self { + Literal::Null(()) + } +} + +impl Display for Literal { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Literal::String(value) => write!(f, "{value}"), + Literal::Uint(value) => write!(f, "{value}"), + Literal::Iint(value) => write!(f, "{value}"), + Literal::Bool(value) => write!(f, "{value}"), + Literal::Null(()) => write!(f, "null"), + Literal::Relation(value) => write!(f, "relation {}", value.schema.name), + } + } +} + +pub trait ExprVisitor { + fn visit_expr(&mut self, expr: &Expr, ctx: C) -> T { + match expr { + Expr::Binary(expr) => self.visit_binary_expr(expr, ctx), + Expr::Unary(expr) => self.visit_unary_expr(expr, ctx), + Expr::Grouping(expr) => self.visit_grouping_expr(expr, ctx), + Expr::Var(expr) => self.visit_var_expr(expr, ctx), + Expr::Assign(expr) => self.visit_assign_expr(expr, ctx), + Expr::Literal(expr) => self.visit_literal_expr(expr, ctx), + Expr::Function(expr) => self.visit_function_expr(expr, ctx), + Expr::Call(expr) => self.visit_call_expr(expr, ctx), + Expr::Alias(expr) => self.visit_alias_expr(expr, ctx), + Expr::Distinct(expr) => self.visit_distinct_expr(expr, ctx), + Expr::Union(expr) => self.visit_union_expr(expr, ctx), + Expr::Difference(expr) => self.visit_difference_expr(expr, ctx), + Expr::Selection(expr) => self.visit_selection_expr(expr, ctx), + Expr::Projection(expr) => self.visit_projection_expr(expr, ctx), + Expr::CartesianProduct(expr) => self.visit_cartesian_product_expr(expr, ctx), + Expr::EquiJoin(expr) => self.visit_equi_join_expr(expr, ctx), + Expr::AntiJoin(expr) => self.visit_anti_join_expr(expr, ctx), + Expr::FixedPointIter(expr) => self.visit_fixed_point_iter_expr(expr, ctx), + } + } + fn visit_binary_expr(&mut self, expr: &BinaryExpr, ctx: C) -> T; + fn visit_unary_expr(&mut self, expr: &UnaryExpr, ctx: C) -> T; + fn visit_grouping_expr(&mut self, expr: &GroupingExpr, ctx: C) -> T; + fn visit_var_expr(&mut self, expr: &VarExpr, ctx: C) -> T; + fn visit_assign_expr(&mut self, expr: &AssignExpr, ctx: C) -> T; + fn visit_literal_expr(&mut self, expr: &LiteralExpr, ctx: C) -> T; + fn visit_function_expr(&mut self, expr: &FunctionExpr, ctx: C) -> T; + fn visit_call_expr(&mut self, expr: &CallExpr, ctx: C) -> T; + fn visit_alias_expr(&mut self, expr: &AliasExpr, ctx: C) -> T; + fn visit_distinct_expr(&mut self, expr: &DistinctExpr, ctx: C) -> T; + fn visit_union_expr(&mut self, expr: &UnionExpr, ctx: C) -> T; + fn visit_difference_expr(&mut self, expr: &DifferenceExpr, ctx: C) -> T; + fn visit_selection_expr(&mut self, expr: &SelectionExpr, ctx: C) -> T; + fn visit_projection_expr(&mut self, expr: &ProjectionExpr, ctx: C) -> T; + fn visit_cartesian_product_expr(&mut self, expr: &CartesianProductExpr, ctx: C) -> T; + fn visit_equi_join_expr(&mut self, expr: &EquiJoinExpr, ctx: C) -> T; + fn visit_anti_join_expr(&mut self, expr: &AntiJoinExpr, ctx: C) -> T; + fn visit_fixed_point_iter_expr(&mut self, expr: &FixedPointIterExpr, ctx: C) -> T; +} + +pub trait ExprVisitorMut { + fn visit_expr(&mut self, expr: &mut Expr, ctx: C) -> T { + match expr { + Expr::Binary(expr) => self.visit_binary_expr(expr, ctx), + Expr::Unary(expr) => self.visit_unary_expr(expr, ctx), + Expr::Grouping(expr) => self.visit_grouping_expr(expr, ctx), + Expr::Var(expr) => self.visit_var_expr(expr, ctx), + Expr::Assign(expr) => self.visit_assign_expr(expr, ctx), + Expr::Literal(expr) => self.visit_literal_expr(expr, ctx), + Expr::Function(expr) => self.visit_function_expr(expr, ctx), + Expr::Call(expr) => self.visit_call_expr(expr, ctx), + Expr::Alias(expr) => self.visit_alias_expr(expr, ctx), + Expr::Distinct(expr) => self.visit_distinct_expr(expr, ctx), + Expr::Union(expr) => self.visit_union_expr(expr, ctx), + Expr::Difference(expr) => self.visit_difference_expr(expr, ctx), + Expr::Selection(expr) => self.visit_selection_expr(expr, ctx), + Expr::Projection(expr) => self.visit_projection_expr(expr, ctx), + Expr::CartesianProduct(expr) => self.visit_cartesian_product_expr(expr, ctx), + Expr::EquiJoin(expr) => self.visit_equi_join_expr(expr, ctx), + Expr::AntiJoin(expr) => self.visit_anti_join_expr(expr, ctx), + Expr::FixedPointIter(expr) => self.visit_fixed_point_iter_expr(expr, ctx), + } + } + fn visit_binary_expr(&mut self, expr: &mut BinaryExpr, ctx: C) -> T; + fn visit_unary_expr(&mut self, expr: &mut UnaryExpr, ctx: C) -> T; + fn visit_grouping_expr(&mut self, expr: &mut GroupingExpr, ctx: C) -> T; + fn visit_var_expr(&mut self, expr: &mut VarExpr, ctx: C) -> T; + fn visit_assign_expr(&mut self, expr: &mut AssignExpr, ctx: C) -> T; + fn visit_literal_expr(&mut self, expr: &mut LiteralExpr, ctx: C) -> T; + fn visit_function_expr(&mut self, expr: &mut FunctionExpr, ctx: C) -> T; + fn visit_call_expr(&mut self, expr: &mut CallExpr, ctx: C) -> T; + fn visit_alias_expr(&mut self, expr: &mut AliasExpr, ctx: C) -> T; + fn visit_distinct_expr(&mut self, expr: &mut DistinctExpr, ctx: C) -> T; + fn visit_union_expr(&mut self, expr: &mut UnionExpr, ctx: C) -> T; + fn visit_difference_expr(&mut self, expr: &mut DifferenceExpr, ctx: C) -> T; + fn visit_selection_expr(&mut self, expr: &mut SelectionExpr, ctx: C) -> T; + fn visit_projection_expr(&mut self, expr: &mut ProjectionExpr, ctx: C) -> T; + fn visit_cartesian_product_expr(&mut self, expr: &mut CartesianProductExpr, ctx: C) -> T; + fn visit_equi_join_expr(&mut self, expr: &mut EquiJoinExpr, ctx: C) -> T; + fn visit_anti_join_expr(&mut self, expr: &mut AntiJoinExpr, ctx: C) -> T; + fn visit_fixed_point_iter_expr(&mut self, expr: &mut FixedPointIterExpr, ctx: C) -> T; +} + +pub trait ExprVisitorOwn { + fn visit_expr(&mut self, expr: Expr, ctx: C) -> T { + match expr { + Expr::Binary(expr) => self.visit_binary_expr(*expr, ctx), + Expr::Unary(expr) => self.visit_unary_expr(*expr, ctx), + Expr::Grouping(expr) => self.visit_grouping_expr(*expr, ctx), + Expr::Var(expr) => self.visit_var_expr(*expr, ctx), + Expr::Assign(expr) => self.visit_assign_expr(*expr, ctx), + Expr::Literal(expr) => self.visit_literal_expr(*expr, ctx), + Expr::Function(expr) => self.visit_function_expr(*expr, ctx), + Expr::Call(expr) => self.visit_call_expr(*expr, ctx), + Expr::Alias(expr) => self.visit_alias_expr(*expr, ctx), + Expr::Distinct(expr) => self.visit_distinct_expr(*expr, ctx), + Expr::Union(expr) => self.visit_union_expr(*expr, ctx), + Expr::Difference(expr) => self.visit_difference_expr(*expr, ctx), + Expr::Selection(expr) => self.visit_selection_expr(*expr, ctx), + Expr::Projection(expr) => self.visit_projection_expr(*expr, ctx), + Expr::CartesianProduct(expr) => self.visit_cartesian_product_expr(*expr, ctx), + Expr::EquiJoin(expr) => self.visit_equi_join_expr(*expr, ctx), + Expr::AntiJoin(expr) => self.visit_anti_join_expr(*expr, ctx), + Expr::FixedPointIter(expr) => self.visit_fixed_point_iter_expr(*expr, ctx), + } + } + fn visit_binary_expr(&mut self, expr: BinaryExpr, ctx: C) -> T; + fn visit_unary_expr(&mut self, expr: UnaryExpr, ctx: C) -> T; + fn visit_grouping_expr(&mut self, expr: GroupingExpr, ctx: C) -> T; + fn visit_var_expr(&mut self, expr: VarExpr, ctx: C) -> T; + fn visit_assign_expr(&mut self, expr: AssignExpr, ctx: C) -> T; + fn visit_literal_expr(&mut self, expr: LiteralExpr, ctx: C) -> T; + fn visit_function_expr(&mut self, expr: FunctionExpr, ctx: C) -> T; + fn visit_call_expr(&mut self, expr: CallExpr, ctx: C) -> T; + fn visit_alias_expr(&mut self, expr: AliasExpr, ctx: C) -> T; + fn visit_distinct_expr(&mut self, expr: DistinctExpr, ctx: C) -> T; + fn visit_union_expr(&mut self, expr: UnionExpr, ctx: C) -> T; + fn visit_difference_expr(&mut self, expr: DifferenceExpr, ctx: C) -> T; + fn visit_selection_expr(&mut self, expr: SelectionExpr, ctx: C) -> T; + fn visit_projection_expr(&mut self, expr: ProjectionExpr, ctx: C) -> T; + fn visit_cartesian_product_expr(&mut self, expr: CartesianProductExpr, ctx: C) -> T; + fn visit_equi_join_expr(&mut self, expr: EquiJoinExpr, ctx: C) -> T; + fn visit_anti_join_expr(&mut self, expr: AntiJoinExpr, ctx: C) -> T; + fn visit_fixed_point_iter_expr(&mut self, expr: FixedPointIterExpr, ctx: C) -> T; +} + +impl MemAddr for Expr {} +impl MemAddr for BinaryExpr {} +impl MemAddr for UnaryExpr {} +impl MemAddr for GroupingExpr {} +impl MemAddr for VarExpr {} +impl MemAddr for AssignExpr {} +impl MemAddr for LiteralExpr {} +impl MemAddr for FunctionExpr {} +impl MemAddr for CallExpr {} +impl MemAddr for AliasExpr {} +impl MemAddr for DistinctExpr {} +impl MemAddr for UnionExpr {} +impl MemAddr for DifferenceExpr {} +impl MemAddr for SelectionExpr {} +impl MemAddr for ProjectionExpr {} +impl MemAddr for CartesianProductExpr {} +impl MemAddr for EquiJoinExpr {} +impl MemAddr for AntiJoinExpr {} +impl MemAddr for FixedPointIterExpr {} diff --git a/packages/coln-query/src/function.rs b/packages/coln-query/src/function.rs new file mode 100644 index 00000000..3fffe462 --- /dev/null +++ b/packages/coln-query/src/function.rs @@ -0,0 +1,67 @@ +use crate::{expr::FunctionExpr, type_resolver::ExprType, variable::Environment}; +use std::{ + cell::RefCell, + fmt::{self, Debug, Display}, + rc::Rc, +}; + +/// Convenience type alias for a reference to a [`Function`]. +pub type FunctionRef = Rc>; + +/// A helper function to avoid writing the interior mutability boilerplate. +pub fn new_function( + name: Option, + declaration: FunctionExpr, + environment: Environment, +) -> FunctionRef { + Rc::new(RefCell::new(Function::new(name, declaration, environment))) +} + +#[derive(Clone)] +pub struct Function { + pub name: Option, + pub declaration: FunctionExpr, + pub environment: Environment, +} + +impl Function { + pub fn new(name: Option, declaration: FunctionExpr, environment: Environment) -> Self { + Self { + name, + declaration, + environment, + } + } + pub fn declaration(&self) -> &FunctionExpr { + &self.declaration + } + pub fn arity(&self) -> usize { + self.declaration().parameters.len() + } + fn to_string_helper(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let params = format!("({})", self.declaration().parameters.join(", ")); + if let Some(name) = &self.name { + write!(f, "") + } else { + write!(f, "") + } + } +} + +impl Display for Function { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.to_string_helper(f) + } +} + +impl Debug for Function { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.to_string_helper(f) + } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct FunctionType { + // TODO: parameters' type + pub return_type: Box, +} diff --git a/packages/coln-query/src/interpreter.rs b/packages/coln-query/src/interpreter.rs new file mode 100644 index 00000000..d87b696a --- /dev/null +++ b/packages/coln-query/src/interpreter.rs @@ -0,0 +1,648 @@ +use crate::{ + context::InterpreterContext, + dbsp::{OrdIndexedNestedStream, RootCircuit}, + error::RuntimeError, + expr::{ + AliasExpr, AntiJoinExpr, AssignExpr, BinaryExpr, CallExpr, CartesianProductExpr, + DifferenceExpr, DistinctExpr, EquiJoinExpr, Expr, ExprVisitor, FixedPointIterExpr, + FunctionExpr, GroupingExpr, LiteralExpr, ProjectionExpr, SelectionExpr, UnaryExpr, + UnionExpr, VarExpr, + }, + function::new_function, + operator::Operator, + operators::{ + coalesce::coalesce_helper, + projection::{ProjectionStrategy, projection_helper}, + reindex::reindex_helper, + }, + relation::{Relation, RelationRef, SchemaTuple, TupleKey, TupleValue, new_relation}, + stmt::{BlockStmt, ExprStmt, Stmt, StmtVisitor, VarStmt}, + variable::{Environment, Value}, +}; +use std::{cell::Ref, rc::Rc}; + +pub struct Interpreter { + root_circuit: Option, +} + +impl Interpreter { + pub fn new(root_circuit: Option) -> Self { + Self { root_circuit } + } + pub fn interpret<'a>( + &mut self, + stmts: impl IntoIterator, + ctx: &mut InterpreterContext, + ) -> Result, RuntimeError> { + // Ensure we have a global scope before interpreting. + debug_assert!(ctx.environment.just_global()); + // We do not call `visit_block` here because the root scope is created + // in the `Environment` constructor and should remain intact across + // multiple calls to `interpret`. + let ret = self.visit_stmts(stmts, ctx); + // Ensure we have a global scope after interpreting. + debug_assert!(ctx.environment.just_global()); + ret + } + pub fn evaluate(&mut self, expr: &Expr, ctx: VisitorCtx) -> Result { + self.visit_expr(expr, ctx) + } + fn visit_stmts<'a>( + &mut self, + stmts: impl IntoIterator, + ctx: VisitorCtx, + ) -> Result, RuntimeError> { + // Functional programming can be so beautiful. Return the last value + // if any. + stmts + .into_iter() + .try_fold(None, |_prev, stmt| self.visit_stmt(stmt, ctx)) + } + fn visit_block<'a, F: FnOnce(&mut Environment)>( + &mut self, + stmts: impl IntoIterator, + ctx: VisitorCtx, + environment: F, + ) -> Result, RuntimeError> { + ctx.environment.begin_scope(); + environment(ctx.environment); + let ret = self.visit_stmts(stmts, ctx); + ctx.environment.end_scope(); + ret + } +} + +macro_rules! comparison_helper { + ($left:expr, $right:expr, $op:tt, $($variant:path),*) => {{ + match (&$left, &$right) { + $( + ($variant(left), $variant(right)) => Ok(Value::Bool(left $op right)), + )* + _ => Err(RuntimeError::new( + format!("expected comparable type, got: {:?} and {:?}", $left, $right), + )), + } + }} +} + +macro_rules! arithmetic_helper { + ($left:expr, $right:expr, $op:tt, $($variant:path),*) => {{ + match (&$left, &$right) { + $( + ($variant(left), $variant(right)) => Ok($variant(left $op right)), + )* + _ => Err(RuntimeError::new( + format!("expected number type, got: {:?} and {:?}", $left, $right), + )), + } + }} +} + +macro_rules! assert_type { + ($value:expr, $variant:path) => { + match $value { + $variant(inner) => Ok(inner), + _ => Err(RuntimeError::new(format!( + "expected {} type, got: {:?}", + stringify!($variant:path), $value + ))), + } + }; +} + +impl Interpreter { + fn visit_lazy_binary_expr(&mut self, expr: &BinaryExpr, ctx: VisitorCtx) -> ExprVisitorResult { + let left = is_truthy(&self.visit_expr(&expr.left, ctx)?); + + if let Operator::And | Operator::Or = expr.operator { + if expr.operator == Operator::And && left || expr.operator == Operator::Or && !left { + let right = is_truthy(&self.visit_expr(&expr.right, ctx)?); + Ok(Value::Bool(right)) + } else { + Ok(Value::Bool(left)) + } + } else { + Err(RuntimeError::new(format!( + "unsupported (lazy) binary operator: {:?}", + expr.operator + ))) + } + } + fn visit_eager_binary_expr(&mut self, expr: &BinaryExpr, ctx: VisitorCtx) -> ExprVisitorResult { + let left = self.visit_expr(&expr.left, ctx)?; + let right = self.visit_expr(&expr.right, ctx)?; + + match expr.operator { + Operator::Equal => { + comparison_helper!(left, right, ==, Value::Iint, Value::Uint, Value::Bool, Value::String, Value::Null) + } + Operator::NotEqual => { + comparison_helper!(left, right, !=, Value::Iint, Value::Uint, Value::Bool, Value::String, Value::Null) + } + Operator::Less => { + comparison_helper!(left, right, <, Value::Iint, Value::Uint, Value::Bool, Value::String) + } + Operator::LessEqual => { + comparison_helper!(left, right, <=, Value::Iint, Value::Uint, Value::Bool, Value::String) + } + Operator::Greater => { + comparison_helper!(left, right, >, Value::Iint, Value::Uint, Value::Bool, Value::String) + } + Operator::GreaterEqual => { + comparison_helper!(left, right, >=, Value::Iint, Value::Uint, Value::Bool, Value::String) + } + Operator::Addition => { + if let (Value::String(left), Value::String(right)) = (&left, &right) { + return Ok(Value::String(format!("{left}{right}"))); + } + arithmetic_helper!(left, right, +, Value::Iint, Value::Uint) + } + Operator::Subtraction => { + arithmetic_helper!(left, right, -, Value::Iint, Value::Uint) + } + Operator::Multiplication => { + arithmetic_helper!(left, right, *, Value::Iint, Value::Uint) + } + Operator::Division => { + arithmetic_helper!(left, right, /, Value::Iint, Value::Uint) + } + _ => Err(RuntimeError::new(format!( + "unsupported (eager) binary operator: {:?}", + expr.operator + ))), + } + } +} + +type VisitorCtx<'a, 'b> = &'a mut InterpreterContext<'b>; + +type ExprVisitorResult = Result; + +impl ExprVisitor> for Interpreter { + fn visit_binary_expr(&mut self, expr: &BinaryExpr, ctx: VisitorCtx) -> ExprVisitorResult { + if let Operator::And | Operator::Or = expr.operator { + self.visit_lazy_binary_expr(expr, ctx) + } else { + self.visit_eager_binary_expr(expr, ctx) + } + } + + fn visit_unary_expr(&mut self, expr: &UnaryExpr, ctx: VisitorCtx) -> ExprVisitorResult { + let operand = self.visit_expr(&expr.operand, ctx)?; + + match expr.operator { + Operator::Subtraction => match operand { + Value::Iint(value) => Ok(Value::Iint(-value)), + _ => Err(RuntimeError::new(format!( + "expected signed int, got: {operand:?}", + ))), + }, + Operator::Not => Ok(Value::Bool(!is_truthy(&operand))), + _ => Err(RuntimeError::new(format!( + "unsupported unary operator: {:?}", + expr.operator + ))), + } + } + + fn visit_grouping_expr(&mut self, expr: &GroupingExpr, ctx: VisitorCtx) -> ExprVisitorResult { + self.visit_expr(&expr.expr, ctx) + } + + fn visit_var_expr(&mut self, expr: &VarExpr, ctx: VisitorCtx) -> ExprVisitorResult { + let name = &expr.name; + ctx.tuple_vars.get(name).map_or_else( + || { + let resolved = expr + .resolved + .as_ref() + // This should never happen because the resolver should have resolved + // all non-tuple variables before the interpreter starts. + .unwrap_or_else(|| panic!("Unresolved variable '{name}'.")); + Ok(ctx.environment.lookup_var(resolved).clone()) + }, + |value| Ok(Value::from(value.clone())), + ) + } + + fn visit_assign_expr(&mut self, expr: &AssignExpr, ctx: VisitorCtx) -> ExprVisitorResult { + let name = &expr.name; + self.visit_expr(&expr.value, ctx).inspect(|value| { + let resolved = expr + .resolved + .as_ref() + // This should never happen because the resolver should have resolved + // all non-tuple variables before the interpreter starts and assigning + // to tuple variables is not allowed. + .unwrap_or_else(|| panic!("Unresolved variable '{name}'.")); + ctx.environment.assign_var(resolved, value.clone()); + }) + } + + fn visit_literal_expr(&mut self, expr: &LiteralExpr, ctx: VisitorCtx) -> ExprVisitorResult { + // Maybe make values reference counted instead of cloning here? + Ok(Value::from(expr.value.clone())) + } + + fn visit_function_expr(&mut self, expr: &FunctionExpr, ctx: VisitorCtx) -> ExprVisitorResult { + Ok(Value::Function(new_function( + // For now, we assume that the function is anonymous, that is, nameless. + None, + // We clone here to let functions own their code and thus, + // we do not have to worry about the lifetime of their code. + expr.clone(), + ctx.environment.clone(), // Clone is cheap and necessary here. + ))) + } + + fn visit_call_expr(&mut self, expr: &CallExpr, ctx: VisitorCtx) -> ExprVisitorResult { + let callee = self + .visit_expr(&expr.callee, ctx) + .and_then(|value| assert_type!(value, Value::Function))?; + let mut callee = callee.borrow_mut(); + + // TODO: Optimize by checking arity in resolver just _once_ statically. + if expr.arguments.len() != callee.arity() { + return Err(RuntimeError::new(format!( + "expected exactly {} arguments, but got {}", + callee.arity(), + expr.arguments.len() + ))); + } + + let args = expr + .arguments + .iter() + .map(|arg| self.visit_expr(arg, ctx)) + .collect::, _>>()?; + + let body: &Vec = + // This is a sin, I know, please forgive me. I bet there is a nicer solution. + // Yet, this is safe because the the `body` and `environment` are disjoint + // borrows from the `callee` struct. + unsafe { &*std::ptr::from_ref(&callee.declaration().body.stmts) as &Vec }; + let mut fn_ctx = InterpreterContext::new(&mut callee.environment); + + self.visit_block(body, &mut fn_ctx, move |environment| { + for arg in args.into_iter() { + environment.define_var(arg); + } + }) + // We return the default value of `null` if the function does not return + // anything. + .map(|value| value.unwrap_or_default()) + } + + fn visit_alias_expr(&mut self, expr: &AliasExpr, ctx: VisitorCtx) -> ExprVisitorResult { + ctx.set_alias(expr.alias.clone()); + self.visit_expr(&expr.relation, ctx) + } + + fn visit_distinct_expr(&mut self, expr: &DistinctExpr, ctx: VisitorCtx) -> ExprVisitorResult { + let relation = self + .visit_expr(&expr.relation, ctx) + .and_then(|value| assert_type!(value, Value::Relation)) + .map(coalesce_helper)?; + let relation_ref = relation.borrow(); + + let distincted = relation_ref.inner.distinct(); + + Ok(Value::Relation(new_relation( + relation_ref.schema.clone(), + distincted, + ))) + } + + fn visit_union_expr(&mut self, expr: &UnionExpr, ctx: VisitorCtx) -> ExprVisitorResult { + let relations: Vec = expr + .relations + .iter() + .map(|relation| { + self.visit_expr(relation, ctx) + .and_then(|value| assert_type!(value, Value::Relation)) + .map(coalesce_helper) + }) + .collect::, _>>()?; + + let relations: Vec> = + relations.iter().map(|relation| relation.borrow()).collect(); + + let (first, others) = relations + .split_first() + .expect("Resolver has *not* done its job and ensured that there are at least two operands to a union!"); + + let unioned = first + .inner + .sum(others.iter().map(|relation| &relation.inner)); + + Ok(Value::Relation(new_relation(first.schema.clone(), unioned))) + } + + fn visit_difference_expr( + &mut self, + expr: &DifferenceExpr, + ctx: VisitorCtx, + ) -> ExprVisitorResult { + let left = self + .visit_expr(&expr.left, ctx) + .and_then(|value| assert_type!(value, Value::Relation)) + .map(coalesce_helper)?; + let right = self + .visit_expr(&expr.right, ctx) + .and_then(|value| assert_type!(value, Value::Relation)) + .map(coalesce_helper)?; + + let left_ref = left.borrow(); + + let differenced = left_ref.inner.minus(&right.borrow().inner); + + Ok(Value::Relation(new_relation( + left_ref.schema.clone(), + differenced, + ))) + } + + fn visit_selection_expr(&mut self, expr: &SelectionExpr, ctx: VisitorCtx) -> ExprVisitorResult { + let relation = self + .visit_expr(&expr.relation, ctx) + .and_then(|value| assert_type!(value, Value::Relation))?; + let relation_ref = relation.borrow(); + let relation_clone = Rc::clone(&relation); + + let condition = expr.condition.clone(); + let environment = ctx.environment.clone(); + let selected = relation_ref.inner.filter(move |(_key, tuple)| { + // No need to run resolver here, already resolved! + let schema = &relation_clone.borrow().schema; + let environment = &mut environment.clone(); + let mut new_ctx = InterpreterContext::new(environment); + new_ctx.extend_tuple_ctx(&None, &schema.tuple, tuple); + let value = Interpreter::new(None) + .evaluate(&condition, &mut new_ctx) + .expect("Runtime error while interpreting selection condition"); + is_truthy(&value) + }); + + Ok(Value::Relation(new_relation( + relation_ref.schema.select(), + selected, + ))) + } + + fn visit_projection_expr( + &mut self, + expr: &ProjectionExpr, + ctx: VisitorCtx, + ) -> ExprVisitorResult { + let relation = self + .visit_expr(&expr.relation, ctx) + .and_then(|value| assert_type!(value, Value::Relation))?; + let relation_ref = relation.borrow(); + + let (schema, projected) = match projection_helper(&expr.attributes) { + ProjectionStrategy::Projection(projection) => { + let (schema, projection) = projection.prepare(&relation_ref.schema); + let projected = relation_ref.inner.map_index({ + let relation_clone = Rc::clone(&relation); + let environment = ctx.environment.clone(); + move |(key, tuple)| { + let schema = &relation_clone.borrow().schema; + let environment = &mut environment.clone(); + let mut new_ctx = InterpreterContext::new(environment); + new_ctx.extend_tuple_ctx(&None, &schema.tuple, tuple); + projection(new_ctx) + } + }); + (schema, projected) + } + ProjectionStrategy::Pick(pick) => { + let schema = pick.prepare(&relation_ref.schema); + let picked = relation_ref.inner.clone(); + (schema, picked) + } + }; + + Ok(Value::Relation(new_relation(schema, projected))) + } + + fn visit_cartesian_product_expr( + &mut self, + expr: &CartesianProductExpr, + ctx: VisitorCtx, + ) -> ExprVisitorResult { + self.visit_equi_join_expr(&expr.inner, ctx) + } + + fn visit_equi_join_expr(&mut self, expr: &EquiJoinExpr, ctx: VisitorCtx) -> ExprVisitorResult { + let left = self + .visit_expr(&expr.left, ctx) + .and_then(|value| assert_type!(value, Value::Relation))?; + // Note the order here. Before we evaluate the right expression, + // we have to consume the alias of the left relation because it is + // replaced by the right relation's alias otherwise. + let left_alias = ctx.consume_alias(); + + let right = self + .visit_expr(&expr.right, ctx) + .and_then(|value| assert_type!(value, Value::Relation))?; + let right_alias = ctx.consume_alias(); + + let (left_key_fields, right_key_fields): (Vec<&Expr>, Vec<&Expr>) = + expr.on.iter().map(|(left, right)| (left, right)).unzip(); + + let (left_indexed, key_fields) = + reindex_helper(&left, left_key_fields.as_slice(), ctx.environment); + let (right_indexed, _) = + reindex_helper(&right, right_key_fields.as_slice(), ctx.environment); + + let joined_schema = left + .borrow() + .schema + .join(&right.borrow().schema, key_fields); + + let (schema, projection) = match expr + .attributes + .as_ref() + .map(|attributes| projection_helper(attributes)) + { + Some(ProjectionStrategy::Projection(projection)) => { + let (projected_schema, projection) = projection.prepare(&joined_schema); + (projected_schema, Some(projection)) + } + Some(ProjectionStrategy::Pick(pick)) => { + let picked_schema = pick.prepare(&joined_schema); + (picked_schema, None) + } + None => (joined_schema, None), + }; + + let joined = left_indexed.join_index(&right_indexed, { + let left_rel = Rc::clone(&left); + let right_rel = Rc::clone(&right); + let environment = ctx.environment.clone(); + move |key: &TupleKey, left: &TupleValue, right: &TupleValue| { + let left_schema = &left_rel.borrow().schema; + let right_schema = &right_rel.borrow().schema; + let joined_tuple: TupleValue = SchemaTuple::new(&left_schema.tuple, left) + .join(&SchemaTuple::new(&right_schema.tuple, right)) + .collect(); + let key_tuple_pair = if let Some(projection) = &projection { + let environment = &mut environment.clone(); + let mut new_ctx = InterpreterContext::new(environment); + new_ctx.extend_tuple_ctx(&left_alias, &left_schema.tuple, left); + new_ctx.extend_tuple_ctx(&right_alias, &right_schema.tuple, right); + projection(new_ctx) + } else { + (key.clone(), joined_tuple) + }; + Some(key_tuple_pair) + } + }); + + Ok(Value::Relation(new_relation(schema, joined))) + } + + fn visit_anti_join_expr(&mut self, expr: &AntiJoinExpr, ctx: VisitorCtx) -> ExprVisitorResult { + let left = self + .visit_expr(&expr.left, ctx) + .and_then(|value| assert_type!(value, Value::Relation))?; + + let right = self + .visit_expr(&expr.right, ctx) + .and_then(|value| assert_type!(value, Value::Relation))?; + + let (left_key_fields, right_key_fields): (Vec<&Expr>, Vec<&Expr>) = + expr.on.iter().map(|(left, right)| (left, right)).unzip(); + + let (left_indexed, key_fields) = + reindex_helper(&left, left_key_fields.as_slice(), ctx.environment); + let (right_indexed, _) = + reindex_helper(&right, right_key_fields.as_slice(), ctx.environment); + + let anti_joined_schema = left + .borrow() + .schema + .anti_join(&right.borrow().schema, key_fields); + let anti_joined = left_indexed.anti_join_index(&right_indexed); + + Ok(Value::Relation(new_relation( + anti_joined_schema, + anti_joined, + ))) + } + + fn visit_fixed_point_iter_expr( + &mut self, + expr: &FixedPointIterExpr, + ctx: VisitorCtx, + ) -> ExprVisitorResult { + let accumulator = self + .visit_expr(&expr.accumulator.1, ctx) + .and_then(|value| assert_type!(value, Value::Relation)) + .map(coalesce_helper)?; + + let imports: Vec = expr + .imports + .iter() + .map(|import| { + self.visit_expr(&import.1, ctx) + .and_then(|value| assert_type!(value, Value::Relation)) + }) + .collect::, _>>()?; + + let (accumulator_init, schema) = { + let accumulator = accumulator.borrow(); + ( + accumulator.inner.expect_root().clone(), + accumulator.schema.clone(), + ) + }; + + let root_circuit = self + .root_circuit + .clone() // Can we just clone a root_circuit? + .expect("Recursion from non-recursive context"); + let accumulated = root_circuit + .recursive(|nested_circuit, acc: OrdIndexedNestedStream| { + let result = self + .visit_block(&expr.step.stmts, ctx, move |environment| { + let accumulator = accumulator.borrow(); + // delta0 does not alter the schema. + let schema = accumulator.schema.clone(); + let accumulator = accumulator + .inner + .delta0(nested_circuit) + .expect_nested() + .plus(&acc); + environment.define_var(new_relation(schema, accumulator)); + + for import in imports.iter() { + let import = import.borrow(); + // delta0 does not alter the schema. + let schema = import.schema.clone(); + let import = import.inner.delta0(nested_circuit); + environment.define_var(new_relation(schema, import)); + } + }) + .expect("Runtime error while interpreting fixed point iteration body") + .expect("Fixed point iteration body did not return a value"); + let result = assert_type!(result, Value::Relation) + .expect("Fixed point iteration body did not return a relation"); + // We have to coalesce the result here because the output is fed + // into a union below, which requires the schema to be coalesced. + let result = coalesce_helper(result); + Ok(result.borrow().inner.expect_nested().clone()) + }) + .expect("Recursive error"); + + let fixed_point = accumulator_init.plus(&accumulated); + + Ok(Value::Relation(new_relation(schema, fixed_point))) + } +} + +type StmtVisitorResult = Result, RuntimeError>; + +impl StmtVisitor> for Interpreter { + fn visit_var_stmt(&mut self, stmt: &VarStmt, ctx: VisitorCtx) -> StmtVisitorResult { + stmt.initializer + .as_ref() + .map_or_else( + // We default to null if no initializer is provided. + || Ok(Value::default()), + |expr| { + self.visit_expr(expr, ctx).inspect(|val| { + if let Value::Function(function) = val { + // Here, a function turns from anonymous to named. + // If the function is later aliased, that is, reassigned to another + // variable, we stick to this original name (NodeJS does it, too). + function.borrow_mut().name = Some(stmt.name.clone()); + } + }) + }, + ) + .map(|value| { + ctx.environment.define_var(value.clone()); + Some(value) + }) + } + + fn visit_expr_stmt(&mut self, stmt: &ExprStmt, ctx: VisitorCtx) -> StmtVisitorResult { + // Evaluate the expression and return the result. + // This is the only statement that can return a value. + self.visit_expr(&stmt.expr, ctx).map(Some) + } + + fn visit_block_stmt(&mut self, stmt: &BlockStmt, ctx: VisitorCtx) -> StmtVisitorResult { + self.visit_block(&stmt.stmts, ctx, |_env| ()) + } +} + +/// Only `null` and `false` are falsy, everything else is truthy. +fn is_truthy(value: &Value) -> bool { + match value { + Value::Null(()) => false, + Value::Bool(value) => *value, + _ => true, + } +} diff --git a/packages/coln-query/src/lib.rs b/packages/coln-query/src/lib.rs new file mode 100644 index 00000000..10d4c416 --- /dev/null +++ b/packages/coln-query/src/lib.rs @@ -0,0 +1,1217 @@ +#![allow(dead_code, unused_variables)] + +pub(crate) mod api; +mod context; +pub mod dbsp; +pub mod error; +pub mod expr; +mod function; +mod interpreter; +pub mod operator; +mod operators; +pub mod optimizer; +pub mod relation; +mod resolver; +pub mod scalar; +pub mod stmt; +pub mod test_helper; +pub mod type_resolver; +mod util; +pub mod variable; + +use crate::{ + dbsp::{DbspHandle, DbspInputs, DbspOutput, RootCircuit, Runtime}, + error::{IncLogError, RuntimeError, SyntaxError}, + optimizer::{NoOptimizer, Optimizer}, + stmt::Code, +}; +use context::{InterpreterContext, ProgramContext, ResolverContext}; +use interpreter::Interpreter; +use resolver::Resolver; +use std::num::NonZeroUsize; +use stmt::Stmt; +use variable::Value; + +// Var: Variable +// Val: Value +// Stmt: Statement +// Expr: Expression +// Ident: Identifier + +pub struct IncLog { + root_circuit: Option, + program_context: ProgramContext, + had_syntax_err: bool, + had_runtime_err: bool, +} + +impl IncLog { + pub fn without_recursion() -> Self { + Self { + root_circuit: None, + program_context: ProgramContext::new(), + had_syntax_err: false, + had_runtime_err: false, + } + } + pub fn with_recursion(root_circuit: RootCircuit) -> Self { + Self { + root_circuit: Some(root_circuit), + program_context: ProgramContext::new(), + had_syntax_err: false, + had_runtime_err: false, + } + } + pub fn execute( + &mut self, + code: impl IntoIterator, + ) -> Result, IncLogError> { + self.program_context + .program + .extend_program(code.into_iter()); + let mut resolver_ctx = ResolverContext::new(&mut self.program_context.scopes); + Resolver::new() + .resolve( + self.program_context.program.unexecuted_code_mut(), + &mut resolver_ctx, + ) + .map_err(|err| self.ack_syntax_err(err)) + .and_then(|()| { + let mut interpreter_ctx = + InterpreterContext::new(&mut self.program_context.environment); + let mut interpreter = Interpreter::new(self.root_circuit.clone()); + interpreter + .interpret( + self.program_context.program.unexecuted_code(), + &mut interpreter_ctx, + ) + .map_err(|err| self.ack_runtime_err(err)) + }) + } + fn ack_syntax_err>(&mut self, err: T) -> IncLogError { + self.had_syntax_err = true; + err.into() + } + fn ack_runtime_err>(&mut self, err: T) -> IncLogError { + self.had_runtime_err = true; + err.into() + } +} + +#[derive(Clone)] +pub struct IncDataLog { + threads: NonZeroUsize, + optimizer: Optimizer, +} + +impl Default for IncDataLog { + fn default() -> Self { + Self { + threads: std::thread::available_parallelism().unwrap_or(NonZeroUsize::new(8).unwrap()), + optimizer: NoOptimizer::default(), + } + } +} + +impl IncDataLog { + pub fn new(threads: NonZeroUsize, optimizer: O) -> Self { + Self { threads, optimizer } + } + pub fn build_circuit_from_ir( + &self, + intermediate_representation: F, + ) -> Result<(DbspHandle, DbspInputs, DbspOutput), IncLogError> + where + Code: IntoIterator, + F: Fn(&mut RootCircuit, &mut DbspInputs) -> Result + + Clone + + Send + + 'static, + { + let optimizer = self.optimizer.clone(); + let (circuit, (inputs, output)) = + Runtime::init_circuit(self.threads, move |root_circuit| { + let mut inputs = DbspInputs::default(); + let naive_program = intermediate_representation(root_circuit, &mut inputs)? + .into_iter() + .collect(); + let optimized_program = optimizer.optimize(naive_program)?; + Ok(Self::build_circuit( + root_circuit, + inputs, + optimized_program, + )?) + })?; + + Ok((circuit, inputs, output)) + } + pub fn build_circuit_from_parser( + &self, + parser: F, + ) -> Result<(DbspHandle, DbspInputs, DbspOutput), IncLogError> + where + F: Fn(&mut RootCircuit) -> Result<(DbspInputs, Code), SyntaxError> + Clone + Send + 'static, + { + let optimizer = self.optimizer.clone(); + let (circuit, (inputs, output)) = + Runtime::init_circuit(self.threads, move |root_circuit| { + let (inputs, naive_program) = parser(root_circuit)?; + let optimized_program = optimizer.optimize(naive_program)?; + Ok(Self::build_circuit( + root_circuit, + inputs, + optimized_program, + )?) + })?; + + Ok((circuit, inputs, output)) + } + fn build_circuit( + root_circuit: &mut RootCircuit, + inputs: DbspInputs, + program: Code, + ) -> Result<(DbspInputs, DbspOutput), RuntimeError> { + let output = IncLog::with_recursion(root_circuit.clone()).execute(program); + + let output = match output { + Ok(Some(Value::Relation(relation))) => { + let relation = relation.borrow(); + let output_handle = relation.inner.output(); + let output_schema = relation.schema.clone(); + DbspOutput::new(output_schema, output_handle) + } + result => { + return Err(RuntimeError::new(format!( + "Expected a relation as program's output, got {result:?}", + ))); + } + }; + + Ok((inputs, output)) + } +} + +#[cfg(test)] +mod test { + use super::*; + use crate::{ + dbsp::{DbspInput, zset}, + expr::{ + AliasExpr, CartesianProductExpr, DifferenceExpr, DistinctExpr, EquiJoinExpr, + FixedPointIterExpr, ProjectionExpr, SelectionExpr, UnionExpr, + }, + relation::TupleValue, + scalar::ScalarTypedValue, + stmt::BlockStmt, + test_helper::{person_profession_data, setup_inc_data_log}, + }; + use expr::{AssignExpr, BinaryExpr, CallExpr, Expr, Literal, LiteralExpr, VarExpr}; + use operator::Operator; + use stmt::{ExprStmt, Stmt, VarStmt}; + use test_helper::{Edge, InputEntity, Person, PredRel, Profession, SetOp}; + + #[test] + fn test_variable_init_assign() -> Result<(), IncLogError> { + let mut inclog = IncLog::without_recursion(); + + let initialization = vec![Stmt::from(VarStmt { + name: "a".to_string(), + initializer: Some(Expr::from(LiteralExpr { + value: Literal::Uint(1), + })), + })]; + + let assignment = vec![Stmt::from(ExprStmt { + expr: Expr::from(AssignExpr::new( + "a", + Expr::from(LiteralExpr { + value: Literal::Uint(2), + }), + )), + })]; + + assert_eq!(inclog.execute(initialization)?.unwrap(), Value::Uint(1)); + + assert_eq!(inclog.execute(assignment)?.unwrap(), Value::Uint(2)); + + Ok(()) + } + + // A function with two parameters which adds two values. + fn new_add_function_expr() -> Expr { + Expr::from(expr::FunctionExpr { + parameters: vec!["a".to_string(), "b".to_string()], + body: BlockStmt { + stmts: vec![Stmt::from(ExprStmt { + expr: Expr::from(BinaryExpr { + operator: Operator::Addition, + left: Expr::from(VarExpr::new("a")), + right: Expr::from(VarExpr::new("b")), + }), + })], + }, + }) + } + + #[test] + fn test_function_declarations() -> Result<(), IncLogError> { + let mut inclog = IncLog::without_recursion(); + + let anonymous_function = vec![Stmt::from(ExprStmt { + expr: new_add_function_expr(), + })]; + + let named_function = vec![Stmt::from(VarStmt { + name: "add".to_string(), + initializer: Some(new_add_function_expr()), + })]; + + let result = inclog.execute(anonymous_function)?.unwrap(); + assert_eq!(format!("{result}"), ""); + + let result = inclog.execute(named_function)?.unwrap(); + assert_eq!(format!("{result}"), ""); + + Ok(()) + } + + #[test] + fn test_function_call() -> Result<(), IncLogError> { + let mut inclog = IncLog::without_recursion(); + + let function_call = vec![ + Stmt::from(VarStmt { + name: "add".to_string(), + initializer: Some(new_add_function_expr()), + }), + Stmt::from(ExprStmt { + expr: Expr::from(CallExpr { + callee: Expr::from(VarExpr::new("add")), + arguments: vec![ + Expr::from(LiteralExpr { + value: Literal::Uint(1), + }), + Expr::from(LiteralExpr { + value: Literal::Uint(2), + }), + ], + }), + }), + ]; + + let result = inclog.execute(function_call)?.unwrap(); + assert_eq!(Value::Uint(3), result); + + Ok(()) + } + + #[test] + fn test_selection_and_projection() -> Result<(), anyhow::Error> { + let (mut circuit, inputs, output) = + setup_inc_data_log().build_circuit_from_ir(|root_circuit, dbsp_inputs| { + Ok([ + Stmt::from(VarStmt { + name: "add".to_string(), + initializer: Some(new_add_function_expr()), + }), + Stmt::from(VarStmt { + name: "constant".to_string(), + initializer: Some(Expr::from(LiteralExpr { + value: Literal::Uint(1), + })), + }), + Stmt::from(VarStmt { + name: "selected".to_string(), + initializer: Some(Expr::from(SelectionExpr { + condition: Expr::from(BinaryExpr { + // Just to demonstrate logical operators. + // A `weight >= 2` is the outcome. + operator: Operator::Or, + left: Expr::from(BinaryExpr { + operator: Operator::Greater, + left: Expr::from(VarExpr::new("weight")), + // Just to demonstrate that we can call a function defined + // at the buildtime context from the runtime context. + right: Expr::from(CallExpr { + callee: Expr::from(VarExpr::new("add")), + arguments: vec![ + Expr::from(VarExpr::new("constant")), + Expr::from(LiteralExpr { + value: Literal::Uint(1), + }), + ], + }), + }), + right: Expr::from(BinaryExpr { + operator: Operator::Equal, + left: Expr::from(VarExpr::new("weight")), + right: Expr::from(LiteralExpr::from(2_u64)), + }), + }), + relation: Expr::from(DbspInput::add( + Edge::schema(), + root_circuit, + dbsp_inputs, + )), + })), + }), + Stmt::from(VarStmt { + name: "projected".to_string(), + initializer: Some(Expr::from(ProjectionExpr { + relation: Expr::from(VarExpr::new("selected")), + attributes: ["from", "to", "weight"] + .into_iter() + .map(|name| (name.to_string(), Expr::from(VarExpr::new(name)))) + .chain([( + // Here we create an entirely new column. + "product_from_to".to_string(), + Expr::from(BinaryExpr { + operator: Operator::Multiplication, + left: Expr::from(VarExpr::new("from")), + right: Expr::from(VarExpr::new("to")), + }), + )]) + .collect(), + })), + }), + ]) + })?; + + circuit.start_transaction()?; + + let edges_input = inputs.get("edges").unwrap(); + + let data1 = [Edge::new(0, 1, 1), Edge::new(1, 2, 2), Edge::new(2, 3, 3)]; + let data2 = [Edge::new(3, 4, 1), Edge::new(4, 5, 2), Edge::new(5, 6, 3)]; + + println!("Insert of data1:"); + + edges_input.insert_with_same_weight(data1.iter(), 2); + + circuit.transaction()?; + + let batch = output.to_batch(); + println!("{}", batch.as_table()); + assert_eq!( + batch.as_debug_zset(), + zset! { + tuple!(1_u64, 2_u64, 2_u64, 2_u64) => 2, + tuple!(2_u64, 3_u64, 3_u64, 6_u64) => 2, + } + ); + + println!("Insert of data2:"); + + edges_input.insert_with_same_weight(data2.iter(), 1); + + circuit.transaction()?; + + let batch = output.to_batch(); + println!("{}", batch.as_table()); + assert_eq!( + batch.as_debug_zset(), + zset! { + tuple!(4_u64, 5_u64, 2_u64, 20_u64) => 1, + tuple!(5_u64, 6_u64, 3_u64, 30_u64) => 1, + } + ); + + println!("Removal of data1:"); + + edges_input.insert_with_same_weight(data1.iter(), -1); + + circuit.transaction()?; + + let batch = output.to_batch(); + println!("{}", batch.as_table()); + assert_eq!( + batch.as_debug_zset(), + zset! { + tuple!(1_u64, 2_u64, 2_u64, 2_u64) => -1, + tuple!(2_u64, 3_u64, 3_u64, 6_u64) => -1, + } + ); + + Ok(()) + } + + #[test] + fn test_standard_join() -> Result<(), anyhow::Error> { + let (mut circuit, inputs, output) = + setup_inc_data_log().build_circuit_from_ir(|root_circuit, dbsp_inputs| { + let code = [ + Stmt::from(VarStmt { + name: "person".to_string(), + initializer: Some(Expr::from(DbspInput::add( + Person::schema(), + root_circuit, + dbsp_inputs, + ))), + }), + Stmt::from(VarStmt { + name: "profession".to_string(), + initializer: Some(Expr::from(DbspInput::add( + Profession::schema(), + root_circuit, + dbsp_inputs, + ))), + }), + Stmt::from(VarStmt { + name: "joined".to_string(), + initializer: Some(Expr::from(EquiJoinExpr { + left: Expr::from(AliasExpr { + relation: Expr::from(VarExpr::new("person")), + alias: "pers".to_string(), + }), + right: Expr::from(AliasExpr { + relation: Expr::from(VarExpr::new("profession")), + alias: "prof".to_string(), + }), + // TODO: Shall we force aliasing here? Technically, + // it isn't required because the left attribute only + // operates on the left relation and the right + // attribute only operates on the right relation. + on: vec![( + Expr::from(VarExpr::new("profession_id")), + Expr::from(VarExpr::new("profession_id")), + )], + // attributes: None, + attributes: Some( + // Here, we filter out the duplicated profession_id + // column that occurs after the join. + [ + ("person_id", "pers.person_id"), + ("person_name", "pers.name"), + ("age", "pers.age"), + ("profession_id", "prof.profession_id"), + ("profession_name", "prof.name"), + ] + .into_iter() + .map(|(name, identifier)| { + (name.to_string(), Expr::from(VarExpr::new(identifier))) + }) + .collect(), + ), + })), + }), + ]; + Ok(code) + })?; + + let person_input = inputs.get("person").unwrap(); + let profession_input = inputs.get("profession").unwrap(); + + for (person_step, profession_step) in person_profession_data() { + person_input.insert_with_same_weight(&person_step, 1); + profession_input.insert_with_same_weight(&profession_step, 1); + + circuit.transaction()?; + + let batch = output.to_batch(); + println!("{}", batch.as_table()); + assert_eq!( + batch.as_debug_zset(), + zset! { + tuple!(0_u64, "Alice", 20_u64, 0_u64, "Engineer") => 1, + tuple!(2_u64, "Charlie", 40_u64, 0_u64, "Engineer") => 1, + tuple!(1_u64, "Bob", 30_u64, 1_u64, "Doctor") => 1, + } + ); + } + + Ok(()) + } + + #[test] + fn test_cartesian_product() -> Result<(), anyhow::Error> { + let (mut circuit, inputs, output) = + setup_inc_data_log().build_circuit_from_ir(|root_circuit, dbsp_inputs| { + let code = [ + Stmt::from(VarStmt { + name: "person".to_string(), + initializer: Some(Expr::from(DbspInput::add( + Person::schema(), + root_circuit, + dbsp_inputs, + ))), + }), + Stmt::from(VarStmt { + name: "profession".to_string(), + initializer: Some(Expr::from(DbspInput::add( + Profession::schema(), + root_circuit, + dbsp_inputs, + ))), + }), + Stmt::from(VarStmt { + name: "joined".to_string(), + initializer: Some(Expr::from(CartesianProductExpr::new( + Expr::from(AliasExpr { + relation: Expr::from(VarExpr::new("person")), + alias: "pers".to_string(), + }), + Expr::from(AliasExpr { + relation: Expr::from(VarExpr::new("profession")), + alias: "prof".to_string(), + }), + None, + ))), + }), + ]; + + Ok(code) + })?; + + let person_input = inputs.get("person").unwrap(); + let profession_input = inputs.get("profession").unwrap(); + + for (person_step, profession_step) in person_profession_data() { + person_input.insert_with_same_weight(&person_step, 1); + profession_input.insert_with_same_weight(&profession_step, 1); + + circuit.transaction()?; + + let batch = output.to_batch(); + println!("{}", batch.as_debug_table()); + assert_eq!( + batch.as_debug_zset(), + zset! { + tuple!(0_u64, "Alice", 20_u64, 0_u64, 0_u64, "Engineer") => 1, + tuple!(0_u64, "Alice", 20_u64, 0_u64, 1_u64, "Doctor") => 1, + tuple!(1_u64, "Bob", 30_u64, 1_u64, 0_u64, "Engineer") => 1, + tuple!(1_u64, "Bob", 30_u64, 1_u64, 1_u64, "Doctor") => 1, + tuple!(2_u64, "Charlie", 40_u64, 0_u64, 0_u64, "Engineer") => 1, + tuple!(2_u64, "Charlie", 40_u64, 0_u64, 1_u64, "Doctor") => 1, + } + ); + } + + Ok(()) + } + + #[test] + fn test_self_join() -> Result<(), anyhow::Error> { + let (mut circuit, inputs, output) = + setup_inc_data_log().build_circuit_from_ir(|root_circuit, dbsp_inputs| { + let code = [ + Stmt::from(VarStmt { + name: "edges".to_string(), + initializer: Some(Expr::from(DbspInput::add( + Edge::schema(), + root_circuit, + dbsp_inputs, + ))), + }), + Stmt::from(VarStmt { + name: "len_1".to_string(), + initializer: Some(Expr::from(ProjectionExpr { + relation: Expr::from(VarExpr::new("edges")), + attributes: ["from", "to"] + .into_iter() + .map(|name| (name.to_string(), Expr::from(VarExpr::new(name)))) + .chain( + [ + ("cumulated_weight", Expr::from(VarExpr::new("weight"))), + ( + "hopcount", + Expr::from(LiteralExpr { + value: Literal::Uint(1), + }), + ), + ] + .map(|(name, expr)| (name.to_string(), expr)), + ) + .collect(), + })), + }), + Stmt::from(VarStmt { + name: "len_2".to_string(), + initializer: Some(Expr::from(EquiJoinExpr { + left: Expr::from(AliasExpr { + relation: Expr::from(VarExpr::new("len_1")), + alias: "cur".to_string(), + }), + right: Expr::from(AliasExpr { + relation: Expr::from(VarExpr::new("edges")), + alias: "next".to_string(), + }), + on: vec![( + Expr::from(VarExpr::new("to")), + Expr::from(VarExpr::new("from")), + )], + attributes: Some( + [ + ("start", Expr::from(VarExpr::new("cur.from"))), + ("end", Expr::from(VarExpr::new("next.to"))), + ( + "cumulated_weight", + Expr::from(BinaryExpr { + operator: Operator::Addition, + left: Expr::from(VarExpr::new("cur.cumulated_weight")), + right: Expr::from(VarExpr::new("next.weight")), + }), + ), + ( + "hopcount", + Expr::from(BinaryExpr { + operator: Operator::Addition, + left: Expr::from(VarExpr::new("cur.hopcount")), + right: Expr::from(LiteralExpr { + value: Literal::Uint(1), + }), + }), + ), + ] + .into_iter() + .map(|(name, expr)| (name.to_string(), expr)) + .collect(), + ), + })), + }), + Stmt::from(VarStmt { + name: "len_3".to_string(), + initializer: Some(Expr::from(EquiJoinExpr { + left: Expr::from(AliasExpr { + relation: Expr::from(VarExpr::new("len_2")), + alias: "cur".to_string(), + }), + right: Expr::from(AliasExpr { + relation: Expr::from(VarExpr::new("edges")), + alias: "next".to_string(), + }), + on: vec![( + Expr::from(VarExpr::new("end")), + Expr::from(VarExpr::new("from")), + )], + attributes: Some( + [ + ("start", Expr::from(VarExpr::new("cur.start"))), + ("end", Expr::from(VarExpr::new("next.to"))), + ( + "cumulated_weight", + Expr::from(BinaryExpr { + operator: Operator::Addition, + left: Expr::from(VarExpr::new("cur.cumulated_weight")), + right: Expr::from(VarExpr::new("next.weight")), + }), + ), + ( + "hopcount", + Expr::from(BinaryExpr { + operator: Operator::Addition, + left: Expr::from(VarExpr::new("cur.hopcount")), + right: Expr::from(LiteralExpr { + value: Literal::Uint(1), + }), + }), + ), + ] + .into_iter() + .map(|(name, expr)| (name.to_string(), expr)) + .collect(), + ), + })), + }), + Stmt::from(VarStmt { + name: "len_4".to_string(), + initializer: Some(Expr::from(EquiJoinExpr { + left: Expr::from(AliasExpr { + relation: Expr::from(VarExpr::new("len_3")), + alias: "cur".to_string(), + }), + right: Expr::from(AliasExpr { + relation: Expr::from(VarExpr::new("edges")), + alias: "next".to_string(), + }), + on: vec![( + Expr::from(VarExpr::new("end")), + Expr::from(VarExpr::new("from")), + )], + attributes: Some( + [ + ("start", Expr::from(VarExpr::new("cur.start"))), + ("end", Expr::from(VarExpr::new("next.to"))), + ( + "cumulated_weight", + Expr::from(BinaryExpr { + operator: Operator::Addition, + left: Expr::from(VarExpr::new("cur.cumulated_weight")), + right: Expr::from(VarExpr::new("next.weight")), + }), + ), + ( + "hopcount", + Expr::from(BinaryExpr { + operator: Operator::Addition, + left: Expr::from(VarExpr::new("cur.hopcount")), + right: Expr::from(LiteralExpr { + value: Literal::Uint(1), + }), + }), + ), + ] + .into_iter() + .map(|(name, expr)| (name.to_string(), expr)) + .collect(), + ), + })), + }), + Stmt::from(VarStmt { + name: "full_closure".to_string(), + initializer: Some(Expr::from(UnionExpr { + relations: ["len_1", "len_2", "len_3", "len_4"] + .into_iter() + .map(|name| Expr::from(VarExpr::new(name))) + .collect(), + })), + }), + ]; + Ok(code) + })?; + + let edges_input = inputs.get("edges").unwrap(); + + let init_data = [ + Edge::new(0, 1, 1), + // This edge is omitted: Edge::new(1, 2, 1), + Edge::new(2, 3, 2), + Edge::new(3, 4, 2), + ]; + + println!("Insert of init_data:"); + + edges_input.insert_with_same_weight(init_data.iter(), 1); + + circuit.transaction()?; + + let batch = output.to_batch(); + println!("{}", batch.as_table()); + assert_eq!( + batch.as_debug_zset(), + zset! { + tuple!(0_u64, 1_u64, 1_u64, 1_u64) => 1, + tuple!(2_u64, 3_u64, 2_u64, 1_u64) => 1, + tuple!(2_u64, 4_u64, 4_u64, 2_u64) => 1, + tuple!(3_u64, 4_u64, 2_u64, 1_u64) => 1, + } + ); + + let extra_data = [Edge::new(1, 2, 1)]; + + println!("Insert of extra_data:"); + + edges_input.insert_with_same_weight(extra_data.iter(), 1); + + circuit.transaction()?; + + let batch = output.to_batch(); + println!("{}", batch.as_table()); + assert_eq!( + batch.as_debug_zset(), + zset! { + tuple!(0_u64, 2_u64, 2_u64, 2_u64) => 1, + tuple!(1_u64, 2_u64, 1_u64, 1_u64) => 1, + tuple!(0_u64, 3_u64, 4_u64, 3_u64) => 1, + tuple!(1_u64, 3_u64, 3_u64, 2_u64) => 1, + tuple!(0_u64, 4_u64, 6_u64, 4_u64) => 1, + tuple!(1_u64, 4_u64, 5_u64, 3_u64) => 1, + } + ); + + Ok(()) + } + + #[test] + fn test_iteration() -> Result<(), anyhow::Error> { + let (mut circuit, inputs, output) = + setup_inc_data_log().build_circuit_from_ir(|root_circuit, dbsp_inputs| { + let code = [ + Stmt::from(VarStmt { + name: "edges".to_string(), + initializer: Some(Expr::from(ProjectionExpr { + relation: Expr::from(DbspInput::add( + Edge::schema(), + root_circuit, + dbsp_inputs, + )), + attributes: ["from", "to", "weight"] + .into_iter() + .map(|name| (name.to_string(), Expr::from(VarExpr::new(name)))) + .collect(), + })), + }), + Stmt::from(VarStmt { + name: "base".to_string(), + initializer: Some(Expr::from(ProjectionExpr { + relation: Expr::from(VarExpr::new("edges")), + attributes: ["from", "to"] + .into_iter() + .map(|name| (name.to_string(), Expr::from(VarExpr::new(name)))) + .chain( + [ + ("cumulated_weight", Expr::from(VarExpr::new("weight"))), + ( + "hopcount", + Expr::from(LiteralExpr { + value: Literal::Uint(1), + }), + ), + ] + .map(|(name, expr)| (name.to_string(), expr)), + ) + .collect(), + })), + }), + Stmt::from(VarStmt { + name: "closure".to_string(), + initializer: Some(Expr::from(FixedPointIterExpr { + imports: ["edges"] + .into_iter() + .map(|name| (name.to_string(), Expr::from(VarExpr::new(name)))) + .collect(), + accumulator: ( + "accumulator".to_string(), + Expr::from(VarExpr::new("base")), + ), + step: BlockStmt { + stmts: vec![Stmt::from(ExprStmt { + expr: Expr::from(EquiJoinExpr { + left: Expr::from(AliasExpr { + relation: Expr::from(VarExpr::new("accumulator")), + alias: "cur".to_string(), + }), + right: Expr::from(AliasExpr { + relation: Expr::from(VarExpr::new("edges")), + alias: "next".to_string(), + }), + on: vec![( + Expr::from(VarExpr::new("to")), + Expr::from(VarExpr::new("from")), + )], + attributes: Some( + [ + ("start", Expr::from(VarExpr::new("cur.from"))), + ("end", Expr::from(VarExpr::new("next.to"))), + ( + "cumulated_weight", + Expr::from(BinaryExpr { + operator: Operator::Addition, + left: Expr::from(VarExpr::new( + "cur.cumulated_weight", + )), + right: Expr::from(VarExpr::new( + "next.weight", + )), + }), + ), + ( + "hopcount", + Expr::from(BinaryExpr { + operator: Operator::Addition, + left: Expr::from(VarExpr::new( + "cur.hopcount", + )), + right: Expr::from(LiteralExpr { + value: Literal::Uint(1), + }), + }), + ), + ] + .into_iter() + .map(|(name, expr)| (name.to_string(), expr)) + .collect(), + ), + }), + })], + }, + })), + }), + ]; + Ok(code) + })?; + let edges_input = inputs.get("edges").unwrap(); + + let init_data = [ + Edge::new(0, 1, 1), + Edge::new(1, 2, 1), + Edge::new(2, 3, 2), + Edge::new(3, 4, 2), + ]; + + println!("Insert of init_data:"); + + edges_input.insert_with_same_weight(init_data.iter(), 1); + + circuit.transaction()?; + + let batch = output.to_batch(); + println!("{}", batch.as_table()); + assert_eq!( + batch.as_debug_zset(), + zset! { + tuple!(0_u64, 1_u64, 1_u64, 1_u64) => 1, + tuple!(0_u64, 2_u64, 2_u64, 2_u64) => 1, + tuple!(1_u64, 2_u64, 1_u64, 1_u64) => 1, + tuple!(0_u64, 3_u64, 4_u64, 3_u64) => 1, + tuple!(1_u64, 3_u64, 3_u64, 2_u64) => 1, + tuple!(2_u64, 3_u64, 2_u64, 1_u64) => 1, + tuple!(0_u64, 4_u64, 6_u64, 4_u64) => 1, + tuple!(1_u64, 4_u64, 5_u64, 3_u64) => 1, + tuple!(2_u64, 4_u64, 4_u64, 2_u64) => 1, + tuple!(3_u64, 4_u64, 2_u64, 1_u64) => 1, + } + ); + + Ok(()) + } + + #[test] + fn test_mvr_store_crdt() -> Result<(), anyhow::Error> { + let (mut circuit, inputs, output) = + setup_inc_data_log().build_circuit_from_ir(|root_circuit, dbsp_inputs| { + let code = [ + // Inputs start. + Stmt::from(VarStmt { + name: "pred".to_string(), + initializer: Some(Expr::from(DbspInput::add( + PredRel::schema(), + root_circuit, + dbsp_inputs, + ))), + }), + Stmt::from(VarStmt { + name: "set".to_string(), + initializer: Some(Expr::from(DbspInput::add( + SetOp::schema(), + root_circuit, + dbsp_inputs, + ))), + }), + // Inputs end. + Stmt::from(VarStmt { + name: "overwritten".to_string(), + initializer: Some(Expr::from(DistinctExpr { + relation: Expr::from(ProjectionExpr { + relation: Expr::from(VarExpr::new("pred")), + attributes: [("RepId", "FromRepId"), ("Ctr", "FromCtr")] + .into_iter() + .map(|(name, origin)| { + (name.to_string(), Expr::from(VarExpr::new(origin))) + }) + .collect(), + }), + })), + }), + Stmt::from(VarStmt { + name: "overwrites".to_string(), + initializer: Some(Expr::from(DistinctExpr { + relation: Expr::from(ProjectionExpr { + relation: Expr::from(VarExpr::new("pred")), + attributes: [("RepId", "ToRepId"), ("Ctr", "ToCtr")] + .into_iter() + .map(|(name, origin)| { + (name.to_string(), Expr::from(VarExpr::new(origin))) + }) + .collect(), + }), + })), + }), + Stmt::from(VarStmt { + name: "isRoot".to_string(), + initializer: Some(Expr::from(DifferenceExpr { + left: Expr::from(ProjectionExpr { + relation: Expr::from(VarExpr::new("set")), + attributes: ["RepId", "Ctr"] + .into_iter() + .map(|name| (name.to_string(), Expr::from(VarExpr::new(name)))) + .collect(), + }), + right: Expr::from(VarExpr::new("overwrites")), + })), + }), + Stmt::from(VarStmt { + name: "isLeaf".to_string(), + initializer: Some(Expr::from(DifferenceExpr { + left: Expr::from(ProjectionExpr { + relation: Expr::from(VarExpr::new("set")), + attributes: ["RepId", "Ctr"] + .into_iter() + .map(|name| (name.to_string(), Expr::from(VarExpr::new(name)))) + .collect(), + }), + right: Expr::from(VarExpr::new("overwritten")), + })), + }), + Stmt::from(VarStmt { + name: "isCausallyReady".to_string(), + initializer: Some(Expr::from(FixedPointIterExpr { + imports: ["pred"] + .into_iter() + .map(|name| (name.to_string(), Expr::from(VarExpr::new(name)))) + .collect(), + accumulator: ( + "isCausallyReady".to_string(), + Expr::from(VarExpr::new("isRoot")), + ), + step: BlockStmt { + stmts: vec![Stmt::from(ExprStmt { + expr: Expr::from(EquiJoinExpr { + left: Expr::from(AliasExpr { + relation: Expr::from(VarExpr::new("isCausallyReady")), + alias: "cur".to_string(), + }), + right: Expr::from(AliasExpr { + relation: Expr::from(VarExpr::new("pred")), + alias: "next".to_string(), + }), + on: vec![ + ( + Expr::from(VarExpr::new("RepId")), + Expr::from(VarExpr::new("FromRepId")), + ), + ( + Expr::from(VarExpr::new("Ctr")), + Expr::from(VarExpr::new("FromCtr")), + ), + ], + attributes: Some( + [ + ("RepId", Expr::from(VarExpr::new("next.ToRepId"))), + ("Ctr", Expr::from(VarExpr::new("next.ToCtr"))), + ] + .into_iter() + .map(|(name, expr)| (name.to_string(), expr)) + .collect(), + ), + }), + })], + }, + })), + }), + Stmt::from(VarStmt { + name: "mvrStore".to_string(), + initializer: Some(Expr::from(EquiJoinExpr { + left: Expr::from(VarExpr::new("isCausallyReady")), + right: Expr::from(EquiJoinExpr { + left: Expr::from(VarExpr::new("isLeaf")), + right: Expr::from(VarExpr::new("set")), + on: vec![ + ( + Expr::from(VarExpr::new("RepId")), + Expr::from(VarExpr::new("RepId")), + ), + ( + Expr::from(VarExpr::new("Ctr")), + Expr::from(VarExpr::new("Ctr")), + ), + ], + // With `attributes: None` the query does not work because + // the fields `rep_id` and `ctr` are both duplicated in + // the tuple output. The EquiJoin below then indexes upon + // both duplicated fields for its `right` operand + // and no join match is found with its `left` operand. + // Welcome to the funny world of relational algebra's semantics + // under name collisions. + attributes: Some( + [ + ("RepId", Expr::from(VarExpr::new("RepId"))), + ("Ctr", Expr::from(VarExpr::new("Ctr"))), + ("Key", Expr::from(VarExpr::new("Key"))), + ("Value", Expr::from(VarExpr::new("Value"))), + ] + .into_iter() + .map(|(name, expr)| (name.to_string(), expr)) + .collect(), + ), + }), + on: vec![ + ( + Expr::from(VarExpr::new("RepId")), + Expr::from(VarExpr::new("RepId")), + ), + ( + Expr::from(VarExpr::new("Ctr")), + Expr::from(VarExpr::new("Ctr")), + ), + ], + attributes: Some( + [ + ("Key", Expr::from(VarExpr::new("Key"))), + ("Value", Expr::from(VarExpr::new("Value"))), + ] + .into_iter() + .map(|(name, expr)| (name.to_string(), expr)) + .collect(), + ), + })), + }), + ]; + Ok(code) + })?; + + let pred_rel_input = inputs.get("pred").unwrap(); + let set_op_input = inputs.get("set").unwrap(); + + // The operation history is as follows: + // In first step (just one root operation setting register with key 1 to + // value 1): + // + // set_0_0(1, 1) + // + // In second step (concurrent writes by replica 0 and 1): + // + // ---> set_0_1(1, 2) + // set_0_0(1, 1) + // ---> set_1_0(1, 3) + // + // In third step (replica 1 does a "merge" operation overwriting the + // previous conflict): + // + // ---> set_0_1(1, 2) + // set_0_0(1, 1) ---> set_1_2(1, 4) + // ---> set_1_0(1, 3) + // + + let pred_rel_data = [ + vec![], + vec![PredRel::new(0, 0, 0, 1), PredRel::new(0, 0, 1, 0)], + vec![PredRel::new(0, 1, 1, 2), PredRel::new(1, 0, 1, 2)], + ]; + + let set_op_data = [ + vec![SetOp::new(0, 0, 1, 1)], + vec![SetOp::new(0, 1, 1, 2), SetOp::new(1, 0, 1, 3)], + vec![SetOp::new(1, 2, 1, 4)], + ]; + + let mut expected = [ + zset! { + tuple!(1_u64, 1_u64) => 1, + }, + zset! { + tuple!(1_u64, 1_u64) => -1, + tuple!(1_u64, 2_u64) => 1, + tuple!(1_u64, 3_u64) => 1, + }, + zset! { + tuple!(1_u64, 2_u64) => -1, + tuple!(1_u64, 3_u64) => -1, + tuple!(1_u64, 4_u64) => 1, + }, + ] + .into_iter(); + + for (pred_rel_step, set_op_step) in pred_rel_data.iter().zip(set_op_data.iter()) { + pred_rel_input.insert_with_same_weight(pred_rel_step.iter(), 1); + set_op_input.insert_with_same_weight(set_op_step.iter(), 1); + + circuit.transaction()?; + + let batch = output.to_batch(); + println!("{}", batch.as_table()); + assert_eq!(batch.as_zset(), expected.next().unwrap()); + } + + Ok(()) + } +} diff --git a/packages/coln-query/src/operator.rs b/packages/coln-query/src/operator.rs new file mode 100644 index 00000000..45d6be63 --- /dev/null +++ b/packages/coln-query/src/operator.rs @@ -0,0 +1,34 @@ +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub enum Operator { + // Comparison operations. + /// Eagerly-evaluated binary operation. + Equal, + /// Eagerly-evaluated binary operation. + NotEqual, + /// Eagerly-evaluated binary operation. + Less, + /// Eagerly-evaluated binary operation. + LessEqual, + /// Eagerly-evaluated binary operation. + Greater, + /// Eagerly-evaluated binary operation. + GreaterEqual, + + // Logical operations on booleans (and values coerced into booleans). + /// Lazily-evaluated binary operation. + And, + /// Lazily-evaluated binary operation. + Or, + /// Eagerly-evaluated unary operation. + Not, + + // Arithmetic operations on numbers. + /// Eagerly-evaluated binary operation. + Addition, + /// Eagerly-evaluated binary operation or eagerly-evaluated unary operation. + Subtraction, + /// Eagerly-evaluated binary operation. + Multiplication, + /// Eagerly-evaluated binary operation. + Division, +} diff --git a/packages/coln-query/src/operators/coalesce.rs b/packages/coln-query/src/operators/coalesce.rs new file mode 100644 index 00000000..5d952a84 --- /dev/null +++ b/packages/coln-query/src/operators/coalesce.rs @@ -0,0 +1,33 @@ +use crate::relation::{RelationRef, SchemaTuple, TupleKey, TupleValue, new_relation}; +use std::rc::Rc; + +/// If the schema is not coalesced, this helper will compact the tuple key and +/// tuple value of the relation to _only_ carry the active fields of the schema. +/// +/// This is important for set operations like union, intersection, and difference, +/// which require equality of schemas to function correctly. +pub fn coalesce_helper(relation: RelationRef) -> RelationRef { + let relation_ref = relation.borrow(); + + if relation_ref.schema.is_coalesced() { + drop(relation_ref); + return relation; + } + + let schema = relation_ref.schema.coalesce(); + let coalesced = relation_ref.inner.map_index({ + let relation = Rc::clone(&relation); + move |(key, tuple)| { + let relation_ref = relation.borrow(); + let key: TupleKey = SchemaTuple::new(&relation_ref.schema.key, key) + .coalesce() + .collect(); + let tuple: TupleValue = SchemaTuple::new(&relation_ref.schema.tuple, tuple) + .coalesce() + .collect(); + (key, tuple) + } + }); + + new_relation(schema, coalesced) +} diff --git a/packages/coln-query/src/operators/mod.rs b/packages/coln-query/src/operators/mod.rs new file mode 100644 index 00000000..c7d2f286 --- /dev/null +++ b/packages/coln-query/src/operators/mod.rs @@ -0,0 +1,3 @@ +pub mod coalesce; +pub mod projection; +pub mod reindex; diff --git a/packages/coln-query/src/operators/projection.rs b/packages/coln-query/src/operators/projection.rs new file mode 100644 index 00000000..33ff1579 --- /dev/null +++ b/packages/coln-query/src/operators/projection.rs @@ -0,0 +1,108 @@ +use crate::{ + context::InterpreterContext, + expr::Expr, + interpreter::Interpreter, + relation::{RelationSchema, Tuple, TupleKey, TupleValue}, + scalar::ScalarTypedValue, +}; + +pub fn projection_helper(attributes: &[(String, Expr)]) -> ProjectionStrategy<'_> { + let requires_projection = attributes + .iter() + .any(|(_, expr)| is_pickable(expr).is_none()); + // We disable the pick optimization for now, as it may cause trouble with + // column ordering. + let requires_projection = true; + + if requires_projection { + ProjectionStrategy::Projection(ProjectionHelper::new(attributes)) + } else { + ProjectionStrategy::Pick(PickHelper::new(attributes)) + } +} + +pub enum ProjectionStrategy<'a> { + Projection(ProjectionHelper), + Pick(PickHelper<'a>), +} + +pub struct ProjectionHelper { + attributes: Vec, + maps: Vec, +} + +impl ProjectionHelper { + fn new(attributes: &[(String, Expr)]) -> Self { + let (attributes, maps) = attributes.iter().cloned().unzip(); + Self { attributes, maps } + } + pub fn prepare( + self, + schema: &RelationSchema, + ) -> ( + RelationSchema, + impl Fn(InterpreterContext) -> (TupleKey, TupleValue) + use<> + Clone, + ) { + let schema = schema.project(self.attributes); + let projection = move |mut ctx: InterpreterContext| { + let value: TupleValue = self + .maps + .iter() + .map(|map| { + ScalarTypedValue::try_from( + Interpreter::new(None) + .evaluate(map, &mut ctx) + .expect("Runtime error while interpreting projection function"), + ) + .expect("Type error while interpreting projection function") + }) + .collect(); + (TupleKey::empty(), value) + }; + (schema, projection) + } +} + +pub struct PickHelper<'a> { + /// First element is the target name of the attribute. + /// Second element is the source name of the attribute, + /// if different from the target name. + attributes: Vec<(&'a String, Option<&'a String>)>, +} + +impl<'a> PickHelper<'a> { + fn new(attributes: &'a [(String, Expr)]) -> Self { + let attributes = attributes + .iter() + .map(|(target_name, expr)| { + is_pickable(expr) + .map(|source_name| (source_name, Some(target_name))) + .expect("Non-pick expression in pick helper") + }) + .collect(); + Self { attributes } + } + pub fn prepare(&self, schema: &RelationSchema) -> RelationSchema { + schema.pick(&self.attributes) + } +} + +/// If the passed expression is _exclusively_ referencing a **tuple** variable +/// and not containing an alias (that is a "."), `Some` is returned with the +/// variable's name. Otherwise, `None` is returned. +/// +/// An alias requires the interpreter to be run to have variables be +/// named according to their alias. +pub fn is_pickable(expr: &Expr) -> Option<&String> { + match expr { + // Unresolved variables are variables from a tuple context. + Expr::Var(inner) => { + if inner.resolved.is_none() && !inner.name.contains(".") { + Some(&inner.name) + } else { + None + } + } + _ => None, + } +} diff --git a/packages/coln-query/src/operators/reindex.rs b/packages/coln-query/src/operators/reindex.rs new file mode 100644 index 00000000..4fa8599b --- /dev/null +++ b/packages/coln-query/src/operators/reindex.rs @@ -0,0 +1,76 @@ +use super::projection::is_pickable; +use crate::{ + context::InterpreterContext, + dbsp::StreamWrapper, + expr::Expr, + interpreter::Interpreter, + relation::{RelationRef, SchemaTuple, TupleKey}, + scalar::ScalarTypedValue, + variable::Environment, +}; +use std::rc::Rc; + +pub fn reindex_helper( + relation: &RelationRef, + on: &[&Expr], + environment: &Environment, +) -> (StreamWrapper, Vec) { + let requires_projection = on.iter().any(|expr| is_pickable(expr).is_none()); + // We disable the pick optimization for now, as it may cause trouble with + // column ordering. + let requires_projection = true; + + let relation_ref = relation.borrow(); + + if requires_projection { + let schema: Vec = on + .iter() + .enumerate() + .map(|(idx, _)| format!("anonym_field_{idx}")) + .collect(); + let indexed = relation_ref.inner.map_index({ + let relation = Rc::clone(relation); + let exprs: Vec = on.iter().map(|expr| (*expr).clone()).collect(); + let environment = environment.clone(); + move |(_key, tuple)| { + let schema = &relation.borrow().schema; + let environment = &mut environment.clone(); + let mut new_ctx = InterpreterContext::new(environment); + new_ctx.extend_tuple_ctx(&None, &schema.tuple, tuple); + let key: TupleKey = exprs + .iter() + .map(|expr| { + ScalarTypedValue::try_from( + Interpreter::new(None) + .evaluate(expr, &mut new_ctx) + .expect("Runtime error while interpreting projection function"), + ) + .expect("Type error while interpreting projection function") + }) + .collect(); + (key, tuple.clone()) + } + }); + (indexed, schema) + } else { + let key_field_picks: Vec = on + .iter() + .map(|expr| { + is_pickable(expr) + .expect("Expected pickable expression") + .clone() + }) + .collect(); + let indexed = relation_ref.inner.map_index({ + let key_field_picks = key_field_picks.clone(); + let relation = Rc::clone(relation); + move |(_key, tuple)| { + let key: TupleKey = SchemaTuple::new(&relation.borrow().schema.tuple, tuple) + .pick(key_field_picks.as_slice()) + .collect(); + (key, tuple.clone()) + } + }); + (indexed, key_field_picks) + } +} diff --git a/packages/coln-query/src/optimizer/mod.rs b/packages/coln-query/src/optimizer/mod.rs new file mode 100644 index 00000000..6054bd51 --- /dev/null +++ b/packages/coln-query/src/optimizer/mod.rs @@ -0,0 +1,18 @@ +use crate::{error::OptimizationError, stmt::Code}; + +/// An optimizer does a series of transformations on the relational algebra IR +/// which retain the semantics of the program but intend to improve performance. +/// For instance, it could do: +/// - predicate pushdown +/// - expression simplification +pub trait Optimizer: Clone { + fn optimize(self, code: Code) -> Result { + // The default impl does nothing and simply returns the IR as is. + Ok(code) + } +} + +#[derive(Clone, Debug, Default)] +pub struct NoOptimizer {} + +impl Optimizer for NoOptimizer {} diff --git a/packages/coln-query/src/relation.rs b/packages/coln-query/src/relation.rs new file mode 100644 index 00000000..6bbebd11 --- /dev/null +++ b/packages/coln-query/src/relation.rs @@ -0,0 +1,619 @@ +use super::scalar::ScalarTypedValue; +use crate::{dbsp::StreamWrapper, error::SyntaxError, scalar::ScalarType}; +use dbsp::{never_none, never_roaring_filter}; +use std::{ + cell::RefCell, + collections::{HashMap, HashSet}, + fmt::{self, Debug, Display}, + rc::Rc, +}; + +pub trait Tuple: FromIterator { + fn empty() -> Self { + Self::from_iter(vec![]) + } + fn data_at(&self, index: usize) -> &ScalarTypedValue; + /// Iterates over _all_ stored fields of the tuple, + /// regardless if they are part of the current schema. + fn data(&self) -> impl Iterator; + /// Assumes that the passed indexes are valid for the tuple. + fn data_to_string(&self) -> String { + let fields = self + .data() + .map(|field| field.to_string()) + .collect::>() + .join(" | "); + format!("| {fields} |") + } +} + +pub struct SchemaTuple<'a, T> { + schema: &'a TupleSchema, + tuple: &'a T, +} + +impl<'a, T: Tuple> SchemaTuple<'a, T> { + pub fn new(schema: &'a TupleSchema, tuple: &'a T) -> Self { + Self { schema, tuple } + } + pub fn fields(&self) -> impl Iterator { + self.schema + .active_fields() + .map(|(index, info)| self.tuple.data_at(index)) + } + pub fn all_fields(&self) -> impl Iterator { + self.schema + .all_fields() + .map(|(index, _info)| self.tuple.data_at(index)) + } + pub fn named_fields( + &self, + alias: &Option, + ) -> impl Iterator { + self.schema + .active_fields() + .map(|(index, info)| (info.name(alias), self.tuple.data_at(index).clone())) + } + pub fn coalesce(&self) -> impl Iterator { + self.schema + .active_fields() + .map(|(index, info)| self.tuple.data_at(index).clone()) + } + pub fn pick(&self, fields: &[String]) -> impl Iterator { + self.schema.active_fields().filter_map(|(index, info)| { + if fields.contains(&info.name) { + Some(self.tuple.data_at(index).clone()) + } else { + None + } + }) + } + pub fn join(&self, other: &Self) -> impl Iterator { + self.fields().chain(other.fields()).cloned() + } +} + +impl Debug for SchemaTuple<'_, TupleValue> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_list() + .entries(self.schema.active_fields().map(|(index, info)| { + format!("{}: {}", info.name(&None), self.tuple.data_at(index)) + })) + .finish() + } +} + +#[derive( + Clone, + Default, + Debug, + Eq, + PartialEq, + Ord, + PartialOrd, + Hash, + size_of::SizeOf, + rkyv::Archive, + rkyv::Serialize, + rkyv::Deserialize, +)] +#[archive_attr(derive(Ord, Eq, PartialEq, PartialOrd))] +pub struct TupleValue { + /// The data of the tuple which can be accessed by the index. + /// Currently, the fields store their types alongside the data. However, + /// this is redundant and could be removed to save space. Interestingly, + /// `ScalarValue`, which is a union without a type tag (contrast it + /// with `ScalarTypedValue`), has the same size, hence, the extra type tag + /// does not increase the size currently. + pub data: Vec, +} + +// impl DbData for TupleValue {} +// + ArchivedDBData +// + IsNone +// + 'static + +never_none!(TupleValue); +never_roaring_filter!(TupleValue); + +#[macro_export] +macro_rules! tuple { + ( $( $key:expr ),* $(,)?) => {{ + let tuple = [$( ScalarTypedValue::from($key) ),*]; + TupleValue { + data: tuple.to_vec(), + } + }}; +} + +impl> FromIterator for TupleValue { + fn from_iter>(iter: I) -> Self { + Self { + data: iter.into_iter().map(|v| v.into()).collect(), + } + } +} + +impl Tuple for TupleValue { + fn data_at(&self, index: usize) -> &ScalarTypedValue { + &self.data[index] + } + fn data(&self) -> impl Iterator { + self.data.iter() + } +} + +impl Display for TupleValue { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.data_to_string()) + } +} + +#[derive( + Clone, + Default, + Debug, + Eq, + PartialEq, + Ord, + PartialOrd, + Hash, + size_of::SizeOf, + rkyv::Archive, + rkyv::Serialize, + rkyv::Deserialize, +)] +#[archive_attr(derive(Ord, Eq, PartialEq, PartialOrd))] +pub struct TupleKey { + pub data: Vec, +} + +never_none!(TupleKey); +never_roaring_filter!(TupleKey); + +impl> FromIterator for TupleKey { + fn from_iter>(iter: I) -> Self { + Self { + data: iter.into_iter().map(|v| v.into()).collect(), + } + } +} + +impl Tuple for TupleKey { + fn data_at(&self, index: usize) -> &ScalarTypedValue { + &self.data[index] + } + fn data(&self) -> impl Iterator { + self.data.iter() + } +} + +impl Display for TupleKey { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.data_to_string()) + } +} + +/// Currently unused. +#[derive(Debug, Hash, Eq, PartialEq, Clone, PartialOrd, Ord)] +struct Identifier { + name: String, +} + +impl Display for Identifier { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(&self.name) + } +} + +/// Convenience type alias for a reference to a [`Relation`]. +pub type RelationRef = Rc>; + +pub fn new_relation>(schema: RelationSchema, inner: T) -> RelationRef { + Rc::new(RefCell::new(Relation::new(schema, inner))) +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct FieldInfo { + /// The field's name. + name: String, + /// Whether the field is active, that is, not eliminated by, e.g., a + /// projection. + active: bool, + // Maybe add type information here, too. +} + +impl FieldInfo { + fn new(name: String) -> Self { + Self { name, active: true } + } + fn name(&self, alias: &Option) -> String { + let name = alias + .as_ref() + .map(|alias| format!("{}.{}", alias, self.name)) + .unwrap_or_else(|| self.name.clone()); + if self.active { + name + } else { + format!("{name}*") + } + } +} + +type Index = usize; + +#[derive(Clone, PartialEq, Eq)] +pub struct TupleSchema { + fields: Vec, +} + +impl TupleSchema { + pub fn new>(fields: impl IntoIterator) -> Self { + Self { + fields: fields + .into_iter() + .map(|name| FieldInfo::new(name.into())) + .collect(), + } + } + pub fn empty() -> Self { + Self { fields: vec![] } + } + /// Only the active fields are included in the count. + pub fn len(&self) -> usize { + self.fields.iter().filter(|info| info.active).count() + } + pub fn is_empty(&self) -> bool { + self.fields.is_empty() + } + /// Includes the active and inactive fields in the count. + pub fn full_len(&self) -> usize { + self.fields.len() + } + fn is_coalesced(&self) -> bool { + !self.fields.iter().any(|info| !info.active) + } + fn coalesce(&self) -> Self { + self.fields + .iter() + .filter(|info| info.active) + .cloned() + .collect() + } + fn active_fields(&self) -> impl Iterator { + self.fields + .iter() + .enumerate() + .filter(|(_index, info)| info.active) + } + fn all_fields(&self) -> impl Iterator { + self.fields.iter().enumerate() + } + pub fn field_names(&self, alias: &Option) -> impl Iterator { + self.active_fields().map(|(_index, info)| info.name(alias)) + } + pub fn all_field_names(&self, alias: &Option) -> impl Iterator { + self.all_fields().map(|(_index, info)| info.name(alias)) + } + fn select(&self) -> Self { + self.clone() + } + /// We mark all fields as inactive, that is, we forget about them. + fn forget(&self) -> Self { + self.fields + .iter() + .map(|info| FieldInfo { + name: info.name.clone(), + active: false, + }) + .collect() + } + /// In contrast to the `project` method, this method does not remove fields + /// from the schema but marks them as inactive, thereby not coalescing the + /// schema and the order of fields. Optionally, you can rename a field by + /// providing an alias/new name/target name as a second element. + fn pick(&self, fields: &Vec<(&String, Option<&String>)>) -> Self { + // For keeping track of duplicated field names. + let mut active = HashSet::with_capacity(fields.len()); + // Don't use active_fields() here because the tuple is not coalesced + // but we only allow picking from the set of active fields though. + self.all_fields() + .map(|(_index, info)| { + // We do not reactivate already inactive fields. + if !info.active { + return info.clone(); + } + if let Some((source_name, target_name)) = + fields.iter().find(|field| *field.0 == info.name) + { + let name = target_name.cloned().unwrap_or_else(|| info.name.clone()); + if !active.contains(&name) { + active.insert(name.clone()); + return FieldInfo::new(name); // Field is active by constructor. + } else { + // We have a duplicated field name, so we mark it as inactive. + return FieldInfo { + name, + active: false, + }; + } + } + // Field is not in the list of fields to pick, so we mark it as inactive. + FieldInfo { + name: info.name.clone(), + active: false, + } + }) + .collect() + } + /// In case of a full projection, we coalesce the schema and remove inactive + /// fields. The order is also redefined according to the projection. + fn project(&self, fields: Vec) -> Self { + fields.into_iter().collect() + } + fn join(&self, other: &Self) -> Self { + let self_active_field_table: HashSet<&String> = + self.active_fields().map(|(_, info)| &info.name).collect(); + // We mark every active field of `other` as inactive if it is + // shadowed by an active field of `self` with the same name. + let other_fields = other.active_fields().map(|(_, info)| { + let mut info = info.clone(); + if self_active_field_table.contains(&info.name) { + info.active = false; + } + info + }); + self.active_fields() + .map(|(_index, info)| info.clone()) + .chain(other_fields) + .collect() + } + fn fields_to_string<'a>( + &self, + fields: impl Iterator, + with_extra: bool, + ) -> String { + let fields = fields + .map(|(_, info)| info.name(&None)) + .collect::>() + .join(" | "); + format!("| {fields} |") + } +} + +impl FromIterator for TupleSchema { + fn from_iter>(iter: I) -> Self { + Self { + fields: iter.into_iter().collect(), + } + } +} + +impl FromIterator for TupleSchema { + fn from_iter>(iter: I) -> Self { + Self { + fields: iter.into_iter().map(FieldInfo::new).collect(), + } + } +} + +impl Debug for TupleSchema { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.fields_to_string(self.all_fields(), true)) + } +} + +impl Display for TupleSchema { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.fields_to_string(self.active_fields(), false)) + } +} + +/// A [`Relation`]'s schema is a set of fields and we store the index of each. +/// Unlike [`RelationType`], this exists at runtime to, e.g., be able to index +/// fields by name. +#[derive(Clone, Debug)] +pub struct RelationSchema { + /// Not a real name to reference the relation but more like a transformation + /// trace. Real names are handled by variable names. + pub name: String, + pub key: TupleSchema, + pub tuple: TupleSchema, +} + +impl RelationSchema { + pub fn new>( + name: T, + tuple_fields: impl IntoIterator, + key_fields: impl IntoIterator, + ) -> Result { + Ok(Self { + name: name.into(), + key: TupleSchema::new(key_fields), + tuple: TupleSchema::new(tuple_fields), + }) + } + pub fn is_coalesced(&self) -> bool { + self.key.is_coalesced() && self.tuple.is_coalesced() + } + pub fn coalesce(&self) -> Self { + Self { + name: format!("[{}-coalesced]", self.name), + key: self.key.coalesce(), + tuple: self.tuple.coalesce(), + } + } + /// Just clones the current schema, as selections do not alter the schema. + pub fn select(&self) -> Self { + Self { + name: format!("[{}-selected]", self.name), + key: self.key.clone(), + tuple: self.tuple.clone(), + } + } + pub fn pick(&self, fields: &Vec<(&String, Option<&String>)>) -> Self { + Self { + name: format!("[{}-picked]", self.name), + // To keep the `ProjectionExpr`'s semantics consistent, + // we erase the key here, too, as we do for the full projection below. + key: self.key.forget(), + tuple: self.tuple.pick(fields), + } + } + pub fn project(&self, fields: Vec) -> Self { + Self { + name: format!("[{}-projected]", self.name), + key: TupleSchema::empty(), + tuple: self.tuple.project(fields), + } + } + pub fn join(&self, other: &Self, key_fields: impl IntoIterator) -> Self { + Self { + name: format!("[{}-{}-joined]", self.name, other.name), + key: key_fields.into_iter().collect(), + tuple: self.tuple.join(&other.tuple), + } + } + pub fn anti_join(&self, other: &Self, key_fields: impl IntoIterator) -> Self { + // We do not need to store the key in the schema, as it is not used + // in the anti-join. + Self { + name: format!("{}-{}-anti-joined", self.name, other.name), + key: key_fields.into_iter().collect(), + tuple: self.tuple.clone(), + } + } +} + +impl Display for RelationSchema { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "", self.name) + } +} + +impl PartialEq for RelationSchema { + fn eq(&self, other: &Self) -> bool { + self.key == other.key && self.tuple == other.tuple + } +} + +impl Eq for RelationSchema {} + +#[derive(Clone)] +pub struct Relation { + /// The schema of the relation. We need to track it on a per-relation basis + /// because it may change during execution. + pub schema: RelationSchema, + pub inner: StreamWrapper, +} + +impl Relation { + pub fn new>(schema: RelationSchema, inner: T) -> Self { + Self { + schema, + inner: inner.into(), + } + } +} + +impl Display for Relation { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.schema) + } +} + +impl Debug for Relation { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.schema) + } +} + +/// Unlike [`RelationSchema`], this does not exist at runtime but is only used +/// during static analysis. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct RelationType { + fields: HashMap, +} + +impl RelationType { + pub fn join(self, other: Self) -> Self { + // We start with other to have duplicate fields' types be taken from self. + let mut fields = other.fields; + fields.extend(self.fields); + Self { fields } + } + pub fn pick>(mut self, fields: impl IntoIterator) -> Self { + let fields = fields + .into_iter() + .filter_map(|name| self.fields.remove_entry(name.as_ref())) + .collect(); + Self { fields } + } + pub fn into_tuple_vars(self) -> HashMap { + self.fields + } + pub fn field_type(&self, name: &str) -> Option<&ScalarType> { + self.fields.get(name) + } + pub fn intersect<'a>(&'a self, other: &Self) -> impl Iterator { + self.fields.iter().filter_map(|(name, self_scalar_type)| { + // No type checking here, that is, we don't check if + // `self_scalar_type` and `other_scalar_type` are the compatible. + other + .fields + .get(name) + .map(|other_scalar_type| (name, *other_scalar_type)) + }) + } +} + +impl<'a> IntoIterator for &'a RelationType { + type Item = (&'a String, &'a ScalarType); + type IntoIter = std::collections::hash_map::Iter<'a, String, ScalarType>; + + fn into_iter(self) -> Self::IntoIter { + self.fields.iter() + } +} + +impl PartialEq for RelationType +where + // Is there a way to avoid the clone here? + T: ExactSizeIterator + Clone, + S: AsRef, +{ + fn eq(&self, iter: &T) -> bool { + let iter = iter.clone(); // Should be cheap, as it is an iterator. + if self.fields.len() != iter.len() { + return false; + } + for name in iter { + if !self.fields.contains_key(name.as_ref()) { + return false; + } + } + true + } +} + +impl<'a> FromIterator<(&'a String, ScalarType)> for RelationType { + fn from_iter>(iter: T) -> Self { + let fields = iter + .into_iter() + .map(|(name, scalar_type)| (name.clone(), scalar_type)) + .collect(); + Self { fields } + } +} + +impl From<&RelationSchema> for RelationType { + fn from(value: &RelationSchema) -> Self { + let fields = value + .tuple + .field_names(&None) + // Until we have proper types in Datalog, we assign the dummy null type. + .map(|name| (name, ScalarType::Null)) + .collect(); + Self { fields } + } +} diff --git a/packages/coln-query/src/resolver.rs b/packages/coln-query/src/resolver.rs new file mode 100644 index 00000000..043da81d --- /dev/null +++ b/packages/coln-query/src/resolver.rs @@ -0,0 +1,382 @@ +use crate::{ + context::ResolverContext, + error::SyntaxError, + expr::{ + AliasExpr, AntiJoinExpr, AssignExpr, BinaryExpr, CallExpr, CartesianProductExpr, + DifferenceExpr, DistinctExpr, EquiJoinExpr, Expr, ExprVisitorMut, FixedPointIterExpr, + FunctionExpr, GroupingExpr, LiteralExpr, ProjectionExpr, SelectionExpr, UnaryExpr, + UnionExpr, VarExpr, + }, + stmt::{BlockStmt, ExprStmt, Stmt, StmtVisitorMut, VarStmt}, + util::{Named, Resolvable}, + variable::SCOPES_CAPACITY, +}; +use std::{collections::HashMap, iter}; + +#[derive(Clone, Copy, Debug)] +pub struct VariableMeta { + initialized: bool, + slot: usize, +} + +impl VariableMeta { + fn new(slot: usize) -> Self { + Self { + initialized: false, + slot, + } + } +} + +pub struct ScopeStack { + inner: Vec>, +} + +impl Default for ScopeStack { + fn default() -> Self { + Self::new() + } +} + +impl ScopeStack { + pub fn new() -> Self { + let mut scope_stack = Self { + inner: Vec::with_capacity(SCOPES_CAPACITY), + }; + // Create the global scope. + scope_stack.begin_scope(); + scope_stack + } + pub fn just_global(&self) -> bool { + self.inner.len() == 1 + } + pub fn begin_scope(&mut self) { + self.inner.push(HashMap::new()); + } + pub fn end_scope(&mut self) { + self.inner.pop(); + } + pub fn innermost(&self) -> Option<&HashMap> { + self.inner.last() + } + pub fn innermost_mut(&mut self) -> Option<&mut HashMap> { + self.inner.last_mut() + } + /// Iterates from innermost to outermost scope. + pub fn iter(&self) -> impl Iterator> { + self.inner.iter().rev() + } + /// Iterates from innermost to outermost scope while returning indexes that + /// work from left to right. + pub fn indexed_iter(&self) -> impl Iterator)> { + self.inner.iter().enumerate().rev() + } +} + +pub struct Resolver {} + +impl Resolver { + pub fn new() -> Self { + Self {} + } + pub fn resolve<'a>( + &mut self, + stmts: impl IntoIterator, + ctx: VisitorCtx, + ) -> Result<(), SyntaxError> { + // Ensure we have a global scope before resolving. + debug_assert!(ctx.scopes.just_global()); + // We do not call `visit_block` here because the root scope is created + // in the `ScopeStack` constructor and should remain intact across + // multiple calls to `resolve`. + let ret = self.visit_stmts(stmts, ctx); + // Ensure we have a global scope after resolving. + debug_assert!(ctx.scopes.just_global()); + ret + } + // declare in Lox + fn declare_var(&mut self, name: &str, ctx: VisitorCtx) -> Result<(), SyntaxError> { + match ctx.scopes.innermost_mut() { + Some(scope) => { + scope.insert(name.to_string(), VariableMeta::new(scope.len())); + Ok(()) + } + None => Err(SyntaxError::new("No scope to declare variable in")), + } + } + // define in Lox + fn define_var(&mut self, name: &String, ctx: VisitorCtx) -> Result<(), SyntaxError> { + match ctx.scopes.innermost_mut() { + Some(scope) => match scope.get_mut(name) { + Some(var) => { + var.initialized = true; + Ok(()) + } + None => Err(SyntaxError::new("Variable not declared in innermost scope")), + }, + None => Err(SyntaxError::new("No scope to find variable to assign to")), + } + } + // resolveLocal in Lox + fn resolve_var( + &mut self, + expr: &mut T, + ctx: VisitorCtx, + ) -> Result<(), SyntaxError> { + for (scope_idx, scope) in ctx.scopes.indexed_iter() { + if let Some(var) = scope.get(expr.name()) { + let slot_idx = var.slot; + expr.set_resolved((scope_idx, slot_idx)); + return Ok(()); + } + } + // We have to tolerate unresolved variables because we are in a tuple context. + // Later on, we can also do static analysis to determine if it is a valid + // reference to a tuple variable by tracking a relation's schema. + if ctx.is_tuple_context { + Ok(()) + } else { + Err(SyntaxError::new(format!( + "Variable '{}' not declared", + expr.name() + ))) + } + } + fn visit_stmts<'a>( + &mut self, + stmts: impl IntoIterator, + ctx: VisitorCtx, + ) -> VisitorResult { + for stmt in stmts { + self.visit_stmt(stmt, ctx)?; + } + Ok(()) + } + fn visit_block<'a, F>( + &mut self, + stmts: impl IntoIterator, + ctx: VisitorCtx, + after_new_scope_actions: F, + ) -> Result<(), SyntaxError> + where + F: FnOnce(&mut Self, VisitorCtx) -> Result<(), SyntaxError>, + { + ctx.scopes.begin_scope(); + after_new_scope_actions(self, ctx)?; + self.visit_stmts(stmts, ctx)?; + ctx.scopes.end_scope(); + Ok(()) + } +} + +impl Resolver { + /// A helper method to visit projection attributes. + fn visit_projection_attributes( + &mut self, + attributes: Option<&mut Vec<(String, Expr)>>, + ctx: VisitorCtx, + ) -> VisitorResult { + ctx.begin_tuple_context(); + let ret = attributes + .map(|attributes| { + attributes + .iter_mut() + .try_for_each(|attribute| self.visit_expr(&mut attribute.1, ctx)) + }) + .unwrap_or(Ok(())); + ctx.end_tuple_context(); + ret + } +} + +type VisitorResult = Result<(), SyntaxError>; +type VisitorCtx<'a, 'b> = &'a mut ResolverContext<'b>; + +impl ExprVisitorMut> for Resolver { + fn visit_binary_expr(&mut self, expr: &mut BinaryExpr, ctx: VisitorCtx) -> VisitorResult { + self.visit_expr(&mut expr.left, ctx) + .and_then(|()| self.visit_expr(&mut expr.right, ctx)) + } + + fn visit_unary_expr(&mut self, expr: &mut UnaryExpr, ctx: VisitorCtx) -> VisitorResult { + self.visit_expr(&mut expr.operand, ctx) + } + + fn visit_grouping_expr(&mut self, expr: &mut GroupingExpr, ctx: VisitorCtx) -> VisitorResult { + self.visit_expr(&mut expr.expr, ctx) + } + + fn visit_var_expr(&mut self, expr: &mut VarExpr, ctx: VisitorCtx) -> VisitorResult { + if let Some(var) = ctx + .scopes + .innermost() + .and_then(|scope| scope.get(&expr.name)) + && !var.initialized + { + return Err(SyntaxError::new( + "Variable referenced in its own initializer", + )); + } + // `resolve_var` returns an error if the variable is not declared. + self.resolve_var(expr, ctx) + } + + fn visit_assign_expr(&mut self, expr: &mut AssignExpr, ctx: VisitorCtx) -> VisitorResult { + self.visit_expr(&mut expr.value, ctx)?; + // `resolve_var` returns an error if the variable is not declared. + self.resolve_var(expr, ctx) + } + + fn visit_literal_expr(&mut self, expr: &mut LiteralExpr, ctx: VisitorCtx) -> VisitorResult { + Ok(()) + } + + fn visit_function_expr(&mut self, expr: &mut FunctionExpr, ctx: VisitorCtx) -> VisitorResult { + self.visit_block(&mut expr.body.stmts, ctx, |resolver, ctx| { + for parameter in &expr.parameters { + resolver.declare_var(parameter, ctx)?; + resolver.define_var(parameter, ctx)?; + } + Ok(()) + }) + } + + fn visit_call_expr(&mut self, expr: &mut CallExpr, ctx: VisitorCtx) -> VisitorResult { + // TODO: check for arity here just once statically. + self.visit_expr(&mut expr.callee, ctx)?; + for arg in &mut expr.arguments { + self.visit_expr(arg, ctx)?; + } + Ok(()) + } + + fn visit_alias_expr(&mut self, expr: &mut AliasExpr, ctx: VisitorCtx) -> VisitorResult { + self.visit_expr(&mut expr.relation, ctx) + } + + fn visit_distinct_expr(&mut self, expr: &mut DistinctExpr, ctx: VisitorCtx) -> VisitorResult { + self.visit_expr(&mut expr.relation, ctx) + } + + fn visit_union_expr(&mut self, expr: &mut UnionExpr, ctx: VisitorCtx) -> VisitorResult { + // TODO: Typecheck: A union is valid if the column types match and + // the amount of columns is the same. + if expr.relations.len() < 2 { + return Err(SyntaxError::new("Union requires at least two relations")); + } + expr.relations + .iter_mut() + .try_for_each(|relation| self.visit_expr(relation, ctx)) + } + + fn visit_difference_expr( + &mut self, + expr: &mut DifferenceExpr, + ctx: VisitorCtx, + ) -> VisitorResult { + self.visit_expr(&mut expr.right, ctx) + .and_then(|()| self.visit_expr(&mut expr.left, ctx)) + } + + fn visit_selection_expr(&mut self, expr: &mut SelectionExpr, ctx: VisitorCtx) -> VisitorResult { + self.visit_expr(&mut expr.relation, ctx).and_then(|()| { + ctx.begin_tuple_context(); + let ret = self.visit_expr(&mut expr.condition, ctx); + ctx.end_tuple_context(); + ret + }) + } + + fn visit_projection_expr( + &mut self, + expr: &mut ProjectionExpr, + ctx: VisitorCtx, + ) -> VisitorResult { + // TODO: statically check that the listed attributes are valid. + // Implement through returning type information through `VisitorResult`. + self.visit_expr(&mut expr.relation, ctx) + .and_then(|()| self.visit_projection_attributes(Some(&mut expr.attributes), ctx)) + } + + fn visit_cartesian_product_expr( + &mut self, + expr: &mut CartesianProductExpr, + ctx: VisitorCtx, + ) -> VisitorResult { + self.visit_equi_join_expr(&mut expr.inner, ctx) + } + + fn visit_equi_join_expr(&mut self, expr: &mut EquiJoinExpr, ctx: VisitorCtx) -> VisitorResult { + // Maybe: statically check that the listed attributes are valid. + // Could be implemented through returning type information through `VisitorResult`. + self.visit_expr(&mut expr.left, ctx) + .and_then(|()| self.visit_expr(&mut expr.right, ctx)) + .and_then(|()| { + expr.on.iter_mut().try_for_each(|(left, right)| { + ctx.begin_tuple_context(); + let ret = self + .visit_expr(left, ctx) + .and_then(|()| self.visit_expr(right, ctx)); + ctx.end_tuple_context(); + ret + }) + }) + .and_then(|()| self.visit_projection_attributes(expr.attributes.as_mut(), ctx)) + } + + fn visit_anti_join_expr(&mut self, expr: &mut AntiJoinExpr, ctx: VisitorCtx) -> VisitorResult { + self.visit_expr(&mut expr.left, ctx) + .and_then(|()| self.visit_expr(&mut expr.right, ctx)) + .and_then(|()| { + expr.on.iter_mut().try_for_each(|(left, right)| { + ctx.begin_tuple_context(); + let ret = self + .visit_expr(left, ctx) + .and_then(|()| self.visit_expr(right, ctx)); + ctx.end_tuple_context(); + ret + }) + }) + } + + fn visit_fixed_point_iter_expr( + &mut self, + expr: &mut FixedPointIterExpr, + ctx: VisitorCtx, + ) -> VisitorResult { + let exprs = iter::once(&mut expr.accumulator) + .chain(expr.imports.iter_mut()) + .try_for_each(|variable| self.visit_expr(&mut variable.1, ctx)); + self.visit_block(&mut expr.step.stmts, ctx, |resolver, ctx| { + iter::once(&expr.accumulator) + .chain(expr.imports.iter()) + .try_for_each(|variable| { + resolver.declare_var(&variable.0, ctx)?; + resolver.define_var(&variable.0, ctx)?; + Ok(()) + }) + }) + } +} + +impl StmtVisitorMut> for Resolver { + fn visit_var_stmt(&mut self, stmt: &mut VarStmt, ctx: VisitorCtx) -> VisitorResult { + self.declare_var(&stmt.name, ctx) + .and_then(|()| { + if let Some(expr) = &mut stmt.initializer { + self.visit_expr(expr, ctx) + } else { + Ok(()) + } + }) + .and_then(|()| self.define_var(&stmt.name, ctx)) + } + + fn visit_expr_stmt(&mut self, stmt: &mut ExprStmt, ctx: VisitorCtx) -> VisitorResult { + self.visit_expr(&mut stmt.expr, ctx) + } + + fn visit_block_stmt(&mut self, stmt: &mut BlockStmt, ctx: VisitorCtx) -> VisitorResult { + self.visit_block(&mut stmt.stmts, ctx, |_resolver, _ctx| Ok(())) + } +} diff --git a/packages/coln-query/src/scalar.rs b/packages/coln-query/src/scalar.rs new file mode 100644 index 00000000..a08470bb --- /dev/null +++ b/packages/coln-query/src/scalar.rs @@ -0,0 +1,156 @@ +use crate::variable::Value; +use std::fmt::{self, Display, Formatter}; + +/// Stores a scalar value plus its type. These are the kinds of values that +/// can be assumed in a relation's column (or a tuple's field). +#[derive( + Debug, + Clone, + PartialEq, + Eq, + PartialOrd, + Ord, + Hash, + size_of::SizeOf, + rkyv::Archive, + rkyv::Serialize, + rkyv::Deserialize, +)] +#[archive_attr(derive(Ord, Eq, PartialEq, PartialOrd))] +pub enum ScalarTypedValue { + /// String. + String(String), + /// Unsigned integer value of 64 bits. + Uint(u64), + /// Signed integer value of 64 bits. + Iint(i64), + /// Boolean. + Bool(bool), + /// A single character. + Char(char), + /// Null. + // The `Null` variant carries the unit type to align its field-arity with + // other variants. That eases the definition of macros operating on the enum. + Null(()), +} + +macro_rules! expect_data { + ( $instance:expr, $variant:path ) => {{ + match $instance { + $variant(data) => data, + _ => panic!( + "Expected a value of type `{}` but got `{}`", + stringify!($variant), + $instance + ), + } + }}; +} + +impl ScalarTypedValue { + pub fn unwrap_into_string(&self) -> String { + expect_data!(self, ScalarTypedValue::String).clone() + } + pub fn unwrap_into_uint(&self) -> u64 { + *expect_data!(self, ScalarTypedValue::Uint) + } + pub fn unwrap_into_iint(&self) -> i64 { + *expect_data!(self, ScalarTypedValue::Iint) + } + pub fn unwrap_into_bool(&self) -> bool { + *expect_data!(self, ScalarTypedValue::Bool) + } + pub fn unwrap_into_char(&self) -> char { + *expect_data!(self, ScalarTypedValue::Char) + } + pub fn unwrap_into_null(&self) { + *expect_data!(self, ScalarTypedValue::Null) + } +} + +impl Default for ScalarTypedValue { + fn default() -> Self { + ScalarTypedValue::Null(()) + } +} + +impl TryFrom for ScalarTypedValue { + type Error = (); + + fn try_from(value: Value) -> Result { + match value { + Value::String(value) => Ok(ScalarTypedValue::String(value)), + Value::Uint(value) => Ok(ScalarTypedValue::Uint(value)), + Value::Iint(value) => Ok(ScalarTypedValue::Iint(value)), + Value::Bool(value) => Ok(ScalarTypedValue::Bool(value)), + Value::Char(value) => Ok(ScalarTypedValue::Char(value)), + Value::Null(()) => Ok(ScalarTypedValue::Null(())), + _ => Err(()), + } + } +} + +impl From for ScalarTypedValue { + fn from(value: String) -> Self { + ScalarTypedValue::String(value) + } +} + +impl From<&str> for ScalarTypedValue { + fn from(value: &str) -> Self { + ScalarTypedValue::String(value.to_owned()) + } +} + +impl From for ScalarTypedValue { + fn from(value: u64) -> Self { + ScalarTypedValue::Uint(value) + } +} + +impl From for ScalarTypedValue { + fn from(value: i64) -> Self { + ScalarTypedValue::Iint(value) + } +} + +impl From for ScalarTypedValue { + fn from(value: char) -> Self { + ScalarTypedValue::Char(value) + } +} + +impl From for ScalarTypedValue { + fn from(value: bool) -> Self { + ScalarTypedValue::Bool(value) + } +} + +impl From<()> for ScalarTypedValue { + fn from(value: ()) -> Self { + ScalarTypedValue::Null(value) + } +} + +impl Display for ScalarTypedValue { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + match self { + ScalarTypedValue::String(value) => write!(f, "{value}"), + ScalarTypedValue::Uint(value) => write!(f, "{value}"), + ScalarTypedValue::Iint(value) => write!(f, "{value}"), + ScalarTypedValue::Bool(value) => write!(f, "{value}"), + ScalarTypedValue::Char(value) => write!(f, "{value}"), + ScalarTypedValue::Null(()) => write!(f, "null"), + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum ScalarType { + String, + Uint, + Iint, + Bool, + Char, + Null, +} diff --git a/packages/coln-query/src/stmt.rs b/packages/coln-query/src/stmt.rs new file mode 100644 index 00000000..1e1164ac --- /dev/null +++ b/packages/coln-query/src/stmt.rs @@ -0,0 +1,113 @@ +use crate::{expr::Expr, impl_from_auto_box, util::MemAddr}; + +pub type Code = Vec; + +#[derive(Clone, Debug)] +pub struct Program { + stmts: Code, + /// Index from which unexecuted code is stored in the program. + partition_index: usize, +} + +impl From for Program { + fn from(stmts: Code) -> Self { + Self { + stmts, + partition_index: 0, + } + } +} + +impl Program { + pub fn empty() -> Self { + Self { + stmts: Vec::new(), + partition_index: 0, + } + } + pub fn unexecuted_code(&self) -> impl Iterator { + self.stmts.iter().skip(self.partition_index) + } + pub fn unexecuted_code_mut(&mut self) -> impl Iterator { + self.stmts.iter_mut().skip(self.partition_index) + } + pub fn extend_program(&mut self, code: impl Iterator) { + self.partition_index = self.stmts.len(); + self.stmts.extend(code); + } +} + +#[derive(Clone, Debug, Eq, PartialEq)] +pub enum Stmt { + // TODO: control flow: IfStmt, WhileStmt, Return?, Print? + Var(Box), + Expr(Box), + Block(Box), +} + +impl_from_auto_box! { + Stmt, + (Stmt::Var, VarStmt), + (Stmt::Expr, ExprStmt), + (Stmt::Block, BlockStmt) +} + +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct VarStmt { + pub name: String, + pub initializer: Option, +} + +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct ExprStmt { + pub expr: Expr, +} + +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct BlockStmt { + pub stmts: Vec, +} + +pub trait StmtVisitor { + fn visit_stmt(&mut self, stmt: &Stmt, ctx: C) -> T { + match stmt { + Stmt::Var(stmt) => self.visit_var_stmt(stmt, ctx), + Stmt::Expr(stmt) => self.visit_expr_stmt(stmt, ctx), + Stmt::Block(stmt) => self.visit_block_stmt(stmt, ctx), + } + } + fn visit_var_stmt(&mut self, stmt: &VarStmt, ctx: C) -> T; + fn visit_expr_stmt(&mut self, stmt: &ExprStmt, ctx: C) -> T; + fn visit_block_stmt(&mut self, stmt: &BlockStmt, ctx: C) -> T; +} + +pub trait StmtVisitorMut { + fn visit_stmt(&mut self, stmt: &mut Stmt, ctx: C) -> T { + match stmt { + Stmt::Var(stmt) => self.visit_var_stmt(stmt, ctx), + Stmt::Expr(stmt) => self.visit_expr_stmt(stmt, ctx), + Stmt::Block(stmt) => self.visit_block_stmt(stmt, ctx), + } + } + fn visit_var_stmt(&mut self, stmt: &mut VarStmt, ctx: C) -> T; + fn visit_expr_stmt(&mut self, stmt: &mut ExprStmt, ctx: C) -> T; + fn visit_block_stmt(&mut self, stmt: &mut BlockStmt, ctx: C) -> T; +} + +pub trait StmtVisitorOwn { + fn visit_stmt(&mut self, stmt: Stmt, ctx: C) -> T { + match stmt { + Stmt::Var(stmt) => self.visit_var_stmt(*stmt, ctx), + Stmt::Expr(stmt) => self.visit_expr_stmt(*stmt, ctx), + Stmt::Block(stmt) => self.visit_block_stmt(*stmt, ctx), + } + } + fn visit_var_stmt(&mut self, stmt: VarStmt, ctx: C) -> T; + fn visit_expr_stmt(&mut self, stmt: ExprStmt, ctx: C) -> T; + fn visit_block_stmt(&mut self, stmt: BlockStmt, ctx: C) -> T; +} + +impl MemAddr for Stmt {} +impl MemAddr for VarStmt {} +impl MemAddr for ExprStmt {} +impl MemAddr for BlockStmt {} diff --git a/packages/coln-query/src/test_helper.rs b/packages/coln-query/src/test_helper.rs new file mode 100644 index 00000000..f0c24a1a --- /dev/null +++ b/packages/coln-query/src/test_helper.rs @@ -0,0 +1,374 @@ +//! This module is only available if compiling with the `test` feature. +//! It provides helpers for testing and benchmarking purposes. + +use crate::{ + IncDataLog, + optimizer::NoOptimizer, + relation::{RelationSchema, TupleKey, TupleValue}, + scalar::ScalarTypedValue, +}; +use std::fmt::Debug; + +pub fn setup_inc_data_log() -> IncDataLog { + IncDataLog::default() +} + +pub trait InputEntity: Into + Into + Clone + Debug { + fn schema() -> RelationSchema; +} + +#[derive(Clone, Debug)] +pub struct Person { + person_id: u64, + name: String, + age: u64, + profession_id: u64, +} + +impl InputEntity for Person { + fn schema() -> RelationSchema { + RelationSchema::new( + "person", + ["person_id", "name", "age", "profession_id"], + ["person_id"], + ) + .expect("Correct schema definition") + } +} + +impl From for TupleKey { + fn from(person: Person) -> Self { + TupleKey { + data: vec![ScalarTypedValue::Uint(person.person_id)], + } + } +} + +impl From for TupleValue { + fn from(person: Person) -> Self { + TupleValue { + data: vec![ + ScalarTypedValue::Uint(person.person_id), + ScalarTypedValue::String(person.name), + ScalarTypedValue::Uint(person.age), + ScalarTypedValue::Uint(person.profession_id), + ], + } + } +} + +#[derive(Clone, Debug)] +pub struct Profession { + profession_id: u64, + name: String, +} + +impl InputEntity for Profession { + fn schema() -> RelationSchema { + RelationSchema::new("profession", ["profession_id", "name"], ["profession_id"]) + .expect("Correct schema definition") + } +} + +impl From for TupleKey { + fn from(profession: Profession) -> Self { + TupleKey { + data: vec![ScalarTypedValue::Uint(profession.profession_id)], + } + } +} + +impl From for TupleValue { + fn from(profession: Profession) -> Self { + TupleValue { + data: vec![ + ScalarTypedValue::Uint(profession.profession_id), + ScalarTypedValue::String(profession.name), + ], + } + } +} + +pub fn person_profession_data() -> [(Vec, Vec); 1] { + [( + vec![ + Person { + person_id: 0, + name: "Alice".to_string(), + age: 20, + profession_id: 0, + }, + Person { + person_id: 1, + name: "Bob".to_string(), + age: 30, + profession_id: 1, + }, + Person { + person_id: 2, + name: "Charlie".to_string(), + age: 40, + profession_id: 0, + }, + ], + vec![ + Profession { + profession_id: 0, + name: "Engineer".to_string(), + }, + Profession { + profession_id: 1, + name: "Doctor".to_string(), + }, + ], + )] +} + +#[derive(Copy, Clone, Debug)] +pub struct PlainRelation { + a: u64, + b: u64, + c: u64, +} + +impl PlainRelation { + pub fn new(a: u64, b: u64, c: u64) -> Self { + Self { a, b, c } + } + const STEPS: usize = 1; + pub fn test_data_1() -> [Vec; Self::STEPS] { + [vec![ + PlainRelation::new(1, 2, 3), + PlainRelation::new(4, 5, 6), + PlainRelation::new(7, 8, 9), + ]] + } + pub fn test_data_2() -> [Vec; Self::STEPS] { + [vec![PlainRelation::new(1, 2, 3)]] + } + pub fn test_data_3() -> [Vec; Self::STEPS] { + [vec![PlainRelation::new(4, 5, 6)]] + } +} + +impl InputEntity for PlainRelation { + fn schema() -> RelationSchema { + RelationSchema::new("plain", ["a", "b", "c"], []).expect("Correct schema definition") + } +} + +impl From for TupleKey { + fn from(fact: PlainRelation) -> Self { + TupleKey { data: vec![] } + } +} + +impl From for TupleValue { + fn from(fact: PlainRelation) -> Self { + TupleValue { + data: vec![ + ScalarTypedValue::Uint(fact.a), + ScalarTypedValue::Uint(fact.b), + ScalarTypedValue::Uint(fact.c), + ], + } + } +} + +#[derive(Copy, Clone, Debug)] +pub struct Edge { + from: u64, + to: u64, + weight: u64, + active: bool, +} + +impl Edge { + pub fn new(from: u64, to: u64, weight: u64) -> Self { + Self { + from, + to, + weight, + active: true, + } + } +} + +impl InputEntity for Edge { + fn schema() -> RelationSchema { + RelationSchema::new("edges", ["from", "to", "weight", "active"], ["from", "to"]) + .expect("Correct schema definition") + } +} + +impl From for TupleKey { + fn from(edge: Edge) -> Self { + TupleKey { + data: vec![ + ScalarTypedValue::Uint(edge.from), + ScalarTypedValue::Uint(edge.to), + ], + } + } +} + +impl From for TupleValue { + fn from(edge: Edge) -> Self { + TupleValue { + data: vec![ + ScalarTypedValue::Uint(edge.from), + ScalarTypedValue::Uint(edge.to), + ScalarTypedValue::Uint(edge.weight), + ScalarTypedValue::Bool(edge.active), + ], + } + } +} + +#[derive(Copy, Clone, Debug)] +pub struct SetOp { + rep_id: u64, + ctr: u64, + key: u64, + value: u64, +} + +impl SetOp { + pub fn new(rep_id: u64, ctr: u64, key: u64, value: u64) -> Self { + Self { + rep_id, + ctr, + key, + value, + } + } +} + +impl InputEntity for SetOp { + fn schema() -> RelationSchema { + RelationSchema::new("set", ["RepId", "Ctr", "Key", "Value"], ["RepId", "Ctr"]) + .expect("Correct schema definition") + } +} + +impl From for TupleKey { + fn from(set_op: SetOp) -> Self { + TupleKey::from_iter([set_op.rep_id, set_op.ctr]) + } +} + +impl From for TupleValue { + fn from(set_op: SetOp) -> Self { + TupleValue::from_iter([set_op.rep_id, set_op.ctr, set_op.key, set_op.value]) + } +} + +#[derive(Copy, Clone, Debug)] +pub struct PredRel { + from_rep_id: u64, + from_ctr: u64, + to_rep_id: u64, + to_ctr: u64, +} + +impl PredRel { + pub fn new(from_rep_id: u64, from_ctr: u64, to_rep_id: u64, to_ctr: u64) -> Self { + Self { + from_rep_id, + from_ctr, + to_rep_id, + to_ctr, + } + } +} + +impl InputEntity for PredRel { + fn schema() -> RelationSchema { + RelationSchema::new( + "pred", + ["FromRepId", "FromCtr", "ToRepId", "ToCtr"], + ["FromRepId", "FromCtr", "ToRepId", "ToCtr"], + ) + .expect("Correct schema definition") + } +} + +impl From for TupleKey { + fn from(pred_rel: PredRel) -> Self { + TupleKey::from_iter([ + pred_rel.from_rep_id, + pred_rel.from_ctr, + pred_rel.to_rep_id, + pred_rel.to_ctr, + ]) + } +} + +impl From for TupleValue { + fn from(pred_rel: PredRel) -> Self { + TupleValue::from_iter([ + pred_rel.from_rep_id, + pred_rel.from_ctr, + pred_rel.to_rep_id, + pred_rel.to_ctr, + ]) + } +} + +/// This function returns test data for an operation history of the MVR CRDT +/// store. The history is as follows. +/// The notation is `set__(, )`. +/// +/// 1. step (just one root operation setting register with key 1 to value 1): +/// +/// ```text +/// set_0_0(1, 1) +/// ``` +/// +/// 2. step (concurrent writes by replica 0 and 1): +/// +/// ```text +/// ---> set_0_1(1, 2) +/// set_0_0(1, 1) +/// ---> set_1_0(1, 3) +/// ``` +/// +/// 3. step (replica 1 does a "merge" operation overwriting the previous +/// conflict): +/// +/// ```text +/// ---> set_0_1(1, 2) +/// set_0_0(1, 1) ---> set_1_2(1, 4) +/// ---> set_1_0(1, 3) +/// ``` +/// +/// 4. step (replica 0 overwrites a not-yet delivered operation): +/// +/// ```text +/// ---> set_0_1(1, 2) +/// set_0_0(1, 1) ---> set_1_2(1, 4) ---> missing ---> set_0_4(1, 6) +/// ---> set_1_0(1, 3) +/// ``` +/// +/// 5. step (replica 0's missing operation arrives): +/// +/// ```text +/// ---> set_0_1(1, 2) +/// set_0_0(1, 1) ---> set_1_2(1, 4) ---> set_0_3(1, 5) ---> set_0_4(1, 6) +/// ---> set_1_0(1, 3) +/// ``` +pub fn mvr_store_operation_history() -> [(Vec, Vec); 5] { + [ + (vec![], vec![SetOp::new(0, 0, 1, 1)]), + ( + vec![PredRel::new(0, 0, 0, 1), PredRel::new(0, 0, 1, 0)], + vec![SetOp::new(0, 1, 1, 2), SetOp::new(1, 0, 1, 3)], + ), + ( + vec![PredRel::new(0, 1, 1, 2), PredRel::new(1, 0, 1, 2)], + vec![SetOp::new(1, 2, 1, 4)], + ), + (vec![PredRel::new(0, 3, 0, 4)], vec![SetOp::new(0, 4, 1, 6)]), + (vec![PredRel::new(1, 2, 0, 3)], vec![SetOp::new(0, 3, 1, 5)]), + ] +} diff --git a/packages/coln-query/src/type_resolver.rs b/packages/coln-query/src/type_resolver.rs new file mode 100644 index 00000000..a333af36 --- /dev/null +++ b/packages/coln-query/src/type_resolver.rs @@ -0,0 +1,286 @@ +use crate::{ + error::SyntaxError, + expr::{ + AliasExpr, AntiJoinExpr, AssignExpr, BinaryExpr, CallExpr, CartesianProductExpr, + DifferenceExpr, DistinctExpr, EquiJoinExpr, Expr, ExprVisitor, FixedPointIterExpr, + FunctionExpr, GroupingExpr, Literal, LiteralExpr, ProjectionExpr, SelectionExpr, UnaryExpr, + UnionExpr, VarExpr, + }, + operator::Operator, + resolver::ScopeStack, + stmt::{BlockStmt, ExprStmt, Stmt, StmtVisitor, VarStmt}, +}; +pub use crate::{function::FunctionType, relation::RelationType, scalar::ScalarType}; +use std::collections::HashMap; + +macro_rules! assert_type { + ($value:expr, $variant:path) => { + match $value { + $variant(inner) => Ok(inner), + _ => Err(SyntaxError::new(format!( + "expected {} type, got: {:?}", + stringify!($variant:path), $value + ))), + } + }; +} + +/// This is "get me the type but don't check it". +#[derive(Default)] +pub struct TypeResolver {} + +impl TypeResolver { + pub fn resolve<'a>( + &mut self, + stmts: impl IntoIterator, + ctx: VisitorCtx, + ) -> Result, SyntaxError> { + // Ensure we have a global scope before resolving. + debug_assert!(ctx.scopes.just_global()); + // We do not call `visit_block` here because the root scope is created + // in the `ScopeStack` constructor and should remain intact across + // multiple calls to `resolve`. + let ret = self.visit_stmts(stmts, ctx); + // Ensure we have a global scope after resolving. + debug_assert!(ctx.scopes.just_global()); + ret + } + pub fn resolve_expr(&mut self, expr: &Expr, ctx: VisitorCtx) -> VisitorResult { + self.visit_expr(expr, ctx) + } + pub fn resolve_stmt(&mut self, stmt: &Stmt, ctx: VisitorCtx) -> VisitorResult { + self.visit_stmt(stmt, ctx) + } + fn visit_stmts<'a>( + &mut self, + stmts: impl IntoIterator, + ctx: VisitorCtx, + ) -> Result, SyntaxError> { + stmts + .into_iter() + .try_fold(None, |_prev, stmt| Ok(Some(self.resolve_stmt(stmt, ctx)?))) + } +} + +type VisitorCtx<'a, 'b> = &'a mut TypeResolverContext<'b>; +type VisitorResult = Result; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum ExprType { + Scalar(ScalarType), + Relation(RelationType), + Function(FunctionType), +} + +pub struct TypeResolverContext<'a> { + scopes: ScopeStack, + tuple_vars: &'a mut HashMap, +} + +impl TypeResolverContext<'_> { + pub fn new<'a>(tuple_vars: &'a mut HashMap) -> TypeResolverContext<'a> { + TypeResolverContext { + scopes: ScopeStack::new(), + tuple_vars, + } + } + fn begin_tuple_context(&mut self, relation_type: RelationType) { + *self.tuple_vars = relation_type.into_tuple_vars(); + } + fn get_type(&self, of: &str) -> Option { + self.tuple_vars + .get(of) + .map(|scalar_type| ExprType::Scalar(*scalar_type)) + .or_else(|| { + for scope in self.scopes.iter() { + if let Some(var_type) = scope.get(of) { + return Some(var_type.clone()); + } + } + None + }) + } + fn end_tuple_context(&mut self) { + self.tuple_vars.clear(); + } +} + +impl TypeResolver { + /// A helper method to deal with projections. + fn visit_projection_attributes( + &mut self, + relation_type: RelationType, + attributes: Option<&Vec<(String, Expr)>>, + ctx: VisitorCtx, + ) -> VisitorResult { + let relation_type = if let Some(attributes) = attributes { + ctx.begin_tuple_context(relation_type); + let relation_type = attributes + .iter() + .map(|(name, expr)| { + self.visit_expr(expr, ctx).and_then(|expr_type| { + Ok((name, assert_type!(expr_type, ExprType::Scalar)?)) + }) + }) + .collect::>()?; + ctx.end_tuple_context(); + relation_type + } else { + relation_type + }; + Ok(ExprType::Relation(relation_type)) + } +} + +impl ExprVisitor> for TypeResolver { + fn visit_binary_expr(&mut self, expr: &BinaryExpr, ctx: VisitorCtx) -> VisitorResult { + self.visit_expr(&expr.left, ctx).and_then(|left_type| { + let right_type = self.visit_expr(&expr.right, ctx)?; + // Maybe don't perform real type checking here, just obtain the type + // from the left expr. + if left_type == right_type { + Ok(left_type) + } else { + Err(SyntaxError::new("Incompatible types")) + } + }) + } + + fn visit_unary_expr(&mut self, expr: &UnaryExpr, ctx: VisitorCtx) -> VisitorResult { + match expr.operator { + Operator::Not => { + // The NOT operator always returns a boolean. + Ok(ExprType::Scalar(ScalarType::Bool)) + } + _ => { + // For other operators, we just return the type of the operand. + self.visit_expr(&expr.operand, ctx) + } + } + } + + fn visit_grouping_expr(&mut self, expr: &GroupingExpr, ctx: VisitorCtx) -> VisitorResult { + self.visit_expr(&expr.expr, ctx) + } + + fn visit_var_expr(&mut self, expr: &VarExpr, ctx: VisitorCtx) -> VisitorResult { + let name = &expr.name; + ctx.get_type(name) + .ok_or_else(|| SyntaxError::new(format!("Variable {name} not found"))) + } + + fn visit_assign_expr(&mut self, expr: &AssignExpr, ctx: VisitorCtx) -> VisitorResult { + // We return the type of the value that is being assigned. + self.visit_expr(&expr.value, ctx) + } + + fn visit_literal_expr(&mut self, expr: &LiteralExpr, ctx: VisitorCtx) -> VisitorResult { + Ok(ExprType::from(&expr.value)) + } + + fn visit_function_expr(&mut self, expr: &FunctionExpr, ctx: VisitorCtx) -> VisitorResult { + // Produces the function's type but this requires a visitor over statements, too. + todo!("Function exprs' type"); + } + + fn visit_call_expr(&mut self, expr: &CallExpr, ctx: VisitorCtx) -> VisitorResult { + // Produces the function's return type. + match self.visit_expr(&expr.callee, ctx)? { + ExprType::Function(function_type) => Ok(*function_type.return_type), + _ => Err(SyntaxError::new("Callee has non-function type")), + } + } + + fn visit_alias_expr(&mut self, expr: &AliasExpr, ctx: VisitorCtx) -> VisitorResult { + self.visit_expr(&expr.relation, ctx) + } + + fn visit_distinct_expr(&mut self, expr: &DistinctExpr, ctx: VisitorCtx) -> VisitorResult { + self.visit_expr(&expr.relation, ctx) + } + + fn visit_union_expr(&mut self, expr: &UnionExpr, ctx: VisitorCtx) -> VisitorResult { + let first = expr + .relations + .first() + .ok_or_else(|| SyntaxError::new("Union expr with only no operands"))?; + self.visit_expr(first, ctx) + } + + fn visit_difference_expr(&mut self, expr: &DifferenceExpr, ctx: VisitorCtx) -> VisitorResult { + // We just take the left's schema, even though we could take right's schema + // equally well. + self.visit_expr(&expr.left, ctx) + } + + fn visit_selection_expr(&mut self, expr: &SelectionExpr, ctx: VisitorCtx) -> VisitorResult { + // Selections don't alter the schema. + self.visit_expr(&expr.relation, ctx) + } + + fn visit_projection_expr(&mut self, expr: &ProjectionExpr, ctx: VisitorCtx) -> VisitorResult { + let relation_type = self + .visit_expr(&expr.relation, ctx) + .and_then(|expr_type| assert_type!(expr_type, ExprType::Relation))?; + self.visit_projection_attributes(relation_type, Some(&expr.attributes), ctx) + } + + fn visit_cartesian_product_expr( + &mut self, + expr: &CartesianProductExpr, + ctx: VisitorCtx, + ) -> VisitorResult { + self.visit_equi_join_expr(&expr.inner, ctx) + } + + fn visit_equi_join_expr(&mut self, expr: &EquiJoinExpr, ctx: VisitorCtx) -> VisitorResult { + let left_relation_type = self + .visit_expr(&expr.left, ctx) + .and_then(|expr_type| assert_type!(expr_type, ExprType::Relation))?; + let right_relation_type = self + .visit_expr(&expr.right, ctx) + .and_then(|expr_type| assert_type!(expr_type, ExprType::Relation))?; + let joined = left_relation_type.join(right_relation_type); + self.visit_projection_attributes(joined, expr.attributes.as_ref(), ctx) + } + + fn visit_anti_join_expr(&mut self, expr: &AntiJoinExpr, ctx: VisitorCtx) -> VisitorResult { + self.visit_expr(&expr.left, ctx) + } + + fn visit_fixed_point_iter_expr( + &mut self, + expr: &FixedPointIterExpr, + ctx: VisitorCtx, + ) -> VisitorResult { + // We steal the relation's type from the accumulator's type. + self.visit_expr(&expr.accumulator.1, ctx) + } +} + +impl From<&Literal> for ExprType { + fn from(value: &Literal) -> Self { + match value { + Literal::String(_) => Self::Scalar(ScalarType::String), + Literal::Iint(_) => Self::Scalar(ScalarType::Iint), + Literal::Uint(_) => Self::Scalar(ScalarType::Uint), + Literal::Bool(_) => Self::Scalar(ScalarType::Bool), + Literal::Null(_) => Self::Scalar(ScalarType::Null), + Literal::Relation(relation) => Self::Relation(RelationType::from(&relation.schema)), + } + } +} + +impl StmtVisitor> for TypeResolver { + fn visit_var_stmt(&mut self, stmt: &VarStmt, ctx: VisitorCtx) -> VisitorResult { + todo!(); + } + + fn visit_expr_stmt(&mut self, stmt: &ExprStmt, ctx: VisitorCtx) -> VisitorResult { + todo!(); + } + + fn visit_block_stmt(&mut self, stmt: &BlockStmt, ctx: VisitorCtx) -> VisitorResult { + todo!(); + } +} diff --git a/packages/coln-query/src/util.rs b/packages/coln-query/src/util.rs new file mode 100644 index 00000000..17adcba4 --- /dev/null +++ b/packages/coln-query/src/util.rs @@ -0,0 +1,47 @@ +use crate::variable::VariableSlot; + +pub trait MemAddr { + /// Make sure that the value is not moved in memory! + fn mem_addr(&self) -> usize { + self as *const Self as *const () as usize + } +} + +pub trait Resolvable { + fn set_resolved(&mut self, resolved: VariableSlot); +} + +pub trait Named { + fn name(&self) -> &str; +} + +/// An AST node identifier. +/// Can be its address in memory if using a pointer-based AST +/// or its index if using a flattened AST. +#[derive(Eq, PartialEq, Hash, Clone, Copy, Debug)] +pub struct NodeRef(usize); + +impl From for NodeRef { + fn from(index: usize) -> Self { + Self(index) + } +} + +impl From<&T> for NodeRef { + fn from(addr: &T) -> Self { + Self(addr.mem_addr()) + } +} + +#[macro_export] +macro_rules! impl_from_auto_box { + ($enum:ty, $(($variant:path, $expr:ty)),*) => { + $( + impl From<$expr> for $enum { + fn from(value: $expr) -> Self { + $variant(Box::new(value)) + } + } + )* + } +} diff --git a/packages/coln-query/src/variable.rs b/packages/coln-query/src/variable.rs new file mode 100644 index 00000000..3f7e8ed7 --- /dev/null +++ b/packages/coln-query/src/variable.rs @@ -0,0 +1,180 @@ +use crate::{ + expr::Literal, function::FunctionRef, relation::RelationRef, scalar::ScalarTypedValue, +}; +use std::{ + cell::{Ref, RefCell}, + fmt, + rc::Rc, +}; + +/// The value of a variable of the [`Interpreter`](crate::interpreter::Interpreter) +/// at runtime. +/// Compared to [`ScalarTypedValue`], this type allows +/// [functions](`FunctionRef`) and [relations](`RelationRef`), too. +#[derive(Clone, Debug)] +pub enum Value { + /// String. + // TODO: maybe make strings reference counted to avoid cloning costs? + String(String), + /// Unsigned integer value of 64 bits. + Uint(u64), + /// Signed integer value of 64 bits. + Iint(i64), + /// Boolean. + Bool(bool), + /// Character. + Char(char), + /// Null. + // The `Null` variant carries the unit type to align its field-arity with + // other variants. That eases the definition of macros operating on the enum. + Null(()), + /// Function. + Function(FunctionRef), + /// Relation. + Relation(RelationRef), +} + +impl Eq for Value {} + +impl PartialEq for Value { + fn eq(&self, other: &Self) -> bool { + match (self, other) { + (Value::String(a), Value::String(b)) => a == b, + (Value::Uint(a), Value::Uint(b)) => a == b, + (Value::Iint(a), Value::Iint(b)) => a == b, + (Value::Bool(a), Value::Bool(b)) => a == b, + (Value::Null(()), Value::Null(())) => true, + (Value::Function(a), Value::Function(b)) => Rc::ptr_eq(a, b), + (Value::Relation(a), Value::Relation(b)) => Rc::ptr_eq(a, b), + _ => false, + } + } +} + +impl Default for Value { + fn default() -> Self { + Value::Null(()) + } +} + +impl From for Value { + fn from(value: RelationRef) -> Self { + Value::Relation(value) + } +} + +impl From for Value { + fn from(value: ScalarTypedValue) -> Self { + match value { + ScalarTypedValue::String(value) => Value::String(value), + ScalarTypedValue::Uint(value) => Value::Uint(value), + ScalarTypedValue::Iint(value) => Value::Iint(value), + ScalarTypedValue::Bool(value) => Value::Bool(value), + ScalarTypedValue::Char(value) => Value::Char(value), + ScalarTypedValue::Null(()) => Value::Null(()), + } + } +} + +impl From for Value { + fn from(literal: Literal) -> Self { + match literal { + Literal::String(value) => Value::String(value), + Literal::Uint(value) => Value::Uint(value), + Literal::Iint(value) => Value::Iint(value), + Literal::Bool(value) => Value::Bool(value), + Literal::Null(()) => Value::Null(()), + Literal::Relation(value) => Value::Relation(Rc::new(RefCell::new(value))), + } + } +} + +impl fmt::Display for Value { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Value::String(value) => write!(f, "{value}"), + Value::Uint(value) => write!(f, "{value}"), + Value::Iint(value) => write!(f, "{value}"), + Value::Bool(value) => write!(f, "{value}"), + Value::Char(value) => write!(f, "{value}"), + Value::Null(()) => write!(f, "null"), + Value::Function(function) => write!(f, "{}", function.borrow()), + Value::Relation(relation) => write!(f, "{}", relation.borrow()), + } + } +} + +/// First entry is the scope, second entry is the variable within that scope. +pub type VariableSlot = (usize, usize); + +/// A scope in the environment. Roughly, the space between two curly braces `{}`. +#[derive(Clone, Debug)] +struct Scope { + /// Variable slots of an environment. + inner: Rc>>, +} + +impl Scope { + fn new() -> Self { + Self { + inner: Rc::new(RefCell::new(Vec::new())), + } + } + fn define_var(&mut self, val: Value) { + self.inner.borrow_mut().push(val); + } + fn assign_var(&mut self, slot_idx: usize, val: Value) { + self.inner.borrow_mut()[slot_idx] = val; + } + fn lookup_var(&self, slot_idx: usize) -> Ref<'_, Value> { + let vec = self.inner.borrow(); + Ref::map(vec, |vec| &vec[slot_idx]) + } +} + +pub const SCOPES_CAPACITY: usize = 8; + +/// The environment of the interpreter. It holds the variables. +#[derive(Clone, Debug)] +pub struct Environment { + /// The vector models a stack of scopes with the root environment at + /// the bottom and the innermost scope at the top. + scopes: Vec, +} + +impl Default for Environment { + fn default() -> Self { + let mut environment = Self { + scopes: Vec::with_capacity(SCOPES_CAPACITY), + }; + // Create the global scope. + environment.begin_scope(); + environment + } +} + +impl Environment { + pub fn just_global(&self) -> bool { + self.scopes.len() == 1 + } + pub fn begin_scope(&mut self) { + self.scopes.push(Scope::new()); + } + pub fn end_scope(&mut self) { + self.scopes.pop(); + } + pub fn define_var>(&mut self, val: T) { + self.scopes + .last_mut() + .expect("no root env") + .define_var(val.into()); + } + pub fn assign_var(&mut self, at: &VariableSlot, val: Value) { + let (scope_idx, slot_idx) = *at; + self.scopes[scope_idx].assign_var(slot_idx, val); + } + pub fn lookup_var(&self, at: &VariableSlot) -> Ref<'_, Value> { + let (scope_idx, slot_idx) = *at; + self.scopes[scope_idx].lookup_var(slot_idx) + } +} diff --git a/packages/coln-store/Cargo.toml b/packages/coln-store/Cargo.toml index 4b8a38d7..afe1015e 100644 --- a/packages/coln-store/Cargo.toml +++ b/packages/coln-store/Cargo.toml @@ -6,6 +6,7 @@ edition = "2024" [dependencies] blake3 = "1.8.5" coln-lang-rs = { path = "../coln-lang-rs", features = ["serde"] } +coln-query = { path = "../coln-query" } hex = "0.4.3" hexane = { git = "https://github.com/automerge/automerge", branch = "hexane_v1", package = "hexane" } leb128 = "0.2.6" From 68567f1ed41aa3a343d8dee558710250e367f0c2 Mon Sep 17 00:00:00 2001 From: Leo Stewen Date: Fri, 12 Jun 2026 11:30:53 +0100 Subject: [PATCH 2/8] Add more shared deps --- Cargo.toml | 6 +++++- packages/coln-query/Cargo.toml | 20 ++++---------------- packages/coln-store/Cargo.toml | 8 ++++---- 3 files changed, 13 insertions(+), 21 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 78d7fcfa..09651fc4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,5 +16,9 @@ repository = "https://github.com/coln-project/Coln" # Useful if we want to align on one version of a shared dependency. [workspace.dependencies] anyhow = "1.0.102" -thiserror = "2.0.18" criterion = "0.8.2" +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0.149" +thiserror = "2.0.18" +tracing = "0.1" +tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt"] } diff --git a/packages/coln-query/Cargo.toml b/packages/coln-query/Cargo.toml index 2cd59cd0..ae8c9745 100644 --- a/packages/coln-query/Cargo.toml +++ b/packages/coln-query/Cargo.toml @@ -8,23 +8,11 @@ test = [] [dependencies] anyhow = { workspace = true } -thiserror = { workspace = true } -dbsp = "0.305.0" -rkyv = { version = "0.7.46", default-features = false, features = [ - "std", - "size_64", - "validation", - "uuid", -] } -size-of = { version = "0.1.7", package = "feldera-size-of", features = [ - "hashbrown", - "time-std", - "xxhash-xxh3", - "arcstr", - "chrono", - "ordered-float", -] } cli-table = "0.4.9" +dbsp = "0.305.0" +rkyv = { version = "0.7.46", default-features = false, features = [ "std", "size_64", "validation", "uuid", ] } +size-of = { version = "0.1.7", package = "feldera-size-of", features = [ "hashbrown", "time-std", "xxhash-xxh3", "arcstr", "chrono", "ordered-float", ] } +thiserror = { workspace = true } [dev-dependencies] criterion = { workspace = true } diff --git a/packages/coln-store/Cargo.toml b/packages/coln-store/Cargo.toml index afe1015e..08f07ae8 100644 --- a/packages/coln-store/Cargo.toml +++ b/packages/coln-store/Cargo.toml @@ -12,8 +12,8 @@ hexane = { git = "https://github.com/automerge/automerge", branch = "hexane_v1", leb128 = "0.2.6" postcard = { version = "1.1.3", features = ["alloc"] } rustyline = "18.0.0" -serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0.149" +serde = { workspace = true } +serde_json = { workspace = true } shlex = "2.0.1" -tracing = "0.1" -tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt"] } +tracing = { workspace = true } +tracing-subscriber = { workspace = true } From 6e68995a83ee7742fdb00c2ad06838d29f619c24 Mon Sep 17 00:00:00 2001 From: Leo Stewen Date: Wed, 17 Jun 2026 12:11:14 +0100 Subject: [PATCH 3/8] Demonstrate mutual recursion with DBSP by using a static program analysis example --- packages/coln-query/points_to_step_1.dl | 118 +++++++ packages/coln-query/points_to_step_2.dl | 130 ++++++++ packages/coln-query/points_to_step_3.dl | 120 +++++++ packages/coln-query/src/dbsp.rs | 406 +++++++++++++++++++++++- 4 files changed, 766 insertions(+), 8 deletions(-) create mode 100644 packages/coln-query/points_to_step_1.dl create mode 100644 packages/coln-query/points_to_step_2.dl create mode 100644 packages/coln-query/points_to_step_3.dl diff --git a/packages/coln-query/points_to_step_1.dl b/packages/coln-query/points_to_step_1.dl new file mode 100644 index 00000000..7972d91b --- /dev/null +++ b/packages/coln-query/points_to_step_1.dl @@ -0,0 +1,118 @@ +//============================================================================ +// points_to.dl +// +// A miniature points-to + call-graph analysis for an OO language, written to +// show MUTUAL RECURSION in Datalog: +// +// VarPointsTo depends on CallGraph (args flow into callee parameters) +// CallGraph depends on VarPointsTo (virtual dispatch needs receiver type) +// +// Neither can be computed first; Souffle solves them together to a fixpoint. +// This is "on-the-fly call graph construction" (cf. the Doop framework). +// +// Run: souffle points_to_step_1.dl +//============================================================================ + +//--------------------------- Input relations (EDB) -------------------------- +.decl Alloc(var:symbol, obj:symbol) // var = new ... (obj = alloc site) +.decl Assign(dst:symbol, src:symbol) // dst = src +.decl VirtualCall(site:symbol, recv:symbol, sig:symbol) // recv.sig(...) at `site` +.decl HeapType(obj:symbol, ty:symbol) // runtime type of an allocated object +.decl Dispatch(ty:symbol, sig:symbol, meth:symbol) // type + signature -> method +.decl ActualArg(site:symbol, arg:symbol) // argument variable passed at a call +.decl FormalParam(meth:symbol, param:symbol) // a method's parameter variable + +//------------------------- Derived relations (IDB) -------------------------- +.decl VarPointsTo(var:symbol, obj:symbol) +.decl CallGraph(site:symbol, meth:symbol) +.output VarPointsTo(IO=stdout) +.output CallGraph(IO=stdout) + +//--------------------------------- Rules ------------------------------------ + +// A variable points to whatever it was directly allocated. +VarPointsTo(V, Obj) :- Alloc(V, Obj). + +// Copies propagate points-to facts (transitively). +VarPointsTo(Dst, Obj) :- + Assign(Dst, Src), + VarPointsTo(Src, Obj). + +// Resolve a virtual call: look at what the receiver points to, take that +// object's type, and dispatch the signature on that type. +// --> this rule READS VarPointsTo. +CallGraph(Site, Meth) :- + VirtualCall(Site, Recv, Sig), + VarPointsTo(Recv, Obj), + HeapType(Obj, Ty), + Dispatch(Ty, Sig, Meth). + +// Once a call edge exists, the actual arguments flow into the callee's +// formal parameters. +// --> this rule READS CallGraph and WRITES VarPointsTo, closing the loop. +VarPointsTo(Param, Obj) :- + CallGraph(Site, Meth), + ActualArg(Site, Arg), + FormalParam(Meth, Param), + VarPointsTo(Arg, Obj). + +//================================ Program =================================== +// The "source program" being analysed, encoded as facts: +// +// class Greeter { void greet(Animal x) { x.speak(); } } // s3 +// class Dog extends Animal { void speak() {...} } +// class Cat extends Animal { void speak() {...} } +// +// void main() { +// Greeter g = new Greeter(); // oG +// Dog d = new Dog(); // oDog +// g.greet(d); // s1 +// Cat c = new Cat(); // oCat +// Animal ac = c; // alias (exercises the Assign rule) +// g.greet(ac); // s2 +// } +//============================================================================ + +Alloc("g", "oG"). +Alloc("d", "oDog"). +Alloc("c", "oCat"). + +Assign("ac", "c"). + +VirtualCall("s1", "g", "greet"). +VirtualCall("s2", "g", "greet"). +VirtualCall("s3", "x", "speak"). + +HeapType("oG", "Greeter"). +HeapType("oDog", "Dog"). +HeapType("oCat", "Cat"). + +Dispatch("Greeter", "greet", "Greeter.greet"). +Dispatch("Dog", "speak", "Dog.speak"). +Dispatch("Cat", "speak", "Cat.speak"). + +ActualArg("s1", "d"). +ActualArg("s2", "ac"). + +FormalParam("Greeter.greet", "x"). + +//============================ Expected output =============================== +// VarPointsTo +// g oG +// d oDog +// c oCat +// ac oCat +// x oDog +// x oCat +// +// CallGraph +// s1 Greeter.greet +// s2 Greeter.greet +// s3 Dog.speak +// s3 Cat.speak +// +// The payoff: s3 (x.speak()) resolves to BOTH Dog.speak and Cat.speak only +// because x received oDog and oCat through the call edges of s1/s2 -- which +// themselves required knowing what g points to. CallGraph and VarPointsTo +// feed each other until the fixpoint; staging them separately can't do this. +//============================================================================ diff --git a/packages/coln-query/points_to_step_2.dl b/packages/coln-query/points_to_step_2.dl new file mode 100644 index 00000000..3230f8ec --- /dev/null +++ b/packages/coln-query/points_to_step_2.dl @@ -0,0 +1,130 @@ +//============================================================================ +// points_to.dl +// +// A miniature points-to + call-graph analysis for an OO language, written to +// show MUTUAL RECURSION in Datalog: +// +// VarPointsTo depends on CallGraph (args flow into callee parameters) +// CallGraph depends on VarPointsTo (virtual dispatch needs receiver type) +// +// Neither can be computed first; Souffle solves them together to a fixpoint. +// This is "on-the-fly call graph construction" (cf. the Doop framework). +// +// Run: souffle points_to_step_2.dl +//============================================================================ + +//--------------------------- Input relations (EDB) -------------------------- +.decl Alloc(var:symbol, obj:symbol) // var = new ... (obj = alloc site) +.decl Assign(dst:symbol, src:symbol) // dst = src +.decl VirtualCall(site:symbol, recv:symbol, sig:symbol) // recv.sig(...) at `site` +.decl HeapType(obj:symbol, ty:symbol) // runtime type of an allocated object +.decl Dispatch(ty:symbol, sig:symbol, meth:symbol) // type + signature -> method +.decl ActualArg(site:symbol, arg:symbol) // argument variable passed at a call +.decl FormalParam(meth:symbol, param:symbol) // a method's parameter variable + +//------------------------- Derived relations (IDB) -------------------------- +.decl VarPointsTo(var:symbol, obj:symbol) +.decl CallGraph(site:symbol, meth:symbol) +.output VarPointsTo(IO=stdout) +.output CallGraph(IO=stdout) + +//--------------------------------- Rules ------------------------------------ + +// A variable points to whatever it was directly allocated. +VarPointsTo(V, Obj) :- Alloc(V, Obj). + +// Copies propagate points-to facts (transitively). +VarPointsTo(Dst, Obj) :- + Assign(Dst, Src), + VarPointsTo(Src, Obj). + +// Resolve a virtual call: look at what the receiver points to, take that +// object's type, and dispatch the signature on that type. +// --> this rule READS VarPointsTo. +CallGraph(Site, Meth) :- + VirtualCall(Site, Recv, Sig), + VarPointsTo(Recv, Obj), + HeapType(Obj, Ty), + Dispatch(Ty, Sig, Meth). + +// Once a call edge exists, the actual arguments flow into the callee's +// formal parameters. +// --> this rule READS CallGraph and WRITES VarPointsTo, closing the loop. +VarPointsTo(Param, Obj) :- + CallGraph(Site, Meth), + ActualArg(Site, Arg), + FormalParam(Meth, Param), + VarPointsTo(Arg, Obj). + +//================================ Program =================================== +// The "source program" being analysed, encoded as facts: +// +// class Greeter { void greet(Animal x) { x.speak(); } } // s3 +// class Dog extends Animal { void speak() {...} } +// class Cat extends Animal { void speak() {...} } +// class Mouse extends Animal { void speak() {...} } +// +// void main() { +// Greeter g = new Greeter(); // oG +// Dog d = new Dog(); // oDog +// g.greet(d); // s1 +// Cat c = new Cat(); // oCat +// Animal ac = c; // alias (exercises the Assign rule) +// g.greet(ac); // s2 +// Mouse m = new Mouse() // oMouse +// g.greet(m); // s4 +// } +//============================================================================ + +Alloc("g", "oG"). +Alloc("d", "oDog"). +Alloc("c", "oCat"). +Alloc("m", "oMouse"). + +Assign("ac", "c"). + +VirtualCall("s1", "g", "greet"). +VirtualCall("s2", "g", "greet"). +VirtualCall("s3", "x", "speak"). +VirtualCall("s4", "g", "greet"). + +HeapType("oG", "Greeter"). +HeapType("oDog", "Dog"). +HeapType("oCat", "Cat"). +HeapType("oMouse", "Mouse"). + +Dispatch("Greeter", "greet", "Greeter.greet"). +Dispatch("Dog", "speak", "Dog.speak"). +Dispatch("Cat", "speak", "Cat.speak"). +Dispatch("Mouse", "speak", "Mouse.speak"). + +ActualArg("s1", "d"). +ActualArg("s2", "ac"). +ActualArg("s4", "m"). + +FormalParam("Greeter.greet", "x"). + +//============================ Expected output =============================== +// VarPointsTo +// g oG +// d oDog +// c oCat +// ac oCat +// x oDog +// x oCat +// m oMouse +// x oMouse +// +// CallGraph +// s1 Greeter.greet +// s2 Greeter.greet +// s3 Dog.speak +// s3 Cat.speak +// s3 Mouse.speak +// s4 Greeter.greet +// +// The payoff: s3 (x.speak()) resolves to BOTH Dog.speak and Cat.speak only +// because x received oDog and oCat through the call edges of s1/s2 -- which +// themselves required knowing what g points to. CallGraph and VarPointsTo +// feed each other until the fixpoint; staging them separately can't do this. +//============================================================================ diff --git a/packages/coln-query/points_to_step_3.dl b/packages/coln-query/points_to_step_3.dl new file mode 100644 index 00000000..ec0bcfbf --- /dev/null +++ b/packages/coln-query/points_to_step_3.dl @@ -0,0 +1,120 @@ +//============================================================================ +// points_to.dl +// +// A miniature points-to + call-graph analysis for an OO language, written to +// show MUTUAL RECURSION in Datalog: +// +// VarPointsTo depends on CallGraph (args flow into callee parameters) +// CallGraph depends on VarPointsTo (virtual dispatch needs receiver type) +// +// Neither can be computed first; Souffle solves them together to a fixpoint. +// This is "on-the-fly call graph construction" (cf. the Doop framework). +// +// Run: souffle points_to_step_3.dl +//============================================================================ + +//--------------------------- Input relations (EDB) -------------------------- +.decl Alloc(var:symbol, obj:symbol) // var = new ... (obj = alloc site) +.decl Assign(dst:symbol, src:symbol) // dst = src +.decl VirtualCall(site:symbol, recv:symbol, sig:symbol) // recv.sig(...) at `site` +.decl HeapType(obj:symbol, ty:symbol) // runtime type of an allocated object +.decl Dispatch(ty:symbol, sig:symbol, meth:symbol) // type + signature -> method +.decl ActualArg(site:symbol, arg:symbol) // argument variable passed at a call +.decl FormalParam(meth:symbol, param:symbol) // a method's parameter variable + +//------------------------- Derived relations (IDB) -------------------------- +.decl VarPointsTo(var:symbol, obj:symbol) +.decl CallGraph(site:symbol, meth:symbol) +.output VarPointsTo(IO=stdout) +.output CallGraph(IO=stdout) + +//--------------------------------- Rules ------------------------------------ + +// A variable points to whatever it was directly allocated. +VarPointsTo(V, Obj) :- Alloc(V, Obj). + +// Copies propagate points-to facts (transitively). +VarPointsTo(Dst, Obj) :- + Assign(Dst, Src), + VarPointsTo(Src, Obj). + +// Resolve a virtual call: look at what the receiver points to, take that +// object's type, and dispatch the signature on that type. +// --> this rule READS VarPointsTo. +CallGraph(Site, Meth) :- + VirtualCall(Site, Recv, Sig), + VarPointsTo(Recv, Obj), + HeapType(Obj, Ty), + Dispatch(Ty, Sig, Meth). + +// Once a call edge exists, the actual arguments flow into the callee's +// formal parameters. +// --> this rule READS CallGraph and WRITES VarPointsTo, closing the loop. +VarPointsTo(Param, Obj) :- + CallGraph(Site, Meth), + ActualArg(Site, Arg), + FormalParam(Meth, Param), + VarPointsTo(Arg, Obj). + +//================================ Program =================================== +// The "source program" being analysed, encoded as facts: +// +// class Greeter { void greet(Animal x) { x.speak(); } } // s3 +// class Dog extends Animal { void speak() {...} } +// class Cat extends Animal { void speak() {...} } +// class Mouse extends Animal { void speak() {...} } +// +// void main() { +// Greeter g = new Greeter(); // oG +// Dog d = new Dog(); // oDog +// g.greet(d); // s1 +// Cat c = new Cat(); // oCat +// Mouse m = new Mouse() // oMouse +// g.greet(m); // s4 +// } +//============================================================================ + +Alloc("g", "oG"). +Alloc("d", "oDog"). +Alloc("c", "oCat"). +Alloc("m", "oMouse"). + +VirtualCall("s1", "g", "greet"). +VirtualCall("s3", "x", "speak"). +VirtualCall("s4", "g", "greet"). + +HeapType("oG", "Greeter"). +HeapType("oDog", "Dog"). +HeapType("oCat", "Cat"). +HeapType("oMouse", "Mouse"). + +Dispatch("Greeter", "greet", "Greeter.greet"). +Dispatch("Dog", "speak", "Dog.speak"). +Dispatch("Cat", "speak", "Cat.speak"). +Dispatch("Mouse", "speak", "Mouse.speak"). + +ActualArg("s1", "d"). +ActualArg("s4", "m"). + +FormalParam("Greeter.greet", "x"). + +//============================ Expected output =============================== +// VarPointsTo +// g oG +// d oDog +// c oCat +// x oDog +// m oMouse +// x oMouse +// +// CallGraph +// s1 Greeter.greet +// s3 Dog.speak +// s3 Mouse.speak +// s4 Greeter.greet +// +// The payoff: s3 (x.speak()) resolves to BOTH Dog.speak and Cat.speak only +// because x received oDog and oCat through the call edges of s1/s2 -- which +// themselves required knowing what g points to. CallGraph and VarPointsTo +// feed each other until the fixpoint; staging them separately can't do this. +//============================================================================ diff --git a/packages/coln-query/src/dbsp.rs b/packages/coln-query/src/dbsp.rs index 0918f6d7..ca482235 100644 --- a/packages/coln-query/src/dbsp.rs +++ b/packages/coln-query/src/dbsp.rs @@ -313,12 +313,7 @@ impl DbspOutput { Self { schema, handle } } pub fn to_batch(&self) -> DbspOutputBatch<'_> { - let inner = self - .handle - .concat() - .consolidate() - .iter() - .collect::>(); + let inner = self.handle.concat().iter().collect::>(); DbspOutputBatch { schema: &self.schema, inner, @@ -444,6 +439,68 @@ mod test { std::thread::available_parallelism().unwrap_or(NonZeroUsize::new(4).unwrap()) } + mod test_utils { + use std::io::{self, IsTerminal, Write}; + + /// Iterator adapter that prints the step count and waits for a keypress + /// before yielding each item if in interactive mode. + pub struct Confirm { + inner: I, + count: usize, + interactive: bool, + } + + impl Iterator for Confirm { + type Item = I::Item; + + fn next(&mut self) -> Option { + if !self.interactive { + return self.inner.next(); + } + if self.count > 0 { + print!("Press Enter to continue (Ctrl-D to stop)... "); + io::stdout().flush().ok(); + let mut line = String::new(); + match io::stdin().read_line(&mut line) { + Ok(0) | Err(_) => return None, // EOF / Ctrl-D -> stop + Ok(_) => {} + }; + } + match self.inner.next() { + Some(item) => { + self.count += 1; + println!("=== Iteration {:03} ===", self.count); + Some(item) + } + None => None, + } + } + } + + /// Whether we're in an interactive `cargo test -- --nocapture` run on a real TTY. + fn confirmation_enabled() -> bool { + let nocapture = std::env::args().any(|a| a == "--nocapture") + || std::env::var_os("RUST_TEST_NOCAPTURE").is_some(); + nocapture && io::stdin().is_terminal() + } + + /// Blanket extension trait: available on every `Iterator`. + pub trait ConfirmExt: Iterator + Sized { + fn confirm_each(self) -> Confirm { + Confirm { + inner: self, + count: 0, + interactive: confirmation_enabled(), + } + } + } + + // Blanket implementation. + impl ConfirmExt for I {} + } + + use test_utils::ConfirmExt; + /// Note that this example only works with acyclic graphs. #[test] fn test_transitive_closure() -> Result<(), anyhow::Error> { @@ -487,7 +544,7 @@ mod test { }, )?; - Ok(((edges_input), closure.accumulate_output())) + Ok((edges_input, closure.accumulate_output())) }, )?; @@ -642,6 +699,340 @@ mod test { Ok(()) } + #[test] + fn test_mutual_recursion() -> Result<(), anyhow::Error> { + const STEPS: usize = 3; + + let ( + mut circuit, + ( + ( + alloc_input, + assign_input, + virtual_call_input, + heap_type_input, + dispatch_input, + actual_arg_input, + formal_param_input, + ), + (var_points_to_output, call_graph_output), + ), + ) = Runtime::init_circuit(worker_threads(), move |root_circuit| { + let (alloc, alloc_input) = root_circuit.add_input_zset::(); + let (assign, assign_input) = root_circuit.add_input_zset::(); + let (virtual_call, virtual_call_input) = root_circuit.add_input_zset::(); + let (heap_type, heap_type_input) = root_circuit.add_input_zset::(); + let (dispatch, dispatch_input) = root_circuit.add_input_zset::(); + let (actual_arg, actual_arg_input) = root_circuit.add_input_zset::(); + let (formal_param, formal_param_input) = root_circuit.add_input_zset::(); + + let (var_points_to, call_graph) = root_circuit.recursive( + |child_circuit, + (var_points_to, call_graph): ( + // TODO: Change to OrdIndexedZSet + Stream<_, OrdZSet>, + Stream<_, OrdZSet>, + )| { + // Import streams from the parent circuit into the child circuit. + let alloc = alloc.delta0(child_circuit); + let assign = assign.delta0(child_circuit); + let virtual_call = virtual_call.delta0(child_circuit); + let heap_type = heap_type.delta0(child_circuit); + let dispatch = dispatch.delta0(child_circuit); + let actual_arg = actual_arg.delta0(child_circuit); + let formal_param = formal_param.delta0(child_circuit); + + let call_graph_next = + virtual_call // 1. virtual_call JOIN var_points_to ON recv + .map_index(|Tup3(site, recv, sig)| { + (recv.clone(), (site.clone(), recv.clone(), sig.clone())) + }) + .join_index( + &var_points_to.map_index(|Tup2(recv, obj)| { + (recv.clone(), (recv.clone(), obj.clone())) + }), + |_recv, (site, _, sig), (_, obj)| { + Some(( + obj.clone(), + Tup3(site.clone(), sig.clone(), obj.clone()), + )) + }, + ) + .join_index( + // 2. ... JOIN heap_type ON obj + &heap_type.map_index(|Tup2(obj, ty)| { + (obj.clone(), (obj.clone(), ty.clone())) + }), + |_obj, Tup3(site, sig, _), (_, ty)| { + Some(((ty.clone(), sig.clone()), (site.clone(), ty.clone()))) + }, + ) // 3. ... JOIN dispatch ON ty and sig + .join_index( + &dispatch.map_index(|Tup3(ty, sig, meth)| { + ((ty.clone(), sig.clone()), meth.clone()) + }), + |_, (site, _), meth| { + Some(( + (site.clone(), meth.clone()), + Tup2(site.clone(), meth.clone()), + )) + }, + ); + + let var_points_to_next = var_points_to + .map_index(|Tup2(src, obj)| (src.clone(), (src.clone(), obj.clone()))) + .join_index( + &assign.map_index(|Tup2(dst, src)| { + (src.clone(), (dst.clone(), src.clone())) + }), + |_src, (_, obj), (dst, _)| { + Some(((dst.clone(), obj.clone()), (dst.clone(), obj.clone()))) + }, + ) + .plus(&alloc.map_index(|Tup2(var, obj)| { + ((var.clone(), obj.clone()), (var.clone(), obj.clone())) + })) + .plus( + &call_graph // 1. call_graph JOIN actual_arg ON site + .map_index(|Tup2(site, meth)| { + (site.clone(), (site.clone(), meth.clone())) + }) + .join_index( + &actual_arg.map_index(|Tup2(site, arg)| { + (site.clone(), (site.clone(), arg.clone())) + }), + |_site, (_, meth), (_, arg)| { + Some((meth.clone(), (meth.clone(), arg.clone()))) + }, + ) // .2. ... JOIN formal_param ON meth + .join_index( + &formal_param.map_index(|Tup2(meth, param)| { + (meth.clone(), (meth.clone(), param.clone())) + }), + |_meth, (_, arg), (_, param)| { + Some(((arg.clone()), (arg.clone(), param.clone()))) + }, + ) // 3. ... JOIN var_points_to ON arg + .join_index( + &var_points_to.map_index(|Tup2(arg, obj)| { + (arg.clone(), (arg.clone(), obj.clone())) + }), + |_arg, (_, param), (_, obj)| { + Some(( + (param.clone(), obj.clone()), + (param.clone(), obj.clone()), + )) + }, + ), + ); + + Ok(( + var_points_to_next + .map(|(_, (param, obj))| Tup2(param.clone(), obj.clone())), + call_graph_next.map(|((site, meth), _)| Tup2(site.clone(), meth.clone())), + )) + }, + )?; + + Ok(( + ( + alloc_input, + assign_input, + virtual_call_input, + heap_type_input, + dispatch_input, + actual_arg_input, + formal_param_input, + ), + ( + var_points_to.accumulate_output(), + call_graph.accumulate_output(), + ), + )) + })?; + + // INPUTS + + type Input = Vec>; + type Str2 = (&'static str, &'static str); + type String2 = Tup2; + type Str3 = (&'static str, &'static str, &'static str); + type String3 = Tup3; + + trait CustomInto { + fn into_input(self) -> T; + } + + impl CustomInto for Str2 { + fn into_input(self) -> String2 { + Tup2(self.0.to_string(), self.1.to_string()) + } + } + + impl CustomInto for Str3 { + fn into_input(self) -> String3 { + Tup3(self.0.to_string(), self.1.to_string(), self.2.to_string()) + } + } + + impl CustomInto>> for T + where + Raw: CustomInto, + T: IntoIterator>, + { + fn into_input(self) -> Vec> { + self.into_iter() + .map(|iteration| { + iteration + .into_iter() + .map(|(data, z_weight)| Tup2(data.into_input(), z_weight)) + .collect() + }) + .collect() + } + } + + let mut alloc_inputs = ([ + vec![(("g", "oG"), 1), (("d", "oDog"), 1), (("c", "oCat"), 1)], + vec![(("m", "oMouse"), 1)], + vec![], + ] as [Vec<(Str2, ZWeight)>; STEPS]) + .into_input() + .into_iter(); + + let mut assign_inputs = ([vec![(("ac", "c"), 1)], vec![], vec![(("ac", "c"), -1)]] + as [Vec<(Str2, ZWeight)>; STEPS]) + .into_input() + .into_iter(); + + let mut virtual_call_inputs = ([ + vec![ + (("s1", "g", "greet"), 1), + (("s2", "g", "greet"), 1), + (("s3", "x", "speak"), 1), + ], + vec![(("s4", "g", "greet"), 1)], + vec![(("s2", "g", "greet"), -1)], + ] as [Vec<(Str3, ZWeight)>; STEPS]) + .into_input() + .into_iter(); + + let mut heap_type_inputs = ([ + vec![ + (("oG", "Greeter"), 1), + (("oDog", "Dog"), 1), + (("oCat", "Cat"), 1), + ], + vec![(("oMouse", "Mouse"), 1)], + vec![], + ] as [Vec<(Str2, ZWeight)>; STEPS]) + .into_input() + .into_iter(); + + let mut dispatch_inputs = ([ + vec![ + (("Greeter", "greet", "Greeter.greet"), 1), + (("Dog", "speak", "Dog.speak"), 1), + (("Cat", "speak", "Cat.speak"), 1), + ], + vec![(("Mouse", "speak", "Mouse.speak"), 1)], + vec![], + ] as [Vec<(Str3, ZWeight)>; STEPS]) + .into_input() + .into_iter(); + + let mut actual_arg_inputs = ([ + vec![(("s1", "d"), 1), (("s2", "ac"), 1)], + vec![(("s4", "m"), 1)], + vec![(("s2", "ac"), 1)], + ] as [Vec<(Str2, ZWeight)>; STEPS]) + .into_input() + .into_iter(); + + let mut formal_param_inputs = ([vec![(("Greeter.greet", "x"), 1)], vec![], vec![]] + as [Vec<(Str2, ZWeight)>; STEPS]) + .into_input() + .into_iter(); + + // OUTPUTS + + let mut var_points_to_expected_outputs = ([ + zset! { + Tup2("ac".to_string(), "oCat".to_string()) => 1, + Tup2("c".to_string(), "oCat".to_string()) => 1, + Tup2("d".to_string(), "oDog".to_string()) => 1, + Tup2("g".to_string(), "oG".to_string()) => 1, + Tup2("x".to_string(), "oDog".to_string()) => 1, + Tup2("x".to_string(), "oCat".to_string()) => 1, + }, + zset! { + Tup2("m".to_string(), "oMouse".to_string()) => 1, + Tup2("x".to_string(), "oMouse".to_string()) => 1, + }, + zset! { + Tup2("ac".to_string(), "oCat".to_string()) => -1, + Tup2("x".to_string(), "oCat".to_string()) => -1, + }, + ] as [OrdZSet; STEPS]) + .into_iter(); + + let mut call_graph_expected_outputs = ([ + zset! { + Tup2("s1".to_string(), "Greeter.greet".to_string()) => 1, + Tup2("s2".to_string(), "Greeter.greet".to_string()) => 1, + Tup2("s3".to_string(), "Dog.speak".to_string()) => 1, + Tup2("s3".to_string(), "Cat.speak".to_string()) => 1, + }, + zset! { + Tup2("s3".to_string(), "Mouse.speak".to_string()) => 1, + Tup2("s4".to_string(), "Greeter.greet".to_string()) => 1, + }, + zset! { + Tup2("s2".to_string(), "Greeter.greet".to_string()) => -1, + Tup2("s3".to_string(), "Cat.speak".to_string()) => -1, + }, + ] as [OrdZSet; STEPS]) + .into_iter(); + + // EXECUTION + + for i in (0..STEPS).confirm_each() { + alloc_input.append(&mut alloc_inputs.next().unwrap()); + assign_input.append(&mut assign_inputs.next().unwrap()); + virtual_call_input.append(&mut virtual_call_inputs.next().unwrap()); + heap_type_input.append(&mut heap_type_inputs.next().unwrap()); + dispatch_input.append(&mut dispatch_inputs.next().unwrap()); + actual_arg_input.append(&mut actual_arg_inputs.next().unwrap()); + formal_param_input.append(&mut formal_param_inputs.next().unwrap()); + + circuit.transaction()?; + + let var_points_to_output = var_points_to_output.concat().iter().collect::>(); + assert_eq!( + var_points_to_output, + var_points_to_expected_outputs + .next() + .unwrap() + .iter() + .collect::>() + ); + println!("Var Points To Output: {var_points_to_output:?}"); + + let call_graph_output = call_graph_output.concat().iter().collect::>(); + assert_eq!( + call_graph_output, + call_graph_expected_outputs + .next() + .unwrap() + .iter() + .collect::>() + ); + println!("Call Graph Output: {call_graph_output:?}"); + } + + Ok(()) + } + // Computes the factorial of the first 10 numbers. #[test] fn test_factorial_with_iterate() -> Result<(), anyhow::Error> { @@ -686,7 +1077,6 @@ mod test { Ok(()) } - // Computes the sum of the first n natural numbers. #[test] fn test_sum_n_natural_numbers_with_add_feedback() -> Result<(), anyhow::Error> { From ada4c1c33af11717dcbac733eafc86117014e644 Mon Sep 17 00:00:00 2001 From: Leo Stewen Date: Wed, 17 Jun 2026 13:29:18 +0100 Subject: [PATCH 4/8] Restructure external API --- packages/coln-query/src/api.rs | 230 --------------------- packages/coln-query/src/api/deltas.rs | 35 ++++ packages/coln-query/src/api/mod.rs | 11 + packages/coln-query/src/api/schema.rs | 26 +++ packages/coln-query/src/api/store.rs | 23 +++ packages/coln-query/src/api/transaction.rs | 167 +++++++++++++++ packages/coln-query/src/api/violations.rs | 22 ++ packages/coln-query/src/lib.rs | 2 +- 8 files changed, 285 insertions(+), 231 deletions(-) delete mode 100644 packages/coln-query/src/api.rs create mode 100644 packages/coln-query/src/api/deltas.rs create mode 100644 packages/coln-query/src/api/mod.rs create mode 100644 packages/coln-query/src/api/schema.rs create mode 100644 packages/coln-query/src/api/store.rs create mode 100644 packages/coln-query/src/api/transaction.rs create mode 100644 packages/coln-query/src/api/violations.rs diff --git a/packages/coln-query/src/api.rs b/packages/coln-query/src/api.rs deleted file mode 100644 index f06aa3ca..00000000 --- a/packages/coln-query/src/api.rs +++ /dev/null @@ -1,230 +0,0 @@ -//! This module defines the public API of the query engine. It consists of: -//! -//! 1. An interface for passing deltas of row-oriented data. There is -//! [TableDelta], [StoreDelta], and [DerivedDataDelta]. -//! 2. An interface for [Transactions](Tx). A transactions can be in exactly one -//! state of [Prepare], [Pending], [Committed], [Aborted], [Rejected]. -//! 3. An interface for constructing the query engine's IR (TBD with RFC 03). - -use crate::scalar::ScalarTypedValue; -use std::error::Error; - -pub struct Table { - name: String, - // TODO: How does the schema interface look like? Maybe it doesn't need to - // be included every time as the schema should be implicitly given through - // the initial queries derived from `coln-compiler`'s IR. - schema: (), -} - -pub type ZWeight = i64; -/// An update to a table. It either represents an insertion or a deletion of a -/// row from a table, see [`z_weight`](`Self::z_weight`) documentation. -pub struct TableDelta { - /// A ZWeight value ... - /// - `== 0` is not allowed per convention but it does not do harm, it - /// behaves as if there was no insertion happening at all. - /// - `n if n > 0` represents an insertion. If `n > 1` it is a duplicated - /// insertion, that is, the row is inserted n-times. - /// - `n if n < 0` represents a deletion. If `n < 1` we remove the row - /// n-times. - z_weight: ZWeight, - /// The row-oriented data. - row: Vec, -} - -/// An update of the EDBPs, that is, insertions or deletions of base facts. -pub struct StoreDelta { - inner: Vec<(Table, Vec)>, -} - -/// An update of the IDBPs, that is, insertions or deletions of derived facts. -pub struct DerivedDataDelta { - /// Contains the delta in the IDB after applying a delta in the EDB (the - /// latter is a [`StoreDelta`]). - inner: Vec<(Table, Vec)>, -} - -/// For each query checking a constraint this reports back identified -/// counterexamples. -pub struct Violations { - /// Contains the counter examples for each unmet constraint. Note that - /// [`Table`] refers to a derived view (defined through a query) rather - /// than a physical input table here. - inner: Vec<(Table, Vec)>, -} - -/// We use the Typestate-Pattern for compile-time enforced transaction states -/// and their transitions to provide a hard-to-abuse API. -pub struct Tx { - state: State, -} - -/// This is the initial state of a Transaction and it is open to receive table -/// deltas. -pub struct Prepare { - delta: StoreDelta, -} - -/// The transaction is ready to apply in theory, that is, all constraints are -/// met. Still, it awaits either an approval or an end user abort. Without an -/// explicit approval, any state change caused by the transaction will be -/// undone. -pub struct Pending<'a, Store: TxStore> { - store: &'a mut Store, - derived_data_delta: DerivedDataDelta, -} - -/// The transaction is finalized and applied to both the storage and query -/// engine. Any state caused by the transaction is already committed. -pub struct Committed { - derived_data_delta: DerivedDataDelta, -} - -/// The transaction is committable in theory, that is, it does _not_ violate any -/// constraint but the end user decided to abort regardless. Any state caused by -/// the transaction is already rolled back. -pub struct Aborted {} - -/// The transaction _must be_ rejected because some constraint is violated. Any -/// state caused by the transaction is already rolled back. -pub struct Rejected { - violations: Violations, -} - -// Open Questions: -// -// 1. How pure do we want the query engine to be? In some ways constraint -// violations are just the result of some queries (antijoins). Do we want to -// "higher" (as in the opposite of lowering) them as part of the query engine -// to constraint violations already? Or shall we just pass through the raw -// query results to higher layers and they figure out the intepretation of -// the query results? (For the antijoin approach that would be checking if -// the result is empty (constraint met) or, otherwise, the output serve as -// counterexamples.) Shall that duty be up to the query engine? -// -// 2. In case of chased laws (inside rules): Are they just some inevitable (but -// somewhat irrelevant) computation step to figure out if all constraints are -// met, or do they require back propagation up to the end user (i.e. they -// need to be communicated back from the query engine)? -// -// 3. Assuming constraint violation interpretation (1) is on the query engine -// and the results of chased laws (IDB) need to be visible to users (2), are -// the two exclusive? I.e., are there cases in which constraints are violated -// but derived data (IDB) caused by the (invalid) transaction is still valid? -// If this is _not_ the case, the interface below may be a decent fit. -// Implementation-wise I suggest we do not communicate a constraint violation -// as an Error case (but rather reserve these for harder engine errors) and -// instead treat a constraint violation as a perfectly valid outcome (part of -// Ok case). -pub enum ExecutionOutcome { - DerivedDataDelta(DerivedDataDelta), - Violations(Violations), -} - -pub enum TryCommitOk<'a, Store: TxStore> { - Pending(Tx>), - Rejected(Tx), -} - -pub enum TryCommitErr { - ExecutionError(ExecutionError), - RollbackError(RollbackError), -} - -impl Tx { - pub fn new(store_delta: StoreDelta) -> Self { - Tx { - state: Prepare { delta: store_delta }, - } - } - /// Convenience method to add data beyond initialization. - pub fn insert)>>(&mut self, deltas: I) { - self.state.delta.inner.extend(deltas); - } - pub fn try_commit<'a, Store: TxStore>( - self, - store: &'a mut Store, - ) -> Result, TryCommitErr> - { - match store - .apply(self.state.delta) - .map_err(TryCommitErr::ExecutionError) - .map(Into::::into)? - { - ExecutionOutcome::DerivedDataDelta(delta) => Ok(TryCommitOk::Pending(Tx { - state: Pending { - store, - derived_data_delta: delta, - }, - })), - ExecutionOutcome::Violations(violations) => { - store.rollback().map_err(TryCommitErr::RollbackError)?; - Ok(TryCommitOk::Rejected(Tx { - state: Rejected { violations }, - })) - } - } - } -} - -impl<'a, Store: TxStore> Tx> { - pub fn commit(self) -> Result, Store::CommitError> { - // Prevent the custom Drop implementation from running at this point. - let md = std::mem::ManuallyDrop::new(self.state); - // Move the store ref. This is safe because `md` will never be dropped, - // so we avoid a double-free, *and* because the returned `Committed` - // state inherits the same lifetime as `self`. - let store = unsafe { std::ptr::read(&md.store) }; - // Move the vector. This is safe because `md` will never be dropped, so - // we avoid a double-free. - let derived_data_delta = unsafe { std::ptr::read(&md.derived_data_delta) }; - store.commit()?; - Ok(Tx { - state: Committed { derived_data_delta }, - }) - } - pub fn abort(self) -> Result, Store::RollbackError> { - // Prevent the custom Drop implementation from running at this point. - let md = std::mem::ManuallyDrop::new(self.state); - // Move the store ref. This is safe because `md` will never be dropped, - // so we avoid a double-free, *and* because the returned `Aborted` - // state inherits the same lifetime as `self`. - let store = unsafe { std::ptr::read(&md.store) }; - // Move the vector. This is safe because `md` will never be dropped, so - // we avoid a double-free, but also required to free the heap allocation - // behind the vector. - let derived_data_delta = unsafe { std::ptr::read(&md.derived_data_delta) }; - store.rollback()?; - Ok(Tx { state: Aborted {} }) - } -} - -// This is to make the API foolproof: If the caller does neither commit nor -// abort the transaction, we take the conservative approach and rollback any -// state change caused by it. -impl<'a, Store: TxStore> Drop for Pending<'a, Store> { - fn drop(&mut self) { - // Happens in best-effort manner to avoid panicking in Drop impls. - // Possibly log the event, though. - let _ = self.store.rollback(); - } -} - -/// A generic transactional engine/store. It prescribes little things upon the -/// the store and it may also be a good fit for the storage engine's public API? -pub(crate) trait TxStore { - type ExecutionOk: Into; - type ExecutionError: Error + Clone; - type RollbackError: Error + Clone; - type CommitError: Error + Clone; - - /// Executes and applies the transaction given by the updates in `delta`. - fn apply(&mut self, delta: StoreDelta) -> Result; - /// Undoes the last transaction by rolling back every state change caused - /// by that transaction. Should only fail in exceptional circumstances. - fn rollback(&mut self) -> Result<(), Self::RollbackError>; - /// Commits the last transaction. Possibly, a no-op or do some cleanup. - /// Should only fail in exceptional circumstances. - fn commit(&mut self) -> Result<(), Self::CommitError>; -} diff --git a/packages/coln-query/src/api/deltas.rs b/packages/coln-query/src/api/deltas.rs new file mode 100644 index 00000000..7ad07b60 --- /dev/null +++ b/packages/coln-query/src/api/deltas.rs @@ -0,0 +1,35 @@ +//! An interface for passing deltas of row-oriented data. There is +//! [TableDelta], [StoreDelta], and [DerivedDataDelta]. + +use super::schema::TableRef; +use crate::scalar::ScalarTypedValue; + +pub type ZWeight = i64; + +/// An update to a base table (part of the EDB). +/// It either represents an insertion or a deletion of a row from a table, +/// see [`z_weight`](`Self::z_weight`) documentation. +pub struct TableDelta { + /// A ZWeight value ... + /// - `== 0` is not allowed per convention but it does not do harm, it + /// behaves as if there was no insertion happening at all. + /// - `n if n > 0` represents an insertion. If `n > 1` it is a duplicated + /// insertion, that is, the row is inserted n-times. + /// - `n if n < 0` represents a deletion. If `n < 1` we remove the row + /// n-times. + z_weight: ZWeight, + /// The row-oriented data. + row: Vec, +} + +/// An update of the EDBPs, that is, insertions or deletions of base facts. +pub struct StoreDelta { + pub inner: Vec<(TableRef, Vec)>, +} + +/// An update of the IDBPs, that is, insertions or deletions of derived facts. +pub struct DerivedDataDelta { + /// Contains the delta in the IDB after applying a delta in the EDB (the + /// latter is a [`StoreDelta`]). + inner: Vec<(TableRef, Vec)>, +} diff --git a/packages/coln-query/src/api/mod.rs b/packages/coln-query/src/api/mod.rs new file mode 100644 index 00000000..9eeead68 --- /dev/null +++ b/packages/coln-query/src/api/mod.rs @@ -0,0 +1,11 @@ +//! This module defines the public API of the query engine intended to be mainly +//! used by `coln-store`. See the other module level documentations. + +// Receiving `coln-compiler`'s IR is blocked by its stabiliztion and hence still +// missing in here. + +mod deltas; +mod schema; +mod store; +mod transaction; +mod violations; diff --git a/packages/coln-query/src/api/schema.rs b/packages/coln-query/src/api/schema.rs new file mode 100644 index 00000000..1f936bd1 --- /dev/null +++ b/packages/coln-query/src/api/schema.rs @@ -0,0 +1,26 @@ +//! This module provides implementations for passing references to tables +//! ([TableRef]) and communicating a schema of a table ([TableSchema]). + +use crate::scalar::ScalarType; + +/// An identifier that uniquely identifies a table (globally across the store). +pub struct TableRef { + inner: String, +} + +pub struct TableSchema { + /// The table's unique identifier/name. + name: TableRef, + /// All fields of the table in their physical order. + columns: Vec, + /// The list of (possibly compound) primary keys into the table, specified + /// as indices into the [`header`](Self::header). + primary_keys: Vec>, +} + +pub struct Column { + /// The column's name. + name: String, + /// Ihe column's (scalar) type. + scalar_type: ScalarType, +} diff --git a/packages/coln-query/src/api/store.rs b/packages/coln-query/src/api/store.rs new file mode 100644 index 00000000..d7db21b8 --- /dev/null +++ b/packages/coln-query/src/api/store.rs @@ -0,0 +1,23 @@ +//! This defines the [API for a transactional store](TxStore). + +use super::{deltas::StoreDelta, transaction::ApplicationOutcome}; +use std::error::Error; + +/// A generic transactional engine/store. +pub(crate) trait TxStore { + type ApplicationOk: Into; + type ApplicationError: Error + Clone; + type RollbackError: Error + Clone; + type CommitError: Error + Clone; + + /// Executes and applies the transaction given by the updates in `delta`. + fn apply(&mut self, delta: StoreDelta) -> Result; + /// Undoes the last transaction by rolling back every state change caused + /// by that transaction. Should only fail in exceptional circumstances. + fn rollback(&mut self) -> Result<(), Self::RollbackError>; + /// Commits the last transaction. Possibly, a no-op or do some cleanup. + /// Should only fail in exceptional circumstances. + fn commit(&mut self) -> Result<(), Self::CommitError>; +} + +// TODO: Implement for the engine! diff --git a/packages/coln-query/src/api/transaction.rs b/packages/coln-query/src/api/transaction.rs new file mode 100644 index 00000000..c9a9ccb3 --- /dev/null +++ b/packages/coln-query/src/api/transaction.rs @@ -0,0 +1,167 @@ +//! An interface for a [Transaction](Tx). A transaction can be in exactly one +//! state of [Prepare], [Pending], [Committed], [Aborted], [Rejected]. + +use super::{ + deltas::{DerivedDataDelta, StoreDelta, TableDelta}, + schema::TableRef, + store::TxStore, + violations::Violations, +}; + +/// We use the Typestate-Pattern for compile-time enforced transaction states +/// and their transitions to provide a hard-to-abuse API. +pub struct Tx { + state: State, +} + +/// This is the initial state of a Transaction and it is open to receive table +/// deltas. +pub struct Prepare { + delta: StoreDelta, +} + +/// The transaction is ready to apply in theory, that is, all _mandatory_ +/// constraints are met (although some _monitored_ constraints may be violated). +/// Yet, the transaction awaits either an approval or an end user abort. Without +/// an explicit approval, any state change caused by the transaction will be +/// undone. +pub struct Pending<'a, Store: TxStore> { + store: &'a mut Store, + derived_data_delta: DerivedDataDelta, + soft_violations: Violations, +} + +/// The transaction is finalized and applied to both the storage and query +/// engine. Any state caused by the transaction is already committed. +pub struct Committed { + derived_data_delta: DerivedDataDelta, +} + +/// The transaction is committable in theory, that is, it does _not_ violate any +/// constraint but the end user decided to abort regardless. Any state caused by +/// the transaction is already rolled back. +pub struct Aborted {} + +/// The transaction _must be_ rejected because some _mandatory_ constraints are +/// violated. Any state caused by the transaction is already rolled back. +pub struct Rejected { + violations: Violations, +} + +/// The outcomes that can happen if updates are applied to the store: +/// +/// 1. [`Self::DerivedDataDelta`], if no constraints are violated. +/// 2. [`Self::HardViolations`], if mandatory constraints are violated. +/// 3. [`Self::SoftViolations`], if monitored constraints are violated. +/// +/// We treat constraint violations as perfectly normal use and report them back +/// as part of the `Ok` case of a `Result` and reserve the `Err` case for hard +/// engine errors. +pub enum ApplicationOutcome { + /// All constraints are met and updates in derived data are communicated + /// back. + DerivedDataDelta(DerivedDataDelta), + /// Mandatory constraints are violated. + HardViolations(Violations), + /// Monitored constraints are violated. Since they only issue a warning but + /// are tolerated in general, we nevertheless apply the transaction, obtain + /// the derived data delta, and report back about the violations. + SoftViolations(DerivedDataDelta, Violations), +} + +pub enum TryCommitOk<'a, Store: TxStore> { + Pending(Tx>), + Rejected(Tx), +} + +pub enum TryCommitErr { + ApplicationError(ApplicationError), + RollbackError(RollbackError), +} + +impl Tx { + pub fn new(store_delta: StoreDelta) -> Self { + Tx { + state: Prepare { delta: store_delta }, + } + } + /// Convenience method to add data beyond initialization. + pub fn insert)>>(&mut self, deltas: I) { + self.state.delta.inner.extend(deltas); + } + pub fn try_commit<'a, Store: TxStore>( + self, + store: &'a mut Store, + ) -> Result, TryCommitErr> + { + match store + .apply(self.state.delta) + .map_err(TryCommitErr::ApplicationError) + .map(Into::::into)? + { + ApplicationOutcome::DerivedDataDelta(delta) => Ok(TryCommitOk::Pending(Tx { + state: Pending { + store, + derived_data_delta: delta, + soft_violations: Violations::none(), + }, + })), + ApplicationOutcome::HardViolations(violations) => { + store.rollback().map_err(TryCommitErr::RollbackError)?; + Ok(TryCommitOk::Rejected(Tx { + state: Rejected { violations }, + })) + } + ApplicationOutcome::SoftViolations(delta, violations) => Ok(TryCommitOk::Pending(Tx { + state: Pending { + store, + derived_data_delta: delta, + soft_violations: violations, + }, + })), + } + } +} + +impl<'a, Store: TxStore> Tx> { + pub fn commit(self) -> Result, Store::CommitError> { + // Prevent the custom Drop implementation from running at this point. + let md = std::mem::ManuallyDrop::new(self.state); + // Move the store ref. This is safe because `md` will never be dropped, + // so we avoid a double-free, *and* because the returned `Committed` + // state inherits the same lifetime as `self`. + let store = unsafe { std::ptr::read(&md.store) }; + // Move the vector. This is safe because `md` will never be dropped, so + // we avoid a double-free. + let derived_data_delta = unsafe { std::ptr::read(&md.derived_data_delta) }; + store.commit()?; + Ok(Tx { + state: Committed { derived_data_delta }, + }) + } + pub fn abort(self) -> Result, Store::RollbackError> { + // Prevent the custom Drop implementation from running at this point. + let md = std::mem::ManuallyDrop::new(self.state); + // Move the store ref. This is safe because `md` will never be dropped, + // so we avoid a double-free, *and* because the returned `Aborted` + // state inherits the same lifetime as `self`. + let store = unsafe { std::ptr::read(&md.store) }; + // Move the vector. This is safe because `md` will never be dropped, so + // we avoid a double-free, but also required to free the heap allocation + // behind the vector. + let derived_data_delta = unsafe { std::ptr::read(&md.derived_data_delta) }; + store.rollback()?; + Ok(Tx { state: Aborted {} }) + } +} + +// This is to make the API foolproof: If the caller does neither commit nor +// abort the transaction, we take the conservative approach and rollback any +// state change caused by it. +impl<'a, Store: TxStore> Drop for Pending<'a, Store> { + fn drop(&mut self) { + // Happens in best-effort manner to avoid panicking in Drop impls. + // Possibly log the event, though. + let _ = self.store.rollback(); + } +} diff --git a/packages/coln-query/src/api/violations.rs b/packages/coln-query/src/api/violations.rs new file mode 100644 index 00000000..663a1e46 --- /dev/null +++ b/packages/coln-query/src/api/violations.rs @@ -0,0 +1,22 @@ +//! This module is concerned with violations and how to report them. Many things +//! are still TBD. + +use super::{deltas::TableDelta, schema::TableRef}; + +/// For each query which is checking a constraint, this reports back identified +/// counterexamples. +pub struct Violations { + /// Contains the counter examples for each unmet constraint. Note that + /// [`TableRef`] refers to a derived view (defined through a query) rather + /// than a physical base table here. + inner: Vec<(TableRef, Vec)>, +} + +impl Violations { + /// Report no violations. + pub fn none() -> Self { + Self { + inner: Vec::new(), // Does not allocate. + } + } +} diff --git a/packages/coln-query/src/lib.rs b/packages/coln-query/src/lib.rs index 10d4c416..84d702b7 100644 --- a/packages/coln-query/src/lib.rs +++ b/packages/coln-query/src/lib.rs @@ -1,6 +1,6 @@ #![allow(dead_code, unused_variables)] -pub(crate) mod api; +pub mod api; mod context; pub mod dbsp; pub mod error; From f330aa76973ac47fb737c950ce5b8c1775a54c4a Mon Sep 17 00:00:00 2001 From: Leo Stewen Date: Tue, 23 Jun 2026 11:11:22 +0100 Subject: [PATCH 5/8] Restructure everything DBSP by having a dedicated test playground, our own wrappers, and cli table prints of deltas --- packages/coln-query/src/dbsp.rs | 1500 ----------------- packages/coln-query/src/dbsp/cli_table.rs | 73 + packages/coln-query/src/dbsp/mod.rs | 6 + packages/coln-query/src/dbsp/playground.rs | 1214 +++++++++++++ .../{ => src/dbsp}/points_to_step_1.dl | 13 +- .../{ => src/dbsp}/points_to_step_2.dl | 13 +- .../{ => src/dbsp}/points_to_step_3.dl | 13 +- packages/coln-query/src/dbsp/wrapper.rs | 360 ++++ packages/coln-query/src/lib.rs | 4 +- 9 files changed, 1664 insertions(+), 1532 deletions(-) delete mode 100644 packages/coln-query/src/dbsp.rs create mode 100644 packages/coln-query/src/dbsp/cli_table.rs create mode 100644 packages/coln-query/src/dbsp/mod.rs create mode 100644 packages/coln-query/src/dbsp/playground.rs rename packages/coln-query/{ => src/dbsp}/points_to_step_1.dl (89%) rename packages/coln-query/{ => src/dbsp}/points_to_step_2.dl (89%) rename packages/coln-query/{ => src/dbsp}/points_to_step_3.dl (89%) create mode 100644 packages/coln-query/src/dbsp/wrapper.rs diff --git a/packages/coln-query/src/dbsp.rs b/packages/coln-query/src/dbsp.rs deleted file mode 100644 index ca482235..00000000 --- a/packages/coln-query/src/dbsp.rs +++ /dev/null @@ -1,1500 +0,0 @@ -use crate::{ - expr::{Literal, LiteralExpr}, - relation::{Relation, RelationSchema, SchemaTuple, TupleKey, TupleValue}, -}; -use cli_table::{Cell, Style, Table, format::Justify}; -pub use dbsp::{ - CircuitHandle, DBData as DbData, DBSPHandle as DbspHandle, Error as DbspError, NestedCircuit, - RootCircuit, Runtime, ZWeight, dynamic::ArchivedDBData as ArchivedDbData, utils::IsNone, zset, -}; -use dbsp::{ - IndexedZSetHandle, IndexedZSetReader, OrdIndexedZSet, OrdZSet, OutputHandle, Stream, - typed_batch::SpineSnapshot, utils::Tup2, -}; -use std::{ - collections::HashMap, - fmt::{Debug, Display}, - iter, -}; - -type OrdStream = Stream>; - -struct OrderedTestStream { - inner: OrdStream, - other: OrdStream, -} - -impl OrderedTestStream { - fn try_out(&self) { - let selected: OrdStream = self.inner.filter(move |tuple| { - // execute filter expression - // return result - true - }); - - // Issue: Apparently, there is no way to select a join key here, - // the entire tuple will be matched, hence, the OrdZSet is not a good - // choice here! - let joined: OrdStream = selected.join_generic(&self.other, |k, _, _| { - // merge left and right tuple - Some((k.clone(), ())) - }); - - let projected: OrdStream = joined.map(|k| { - // project tuple - k.clone() - // (k.clone(), ()) - }); - } -} - -struct IndexedTestStream { - a: Relation, - b: Relation, -} - -impl IndexedTestStream { - fn try_out(&self) { - let copy: usize = 12; - let non_copy = String::from("hi"); - - let selected = self.a.inner.filter(move |tuple| { - // Put attributes into scope. How? - // - Define vars uninitialized beforehand and just assign them here? This would - // allow running the resolver just once before the interpreter starts. - // - Define vars with a var declaration stmt with an initializer? But this - // requires running the resolver for each tuple. - // - Hacky through the interpreter? - // - Through an (anonymous) function call? - // execute filter expression - // return result - let access_copy = copy; - let access_non_copy = &non_copy; - true - }); - - let joined = selected.join_index(&self.b.inner, |k, left, right| { - // merge left and right tuple - Some((k.clone(), right.clone())) - }); - - let projected = joined.map_index(|(k, tuple)| { - // project tuple - (k.clone(), tuple.clone()) - }); - } -} - -pub fn new_ord_indexed_stream( - circuit: &mut RootCircuit, -) -> (OrdIndexedRootStream, OrdIndexedStreamInputHandle) { - circuit.add_input_indexed_zset::() -} - -pub type OrdIndexedStreamInputHandle = IndexedZSetHandle; - -pub type OrdIndexedStreamOutputHandle = - OutputHandle>>; - -pub type OrdIndexedStream = Stream>; - -pub type OrdIndexedRootStream = OrdIndexedStream; -pub type OrdIndexedNestedStream = OrdIndexedStream; - -/// A wrapper of DBSP's streams carrying [`dbsp::OrdIndexedZSet`] but -/// generic-free over the circuit type. This limits the nesting level to one -/// level but this does not matter for practical applications. -#[derive(Clone)] -pub enum StreamWrapper { - Root(OrdIndexedRootStream), - Nested(OrdIndexedNestedStream), -} - -impl StreamWrapper { - pub fn distinct(&self) -> StreamWrapper { - match self { - Self::Root(stream) => Self::Root(stream.distinct()), - Self::Nested(stream) => Self::Nested(stream.distinct()), - } - } - - pub fn sum<'a, I>(&'a self, streams: I) -> StreamWrapper - where - I: IntoIterator, - { - match self { - Self::Root(stream) => { - Self::Root(stream.sum(streams.into_iter().map(|s| s.expect_root()))) - } - Self::Nested(stream) => { - Self::Nested(stream.sum(streams.into_iter().map(|s| s.expect_nested()))) - } - } - } - - pub fn minus(&self, other: &Self) -> Self { - match self { - Self::Root(stream) => Self::Root(stream.minus(other.expect_root())), - Self::Nested(stream) => Self::Nested(stream.minus(other.expect_nested())), - } - } - - pub fn map_index(&self, map_func: F) -> StreamWrapper - where - F: Fn((&TupleKey, &TupleValue)) -> (TupleKey, TupleValue) + 'static, - { - match self { - Self::Root(stream) => Self::Root(stream.map_index(map_func)), - Self::Nested(stream) => Self::Nested(stream.map_index(map_func)), - } - } - - pub fn filter(&self, filter_func: F) -> Self - where - F: Fn((&TupleKey, &TupleValue)) -> bool + 'static, - { - match self { - Self::Root(stream) => Self::Root(stream.filter(filter_func)), - Self::Nested(stream) => Self::Nested(stream.filter(filter_func)), - } - } - - pub fn join_index(&self, other: &Self, join: F) -> Self - where - F: Fn(&TupleKey, &TupleValue, &TupleValue) -> It + Clone + 'static, - It: IntoIterator + 'static, - { - match self { - Self::Root(stream) => Self::Root(stream.join_index(other.expect_root(), join)), - Self::Nested(stream) => Self::Nested(stream.join_index(other.expect_nested(), join)), - } - } - - pub fn anti_join_index(&self, other: &Self) -> Self { - match self { - Self::Root(stream) => Self::Root(stream.antijoin(other.expect_root())), - Self::Nested(stream) => Self::Nested(stream.antijoin(other.expect_nested())), - } - } - - /// The delta0 operator imports a stream from the parent circuit into the - /// child circuit. - pub fn delta0(&self, child_circuit: &NestedCircuit) -> Self { - match self { - // Transitions from RootStream to NestedStream - Self::Root(stream) => Self::Nested(stream.delta0(child_circuit)), - Self::Nested(stream) => panic!("No further nesting for beyond NestedStreams"), - } - } - - pub fn output(&self) -> OrdIndexedStreamOutputHandle { - match self { - Self::Root(stream) => stream.accumulate_output(), - Self::Nested(stream) => panic!("Nested streams do not support output()"), - } - } - - pub fn expect_root(&self) -> &OrdIndexedRootStream { - if let Self::Root(stream) = self { - stream - } else { - panic!("Expected RootStream") - } - } - - pub fn expect_nested(&self) -> &OrdIndexedNestedStream { - if let Self::Nested(stream) = self { - stream - } else { - panic!("Expected NestedStream") - } - } -} - -impl From for StreamWrapper { - fn from(stream: OrdIndexedRootStream) -> Self { - Self::Root(stream) - } -} - -impl From for StreamWrapper { - fn from(stream: OrdIndexedNestedStream) -> Self { - Self::Nested(stream) - } -} - -impl IntoIterator for &'_ StreamWrapper { - type Item = Self; - type IntoIter = std::iter::Once; - - fn into_iter(self) -> Self::IntoIter { - std::iter::once(self) - } -} - -#[derive(Default, Debug, Clone)] -pub struct DbspInputs { - inputs: HashMap, -} - -impl DbspInputs { - fn insert(&mut self, name: String, input: DbspInput) { - self.inputs.insert(name, input); - } - pub fn get(&self, name: &str) -> Option<&DbspInput> { - self.inputs.get(name) - } - pub fn take(&mut self, name: &str) -> Option { - self.inputs.remove(name) - } - pub fn iter(&self) -> impl Iterator { - self.inputs.values() - } -} - -#[derive(Clone)] -pub struct DbspInput { - schema: RelationSchema, - handle: OrdIndexedStreamInputHandle, -} - -impl DbspInput { - pub fn add( - schema: RelationSchema, - circuit: &mut RootCircuit, - inputs: &mut DbspInputs, - ) -> LiteralExpr { - let (stream, handle) = new_ord_indexed_stream(circuit); - let input = Self { - schema: schema.clone(), - handle, - }; - inputs.insert(schema.name.clone(), input); - LiteralExpr { - value: Literal::Relation(Relation::new(schema, stream)), - } - } - pub fn handle(&self) -> &OrdIndexedStreamInputHandle { - &self.handle - } - pub fn insert<'a, T: Into + Into + Clone + 'a>( - &self, - tuples: impl IntoIterator, - ) { - tuples.into_iter().for_each(|(tuple, z_weight)| { - self.handle - .push(tuple.clone().into(), (tuple.clone().into(), z_weight)) - }) - } - pub fn insert_with_same_weight<'a, T: Into + Into + Clone + 'a>( - &self, - tuples: impl IntoIterator, - z_weight: ZWeight, - ) { - self.insert(tuples.into_iter().map(|tuple| (tuple, z_weight))); - } -} - -impl Debug for DbspInput { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("DbspInput") - .field("schema", &self.schema) - .finish() - } -} - -pub struct DbspOutput { - handle: OrdIndexedStreamOutputHandle, - schema: RelationSchema, -} - -impl DbspOutput { - pub fn new(schema: RelationSchema, handle: OrdIndexedStreamOutputHandle) -> Self { - Self { schema, handle } - } - pub fn to_batch(&self) -> DbspOutputBatch<'_> { - let inner = self.handle.concat().iter().collect::>(); - DbspOutputBatch { - schema: &self.schema, - inner, - } - } -} - -pub struct DbspOutputBatch<'a> { - schema: &'a RelationSchema, - inner: Vec<(TupleKey, TupleValue, ZWeight)>, -} - -impl DbspOutputBatch<'_> { - const JUSTIFICATION: Justify = Justify::Right; - - pub fn as_table(&self) -> impl Display { - self.inner - .iter() - .map(|(key, tuple, weight)| { - iter::once(weight.to_string().cell().justify(Self::JUSTIFICATION)).chain( - SchemaTuple::new(&self.schema.tuple, tuple) - .fields() - .map(|attribute| attribute.to_string().cell().justify(Self::JUSTIFICATION)) - .collect::>(), - ) - }) - .table() - .title( - iter::once("z-weight".cell()) - .chain(self.schema.tuple.field_names(&None).map(|name| name.cell())), - ) - .bold(true) - .display() - .expect("Table error") - } - pub fn as_debug_table(&self) -> impl Display { - self.inner - .iter() - .map(|(key, tuple, weight)| { - // We ensure that the key and tuple data lengths match the - // respective schema field lengths. - debug_assert!(key.data.len() == self.schema.key.full_len()); - debug_assert!(tuple.data.len() == self.schema.tuple.full_len()); - iter::once(weight.to_string().cell().justify(Self::JUSTIFICATION)) - .chain( - SchemaTuple::new(&self.schema.key, key) - .all_fields() - .map(|attribute| { - attribute.to_string().cell().justify(Self::JUSTIFICATION) - }) - .collect::>(), - ) - .chain( - SchemaTuple::new(&self.schema.tuple, tuple) - .all_fields() - .map(|attribute| { - attribute.to_string().cell().justify(Self::JUSTIFICATION) - }) - .collect::>(), - ) - }) - .table() - .title( - iter::once("z-weight".cell()) - .chain( - self.schema - .key - .all_field_names(&None) - .map(|name| format!("[key] {name}").cell()), - ) - .chain( - self.schema - .tuple - .all_field_names(&None) - .map(|name| format!("[value] {name}").cell()), - ), - ) - .bold(true) - .display() - .expect("Table error") - } - pub fn as_data(&self) -> impl Iterator { - self.inner - .iter() - .map(|(_key, tuple, weight)| (*weight, tuple)) - } - pub fn as_zset(&self) -> OrdZSet { - let keys = self - .inner - .iter() - .map(|(_key, tuple, weight)| { - let tuple: TupleValue = SchemaTuple::new(&self.schema.tuple, tuple) - .fields() - .cloned() - .collect(); - Tup2(tuple, *weight) - }) - .collect::>(); - OrdZSet::from_keys((), keys) - } - pub fn as_debug_zset(&self) -> OrdZSet { - let keys = self - .inner - .iter() - .map(|(_key, tuple, weight)| Tup2(tuple.clone(), *weight)) - .collect::>(); - OrdZSet::from_keys((), keys) - } -} - -#[cfg(test)] -mod test { - use super::*; - use dbsp::{ - Circuit, indexed_zset, - operator::{Generator, Z1}, - utils::{Tup2, Tup3, Tup4}, - zset, zset_set, - }; - use std::{cell::RefCell, num::NonZeroUsize, rc::Rc}; - - fn worker_threads() -> NonZeroUsize { - std::thread::available_parallelism().unwrap_or(NonZeroUsize::new(4).unwrap()) - } - - mod test_utils { - use std::io::{self, IsTerminal, Write}; - - /// Iterator adapter that prints the step count and waits for a keypress - /// before yielding each item if in interactive mode. - pub struct Confirm { - inner: I, - count: usize, - interactive: bool, - } - - impl Iterator for Confirm { - type Item = I::Item; - - fn next(&mut self) -> Option { - if !self.interactive { - return self.inner.next(); - } - if self.count > 0 { - print!("Press Enter to continue (Ctrl-D to stop)... "); - io::stdout().flush().ok(); - let mut line = String::new(); - match io::stdin().read_line(&mut line) { - Ok(0) | Err(_) => return None, // EOF / Ctrl-D -> stop - Ok(_) => {} - }; - } - match self.inner.next() { - Some(item) => { - self.count += 1; - println!("=== Iteration {:03} ===", self.count); - Some(item) - } - None => None, - } - } - } - - /// Whether we're in an interactive `cargo test -- --nocapture` run on a real TTY. - fn confirmation_enabled() -> bool { - let nocapture = std::env::args().any(|a| a == "--nocapture") - || std::env::var_os("RUST_TEST_NOCAPTURE").is_some(); - nocapture && io::stdin().is_terminal() - } - - /// Blanket extension trait: available on every `Iterator`. - pub trait ConfirmExt: Iterator + Sized { - fn confirm_each(self) -> Confirm { - Confirm { - inner: self, - count: 0, - interactive: confirmation_enabled(), - } - } - } - - // Blanket implementation. - impl ConfirmExt for I {} - } - - use test_utils::ConfirmExt; - - /// Note that this example only works with acyclic graphs. - #[test] - fn test_transitive_closure() -> Result<(), anyhow::Error> { - const STEPS: usize = 2; - - let (mut circuit_handle, (edges_input, output_handle)) = Runtime::init_circuit( - worker_threads(), - move |root_circuit| { - let (edges, edges_input) = root_circuit.add_input_zset(); - - // Create a base relation with all paths of length 1. - let len_1 = edges.map(|Tup3(from, to, weight)| Tup4(*from, *to, *weight, 1)); - - let closure = root_circuit.recursive( - |child_circuit, len_n_minus_1: Stream<_, OrdZSet>>| { - // Import the `edges` and `len_1` relation from the parent circuit. - let edges = edges.delta0(child_circuit); - let len_1 = len_1.delta0(child_circuit); - - // Perform an iterative step (n-1 to n) through joining the - // paths of length n-1 with the edges. - let len_n = len_n_minus_1 - .map_index(|Tup4(start, end, cum_weight, hopcnt)| { - ( - *end, - Tup4(*start, *end, *cum_weight, *hopcnt), - ) - }) - .join( - &edges.map_index(|Tup3(from, to, weight)| { - (*from, Tup3(*from, *to, *weight)) - }), - |_end_from, - Tup4(start, _end, cum_weight, hopcnt), - Tup3(_from, to, weight)| { - Tup4(*start, *to, cum_weight + weight, hopcnt + 1) - }, - ).plus(&len_1); - - Ok(len_n) - }, - )?; - - Ok((edges_input, closure.accumulate_output())) - }, - )?; - - let mut edges_data = ([ - // The first clock cycle adds a graph of four nodes: - // |0| -1-> |1| -1-> |2| -2-> |3| -2-> |4| - vec![ - Tup2(Tup3(0_usize, 1_usize, 1_usize), 1_i64), - Tup2(Tup3(1, 2, 1), 1), - Tup2(Tup3(2, 3, 2), 1), - Tup2(Tup3(3, 4, 2), 1), - ], - // The second clock cycle removes the edge |1| -1-> |2|. - vec![Tup2(Tup3(1, 2, 1), -1)], - // The third clock cycle would introduce a cycle but that would - // cause the fixed point computation to never terminate. - // In total, we have the following graph: - // |0| -1-> |1| -1-> |2| -2-> |3| -2-> |4| - // ^ | - // | | - // ------------------3------------------ - // zset_set! { Tup3(1,2,1), Tup3(4, 0, 3)} - ] as [_; STEPS]) - .into_iter(); - - let mut expected_outputs = ([ - // We expect the full transitive closure in the first clock cycle. - zset! { - Tup4(0_usize, 1_usize, 1_usize, 1_usize) => 1, - Tup4(0, 2, 2, 2) => 1, - Tup4(0, 3, 4, 3) => 1, - Tup4(0, 4, 6, 4) => 1, - Tup4(1, 2, 1, 1) => 1, - Tup4(1, 3, 3, 2) => 1, - Tup4(1, 4, 5, 3) => 1, - Tup4(2, 3, 2, 1) => 1, - Tup4(2, 4, 4, 2) => 1, - Tup4(3, 4, 2, 1) => 1, - }, - // These paths are removed in the second clock cycle. - zset! { - Tup4(0, 2, 2, 2) => -1, - Tup4(0, 3, 4, 3) => -1, - Tup4(0, 4, 6, 4) => -1, - Tup4(1, 2, 1, 1) => -1, - Tup4(1, 3, 3, 2) => -1, - Tup4(1, 4, 5, 3) => -1, - }, - // This does not matter anymore, as the computation does not - // terminate anymore due to the cycle. - // zset! {}, - ] as [_; STEPS]) - .into_iter(); - - for _ in 0..STEPS { - edges_input.append(&mut edges_data.next().unwrap()); - circuit_handle.transaction()?; - let output = output_handle.concat().consolidate(); - assert_eq!(output, expected_outputs.next().unwrap()); - } - - Ok(()) - } - - // Taken from the [DBSP docs](https://docs.rs/dbsp/latest/dbsp/circuit/circuit_builder/struct.ChildCircuit.html#method.recursive). - #[test] - fn test_recursive() -> Result<(), anyhow::Error> { - const STEPS: usize = 3; - - // Propagate labels along graph edges. - let (mut circuit, output_handle) = - Runtime::init_circuit(worker_threads(), move |root_circuit| { - // Graph topology. - let mut edges = ([ - // Start with four nodes connected in a cycle. - zset_set! { Tup2(1, 2), Tup2(2, 3), Tup2(3, 4), Tup2(4, 1) }, - // Add an edge. - zset_set! { Tup2(4, 5) }, - // Remove an edge, breaking the cycle. - zset! { Tup2(1, 2) => -1 }, - ] as [OrdZSet>; STEPS]) - .into_iter(); - let edges = root_circuit.add_source(Generator::new(move || edges.next().unwrap())); - - // Initial labeling of the graph. - let mut init_labels = ([ - // Start with a single label on node 1. - zset_set! { Tup2(1, "l1".to_string()) }, - // Add a label to node 2. - zset_set! { Tup2(2, "l2".to_string()) }, - zset! {}, - ] as [OrdZSet>; STEPS]) - .into_iter(); - - let init_labels = - root_circuit.add_source(Generator::new(move || init_labels.next().unwrap())); - - let labels = root_circuit.recursive( - |child_circuit, labels: Stream<_, OrdZSet>>| { - // Import `edges` and `init_labels` relations from the parent circuit. - let edges = edges.delta0(child_circuit); - let init_labels = init_labels.delta0(child_circuit); - - // Given an edge `from -> to` where the `from` node is labeled with `l`, - // propagate `l` to node `to`. - let result = labels - .map_index(|Tup2(x, y)| (*x, y.clone())) - .join(&edges.map_index(|Tup2(x, y)| (*x, *y)), |_from, l, to| { - Tup2(*to, l.clone()) - }) - .plus(&init_labels); - Ok(result) - }, - )?; - - Ok(labels.accumulate_output()) - })?; - - // Expected _changes_ to the output graph labeling after each clock cycle. - let mut expected_outputs = ([ - zset! { - Tup2(1, "l1".to_string()) => 1, - Tup2(2, "l1".to_string()) => 1, - Tup2(3, "l1".to_string()) => 1, - Tup2(4, "l1".to_string()) => 1 - }, - zset! { - Tup2(1, "l2".to_string()) => 1, - Tup2(2, "l2".to_string()) => 1, - Tup2(3, "l2".to_string()) => 1, - Tup2(4, "l2".to_string()) => 1, - Tup2(5, "l1".to_string()) => 1, - Tup2(5, "l2".to_string()) => 1 - }, - zset! { - Tup2(2, "l1".to_string()) => -1, - Tup2(3, "l1".to_string()) => -1, - Tup2(4, "l1".to_string()) => -1, - Tup2(5, "l1".to_string()) => -1 - }, - ] as [OrdZSet>; STEPS]) - .into_iter(); - - for _ in 0..STEPS { - circuit.transaction()?; - let output = output_handle.concat().consolidate(); - assert_eq!(output, expected_outputs.next().unwrap()); - let as_vec = output.iter().collect::>(); - println!("Output: {as_vec:?}"); - } - - Ok(()) - } - - #[test] - fn test_mutual_recursion() -> Result<(), anyhow::Error> { - const STEPS: usize = 3; - - let ( - mut circuit, - ( - ( - alloc_input, - assign_input, - virtual_call_input, - heap_type_input, - dispatch_input, - actual_arg_input, - formal_param_input, - ), - (var_points_to_output, call_graph_output), - ), - ) = Runtime::init_circuit(worker_threads(), move |root_circuit| { - let (alloc, alloc_input) = root_circuit.add_input_zset::(); - let (assign, assign_input) = root_circuit.add_input_zset::(); - let (virtual_call, virtual_call_input) = root_circuit.add_input_zset::(); - let (heap_type, heap_type_input) = root_circuit.add_input_zset::(); - let (dispatch, dispatch_input) = root_circuit.add_input_zset::(); - let (actual_arg, actual_arg_input) = root_circuit.add_input_zset::(); - let (formal_param, formal_param_input) = root_circuit.add_input_zset::(); - - let (var_points_to, call_graph) = root_circuit.recursive( - |child_circuit, - (var_points_to, call_graph): ( - // TODO: Change to OrdIndexedZSet - Stream<_, OrdZSet>, - Stream<_, OrdZSet>, - )| { - // Import streams from the parent circuit into the child circuit. - let alloc = alloc.delta0(child_circuit); - let assign = assign.delta0(child_circuit); - let virtual_call = virtual_call.delta0(child_circuit); - let heap_type = heap_type.delta0(child_circuit); - let dispatch = dispatch.delta0(child_circuit); - let actual_arg = actual_arg.delta0(child_circuit); - let formal_param = formal_param.delta0(child_circuit); - - let call_graph_next = - virtual_call // 1. virtual_call JOIN var_points_to ON recv - .map_index(|Tup3(site, recv, sig)| { - (recv.clone(), (site.clone(), recv.clone(), sig.clone())) - }) - .join_index( - &var_points_to.map_index(|Tup2(recv, obj)| { - (recv.clone(), (recv.clone(), obj.clone())) - }), - |_recv, (site, _, sig), (_, obj)| { - Some(( - obj.clone(), - Tup3(site.clone(), sig.clone(), obj.clone()), - )) - }, - ) - .join_index( - // 2. ... JOIN heap_type ON obj - &heap_type.map_index(|Tup2(obj, ty)| { - (obj.clone(), (obj.clone(), ty.clone())) - }), - |_obj, Tup3(site, sig, _), (_, ty)| { - Some(((ty.clone(), sig.clone()), (site.clone(), ty.clone()))) - }, - ) // 3. ... JOIN dispatch ON ty and sig - .join_index( - &dispatch.map_index(|Tup3(ty, sig, meth)| { - ((ty.clone(), sig.clone()), meth.clone()) - }), - |_, (site, _), meth| { - Some(( - (site.clone(), meth.clone()), - Tup2(site.clone(), meth.clone()), - )) - }, - ); - - let var_points_to_next = var_points_to - .map_index(|Tup2(src, obj)| (src.clone(), (src.clone(), obj.clone()))) - .join_index( - &assign.map_index(|Tup2(dst, src)| { - (src.clone(), (dst.clone(), src.clone())) - }), - |_src, (_, obj), (dst, _)| { - Some(((dst.clone(), obj.clone()), (dst.clone(), obj.clone()))) - }, - ) - .plus(&alloc.map_index(|Tup2(var, obj)| { - ((var.clone(), obj.clone()), (var.clone(), obj.clone())) - })) - .plus( - &call_graph // 1. call_graph JOIN actual_arg ON site - .map_index(|Tup2(site, meth)| { - (site.clone(), (site.clone(), meth.clone())) - }) - .join_index( - &actual_arg.map_index(|Tup2(site, arg)| { - (site.clone(), (site.clone(), arg.clone())) - }), - |_site, (_, meth), (_, arg)| { - Some((meth.clone(), (meth.clone(), arg.clone()))) - }, - ) // .2. ... JOIN formal_param ON meth - .join_index( - &formal_param.map_index(|Tup2(meth, param)| { - (meth.clone(), (meth.clone(), param.clone())) - }), - |_meth, (_, arg), (_, param)| { - Some(((arg.clone()), (arg.clone(), param.clone()))) - }, - ) // 3. ... JOIN var_points_to ON arg - .join_index( - &var_points_to.map_index(|Tup2(arg, obj)| { - (arg.clone(), (arg.clone(), obj.clone())) - }), - |_arg, (_, param), (_, obj)| { - Some(( - (param.clone(), obj.clone()), - (param.clone(), obj.clone()), - )) - }, - ), - ); - - Ok(( - var_points_to_next - .map(|(_, (param, obj))| Tup2(param.clone(), obj.clone())), - call_graph_next.map(|((site, meth), _)| Tup2(site.clone(), meth.clone())), - )) - }, - )?; - - Ok(( - ( - alloc_input, - assign_input, - virtual_call_input, - heap_type_input, - dispatch_input, - actual_arg_input, - formal_param_input, - ), - ( - var_points_to.accumulate_output(), - call_graph.accumulate_output(), - ), - )) - })?; - - // INPUTS - - type Input = Vec>; - type Str2 = (&'static str, &'static str); - type String2 = Tup2; - type Str3 = (&'static str, &'static str, &'static str); - type String3 = Tup3; - - trait CustomInto { - fn into_input(self) -> T; - } - - impl CustomInto for Str2 { - fn into_input(self) -> String2 { - Tup2(self.0.to_string(), self.1.to_string()) - } - } - - impl CustomInto for Str3 { - fn into_input(self) -> String3 { - Tup3(self.0.to_string(), self.1.to_string(), self.2.to_string()) - } - } - - impl CustomInto>> for T - where - Raw: CustomInto, - T: IntoIterator>, - { - fn into_input(self) -> Vec> { - self.into_iter() - .map(|iteration| { - iteration - .into_iter() - .map(|(data, z_weight)| Tup2(data.into_input(), z_weight)) - .collect() - }) - .collect() - } - } - - let mut alloc_inputs = ([ - vec![(("g", "oG"), 1), (("d", "oDog"), 1), (("c", "oCat"), 1)], - vec![(("m", "oMouse"), 1)], - vec![], - ] as [Vec<(Str2, ZWeight)>; STEPS]) - .into_input() - .into_iter(); - - let mut assign_inputs = ([vec![(("ac", "c"), 1)], vec![], vec![(("ac", "c"), -1)]] - as [Vec<(Str2, ZWeight)>; STEPS]) - .into_input() - .into_iter(); - - let mut virtual_call_inputs = ([ - vec![ - (("s1", "g", "greet"), 1), - (("s2", "g", "greet"), 1), - (("s3", "x", "speak"), 1), - ], - vec![(("s4", "g", "greet"), 1)], - vec![(("s2", "g", "greet"), -1)], - ] as [Vec<(Str3, ZWeight)>; STEPS]) - .into_input() - .into_iter(); - - let mut heap_type_inputs = ([ - vec![ - (("oG", "Greeter"), 1), - (("oDog", "Dog"), 1), - (("oCat", "Cat"), 1), - ], - vec![(("oMouse", "Mouse"), 1)], - vec![], - ] as [Vec<(Str2, ZWeight)>; STEPS]) - .into_input() - .into_iter(); - - let mut dispatch_inputs = ([ - vec![ - (("Greeter", "greet", "Greeter.greet"), 1), - (("Dog", "speak", "Dog.speak"), 1), - (("Cat", "speak", "Cat.speak"), 1), - ], - vec![(("Mouse", "speak", "Mouse.speak"), 1)], - vec![], - ] as [Vec<(Str3, ZWeight)>; STEPS]) - .into_input() - .into_iter(); - - let mut actual_arg_inputs = ([ - vec![(("s1", "d"), 1), (("s2", "ac"), 1)], - vec![(("s4", "m"), 1)], - vec![(("s2", "ac"), 1)], - ] as [Vec<(Str2, ZWeight)>; STEPS]) - .into_input() - .into_iter(); - - let mut formal_param_inputs = ([vec![(("Greeter.greet", "x"), 1)], vec![], vec![]] - as [Vec<(Str2, ZWeight)>; STEPS]) - .into_input() - .into_iter(); - - // OUTPUTS - - let mut var_points_to_expected_outputs = ([ - zset! { - Tup2("ac".to_string(), "oCat".to_string()) => 1, - Tup2("c".to_string(), "oCat".to_string()) => 1, - Tup2("d".to_string(), "oDog".to_string()) => 1, - Tup2("g".to_string(), "oG".to_string()) => 1, - Tup2("x".to_string(), "oDog".to_string()) => 1, - Tup2("x".to_string(), "oCat".to_string()) => 1, - }, - zset! { - Tup2("m".to_string(), "oMouse".to_string()) => 1, - Tup2("x".to_string(), "oMouse".to_string()) => 1, - }, - zset! { - Tup2("ac".to_string(), "oCat".to_string()) => -1, - Tup2("x".to_string(), "oCat".to_string()) => -1, - }, - ] as [OrdZSet; STEPS]) - .into_iter(); - - let mut call_graph_expected_outputs = ([ - zset! { - Tup2("s1".to_string(), "Greeter.greet".to_string()) => 1, - Tup2("s2".to_string(), "Greeter.greet".to_string()) => 1, - Tup2("s3".to_string(), "Dog.speak".to_string()) => 1, - Tup2("s3".to_string(), "Cat.speak".to_string()) => 1, - }, - zset! { - Tup2("s3".to_string(), "Mouse.speak".to_string()) => 1, - Tup2("s4".to_string(), "Greeter.greet".to_string()) => 1, - }, - zset! { - Tup2("s2".to_string(), "Greeter.greet".to_string()) => -1, - Tup2("s3".to_string(), "Cat.speak".to_string()) => -1, - }, - ] as [OrdZSet; STEPS]) - .into_iter(); - - // EXECUTION - - for i in (0..STEPS).confirm_each() { - alloc_input.append(&mut alloc_inputs.next().unwrap()); - assign_input.append(&mut assign_inputs.next().unwrap()); - virtual_call_input.append(&mut virtual_call_inputs.next().unwrap()); - heap_type_input.append(&mut heap_type_inputs.next().unwrap()); - dispatch_input.append(&mut dispatch_inputs.next().unwrap()); - actual_arg_input.append(&mut actual_arg_inputs.next().unwrap()); - formal_param_input.append(&mut formal_param_inputs.next().unwrap()); - - circuit.transaction()?; - - let var_points_to_output = var_points_to_output.concat().iter().collect::>(); - assert_eq!( - var_points_to_output, - var_points_to_expected_outputs - .next() - .unwrap() - .iter() - .collect::>() - ); - println!("Var Points To Output: {var_points_to_output:?}"); - - let call_graph_output = call_graph_output.concat().iter().collect::>(); - assert_eq!( - call_graph_output, - call_graph_expected_outputs - .next() - .unwrap() - .iter() - .collect::>() - ); - println!("Call Graph Output: {call_graph_output:?}"); - } - - Ok(()) - } - - // Computes the factorial of the first 10 numbers. - #[test] - fn test_factorial_with_iterate() -> Result<(), anyhow::Error> { - let (mut circuit, output) = Runtime::init_circuit(worker_threads(), |circuit| { - // Generate sequence 0, 1, 2, ... - let mut n: usize = 0; - let source = circuit.add_source(Generator::new(move || { - let result = n; - n += 1; - result - })); - // Compute factorial of each number in the sequence. - let fact = circuit.iterate(|child| { - let counter = Rc::new(RefCell::new(1)); - let counter_clone = Rc::clone(&counter); - let countdown = source.delta0(child).apply(move |parent_val| { - let mut counter_borrow = counter_clone.borrow_mut(); - *counter_borrow += *parent_val; - let res = *counter_borrow; - *counter_borrow -= 1; - res - }); - let (z1_output, z1_feedback) = child.add_feedback_with_export(Z1::new(1)); - let multiplication = - countdown.apply2(&z1_output.local, |n1: &usize, n2: &usize| n1 * n2); - z1_feedback.connect(&multiplication); - // Stop iterating when the counter reaches 0. - Ok((async move || Ok(*counter.borrow() == 0), z1_output.export)) - })?; - Ok(fact.output()) - })?; - - let factorial = |n: usize| (1..=n).product::(); - const ITERATIONS: usize = 10; - for i in 0..ITERATIONS { - circuit.transaction()?; - let result = output.take_from_all(); - let result = result.first().unwrap(); - println!("Iteration {:3}: {:3}! = {}", i + 1, i, result); - assert_eq!(*result, factorial(i)); - } - - Ok(()) - } - // Computes the sum of the first n natural numbers. - #[test] - fn test_sum_n_natural_numbers_with_add_feedback() -> Result<(), anyhow::Error> { - let (mut circuit, output) = Runtime::init_circuit(worker_threads(), |circuit| { - // Create a data source. - let mut n: usize = 1; - let source = circuit.add_source(Generator::new(move || { - let result = n; - n += 1; - result - })); - // Create z1. `z1_output` will contain the output stream of `z1`; `z1_feedback` - // is a placeholder where we can later plug the input to `z1`. - let (z1_output, z1_feedback) = circuit.add_feedback(Z1::new(0)); - // Connect outputs of `source` and `z1` to the plus operator. - let plus = source.apply2(&z1_output, |n1: &usize, n2: &usize| n1 + n2); - // Connect the output of `+` as input to `z1`. - z1_feedback.connect(&plus); - - Ok(z1_output.output()) - })?; - - let sum_first_natural_numbers = |n: usize| (n * (n + 1)) / 2; - let iterations = 10; - for i in 1..=iterations { - circuit.transaction()?; - let result = output.take_from_all(); - let result = result.first().unwrap(); - println!("Step {:3}: \\sum_{{i=0}}^{} i = {}", i, i - 1, result); - assert_eq!(*result, sum_first_natural_numbers(i - 1)); - } - - Ok(()) - } - - #[test] - fn test_not_operator() -> Result<(), anyhow::Error> { - const STEPS: usize = 2; - - let (mut circuit, ((left_input, right_input), output)) = - Runtime::init_circuit(worker_threads(), |root_circuit| { - let (left, left_input) = - root_circuit.add_input_indexed_zset::, Tup2>(); - - let (right, right_input) = - root_circuit.add_input_indexed_zset::, Tup2>(); - - let set_minus = left.minus(&right); - - Ok(((left_input, right_input), set_minus.accumulate_output())) - })?; - - let mut left_data = ([ - vec![ - Tup2(Tup2(1_usize, 1_usize), Tup2(Tup2(1_usize, 1_usize), 1_i64)), - Tup2(Tup2(1, 2), Tup2(Tup2(1, 2), 1)), - Tup2(Tup2(1, 3), Tup2(Tup2(1, 3), 1)), - Tup2(Tup2(1, 4), Tup2(Tup2(1, 4), 1)), - ], - vec![], - ] as [_; STEPS]) - .into_iter(); - - let mut right_data = ([ - vec![ - Tup2(Tup2(1_usize, 2_usize), Tup2(Tup2(1_usize, 2_usize), 1_i64)), - Tup2(Tup2(1, 3), Tup2(Tup2(1, 3), 1)), - ], - vec![Tup2(Tup2(1, 4), Tup2(Tup2(1, 4), 1))], - ] as [_; STEPS]) - .into_iter(); - - let mut expected_outputs = ([ - indexed_zset! {Tup2 => Tup2: - Tup2(1, 1) => { Tup2(1, 1) => 1 }, - Tup2(1, 4) => { Tup2(1, 4) => 1 }, - }, - indexed_zset! {Tup2 => Tup2: - Tup2(1, 4) => { Tup2(1, 4) => -1 }, - }, - ] as [_; STEPS]) - .into_iter(); - - for i in 1..=STEPS { - left_input.append(&mut left_data.next().unwrap()); - right_input.append(&mut right_data.next().unwrap()); - circuit.transaction()?; - let output = output.concat().consolidate(); - println!("{output:?}"); - assert_eq!(output, expected_outputs.next().unwrap()); - } - - Ok(()) - } - - #[test] - fn test_cartesian_product() -> Result<(), anyhow::Error> { - const STEPS: usize = 2; - - let (mut circuit, ((left_input, right_input), output)) = - Runtime::init_circuit(worker_threads(), |root_circuit| { - let (left, left_input) = - root_circuit.add_input_indexed_zset::, Tup2>(); - let left = left.map_index(|(_k, v)| ((), *v)); - - let (right, right_input) = - root_circuit.add_input_indexed_zset::, Tup2>(); - let right = right.map_index(|(_k, v)| ((), *v)); - - let cartesian_product = - left.join_index(&right, |_k, Tup2(l1, l2), Tup2(r1, r2)| { - // Merge left and right tuples. - Some(((), Tup4(*l1, *l2, *r1, *r2))) - }); - - Ok(( - (left_input, right_input), - cartesian_product.accumulate_output(), - )) - })?; - - let mut left_data = ([ - vec![ - Tup2(Tup2(1_usize, 1_usize), Tup2(Tup2(1_usize, 1_usize), 1_i64)), - Tup2(Tup2(1, 1), Tup2(Tup2(1, 1), 1)), // duplicate of the above! - Tup2(Tup2(1, 2), Tup2(Tup2(1, 2), 1)), - Tup2(Tup2(1, 3), Tup2(Tup2(1, 3), 1)), - ], - vec![], - ] as [_; STEPS]) - .into_iter(); - - let mut right_data = ([ - vec![ - Tup2(Tup2(2_usize, 1_usize), Tup2(Tup2(2_usize, 1_usize), 1_i64)), - Tup2(Tup2(2, 2), Tup2(Tup2(2, 2), 1)), - ], - vec![Tup2(Tup2(2, 3), Tup2(Tup2(2, 3), 1))], - ] as [_; STEPS]) - .into_iter(); - - let mut expected_outputs = ([ - indexed_zset! {() => Tup4: - () => { Tup4(1, 1, 2, 1) => 2 }, - () => { Tup4(1, 1, 2, 2) => 2 }, - () => { Tup4(1, 2, 2, 1) => 1 }, - () => { Tup4(1, 2, 2, 2) => 1 }, - () => { Tup4(1, 3, 2, 1) => 1 }, - () => { Tup4(1, 3, 2, 2) => 1 }, - }, - indexed_zset! {() => Tup4: - () => { Tup4(1, 1, 2, 3) => 2 }, - () => { Tup4(1, 2, 2, 3) => 1 }, - () => { Tup4(1, 3, 2, 3) => 1 }, - }, - ] as [_; STEPS]) - .into_iter(); - - for i in 1..=STEPS { - left_input.append(&mut left_data.next().unwrap()); - right_input.append(&mut right_data.next().unwrap()); - circuit.transaction()?; - let result = output.concat().consolidate(); - println!("Result in {i}: {result:?}"); - assert_eq!(result, expected_outputs.next().unwrap()); - } - - Ok(()) - } - - #[test] - fn negative_zweight_behavior() -> Result<(), anyhow::Error> { - const STEPS: usize = 6; - - let (mut circuit, ((left_input, right_input), output)) = - Runtime::init_circuit(worker_threads(), |root_circuit| { - let (left, left_input) = - root_circuit.add_input_indexed_zset::, Tup2>(); - - let (right, right_input) = - root_circuit.add_input_indexed_zset::, Tup2>(); - - let joined = left.join_index(&right, |k, Tup2(l1, l2), Tup2(r1, r2)| { - // Merge left and right tuples. - Some((*k, Tup4(*l1, *l2, *r1, *r2))) - }); - - Ok(((left_input, right_input), joined.accumulate_output())) - })?; - - let mut left_data = ([ - vec![Tup2( - Tup2(1_usize, 1_usize), - Tup2(Tup2(1_usize, 1_usize), 1_i64), - )], - vec![Tup2(Tup2(1, 1), Tup2(Tup2(1, 1), -3))], - vec![Tup2(Tup2(1, 1), Tup2(Tup2(1, 1), 1))], - vec![Tup2(Tup2(1, 1), Tup2(Tup2(1, 1), 1))], - vec![Tup2(Tup2(1, 1), Tup2(Tup2(1, 1), 1))], - vec![Tup2(Tup2(1, 1), Tup2(Tup2(1, 1), 0))], - ] as [_; STEPS]) - .into_iter(); - - let mut right_data = ([ - vec![Tup2( - Tup2(1_usize, 1_usize), - Tup2(Tup2(2_usize, 2_usize), 1_i64), - )], - vec![], - vec![], - vec![], - vec![], - vec![], - ] as [_; STEPS]) - .into_iter(); - - let mut expected_outputs = ([ - indexed_zset! {Tup2 => Tup4: - Tup2(1, 1) => { Tup4(1, 1, 2, 2) => 1 }, // "Lifetime" Total: +1 - }, - indexed_zset! {Tup2 => Tup4: - Tup2(1, 1) => { Tup4(1, 1, 2, 2) => -3 }, // "Lifetime" Total: -2 - }, - indexed_zset! {Tup2 => Tup4: - Tup2(1, 1) => { Tup4(1, 1, 2, 2) => 1 }, // "Lifetime" Total: -1 - }, - indexed_zset! {Tup2 => Tup4: - Tup2(1, 1) => { Tup4(1, 1, 2, 2) => 1 }, // "Lifetime" Total: +0 - }, - indexed_zset! {Tup2 => Tup4: - Tup2(1, 1) => { Tup4(1, 1, 2, 2) => 1 }, // "Lifetime" Total: +1 - }, - indexed_zset! {Tup2 => Tup4: - // No output delta because the zweight is 0; "Lifetime" Total: +1 - }, - ] as [_; STEPS]) - .into_iter(); - - for i in 1..=STEPS { - left_input.append(&mut left_data.next().unwrap()); - right_input.append(&mut right_data.next().unwrap()); - circuit.transaction()?; - let output = output.concat().consolidate(); - println!("OUTPUT DELTA IN {i}: {output:?}"); - assert_eq!(output, expected_outputs.next().unwrap()); - } - - Ok(()) - } - - #[test] - fn multiple_outputs() -> Result<(), anyhow::Error> { - const STEPS: usize = 2; - - let (mut circuit, ((left_input, right_input), (filter_output, join_output))) = - Runtime::init_circuit(worker_threads(), |root_circuit| { - let (left, left_input) = - root_circuit.add_input_indexed_zset::, Tup2>(); - - let (right, right_input) = - root_circuit.add_input_indexed_zset::, Tup2>(); - - let left_filtered = left.filter(|(k, v)| k.0 == 2); - - let joined = left_filtered.join_index(&right, |k, Tup2(l1, l2), Tup2(r1, r2)| { - // Merge left and right tuples. - Some((*k, Tup4(*l1, *l2, *r1, *r2))) - }); - - // We output both the intermediate filter result and the final join result. - Ok(( - (left_input, right_input), - ( - left_filtered.accumulate_output(), - joined.accumulate_output(), - ), - )) - })?; - - let mut left_data = ([ - vec![ - Tup2(Tup2(1_usize, 1_usize), Tup2(Tup2(1_usize, 1_usize), 1_i64)), - Tup2(Tup2(2, 1), Tup2(Tup2(1, 1), 1)), - Tup2(Tup2(2, 3), Tup2(Tup2(1, 1), 1)), - ], - vec![ - Tup2(Tup2(1, 2), Tup2(Tup2(1, 1), 1)), - Tup2(Tup2(2, 2), Tup2(Tup2(1, 1), 1)), - ], - ] as [_; STEPS]) - .into_iter(); - - let mut right_data = ([ - vec![ - Tup2(Tup2(2_usize, 1_usize), Tup2(Tup2(2_usize, 2_usize), 1_i64)), - Tup2(Tup2(2, 2), Tup2(Tup2(2, 2), 1)), - ], - vec![Tup2(Tup2(2, 3), Tup2(Tup2(2, 2), 1))], - ] as [_; STEPS]) - .into_iter(); - - let mut expected_filter_outputs = ([ - indexed_zset! {Tup2 => Tup2: - Tup2(2, 1) => { Tup2(1, 1) => 1 }, - Tup2(2, 3) => { Tup2(1, 1) => 1 }, - }, - indexed_zset! {Tup2 => Tup2: - Tup2(2, 2) => { Tup2(1, 1) => 1 }, - }, - ] as [_; STEPS]) - .into_iter(); - - let mut expected_join_outputs = ([ - indexed_zset! {Tup2 => Tup4: - Tup2(2, 1) => { Tup4(1, 1, 2, 2) => 1 }, - }, - indexed_zset! {Tup2 => Tup4: - Tup2(2, 2) => { Tup4(1, 1, 2, 2) => 1 }, - Tup2(2, 3) => { Tup4(1, 1, 2, 2) => 1 }, - }, - ] as [_; STEPS]) - .into_iter(); - - for i in 1..=STEPS { - left_input.append(&mut left_data.next().unwrap()); - right_input.append(&mut right_data.next().unwrap()); - circuit.transaction()?; - let filter_result = filter_output.concat().consolidate(); - println!("FILTER {filter_result:?}"); - assert_eq!(filter_result, expected_filter_outputs.next().unwrap()); - let join_result = join_output.concat().consolidate(); - println!("JOIN {join_result:?}"); - assert_eq!(join_result, expected_join_outputs.next().unwrap()); - } - - Ok(()) - } - - #[test] - fn rollback_test() -> Result<(), anyhow::Error> { - const STEPS: usize = 4; - - let (mut circuit, ((left_input, right_input), output)) = - Runtime::init_circuit(worker_threads(), |root_circuit| { - let (left, left_input) = - root_circuit.add_input_indexed_zset::, Tup2>(); - - let (right, right_input) = - root_circuit.add_input_indexed_zset::, Tup2>(); - - let antijoined = left.antijoin(&right); - - Ok(((left_input, right_input), antijoined.accumulate_output())) - })?; - - let mut left_data = ([ - // The initial transaction. - vec![Tup2( - Tup2(1_usize, 1_usize), - // Note: Only the key matters for determining equality. - Tup2(Tup2(1_usize, 1_usize), 1_i64), - )], - // A transaction which causes a constraint violation. - vec![Tup2(Tup2(1, 2), Tup2(Tup2(1, 2), 1))], - // We rollback/undo the previous transaction (by inverting the zweight). - vec![Tup2(Tup2(1, 2), Tup2(Tup2(1, 2), -1))], - // A new transaction which does not violate any constraint. - vec![Tup2(Tup2(1, 2), Tup2(Tup2(1, 2), 1))], - ] as [_; STEPS]) - .into_iter(); - - let mut right_data = ([ - // The initial transaction. - vec![Tup2( - Tup2(1_usize, 1_usize), - // Note: Only the key matters for determining equality. - Tup2(Tup2(2_usize, 2_usize), 1_i64), - )], - // A transaction which causes a constraint violation. - vec![], - // We rollback/undo the previous transaction (by inverting the zweight). - vec![], - // A new transaction which does not violate any constraint. - vec![ - Tup2(Tup2(1, 2), Tup2(Tup2(1, 2), 1)), - // For L ANTIJOIN R it is okay to have R carry "excess" tuples. - Tup2(Tup2(1, 3), Tup2(Tup2(1, 3), 1)), - ], - ] as [_; STEPS]) - .into_iter(); - - let mut expected_outputs = ([ - indexed_zset! {Tup2 => Tup2: - // Empty output, i.e., no constraint violated. - }, - indexed_zset! {Tup2 => Tup2: - // Violation due to non-empty result. - Tup2(1, 2) => { Tup2(1, 2) => 1 }, - }, - indexed_zset! {Tup2 => Tup2: - // Rollback cleans operator state, result does not matter. - Tup2(1, 2) => { Tup2(1, 2) => -1 }, - }, - indexed_zset! {Tup2 => Tup2: - // Empty output, i.e., no constraint violated. - }, - ] as [_; STEPS]) - .into_iter(); - - for i in 1..=STEPS { - left_input.append(&mut left_data.next().unwrap()); - right_input.append(&mut right_data.next().unwrap()); - circuit.transaction()?; - let output = output.concat().consolidate(); - println!("OUTPUT DELTA IN {i}: {output:?}"); - assert_eq!(output, expected_outputs.next().unwrap()); - } - - Ok(()) - } -} diff --git a/packages/coln-query/src/dbsp/cli_table.rs b/packages/coln-query/src/dbsp/cli_table.rs new file mode 100644 index 00000000..099a88cd --- /dev/null +++ b/packages/coln-query/src/dbsp/cli_table.rs @@ -0,0 +1,73 @@ +use cli_table::{ + Cell, CellStruct, Table, + format::{Border, HorizontalLine, Justify, Separator}, +}; +use dbsp::{ZWeight, utils::Tup2}; +use std::fmt::Debug; + +pub trait AsCliTableRow { + fn cli_table_header() -> Vec; + fn as_cli_table_row(&self) -> Vec; +} + +impl AsCliTableRow for &T { + fn cli_table_header() -> Vec { + T::cli_table_header() + } + fn as_cli_table_row(&self) -> Vec { + T::as_cli_table_row(*self) + } +} + +pub trait ToCliTable { + fn to_cli_table(self) -> impl std::fmt::Display; +} + +impl ToCliTable for Iter +where + Iter: IntoIterator, + Iter::Item: AsCliTableRow, +{ + fn to_cli_table(self) -> impl std::fmt::Display { + let double_h_line = HorizontalLine::new('=', '=', '≠', '='); + let single_h_line = HorizontalLine::new('-', '-', '+', '-'); + self.into_iter() + .map(|row| row.as_cli_table_row()) + .table() + .border( + Border::builder() + .top(double_h_line) + .bottom(double_h_line) + .build(), + ) + .separator(Separator::builder().title(Some(single_h_line)).build()) + .title(Iter::Item::cli_table_header()) + .display() + .expect("Table build error") + } +} + +impl AsCliTableRow for (K, V, ZWeight) { + fn cli_table_header() -> Vec { + vec!["z-weight".cell(), "key".cell(), "value".cell()] + } + fn as_cli_table_row(&self) -> Vec { + vec![ + format!("{:?}", self.2).cell().justify(Justify::Right), + format!("{:?}", self.0).cell().justify(Justify::Left), + format!("{:?}", self.1).cell().justify(Justify::Left), + ] + } +} + +impl AsCliTableRow for Tup2 { + fn cli_table_header() -> Vec { + vec!["z-weight".cell(), "key".cell()] + } + fn as_cli_table_row(&self) -> Vec { + vec![ + format!("{:?}", self.1).cell().justify(Justify::Right), + format!("{:?}", self.0).cell().justify(Justify::Left), + ] + } +} diff --git a/packages/coln-query/src/dbsp/mod.rs b/packages/coln-query/src/dbsp/mod.rs new file mode 100644 index 00000000..ad0023f1 --- /dev/null +++ b/packages/coln-query/src/dbsp/mod.rs @@ -0,0 +1,6 @@ +mod cli_table; +#[cfg(test)] +mod playground; +mod wrapper; + +pub use wrapper::*; diff --git a/packages/coln-query/src/dbsp/playground.rs b/packages/coln-query/src/dbsp/playground.rs new file mode 100644 index 00000000..6987e89c --- /dev/null +++ b/packages/coln-query/src/dbsp/playground.rs @@ -0,0 +1,1214 @@ +//! This module is feature-gated behind `test`. It only contains raw DBSP tests +//! and experiments without any wrappers from our end. + +mod utils { + use dbsp::ZWeight; + use dbsp::utils::Tup2; + use std::io::{self, IsTerminal, Write}; + use std::num::NonZeroUsize; + + pub fn worker_threads() -> NonZeroUsize { + std::thread::available_parallelism().unwrap_or(NonZeroUsize::new(4).unwrap()) + } + + /// Iterator adapter that waits for a keypress before yielding each item if in + /// interactive mode. Useful for observing an incremental computation step by step. + pub struct Confirm { + iter: Iter, + interactive: bool, + } + + impl Confirm { + fn new(iter: Iter, interactive: bool) -> Self { + Self { iter, interactive } + } + } + + // If the underlying Iter is an ExactSizeIterator make Confirm + // be one, too. + impl ExactSizeIterator for Confirm {} + + impl Iterator for Confirm { + type Item = Iter::Item; + + fn next(&mut self) -> Option { + if !self.interactive { + return self.iter.next(); + } + print!("Press Enter to continue (Ctrl-D to stop)..."); + io::stdout().flush().ok(); + let mut line = String::new(); + match io::stdin().read_line(&mut line) { + Ok(0) | Err(_) => None, // EOF / Ctrl-D -> stop + Ok(_) => self.iter.next(), + } + } + + fn size_hint(&self) -> (usize, Option) { + self.iter.size_hint() + } + } + + /// Whether we're in an interactive `cargo test -- --nocapture` + /// and run on a real TTY. + fn confirmation_enabled() -> bool { + let nocapture = || { + std::env::args().any(|a| a == "--nocapture") + || std::env::var_os("RUST_TEST_NOCAPTURE").is_some() + }; + io::stdin().is_terminal() && nocapture() + } + + /// Blanket extension trait with a default impl on every `Iterator`. + pub trait ConfirmIterExt: Iterator + Sized { + fn confirm_each_auto(self) -> Confirm { + Confirm::new(self, confirmation_enabled()) + } + fn confirm_each(self, interactive: bool) -> Confirm { + Confirm::new(self, interactive) + } + } + + // Blanket implementation for Iterators. + impl ConfirmIterExt for Iter {} + + pub struct Bounded { + bound: usize, + delim: char, + } + impl Bounded { + fn new(bound: usize) -> Self { + Self { bound, delim: '/' } + } + } + pub struct Unbounded; + + /// Iterator adaptor that displays the progress for an iterator. + pub struct Progress { + iter: Iter, + count: usize, + prefix: char, + suffix: char, + bound: Bound, + } + + impl Progress { + fn new(iter: Iter) -> Self { + Self { + iter, + count: 0, + prefix: '\n', + suffix: ' ', + bound: Unbounded, + } + } + pub fn with_delims(mut self, prefix: char, suffix: char) -> Self { + self.prefix = prefix; + self.suffix = suffix; + self + } + } + + impl Progress { + pub fn with_bound(self) -> Progress { + let bound = self.iter.len(); + Progress { + iter: self.iter, + count: self.count, + prefix: self.prefix, + suffix: self.suffix, + bound: Bounded::new(bound), + } + } + } + + impl Progress { + pub fn with_delims( + mut self, + progress_prefix: char, + bounded_delim: char, + progress_suffix: char, + ) -> Self { + self.prefix = progress_prefix; + self.suffix = progress_suffix; + self.bound.delim = bounded_delim; + self + } + } + + trait ProgressDisplay + where + Self: Sized, + { + fn display(&self, progress: &Progress) -> impl std::fmt::Display; + } + + impl ProgressDisplay for Bounded { + fn display(&self, progress: &Progress) -> impl std::fmt::Display { + if progress.count > self.bound { + return "DONE".to_string(); + } + format!("{}{}{}", progress.count, self.delim, self.bound) + } + } + + impl ProgressDisplay for Unbounded { + fn display(&self, progress: &Progress) -> impl std::fmt::Display { + format!("{}", progress.count) + } + } + + // If the underlying Iter is an ExactSizeIterator make Progress + // be one, too. + impl ExactSizeIterator for Progress {} + + impl Iterator for Progress + where + Iter: Iterator, + Bound: ProgressDisplay, + { + type Item = Iter::Item; + + fn next(&mut self) -> Option { + self.count += 1; + print!("{}{}{}", self.prefix, self.bound.display(self), self.suffix); + self.iter.next() + } + + fn size_hint(&self) -> (usize, Option) { + self.iter.size_hint() + } + } + + pub trait ProgressIterExt: Iterator + Sized { + fn progress(self) -> Progress; + } + + // Blanket implementation for Iterators. + impl ProgressIterExt for Iter { + fn progress(self) -> Progress { + Progress::new(self) + } + } + + pub type ZWeightElement = Tup2; + pub type ZWeightIndexElement = Tup2>; + pub type Batch = Vec>; + pub type IndexBatch = Vec>; +} + +use super::cli_table::ToCliTable; +use dbsp::{ + Circuit, IndexedZSetReader, OrdZSet, Runtime, Stream, ZWeight, indexed_zset, + operator::{Generator, Z1}, + utils::{Tup2, Tup3, Tup4}, + zset, +}; +use std::{cell::RefCell, rc::Rc}; +use utils::{Batch, IndexBatch, worker_threads}; +use utils::{ConfirmIterExt, ProgressIterExt}; + +/// Note that this example only works with acyclic graphs. +#[test] +fn test_transitive_closure() -> Result<(), anyhow::Error> { + const STEPS: usize = 2; + + let (mut circuit_handle, (edges_input, output_handle)) = + Runtime::init_circuit(worker_threads(), move |root_circuit| { + let (edges, edges_input) = root_circuit.add_input_zset(); + + // Create a base relation with all paths of length 1. + let len_1 = edges.map(|Tup3(from, to, weight)| Tup4(*from, *to, *weight, 1)); + + let closure = root_circuit.recursive( + |child_circuit, len_n_minus_1: Stream<_, OrdZSet>>| { + // Import the `edges` and `len_1` relation from the parent circuit. + let edges = edges.delta0(child_circuit); + let len_1 = len_1.delta0(child_circuit); + + // Perform an iterative step (n-1 to n) through joining the + // paths of length n-1 with the edges. + let len_n = len_n_minus_1 + .map_index(|Tup4(start, end, cum_weight, hopcnt)| { + ( + *end, + Tup4(*start, *end, *cum_weight, *hopcnt), + ) + }) + .join( + &edges.map_index(|Tup3(from, to, weight)| { + (*from, Tup3(*from, *to, *weight)) + }), + |_end_from, + Tup4(start, _end, cum_weight, hopcnt), + Tup3(_from, to, weight)| { + Tup4(*start, *to, cum_weight + weight, hopcnt + 1) + }, + ).plus(&len_1); + + Ok(len_n) + }, + )?; + + Ok((edges_input, closure.accumulate_output())) + })?; + + let mut edges_data = ([ + // The first clock cycle adds a graph of four nodes: + // |0| -1-> |1| -1-> |2| -2-> |3| -2-> |4| + vec![ + Tup2(Tup3(0_usize, 1_usize, 1_usize), 1_i64), + Tup2(Tup3(1, 2, 1), 1), + Tup2(Tup3(2, 3, 2), 1), + Tup2(Tup3(3, 4, 2), 1), + ], + // The second clock cycle removes the edge |1| -1-> |2|. + vec![Tup2(Tup3(1, 2, 1), -1)], + // The third clock cycle would introduce a cycle but that would + // cause the fixed point computation to never terminate. + // In total, we have the following graph: + // |0| -1-> |1| -1-> |2| -2-> |3| -2-> |4| + // ^ | + // | | + // ------------------3------------------ + // zset_set! { Tup3(1,2,1), Tup3(4, 0, 3)} + ] as [_; STEPS]) + .into_iter(); + + let mut expected_outputs = ([ + // We expect the full transitive closure in the first clock cycle. + zset! { + Tup4(0_usize, 1_usize, 1_usize, 1_usize) => 1, + Tup4(0, 2, 2, 2) => 1, + Tup4(0, 3, 4, 3) => 1, + Tup4(0, 4, 6, 4) => 1, + Tup4(1, 2, 1, 1) => 1, + Tup4(1, 3, 3, 2) => 1, + Tup4(1, 4, 5, 3) => 1, + Tup4(2, 3, 2, 1) => 1, + Tup4(2, 4, 4, 2) => 1, + Tup4(3, 4, 2, 1) => 1, + }, + // These paths are removed in the second clock cycle. + zset! { + Tup4(0, 2, 2, 2) => -1, + Tup4(0, 3, 4, 3) => -1, + Tup4(0, 4, 6, 4) => -1, + Tup4(1, 2, 1, 1) => -1, + Tup4(1, 3, 3, 2) => -1, + Tup4(1, 4, 5, 3) => -1, + }, + // This does not matter anymore, as the computation does not + // terminate anymore due to the cycle. + // zset! {}, + ] as [_; STEPS]) + .into_iter(); + + for _ in 0..STEPS { + edges_input.append(&mut edges_data.next().unwrap()); + circuit_handle.transaction()?; + let output = output_handle.concat().consolidate(); + assert_eq!(output, expected_outputs.next().unwrap()); + } + + Ok(()) +} + +// Adapted from the [DBSP docs](https://docs.rs/dbsp/latest/dbsp/circuit/circuit_builder/struct.ChildCircuit.html#method.recursive). +#[test] +fn test_recursive() -> Result<(), anyhow::Error> { + const STEPS: usize = 3; + + // Propagate labels along graph edges. + let (mut circuit, ((edges_input, init_labels_input), labels_output)) = + Runtime::init_circuit(worker_threads(), move |root_circuit| { + let (edges, edges_input) = root_circuit.add_input_zset::>(); + let (init_labels, init_labels_input) = + root_circuit.add_input_zset::>(); + + let labels = root_circuit.recursive( + |child_circuit, labels: Stream<_, OrdZSet>>| { + // Import `edges` and `init_labels` relations from the parent circuit. + let edges = edges.delta0(child_circuit); + let init_labels = init_labels.delta0(child_circuit); + + // Given an edge `from -> to` where the `from` node is labeled with `l`, + // propagate `l` to node `to`. + let result = labels + .map_index(|Tup2(x, y)| (*x, y.clone())) + .join(&edges.map_index(|Tup2(x, y)| (*x, *y)), |_from, l, to| { + Tup2(*to, l.clone()) + }) + .plus(&init_labels); + Ok(result) + }, + )?; + + Ok(((edges_input, init_labels_input), labels.accumulate_output())) + })?; + + // Graph topology. + let mut edges_inputs = ([ + // Start with four nodes connected in a cycle. + vec![ + Tup2(Tup2(1, 2), 1), + Tup2(Tup2(2, 3), 1), + Tup2(Tup2(3, 4), 1), + Tup2(Tup2(4, 1), 1), + ], + // Add an edge. + vec![Tup2(Tup2(4, 5), 1)], + // Remove an edge, breaking the cycle. + vec![Tup2(Tup2(1, 2), -1)], + ] as [Vec, ZWeight>>; STEPS]) + .into_iter(); + + // Initial labeling of the graph. + let mut init_labels_inputs = ([ + // Start with a single label on node 1. + vec![Tup2(Tup2(1, "l1".to_string()), 1)], + // Add a label to node 2. + vec![Tup2(Tup2(2, "l2".to_string()), 1)], + vec![], + ] as [Vec, ZWeight>>; STEPS]) + .into_iter(); + + // Expected _changes_ to the output graph labeling after each clock cycle. + let mut label_expected_outputs = ([ + zset! { + Tup2(1, "l1".to_string()) => 1, + Tup2(2, "l1".to_string()) => 1, + Tup2(3, "l1".to_string()) => 1, + Tup2(4, "l1".to_string()) => 1 + }, + zset! { + Tup2(1, "l2".to_string()) => 1, + Tup2(2, "l2".to_string()) => 1, + Tup2(3, "l2".to_string()) => 1, + Tup2(4, "l2".to_string()) => 1, + Tup2(5, "l1".to_string()) => 1, + Tup2(5, "l2".to_string()) => 1 + }, + zset! { + Tup2(2, "l1".to_string()) => -1, + Tup2(3, "l1".to_string()) => -1, + Tup2(4, "l1".to_string()) => -1, + Tup2(5, "l1".to_string()) => -1 + }, + ] as [OrdZSet>; STEPS]) + .into_iter(); + + for _ in (0..STEPS).confirm_each_auto().progress().with_bound() { + edges_input.append(&mut edges_inputs.next().unwrap()); + init_labels_input.append(&mut init_labels_inputs.next().unwrap()); + circuit.transaction()?; + let labels_output = labels_output.concat(); + print!("Labels\n{}", labels_output.iter().to_cli_table()); + assert_eq!( + labels_output.consolidate(), + label_expected_outputs.next().unwrap() + ); + } + + Ok(()) +} + +#[test] +fn test_not_operator() -> Result<(), anyhow::Error> { + const STEPS: usize = 2; + + let (mut circuit, ((left_input, right_input), output)) = + Runtime::init_circuit(worker_threads(), |root_circuit| { + let (left, left_input) = + root_circuit.add_input_indexed_zset::, Tup2>(); + + let (right, right_input) = + root_circuit.add_input_indexed_zset::, Tup2>(); + + let set_minus = left.minus(&right); + + Ok(((left_input, right_input), set_minus.accumulate_output())) + })?; + + let mut left_data = ([ + vec![ + Tup2(Tup2(1, 1), Tup2(Tup2(1, 1), 1)), + Tup2(Tup2(1, 2), Tup2(Tup2(1, 2), 1)), + Tup2(Tup2(1, 3), Tup2(Tup2(1, 3), 1)), + Tup2(Tup2(1, 4), Tup2(Tup2(1, 4), 1)), + ], + vec![], + ] as [IndexBatch, Tup2>; STEPS]) + .into_iter(); + + let mut right_data = ([ + vec![ + Tup2(Tup2(1, 2), Tup2(Tup2(1, 2), 1)), + Tup2(Tup2(1, 3), Tup2(Tup2(1, 3), 1)), + ], + vec![Tup2(Tup2(1, 4), Tup2(Tup2(1, 4), 1))], + ] as [IndexBatch, Tup2>; STEPS]) + .into_iter(); + + let mut expected_outputs = ([ + indexed_zset! {Tup2 => Tup2: + Tup2(1, 1) => { Tup2(1, 1) => 1 }, + Tup2(1, 4) => { Tup2(1, 4) => 1 }, + }, + indexed_zset! {Tup2 => Tup2: + Tup2(1, 4) => { Tup2(1, 4) => -1 }, + }, + ] as [_; STEPS]) + .into_iter(); + + for i in (0..STEPS).confirm_each_auto().progress().with_bound() { + left_input.append(&mut left_data.next().unwrap()); + right_input.append(&mut right_data.next().unwrap()); + circuit.transaction()?; + let output = output.concat(); + print!("{}", output.iter().to_cli_table()); + assert_eq!(output.consolidate(), expected_outputs.next().unwrap()); + } + + Ok(()) +} + +#[test] +fn test_cartesian_product() -> Result<(), anyhow::Error> { + const STEPS: usize = 2; + + let (mut circuit, ((left_input, right_input), output)) = + Runtime::init_circuit(worker_threads(), |root_circuit| { + let (left, left_input) = + root_circuit.add_input_indexed_zset::, Tup2>(); + let left = left.map_index(|(_k, v)| ((), *v)); + + let (right, right_input) = + root_circuit.add_input_indexed_zset::, Tup2>(); + let right = right.map_index(|(_k, v)| ((), *v)); + + let cartesian_product = left.join_index(&right, |_k, Tup2(l1, l2), Tup2(r1, r2)| { + // Merge left and right tuples. + Some(((), Tup4(*l1, *l2, *r1, *r2))) + }); + + Ok(( + (left_input, right_input), + cartesian_product.accumulate_output(), + )) + })?; + + let mut left_data = ([ + vec![ + Tup2(Tup2(1_usize, 1_usize), Tup2(Tup2(1_usize, 1_usize), 1_i64)), + Tup2(Tup2(1, 1), Tup2(Tup2(1, 1), 1)), // duplicate of the above! + Tup2(Tup2(1, 2), Tup2(Tup2(1, 2), 1)), + Tup2(Tup2(1, 3), Tup2(Tup2(1, 3), 1)), + ], + vec![], + ] as [_; STEPS]) + .into_iter(); + + let mut right_data = ([ + vec![ + Tup2(Tup2(2_usize, 1_usize), Tup2(Tup2(2_usize, 1_usize), 1_i64)), + Tup2(Tup2(2, 2), Tup2(Tup2(2, 2), 1)), + ], + vec![Tup2(Tup2(2, 3), Tup2(Tup2(2, 3), 1))], + ] as [_; STEPS]) + .into_iter(); + + let mut expected_outputs = ([ + indexed_zset! {() => Tup4: + () => { Tup4(1, 1, 2, 1) => 2 }, + () => { Tup4(1, 1, 2, 2) => 2 }, + () => { Tup4(1, 2, 2, 1) => 1 }, + () => { Tup4(1, 2, 2, 2) => 1 }, + () => { Tup4(1, 3, 2, 1) => 1 }, + () => { Tup4(1, 3, 2, 2) => 1 }, + }, + indexed_zset! {() => Tup4: + () => { Tup4(1, 1, 2, 3) => 2 }, + () => { Tup4(1, 2, 2, 3) => 1 }, + () => { Tup4(1, 3, 2, 3) => 1 }, + }, + ] as [_; STEPS]) + .into_iter(); + + for i in (0..STEPS).confirm_each_auto().progress().with_bound() { + left_input.append(&mut left_data.next().unwrap()); + right_input.append(&mut right_data.next().unwrap()); + circuit.transaction()?; + let output = output.concat(); + print!("{}", output.iter().to_cli_table()); + assert_eq!(output.consolidate(), expected_outputs.next().unwrap()); + } + + Ok(()) +} + +#[test] +fn negative_zweight_behavior() -> Result<(), anyhow::Error> { + const STEPS: usize = 6; + + let (mut circuit, ((left_input, right_input), output)) = + Runtime::init_circuit(worker_threads(), |root_circuit| { + let (left, left_input) = + root_circuit.add_input_indexed_zset::, Tup2>(); + + let (right, right_input) = + root_circuit.add_input_indexed_zset::, Tup2>(); + + let joined = left.join_index(&right, |k, Tup2(l1, l2), Tup2(r1, r2)| { + // Merge left and right tuples. + Some((*k, Tup4(*l1, *l2, *r1, *r2))) + }); + + Ok(((left_input, right_input), joined.accumulate_output())) + })?; + + let mut left_data = ([ + vec![Tup2( + Tup2(1_usize, 1_usize), + Tup2(Tup2(1_usize, 1_usize), 1_i64), + )], + vec![Tup2(Tup2(1, 1), Tup2(Tup2(1, 1), -3))], + vec![Tup2(Tup2(1, 1), Tup2(Tup2(1, 1), 1))], + vec![Tup2(Tup2(1, 1), Tup2(Tup2(1, 1), 1))], + vec![Tup2(Tup2(1, 1), Tup2(Tup2(1, 1), 1))], + vec![Tup2(Tup2(1, 1), Tup2(Tup2(1, 1), 0))], + ] as [_; STEPS]) + .into_iter(); + + let mut right_data = ([ + vec![Tup2( + Tup2(1_usize, 1_usize), + Tup2(Tup2(2_usize, 2_usize), 1_i64), + )], + vec![], + vec![], + vec![], + vec![], + vec![], + ] as [_; STEPS]) + .into_iter(); + + let mut expected_outputs = ([ + indexed_zset! {Tup2 => Tup4: + Tup2(1, 1) => { Tup4(1, 1, 2, 2) => 1 }, // "Lifetime" Total: +1 + }, + indexed_zset! {Tup2 => Tup4: + Tup2(1, 1) => { Tup4(1, 1, 2, 2) => -3 }, // "Lifetime" Total: -2 + }, + indexed_zset! {Tup2 => Tup4: + Tup2(1, 1) => { Tup4(1, 1, 2, 2) => 1 }, // "Lifetime" Total: -1 + }, + indexed_zset! {Tup2 => Tup4: + Tup2(1, 1) => { Tup4(1, 1, 2, 2) => 1 }, // "Lifetime" Total: +0 + }, + indexed_zset! {Tup2 => Tup4: + Tup2(1, 1) => { Tup4(1, 1, 2, 2) => 1 }, // "Lifetime" Total: +1 + }, + indexed_zset! {Tup2 => Tup4: + // No output delta because the zweight is 0; "Lifetime" Total: +1 + }, + ] as [_; STEPS]) + .into_iter(); + + for i in (0..STEPS).confirm_each_auto().progress().with_bound() { + left_input.append(&mut left_data.next().unwrap()); + right_input.append(&mut right_data.next().unwrap()); + circuit.transaction()?; + let output = output.concat(); + print!("{}", output.iter().to_cli_table()); + assert_eq!(output.consolidate(), expected_outputs.next().unwrap()); + } + + Ok(()) +} + +#[test] +fn multiple_outputs() -> Result<(), anyhow::Error> { + const STEPS: usize = 2; + + let (mut circuit, ((left_input, right_input), (filter_output, join_output))) = + Runtime::init_circuit(worker_threads(), |root_circuit| { + let (left, left_input) = + root_circuit.add_input_indexed_zset::, Tup2>(); + + let (right, right_input) = + root_circuit.add_input_indexed_zset::, Tup2>(); + + let left_filtered = left.filter(|(k, v)| k.0 == 2); + + let joined = left_filtered.join_index(&right, |k, Tup2(l1, l2), Tup2(r1, r2)| { + // Merge left and right tuples. + Some((*k, Tup4(*l1, *l2, *r1, *r2))) + }); + + // We output both the intermediate filter result and the final join result. + Ok(( + (left_input, right_input), + ( + left_filtered.accumulate_output(), + joined.accumulate_output(), + ), + )) + })?; + + let mut left_data = ([ + vec![ + Tup2(Tup2(1_usize, 1_usize), Tup2(Tup2(1_usize, 1_usize), 1_i64)), + Tup2(Tup2(2, 1), Tup2(Tup2(1, 1), 1)), + Tup2(Tup2(2, 3), Tup2(Tup2(1, 1), 1)), + ], + vec![ + Tup2(Tup2(1, 2), Tup2(Tup2(1, 1), 1)), + Tup2(Tup2(2, 2), Tup2(Tup2(1, 1), 1)), + ], + ] as [_; STEPS]) + .into_iter(); + + let mut right_data = ([ + vec![ + Tup2(Tup2(2_usize, 1_usize), Tup2(Tup2(2_usize, 2_usize), 1_i64)), + Tup2(Tup2(2, 2), Tup2(Tup2(2, 2), 1)), + ], + vec![Tup2(Tup2(2, 3), Tup2(Tup2(2, 2), 1))], + ] as [_; STEPS]) + .into_iter(); + + let mut expected_filter_outputs = ([ + indexed_zset! {Tup2 => Tup2: + Tup2(2, 1) => { Tup2(1, 1) => 1 }, + Tup2(2, 3) => { Tup2(1, 1) => 1 }, + }, + indexed_zset! {Tup2 => Tup2: + Tup2(2, 2) => { Tup2(1, 1) => 1 }, + }, + ] as [_; STEPS]) + .into_iter(); + + let mut expected_join_outputs = ([ + indexed_zset! {Tup2 => Tup4: + Tup2(2, 1) => { Tup4(1, 1, 2, 2) => 1 }, + }, + indexed_zset! {Tup2 => Tup4: + Tup2(2, 2) => { Tup4(1, 1, 2, 2) => 1 }, + Tup2(2, 3) => { Tup4(1, 1, 2, 2) => 1 }, + }, + ] as [_; STEPS]) + .into_iter(); + + for i in (0..STEPS).confirm_each_auto().progress().with_bound() { + left_input.append(&mut left_data.next().unwrap()); + right_input.append(&mut right_data.next().unwrap()); + circuit.transaction()?; + let filter_output = filter_output.concat(); + print!("Filter\n{}", filter_output.iter().to_cli_table()); + assert_eq!( + filter_output.consolidate(), + expected_filter_outputs.next().unwrap() + ); + let join_output = join_output.concat(); + print!("Join\n{}", join_output.iter().to_cli_table()); + assert_eq!( + join_output.consolidate(), + expected_join_outputs.next().unwrap() + ); + } + + Ok(()) +} + +#[test] +fn rollback_test() -> Result<(), anyhow::Error> { + const STEPS: usize = 4; + + let (mut circuit, ((left_input, right_input), output)) = + Runtime::init_circuit(worker_threads(), |root_circuit| { + let (left, left_input) = + root_circuit.add_input_indexed_zset::, Tup2>(); + + let (right, right_input) = + root_circuit.add_input_indexed_zset::, Tup2>(); + + let antijoined = left.antijoin(&right); + + Ok(((left_input, right_input), antijoined.accumulate_output())) + })?; + + let mut left_data = ([ + // The initial transaction. + vec![Tup2( + Tup2(1_usize, 1_usize), + // Note: Only the key matters for determining equality. + Tup2(Tup2(1_usize, 1_usize), 1_i64), + )], + // A transaction which causes a constraint violation. + vec![Tup2(Tup2(1, 2), Tup2(Tup2(1, 2), 1))], + // We rollback/undo the previous transaction (by inverting the zweight). + vec![Tup2(Tup2(1, 2), Tup2(Tup2(1, 2), -1))], + // A new transaction which does not violate any constraint. + vec![Tup2(Tup2(1, 2), Tup2(Tup2(1, 2), 1))], + ] as [_; STEPS]) + .into_iter(); + + let mut right_data = ([ + // The initial transaction. + vec![Tup2( + Tup2(1_usize, 1_usize), + // Note: Only the key matters for determining equality. + Tup2(Tup2(2_usize, 2_usize), 1_i64), + )], + // A transaction which causes a constraint violation. + vec![], + // We rollback/undo the previous transaction (by inverting the zweight). + vec![], + // A new transaction which does not violate any constraint. + vec![ + Tup2(Tup2(1, 2), Tup2(Tup2(1, 2), 1)), + // For L ANTIJOIN R it is okay to have R carry "excess" tuples. + Tup2(Tup2(1, 3), Tup2(Tup2(1, 3), 1)), + ], + ] as [_; STEPS]) + .into_iter(); + + let mut expected_outputs = ([ + indexed_zset! {Tup2 => Tup2: + // Empty output, i.e., no constraint violated. + }, + indexed_zset! {Tup2 => Tup2: + // Violation due to non-empty result. + Tup2(1, 2) => { Tup2(1, 2) => 1 }, + }, + indexed_zset! {Tup2 => Tup2: + // Rollback cleans operator state, result does not matter. + Tup2(1, 2) => { Tup2(1, 2) => -1 }, + }, + indexed_zset! {Tup2 => Tup2: + // Empty output, i.e., no constraint violated. + }, + ] as [_; STEPS]) + .into_iter(); + + for i in (0..STEPS).confirm_each_auto().progress().with_bound() { + left_input.append(&mut left_data.next().unwrap()); + right_input.append(&mut right_data.next().unwrap()); + circuit.transaction()?; + let output = output.concat(); + print!("{}", output.iter().to_cli_table()); + assert_eq!(output.consolidate(), expected_outputs.next().unwrap()); + } + + Ok(()) +} + +// Computes the factorial of the first 10 numbers. This may be useful for custom +// termination criteria besides reaching a fixed point but we don't know yet. +#[test] +fn test_factorial_with_iterate() -> Result<(), anyhow::Error> { + let (mut circuit, output) = Runtime::init_circuit(worker_threads(), |circuit| { + // Generate sequence 0, 1, 2, ... + let mut n: usize = 0; + let source = circuit.add_source(Generator::new(move || { + let result = n; + n += 1; + result + })); + // Compute factorial of each number in the sequence. + let fact = circuit.iterate(|child_circuit| { + let counter = Rc::new(RefCell::new(1)); + let counter_clone = Rc::clone(&counter); + let countdown = source.delta0(child_circuit).apply(move |parent_val| { + let mut counter_borrow = counter_clone.borrow_mut(); + *counter_borrow += *parent_val; + let res = *counter_borrow; + *counter_borrow -= 1; + res + }); + let (z1_output, z1_feedback) = child_circuit.add_feedback_with_export(Z1::new(1)); + let multiplication = + countdown.apply2(&z1_output.local, |n1: &usize, n2: &usize| n1 * n2); + z1_feedback.connect(&multiplication); + // Stop iterating when the counter reaches 0. + Ok((async move || Ok(*counter.borrow() == 0), z1_output.export)) + })?; + Ok(fact.output()) + })?; + + let factorial = |n: usize| (1..=n).product::(); + const ITERATIONS: usize = 10; + for i in 0..ITERATIONS { + circuit.transaction()?; + let result = output.take_from_all(); + let result = result.first().unwrap(); + println!("Iteration {:3}: {:3}! = {}", i + 1, i, result); + assert_eq!(*result, factorial(i)); + } + + Ok(()) +} + +// Also notice the points_to_step_{1,2,3}.dl files for a Datalog implementation +// of this query which can be executed on Souffle. +#[test] +fn test_mutual_recursion() -> Result<(), anyhow::Error> { + type String2 = Tup2; + type String3 = Tup3; + + const STEPS: usize = 3; + + let ( + mut circuit, + ( + ( + alloc_input, + assign_input, + virtual_call_input, + heap_type_input, + dispatch_input, + actual_arg_input, + formal_param_input, + ), + (var_points_to_output, call_graph_output), + ), + ) = Runtime::init_circuit(worker_threads(), move |root_circuit| { + let (alloc, alloc_input) = root_circuit.add_input_zset::(); + let (assign, assign_input) = root_circuit.add_input_zset::(); + let (virtual_call, virtual_call_input) = root_circuit.add_input_zset::(); + let (heap_type, heap_type_input) = root_circuit.add_input_zset::(); + let (dispatch, dispatch_input) = root_circuit.add_input_zset::(); + let (actual_arg, actual_arg_input) = root_circuit.add_input_zset::(); + let (formal_param, formal_param_input) = root_circuit.add_input_zset::(); + + let (var_points_to, call_graph) = root_circuit.recursive( + |child_circuit, + (var_points_to, call_graph): ( + Stream<_, OrdZSet>, + Stream<_, OrdZSet>, + )| { + // Import streams from the parent circuit into the child circuit. + let alloc = alloc.delta0(child_circuit); + let assign = assign.delta0(child_circuit); + let virtual_call = virtual_call.delta0(child_circuit); + let heap_type = heap_type.delta0(child_circuit); + let dispatch = dispatch.delta0(child_circuit); + let actual_arg = actual_arg.delta0(child_circuit); + let formal_param = formal_param.delta0(child_circuit); + + let call_graph_next = virtual_call + .map_index(|Tup3(site, recv, sig)| { + (recv.clone(), (site.clone(), recv.clone(), sig.clone())) + }) + .join_index( + // 1. virtual_call JOIN var_points_to ON recv + &var_points_to.map_index(|Tup2(recv, obj)| { + (recv.clone(), (recv.clone(), obj.clone())) + }), + |_recv, (site, _, sig), (_, obj)| { + Some((obj.clone(), Tup3(site.clone(), sig.clone(), obj.clone()))) + }, + ) + .join_index( + // 2. ... JOIN heap_type ON obj + &heap_type + .map_index(|Tup2(obj, ty)| (obj.clone(), (obj.clone(), ty.clone()))), + |_obj, Tup3(site, sig, _), (_, ty)| { + Some(((ty.clone(), sig.clone()), (site.clone(), ty.clone()))) + }, + ) + .join_index( + // 3. ... JOIN dispatch ON ty and sig + &dispatch.map_index(|Tup3(ty, sig, meth)| { + ((ty.clone(), sig.clone()), meth.clone()) + }), + |_, (site, _), meth| { + Some(( + (site.clone(), meth.clone()), + Tup2(site.clone(), meth.clone()), + )) + }, + ); + + let var_points_to_next = var_points_to + .map_index(|Tup2(src, obj)| (src.clone(), (src.clone(), obj.clone()))) + .join_index( + &assign + .map_index(|Tup2(dst, src)| (src.clone(), (dst.clone(), src.clone()))), + |_src, (_, obj), (dst, _)| { + Some(((dst.clone(), obj.clone()), (dst.clone(), obj.clone()))) + }, + ) + .plus(&alloc.map_index(|Tup2(var, obj)| { + ((var.clone(), obj.clone()), (var.clone(), obj.clone())) + })) + .plus( + &call_graph + .map_index(|Tup2(site, meth)| { + (site.clone(), (site.clone(), meth.clone())) + }) + .join_index( + // 1. call_graph JOIN actual_arg ON site + &actual_arg.map_index(|Tup2(site, arg)| { + (site.clone(), (site.clone(), arg.clone())) + }), + |_site, (_, meth), (_, arg)| { + Some((meth.clone(), (meth.clone(), arg.clone()))) + }, + ) + .join_index( + // .2. ... JOIN formal_param ON meth + &formal_param.map_index(|Tup2(meth, param)| { + (meth.clone(), (meth.clone(), param.clone())) + }), + |_meth, (_, arg), (_, param)| { + Some(((arg.clone()), (arg.clone(), param.clone()))) + }, + ) + .join_index( + // 3. ... JOIN var_points_to ON arg + &var_points_to.map_index(|Tup2(arg, obj)| { + (arg.clone(), (arg.clone(), obj.clone())) + }), + |_arg, (_, param), (_, obj)| { + Some(( + (param.clone(), obj.clone()), + (param.clone(), obj.clone()), + )) + }, + ), + ); + + Ok(( + var_points_to_next.map(|(_, (param, obj))| Tup2(param.clone(), obj.clone())), + call_graph_next.map(|((site, meth), _)| Tup2(site.clone(), meth.clone())), + )) + }, + )?; + + Ok(( + ( + alloc_input, + assign_input, + virtual_call_input, + heap_type_input, + dispatch_input, + actual_arg_input, + formal_param_input, + ), + ( + var_points_to.accumulate_output(), + call_graph.accumulate_output(), + ), + )) + })?; + + fn owned_string2( + ((s1, s2), weight): ((&str, &str), ZWeight), + ) -> Tup2, ZWeight> { + Tup2(Tup2(s1.to_owned(), s2.to_owned()), weight) + } + fn owned_string3( + ((s1, s2, s3), weight): ((&str, &str, &str), ZWeight), + ) -> Tup2, ZWeight> { + Tup2(Tup3(s1.to_owned(), s2.to_owned(), s3.to_owned()), weight) + } + + let mut alloc_inputs = ([ + vec![(("g", "oG"), 1), (("d", "oDog"), 1), (("c", "oCat"), 1)] + .into_iter() + .map(owned_string2) + .collect(), + vec![(("m", "oMouse"), 1)] + .into_iter() + .map(owned_string2) + .collect(), + vec![], + ] as [Batch; STEPS]) + .into_iter(); + + let mut assign_inputs = ([ + vec![(("ac", "c"), 1)] + .into_iter() + .map(owned_string2) + .collect(), + vec![], + vec![(("ac", "c"), -1)] + .into_iter() + .map(owned_string2) + .collect(), + ] as [Batch; STEPS]) + .into_iter(); + + let mut virtual_call_inputs = ([ + vec![ + (("s1", "g", "greet"), 1), + (("s2", "g", "greet"), 1), + (("s3", "x", "speak"), 1), + ] + .into_iter() + .map(owned_string3) + .collect(), + vec![(("s4", "g", "greet"), 1)] + .into_iter() + .map(owned_string3) + .collect(), + vec![(("s2", "g", "greet"), -1)] + .into_iter() + .map(owned_string3) + .collect(), + ] as [Batch; STEPS]) + .into_iter(); + + let mut heap_type_inputs = ([ + vec![ + (("oG", "Greeter"), 1), + (("oDog", "Dog"), 1), + (("oCat", "Cat"), 1), + ] + .into_iter() + .map(owned_string2) + .collect(), + vec![(("oMouse", "Mouse"), 1)] + .into_iter() + .map(owned_string2) + .collect(), + vec![], + ] as [Batch; STEPS]) + .into_iter(); + + let mut dispatch_inputs = ([ + vec![ + (("Greeter", "greet", "Greeter.greet"), 1), + (("Dog", "speak", "Dog.speak"), 1), + (("Cat", "speak", "Cat.speak"), 1), + ] + .into_iter() + .map(owned_string3) + .collect(), + vec![(("Mouse", "speak", "Mouse.speak"), 1)] + .into_iter() + .map(owned_string3) + .collect(), + vec![], + ] as [Batch; STEPS]) + .into_iter(); + + let mut actual_arg_inputs = ([ + vec![(("s1", "d"), 1), (("s2", "ac"), 1)] + .into_iter() + .map(owned_string2) + .collect(), + vec![(("s4", "m"), 1)] + .into_iter() + .map(owned_string2) + .collect(), + vec![(("s2", "ac"), 1)] + .into_iter() + .map(owned_string2) + .collect(), + ] as [Batch; STEPS]) + .into_iter(); + + let mut formal_param_inputs = ([ + vec![(("Greeter.greet", "x"), 1)] + .into_iter() + .map(owned_string2) + .collect(), + vec![], + vec![], + ] as [Batch; STEPS]) + .into_iter(); + + let mut var_points_to_expected_outputs = ([ + zset! { + Tup2("ac".to_string(), "oCat".to_string()) => 1, + Tup2("c".to_string(), "oCat".to_string()) => 1, + Tup2("d".to_string(), "oDog".to_string()) => 1, + Tup2("g".to_string(), "oG".to_string()) => 1, + Tup2("x".to_string(), "oDog".to_string()) => 1, + Tup2("x".to_string(), "oCat".to_string()) => 1, + }, + zset! { + Tup2("m".to_string(), "oMouse".to_string()) => 1, + Tup2("x".to_string(), "oMouse".to_string()) => 1, + }, + zset! { + Tup2("ac".to_string(), "oCat".to_string()) => -1, + Tup2("x".to_string(), "oCat".to_string()) => -1, + }, + ] as [OrdZSet; STEPS]) + .into_iter(); + + let mut call_graph_expected_outputs = ([ + zset! { + Tup2("s1".to_string(), "Greeter.greet".to_string()) => 1, + Tup2("s2".to_string(), "Greeter.greet".to_string()) => 1, + Tup2("s3".to_string(), "Dog.speak".to_string()) => 1, + Tup2("s3".to_string(), "Cat.speak".to_string()) => 1, + }, + zset! { + Tup2("s3".to_string(), "Mouse.speak".to_string()) => 1, + Tup2("s4".to_string(), "Greeter.greet".to_string()) => 1, + }, + zset! { + Tup2("s2".to_string(), "Greeter.greet".to_string()) => -1, + Tup2("s3".to_string(), "Cat.speak".to_string()) => -1, + }, + ] as [OrdZSet; STEPS]) + .into_iter(); + + for i in (0..STEPS).confirm_each_auto().progress().with_bound() { + println!("====== Inputs ======"); + + let mut input = alloc_inputs.next().unwrap(); + println!("Alloc\n{}", input.iter().to_cli_table()); + alloc_input.append(&mut input); + + let mut input = assign_inputs.next().unwrap(); + println!("Assign\n{}", input.iter().to_cli_table()); + assign_input.append(&mut input); + + let mut input = virtual_call_inputs.next().unwrap(); + println!("VirtualCall\n{}", input.iter().to_cli_table()); + virtual_call_input.append(&mut input); + + let mut input = heap_type_inputs.next().unwrap(); + println!("HeapType\n{}", input.iter().to_cli_table()); + heap_type_input.append(&mut input); + + let mut input = dispatch_inputs.next().unwrap(); + println!("Dispatch\n{}", input.iter().to_cli_table()); + dispatch_input.append(&mut input); + + let mut input = actual_arg_inputs.next().unwrap(); + println!("ActualArg\n{}", input.iter().to_cli_table()); + actual_arg_input.append(&mut input); + + let mut input = formal_param_inputs.next().unwrap(); + println!("FormalParam\n{}", input.iter().to_cli_table()); + formal_param_input.append(&mut input); + + circuit.transaction()?; + + println!("====== Outputs ======"); + let var_points_to_output = var_points_to_output.concat(); + println!( + "VarPointsTo\n{}", + var_points_to_output.iter().to_cli_table() + ); + assert_eq!( + var_points_to_output.consolidate(), + var_points_to_expected_outputs.next().unwrap(), + ); + let call_graph_output = call_graph_output.concat(); + println!("CallGraph\n{}", call_graph_output.iter().to_cli_table()); + assert_eq!( + call_graph_output.consolidate(), + call_graph_expected_outputs.next().unwrap(), + ); + } + + Ok(()) +} diff --git a/packages/coln-query/points_to_step_1.dl b/packages/coln-query/src/dbsp/points_to_step_1.dl similarity index 89% rename from packages/coln-query/points_to_step_1.dl rename to packages/coln-query/src/dbsp/points_to_step_1.dl index 7972d91b..e3e00d4b 100644 --- a/packages/coln-query/points_to_step_1.dl +++ b/packages/coln-query/src/dbsp/points_to_step_1.dl @@ -1,16 +1,14 @@ //============================================================================ -// points_to.dl -// // A miniature points-to + call-graph analysis for an OO language, written to -// show MUTUAL RECURSION in Datalog: +// show mutual recursion in Datalog: // // VarPointsTo depends on CallGraph (args flow into callee parameters) // CallGraph depends on VarPointsTo (virtual dispatch needs receiver type) // // Neither can be computed first; Souffle solves them together to a fixpoint. -// This is "on-the-fly call graph construction" (cf. the Doop framework). +// This is "on-the-fly call graph construction". // -// Run: souffle points_to_step_1.dl +// Run: souffle points_to_step_1.dl //============================================================================ //--------------------------- Input relations (EDB) -------------------------- @@ -110,9 +108,4 @@ FormalParam("Greeter.greet", "x"). // s2 Greeter.greet // s3 Dog.speak // s3 Cat.speak -// -// The payoff: s3 (x.speak()) resolves to BOTH Dog.speak and Cat.speak only -// because x received oDog and oCat through the call edges of s1/s2 -- which -// themselves required knowing what g points to. CallGraph and VarPointsTo -// feed each other until the fixpoint; staging them separately can't do this. //============================================================================ diff --git a/packages/coln-query/points_to_step_2.dl b/packages/coln-query/src/dbsp/points_to_step_2.dl similarity index 89% rename from packages/coln-query/points_to_step_2.dl rename to packages/coln-query/src/dbsp/points_to_step_2.dl index 3230f8ec..5864e6c1 100644 --- a/packages/coln-query/points_to_step_2.dl +++ b/packages/coln-query/src/dbsp/points_to_step_2.dl @@ -1,16 +1,14 @@ //============================================================================ -// points_to.dl -// // A miniature points-to + call-graph analysis for an OO language, written to -// show MUTUAL RECURSION in Datalog: +// show mutual recursion in Datalog: // // VarPointsTo depends on CallGraph (args flow into callee parameters) // CallGraph depends on VarPointsTo (virtual dispatch needs receiver type) // // Neither can be computed first; Souffle solves them together to a fixpoint. -// This is "on-the-fly call graph construction" (cf. the Doop framework). +// This is "on-the-fly call graph construction". // -// Run: souffle points_to_step_2.dl +// Run: souffle points_to_step_2.dl //============================================================================ //--------------------------- Input relations (EDB) -------------------------- @@ -122,9 +120,4 @@ FormalParam("Greeter.greet", "x"). // s3 Cat.speak // s3 Mouse.speak // s4 Greeter.greet -// -// The payoff: s3 (x.speak()) resolves to BOTH Dog.speak and Cat.speak only -// because x received oDog and oCat through the call edges of s1/s2 -- which -// themselves required knowing what g points to. CallGraph and VarPointsTo -// feed each other until the fixpoint; staging them separately can't do this. //============================================================================ diff --git a/packages/coln-query/points_to_step_3.dl b/packages/coln-query/src/dbsp/points_to_step_3.dl similarity index 89% rename from packages/coln-query/points_to_step_3.dl rename to packages/coln-query/src/dbsp/points_to_step_3.dl index ec0bcfbf..353bebce 100644 --- a/packages/coln-query/points_to_step_3.dl +++ b/packages/coln-query/src/dbsp/points_to_step_3.dl @@ -1,16 +1,14 @@ //============================================================================ -// points_to.dl -// // A miniature points-to + call-graph analysis for an OO language, written to -// show MUTUAL RECURSION in Datalog: +// show mutual recursion in Datalog: // // VarPointsTo depends on CallGraph (args flow into callee parameters) // CallGraph depends on VarPointsTo (virtual dispatch needs receiver type) // // Neither can be computed first; Souffle solves them together to a fixpoint. -// This is "on-the-fly call graph construction" (cf. the Doop framework). +// This is "on-the-fly call graph construction". // -// Run: souffle points_to_step_3.dl +// Run: souffle points_to_step_3.dl //============================================================================ //--------------------------- Input relations (EDB) -------------------------- @@ -112,9 +110,4 @@ FormalParam("Greeter.greet", "x"). // s3 Dog.speak // s3 Mouse.speak // s4 Greeter.greet -// -// The payoff: s3 (x.speak()) resolves to BOTH Dog.speak and Cat.speak only -// because x received oDog and oCat through the call edges of s1/s2 -- which -// themselves required knowing what g points to. CallGraph and VarPointsTo -// feed each other until the fixpoint; staging them separately can't do this. //============================================================================ diff --git a/packages/coln-query/src/dbsp/wrapper.rs b/packages/coln-query/src/dbsp/wrapper.rs new file mode 100644 index 00000000..2b213dee --- /dev/null +++ b/packages/coln-query/src/dbsp/wrapper.rs @@ -0,0 +1,360 @@ +use crate::{ + expr::{Literal, LiteralExpr}, + relation::{Relation, RelationSchema, SchemaTuple, TupleKey, TupleValue}, +}; +use cli_table::{Cell, Style, Table, format::Justify}; +pub use dbsp::{ + DBSPHandle as DbspHandle, Error as DbspError, NestedCircuit, RootCircuit, Runtime, ZWeight, +}; +use dbsp::{ + IndexedZSetHandle, IndexedZSetReader, OrdIndexedZSet, OrdZSet, OutputHandle, Stream, + typed_batch::SpineSnapshot, utils::Tup2, +}; +#[allow(unused_imports, reason = "For testing purposes")] +pub use dbsp::{indexed_zset, zset, zset_set}; +use std::{ + collections::HashMap, + fmt::{Debug, Display}, + iter, +}; + +type OrdStream = Stream>; + +pub fn new_ord_indexed_stream( + circuit: &mut RootCircuit, +) -> (OrdIndexedRootStream, OrdIndexedStreamInputHandle) { + circuit.add_input_indexed_zset::() +} + +pub type OrdIndexedStreamInputHandle = IndexedZSetHandle; + +pub type OrdIndexedStreamOutputHandle = + OutputHandle>>; + +pub type OrdIndexedStream = Stream>; + +pub type OrdIndexedRootStream = OrdIndexedStream; +pub type OrdIndexedNestedStream = OrdIndexedStream; + +/// A wrapper of DBSP's streams carrying [`dbsp::OrdIndexedZSet`] but +/// generic-free over the circuit type. This limits the nesting level to one +/// level but this does not matter for practical applications. +#[derive(Clone)] +pub enum StreamWrapper { + Root(OrdIndexedRootStream), + Nested(OrdIndexedNestedStream), +} + +impl StreamWrapper { + pub fn distinct(&self) -> StreamWrapper { + match self { + Self::Root(stream) => Self::Root(stream.distinct()), + Self::Nested(stream) => Self::Nested(stream.distinct()), + } + } + + pub fn sum<'a, I>(&'a self, streams: I) -> StreamWrapper + where + I: IntoIterator, + { + match self { + Self::Root(stream) => { + Self::Root(stream.sum(streams.into_iter().map(|s| s.expect_root()))) + } + Self::Nested(stream) => { + Self::Nested(stream.sum(streams.into_iter().map(|s| s.expect_nested()))) + } + } + } + + pub fn minus(&self, other: &Self) -> Self { + match self { + Self::Root(stream) => Self::Root(stream.minus(other.expect_root())), + Self::Nested(stream) => Self::Nested(stream.minus(other.expect_nested())), + } + } + + pub fn map_index(&self, map_func: F) -> StreamWrapper + where + F: Fn((&TupleKey, &TupleValue)) -> (TupleKey, TupleValue) + 'static, + { + match self { + Self::Root(stream) => Self::Root(stream.map_index(map_func)), + Self::Nested(stream) => Self::Nested(stream.map_index(map_func)), + } + } + + pub fn filter(&self, filter_func: F) -> Self + where + F: Fn((&TupleKey, &TupleValue)) -> bool + 'static, + { + match self { + Self::Root(stream) => Self::Root(stream.filter(filter_func)), + Self::Nested(stream) => Self::Nested(stream.filter(filter_func)), + } + } + + pub fn join_index(&self, other: &Self, join: F) -> Self + where + F: Fn(&TupleKey, &TupleValue, &TupleValue) -> It + Clone + 'static, + It: IntoIterator + 'static, + { + match self { + Self::Root(stream) => Self::Root(stream.join_index(other.expect_root(), join)), + Self::Nested(stream) => Self::Nested(stream.join_index(other.expect_nested(), join)), + } + } + + pub fn anti_join_index(&self, other: &Self) -> Self { + match self { + Self::Root(stream) => Self::Root(stream.antijoin(other.expect_root())), + Self::Nested(stream) => Self::Nested(stream.antijoin(other.expect_nested())), + } + } + + /// The delta0 operator imports a stream from the parent circuit into the + /// child circuit. + pub fn delta0(&self, child_circuit: &NestedCircuit) -> Self { + match self { + // Transitions from RootStream to NestedStream + Self::Root(stream) => Self::Nested(stream.delta0(child_circuit)), + Self::Nested(stream) => panic!("No further nesting for beyond NestedStreams"), + } + } + + pub fn output(&self) -> OrdIndexedStreamOutputHandle { + match self { + Self::Root(stream) => stream.accumulate_output(), + Self::Nested(stream) => panic!("Nested streams do not support output()"), + } + } + + pub fn expect_root(&self) -> &OrdIndexedRootStream { + if let Self::Root(stream) = self { + stream + } else { + panic!("Expected RootStream") + } + } + + pub fn expect_nested(&self) -> &OrdIndexedNestedStream { + if let Self::Nested(stream) = self { + stream + } else { + panic!("Expected NestedStream") + } + } +} + +impl From for StreamWrapper { + fn from(stream: OrdIndexedRootStream) -> Self { + Self::Root(stream) + } +} + +impl From for StreamWrapper { + fn from(stream: OrdIndexedNestedStream) -> Self { + Self::Nested(stream) + } +} + +impl IntoIterator for &'_ StreamWrapper { + type Item = Self; + type IntoIter = std::iter::Once; + + fn into_iter(self) -> Self::IntoIter { + std::iter::once(self) + } +} + +#[derive(Default, Debug, Clone)] +pub struct DbspInputs { + inputs: HashMap, +} + +impl DbspInputs { + fn insert(&mut self, name: String, input: DbspInput) { + self.inputs.insert(name, input); + } + pub fn get(&self, name: &str) -> Option<&DbspInput> { + self.inputs.get(name) + } + pub fn take(&mut self, name: &str) -> Option { + self.inputs.remove(name) + } + pub fn iter(&self) -> impl Iterator { + self.inputs.values() + } +} + +#[derive(Clone)] +pub struct DbspInput { + schema: RelationSchema, + handle: OrdIndexedStreamInputHandle, +} + +impl DbspInput { + pub fn add( + schema: RelationSchema, + circuit: &mut RootCircuit, + inputs: &mut DbspInputs, + ) -> LiteralExpr { + let (stream, handle) = new_ord_indexed_stream(circuit); + let input = Self { + schema: schema.clone(), + handle, + }; + inputs.insert(schema.name.clone(), input); + LiteralExpr { + value: Literal::Relation(Relation::new(schema, stream)), + } + } + pub fn handle(&self) -> &OrdIndexedStreamInputHandle { + &self.handle + } + pub fn insert<'a, T: Into + Into + Clone + 'a>( + &self, + tuples: impl IntoIterator, + ) { + tuples.into_iter().for_each(|(tuple, z_weight)| { + self.handle + .push(tuple.clone().into(), (tuple.clone().into(), z_weight)) + }) + } + pub fn insert_with_same_weight<'a, T: Into + Into + Clone + 'a>( + &self, + tuples: impl IntoIterator, + z_weight: ZWeight, + ) { + self.insert(tuples.into_iter().map(|tuple| (tuple, z_weight))); + } +} + +impl Debug for DbspInput { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("DbspInput") + .field("schema", &self.schema) + .finish() + } +} + +pub struct DbspOutput { + handle: OrdIndexedStreamOutputHandle, + schema: RelationSchema, +} + +impl DbspOutput { + pub fn new(schema: RelationSchema, handle: OrdIndexedStreamOutputHandle) -> Self { + Self { schema, handle } + } + pub fn to_batch(&self) -> DbspOutputBatch<'_> { + let inner = self.handle.concat().iter().collect::>(); + DbspOutputBatch { + schema: &self.schema, + inner, + } + } +} + +pub struct DbspOutputBatch<'a> { + schema: &'a RelationSchema, + inner: Vec<(TupleKey, TupleValue, ZWeight)>, +} + +impl DbspOutputBatch<'_> { + const JUSTIFICATION: Justify = Justify::Right; + + pub fn as_table(&self) -> impl Display { + self.inner + .iter() + .map(|(key, tuple, weight)| { + iter::once(weight.to_string().cell().justify(Self::JUSTIFICATION)).chain( + SchemaTuple::new(&self.schema.tuple, tuple) + .fields() + .map(|attribute| attribute.to_string().cell().justify(Self::JUSTIFICATION)) + .collect::>(), + ) + }) + .table() + .title( + iter::once("z-weight".cell()) + .chain(self.schema.tuple.field_names(&None).map(|name| name.cell())), + ) + .bold(true) + .display() + .expect("Table error") + } + pub fn as_debug_table(&self) -> impl Display { + self.inner + .iter() + .map(|(key, tuple, weight)| { + // We ensure that the key and tuple data lengths match the + // respective schema field lengths. + debug_assert!(key.data.len() == self.schema.key.full_len()); + debug_assert!(tuple.data.len() == self.schema.tuple.full_len()); + iter::once(weight.to_string().cell().justify(Self::JUSTIFICATION)) + .chain( + SchemaTuple::new(&self.schema.key, key) + .all_fields() + .map(|attribute| { + attribute.to_string().cell().justify(Self::JUSTIFICATION) + }) + .collect::>(), + ) + .chain( + SchemaTuple::new(&self.schema.tuple, tuple) + .all_fields() + .map(|attribute| { + attribute.to_string().cell().justify(Self::JUSTIFICATION) + }) + .collect::>(), + ) + }) + .table() + .title( + iter::once("z-weight".cell()) + .chain( + self.schema + .key + .all_field_names(&None) + .map(|name| format!("[key] {name}").cell()), + ) + .chain( + self.schema + .tuple + .all_field_names(&None) + .map(|name| format!("[value] {name}").cell()), + ), + ) + .bold(true) + .display() + .expect("Table error") + } + pub fn as_data(&self) -> impl Iterator { + self.inner + .iter() + .map(|(_key, tuple, weight)| (*weight, tuple)) + } + pub fn as_zset(&self) -> OrdZSet { + let keys = self + .inner + .iter() + .map(|(_key, tuple, weight)| { + let tuple: TupleValue = SchemaTuple::new(&self.schema.tuple, tuple) + .fields() + .cloned() + .collect(); + Tup2(tuple, *weight) + }) + .collect::>(); + OrdZSet::from_keys((), keys) + } + pub fn as_debug_zset(&self) -> OrdZSet { + let keys = self + .inner + .iter() + .map(|(_key, tuple, weight)| Tup2(tuple.clone(), *weight)) + .collect::>(); + OrdZSet::from_keys((), keys) + } +} diff --git a/packages/coln-query/src/lib.rs b/packages/coln-query/src/lib.rs index 84d702b7..162f4383 100644 --- a/packages/coln-query/src/lib.rs +++ b/packages/coln-query/src/lib.rs @@ -2,7 +2,7 @@ pub mod api; mod context; -pub mod dbsp; +pub(crate) mod dbsp; pub mod error; pub mod expr; mod function; @@ -196,7 +196,7 @@ mod test { use super::*; use crate::{ dbsp::{DbspInput, zset}, - expr::{ + expr::{ AliasExpr, CartesianProductExpr, DifferenceExpr, DistinctExpr, EquiJoinExpr, FixedPointIterExpr, ProjectionExpr, SelectionExpr, UnionExpr, }, From 153bfc4d48922d3fb4565cdebfe4f16d74c0244c Mon Sep 17 00:00:00 2001 From: Leo Stewen Date: Tue, 23 Jun 2026 11:12:59 +0100 Subject: [PATCH 6/8] Update gitignore to not track macos' .DS_Store files --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index de9eed17..a7383542 100644 --- a/.gitignore +++ b/.gitignore @@ -350,3 +350,6 @@ manual/forester ## Rust /target/ + +## MacOS +.DS_Store From da8dadac2cc757ec7d64dc0e60ba78839134c0c3 Mon Sep 17 00:00:00 2001 From: Leo Stewen Date: Thu, 25 Jun 2026 15:31:07 +0100 Subject: [PATCH 7/8] Add toy example to compute the transitive closure with DBSP via its iterate() method instead of recursive --- packages/coln-query/src/dbsp/playground.rs | 143 +++++++++++++++++++++ 1 file changed, 143 insertions(+) diff --git a/packages/coln-query/src/dbsp/playground.rs b/packages/coln-query/src/dbsp/playground.rs index 6987e89c..8879cf48 100644 --- a/packages/coln-query/src/dbsp/playground.rs +++ b/packages/coln-query/src/dbsp/playground.rs @@ -850,6 +850,149 @@ fn test_factorial_with_iterate() -> Result<(), anyhow::Error> { Ok(()) } +/// This is a variant of [`test_transitive_closure`] using the iterate_with_condition() method. +#[test] +fn test_iterative_circuit() -> Result<(), anyhow::Error> { + const STEPS: usize = 2; + + let (mut circuit_handle, (edges_input, output_handle)) = + Runtime::init_circuit(1, move |root_circuit| { + let (edges, edges_input) = root_circuit.add_input_zset(); + + // Create a base relation with all paths of length 1. + let len_1 = edges.map(|Tup3(from, to, weight)| Tup4(*from, *to, *weight, 1)); + + // Safety measure to prevent infinite iterations. + const MAX_ITERATIONS: usize = 128; + let iteration_count = Rc::new(RefCell::new(0)); + + // TODO: + // - How does this look with mutual recursion, that is, mutliple conditions? + // - Why does this not work for more than one thread? + let closure = root_circuit.iterate_with_condition(|child_circuit| { + // Feedback carries only the frontier (the delta from the last step). + let (frontier, frontier_feedback) = child_circuit + .add_feedback(Z1::new( + OrdZSet::>::default(), + )); + + // delta0 fires only at inner step 0, injecting the base case exactly once. + let edges_inner = edges.delta0(child_circuit); + let len_1_inner = len_1.delta0(child_circuit); + + // Extend the frontier by one hop. + // At step 0: frontier={}, so the result is just len_1_inner, the base case. + // At step n with n > 0: len_1_inner={}, so it's purely frontier ⋈ edges. + let new_frontier = frontier + .map_index(move |Tup4(start, end, cum_weight, hopcnt)| { + (*end, Tup4(*start, *end, *cum_weight, *hopcnt)) + }) + .join( + &edges_inner + .map_index(|Tup3(from, to, weight)| (*from, Tup3(*from, *to, *weight))), + |_end_from, + Tup4(start, _end, cum_weight, hopcnt), + Tup3(_from, to, weight)| { + Tup4(*start, *to, cum_weight + weight, hopcnt + 1) + }, + ) + .plus(&len_1_inner); + + frontier_feedback.connect(&new_frontier); + + let condition = new_frontier.condition(move |z| { + let mut iteration_count = iteration_count.borrow_mut(); + *iteration_count += 1; + let hit_max_iterations = *iteration_count >= MAX_ITERATIONS; + if hit_max_iterations { + println!("MAX ITERATIONS HIT"); + } + let no_more_delta = z.is_empty(); + println!("ITERATION {}, IS EMPTY {}", iteration_count, z.is_empty()); + if no_more_delta { + *iteration_count = 0; + } + no_more_delta || hit_max_iterations + }); + + // Integrate across all inner iterations to collect every frontier. + // The frontier at convergence is empty — we need the union of all of them. + let all_paths = new_frontier.integrate(); + + Ok((condition, all_paths.export())) + })?; + + Ok((edges_input, closure.accumulate_output())) + })?; + + let mut edges_data = ([ + // The first clock cycle adds a graph of four nodes: + // |0| -1-> |1| -1-> |2| -2-> |3| -2-> |4| + vec![ + Tup2(Tup3(0_usize, 1_usize, 1_usize), 1_i64), + Tup2(Tup3(1, 2, 1), 1), + Tup2(Tup3(2, 3, 2), 1), + Tup2(Tup3(3, 4, 2), 1), + ], + // The second clock cycle removes the edge |1| -1-> |2|. + vec![Tup2(Tup3(1, 2, 1), -1)], + // The third clock cycle would introduce a cycle but that would + // cause the fixed point computation to never terminate. + // In total, we have the following graph: + // |0| -1-> |1| -1-> |2| -2-> |3| -2-> |4| + // ^ | + // | | + // ------------------3------------------ + // vec![Tup2(Tup3(1, 2, 1), 1), Tup2(Tup3(4, 0, 3), 1)], + ] as [Vec, ZWeight>>; STEPS]) + .into_iter(); + + let mut expected_outputs = ([ + // We expect the full transitive closure in the first clock cycle. + zset! { + Tup4(0_usize, 1_usize, 1_usize, 1_usize) => 1, + Tup4(0, 2, 2, 2) => 1, + Tup4(0, 3, 4, 3) => 1, + Tup4(0, 4, 6, 4) => 1, + Tup4(1, 2, 1, 1) => 1, + Tup4(1, 3, 3, 2) => 1, + Tup4(1, 4, 5, 3) => 1, + Tup4(2, 3, 2, 1) => 1, + Tup4(2, 4, 4, 2) => 1, + Tup4(3, 4, 2, 1) => 1, + }, + // These paths are removed in the second clock cycle. + zset! { + Tup4(0, 2, 2, 2) => -1, + Tup4(0, 3, 4, 3) => -1, + Tup4(0, 4, 6, 4) => -1, + Tup4(1, 2, 1, 1) => -1, + Tup4(1, 3, 3, 2) => -1, + Tup4(1, 4, 5, 3) => -1, + }, + // This does not matter anymore, as the computation does not + // terminate anymore due to the cycle. + // zset! {}, + ] as [_; STEPS]) + .into_iter(); + + for i in (0..STEPS).confirm_each_auto().progress().with_bound() { + println!("====== Inputs ======"); + let mut input = edges_data.next().unwrap(); + println!("Edges\n{}", input.iter().to_cli_table()); + edges_input.append(&mut input); + + circuit_handle.transaction()?; + + println!("====== Outputs ======"); + let output = output_handle.concat(); + println!("Transitive Closure\n{}", output.iter().to_cli_table()); + assert_eq!(output.consolidate(), expected_outputs.next().unwrap(),); + } + + Ok(()) +} + // Also notice the points_to_step_{1,2,3}.dl files for a Datalog implementation // of this query which can be executed on Souffle. #[test] From 2829294778f4b0a45b993db8d144ad19445aec97 Mon Sep 17 00:00:00 2001 From: Leo Stewen Date: Thu, 25 Jun 2026 15:31:26 +0100 Subject: [PATCH 8/8] [WIP] Towards a Tx based API for the query engine --- packages/coln-query/src/api/deltas.rs | 25 +++++++----- packages/coln-query/src/api/mod.rs | 10 ++--- packages/coln-query/src/api/schema.rs | 1 + packages/coln-query/src/api/store.rs | 2 +- packages/coln-query/src/api/transaction.rs | 3 +- packages/coln-query/src/api/violations.rs | 4 +- packages/coln-query/src/dbsp/wrapper.rs | 11 +++++- packages/coln-query/src/engine.rs | 4 ++ packages/coln-query/src/error.rs | 21 ++++++----- packages/coln-query/src/interpreter.rs | 44 ++++++++++------------ packages/coln-query/src/lib.rs | 10 +++-- packages/coln-query/src/optimizer/mod.rs | 1 + packages/coln-query/src/runtime/mod.rs | 21 +++++++++++ 13 files changed, 97 insertions(+), 60 deletions(-) create mode 100644 packages/coln-query/src/engine.rs create mode 100644 packages/coln-query/src/runtime/mod.rs diff --git a/packages/coln-query/src/api/deltas.rs b/packages/coln-query/src/api/deltas.rs index 7ad07b60..579b83d7 100644 --- a/packages/coln-query/src/api/deltas.rs +++ b/packages/coln-query/src/api/deltas.rs @@ -1,18 +1,17 @@ //! An interface for passing deltas of row-oriented data. There is -//! [TableDelta], [StoreDelta], and [DerivedDataDelta]. +//! [RowDelta], [TableDelta], [StoreDelta], and [DerivedDataDelta]. use super::schema::TableRef; use crate::scalar::ScalarTypedValue; pub type ZWeight = i64; -/// An update to a base table (part of the EDB). +/// An update of a row of some base table. /// It either represents an insertion or a deletion of a row from a table, /// see [`z_weight`](`Self::z_weight`) documentation. -pub struct TableDelta { +pub struct RowDelta { /// A ZWeight value ... - /// - `== 0` is not allowed per convention but it does not do harm, it - /// behaves as if there was no insertion happening at all. + /// - `== 0` behaves as if there was no insertion happening at all. /// - `n if n > 0` represents an insertion. If `n > 1` it is a duplicated /// insertion, that is, the row is inserted n-times. /// - `n if n < 0` represents a deletion. If `n < 1` we remove the row @@ -22,14 +21,22 @@ pub struct TableDelta { row: Vec, } -/// An update of the EDBPs, that is, insertions or deletions of base facts. +/// An update to a base table (part of the EDB). +pub struct TableDelta { + /// A unique identifier to a table. + table: TableRef, + /// The row-oriented updates of the table. + delta: Vec, +} + +/// An update of the EDB, that is, insertions or deletions of base facts. pub struct StoreDelta { - pub inner: Vec<(TableRef, Vec)>, + pub inner: Vec, } -/// An update of the IDBPs, that is, insertions or deletions of derived facts. +/// An update of the IDB, that is, insertions or deletions of derived facts. pub struct DerivedDataDelta { /// Contains the delta in the IDB after applying a delta in the EDB (the /// latter is a [`StoreDelta`]). - inner: Vec<(TableRef, Vec)>, + inner: Vec, } diff --git a/packages/coln-query/src/api/mod.rs b/packages/coln-query/src/api/mod.rs index 9eeead68..5f5ab491 100644 --- a/packages/coln-query/src/api/mod.rs +++ b/packages/coln-query/src/api/mod.rs @@ -4,8 +4,8 @@ // Receiving `coln-compiler`'s IR is blocked by its stabiliztion and hence still // missing in here. -mod deltas; -mod schema; -mod store; -mod transaction; -mod violations; +pub mod deltas; +pub mod schema; +pub mod store; +pub mod transaction; +pub mod violations; diff --git a/packages/coln-query/src/api/schema.rs b/packages/coln-query/src/api/schema.rs index 1f936bd1..1e80c83d 100644 --- a/packages/coln-query/src/api/schema.rs +++ b/packages/coln-query/src/api/schema.rs @@ -4,6 +4,7 @@ use crate::scalar::ScalarType; /// An identifier that uniquely identifies a table (globally across the store). +#[derive(Eq, PartialEq, Hash, Debug, Clone)] pub struct TableRef { inner: String, } diff --git a/packages/coln-query/src/api/store.rs b/packages/coln-query/src/api/store.rs index d7db21b8..a1861dd2 100644 --- a/packages/coln-query/src/api/store.rs +++ b/packages/coln-query/src/api/store.rs @@ -4,7 +4,7 @@ use super::{deltas::StoreDelta, transaction::ApplicationOutcome}; use std::error::Error; /// A generic transactional engine/store. -pub(crate) trait TxStore { +pub trait TxStore { type ApplicationOk: Into; type ApplicationError: Error + Clone; type RollbackError: Error + Clone; diff --git a/packages/coln-query/src/api/transaction.rs b/packages/coln-query/src/api/transaction.rs index c9a9ccb3..cb155523 100644 --- a/packages/coln-query/src/api/transaction.rs +++ b/packages/coln-query/src/api/transaction.rs @@ -3,7 +3,6 @@ use super::{ deltas::{DerivedDataDelta, StoreDelta, TableDelta}, - schema::TableRef, store::TxStore, violations::Violations, }; @@ -86,7 +85,7 @@ impl Tx { } } /// Convenience method to add data beyond initialization. - pub fn insert)>>(&mut self, deltas: I) { + pub fn insert>(&mut self, deltas: I) { self.state.delta.inner.extend(deltas); } pub fn try_commit<'a, Store: TxStore>( diff --git a/packages/coln-query/src/api/violations.rs b/packages/coln-query/src/api/violations.rs index 663a1e46..227b30f9 100644 --- a/packages/coln-query/src/api/violations.rs +++ b/packages/coln-query/src/api/violations.rs @@ -1,7 +1,7 @@ //! This module is concerned with violations and how to report them. Many things //! are still TBD. -use super::{deltas::TableDelta, schema::TableRef}; +use super::deltas::TableDelta; /// For each query which is checking a constraint, this reports back identified /// counterexamples. @@ -9,7 +9,7 @@ pub struct Violations { /// Contains the counter examples for each unmet constraint. Note that /// [`TableRef`] refers to a derived view (defined through a query) rather /// than a physical base table here. - inner: Vec<(TableRef, Vec)>, + inner: Vec, } impl Violations { diff --git a/packages/coln-query/src/dbsp/wrapper.rs b/packages/coln-query/src/dbsp/wrapper.rs index 2b213dee..adc07f38 100644 --- a/packages/coln-query/src/dbsp/wrapper.rs +++ b/packages/coln-query/src/dbsp/wrapper.rs @@ -67,6 +67,13 @@ impl StreamWrapper { } } + pub fn plus(&self, other: &Self) -> Self { + match self { + Self::Root(stream) => Self::Root(stream.plus(other.expect_root())), + Self::Nested(stream) => Self::Nested(stream.plus(other.expect_nested())), + } + } + pub fn minus(&self, other: &Self) -> Self { match self { Self::Root(stream) => Self::Root(stream.minus(other.expect_root())), @@ -74,7 +81,7 @@ impl StreamWrapper { } } - pub fn map_index(&self, map_func: F) -> StreamWrapper + pub fn map_index(&self, map_func: F) -> Self where F: Fn((&TupleKey, &TupleValue)) -> (TupleKey, TupleValue) + 'static, { @@ -129,7 +136,7 @@ impl StreamWrapper { } } - pub fn expect_root(&self) -> &OrdIndexedRootStream { + fn expect_root(&self) -> &OrdIndexedRootStream { if let Self::Root(stream) = self { stream } else { diff --git a/packages/coln-query/src/engine.rs b/packages/coln-query/src/engine.rs new file mode 100644 index 00000000..94e2e4da --- /dev/null +++ b/packages/coln-query/src/engine.rs @@ -0,0 +1,4 @@ +struct Engine { + optimizer: Optimizer, + runtime: Runtime, +} diff --git a/packages/coln-query/src/error.rs b/packages/coln-query/src/error.rs index 8c7502d6..2ce44ec5 100644 --- a/packages/coln-query/src/error.rs +++ b/packages/coln-query/src/error.rs @@ -9,9 +9,9 @@ pub enum IncLogError { #[error(transparent)] Optimization(#[from] OptimizationError), #[error(transparent)] - Runtime(#[from] RuntimeError), - #[error(transparent)] Engine(#[from] EngineError), + #[error(transparent)] + Runtime(#[from] RuntimeError), } #[derive(Error, Debug, Clone, PartialEq, Eq)] @@ -32,7 +32,7 @@ impl SyntaxError { #[derive(Error, Debug, Clone, PartialEq, Eq)] #[error("{message}")] -/// An error that occurs during an optimization pass prior to run time. +/// An error that occurs during an optimization pass prior to runtime. pub struct OptimizationError { pub message: String, } @@ -47,15 +47,16 @@ impl OptimizationError { #[derive(Error, Debug, Clone, PartialEq, Eq)] #[error("{message}")] -/// An error that occurs during runtime of the tree-walk interpreter. +/// An error which occurs during runtime of the circuit constructing, +/// tree-walk interpreter. // TODO: Instead of being generic, we could introduce: // - a type error // - a reference error -pub struct RuntimeError { +pub struct EngineError { message: String, } -impl RuntimeError { +impl EngineError { pub fn new>(message: T) -> Self { Self { message: message.into(), @@ -65,15 +66,15 @@ impl RuntimeError { #[derive(Error, Debug, Clone, PartialEq, Eq)] #[error("{message}")] -/// An error that occurs during runtime of the underlying engine (currently -/// DBSP). -pub struct EngineError { +/// An error that occurs during runtime of the underlying (incremental) +/// query execution engine (currently only DBSP). +pub struct RuntimeError { message: String, } impl From for IncLogError { fn from(value: DbspError) -> Self { - IncLogError::Engine(EngineError { + IncLogError::Runtime(RuntimeError { message: value.to_string(), }) } diff --git a/packages/coln-query/src/interpreter.rs b/packages/coln-query/src/interpreter.rs index d87b696a..a30a4f5f 100644 --- a/packages/coln-query/src/interpreter.rs +++ b/packages/coln-query/src/interpreter.rs @@ -1,7 +1,7 @@ use crate::{ context::InterpreterContext, dbsp::{OrdIndexedNestedStream, RootCircuit}, - error::RuntimeError, + error::EngineError, expr::{ AliasExpr, AntiJoinExpr, AssignExpr, BinaryExpr, CallExpr, CartesianProductExpr, DifferenceExpr, DistinctExpr, EquiJoinExpr, Expr, ExprVisitor, FixedPointIterExpr, @@ -33,7 +33,7 @@ impl Interpreter { &mut self, stmts: impl IntoIterator, ctx: &mut InterpreterContext, - ) -> Result, RuntimeError> { + ) -> Result, EngineError> { // Ensure we have a global scope before interpreting. debug_assert!(ctx.environment.just_global()); // We do not call `visit_block` here because the root scope is created @@ -44,14 +44,14 @@ impl Interpreter { debug_assert!(ctx.environment.just_global()); ret } - pub fn evaluate(&mut self, expr: &Expr, ctx: VisitorCtx) -> Result { + pub fn evaluate(&mut self, expr: &Expr, ctx: VisitorCtx) -> Result { self.visit_expr(expr, ctx) } fn visit_stmts<'a>( &mut self, stmts: impl IntoIterator, ctx: VisitorCtx, - ) -> Result, RuntimeError> { + ) -> Result, EngineError> { // Functional programming can be so beautiful. Return the last value // if any. stmts @@ -63,7 +63,7 @@ impl Interpreter { stmts: impl IntoIterator, ctx: VisitorCtx, environment: F, - ) -> Result, RuntimeError> { + ) -> Result, EngineError> { ctx.environment.begin_scope(); environment(ctx.environment); let ret = self.visit_stmts(stmts, ctx); @@ -78,7 +78,7 @@ macro_rules! comparison_helper { $( ($variant(left), $variant(right)) => Ok(Value::Bool(left $op right)), )* - _ => Err(RuntimeError::new( + _ => Err(EngineError::new( format!("expected comparable type, got: {:?} and {:?}", $left, $right), )), } @@ -91,7 +91,7 @@ macro_rules! arithmetic_helper { $( ($variant(left), $variant(right)) => Ok($variant(left $op right)), )* - _ => Err(RuntimeError::new( + _ => Err(EngineError::new( format!("expected number type, got: {:?} and {:?}", $left, $right), )), } @@ -102,7 +102,7 @@ macro_rules! assert_type { ($value:expr, $variant:path) => { match $value { $variant(inner) => Ok(inner), - _ => Err(RuntimeError::new(format!( + _ => Err(EngineError::new(format!( "expected {} type, got: {:?}", stringify!($variant:path), $value ))), @@ -122,7 +122,7 @@ impl Interpreter { Ok(Value::Bool(left)) } } else { - Err(RuntimeError::new(format!( + Err(EngineError::new(format!( "unsupported (lazy) binary operator: {:?}", expr.operator ))) @@ -166,7 +166,7 @@ impl Interpreter { Operator::Division => { arithmetic_helper!(left, right, /, Value::Iint, Value::Uint) } - _ => Err(RuntimeError::new(format!( + _ => Err(EngineError::new(format!( "unsupported (eager) binary operator: {:?}", expr.operator ))), @@ -176,7 +176,7 @@ impl Interpreter { type VisitorCtx<'a, 'b> = &'a mut InterpreterContext<'b>; -type ExprVisitorResult = Result; +type ExprVisitorResult = Result; impl ExprVisitor> for Interpreter { fn visit_binary_expr(&mut self, expr: &BinaryExpr, ctx: VisitorCtx) -> ExprVisitorResult { @@ -193,12 +193,12 @@ impl ExprVisitor> for Interpreter { match expr.operator { Operator::Subtraction => match operand { Value::Iint(value) => Ok(Value::Iint(-value)), - _ => Err(RuntimeError::new(format!( + _ => Err(EngineError::new(format!( "expected signed int, got: {operand:?}", ))), }, Operator::Not => Ok(Value::Bool(!is_truthy(&operand))), - _ => Err(RuntimeError::new(format!( + _ => Err(EngineError::new(format!( "unsupported unary operator: {:?}", expr.operator ))), @@ -263,7 +263,7 @@ impl ExprVisitor> for Interpreter { // TODO: Optimize by checking arity in resolver just _once_ statically. if expr.arguments.len() != callee.arity() { - return Err(RuntimeError::new(format!( + return Err(EngineError::new(format!( "expected exactly {} arguments, but got {}", callee.arity(), expr.arguments.len() @@ -552,10 +552,7 @@ impl ExprVisitor> for Interpreter { let (accumulator_init, schema) = { let accumulator = accumulator.borrow(); - ( - accumulator.inner.expect_root().clone(), - accumulator.schema.clone(), - ) + (accumulator.inner.clone(), accumulator.schema.clone()) }; let root_circuit = self @@ -569,11 +566,8 @@ impl ExprVisitor> for Interpreter { let accumulator = accumulator.borrow(); // delta0 does not alter the schema. let schema = accumulator.schema.clone(); - let accumulator = accumulator - .inner - .delta0(nested_circuit) - .expect_nested() - .plus(&acc); + let accumulator = + accumulator.inner.delta0(nested_circuit).plus(&acc.into()); environment.define_var(new_relation(schema, accumulator)); for import in imports.iter() { @@ -595,13 +589,13 @@ impl ExprVisitor> for Interpreter { }) .expect("Recursive error"); - let fixed_point = accumulator_init.plus(&accumulated); + let fixed_point = accumulator_init.plus(&accumulated.into()); Ok(Value::Relation(new_relation(schema, fixed_point))) } } -type StmtVisitorResult = Result, RuntimeError>; +type StmtVisitorResult = Result, EngineError>; impl StmtVisitor> for Interpreter { fn visit_var_stmt(&mut self, stmt: &VarStmt, ctx: VisitorCtx) -> StmtVisitorResult { diff --git a/packages/coln-query/src/lib.rs b/packages/coln-query/src/lib.rs index 162f4383..e91e0ca1 100644 --- a/packages/coln-query/src/lib.rs +++ b/packages/coln-query/src/lib.rs @@ -3,6 +3,7 @@ pub mod api; mod context; pub(crate) mod dbsp; +mod engine; pub mod error; pub mod expr; mod function; @@ -12,6 +13,7 @@ mod operators; pub mod optimizer; pub mod relation; mod resolver; +mod runtime; pub mod scalar; pub mod stmt; pub mod test_helper; @@ -21,7 +23,7 @@ pub mod variable; use crate::{ dbsp::{DbspHandle, DbspInputs, DbspOutput, RootCircuit, Runtime}, - error::{IncLogError, RuntimeError, SyntaxError}, + error::{EngineError, IncLogError, SyntaxError}, optimizer::{NoOptimizer, Optimizer}, stmt::Code, }; @@ -170,7 +172,7 @@ impl IncDataLog { root_circuit: &mut RootCircuit, inputs: DbspInputs, program: Code, - ) -> Result<(DbspInputs, DbspOutput), RuntimeError> { + ) -> Result<(DbspInputs, DbspOutput), EngineError> { let output = IncLog::with_recursion(root_circuit.clone()).execute(program); let output = match output { @@ -181,7 +183,7 @@ impl IncDataLog { DbspOutput::new(output_schema, output_handle) } result => { - return Err(RuntimeError::new(format!( + return Err(EngineError::new(format!( "Expected a relation as program's output, got {result:?}", ))); } @@ -196,7 +198,7 @@ mod test { use super::*; use crate::{ dbsp::{DbspInput, zset}, - expr::{ + expr::{ AliasExpr, CartesianProductExpr, DifferenceExpr, DistinctExpr, EquiJoinExpr, FixedPointIterExpr, ProjectionExpr, SelectionExpr, UnionExpr, }, diff --git a/packages/coln-query/src/optimizer/mod.rs b/packages/coln-query/src/optimizer/mod.rs index 6054bd51..0b92ae42 100644 --- a/packages/coln-query/src/optimizer/mod.rs +++ b/packages/coln-query/src/optimizer/mod.rs @@ -12,6 +12,7 @@ pub trait Optimizer: Clone { } } +/// A stupid stub implementation which does not optimize anything. #[derive(Clone, Debug, Default)] pub struct NoOptimizer {} diff --git a/packages/coln-query/src/runtime/mod.rs b/packages/coln-query/src/runtime/mod.rs new file mode 100644 index 00000000..8d4ad568 --- /dev/null +++ b/packages/coln-query/src/runtime/mod.rs @@ -0,0 +1,21 @@ +use crate::{ + api::deltas::{DerivedDataDelta, RowDelta, StoreDelta}, + error::RuntimeError, +}; + +pub trait Runtime +where + I: InputHandle, + O: OutputHandle, +{ + fn apply(&mut self, delta: StoreDelta); + fn run(&mut self) -> Result; +} + +pub trait InputHandle { + fn feed>(&mut self, delta: I); +} + +pub trait OutputHandle { + fn fetch(&self) -> impl Iterator; +}