diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c2b95e65..0b0ab0c0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -30,6 +30,7 @@ jobs: steps: - uses: actions/checkout@v4 - run: python3 scripts/check-tree-size.py + - run: python3 scripts/check-release-version.py --shared-crates rust: runs-on: ubuntu-24.04 @@ -320,4 +321,4 @@ jobs: sed -i -E "/umf\.git/ s/(rev|tag) = \"[^\"]*\"/tag = \"$tag\"/" Cargo.toml crates/weft-core/Cargo.toml cargo update -p umf-core -p umf-codegen # Red here means upstream moved; it tells the maintainer to bump the tag. - - run: cargo test -p weft-core -p weft-postgresql + - run: cargo test -p weft-model -p weft-core -p weft-postgresql diff --git a/CHANGELOG.md b/CHANGELOG.md index 365de311..fdc7290f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,67 @@ Older releases: see [GitHub Releases](https://github.com/DocumentDrivenDX/weft/releases). +## Unreleased + +- New crate `weft-model` 0.1.0 (own semver; `crates/weft-model/README.md`): UMF + 0.10 document preparation, name and identity resolution, logical types, + descriptors, exact literals, exact instants and diagnostics, moved out of + `weft-core` so other compilers can depend on them. `weft-core` re-exports them + under their old module paths (`model`, `application_model`, `ir`, `error`, + `json`, `current_string_domain`, `syntax::Name`), so existing code compiles + unchanged. +- `Catalog` admission flags are an explicit `Admission` profile + (`Catalog::prepare_with`, `Catalog::admit`, `Catalog::admission`); prepared + documents are read through `Catalog::documents`. +- `LogicalType` is generic over its family set. weft's dialect profiles keep the + four-family `Family` and cannot represent anything wider, so weft-sql 0.1 + through 0.3 accept and refuse exactly what they did. The opt-in `ScalarFamily` + adds UMF `timestamp` (exact UTC instants) and `float` (finite binary64 tokens), + available through `Catalog::field_as`, `Catalog::entity_descriptor_as` and the + other `*_as` methods; `Admission::nullable_fields` types `absent-allowed` + Fields as nullable. +- CoreLiteral admission against UMF Field domains (`security_literals`, and the + field-domain normalizer from `security_ontology`) moves to + `weft_model::core_literal`; `weft_core::security_literals` re-exports it. + Diagnostic codes are unchanged. +- New crate `weft-expr` 0.1.0 (own semver; `crates/weft-expr/README.md`): typed + `Field`/`Value` operands, comparison operators and their typing rule, exact + arithmetic domains and expressions, and the `weft.update-selection/1` + predicate format of CONTRACT-009. `weft-core` re-exports the operand, + comparison and arithmetic types under their old paths. The mutation frontend + builds selections with it, and `inspect_selection` and the PostgreSQL mutation + lowering decode them with one shared walker instead of two hand-written ones. + Wire shapes and refusal messages are unchanged. +- New crate `weft-backend` 0.1.0 (own semver; `crates/weft-backend/README.md`): + the CONTRACT-002 backend boundary. Manifest types and validation (accepted + interface versions and language pairs come from the frontend), the `Frontend` + and `Backend` traits, a `TargetArtifact` emit output (`sql::Emission` for + SQL backends, `NoArtifact` for backends that emit nothing), the checked compile + pipeline and `Registry`. `weft_core::backend` becomes weft's frontend + (`WeftPlans`) and keeps its `Backend` trait, `Context`, `Registry` and type + paths, so backend implementations compile unchanged; check order, refusal + codes and the `Compilation` wire shape are unchanged. +- New crate `weft-postgresql-emit` 0.1.0 (own semver; + `crates/weft-postgresql-emit/README.md`): PostgreSQL identifier quoting, + parameter slots, string literals, typed casts with the `C` collation and exact + result decoders, moved out of `weft-postgresql` (which re-exports + `Identifier`, `qualified`, `Parameters` and `CatalogDomain`). Duplicated cast, + literal and decoder helpers in the Truss runtime, candidate and mutation + lowerings now call it; emitted SQL is unchanged. The Truss layout stays in + `weft-postgresql`. +- The SQL carrier has result decoders for the opt-in families: `utc-instant` + (`YYYY-MM-DDTHH:MM:SS.ffffffZ`) for `timestamp` and `ieee754-binary64-hex` (the + binary64 bit pattern) for `float`. `Emission`, `Column` and `Representation` + take a family-set parameter that defaults to `Family`, so weft's emissions + cannot carry these columns and weft's emission checks refuse the new decoders. + `weft_postgresql_emit::decodable` projects a PostgreSQL expression to its + decoder's text, and `decoder_for` now returns a decoder for every family. + Experimental: only unit tests exercise them. +- `bun scripts/module-boundaries-update.ts` regenerates the dependency and + public-declaration inventory in `docs/helix/02-design/module-boundaries.json`. + `scripts/check-release-version.py --shared-crates` (run in CI) checks that each + shared crate's README states its Cargo version. + ## 0.4.58 - 2026-11-11 - Generated read algebra v6 to v8 and ordinary pages (PR #59). diff --git a/Cargo.lock b/Cargo.lock index efa9ada0..5e997421 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2228,6 +2228,15 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "weft-backend" +version = "0.1.0" +dependencies = [ + "serde", + "serde_json", + "weft-model", +] + [[package]] name = "weft-backend-probe" version = "0.0.0" @@ -2251,6 +2260,9 @@ dependencies = [ "sqlparser", "umf-codegen", "umf-core", + "weft-backend", + "weft-expr", + "weft-model", ] [[package]] @@ -2263,6 +2275,15 @@ dependencies = [ "weft-core", ] +[[package]] +name = "weft-expr" +version = "0.1.0" +dependencies = [ + "serde", + "serde_json", + "weft-model", +] + [[package]] name = "weft-frontend-probe" version = "0.0.0" @@ -2271,6 +2292,18 @@ dependencies = [ "weft-core", ] +[[package]] +name = "weft-model" +version = "0.1.0" +dependencies = [ + "base64", + "jsonschema", + "num-bigint", + "serde", + "serde_json", + "sha2", +] + [[package]] name = "weft-postgresql" version = "0.1.0" @@ -2281,6 +2314,18 @@ dependencies = [ "serde_json", "sqlparser", "weft-core", + "weft-expr", + "weft-postgresql-emit", +] + +[[package]] +name = "weft-postgresql-emit" +version = "0.1.0" +dependencies = [ + "base64", + "serde_json", + "weft-backend", + "weft-model", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 4a7eecca..f234a271 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = ["crates/weft-databricks","crates/weft-postgresql","crates/weft-runtime","crates/weft-python","crates/weft-wasm","tests/register-backend/probe","tests/frontend/wasm-probe","crates/weft-core"] +members = ["crates/weft-databricks","crates/weft-postgresql","crates/weft-runtime","crates/weft-python","crates/weft-wasm","tests/register-backend/probe","tests/frontend/wasm-probe","crates/weft-core","crates/weft-model","crates/weft-expr","crates/weft-backend","crates/weft-postgresql-emit"] resolver = "2" [workspace.package] version = "0.1.0" diff --git a/crates/weft-backend/Cargo.toml b/crates/weft-backend/Cargo.toml new file mode 100644 index 00000000..599e4710 --- /dev/null +++ b/crates/weft-backend/Cargo.toml @@ -0,0 +1,14 @@ +[package] +license = "MIT OR Apache-2.0" +name = "weft-backend" +# Own semver, independent of the workspace and wheel versions. See README.md. +version = "0.1.0" +edition.workspace = true +publish.workspace = true +rust-version = "1.90" +description = "Backend manifests, the assess/lower/emit boundary, registry and SQL emission carrier shared by weft and other compilers" + +[dependencies] +weft-model = { path = "../weft-model" } +serde.workspace = true +serde_json.workspace = true diff --git a/crates/weft-backend/LICENSE-APACHE b/crates/weft-backend/LICENSE-APACHE new file mode 100644 index 00000000..0398f585 --- /dev/null +++ b/crates/weft-backend/LICENSE-APACHE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2026 DocumentDrivenDX contributors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/crates/weft-backend/LICENSE-MIT b/crates/weft-backend/LICENSE-MIT new file mode 100644 index 00000000..d95948b5 --- /dev/null +++ b/crates/weft-backend/LICENSE-MIT @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 DocumentDrivenDX contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/crates/weft-backend/LICENSE.md b/crates/weft-backend/LICENSE.md new file mode 100644 index 00000000..2381950f --- /dev/null +++ b/crates/weft-backend/LICENSE.md @@ -0,0 +1,239 @@ +# License + +First-party software and documentation in this repository are dual-licensed, at +your option, under either the MIT License or the Apache License, Version 2.0. + +SPDX-License-Identifier: MIT OR Apache-2.0 + +Third-party code, dependencies, standards, fixtures, datasets and other externally +sourced material retain their original licenses and notices. This grant does not +relicense that material. Frozen evidence and embedded dependency copies retain +their original bytes; accompanying notices govern their use. + +## MIT License + +MIT License + +Copyright (c) 2026 DocumentDrivenDX contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +## Apache License, Version 2.0 + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2026 DocumentDrivenDX contributors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/crates/weft-backend/README.md b/crates/weft-backend/README.md new file mode 100644 index 00000000..826fb49a --- /dev/null +++ b/crates/weft-backend/README.md @@ -0,0 +1,57 @@ +# weft-backend 0.1.0 + +The backend boundary of CONTRACT-002, shared by weft's compiler (`weft-core`) +and other compilers (Fireweed's `fireweed-compiler`): backend manifests and +their validation, the frontend and backend traits, the checked +validate-binding / assess / lower / emit pipeline, refusal diagnostics, the +registry, and the SQL emission carrier as one artifact type. + +It depends on `weft-model` (catalog, identities, logical types, diagnostics, +bounded JSON). It contains no dialect plan, no physical mapping and no target +SQL text. + +## Version + +Own semver, independent of the workspace and the `umf-weft` wheel; +`weft_backend::VERSION` reports it. While the major version is 0, a minor bump +may break the public surface below; a patch bump may not. Manifest and +compilation wire shapes, refusal codes and the order of pipeline checks are +part of the surface. + +## Public surface + +| Module | Items | Meaning | +| --- | --- | --- | +| `manifest` | `Manifest`, `LanguageProfile`, `TargetProfile`, `Capability`, `Status`, `Obligation`, `ObligationOwner`, `ManifestRules`, `validate_manifest`, `validate_manifest_json`, `OpaqueManifest`, `take_manifest_opaque`, `restore_manifest_opaque`, `failure`, `nonempty`, `unique_strings` | Manifest types and structural validation. Accepted `interfaceVersion`s and dialect/IR pairs come from the caller's `ManifestRules`. | +| `boundary` | `Frontend`, `Backend`, `TargetArtifact`, `NoArtifact`, `Context<'a, F>`, `Validated`, `Assessment`, `Selection`, `BindingInput`, `Target`, `Qualification`, `Compilation`, `Registry`, `merge_obligations`, `coverage_contains` | The checked pipeline. | +| `sql` | `Emission`, `ParameterSlot`, `ParameterType`, `ExactParameterKind`, `Column`, `Representation`, `ScalarCarrier`, `ScalarDecoder`, `OuterJoin`, `NativeMetadata`, `SqlChecks` | The SQL artifact: statement text, typed parameter slots, result columns with carriers and exact decoders. Target-neutral (PostgreSQL and Databricks both emit it). `Emission`: weft emits core-family columns only; `Emission` adds timestamp and float columns. `ScalarDecoder::exact` picks a family's decoder: `text`, `boolean`, `exact-integer`, `exact-decimal`, `utc-instant` (`YYYY-MM-DDTHH:MM:SS.ffffffZ`, timestamp) and `ieee754-binary64-hex` (16 hex digits, most significant byte first; float). The last two are experimental: only unit tests use them. | + +## The pipeline + +A `Frontend` names its typed program (`Program<'a>`) and supplies the language +pair, IR version, required operations, model pins and selected identities of a +program, plus three optional hooks: `read_binding`, `preflight` (refusals before +any backend callback) and `admit_catalog`. + +A `Backend` declares a manifest and implements `validate_binding`, `assess`, +`lower` and `emit`. `emit` returns the backend's `Artifact`, any type that +implements `TargetArtifact`: weft's backends return `sql::Emission`; a backend +that emits nothing (an in-memory reference interpreter) returns `NoArtifact`. + +`Registry::compile` checks, in this order: backend version, program IR and +operation identities, language pair, target profile, binding profile, model pins, +binding size and digest, binding JSON, the frontend's preflight; then calls +`validate_binding`, checks identity coverage and binding-derived capabilities, +calls `assess` and checks every required operation against its declaration, +status, candidate opt-in and evidence; then `lower`, `emit`, the artifact's own +`check`, and merges obligations. Backend panics become `WFT-BACKEND-FAILURE`. + +## What stays in weft-core + +`weft_core::backend` is weft's frontend (`WeftPlans`): the `Plan` enum over +weft-sql 0.1–0.3 plans, the `weft-backend/0.2.x` manifest rules, the Truss +runtime binding reader, capability preflights, and the emission checks in +`backend_emission`. Its `Backend` trait, `Context`, `Registry` and `Compilation` +keep their paths and shapes, so every backend implementation compiles unchanged. +The `backend03` interface (0.4 plans), `security_backend` and the mutation +`SelectionBackend` are separate weft boundaries and stay in weft-core. diff --git a/crates/weft-backend/src/boundary.rs b/crates/weft-backend/src/boundary.rs new file mode 100644 index 00000000..b50d8812 --- /dev/null +++ b/crates/weft-backend/src/boundary.rs @@ -0,0 +1,557 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +//! The checked backend pipeline: a frontend's typed program, a trusted backend +//! that validates its binding, assesses every required operation, lowers and +//! emits a target artifact, and the registry that runs them in a fixed order. +use crate::manifest::{ + failure, nonempty, unique_strings, validate_manifest, Capability, LanguageProfile, Manifest, ManifestRules, Obligation, + Status, TargetProfile, +}; +use serde::Serialize; +use serde_json::Value; +use std::{collections::BTreeSet, marker::PhantomData}; +use weft_model::{ + catalog::Catalog, + diagnostic::Result, + json::{checked_json, sha256}, + types::ModelPin, +}; + +/// A compiler frontend: its typed program and the rules the shared pipeline +/// applies to it. weft's dialect plans are one frontend; Fireweed's is another. +pub trait Frontend: ManifestRules + Send + Sync + 'static { + /// A borrowed, typed program (logical plan). + type Program<'a>: Copy; + /// Shorten a program borrow. Projections of `Program` are invariant in their + /// lifetime, so implementations write `program` here. + fn reborrow<'s, 'l: 's>(program: Self::Program<'l>) -> Self::Program<'s>; + /// The program's dialect/IR pair. + fn language(program: Self::Program<'_>) -> LanguageProfile; + /// The IR version the program itself carries; must equal `language().ir_version`. + fn ir_version(program: Self::Program<'_>) -> String; + /// Operation identities the program requires. + fn capabilities(program: Self::Program<'_>) -> Vec; + /// Model pins the program was resolved against. + fn pins(program: Self::Program<'_>) -> Vec; + /// Every model identity the program reads; a binding must cover all of them. + fn selection(program: Self::Program<'_>) -> Selection; + /// Parse binding JSON. Default: bounded, duplicate-refusing [`checked_json`]. + fn read_binding(_target: &Target, _ir_version: &str, json: &str) -> std::result::Result { + checked_json(json).map_err(|_| ()) + } + /// Refusals that must happen before any backend callback. Default: none. + fn preflight(_manifest: &Manifest, _program: Self::Program<'_>, _target: &Target, _language: &LanguageProfile) -> Result<()> { + Ok(()) + } + /// Refuse catalogs this pipeline must not compile. Default: none. + fn admit_catalog(_catalog: &Catalog) -> Result<()> { + Ok(()) + } +} +/// What a backend's `emit` returns. The pipeline merges its obligations and lets +/// it check itself against the program. +pub trait TargetArtifact: Send { + fn obligations_mut(&mut self) -> &mut Vec; + fn check(&self, _program: F::Program<'_>, _selection: &Selection, _assessments: &[Assessment]) -> Result<()> { + Ok(()) + } +} +/// The artifact of a backend that emits nothing (for example an in-memory +/// reference interpreter): only obligations. +#[derive(Debug, Clone, Default, PartialEq, Serialize)] +pub struct NoArtifact { + pub obligations: Vec, +} +impl TargetArtifact for NoArtifact { + fn obligations_mut(&mut self) -> &mut Vec { + &mut self.obligations + } +} + +/// Backend-owned mapping and target types remain statically checked inside this trait. +/// Only an explicitly registered host object implements it; manifests never load code. +pub trait Backend: Send + Sync + 'static { + type Mapping: Send + Sync; + type TargetPlan: Send + Sync; + type Artifact: TargetArtifact; + fn describe(&self) -> Result; + fn validate_binding(&self, context: &Context<'_, F>) -> Result>; + fn assess(&self, context: &Context<'_, F>, mapping: &Self::Mapping) -> Result>; + fn lower(&self, context: &Context<'_, F>, mapping: &Self::Mapping) -> Result; + fn emit(&self, context: &Context<'_, F>, plan: &Self::TargetPlan) -> Result; +} +#[derive(Debug, Clone)] +pub struct BindingInput { + pub profile: String, + pub json: String, + pub sha256: String, +} +#[derive(Debug, Clone)] +pub struct Target { + pub backend_id: String, + pub backend_version: String, + pub profile_id: String, + pub allow_candidate: bool, +} +/// Every selected identity must receive an explicit binding/codec assessment. +#[derive(Debug, Clone, Default, PartialEq)] +pub struct Selection { + pub records: Vec, + pub fields: Vec, + pub types: Vec, + pub relationships: Vec, +} +pub struct Context<'a, F: Frontend> { + pub catalog: &'a Catalog, + pub plan: F::Program<'a>, + pub target: &'a TargetProfile, + pub binding: &'a BindingInput, + pub binding_value: &'a Value, + pub selection: &'a Selection, +} +pub struct Validated { + pub mapping: T, + pub additional_capabilities: Vec, + pub coverage: Selection, + pub obligations: Vec, +} +#[derive(Debug, Clone, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct Assessment { + pub id: String, + pub status: Status, + pub evidence: Vec, + pub obligations: Vec, +} +#[derive(Debug, Clone, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct Qualification { + pub assessment: Assessment, + pub declaration: Capability, +} +#[derive(Debug, Clone, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct Compilation { + pub backend_id: String, + pub backend_version: String, + pub target_profile: TargetProfile, + pub binding_profile: String, + pub binding_sha256: String, + pub qualifications: Vec, + /// The backend's target artifact (wire name kept from the SQL-only boundary). + pub emission: A, +} +trait Registered: Send + Sync { + fn manifest(&self) -> &Manifest; + fn compile( + &self, + catalog: &Catalog, + plan: F::Program<'_>, + target: &Target, + binding: &BindingInput, + ) -> Result>; +} +struct Adapter { + backend: B, + manifest: Manifest, + frontend: PhantomData F>, +} +impl> Registered for Adapter { + fn manifest(&self) -> &Manifest { + &self.manifest + } + fn compile( + &self, + catalog: &Catalog, + plan: F::Program<'_>, + target: &Target, + binding: &BindingInput, + ) -> Result> { + if target.backend_version != self.manifest.backend_version { + return Err(failure( + "WFT-BACKEND-VERSION", + "Selected backend version is not registered", + )); + } + let language = F::language(plan); + let actual_ir = F::ir_version(plan); + if actual_ir != language.ir_version || !unique_strings(&F::capabilities(plan)) { + return Err(failure( + "WFT-BACKEND-VERSION", + "Typed plan version or operation identities are invalid", + )); + } + if !self.manifest.language_profiles.contains(&language) { + return Err(failure( + "WFT-BACKEND-VERSION", + "Registered backend does not accept the selected dialect/IR pair", + )); + } + let profile = self + .manifest + .target_profiles + .iter() + .find(|p| p.id == target.profile_id) + .ok_or_else(|| { + failure( + "WFT-BACKEND-VERSION", + "Selected target profile is not registered", + ) + })?; + if binding.profile != self.manifest.binding_profile { + return Err(failure( + "WFT-BINDING", + "Binding profile does not match the selected registered backend", + )); + } + if F::pins(plan) != catalog.pins() { + return Err(failure( + "WFT-PIN", + "Plan model pins do not match the supplied catalog", + )); + } + if binding.json.len() > 4 * 1024 * 1024 { + return Err(failure("WFT-LIMIT", "Binding exceeds four MiB")); + } + if sha256(binding.json.as_bytes()) != binding.sha256 { + return Err(failure("WFT-PIN", "Binding byte digest mismatch")); + } + let value = F::read_binding(target, &actual_ir, &binding.json).map_err(|_| { + failure( + "WFT-BINDING", + "Binding JSON is malformed or repeats members", + ) + })?; + if !value.is_object() { + return Err(failure("WFT-BINDING", "Binding root must be an object")); + } + let selection = F::selection(plan); + F::preflight(&self.manifest, plan, target, &language)?; + let context = Context { + catalog, + plan: F::reborrow(plan), + target: profile, + binding, + binding_value: &value, + selection: &selection, + }; + let validated = self.backend.validate_binding(&context)?; + if !coverage_contains(&validated.coverage, &selection) { + return Err(failure( + "WFT-BINDING", + "Backend mapping omits a selected record, field, type or relationship identity", + )); + } + if !unique_strings(&validated.additional_capabilities) + || validated.additional_capabilities.len() > 4096 + { + return Err(failure( + "WFT-CAPABILITY", + "Binding-derived capabilities must be distinct and bounded", + )); + } + let required: BTreeSet = F::capabilities(plan) + .into_iter() + .chain(validated.additional_capabilities.iter().cloned()) + .collect(); + let assessments = self.backend.assess(&context, &validated.mapping)?; + let mut qualifications = Vec::new(); + let mut seen = BTreeSet::new(); + let mut obligations = Vec::new(); + merge_obligations(&mut obligations, &validated.obligations)?; + for a in &assessments { + if !seen.insert(a.id.as_str()) || !required.contains(&a.id) { + return Err(failure( + "WFT-CAPABILITY", + "Assessment has duplicate or unrequested operations", + )); + } + let declaration = self + .manifest + .capabilities + .iter() + .find(|c| { + c.id == a.id + && c.target_profiles.contains(&target.profile_id) + && c.language_profiles.contains(&language) + }) + .ok_or_else(|| { + failure( + "WFT-CAPABILITY", + "Operation is not declared for the selected target/language profile", + ) + })?; + if declaration.status == Status::Unsupported || a.status == Status::Unsupported { + return Err(failure( + "WFT-CAPABILITY", + "Selected operation is unsupported", + )); + } + if declaration.status == Status::Candidate && a.status == Status::Supported { + return Err(failure( + "WFT-CAPABILITY", + "Candidate declaration cannot be upgraded by assessment", + )); + } + if a.status == Status::Candidate && !target.allow_candidate { + return Err(failure( + "WFT-CAPABILITY", + "Candidate operation requires explicit allowCandidate", + )); + } + if !unique_strings(&a.evidence) + || a.evidence.iter().any(|e| !declaration.evidence.contains(e)) + || (a.status == Status::Supported && a.evidence.is_empty()) + { + return Err(failure( + "WFT-CAPABILITY", + "Assessment has missing or undeclared qualification evidence", + )); + } + merge_obligations(&mut obligations, &declaration.obligations)?; + merge_obligations(&mut obligations, &a.obligations)?; + qualifications.push(Qualification { + assessment: a.clone(), + declaration: declaration.clone(), + }); + } + if seen.len() != required.len() { + return Err(failure( + "WFT-CAPABILITY", + "Assessment omits a required operation", + )); + } + let lowered = self.backend.lower(&context, &validated.mapping)?; + let mut emission = self.backend.emit(&context, &lowered)?; + emission.check(plan, &selection, &assessments)?; + let emitted_obligations = std::mem::take(emission.obligations_mut()); + merge_obligations(emission.obligations_mut(), &emitted_obligations)?; + merge_obligations(emission.obligations_mut(), &obligations)?; + Ok(Compilation { + backend_id: self.manifest.backend_id.clone(), + backend_version: self.manifest.backend_version.clone(), + target_profile: profile.clone(), + binding_profile: binding.profile.clone(), + binding_sha256: binding.sha256.clone(), + qualifications, + emission, + }) + } +} +/// Merge `from` into `into`: validate, de-duplicate by ID, refuse conflicting +/// duplicates, keep ID order. +pub fn merge_obligations(into: &mut Vec, from: &[Obligation]) -> Result<()> { + for o in from { + if !nonempty(&o.id) + || !o.parameters.is_object() + || !o.failure_code.starts_with("WFT-") + || o.failure_code.len() <= 4 + || !o.failure_code[4..] + .bytes() + .all(|b| b.is_ascii_uppercase() || b.is_ascii_digit() || b == b'-') + { + return Err(failure( + "WFT-OBLIGATION", + "Backend returned a malformed obligation", + )); + } + if let Some(existing) = into.iter().find(|e| e.id == o.id) { + if existing != o { + return Err(failure( + "WFT-OBLIGATION", + "Obligation ID has conflicting requirements", + )); + } + } else { + into.push(o.clone()); + } + } + into.sort_by(|a, b| a.id.cmp(&b.id)); + Ok(()) +} +/// Whether `coverage` contains every identity in `selected`. +pub fn coverage_contains(coverage: &Selection, selected: &Selection) -> bool { + selected + .records + .iter() + .all(|i| coverage.records.contains(i)) + && selected.fields.iter().all(|i| coverage.fields.contains(i)) + && selected.types.iter().all(|i| coverage.types.contains(i)) + && selected + .relationships + .iter() + .all(|i| coverage.relationships.contains(i)) +} +/// Registered backends by ID, all emitting artifact type `A` for frontend `F`. +pub struct Registry { + backends: std::collections::BTreeMap>>, +} +impl Default for Registry { + fn default() -> Self { + Self { backends: Default::default() } + } +} +impl + 'static> Registry { + pub fn register>(&mut self, backend: B) -> Result<()> { + let manifest = + std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| backend.describe())) + .map_err(|_| { + failure( + "WFT-BACKEND-FAILURE", + "Registered backend panicked while describing itself", + ) + })??; + validate_manifest::(&manifest)?; + if self.backends.contains_key(&manifest.backend_id) { + return Err(failure( + "WFT-BACKEND-VERSION", + "Backend identity is already registered", + )); + } + self.backends.insert( + manifest.backend_id.clone(), + Box::new(Adapter { backend, manifest, frontend: PhantomData }), + ); + Ok(()) + } + pub fn manifest(&self, id: &str) -> Option<&Manifest> { + self.backends.get(id).map(|b| b.manifest()) + } + pub fn compile( + &self, + catalog: &Catalog, + plan: F::Program<'_>, + target: &Target, + binding: &BindingInput, + ) -> Result> { + F::admit_catalog(catalog)?; + let backend = self.backends.get(&target.backend_id).ok_or_else(|| { + failure( + "WFT-BACKEND-MISSING", + "Selected backend identity is not registered", + ) + })?; + std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + backend.compile(catalog, plan, target, binding) + })) + .map_err(|_| { + failure( + "WFT-BACKEND-FAILURE", + "Registered backend panicked during compilation", + ) + })? + } +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + use weft_model::{catalog::ModuleInput, types::Identity}; + + /// A toy frontend: a program is a list of required operations over one Record. + struct Toy; + #[derive(Clone, Copy)] + struct ToyProgram<'a> { + capabilities: &'a [String], + pins: &'a [ModelPin], + record: &'a Identity, + } + impl ManifestRules for Toy { + fn admits_interface(interface: &str) -> bool { + interface == "toy-backend/1" + } + fn admits_language(_: &str, p: &LanguageProfile) -> bool { + p.dialect_profile == "toy/1" && p.ir_version == "toy-ir/1" + } + } + impl Frontend for Toy { + type Program<'a> = ToyProgram<'a>; + fn reborrow<'s, 'l: 's>(program: ToyProgram<'l>) -> ToyProgram<'s> { + program + } + fn language(_: ToyProgram<'_>) -> LanguageProfile { + LanguageProfile { dialect_profile: "toy/1".into(), ir_version: "toy-ir/1".into() } + } + fn ir_version(_: ToyProgram<'_>) -> String { + "toy-ir/1".into() + } + fn capabilities(program: ToyProgram<'_>) -> Vec { + program.capabilities.to_vec() + } + fn pins(program: ToyProgram<'_>) -> Vec { + program.pins.to_vec() + } + fn selection(program: ToyProgram<'_>) -> Selection { + Selection { records: vec![program.record.clone()], ..Selection::default() } + } + } + /// An in-memory backend: emits nothing, carries one host obligation. + struct Memory { + cover: bool, + } + impl Backend for Memory { + type Mapping = (); + type TargetPlan = (); + type Artifact = NoArtifact; + fn describe(&self) -> Result { + Ok(serde_json::from_value(json!({"backendId":"toy.memory","backendVersion":"1","interfaceVersion":"toy-backend/1", + "languageProfiles":[{"dialectProfile":"toy/1","irVersion":"toy-ir/1"}],"bindingProfile":"toy.memory/1", + "targetProfiles":[{"id":"memory","engine":"memory","engineVersion":"1","sessionSettings":{},"storageLayoutRevision":"1","publicationRevision":"1"}], + "capabilities":[{"id":"scan","targetProfiles":["memory"],"languageProfiles":[{"dialectProfile":"toy/1","irVersion":"toy-ir/1"}], + "logicalDomain":{"rows":true},"resultDomain":{"rows":true},"constraints":[],"obligations":[],"status":"supported","evidence":["unit"]}], + "evidence":["unit"]})).unwrap()) + } + fn validate_binding(&self, context: &Context<'_, Toy>) -> Result> { + let coverage = if self.cover { context.selection.clone() } else { Selection::default() }; + Ok(Validated { mapping: (), additional_capabilities: vec![], coverage, obligations: vec![] }) + } + fn assess(&self, _: &Context<'_, Toy>, _: &()) -> Result> { + Ok(vec![Assessment { id: "scan".into(), status: Status::Supported, evidence: vec!["unit".into()], obligations: vec![] }]) + } + fn lower(&self, _: &Context<'_, Toy>, _: &()) -> Result<()> { + Ok(()) + } + fn emit(&self, _: &Context<'_, Toy>, _: &()) -> Result { + Ok(NoArtifact { obligations: vec![Obligation { id: "snapshot".into(), parameters: json!({}), owner: crate::manifest::ObligationOwner::Host, failure_code: "WFT-SNAPSHOT".into() }] }) + } + } + fn catalog() -> Catalog { + let text = include_str!("../../../tests/raw-postgresql/table-lifecycle-model.json").to_owned(); + let doc: Value = serde_json::from_str(&text).unwrap(); + let pin = ModelPin { document_id: doc["id"].as_str().unwrap().into(), revision: "1".into(), umf_version: "0.10.0".into(), sha256: sha256(text.as_bytes()) }; + Catalog::prepare(vec![ModuleInput { document_json: text, pin, selected_module_ids: vec!["demo".into()] }]).unwrap() + } + fn run(cover: bool, binding_json: &str) -> Result> { + let catalog = catalog(); + let mut registry: Registry = Registry::default(); + registry.register(Memory { cover }).unwrap(); + assert_eq!(registry.register(Memory { cover }).unwrap_err().code, "WFT-BACKEND-VERSION"); + let capabilities = vec!["scan".to_string()]; + let pins = catalog.pins(); + let record = Identity { document_id: pins[0].document_id.clone(), revision: "1".into(), module: "demo".into(), element: "Note".into() }; + let program = ToyProgram { capabilities: &capabilities, pins: &pins, record: &record }; + let target = Target { backend_id: "toy.memory".into(), backend_version: "1".into(), profile_id: "memory".into(), allow_candidate: false }; + let binding = BindingInput { profile: "toy.memory/1".into(), json: binding_json.into(), sha256: sha256(binding_json.as_bytes()) }; + registry.compile(&catalog, program, &target, &binding) + } + #[test] + fn a_frontend_and_an_artifactless_backend_run_the_checked_pipeline() { + let compiled = run(true, "{}").unwrap(); + assert_eq!(compiled.qualifications.len(), 1); + assert_eq!(compiled.emission.obligations[0].id, "snapshot"); + assert_eq!(serde_json::to_value(&compiled).unwrap()["emission"], json!({"obligations":[{"id":"snapshot","parameters":{},"owner":"host","failureCode":"WFT-SNAPSHOT"}]})); + } + #[test] + fn pipeline_refusals_come_from_the_shared_boundary() { + assert_eq!(run(false, "{}").unwrap_err().code, "WFT-BINDING"); + assert_eq!(run(true, "[]").unwrap_err().code, "WFT-BINDING"); + assert_eq!(run(true, r#"{"a":1,"a":2}"#).unwrap_err().code, "WFT-BINDING"); + } + #[test] + fn manifest_rules_belong_to_the_frontend() { + let mut manifest = Memory { cover: true }.describe().unwrap(); + crate::manifest::validate_manifest::(&manifest).unwrap(); + manifest.interface_version = "weft-backend/0.2.0".into(); + assert_eq!(crate::manifest::validate_manifest::(&manifest).unwrap_err().code, "WFT-BACKEND-VERSION"); + } +} diff --git a/crates/weft-backend/src/lib.rs b/crates/weft-backend/src/lib.rs new file mode 100644 index 00000000..6794a1d7 --- /dev/null +++ b/crates/weft-backend/src/lib.rs @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +//! The backend boundary shared by weft and other compilers (CONTRACT-002). +//! +//! - [`manifest`]: backend manifests, capabilities, obligations and their +//! validation. Which interface versions and language/IR pairs are valid is the +//! frontend's choice ([`boundary::Frontend`]). +//! - [`boundary`]: the `Frontend` and `Backend` traits, the binding context, +//! assessment, the checked compile pipeline and the `Registry`. A backend's +//! `emit` returns a target artifact of its own type. +//! - [`sql`]: one artifact type, the SQL emission carrier (statement text, +//! typed parameter slots, result columns and decoders). +//! +//! Manifests never load code; only an explicitly registered host object is a +//! backend. See `README.md` for the versioned surface. +pub mod boundary; +pub mod manifest; +pub mod sql; + +/// This crate's contract version (`README.md`). +pub const VERSION: &str = env!("CARGO_PKG_VERSION"); diff --git a/crates/weft-backend/src/manifest.rs b/crates/weft-backend/src/manifest.rs new file mode 100644 index 00000000..200b41dd --- /dev/null +++ b/crates/weft-backend/src/manifest.rs @@ -0,0 +1,244 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +//! Backend manifests (CONTRACT-002): identity, language and target profiles, +//! capabilities with evidence and obligations, and their structural validation. +use serde::{Deserialize, Serialize}; +use serde_json::Value; +use std::collections::BTreeSet; +use weft_model::{ + diagnostic::{Diagnostic, Result}, + json::checked_json, +}; +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", deny_unknown_fields)] +pub struct LanguageProfile { + pub dialect_profile: String, + pub ir_version: String, +} +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", deny_unknown_fields)] +pub struct TargetProfile { + pub id: String, + pub engine: String, + pub engine_version: String, + pub session_settings: Value, + pub storage_layout_revision: String, + pub publication_revision: String, +} +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum Status { + Supported, + Candidate, + Unsupported, +} +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum ObligationOwner { + Host, + Backend, +} +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", deny_unknown_fields)] +pub struct Obligation { + pub id: String, + pub parameters: Value, + pub owner: ObligationOwner, + pub failure_code: String, +} +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", deny_unknown_fields)] +pub struct Capability { + pub id: String, + pub target_profiles: Vec, + pub language_profiles: Vec, + pub logical_domain: Value, + pub result_domain: Value, + pub constraints: Vec, + pub obligations: Vec, + pub status: Status, + pub evidence: Vec, +} +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", deny_unknown_fields)] +pub struct Manifest { + pub backend_id: String, + pub backend_version: String, + pub interface_version: String, + pub language_profiles: Vec, + pub binding_profile: String, + pub target_profiles: Vec, + pub capabilities: Vec, + pub evidence: Vec, +} +/// A capability-phase refusal. +pub fn failure(code: &str, message: &str) -> Diagnostic { + Diagnostic::new(code, "capability", message) +} +/// Non-empty text without NUL. +pub fn nonempty(s: &str) -> bool { + !s.is_empty() && !s.contains('\0') +} +/// Distinct non-empty texts. +pub fn unique_strings(v: &[String]) -> bool { + let mut seen = BTreeSet::new(); + v.iter().all(|s| nonempty(s) && seen.insert(s)) +} +/// Which manifest interface versions and language/IR pairs a frontend accepts. +pub trait ManifestRules { + /// Whether `interface` (a manifest `interfaceVersion`) is accepted. + fn admits_interface(interface: &str) -> bool; + /// Whether `profile` is a valid language/IR pair under `interface`. + fn admits_language(interface: &str, profile: &LanguageProfile) -> bool; +} +fn valid_languages(profiles: &[LanguageProfile], interface: &str) -> bool { + let mut seen = BTreeSet::new(); + !profiles.is_empty() + && profiles.iter().all(|p| R::admits_language(interface, p)) + && profiles.iter().all(|p| seen.insert((&p.dialect_profile, &p.ir_version))) +} +/// Opaque declaration members, held aside while the typed manifest is read. +pub struct OpaqueManifest { settings:Vec, capabilities:Vec<(Value,Value,Vec)> } +fn take_opaque(value:&mut Value,key:&str)->Result{ + let member=value.as_object_mut().and_then(|o|o.get_mut(key)).ok_or_else(||failure("WFT-BACKEND-VERSION","Missing opaque declaration member"))?; + Ok(std::mem::replace(member,Value::Object(Default::default()))) +} +pub fn take_manifest_opaque(value:&mut Value)->Result{ + if !value.is_object(){return Err(failure("WFT-BACKEND-VERSION","Manifest must be an object"));} + let mut settings=Vec::new();let mut capabilities=Vec::new(); + for target in value["targetProfiles"].as_array_mut().ok_or_else(||failure("WFT-BACKEND-VERSION","Invalid targets"))? {settings.push(take_opaque(target,"sessionSettings")?);} + for capability in value["capabilities"].as_array_mut().ok_or_else(||failure("WFT-BACKEND-VERSION","Invalid capabilities"))? { + if !capability["status"].as_str().is_some_and(|s|matches!(s,"supported"|"candidate"|"unsupported")){return Err(failure("WFT-BACKEND-VERSION","Status must be a declared string token"));} + let logical=take_opaque(capability,"logicalDomain")?;let result=take_opaque(capability,"resultDomain")?;let mut parameters=Vec::new(); + for obligation in capability["obligations"].as_array_mut().ok_or_else(||failure("WFT-BACKEND-VERSION","Invalid obligations"))? { + if !obligation["owner"].as_str().is_some_and(|s|matches!(s,"host"|"backend")){return Err(failure("WFT-BACKEND-VERSION","Obligation owner must be a declared string token"));} + parameters.push(take_opaque(obligation,"parameters")?); + } + capabilities.push((logical,result,parameters)); + }Ok(OpaqueManifest{settings,capabilities}) +} +pub fn restore_manifest_opaque(targets:&mut [TargetProfile],capabilities:&mut [Capability],opaque:OpaqueManifest)->Result<()>{ + if targets.len()!=opaque.settings.len()||capabilities.len()!=opaque.capabilities.len(){return Err(failure("WFT-BACKEND-VERSION","Declaration correspondence changed"));} + for (target,setting) in targets.iter_mut().zip(opaque.settings){target.session_settings=setting;} + for (capability,(logical,result,parameters)) in capabilities.iter_mut().zip(opaque.capabilities){ + if capability.obligations.len()!=parameters.len(){return Err(failure("WFT-BACKEND-VERSION","Obligation correspondence changed"));} + capability.logical_domain=logical;capability.result_domain=result; + for (obligation,parameter) in capability.obligations.iter_mut().zip(parameters){obligation.parameters=parameter;} + }Ok(()) +} +/// Read and validate a manifest JSON text under frontend rules `R`. +pub fn validate_manifest_json(raw: &str) -> Result { + if raw.len() > 1024 * 1024 { + return Err(failure("WFT-LIMIT", "Backend manifest exceeds one MiB")); + } + let mut value = checked_json(raw).map_err(|_| { + failure( + "WFT-BACKEND-VERSION", + "Malformed or duplicate-key backend manifest JSON", + ) + })?; + let opaque=take_manifest_opaque(&mut value)?; + let mut manifest: Manifest = serde_json::from_value(value).map_err(|_| { + failure( + "WFT-BACKEND-VERSION", + "Backend manifest has unknown or malformed members", + ) + })?; + restore_manifest_opaque(&mut manifest.target_profiles,&mut manifest.capabilities,opaque)?; + validate_manifest::(&manifest)?; + Ok(manifest) +} +/// Validate a manifest's structure under frontend rules `R`. +pub fn validate_manifest(m: &Manifest) -> Result<()> { + if !R::admits_interface(&m.interface_version) || !valid_languages::(&m.language_profiles, &m.interface_version) { + return Err(failure( + "WFT-BACKEND-VERSION", + "Unsupported backend interface or language/IR pair", + )); + } + if ![&m.backend_id, &m.backend_version, &m.binding_profile] + .iter() + .all(|s| nonempty(s)) + || m.target_profiles.is_empty() + || m.target_profiles.len() > 256 + || m.capabilities.is_empty() + || m.capabilities.len() > 4096 + || !unique_strings(&m.evidence) + { + return Err(failure( + "WFT-BACKEND-VERSION", + "Backend manifest identity, collection bounds or evidence IDs are invalid", + )); + } + let mut targets = BTreeSet::new(); + for t in &m.target_profiles { + if ![ + &t.id, + &t.engine, + &t.engine_version, + &t.storage_layout_revision, + &t.publication_revision, + ] + .iter() + .all(|s| nonempty(s)) + || !t.session_settings.is_object() + || !targets.insert(t.id.as_str()) + { + return Err(failure( + "WFT-BACKEND-VERSION", + "Target profiles must be distinct, pinned and structurally explicit", + )); + } + } + let mut caps = BTreeSet::new(); + for c in &m.capabilities { + if !nonempty(&c.id) + || !caps.insert(c.id.as_str()) + || c.target_profiles.is_empty() + || !unique_strings(&c.target_profiles) + || c.target_profiles + .iter() + .any(|p| !targets.contains(p.as_str())) + || !valid_languages::(&c.language_profiles, &m.interface_version) + || c.language_profiles + .iter() + .any(|p| !m.language_profiles.contains(p)) + { + return Err(failure( + "WFT-BACKEND-VERSION", + "Capabilities must bind distinct IDs to declared target and language profiles", + )); + } + if !c.logical_domain.is_object() + || c.logical_domain.as_object().unwrap().is_empty() + || !c.result_domain.is_object() + || c.result_domain.as_object().unwrap().is_empty() + || !unique_strings(&c.constraints) + || !unique_strings(&c.evidence) + || c.evidence.iter().any(|e| !m.evidence.contains(e)) + || (c.status == Status::Supported && c.evidence.is_empty()) + { + return Err(failure( + "WFT-CAPABILITY", + "Capability domains, constraints and qualified evidence must be explicit", + )); + } + let mut obligations = BTreeSet::new(); + for o in &c.obligations { + if !nonempty(&o.id) + || !o.parameters.is_object() + || (!o.failure_code.starts_with("WFT-") || o.failure_code.len() <= 4) + || !o.failure_code[4..] + .bytes() + .all(|b| b.is_ascii_uppercase() || b.is_ascii_digit() || b == b'-') + || !obligations.insert(o.id.as_str()) + { + return Err(failure( + "WFT-OBLIGATION", + "Capability obligations are malformed or ambiguous", + )); + } + } + } + Ok(()) +} + diff --git a/crates/weft-backend/src/sql.rs b/crates/weft-backend/src/sql.rs new file mode 100644 index 00000000..29fa6c73 --- /dev/null +++ b/crates/weft-backend/src/sql.rs @@ -0,0 +1,218 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +//! The SQL emission carrier: one [`crate::boundary::TargetArtifact`]. Statement +//! text, typed parameter slots in position order, and result columns with their +//! carriers and exact decoders. Target-neutral: PostgreSQL and Databricks +//! backends both emit it. +use crate::{boundary::{Frontend, TargetArtifact}, manifest::{failure, Obligation}}; +use serde::{Deserialize, Serialize}; +use serde_json::Value; +use weft_model::{ + diagnostic::Result, + types::{Family, FamilySet, ScalarFamily}, +}; + +/// Explicit backend-owned facts, never synthetic authored UMF Field identities. +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[serde(rename_all = "camelCase")] +pub enum NativeMetadata { + Version, + CreatedBy, + CreatedAt, + UpdatedBy, + UpdatedAt, + OriginalAuthor, + OriginalAt, + LoadId, +} +#[derive(Debug, Clone, PartialEq, Serialize)] +#[serde(untagged)] +pub enum ParameterType { + Atomic(weft_model::types::LogicalType), + Exact { kind: ExactParameterKind, #[serde(rename="scalarType")] scalar_type:weft_model::descriptor::ExactScalarType }, +} +#[derive(Debug, Clone, PartialEq, Serialize)] +pub enum ExactParameterKind { #[serde(rename="exactScalar")] ExactScalar } +impl From for ParameterType { + fn from(value:weft_model::types::LogicalType)->Self { Self::Atomic(value) } +} +impl ParameterType { + pub fn atomic(&self)->Result<&weft_model::types::LogicalType> { + match self { Self::Atomic(value)=>Ok(value), _=>Err(failure("WFT-EMISSION","Atomic parameter required by this backend")) } + } +} +impl PartialEq for ParameterType { + fn eq(&self,other:&weft_model::types::LogicalType)->bool { matches!(self,Self::Atomic(value) if value==other) } +} +#[derive(Debug, Clone, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct ParameterSlot { + pub position: usize, + pub logical_type: ParameterType, + pub value: String, + pub origin: Value, +} +#[derive(Debug, Clone, Serialize)] +#[serde(rename_all = "kebab-case")] +pub enum ScalarCarrier { + Text, + Boolean, +} +/// How a host decodes one scalar result value from its carrier. Every decoder +/// is exact: it reproduces the value without rounding or session dependence. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] +#[serde(rename_all = "kebab-case")] +pub enum ScalarDecoder { + /// The text value as is. + Text, + /// A boolean. + Boolean, + /// A canonical decimal integer token. + ExactInteger, + /// A canonical decimal token. + ExactDecimal, + /// `utc-instant`: a UTC instant as text `YYYY-MM-DDTHH:MM:SS.ffffffZ`, a + /// four-digit year and exactly six fractional digits (microseconds). Hosts + /// refuse any other text. For the opt-in `timestamp` family. + UtcInstant, + /// `ieee754-binary64-hex`: the IEEE 754 binary64 bit pattern as 16 + /// lowercase hexadecimal digits, most significant byte first. Hosts refuse + /// other text and non-finite values (UMF floats are finite). For the opt-in + /// `float` family. + Ieee754Binary64Hex, +} +impl ScalarDecoder { + /// The exact decoder for a column of `family`, in any family set. + pub fn exact(family: &T) -> Self { + match family.widen() { + ScalarFamily::String => Self::Text, + ScalarFamily::Boolean => Self::Boolean, + ScalarFamily::Integer => Self::ExactInteger, + ScalarFamily::Decimal => Self::ExactDecimal, + ScalarFamily::Timestamp => Self::UtcInstant, + ScalarFamily::Float => Self::Ieee754Binary64Hex, + } + } +} +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct OuterJoin { + pub scan: String, + pub record: weft_model::types::Identity, +} +#[derive(Debug, Clone, Serialize)] +#[serde(tag = "kind", rename_all = "camelCase")] +pub enum Representation { + NativeMetadata { + metadata: NativeMetadata, + #[serde(rename = "logicalType")] + logical_type: weft_model::types::LogicalType, + carrier: ScalarCarrier, + decoder: ScalarDecoder, + }, + Scalar { + #[serde(rename = "logicalType")] + logical_type: weft_model::types::LogicalType, + carrier: ScalarCarrier, + decoder: ScalarDecoder, + }, + Value { + descriptor: weft_model::types::Identity, + #[serde(rename = "nativeNull")] + native_null: bool, + #[serde(rename = "outerJoin", skip_serializing_if = "Option::is_none")] + outer_join: Option, + }, + CompleteRelatedKeys { + relationship: weft_model::descriptor::RelationshipIdentity, + key: weft_model::descriptor::AuthoredKey, + bound: u16, + }, + RelatedKeys { + relationship: weft_model::descriptor::RelationshipIdentity, + key: weft_model::descriptor::AuthoredKey, + bound: u16, + }, +} +#[derive(Debug, Clone, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct Column { + pub position: usize, + pub output_name: String, + /// Physical result label; only explicitly admitted 0.3 positional outputs may carry it. + #[serde(skip_serializing_if = "Option::is_none")] + pub carrier_name: Option, + pub representation: Representation, + pub source_identities: Vec, + pub nullable: bool, +} +/// A SQL artifact. `T` is the family set of its scalar result columns: weft's +/// backends emit [`Family`] columns only; a compiler with timestamp and float +/// columns emits `Emission`. +#[derive(Debug, Clone, Serialize)] +pub struct Emission { + pub sql: String, + pub parameters: Vec, + pub columns: Vec>, + pub obligations: Vec, +} + +impl TargetArtifact for Emission +where + F: SqlChecks, + T: Send, +{ + fn obligations_mut(&mut self) -> &mut Vec { + &mut self.obligations + } + fn check(&self, program: F::Program<'_>, selection: &crate::boundary::Selection, assessments: &[crate::boundary::Assessment]) -> Result<()> { + F::check_emission(program, self, selection, assessments) + } +} +/// A frontend's checks on an emitted SQL artifact against its program +/// (output order, decoders, parameter domains). Default: none. +pub trait SqlChecks: Frontend { + fn check_emission(_program: Self::Program<'_>, _emission: &Emission, _selection: &crate::boundary::Selection, _assessments: &[crate::boundary::Assessment]) -> Result<()> { + Ok(()) + } +} +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + use weft_model::types::{Identity, LogicalType, ScalarType}; + #[test] + fn opt_in_columns_carry_timestamp_and_float_decoders() { + let column = |position: usize, family: ScalarFamily| Column { + position, + output_name: family.umf_name().into(), + carrier_name: None, + representation: Representation::Scalar { + logical_type: ScalarType::plain(family, false), + carrier: ScalarCarrier::Text, + decoder: ScalarDecoder::exact(&family), + }, + source_identities: Vec::::new(), + nullable: false, + }; + let emission = Emission:: { + sql: "SELECT".into(), + parameters: vec![], + columns: vec![column(1, ScalarFamily::Timestamp), column(2, ScalarFamily::Float)], + obligations: vec![], + }; + let value = serde_json::to_value(&emission).unwrap(); + assert_eq!(value["columns"][0]["representation"]["decoder"], json!("utc-instant")); + assert_eq!(value["columns"][0]["representation"]["logicalType"]["family"], json!("timestamp")); + assert_eq!(value["columns"][1]["representation"]["decoder"], json!("ieee754-binary64-hex")); + // weft's core families keep their decoders. + for (family, decoder) in [ + (Family::String, "text"), + (Family::Boolean, "boolean"), + (Family::Integer, "exact-integer"), + (Family::Decimal, "exact-decimal"), + ] { + assert_eq!(serde_json::to_value(ScalarDecoder::exact(&family)).unwrap(), json!(decoder)); + let _: LogicalType = LogicalType::plain(family, true); + } + } +} diff --git a/crates/weft-core/Cargo.toml b/crates/weft-core/Cargo.toml index f609a136..6e8cdc06 100644 --- a/crates/weft-core/Cargo.toml +++ b/crates/weft-core/Cargo.toml @@ -16,6 +16,9 @@ bounded-current-string-candidate=["current-query-refinement-candidate"] current-query-refinement-candidate=[] [dependencies] +weft-model = { path = "../weft-model" } +weft-expr = { path = "../weft-expr" } +weft-backend = { path = "../weft-backend" } umf-core = { git = "https://github.com/DocumentDrivenDX/umf.git", tag = "umf-v0.10.11", default-features = false, features = ["full-core"], optional = true } umf-codegen = { git = "https://github.com/DocumentDrivenDX/umf.git", tag = "umf-v0.10.11", optional = true } base64 = "=0.22.1" diff --git a/crates/weft-core/src/application_binary.rs b/crates/weft-core/src/application_binary.rs index d732a8ba..3adc60c0 100644 --- a/crates/weft-core/src/application_binary.rs +++ b/crates/weft-core/src/application_binary.rs @@ -20,7 +20,7 @@ impl Scope<'_> { let scalar_type=match &descriptor.shape { Shape::ExactScalar{scalar_type:ExactScalarType::Binary} if self.binary_equality=>ExactScalarType::Binary, Shape::ExactScalar{scalar_type:ExactScalarType::Timestamp} if self.timestamp_equality=>ExactScalarType::Timestamp, - Shape::ExactScalar{scalar_type:ExactScalarType::Date} if self.catalog.exact_dates=>ExactScalarType::Date, + Shape::ExactScalar{scalar_type:ExactScalarType::Date} if self.catalog.admission().exact_dates=>ExactScalarType::Date, _=>return Ok(None), }; match descriptor.availability.as_deref() { diff --git a/crates/weft-core/src/application_ir.rs b/crates/weft-core/src/application_ir.rs index 52e113f0..519b00cd 100644 --- a/crates/weft-core/src/application_ir.rs +++ b/crates/weft-core/src/application_ir.rs @@ -29,34 +29,8 @@ pub struct EntityProjection { pub facts: Vec, pub relationships: bool, } -#[derive(Debug, Clone, Serialize)] -pub struct Field { - pub scan: String, - pub identity: Identity, - #[serde(rename = "type")] - pub logical_type: LogicalType, - pub span: Span, -} -#[derive(Debug, Clone, Serialize)] -#[serde(tag = "kind", rename_all = "camelCase")] -pub enum Value { - Literal { - value: String, - #[serde(rename = "type")] - logical_type: LogicalType, - span: Span, - }, - Parameter { - name: String, - value: String, - #[serde(rename = "type")] - logical_type: LogicalType, - span: Span, - }, - Field { - field: Field, - }, -} +/// Typed operands live in weft-expr; their wire shapes are unchanged. +pub use weft_expr::operand::{Field, Value}; #[derive(Debug, Clone, Serialize)] pub struct ExactField { pub scan: String, @@ -137,19 +111,8 @@ pub enum Expression { bound: u16, }, } -/// Explicit backend-owned facts, never synthetic authored UMF Field identities. -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] -#[serde(rename_all = "camelCase")] -pub enum NativeMetadata { - Version, - CreatedBy, - CreatedAt, - UpdatedBy, - UpdatedAt, - OriginalAuthor, - OriginalAt, - LoadId, -} +/// Explicit backend-owned facts; the type lives with the SQL carrier in weft-backend. +pub use weft_backend::sql::NativeMetadata; #[derive(Debug, Clone, Serialize)] pub struct Output { pub name: String, diff --git a/crates/weft-core/src/application_model.rs b/crates/weft-core/src/application_model.rs index 02cb24ef..df7970e9 100644 --- a/crates/weft-core/src/application_model.rs +++ b/crates/weft-core/src/application_model.rs @@ -1,583 +1,3 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 -//! Identity-based application descriptors; native encodings remain backend obligations. -use crate::{ - error::{Diagnostic, Result}, - ir::{Identity, LogicalType, Span}, - model::{scalar_type, scalar_type_projected, Catalog, Record}, - syntax::Name, -}; -use serde::Serialize; -use serde_json::Value; -use std::collections::BTreeSet; -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct Member { - pub name: String, - pub identity: Identity, -} -#[derive(Debug, Clone, Serialize)] -#[serde(tag = "kind", rename_all = "camelCase")] -pub enum Shape { - /// Projection-only exact token. This is not a comparable LogicalType. - ExactScalar { - #[serde(rename = "scalarType")] - scalar_type: ExactScalarType, - }, - Scalar { - #[serde(rename = "type")] - logical_type: LogicalType, - }, - Sequence { - item: Identity, - }, - Map { - item: Identity, - }, - Structured { - record: Identity, - }, - Record { - members: Vec, - }, -} -#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize, Serialize)] -#[serde(rename_all = "camelCase")] -pub enum ExactScalarType { Timestamp, Binary, Date } -#[derive(Debug, Clone, Serialize)] -pub struct Descriptor { - pub identity: Identity, - pub availability: Option, - #[serde(flatten)] - pub shape: Shape, -} -#[derive(Debug, Clone, Serialize)] -pub struct EntityDescriptor { - pub members: Vec, - pub graph: Vec, -} -#[derive(Debug, Clone, Serialize)] -pub struct AuthoredKey { - pub id: String, - pub fields: Vec, - pub types: Vec, -} -fn fail(message: &str) -> Diagnostic { - Diagnostic::new("WFT-TYPE", "model", message) -} -impl Catalog { - pub(crate) fn local_element( - &self, - record: &Record, - reference: &Value, - ) -> Result<(Identity, Value)> { - let (identity, element) = self.local_element_metadata(record, reference)?; - self.validate_selected010(&record.pin, element)?; - Ok((identity, element.clone())) - } - fn local_element_metadata<'a>( - &'a self, - record: &Record, - reference: &Value, - ) -> Result<(Identity, &'a Value)> { - let mid = reference["module"] - .as_str() - .ok_or_else(|| fail("Missing local module reference"))?; - let eid = reference["element"] - .as_str() - .ok_or_else(|| fail("Missing local element reference"))?; - let element = self.local_element_by_ids(record.document, mid, eid) - .ok_or_else(|| fail("Unresolved local type dependency"))?; - let mut identity = record.identity.clone(); - identity.module = mid.into(); - identity.element = eid.into(); - Ok((identity, element)) - } - /// Count authored names before interpreting any matching Field's value meaning. - pub(crate) fn member_name_matches(&self, record: &Record, name: &Name) -> Result { - let refs = record.value["members"].as_array() - .ok_or_else(|| fail("Record needs explicit ordered members"))?; - let mut count = 0; - for reference in refs { - let (_, field) = self.local_element_metadata(record, reference)?; - if field["kind"] != "field" { return Err(fail("Record member must reference a Field")); } - let authored = self.query_name(&field) - .ok_or_else(|| fail("Selected Field needs an authored name"))?; - count += usize::from(name.matches(authored)); - } - Ok(count) - } - fn members(&self, record: &Record, value: &Value) -> Result> { - let refs = value["members"] - .as_array() - .ok_or_else(|| fail("Record needs explicit ordered members"))?; - let mut ids = BTreeSet::new(); - let mut members = Vec::new(); - for r in refs { - let (identity, field) = self.local_element(record, r)?; - if field["kind"] != "field" { - return Err(fail("Record member must reference a Field")); - } - if !ids.insert((identity.module.clone(), identity.element.clone())) { - return Err(fail("Repeated member identity")); - } - let name = self.query_name(&field) - .ok_or_else(|| fail("Selected Field needs an authored name"))? - .into(); - members.push(Member { name, identity }); - } - Ok(members) - } - /// Original ordered membership metadata, without interpreting unselected values. - pub fn record_members(&self, record: &Record) -> Result> { - self.members(record, &record.value) - } - pub fn entity_descriptor(&self, record: &Record) -> Result { - Ok(EntityDescriptor { - members: self.members(record, &record.value)?, - graph: self.type_graph(record, record.identity.clone(), record.value.clone())?, - }) - } - pub fn member_descriptor( - &self, - record: &Record, - name: &Name, - ) -> Result<(Member, Vec)> { - let matches: Vec<_> = self - .members(record, &record.value)? - .into_iter() - .filter(|m| name.matches(&m.name)) - .collect(); - if matches.len() != 1 { - return Err(Diagnostic::new( - if matches.is_empty() { - "WFT-NAME-MISSING" - } else { - "WFT-NAME-AMBIGUOUS" - }, - "resolve", - "Selected member name missing or ambiguous", - ).column(matches.len()>1) - .at(&name.span)); - } - let member = matches.into_iter().next().unwrap(); - let (_, v) = self.local_element( - record, - &serde_json::json!({"module":member.identity.module,"element":member.identity.element}), - )?; - let graph = self.type_graph(record, member.identity.clone(), v)?; - Ok((member, graph)) - } - /// Exact authored membership lookup for backend admission after resolution. - pub fn member_descriptor_by_identity( - &self, - record: &Record, - identity: &Identity, - ) -> Result<(Member, Vec)> { - let mut matches = self - .members(record, &record.value)? - .into_iter() - .filter(|m| &m.identity == identity); - let member = matches - .next() - .ok_or_else(|| fail("Original Record does not own selected member identity"))?; - if matches.next().is_some() { - return Err(fail("Original member identity is ambiguous")); - } - let (_, value) = self.local_element( - record, - &serde_json::json!({"module":identity.module,"element":identity.element}), - )?; - let graph = self.type_graph(record, identity.clone(), value)?; - Ok((member, graph)) - } - fn type_graph( - &self, - record: &Record, - identity: Identity, - value: Value, - ) -> Result> { - fn visit( - c: &Catalog, - r: &Record, - id: Identity, - v: Value, - seen: &mut BTreeSet<(String, String)>, - graph: &mut Vec, - depth: usize, - ) -> Result<()> { - if !seen.insert((id.module.clone(), id.element.clone())) { - return Ok(()); - } - if depth > 128 { - return Err(Diagnostic::new( - "WFT-LIMIT", - "model", - "Selected type graph exceeds depth 128", - )); - } - if seen.len() > 4096 { - return Err(Diagnostic::new( - "WFT-LIMIT", - "model", - "Selected type graph exceeds 4096 identities", - )); - } - let mut dependencies = Vec::new(); - let (availability, shape) = if v["kind"] == "record" { - let members = c.members(r, &v)?; - for m in &members { - dependencies.push(c.local_element(r,&serde_json::json!({"module":m.identity.module,"element":m.identity.element}))?); - } - (None, Shape::Record { members }) - } else if v["kind"] == "field" { - let availability = v["nullability"] - .as_str() - .filter(|s| matches!(*s, "required" | "absent-allowed")) - .ok_or_else(|| fail("Selected Field availability is not established"))? - .to_string(); - let references = v - .get("references") - .and_then(Value::as_array) - .cloned() - .unwrap_or_default(); - let shape = match v["cardinality"].as_str() { - Some("array" | "map") => { - if !references.is_empty() || v.get("scalarType").is_some() { - return Err(fail( - "Container cannot imply scalar or structured meaning", - )); - } - let dep = c.local_element(r, &v["itemType"])?; - if dep.1["kind"] != "field" { - return Err(fail("Container itemType must reference a Field")); - } - let item = dep.0.clone(); - dependencies.push(dep); - if v["cardinality"] == "array" { - Shape::Sequence { item } - } else { - Shape::Map { item } - } - } - Some("one") - if references.len() == 1 && references[0]["role"] == "record-type" => - { - let dep = c.local_element(r, &references[0])?; - if dep.1["kind"] != "record" || v.get("scalarType").is_some() { - return Err(fail( - "Structured Field needs a sole record-type reference", - )); - } - let record = dep.0.clone(); - dependencies.push(dep); - Shape::Structured { record } - } - Some("one") => { - let mut required = v.clone(); - required["nullability"] = Value::String("required".into()); - let name = Name { - value: v["name"].as_str().unwrap_or("").into(), - quoted: true, - span: Span { start: 0, end: 0 }, - }; - if c.exact_scalars && (matches!(v["scalarType"].as_str(), Some("timestamp" | "binary")) || c.exact_dates && r.pin.umf_version=="0.10.0" && v["scalarType"]=="date") { - if !references.is_empty() || v.get("itemType").is_some() || v.get("recordType").is_some() { - return Err(fail("Exact scalar cannot carry references or item meaning")); - } - Shape::ExactScalar { scalar_type: if v["scalarType"] == "timestamp" { - ExactScalarType::Timestamp - } else if v["scalarType"]=="date" { ExactScalarType::Date } else { ExactScalarType::Binary } } - } else { - Shape::Scalar { - logical_type: {let mut ty=scalar_type_projected(&required, &name, c.exact_numbers, c.exact_numbers, c.bounded_current_strings, c.field_projection_decimal38)?;if c.sdk_scalar_presence && availability=="absent-allowed" {ty.nullable=true;}ty}, - } - } - } - _ => return Err(fail("Selected cardinality is unsupported")), - }; - if !matches!(shape, Shape::Scalar { .. }) - && v.get("facets") - .and_then(Value::as_object) - .is_some_and(|f| !f.is_empty()) - { - return Err(fail( - "Selected container or structured facets are unsupported", - )); - } - (Some(availability), shape) - } else { - return Err(fail( - "Selected type graph contains an unsupported element role", - )); - }; - graph.push(Descriptor { - identity: id, - availability, - shape, - }); - for (id, v) in dependencies { - visit(c, r, id, v, seen, graph, depth + 1)?; - } - Ok(()) - } - let mut graph = Vec::new(); - visit( - self, - record, - identity, - value, - &mut BTreeSet::new(), - &mut graph, - 0, - )?; - Ok(graph) - } - pub fn authored_key(&self, record: &Record, key_id: &str) -> Result { - let keys = record.value["keys"] - .as_array() - .ok_or_else(|| fail("Record has no authored unique key"))?; - let matched: Vec<_> = keys.iter().filter(|k| k["id"] == key_id).collect(); - if matched.len() != 1 { - return Err(fail("Authored key identity is missing or ambiguous")); - } - let members = self.members(record, &record.value)?; - let mut fields = Vec::new(); - let mut types = Vec::new(); - let mut seen = BTreeSet::new(); - for reference in matched[0]["fields"].as_array().unwrap() { - let (id, field) = self.local_element(record, reference)?; - if !members.iter().any(|m| m.identity == id) - || !seen.insert((id.module.clone(), id.element.clone())) - { - return Err(fail("Key must contain distinct declared member Fields")); - } - let name = Name { - value: self.query_name(&field).unwrap_or("").into(), - quoted: true, - span: Span { start: 0, end: 0 }, - }; - types.push(scalar_type(&field, &name, self.exact_numbers, self.exact_numbers, self.bounded_current_strings)?); - fields.push(id); - } - Ok(AuthoredKey { - id: key_id.into(), - fields, - types, - }) - } -} -#[derive(Debug, Clone, PartialEq, Eq, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct RelationshipIdentity { - pub document_id: String, - pub revision: String, - pub module: String, - pub relationship: String, -} -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct RelationshipRead { - pub identity: RelationshipIdentity, - pub inverse: bool, - pub from: Identity, - pub to: Identity, - pub source_key: AuthoredKey, - pub target_key: AuthoredKey, - pub source_multiplicity: Value, - pub target_multiplicity: Value, - pub target_lifecycle: String, -} -impl Catalog { - pub(crate) fn entity_relationships(&self, source: &Record) -> Result> { - let mut names = Vec::new(); - for module in self.documents[source.document]["modules"].as_array().unwrap() { - if !self.inputs()[source.document].selected_module_ids.iter().any(|id| module["id"] == id.as_str()) { continue; } - let Some(relationships) = module["relationships"].as_array() else { continue; }; - for relationship in relationships { - for (side, label) in [("source", "name"), ("target", "inverse")] { - if let Some(name) = relationship[label].as_str() { - if relationship[side].as_array().unwrap().iter().any(|endpoint| - endpoint["module"] == source.identity.module && endpoint["element"] == source.identity.element) - { - names.push(name.to_owned()); - } - } - } - } - } - names.into_iter().map(|value| { - let name = Name { value: value.clone(), quoted: true, span: Span { start: 0, end: 0 } }; - self.relationship_read(source, &name).map(|read| (value, read)) - }).collect() - } - pub fn relationship_read(&self, source: &Record, name: &Name) -> Result { - let mut matches = Vec::new(); - for module in self.documents[source.document]["modules"] - .as_array() - .unwrap() - { - if !self.inputs()[source.document] - .selected_module_ids - .iter() - .any(|id| module["id"] == id.as_str()) - { - continue; - } - let Some(relationships) = module["relationships"].as_array() else { - continue; - }; - let mut ids = BTreeSet::new(); - for rel in relationships { - if !ids.insert(rel["id"].as_str().unwrap()) { - return Err(fail("Duplicate relationship identity")); - } - let endpoint_matches = |v: &Value| { - v["module"] == source.identity.module && v["element"] == source.identity.element - }; - let forward = name.matches(rel["name"].as_str().unwrap()) - && rel["source"] - .as_array() - .unwrap() - .iter() - .any(endpoint_matches); - let inverse = rel["inverse"].as_str().is_some_and(|n| name.matches(n)) - && rel["target"] - .as_array() - .unwrap() - .iter() - .any(endpoint_matches); - if forward { - matches.push((module, rel, false)); - } - if inverse { - matches.push((module, rel, true)); - } - } - } - if matches.len() != 1 { - return Err(Diagnostic::new( - if matches.is_empty() { - "WFT-NAME-MISSING" - } else { - "WFT-NAME-AMBIGUOUS" - }, - "resolve", - "Relationship name is missing or ambiguous", - ).column(matches.len()>1) - .at(&name.span)); - } - let (module, rel, inverse) = matches[0]; - self.relationship_read_definition(source,module,rel,inverse) - } - /// Resolve original relationship identity and direction without a name alias. - pub fn relationship_read_identity(&self,source:&Record,module_id:&str,id:&str,inverse:bool)->Result{ - let mut found=Vec::new(); - for module in self.documents[source.document]["modules"].as_array().unwrap(){ - if module["id"]!=module_id||!self.inputs()[source.document].selected_module_ids.iter().any(|m|m==module_id){continue} - if let Some(relationships)=module["relationships"].as_array(){for rel in relationships{if rel["id"]==id{found.push((module,rel));}}} - } - if found.len()!=1{return Err(fail("Original relationship identity is missing or ambiguous"))} - let(module,rel)=found[0];let side=if inverse{"target"}else{"source"}; - if !rel[side].as_array().is_some_and(|ends|ends.iter().any(|e|e["module"]==source.identity.module&&e["element"]==source.identity.element)){return Err(fail("Original relationship direction does not start at source Record"))} - self.relationship_read_definition(source,module,rel,inverse) - } - fn relationship_read_definition(&self,source:&Record,module:&Value,rel:&Value,inverse:bool)->Result{ - if rel["source"].as_array().unwrap().len() != 1 - || rel["target"].as_array().unwrap().len() != 1 - || rel["directed"] != true - || rel.get("associationRecord").is_some() - { - return Err(fail("Relationship requires a supported monomorphic directed traversal without an association Record")); - } - let lifecycle = rel["targetLifecycle"] - .as_str() - .filter(|v| matches!(*v, "owned" | "independent")) - .ok_or_else(|| fail("Relationship lifecycle meaning is unsupported"))?; - for side in ["sourceMultiplicity", "targetMultiplicity"] { - let m = &rel[side]; - let min = m["min"].as_u64().unwrap(); - if m["max"] != "*" && m["max"].as_u64().is_none_or(|max| max < min) { - return Err(fail("Relationship multiplicity is inconsistent")); - } - } - // Validate the authored target key even for inverse traversal; neither direction invents a key. - let (target_id, target_value) = self.local_element(source, &rel["target"][0])?; - if target_value["kind"] != "record" { - return Err(fail("Relationship target must be a Record")); - } - let target_record = Record { - identity: target_id.clone(), - pin: source.pin.clone(), - value: target_value, - document: source.document, - }; - let forward_key = - self.authored_key(&target_record, rel["target"][0]["key"].as_str().unwrap())?; - let (source_id, source_value) = self.local_element(source, &rel["source"][0])?; - if source_value["kind"] != "record" { - return Err(fail("Relationship source must be a Record")); - } - let from_record = Record { - identity: source_id.clone(), - pin: source.pin.clone(), - value: source_value, - document: source.document, - }; - let keys = from_record.value["keys"] - .as_array() - .ok_or_else(|| fail("Relationship source endpoint needs an authored key"))?; - let primary: Vec<_> = keys.iter().filter(|k| k["primary"] == true).collect(); - let key = if primary.len() == 1 { - primary[0] - } else if primary.is_empty() && keys.len() == 1 { - &keys[0] - } else { - return Err(fail("Relationship source endpoint key is ambiguous")); - }; - let source_key = self.authored_key(&from_record, key["id"].as_str().unwrap())?; - let (to, query_source_key, target_key) = if inverse { - (source_id.clone(), forward_key, source_key) - } else { - (target_id.clone(), source_key, forward_key) - }; - Ok(RelationshipRead { - identity: RelationshipIdentity { - document_id: source.identity.document_id.clone(), - revision: source.identity.revision.clone(), - module: module["id"].as_str().unwrap().into(), - relationship: rel["id"].as_str().unwrap().into(), - }, - inverse, - from: if inverse { target_id } else { source_id }, - to, - source_key: query_source_key, - target_key, - source_multiplicity: rel["sourceMultiplicity"].clone(), - target_multiplicity: rel["targetMultiplicity"].clone(), - target_lifecycle: lifecycle.into(), - }) - } -} - -impl Catalog { - /// Inspect a declared scalar domain, including a standalone read DTO. This - /// does not establish Record membership, a physical source or read permission. - pub fn scalar_descriptor_by_identity(&self, identity: &Identity) -> Result { - let mut found = Vec::new(); - for (index,input) in self.inputs().iter().enumerate() { - if input.pin.document_id!=identity.document_id || input.pin.revision!=identity.revision {continue} - for module in self.documents[index]["modules"].as_array().ok_or_else(||fail("Original modules required"))? { - if module["id"]!=identity.module || !input.selected_module_ids.contains(&identity.module) {continue} - for element in module["elements"].as_array().ok_or_else(||fail("Original elements required"))? { - if element["id"]!=identity.element {continue} - self.validate_selected010(&input.pin,element)?; - let name=Name{value:identity.element.clone(),quoted:true,span:crate::ir::Span{start:0,end:0}}; - let logical_type=scalar_type(element,&name,self.exact_numbers,self.exact_numbers, self.bounded_current_strings)?; - found.push(Descriptor{identity:identity.clone(),availability:Some("required".into()),shape:Shape::Scalar{logical_type}}); - } - } - } - if found.len()!=1 {return Err(fail("Original scalar domain is missing or ambiguous"))} - Ok(found.remove(0)) - } -} +//! Re-export of `weft_model::descriptor`. +pub use weft_model::descriptor::*; diff --git a/crates/weft-core/src/application_resolve.rs b/crates/weft-core/src/application_resolve.rs index f0b222f8..5ea0f170 100644 --- a/crates/weft-core/src/application_resolve.rs +++ b/crates/weft-core/src/application_resolve.rs @@ -61,7 +61,7 @@ fn resolve_inner( timestamp_equality:bool, bounded_current_strings:bool, ) -> Result { - if catalog.bounded_current_strings != bounded_current_strings { + if catalog.admission().bounded_current_strings != bounded_current_strings { return Err(fail("WFT-VERSION", "Catalog requires its explicitly versioned bounded-string resolver")); } let complete_entity = profile.as_ref().is_some_and(|p| matches!(p.subset,ir::Subset::CompleteEntity|ir::Subset::CompleteEntityPage)); diff --git a/crates/weft-core/src/application_temporal.rs b/crates/weft-core/src/application_temporal.rs index 0676d80d..d78b9610 100644 --- a/crates/weft-core/src/application_temporal.rs +++ b/crates/weft-core/src/application_temporal.rs @@ -455,10 +455,11 @@ pub fn prepare(value: &Value, page: Option<(&KeyRef, u16)>) -> Result<(Request, } p.word("from")?; let source = p.source()?; - let mut catalog = Catalog::prepare_truss_runtime(request.modules.clone(), true)?; - catalog.identity_name_fallback = true; - catalog.exact_scalars = true; - catalog.exact_dates = true; + let catalog = Catalog::prepare_truss_runtime(request.modules.clone(), true)?.admit(|admission| { + admission.identity_name_fallback = true; + admission.exact_scalars = true; + admission.exact_dates = true; + }); let record = catalog.record(source.namespace.as_ref(), &source.name)?; let members = catalog.record_members(&record)?; let inspected = inspection::inspect( diff --git a/crates/weft-core/src/arithmetic_plan.rs b/crates/weft-core/src/arithmetic_plan.rs index eff9acf0..74170350 100644 --- a/crates/weft-core/src/arithmetic_plan.rs +++ b/crates/weft-core/src/arithmetic_plan.rs @@ -7,13 +7,7 @@ use crate::{ }; use serde::Serialize; /// Additive 0.3 scalar comparison operators; old =/> variants retain their wire shape. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] -#[serde(rename_all = "camelCase")] -pub enum ComparisonOperator { Less, LessEqual, GreaterEqual, NotEqual } -impl ComparisonOperator { - pub fn sql(self) -> &'static str { match self { Self::Less => "<", Self::LessEqual => "<=", Self::GreaterEqual => ">=", Self::NotEqual => "<>" } } - pub fn capability(self) -> &'static str { match self { Self::Less => "compare.less", Self::LessEqual => "compare.lessEqual", Self::GreaterEqual => "compare.greaterEqual", Self::NotEqual => "compare.notEqual" } } -} +pub use weft_expr::compare::ComparisonOperator; #[derive(Debug, Clone, Serialize)] #[serde(tag = "op", rename_all = "camelCase")] pub enum Predicate { diff --git a/crates/weft-core/src/arithmetic_resolve.rs b/crates/weft-core/src/arithmetic_resolve.rs index a1d477dc..d3d148f2 100644 --- a/crates/weft-core/src/arithmetic_resolve.rs +++ b/crates/weft-core/src/arithmetic_resolve.rs @@ -4,116 +4,11 @@ use crate::{ application_ir::Field, application_resolve::Parameter, arithmetic_syntax::{Expression as Syntax, Kind as SyntaxKind}, - error::{Diagnostic, Result}, - ir::{Family, Span}, + error::Result, syntax::{Column, Name}, }; -use serde::Serialize; -#[derive(Debug, Clone, PartialEq, Eq, Serialize)] -#[serde(tag = "family", rename_all = "camelCase")] -pub enum Domain { - Integer, - Decimal { scale: u64 }, -} -impl Domain { - fn scale(&self) -> u64 { - match self { - Self::Integer => 0, - Self::Decimal { scale } => *scale, - } - } - fn combine(&self, right: &Self, operator: char, span: &Span) -> Result { - if matches!((self, right), (Self::Integer, Self::Integer)) { - return Ok(Self::Integer); - } - let scale = if operator == '*' { - self.scale().checked_add(right.scale()).ok_or_else(|| { - fail( - "WFT-LIMIT", - "Derived decimal scale exceeds representable metadata", - span, - ) - })? - } else { - self.scale().max(right.scale()) - }; - Ok(Self::Decimal { scale }) - } -} -#[derive(Debug, Clone, Serialize)] -pub struct Expression { - pub domain: Domain, - pub span: Span, - pub kind: Kind, -} -#[derive(Debug, Clone, Serialize)] -#[serde(tag = "op", rename_all = "camelCase")] -pub enum Kind { - Field { - field: Field, - }, - Literal { - value: String, - }, - Parameter { - name: String, - value: String, - }, - Negate { - operand: Box, - }, - Binary { - operator: char, - left: Box, - right: Box, - }, -} -fn fail(code: &str, message: &str, span: &Span) -> Diagnostic { - Diagnostic::new(code, "resolve", message).at(span) -} -pub(crate) fn token_domain(token: &str, family: Option<&Family>, span: &Span) -> Result { - if token.len() > crate::arithmetic_syntax::MAX_LITERAL_BYTES { - return Err(fail( - "WFT-LIMIT", - "Numeric token exceeds expression limit", - span, - )); - } - let unsigned = token.strip_prefix('-').unwrap_or(token); - let mut parts = unsigned.split('.'); - let whole = parts.next().unwrap_or(""); - let fraction = parts.next(); - if whole.is_empty() - || !whole.bytes().all(|b| b.is_ascii_digit()) - || parts.next().is_some() - || fraction.is_some_and(|f| f.is_empty() || !f.bytes().all(|b| b.is_ascii_digit())) - { - return Err(fail( - "WFT-NUMERIC-DOMAIN", - "Exact numeric token requires base-ten digits", - span, - )); - } - match family { - Some(Family::Integer) if fraction.is_none() => Ok(Domain::Integer), - Some(Family::Integer) => Err(fail( - "WFT-NUMERIC-DOMAIN", - "Integer parameter has a fractional token", - span, - )), - Some(Family::Decimal) => Ok(Domain::Decimal { - scale: fraction.map_or(0, |f| f.len() as u64), - }), - Some(_) => Err(fail( - "WFT-TYPE", - "Arithmetic parameter must be numeric", - span, - )), - None => Ok(fraction.map_or(Domain::Integer, |f| Domain::Decimal { - scale: f.len() as u64, - })), - } -} +pub use weft_expr::arithmetic::{Domain, Expression, Kind}; +pub(crate) use weft_expr::arithmetic::token_domain; pub(crate) fn resolve(syntax: &Syntax, field: &mut F, parameter: &mut P) -> Result where F: FnMut(&Column) -> Result, @@ -123,28 +18,7 @@ where let (domain, kind) = match &syntax.kind { SyntaxKind::Field(column) => { let value = field(column)?; - if value.logical_type.nullable { - return Err(fail( - "WFT-TYPE", - "Arithmetic requires an admitted required scalar", - &span, - )); - } - let domain = match value.logical_type.family { - Family::Integer => Domain::Integer, - Family::Decimal => Domain::Decimal { - scale: value.logical_type.facets["scale"].as_u64().ok_or_else(|| { - fail("WFT-TYPE", "Authored decimal scale is unavailable", &span) - })?, - }, - _ => { - return Err(fail( - "WFT-TYPE", - "Arithmetic requires numeric fields", - &span, - )) - } - }; + let domain = Domain::of_field(&value.logical_type, &span)?; (domain, Kind::Field { field: value }) } SyntaxKind::Literal(value) => ( @@ -156,7 +30,7 @@ where SyntaxKind::Parameter(name) => { let value = parameter(name)?; ( - token_domain(&value.value, Some(&value.family), &span)?, + token_domain(&value.value, Some(value.family.clone().into()), &span)?, Kind::Parameter { name: name.value.clone(), value: value.value, @@ -197,7 +71,7 @@ mod tests { use super::*; use crate::{ arithmetic_syntax::{self, Budget}, - ir::{Identity, LogicalType}, + ir::{Family, Identity, LogicalType}, syntax::Parser, }; use serde_json::json; diff --git a/crates/weft-core/src/arithmetic_syntax.rs b/crates/weft-core/src/arithmetic_syntax.rs index 039dfc60..811e1b93 100644 --- a/crates/weft-core/src/arithmetic_syntax.rs +++ b/crates/weft-core/src/arithmetic_syntax.rs @@ -8,7 +8,7 @@ use crate::{ }; pub(crate) const MAX_NODES: usize = 256; pub(crate) const MAX_DEPTH: usize = 32; -pub(crate) const MAX_LITERAL_BYTES: usize = 1024; +pub(crate) use weft_expr::arithmetic::MAX_LITERAL_BYTES; #[derive(Debug, Clone)] pub(crate) enum Kind { Field(Column), diff --git a/crates/weft-core/src/backend.rs b/crates/weft-core/src/backend.rs index dd4d7ccb..d53bdaa5 100644 --- a/crates/weft-core/src/backend.rs +++ b/crates/weft-core/src/backend.rs @@ -1,247 +1,23 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 //! Trusted, explicitly registered backend declarations. No content-driven loading. -use crate::{ - error::{Diagnostic, Result}, - json::checked_json, +use crate::{error::Result, json::checked_json}; +pub use weft_backend::manifest::{ + Capability, LanguageProfile, Manifest, Obligation, ObligationOwner, Status, TargetProfile, }; -use serde::{Deserialize, Serialize}; -use serde_json::Value; -use std::collections::BTreeSet; -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "camelCase", deny_unknown_fields)] -pub struct LanguageProfile { - pub dialect_profile: String, - pub ir_version: String, -} -#[derive(Debug, Clone, Serialize, Deserialize)] -#[serde(rename_all = "camelCase", deny_unknown_fields)] -pub struct TargetProfile { - pub id: String, - pub engine: String, - pub engine_version: String, - pub session_settings: Value, - pub storage_layout_revision: String, - pub publication_revision: String, -} -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "lowercase")] -pub enum Status { - Supported, - Candidate, - Unsupported, -} -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "lowercase")] -pub enum ObligationOwner { - Host, - Backend, -} -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] -#[serde(rename_all = "camelCase", deny_unknown_fields)] -pub struct Obligation { - pub id: String, - pub parameters: Value, - pub owner: ObligationOwner, - pub failure_code: String, -} -#[derive(Debug, Clone, Serialize, Deserialize)] -#[serde(rename_all = "camelCase", deny_unknown_fields)] -pub struct Capability { - pub id: String, - pub target_profiles: Vec, - pub language_profiles: Vec, - pub logical_domain: Value, - pub result_domain: Value, - pub constraints: Vec, - pub obligations: Vec, - pub status: Status, - pub evidence: Vec, -} -#[derive(Debug, Clone, Serialize, Deserialize)] -#[serde(rename_all = "camelCase", deny_unknown_fields)] -pub struct Manifest { - pub backend_id: String, - pub backend_version: String, - pub interface_version: String, - pub language_profiles: Vec, - pub binding_profile: String, - pub target_profiles: Vec, - pub capabilities: Vec, - pub evidence: Vec, -} -fn failure(code: &str, message: &str) -> Diagnostic { - Diagnostic::new(code, "capability", message) -} -fn nonempty(s: &str) -> bool { - !s.is_empty() && !s.contains('\0') -} -fn unique_strings(v: &[String]) -> bool { - let mut seen = BTreeSet::new(); - v.iter().all(|s| nonempty(s) && seen.insert(s)) -} -fn profile_key(p: &LanguageProfile) -> (&str, &str) { - (&p.dialect_profile, &p.ir_version) -} -fn valid_languages(profiles: &[LanguageProfile], interface: &str) -> bool { - let mut seen = BTreeSet::new(); - !profiles.is_empty() - && profiles.iter().all(|p| { - matches!( - profile_key(p), - ("weft-sql/0.1.0", "weft-ir/0.1.0") | ("weft-sql/0.2.0", "weft-ir/0.2.0") | ("weft-sql/0.3.0", "weft-ir/0.3.0") | ("weft-sql/0.2.0", "weft-ir/engine-navigation/0.1") - ) || (interface == "weft-backend/0.2.8" && matches!(profile_key(p),("weft-sql/0.2.0","weft-ir/engine-ordered-page/0.1")|("weft-sql/0.2.0","weft-ir/engine-navigation/0.2")|("weft-sql/0.2.0","weft-ir/engine-ordered-read/0.1"))) || (interface == "weft-backend/0.2.1" && profile_key(p) == ("weft-sql/0.2.0", "weft-ir/0.2.1")) - || (interface == "weft-backend/0.2.9" && profile_key(p) == ("weft-sql/0.2.0", "weft-ir/0.2.8")) - || (interface == "weft-backend/0.2.7" && profile_key(p) == ("weft-sql/0.2.0", "weft-ir/0.2.7")) - || (interface == "weft-backend/0.2.6" && profile_key(p) == ("weft-sql/0.2.0", "weft-ir/0.2.6")) - || (interface == "weft-backend/0.2.5" && profile_key(p) == ("weft-sql/0.2.0", "weft-ir/0.2.5")) - || (interface == "weft-backend/0.2.4" && profile_key(p) == ("weft-sql/0.2.0", "weft-ir/0.2.4")) - || (interface == "weft-backend/0.2.3" && profile_key(p) == ("weft-sql/0.2.0", "weft-ir/0.2.3")) - || (interface == "weft-backend/0.2.2" && profile_key(p) == ("weft-sql/0.2.0", "weft-ir/0.2.2")) - }) && profiles.iter().all(|p| seen.insert(profile_key(p))) -} -pub(crate) struct OpaqueManifest { settings:Vec, capabilities:Vec<(Value,Value,Vec)> } -fn take_opaque(value:&mut Value,key:&str)->Result{ - let member=value.as_object_mut().and_then(|o|o.get_mut(key)).ok_or_else(||failure("WFT-BACKEND-VERSION","Missing opaque declaration member"))?; - Ok(std::mem::replace(member,Value::Object(Default::default()))) -} -pub(crate) fn take_manifest_opaque(value:&mut Value)->Result{ - if !value.is_object(){return Err(failure("WFT-BACKEND-VERSION","Manifest must be an object"));} - let mut settings=Vec::new();let mut capabilities=Vec::new(); - for target in value["targetProfiles"].as_array_mut().ok_or_else(||failure("WFT-BACKEND-VERSION","Invalid targets"))? {settings.push(take_opaque(target,"sessionSettings")?);} - for capability in value["capabilities"].as_array_mut().ok_or_else(||failure("WFT-BACKEND-VERSION","Invalid capabilities"))? { - if !capability["status"].as_str().is_some_and(|s|matches!(s,"supported"|"candidate"|"unsupported")){return Err(failure("WFT-BACKEND-VERSION","Status must be a declared string token"));} - let logical=take_opaque(capability,"logicalDomain")?;let result=take_opaque(capability,"resultDomain")?;let mut parameters=Vec::new(); - for obligation in capability["obligations"].as_array_mut().ok_or_else(||failure("WFT-BACKEND-VERSION","Invalid obligations"))? { - if !obligation["owner"].as_str().is_some_and(|s|matches!(s,"host"|"backend")){return Err(failure("WFT-BACKEND-VERSION","Obligation owner must be a declared string token"));} - parameters.push(take_opaque(obligation,"parameters")?); - } - capabilities.push((logical,result,parameters)); - }Ok(OpaqueManifest{settings,capabilities}) -} -pub(crate) fn restore_manifest_opaque(targets:&mut [TargetProfile],capabilities:&mut [Capability],opaque:OpaqueManifest)->Result<()>{ - if targets.len()!=opaque.settings.len()||capabilities.len()!=opaque.capabilities.len(){return Err(failure("WFT-BACKEND-VERSION","Declaration correspondence changed"));} - for (target,setting) in targets.iter_mut().zip(opaque.settings){target.session_settings=setting;} - for (capability,(logical,result,parameters)) in capabilities.iter_mut().zip(opaque.capabilities){ - if capability.obligations.len()!=parameters.len(){return Err(failure("WFT-BACKEND-VERSION","Obligation correspondence changed"));} - capability.logical_domain=logical;capability.result_domain=result; - for (obligation,parameter) in capability.obligations.iter_mut().zip(parameters){obligation.parameters=parameter;} - }Ok(()) -} +pub(crate) use weft_backend::manifest::{restore_manifest_opaque, take_manifest_opaque}; +use weft_backend::{ + boundary::Frontend, + manifest::{failure, ManifestRules}, + sql::SqlChecks, +}; +#[cfg(test)] +use weft_backend::boundary::merge_obligations; pub fn validate_manifest_json(raw: &str) -> Result { - if raw.len() > 1024 * 1024 { - return Err(failure("WFT-LIMIT", "Backend manifest exceeds one MiB")); - } - let mut value = checked_json(raw).map_err(|_| { - failure( - "WFT-BACKEND-VERSION", - "Malformed or duplicate-key backend manifest JSON", - ) - })?; - let opaque=take_manifest_opaque(&mut value)?; - let mut manifest: Manifest = serde_json::from_value(value).map_err(|_| { - failure( - "WFT-BACKEND-VERSION", - "Backend manifest has unknown or malformed members", - ) - })?; - restore_manifest_opaque(&mut manifest.target_profiles,&mut manifest.capabilities,opaque)?; - validate_manifest(&manifest)?; - Ok(manifest) + weft_backend::manifest::validate_manifest_json::(raw) } pub fn validate_manifest(m: &Manifest) -> Result<()> { - if !matches!(m.interface_version.as_str(), "weft-backend/0.2.0" | "weft-backend/0.2.1" | "weft-backend/0.2.2" | "weft-backend/0.2.3" | "weft-backend/0.2.4" | "weft-backend/0.2.5" | "weft-backend/0.2.6" | "weft-backend/0.2.7" | "weft-backend/0.2.8" | "weft-backend/0.2.9") || !valid_languages(&m.language_profiles, &m.interface_version) { - return Err(failure( - "WFT-BACKEND-VERSION", - "Unsupported backend interface or language/IR pair", - )); - } - if ![&m.backend_id, &m.backend_version, &m.binding_profile] - .iter() - .all(|s| nonempty(s)) - || m.target_profiles.is_empty() - || m.target_profiles.len() > 256 - || m.capabilities.is_empty() - || m.capabilities.len() > 4096 - || !unique_strings(&m.evidence) - { - return Err(failure( - "WFT-BACKEND-VERSION", - "Backend manifest identity, collection bounds or evidence IDs are invalid", - )); - } - let mut targets = BTreeSet::new(); - for t in &m.target_profiles { - if ![ - &t.id, - &t.engine, - &t.engine_version, - &t.storage_layout_revision, - &t.publication_revision, - ] - .iter() - .all(|s| nonempty(s)) - || !t.session_settings.is_object() - || !targets.insert(t.id.as_str()) - { - return Err(failure( - "WFT-BACKEND-VERSION", - "Target profiles must be distinct, pinned and structurally explicit", - )); - } - } - let mut caps = BTreeSet::new(); - for c in &m.capabilities { - if !nonempty(&c.id) - || !caps.insert(c.id.as_str()) - || c.target_profiles.is_empty() - || !unique_strings(&c.target_profiles) - || c.target_profiles - .iter() - .any(|p| !targets.contains(p.as_str())) - || !valid_languages(&c.language_profiles, &m.interface_version) - || c.language_profiles - .iter() - .any(|p| !m.language_profiles.contains(p)) - { - return Err(failure( - "WFT-BACKEND-VERSION", - "Capabilities must bind distinct IDs to declared target and language profiles", - )); - } - if !c.logical_domain.is_object() - || c.logical_domain.as_object().unwrap().is_empty() - || !c.result_domain.is_object() - || c.result_domain.as_object().unwrap().is_empty() - || !unique_strings(&c.constraints) - || !unique_strings(&c.evidence) - || c.evidence.iter().any(|e| !m.evidence.contains(e)) - || (c.status == Status::Supported && c.evidence.is_empty()) - { - return Err(failure( - "WFT-CAPABILITY", - "Capability domains, constraints and qualified evidence must be explicit", - )); - } - let mut obligations = BTreeSet::new(); - for o in &c.obligations { - if !nonempty(&o.id) - || !o.parameters.is_object() - || (!o.failure_code.starts_with("WFT-") || o.failure_code.len() <= 4) - || !o.failure_code[4..] - .bytes() - .all(|b| b.is_ascii_uppercase() || b.is_ascii_digit() || b == b'-') - || !obligations.insert(o.id.as_str()) - { - return Err(failure( - "WFT-OBLIGATION", - "Capability obligations are malformed or ambiguous", - )); - } - } - } - Ok(()) + weft_backend::manifest::validate_manifest::(m) } - /// Backend-owned mapping and target types remain statically checked inside this trait. /// Only an explicitly registered host object implements it; manifests never load code. pub trait Backend: Send + Sync + 'static { @@ -299,273 +75,89 @@ impl Plan<'_> { } } } -#[derive(Debug, Clone)] -pub struct BindingInput { - pub profile: String, - pub json: String, - pub sha256: String, -} -#[derive(Debug, Clone)] -pub struct Target { - pub backend_id: String, - pub backend_version: String, - pub profile_id: String, - pub allow_candidate: bool, -} -/// Every selected identity must receive an explicit binding/codec assessment. -#[derive(Debug, Clone, Default, PartialEq)] -pub struct Selection { - pub records: Vec, - pub fields: Vec, - pub types: Vec, - pub relationships: Vec, -} -pub struct Context<'a> { - pub catalog: &'a crate::model::Catalog, - pub plan: Plan<'a>, - pub target: &'a TargetProfile, - pub binding: &'a BindingInput, - pub binding_value: &'a Value, - pub selection: &'a Selection, -} -pub struct Validated { - pub mapping: T, - pub additional_capabilities: Vec, - pub coverage: Selection, - pub obligations: Vec, -} -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct Assessment { - pub id: String, - pub status: Status, - pub evidence: Vec, - pub obligations: Vec, -} -#[derive(Debug, Clone, PartialEq, Serialize)] -#[serde(untagged)] -pub enum ParameterType { - Atomic(crate::ir::LogicalType), - Exact { kind: ExactParameterKind, #[serde(rename="scalarType")] scalar_type:crate::application_model::ExactScalarType }, -} -#[derive(Debug, Clone, PartialEq, Serialize)] -pub enum ExactParameterKind { #[serde(rename="exactScalar")] ExactScalar } -impl From for ParameterType { - fn from(value:crate::ir::LogicalType)->Self { Self::Atomic(value) } -} -impl ParameterType { - pub fn atomic(&self)->Result<&crate::ir::LogicalType> { - match self { Self::Atomic(value)=>Ok(value), _=>Err(failure("WFT-EMISSION","Atomic parameter required by this backend")) } +pub use weft_backend::boundary::{Assessment, BindingInput, Selection, Target, Validated}; +/// The binding context a weft backend receives. +pub type Context<'a> = weft_backend::boundary::Context<'a, WeftPlans>; +pub use weft_backend::sql::{ + Column, Emission, ExactParameterKind, OuterJoin, ParameterSlot, ParameterType, Representation, ScalarCarrier, + ScalarDecoder, +}; +pub use weft_backend::boundary::Qualification; +/// A weft compilation: a [`weft_backend::boundary::Compilation`] of SQL [`Emission`]. +pub type Compilation = weft_backend::boundary::Compilation; + +/// weft's frontend for the shared backend pipeline: weft-sql 0.1-0.3 plans and +/// engine pages, weft-backend/0.2.x manifests. +pub struct WeftPlans; +impl ManifestRules for WeftPlans { + fn admits_interface(interface: &str) -> bool { + matches!(interface, "weft-backend/0.2.0" | "weft-backend/0.2.1" | "weft-backend/0.2.2" | "weft-backend/0.2.3" | "weft-backend/0.2.4" | "weft-backend/0.2.5" | "weft-backend/0.2.6" | "weft-backend/0.2.7" | "weft-backend/0.2.8" | "weft-backend/0.2.9") + } + fn admits_language(interface: &str, p: &LanguageProfile) -> bool { + fn profile_key(p: &LanguageProfile) -> (&str, &str) { + (&p.dialect_profile, &p.ir_version) + } + matches!( + profile_key(p), + ("weft-sql/0.1.0", "weft-ir/0.1.0") | ("weft-sql/0.2.0", "weft-ir/0.2.0") | ("weft-sql/0.3.0", "weft-ir/0.3.0") | ("weft-sql/0.2.0", "weft-ir/engine-navigation/0.1") + ) || (interface == "weft-backend/0.2.8" && matches!(profile_key(p),("weft-sql/0.2.0","weft-ir/engine-ordered-page/0.1")|("weft-sql/0.2.0","weft-ir/engine-navigation/0.2")|("weft-sql/0.2.0","weft-ir/engine-ordered-read/0.1"))) || (interface == "weft-backend/0.2.1" && profile_key(p) == ("weft-sql/0.2.0", "weft-ir/0.2.1")) + || (interface == "weft-backend/0.2.9" && profile_key(p) == ("weft-sql/0.2.0", "weft-ir/0.2.8")) + || (interface == "weft-backend/0.2.7" && profile_key(p) == ("weft-sql/0.2.0", "weft-ir/0.2.7")) + || (interface == "weft-backend/0.2.6" && profile_key(p) == ("weft-sql/0.2.0", "weft-ir/0.2.6")) + || (interface == "weft-backend/0.2.5" && profile_key(p) == ("weft-sql/0.2.0", "weft-ir/0.2.5")) + || (interface == "weft-backend/0.2.4" && profile_key(p) == ("weft-sql/0.2.0", "weft-ir/0.2.4")) + || (interface == "weft-backend/0.2.3" && profile_key(p) == ("weft-sql/0.2.0", "weft-ir/0.2.3")) + || (interface == "weft-backend/0.2.2" && profile_key(p) == ("weft-sql/0.2.0", "weft-ir/0.2.2")) } } -impl PartialEq for ParameterType { - fn eq(&self,other:&crate::ir::LogicalType)->bool { matches!(self,Self::Atomic(value) if value==other) } -} -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct ParameterSlot { - pub position: usize, - pub logical_type: ParameterType, - pub value: String, - pub origin: Value, -} -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "kebab-case")] -pub enum ScalarCarrier { - Text, - Boolean, -} -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "kebab-case")] -pub enum ScalarDecoder { - Text, - Boolean, - ExactInteger, - ExactDecimal, -} -#[derive(Debug, Clone, PartialEq, Eq, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct OuterJoin { - pub scan: String, - pub record: crate::ir::Identity, -} -#[derive(Debug, Clone, Serialize)] -#[serde(tag = "kind", rename_all = "camelCase")] -pub enum Representation { - NativeMetadata { - metadata: crate::application_ir::NativeMetadata, - #[serde(rename = "logicalType")] - logical_type: crate::ir::LogicalType, - carrier: ScalarCarrier, - decoder: ScalarDecoder, - }, - Scalar { - #[serde(rename = "logicalType")] - logical_type: crate::ir::LogicalType, - carrier: ScalarCarrier, - decoder: ScalarDecoder, - }, - Value { - descriptor: crate::ir::Identity, - #[serde(rename = "nativeNull")] - native_null: bool, - #[serde(rename = "outerJoin", skip_serializing_if = "Option::is_none")] - outer_join: Option, - }, - CompleteRelatedKeys { - relationship: crate::application_model::RelationshipIdentity, - key: crate::application_model::AuthoredKey, - bound: u16, - }, - RelatedKeys { - relationship: crate::application_model::RelationshipIdentity, - key: crate::application_model::AuthoredKey, - bound: u16, - }, -} -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct Column { - pub position: usize, - pub output_name: String, - /// Physical result label; only explicitly admitted 0.3 positional outputs may carry it. - #[serde(skip_serializing_if = "Option::is_none")] - pub carrier_name: Option, - pub representation: Representation, - pub source_identities: Vec, - pub nullable: bool, -} -#[derive(Debug, Clone, Serialize)] -pub struct Emission { - pub sql: String, - pub parameters: Vec, - pub columns: Vec, - pub obligations: Vec, -} -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct Qualification { - pub assessment: Assessment, - pub declaration: Capability, -} -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct Compilation { - pub backend_id: String, - pub backend_version: String, - pub target_profile: TargetProfile, - pub binding_profile: String, - pub binding_sha256: String, - pub qualifications: Vec, - pub emission: Emission, -} -trait Registered: Send + Sync { - fn manifest(&self) -> &Manifest; - fn compile( - &self, - catalog: &crate::model::Catalog, - plan: Plan<'_>, - target: &Target, - binding: &BindingInput, - ) -> Result; -} -struct Adapter { - backend: B, - manifest: Manifest, -} -impl Registered for Adapter { - fn manifest(&self) -> &Manifest { - &self.manifest +impl Frontend for WeftPlans { + type Program<'a> = Plan<'a>; + fn reborrow<'s, 'l: 's>(plan: Plan<'l>) -> Plan<'s> { + plan } - fn compile( - &self, - catalog: &crate::model::Catalog, - plan: Plan<'_>, - target: &Target, - binding: &BindingInput, - ) -> Result { - if target.backend_version != self.manifest.backend_version { - return Err(failure( - "WFT-BACKEND-VERSION", - "Selected backend version is not registered", - )); - } - let language = plan.language(); - let actual_ir = match plan { + fn language(plan: Plan<'_>) -> LanguageProfile { + plan.language() + } + fn ir_version(plan: Plan<'_>) -> String { + match plan { Plan::V01(p) => p.ir_version.clone(), Plan::V02(p) => p.ir_version.clone(), Plan::V03(p) => p.ir_version.clone(), #[cfg(feature="engine-ordered-page-candidate")] Plan::Ordered(p)=>p.version().into(), - }; - if actual_ir != language.ir_version || !unique_strings(plan.capabilities()) { - return Err(failure( - "WFT-BACKEND-VERSION", - "Typed plan version or operation identities are invalid", - )); - } - if !self.manifest.language_profiles.contains(&language) { - return Err(failure( - "WFT-BACKEND-VERSION", - "Registered backend does not accept the selected dialect/IR pair", - )); - } - let profile = self - .manifest - .target_profiles - .iter() - .find(|p| p.id == target.profile_id) - .ok_or_else(|| { - failure( - "WFT-BACKEND-VERSION", - "Selected target profile is not registered", - ) - })?; - if binding.profile != self.manifest.binding_profile { - return Err(failure( - "WFT-BINDING", - "Binding profile does not match the selected registered backend", - )); - } - if plan.pins() != catalog.pins().as_slice() { - return Err(failure( - "WFT-PIN", - "Plan model pins do not match the supplied catalog", - )); } - if binding.json.len() > 4 * 1024 * 1024 { - return Err(failure("WFT-LIMIT", "Binding exceeds four MiB")); - } - if crate::json::sha256(binding.json.as_bytes()) != binding.sha256 { - return Err(failure("WFT-PIN", "Binding byte digest mismatch")); - } - let value = (if target.backend_id == "truss.postgresql.runtime" + } + fn capabilities(plan: Plan<'_>) -> Vec { + plan.capabilities().to_vec() + } + fn pins(plan: Plan<'_>) -> Vec { + plan.pins().to_vec() + } + fn selection(plan: Plan<'_>) -> Selection { + selected(plan) + } + fn read_binding(target: &Target, actual_ir: &str, json: &str) -> std::result::Result { + (if target.backend_id == "truss.postgresql.runtime" && ((target.backend_version == "0.4.41" && target.profile_id == "truss-runtime-pg18.6/0.5" && actual_ir == "weft-ir/0.2.7") || (matches!(target.backend_version.as_str(),"0.4.43"|"0.4.44") && matches!(target.profile_id.as_str(),"truss-runtime-pg18.6/0.6"|"truss-runtime-pg18.6-export/0.2") && actual_ir == "weft-ir/0.2.8")) { - crate::json::checked_truss_runtime_catalog_json(&binding.json) + crate::json::checked_truss_runtime_catalog_json(json) } else { - checked_json(&binding.json) - }).map_err(|_| { - failure( - "WFT-BINDING", - "Binding JSON is malformed or repeats members", - ) - })?; - if !value.is_object() { - return Err(failure("WFT-BINDING", "Binding root must be an object")); - } - let selection = selected(plan); + checked_json(json) + }).map_err(|_| ()) + } + fn preflight(manifest: &Manifest, plan: Plan<'_>, target: &Target, language: &LanguageProfile) -> Result<()> { + let language = language.clone(); // Unbounded source integers are a new explicit opt-in. Refuse before // unchanged backend binding code can assume an authored width. if plan.capabilities().iter().any(|c| c == "type.integer.unbounded") - && !self.manifest.capabilities.iter().any(|c| c.id == "type.integer.unbounded" + && !manifest.capabilities.iter().any(|c| c.id == "type.integer.unbounded" && (c.status == Status::Supported || (c.status == Status::Candidate && target.allow_candidate)) && c.target_profiles.contains(&target.profile_id) && c.language_profiles.contains(&language)) { return Err(failure("WFT-CAPABILITY", "Selected backend profile does not admit mathematical integer source domains")); } if plan.capabilities().iter().any(|c| c == "type.decimal.unbounded") - && !self.manifest.capabilities.iter().any(|c| c.id == "type.decimal.unbounded" + && !manifest.capabilities.iter().any(|c| c.id == "type.decimal.unbounded" && (c.status == Status::Supported || (c.status == Status::Candidate && target.allow_candidate)) && c.target_profiles.contains(&target.profile_id) && c.language_profiles.contains(&language)) { return Err(failure("WFT-CAPABILITY", "Selected backend profile does not admit mathematical decimal source domains")); @@ -575,7 +167,7 @@ impl Registered for Adapter { if matches!(plan, Plan::V03(_)) { let distinct_count=plan.capabilities().iter().any(|id|id=="aggregate.countDistinct"); for id in plan.capabilities().iter().filter(|id| ["join.left", "value.outerJoinPresence", "compare.less", "compare.lessEqual", "compare.greaterEqual", "compare.notEqual", "compare.scalarJoin", "project.positionedOutputs", "project.distinct", "predicate.nativeNull", "compare.nullAwareStringEqual", "value.nativeNull", "predicate.stringIn", "aggregate.countDistinct", "aggregate.countDistinct.optional", "aggregate.havingCountDistinctGreater"].contains(&id.as_str()) || (distinct_count && ["aggregate","group"].contains(&id.as_str()))) { - if !self.manifest.capabilities.iter().any(|c| &c.id == id + if !manifest.capabilities.iter().any(|c| &c.id == id && (c.status == Status::Supported || (c.status == Status::Candidate && target.allow_candidate)) && c.target_profiles.contains(&target.profile_id) && c.language_profiles.contains(&language)) { return Err(failure("WFT-CAPABILITY", "Selected backend profile does not admit the new comparison operator")); @@ -583,207 +175,59 @@ impl Registered for Adapter { } } if plan.capabilities().iter().any(|id| id == "compare.binary.optionalEqual") - && !self.manifest.capabilities.iter().any(|c| c.id == "compare.binary.optionalEqual" + && !manifest.capabilities.iter().any(|c| c.id == "compare.binary.optionalEqual" && (c.status == Status::Supported || (c.status == Status::Candidate && target.allow_candidate)) && c.target_profiles.contains(&target.profile_id) && c.language_profiles.contains(&language)) { return Err(failure("WFT-CAPABILITY", "Selected backend profile does not admit optional binary equality")); } for id in plan.capabilities().iter().filter(|id| matches!(id.as_str(),"compare.timestamp.instantEqual"|"compare.timestamp.optionalInstantEqual")) { - if !self.manifest.capabilities.iter().any(|c|&c.id==id + if !manifest.capabilities.iter().any(|c|&c.id==id && (c.status==Status::Supported || (c.status==Status::Candidate&&target.allow_candidate)) && c.target_profiles.contains(&target.profile_id)&&c.language_profiles.contains(&language)) { return Err(failure("WFT-CAPABILITY","Selected backend does not admit exact instant equality")); } } if plan.capabilities().iter().any(|id| id == "type.decimal.scale2") - && !self.manifest.capabilities.iter().any(|c| c.id == "type.decimal.scale2" + && !manifest.capabilities.iter().any(|c| c.id == "type.decimal.scale2" && (c.status == Status::Supported || (c.status == Status::Candidate && target.allow_candidate)) && c.target_profiles.contains(&target.profile_id) && c.language_profiles.contains(&language)) { return Err(failure("WFT-CAPABILITY", "Selected backend profile does not admit declared decimal18,2")); } - let context = Context { - catalog, - plan, - target: profile, - binding, - binding_value: &value, - selection: &selection, - }; - let validated = self.backend.validate_binding(&context)?; - if !coverage_contains(&validated.coverage, &selection) { - return Err(failure( - "WFT-BINDING", - "Backend mapping omits a selected record, field, type or relationship identity", - )); - } - if !unique_strings(&validated.additional_capabilities) - || validated.additional_capabilities.len() > 4096 - { - return Err(failure( - "WFT-CAPABILITY", - "Binding-derived capabilities must be distinct and bounded", - )); - } - let required: BTreeSet = plan - .capabilities() - .iter() - .chain(&validated.additional_capabilities) - .cloned() - .collect(); - let assessments = self.backend.assess(&context, &validated.mapping)?; - let mut qualifications = Vec::new(); - let mut seen = BTreeSet::new(); - let mut obligations = Vec::new(); - merge_obligations(&mut obligations, &validated.obligations)?; - for a in &assessments { - if !seen.insert(a.id.as_str()) || !required.contains(&a.id) { - return Err(failure( - "WFT-CAPABILITY", - "Assessment has duplicate or unrequested operations", - )); - } - let declaration = self - .manifest - .capabilities - .iter() - .find(|c| { - c.id == a.id - && c.target_profiles.contains(&target.profile_id) - && c.language_profiles.contains(&language) - }) - .ok_or_else(|| { - failure( - "WFT-CAPABILITY", - "Operation is not declared for the selected target/language profile", - ) - })?; - if declaration.status == Status::Unsupported || a.status == Status::Unsupported { - return Err(failure( - "WFT-CAPABILITY", - "Selected operation is unsupported", - )); - } - if declaration.status == Status::Candidate && a.status == Status::Supported { - return Err(failure( - "WFT-CAPABILITY", - "Candidate declaration cannot be upgraded by assessment", - )); - } - if a.status == Status::Candidate && !target.allow_candidate { - return Err(failure( - "WFT-CAPABILITY", - "Candidate operation requires explicit allowCandidate", - )); - } - if !unique_strings(&a.evidence) - || a.evidence.iter().any(|e| !declaration.evidence.contains(e)) - || (a.status == Status::Supported && a.evidence.is_empty()) - { - return Err(failure( - "WFT-CAPABILITY", - "Assessment has missing or undeclared qualification evidence", - )); - } - merge_obligations(&mut obligations, &declaration.obligations)?; - merge_obligations(&mut obligations, &a.obligations)?; - qualifications.push(Qualification { - assessment: a.clone(), - declaration: declaration.clone(), - }); - } - if seen.len() != required.len() { - return Err(failure( - "WFT-CAPABILITY", - "Assessment omits a required operation", - )); - } - let lowered = self.backend.lower(&context, &validated.mapping)?; - let mut emission = self.backend.emit(&context, &lowered)?; - crate::backend_emission::validate(plan, &emission, &selection, &assessments)?; - let emitted_obligations = std::mem::take(&mut emission.obligations); - merge_obligations(&mut emission.obligations, &emitted_obligations)?; - merge_obligations(&mut emission.obligations, &obligations)?; - Ok(Compilation { - backend_id: self.manifest.backend_id.clone(), - backend_version: self.manifest.backend_version.clone(), - target_profile: profile.clone(), - binding_profile: binding.profile.clone(), - binding_sha256: binding.sha256.clone(), - qualifications, - emission, - }) + Ok(()) } -} -fn merge_obligations(into: &mut Vec, from: &[Obligation]) -> Result<()> { - for o in from { - if !nonempty(&o.id) - || !o.parameters.is_object() - || !o.failure_code.starts_with("WFT-") - || o.failure_code.len() <= 4 - || !o.failure_code[4..] - .bytes() - .all(|b| b.is_ascii_uppercase() || b.is_ascii_digit() || b == b'-') - { - return Err(failure( - "WFT-OBLIGATION", - "Backend returned a malformed obligation", - )); - } - if let Some(existing) = into.iter().find(|e| e.id == o.id) { - if existing != o { - return Err(failure( - "WFT-OBLIGATION", - "Obligation ID has conflicting requirements", - )); - } - } else { - into.push(o.clone()); + fn admit_catalog(catalog: &crate::model::Catalog) -> Result<()> { + if catalog.admission().bounded_current_strings { + return Err(failure("WFT-VERSION", "Bounded-string catalogs require the dedicated current-query candidate compiler")); } + Ok(()) } - into.sort_by(|a, b| a.id.cmp(&b.id)); - Ok(()) } -fn coverage_contains(coverage: &Selection, selected: &Selection) -> bool { - selected - .records - .iter() - .all(|i| coverage.records.contains(i)) - && selected.fields.iter().all(|i| coverage.fields.contains(i)) - && selected.types.iter().all(|i| coverage.types.contains(i)) - && selected - .relationships - .iter() - .all(|i| coverage.relationships.contains(i)) +impl SqlChecks for WeftPlans { + fn check_emission(plan: Plan<'_>, emission: &Emission, selection: &Selection, assessments: &[Assessment]) -> Result<()> { + crate::backend_emission::validate(plan, emission, selection, assessments) + } } +/// Bridges a weft [`Backend`] to the shared pipeline. +struct Weft(B); +impl weft_backend::boundary::Backend for Weft { + type Mapping = B::Mapping; + type TargetPlan = B::TargetPlan; + type Artifact = Emission; + fn describe(&self) -> Result { self.0.describe() } + fn validate_binding(&self, context: &Context<'_>) -> Result> { self.0.validate_binding(context) } + fn assess(&self, context: &Context<'_>, mapping: &B::Mapping) -> Result> { self.0.assess(context, mapping) } + fn lower(&self, context: &Context<'_>, mapping: &B::Mapping) -> Result { self.0.lower(context, mapping) } + fn emit(&self, context: &Context<'_>, plan: &B::TargetPlan) -> Result { self.0.emit(context, plan) } +} +/// Registered weft backends by ID; the checked pipeline is `weft_backend`'s. #[derive(Default)] -pub struct Registry { - backends: std::collections::BTreeMap>, -} +pub struct Registry(weft_backend::boundary::Registry); impl Registry { pub fn register(&mut self, backend: B) -> Result<()> { - let manifest = - std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| backend.describe())) - .map_err(|_| { - failure( - "WFT-BACKEND-FAILURE", - "Registered backend panicked while describing itself", - ) - })??; - validate_manifest(&manifest)?; - if self.backends.contains_key(&manifest.backend_id) { - return Err(failure( - "WFT-BACKEND-VERSION", - "Backend identity is already registered", - )); - } - self.backends.insert( - manifest.backend_id.clone(), - Box::new(Adapter { backend, manifest }), - ); - Ok(()) + self.0.register(Weft(backend)) } pub fn manifest(&self, id: &str) -> Option<&Manifest> { - self.backends.get(id).map(|b| b.manifest()) + self.0.manifest(id) } pub fn compile( &self, @@ -792,24 +236,7 @@ impl Registry { target: &Target, binding: &BindingInput, ) -> Result { - if catalog.bounded_current_strings { - return Err(failure("WFT-VERSION", "Bounded-string catalogs require the dedicated current-query candidate compiler")); - } - let backend = self.backends.get(&target.backend_id).ok_or_else(|| { - failure( - "WFT-BACKEND-MISSING", - "Selected backend identity is not registered", - ) - })?; - std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { - backend.compile(catalog, plan, target, binding) - })) - .map_err(|_| { - failure( - "WFT-BACKEND-FAILURE", - "Registered backend panicked during compilation", - ) - })? + self.0.compile(catalog, plan, target, binding) } } /// Exact original application selection, including admitted source navigation. @@ -1004,7 +431,7 @@ fn selected(plan: Plan<'_>) -> Selection { #[cfg(test)] mod tests { use super::*; - use serde_json::json; + use serde_json::{json, Value}; #[test] fn arithmetic_selection_retains_original_operands_and_versions() { let cases: Value = serde_json::from_str(include_str!("../../../tests/application/fixtures/cases.json")).unwrap(); @@ -1060,6 +487,7 @@ mod tests { #[cfg(test)] mod lineage_candidate_refusal_tests{ use super::*; + use serde_json::Value; struct Closed; impl Backend for Closed{ type Mapping=();type TargetPlan=(); diff --git a/crates/weft-core/src/backend03_emission.rs b/crates/weft-core/src/backend03_emission.rs index 864a760c..735a354c 100644 --- a/crates/weft-core/src/backend03_emission.rs +++ b/crates/weft-core/src/backend03_emission.rs @@ -704,7 +704,7 @@ fn validate_parameter( } let domain = crate::arithmetic_resolve::token_domain( &p.value, - Some(&Family::Decimal), + Some(weft_model::types::ScalarFamily::Decimal), &crate::ir::Span { start: 0, end: 0 }, ) .map_err(|_| fail("WFT-EMIT", "Decimal token is not exact base ten"))?; diff --git a/crates/weft-core/src/backend03_registry.rs b/crates/weft-core/src/backend03_registry.rs index 1dab8af5..41d80eb9 100644 --- a/crates/weft-core/src/backend03_registry.rs +++ b/crates/weft-core/src/backend03_registry.rs @@ -45,7 +45,7 @@ impl Registry { target: &Target, binding: &BindingInput, ) -> Result { - if catalog.bounded_current_strings { + if catalog.admission().bounded_current_strings { return Err(fail("WFT-VERSION", "Bounded-string catalogs require the dedicated current-query candidate compiler")); } let b = self diff --git a/crates/weft-core/src/backend_emission.rs b/crates/weft-core/src/backend_emission.rs index 76897366..c3f87770 100644 --- a/crates/weft-core/src/backend_emission.rs +++ b/crates/weft-core/src/backend_emission.rs @@ -117,7 +117,7 @@ pub(crate) fn validate( if !admitted || logical_type.facets != serde_json::json!({"scale":scale}) { return Err(fail("Arithmetic decimal parameter requires explicit 0.3 capability admission")); } - let domain = crate::arithmetic_resolve::token_domain(&p.value, Some(&Family::Decimal), &crate::ir::Span {start:0,end:0}) + let domain = crate::arithmetic_resolve::token_domain(&p.value, Some(weft_model::types::ScalarFamily::Decimal), &crate::ir::Span {start:0,end:0}) .map_err(|_| fail("Arithmetic decimal parameter is not exact base-ten text"))?; if domain != (crate::arithmetic_resolve::Domain::Decimal {scale}) { return Err(fail("Arithmetic decimal parameter changes its original lexical scale")); diff --git a/crates/weft-core/src/compile.rs b/crates/weft-core/src/compile.rs index b465b73e..2fc38c05 100644 --- a/crates/weft-core/src/compile.rs +++ b/crates/weft-core/src/compile.rs @@ -348,7 +348,7 @@ impl Compiler { if sdk_scalar_presence && (req.modules.is_empty()||req.modules.iter().any(|m|m.pin.umf_version!="0.10.0")||req.interface_version!="weft-compile/0.2.0" || req.dialect!="weft-sql/0.2.0" || req.target.backend_id!="ashlar.databricks" || !matches!(req.target.backend_version.as_str(),"0.1.0-local-spark4-sdk-candidate"|"0.1.0-local-spark4-sdk-pages-candidate") || req.target.target_profile!="spark4.0.1-delta4.0.0-sdk-candidate" || (ordered_page.is_some() && req.target.backend_version!="0.1.0-local-spark4-sdk-pages-candidate") || page.is_some() || cursor_delivery || field_projection_decimal38 || req.security.is_some() || req.exact_parameters.is_some()) { return Err(Diagnostic::new("WFT-SDK-PRESENCE","input","Explicit non-temporal SDK presence profile required")); } - let mut catalog = if matches!(req.interface_version.as_str(), "weft-security-compile/0.1.0" | "weft-security-compile/0.2.0" | "weft-compile/0.5.0") { + let catalog = if matches!(req.interface_version.as_str(), "weft-security-compile/0.1.0" | "weft-security-compile/0.2.0" | "weft-compile/0.5.0") { crate::model::Catalog::prepare_security(req.modules)? } else if truss_runtime_catalog { crate::model::Catalog::prepare_truss_runtime(req.modules, exact_numbers)? @@ -357,11 +357,13 @@ impl Compiler { } else { crate::model::Catalog::prepare(req.modules)? }; - catalog.identity_name_fallback = identity_name_fallback; - catalog.field_projection_decimal38=field_projection_decimal38; - catalog.sdk_scalar_presence=sdk_scalar_presence; - catalog.exact_scalars = matches!(req.interface_version.as_str(), "weft-compile/0.2.1" | "weft-compile/0.2.2" | "weft-compile/0.2.3" | "weft-compile/0.2.4" | "weft-compile/0.2.5" | "weft-compile/0.2.6" | "weft-compile/0.2.7"|"weft-compile/0.2.8"); - catalog.exact_dates = matches!(req.interface_version.as_str(),"weft-compile/0.2.7"|"weft-compile/0.2.8"); + let catalog = catalog.admit(|admission| { + admission.identity_name_fallback = identity_name_fallback; + admission.field_projection_decimal38=field_projection_decimal38; + admission.sdk_scalar_presence=sdk_scalar_presence; + admission.exact_scalars = matches!(req.interface_version.as_str(), "weft-compile/0.2.1" | "weft-compile/0.2.2" | "weft-compile/0.2.3" | "weft-compile/0.2.4" | "weft-compile/0.2.5" | "weft-compile/0.2.6" | "weft-compile/0.2.7"|"weft-compile/0.2.8"); + admission.exact_dates = matches!(req.interface_version.as_str(),"weft-compile/0.2.7"|"weft-compile/0.2.8"); + }); if let Some(source)=req.security.as_ref() { let packet=crate::security_source::SecuritySourcePacket::read( source["policyJson"].as_str().expect("security request schema"), @@ -681,7 +683,7 @@ fn validate_application_plan(catalog:&crate::model::Catalog,plan:&mut crate::app if interface_version=="weft-compile/0.2.8" { plan.ir_version="weft-ir/0.2.8".into(); if !Plan028::is_valid(&serde_json::to_value(&*plan).expect("typed selective query plan")) {return Err(Diagnostic::new("WFT-IR","resolve","Resolved plan violates selective query semantics"));} - } else if catalog.exact_dates { + } else if catalog.admission().exact_dates { plan.ir_version="weft-ir/0.2.7".into(); if !Plan027::is_valid(&serde_json::to_value(&*plan).expect("typed canonical date plan")) {return Err(Diagnostic::new("WFT-IR","resolve","Resolved plan violates canonical date semantics"));} } else if timestamp_equality { @@ -700,7 +702,7 @@ fn validate_application_plan(catalog:&crate::model::Catalog,plan:&mut crate::app plan.ir_version = "weft-ir/0.2.2".into(); let value = serde_json::to_value(&*plan).map_err(|_| Diagnostic::new("WFT-IR", "resolve", "Invalid resolved entity plan"))?; if !Plan022::is_valid(&value) { return Err(Diagnostic::new("WFT-IR", "resolve", "Resolved plan violates versioned entity semantics")); } - } else if catalog.exact_scalars { plan.ir_version = "weft-ir/0.2.1".into(); } + } else if catalog.admission().exact_scalars { plan.ir_version = "weft-ir/0.2.1".into(); } Ok(()) } diff --git a/crates/weft-core/src/current_presence_graph_requirements.rs b/crates/weft-core/src/current_presence_graph_requirements.rs index 0fee6372..496d02d9 100644 --- a/crates/weft-core/src/current_presence_graph_requirements.rs +++ b/crates/weft-core/src/current_presence_graph_requirements.rs @@ -120,7 +120,7 @@ pub fn derive<'context,'parent>(context:&'context CurrentPresencePreparation<'pa "core-relationship"=>{ let reference=SecurityAssociationRef::read(&selection["relationship"])?;let SecurityAssociationRef::Relationship(r)=&reference else{return Err(fail())}; let pins=catalog.pins();let pin=pins.iter().find(|p|p.document_id==r.document_id).ok_or_else(fail)?;let revision=pin.revision.clone(); - let doc=catalog.documents.iter().find(|d|d["id"]==r.document_id).ok_or_else(fail)?;let module=doc["modules"].as_array().ok_or_else(fail)?.iter().find(|m|m["id"]==r.module_id).ok_or_else(fail)?; + let doc=catalog.documents().iter().find(|d|d["id"]==r.document_id).ok_or_else(fail)?;let module=doc["modules"].as_array().ok_or_else(fail)?.iter().find(|m|m["id"]==r.module_id).ok_or_else(fail)?; let mut matches=module["relationships"].as_array().ok_or_else(fail)?.iter().filter(|a|a["id"]==r.relationship_id);let authored=matches.next().ok_or_else(fail)?;if matches.next().is_some(){return Err(fail())} // Keep original metadata, including annotations and exact parsed values. // These required declarations enable correspondence, not semantic admission. diff --git a/crates/weft-core/src/current_string_domain.rs b/crates/weft-core/src/current_string_domain.rs index 1646248d..34b8f21b 100644 --- a/crates/weft-core/src/current_string_domain.rs +++ b/crates/weft-core/src/current_string_domain.rs @@ -1,173 +1,3 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 -//! Exact bounded Unicode scalar domains for the explicitly selected new candidate. -//! Pure type/value checks confer no permission or native correspondence. -use crate::{error::{Diagnostic, Result}, ir::{Family, LogicalType}}; -use serde_json::{json, Value}; - -const MAX_BOUND: u64 = 9_007_199_254_740_991; -fn fail() -> Diagnostic { - Diagnostic::new("WFT-CURRENT-STRING-DOMAIN", "capability", "Current string domain refused") -} -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub struct Length { min: u64, max: u64 } -impl Length { - pub fn min(&self) -> u64 { self.min } - pub fn max(&self) -> u64 { self.max } -} -// Interpret the JSON number exactly, without f64 or exponent-sized allocation. -fn bound(value: &Value) -> Result { - let number = value.as_number().ok_or_else(fail)?; - if let Some(n) = number.as_u64() { return if n <= MAX_BOUND { Ok(n) } else { Err(fail()) }; } - let token = number.to_string(); - let (mantissa, exponent) = token.split_once(['e','E']).unwrap_or((&token,"0")); - let negative = mantissa.starts_with('-'); - let mantissa = mantissa.strip_prefix('-').unwrap_or(mantissa); - let fraction = mantissa.split_once('.').map_or(0, |(_,f)| f.len()); - let digits: Vec = mantissa.bytes().filter(|b| *b != b'.').collect(); - let Some(first) = digits.iter().position(|b| *b != b'0') else { return Ok(0); }; - if negative { return Err(fail()); } - let digits = &digits[first..]; - let shift = exponent.parse::().map_err(|_| fail())? - .checked_sub(i64::try_from(fraction).map_err(|_|fail())?).ok_or_else(fail)?; - let (significant, zeros) = if shift < 0 { - let removed = usize::try_from(shift.checked_neg().ok_or_else(fail)?).map_err(|_|fail())?; - if removed >= digits.len() || digits[digits.len()-removed..].iter().any(|b| *b != b'0') { return Err(fail()); } - (&digits[..digits.len()-removed], 0) - } else { - (digits, usize::try_from(shift).map_err(|_|fail())?) - }; - if significant.len().checked_add(zeros).ok_or_else(fail)? > 16 { return Err(fail()); } - let mut n = 0u64; - for digit in significant { n = n.checked_mul(10).and_then(|v| v.checked_add((digit-b'0') as u64)).ok_or_else(fail)?; } - for _ in 0..zeros { n = n.checked_mul(10).ok_or_else(fail)?; } - if n > MAX_BOUND { return Err(fail()); } - Ok(n) -} -/// Empty facets mean the existing unconstrained String, not a guessed bound. -pub fn bounds(facets: &Value) -> Result> { - let object = facets.as_object().ok_or_else(fail)?; - if object.is_empty() { return Ok(None); } - if object.len() != 1 { return Err(fail()); } - let length = object.get("length").and_then(Value::as_object).ok_or_else(fail)?; - if length.len() != 3 || length.get("unit").and_then(Value::as_str) != Some("unicode-scalar") { - return Err(fail()); - } - let min = bound(length.get("min").ok_or_else(fail)?)?; - let max = bound(length.get("max").ok_or_else(fail)?)?; - if min > max || max > MAX_BOUND { return Err(fail()); } - Ok(Some(Length { min, max })) -} -/// The original profiles always pass false; no old profile is widened by this helper. -pub fn supported(t: &LogicalType, bounded: bool) -> bool { - !t.nullable && match t.family { - Family::String => t.facets == json!({}) || (bounded && bounds(&t.facets).is_ok()), - Family::Integer => t.facets == json!({"integerWidth":{"bits":64,"signed":true}}), - _ => false, - } -} -pub fn validate(t: &LogicalType, value: &str, bounded: bool) -> Result<()> { - if t.family != Family::String || !supported(t, bounded) { return Err(fail()); } - validate_facets(&t.facets, value) -} -/// Literal admission checks the actual non-null value even for a nullable Field. -pub fn validate_facets(facets: &Value, value: &str) -> Result<()> { - if let Some(length) = bounds(facets)? { - // Work depends on the actual framed value, never on a declared maximum. - let count = value.chars().count() as u64; - if count < length.min || count > length.max { return Err(fail()); } - } - Ok(()) -} -/// Semantically identical bounded domains can retain different exact JSON spellings. -/// Identity and comparison-scope checks remain separate mandatory obligations. -pub fn same_domain(left: &LogicalType, right: &LogicalType, bounded: bool) -> bool { - supported(left, bounded) && supported(right, bounded) - && (left == right || (bounded && left.family == Family::String - && right.family == Family::String && bounds(&left.facets).ok() == bounds(&right.facets).ok())) -} -/// A literal keeps its own original domain/value. The other operand's bounds -/// constrain that operand, not this value. Field/field pairing is a separate rule. -pub fn literal_compatible(original: &LogicalType, effective: &LogicalType, value: &str, bounded: bool) -> bool { - original.family == Family::String && effective.family == Family::String - && supported(effective, bounded) && validate(original, value, bounded).is_ok() - && (original == effective || bounded) -} - -#[cfg(test)] -mod tests { - use super::*; - fn t(min:u64,max:u64)->LogicalType { - LogicalType {family:Family::String,nullable:false,facets:json!({"length":{"min":min,"max":max,"unit":"unicode-scalar"}})} - } - #[test] - fn exact_domains_and_safe_bounds_refuse_other_meanings() { - assert_eq!(bounds(&json!({})).unwrap(),None); - assert_eq!(bounds(&t(0,MAX_BOUND).facets).unwrap().unwrap().max(),MAX_BOUND); - assert_eq!(bounds(&t(2,3).facets).unwrap().unwrap().min(),2); - for facets in [json!(null),json!([]),json!({"length":null}),json!({"other":{}}), - json!({"length":{"min":0,"max":1,"unit":"unicode-scalar"},"extra":true}), - json!({"length":{"min":0,"max":1,"unit":"unicode-scalar","extra":0}}), - json!({"length":{"max":1,"unit":"unicode-scalar"}}), - json!({"length":{"min":0,"unit":"unicode-scalar"}}), - json!({"length":{"min":0,"max":1}}), - json!({"length":{"min":-1,"max":1,"unit":"unicode-scalar"}}), - json!({"length":{"min":true,"max":1,"unit":"unicode-scalar"}}), - json!({"length":{"min":0.1,"max":1,"unit":"unicode-scalar"}}), - json!({"length":{"min":0,"max":1.5,"unit":"unicode-scalar"}}), - t(2,1).facets,t(0,MAX_BOUND+1).facets] { - assert!(bounds(&facets).is_err(),"{facets}"); - } - for unit in ["byte","utf16","unicode-code-unit","grapheme","Unicode-scalar",""] { - let mut f=t(0,3).facets;f["length"]["unit"]=json!(unit);assert!(bounds(&f).is_err()); - } - } - #[test] - fn mathematical_json_integer_bounds_keep_original_representation() { - for (text, expected) in [("0.0",0),("-0",0),("-0.0e999999999999999999999999",0),("1.0",1),("1e0",1),("1.5e1",15),("9007199254740991.0000000",MAX_BOUND),("9.007199254740991e15",MAX_BOUND)] { - let value:Value=serde_json::from_str(text).unwrap();let original=value.clone(); - assert_eq!(bound(&value).unwrap(),expected);assert_eq!(value,original); - } - for text in ["0.1","1e-1","-1.0","9007199254740992.0","9007199254740991.0000001","1e999999999999999999999999","1e-999999999999999999999999"] { - assert!(bound(&serde_json::from_str::(text).unwrap()).is_err(),"{text}"); - } - } - #[test] - fn independent_scalar_count_vectors_detect_bytes_units_and_normalization() { - // Expected lengths are authored, not obtained from the implementation. - for (value,count) in [("",0),("A",1),("😀",1),("é",1),("e\u{301}",2),("😀e\u{301}",3),("\r\n",2),("\0",1)] { - assert!(validate(&t(count,count),value,true).is_ok()); - if count>0 {assert!(validate(&t(0,count-1),value,true).is_err());} - assert!(validate(&t(count+1,MAX_BOUND),value,true).is_err()); - } - assert_eq!("😀".len(),4);assert_eq!("😀".encode_utf16().count(),2); - assert!(validate(&t(1,1),"😀",true).is_ok()); - assert!(validate(&t(1,1),"e\u{301}",true).is_err()); - assert_ne!("é","e\u{301}"); - assert!(serde_json::from_str::(r#""\ud800""#).is_err()); - } - #[test] - fn literal_provenance_and_distinct_effective_bounds_are_retained() { - let original=t(1,3);let effective=t(10,64);let before=original.facets.clone(); - assert!(literal_compatible(&original,&effective,"é",true)); - assert!(!literal_compatible(&original,&effective,"four",true)); - assert!(!literal_compatible(&original,&effective,"",true)); - assert_eq!(original.facets,before); - let equivalent=LogicalType{family:Family::String,nullable:false,facets:serde_json::from_str(r#"{"length":{"min":1.0,"max":3e0,"unit":"unicode-scalar"}}"#).unwrap()}; - assert!(same_domain(&original,&equivalent,true)); - assert!(!same_domain(&original,&effective,true)); - assert!(!same_domain(&original,&equivalent,false)); - let mut other=effective.clone();other.family=Family::Integer; - assert!(!literal_compatible(&original,&other,"1",true)); - other=effective.clone();other.nullable=true; - assert!(!literal_compatible(&original,&other,"é",true)); - } - #[test] - fn old_profiles_and_nullable_domains_remain_closed() { - assert!(!supported(&t(0,3),false));assert!(validate(&t(0,3),"A",false).is_err()); - assert!(!literal_compatible(&t(0,3),&t(0,3),"A",false)); - let mut nullable=t(0,3);nullable.nullable=true;assert!(!supported(&nullable,true)); - let unbounded=LogicalType{family:Family::String,nullable:false,facets:json!({})}; - assert!(supported(&unbounded,false));assert!(validate(&unbounded,"😀",false).is_ok()); - assert!(literal_compatible(&unbounded,&unbounded,"😀",false)); - } -} +//! Re-export of `weft_model::string_domain`. +pub use weft_model::string_domain::*; diff --git a/crates/weft-core/src/error.rs b/crates/weft-core/src/error.rs index 06eaead3..6187b035 100644 --- a/crates/weft-core/src/error.rs +++ b/crates/weft-core/src/error.rs @@ -1,55 +1,3 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 -use crate::ir::Span; -use serde::Serialize; -#[derive(Debug, Clone, Copy)] -pub(crate) enum ResolutionReason { - ColumnMissing, - ColumnAmbiguous, - SourceUnavailable, -} -impl ResolutionReason { - pub(crate) fn code(self) -> &'static str { - match self { - Self::ColumnMissing => "WFT-COLUMN-MISSING", - Self::ColumnAmbiguous => "WFT-COLUMN-AMBIGUOUS", - Self::SourceUnavailable => "WFT-SOURCE-UNAVAILABLE", - } - } -} -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct Diagnostic { - pub code: String, - pub severity: String, - pub message: String, - pub phase: String, - #[serde(skip_serializing_if = "Option::is_none")] - pub source_span: Option, - #[serde(skip)] - pub(crate) resolution_reason: Option, -} -impl Diagnostic { - pub fn new(code: &str, phase: &str, message: &str) -> Self { - Self { - code: code.into(), - severity: "error".into(), - message: message.into(), - phase: phase.into(), - source_span: None, - resolution_reason: None, - } - } - pub fn at(mut self, span: &Span) -> Self { - self.source_span = Some(span.clone()); - self - } - pub(crate) fn column(mut self, ambiguous: bool) -> Self { - self.resolution_reason = Some(if ambiguous { ResolutionReason::ColumnAmbiguous } else { ResolutionReason::ColumnMissing }); - self - } - pub(crate) fn unavailable_source(mut self) -> Self { - self.resolution_reason = Some(ResolutionReason::SourceUnavailable); - self - } -} -pub type Result = std::result::Result; +//! Re-export of `weft_model::diagnostic`. +pub use weft_model::diagnostic::*; diff --git a/crates/weft-core/src/exact.rs b/crates/weft-core/src/exact.rs index 26e516f0..30c89d24 100644 --- a/crates/weft-core/src/exact.rs +++ b/crates/weft-core/src/exact.rs @@ -1,106 +1,18 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 +//! Exact literal checks live in `weft_model::exact`; this module builds the +//! weft-sql 0.1 IR literal from a checked token. use crate::{ - error::{Diagnostic, Result}, - ir::{Expression, Family, LogicalType}, - syntax::{Literal, LiteralKind}, + error::Result, + ir::{Expression, LogicalType}, + syntax::Literal, }; +pub use weft_model::exact::validate_binary_token; +pub(crate) use weft_model::exact::{validate_date_token, validate_mathematical_decimal}; pub fn literal(input: &Literal, expected: &LogicalType) -> Result { - let error = |code, message| Diagnostic::new(code, "type", message).at(&input.span); - match (&input.kind, &expected.family) { - (LiteralKind::String, Family::String) => { - crate::current_string_domain::validate_facets(&expected.facets, &input.value) - .map_err(|_| error("WFT-STRING-DOMAIN", "String literal exceeds selected domain"))?; - } - (LiteralKind::Boolean, Family::Boolean) => {} - (LiteralKind::Number, Family::Integer) => { - if input.value.contains('.') { - return Err(error( - "WFT-NUMERIC-DOMAIN", - "Integer literal must be integral base-ten text", - )); - } - let digits = input.value.strip_prefix('-').unwrap_or(&input.value); - if digits.is_empty() || !digits.bytes().all(|b| b.is_ascii_digit()) { return Err(error("WFT-NUMERIC-DOMAIN", "Integer literal must retain exact base-ten digits")); } - if expected.facets != serde_json::json!({}) { - let n = input.value.parse::().map_err(|_| { - error( - "WFT-NUMERIC-DOMAIN", - "Integer literal exceeds selected domain", - ) - })?; - let width = &expected.facets["integerWidth"]; - let bits = width["bits"].as_u64().unwrap(); - let (min, max) = if width["signed"] == true { - (-(1i128 << (bits - 1)), (1i128 << (bits - 1)) - 1) - } else { - (0, (1i128 << bits) - 1) - }; - if n < min || n > max { - return Err(error( - "WFT-NUMERIC-DOMAIN", - "Integer literal exceeds selected domain", - )); - } - } - } - (LiteralKind::Number, Family::Decimal) => { - if expected.facets == serde_json::json!({}) { - validate_mathematical_decimal(&input.value).map_err(|_|error("WFT-NUMERIC-DOMAIN","Decimal literal must be exact finite base-ten text"))?; - return Ok(Expression::Literal{value:input.value.clone(),logical_type:expected.clone(),span:input.span.clone()}); - } - let scale = expected.facets["scale"].as_u64().unwrap() as usize; - let precision = expected.facets["precision"].as_u64().unwrap() as usize; - let unsigned = input.value.strip_prefix('-').unwrap_or(&input.value); - let mut parts = unsigned.split('.'); - let whole = parts.next().unwrap(); - let frac = parts.next().unwrap_or("").trim_end_matches('0'); - if frac.len() > scale { - return Err(error( - "WFT-NUMERIC-DOMAIN", - "Decimal scale exceeds selected domain", - )); - } - let coefficient = format!("{whole}{frac}{}", "0".repeat(scale - frac.len())); - if coefficient.trim_start_matches('0').len() > precision { - return Err(error( - "WFT-NUMERIC-DOMAIN", - "Decimal precision exceeds selected domain", - )); - } - } - _ => return Err(error("WFT-TYPE", "Literal family does not match the field")), - } + weft_model::exact::check_literal(input, expected)?; Ok(Expression::Literal { value: input.value.clone(), logical_type: expected.clone(), span: input.span.clone(), }) } - -/// Fixed-point finite token grammar; no conversion through native floats. -pub(crate) fn validate_mathematical_decimal(value:&str)->Result<()> { - let unsigned=value.strip_prefix('-').unwrap_or(value);let mut parts=unsigned.split('.'); - let whole=parts.next().unwrap_or("");let fraction=parts.next(); - if value.len()>1048576 || whole.is_empty() || !whole.bytes().all(|b|b.is_ascii_digit()) - || fraction.is_some_and(|f|f.is_empty()||!f.bytes().all(|b|b.is_ascii_digit())) || parts.next().is_some(){ - return Err(Diagnostic::new("WFT-NUMERIC-DOMAIN","type","Finite fixed-point decimal token required")); - }Ok(()) -} - -/// Byte values use bounded canonical standard base64; original tokens are retained. -pub fn validate_binary_token(value:&str)->Result<()> { - use base64::Engine; - if value.len()>1048576 {return Err(Diagnostic::new("WFT-LIMIT","type","Binary token exceeds one MiB"));} - let bytes=base64::engine::general_purpose::STANDARD.decode(value).map_err(|_|Diagnostic::new("WFT-BINARY-DOMAIN","type","Canonical base64 required"))?; - if base64::engine::general_purpose::STANDARD.encode(bytes)!=value {return Err(Diagnostic::new("WFT-BINARY-DOMAIN","type","Canonical base64 required"));} - Ok(()) -} - -/// Explicit Truss canonical date subset: Gregorian YYYY-MM-DD, years 0001..9999. -/// UMF core declares the family; temporal refinements are not inferred here. -pub(crate) fn validate_date_token(value:&str)->Result<()> { - let fail=||Diagnostic::new("WFT-DATE-DOMAIN","type","Canonical Gregorian YYYY-MM-DD required"); - if value.len()!=10 || !value.is_ascii() {return Err(fail());} - crate::exact_instant::key(&format!("{value}T00:00:00Z")).map_err(|_|fail())?; - Ok(()) -} diff --git a/crates/weft-core/src/exact_instant.rs b/crates/weft-core/src/exact_instant.rs index f2de9a62..82cf8a5c 100644 --- a/crates/weft-core/src/exact_instant.rs +++ b/crates/weft-core/src/exact_instant.rs @@ -1,56 +1,3 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 -//! Consumer binding of UMF CONTRACT-065; generic timestamp and Key stay unchanged. -use crate::error::{Diagnostic, Result}; -pub(crate) const PROFILE: &str = "umf-rfc3339-exact-instant/1.0.0"; -pub(crate) fn key(token: &str) -> Result<(i64, &str)> { - let fail = || Diagnostic::new("WFT-TIMESTAMP-DOMAIN", "type", "Selected RFC3339 exact instant token required"); - let bytes = token.as_bytes(); - if bytes.len() > 1048576 { return Err(Diagnostic::new("WFT-LIMIT", "type", "Timestamp token exceeds one MiB")); } - if bytes.len() < 20 || !bytes.is_ascii() || bytes[4] != b'-' || bytes[7] != b'-' - || !matches!(bytes[10], b'T' | b't') || bytes[13] != b':' || bytes[16] != b':' { return Err(fail()); } - let number = |start: usize, end: usize| -> Result { - let value = &bytes[start..end]; - if !value.iter().all(u8::is_ascii_digit) { return Err(fail()); } - Ok(value.iter().fold(0, |sum, digit| sum * 10 + i64::from(digit - b'0'))) - }; - let year = number(0,4)?; let month = number(5,7)?; let day = number(8,10)?; - let hour = number(11,13)?; let minute = number(14,16)?; let second = number(17,19)?; - let leap = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); - let lengths = [31, if leap {29} else {28}, 31,30,31,30,31,31,30,31,30,31]; - if year == 0 || !(1..=12).contains(&month) || day < 1 || day > lengths[(month-1) as usize] - || hour > 23 || minute > 59 || second > 59 { return Err(fail()); } - let mut position = 19; - let mut fraction = ""; - if bytes[position] == b'.' { - position += 1; let start = position; - while position < bytes.len() && bytes[position].is_ascii_digit() { position += 1; } - if position == start { return Err(fail()); } - fraction = token[start..position].trim_end_matches('0'); - } - if position >= bytes.len() { return Err(fail()); } - let offset = if matches!(bytes[position], b'Z' | b'z') { - if position + 1 != bytes.len() { return Err(fail()); } 0 - } else { - if position + 6 != bytes.len() || !matches!(bytes[position], b'+' | b'-') || bytes[position+3] != b':' { return Err(fail()); } - let hours = number(position+1,position+3)?; let minutes = number(position+4,position+6)?; - if hours > 23 || minutes > 59 { return Err(fail()); } - (hours * 60 + minutes) * if bytes[position] == b'-' {-1} else {1} - }; - let before = |y: i64| { let n = y - 1; n * 365 + n / 4 - n / 100 + n / 400 }; - let days = before(year) - before(1970) + lengths[..(month-1) as usize].iter().sum::() + day - 1; - Ok((days * 86400 + hour * 3600 + minute * 60 + second - offset * 60, fraction)) -} - -#[cfg(test)] -mod tests { - use super::key; - #[test] fn exact_instants() { - assert_eq!(key("2024-01-01T00:00:00.123456789Z").unwrap(), key("2023-12-31T19:00:00.1234567890-05:00").unwrap()); - assert_ne!(key("2024-01-01T00:00:00.1234567Z").unwrap(), key("2024-01-01T00:00:00.1234568Z").unwrap()); - assert_eq!(key("1969-12-31t23:59:59.5000z").unwrap(), (-1,"5")); - assert_eq!(key("1970-01-01T00:00:00-00:00").unwrap(), (0,"")); - assert_eq!(key("0001-01-01T00:00:00+23:59").unwrap().0, -62135683140); - assert_eq!(key("9999-12-31T23:59:59-23:59").unwrap().0, 253402387139); - for token in ["1900-02-29T00:00:00Z", "0000-01-01T00:00:00Z", "2016-12-31T23:59:60Z", "2024-01-01T00:00:00+24:00", "2024-01-01T00:00:00Z\n"] { assert!(key(token).is_err()); } - } -} +//! Re-export of `weft_model::exact_instant`. +pub(crate) use weft_model::exact_instant::{key, PROFILE}; diff --git a/crates/weft-core/src/ir.rs b/crates/weft-core/src/ir.rs index 9d668f3b..b2e6c434 100644 --- a/crates/weft-core/src/ir.rs +++ b/crates/weft-core/src/ir.rs @@ -1,45 +1,7 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 use serde::{Deserialize, Serialize}; -use serde_json::Value; -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct Span { - pub start: usize, - pub end: usize, -} -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct Identity { - pub document_id: String, - pub revision: String, - pub module: String, - pub element: String, -} -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "camelCase", deny_unknown_fields)] -pub struct ModelPin { - pub document_id: String, - pub revision: String, - pub umf_version: String, - pub sha256: String, -} -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub enum Family { - #[serde(rename = "boolean")] - Boolean, - #[serde(rename = "string")] - String, - #[serde(rename = "integer")] - Integer, - #[serde(rename = "decimal")] - Decimal, -} -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct LogicalType { - pub family: Family, - pub facets: Value, - pub nullable: bool, -} +pub use weft_model::types::{Family, Identity, LogicalType, ModelPin, Span}; #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(tag = "op", rename_all = "camelCase")] pub enum Expression { diff --git a/crates/weft-core/src/json.rs b/crates/weft-core/src/json.rs index 270d8298..def222ad 100644 --- a/crates/weft-core/src/json.rs +++ b/crates/weft-core/src/json.rs @@ -1,239 +1,3 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 -use serde::de::{MapAccess, Visitor}; -use serde::{Deserialize, Deserializer}; -use serde_json::{Value, value::RawValue}; -use sha2::{Digest, Sha256}; -use std::{collections::BTreeSet, fmt}; -type RawResult = std::result::Result; -pub fn sha256(bytes: &[u8]) -> String { - format!("{:x}", Sha256::digest(bytes)) -} -struct RawObject(Vec<(String, Box)>); -impl<'de> Deserialize<'de> for RawObject { - fn deserialize>(d: D) -> std::result::Result { - struct ObjectVisitor; - impl<'de> Visitor<'de> for ObjectVisitor { - type Value = RawObject; - fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.write_str("an object") - } - fn visit_map>( - self, - mut map: M, - ) -> std::result::Result { - let mut entries = Vec::new(); - while let Some(entry) = map.next_entry::>()? { - entries.push(entry); - } - Ok(RawObject(entries)) - } - } - d.deserialize_map(ObjectVisitor) - } -} -// Traverse raw nested values so even arbitrary-size unknown numbers are never f64. -fn inspect_json( - raw: &str, - depth: usize, - count: &mut usize, - max_depth: usize, - max_nodes: usize, -) -> RawResult<()> { - if depth > max_depth || *count >= max_nodes { - return Err("WFT-LIMIT"); - } - *count += 1; - match raw.trim_start().as_bytes().first() { - Some(b'{') => { - let object: RawObject = serde_json::from_str(raw).map_err(|_| "WFT-INPUT")?; - let mut keys = BTreeSet::new(); - for (key, value) in object.0 { - if !keys.insert(key) { - return Err("WFT-JSON-DUPLICATE"); - } - inspect_json(value.get(), depth + 1, count, max_depth, max_nodes)?; - } - } - Some(b'[') => { - let values: Vec> = serde_json::from_str(raw).map_err(|_| "WFT-INPUT")?; - for value in values { - inspect_json(value.get(), depth + 1, count, max_depth, max_nodes)?; - } - } - _ => { - let _: Value = serde_json::from_str(raw).map_err(|_| "WFT-INPUT")?; - } - } - Ok(()) -} -// Value's arbitrary-precision/raw-value visitor reserves ordinary object keys. -// Construct objects ourselves so unknown JSON members remain ordinary data. -fn faithful_tree(raw: &str) -> RawResult { - match raw.trim_start().as_bytes().first() { - Some(b'{') => { - let object: RawObject = serde_json::from_str(raw).map_err(|_| "WFT-INPUT")?; - let mut out = serde_json::Map::new(); - for (key, value) in object.0 { - out.insert(key, faithful_tree(value.get())?); - } - Ok(Value::Object(out)) - } - Some(b'[') => { - let values: Vec> = serde_json::from_str(raw).map_err(|_| "WFT-INPUT")?; - values - .iter() - .map(|v| faithful_tree(v.get())) - .collect::>>() - .map(Value::Array) - } - _ => serde_json::from_str(raw).map_err(|_| "WFT-INPUT"), - } -} -pub fn checked_json(raw: &str) -> RawResult { - inspect_json(raw, 0, &mut 0, 128, 100_000)?; - faithful_tree(raw) -} - -/// The selected Truss runtime catalog has a four-MiB byte envelope. A thousand -/// records with twelve fields exceeds the general 100,000-value limit even -/// though both original model and binding fit that existing byte envelope. -/// Keep generic transport admission unchanged; this reader grants no authority. -pub fn checked_truss_runtime_catalog_json(raw: &str) -> RawResult { - if raw.len() > 4 * 1024 * 1024 { - return Err("WFT-LIMIT"); - } - inspect_json(raw, 0, &mut 0, 128, 1_000_000)?; - faithful_tree(raw) -} - -#[cfg(test)] -mod truss_runtime_catalog_tests { - use super::*; - - #[test] - fn larger_catalog_budget_does_not_change_generic_admission() { - let raw = format!("[{}]", vec!["0"; 100_000].join(",")); - assert_eq!(checked_json(&raw), Err("WFT-LIMIT")); - assert_eq!(checked_truss_runtime_catalog_json(&raw).unwrap().as_array().unwrap().len(), 100_000); - } - - #[test] - fn runtime_catalog_keeps_byte_depth_value_and_duplicate_limits() { - let at_limit = format!("[{}]", vec!["0"; 999_999].join(",")); - assert!(checked_truss_runtime_catalog_json(&at_limit).is_ok()); - let over_limit = format!("[{}]", vec!["0"; 1_000_000].join(",")); - assert_eq!(checked_truss_runtime_catalog_json(&over_limit), Err("WFT-LIMIT")); - assert_eq!(checked_truss_runtime_catalog_json(&" ".repeat(4 * 1024 * 1024 + 1)), Err("WFT-LIMIT")); - assert_eq!(checked_truss_runtime_catalog_json(&format!("{}0{}", "[".repeat(129), "]".repeat(129))), Err("WFT-LIMIT")); - assert_eq!(checked_truss_runtime_catalog_json(r#"{"a":1,"\u0061":2}"#), Err("WFT-JSON-DUPLICATE")); - } -} - -/// Bounded transport reader; root depth is one and each value counts once. -/// Parsing is custody only and grants no result release authority. -#[allow(dead_code)] // Pending runtime-cell consumer. -pub(crate) fn checked_json_bounded( - raw: &str, - max_bytes: usize, - max_depth: usize, - max_nodes: usize, -) -> RawResult { - if raw.len() > max_bytes { - return Err("WFT-LIMIT"); - } - inspect_json(raw, 1, &mut 0, max_depth, max_nodes)?; - faithful_tree(raw) -} -/// Same bounded reader with parse visits consumed from the caller's work ledger. -/// Raw inspection temporaries are separately bounded by input bytes and depth; -/// this is not a process-memory bound. -pub(crate) fn checked_json_bounded_charged( - raw: &str, - max_bytes: usize, - max_depth: usize, - remaining_work: &mut usize, -) -> RawResult { - if raw.len() > max_bytes { - return Err("WFT-LIMIT"); - } - let mut count = 0; - inspect_json(raw, 1, &mut count, max_depth, *remaining_work)?; - *remaining_work = remaining_work.checked_sub(count).ok_or("WFT-LIMIT")?; - faithful_tree(raw) -} -#[cfg(test)] -mod bounded_transport_tests { - use super::checked_json_bounded_charged; - #[test] - fn charged_reader_uses_the_callers_actual_remaining_work() { - let raw = "{\"a\":[1,2]}"; - let mut work = 4; - assert!(checked_json_bounded_charged(raw, raw.len(), 3, &mut work).is_ok()); - assert_eq!(work, 0); - let mut work = 3; - assert!(checked_json_bounded_charged(raw, raw.len(), 3, &mut work).is_err()); - assert_eq!(work, 3); - } - use super::*; - #[test] - fn ordinary_reserved_looking_objects_survive_every_reader() { - for key in [ - "$serde_json::private::Number", - "$serde_json::private::RawValue", - ] { - let raw = format!("{{\"{key}\":\"not JSON\",\"sibling\":[1.00e0,true]}}"); - let mut map = serde_json::Map::new(); - map.insert(key.into(), Value::String("not JSON".into())); - map.insert( - "sibling".into(), - Value::Array(vec![ - serde_json::from_str("1.00e0").unwrap(), - Value::Bool(true), - ]), - ); - let expected = Value::Object(map); - assert_eq!(checked_json(&raw).unwrap(), expected); - assert_eq!( - checked_json_bounded(&raw, raw.len(), 3, 5).unwrap(), - expected - ); - let mut work = 5; - assert_eq!( - checked_json_bounded_charged(&raw, raw.len(), 3, &mut work).unwrap(), - expected - ); - assert_eq!(work, 0); - } - } - #[test] - fn byte_limits_count_utf8_and_whitespace() { - let raw = " \"é\" "; - assert!(checked_json_bounded(raw, raw.len(), 1, 1).is_ok()); - assert_eq!( - checked_json_bounded(raw, raw.len() - 1, 1, 1), - Err("WFT-LIMIT") - ); - } - #[test] - fn depth_counts_root_and_nodes_count_values_not_names() { - let raw = r#"{"rows":[[null,true]]}"#; - assert!(checked_json_bounded(raw, raw.len(), 4, 5).is_ok()); - assert_eq!(checked_json_bounded(raw, raw.len(), 3, 5), Err("WFT-LIMIT")); - assert_eq!(checked_json_bounded(raw, raw.len(), 4, 4), Err("WFT-LIMIT")); - assert_eq!(checked_json_bounded("null", 4, 0, 1), Err("WFT-LIMIT")); - assert_eq!(checked_json_bounded("null", 4, 1, 0), Err("WFT-LIMIT")); - } - #[test] - fn duplicate_members_and_malformed_json_refuse() { - for raw in [r#"{"x":1,"x":2}"#, r#"[{"x":1,"\u0078":2}]"#] { - assert_eq!( - checked_json_bounded(raw, raw.len(), 64, 1_000_000), - Err("WFT-JSON-DUPLICATE") - ); - } - assert_eq!( - checked_json_bounded("[null,]", 100, 64, 100), - Err("WFT-INPUT") - ); - } -} +//! Re-export of `weft_model::json`. +pub use weft_model::json::*; diff --git a/crates/weft-core/src/model.rs b/crates/weft-core/src/model.rs index e86fd541..6af5e9ec 100644 --- a/crates/weft-core/src/model.rs +++ b/crates/weft-core/src/model.rs @@ -1,840 +1,3 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 -use crate::syntax::Name; -use crate::{ - error::{Diagnostic, Result}, - ir::{Family, Identity, LogicalType, ModelPin}, - json::{checked_json, sha256}, -}; -use serde::{Deserialize, Serialize}; -use serde_json::{json, Value}; -use std::collections::{BTreeSet, HashMap}; - -type ModuleElementIndex = HashMap)>; -type RecordLocation = (usize, usize); -#[derive(Debug, Default, Clone)] -struct RecordNameIndex { - quoted_names: HashMap>, - unquoted_names: HashMap>, - quoted_unnamed_ids: HashMap>, - unquoted_unnamed_ids: HashMap>, - scoped_quoted_names: HashMap<(String, String), Vec>, - scoped_unquoted_names: HashMap<(String, String), Vec>, - scoped_quoted_unnamed_ids: HashMap<(String, String), Vec>, - scoped_unquoted_unnamed_ids: HashMap<(String, String), Vec>, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "camelCase", deny_unknown_fields)] -pub struct ModuleInput { - pub document_json: String, - pub pin: ModelPin, - pub selected_module_ids: Vec, -} -#[derive(Debug, Clone)] -/// A prepared snapshot retains exact source and parsed meaning together. -/// Direct mutation is inaccessible: -/// ```compile_fail -/// fn tamper(catalog: &mut weft_core::model::Catalog) { -/// catalog.inputs[0].document_json.clear(); -/// } -/// ``` -/// The public accessor also cannot mutate retained source: -/// ```compile_fail -/// fn tamper(catalog: &mut weft_core::model::Catalog) { -/// catalog.inputs()[0].document_json.clear(); -/// } -/// ``` - -pub struct Catalog { - inputs: Vec, - pub(crate) exact_numbers: bool, - pub(crate) field_projection_decimal38: bool, - pub(crate) sdk_scalar_presence: bool, - security_inputs: bool, - pub(crate) identity_name_fallback: bool, - raw_read_opaque_extensions: bool, - native_operation_domains: bool, - logical_defaults020: bool, - pub(crate) exact_scalars: bool, - pub(crate) exact_dates: bool, - pub(crate) bounded_current_strings: bool, - pub(crate) documents: Vec, - element_locations: Vec, - record_names: Vec, -} -#[derive(Debug, Clone)] -pub struct Record { - pub identity: Identity, - pub pin: ModelPin, - pub value: Value, - pub document: usize, -} -fn fail(code: &str, message: &str) -> Diagnostic { - Diagnostic::new(code, "model", message) -} -/// UMF 0.10 selected semantics are admitted only where this compiler can -/// preserve their value domain. External semantic type references are retained -/// in source but remain outside the raw scalar profile. -pub(crate) fn selected010(pin: &ModelPin, element: &Value) -> Result<()> { - selected010_inner(pin, element, false, false, false, None) -} -pub(crate) fn selected010_raw_read(pin: &ModelPin, element: &Value, document: &Value) -> Result<()> { - selected010_inner(pin, element, true, false, false, Some(document)) -} -pub(crate) fn selected010_table_operation(pin: &ModelPin, element: &Value, document: &Value) -> Result<()> { - selected010_inner(pin, element, true, true, false, Some(document)) -} -pub(crate) fn selected010_table_operation020(pin: &ModelPin, element: &Value, document: &Value) -> Result<()> { - selected010_inner(pin, element, true, true, true, Some(document)) -} -fn selected010_inner(pin: &ModelPin, element: &Value, allow_raw_read_metadata: bool, native_domains: bool, logical_defaults020: bool, document: Option<&Value>) -> Result<()> { - if pin.umf_version != "0.10.0" { return Ok(()); } - const KNOWN: &[&str] = &["id", "name", "title", "description", "kind", "scalarType", - "nullability", "cardinality", "itemType", "recordType", "facets", "members", - "keys", "references", "extensions", "semanticTypes"]; - let extensions_supported = element.get("extensions").is_none_or(|value| { - value.as_object().is_some_and(|map| { - if !allow_raw_read_metadata { return map.is_empty(); } - map.iter().all(|(name, payload)| { - payload.is_object() && match name.as_str() { - "umf.ddd" => element["kind"] == "record", - "hohfeld.view" => matches!(element["kind"].as_str(), Some("record" | "field")), - "arras.view" => matches!(element["kind"].as_str(), Some("record" | "field")) - && document.is_some_and(|doc| doc["vocabularies"]["arras.view"]["version"] == "0.1.0"), - _ => false, - } - }) - }) - }); - let default020_is_closed = element.get("default").is_none_or(|default| { - logical_defaults020 - && element["kind"] == "field" - && default.as_object().is_some_and(|members| { - members.len() == 2 - && members.contains_key("on") - && members.contains_key("value") - && default["on"] == "missing" - && !default["value"].is_null() - }) - }); - if element.as_object().is_none_or(|m|m.keys().any(|k|!KNOWN.contains(&k.as_str())&&!(native_domains&&element["kind"]=="field"&&k=="allowedValues")&&!(logical_defaults020&&element["kind"]=="field"&&k=="default"&&default020_is_closed))) - || element.get("semanticTypes").is_some() - || !extensions_supported - { return Err(fail("WFT-TYPE", "Selected UMF 0.10 element has semantics outside the raw scalar profile")); } - if element["kind"] == "record" && element.get("references").and_then(Value::as_array).is_some_and(|r|!r.is_empty()) { - return Err(fail("WFT-TYPE", "Selected UMF 0.10 Record reference roles are unsupported")); - } - for member in element.get("members").and_then(Value::as_array).into_iter().flatten() { - if member.as_object().is_none_or(|m|m.keys().any(|k|!matches!(k.as_str(),"module"|"element"))) { - return Err(fail("WFT-TYPE", "Selected UMF 0.10 reference has unsupported qualifiers")); - } - } - Ok(()) -} -#[jsonschema::validator(path = "../../spec/upstream/umf-0.10.0-bundled.schema.json")] -struct Envelope10; -impl Catalog { - /// Immutable evidence of the trusted SDK scalar presence preparation path. - pub fn sdk_scalar_presence(&self)->bool {self.sdk_scalar_presence} - /// New host-selected profile only; authored names always take precedence. - pub(crate) fn query_name<'a>(&self, element: &'a Value) -> Option<&'a str> { - element.get("name").and_then(Value::as_str).or_else(|| { - self.identity_name_fallback.then(|| element["id"].as_str()).flatten() - }) - } - - pub(crate) fn validate_selected010(&self, pin: &ModelPin, element: &Value) -> Result<()> { - if self.native_operation_domains || self.raw_read_opaque_extensions { - let document = self.inputs.iter().zip(&self.documents) - .find(|(input, _)| input.pin == *pin) - .map(|(_, document)| document) - .ok_or_else(|| fail("WFT-PIN", "Selected element has no exact owning document"))?; - if self.native_operation_domains && self.logical_defaults020 { - selected010_table_operation020(pin, element, document) - } else if self.native_operation_domains { - selected010_table_operation(pin, element, document) - } - else { selected010_raw_read(pin, element, document) } - } - else { selected010(pin, element) } - } - - /// Read-only original inputs. Clone and prepare a new Catalog to change them. - pub fn inputs(&self) -> &[ModuleInput] { - &self.inputs - } - /// Explicit host-selected admission of facetless mathematical numeric fields. - /// Source bytes/pins remain original; default preparation is unchanged. - pub fn prepare_exact_numbers(inputs: Vec) -> Result { - let mut catalog=Self::prepare(inputs)?;catalog.exact_numbers=true;Ok(catalog) - } - pub fn prepare(inputs: Vec) -> Result { - Self::prepare_version(inputs, None, false) - } - pub(crate) fn prepare_truss_runtime(inputs: Vec, exact_numbers: bool) -> Result { - let mut catalog = Self::prepare_version(inputs, None, true)?; - catalog.exact_numbers = exact_numbers; - catalog.raw_read_opaque_extensions = true; - Ok(catalog) - } - /// Source custody only; security activation remains independently gated. - pub fn prepare_security(inputs: Vec) -> Result { - Self::prepare_version(inputs, Some("0.10.0"), false) - } - pub(crate) fn prepare_raw_read010(inputs: Vec) -> Result { - let mut catalog = Self::prepare_version(inputs, Some("0.10.0"), false)?; - catalog.identity_name_fallback = true; - catalog.raw_read_opaque_extensions = true; - Ok(catalog) - } - /// Finite Unity aggregate admission retains the raw-read checks while - /// permitting source facets that its bounded aggregate owners validate. - pub(crate) fn prepare_raw_read010_finite_current_aggregate(inputs: Vec) -> Result { - let mut catalog = Self::prepare_raw_read010(inputs)?; - catalog.exact_numbers = true; - catalog.field_projection_decimal38 = true; - Ok(catalog) - } - pub(crate) fn prepare_table_operation010(inputs: Vec) -> Result { - let mut catalog=Self::prepare_raw_read010(inputs)?; - catalog.native_operation_domains=true; - Ok(catalog) - } - pub(crate) fn prepare_table_operation020(inputs: Vec) -> Result { - let mut catalog=Self::prepare_table_operation010(inputs)?; - catalog.logical_defaults020=true; - Ok(catalog) - } - pub(crate) fn prepare_security_bounded_strings(inputs: Vec) -> Result { - let mut catalog = Self::prepare_security(inputs)?; - catalog.bounded_current_strings = true; - Ok(catalog) - } - fn prepare_version(inputs: Vec, required_version: Option<&str>, truss_runtime: bool) -> Result { - if inputs.is_empty() || inputs.len() > 32 { - return Err(fail( - "WFT-LIMIT", - "Module bundle size is outside the supported bound", - )); - } - let mut documents = Vec::new(); - let mut ids = BTreeSet::new(); - let mut selections = 0; - for input in &inputs { - if input.document_json.len() > 4 * 1024 * 1024 { - return Err(fail("WFT-LIMIT", "Owning document exceeds byte limit")); - } - if input.pin.sha256 != sha256(input.document_json.as_bytes()) { - return Err(fail("WFT-PIN", "Owning document digest mismatch")); - } - let doc = (if truss_runtime { - crate::json::checked_truss_runtime_catalog_json(&input.document_json) - } else { - checked_json(&input.document_json) - }) - .map_err(|c| fail(c, "Owning document JSON refused"))?; - if doc["id"] != input.pin.document_id { - return Err(fail("WFT-PIN", "Owning document identity mismatch")); - } - if input.pin.umf_version != "0.10.0" - || doc["umf"] != "0.10.0" - || required_version.is_some_and(|version| input.pin.umf_version != version) - { - return Err(fail("WFT-MODEL-VERSION", "Unsupported UMF profile")); - } - if input.pin.revision.is_empty() || !ids.insert(input.pin.document_id.clone()) { - return Err(fail( - "WFT-MODEL", - "Empty revision or repeated owning document", - )); - } - if !Envelope10::is_valid(&doc) { - return Err(fail( - "WFT-MODEL", - "Document violates the pinned UMF envelope", - )); - } - let modules = doc["modules"].as_array().expect("schema checked modules"); - let mut mids = BTreeSet::new(); - for module in modules { - if !mids.insert(module["id"].as_str().unwrap()) { - return Err(fail("WFT-MODEL", "Duplicate module identity")); - } - let mut eids = BTreeSet::new(); - for element in module["elements"].as_array().unwrap() { - if !eids.insert(element["id"].as_str().unwrap()) { - return Err(fail("WFT-MODEL", "Duplicate element identity")); - } - } - } - let mut selected = BTreeSet::new(); - if input.selected_module_ids.is_empty() { - return Err(fail("WFT-MODEL", "No module selected")); - } - for id in &input.selected_module_ids { - if !selected.insert(id) || !mids.contains(id.as_str()) { - return Err(fail("WFT-MODEL", "Missing or repeated selected module")); - } - } - selections += selected.len(); - if selections > 256 { - return Err(fail("WFT-LIMIT", "Selected module count exceeds bound")); - } - documents.push(doc); - } - // Index coordinates only. Original immutable documents and their order - // remain authoritative; no Field meaning or authorization is cached. - let element_locations = documents.iter().map(|document| { - document["modules"].as_array().unwrap().iter().enumerate().map(|(mi, module)| { - let elements = module["elements"].as_array().unwrap().iter().enumerate() - .map(|(ei, element)| (element["id"].as_str().unwrap().to_owned(), ei)).collect(); - (module["id"].as_str().unwrap().to_owned(), (mi, elements)) - }).collect() - }).collect(); - let record_names = documents.iter().map(|document| { - let mut index = RecordNameIndex::default(); - for (module_index, module) in document["modules"].as_array().unwrap().iter().enumerate() { - for (element_index, element) in module["elements"].as_array().unwrap().iter().enumerate() { - if element["kind"] != "record" { continue; } - let namespace = module["namespace"].as_str(); - if let Some(name) = element["name"].as_str() { - index.quoted_names.entry(name.to_owned()).or_default().push((module_index, element_index)); - index.unquoted_names.entry(name.to_ascii_lowercase()).or_default().push((module_index, element_index)); - if let Some(namespace) = namespace { - let folded_namespace = namespace.to_ascii_lowercase(); - index.scoped_quoted_names.entry((namespace.to_owned(), name.to_owned())).or_default().push((module_index, element_index)); - index.scoped_unquoted_names.entry((namespace.to_owned(), name.to_ascii_lowercase())).or_default().push((module_index, element_index)); - if folded_namespace != namespace { - index.scoped_quoted_names.entry((folded_namespace.clone(), name.to_owned())).or_default().push((module_index, element_index)); - index.scoped_unquoted_names.entry((folded_namespace, name.to_ascii_lowercase())).or_default().push((module_index, element_index)); - } - } - } else if let Some(id) = element["id"].as_str() { - index.quoted_unnamed_ids.entry(id.to_owned()).or_default().push((module_index, element_index)); - index.unquoted_unnamed_ids.entry(id.to_ascii_lowercase()).or_default().push((module_index, element_index)); - if let Some(namespace) = namespace { - let folded_namespace = namespace.to_ascii_lowercase(); - index.scoped_quoted_unnamed_ids.entry((namespace.to_owned(), id.to_owned())).or_default().push((module_index, element_index)); - index.scoped_unquoted_unnamed_ids.entry((namespace.to_owned(), id.to_ascii_lowercase())).or_default().push((module_index, element_index)); - if folded_namespace != namespace { - index.scoped_quoted_unnamed_ids.entry((folded_namespace.clone(), id.to_owned())).or_default().push((module_index, element_index)); - index.scoped_unquoted_unnamed_ids.entry((folded_namespace, id.to_ascii_lowercase())).or_default().push((module_index, element_index)); - } - } - } - } - } - index - }).collect(); - Ok(Self { inputs, documents, element_locations, record_names, exact_numbers:false, field_projection_decimal38:false, sdk_scalar_presence:false, exact_scalars:false, exact_dates:false, bounded_current_strings:false, security_inputs:required_version == Some("0.10.0"), identity_name_fallback:false, raw_read_opaque_extensions:false, native_operation_domains:false, logical_defaults020:false }) - } - pub(crate) fn local_element_by_ids(&self, document: usize, module: &str, element: &str) -> Option<&Value> { - let (module_index, elements) = self.element_locations.get(document)?.get(module)?; - let element_index = elements.get(element)?; - self.documents.get(document)?.get("modules")?.get(*module_index)?.get("elements")?.get(*element_index) - } - pub fn pins(&self) -> Vec { - self.inputs.iter().map(|i| i.pin.clone()).collect() - } - /// Resolve a selected Record by complete original identity, never its name. - pub fn record_by_identity(&self, identity: &Identity) -> Result { - let mut found = Vec::new(); - for (document, input) in self.inputs.iter().enumerate() { - if input.pin.document_id != identity.document_id - || input.pin.revision != identity.revision - { - continue; - } - if !input.selected_module_ids.contains(&identity.module) { continue; } - if let Some(element) = self.local_element_by_ids(document, &identity.module, &identity.element) { - if element["kind"] == "record" { - self.validate_selected010(&input.pin, element)?; - found.push(Record { identity: identity.clone(), pin: input.pin.clone(), value: element.clone(), document }); - } - } - } - if found.len() != 1 { - return Err(fail( - "WFT-NAME-MISSING", - "Original Record identity is missing or ambiguous", - )); - } - Ok(found.pop().unwrap()) - } - pub fn record(&self, namespace: Option<&Name>, name: &Name) -> Result { - let mut found = Vec::new(); - for (d, input) in self.inputs.iter().enumerate() { - let index = &self.record_names[d]; - let named = if let Some(namespace) = namespace { - let key = (namespace.value.clone(), name.value.clone()); - if name.quoted { - index.scoped_quoted_names.get(&key) - } else { - index.scoped_unquoted_names.get(&key) - } - } else { - if name.quoted { - index.quoted_names.get(&name.value) - } else { - index.unquoted_names.get(&name.value) - } - }.map(Vec::as_slice).unwrap_or(&[]); - let unnamed = if self.identity_name_fallback { - if let Some(namespace) = namespace { - let key = (namespace.value.clone(), name.value.clone()); - if name.quoted { - index.scoped_quoted_unnamed_ids.get(&key) - } else { - index.scoped_unquoted_unnamed_ids.get(&key) - } - } else { - if name.quoted { - index.quoted_unnamed_ids.get(&name.value) - } else { - index.unquoted_unnamed_ids.get(&name.value) - } - }.map(Vec::as_slice).unwrap_or(&[]) - } else { &[] }; - let (mut named_index, mut unnamed_index) = (0, 0); - while named_index < named.len() || unnamed_index < unnamed.len() { - let (module_index, element_index) = if named_index == named.len() - || (unnamed_index < unnamed.len() && unnamed[unnamed_index] < named[named_index]) - { - let location = unnamed[unnamed_index]; - unnamed_index += 1; - location - } else { - let location = named[named_index]; - named_index += 1; - location - }; - let module = &self.documents[d]["modules"][module_index]; - if !input - .selected_module_ids - .iter() - .any(|id| module["id"] == id.as_str()) - { - continue; - } - if namespace.is_some_and(|n| !n.matches(module["namespace"].as_str().unwrap())) { - continue; - } - let e = &module["elements"][element_index]; - if e["kind"] == "record" { - self.validate_selected010(&input.pin, e)?; - if self.raw_read_opaque_extensions && input.pin.umf_version == "0.10.0" { - for member in e["members"].as_array().ok_or_else(|| fail("WFT-TYPE", "Selected UMF 0.10 Record has no members"))? { - let validate_reference = |reference: &Value| -> Result<()> { - let module_id = reference["module"].as_str().ok_or_else(|| fail("WFT-TYPE", "Selected UMF 0.10 reference module absent"))?; - let element_id = reference["element"].as_str().ok_or_else(|| fail("WFT-TYPE", "Selected UMF 0.10 reference element absent"))?; - let (module_index, element_indexes) = self.element_locations[d].get(module_id).ok_or_else(|| fail("WFT-TYPE", "Selected UMF 0.10 reference module absent"))?; - let element_index = element_indexes.get(element_id).ok_or_else(|| fail("WFT-TYPE", "Selected UMF 0.10 reference element absent"))?; - let field = &self.documents[d]["modules"][*module_index]["elements"][*element_index]; - self.validate_selected010(&input.pin, field)?; - if let Some(item) = field.get("itemType") { - let item_module = item["module"].as_str().ok_or_else(|| fail("WFT-TYPE", "Selected UMF 0.10 item module absent"))?; - let item_element = item["element"].as_str().ok_or_else(|| fail("WFT-TYPE", "Selected UMF 0.10 item element absent"))?; - let (item_module_index, item_indexes) = self.element_locations[d].get(item_module).ok_or_else(|| fail("WFT-TYPE", "Selected UMF 0.10 item module absent"))?; - let item_index = item_indexes.get(item_element).ok_or_else(|| fail("WFT-TYPE", "Selected UMF 0.10 item element absent"))?; - self.validate_selected010(&input.pin, &self.documents[d]["modules"][*item_module_index]["elements"][*item_index])?; - } - Ok(()) - }; - validate_reference(member)?; - } - } - found.push(Record { - identity: Identity { - document_id: input.pin.document_id.clone(), - revision: input.pin.revision.clone(), - module: module["id"].as_str().unwrap().into(), - element: e["id"].as_str().unwrap().into(), - }, - pin: input.pin.clone(), - value: e.clone(), - document: d, - }); - } - } - } - if found.len() > 1 { - return Err(Diagnostic::new( - "WFT-NAME-AMBIGUOUS", - "resolve", - "Source name matches multiple records", - ).unavailable_source() - .at(&name.span)); - } - found.pop().ok_or_else(|| { - Diagnostic::new( - "WFT-NAME-MISSING", - "resolve", - "No queryable record matches source", - ).unavailable_source() - .at(&name.span) - }) - } - pub fn field(&self, record: &Record, name: &Name) -> Result<(Identity, LogicalType, String)> { - self.field_mode(record,name,self.security_inputs) - } - pub(crate) fn field03(&self, record:&Record,name:&Name)->Result<(Identity,LogicalType,String)> { - self.field_mode(record,name,true) - } - fn field_mode(&self,record:&Record,name:&Name,optional03:bool)->Result<(Identity,LogicalType,String)> { - let members = record.value["members"] - .as_array() - .ok_or_else(|| fail("WFT-TYPE", "Selected record has no explicit members"))?; - let mut refs = BTreeSet::new(); - let mut found = Vec::new(); - for reference in members { - let mid = reference["module"].as_str().unwrap(); - let eid = reference["element"].as_str().unwrap(); - if !refs.insert((mid, eid)) { - return Err(fail("WFT-MODEL", "Duplicate record member reference")); - } - if !self.element_locations[record.document].contains_key(mid) { - return Err(fail("WFT-MODEL", "Referenced local module missing")); - } - let element = self.local_element_by_ids(record.document, mid, eid) - .ok_or_else(|| fail("WFT-MODEL", "Referenced local element missing"))?; - if name.matches(self.query_name(element).unwrap_or("")) { - let mut identity = record.identity.clone(); - identity.module = mid.into(); - identity.element = eid.into(); - found.push((identity, element)); - } - } - if found.len() > 1 { - return Err(Diagnostic::new( - "WFT-NAME-AMBIGUOUS", - "resolve", - "Property name matches multiple members", - ).column(true) - .at(&name.span)); - } - let (identity, field) = found.pop().ok_or_else(|| { - Diagnostic::new( - "WFT-NAME-MISSING", - "resolve", - "Property not found among record members", - ).column(false) - .at(&name.span) - })?; - self.validate_selected010(&record.pin, field)?; - let ideal; - let selected=if optional03 && field["nullability"]=="absent-allowed" { - // Derive the ideal scalar domain, leaving original selected source bytes untouched. - ideal={let mut v=field.clone();v["nullability"]=Value::String("required".into());v};&ideal - } else {field}; - let logical_type = scalar_type_projected(selected, name, self.exact_numbers, self.exact_numbers, self.bounded_current_strings, self.field_projection_decimal38)?; - Ok(( - identity, - logical_type, - self.query_name(field).expect("matched query name").into(), - )) - } -} - -#[cfg(test)] -mod selected_module020_tests { - use super::*; - use serde_json::json; - - fn pin() -> ModelPin { - ModelPin { - document_id: "demo".into(), - revision: "1".into(), - umf_version: "0.10.0".into(), - sha256: "0".repeat(64), - } - } - - fn field(default: Value) -> Value { - json!({ - "kind":"field", "scalarType":"integer", "cardinality":"one", - "nullability":"required", "extensions":{}, "facets":{}, - "default":default - }) - } - - #[test] - fn module020_is_the_only_selected_path_that_admits_closed_non_null_defaults() { - let pin = pin(); - let document = json!({}); - let accepted = field(json!({"on":"missing","value":{"integerToken":"5"}})); - assert!(selected010_table_operation020(&pin, &accepted, &document).is_ok()); - assert!(selected010_table_operation(&pin, &accepted, &document).is_err()); - assert!(selected010(&pin, &accepted).is_err()); - - for invalid in [ - json!({"on":"missing","value":null}), - json!({"on":"null","value":{"integerToken":"5"}}), - json!({"on":"missing","value":{"integerToken":"5"},"extra":true}), - ] { - let field = field(invalid); - assert!(selected010_table_operation020(&pin, &field, &document).is_err()); - } - } -} - -pub(crate) fn scalar_type(field: &Value, name: &Name, mathematical_integer: bool, mathematical_decimal: bool, bounded_strings: bool) -> Result { - scalar_type_projected(field,name,mathematical_integer,mathematical_decimal,bounded_strings,false) -} -pub(crate) fn scalar_type_projected(field: &Value, name: &Name, mathematical_integer: bool, mathematical_decimal: bool, bounded_strings: bool, field_decimal38:bool) -> Result { - let err = || { - Diagnostic::new("WFT-TYPE", "type", "Selected field meaning is unsupported").at(&name.span) - }; - if field["kind"] != "field" - || field["nullability"] != "required" - || field["cardinality"] != "one" - || field.get("itemType").is_some() - || field.get("recordType").is_some() - || field["references"] - .as_array() - .is_some_and(|r| !r.is_empty()) - { - return Err(err()); - } - let family = match field["scalarType"].as_str() { - Some("boolean") => Family::Boolean, - Some("string") => Family::String, - Some("integer") => Family::Integer, - Some("decimal") => Family::Decimal, - _ => return Err(err()), - }; - let facets = field.get("facets").cloned().unwrap_or(json!({})); - let allowed: &[&str] = match family { - Family::Integer => &["integerWidth"], - Family::Decimal => &["precision", "scale"], - Family::String if bounded_strings => &["length"], - _ => &[], - }; - if facets - .as_object() - .unwrap() - .keys() - .any(|k| !allowed.contains(&k.as_str())) - { - return Err(err()); - } - if family == Family::String && bounded_strings { - crate::current_string_domain::bounds(&facets).map_err(|_| err())?; - } - if family == Family::Integer && !(mathematical_integer && facets == json!({})) { - let width = &facets["integerWidth"]; - let bits = width["bits"].as_u64().ok_or_else(err)?; - if !(1..=64).contains(&bits) - || !width["signed"].is_boolean() - || width.as_object().is_none_or(|m| m.len() != 2) - { - return Err(err()); - } - } - if family == Family::Decimal && !(mathematical_decimal && facets==json!({})) { - let p = facets["precision"].as_u64().ok_or_else(err)?; - let s = facets["scale"].as_u64().ok_or_else(err)?; - if !(1..=if field_decimal38 {38}else{28}).contains(&p) || s > p { - return Err(err()); - } - } - Ok(LogicalType { - family, - facets, - nullable: false, - }) -} - -#[cfg(test)] -mod core010_admission_tests { - use super::*; - - fn current_input() -> ModuleInput { - let document_json = include_str!("../../../tests/raw-postgresql/table-lifecycle-model.json").to_owned(); - let doc: Value = serde_json::from_str(&document_json).unwrap(); - ModuleInput { - pin: ModelPin { - document_id: doc["id"].as_str().unwrap().to_owned(), - revision: "current-fixture".into(), - umf_version: "0.10.0".into(), - sha256: sha256(document_json.as_bytes()), - }, - document_json, - selected_module_ids: vec!["demo".into()], - } - } - - #[test] - fn current_010_is_admitted_and_older_core_versions_are_rejected() { - let input = current_input(); - let catalog = Catalog::prepare(vec![input.clone()]).expect("UMF 0.10 is supported"); - assert_eq!(catalog.pins()[0].umf_version, "0.10.0"); - Catalog::prepare_security(vec![input]).expect("security preparation uses the same current core"); - - for version in ["0.7.0", "0.8.0"] { - let mut document: Value = serde_json::from_str(¤t_input().document_json).unwrap(); - document["umf"] = json!(version); - let document_json = document.to_string(); - let mut input = current_input(); - input.document_json = document_json.clone(); - input.pin.umf_version = version.into(); - input.pin.sha256 = sha256(document_json.as_bytes()); - let error = Catalog::prepare(vec![input.clone()]).unwrap_err(); - assert_eq!(error.code, "WFT-MODEL-VERSION", "version {version}"); - assert_eq!(Catalog::prepare_security(vec![input]).unwrap_err().code, "WFT-MODEL-VERSION"); - } - } - - #[test] - fn arras_view_requires_the_exact_owning_document_declaration() { - let mut input_a = current_input(); - let mut document_a: Value = serde_json::from_str(&input_a.document_json).unwrap(); - document_a["vocabularies"]["arras.view"] = json!({"version":"0.1.0"}); - let field_a = document_a["modules"][0]["elements"].as_array_mut().unwrap() - .iter_mut().find(|element| element["kind"] == "field").unwrap(); - field_a["extensions"]["arras.view"] = json!({"opaque":"preserved"}); - input_a.document_json = document_a.to_string(); - input_a.pin.sha256 = sha256(input_a.document_json.as_bytes()); - - let mut input_b = input_a.clone(); - let mut document_b: Value = serde_json::from_str(&input_b.document_json).unwrap(); - document_b["id"] = json!("second-document"); - document_b["vocabularies"].as_object_mut().unwrap().remove("arras.view"); - input_b.pin.document_id = "second-document".into(); - let field_b = document_b["modules"][0]["elements"].as_array_mut().unwrap() - .iter_mut().find(|element| element["kind"] == "field").unwrap(); - field_b["extensions"]["arras.view"] = json!({"opaque":"belongs to B"}); - input_b.document_json = document_b.to_string(); - input_b.pin.sha256 = sha256(input_b.document_json.as_bytes()); - - let catalog = Catalog::prepare_raw_read010(vec![input_a.clone(), input_b.clone()]).unwrap(); - let field_a = &catalog.documents[0]["modules"][0]["elements"].as_array().unwrap() - .iter().find(|element| element["kind"] == "field").unwrap(); - assert!(catalog.validate_selected010(&input_a.pin, field_a).is_ok()); - let field_b = &catalog.documents[1]["modules"][0]["elements"].as_array().unwrap() - .iter().find(|element| element["kind"] == "field").unwrap(); - assert_eq!(catalog.validate_selected010(&input_b.pin, field_b).unwrap_err().code, "WFT-TYPE"); - - // The ordinary selected profile has no document metadata context. - let ordinary = Catalog::prepare(vec![input_a.clone()]).unwrap(); - assert_eq!(ordinary.validate_selected010(&input_a.pin, field_a).unwrap_err().code, "WFT-TYPE"); - let mut foreign_pin = input_a.pin.clone(); - foreign_pin.revision = "foreign".into(); - assert_eq!(catalog.validate_selected010(&foreign_pin, field_a).unwrap_err().code, "WFT-PIN"); - } - - #[test] - fn indexed_record_lookup_preserves_name_matching_and_ambiguity() { - let mut input = current_input(); - let mut document: Value = serde_json::from_str(&input.document_json).unwrap(); - document["modules"][0]["elements"].as_array_mut().unwrap() - .iter_mut().find(|element| element["id"] == "Note").unwrap()["name"] = json!("Display"); - input.document_json = document.to_string(); - input.pin.sha256 = sha256(input.document_json.as_bytes()); - let single = Catalog::prepare_raw_read010(vec![input.clone()]).unwrap(); - let name = |value: &str, quoted| Name { - value: value.into(), quoted, span: crate::ir::Span { start: 0, end: 0 }, - }; - assert_eq!(single.record(None, &name("display", false)).unwrap().identity.element, "Note"); - assert!(single.record(None, &name("DISPLAY", false)).is_err()); - assert_eq!(single.record(None, &name("Display", true)).unwrap().identity.element, "Note"); - assert!(single.record(None, &name("display", true)).is_err()); - - let elements = document["modules"][0]["elements"].as_array_mut().unwrap(); - let mut duplicate = elements.iter().find(|element| element["id"] == "Note").unwrap().clone(); - duplicate["id"] = json!("Note2"); - duplicate["name"] = json!("DISPLAY"); - elements.push(duplicate); - input.document_json = document.to_string(); - input.pin.sha256 = sha256(input.document_json.as_bytes()); - let catalog = Catalog::prepare_raw_read010(vec![input]).unwrap(); - - assert_eq!(catalog.record(None, &name("display", false)).unwrap_err().code, "WFT-NAME-AMBIGUOUS"); - assert_eq!(catalog.record(None, &name("Display", true)).unwrap().identity.element, "Note"); - assert_eq!(catalog.record(None, &name("DISPLAY", true)).unwrap().identity.element, "Note2"); - // A declared display Name takes precedence over the identity-ID fallback. - assert!(catalog.record(None, &name("Note", true)).is_err()); - } - - #[test] - fn current_selected_content_still_refuses_unrecognized_extension_meaning() { - let mut input = current_input(); - let mut document: Value = serde_json::from_str(&input.document_json).unwrap(); - let field = document["modules"][0]["elements"].as_array_mut().unwrap() - .iter_mut().find(|element| element["id"] == "owner").unwrap(); - field["extensions"]["example.unknown"] = json!({"payload": 1}); - input.document_json = document.to_string(); - input.pin.sha256 = sha256(input.document_json.as_bytes()); - let mut catalog = Catalog::prepare(vec![input]).unwrap(); - catalog.identity_name_fallback = true; - let name = Name { value: "owner".into(), quoted: true, span: crate::ir::Span { start: 0, end: 0 } }; - let record_name = Name { value: "Note".into(), quoted: true, span: crate::ir::Span { start: 0, end: 0 } }; - let record = catalog.record(None, &record_name).unwrap(); - assert_eq!(catalog.field(&record, &name).unwrap_err().code, "WFT-TYPE"); - } - - #[test] - fn exact_numeric_mode_remains_available_for_current_core_sources() { - let mut document: Value = serde_json::from_str(current_input().document_json.as_str()).unwrap(); - let elements = document["modules"][0]["elements"].as_array_mut().unwrap(); - for element in elements.iter_mut() { - if element["name"].is_null() { - element["name"] = element["id"].clone(); - } - } - let record = elements.iter_mut().find(|element| element["id"] == "Note").unwrap(); - record["members"].as_array_mut().unwrap().extend([ - json!({"module":"demo","element":"amount"}), - json!({"module":"demo","element":"units"}), - ]); - elements.extend([ - json!({"id":"amount","name":"amount","kind":"field","scalarType":"decimal","nullability":"required","cardinality":"one","extensions":{}}), - json!({"id":"units","name":"units","kind":"field","scalarType":"integer","nullability":"required","cardinality":"one","extensions":{}}), - ]); - let document_json = document.to_string(); - let input = ModuleInput { - pin: ModelPin { - document_id: document["id"].as_str().unwrap().to_owned(), - revision: "numeric-current".into(), - umf_version: "0.10.0".into(), - sha256: sha256(document_json.as_bytes()), - }, - document_json, - selected_module_ids: vec!["demo".into()], - }; - let identity = Identity { - document_id: input.pin.document_id.clone(), - revision: input.pin.revision.clone(), - module: "demo".into(), - element: "Note".into(), - }; - let default = Catalog::prepare(vec![input.clone()]).unwrap(); - assert!(default.entity_descriptor(&default.record_by_identity(&identity).unwrap()).is_err()); - let exact = Catalog::prepare_exact_numbers(vec![input.clone()]).unwrap(); - let graph = exact.entity_descriptor(&exact.record_by_identity(&identity).unwrap()).unwrap().graph; - assert_eq!(exact.inputs(), &[input]); - for id in ["amount", "units"] { - let descriptor = graph.iter().find(|d| d.identity.element == id).unwrap(); - let crate::application_model::Shape::Scalar { logical_type } = &descriptor.shape else { panic!() }; - assert_eq!(logical_type.facets, json!({})); - let value = if id == "amount" { "9007199254740993.1234567890123456789000" } else { "900719925474099312345678901234567890" }; - crate::exact::literal(&crate::syntax::Literal { value: value.into(), kind: crate::syntax::LiteralKind::Number, span: crate::ir::Span { start: 0, end: 0 } }, logical_type).unwrap(); - } - } - - #[test] - fn derived_commerce_path_successor_passes_the_current_core_schema() { - let document_json = include_str!("../../../tests/fixtures/umf010/commerce-paths.json").to_owned(); - let document: Value = serde_json::from_str(&document_json).unwrap(); - let catalog = Catalog::prepare(vec![ModuleInput { - pin: ModelPin { - document_id: document["id"].as_str().unwrap().to_owned(), - revision: "commerce-paths-010".into(), - umf_version: "0.10.0".into(), - sha256: sha256(document_json.as_bytes()), - }, - document_json, - selected_module_ids: vec!["domain".into()], - }]).expect("the adapted test source validates under current UMF core"); - assert_eq!(catalog.pins()[0].umf_version, "0.10.0"); - } -} +//! Re-export of `weft_model::catalog`. UMF loading and resolution live in weft-model. +pub use weft_model::catalog::*; diff --git a/crates/weft-core/src/mutation.rs b/crates/weft-core/src/mutation.rs index 2a33de80..74b000b8 100644 --- a/crates/weft-core/src/mutation.rs +++ b/crates/weft-core/src/mutation.rs @@ -337,7 +337,6 @@ impl MutationRegistry { record: &record, parameters, used: BTreeMap::new(), - dependencies: Vec::new(), }; let mut assignments = Vec::new(); let mut provenance = Vec::new(); @@ -374,14 +373,15 @@ impl MutationRegistry { if resolver.used.len() != parameters.len() { return Err(fail("TYPE", "type", "Unused parameter refused")); } + let dependencies = predicate.dependencies(); let selection = generated( - json!({"protocol":"weft.update-selection/1","record":{"module":record.identity.module,"element":record.identity.element},"key":request["target"],"predicate":predicate}), + json!({"protocol":weft_expr::selection::PROTOCOL,"record":{"module":record.identity.module,"element":record.identity.element},"key":request["target"],"predicate":predicate.to_json()}), )?; let source_envelope = generated(json!({"dialect":request["dialect"],"sql":sql,"parameters":parameters}))?; let parameter_artifact = generated(request["parameters"].clone())?; let plan = generated( - json!({"protocol":"umf.actions.plan/1","model":request["modelPin"],"action":request["action"],"target":request["target"],"source":source_envelope,"selection":{"profile":request["selectionProfile"],"artifact":selection,"dependencies":resolver.dependencies},"inputs":{"entityParameter":prepared["entityParameter"],"assignments":assignments},"execution":{"selection":"frozen","atomicity":"batch","conflict":"stop","partition":"manifest-order","batchSize":batch},"limits":{"maxItems":max_items,"maxManifestBytes":max_bytes}}), + json!({"protocol":"umf.actions.plan/1","model":request["modelPin"],"action":request["action"],"target":request["target"],"source":source_envelope,"selection":{"profile":request["selectionProfile"],"artifact":selection,"dependencies":dependencies},"inputs":{"entityParameter":prepared["entityParameter"],"assignments":assignments},"execution":{"selection":"frozen","atomicity":"batch","conflict":"stop","partition":"manifest-order","batchSize":batch},"limits":{"maxItems":max_items,"maxManifestBytes":max_bytes}}), )?; Ok( json!({"interfaceVersion":"weft-mutation-compile/0.1.0","status":"compiled","plan":plan,"provenance":{"sourceSha256":source_envelope.sha256,"parameters":parameter_artifact,"entityParameter":prepared["entityParameter"],"action":request["action"],"assignments":provenance},"diagnostics":[]}), diff --git a/crates/weft-core/src/mutation_backend.rs b/crates/weft-core/src/mutation_backend.rs index 53dca3cf..446932a3 100644 --- a/crates/weft-core/src/mutation_backend.rs +++ b/crates/weft-core/src/mutation_backend.rs @@ -4,6 +4,8 @@ use crate::mutation::{fail, validate_ref, Artifact, MResult, MutationDiagnostic} use serde::Serialize; use serde_json::Value; use std::collections::BTreeSet; +use weft_expr::{compare::CompareOp, selection::{self, Refusal}}; +use weft_model::types::ScalarFamily; #[derive(Clone, Debug, Serialize, PartialEq, Eq)] #[serde(rename_all = "lowercase")] @@ -454,7 +456,7 @@ pub fn inspect_selection(selection: &Artifact, model: &Artifact) -> MResult MResult> { exact(program, &["protocol", "record", "key", "predicate"])?; - if program["protocol"] != "weft.update-selection/1" || document["umf"] != "0.10.0" { + if program["protocol"] != selection::PROTOCOL || document["umf"] != "0.10.0" { return Err(refused("Exact selection/core profile required")); } validate_ref(&program["key"], true)?; @@ -488,50 +490,31 @@ fn validate_selection(program: &Value, document: &Value) -> MResult> return Err(refused("Original Key component is not a Field")); } } - let mut work = vec![(&program["predicate"], 1)]; - let mut count = 0; - while let Some((node, depth)) = work.pop() { - if depth > 24 { - return Err(fail("LIMIT", "limit", "Selection predicate depth exceeded")); - } - let o = node - .as_object() - .filter(|o| o.len() == 1) - .ok_or_else(|| refused("Exact predicate node required"))?; - if let Some(compare) = o.get("compare") { - exact(compare, &["field", "op", "value"])?; + selection::walk( + &program["predicate"], + |compare| { if !members.contains(&compare["field"]) { return Err(refused("Predicate Field outside Record")); } let f = scalar(document, &compare["field"])?; - let op = compare["op"].as_str().unwrap_or(""); - if !matches!(op, "eq" | "ne" | "lt" | "le" | "gt" | "ge") - || f["scalarType"] == "boolean" && !matches!(op, "eq" | "ne") - { + let op = compare["op"].as_str().and_then(CompareOp::parse); + let family = f["scalarType"].as_str().and_then(ScalarFamily::from_umf); + if op.is_none_or(|op| family.is_some_and(|family| !op.admits(family))) { return Err(refused("Predicate operator/domain refused")); } crate::security_literals::check_literal(f, &compare["value"]) - .map_err(|_| refused("Predicate literal violates original Field"))?; - count += 1; - if count > 256 { - return Err(fail( - "LIMIT", - "limit", - "Selection comparison bound exceeded", - )); - } - } else { - let children = o - .get("and") - .or_else(|| o.get("or")) - .and_then(Value::as_array) - .filter(|cs| cs.len() >= 2) - .ok_or_else(|| refused("Unknown predicate or operand bounds"))?; - if children.len() > 64 { - return Err(fail("LIMIT", "limit", "Selection operand count exceeded")); - } - work.extend(children.iter().rev().map(|child| (child, depth + 1))); - } - } + .map_err(|_| refused("Predicate literal violates original Field")) + }, + |refusal| match refusal { + Refusal::Depth => fail("LIMIT", "limit", "Selection predicate depth exceeded"), + Refusal::Node => refused("Exact predicate node required"), + Refusal::CompareObject => refused("Selection object required"), + Refusal::CompareMembers => refused("Unknown selection member"), + Refusal::Operator => refused("Predicate operator/domain refused"), + Refusal::Operands => refused("Unknown predicate or operand bounds"), + Refusal::OperandCount => fail("LIMIT", "limit", "Selection operand count exceeded"), + Refusal::Comparisons => fail("LIMIT", "limit", "Selection comparison bound exceeded"), + }, + )?; Ok(fields.clone()) } diff --git a/crates/weft-core/src/mutation_selection.rs b/crates/weft-core/src/mutation_selection.rs index d179a23e..8b48d2e6 100644 --- a/crates/weft-core/src/mutation_selection.rs +++ b/crates/weft-core/src/mutation_selection.rs @@ -6,14 +6,15 @@ use crate::{ mutation_syntax::{Input, Predicate}, security_literals::check_literal, }; -use serde_json::{json, Value}; +use serde_json::Value; use std::collections::BTreeMap; +use weft_expr::selection; +use weft_model::types::ScalarFamily; pub(crate) struct Resolver<'a> { pub(crate) catalog: &'a Catalog, pub(crate) record: &'a Record, pub(crate) parameters: &'a serde_json::Map, pub(crate) used: BTreeMap, - pub(crate) dependencies: Vec, } impl Resolver<'_> { pub(crate) fn field(&self, name: &crate::syntax::Name) -> MResult<(Value, Value)> { @@ -22,7 +23,7 @@ impl Resolver<'_> { .as_array() .ok_or_else(|| fail("RESOLVE", "resolve", "Explicit Record members required"))? { - let module = self.catalog.documents[self.record.document]["modules"] + let module = self.catalog.documents()[self.record.document]["modules"] .as_array() .unwrap() .iter() @@ -94,38 +95,31 @@ impl Resolver<'_> { .map_err(|_| fail("TYPE", "type", "Literal violates original Field domain"))?; Ok(value) } - pub(crate) fn predicate(&mut self, p: &Predicate, depth: usize) -> MResult { - if depth > 24 { + pub(crate) fn predicate(&mut self, p: &Predicate, depth: usize) -> MResult { + if depth > selection::MAX_DEPTH { return Err(fail("LIMIT", "limit", "Decoded predicate depth exceeded")); } match p { Predicate::Compare(name, op, input) => { let (reference, field) = self.field(name)?; - if field["scalarType"] == "boolean" && !matches!(op.as_str(), "eq" | "ne") { + let family = field["scalarType"].as_str().and_then(ScalarFamily::from_umf); + if family.is_some_and(|family| !op.admits(family)) { return Err(fail("TYPE", "type", "Boolean ordering refused")); } let value = self.value(input, &reference, &field)?; - if !self.dependencies.contains(&reference) { - self.dependencies.push(reference.clone()); - } - Ok(json!({"compare":{"field":reference,"op":op,"value":value}})) + Ok(selection::Predicate::Compare(selection::Compare { field: reference, op: *op, value })) } Predicate::And(children) | Predicate::Or(children) => { let and = matches!(p, Predicate::And(_)); - let operator = if and { "and" } else { "or" }; let mut out = Vec::new(); for child in children { let next = self.predicate(child, depth + 1)?; - if let Some(same) = next.get(operator).and_then(Value::as_array) { - out.extend(same.iter().cloned()); - } else { - out.push(next); - } - if out.len() > 64 { + selection::Predicate::splice(&mut out, and, next); + if out.len() > selection::MAX_OPERANDS { return Err(fail("LIMIT", "limit", "Predicate operand bound exceeded")); } } - Ok(json!({operator:out})) + Ok(if and { selection::Predicate::And(out) } else { selection::Predicate::Or(out) }) } } } diff --git a/crates/weft-core/src/mutation_syntax.rs b/crates/weft-core/src/mutation_syntax.rs index add2ffdd..a754eb16 100644 --- a/crates/weft-core/src/mutation_syntax.rs +++ b/crates/weft-core/src/mutation_syntax.rs @@ -2,6 +2,7 @@ //! Bounded UPDATE syntax, separate from existing read interfaces. use super::mutation::{fail, MResult}; use crate::{ir::Span, syntax::Name}; +use weft_expr::compare::CompareOp; use serde_json::{json, Value}; #[derive(Clone, Debug)] enum Token { @@ -18,7 +19,7 @@ pub enum Input { } #[derive(Clone, Debug)] pub enum Predicate { - Compare(Name, String, Input), + Compare(Name, CompareOp, Input), And(Vec), Or(Vec), } @@ -180,25 +181,25 @@ impl Parser { let field = self.name()?; let op = if self.peek('=') { self.at += 1; - "eq" + CompareOp::Eq } else if self.peek('<') { self.at += 1; if self.peek('>') { self.at += 1; - "ne" + CompareOp::Ne } else if self.peek('=') { self.at += 1; - "le" + CompareOp::Le } else { - "lt" + CompareOp::Lt } } else if self.peek('>') { self.at += 1; if self.peek('=') { self.at += 1; - "ge" + CompareOp::Ge } else { - "gt" + CompareOp::Gt } } else { return Err(syntax()); @@ -211,7 +212,7 @@ impl Parser { "Predicate comparison bound exceeded", )); } - Ok(Predicate::Compare(field, op.into(), self.input()?)) + Ok(Predicate::Compare(field, op, self.input()?)) } // Iterative precedence/grouping avoids host-dependent recursion at the 128-group bound. fn predicate(&mut self) -> MResult { diff --git a/crates/weft-core/src/security_candidate_incidence.rs b/crates/weft-core/src/security_candidate_incidence.rs index d0295387..55092913 100644 --- a/crates/weft-core/src/security_candidate_incidence.rs +++ b/crates/weft-core/src/security_candidate_incidence.rs @@ -12,7 +12,7 @@ impl CandidateIncidenceValue{ pub fn key_id(&self)->&str{&self.key_id} pub fn components(&self)->&[ScalarLiteral]{&self.components} pub fn component_domains(&self)->&[Value]{&self.component_domains} - pub fn require_catalog(&self,catalog:&Catalog)->Result<()>{if catalog.pins()!=self.model_pins||catalog.inputs().len()!=catalog.documents.len(){return Err(fail());}for (input,document) in catalog.inputs().iter().zip(&catalog.documents){if crate::json::sha256(input.document_json.as_bytes())!=input.pin.sha256||input.pin.document_id!=document["id"]||input.pin.umf_version!=document["umf"]||crate::json::checked_json(&input.document_json).map_err(|_|fail())?!=*document{return Err(fail());}}Ok(())} + pub fn require_catalog(&self,catalog:&Catalog)->Result<()>{if catalog.pins()!=self.model_pins||catalog.inputs().len()!=catalog.documents().len(){return Err(fail());}for (input,document) in catalog.inputs().iter().zip(catalog.documents()){if crate::json::sha256(input.document_json.as_bytes())!=input.pin.sha256||input.pin.document_id!=document["id"]||input.pin.umf_version!=document["umf"]||crate::json::checked_json(&input.document_json).map_err(|_|fail())?!=*document{return Err(fail());}}Ok(())} pub fn read(plan:&SecurityCandidateLogicalPlan,catalog:&Catalog,association:&SecurityAssociationRef,role:&str,side:CandidateSide,target:&SecurityRef,key_id:&str,components:&[Value])->Result{ Self::read_bounded(plan,catalog,association,role,side,target,key_id,components,4_000_000) diff --git a/crates/weft-core/src/security_candidate_ontology.rs b/crates/weft-core/src/security_candidate_ontology.rs index e0b91f3a..6490fd83 100644 --- a/crates/weft-core/src/security_candidate_ontology.rs +++ b/crates/weft-core/src/security_candidate_ontology.rs @@ -47,7 +47,7 @@ impl SecurityCandidateOntologyClosure{ } }else{ let SecurityAssociationRef::Relationship(r)=&reference else{return Err(fail());}; - let doc=catalog.documents.iter().find(|d|d["id"]==r.document_id).ok_or_else(fail)?; + let doc=catalog.documents().iter().find(|d|d["id"]==r.document_id).ok_or_else(fail)?; let module=doc["modules"].as_array().ok_or_else(fail)?.iter().find(|m|m["id"]==r.module_id).ok_or_else(fail)?; let relations:Vec<_>=module["relationships"].as_array().ok_or_else(fail)?.iter().filter(|x|x["id"]==r.relationship_id).collect();if relations.len()!=1{return Err(fail());}let relation=relations[0]; known(relation,&["id","name","source","target","sourceMultiplicity","targetMultiplicity","targetLifecycle","directed","inverse","associationRecord"])?; diff --git a/crates/weft-core/src/security_ir.rs b/crates/weft-core/src/security_ir.rs index 1acd771f..bc0374a5 100644 --- a/crates/weft-core/src/security_ir.rs +++ b/crates/weft-core/src/security_ir.rs @@ -108,7 +108,11 @@ mod tests{ let fixture:Value=serde_json::from_str(include_str!("../tests/security-source-fixture.json")).unwrap(); let source=&fixture["resolution"]["documents"][0];let text=source["document"].to_string(); let inputs=serde_json::from_value(serde_json::json!([{"documentJson":text,"pin":{"documentId":source["document"]["id"],"revision":source["revision"],"umfVersion":"0.10.0","sha256":crate::json::sha256(text.as_bytes())},"selectedModuleIds":["m"]}])).unwrap(); - let mut catalog=Catalog::prepare_security(inputs).unwrap();let packet=SecuritySourcePacket::read(&fixture["policy"].to_string(),&fixture["resolution"]["ontology"].to_string(),&catalog).unwrap();let plan=SecurityLogicalPlan::read(packet,&catalog).unwrap(); - catalog.documents[0]["modules"][0]["elements"][0]["name"]=serde_json::json!("changed");assert!(plan.require_catalog(&catalog).is_err()); + let catalog=Catalog::prepare_security(inputs).unwrap();let packet=SecuritySourcePacket::read(&fixture["policy"].to_string(),&fixture["resolution"]["ontology"].to_string(),&catalog).unwrap();let plan=SecurityLogicalPlan::read(packet,&catalog).unwrap(); + // Prepared documents are immutable outside weft-model (whose own test covers + // in-place tampering); a catalog prepared from changed definitions must refuse. + let mut changed=source["document"].clone();changed["modules"][0]["elements"][0]["name"]=serde_json::json!("changed");let text=changed.to_string(); + let inputs=serde_json::from_value(serde_json::json!([{"documentJson":text,"pin":{"documentId":source["document"]["id"],"revision":source["revision"],"umfVersion":"0.10.0","sha256":crate::json::sha256(text.as_bytes())},"selectedModuleIds":["m"]}])).unwrap(); + let changed=Catalog::prepare_security(inputs).unwrap();assert!(plan.require_catalog(&catalog).is_ok());assert!(plan.require_catalog(&changed).is_err()); } } diff --git a/crates/weft-core/src/security_literals.rs b/crates/weft-core/src/security_literals.rs index cb454ff5..81f50502 100644 --- a/crates/weft-core/src/security_literals.rs +++ b/crates/weft-core/src/security_literals.rs @@ -1,172 +1,4 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 -//! Exact scalar literal/refinement checks for the shared security subset. No native authorization. -use crate::error::{Diagnostic,Result}; -use num_bigint::{BigInt,Sign}; -use serde_json::{json,Value}; -use std::collections::BTreeSet; -pub(crate) const MAX_TEXT:usize=4_000_000; -fn fail()->Diagnostic{Diagnostic::new("WFT-SECURITY-LITERAL","model","Security literal or refinement refused")} -#[derive(Debug,Clone,PartialEq,Eq,PartialOrd,Ord)] -pub enum ScalarLiteral{Boolean(bool),String(String),Binary(String),Number(BigInt)} -/// A validated scalar Field with its authored allowed-value domain normalized once. -/// Reusing this avoids reparsing an allowed-value list for every invocation value. -#[derive(Debug,Clone)] -pub struct PreparedLiteral { - base: Value, - allowed: Option>, -} -impl PreparedLiteral { - pub fn new(field:&Value)->Result{ - validate_field(field)?; - Self::from_validated(field) - } - pub(crate) fn from_validated(field:&Value)->Result{ - let base=json!({ - "kind":field["kind"], - "cardinality":field["cardinality"], - "nullability":field["nullability"], - "scalarType":field["scalarType"], - "facets":normalized_facets(field)?, - }); - let authored=field.get("allowedValues").cloned(); - let allowed=if let Some(values)=authored { - let values=values.as_array().ok_or_else(fail)?; - let mut identities=BTreeSet::new(); - for value in values { - let identity=scalar(&base,value,true)?.ok_or_else(fail)?; - if !identities.insert(identity){return Err(fail());} - } - Some(identities) - } else {None}; - Ok(Self{base,allowed}) - } - pub fn check(&self,value:&Value)->Result<()>{ - if let Some(identity)=scalar(&self.base,value,true)? { - if self.allowed.as_ref().is_some_and(|allowed|!allowed.contains(&identity)){return Err(fail());} - } - Ok(()) - } - - /// Export the already validated, normalized operands needed by the closed - /// generated query codec VM. This does not emit source text or attest to a - /// trusted build; it serializes only the constraints this owner has already - /// admitted and compiled. - pub fn generated_query_refinements(&self)->Result{ - let facets=&self.base["facets"]; - let range=if let Some(range)=facets.get("range"){ - let min=range.get("min").map(|value|numeric(&self.base,value).map(|number|number.to_str_radix(10))).transpose()?; - let max=range.get("max").map(|value|numeric(&self.base,value).map(|number|number.to_str_radix(10))).transpose()?; - json!({ - "minCoefficient":min, - "minInclusive":range.get("minInclusive").and_then(Value::as_bool).unwrap_or(true), - "maxCoefficient":max, - "maxInclusive":range.get("maxInclusive").and_then(Value::as_bool).unwrap_or(true), - }) - }else{Value::Null}; - let allowed=self.allowed.as_ref().map(|values|values.iter().map(|value|match value{ - ScalarLiteral::Boolean(value)=>json!({"kind":"boolean","value":value}), - ScalarLiteral::String(value)=>json!({"kind":"string","value":value}), - ScalarLiteral::Binary(value)=>json!({"kind":"binaryHex","value":value}), - ScalarLiteral::Number(value)=>json!({"kind":"numberCoefficient","value":value.to_str_radix(10)}), - }).collect::>()); - Ok(json!({ - "length":facets.get("length").cloned().unwrap_or(Value::Null), - "integerWidth":facets.get("integerWidth").cloned().unwrap_or(Value::Null), - "range":range, - "allowedIdentities":allowed, - })) - } -} -pub(crate) fn coefficient(token:&str,scale:u64,precision:Option)->Result{ - if token.len()>MAX_TEXT{return Err(fail());} - let (negative,body)=if let Some(v)=token.strip_prefix('-'){(true,v)}else{(false,token)}; - let at=body.find(['e','E']);let (mantissa,exponent)=if let Some(at)=at{(&body[..at],&body[at+1..])}else{(body,"0")}; - let exponent_digits=exponent.strip_prefix(['+','-']).unwrap_or(exponent); - if exponent_digits.is_empty()||!exponent_digits.bytes().all(|b|b.is_ascii_digit()){return Err(fail());} - let (whole,fraction)=if let Some((whole,fraction))=mantissa.split_once('.'){ - if fraction.is_empty()||!fraction.bytes().all(|b|b.is_ascii_digit()){return Err(fail());}(whole,fraction) - }else{(mantissa,"")}; - if whole.is_empty()||!whole.bytes().all(|b|b.is_ascii_digit())||(whole.len()>1&&whole.starts_with('0')){return Err(fail());} - let joined=format!("{whole}{fraction}");let mut digits=joined.trim_start_matches('0').to_string(); - if digits.is_empty(){return Ok(BigInt::from(0));} - if exponent.len()>32{return Err(fail());}let exponent: i128=exponent.parse().map_err(|_|fail())?; - let shift=exponent-fraction.len() as i128+scale as i128; - if shift<0{let cut=-shift;if cut>digits.len() as i128{return Err(fail());}let at=digits.len()-cut as usize;if !digits[at..].bytes().all(|b|b==b'0'){return Err(fail());}digits.truncate(at);} - else{let bound=precision.unwrap_or(MAX_TEXT as u64).min(MAX_TEXT as u64);if digits.len() as i128+shift>bound as i128{return Err(fail());}digits.extend(std::iter::repeat_n('0',shift as usize));} - if precision.is_some_and(|p|digits.len() as u64>p){return Err(fail());} - if digits.is_empty(){return Ok(BigInt::from(0));} - if negative{digits.insert(0,'-');}BigInt::parse_bytes(digits.as_bytes(),10).ok_or_else(fail) -} -pub(crate) fn unsigned(v:&Value)->Result{ - if !v.is_number(){return Err(fail());}let text=v.to_string();let n=coefficient(&text,0,None)?; - if n.sign()==Sign::Minus||text.starts_with('-')&&n==BigInt::from(0){return Err(fail());} - let (_,words)=n.to_u64_digits();if words.len()>1{return Err(fail());}let value=words.first().copied().unwrap_or(0);if value>9_007_199_254_740_991{return Err(fail());}Ok(value) -} -pub(crate) fn normalized_facets(field:&Value)->Result{ - let mut facets=field.get("facets").cloned().unwrap_or(json!({})); - for key in ["precision","scale"]{if let Some(value)=facets.get(key){let n=unsigned(value)?;facets[key]=json!(n);}} - for (group,keys) in [("length",&["min","max"][..]),("integerWidth",&["bits"][..])]{for key in keys{if let Some(value)=facets.get(group).and_then(|g|g.get(*key)){let n=unsigned(value)?;facets[group][*key]=json!(n);}}} - Ok(facets) -} -fn numeric(field:&Value,value:&Value)->Result{ - let facets=normalized_facets(field)?;let kind=field["scalarType"].as_str().ok_or_else(fail)?; - let wrapper=if kind=="integer"{"integerToken"}else if kind=="decimal"{"decimalToken"}else{return Err(fail());}; - let scale=if kind=="decimal"{unsigned(&facets["scale"])?}else{0};let precision=if kind=="decimal"{Some(unsigned(&facets["precision"])?)}else{None}; - coefficient(value[wrapper].as_str().ok_or_else(fail)?,scale,precision) -} -fn power_of_two(n:&BigInt)->bool{n.magnitude().to_u64_digits().iter().map(|n|n.count_ones() as u64).sum::()==1} -fn fits_width(n:&BigInt,width:&Value)->Result<()>{ - let bits=unsigned(&width["bits"])?;if bits==0{return Err(fail());}let magnitude=n.magnitude().bits();let negative=n.sign()==Sign::Minus; - let signed=width["signed"].as_bool().ok_or_else(fail)?; - if if signed{if negative{magnitudeResult>{ - if field["kind"]!="field"||field["cardinality"]!="one"{return Err(fail());} - if value.is_null(){return if field["nullability"]=="absent-allowed"{Ok(None)}else{Err(fail())};} - if value.as_object().is_none_or(|m|m.len()!=1){return Err(fail());} - let facets=normalized_facets(field)?;let kind=field["scalarType"].as_str().ok_or_else(fail)?; - let (identity,length)=match kind{ - "boolean"=>(ScalarLiteral::Boolean(value["boolean"].as_bool().ok_or_else(fail)?),None), - "string"=>{let text=value["string"].as_str().ok_or_else(fail)?;(ScalarLiteral::String(text.into()),Some(text.chars().count() as u64))}, - "binary"=>{let text=value["binaryHex"].as_str().ok_or_else(fail)?;if text.len()%2!=0||!text.bytes().all(|b|b.is_ascii_hexdigit()){return Err(fail());}(ScalarLiteral::Binary(text.to_ascii_lowercase()),Some(text.len() as u64/2))}, - "integer"|"decimal"=>{let n=numeric(field,value)?;if let Some(width)=facets.get("integerWidth"){fits_width(&n,width)?;} - if refinements{if let Some(range)=facets.get("range"){for (end,lower) in [("min",true),("max",false)]{if let Some(bound)=range.get(end){let b=numeric(field,bound)?;if if lower{nb||n==b&&range["maxInclusive"]==false}{return Err(fail());}}}}} - (ScalarLiteral::Number(n),None) - },_=>return Err(fail()) - }; - if let Some(bounds)=facets.get("length"){ - let length=length.ok_or_else(fail)?;let expected=if kind=="string"{"unicode-scalar"}else{"byte"};if bounds["unit"]!=expected{return Err(fail());} - if bounds.get("min").is_some_and(|v|unsigned(v).is_err()||lengthunsigned(v).unwrap()){return Err(fail());} - } - if refinements{if let Some(allowed)=field["allowedValues"].as_array(){let mut matched=false;for candidate in allowed{if scalar(field,candidate,false)?.as_ref()==Some(&identity){matched=true;break;}}if !matched{return Err(fail());}}} - Ok(Some(identity)) -} -pub fn check_literal(field:&Value,value:&Value)->Result<()>{crate::security_ontology::domain(field)?;scalar(field,value,true).map(|_|())} -fn at_extreme(field:&Value,n:&BigInt,upper:bool)->Result{ - let f=normalized_facets(field)?;if field["scalarType"]=="integer"{ - let Some(w)=f.get("integerWidth") else{return Ok(false);};let bits=unsigned(&w["bits"])?;let signed=w["signed"].as_bool().ok_or_else(fail)?; - if !upper{return Ok(if signed{n.sign()==Sign::Minus&&n.magnitude().bits()==bits&&power_of_two(n)}else{*n==BigInt::from(0)});} - let expected=if signed{bits-1}else{bits};let ones=n.magnitude().to_u64_digits().iter().map(|w|w.count_ones() as u64).sum::(); - return Ok(n.sign()!=Sign::Minus&&n.magnitude().bits()==expected&&ones==expected); - } - let precision=unsigned(&f["precision"])?;let text=n.to_str_radix(10);let digits=text.trim_start_matches('-'); - Ok((if upper{n.sign()!=Sign::Minus}else{n.sign()==Sign::Minus})&&digits.len() as u64==precision&&digits.bytes().all(|b|b==b'9')) -} -pub fn validate_field(field:&Value)->Result<()>{ - crate::security_ontology::domain(field)?; - let facets=normalized_facets(field)?; - if let Some(length)=facets.get("length"){if length.get("min").is_some()&&length.get("max").is_some()&&unsigned(&length["min"])? > unsigned(&length["max"])?{return Err(fail());}} - if let Some(range)=facets.get("range"){ - let mut base=field.clone();base["facets"].as_object_mut().ok_or_else(fail)?.remove("range");base.as_object_mut().unwrap().remove("allowedValues"); - for (end,flag) in [("min","minInclusive"),("max","maxInclusive")]{if let Some(bound)=range.get(end){if bound.is_null(){return Err(fail());}scalar(&base,bound,false)?;}else if range.get(flag).is_some(){return Err(fail());}} - if range.get("min").is_some()&&range["minInclusive"]==false&&at_extreme(field,&numeric(field,&range["min"])?,true)?{return Err(fail());} - if range.get("max").is_some()&&range["maxInclusive"]==false&&at_extreme(field,&numeric(field,&range["max"])?,false)?{return Err(fail());} - if range.get("min").is_some()&&range.get("max").is_some(){let min=numeric(field,&range["min"])?;let max=numeric(field,&range["max"])?;let low=min+BigInt::from(if range["minInclusive"]==false{1}else{0});let high=max-BigInt::from(if range["maxInclusive"]==false{1}else{0});if low>high{return Err(fail());}} - } - if let Some(allowed)=field["allowedValues"].as_array(){let mut base=field.clone();base.as_object_mut().unwrap().remove("allowedValues");let mut unique=BTreeSet::new();for value in allowed{let identity=scalar(&base,value,true)?.ok_or_else(fail)?;if !unique.insert(identity){return Err(fail());}}} - if let Some(examples)=field["examples"].as_array(){for value in examples{scalar(field,value,false)?;}} - if let Some(default)=field.get("default"){if default.as_object().is_none_or(|m|m.keys().any(|k|k!="on"&&k!="value"))||!matches!(default["on"].as_str(),Some("missing"|"null"|"missing-or-null")){return Err(fail());}check_literal(field,&default["value"])?;} - Ok(()) -} - -pub(crate) fn normalized_literal(field:&Value,value:&Value)->Result>{validate_field(field)?;scalar(field,value,true)} +//! Re-export of `weft_model::core_literal` (CoreLiteral admission against UMF Field domains). +pub use weft_model::core_literal::{check_literal, validate_field, PreparedLiteral, ScalarLiteral}; +pub(crate) use weft_model::core_literal::{coefficient, normalized_facets, normalized_literal, unsigned, MAX_TEXT}; diff --git a/crates/weft-core/src/security_obligation_sources.rs b/crates/weft-core/src/security_obligation_sources.rs index 36cdb46b..5d176ea7 100644 --- a/crates/weft-core/src/security_obligation_sources.rs +++ b/crates/weft-core/src/security_obligation_sources.rs @@ -177,7 +177,7 @@ impl<'a> SourceIssuer<'a, '_> { if self.rule_events.len()>4096 {return Err(fail());} Ok(()) } fn field_carrier(&mut self, ctx: &'a SecurityBackendContext<'_>, reference: &SecurityRef) -> Result<&'a serde_json::Value> { - for document in &ctx.catalog().documents { + for document in ctx.catalog().documents() { self.charge(0)?; if document["id"].as_str() != Some(reference.document_id.as_str()) { continue; } for module in document["modules"].as_array().ok_or_else(fail)? { diff --git a/crates/weft-core/src/security_ontology.rs b/crates/weft-core/src/security_ontology.rs index fe9940e7..9bf24b5d 100644 --- a/crates/weft-core/src/security_ontology.rs +++ b/crates/weft-core/src/security_ontology.rs @@ -1,7 +1,7 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 //! Exact ontology reference/key/classification closure. Does not authorize native execution. use crate::{error::{Diagnostic,Result},model::Catalog,security_source::SecuritySourcePacket}; -use serde_json::{json,Value}; +use serde_json::Value; use std::collections::{BTreeMap,BTreeSet}; #[derive(Debug,Clone,PartialEq,Eq,PartialOrd,Ord,serde::Serialize,serde::Deserialize)] #[serde(rename_all="camelCase",deny_unknown_fields)] @@ -17,30 +17,10 @@ pub(crate) struct TypeBinding{pub source:Value,pub fields:BTreeSet, #[derive(Debug)] pub struct SecurityOntologyClosure{pub(crate) types:BTreeMap,pub(crate) fields:BTreeMap,pub(crate) subject:SecurityRef,pub(crate) context:BTreeSet} pub(crate) fn locate<'a>(catalog:&'a Catalog,r:&SecurityRef)->Result<&'a Value>{ - let doc=catalog.documents.iter().find(|d|d["id"]==r.document_id).ok_or_else(fail)?; + let doc=catalog.documents().iter().find(|d|d["id"]==r.document_id).ok_or_else(fail)?; doc["modules"].as_array().ok_or_else(fail)?.iter().find(|m|m["id"]==r.module_id).ok_or_else(fail)?["elements"].as_array().ok_or_else(fail)?.iter().find(|e|e["id"]==r.element_id).ok_or_else(fail) } -pub(crate) fn domain(field:&Value)->Result{ - members(field,&["id","kind","name","description","extensions","scalarType","nullability","cardinality","facets","references","title","aliases","examples","allowedValues","default"])?; - if field["kind"]!="field"||field["cardinality"]!="one"||!matches!(field["scalarType"].as_str(),Some("boolean"|"string"|"integer"|"decimal"|"binary"))||field["references"].as_array().is_some_and(|r|!r.is_empty()){return Err(fail());} - let facets=crate::security_literals::normalized_facets(field)?; - members(&facets,&["length","precision","scale","integerWidth","range","collectionSize"])?; - for (group,keys) in [("length",&["min","max","unit"][..]),("integerWidth",&["bits","signed"][..]),("range",&["min","max","minInclusive","maxInclusive"][..]),("collectionSize",&["min","max"][..])]{if let Some(value)=facets.get(group){members(value,keys)?;}} - if !matches!(field["nullability"].as_str(),Some("required"|"absent-allowed"|"unspecified")){return Err(fail());} - let kind=field["scalarType"].as_str().unwrap(); - if facets.get("collectionSize").is_some()||((facets.get("precision").is_some()||facets.get("scale").is_some())&&kind!="decimal")||(facets.get("integerWidth").is_some()&&kind!="integer")||(facets.get("range").is_some()&&!matches!(kind,"integer"|"decimal")){return Err(fail());} - if let Some(length)=facets.get("length"){ - if length["unit"]!=if kind=="string"{"unicode-scalar"}else if kind=="binary"{"byte"}else{return Err(fail());}{return Err(fail());} - } - if let Some(width)=facets.get("integerWidth"){ - let bits=width["bits"].as_u64().ok_or_else(fail)?;if bits==0||bits>9_007_199_254_740_991||!width["signed"].is_boolean(){return Err(fail());} - } - if field["scalarType"]=="decimal"{ - let precision=facets["precision"].as_u64().ok_or_else(fail)?;let scale=facets["scale"].as_u64().ok_or_else(fail)?; - if precision==0||precision>9_007_199_254_740_991||scale>precision{return Err(fail());} - } - Ok(json!({"scalarType":field["scalarType"],"cardinality":field["cardinality"],"nullability":field["nullability"],"facets":facets,"allowedValues":field.get("allowedValues").unwrap_or(&Value::Null)})) -} +pub(crate) use weft_model::core_literal::field_domain as domain; /// Original authored scalar-key references, with no native identity or authority. pub fn authored_key_fields(catalog:&Catalog,record_ref:&SecurityRef,key_id:&str)->Result> { let record=locate(catalog,record_ref)?;if record["kind"]!="record"{return Err(fail());} diff --git a/crates/weft-core/src/security_record_homes.rs b/crates/weft-core/src/security_record_homes.rs index 77332e52..fee5dd3d 100644 --- a/crates/weft-core/src/security_record_homes.rs +++ b/crates/weft-core/src/security_record_homes.rs @@ -120,7 +120,7 @@ impl<'a> Index<'a> { let mut revisions = BTreeMap::new(); let mut elements = BTreeMap::new(); let mut types = BTreeMap::new(); - for (doc, input) in ctx.catalog().documents.iter().zip(ctx.catalog().inputs()) { + for (doc, input) in ctx.catalog().documents().iter().zip(ctx.catalog().inputs()) { b.charge(input.document_json.len())?; b.charge(input.pin.document_id.len())?; b.charge(input.pin.revision.len())?; diff --git a/crates/weft-core/src/security_semantic_coverage.rs b/crates/weft-core/src/security_semantic_coverage.rs index 24d192f2..b5a9515e 100644 --- a/crates/weft-core/src/security_semantic_coverage.rs +++ b/crates/weft-core/src/security_semantic_coverage.rs @@ -50,7 +50,7 @@ impl<'a> OwnerIndex<'a>{ fn build(ctx:&'a SecurityBackendContext<'_>,b:&mut Budget)->Result{ let mut revisions=BTreeMap::new();let mut elements=BTreeMap::new(); for input in ctx.catalog().inputs(){b.text(&input.document_json)?;b.text(&input.pin.document_id)?;b.text(&input.pin.revision)?;if revisions.insert(input.pin.document_id.clone(),input.pin.revision.clone()).is_some(){return Err(fail());}} - for (doc,input) in ctx.catalog().documents.iter().zip(ctx.catalog().inputs()){b.step()?;let document=doc["id"].as_str().ok_or_else(fail)?; + for (doc,input) in ctx.catalog().documents().iter().zip(ctx.catalog().inputs()){b.step()?;let document=doc["id"].as_str().ok_or_else(fail)?; let mut selected=BTreeSet::new();for id in &input.selected_module_ids{b.text(id)?;selected.insert(id.as_str());} for module in doc["modules"].as_array().ok_or_else(fail)?{b.step()?;let mid=module["id"].as_str().ok_or_else(fail)?;b.text(mid)?;if !selected.contains(mid){continue;} for element in module["elements"].as_array().ok_or_else(fail)?{b.step()?;let eid=element["id"].as_str().ok_or_else(fail)?;b.text(document)?;b.text(mid)?;b.text(eid)?; diff --git a/crates/weft-core/src/security_source.rs b/crates/weft-core/src/security_source.rs index 9babdaa0..70a2a0ea 100644 --- a/crates/weft-core/src/security_source.rs +++ b/crates/weft-core/src/security_source.rs @@ -17,8 +17,8 @@ fn fail(code:&str)->Diagnostic{Diagnostic::new(code,"model","Security source adm pub struct SecuritySourcePacket{model_pins:Vec,policy_json:String,ontology_json:String,policy:Value,ontology:Value} impl SecuritySourcePacket{ pub(crate) fn require_catalog(&self,catalog:&Catalog)->Result<()>{ - if catalog.pins()!=self.model_pins||catalog.inputs().len()!=catalog.documents.len(){return Err(fail("WFT-SECURITY-PIN"));} - for (input,document) in catalog.inputs().iter().zip(&catalog.documents){ + if catalog.pins()!=self.model_pins||catalog.inputs().len()!=catalog.documents().len(){return Err(fail("WFT-SECURITY-PIN"));} + for (input,document) in catalog.inputs().iter().zip(catalog.documents()){ if sha256(input.document_json.as_bytes())!=input.pin.sha256||input.pin.document_id!=document["id"]||input.pin.umf_version!=document["umf"]||checked_json(&input.document_json).map_err(|_|fail("WFT-SECURITY-PIN"))?!=*document{return Err(fail("WFT-SECURITY-PIN"));} }Ok(()) } @@ -38,8 +38,8 @@ impl SecuritySourcePacket{ let mut nodes=0;if let Some(rules)=policy["rules"].as_array(){for rule in rules{bounds(&rule["condition"],1,&mut nodes)?;}} if !(if candidate{CandidatePolicySchema::is_valid(&policy)&&CandidateOntologySchema::is_valid(&ontology)}else{PolicySchema::is_valid(&policy)&&OntologySchema::is_valid(&ontology)}){return Err(fail("WFT-SECURITY-SOURCE"));} if policy["ontology"]["documentId"]!=ontology["documentId"]||policy["ontology"]["revision"]!=ontology["revision"]{return Err(fail("WFT-SECURITY-PIN"));} - if catalog.inputs().len()!=catalog.documents.len(){return Err(fail("WFT-SECURITY-PIN"));} - for (input,document) in catalog.inputs().iter().zip(&catalog.documents){ + if catalog.inputs().len()!=catalog.documents().len(){return Err(fail("WFT-SECURITY-PIN"));} + for (input,document) in catalog.inputs().iter().zip(catalog.documents()){ if input.document_json.len()>4*1024*1024||sha256(input.document_json.as_bytes())!=input.pin.sha256||input.pin.document_id!=document["id"]||input.pin.umf_version!=document["umf"]||checked_json(&input.document_json).map_err(|_|fail("WFT-SECURITY-PIN"))?!=*document{return Err(fail("WFT-SECURITY-PIN"));} } let docs=ontology["documents"].as_array().unwrap();let mut ids=BTreeSet::new(); diff --git a/crates/weft-core/src/syntax.rs b/crates/weft-core/src/syntax.rs index 2ddd5185..d8cef90a 100644 --- a/crates/weft-core/src/syntax.rs +++ b/crates/weft-core/src/syntax.rs @@ -3,24 +3,7 @@ use crate::{ error::{Diagnostic, Result}, ir::Span, }; -#[derive(Debug, Clone)] -pub struct Name { - pub value: String, - pub quoted: bool, - pub span: Span, -} -impl Name { - pub fn matches(&self, value: &str) -> bool { - if self.quoted { - self.value == value - } else { - self.value == value.to_ascii_lowercase() - } - } - pub fn key(&self) -> &str { - &self.value - } -} +pub use weft_model::name::Name; #[derive(Debug, Clone)] pub struct Column { pub unqualified: bool, @@ -28,18 +11,7 @@ pub struct Column { pub field: Name, pub span: Span, } -#[derive(Debug, Clone)] -pub enum LiteralKind { - Number, - String, - Boolean, -} -#[derive(Debug, Clone)] -pub struct Literal { - pub value: String, - pub kind: LiteralKind, - pub span: Span, -} +pub use weft_model::exact::{Literal, LiteralKind}; #[derive(Debug, Clone)] pub enum Operand { Column(Column), diff --git a/crates/weft-expr/Cargo.toml b/crates/weft-expr/Cargo.toml new file mode 100644 index 00000000..d5e12075 --- /dev/null +++ b/crates/weft-expr/Cargo.toml @@ -0,0 +1,14 @@ +[package] +license = "MIT OR Apache-2.0" +name = "weft-expr" +# Own semver, independent of the workspace and wheel versions. See README.md. +version = "0.1.0" +edition.workspace = true +publish.workspace = true +rust-version = "1.90" +description = "Typed field, literal, comparison and exact-arithmetic expressions and the weft.update-selection/1 predicate format" + +[dependencies] +weft-model = { path = "../weft-model" } +serde.workspace = true +serde_json.workspace = true diff --git a/crates/weft-expr/LICENSE-APACHE b/crates/weft-expr/LICENSE-APACHE new file mode 100644 index 00000000..0398f585 --- /dev/null +++ b/crates/weft-expr/LICENSE-APACHE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2026 DocumentDrivenDX contributors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/crates/weft-expr/LICENSE-MIT b/crates/weft-expr/LICENSE-MIT new file mode 100644 index 00000000..d95948b5 --- /dev/null +++ b/crates/weft-expr/LICENSE-MIT @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 DocumentDrivenDX contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/crates/weft-expr/LICENSE.md b/crates/weft-expr/LICENSE.md new file mode 100644 index 00000000..2381950f --- /dev/null +++ b/crates/weft-expr/LICENSE.md @@ -0,0 +1,239 @@ +# License + +First-party software and documentation in this repository are dual-licensed, at +your option, under either the MIT License or the Apache License, Version 2.0. + +SPDX-License-Identifier: MIT OR Apache-2.0 + +Third-party code, dependencies, standards, fixtures, datasets and other externally +sourced material retain their original licenses and notices. This grant does not +relicense that material. Frozen evidence and embedded dependency copies retain +their original bytes; accompanying notices govern their use. + +## MIT License + +MIT License + +Copyright (c) 2026 DocumentDrivenDX contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +## Apache License, Version 2.0 + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2026 DocumentDrivenDX contributors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/crates/weft-expr/README.md b/crates/weft-expr/README.md new file mode 100644 index 00000000..480b42c9 --- /dev/null +++ b/crates/weft-expr/README.md @@ -0,0 +1,38 @@ +# weft-expr 0.1.0 + +The typed expression and predicate core shared by weft's compiler (`weft-core`) +and other compilers that type programs against UMF models (Fireweed's +`fireweed-compiler`): Field references, literal and parameter operands, +comparisons and their typing rule, exact arithmetic domains, and the +`weft.update-selection/1` predicate format of CONTRACT-009. + +It depends on `weft-model` for identities, logical types, family sets and +diagnostics. It parses no source text and lowers nothing to a backend. + +## Version + +Own semver, independent of the workspace and the `umf-weft` wheel; +`weft_expr::VERSION` reports it. While the major version is 0, a minor bump may +break the public surface below; a patch bump may not. Wire shapes and diagnostic +codes are part of the surface. + +## Public surface + +| Module | Items | Meaning | +| --- | --- | --- | +| `operand` | `Field`, `Value` | A Field read through a scan occurrence; a literal, typed parameter or Field operand. Serialized exactly as weft's 0.2/0.3 logical plans serialize them. | +| `compare` | `CompareOp`, `check`, `ComparisonOperator` | The six comparisons (`eq ne lt le gt ge`) with SQL text, and the typing rule: same family and facets on both sides, ordering defined on every family except boolean. `ComparisonOperator` is weft-sql 0.3's four-member wire enum. | +| `arithmetic` | `Domain`, `Expression`, `Kind`, `token_domain`, `MAX_LITERAL_BYTES` | Exact integer/fixed-scale decimal arithmetic: `*` adds scales, `+`/`-` take the larger scale; tokens are kept. Field operands must be required integers or scaled decimals. | +| `selection` | `PROTOCOL`, `Predicate`, `Compare`, `Refusal`, `walk`, `MAX_DEPTH`, `MIN_OPERANDS`, `MAX_OPERANDS`, `MAX_COMPARISONS` | `weft.update-selection/1`: decode with a pre-order visitor for the caller's domain checks, encode, comparisons in source order, plan `dependencies` in first-use order, connective splicing. | + +`F` is a `weft_model::types::FamilySet`. weft's profiles use the default +`Family`; a compiler that types timestamps and floats uses `ScalarFamily`. + +## What stays in weft-core + +Each weft dialect version's plan tree (`ir::Expression`, `application_ir::Predicate`, +`arithmetic_plan::Predicate`) keeps its own frozen wire shape and stays with the +dialect that owns it; those trees are built from the operands above. Source +parsing and name resolution (`arithmetic_resolve::resolve`, the mutation SQL +parser) stay in weft-core because they read weft-sql text. Checking that a +CoreLiteral is in a UMF Field's domain is `weft_model::core_literal`. diff --git a/crates/weft-expr/src/arithmetic.rs b/crates/weft-expr/src/arithmetic.rs new file mode 100644 index 00000000..803b6b3b --- /dev/null +++ b/crates/weft-expr/src/arithmetic.rs @@ -0,0 +1,173 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +//! Exact arithmetic over integer and fixed-scale decimal domains. +//! +//! An [`Expression`] keeps every original token; its [`Domain`] is derived +//! exactly: integer op integer is integer, `*` adds decimal scales, `+` and `-` +//! take the larger scale. Wire shapes are weft-sql 0.3's plan shapes +//! (CONTRACT `exact-arithmetic-v0.3`). Division and float arithmetic are not +//! exact and are not defined here. +use crate::operand::Field; +use serde::Serialize; +use weft_model::{ + diagnostic::{Diagnostic, Result}, + types::{Family, FamilySet, LogicalType, ScalarFamily, Span}, +}; + +/// Largest numeric token an arithmetic expression reads. +pub const MAX_LITERAL_BYTES: usize = 1024; + +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] +#[serde(tag = "family", rename_all = "camelCase")] +pub enum Domain { + Integer, + Decimal { scale: u64 }, +} +impl Domain { + pub fn scale(&self) -> u64 { + match self { + Self::Integer => 0, + Self::Decimal { scale } => *scale, + } + } + /// The domain of `self operator right` for `operator` in `+`, `-`, `*`. + pub fn combine(&self, right: &Self, operator: char, span: &Span) -> Result { + if matches!((self, right), (Self::Integer, Self::Integer)) { + return Ok(Self::Integer); + } + let scale = if operator == '*' { + self.scale().checked_add(right.scale()).ok_or_else(|| { + fail( + "WFT-LIMIT", + "Derived decimal scale exceeds representable metadata", + span, + ) + })? + } else { + self.scale().max(right.scale()) + }; + Ok(Self::Decimal { scale }) + } + /// The domain of a Field operand: a required integer, or a required decimal + /// with an authored scale. Nullable, non-numeric and float Fields refuse. + pub fn of_field(logical_type: &LogicalType, span: &Span) -> Result { + if logical_type.nullable { + return Err(fail("WFT-TYPE", "Arithmetic requires an admitted required scalar", span)); + } + match logical_type.family.widen() { + ScalarFamily::Integer => Ok(Self::Integer), + ScalarFamily::Decimal => Ok(Self::Decimal { + scale: logical_type.facets["scale"].as_u64().ok_or_else(|| { + fail("WFT-TYPE", "Authored decimal scale is unavailable", span) + })?, + }), + _ => Err(fail("WFT-TYPE", "Arithmetic requires numeric fields", span)), + } + } +} +#[derive(Debug, Clone, Serialize)] +pub struct Expression { + pub domain: Domain, + pub span: Span, + pub kind: Kind, +} +#[derive(Debug, Clone, Serialize)] +#[serde(tag = "op", rename_all = "camelCase")] +pub enum Kind { + Field { + field: Field, + }, + Literal { + value: String, + }, + Parameter { + name: String, + value: String, + }, + Negate { + operand: Box>, + }, + Binary { + operator: char, + left: Box>, + right: Box>, + }, +} +fn fail(code: &str, message: &str, span: &Span) -> Diagnostic { + Diagnostic::new(code, "resolve", message).at(span) +} +/// The domain of an exact base-ten token. `family` is the declared family of a +/// typed parameter; `None` derives the domain from the token's own digits. +pub fn token_domain(token: &str, family: Option, span: &Span) -> Result { + if token.len() > MAX_LITERAL_BYTES { + return Err(fail( + "WFT-LIMIT", + "Numeric token exceeds expression limit", + span, + )); + } + let unsigned = token.strip_prefix('-').unwrap_or(token); + let mut parts = unsigned.split('.'); + let whole = parts.next().unwrap_or(""); + let fraction = parts.next(); + if whole.is_empty() + || !whole.bytes().all(|b| b.is_ascii_digit()) + || parts.next().is_some() + || fraction.is_some_and(|f| f.is_empty() || !f.bytes().all(|b| b.is_ascii_digit())) + { + return Err(fail( + "WFT-NUMERIC-DOMAIN", + "Exact numeric token requires base-ten digits", + span, + )); + } + match family { + Some(ScalarFamily::Integer) if fraction.is_none() => Ok(Domain::Integer), + Some(ScalarFamily::Integer) => Err(fail( + "WFT-NUMERIC-DOMAIN", + "Integer parameter has a fractional token", + span, + )), + Some(ScalarFamily::Decimal) => Ok(Domain::Decimal { + scale: fraction.map_or(0, |f| f.len() as u64), + }), + Some(_) => Err(fail( + "WFT-TYPE", + "Arithmetic parameter must be numeric", + span, + )), + None => Ok(fraction.map_or(Domain::Integer, |f| Domain::Decimal { + scale: f.len() as u64, + })), + } +} + +#[cfg(test)] +mod tests { + use super::*; + use weft_model::types::ScalarType; + const SPAN: Span = Span { start: 0, end: 0 }; + #[test] + fn exact_scale_rules() { + let d = |scale| Domain::Decimal { scale }; + assert_eq!(Domain::Integer.combine(&Domain::Integer, '*', &SPAN).unwrap(), Domain::Integer); + assert_eq!(d(2).combine(&d(4), '*', &SPAN).unwrap(), d(6)); + assert_eq!(d(2).combine(&d(4), '+', &SPAN).unwrap(), d(4)); + assert_eq!(Domain::Integer.combine(&d(3), '-', &SPAN).unwrap(), d(3)); + assert_eq!(d(u64::MAX).combine(&d(1), '*', &SPAN).unwrap_err().code, "WFT-LIMIT"); + } + #[test] + fn tokens_and_fields() { + assert_eq!(token_domain("12.500", None, &SPAN).unwrap(), Domain::Decimal { scale: 3 }); + assert_eq!(token_domain("-7", Some(ScalarFamily::Integer), &SPAN).unwrap(), Domain::Integer); + for bad in ["1e2", "+1", "1.", ".5", ""] { + assert_eq!(token_domain(bad, None, &SPAN).unwrap_err().code, "WFT-NUMERIC-DOMAIN", "{bad}"); + } + assert_eq!(token_domain("1.5", Some(ScalarFamily::Float), &SPAN).unwrap_err().code, "WFT-TYPE"); + assert_eq!(token_domain(&"1".repeat(MAX_LITERAL_BYTES + 1), None, &SPAN).unwrap_err().code, "WFT-LIMIT"); + let decimal = LogicalType { family: Family::Decimal, facets: serde_json::json!({"precision":10,"scale":2}), nullable: false }; + assert_eq!(Domain::of_field(&decimal, &SPAN).unwrap(), Domain::Decimal { scale: 2 }); + assert!(Domain::of_field(&LogicalType { nullable: true, ..decimal }, &SPAN).is_err()); + assert!(Domain::of_field(&ScalarType::plain(ScalarFamily::Float, false), &SPAN).is_err()); + assert!(Domain::of_field(&ScalarType::plain(ScalarFamily::Timestamp, false), &SPAN).is_err()); + } +} diff --git a/crates/weft-expr/src/compare.rs b/crates/weft-expr/src/compare.rs new file mode 100644 index 00000000..e0beaa51 --- /dev/null +++ b/crates/weft-expr/src/compare.rs @@ -0,0 +1,125 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +//! Comparison operators and their typing rule. +use serde::{Deserialize, Serialize}; +use weft_model::{ + diagnostic::{Diagnostic, Result}, + types::{FamilySet, LogicalType, ScalarFamily, Span}, +}; + +/// The six scalar comparisons. Wire spelling is the `weft.update-selection/1` +/// spelling: `eq`, `ne`, `lt`, `le`, `gt`, `ge`. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum CompareOp { + Eq, + Ne, + Lt, + Le, + Gt, + Ge, +} +impl CompareOp { + pub const ALL: [CompareOp; 6] = [Self::Eq, Self::Ne, Self::Lt, Self::Le, Self::Gt, Self::Ge]; + /// Read the selection spelling. + pub fn parse(name: &str) -> Option { + Self::ALL.into_iter().find(|op| op.name() == name) + } + /// The selection spelling. + pub fn name(self) -> &'static str { + match self { + Self::Eq => "eq", + Self::Ne => "ne", + Self::Lt => "lt", + Self::Le => "le", + Self::Gt => "gt", + Self::Ge => "ge", + } + } + /// Standard SQL operator text. + pub fn sql(self) -> &'static str { + match self { + Self::Eq => "=", + Self::Ne => "<>", + Self::Lt => "<", + Self::Le => "<=", + Self::Gt => ">", + Self::Ge => ">=", + } + } + /// True for `lt`, `le`, `gt`, `ge`. + pub fn orders(self) -> bool { + !matches!(self, Self::Eq | Self::Ne) + } + /// Whether this comparison is defined on `family`: equality on every family, + /// ordering on every family except boolean. + pub fn admits(self, family: ScalarFamily) -> bool { + !(self.orders() && family == ScalarFamily::Boolean) + } +} +impl From for CompareOp { + fn from(op: ComparisonOperator) -> Self { + match op { + ComparisonOperator::Less => Self::Lt, + ComparisonOperator::LessEqual => Self::Le, + ComparisonOperator::GreaterEqual => Self::Ge, + ComparisonOperator::NotEqual => Self::Ne, + } + } +} + +/// Type a comparison of two operands: same family and facets, and the operator +/// defined on that family. Nullability may differ; a null operand makes the +/// comparison unknown, which is the evaluator's concern. +pub fn check(op: CompareOp, left: &LogicalType, right: &LogicalType, span: &Span) -> Result<()> { + let fail = |message| Diagnostic::new("WFT-TYPE", "type", message).at(span); + if left.family != right.family || left.facets != right.facets { + return Err(fail("Comparison operands have different value domains")); + } + if !op.admits(left.family.widen()) { + return Err(fail("Comparison is not defined on this value family")); + } + Ok(()) +} + +/// weft-sql 0.3 additive scalar comparison operators. The original `=` and `>` +/// keep their own plan shapes, so this wire enum has four members. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] +#[serde(rename_all = "camelCase")] +pub enum ComparisonOperator { Less, LessEqual, GreaterEqual, NotEqual } +impl ComparisonOperator { + pub fn sql(self) -> &'static str { CompareOp::from(self).sql() } + pub fn capability(self) -> &'static str { match self { Self::Less => "compare.less", Self::LessEqual => "compare.lessEqual", Self::GreaterEqual => "compare.greaterEqual", Self::NotEqual => "compare.notEqual" } } +} + +#[cfg(test)] +mod tests { + use super::*; + use weft_model::types::{Family, ScalarType}; + const SPAN: Span = Span { start: 0, end: 0 }; + #[test] + fn spellings_round_trip_and_match_legacy_sql() { + for op in CompareOp::ALL { + assert_eq!(CompareOp::parse(op.name()), Some(op)); + assert_eq!(serde_json::to_value(op).unwrap(), op.name()); + } + assert_eq!(CompareOp::parse("EQ"), None); + for (legacy, sql) in [(ComparisonOperator::Less, "<"), (ComparisonOperator::LessEqual, "<="), (ComparisonOperator::GreaterEqual, ">="), (ComparisonOperator::NotEqual, "<>")] { + assert_eq!(legacy.sql(), sql); + } + assert_eq!(serde_json::to_value(ComparisonOperator::LessEqual).unwrap(), "lessEqual"); + } + #[test] + fn ordering_is_refused_only_on_booleans() { + let boolean = LogicalType::plain(Family::Boolean, false); + assert!(check(CompareOp::Eq, &boolean, &boolean, &SPAN).is_ok()); + assert_eq!(check(CompareOp::Lt, &boolean, &boolean, &SPAN).unwrap_err().code, "WFT-TYPE"); + let instant = ScalarType::plain(ScalarFamily::Timestamp, true); + let float = ScalarType::plain(ScalarFamily::Float, false); + assert!(check(CompareOp::Ge, &instant, &ScalarType::plain(ScalarFamily::Timestamp, false), &SPAN).is_ok()); + assert!(check(CompareOp::Lt, &float, &float, &SPAN).is_ok()); + assert!(check(CompareOp::Eq, &instant, &float, &SPAN).is_err()); + let narrow = LogicalType { family: Family::Decimal, facets: serde_json::json!({"precision":4,"scale":2}), nullable: false }; + let wide = LogicalType { facets: serde_json::json!({"precision":6,"scale":2}), ..narrow.clone() }; + assert!(check(CompareOp::Eq, &narrow, &wide, &SPAN).is_err()); + } +} diff --git a/crates/weft-expr/src/lib.rs b/crates/weft-expr/src/lib.rs new file mode 100644 index 00000000..f31f3e6f --- /dev/null +++ b/crates/weft-expr/src/lib.rs @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +//! Typed expression and predicate core shared by weft and other compilers. +//! +//! Field references and literal/parameter operands over a [`weft_model`] family +//! set, comparison operators and their typing rule, exact arithmetic domains, +//! and the `weft.update-selection/1` predicate format (CONTRACT-009). Parsing +//! source text and lowering to a backend stay with each compiler. See +//! `README.md` for the versioned public surface. +pub mod arithmetic; +pub mod compare; +pub mod operand; +pub mod selection; + +/// This crate's contract version (`README.md`). +pub const VERSION: &str = env!("CARGO_PKG_VERSION"); diff --git a/crates/weft-expr/src/operand.rs b/crates/weft-expr/src/operand.rs new file mode 100644 index 00000000..64af8b62 --- /dev/null +++ b/crates/weft-expr/src/operand.rs @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +//! Typed operands: a Field read through a scan occurrence, and a literal, +//! parameter or Field value. Wire shapes are weft's logical-plan shapes. +use serde::{Deserialize, Serialize}; +use weft_model::types::{Family, Identity, LogicalType, Span}; + +/// A selected Field read through scan occurrence `scan`. +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub struct Field { + pub scan: String, + pub identity: Identity, + #[serde(rename = "type")] + pub logical_type: LogicalType, + pub span: Span, +} +/// A comparison operand: an exact literal token, a typed parameter, or a Field. +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +#[serde(tag = "kind", rename_all = "camelCase")] +pub enum Value { + Literal { + value: String, + #[serde(rename = "type")] + logical_type: LogicalType, + span: Span, + }, + Parameter { + name: String, + value: String, + #[serde(rename = "type")] + logical_type: LogicalType, + span: Span, + }, + Field { + field: Field, + }, +} +impl Value { + pub fn logical_type(&self) -> &LogicalType { + match self { + Self::Literal { logical_type, .. } | Self::Parameter { logical_type, .. } => logical_type, + Self::Field { field } => &field.logical_type, + } + } +} diff --git a/crates/weft-expr/src/selection.rs b/crates/weft-expr/src/selection.rs new file mode 100644 index 00000000..fe72501e --- /dev/null +++ b/crates/weft-expr/src/selection.rs @@ -0,0 +1,195 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +//! The `weft.update-selection/1` predicate format (CONTRACT-009). +//! +//! A predicate is exactly `{compare:{field:FieldRef,op,value:CoreLiteral}}`, +//! `{and:[Predicate; 2..=64]}` or `{or:[Predicate; 2..=64]}`, at most +//! [`MAX_DEPTH`] deep with at most [`MAX_COMPARISONS`] comparisons. FieldRef and +//! CoreLiteral stay JSON here: whether a FieldRef names a Field of the selected +//! Record and whether a literal is in that Field's domain is the caller's check, +//! made through the visitor passed to [`walk`]. +use crate::compare::CompareOp; +use serde_json::{json, Value}; + +pub const PROTOCOL: &str = "weft.update-selection/1"; +pub const MAX_DEPTH: usize = 24; +pub const MIN_OPERANDS: usize = 2; +pub const MAX_OPERANDS: usize = 64; +pub const MAX_COMPARISONS: usize = 256; + +/// One comparison: a local FieldRef (`{module, element}`), an operator and a UMF +/// CoreLiteral. +#[derive(Debug, Clone, PartialEq)] +pub struct Compare { + pub field: Value, + pub op: CompareOp, + pub value: Value, +} +#[derive(Debug, Clone, PartialEq)] +pub enum Predicate { + Compare(Compare), + And(Vec), + Or(Vec), +} +/// Why a predicate is structurally outside the format. Callers attach their +/// own diagnostic codes and messages. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Refusal { + /// Nesting deeper than [`MAX_DEPTH`]. + Depth, + /// A node that is not a one-member object. + Node, + /// A `compare` member that is not an object. + CompareObject, + /// A `compare` object without exactly `field`, `op` and `value`. + CompareMembers, + /// An operator outside `eq ne lt le gt ge`. + Operator, + /// Neither `compare`, nor `and`/`or` with at least [`MIN_OPERANDS`] operands. + Operands, + /// More than [`MAX_OPERANDS`] operands. + OperandCount, + /// More than [`MAX_COMPARISONS`] comparisons. + Comparisons, +} + +/// Decode a predicate, calling `visit` on each `compare` object (members checked, +/// operator not yet parsed) in source pre-order. A `visit` error stops the walk; +/// structural refusals go through `refuse`. The order of checks is fixed so that +/// every caller reports the same first refusal. +pub fn walk( + root: &Value, + mut visit: impl FnMut(&Value) -> Result<(), E>, + refuse: impl Fn(Refusal) -> E, +) -> Result { + let mut comparisons = 0; + walk_node(root, 1, &mut comparisons, &mut visit, &refuse) +} +fn walk_node( + node: &Value, + depth: usize, + comparisons: &mut usize, + visit: &mut impl FnMut(&Value) -> Result<(), E>, + refuse: &impl Fn(Refusal) -> E, +) -> Result { + if depth > MAX_DEPTH { + return Err(refuse(Refusal::Depth)); + } + let object = node.as_object().filter(|o| o.len() == 1).ok_or_else(|| refuse(Refusal::Node))?; + if let Some(compare) = object.get("compare") { + let members = compare.as_object().ok_or_else(|| refuse(Refusal::CompareObject))?; + if members.len() != 3 || ["field", "op", "value"].iter().any(|k| !members.contains_key(*k)) { + return Err(refuse(Refusal::CompareMembers)); + } + visit(compare)?; + let op = compare["op"].as_str().and_then(CompareOp::parse).ok_or_else(|| refuse(Refusal::Operator))?; + *comparisons += 1; + if *comparisons > MAX_COMPARISONS { + return Err(refuse(Refusal::Comparisons)); + } + return Ok(Predicate::Compare(Compare { field: compare["field"].clone(), op, value: compare["value"].clone() })); + } + let (and, children) = match (object.get("and"), object.get("or")) { + (Some(children), _) => (true, children), + (None, Some(children)) => (false, children), + _ => return Err(refuse(Refusal::Operands)), + }; + let children = children.as_array().filter(|c| c.len() >= MIN_OPERANDS).ok_or_else(|| refuse(Refusal::Operands))?; + if children.len() > MAX_OPERANDS { + return Err(refuse(Refusal::OperandCount)); + } + let children = children + .iter() + .map(|child| walk_node(child, depth + 1, comparisons, visit, refuse)) + .collect::, E>>()?; + Ok(if and { Predicate::And(children) } else { Predicate::Or(children) }) +} + +impl Predicate { + /// The format's JSON. `walk(&p.to_json(), ..)` returns `p`. + pub fn to_json(&self) -> Value { + match self { + Self::Compare(c) => json!({"compare": {"field": c.field, "op": c.op.name(), "value": c.value}}), + Self::And(children) => json!({"and": children.iter().map(Self::to_json).collect::>()}), + Self::Or(children) => json!({"or": children.iter().map(Self::to_json).collect::>()}), + } + } + /// Comparisons in source pre-order. + pub fn comparisons(&self) -> Vec<&Compare> { + let mut out = Vec::new(); + fn visit<'a>(p: &'a Predicate, out: &mut Vec<&'a Compare>) { + match p { + Predicate::Compare(c) => out.push(c), + Predicate::And(children) | Predicate::Or(children) => children.iter().for_each(|c| visit(c, out)), + } + } + visit(self, &mut out); + out + } + /// Every compared FieldRef once, in first source-use order (CONTRACT-009 + /// plan `dependencies`). + pub fn dependencies(&self) -> Vec { + let mut out: Vec = Vec::new(); + for c in self.comparisons() { + if !out.contains(&c.field) { + out.push(c.field.clone()); + } + } + out + } + /// Append `child` to a conjunction (`and == true`) or disjunction under + /// construction, splicing a child of the same connective into it. + pub fn splice(operands: &mut Vec, and: bool, child: Predicate) { + match child { + Self::And(children) if and => operands.extend(children), + Self::Or(children) if !and => operands.extend(children), + other => operands.push(other), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + fn cmp(element: &str, op: &str) -> Value { + json!({"compare":{"field":{"module":"m","element":element},"op":op,"value":{"integerToken":"1"}}}) + } + fn decode(v: &Value) -> Result { + walk(v, |_| Ok(()), |r| r) + } + #[test] + fn round_trip_and_dependencies() { + let source = json!({"and":[cmp("a","eq"), {"or":[cmp("b","lt"), cmp("a","ge")]}]}); + let p = decode(&source).unwrap(); + assert_eq!(p.to_json(), source); + assert_eq!(p.dependencies(), vec![json!({"module":"m","element":"a"}), json!({"module":"m","element":"b"})]); + assert_eq!(p.comparisons().iter().map(|c| c.op).collect::>(), [CompareOp::Eq, CompareOp::Lt, CompareOp::Ge]); + } + #[test] + fn structural_refusals() { + assert_eq!(decode(&json!({"and":[cmp("a","eq")]})), Err(Refusal::Operands)); + assert_eq!(decode(&json!({"not":cmp("a","eq")})), Err(Refusal::Operands)); + assert_eq!(decode(&json!({"and":[cmp("a","eq")],"or":[]})), Err(Refusal::Node)); + assert_eq!(decode(&json!({"compare":{"field":{},"op":"eq"}})), Err(Refusal::CompareMembers)); + assert_eq!(decode(&json!({"compare":[]})), Err(Refusal::CompareObject)); + assert_eq!(decode(&cmp("a", "like")), Err(Refusal::Operator)); + let wide: Vec = (0..65).map(|_| cmp("a", "eq")).collect(); + assert_eq!(decode(&json!({"or": wide})), Err(Refusal::OperandCount)); + let mut deep = cmp("a", "eq"); + for _ in 0..MAX_DEPTH { deep = json!({"and":[deep.clone(), cmp("b","eq")]}); } + assert_eq!(decode(&deep), Err(Refusal::Depth)); + let many: Vec = (0..64).map(|_| json!({"and":[cmp("a","eq"), cmp("a","eq"), cmp("a","eq"), cmp("a","eq"), cmp("a","eq")]})).collect(); + assert_eq!(decode(&json!({"or": many})), Err(Refusal::Comparisons)); + } + #[test] + fn visitor_errors_come_first_in_pre_order() { + let source = json!({"and":[cmp("bad","eq"), cmp("a","like")]}); + assert_eq!(walk(&source, |c| if c["field"]["element"] == "bad" { Err("domain") } else { Ok(()) }, |_| "structure"), Err("domain")); + } + #[test] + fn splice_flattens_same_connective() { + let mut operands = Vec::new(); + Predicate::splice(&mut operands, true, Predicate::And(vec![decode(&cmp("a","eq")).unwrap(), decode(&cmp("b","eq")).unwrap()])); + Predicate::splice(&mut operands, true, Predicate::Or(vec![decode(&cmp("c","eq")).unwrap(), decode(&cmp("d","eq")).unwrap()])); + assert_eq!(operands.len(), 3); + } +} diff --git a/crates/weft-model/Cargo.toml b/crates/weft-model/Cargo.toml new file mode 100644 index 00000000..13a62df5 --- /dev/null +++ b/crates/weft-model/Cargo.toml @@ -0,0 +1,17 @@ +[package] +license = "MIT OR Apache-2.0" +name = "weft-model" +# Own semver, independent of the workspace and wheel versions. See README.md. +version = "0.1.0" +edition.workspace = true +publish.workspace = true +rust-version = "1.90" +description = "UMF model loading, identity resolution, logical typing and exact values shared by weft and other compilers" + +[dependencies] +base64 = "=0.22.1" +serde.workspace = true +serde_json.workspace = true +sha2.workspace = true +num-bigint = "=0.4.8" +jsonschema = { version = "=0.56.0", default-features = false, features = ["macros", "arbitrary-precision"] } diff --git a/crates/weft-model/LICENSE-APACHE b/crates/weft-model/LICENSE-APACHE new file mode 100644 index 00000000..0398f585 --- /dev/null +++ b/crates/weft-model/LICENSE-APACHE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2026 DocumentDrivenDX contributors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/crates/weft-model/LICENSE-MIT b/crates/weft-model/LICENSE-MIT new file mode 100644 index 00000000..d95948b5 --- /dev/null +++ b/crates/weft-model/LICENSE-MIT @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 DocumentDrivenDX contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/crates/weft-model/LICENSE.md b/crates/weft-model/LICENSE.md new file mode 100644 index 00000000..2381950f --- /dev/null +++ b/crates/weft-model/LICENSE.md @@ -0,0 +1,239 @@ +# License + +First-party software and documentation in this repository are dual-licensed, at +your option, under either the MIT License or the Apache License, Version 2.0. + +SPDX-License-Identifier: MIT OR Apache-2.0 + +Third-party code, dependencies, standards, fixtures, datasets and other externally +sourced material retain their original licenses and notices. This grant does not +relicense that material. Frozen evidence and embedded dependency copies retain +their original bytes; accompanying notices govern their use. + +## MIT License + +MIT License + +Copyright (c) 2026 DocumentDrivenDX contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +## Apache License, Version 2.0 + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2026 DocumentDrivenDX contributors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/crates/weft-model/README.md b/crates/weft-model/README.md new file mode 100644 index 00000000..859f76f2 --- /dev/null +++ b/crates/weft-model/README.md @@ -0,0 +1,62 @@ +# weft-model 0.1.0 + +UMF model loading, identity resolution, logical typing and exact values, shared by +weft's compiler (`weft-core`) and other compilers that type programs against UMF +models (Fireweed's `fireweed-compiler`). + +This crate reads pinned UMF 0.10 documents, resolves authored names to original +identities, derives logical scalar types, and checks exact literal tokens. It does +no SQL parsing, planning, backend lowering, execution or IO. It depends on no +other workspace crate. + +## Version + +The crate has its own semver, independent of the workspace and the `umf-weft` +wheel. `weft_model::VERSION` reports it. While the major version is 0, a minor +bump may break the public surface below; a patch bump may not. Diagnostic codes +are part of the surface. + +## Public surface + +| Module | Items | Meaning | +| --- | --- | --- | +| `catalog` | `Catalog`, `ModuleInput`, `Record`, `Admission`, `ScalarRules`, `scalar_type`, `scalar_type_projected`, `scalar_type_with`, `selected010*` | Prepare a bundle of pinned UMF 0.10 documents (digest, identity, schema and limit checks), keep the original bytes, resolve Records and Fields by authored name or identity, and type selected Fields. | +| `core_literal` | `field_domain`, `validate_field`, `check_literal`, `PreparedLiteral`, `ScalarLiteral`, `normalized_facets`, `normalized_literal`, `coefficient`, `unsigned`, `MAX_TEXT` | UMF CoreLiteral JSON (`{"integerToken":..}`, `{"string":..}`, ...) admitted against a selected Field's domain: facets, integer widths, decimal precision/scale, ranges, allowed values, examples and defaults. Refusals keep weft's historical codes `WFT-SECURITY-LITERAL` and `WFT-SECURITY-ONTOLOGY`. | +| `descriptor` | `Shape`, `Descriptor`, `EntityDescriptor`, `Member`, `AuthoredKey`, `ExactScalarType`, `RelationshipRead`, `RelationshipIdentity` | Identity-based type graphs of Records and their members, authored keys and relationship reads. | +| `types` | `Span`, `Identity`, `ModelPin`, `Family`, `ScalarFamily`, `FamilySet`, `LogicalType`, `ScalarType` | Resolved identities and logical scalar types. | +| `name` | `Name` | Authored names; quoted names match exactly, unquoted names ASCII-case-fold. | +| `exact` | `Literal`, `LiteralKind`, `check_literal`, `coefficient`, `validate_float_token`, `validate_mathematical_decimal`, `validate_binary_token`, `validate_date_token`, `MAX_TOKEN` | Exact literal tokens and value-domain checks; exact decimal coefficients. No conversion through native floats. | +| `exact_instant` | `PROFILE`, `key` | UMF CONTRACT-065 RFC 3339 exact instants (`umf-rfc3339-exact-instant/1.0.0`): an exact `(epoch seconds, fractional digits)` comparison key at any precision. | +| `string_domain` | `Length`, `bounds`, `supported`, `validate`, `validate_facets`, `same_domain`, `literal_compatible` | Bounded Unicode-scalar string domains. | +| `json` | `sha256`, `checked_json`, `checked_truss_runtime_catalog_json`, `checked_json_bounded`, `checked_json_bounded_charged` | Bounded, duplicate-refusing JSON reading and digests. | +| `diagnostic` | `Diagnostic`, `Result`, `ResolutionReason` | Refusals: stable `code`, `phase`, `message`, optional source span. | + +## Family sets: how new types stay out of old profiles + +`LogicalType` is generic over the families a profile admits. + +- `Family` is the closed set every existing weft dialect profile admits: boolean, + string, integer, decimal. weft's IR and backends use `LogicalType` with this + default, so they cannot hold a timestamp or a float. The wire format of + `Family` is unchanged and refuses `"timestamp"` and `"float"`. +- `ScalarFamily` adds UMF `timestamp` (an exact UTC instant, any fractional + precision, millisecond resolution included) and `float` (a finite IEEE 754 + binary64 value written as a UMF float token, kept as its token). A caller opts + in by asking for it: `Catalog::field_as::`, + `Catalog::entity_descriptor_as::`, and the other `*_as` methods. +- Nullability is a flag on every `LogicalType`. A selected `absent-allowed` + scalar Field types as `nullable: true` only when the catalog's + `Admission::nullable_fields` is set; otherwise it refuses as before. + +`Admission::default()` is weft-sql 0.1's profile. weft-core sets the existing +flags exactly where it set the former crate-private fields; it never sets +`nullable_fields`. + +## Not in this crate + +SQL text, logical plans, expression trees (see `weft-expr`), backend manifests, +lowering and SQL emission stay in `weft-core` and the backend crates. + +UMF extensions that only one compiler interprets (for example Fireweed's +`umf.fireweed` text sets and JSON bodies) are refused by the selected-element +checks here unless an admission flag names them; they belong to that compiler. diff --git a/crates/weft-model/src/catalog.rs b/crates/weft-model/src/catalog.rs new file mode 100644 index 00000000..746b21d6 --- /dev/null +++ b/crates/weft-model/src/catalog.rs @@ -0,0 +1,1005 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +use crate::name::Name; +use crate::{ + diagnostic::{Diagnostic, Result}, + types::{FamilySet, Identity, LogicalType, ModelPin, ScalarFamily}, + json::{checked_json, sha256}, +}; +use serde::{Deserialize, Serialize}; +use serde_json::{json, Value}; +use std::collections::{BTreeSet, HashMap}; + +type ModuleElementIndex = HashMap)>; +type RecordLocation = (usize, usize); +#[derive(Debug, Default, Clone)] +struct RecordNameIndex { + quoted_names: HashMap>, + unquoted_names: HashMap>, + quoted_unnamed_ids: HashMap>, + unquoted_unnamed_ids: HashMap>, + scoped_quoted_names: HashMap<(String, String), Vec>, + scoped_unquoted_names: HashMap<(String, String), Vec>, + scoped_quoted_unnamed_ids: HashMap<(String, String), Vec>, + scoped_unquoted_unnamed_ids: HashMap<(String, String), Vec>, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", deny_unknown_fields)] +pub struct ModuleInput { + pub document_json: String, + pub pin: ModelPin, + pub selected_module_ids: Vec, +} +#[derive(Debug, Clone)] +/// A prepared snapshot retains exact source and parsed meaning together. +/// Direct mutation is inaccessible: +/// ```compile_fail +/// fn tamper(catalog: &mut weft_model::catalog::Catalog) { +/// catalog.inputs[0].document_json.clear(); +/// } +/// ``` +/// The public accessor also cannot mutate retained source: +/// ```compile_fail +/// fn tamper(catalog: &mut weft_model::catalog::Catalog) { +/// catalog.inputs()[0].document_json.clear(); +/// } +/// ``` + +pub struct Catalog { + inputs: Vec, + pub(crate) admission: Admission, + pub(crate) documents: Vec, + pub(crate) element_locations: Vec, + record_names: Vec, +} +/// Host-selected admission profile for a prepared [`Catalog`]. +/// +/// Every flag defaults to `false`; the default is weft-sql 0.1's profile. A +/// compiler selects flags when it prepares a catalog ([`Catalog::prepare_with`], +/// [`Catalog::admit`]); flags never widen on their own. `nullable_fields` admits +/// UMF meanings that no existing weft dialect profile accepts. Which scalar +/// families are admitted is chosen by the caller's family set +/// ([`crate::types::FamilySet`]), not by a flag here. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +#[non_exhaustive] +pub struct Admission { + /// Facetless `integer` and `decimal` Fields as mathematical domains. + pub exact_numbers: bool, + /// Decimal precision up to 38 instead of 28 for projected Fields. + pub field_projection_decimal38: bool, + /// Evidence that the trusted SDK scalar-presence preparation path ran. + pub sdk_scalar_presence: bool, + /// Security preparation: `absent-allowed` scalars are typed by their ideal domain. + pub security_inputs: bool, + /// Unnamed elements are addressable by their identity. + pub identity_name_fallback: bool, + /// Selected `umf.ddd`, `hohfeld.view` and declared `arras.view` extensions pass. + pub raw_read_opaque_extensions: bool, + /// Field `allowedValues` pass selected-element validation. + pub native_operation_domains: bool, + /// Closed non-null `default` on missing passes selected-element validation. + pub logical_defaults020: bool, + /// Projection-only exact `timestamp` and `binary` descriptors. + pub exact_scalars: bool, + /// Projection-only exact `date` descriptors. + pub exact_dates: bool, + /// String `length` facets with unicode-scalar bounds. + pub bounded_current_strings: bool, + /// `absent-allowed` scalar Fields type as nullable [`LogicalType`]s. + pub nullable_fields: bool, +} +impl Admission { + /// The scalar rules this profile applies to selected Fields. + pub fn scalar_rules(&self) -> ScalarRules { + ScalarRules { + mathematical_integer: self.exact_numbers, + mathematical_decimal: self.exact_numbers, + bounded_strings: self.bounded_current_strings, + decimal38: self.field_projection_decimal38, + nullable_fields: self.nullable_fields, + } + } +} +#[derive(Debug, Clone)] +pub struct Record { + pub identity: Identity, + pub pin: ModelPin, + pub value: Value, + pub document: usize, +} +fn fail(code: &str, message: &str) -> Diagnostic { + Diagnostic::new(code, "model", message) +} +/// UMF 0.10 selected semantics are admitted only where this compiler can +/// preserve their value domain. External semantic type references are retained +/// in source but remain outside the raw scalar profile. +pub fn selected010(pin: &ModelPin, element: &Value) -> Result<()> { + selected010_inner(pin, element, false, false, false, None) +} +pub fn selected010_raw_read(pin: &ModelPin, element: &Value, document: &Value) -> Result<()> { + selected010_inner(pin, element, true, false, false, Some(document)) +} +pub fn selected010_table_operation(pin: &ModelPin, element: &Value, document: &Value) -> Result<()> { + selected010_inner(pin, element, true, true, false, Some(document)) +} +pub fn selected010_table_operation020(pin: &ModelPin, element: &Value, document: &Value) -> Result<()> { + selected010_inner(pin, element, true, true, true, Some(document)) +} +fn selected010_inner(pin: &ModelPin, element: &Value, allow_raw_read_metadata: bool, native_domains: bool, logical_defaults020: bool, document: Option<&Value>) -> Result<()> { + if pin.umf_version != "0.10.0" { return Ok(()); } + const KNOWN: &[&str] = &["id", "name", "title", "description", "kind", "scalarType", + "nullability", "cardinality", "itemType", "recordType", "facets", "members", + "keys", "references", "extensions", "semanticTypes"]; + let extensions_supported = element.get("extensions").is_none_or(|value| { + value.as_object().is_some_and(|map| { + if !allow_raw_read_metadata { return map.is_empty(); } + map.iter().all(|(name, payload)| { + payload.is_object() && match name.as_str() { + "umf.ddd" => element["kind"] == "record", + "hohfeld.view" => matches!(element["kind"].as_str(), Some("record" | "field")), + "arras.view" => matches!(element["kind"].as_str(), Some("record" | "field")) + && document.is_some_and(|doc| doc["vocabularies"]["arras.view"]["version"] == "0.1.0"), + _ => false, + } + }) + }) + }); + let default020_is_closed = element.get("default").is_none_or(|default| { + logical_defaults020 + && element["kind"] == "field" + && default.as_object().is_some_and(|members| { + members.len() == 2 + && members.contains_key("on") + && members.contains_key("value") + && default["on"] == "missing" + && !default["value"].is_null() + }) + }); + if element.as_object().is_none_or(|m|m.keys().any(|k|!KNOWN.contains(&k.as_str())&&!(native_domains&&element["kind"]=="field"&&k=="allowedValues")&&!(logical_defaults020&&element["kind"]=="field"&&k=="default"&&default020_is_closed))) + || element.get("semanticTypes").is_some() + || !extensions_supported + { return Err(fail("WFT-TYPE", "Selected UMF 0.10 element has semantics outside the raw scalar profile")); } + if element["kind"] == "record" && element.get("references").and_then(Value::as_array).is_some_and(|r|!r.is_empty()) { + return Err(fail("WFT-TYPE", "Selected UMF 0.10 Record reference roles are unsupported")); + } + for member in element.get("members").and_then(Value::as_array).into_iter().flatten() { + if member.as_object().is_none_or(|m|m.keys().any(|k|!matches!(k.as_str(),"module"|"element"))) { + return Err(fail("WFT-TYPE", "Selected UMF 0.10 reference has unsupported qualifiers")); + } + } + Ok(()) +} +#[jsonschema::validator(path = "../../spec/upstream/umf-0.10.0-bundled.schema.json")] +struct Envelope10; +impl Catalog { + /// Immutable evidence of the trusted SDK scalar presence preparation path. + pub fn sdk_scalar_presence(&self)->bool {self.admission.sdk_scalar_presence} + /// The admission profile this catalog was prepared under. + pub fn admission(&self) -> &Admission { &self.admission } + /// Select admission flags for a freshly prepared catalog. Source custody is unchanged. + pub fn admit(mut self, select: impl FnOnce(&mut Admission)) -> Self { + select(&mut self.admission); + self + } + /// Prepare under an explicit admission profile. + pub fn prepare_with(inputs: Vec, admission: Admission) -> Result { + let mut catalog = Self::prepare(inputs)?; + catalog.admission = admission; + Ok(catalog) + } + /// Parsed documents, in input order. Each equals `checked_json` of its input. + pub fn documents(&self) -> &[Value] { &self.documents } + /// Re-check retained custody: digests, identities, versions and parsed meaning. + pub fn verify_retained_sources(&self) -> bool { + self.inputs.len() == self.documents.len() + && self.inputs.iter().zip(&self.documents).all(|(input, document)| { + sha256(input.document_json.as_bytes()) == input.pin.sha256 + && input.pin.document_id == document["id"] + && input.pin.umf_version == document["umf"] + && checked_json(&input.document_json).is_ok_and(|parsed| parsed == *document) + }) + } + /// New host-selected profile only; authored names always take precedence. + pub fn query_name<'a>(&self, element: &'a Value) -> Option<&'a str> { + element.get("name").and_then(Value::as_str).or_else(|| { + self.admission.identity_name_fallback.then(|| element["id"].as_str()).flatten() + }) + } + + pub fn validate_selected010(&self, pin: &ModelPin, element: &Value) -> Result<()> { + if self.admission.native_operation_domains || self.admission.raw_read_opaque_extensions { + let document = self.inputs.iter().zip(&self.documents) + .find(|(input, _)| input.pin == *pin) + .map(|(_, document)| document) + .ok_or_else(|| fail("WFT-PIN", "Selected element has no exact owning document"))?; + if self.admission.native_operation_domains && self.admission.logical_defaults020 { + selected010_table_operation020(pin, element, document) + } else if self.admission.native_operation_domains { + selected010_table_operation(pin, element, document) + } + else { selected010_raw_read(pin, element, document) } + } + else { selected010(pin, element) } + } + + /// Read-only original inputs. Clone and prepare a new Catalog to change them. + pub fn inputs(&self) -> &[ModuleInput] { + &self.inputs + } + /// Explicit host-selected admission of facetless mathematical numeric fields. + /// Source bytes/pins remain original; default preparation is unchanged. + pub fn prepare_exact_numbers(inputs: Vec) -> Result { + let mut catalog=Self::prepare(inputs)?;catalog.admission.exact_numbers=true;Ok(catalog) + } + pub fn prepare(inputs: Vec) -> Result { + Self::prepare_version(inputs, None, false) + } + pub fn prepare_truss_runtime(inputs: Vec, exact_numbers: bool) -> Result { + let mut catalog = Self::prepare_version(inputs, None, true)?; + catalog.admission.exact_numbers = exact_numbers; + catalog.admission.raw_read_opaque_extensions = true; + Ok(catalog) + } + /// Source custody only; security activation remains independently gated. + pub fn prepare_security(inputs: Vec) -> Result { + Self::prepare_version(inputs, Some("0.10.0"), false) + } + pub fn prepare_raw_read010(inputs: Vec) -> Result { + let mut catalog = Self::prepare_version(inputs, Some("0.10.0"), false)?; + catalog.admission.identity_name_fallback = true; + catalog.admission.raw_read_opaque_extensions = true; + Ok(catalog) + } + /// Finite Unity aggregate admission retains the raw-read checks while + /// permitting source facets that its bounded aggregate owners validate. + pub fn prepare_raw_read010_finite_current_aggregate(inputs: Vec) -> Result { + let mut catalog = Self::prepare_raw_read010(inputs)?; + catalog.admission.exact_numbers = true; + catalog.admission.field_projection_decimal38 = true; + Ok(catalog) + } + pub fn prepare_table_operation010(inputs: Vec) -> Result { + let mut catalog=Self::prepare_raw_read010(inputs)?; + catalog.admission.native_operation_domains=true; + Ok(catalog) + } + pub fn prepare_table_operation020(inputs: Vec) -> Result { + let mut catalog=Self::prepare_table_operation010(inputs)?; + catalog.admission.logical_defaults020=true; + Ok(catalog) + } + pub fn prepare_security_bounded_strings(inputs: Vec) -> Result { + let mut catalog = Self::prepare_security(inputs)?; + catalog.admission.bounded_current_strings = true; + Ok(catalog) + } + fn prepare_version(inputs: Vec, required_version: Option<&str>, truss_runtime: bool) -> Result { + if inputs.is_empty() || inputs.len() > 32 { + return Err(fail( + "WFT-LIMIT", + "Module bundle size is outside the supported bound", + )); + } + let mut documents = Vec::new(); + let mut ids = BTreeSet::new(); + let mut selections = 0; + for input in &inputs { + if input.document_json.len() > 4 * 1024 * 1024 { + return Err(fail("WFT-LIMIT", "Owning document exceeds byte limit")); + } + if input.pin.sha256 != sha256(input.document_json.as_bytes()) { + return Err(fail("WFT-PIN", "Owning document digest mismatch")); + } + let doc = (if truss_runtime { + crate::json::checked_truss_runtime_catalog_json(&input.document_json) + } else { + checked_json(&input.document_json) + }) + .map_err(|c| fail(c, "Owning document JSON refused"))?; + if doc["id"] != input.pin.document_id { + return Err(fail("WFT-PIN", "Owning document identity mismatch")); + } + if input.pin.umf_version != "0.10.0" + || doc["umf"] != "0.10.0" + || required_version.is_some_and(|version| input.pin.umf_version != version) + { + return Err(fail("WFT-MODEL-VERSION", "Unsupported UMF profile")); + } + if input.pin.revision.is_empty() || !ids.insert(input.pin.document_id.clone()) { + return Err(fail( + "WFT-MODEL", + "Empty revision or repeated owning document", + )); + } + if !Envelope10::is_valid(&doc) { + return Err(fail( + "WFT-MODEL", + "Document violates the pinned UMF envelope", + )); + } + let modules = doc["modules"].as_array().expect("schema checked modules"); + let mut mids = BTreeSet::new(); + for module in modules { + if !mids.insert(module["id"].as_str().unwrap()) { + return Err(fail("WFT-MODEL", "Duplicate module identity")); + } + let mut eids = BTreeSet::new(); + for element in module["elements"].as_array().unwrap() { + if !eids.insert(element["id"].as_str().unwrap()) { + return Err(fail("WFT-MODEL", "Duplicate element identity")); + } + } + } + let mut selected = BTreeSet::new(); + if input.selected_module_ids.is_empty() { + return Err(fail("WFT-MODEL", "No module selected")); + } + for id in &input.selected_module_ids { + if !selected.insert(id) || !mids.contains(id.as_str()) { + return Err(fail("WFT-MODEL", "Missing or repeated selected module")); + } + } + selections += selected.len(); + if selections > 256 { + return Err(fail("WFT-LIMIT", "Selected module count exceeds bound")); + } + documents.push(doc); + } + // Index coordinates only. Original immutable documents and their order + // remain authoritative; no Field meaning or authorization is cached. + let element_locations = documents.iter().map(|document| { + document["modules"].as_array().unwrap().iter().enumerate().map(|(mi, module)| { + let elements = module["elements"].as_array().unwrap().iter().enumerate() + .map(|(ei, element)| (element["id"].as_str().unwrap().to_owned(), ei)).collect(); + (module["id"].as_str().unwrap().to_owned(), (mi, elements)) + }).collect() + }).collect(); + let record_names = documents.iter().map(|document| { + let mut index = RecordNameIndex::default(); + for (module_index, module) in document["modules"].as_array().unwrap().iter().enumerate() { + for (element_index, element) in module["elements"].as_array().unwrap().iter().enumerate() { + if element["kind"] != "record" { continue; } + let namespace = module["namespace"].as_str(); + if let Some(name) = element["name"].as_str() { + index.quoted_names.entry(name.to_owned()).or_default().push((module_index, element_index)); + index.unquoted_names.entry(name.to_ascii_lowercase()).or_default().push((module_index, element_index)); + if let Some(namespace) = namespace { + let folded_namespace = namespace.to_ascii_lowercase(); + index.scoped_quoted_names.entry((namespace.to_owned(), name.to_owned())).or_default().push((module_index, element_index)); + index.scoped_unquoted_names.entry((namespace.to_owned(), name.to_ascii_lowercase())).or_default().push((module_index, element_index)); + if folded_namespace != namespace { + index.scoped_quoted_names.entry((folded_namespace.clone(), name.to_owned())).or_default().push((module_index, element_index)); + index.scoped_unquoted_names.entry((folded_namespace, name.to_ascii_lowercase())).or_default().push((module_index, element_index)); + } + } + } else if let Some(id) = element["id"].as_str() { + index.quoted_unnamed_ids.entry(id.to_owned()).or_default().push((module_index, element_index)); + index.unquoted_unnamed_ids.entry(id.to_ascii_lowercase()).or_default().push((module_index, element_index)); + if let Some(namespace) = namespace { + let folded_namespace = namespace.to_ascii_lowercase(); + index.scoped_quoted_unnamed_ids.entry((namespace.to_owned(), id.to_owned())).or_default().push((module_index, element_index)); + index.scoped_unquoted_unnamed_ids.entry((namespace.to_owned(), id.to_ascii_lowercase())).or_default().push((module_index, element_index)); + if folded_namespace != namespace { + index.scoped_quoted_unnamed_ids.entry((folded_namespace.clone(), id.to_owned())).or_default().push((module_index, element_index)); + index.scoped_unquoted_unnamed_ids.entry((folded_namespace, id.to_ascii_lowercase())).or_default().push((module_index, element_index)); + } + } + } + } + } + index + }).collect(); + Ok(Self { inputs, documents, element_locations, record_names, admission: Admission { security_inputs: required_version == Some("0.10.0"), ..Admission::default() } }) + } + pub fn local_element_by_ids(&self, document: usize, module: &str, element: &str) -> Option<&Value> { + let (module_index, elements) = self.element_locations.get(document)?.get(module)?; + let element_index = elements.get(element)?; + self.documents.get(document)?.get("modules")?.get(*module_index)?.get("elements")?.get(*element_index) + } + pub fn pins(&self) -> Vec { + self.inputs.iter().map(|i| i.pin.clone()).collect() + } + /// Resolve a selected Record by complete original identity, never its name. + pub fn record_by_identity(&self, identity: &Identity) -> Result { + let mut found = Vec::new(); + for (document, input) in self.inputs.iter().enumerate() { + if input.pin.document_id != identity.document_id + || input.pin.revision != identity.revision + { + continue; + } + if !input.selected_module_ids.contains(&identity.module) { continue; } + if let Some(element) = self.local_element_by_ids(document, &identity.module, &identity.element) { + if element["kind"] == "record" { + self.validate_selected010(&input.pin, element)?; + found.push(Record { identity: identity.clone(), pin: input.pin.clone(), value: element.clone(), document }); + } + } + } + if found.len() != 1 { + return Err(fail( + "WFT-NAME-MISSING", + "Original Record identity is missing or ambiguous", + )); + } + Ok(found.pop().unwrap()) + } + pub fn record(&self, namespace: Option<&Name>, name: &Name) -> Result { + let mut found = Vec::new(); + for (d, input) in self.inputs.iter().enumerate() { + let index = &self.record_names[d]; + let named = if let Some(namespace) = namespace { + let key = (namespace.value.clone(), name.value.clone()); + if name.quoted { + index.scoped_quoted_names.get(&key) + } else { + index.scoped_unquoted_names.get(&key) + } + } else { + if name.quoted { + index.quoted_names.get(&name.value) + } else { + index.unquoted_names.get(&name.value) + } + }.map(Vec::as_slice).unwrap_or(&[]); + let unnamed = if self.admission.identity_name_fallback { + if let Some(namespace) = namespace { + let key = (namespace.value.clone(), name.value.clone()); + if name.quoted { + index.scoped_quoted_unnamed_ids.get(&key) + } else { + index.scoped_unquoted_unnamed_ids.get(&key) + } + } else { + if name.quoted { + index.quoted_unnamed_ids.get(&name.value) + } else { + index.unquoted_unnamed_ids.get(&name.value) + } + }.map(Vec::as_slice).unwrap_or(&[]) + } else { &[] }; + let (mut named_index, mut unnamed_index) = (0, 0); + while named_index < named.len() || unnamed_index < unnamed.len() { + let (module_index, element_index) = if named_index == named.len() + || (unnamed_index < unnamed.len() && unnamed[unnamed_index] < named[named_index]) + { + let location = unnamed[unnamed_index]; + unnamed_index += 1; + location + } else { + let location = named[named_index]; + named_index += 1; + location + }; + let module = &self.documents[d]["modules"][module_index]; + if !input + .selected_module_ids + .iter() + .any(|id| module["id"] == id.as_str()) + { + continue; + } + if namespace.is_some_and(|n| !n.matches(module["namespace"].as_str().unwrap())) { + continue; + } + let e = &module["elements"][element_index]; + if e["kind"] == "record" { + self.validate_selected010(&input.pin, e)?; + if self.admission.raw_read_opaque_extensions && input.pin.umf_version == "0.10.0" { + for member in e["members"].as_array().ok_or_else(|| fail("WFT-TYPE", "Selected UMF 0.10 Record has no members"))? { + let validate_reference = |reference: &Value| -> Result<()> { + let module_id = reference["module"].as_str().ok_or_else(|| fail("WFT-TYPE", "Selected UMF 0.10 reference module absent"))?; + let element_id = reference["element"].as_str().ok_or_else(|| fail("WFT-TYPE", "Selected UMF 0.10 reference element absent"))?; + let (module_index, element_indexes) = self.element_locations[d].get(module_id).ok_or_else(|| fail("WFT-TYPE", "Selected UMF 0.10 reference module absent"))?; + let element_index = element_indexes.get(element_id).ok_or_else(|| fail("WFT-TYPE", "Selected UMF 0.10 reference element absent"))?; + let field = &self.documents[d]["modules"][*module_index]["elements"][*element_index]; + self.validate_selected010(&input.pin, field)?; + if let Some(item) = field.get("itemType") { + let item_module = item["module"].as_str().ok_or_else(|| fail("WFT-TYPE", "Selected UMF 0.10 item module absent"))?; + let item_element = item["element"].as_str().ok_or_else(|| fail("WFT-TYPE", "Selected UMF 0.10 item element absent"))?; + let (item_module_index, item_indexes) = self.element_locations[d].get(item_module).ok_or_else(|| fail("WFT-TYPE", "Selected UMF 0.10 item module absent"))?; + let item_index = item_indexes.get(item_element).ok_or_else(|| fail("WFT-TYPE", "Selected UMF 0.10 item element absent"))?; + self.validate_selected010(&input.pin, &self.documents[d]["modules"][*item_module_index]["elements"][*item_index])?; + } + Ok(()) + }; + validate_reference(member)?; + } + } + found.push(Record { + identity: Identity { + document_id: input.pin.document_id.clone(), + revision: input.pin.revision.clone(), + module: module["id"].as_str().unwrap().into(), + element: e["id"].as_str().unwrap().into(), + }, + pin: input.pin.clone(), + value: e.clone(), + document: d, + }); + } + } + } + if found.len() > 1 { + return Err(Diagnostic::new( + "WFT-NAME-AMBIGUOUS", + "resolve", + "Source name matches multiple records", + ).unavailable_source() + .at(&name.span)); + } + found.pop().ok_or_else(|| { + Diagnostic::new( + "WFT-NAME-MISSING", + "resolve", + "No queryable record matches source", + ).unavailable_source() + .at(&name.span) + }) + } + pub fn field(&self, record: &Record, name: &Name) -> Result<(Identity, LogicalType, String)> { + self.field_mode(record,name,self.admission.security_inputs) + } + /// [`Catalog::field`] over an explicit family set, e.g. [`ScalarFamily`]. + pub fn field_as(&self, record: &Record, name: &Name) -> Result<(Identity, LogicalType, String)> { + self.field_mode(record,name,self.admission.security_inputs) + } + pub fn field03(&self, record:&Record,name:&Name)->Result<(Identity,LogicalType,String)> { + self.field_mode(record,name,true) + } + fn field_mode(&self,record:&Record,name:&Name,optional03:bool)->Result<(Identity,LogicalType,String)> { + let members = record.value["members"] + .as_array() + .ok_or_else(|| fail("WFT-TYPE", "Selected record has no explicit members"))?; + let mut refs = BTreeSet::new(); + let mut found = Vec::new(); + for reference in members { + let mid = reference["module"].as_str().unwrap(); + let eid = reference["element"].as_str().unwrap(); + if !refs.insert((mid, eid)) { + return Err(fail("WFT-MODEL", "Duplicate record member reference")); + } + if !self.element_locations[record.document].contains_key(mid) { + return Err(fail("WFT-MODEL", "Referenced local module missing")); + } + let element = self.local_element_by_ids(record.document, mid, eid) + .ok_or_else(|| fail("WFT-MODEL", "Referenced local element missing"))?; + if name.matches(self.query_name(element).unwrap_or("")) { + let mut identity = record.identity.clone(); + identity.module = mid.into(); + identity.element = eid.into(); + found.push((identity, element)); + } + } + if found.len() > 1 { + return Err(Diagnostic::new( + "WFT-NAME-AMBIGUOUS", + "resolve", + "Property name matches multiple members", + ).column(true) + .at(&name.span)); + } + let (identity, field) = found.pop().ok_or_else(|| { + Diagnostic::new( + "WFT-NAME-MISSING", + "resolve", + "Property not found among record members", + ).column(false) + .at(&name.span) + })?; + self.validate_selected010(&record.pin, field)?; + let ideal; + let selected=if optional03 && field["nullability"]=="absent-allowed" { + // Derive the ideal scalar domain, leaving original selected source bytes untouched. + ideal={let mut v=field.clone();v["nullability"]=Value::String("required".into());v};&ideal + } else {field}; + let logical_type = scalar_type_with(selected, name, self.admission.scalar_rules())?; + Ok(( + identity, + logical_type, + self.query_name(field).expect("matched query name").into(), + )) + } +} + +#[cfg(test)] +mod selected_module020_tests { + use super::*; + use serde_json::json; + + fn pin() -> ModelPin { + ModelPin { + document_id: "demo".into(), + revision: "1".into(), + umf_version: "0.10.0".into(), + sha256: "0".repeat(64), + } + } + + fn field(default: Value) -> Value { + json!({ + "kind":"field", "scalarType":"integer", "cardinality":"one", + "nullability":"required", "extensions":{}, "facets":{}, + "default":default + }) + } + + #[test] + fn module020_is_the_only_selected_path_that_admits_closed_non_null_defaults() { + let pin = pin(); + let document = json!({}); + let accepted = field(json!({"on":"missing","value":{"integerToken":"5"}})); + assert!(selected010_table_operation020(&pin, &accepted, &document).is_ok()); + assert!(selected010_table_operation(&pin, &accepted, &document).is_err()); + assert!(selected010(&pin, &accepted).is_err()); + + for invalid in [ + json!({"on":"missing","value":null}), + json!({"on":"null","value":{"integerToken":"5"}}), + json!({"on":"missing","value":{"integerToken":"5"},"extra":true}), + ] { + let field = field(invalid); + assert!(selected010_table_operation020(&pin, &field, &document).is_err()); + } + } +} + +/// Rules for deriving a [`LogicalType`] from one selected UMF Field. +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] +pub struct ScalarRules { + pub mathematical_integer: bool, + pub mathematical_decimal: bool, + pub bounded_strings: bool, + pub decimal38: bool, + pub nullable_fields: bool, +} +pub fn scalar_type(field: &Value, name: &Name, mathematical_integer: bool, mathematical_decimal: bool, bounded_strings: bool) -> Result { + scalar_type_projected(field,name,mathematical_integer,mathematical_decimal,bounded_strings,false) +} +pub fn scalar_type_projected(field: &Value, name: &Name, mathematical_integer: bool, mathematical_decimal: bool, bounded_strings: bool, field_decimal38:bool) -> Result { + scalar_type_with(field, name, ScalarRules { mathematical_integer, mathematical_decimal, bounded_strings, decimal38: field_decimal38, ..ScalarRules::default() }) +} +/// Derive the logical type of a selected scalar Field over family set `F`, or +/// refuse its meaning. A family outside `F` refuses like any unsupported meaning. +pub fn scalar_type_with(field: &Value, name: &Name, rules: ScalarRules) -> Result> { + let err = || { + Diagnostic::new("WFT-TYPE", "type", "Selected field meaning is unsupported").at(&name.span) + }; + let nullable = match field["nullability"].as_str() { + Some("required") => false, + Some("absent-allowed") if rules.nullable_fields => true, + _ => return Err(err()), + }; + if field["kind"] != "field" + || field["cardinality"] != "one" + || field.get("itemType").is_some() + || field.get("recordType").is_some() + || field["references"] + .as_array() + .is_some_and(|r| !r.is_empty()) + { + return Err(err()); + } + let family: F = field["scalarType"].as_str() + .and_then(ScalarFamily::from_umf) + .and_then(F::admit) + .ok_or_else(err)?; + let wide = family.widen(); + let facets = field.get("facets").cloned().unwrap_or(json!({})); + let allowed: &[&str] = match wide { + ScalarFamily::Integer => &["integerWidth"], + ScalarFamily::Decimal => &["precision", "scale"], + ScalarFamily::String if rules.bounded_strings => &["length"], + _ => &[], + }; + if facets + .as_object() + .unwrap() + .keys() + .any(|k| !allowed.contains(&k.as_str())) + { + return Err(err()); + } + if wide == ScalarFamily::String && rules.bounded_strings { + crate::string_domain::bounds(&facets).map_err(|_| err())?; + } + if wide == ScalarFamily::Integer && !(rules.mathematical_integer && facets == json!({})) { + let width = &facets["integerWidth"]; + let bits = width["bits"].as_u64().ok_or_else(err)?; + if !(1..=64).contains(&bits) + || !width["signed"].is_boolean() + || width.as_object().is_none_or(|m| m.len() != 2) + { + return Err(err()); + } + } + if wide == ScalarFamily::Decimal && !(rules.mathematical_decimal && facets==json!({})) { + let p = facets["precision"].as_u64().ok_or_else(err)?; + let s = facets["scale"].as_u64().ok_or_else(err)?; + if !(1..=if rules.decimal38 {38}else{28}).contains(&p) || s > p { + return Err(err()); + } + } + Ok(LogicalType { + family, + facets, + nullable, + }) +} + +#[cfg(test)] +mod core010_admission_tests { + use super::*; + + fn current_input() -> ModuleInput { + let document_json = include_str!("../../../tests/raw-postgresql/table-lifecycle-model.json").to_owned(); + let doc: Value = serde_json::from_str(&document_json).unwrap(); + ModuleInput { + pin: ModelPin { + document_id: doc["id"].as_str().unwrap().to_owned(), + revision: "current-fixture".into(), + umf_version: "0.10.0".into(), + sha256: sha256(document_json.as_bytes()), + }, + document_json, + selected_module_ids: vec!["demo".into()], + } + } + + #[test] + fn current_010_is_admitted_and_older_core_versions_are_rejected() { + let input = current_input(); + let catalog = Catalog::prepare(vec![input.clone()]).expect("UMF 0.10 is supported"); + assert_eq!(catalog.pins()[0].umf_version, "0.10.0"); + Catalog::prepare_security(vec![input]).expect("security preparation uses the same current core"); + + for version in ["0.7.0", "0.8.0"] { + let mut document: Value = serde_json::from_str(¤t_input().document_json).unwrap(); + document["umf"] = json!(version); + let document_json = document.to_string(); + let mut input = current_input(); + input.document_json = document_json.clone(); + input.pin.umf_version = version.into(); + input.pin.sha256 = sha256(document_json.as_bytes()); + let error = Catalog::prepare(vec![input.clone()]).unwrap_err(); + assert_eq!(error.code, "WFT-MODEL-VERSION", "version {version}"); + assert_eq!(Catalog::prepare_security(vec![input]).unwrap_err().code, "WFT-MODEL-VERSION"); + } + } + + #[test] + fn arras_view_requires_the_exact_owning_document_declaration() { + let mut input_a = current_input(); + let mut document_a: Value = serde_json::from_str(&input_a.document_json).unwrap(); + document_a["vocabularies"]["arras.view"] = json!({"version":"0.1.0"}); + let field_a = document_a["modules"][0]["elements"].as_array_mut().unwrap() + .iter_mut().find(|element| element["kind"] == "field").unwrap(); + field_a["extensions"]["arras.view"] = json!({"opaque":"preserved"}); + input_a.document_json = document_a.to_string(); + input_a.pin.sha256 = sha256(input_a.document_json.as_bytes()); + + let mut input_b = input_a.clone(); + let mut document_b: Value = serde_json::from_str(&input_b.document_json).unwrap(); + document_b["id"] = json!("second-document"); + document_b["vocabularies"].as_object_mut().unwrap().remove("arras.view"); + input_b.pin.document_id = "second-document".into(); + let field_b = document_b["modules"][0]["elements"].as_array_mut().unwrap() + .iter_mut().find(|element| element["kind"] == "field").unwrap(); + field_b["extensions"]["arras.view"] = json!({"opaque":"belongs to B"}); + input_b.document_json = document_b.to_string(); + input_b.pin.sha256 = sha256(input_b.document_json.as_bytes()); + + let catalog = Catalog::prepare_raw_read010(vec![input_a.clone(), input_b.clone()]).unwrap(); + let field_a = &catalog.documents[0]["modules"][0]["elements"].as_array().unwrap() + .iter().find(|element| element["kind"] == "field").unwrap(); + assert!(catalog.validate_selected010(&input_a.pin, field_a).is_ok()); + let field_b = &catalog.documents[1]["modules"][0]["elements"].as_array().unwrap() + .iter().find(|element| element["kind"] == "field").unwrap(); + assert_eq!(catalog.validate_selected010(&input_b.pin, field_b).unwrap_err().code, "WFT-TYPE"); + + // The ordinary selected profile has no document metadata context. + let ordinary = Catalog::prepare(vec![input_a.clone()]).unwrap(); + assert_eq!(ordinary.validate_selected010(&input_a.pin, field_a).unwrap_err().code, "WFT-TYPE"); + let mut foreign_pin = input_a.pin.clone(); + foreign_pin.revision = "foreign".into(); + assert_eq!(catalog.validate_selected010(&foreign_pin, field_a).unwrap_err().code, "WFT-PIN"); + } + + #[test] + fn indexed_record_lookup_preserves_name_matching_and_ambiguity() { + let mut input = current_input(); + let mut document: Value = serde_json::from_str(&input.document_json).unwrap(); + document["modules"][0]["elements"].as_array_mut().unwrap() + .iter_mut().find(|element| element["id"] == "Note").unwrap()["name"] = json!("Display"); + input.document_json = document.to_string(); + input.pin.sha256 = sha256(input.document_json.as_bytes()); + let single = Catalog::prepare_raw_read010(vec![input.clone()]).unwrap(); + let name = |value: &str, quoted| Name { + value: value.into(), quoted, span: crate::types::Span { start: 0, end: 0 }, + }; + assert_eq!(single.record(None, &name("display", false)).unwrap().identity.element, "Note"); + assert!(single.record(None, &name("DISPLAY", false)).is_err()); + assert_eq!(single.record(None, &name("Display", true)).unwrap().identity.element, "Note"); + assert!(single.record(None, &name("display", true)).is_err()); + + let elements = document["modules"][0]["elements"].as_array_mut().unwrap(); + let mut duplicate = elements.iter().find(|element| element["id"] == "Note").unwrap().clone(); + duplicate["id"] = json!("Note2"); + duplicate["name"] = json!("DISPLAY"); + elements.push(duplicate); + input.document_json = document.to_string(); + input.pin.sha256 = sha256(input.document_json.as_bytes()); + let catalog = Catalog::prepare_raw_read010(vec![input]).unwrap(); + + assert_eq!(catalog.record(None, &name("display", false)).unwrap_err().code, "WFT-NAME-AMBIGUOUS"); + assert_eq!(catalog.record(None, &name("Display", true)).unwrap().identity.element, "Note"); + assert_eq!(catalog.record(None, &name("DISPLAY", true)).unwrap().identity.element, "Note2"); + // A declared display Name takes precedence over the identity-ID fallback. + assert!(catalog.record(None, &name("Note", true)).is_err()); + } + + #[test] + fn current_selected_content_still_refuses_unrecognized_extension_meaning() { + let mut input = current_input(); + let mut document: Value = serde_json::from_str(&input.document_json).unwrap(); + let field = document["modules"][0]["elements"].as_array_mut().unwrap() + .iter_mut().find(|element| element["id"] == "owner").unwrap(); + field["extensions"]["example.unknown"] = json!({"payload": 1}); + input.document_json = document.to_string(); + input.pin.sha256 = sha256(input.document_json.as_bytes()); + let mut catalog = Catalog::prepare(vec![input]).unwrap(); + catalog.admission.identity_name_fallback = true; + let name = Name { value: "owner".into(), quoted: true, span: crate::types::Span { start: 0, end: 0 } }; + let record_name = Name { value: "Note".into(), quoted: true, span: crate::types::Span { start: 0, end: 0 } }; + let record = catalog.record(None, &record_name).unwrap(); + assert_eq!(catalog.field(&record, &name).unwrap_err().code, "WFT-TYPE"); + } + + #[test] + fn exact_numeric_mode_remains_available_for_current_core_sources() { + let mut document: Value = serde_json::from_str(current_input().document_json.as_str()).unwrap(); + let elements = document["modules"][0]["elements"].as_array_mut().unwrap(); + for element in elements.iter_mut() { + if element["name"].is_null() { + element["name"] = element["id"].clone(); + } + } + let record = elements.iter_mut().find(|element| element["id"] == "Note").unwrap(); + record["members"].as_array_mut().unwrap().extend([ + json!({"module":"demo","element":"amount"}), + json!({"module":"demo","element":"units"}), + ]); + elements.extend([ + json!({"id":"amount","name":"amount","kind":"field","scalarType":"decimal","nullability":"required","cardinality":"one","extensions":{}}), + json!({"id":"units","name":"units","kind":"field","scalarType":"integer","nullability":"required","cardinality":"one","extensions":{}}), + ]); + let document_json = document.to_string(); + let input = ModuleInput { + pin: ModelPin { + document_id: document["id"].as_str().unwrap().to_owned(), + revision: "numeric-current".into(), + umf_version: "0.10.0".into(), + sha256: sha256(document_json.as_bytes()), + }, + document_json, + selected_module_ids: vec!["demo".into()], + }; + let identity = Identity { + document_id: input.pin.document_id.clone(), + revision: input.pin.revision.clone(), + module: "demo".into(), + element: "Note".into(), + }; + let default = Catalog::prepare(vec![input.clone()]).unwrap(); + assert!(default.entity_descriptor(&default.record_by_identity(&identity).unwrap()).is_err()); + let exact = Catalog::prepare_exact_numbers(vec![input.clone()]).unwrap(); + let graph = exact.entity_descriptor(&exact.record_by_identity(&identity).unwrap()).unwrap().graph; + assert_eq!(exact.inputs(), &[input]); + for id in ["amount", "units"] { + let descriptor = graph.iter().find(|d| d.identity.element == id).unwrap(); + let crate::descriptor::Shape::Scalar { logical_type } = &descriptor.shape else { panic!() }; + assert_eq!(logical_type.facets, json!({})); + let value = if id == "amount" { "9007199254740993.1234567890123456789000" } else { "900719925474099312345678901234567890" }; + crate::exact::check_literal(&crate::exact::Literal { value: value.into(), kind: crate::exact::LiteralKind::Number, span: crate::types::Span { start: 0, end: 0 } }, logical_type).unwrap(); + } + } + + fn opt_in_input() -> (ModuleInput, Identity) { + let mut document: Value = serde_json::from_str(current_input().document_json.as_str()).unwrap(); + let elements = document["modules"][0]["elements"].as_array_mut().unwrap(); + for element in elements.iter_mut() { + if element["name"].is_null() { element["name"] = element["id"].clone(); } + } + let record = elements.iter_mut().find(|element| element["id"] == "Note").unwrap(); + record["members"].as_array_mut().unwrap().extend([ + json!({"module":"demo","element":"seen"}), + json!({"module":"demo","element":"score"}), + json!({"module":"demo","element":"memo"}), + ]); + elements.extend([ + json!({"id":"seen","name":"seen","kind":"field","scalarType":"timestamp","nullability":"required","cardinality":"one","extensions":{}}), + json!({"id":"score","name":"score","kind":"field","scalarType":"float","nullability":"absent-allowed","cardinality":"one","extensions":{}}), + json!({"id":"memo","name":"memo","kind":"field","scalarType":"string","nullability":"absent-allowed","cardinality":"one","extensions":{}}), + ]); + let document_json = document.to_string(); + let input = ModuleInput { + pin: ModelPin { + document_id: document["id"].as_str().unwrap().to_owned(), + revision: "opt-in".into(), + umf_version: "0.10.0".into(), + sha256: sha256(document_json.as_bytes()), + }, + document_json, + selected_module_ids: vec!["demo".into()], + }; + let identity = Identity { document_id: input.pin.document_id.clone(), revision: "opt-in".into(), module: "demo".into(), element: "Note".into() }; + (input, identity) + } + + #[test] + fn core_typing_refuses_timestamps_floats_and_nullable_fields() { + let (input, identity) = opt_in_input(); + let catalog = Catalog::prepare(vec![input.clone()]).unwrap(); + let record = catalog.record_by_identity(&identity).unwrap(); + for field in ["seen", "score", "memo"] { + assert_eq!(catalog.field(&record, &Name::quoted(field)).unwrap_err().code, "WFT-TYPE", "{field}"); + } + // Even with nullable admission, the core family set cannot name the new families. + let nullable = Catalog::prepare(vec![input]).unwrap().admit(|a| a.nullable_fields = true); + let record = nullable.record_by_identity(&identity).unwrap(); + assert!(nullable.field(&record, &Name::quoted("memo")).unwrap().1.nullable); + for field in ["seen", "score"] { + assert_eq!(nullable.field(&record, &Name::quoted(field)).unwrap_err().code, "WFT-TYPE", "{field}"); + } + assert_eq!(nullable.entity_descriptor(&record).unwrap_err().code, "WFT-TYPE"); + assert_eq!(nullable.entity_descriptor_as::(&record).unwrap().graph.len(), nullable.record_members(&record).unwrap().len() + 1); + } + + #[test] + fn opt_in_family_set_types_timestamps_floats_and_nullable_fields() { + let (input, identity) = opt_in_input(); + let mut admission = Admission::default(); + admission.nullable_fields = true; + let catalog = Catalog::prepare_with(vec![input.clone()], admission).unwrap(); + let record = catalog.record_by_identity(&identity).unwrap(); + let typed = |field: &str| catalog.field_as::(&record, &Name::quoted(field)).unwrap().1; + assert_eq!(typed("seen"), crate::types::ScalarType::plain(ScalarFamily::Timestamp, false)); + assert_eq!(typed("score"), crate::types::ScalarType::plain(ScalarFamily::Float, true)); + assert_eq!(typed("memo"), crate::types::ScalarType::plain(ScalarFamily::String, true)); + let graph = catalog.entity_descriptor_as::(&record).unwrap().graph; + let shape = |id: &str| &graph.iter().find(|d| d.identity.element == id).unwrap().shape; + assert!(matches!(shape("seen"), crate::descriptor::Shape::Scalar { logical_type } if logical_type.family == ScalarFamily::Timestamp && !logical_type.nullable)); + assert!(matches!(shape("score"), crate::descriptor::Shape::Scalar { logical_type } if logical_type.family == ScalarFamily::Float && logical_type.nullable)); + // Without nullable admission, absent-allowed Fields still refuse. + let strict = Catalog::prepare(vec![input]).unwrap(); + let record = strict.record_by_identity(&identity).unwrap(); + assert_eq!(strict.field_as::(&record, &Name::quoted("score")).unwrap_err().code, "WFT-TYPE"); + assert_eq!(strict.field_as::(&record, &Name::quoted("seen")).unwrap().1.family, ScalarFamily::Timestamp); + } + + #[test] + fn retained_sources_verify() { + let catalog = Catalog::prepare(vec![current_input()]).unwrap(); + assert!(catalog.verify_retained_sources()); + let mut tampered = catalog.clone(); + tampered.documents[0]["modules"][0]["elements"][0]["name"] = json!("changed"); + assert!(!tampered.verify_retained_sources()); + } + + #[test] + fn derived_commerce_path_successor_passes_the_current_core_schema() { + let document_json = include_str!("../../../tests/fixtures/umf010/commerce-paths.json").to_owned(); + let document: Value = serde_json::from_str(&document_json).unwrap(); + let catalog = Catalog::prepare(vec![ModuleInput { + pin: ModelPin { + document_id: document["id"].as_str().unwrap().to_owned(), + revision: "commerce-paths-010".into(), + umf_version: "0.10.0".into(), + sha256: sha256(document_json.as_bytes()), + }, + document_json, + selected_module_ids: vec!["domain".into()], + }]).expect("the adapted test source validates under current UMF core"); + assert_eq!(catalog.pins()[0].umf_version, "0.10.0"); + } +} diff --git a/crates/weft-model/src/core_literal.rs b/crates/weft-model/src/core_literal.rs new file mode 100644 index 00000000..7f27ce68 --- /dev/null +++ b/crates/weft-model/src/core_literal.rs @@ -0,0 +1,212 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +//! UMF CoreLiteral admission against a selected Field's value domain: exact +//! scalar literals, facets, ranges, allowed values, examples and defaults. +//! Diagnostic codes `WFT-SECURITY-LITERAL` and `WFT-SECURITY-ONTOLOGY` are the +//! historical codes of weft's security subset, where this module started; they +//! are kept so existing reports do not change. No native authorization. +use crate::diagnostic::{Diagnostic,Result}; +use num_bigint::{BigInt,Sign}; +use serde_json::{json,Value}; +use std::collections::BTreeSet; +pub const MAX_TEXT:usize=4_000_000; +fn fail()->Diagnostic{Diagnostic::new("WFT-SECURITY-LITERAL","model","Security literal or refinement refused")} +fn ontology_fail()->Diagnostic{Diagnostic::new("WFT-SECURITY-ONTOLOGY","model","Security ontology closure refused")} +fn ontology_members(v:&Value,known:&[&str])->Result<()>{let object=v.as_object().ok_or_else(ontology_fail)?;if object.keys().any(|k|!known.contains(&k.as_str())){return Err(ontology_fail());}Ok(())} +#[derive(Debug,Clone,PartialEq,Eq,PartialOrd,Ord)] +pub enum ScalarLiteral{Boolean(bool),String(String),Binary(String),Number(BigInt)} +/// A validated scalar Field with its authored allowed-value domain normalized once. +/// Reusing this avoids reparsing an allowed-value list for every invocation value. +#[derive(Debug,Clone)] +pub struct PreparedLiteral { + base: Value, + allowed: Option>, +} +impl PreparedLiteral { + pub fn new(field:&Value)->Result{ + validate_field(field)?; + Self::from_validated(field) + } + pub fn from_validated(field:&Value)->Result{ + let base=json!({ + "kind":field["kind"], + "cardinality":field["cardinality"], + "nullability":field["nullability"], + "scalarType":field["scalarType"], + "facets":normalized_facets(field)?, + }); + let authored=field.get("allowedValues").cloned(); + let allowed=if let Some(values)=authored { + let values=values.as_array().ok_or_else(fail)?; + let mut identities=BTreeSet::new(); + for value in values { + let identity=scalar(&base,value,true)?.ok_or_else(fail)?; + if !identities.insert(identity){return Err(fail());} + } + Some(identities) + } else {None}; + Ok(Self{base,allowed}) + } + pub fn check(&self,value:&Value)->Result<()>{ + if let Some(identity)=scalar(&self.base,value,true)? { + if self.allowed.as_ref().is_some_and(|allowed|!allowed.contains(&identity)){return Err(fail());} + } + Ok(()) + } + + /// Export the already validated, normalized operands needed by the closed + /// generated query codec VM. This does not emit source text or attest to a + /// trusted build; it serializes only the constraints this owner has already + /// admitted and compiled. + pub fn generated_query_refinements(&self)->Result{ + let facets=&self.base["facets"]; + let range=if let Some(range)=facets.get("range"){ + let min=range.get("min").map(|value|numeric(&self.base,value).map(|number|number.to_str_radix(10))).transpose()?; + let max=range.get("max").map(|value|numeric(&self.base,value).map(|number|number.to_str_radix(10))).transpose()?; + json!({ + "minCoefficient":min, + "minInclusive":range.get("minInclusive").and_then(Value::as_bool).unwrap_or(true), + "maxCoefficient":max, + "maxInclusive":range.get("maxInclusive").and_then(Value::as_bool).unwrap_or(true), + }) + }else{Value::Null}; + let allowed=self.allowed.as_ref().map(|values|values.iter().map(|value|match value{ + ScalarLiteral::Boolean(value)=>json!({"kind":"boolean","value":value}), + ScalarLiteral::String(value)=>json!({"kind":"string","value":value}), + ScalarLiteral::Binary(value)=>json!({"kind":"binaryHex","value":value}), + ScalarLiteral::Number(value)=>json!({"kind":"numberCoefficient","value":value.to_str_radix(10)}), + }).collect::>()); + Ok(json!({ + "length":facets.get("length").cloned().unwrap_or(Value::Null), + "integerWidth":facets.get("integerWidth").cloned().unwrap_or(Value::Null), + "range":range, + "allowedIdentities":allowed, + })) + } +} +/// Exact decimal coefficient ([`crate::exact::coefficient`]) with this module's diagnostic. +pub fn coefficient(token:&str,scale:u64,precision:Option)->Result{ + crate::exact::coefficient(token,scale,precision).ok_or_else(fail) +} +pub fn unsigned(v:&Value)->Result{ + if !v.is_number(){return Err(fail());}let text=v.to_string();let n=coefficient(&text,0,None)?; + if n.sign()==Sign::Minus||text.starts_with('-')&&n==BigInt::from(0){return Err(fail());} + let (_,words)=n.to_u64_digits();if words.len()>1{return Err(fail());}let value=words.first().copied().unwrap_or(0);if value>9_007_199_254_740_991{return Err(fail());}Ok(value) +} +pub fn normalized_facets(field:&Value)->Result{ + let mut facets=field.get("facets").cloned().unwrap_or(json!({})); + for key in ["precision","scale"]{if let Some(value)=facets.get(key){let n=unsigned(value)?;facets[key]=json!(n);}} + for (group,keys) in [("length",&["min","max"][..]),("integerWidth",&["bits"][..])]{for key in keys{if let Some(value)=facets.get(group).and_then(|g|g.get(*key)){let n=unsigned(value)?;facets[group][*key]=json!(n);}}} + Ok(facets) +} +fn numeric(field:&Value,value:&Value)->Result{ + let facets=normalized_facets(field)?;let kind=field["scalarType"].as_str().ok_or_else(fail)?; + let wrapper=if kind=="integer"{"integerToken"}else if kind=="decimal"{"decimalToken"}else{return Err(fail());}; + let scale=if kind=="decimal"{unsigned(&facets["scale"])?}else{0};let precision=if kind=="decimal"{Some(unsigned(&facets["precision"])?)}else{None}; + coefficient(value[wrapper].as_str().ok_or_else(fail)?,scale,precision) +} +fn power_of_two(n:&BigInt)->bool{n.magnitude().to_u64_digits().iter().map(|n|n.count_ones() as u64).sum::()==1} +fn fits_width(n:&BigInt,width:&Value)->Result<()>{ + let bits=unsigned(&width["bits"])?;if bits==0{return Err(fail());}let magnitude=n.magnitude().bits();let negative=n.sign()==Sign::Minus; + let signed=width["signed"].as_bool().ok_or_else(fail)?; + if if signed{if negative{magnitudeResult>{ + if field["kind"]!="field"||field["cardinality"]!="one"{return Err(fail());} + if value.is_null(){return if field["nullability"]=="absent-allowed"{Ok(None)}else{Err(fail())};} + if value.as_object().is_none_or(|m|m.len()!=1){return Err(fail());} + let facets=normalized_facets(field)?;let kind=field["scalarType"].as_str().ok_or_else(fail)?; + let (identity,length)=match kind{ + "boolean"=>(ScalarLiteral::Boolean(value["boolean"].as_bool().ok_or_else(fail)?),None), + "string"=>{let text=value["string"].as_str().ok_or_else(fail)?;(ScalarLiteral::String(text.into()),Some(text.chars().count() as u64))}, + "binary"=>{let text=value["binaryHex"].as_str().ok_or_else(fail)?;if text.len()%2!=0||!text.bytes().all(|b|b.is_ascii_hexdigit()){return Err(fail());}(ScalarLiteral::Binary(text.to_ascii_lowercase()),Some(text.len() as u64/2))}, + "integer"|"decimal"=>{let n=numeric(field,value)?;if let Some(width)=facets.get("integerWidth"){fits_width(&n,width)?;} + if refinements{if let Some(range)=facets.get("range"){for (end,lower) in [("min",true),("max",false)]{if let Some(bound)=range.get(end){let b=numeric(field,bound)?;if if lower{nb||n==b&&range["maxInclusive"]==false}{return Err(fail());}}}}} + (ScalarLiteral::Number(n),None) + },_=>return Err(fail()) + }; + if let Some(bounds)=facets.get("length"){ + let length=length.ok_or_else(fail)?;let expected=if kind=="string"{"unicode-scalar"}else{"byte"};if bounds["unit"]!=expected{return Err(fail());} + if bounds.get("min").is_some_and(|v|unsigned(v).is_err()||lengthunsigned(v).unwrap()){return Err(fail());} + } + if refinements{if let Some(allowed)=field["allowedValues"].as_array(){let mut matched=false;for candidate in allowed{if scalar(field,candidate,false)?.as_ref()==Some(&identity){matched=true;break;}}if !matched{return Err(fail());}}} + Ok(Some(identity)) +} +pub fn check_literal(field:&Value,value:&Value)->Result<()>{field_domain(field)?;scalar(field,value,true).map(|_|())} +fn at_extreme(field:&Value,n:&BigInt,upper:bool)->Result{ + let f=normalized_facets(field)?;if field["scalarType"]=="integer"{ + let Some(w)=f.get("integerWidth") else{return Ok(false);};let bits=unsigned(&w["bits"])?;let signed=w["signed"].as_bool().ok_or_else(fail)?; + if !upper{return Ok(if signed{n.sign()==Sign::Minus&&n.magnitude().bits()==bits&&power_of_two(n)}else{*n==BigInt::from(0)});} + let expected=if signed{bits-1}else{bits};let ones=n.magnitude().to_u64_digits().iter().map(|w|w.count_ones() as u64).sum::(); + return Ok(n.sign()!=Sign::Minus&&n.magnitude().bits()==expected&&ones==expected); + } + let precision=unsigned(&f["precision"])?;let text=n.to_str_radix(10);let digits=text.trim_start_matches('-'); + Ok((if upper{n.sign()!=Sign::Minus}else{n.sign()==Sign::Minus})&&digits.len() as u64==precision&&digits.bytes().all(|b|b==b'9')) +} +pub fn validate_field(field:&Value)->Result<()>{ + field_domain(field)?; + let facets=normalized_facets(field)?; + if let Some(length)=facets.get("length"){if length.get("min").is_some()&&length.get("max").is_some()&&unsigned(&length["min"])? > unsigned(&length["max"])?{return Err(fail());}} + if let Some(range)=facets.get("range"){ + let mut base=field.clone();base["facets"].as_object_mut().ok_or_else(fail)?.remove("range");base.as_object_mut().unwrap().remove("allowedValues"); + for (end,flag) in [("min","minInclusive"),("max","maxInclusive")]{if let Some(bound)=range.get(end){if bound.is_null(){return Err(fail());}scalar(&base,bound,false)?;}else if range.get(flag).is_some(){return Err(fail());}} + if range.get("min").is_some()&&range["minInclusive"]==false&&at_extreme(field,&numeric(field,&range["min"])?,true)?{return Err(fail());} + if range.get("max").is_some()&&range["maxInclusive"]==false&&at_extreme(field,&numeric(field,&range["max"])?,false)?{return Err(fail());} + if range.get("min").is_some()&&range.get("max").is_some(){let min=numeric(field,&range["min"])?;let max=numeric(field,&range["max"])?;let low=min+BigInt::from(if range["minInclusive"]==false{1}else{0});let high=max-BigInt::from(if range["maxInclusive"]==false{1}else{0});if low>high{return Err(fail());}} + } + if let Some(allowed)=field["allowedValues"].as_array(){let mut base=field.clone();base.as_object_mut().unwrap().remove("allowedValues");let mut unique=BTreeSet::new();for value in allowed{let identity=scalar(&base,value,true)?.ok_or_else(fail)?;if !unique.insert(identity){return Err(fail());}}} + if let Some(examples)=field["examples"].as_array(){for value in examples{scalar(field,value,false)?;}} + if let Some(default)=field.get("default"){if default.as_object().is_none_or(|m|m.keys().any(|k|k!="on"&&k!="value"))||!matches!(default["on"].as_str(),Some("missing"|"null"|"missing-or-null")){return Err(fail());}check_literal(field,&default["value"])?;} + Ok(()) +} + +pub fn normalized_literal(field:&Value,value:&Value)->Result>{validate_field(field)?;scalar(field,value,true)} + +/// Normalize a selected scalar Field's UMF value domain (scalar type, cardinality, +/// nullability, facets, allowed values), or refuse it. +pub fn field_domain(field:&Value)->Result{ + ontology_members(field,&["id","kind","name","description","extensions","scalarType","nullability","cardinality","facets","references","title","aliases","examples","allowedValues","default"])?; + if field["kind"]!="field"||field["cardinality"]!="one"||!matches!(field["scalarType"].as_str(),Some("boolean"|"string"|"integer"|"decimal"|"binary"))||field["references"].as_array().is_some_and(|r|!r.is_empty()){return Err(ontology_fail());} + let facets=normalized_facets(field)?; + ontology_members(&facets,&["length","precision","scale","integerWidth","range","collectionSize"])?; + for (group,keys) in [("length",&["min","max","unit"][..]),("integerWidth",&["bits","signed"][..]),("range",&["min","max","minInclusive","maxInclusive"][..]),("collectionSize",&["min","max"][..])]{if let Some(value)=facets.get(group){ontology_members(value,keys)?;}} + if !matches!(field["nullability"].as_str(),Some("required"|"absent-allowed"|"unspecified")){return Err(ontology_fail());} + let kind=field["scalarType"].as_str().unwrap(); + if facets.get("collectionSize").is_some()||((facets.get("precision").is_some()||facets.get("scale").is_some())&&kind!="decimal")||(facets.get("integerWidth").is_some()&&kind!="integer")||(facets.get("range").is_some()&&!matches!(kind,"integer"|"decimal")){return Err(ontology_fail());} + if let Some(length)=facets.get("length"){ + if length["unit"]!=if kind=="string"{"unicode-scalar"}else if kind=="binary"{"byte"}else{return Err(ontology_fail());}{return Err(ontology_fail());} + } + if let Some(width)=facets.get("integerWidth"){ + let bits=width["bits"].as_u64().ok_or_else(ontology_fail)?;if bits==0||bits>9_007_199_254_740_991||!width["signed"].is_boolean(){return Err(ontology_fail());} + } + if field["scalarType"]=="decimal"{ + let precision=facets["precision"].as_u64().ok_or_else(ontology_fail)?;let scale=facets["scale"].as_u64().ok_or_else(ontology_fail)?; + if precision==0||precision>9_007_199_254_740_991||scale>precision{return Err(ontology_fail());} + } + Ok(json!({"scalarType":field["scalarType"],"cardinality":field["cardinality"],"nullability":field["nullability"],"facets":facets,"allowedValues":field.get("allowedValues").unwrap_or(&Value::Null)})) +} + +#[cfg(test)] +mod tests { + use super::*; + fn field(scalar: &str, facets: Value) -> Value { + json!({"id":"f","kind":"field","scalarType":scalar,"cardinality":"one","nullability":"required","facets":facets}) + } + #[test] + fn literals_are_checked_against_the_field_domain() { + let small = field("integer", json!({"integerWidth":{"bits":8,"signed":true},"range":{"min":{"integerToken":"-5"},"max":{"integerToken":"5"},"maxInclusive":false}})); + validate_field(&small).unwrap(); + check_literal(&small, &json!({"integerToken":"-5"})).unwrap(); + for bad in [json!({"integerToken":"5"}), json!({"integerToken":"1.5"}), json!({"string":"1"}), Value::Null] { + assert_eq!(check_literal(&small, &bad).unwrap_err().code, "WFT-SECURITY-LITERAL", "{bad}"); + } + let money = field("decimal", json!({"precision":4,"scale":2})); + check_literal(&money, &json!({"decimalToken":"99.99"})).unwrap(); + assert!(check_literal(&money, &json!({"decimalToken":"100.00"})).is_err()); + let mut tagged = field("string", json!({"length":{"max":3,"unit":"unicode-scalar"}})); + tagged["allowedValues"] = json!([{"string":"a"},{"string":"bc"}]); + let prepared = PreparedLiteral::new(&tagged).unwrap(); + prepared.check(&json!({"string":"bc"})).unwrap(); + assert!(prepared.check(&json!({"string":"x"})).is_err()); + assert_eq!(field_domain(&field("float", json!({}))).unwrap_err().code, "WFT-SECURITY-ONTOLOGY"); + } +} diff --git a/crates/weft-model/src/descriptor.rs b/crates/weft-model/src/descriptor.rs new file mode 100644 index 00000000..43669a99 --- /dev/null +++ b/crates/weft-model/src/descriptor.rs @@ -0,0 +1,613 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +//! Identity-based application descriptors; native encodings remain backend obligations. +use crate::{ + diagnostic::{Diagnostic, Result}, + types::{Family, FamilySet, Identity, LogicalType, Span}, + catalog::{scalar_type_with, Catalog, Record, ScalarRules}, + name::Name, +}; +use serde::Serialize; +use serde_json::Value; +use std::collections::BTreeSet; +#[derive(Debug, Clone, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct Member { + pub name: String, + pub identity: Identity, +} +/// A descriptor's value shape. `F` is the admitted family set (see +/// [`crate::types::FamilySet`]); weft's profiles use the default [`Family`]. +#[derive(Debug, Clone, Serialize)] +#[serde(tag = "kind", rename_all = "camelCase")] +pub enum Shape { + /// Projection-only exact token. This is not a comparable LogicalType. + ExactScalar { + #[serde(rename = "scalarType")] + scalar_type: ExactScalarType, + }, + Scalar { + #[serde(rename = "type")] + logical_type: LogicalType, + }, + Sequence { + item: Identity, + }, + Map { + item: Identity, + }, + Structured { + record: Identity, + }, + Record { + members: Vec, + }, +} +#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub enum ExactScalarType { Timestamp, Binary, Date } +#[derive(Debug, Clone, Serialize)] +pub struct Descriptor { + pub identity: Identity, + pub availability: Option, + #[serde(flatten)] + pub shape: Shape, +} +#[derive(Debug, Clone, Serialize)] +pub struct EntityDescriptor { + pub members: Vec, + pub graph: Vec>, +} +#[derive(Debug, Clone, Serialize)] +pub struct AuthoredKey { + pub id: String, + pub fields: Vec, + pub types: Vec>, +} +fn fail(message: &str) -> Diagnostic { + Diagnostic::new("WFT-TYPE", "model", message) +} +impl Catalog { + pub fn local_element( + &self, + record: &Record, + reference: &Value, + ) -> Result<(Identity, Value)> { + let (identity, element) = self.local_element_metadata(record, reference)?; + self.validate_selected010(&record.pin, element)?; + Ok((identity, element.clone())) + } + fn local_element_metadata<'a>( + &'a self, + record: &Record, + reference: &Value, + ) -> Result<(Identity, &'a Value)> { + let mid = reference["module"] + .as_str() + .ok_or_else(|| fail("Missing local module reference"))?; + let eid = reference["element"] + .as_str() + .ok_or_else(|| fail("Missing local element reference"))?; + let element = self.local_element_by_ids(record.document, mid, eid) + .ok_or_else(|| fail("Unresolved local type dependency"))?; + let mut identity = record.identity.clone(); + identity.module = mid.into(); + identity.element = eid.into(); + Ok((identity, element)) + } + /// Count authored names before interpreting any matching Field's value meaning. + pub fn member_name_matches(&self, record: &Record, name: &Name) -> Result { + let refs = record.value["members"].as_array() + .ok_or_else(|| fail("Record needs explicit ordered members"))?; + let mut count = 0; + for reference in refs { + let (_, field) = self.local_element_metadata(record, reference)?; + if field["kind"] != "field" { return Err(fail("Record member must reference a Field")); } + let authored = self.query_name(&field) + .ok_or_else(|| fail("Selected Field needs an authored name"))?; + count += usize::from(name.matches(authored)); + } + Ok(count) + } + fn members(&self, record: &Record, value: &Value) -> Result> { + let refs = value["members"] + .as_array() + .ok_or_else(|| fail("Record needs explicit ordered members"))?; + let mut ids = BTreeSet::new(); + let mut members = Vec::new(); + for r in refs { + let (identity, field) = self.local_element(record, r)?; + if field["kind"] != "field" { + return Err(fail("Record member must reference a Field")); + } + if !ids.insert((identity.module.clone(), identity.element.clone())) { + return Err(fail("Repeated member identity")); + } + let name = self.query_name(&field) + .ok_or_else(|| fail("Selected Field needs an authored name"))? + .into(); + members.push(Member { name, identity }); + } + Ok(members) + } + /// Original ordered membership metadata, without interpreting unselected values. + pub fn record_members(&self, record: &Record) -> Result> { + self.members(record, &record.value) + } + pub fn entity_descriptor(&self, record: &Record) -> Result { + self.entity_descriptor_as(record) + } + /// [`Catalog::entity_descriptor`] over an explicit family set. + pub fn entity_descriptor_as(&self, record: &Record) -> Result> { + Ok(EntityDescriptor { + members: self.members(record, &record.value)?, + graph: self.type_graph(record, record.identity.clone(), record.value.clone())?, + }) + } + pub fn member_descriptor( + &self, + record: &Record, + name: &Name, + ) -> Result<(Member, Vec)> { + self.member_descriptor_as(record, name) + } + /// [`Catalog::member_descriptor`] over an explicit family set. + pub fn member_descriptor_as( + &self, + record: &Record, + name: &Name, + ) -> Result<(Member, Vec>)> { + let matches: Vec<_> = self + .members(record, &record.value)? + .into_iter() + .filter(|m| name.matches(&m.name)) + .collect(); + if matches.len() != 1 { + return Err(Diagnostic::new( + if matches.is_empty() { + "WFT-NAME-MISSING" + } else { + "WFT-NAME-AMBIGUOUS" + }, + "resolve", + "Selected member name missing or ambiguous", + ).column(matches.len()>1) + .at(&name.span)); + } + let member = matches.into_iter().next().unwrap(); + let (_, v) = self.local_element( + record, + &serde_json::json!({"module":member.identity.module,"element":member.identity.element}), + )?; + let graph = self.type_graph(record, member.identity.clone(), v)?; + Ok((member, graph)) + } + /// Exact authored membership lookup for backend admission after resolution. + pub fn member_descriptor_by_identity( + &self, + record: &Record, + identity: &Identity, + ) -> Result<(Member, Vec)> { + self.member_descriptor_by_identity_as(record, identity) + } + /// [`Catalog::member_descriptor_by_identity`] over an explicit family set. + pub fn member_descriptor_by_identity_as( + &self, + record: &Record, + identity: &Identity, + ) -> Result<(Member, Vec>)> { + let mut matches = self + .members(record, &record.value)? + .into_iter() + .filter(|m| &m.identity == identity); + let member = matches + .next() + .ok_or_else(|| fail("Original Record does not own selected member identity"))?; + if matches.next().is_some() { + return Err(fail("Original member identity is ambiguous")); + } + let (_, value) = self.local_element( + record, + &serde_json::json!({"module":identity.module,"element":identity.element}), + )?; + let graph = self.type_graph(record, identity.clone(), value)?; + Ok((member, graph)) + } + fn type_graph( + &self, + record: &Record, + identity: Identity, + value: Value, + ) -> Result>> { + fn visit( + c: &Catalog, + r: &Record, + id: Identity, + v: Value, + seen: &mut BTreeSet<(String, String)>, + graph: &mut Vec>, + depth: usize, + ) -> Result<()> { + if !seen.insert((id.module.clone(), id.element.clone())) { + return Ok(()); + } + if depth > 128 { + return Err(Diagnostic::new( + "WFT-LIMIT", + "model", + "Selected type graph exceeds depth 128", + )); + } + if seen.len() > 4096 { + return Err(Diagnostic::new( + "WFT-LIMIT", + "model", + "Selected type graph exceeds 4096 identities", + )); + } + let mut dependencies = Vec::new(); + let (availability, shape) = if v["kind"] == "record" { + let members = c.members(r, &v)?; + for m in &members { + dependencies.push(c.local_element(r,&serde_json::json!({"module":m.identity.module,"element":m.identity.element}))?); + } + (None, Shape::Record { members }) + } else if v["kind"] == "field" { + let availability = v["nullability"] + .as_str() + .filter(|s| matches!(*s, "required" | "absent-allowed")) + .ok_or_else(|| fail("Selected Field availability is not established"))? + .to_string(); + let references = v + .get("references") + .and_then(Value::as_array) + .cloned() + .unwrap_or_default(); + let shape = match v["cardinality"].as_str() { + Some("array" | "map") => { + if !references.is_empty() || v.get("scalarType").is_some() { + return Err(fail( + "Container cannot imply scalar or structured meaning", + )); + } + let dep = c.local_element(r, &v["itemType"])?; + if dep.1["kind"] != "field" { + return Err(fail("Container itemType must reference a Field")); + } + let item = dep.0.clone(); + dependencies.push(dep); + if v["cardinality"] == "array" { + Shape::Sequence { item } + } else { + Shape::Map { item } + } + } + Some("one") + if references.len() == 1 && references[0]["role"] == "record-type" => + { + let dep = c.local_element(r, &references[0])?; + if dep.1["kind"] != "record" || v.get("scalarType").is_some() { + return Err(fail( + "Structured Field needs a sole record-type reference", + )); + } + let record = dep.0.clone(); + dependencies.push(dep); + Shape::Structured { record } + } + Some("one") => { + let mut required = v.clone(); + required["nullability"] = Value::String("required".into()); + let name = Name { + value: v["name"].as_str().unwrap_or("").into(), + quoted: true, + span: Span { start: 0, end: 0 }, + }; + if c.admission().exact_scalars && (matches!(v["scalarType"].as_str(), Some("timestamp" | "binary")) || c.admission().exact_dates && r.pin.umf_version=="0.10.0" && v["scalarType"]=="date") { + if !references.is_empty() || v.get("itemType").is_some() || v.get("recordType").is_some() { + return Err(fail("Exact scalar cannot carry references or item meaning")); + } + Shape::ExactScalar { scalar_type: if v["scalarType"] == "timestamp" { + ExactScalarType::Timestamp + } else if v["scalarType"]=="date" { ExactScalarType::Date } else { ExactScalarType::Binary } } + } else { + Shape::Scalar { + logical_type: {let mut ty=scalar_type_with(&required, &name, ScalarRules { nullable_fields: false, ..c.admission().scalar_rules() })?;if (c.admission().sdk_scalar_presence || c.admission().nullable_fields) && availability=="absent-allowed" {ty.nullable=true;}ty}, + } + } + } + _ => return Err(fail("Selected cardinality is unsupported")), + }; + if !matches!(shape, Shape::Scalar { .. }) + && v.get("facets") + .and_then(Value::as_object) + .is_some_and(|f| !f.is_empty()) + { + return Err(fail( + "Selected container or structured facets are unsupported", + )); + } + (Some(availability), shape) + } else { + return Err(fail( + "Selected type graph contains an unsupported element role", + )); + }; + graph.push(Descriptor { + identity: id, + availability, + shape, + }); + for (id, v) in dependencies { + visit(c, r, id, v, seen, graph, depth + 1)?; + } + Ok(()) + } + let mut graph = Vec::new(); + visit( + self, + record, + identity, + value, + &mut BTreeSet::new(), + &mut graph, + 0, + )?; + Ok(graph) + } + pub fn authored_key(&self, record: &Record, key_id: &str) -> Result { + self.authored_key_as(record, key_id) + } + /// [`Catalog::authored_key`] over an explicit family set. + pub fn authored_key_as(&self, record: &Record, key_id: &str) -> Result> { + let keys = record.value["keys"] + .as_array() + .ok_or_else(|| fail("Record has no authored unique key"))?; + let matched: Vec<_> = keys.iter().filter(|k| k["id"] == key_id).collect(); + if matched.len() != 1 { + return Err(fail("Authored key identity is missing or ambiguous")); + } + let members = self.members(record, &record.value)?; + let mut fields = Vec::new(); + let mut types = Vec::new(); + let mut seen = BTreeSet::new(); + for reference in matched[0]["fields"].as_array().unwrap() { + let (id, field) = self.local_element(record, reference)?; + if !members.iter().any(|m| m.identity == id) + || !seen.insert((id.module.clone(), id.element.clone())) + { + return Err(fail("Key must contain distinct declared member Fields")); + } + let name = Name { + value: self.query_name(&field).unwrap_or("").into(), + quoted: true, + span: Span { start: 0, end: 0 }, + }; + types.push(scalar_type_with(&field, &name, ScalarRules { decimal38: false, nullable_fields: false, ..self.admission().scalar_rules() })?); + fields.push(id); + } + Ok(AuthoredKey { + id: key_id.into(), + fields, + types, + }) + } +} +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct RelationshipIdentity { + pub document_id: String, + pub revision: String, + pub module: String, + pub relationship: String, +} +#[derive(Debug, Clone, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct RelationshipRead { + pub identity: RelationshipIdentity, + pub inverse: bool, + pub from: Identity, + pub to: Identity, + pub source_key: AuthoredKey, + pub target_key: AuthoredKey, + pub source_multiplicity: Value, + pub target_multiplicity: Value, + pub target_lifecycle: String, +} +impl Catalog { + pub fn entity_relationships(&self, source: &Record) -> Result> { + let mut names = Vec::new(); + for module in self.documents[source.document]["modules"].as_array().unwrap() { + if !self.inputs()[source.document].selected_module_ids.iter().any(|id| module["id"] == id.as_str()) { continue; } + let Some(relationships) = module["relationships"].as_array() else { continue; }; + for relationship in relationships { + for (side, label) in [("source", "name"), ("target", "inverse")] { + if let Some(name) = relationship[label].as_str() { + if relationship[side].as_array().unwrap().iter().any(|endpoint| + endpoint["module"] == source.identity.module && endpoint["element"] == source.identity.element) + { + names.push(name.to_owned()); + } + } + } + } + } + names.into_iter().map(|value| { + let name = Name { value: value.clone(), quoted: true, span: Span { start: 0, end: 0 } }; + self.relationship_read(source, &name).map(|read| (value, read)) + }).collect() + } + pub fn relationship_read(&self, source: &Record, name: &Name) -> Result { + let mut matches = Vec::new(); + for module in self.documents[source.document]["modules"] + .as_array() + .unwrap() + { + if !self.inputs()[source.document] + .selected_module_ids + .iter() + .any(|id| module["id"] == id.as_str()) + { + continue; + } + let Some(relationships) = module["relationships"].as_array() else { + continue; + }; + let mut ids = BTreeSet::new(); + for rel in relationships { + if !ids.insert(rel["id"].as_str().unwrap()) { + return Err(fail("Duplicate relationship identity")); + } + let endpoint_matches = |v: &Value| { + v["module"] == source.identity.module && v["element"] == source.identity.element + }; + let forward = name.matches(rel["name"].as_str().unwrap()) + && rel["source"] + .as_array() + .unwrap() + .iter() + .any(endpoint_matches); + let inverse = rel["inverse"].as_str().is_some_and(|n| name.matches(n)) + && rel["target"] + .as_array() + .unwrap() + .iter() + .any(endpoint_matches); + if forward { + matches.push((module, rel, false)); + } + if inverse { + matches.push((module, rel, true)); + } + } + } + if matches.len() != 1 { + return Err(Diagnostic::new( + if matches.is_empty() { + "WFT-NAME-MISSING" + } else { + "WFT-NAME-AMBIGUOUS" + }, + "resolve", + "Relationship name is missing or ambiguous", + ).column(matches.len()>1) + .at(&name.span)); + } + let (module, rel, inverse) = matches[0]; + self.relationship_read_definition(source,module,rel,inverse) + } + /// Resolve original relationship identity and direction without a name alias. + pub fn relationship_read_identity(&self,source:&Record,module_id:&str,id:&str,inverse:bool)->Result{ + let mut found=Vec::new(); + for module in self.documents[source.document]["modules"].as_array().unwrap(){ + if module["id"]!=module_id||!self.inputs()[source.document].selected_module_ids.iter().any(|m|m==module_id){continue} + if let Some(relationships)=module["relationships"].as_array(){for rel in relationships{if rel["id"]==id{found.push((module,rel));}}} + } + if found.len()!=1{return Err(fail("Original relationship identity is missing or ambiguous"))} + let(module,rel)=found[0];let side=if inverse{"target"}else{"source"}; + if !rel[side].as_array().is_some_and(|ends|ends.iter().any(|e|e["module"]==source.identity.module&&e["element"]==source.identity.element)){return Err(fail("Original relationship direction does not start at source Record"))} + self.relationship_read_definition(source,module,rel,inverse) + } + fn relationship_read_definition(&self,source:&Record,module:&Value,rel:&Value,inverse:bool)->Result{ + if rel["source"].as_array().unwrap().len() != 1 + || rel["target"].as_array().unwrap().len() != 1 + || rel["directed"] != true + || rel.get("associationRecord").is_some() + { + return Err(fail("Relationship requires a supported monomorphic directed traversal without an association Record")); + } + let lifecycle = rel["targetLifecycle"] + .as_str() + .filter(|v| matches!(*v, "owned" | "independent")) + .ok_or_else(|| fail("Relationship lifecycle meaning is unsupported"))?; + for side in ["sourceMultiplicity", "targetMultiplicity"] { + let m = &rel[side]; + let min = m["min"].as_u64().unwrap(); + if m["max"] != "*" && m["max"].as_u64().is_none_or(|max| max < min) { + return Err(fail("Relationship multiplicity is inconsistent")); + } + } + // Validate the authored target key even for inverse traversal; neither direction invents a key. + let (target_id, target_value) = self.local_element(source, &rel["target"][0])?; + if target_value["kind"] != "record" { + return Err(fail("Relationship target must be a Record")); + } + let target_record = Record { + identity: target_id.clone(), + pin: source.pin.clone(), + value: target_value, + document: source.document, + }; + let forward_key = + self.authored_key(&target_record, rel["target"][0]["key"].as_str().unwrap())?; + let (source_id, source_value) = self.local_element(source, &rel["source"][0])?; + if source_value["kind"] != "record" { + return Err(fail("Relationship source must be a Record")); + } + let from_record = Record { + identity: source_id.clone(), + pin: source.pin.clone(), + value: source_value, + document: source.document, + }; + let keys = from_record.value["keys"] + .as_array() + .ok_or_else(|| fail("Relationship source endpoint needs an authored key"))?; + let primary: Vec<_> = keys.iter().filter(|k| k["primary"] == true).collect(); + let key = if primary.len() == 1 { + primary[0] + } else if primary.is_empty() && keys.len() == 1 { + &keys[0] + } else { + return Err(fail("Relationship source endpoint key is ambiguous")); + }; + let source_key = self.authored_key(&from_record, key["id"].as_str().unwrap())?; + let (to, query_source_key, target_key) = if inverse { + (source_id.clone(), forward_key, source_key) + } else { + (target_id.clone(), source_key, forward_key) + }; + Ok(RelationshipRead { + identity: RelationshipIdentity { + document_id: source.identity.document_id.clone(), + revision: source.identity.revision.clone(), + module: module["id"].as_str().unwrap().into(), + relationship: rel["id"].as_str().unwrap().into(), + }, + inverse, + from: if inverse { target_id } else { source_id }, + to, + source_key: query_source_key, + target_key, + source_multiplicity: rel["sourceMultiplicity"].clone(), + target_multiplicity: rel["targetMultiplicity"].clone(), + target_lifecycle: lifecycle.into(), + }) + } +} + +impl Catalog { + /// Inspect a declared scalar domain, including a standalone read DTO. This + /// does not establish Record membership, a physical source or read permission. + pub fn scalar_descriptor_by_identity(&self, identity: &Identity) -> Result { + self.scalar_descriptor_by_identity_as(identity) + } + /// [`Catalog::scalar_descriptor_by_identity`] over an explicit family set. + pub fn scalar_descriptor_by_identity_as(&self, identity: &Identity) -> Result> { + let mut found = Vec::new(); + for (index,input) in self.inputs().iter().enumerate() { + if input.pin.document_id!=identity.document_id || input.pin.revision!=identity.revision {continue} + for module in self.documents[index]["modules"].as_array().ok_or_else(||fail("Original modules required"))? { + if module["id"]!=identity.module || !input.selected_module_ids.contains(&identity.module) {continue} + for element in module["elements"].as_array().ok_or_else(||fail("Original elements required"))? { + if element["id"]!=identity.element {continue} + self.validate_selected010(&input.pin,element)?; + let name=Name{value:identity.element.clone(),quoted:true,span:crate::types::Span{start:0,end:0}}; + let logical_type=scalar_type_with(element,&name,ScalarRules { decimal38: false, nullable_fields: false, ..self.admission().scalar_rules() })?; + found.push(Descriptor{identity:identity.clone(),availability:Some("required".into()),shape:Shape::Scalar{logical_type}}); + } + } + } + if found.len()!=1 {return Err(fail("Original scalar domain is missing or ambiguous"))} + Ok(found.remove(0)) + } +} diff --git a/crates/weft-model/src/diagnostic.rs b/crates/weft-model/src/diagnostic.rs new file mode 100644 index 00000000..16970bd0 --- /dev/null +++ b/crates/weft-model/src/diagnostic.rs @@ -0,0 +1,55 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +use crate::types::Span; +use serde::Serialize; +#[derive(Debug, Clone, Copy)] +pub enum ResolutionReason { + ColumnMissing, + ColumnAmbiguous, + SourceUnavailable, +} +impl ResolutionReason { + pub fn code(self) -> &'static str { + match self { + Self::ColumnMissing => "WFT-COLUMN-MISSING", + Self::ColumnAmbiguous => "WFT-COLUMN-AMBIGUOUS", + Self::SourceUnavailable => "WFT-SOURCE-UNAVAILABLE", + } + } +} +#[derive(Debug, Clone, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct Diagnostic { + pub code: String, + pub severity: String, + pub message: String, + pub phase: String, + #[serde(skip_serializing_if = "Option::is_none")] + pub source_span: Option, + #[serde(skip)] + pub resolution_reason: Option, +} +impl Diagnostic { + pub fn new(code: &str, phase: &str, message: &str) -> Self { + Self { + code: code.into(), + severity: "error".into(), + message: message.into(), + phase: phase.into(), + source_span: None, + resolution_reason: None, + } + } + pub fn at(mut self, span: &Span) -> Self { + self.source_span = Some(span.clone()); + self + } + pub fn column(mut self, ambiguous: bool) -> Self { + self.resolution_reason = Some(if ambiguous { ResolutionReason::ColumnAmbiguous } else { ResolutionReason::ColumnMissing }); + self + } + pub fn unavailable_source(mut self) -> Self { + self.resolution_reason = Some(ResolutionReason::SourceUnavailable); + self + } +} +pub type Result = std::result::Result; diff --git a/crates/weft-model/src/exact.rs b/crates/weft-model/src/exact.rs new file mode 100644 index 00000000..2ed42703 --- /dev/null +++ b/crates/weft-model/src/exact.rs @@ -0,0 +1,263 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +//! Exact literal tokens and their value-domain checks. Tokens are never converted +//! through native floats; the original text is what a compiler retains. +use crate::{ + diagnostic::{Diagnostic, Result}, + types::{FamilySet, LogicalType, ScalarFamily, Span}, +}; +use num_bigint::BigInt; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum LiteralKind { + Number, + String, + Boolean, +} +/// A source literal token: base-ten number text, string text, or `true`/`false`. +#[derive(Debug, Clone)] +pub struct Literal { + pub value: String, + pub kind: LiteralKind, + pub span: Span, +} +/// Largest token any exact check reads. +pub const MAX_TOKEN: usize = 4_000_000; + +/// Check that `input` is a value of `expected`'s family and facets. +/// +/// Core families keep their weft-sql 0.1 rules. `Timestamp` takes a string token +/// in the RFC 3339 exact-instant profile ([`crate::exact_instant`]); `Float` +/// takes a number token whose nearest binary64 value is finite. Those two +/// families exist only in [`ScalarFamily`]; weft's core [`crate::types::Family`] +/// cannot name them. +pub fn check_literal(input: &Literal, expected: &LogicalType) -> Result<()> { + let error = |code, message| Diagnostic::new(code, "type", message).at(&input.span); + match (&input.kind, expected.family.widen()) { + (LiteralKind::String, ScalarFamily::String) => { + crate::string_domain::validate_facets(&expected.facets, &input.value) + .map_err(|_| error("WFT-STRING-DOMAIN", "String literal exceeds selected domain"))?; + } + (LiteralKind::Boolean, ScalarFamily::Boolean) => {} + (LiteralKind::Number, ScalarFamily::Integer) => { + if input.value.contains('.') { + return Err(error( + "WFT-NUMERIC-DOMAIN", + "Integer literal must be integral base-ten text", + )); + } + let digits = input.value.strip_prefix('-').unwrap_or(&input.value); + if digits.is_empty() || !digits.bytes().all(|b| b.is_ascii_digit()) { return Err(error("WFT-NUMERIC-DOMAIN", "Integer literal must retain exact base-ten digits")); } + if expected.facets != serde_json::json!({}) { + let n = input.value.parse::().map_err(|_| { + error( + "WFT-NUMERIC-DOMAIN", + "Integer literal exceeds selected domain", + ) + })?; + let width = &expected.facets["integerWidth"]; + let bits = width["bits"].as_u64().unwrap(); + let (min, max) = if width["signed"] == true { + (-(1i128 << (bits - 1)), (1i128 << (bits - 1)) - 1) + } else { + (0, (1i128 << bits) - 1) + }; + if n < min || n > max { + return Err(error( + "WFT-NUMERIC-DOMAIN", + "Integer literal exceeds selected domain", + )); + } + } + } + (LiteralKind::Number, ScalarFamily::Decimal) => { + if expected.facets == serde_json::json!({}) { + validate_mathematical_decimal(&input.value).map_err(|_|error("WFT-NUMERIC-DOMAIN","Decimal literal must be exact finite base-ten text"))?; + return Ok(()); + } + let scale = expected.facets["scale"].as_u64().unwrap() as usize; + let precision = expected.facets["precision"].as_u64().unwrap() as usize; + let unsigned = input.value.strip_prefix('-').unwrap_or(&input.value); + let mut parts = unsigned.split('.'); + let whole = parts.next().unwrap(); + let frac = parts.next().unwrap_or("").trim_end_matches('0'); + if frac.len() > scale { + return Err(error( + "WFT-NUMERIC-DOMAIN", + "Decimal scale exceeds selected domain", + )); + } + let coefficient = format!("{whole}{frac}{}", "0".repeat(scale - frac.len())); + if coefficient.trim_start_matches('0').len() > precision { + return Err(error( + "WFT-NUMERIC-DOMAIN", + "Decimal precision exceeds selected domain", + )); + } + } + (LiteralKind::String, ScalarFamily::Timestamp) => { + if expected.facets != serde_json::json!({}) { + return Err(error("WFT-TYPE", "Timestamp facets are unsupported")); + } + crate::exact_instant::key(&input.value).map_err(|d| error( + if d.code == "WFT-LIMIT" { "WFT-LIMIT" } else { "WFT-TIMESTAMP-DOMAIN" }, + "Timestamp literal must be an RFC 3339 exact instant", + ))?; + } + (LiteralKind::Number, ScalarFamily::Float) => { + if expected.facets != serde_json::json!({}) { + return Err(error("WFT-TYPE", "Float facets are unsupported")); + } + validate_float_token(&input.value) + .map_err(|_| error("WFT-NUMERIC-DOMAIN", "Float literal must be finite base-ten text"))?; + } + _ => return Err(error("WFT-TYPE", "Literal family does not match the field")), + } + Ok(()) +} + +/// UMF `floatToken` grammar (`-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?`) whose +/// nearest IEEE 754 binary64 value is finite. The token itself stays the value. +pub fn validate_float_token(value: &str) -> Result<()> { + let fail = || Diagnostic::new("WFT-NUMERIC-DOMAIN", "type", "Finite float token required"); + if value.len() > MAX_TOKEN { return Err(fail()); } + let body = value.strip_prefix('-').unwrap_or(value); + let (mantissa, exponent) = match body.find(['e', 'E']) { + Some(at) => (&body[..at], Some(&body[at + 1..])), + None => (body, None), + }; + let (whole, fraction) = match mantissa.split_once('.') { + Some((whole, fraction)) => (whole, Some(fraction)), + None => (mantissa, None), + }; + let digits = |s: &str| !s.is_empty() && s.bytes().all(|b| b.is_ascii_digit()); + if !digits(whole) || (whole.len() > 1 && whole.starts_with('0')) + || fraction.is_some_and(|f| !digits(f)) + || exponent.is_some_and(|e| !digits(e.strip_prefix(['+', '-']).unwrap_or(e))) + { + return Err(fail()); + } + match value.parse::() { + Ok(v) if v.is_finite() => Ok(()), + _ => Err(fail()), + } +} + +/// Exact decimal arithmetic: the integer coefficient `c` with `token == c * 10^-scale`. +/// +/// Accepts UMF number tokens with an optional exponent. Returns `None` when the +/// token is malformed, needs more than `scale` fractional digits, or has more than +/// `precision` coefficient digits. Callers attach their own diagnostic. +pub fn coefficient(token: &str, scale: u64, precision: Option) -> Option { + if token.len() > MAX_TOKEN { return None; } + let (negative, body) = if let Some(v) = token.strip_prefix('-') { (true, v) } else { (false, token) }; + let at = body.find(['e', 'E']); + let (mantissa, exponent) = if let Some(at) = at { (&body[..at], &body[at + 1..]) } else { (body, "0") }; + let exponent_digits = exponent.strip_prefix(['+', '-']).unwrap_or(exponent); + if exponent_digits.is_empty() || !exponent_digits.bytes().all(|b| b.is_ascii_digit()) { return None; } + let (whole, fraction) = if let Some((whole, fraction)) = mantissa.split_once('.') { + if fraction.is_empty() || !fraction.bytes().all(|b| b.is_ascii_digit()) { return None; } + (whole, fraction) + } else { (mantissa, "") }; + if whole.is_empty() || !whole.bytes().all(|b| b.is_ascii_digit()) || (whole.len() > 1 && whole.starts_with('0')) { return None; } + let joined = format!("{whole}{fraction}"); + let mut digits = joined.trim_start_matches('0').to_string(); + if digits.is_empty() { return Some(BigInt::from(0)); } + if exponent.len() > 32 { return None; } + let exponent: i128 = exponent.parse().ok()?; + let shift = exponent - fraction.len() as i128 + scale as i128; + if shift < 0 { + let cut = -shift; + if cut > digits.len() as i128 { return None; } + let at = digits.len() - cut as usize; + if !digits[at..].bytes().all(|b| b == b'0') { return None; } + digits.truncate(at); + } else { + let bound = precision.unwrap_or(MAX_TOKEN as u64).min(MAX_TOKEN as u64); + if digits.len() as i128 + shift > bound as i128 { return None; } + digits.extend(std::iter::repeat_n('0', shift as usize)); + } + if precision.is_some_and(|p| digits.len() as u64 > p) { return None; } + if digits.is_empty() { return Some(BigInt::from(0)); } + if negative { digits.insert(0, '-'); } + BigInt::parse_bytes(digits.as_bytes(), 10) +} + +/// Fixed-point finite token grammar; no conversion through native floats. +pub fn validate_mathematical_decimal(value:&str)->Result<()> { + let unsigned=value.strip_prefix('-').unwrap_or(value);let mut parts=unsigned.split('.'); + let whole=parts.next().unwrap_or("");let fraction=parts.next(); + if value.len()>1048576 || whole.is_empty() || !whole.bytes().all(|b|b.is_ascii_digit()) + || fraction.is_some_and(|f|f.is_empty()||!f.bytes().all(|b|b.is_ascii_digit())) || parts.next().is_some(){ + return Err(Diagnostic::new("WFT-NUMERIC-DOMAIN","type","Finite fixed-point decimal token required")); + }Ok(()) +} + +/// Byte values use bounded canonical standard base64; original tokens are retained. +pub fn validate_binary_token(value:&str)->Result<()> { + use base64::Engine; + if value.len()>1048576 {return Err(Diagnostic::new("WFT-LIMIT","type","Binary token exceeds one MiB"));} + let bytes=base64::engine::general_purpose::STANDARD.decode(value).map_err(|_|Diagnostic::new("WFT-BINARY-DOMAIN","type","Canonical base64 required"))?; + if base64::engine::general_purpose::STANDARD.encode(bytes)!=value {return Err(Diagnostic::new("WFT-BINARY-DOMAIN","type","Canonical base64 required"));} + Ok(()) +} + +/// Explicit Truss canonical date subset: Gregorian YYYY-MM-DD, years 0001..9999. +/// UMF core declares the family; temporal refinements are not inferred here. +pub fn validate_date_token(value:&str)->Result<()> { + let fail=||Diagnostic::new("WFT-DATE-DOMAIN","type","Canonical Gregorian YYYY-MM-DD required"); + if value.len()!=10 || !value.is_ascii() {return Err(fail());} + crate::exact_instant::key(&format!("{value}T00:00:00Z")).map_err(|_|fail())?; + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::types::Family; + use serde_json::json; + fn lit(value: &str, kind: LiteralKind) -> Literal { + Literal { value: value.into(), kind, span: Span { start: 0, end: 0 } } + } + #[test] + fn timestamp_literals_use_the_exact_instant_profile() { + let t = LogicalType::plain(ScalarFamily::Timestamp, false); + for ok in ["2024-01-01T00:00:00Z", "2024-01-01T00:00:00.123Z", "2023-12-31T19:00:00.1234567890-05:00"] { + check_literal(&lit(ok, LiteralKind::String), &t).unwrap(); + } + for bad in ["2024-01-01", "2024-01-01T00:00:00", "1900-02-29T00:00:00Z", "2016-12-31T23:59:60Z"] { + assert_eq!(check_literal(&lit(bad, LiteralKind::String), &t).unwrap_err().code, "WFT-TIMESTAMP-DOMAIN", "{bad}"); + } + assert_eq!(check_literal(&lit("1", LiteralKind::Number), &t).unwrap_err().code, "WFT-TYPE"); + let faceted = LogicalType { facets: json!({"precision": 3}), ..t }; + assert_eq!(check_literal(&lit("2024-01-01T00:00:00Z", LiteralKind::String), &faceted).unwrap_err().code, "WFT-TYPE"); + } + #[test] + fn float_literals_are_finite_umf_float_tokens() { + let f = LogicalType::plain(ScalarFamily::Float, true); + for ok in ["0", "-0", "1.5", "1e308", "-2.5E-3", "123456789012345678901234567890"] { + check_literal(&lit(ok, LiteralKind::Number), &f).unwrap(); + } + for bad in ["1e309", "01", ".5", "1.", "NaN", "inf", "1e", "+1", " 1"] { + assert_eq!(check_literal(&lit(bad, LiteralKind::Number), &f).unwrap_err().code, "WFT-NUMERIC-DOMAIN", "{bad}"); + } + assert_eq!(check_literal(&lit("1", LiteralKind::String), &f).unwrap_err().code, "WFT-TYPE"); + } + #[test] + fn core_families_refuse_opt_in_tokens_unchanged() { + let decimal: LogicalType = LogicalType { family: Family::Decimal, facets: json!({"precision": 4, "scale": 2}), nullable: false }; + assert_eq!(check_literal(&lit("1e2", LiteralKind::Number), &decimal).unwrap_err().code, "WFT-NUMERIC-DOMAIN"); + let string = LogicalType::plain(Family::String, false); + // The same tokens refuse the core families that cannot carry them. + let integer = LogicalType::plain(Family::Integer, false); + assert_eq!(check_literal(&lit("1.5", LiteralKind::Number), &integer).unwrap_err().code, "WFT-NUMERIC-DOMAIN"); + check_literal(&lit("2024-01-01T00:00:00Z", LiteralKind::String), &string).unwrap(); + } + #[test] + fn coefficients_are_exact() { + assert_eq!(coefficient("1.25", 2, Some(3)), Some(BigInt::from(125))); + assert_eq!(coefficient("-1.5e1", 0, None), Some(BigInt::from(-15))); + assert_eq!(coefficient("1.255", 2, None), None); + assert_eq!(coefficient("1000", 0, Some(3)), None); + assert_eq!(coefficient("0.000", 0, None), Some(BigInt::from(0))); + } +} diff --git a/crates/weft-model/src/exact_instant.rs b/crates/weft-model/src/exact_instant.rs new file mode 100644 index 00000000..8406124b --- /dev/null +++ b/crates/weft-model/src/exact_instant.rs @@ -0,0 +1,56 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +//! Consumer binding of UMF CONTRACT-065; generic timestamp and Key stay unchanged. +use crate::diagnostic::{Diagnostic, Result}; +pub const PROFILE: &str = "umf-rfc3339-exact-instant/1.0.0"; +pub fn key(token: &str) -> Result<(i64, &str)> { + let fail = || Diagnostic::new("WFT-TIMESTAMP-DOMAIN", "type", "Selected RFC3339 exact instant token required"); + let bytes = token.as_bytes(); + if bytes.len() > 1048576 { return Err(Diagnostic::new("WFT-LIMIT", "type", "Timestamp token exceeds one MiB")); } + if bytes.len() < 20 || !bytes.is_ascii() || bytes[4] != b'-' || bytes[7] != b'-' + || !matches!(bytes[10], b'T' | b't') || bytes[13] != b':' || bytes[16] != b':' { return Err(fail()); } + let number = |start: usize, end: usize| -> Result { + let value = &bytes[start..end]; + if !value.iter().all(u8::is_ascii_digit) { return Err(fail()); } + Ok(value.iter().fold(0, |sum, digit| sum * 10 + i64::from(digit - b'0'))) + }; + let year = number(0,4)?; let month = number(5,7)?; let day = number(8,10)?; + let hour = number(11,13)?; let minute = number(14,16)?; let second = number(17,19)?; + let leap = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); + let lengths = [31, if leap {29} else {28}, 31,30,31,30,31,31,30,31,30,31]; + if year == 0 || !(1..=12).contains(&month) || day < 1 || day > lengths[(month-1) as usize] + || hour > 23 || minute > 59 || second > 59 { return Err(fail()); } + let mut position = 19; + let mut fraction = ""; + if bytes[position] == b'.' { + position += 1; let start = position; + while position < bytes.len() && bytes[position].is_ascii_digit() { position += 1; } + if position == start { return Err(fail()); } + fraction = token[start..position].trim_end_matches('0'); + } + if position >= bytes.len() { return Err(fail()); } + let offset = if matches!(bytes[position], b'Z' | b'z') { + if position + 1 != bytes.len() { return Err(fail()); } 0 + } else { + if position + 6 != bytes.len() || !matches!(bytes[position], b'+' | b'-') || bytes[position+3] != b':' { return Err(fail()); } + let hours = number(position+1,position+3)?; let minutes = number(position+4,position+6)?; + if hours > 23 || minutes > 59 { return Err(fail()); } + (hours * 60 + minutes) * if bytes[position] == b'-' {-1} else {1} + }; + let before = |y: i64| { let n = y - 1; n * 365 + n / 4 - n / 100 + n / 400 }; + let days = before(year) - before(1970) + lengths[..(month-1) as usize].iter().sum::() + day - 1; + Ok((days * 86400 + hour * 3600 + minute * 60 + second - offset * 60, fraction)) +} + +#[cfg(test)] +mod tests { + use super::key; + #[test] fn exact_instants() { + assert_eq!(key("2024-01-01T00:00:00.123456789Z").unwrap(), key("2023-12-31T19:00:00.1234567890-05:00").unwrap()); + assert_ne!(key("2024-01-01T00:00:00.1234567Z").unwrap(), key("2024-01-01T00:00:00.1234568Z").unwrap()); + assert_eq!(key("1969-12-31t23:59:59.5000z").unwrap(), (-1,"5")); + assert_eq!(key("1970-01-01T00:00:00-00:00").unwrap(), (0,"")); + assert_eq!(key("0001-01-01T00:00:00+23:59").unwrap().0, -62135683140); + assert_eq!(key("9999-12-31T23:59:59-23:59").unwrap().0, 253402387139); + for token in ["1900-02-29T00:00:00Z", "0000-01-01T00:00:00Z", "2016-12-31T23:59:60Z", "2024-01-01T00:00:00+24:00", "2024-01-01T00:00:00Z\n"] { assert!(key(token).is_err()); } + } +} diff --git a/crates/weft-model/src/json.rs b/crates/weft-model/src/json.rs new file mode 100644 index 00000000..6269c4b5 --- /dev/null +++ b/crates/weft-model/src/json.rs @@ -0,0 +1,239 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +use serde::de::{MapAccess, Visitor}; +use serde::{Deserialize, Deserializer}; +use serde_json::{Value, value::RawValue}; +use sha2::{Digest, Sha256}; +use std::{collections::BTreeSet, fmt}; +type RawResult = std::result::Result; +pub fn sha256(bytes: &[u8]) -> String { + format!("{:x}", Sha256::digest(bytes)) +} +struct RawObject(Vec<(String, Box)>); +impl<'de> Deserialize<'de> for RawObject { + fn deserialize>(d: D) -> std::result::Result { + struct ObjectVisitor; + impl<'de> Visitor<'de> for ObjectVisitor { + type Value = RawObject; + fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str("an object") + } + fn visit_map>( + self, + mut map: M, + ) -> std::result::Result { + let mut entries = Vec::new(); + while let Some(entry) = map.next_entry::>()? { + entries.push(entry); + } + Ok(RawObject(entries)) + } + } + d.deserialize_map(ObjectVisitor) + } +} +// Traverse raw nested values so even arbitrary-size unknown numbers are never f64. +fn inspect_json( + raw: &str, + depth: usize, + count: &mut usize, + max_depth: usize, + max_nodes: usize, +) -> RawResult<()> { + if depth > max_depth || *count >= max_nodes { + return Err("WFT-LIMIT"); + } + *count += 1; + match raw.trim_start().as_bytes().first() { + Some(b'{') => { + let object: RawObject = serde_json::from_str(raw).map_err(|_| "WFT-INPUT")?; + let mut keys = BTreeSet::new(); + for (key, value) in object.0 { + if !keys.insert(key) { + return Err("WFT-JSON-DUPLICATE"); + } + inspect_json(value.get(), depth + 1, count, max_depth, max_nodes)?; + } + } + Some(b'[') => { + let values: Vec> = serde_json::from_str(raw).map_err(|_| "WFT-INPUT")?; + for value in values { + inspect_json(value.get(), depth + 1, count, max_depth, max_nodes)?; + } + } + _ => { + let _: Value = serde_json::from_str(raw).map_err(|_| "WFT-INPUT")?; + } + } + Ok(()) +} +// Value's arbitrary-precision/raw-value visitor reserves ordinary object keys. +// Construct objects ourselves so unknown JSON members remain ordinary data. +fn faithful_tree(raw: &str) -> RawResult { + match raw.trim_start().as_bytes().first() { + Some(b'{') => { + let object: RawObject = serde_json::from_str(raw).map_err(|_| "WFT-INPUT")?; + let mut out = serde_json::Map::new(); + for (key, value) in object.0 { + out.insert(key, faithful_tree(value.get())?); + } + Ok(Value::Object(out)) + } + Some(b'[') => { + let values: Vec> = serde_json::from_str(raw).map_err(|_| "WFT-INPUT")?; + values + .iter() + .map(|v| faithful_tree(v.get())) + .collect::>>() + .map(Value::Array) + } + _ => serde_json::from_str(raw).map_err(|_| "WFT-INPUT"), + } +} +pub fn checked_json(raw: &str) -> RawResult { + inspect_json(raw, 0, &mut 0, 128, 100_000)?; + faithful_tree(raw) +} + +/// The selected Truss runtime catalog has a four-MiB byte envelope. A thousand +/// records with twelve fields exceeds the general 100,000-value limit even +/// though both original model and binding fit that existing byte envelope. +/// Keep generic transport admission unchanged; this reader grants no authority. +pub fn checked_truss_runtime_catalog_json(raw: &str) -> RawResult { + if raw.len() > 4 * 1024 * 1024 { + return Err("WFT-LIMIT"); + } + inspect_json(raw, 0, &mut 0, 128, 1_000_000)?; + faithful_tree(raw) +} + +#[cfg(test)] +mod truss_runtime_catalog_tests { + use super::*; + + #[test] + fn larger_catalog_budget_does_not_change_generic_admission() { + let raw = format!("[{}]", vec!["0"; 100_000].join(",")); + assert_eq!(checked_json(&raw), Err("WFT-LIMIT")); + assert_eq!(checked_truss_runtime_catalog_json(&raw).unwrap().as_array().unwrap().len(), 100_000); + } + + #[test] + fn runtime_catalog_keeps_byte_depth_value_and_duplicate_limits() { + let at_limit = format!("[{}]", vec!["0"; 999_999].join(",")); + assert!(checked_truss_runtime_catalog_json(&at_limit).is_ok()); + let over_limit = format!("[{}]", vec!["0"; 1_000_000].join(",")); + assert_eq!(checked_truss_runtime_catalog_json(&over_limit), Err("WFT-LIMIT")); + assert_eq!(checked_truss_runtime_catalog_json(&" ".repeat(4 * 1024 * 1024 + 1)), Err("WFT-LIMIT")); + assert_eq!(checked_truss_runtime_catalog_json(&format!("{}0{}", "[".repeat(129), "]".repeat(129))), Err("WFT-LIMIT")); + assert_eq!(checked_truss_runtime_catalog_json(r#"{"a":1,"\u0061":2}"#), Err("WFT-JSON-DUPLICATE")); + } +} + +/// Bounded transport reader; root depth is one and each value counts once. +/// Parsing is custody only and grants no result release authority. +#[allow(dead_code)] // Pending runtime-cell consumer. +pub fn checked_json_bounded( + raw: &str, + max_bytes: usize, + max_depth: usize, + max_nodes: usize, +) -> RawResult { + if raw.len() > max_bytes { + return Err("WFT-LIMIT"); + } + inspect_json(raw, 1, &mut 0, max_depth, max_nodes)?; + faithful_tree(raw) +} +/// Same bounded reader with parse visits consumed from the caller's work ledger. +/// Raw inspection temporaries are separately bounded by input bytes and depth; +/// this is not a process-memory bound. +pub fn checked_json_bounded_charged( + raw: &str, + max_bytes: usize, + max_depth: usize, + remaining_work: &mut usize, +) -> RawResult { + if raw.len() > max_bytes { + return Err("WFT-LIMIT"); + } + let mut count = 0; + inspect_json(raw, 1, &mut count, max_depth, *remaining_work)?; + *remaining_work = remaining_work.checked_sub(count).ok_or("WFT-LIMIT")?; + faithful_tree(raw) +} +#[cfg(test)] +mod bounded_transport_tests { + use super::checked_json_bounded_charged; + #[test] + fn charged_reader_uses_the_callers_actual_remaining_work() { + let raw = "{\"a\":[1,2]}"; + let mut work = 4; + assert!(checked_json_bounded_charged(raw, raw.len(), 3, &mut work).is_ok()); + assert_eq!(work, 0); + let mut work = 3; + assert!(checked_json_bounded_charged(raw, raw.len(), 3, &mut work).is_err()); + assert_eq!(work, 3); + } + use super::*; + #[test] + fn ordinary_reserved_looking_objects_survive_every_reader() { + for key in [ + "$serde_json::private::Number", + "$serde_json::private::RawValue", + ] { + let raw = format!("{{\"{key}\":\"not JSON\",\"sibling\":[1.00e0,true]}}"); + let mut map = serde_json::Map::new(); + map.insert(key.into(), Value::String("not JSON".into())); + map.insert( + "sibling".into(), + Value::Array(vec![ + serde_json::from_str("1.00e0").unwrap(), + Value::Bool(true), + ]), + ); + let expected = Value::Object(map); + assert_eq!(checked_json(&raw).unwrap(), expected); + assert_eq!( + checked_json_bounded(&raw, raw.len(), 3, 5).unwrap(), + expected + ); + let mut work = 5; + assert_eq!( + checked_json_bounded_charged(&raw, raw.len(), 3, &mut work).unwrap(), + expected + ); + assert_eq!(work, 0); + } + } + #[test] + fn byte_limits_count_utf8_and_whitespace() { + let raw = " \"é\" "; + assert!(checked_json_bounded(raw, raw.len(), 1, 1).is_ok()); + assert_eq!( + checked_json_bounded(raw, raw.len() - 1, 1, 1), + Err("WFT-LIMIT") + ); + } + #[test] + fn depth_counts_root_and_nodes_count_values_not_names() { + let raw = r#"{"rows":[[null,true]]}"#; + assert!(checked_json_bounded(raw, raw.len(), 4, 5).is_ok()); + assert_eq!(checked_json_bounded(raw, raw.len(), 3, 5), Err("WFT-LIMIT")); + assert_eq!(checked_json_bounded(raw, raw.len(), 4, 4), Err("WFT-LIMIT")); + assert_eq!(checked_json_bounded("null", 4, 0, 1), Err("WFT-LIMIT")); + assert_eq!(checked_json_bounded("null", 4, 1, 0), Err("WFT-LIMIT")); + } + #[test] + fn duplicate_members_and_malformed_json_refuse() { + for raw in [r#"{"x":1,"x":2}"#, r#"[{"x":1,"\u0078":2}]"#] { + assert_eq!( + checked_json_bounded(raw, raw.len(), 64, 1_000_000), + Err("WFT-JSON-DUPLICATE") + ); + } + assert_eq!( + checked_json_bounded("[null,]", 100, 64, 100), + Err("WFT-INPUT") + ); + } +} diff --git a/crates/weft-model/src/lib.rs b/crates/weft-model/src/lib.rs new file mode 100644 index 00000000..c396c098 --- /dev/null +++ b/crates/weft-model/src/lib.rs @@ -0,0 +1,22 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +//! UMF model loading, identity resolution, logical typing and exact values. +//! +//! This crate is the shared base of weft's compiler and of other compilers that +//! type programs against UMF models. It reads pinned UMF 0.10 documents, resolves +//! authored names to original identities, derives logical scalar types under an +//! explicit [`catalog::Admission`] profile, and checks exact literal tokens. It does +//! no SQL parsing, planning, backend lowering or IO. See `README.md` for the +//! versioned public surface. +pub mod catalog; +pub mod core_literal; +pub mod descriptor; +pub mod diagnostic; +pub mod exact; +pub mod exact_instant; +pub mod json; +pub mod name; +pub mod string_domain; +pub mod types; + +/// This crate's contract version (`README.md`). +pub const VERSION: &str = env!("CARGO_PKG_VERSION"); diff --git a/crates/weft-model/src/name.rs b/crates/weft-model/src/name.rs new file mode 100644 index 00000000..82e30064 --- /dev/null +++ b/crates/weft-model/src/name.rs @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +//! Authored source names. Quoted names match exactly; unquoted names are written +//! lower-case and match the ASCII-lower-cased authored name. +use crate::types::Span; + +#[derive(Debug, Clone)] +pub struct Name { + pub value: String, + pub quoted: bool, + pub span: Span, +} +impl Name { + pub fn matches(&self, value: &str) -> bool { + if self.quoted { + self.value == value + } else { + self.value == value.to_ascii_lowercase() + } + } + pub fn key(&self) -> &str { + &self.value + } + /// A quoted name for identity-driven lookups that have no source span. + pub fn quoted(value: &str) -> Self { + Self { value: value.into(), quoted: true, span: Span { start: 0, end: 0 } } + } +} diff --git a/crates/weft-model/src/string_domain.rs b/crates/weft-model/src/string_domain.rs new file mode 100644 index 00000000..7f37b634 --- /dev/null +++ b/crates/weft-model/src/string_domain.rs @@ -0,0 +1,173 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +//! Exact bounded Unicode scalar domains for the explicitly selected new candidate. +//! Pure type/value checks confer no permission or native correspondence. +use crate::{diagnostic::{Diagnostic, Result}, types::{Family, LogicalType}}; +use serde_json::{json, Value}; + +const MAX_BOUND: u64 = 9_007_199_254_740_991; +fn fail() -> Diagnostic { + Diagnostic::new("WFT-CURRENT-STRING-DOMAIN", "capability", "Current string domain refused") +} +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Length { min: u64, max: u64 } +impl Length { + pub fn min(&self) -> u64 { self.min } + pub fn max(&self) -> u64 { self.max } +} +// Interpret the JSON number exactly, without f64 or exponent-sized allocation. +fn bound(value: &Value) -> Result { + let number = value.as_number().ok_or_else(fail)?; + if let Some(n) = number.as_u64() { return if n <= MAX_BOUND { Ok(n) } else { Err(fail()) }; } + let token = number.to_string(); + let (mantissa, exponent) = token.split_once(['e','E']).unwrap_or((&token,"0")); + let negative = mantissa.starts_with('-'); + let mantissa = mantissa.strip_prefix('-').unwrap_or(mantissa); + let fraction = mantissa.split_once('.').map_or(0, |(_,f)| f.len()); + let digits: Vec = mantissa.bytes().filter(|b| *b != b'.').collect(); + let Some(first) = digits.iter().position(|b| *b != b'0') else { return Ok(0); }; + if negative { return Err(fail()); } + let digits = &digits[first..]; + let shift = exponent.parse::().map_err(|_| fail())? + .checked_sub(i64::try_from(fraction).map_err(|_|fail())?).ok_or_else(fail)?; + let (significant, zeros) = if shift < 0 { + let removed = usize::try_from(shift.checked_neg().ok_or_else(fail)?).map_err(|_|fail())?; + if removed >= digits.len() || digits[digits.len()-removed..].iter().any(|b| *b != b'0') { return Err(fail()); } + (&digits[..digits.len()-removed], 0) + } else { + (digits, usize::try_from(shift).map_err(|_|fail())?) + }; + if significant.len().checked_add(zeros).ok_or_else(fail)? > 16 { return Err(fail()); } + let mut n = 0u64; + for digit in significant { n = n.checked_mul(10).and_then(|v| v.checked_add((digit-b'0') as u64)).ok_or_else(fail)?; } + for _ in 0..zeros { n = n.checked_mul(10).ok_or_else(fail)?; } + if n > MAX_BOUND { return Err(fail()); } + Ok(n) +} +/// Empty facets mean the existing unconstrained String, not a guessed bound. +pub fn bounds(facets: &Value) -> Result> { + let object = facets.as_object().ok_or_else(fail)?; + if object.is_empty() { return Ok(None); } + if object.len() != 1 { return Err(fail()); } + let length = object.get("length").and_then(Value::as_object).ok_or_else(fail)?; + if length.len() != 3 || length.get("unit").and_then(Value::as_str) != Some("unicode-scalar") { + return Err(fail()); + } + let min = bound(length.get("min").ok_or_else(fail)?)?; + let max = bound(length.get("max").ok_or_else(fail)?)?; + if min > max || max > MAX_BOUND { return Err(fail()); } + Ok(Some(Length { min, max })) +} +/// The original profiles always pass false; no old profile is widened by this helper. +pub fn supported(t: &LogicalType, bounded: bool) -> bool { + !t.nullable && match t.family { + Family::String => t.facets == json!({}) || (bounded && bounds(&t.facets).is_ok()), + Family::Integer => t.facets == json!({"integerWidth":{"bits":64,"signed":true}}), + _ => false, + } +} +pub fn validate(t: &LogicalType, value: &str, bounded: bool) -> Result<()> { + if t.family != Family::String || !supported(t, bounded) { return Err(fail()); } + validate_facets(&t.facets, value) +} +/// Literal admission checks the actual non-null value even for a nullable Field. +pub fn validate_facets(facets: &Value, value: &str) -> Result<()> { + if let Some(length) = bounds(facets)? { + // Work depends on the actual framed value, never on a declared maximum. + let count = value.chars().count() as u64; + if count < length.min || count > length.max { return Err(fail()); } + } + Ok(()) +} +/// Semantically identical bounded domains can retain different exact JSON spellings. +/// Identity and comparison-scope checks remain separate mandatory obligations. +pub fn same_domain(left: &LogicalType, right: &LogicalType, bounded: bool) -> bool { + supported(left, bounded) && supported(right, bounded) + && (left == right || (bounded && left.family == Family::String + && right.family == Family::String && bounds(&left.facets).ok() == bounds(&right.facets).ok())) +} +/// A literal keeps its own original domain/value. The other operand's bounds +/// constrain that operand, not this value. Field/field pairing is a separate rule. +pub fn literal_compatible(original: &LogicalType, effective: &LogicalType, value: &str, bounded: bool) -> bool { + original.family == Family::String && effective.family == Family::String + && supported(effective, bounded) && validate(original, value, bounded).is_ok() + && (original == effective || bounded) +} + +#[cfg(test)] +mod tests { + use super::*; + fn t(min:u64,max:u64)->LogicalType { + LogicalType {family:Family::String,nullable:false,facets:json!({"length":{"min":min,"max":max,"unit":"unicode-scalar"}})} + } + #[test] + fn exact_domains_and_safe_bounds_refuse_other_meanings() { + assert_eq!(bounds(&json!({})).unwrap(),None); + assert_eq!(bounds(&t(0,MAX_BOUND).facets).unwrap().unwrap().max(),MAX_BOUND); + assert_eq!(bounds(&t(2,3).facets).unwrap().unwrap().min(),2); + for facets in [json!(null),json!([]),json!({"length":null}),json!({"other":{}}), + json!({"length":{"min":0,"max":1,"unit":"unicode-scalar"},"extra":true}), + json!({"length":{"min":0,"max":1,"unit":"unicode-scalar","extra":0}}), + json!({"length":{"max":1,"unit":"unicode-scalar"}}), + json!({"length":{"min":0,"unit":"unicode-scalar"}}), + json!({"length":{"min":0,"max":1}}), + json!({"length":{"min":-1,"max":1,"unit":"unicode-scalar"}}), + json!({"length":{"min":true,"max":1,"unit":"unicode-scalar"}}), + json!({"length":{"min":0.1,"max":1,"unit":"unicode-scalar"}}), + json!({"length":{"min":0,"max":1.5,"unit":"unicode-scalar"}}), + t(2,1).facets,t(0,MAX_BOUND+1).facets] { + assert!(bounds(&facets).is_err(),"{facets}"); + } + for unit in ["byte","utf16","unicode-code-unit","grapheme","Unicode-scalar",""] { + let mut f=t(0,3).facets;f["length"]["unit"]=json!(unit);assert!(bounds(&f).is_err()); + } + } + #[test] + fn mathematical_json_integer_bounds_keep_original_representation() { + for (text, expected) in [("0.0",0),("-0",0),("-0.0e999999999999999999999999",0),("1.0",1),("1e0",1),("1.5e1",15),("9007199254740991.0000000",MAX_BOUND),("9.007199254740991e15",MAX_BOUND)] { + let value:Value=serde_json::from_str(text).unwrap();let original=value.clone(); + assert_eq!(bound(&value).unwrap(),expected);assert_eq!(value,original); + } + for text in ["0.1","1e-1","-1.0","9007199254740992.0","9007199254740991.0000001","1e999999999999999999999999","1e-999999999999999999999999"] { + assert!(bound(&serde_json::from_str::(text).unwrap()).is_err(),"{text}"); + } + } + #[test] + fn independent_scalar_count_vectors_detect_bytes_units_and_normalization() { + // Expected lengths are authored, not obtained from the implementation. + for (value,count) in [("",0),("A",1),("😀",1),("é",1),("e\u{301}",2),("😀e\u{301}",3),("\r\n",2),("\0",1)] { + assert!(validate(&t(count,count),value,true).is_ok()); + if count>0 {assert!(validate(&t(0,count-1),value,true).is_err());} + assert!(validate(&t(count+1,MAX_BOUND),value,true).is_err()); + } + assert_eq!("😀".len(),4);assert_eq!("😀".encode_utf16().count(),2); + assert!(validate(&t(1,1),"😀",true).is_ok()); + assert!(validate(&t(1,1),"e\u{301}",true).is_err()); + assert_ne!("é","e\u{301}"); + assert!(serde_json::from_str::(r#""\ud800""#).is_err()); + } + #[test] + fn literal_provenance_and_distinct_effective_bounds_are_retained() { + let original=t(1,3);let effective=t(10,64);let before=original.facets.clone(); + assert!(literal_compatible(&original,&effective,"é",true)); + assert!(!literal_compatible(&original,&effective,"four",true)); + assert!(!literal_compatible(&original,&effective,"",true)); + assert_eq!(original.facets,before); + let equivalent=LogicalType{family:Family::String,nullable:false,facets:serde_json::from_str(r#"{"length":{"min":1.0,"max":3e0,"unit":"unicode-scalar"}}"#).unwrap()}; + assert!(same_domain(&original,&equivalent,true)); + assert!(!same_domain(&original,&effective,true)); + assert!(!same_domain(&original,&equivalent,false)); + let mut other=effective.clone();other.family=Family::Integer; + assert!(!literal_compatible(&original,&other,"1",true)); + other=effective.clone();other.nullable=true; + assert!(!literal_compatible(&original,&other,"é",true)); + } + #[test] + fn old_profiles_and_nullable_domains_remain_closed() { + assert!(!supported(&t(0,3),false));assert!(validate(&t(0,3),"A",false).is_err()); + assert!(!literal_compatible(&t(0,3),&t(0,3),"A",false)); + let mut nullable=t(0,3);nullable.nullable=true;assert!(!supported(&nullable,true)); + let unbounded=LogicalType{family:Family::String,nullable:false,facets:json!({})}; + assert!(supported(&unbounded,false));assert!(validate(&unbounded,"😀",false).is_ok()); + assert!(literal_compatible(&unbounded,&unbounded,"😀",false)); + } +} diff --git a/crates/weft-model/src/types.rs b/crates/weft-model/src/types.rs new file mode 100644 index 00000000..a6da578f --- /dev/null +++ b/crates/weft-model/src/types.rs @@ -0,0 +1,178 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +//! Resolved UMF identities and logical scalar types. +//! +//! A [`LogicalType`] is parameterized by the family set a profile admits: +//! +//! - [`Family`] is the closed set every existing weft dialect profile admits +//! (boolean, string, integer, decimal). `LogicalType` defaults to it, so weft's +//! IR and backends cannot represent anything wider. +//! - [`ScalarFamily`] adds UMF `timestamp` and `float`. New profiles and other +//! compilers (Fireweed) select it explicitly, as [`ScalarType`]. +//! +//! The split is enforced by the type system, not by refusal checks: an old +//! profile has no value that could carry a timestamp or float. +use serde::{Deserialize, Serialize}; +use serde_json::Value; + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct Span { + pub start: usize, + pub end: usize, +} +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Identity { + pub document_id: String, + pub revision: String, + pub module: String, + pub element: String, +} +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", deny_unknown_fields)] +pub struct ModelPin { + pub document_id: String, + pub revision: String, + pub umf_version: String, + pub sha256: String, +} +/// The four UMF scalar families every weft dialect profile admits. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub enum Family { + #[serde(rename = "boolean")] + Boolean, + #[serde(rename = "string")] + String, + #[serde(rename = "integer")] + Integer, + #[serde(rename = "decimal")] + Decimal, +} +/// UMF scalar families available to opt-in profiles: [`Family`] plus `timestamp` +/// and `float`. Wire spellings are the UMF `scalarType` names. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum ScalarFamily { + Boolean, + String, + Integer, + Decimal, + /// An exact UTC instant: an RFC 3339 token with any fractional precision, + /// ordered and compared through [`crate::exact_instant::key`]. + Timestamp, + /// A finite IEEE 754 binary64 value written as a UMF float token. The + /// original token is retained. + Float, +} +impl ScalarFamily { + /// Read a UMF `scalarType` name. Unknown and unsupported families are `None`. + pub fn from_umf(name: &str) -> Option { + Some(match name { + "boolean" => Self::Boolean, + "string" => Self::String, + "integer" => Self::Integer, + "decimal" => Self::Decimal, + "timestamp" => Self::Timestamp, + "float" => Self::Float, + _ => return None, + }) + } + /// The UMF `scalarType` spelling. + pub fn umf_name(self) -> &'static str { + match self { + Self::Boolean => "boolean", + Self::String => "string", + Self::Integer => "integer", + Self::Decimal => "decimal", + Self::Timestamp => "timestamp", + Self::Float => "float", + } + } +} +impl From for ScalarFamily { + fn from(family: Family) -> Self { + match family { + Family::Boolean => Self::Boolean, + Family::String => Self::String, + Family::Integer => Self::Integer, + Family::Decimal => Self::Decimal, + } + } +} +impl TryFrom for Family { + type Error = ScalarFamily; + fn try_from(family: ScalarFamily) -> std::result::Result { + Ok(match family { + ScalarFamily::Boolean => Self::Boolean, + ScalarFamily::String => Self::String, + ScalarFamily::Integer => Self::Integer, + ScalarFamily::Decimal => Self::Decimal, + other => return Err(other), + }) + } +} +/// A set of scalar families a profile admits. Implemented by [`Family`] and +/// [`ScalarFamily`]; typing functions are generic over it. +pub trait FamilySet: Clone + PartialEq + std::fmt::Debug { + /// Admit `family`, or `None` when it is outside this set. + fn admit(family: ScalarFamily) -> Option; + /// This family as a [`ScalarFamily`]. + fn widen(&self) -> ScalarFamily; +} +impl FamilySet for Family { + fn admit(family: ScalarFamily) -> Option { Self::try_from(family).ok() } + fn widen(&self) -> ScalarFamily { self.clone().into() } +} +impl FamilySet for ScalarFamily { + fn admit(family: ScalarFamily) -> Option { Some(family) } + fn widen(&self) -> ScalarFamily { *self } +} +/// A logical scalar type: family, UMF facets and nullability. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct LogicalType { + pub family: F, + pub facets: Value, + pub nullable: bool, +} +/// A logical type over the opt-in family set. +pub type ScalarType = LogicalType; +impl LogicalType { + /// A facetless type of `family`. + pub fn plain(family: F, nullable: bool) -> Self { + Self { family, facets: Value::Object(Default::default()), nullable } + } + /// The same type over the opt-in family set. + pub fn widen(&self) -> ScalarType { + LogicalType { family: self.family.widen(), facets: self.facets.clone(), nullable: self.nullable } + } + /// Narrow to another family set, or `None` when the family is outside it. + pub fn narrow(&self) -> Option> { + Some(LogicalType { family: G::admit(self.family.widen())?, facets: self.facets.clone(), nullable: self.nullable }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn core_family_wire_format_is_unchanged_and_refuses_opt_in_names() { + assert_eq!(serde_json::to_value(Family::Decimal).unwrap(), "decimal"); + for name in ["timestamp", "float"] { + assert!(serde_json::from_value::(serde_json::json!(name)).is_err()); + assert!(serde_json::from_value::(serde_json::json!({"family":name,"facets":{},"nullable":false})).is_err()); + } + } + #[test] + fn opt_in_families_use_umf_spellings_and_narrow_explicitly() { + for name in ["boolean", "string", "integer", "decimal", "timestamp", "float"] { + let family = ScalarFamily::from_umf(name).unwrap(); + assert_eq!(family.umf_name(), name); + assert_eq!(serde_json::to_value(family).unwrap(), name); + } + assert_eq!(ScalarFamily::from_umf("time"), None); + let timestamp = ScalarType::plain(ScalarFamily::Timestamp, true); + assert_eq!(timestamp.narrow::(), None); + let decimal = ScalarType::plain(ScalarFamily::Decimal, false); + assert_eq!(decimal.narrow::(), Some(LogicalType::plain(Family::Decimal, false))); + assert_eq!(LogicalType::plain(Family::Integer, true).widen(), ScalarType::plain(ScalarFamily::Integer, true)); + } +} diff --git a/crates/weft-postgresql-emit/Cargo.toml b/crates/weft-postgresql-emit/Cargo.toml new file mode 100644 index 00000000..83f7e54c --- /dev/null +++ b/crates/weft-postgresql-emit/Cargo.toml @@ -0,0 +1,15 @@ +[package] +license = "MIT OR Apache-2.0" +name = "weft-postgresql-emit" +# Own semver, independent of the workspace and wheel versions. See README.md. +version = "0.1.0" +edition.workspace = true +publish.workspace = true +rust-version = "1.90" +description = "PostgreSQL emission primitives: identifier quoting, parameter slots, string literals, typed casts, collation and result decoders" + +[dependencies] +weft-model = { path = "../weft-model" } +weft-backend = { path = "../weft-backend" } +base64 = "=0.22.1" +serde_json.workspace = true diff --git a/crates/weft-postgresql-emit/LICENSE-APACHE b/crates/weft-postgresql-emit/LICENSE-APACHE new file mode 100644 index 00000000..0398f585 --- /dev/null +++ b/crates/weft-postgresql-emit/LICENSE-APACHE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2026 DocumentDrivenDX contributors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/crates/weft-postgresql-emit/LICENSE-MIT b/crates/weft-postgresql-emit/LICENSE-MIT new file mode 100644 index 00000000..d95948b5 --- /dev/null +++ b/crates/weft-postgresql-emit/LICENSE-MIT @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 DocumentDrivenDX contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/crates/weft-postgresql-emit/LICENSE.md b/crates/weft-postgresql-emit/LICENSE.md new file mode 100644 index 00000000..2381950f --- /dev/null +++ b/crates/weft-postgresql-emit/LICENSE.md @@ -0,0 +1,239 @@ +# License + +First-party software and documentation in this repository are dual-licensed, at +your option, under either the MIT License or the Apache License, Version 2.0. + +SPDX-License-Identifier: MIT OR Apache-2.0 + +Third-party code, dependencies, standards, fixtures, datasets and other externally +sourced material retain their original licenses and notices. This grant does not +relicense that material. Frozen evidence and embedded dependency copies retain +their original bytes; accompanying notices govern their use. + +## MIT License + +MIT License + +Copyright (c) 2026 DocumentDrivenDX contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +## Apache License, Version 2.0 + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2026 DocumentDrivenDX contributors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/crates/weft-postgresql-emit/README.md b/crates/weft-postgresql-emit/README.md new file mode 100644 index 00000000..7a9da986 --- /dev/null +++ b/crates/weft-postgresql-emit/README.md @@ -0,0 +1,31 @@ +# weft-postgresql-emit 0.1.0 + +PostgreSQL emission primitives shared by weft's PostgreSQL backends +(`weft-postgresql`) and other compilers' PostgreSQL bindings (Fireweed): quoted +identifiers, numbered parameter slots, string literals, typed casts with the +`C` collation, and exact result decoders. + +It depends on `weft-model` (logical types, family sets, diagnostics) and +`weft-backend` (the SQL carrier's `ParameterSlot` and `ScalarDecoder`). It +knows no storage layout: Truss's table and property layout stays in +`weft-postgresql`, and a Fireweed layout is a different binding over the same +primitives. + +## Version + +Own semver, independent of the workspace and the `umf-weft` wheel; +`weft_postgresql_emit::VERSION` reports it. While the major version is 0, a +minor bump may break the public surface below; a patch bump may not. Emitted +text and refusal codes are part of the surface. + +## Public surface + +| Item | Meaning | +| --- | --- | +| `Identifier`, `qualified` | A quoted identifier in the standard `NAMEDATALEN=64` profile (1–63 UTF-8 bytes, no NUL, `"` doubled); namespace and object quoted separately. | +| `Parameters`, `CatalogDomain` | Per-query numbered slots `$1..$1024`. Values never enter SQL text. Typed (`push`), exact binary (canonical base64), exact timestamp and date slots, and signed native catalog discriminators (`int4`/`int2`). | +| `string_literal`, `escape_string_literal` | `'...'` and `E'...'` literals; `None` for text containing NUL. | +| `COLLATE_C`, `collate_c` | The bytewise `pg_catalog."C"` collation used for every text comparison and ordering. | +| `cast_type`, `typed` | `expr::pg_catalog.text COLLATE pg_catalog."C"`, `::bool`, `::numeric` (exact integers and decimals); for opt-in families `::timestamptz` and `::float8`. | +| `decoder`, `decoder_for` | The exact result decoder for a column's family (`ScalarDecoder::exact`). | +| `decodable` | Projects an expression to the text its decoder reads: `to_char(.. AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"')` for timestamps, `encode(float8send(..), 'hex')` for floats, `::text` for integers and decimals. Instants outside years 0001–9999 or infinite are not decodable. Experimental: only unit tests use it. | diff --git a/crates/weft-postgresql-emit/src/lib.rs b/crates/weft-postgresql-emit/src/lib.rs new file mode 100644 index 00000000..e64c74c1 --- /dev/null +++ b/crates/weft-postgresql-emit/src/lib.rs @@ -0,0 +1,376 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +//! PostgreSQL emission primitives shared by weft's PostgreSQL backends and other +//! compilers' PostgreSQL bindings: quoted identifiers, numbered parameter slots, +//! string literals, typed casts with the `C` collation, and result decoders. +//! Storage layouts (Truss, Fireweed) and engine support are separate bindings +//! over these primitives. See `README.md` for the versioned surface. +use weft_backend::sql::{ExactParameterKind, ParameterSlot, ParameterType, ScalarDecoder}; +use weft_model::{ + descriptor::ExactScalarType, + diagnostic::{Diagnostic, Result}, + types::{Family, FamilySet, LogicalType, ScalarFamily}, +}; + +/// This crate's contract version (`README.md`). +pub const VERSION: &str = env!("CARGO_PKG_VERSION"); +/// Exact quoted identifier in the proposed standard NAMEDATALEN=64 profile. +/// Nonstandard server builds require a separately qualified identifier profile. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Identifier(String); +impl Identifier { + pub fn new(value: &str) -> Result { + if value.is_empty() || value.len() > 63 || value.contains('\0') { + return Err(Diagnostic::new( + "WFT-BINDING", + "binding", + "PostgreSQL identifier is empty, contains NUL or exceeds 63 UTF-8 bytes", + )); + } + Ok(Self(value.to_owned())) + } + pub fn sql(&self) -> String { + format!("\"{}\"", self.0.replace('"', "\"\"")) + } +} +/// Namespace and object are separately quoted; dots in either remain literal. +pub fn qualified(namespace: &Identifier, object: &Identifier) -> String { + format!("{}.{}", namespace.sql(), object.sql()) +} +/// Per-query slots: values never enter emitted SQL. Common emission validation +/// remains responsible for exact lexical/domain checking before any artifact. +#[derive(Debug, Default, Clone)] +pub struct Parameters(Vec); +impl Parameters { + pub fn push( + &mut self, + logical_type: LogicalType, + value: String, + origin: serde_json::Value, + ) -> Result { + if self.0.len() >= 1024 { + return Err(Diagnostic::new( + "WFT-LIMIT", + "emit", + "PostgreSQL parameter count exceeds 1024", + )); + } + let position = self.0.len() + 1; + self.0.push(ParameterSlot { + position, + logical_type: logical_type.into(), + value, + origin, + }); + Ok(format!("${position}")) + } + pub fn push_binary(&mut self, value: String, origin: serde_json::Value) -> Result { + use base64::Engine; + if value.len() > 1048576 || self.0.len() >= 1024 { + return Err(Diagnostic::new( + "WFT-LIMIT", + "emit", + "Binary parameter bounds exceeded", + )); + } + let bytes = base64::engine::general_purpose::STANDARD + .decode(&value) + .map_err(|_| { + Diagnostic::new("WFT-BINARY-DOMAIN", "emit", "Canonical base64 required") + })?; + if base64::engine::general_purpose::STANDARD.encode(bytes) != value { + return Err(Diagnostic::new( + "WFT-BINARY-DOMAIN", + "emit", + "Canonical base64 required", + )); + } + let position = self.0.len() + 1; + self.0.push(ParameterSlot { + position, + logical_type: ParameterType::Exact { + kind: ExactParameterKind::ExactScalar, + scalar_type: ExactScalarType::Binary, + }, + value, + origin, + }); + Ok(format!("${position}")) + } + pub fn push_timestamp(&mut self, value: String, origin: serde_json::Value) -> Result { + if value.len() > 1048576 || self.0.len() >= 1024 { + return Err(Diagnostic::new( + "WFT-LIMIT", + "emit", + "Timestamp parameter bounds exceeded", + )); + } + let position = self.0.len() + 1; + self.0.push(ParameterSlot { + position, + logical_type: ParameterType::Exact { + kind: ExactParameterKind::ExactScalar, + scalar_type: ExactScalarType::Timestamp, + }, + value, + origin, + }); + Ok(format!("${position}")) + } + pub fn push_date(&mut self, value: String, origin: serde_json::Value) -> Result { + if value.len() != 10 || self.0.len() >= 1024 { + return Err(Diagnostic::new( + "WFT-LIMIT", + "emit", + "Date parameter bounds exceeded", + )); + } + let position = self.0.len() + 1; + self.0.push(ParameterSlot { + position, + logical_type: ParameterType::Exact { + kind: ExactParameterKind::ExactScalar, + scalar_type: ExactScalarType::Date, + }, + value, + origin, + }); + Ok(format!("${position}")) + } + pub fn into_slots(self) -> Vec { + self.0 + } + /// Slots pushed so far, in position order. + pub fn slots(&self) -> &[ParameterSlot] { + &self.0 + } + /// Replace the recorded origin of slot `index` (0-based). Values, types and + /// positions cannot be changed after a push. + pub fn set_origin(&mut self, index: usize, origin: serde_json::Value) -> bool { + self.0.get_mut(index).map(|slot| slot.origin = origin).is_some() + } +} +/// Native catalog discriminator domain, independent of logical business keys. +/// Historical nonpositive IDs are retained; allocation policy belongs to Truss. +#[derive(Debug, Clone, Copy)] +pub enum CatalogDomain { + Int, + SmallInt, +} +impl Parameters { + pub fn catalog( + &mut self, + domain: CatalogDomain, + text: &str, + origin: serde_json::Value, + ) -> Result { + let number = text.parse::().ok(); + let bits = match domain { + CatalogDomain::Int => 32, + CatalogDomain::SmallInt => 16, + }; + let valid = number.is_some_and(|value| { + value.to_string() == text + && match domain { + CatalogDomain::Int => i32::try_from(value).is_ok(), + CatalogDomain::SmallInt => i16::try_from(value).is_ok(), + } + }); + if !valid { + return Err(Diagnostic::new( + "WFT-BINDING", + "binding", + "Catalog discriminator is not canonical text in its signed native domain", + )); + } + self.push( + LogicalType { + family: Family::Integer, + facets: serde_json::json!({"integerWidth":{"bits":bits,"signed":true}}), + nullable: false, + }, + text.into(), + origin, + ) + } +} +/// The bytewise collation every emitted text comparison and ordering uses. +pub const COLLATE_C: &str = "COLLATE pg_catalog.\"C\""; +/// `expression COLLATE pg_catalog."C"`. +pub fn collate_c(expression: &str) -> String { + format!("{expression} {COLLATE_C}") +} +/// A standard-conforming string literal `'...'`, or `None` for text containing +/// NUL, which PostgreSQL text cannot represent. +pub fn string_literal(text: &str) -> Option { + (!text.contains('\0')).then(|| format!("'{}'", text.replace('\'', "''"))) +} +/// An escape string literal `E'...'` (backslashes doubled), or `None` for NUL. +pub fn escape_string_literal(text: &str) -> Option { + (!text.contains('\0')).then(|| format!("E'{}'", text.replace('\\', "\\\\").replace('\'', "''"))) +} +/// The `pg_catalog` type a value of `family` is carried as: `text`, `bool`, +/// `numeric` (integers and decimals, exact), and for opt-in families +/// `timestamptz` and `float8`. +pub fn cast_type(family: &F) -> &'static str { + match family.widen() { + ScalarFamily::String => "text", + ScalarFamily::Boolean => "bool", + ScalarFamily::Integer | ScalarFamily::Decimal => "numeric", + ScalarFamily::Timestamp => "timestamptz", + ScalarFamily::Float => "float8", + } +} +/// `expression::pg_catalog.`, collated `C` when the family is string. +pub fn typed(expression: &str, family: &F) -> String { + let value = format!("{expression}::pg_catalog.{}", cast_type(family)); + if family.widen() == ScalarFamily::String { collate_c(&value) } else { value } +} +/// The exact result decoder for a column of a core family. +pub fn decoder(family: &Family) -> ScalarDecoder { + ScalarDecoder::exact(family) +} +/// The exact result decoder for a column of any family set. +pub fn decoder_for(family: &F) -> ScalarDecoder { + ScalarDecoder::exact(family) +} +/// Project `expression` (already typed as `family`) to the text its decoder +/// reads: `timestamptz` as `to_char(.. AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"')` +/// for [`ScalarDecoder::UtcInstant`]; `float8` as `encode(float8send(..), 'hex')` +/// for [`ScalarDecoder::Ieee754Binary64Hex`], independent of +/// `extra_float_digits`; integers and decimals as `::pg_catalog.text`; strings +/// and booleans unchanged. +/// +/// `to_char` gives `NULL` for infinite instants and has no era marker, so an +/// instant outside years 0001–9999 is not decodable: hosts refuse its text. +pub fn decodable(expression: &str, family: &F) -> String { + match family.widen() { + ScalarFamily::String | ScalarFamily::Boolean => expression.to_owned(), + ScalarFamily::Integer | ScalarFamily::Decimal => format!("({expression})::pg_catalog.text"), + ScalarFamily::Timestamp => format!( + "pg_catalog.to_char(({expression}) AT TIME ZONE 'UTC', 'YYYY-MM-DD\"T\"HH24:MI:SS.US\"Z\"')" + ), + ScalarFamily::Float => format!("pg_catalog.encode(pg_catalog.float8send(({expression})::pg_catalog.float8), 'hex')"), + } +} +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + use weft_model::types::Family; + #[test] + fn identifiers_quote_every_component_and_never_truncate() { + let namespace = Identifier::new("sales; DROP SCHEMA x;--").unwrap(); + let table = Identifier::new("odd.\"object").unwrap(); + assert_eq!( + qualified(&namespace, &table), + "\"sales; DROP SCHEMA x;--\".\"odd.\"\"object\"" + ); + assert_eq!(Identifier::new("MiXeD").unwrap().sql(), "\"MiXeD\""); + assert!(Identifier::new(&"é".repeat(31)).is_ok()); + assert!(Identifier::new(&"é".repeat(32)).is_err()); + assert!(Identifier::new(&"a".repeat(63)).is_ok()); + for invalid in ["", "a\0b", &"a".repeat(64)] { + assert!(Identifier::new(invalid).is_err()); + } + } + #[test] + fn catalog_slots_keep_signed_native_ids_and_refuse_overflow() { + let mut parameters = Parameters::default(); + for text in ["-2147483648", "0", "2147483647"] { + parameters + .catalog(CatalogDomain::Int, text, json!({"catalog":"type"})) + .unwrap(); + } + for text in ["-32768", "0", "32767"] { + parameters + .catalog(CatalogDomain::SmallInt, text, json!({"catalog":"key"})) + .unwrap(); + } + for (domain, text) in [ + (CatalogDomain::Int, "2147483648"), + (CatalogDomain::Int, "-2147483649"), + (CatalogDomain::SmallInt, "32768"), + (CatalogDomain::SmallInt, "-32769"), + (CatalogDomain::Int, "-0"), + (CatalogDomain::Int, "01"), + (CatalogDomain::Int, "+1"), + (CatalogDomain::Int, "1.0"), + (CatalogDomain::Int, "1;DROP TABLE x"), + ] { + assert_eq!( + parameters + .catalog(domain, text, json!({})) + .unwrap_err() + .code, + "WFT-BINDING" + ); + } + let slots = parameters.into_slots(); + assert_eq!(slots.len(), 6); + assert_eq!(slots[0].value, "-2147483648"); + assert_eq!( + slots[0].logical_type.atomic().unwrap().facets, + json!({"integerWidth":{"bits":32,"signed":true}}) + ); + assert_eq!( + slots[3].logical_type.atomic().unwrap().facets, + json!({"integerWidth":{"bits":16,"signed":true}}) + ); + } + #[test] + fn literals_casts_collation_and_decoders() { + assert_eq!(string_literal("it's").unwrap(), "'it''s'"); + assert_eq!(escape_string_literal(r"a\b'c").unwrap(), r"E'a\\b''c'"); + assert!(string_literal("a\0").is_none() && escape_string_literal("a\0").is_none()); + assert_eq!(typed("$1", &Family::String), "$1::pg_catalog.text COLLATE pg_catalog.\"C\""); + assert_eq!(typed("$2", &Family::Decimal), "$2::pg_catalog.numeric"); + assert_eq!(typed("$3", &ScalarFamily::Timestamp), "$3::pg_catalog.timestamptz"); + assert_eq!(cast_type(&ScalarFamily::Float), "float8"); + assert!(matches!(decoder(&Family::Integer), ScalarDecoder::ExactInteger)); + assert!(matches!(decoder_for(&ScalarFamily::Boolean), ScalarDecoder::Boolean)); + assert_eq!(decoder_for(&ScalarFamily::Timestamp), ScalarDecoder::UtcInstant); + assert_eq!(decoder_for(&ScalarFamily::Float), ScalarDecoder::Ieee754Binary64Hex); + assert_eq!(serde_json::to_value(ScalarDecoder::UtcInstant).unwrap(), json!("utc-instant")); + assert_eq!(serde_json::to_value(ScalarDecoder::Ieee754Binary64Hex).unwrap(), json!("ieee754-binary64-hex")); + assert_eq!(decodable("x", &ScalarFamily::String), "x"); + assert_eq!(decodable("$1", &Family::Decimal), "($1)::pg_catalog.text"); + assert_eq!( + decodable("t.at", &ScalarFamily::Timestamp), + "pg_catalog.to_char((t.at) AT TIME ZONE 'UTC', 'YYYY-MM-DD\"T\"HH24:MI:SS.US\"Z\"')" + ); + assert_eq!( + decodable("t.ratio", &ScalarFamily::Float), + "pg_catalog.encode(pg_catalog.float8send((t.ratio)::pg_catalog.float8), 'hex')" + ); + } + #[test] + fn slots_preserve_exact_values_and_origins_without_deduplicating() { + let ty = LogicalType { + family: Family::Integer, + facets: json!({"integerWidth":{"bits":64,"signed":false}}), + nullable: false, + }; + let mut slots = Parameters::default(); + for i in 0..1024 { + assert_eq!( + slots + .push( + ty.clone(), + "18446744073709551615".into(), + json!({"literal":i}) + ) + .unwrap(), + format!("${}", i + 1) + ); + } + assert_eq!( + slots.push(ty, "1".into(), json!({})).unwrap_err().code, + "WFT-LIMIT" + ); + let slots = slots.into_slots(); + assert_eq!(slots.len(), 1024); + assert_eq!(slots[1023].value, "18446744073709551615"); + assert_eq!(slots[1023].origin, json!({"literal":1023})); + } +} + diff --git a/crates/weft-postgresql/Cargo.toml b/crates/weft-postgresql/Cargo.toml index 503e6496..0b38ba9a 100644 --- a/crates/weft-postgresql/Cargo.toml +++ b/crates/weft-postgresql/Cargo.toml @@ -7,6 +7,8 @@ publish.workspace = true [dependencies] sqlparser.workspace = true weft-core = { path = "../weft-core" } +weft-expr = { path = "../weft-expr" } +weft-postgresql-emit = { path = "../weft-postgresql-emit" } serde_json.workspace = true serde.workspace = true base64 = "=0.22.1" diff --git a/crates/weft-postgresql/src/candidate.rs b/crates/weft-postgresql/src/candidate.rs index 8afb2a87..57de8479 100644 --- a/crates/weft-postgresql/src/candidate.rs +++ b/crates/weft-postgresql/src/candidate.rs @@ -368,12 +368,7 @@ impl Backend for Candidate { Identifier::new(&o.name)?.sql() )); let ty = o.expression.logical_type().clone(); - let decoder = match ty.family { - Family::String => ScalarDecoder::Text, - Family::Boolean => ScalarDecoder::Boolean, - Family::Integer => ScalarDecoder::ExactInteger, - Family::Decimal => ScalarDecoder::ExactDecimal, - }; + let decoder = weft_postgresql_emit::decoder(&ty.family); fn origins(e: &Expression) -> Vec { match e { Expression::Field { identity, .. } => vec![identity.clone()], @@ -617,17 +612,7 @@ impl Build<'_> { value.clone(), json!({"literalSpan":span}), )?; - let cast = match logical_type.family { - Family::String => "text", - Family::Boolean => "bool", - Family::Integer | Family::Decimal => "numeric", - }; - let value = format!("{slot}::pg_catalog.{cast}"); - if logical_type.family == Family::String { - format!("{value} COLLATE pg_catalog.\"C\"") - } else { - value - } + weft_postgresql_emit::typed(&slot, &logical_type.family) } Expression::Equal { .. } => format!("({} = {})", operands[0], operands[1]), Expression::And { .. } => format!("({} AND {})", operands[0], operands[1]), diff --git a/crates/weft-postgresql/src/candidate/application.rs b/crates/weft-postgresql/src/candidate/application.rs index 4af83be0..867d18f1 100644 --- a/crates/weft-postgresql/src/candidate/application.rs +++ b/crates/weft-postgresql/src/candidate/application.rs @@ -74,14 +74,7 @@ fn descriptor<'a>( .find(|d| &d.identity == id) .ok_or_else(|| fail("Selected descriptor is missing")) } -fn decoder(family: &Family) -> ScalarDecoder { - match family { - Family::String => ScalarDecoder::Text, - Family::Boolean => ScalarDecoder::Boolean, - Family::Integer => ScalarDecoder::ExactInteger, - Family::Decimal => ScalarDecoder::ExactDecimal, - } -} +use weft_postgresql_emit::decoder; pub(super) fn lower( context: &Context<'_>, mapping: &Admission, @@ -760,17 +753,7 @@ impl Build<'_> { } => (value, logical_type, json!({"parameter":name,"span":span})), }; let slot = self.parameters.push(ty.clone(), value.clone(), origin)?; - let cast = match ty.family { - Family::String => "text", - Family::Boolean => "bool", - Family::Integer | Family::Decimal => "numeric", - }; - let value = format!("{slot}::pg_catalog.{cast}"); - Ok(if ty.family == Family::String { - format!("{value} COLLATE pg_catalog.\"C\"") - } else { - value - }) + Ok(weft_postgresql_emit::typed(&slot, &ty.family)) } fn application_predicate(&mut self, p: &app::Predicate) -> Result { Ok(match p { diff --git a/crates/weft-postgresql/src/lib.rs b/crates/weft-postgresql/src/lib.rs index e6980ea0..fc6e3333 100644 --- a/crates/weft-postgresql/src/lib.rs +++ b/crates/weft-postgresql/src/lib.rs @@ -28,279 +28,9 @@ mod tree; pub mod truss_mutation; pub mod truss_runtime; pub mod value_definition; -// PostgreSQL emission primitives. Storage mappings and engine support are separate. -use weft_core::{ - backend::ParameterSlot, - error::{Diagnostic, Result}, - ir::LogicalType, -}; - -/// Exact quoted identifier in the proposed standard NAMEDATALEN=64 profile. -/// Nonstandard server builds require a separately qualified identifier profile. -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct Identifier(String); -impl Identifier { - pub fn new(value: &str) -> Result { - if value.is_empty() || value.len() > 63 || value.contains('\0') { - return Err(Diagnostic::new( - "WFT-BINDING", - "binding", - "PostgreSQL identifier is empty, contains NUL or exceeds 63 UTF-8 bytes", - )); - } - Ok(Self(value.to_owned())) - } - pub fn sql(&self) -> String { - format!("\"{}\"", self.0.replace('"', "\"\"")) - } -} -/// Namespace and object are separately quoted; dots in either remain literal. -pub fn qualified(namespace: &Identifier, object: &Identifier) -> String { - format!("{}.{}", namespace.sql(), object.sql()) -} -/// Per-query slots: values never enter emitted SQL. Common emission validation -/// remains responsible for exact lexical/domain checking before any artifact. -#[derive(Debug, Default, Clone)] -pub struct Parameters(Vec); -impl Parameters { - pub fn push( - &mut self, - logical_type: LogicalType, - value: String, - origin: serde_json::Value, - ) -> Result { - if self.0.len() >= 1024 { - return Err(Diagnostic::new( - "WFT-LIMIT", - "emit", - "PostgreSQL parameter count exceeds 1024", - )); - } - let position = self.0.len() + 1; - self.0.push(ParameterSlot { - position, - logical_type: logical_type.into(), - value, - origin, - }); - Ok(format!("${position}")) - } - pub fn push_binary(&mut self, value: String, origin: serde_json::Value) -> Result { - use base64::Engine; - if value.len() > 1048576 || self.0.len() >= 1024 { - return Err(Diagnostic::new( - "WFT-LIMIT", - "emit", - "Binary parameter bounds exceeded", - )); - } - let bytes = base64::engine::general_purpose::STANDARD - .decode(&value) - .map_err(|_| { - Diagnostic::new("WFT-BINARY-DOMAIN", "emit", "Canonical base64 required") - })?; - if base64::engine::general_purpose::STANDARD.encode(bytes) != value { - return Err(Diagnostic::new( - "WFT-BINARY-DOMAIN", - "emit", - "Canonical base64 required", - )); - } - let position = self.0.len() + 1; - self.0.push(ParameterSlot { - position, - logical_type: weft_core::backend::ParameterType::Exact { - kind: weft_core::backend::ExactParameterKind::ExactScalar, - scalar_type: weft_core::application_model::ExactScalarType::Binary, - }, - value, - origin, - }); - Ok(format!("${position}")) - } - pub fn push_timestamp(&mut self, value: String, origin: serde_json::Value) -> Result { - if value.len() > 1048576 || self.0.len() >= 1024 { - return Err(Diagnostic::new( - "WFT-LIMIT", - "emit", - "Timestamp parameter bounds exceeded", - )); - } - let position = self.0.len() + 1; - self.0.push(ParameterSlot { - position, - logical_type: weft_core::backend::ParameterType::Exact { - kind: weft_core::backend::ExactParameterKind::ExactScalar, - scalar_type: weft_core::application_model::ExactScalarType::Timestamp, - }, - value, - origin, - }); - Ok(format!("${position}")) - } - pub(crate) fn push_date(&mut self, value: String, origin: serde_json::Value) -> Result { - if value.len() != 10 || self.0.len() >= 1024 { - return Err(Diagnostic::new( - "WFT-LIMIT", - "emit", - "Date parameter bounds exceeded", - )); - } - let position = self.0.len() + 1; - self.0.push(ParameterSlot { - position, - logical_type: weft_core::backend::ParameterType::Exact { - kind: weft_core::backend::ExactParameterKind::ExactScalar, - scalar_type: weft_core::application_model::ExactScalarType::Date, - }, - value, - origin, - }); - Ok(format!("${position}")) - } - pub fn into_slots(self) -> Vec { - self.0 - } -} -/// Native catalog discriminator domain, independent of logical business keys. -/// Historical nonpositive IDs are retained; allocation policy belongs to Truss. -#[derive(Debug, Clone, Copy)] -pub enum CatalogDomain { - Int, - SmallInt, -} -impl Parameters { - pub fn catalog( - &mut self, - domain: CatalogDomain, - text: &str, - origin: serde_json::Value, - ) -> Result { - let number = text.parse::().ok(); - let bits = match domain { - CatalogDomain::Int => 32, - CatalogDomain::SmallInt => 16, - }; - let valid = number.is_some_and(|value| { - value.to_string() == text - && match domain { - CatalogDomain::Int => i32::try_from(value).is_ok(), - CatalogDomain::SmallInt => i16::try_from(value).is_ok(), - } - }); - if !valid { - return Err(Diagnostic::new( - "WFT-BINDING", - "binding", - "Catalog discriminator is not canonical text in its signed native domain", - )); - } - self.push( - LogicalType { - family: weft_core::ir::Family::Integer, - facets: serde_json::json!({"integerWidth":{"bits":bits,"signed":true}}), - nullable: false, - }, - text.into(), - origin, - ) - } -} -#[cfg(test)] -mod tests { - use super::*; - use serde_json::json; - use weft_core::ir::Family; - #[test] - fn identifiers_quote_every_component_and_never_truncate() { - let namespace = Identifier::new("sales; DROP SCHEMA x;--").unwrap(); - let table = Identifier::new("odd.\"object").unwrap(); - assert_eq!( - qualified(&namespace, &table), - "\"sales; DROP SCHEMA x;--\".\"odd.\"\"object\"" - ); - assert_eq!(Identifier::new("MiXeD").unwrap().sql(), "\"MiXeD\""); - assert!(Identifier::new(&"é".repeat(31)).is_ok()); - assert!(Identifier::new(&"é".repeat(32)).is_err()); - assert!(Identifier::new(&"a".repeat(63)).is_ok()); - for invalid in ["", "a\0b", &"a".repeat(64)] { - assert!(Identifier::new(invalid).is_err()); - } - } - #[test] - fn catalog_slots_keep_signed_native_ids_and_refuse_overflow() { - let mut parameters = Parameters::default(); - for text in ["-2147483648", "0", "2147483647"] { - parameters - .catalog(CatalogDomain::Int, text, json!({"catalog":"type"})) - .unwrap(); - } - for text in ["-32768", "0", "32767"] { - parameters - .catalog(CatalogDomain::SmallInt, text, json!({"catalog":"key"})) - .unwrap(); - } - for (domain, text) in [ - (CatalogDomain::Int, "2147483648"), - (CatalogDomain::Int, "-2147483649"), - (CatalogDomain::SmallInt, "32768"), - (CatalogDomain::SmallInt, "-32769"), - (CatalogDomain::Int, "-0"), - (CatalogDomain::Int, "01"), - (CatalogDomain::Int, "+1"), - (CatalogDomain::Int, "1.0"), - (CatalogDomain::Int, "1;DROP TABLE x"), - ] { - assert_eq!( - parameters - .catalog(domain, text, json!({})) - .unwrap_err() - .code, - "WFT-BINDING" - ); - } - let slots = parameters.into_slots(); - assert_eq!(slots.len(), 6); - assert_eq!(slots[0].value, "-2147483648"); - assert_eq!( - slots[0].logical_type.atomic().unwrap().facets, - json!({"integerWidth":{"bits":32,"signed":true}}) - ); - assert_eq!( - slots[3].logical_type.atomic().unwrap().facets, - json!({"integerWidth":{"bits":16,"signed":true}}) - ); - } - #[test] - fn slots_preserve_exact_values_and_origins_without_deduplicating() { - let ty = LogicalType { - family: Family::Integer, - facets: json!({"integerWidth":{"bits":64,"signed":false}}), - nullable: false, - }; - let mut slots = Parameters::default(); - for i in 0..1024 { - assert_eq!( - slots - .push( - ty.clone(), - "18446744073709551615".into(), - json!({"literal":i}) - ) - .unwrap(), - format!("${}", i + 1) - ); - } - assert_eq!( - slots.push(ty, "1".into(), json!({})).unwrap_err().code, - "WFT-LIMIT" - ); - let slots = slots.into_slots(); - assert_eq!(slots.len(), 1024); - assert_eq!(slots[1023].value, "18446744073709551615"); - assert_eq!(slots[1023].origin, json!({"literal":1023})); - } -} +// PostgreSQL emission primitives live in weft-postgresql-emit; storage mappings +// (the Truss layout) and engine support stay here. +pub use weft_postgresql_emit::{qualified, CatalogDomain, Identifier, Parameters}; pub mod leaf_codec_definition; diff --git a/crates/weft-postgresql/src/mutation.rs b/crates/weft-postgresql/src/mutation.rs index 0f47dec8..6d297fd1 100644 --- a/crates/weft-postgresql/src/mutation.rs +++ b/crates/weft-postgresql/src/mutation.rs @@ -11,6 +11,7 @@ use crate::{ }; use serde_json::{json, Value}; use std::collections::{BTreeMap, BTreeSet}; +use weft_expr::selection::{self, Refusal}; use weft_core::{ ir::{Family, Identity, ModelPin}, model::Catalog, @@ -51,13 +52,10 @@ fn original(result: weft_core::error::Result) -> MResult { }) } fn literal(text: &str) -> MResult { - if text.contains('\0') { - return Err(fail( - "ACTION", - "Native PostgreSQL text cannot represent NUL", - )); - } - Ok(format!("'{}'", text.replace('\'', "''"))) + weft_postgresql_emit::string_literal(text).ok_or_else(|| fail( + "ACTION", + "Native PostgreSQL text cannot represent NUL", + )) } impl OriginalMutationBackend { pub fn new( @@ -166,7 +164,7 @@ impl OriginalMutationBackend { weft_core::mutation_backend::inspect_selection(selection, model)?; let program = selection.read()?; let document = model.read()?; - if program["protocol"] != "weft.update-selection/1" { + if program["protocol"] != selection::PROTOCOL { return Err(fail("ACTION", "Exact selection profile required")); } let identity = Identity { @@ -247,41 +245,26 @@ impl OriginalMutationBackend { alias.sql(), original(Identifier::new(&self.columns.version.name))?.sql() ); - let mut refs = Vec::new(); + // inspect_selection above validated structure and domains; the shared + // decoder only re-reads the admitted predicate. + let predicate = selection::walk( + &program["predicate"], + |_| Ok(()), + |refusal| match refusal { + Refusal::Depth => fail("LIMIT", "Selection depth exceeded"), + Refusal::Comparisons => fail("LIMIT", "Comparison bound exceeded"), + Refusal::Operator => fail("ACTION", "Unknown comparison operation"), + _ => fail("ACTION", "Typed predicate node required"), + }, + )?; let mut operations: BTreeMap> = BTreeMap::new(); - let mut work = vec![(&program["predicate"], 1)]; - let mut nodes = 0; - while let Some((node, depth)) = work.pop() { - if depth > 24 { - return Err(fail("LIMIT", "Selection depth exceeded")); - } - if let Some(c) = node.get("compare") { - nodes += 1; - if nodes > 256 { - return Err(fail("LIMIT", "Comparison bound exceeded")); - } - let operation = match c["op"].as_str() { - Some("eq" | "ne") => Operation::Equality, - Some("lt" | "le" | "gt" | "ge") => Operation::Ordering, - _ => return Err(fail("ACTION", "Unknown comparison operation")), - }; - operations - .entry(weft_core::mutation::canonical(&c["field"])) - .or_default() - .insert(operation); - if !refs.contains(&c["field"]) { - refs.push(c["field"].clone()); - } - } else { - let cs = node - .get("and") - .or_else(|| node.get("or")) - .and_then(Value::as_array) - .filter(|cs| (2..=64).contains(&cs.len())) - .ok_or_else(|| fail("ACTION", "Typed predicate node required"))?; - work.extend(cs.iter().rev().map(|c| (c, depth + 1))); - } + for c in predicate.comparisons() { + operations + .entry(weft_core::mutation::canonical(&c.field)) + .or_default() + .insert(if c.op.orders() { Operation::Ordering } else { Operation::Equality }); } + let mut refs = predicate.dependencies(); for field in &key.fields { let r = json!({"module":field.module,"element":field.element}); if !refs.contains(&r) { diff --git a/crates/weft-postgresql/src/relationship_lowering.rs b/crates/weft-postgresql/src/relationship_lowering.rs index 1aad40f9..51be00ad 100644 --- a/crates/weft-postgresql/src/relationship_lowering.rs +++ b/crates/weft-postgresql/src/relationship_lowering.rs @@ -189,7 +189,7 @@ pub fn lower<'a>( logical_type: logical.clone(), span: span.clone(), }; - let before = staged.0.len(); + let before = staged.slots().len(); let right = native(&literal, &[], None, &mut staged)?; if let Value::Parameter { name, @@ -198,15 +198,15 @@ pub fn lower<'a>( span, } = value { - if staged.0.len() != before + 1 - || staged.0[before].value != *value - || staged.0[before].logical_type != *logical_type + if staged.slots().len() != before + 1 + || staged.slots()[before].value != *value + || staged.slots()[before].logical_type != *logical_type { return Err(fail( "Relationship named parameter must retain one exact typed slot", )); } - staged.0[before].origin = serde_json::json!({"parameter":name,"span":span}); + staged.set_origin(before, serde_json::json!({"parameter":name,"span":span})); } predicates.push(format!("({left})=({right})")); } diff --git a/crates/weft-postgresql/src/result_definition.rs b/crates/weft-postgresql/src/result_definition.rs index 5bb0386a..9617d9eb 100644 --- a/crates/weft-postgresql/src/result_definition.rs +++ b/crates/weft-postgresql/src/result_definition.rs @@ -3,7 +3,7 @@ use crate::property_definition::PropertyAdmission; use weft_core::{ application_model::{Descriptor, Shape}, - backend::{Column, Representation, ScalarCarrier, ScalarDecoder}, + backend::{Column, Representation, ScalarCarrier}, error::{Diagnostic, Result}, ir::Family, }; @@ -335,12 +335,7 @@ fn column(descriptor: &Descriptor, position: usize, output_name: &str) -> Result Representation::Scalar { logical_type: logical_type.clone(), carrier: ScalarCarrier::Text, - decoder: match logical_type.family { - Family::String => ScalarDecoder::Text, - Family::Boolean => ScalarDecoder::Boolean, - Family::Integer => ScalarDecoder::ExactInteger, - Family::Decimal => ScalarDecoder::ExactDecimal, - }, + decoder: weft_postgresql_emit::decoder(&logical_type.family), } } _ => Representation::Value { @@ -538,12 +533,7 @@ fn scalar_column( representation: Representation::Scalar { logical_type: logical_type.clone(), carrier: ScalarCarrier::Text, - decoder: match logical_type.family { - Family::String => ScalarDecoder::Text, - Family::Boolean => ScalarDecoder::Boolean, - Family::Integer => ScalarDecoder::ExactInteger, - Family::Decimal => ScalarDecoder::ExactDecimal, - }, + decoder: weft_postgresql_emit::decoder(&logical_type.family), }, source_identities: sources, nullable: logical_type.nullable, diff --git a/crates/weft-postgresql/src/select_definition.rs b/crates/weft-postgresql/src/select_definition.rs index a0ef605f..f2c8f04d 100644 --- a/crates/weft-postgresql/src/select_definition.rs +++ b/crates/weft-postgresql/src/select_definition.rs @@ -1144,11 +1144,11 @@ fn render_application_equality<'a>( Expression::Literal { .. }, ) = (predicate, node) { - let before = parameters.0.len(); + let before = parameters.slots().len(); let sql = native(node, operands, access, parameters)?; - if parameters.0.len() != before + 1 - || parameters.0[before].value != *value - || parameters.0[before].logical_type != *logical_type + if parameters.slots().len() != before + 1 + || parameters.slots()[before].value != *value + || parameters.slots()[before].logical_type != *logical_type { return Err(Diagnostic::new( "WFT-BINDING", @@ -1156,7 +1156,7 @@ fn render_application_equality<'a>( "Named parameter conversion must retain one exact typed value slot", )); } - parameters.0[before].origin = serde_json::json!({"parameter":name,"span":span}); + parameters.set_origin(before, serde_json::json!({"parameter":name,"span":span})); Ok(sql) } else { native(node, operands, access, parameters) diff --git a/crates/weft-postgresql/src/truss_runtime.rs b/crates/weft-postgresql/src/truss_runtime.rs index 90233b62..5068eec5 100644 --- a/crates/weft-postgresql/src/truss_runtime.rs +++ b/crates/weft-postgresql/src/truss_runtime.rs @@ -244,13 +244,7 @@ fn native(id: &str) -> Result<()> { Ok(()) } fn literal(s: &str) -> Result { - if s.contains('\0') { - return Err(fail("NUL cannot enter PostgreSQL text")); - } - Ok(format!( - "E'{}'", - s.replace('\\', "\\\\").replace('\'', "''") - )) + weft_postgresql_emit::escape_string_literal(s).ok_or_else(|| fail("NUL cannot enter PostgreSQL text")) } fn cursor_obligation()->Obligation { Obligation { id:"truss.runtime.cursor-export/0.1".into(),owner:ObligationOwner::Host,failure_code:"WFT-OBLIGATION".into(), @@ -926,14 +920,7 @@ impl Backend for Runtime { }) } } -fn decoder(f: &Family) -> ScalarDecoder { - match f { - Family::String => ScalarDecoder::Text, - Family::Boolean => ScalarDecoder::Boolean, - Family::Integer => ScalarDecoder::ExactInteger, - Family::Decimal => ScalarDecoder::ExactDecimal, - } -} +use weft_postgresql_emit::decoder; struct Build<'a> { mapping: &'a Mapping, plan: &'a app::Plan, @@ -1054,11 +1041,7 @@ impl Build<'_> { let slot = self .parameters .push(logical_type.clone(), value.clone(), origin.unwrap_or_else(|| json!(v)))?; - Ok(match logical_type.family { - Family::String => format!("{slot}::pg_catalog.text COLLATE pg_catalog.\"C\""), - Family::Boolean => format!("{slot}::pg_catalog.bool"), - Family::Integer | Family::Decimal => format!("{slot}::pg_catalog.numeric"), - }) + Ok(weft_postgresql_emit::typed(&slot, &logical_type.family)) } } } diff --git a/docs/helix/02-design/architecture.md b/docs/helix/02-design/architecture.md index 405fd1aa..239414db 100644 --- a/docs/helix/02-design/architecture.md +++ b/docs/helix/02-design/architecture.md @@ -102,9 +102,13 @@ never establishes semantic conformance. | Module | Responsibility / Owned Types | Public API | Allowed Dependencies | Forbidden Dependencies | | --- | --- | --- | --- | --- | -| `crates/weft-core` | Original owning-version model interpretation, logical AST/IR, capability gates and compile protocol | Compiler/Registry, CONTRACT-001–003 | Locked external compiler and UMF semantic libraries; no workspace imports | Backend crates, embedding crates, storage/host implementations | +| `crates/weft-model` | UMF 0.10 document preparation, identity resolution, logical types and family sets, exact literals and instants (own semver; [README](../../../crates/weft-model/README.md)) | `Catalog`, `Admission`, `LogicalType`, `check_literal` | Locked serialization, digest and schema libraries; no workspace imports | weft-core, other compilers (Fireweed) | +| `crates/weft-expr` | Typed Field/Value operands, comparisons, exact arithmetic domains, `weft.update-selection/1` codec (own semver; [README](../../../crates/weft-expr/README.md)) | `operand`, `compare`, `arithmetic`, `selection` | `weft-model`; locked serialization libraries | weft-core, backends, other compilers (Fireweed) | +| `crates/weft-backend` | CONTRACT-002 manifests, `Frontend`/`Backend`/`TargetArtifact` traits, checked compile pipeline, registry, SQL emission carrier (own semver; [README](../../../crates/weft-backend/README.md)) | `manifest`, `boundary`, `sql` | `weft-model`; locked serialization libraries | weft-core, other compilers (Fireweed) | +| `crates/weft-postgresql-emit` | PostgreSQL identifier quoting, parameter slots, string literals, typed casts, `C` collation, result decoders (own semver; [README](../../../crates/weft-postgresql-emit/README.md)) | `Identifier`, `Parameters`, `typed`, `decoder` | `weft-model`, `weft-backend`; locked encoding libraries | weft-postgresql, other compilers' PostgreSQL bindings (Fireweed) | +| `crates/weft-core` | Dialect parsing, logical AST/IR, capability gates and compile protocol; re-exports weft-model under its historical module paths | Compiler/Registry, CONTRACT-001–003 | `weft-model`, `weft-expr`, `weft-backend`; locked external compiler and UMF semantic libraries | Backend crates, embedding crates, storage/host implementations | | `crates/weft-databricks` | Target binding, finite native representation, SQL/decoder obligations; `count_having` and `left_join` own their separate explicit profiles | Backend implementations of CONTRACT-002 | `weft-core`; locked serialization dependencies | Runtime/wrappers, PostgreSQL adapter, live storage clients | -| `crates/weft-postgresql` | PostgreSQL target meanings and SQL/obligations | Backend implementations of CONTRACT-002 | `weft-core`; locked target dependencies | Runtime/wrappers, Databricks adapter, live storage clients | +| `crates/weft-postgresql` | PostgreSQL target meanings and SQL/obligations | Backend implementations of CONTRACT-002; the Truss storage layout | `weft-core`, `weft-expr`, `weft-postgresql-emit`; locked target dependencies | Runtime/wrappers, Databricks adapter, live storage clients | | `tests/register-backend/probe` | Independent conformance backend | Registered test Backend | `weft-core` | Concrete production adapters/runtime | | `tests/frontend/wasm-probe` | Browser core probe entrypoint | WASM probe exports | `weft-core` | Target adapters/runtime | | `crates/weft-runtime` | Explicit feature-selected compiler composition and portable entrypoint | `compile_json`; explicit `implicit_arithmetic::compile_json` | `weft-core`, Databricks/PostgreSQL adapters, backend probe | Python/WASM wrappers, storage/host implementations | @@ -151,7 +155,7 @@ execution authority remain Truss/host responsibilities. Module-map adoption establishes source ownership and lexical inventory, not native, feature-union, package or release qualification. -**Integration Owners**: UMF document interpretation -> `weft-core` owning-version reader; Databricks physical translation -> `weft-databricks`; PostgreSQL physical translation -> `weft-postgresql`; Python/browser translation -> their thin wrappers. External execution, original public UMF receipts, native source guards and authority/ACK admission belong to the caller host, not these compiler packages. +**Integration Owners**: UMF document interpretation -> `weft-model` owning-version reader, consumed through `weft-core`; Databricks physical translation -> `weft-databricks`; PostgreSQL physical translation -> `weft-postgresql`; Python/browser translation -> their thin wrappers. External execution, original public UMF receipts, native source guards and authority/ACK admission belong to the caller host, not these compiler packages. **Construction Policy**: `weft-runtime` registers explicitly compiled features; request backend ID/version/profile selects registered code. A SQL expression MUST NOT choose or silently switch a backend. Existing library consumers may construct their own Registry through CONTRACT-002. diff --git a/docs/helix/02-design/contracts/CONTRACT-002-backend-interface.md b/docs/helix/02-design/contracts/CONTRACT-002-backend-interface.md index b3c2f022..9808d3ea 100644 --- a/docs/helix/02-design/contracts/CONTRACT-002-backend-interface.md +++ b/docs/helix/02-design/contracts/CONTRACT-002-backend-interface.md @@ -68,7 +68,14 @@ The backend allocates parameter slots once from typed literals and discriminator values. PostgreSQL `$n` and Databricks `:pn` are initial emission conventions; values are never interpolated into SQL text. Identifier escaping, identifier length/version limits and physical discriminator filters are target-owned and -must be covered by native evidence. Result numeric carriers must remain exact text. +must be covered by native evidence. Result numeric carriers must remain exact text. The PostgreSQL conventions (quoted +identifiers, `$n` slots, string literals, typed casts with the `C` collation and +exact decoders) are implemented once in the `weft-postgresql-emit` crate; storage +layouts such as Truss's are separate bindings over it. For the opt-in UMF `timestamp` +and `float` families (not admitted by any weft dialect profile), `Emission` +columns use the exact text decoders `utc-instant` (`YYYY-MM-DDTHH:MM:SS.ffffffZ`) and +`ieee754-binary64-hex` (the binary64 bit pattern, most significant byte first); these +are experimental and covered by unit tests only. ## Initial Backend Profiles @@ -183,6 +190,13 @@ unknown members, repeated IDs, mismatched pairs, missing domains and supported capabilities without declared evidence refuse. A declared evidence ID remains a trusted qualification reference; the compiler does not fetch or execute it. +The checked pipeline is implemented once, in the `weft-backend` crate +(`weft_backend::boundary`): a `Frontend` supplies the typed program and the +manifest rules, a `Backend` returns any `TargetArtifact` from `emit` (weft's +backends return the SQL `Emission`; a backend that emits nothing returns +`NoArtifact`), and `Registry` runs the checks below in a fixed order. weft's +frontend is `weft_core::backend::WeftPlans`. + The Rust `Backend` trait in `crates/weft-core/src/backend.rs` uses associated `Mapping` and `TargetPlan` types. Each registered implementation owns those types; the heterogeneous registry erases them only around its generic adapter, which diff --git a/docs/helix/02-design/contracts/CONTRACT-009-mutation-action-plans.md b/docs/helix/02-design/contracts/CONTRACT-009-mutation-action-plans.md index acb91a70..14371120 100644 --- a/docs/helix/02-design/contracts/CONTRACT-009-mutation-action-plans.md +++ b/docs/helix/02-design/contracts/CONTRACT-009-mutation-action-plans.md @@ -61,6 +61,8 @@ MutationResponse is `{interfaceVersion:"weft-mutation-compile/0.1.0",status:"com Selection artifact mediaType is application/json and its utf8 decodes as `{protocol:"weft.update-selection/1",record:RecordRef,key:KeyRef,predicate:Predicate}`. Predicate is exactly `{compare:{field:FieldRef,op:"eq"|"ne"|"lt"|"le"|"gt"|"ge",value:CoreLiteral}}`, `{and:Predicate[]}` or `{or:Predicate[]}`; and/or arrays have 2..64 operands. No raw SQL, free property-path or executable text is admitted as an AST node. All refs are local under modelPin; key equals plan.target. dependencies are all predicate fields once in first source-use order. Backend assessment/lowering must use this exact profile and supply independent semantic/native evidence; new executor registration requires no frontend switch by backend ID. +The predicate format is implemented once, in the `weft-expr` crate's `selection` module (`weft_expr::selection`: `walk`, `Predicate`, `Refusal`, the bounds above as constants). The frontend builds selections with it, `weft_core::mutation_backend::inspect_selection` decodes them with it before checking each comparison against the original Field domain, and the PostgreSQL lowering re-reads admitted selections with it. + Compiler consumes the host-installed `umf.actions.plan-model/1` preparation artifact generated by CONTRACT-902's original Registry-backed inspector for THESE model/action bytes. It checks original byte/digest/identity correspondence and matches the SQL's complete SET destination/input list to the artifact's exact inspected binding map. This bounded transport/matching component is implemented in Rust against UMF-owned schemas and independent positive/refusal corpus; it does not call TypeScript, spawn a JavaScript sidecar or reinterpret arbitrary action AST/authorization semantics. Arbitrary new declarations require new owner inspection/export before compilation; a corpus or client-set eligibility flag cannot substitute for that preparation. It cannot choose a different action, change authorization or infer a business operation from a field name. Refusal for mismatch occurs before producing a compiled plan. Directly supplied typed plans must still pass upstream inspection; compilation is not execution admission. ### Selection backend boundary diff --git a/docs/helix/02-design/module-boundaries.json b/docs/helix/02-design/module-boundaries.json index 85f0fb33..0652e7e6 100644 --- a/docs/helix/02-design/module-boundaries.json +++ b/docs/helix/02-design/module-boundaries.json @@ -1,6 +1,6 @@ { "version": 1, - "scope": "All eleven Cargo workspace packages, canonical source/manifests and path dependencies, complete direct dependency identity (source/version/alias/target/features), all src Rust files and conservative lexical public declarations. No complete Rust name-resolution or intra-core dependency proof.", + "scope": "All Cargo workspace packages, canonical source/manifests and path dependencies, complete direct dependency identity (source/version/alias/target/features), all src Rust files and conservative lexical public declarations. No complete Rust name-resolution or intra-core dependency proof.", "packages": [ { "name": "weft-databricks", @@ -614,7 +614,7 @@ "name": "weft-core", "manifest": "crates/weft-core/Cargo.toml", "responsibility": "Semantic compiler", - "ownedTypes": "Catalog, prepared documents, exact literals, atomic reports and blocked security simulation", + "ownedTypes": "SQL dialect parsing, resolution, logical plans, backend ports, atomic reports and blocked security simulation; re-exports weft-model under its historical module paths", "constructionAndIntegration": "Runtime and backend implementations consume explicit pure-library ports; src/bin/frontend.rs is a scoped host-owned probe with stdin/stdout IO.", "dependencies": [ { @@ -751,6 +751,42 @@ ], "defaultFeatures": false, "path": null + }, + { + "name": "weft-backend", + "kind": "normal", + "optional": false, + "source": null, + "req": "*", + "rename": null, + "target": null, + "features": [], + "defaultFeatures": true, + "path": "crates/weft-backend" + }, + { + "name": "weft-expr", + "kind": "normal", + "optional": false, + "source": null, + "req": "*", + "rename": null, + "target": null, + "features": [], + "defaultFeatures": true, + "path": "crates/weft-expr" + }, + { + "name": "weft-model", + "kind": "normal", + "optional": false, + "source": null, + "req": "*", + "rename": null, + "target": null, + "features": [], + "defaultFeatures": true, + "path": "crates/weft-model" } ], "sources": [ @@ -770,12 +806,7 @@ "struct:EntityProjection", "field:facts", "field:relationships", - "struct:Field", - "field:scan", - "field:identity", - "field:logical_type", - "field:span", - "enum:Value", + "use:weft_expr : : operand : : { Field , Value }", "struct:ExactField", "field:scan", "field:identity", @@ -791,7 +822,7 @@ "field:right", "field:on", "enum:Expression", - "enum:NativeMetadata", + "use:weft_backend : : sql : : NativeMetadata", "struct:Output", "field:name", "field:expression", @@ -847,45 +878,7 @@ { "path": "crates/weft-core/src/application_model.rs", "publicDeclarations": [ - "struct:Member", - "field:name", - "field:identity", - "enum:Shape", - "enum:ExactScalarType", - "struct:Descriptor", - "field:identity", - "field:availability", - "field:shape", - "struct:EntityDescriptor", - "field:members", - "field:graph", - "struct:AuthoredKey", - "field:id", - "field:fields", - "field:types", - "fn:record_members", - "fn:entity_descriptor", - "fn:member_descriptor", - "fn:member_descriptor_by_identity", - "fn:authored_key", - "struct:RelationshipIdentity", - "field:document_id", - "field:revision", - "field:module", - "field:relationship", - "struct:RelationshipRead", - "field:identity", - "field:inverse", - "field:from", - "field:to", - "field:source_key", - "field:target_key", - "field:source_multiplicity", - "field:target_multiplicity", - "field:target_lifecycle", - "fn:relationship_read", - "fn:relationship_read_identity", - "fn:scalar_descriptor_by_identity" + "use:weft_model : : descriptor : : *" ], "pathReferences": [] }, @@ -1130,9 +1123,7 @@ { "path": "crates/weft-core/src/arithmetic_plan.rs", "publicDeclarations": [ - "enum:ComparisonOperator", - "fn:sql", - "fn:capability", + "use:weft_expr : : compare : : ComparisonOperator", "enum:Predicate", "enum:JoinKind", "struct:Join", @@ -1206,12 +1197,7 @@ { "path": "crates/weft-core/src/arithmetic_resolve.rs", "publicDeclarations": [ - "enum:Domain", - "struct:Expression", - "field:domain", - "field:span", - "field:kind", - "enum:Kind" + "use:weft_expr : : arithmetic : : { Domain , Expression , Kind }" ], "pathReferences": [] }, @@ -1227,42 +1213,7 @@ { "path": "crates/weft-core/src/backend.rs", "publicDeclarations": [ - "struct:LanguageProfile", - "field:dialect_profile", - "field:ir_version", - "struct:TargetProfile", - "field:id", - "field:engine", - "field:engine_version", - "field:session_settings", - "field:storage_layout_revision", - "field:publication_revision", - "enum:Status", - "enum:ObligationOwner", - "struct:Obligation", - "field:id", - "field:parameters", - "field:owner", - "field:failure_code", - "struct:Capability", - "field:id", - "field:target_profiles", - "field:language_profiles", - "field:logical_domain", - "field:result_domain", - "field:constraints", - "field:obligations", - "field:status", - "field:evidence", - "struct:Manifest", - "field:backend_id", - "field:backend_version", - "field:interface_version", - "field:language_profiles", - "field:binding_profile", - "field:target_profiles", - "field:capabilities", - "field:evidence", + "use:weft_backend : : manifest : : { Capability , LanguageProfile , Manifest , Obligation , ObligationOwner , Status , TargetProfile , }", "fn:validate_manifest_json", "fn:validate_manifest", "trait:Backend", @@ -1270,74 +1221,12 @@ "fn:language", "fn:capabilities", "fn:pins", - "struct:BindingInput", - "field:profile", - "field:json", - "field:sha256", - "struct:Target", - "field:backend_id", - "field:backend_version", - "field:profile_id", - "field:allow_candidate", - "struct:Selection", - "field:records", - "field:fields", - "field:types", - "field:relationships", - "struct:Context", - "field:catalog", - "field:plan", - "field:target", - "field:binding", - "field:binding_value", - "field:selection", - "struct:Validated", - "field:mapping", - "field:additional_capabilities", - "field:coverage", - "field:obligations", - "struct:Assessment", - "field:id", - "field:status", - "field:evidence", - "field:obligations", - "enum:ParameterType", - "enum:ExactParameterKind", - "fn:atomic", - "struct:ParameterSlot", - "field:position", - "field:logical_type", - "field:value", - "field:origin", - "enum:ScalarCarrier", - "enum:ScalarDecoder", - "struct:OuterJoin", - "field:scan", - "field:record", - "enum:Representation", - "struct:Column", - "field:position", - "field:output_name", - "field:carrier_name", - "field:representation", - "field:source_identities", - "field:nullable", - "struct:Emission", - "field:sql", - "field:parameters", - "field:columns", - "field:obligations", - "struct:Qualification", - "field:assessment", - "field:declaration", - "struct:Compilation", - "field:backend_id", - "field:backend_version", - "field:target_profile", - "field:binding_profile", - "field:binding_sha256", - "field:qualifications", - "field:emission", + "use:weft_backend : : boundary : : { Assessment , BindingInput , Selection , Target , Validated }", + "type:Context", + "use:weft_backend : : sql : : { Column , Emission , ExactParameterKind , OuterJoin , ParameterSlot , ParameterType , Representation , ScalarCarrier , ScalarDecoder , }", + "use:weft_backend : : boundary : : Qualification", + "type:Compilation", + "struct:WeftPlans", "struct:Registry", "fn:register", "fn:manifest", @@ -1739,38 +1628,22 @@ { "path": "crates/weft-core/src/current_string_domain.rs", "publicDeclarations": [ - "struct:Length", - "fn:min", - "fn:max", - "fn:bounds", - "fn:supported", - "fn:validate", - "fn:validate_facets", - "fn:same_domain", - "fn:literal_compatible" + "use:weft_model : : string_domain : : *" ], "pathReferences": [] }, { "path": "crates/weft-core/src/error.rs", "publicDeclarations": [ - "struct:Diagnostic", - "field:code", - "field:severity", - "field:message", - "field:phase", - "field:source_span", - "fn:new", - "fn:at", - "type:Result" + "use:weft_model : : diagnostic : : *" ], "pathReferences": [] }, { "path": "crates/weft-core/src/exact.rs", "publicDeclarations": [ - "fn:literal", - "fn:validate_binary_token" + "use:weft_model : : exact : : validate_binary_token", + "fn:literal" ], "pathReferences": [] }, @@ -1782,24 +1655,7 @@ { "path": "crates/weft-core/src/ir.rs", "publicDeclarations": [ - "struct:Span", - "field:start", - "field:end", - "struct:Identity", - "field:document_id", - "field:revision", - "field:module", - "field:element", - "struct:ModelPin", - "field:document_id", - "field:revision", - "field:umf_version", - "field:sha256", - "enum:Family", - "struct:LogicalType", - "field:family", - "field:facets", - "field:nullable", + "use:weft_model : : types : : { Family , Identity , LogicalType , ModelPin , Span }", "enum:Expression", "fn:logical_type", "fn:span", @@ -1819,9 +1675,7 @@ { "path": "crates/weft-core/src/json.rs", "publicDeclarations": [ - "fn:sha256", - "fn:checked_json", - "fn:checked_truss_runtime_catalog_json" + "use:weft_model : : json : : *" ], "pathReferences": [] }, @@ -1898,25 +1752,7 @@ { "path": "crates/weft-core/src/model.rs", "publicDeclarations": [ - "struct:ModuleInput", - "field:document_json", - "field:pin", - "field:selected_module_ids", - "struct:Catalog", - "struct:Record", - "field:identity", - "field:pin", - "field:value", - "field:document", - "fn:sdk_scalar_presence", - "fn:inputs", - "fn:prepare_exact_numbers", - "fn:prepare", - "fn:prepare_security", - "fn:pins", - "fn:record_by_identity", - "fn:record", - "fn:field" + "use:weft_model : : catalog : : *" ], "pathReferences": [] }, @@ -2606,13 +2442,7 @@ { "path": "crates/weft-core/src/security_literals.rs", "publicDeclarations": [ - "enum:ScalarLiteral", - "struct:PreparedLiteral", - "fn:new", - "fn:check", - "fn:generated_query_refinements", - "fn:check_literal", - "fn:validate_field" + "use:weft_model : : core_literal : : { check_literal , validate_field , PreparedLiteral , ScalarLiteral }" ], "pathReferences": [] }, @@ -2958,22 +2788,13 @@ { "path": "crates/weft-core/src/syntax.rs", "publicDeclarations": [ - "struct:Name", - "field:value", - "field:quoted", - "field:span", - "fn:matches", - "fn:key", + "use:weft_model : : name : : Name", "struct:Column", "field:unqualified", "field:alias", "field:field", "field:span", - "enum:LiteralKind", - "struct:Literal", - "field:value", - "field:kind", - "field:span", + "use:weft_model : : exact : : { Literal , LiteralKind }", "enum:Operand", "struct:Predicate", "field:left", @@ -3195,11 +3016,11 @@ ] }, { - "name": "weft-postgresql", - "manifest": "crates/weft-postgresql/Cargo.toml", - "responsibility": "PostgreSQL backend", - "ownedTypes": "PostgreSQL lowering and receipt replay", - "constructionAndIntegration": "runtime chooses this backend; no Python/WASM dependency", + "name": "weft-model", + "manifest": "crates/weft-model/Cargo.toml", + "responsibility": "Shared UMF model layer (own semver; see crates/weft-model/README.md)", + "ownedTypes": "Catalog, Admission, prepared documents, identities, logical types and family sets, descriptors, diagnostics, exact literals and instants", + "constructionAndIntegration": "Pure library with no workspace dependencies; weft-core and other compilers prepare catalogs and select admission explicitly", "dependencies": [ { "name": "base64", @@ -3229,231 +3050,1052 @@ "path": null }, { - "name": "serde", + "name": "num-bigint", "kind": "normal", "optional": false, "source": "registry+https://github.com/rust-lang/crates.io-index", - "req": "=1.0.228", + "req": "=0.4.8", "rename": null, "target": null, - "features": [ - "derive" - ], + "features": [], "defaultFeatures": true, "path": null }, { - "name": "serde_json", + "name": "serde", "kind": "normal", "optional": false, "source": "registry+https://github.com/rust-lang/crates.io-index", - "req": "=1.0.145", + "req": "=1.0.228", "rename": null, "target": null, "features": [ - "arbitrary_precision", - "raw_value" + "derive" ], "defaultFeatures": true, "path": null }, { - "name": "sqlparser", + "name": "serde_json", "kind": "normal", "optional": false, "source": "registry+https://github.com/rust-lang/crates.io-index", - "req": "=0.63.0", + "req": "=1.0.145", "rename": null, "target": null, "features": [ - "std", - "visitor" + "arbitrary_precision", + "raw_value" ], - "defaultFeatures": false, + "defaultFeatures": true, "path": null }, { - "name": "weft-core", + "name": "sha2", "kind": "normal", "optional": false, - "source": null, - "req": "*", + "source": "registry+https://github.com/rust-lang/crates.io-index", + "req": "=0.10.9", "rename": null, "target": null, "features": [], "defaultFeatures": true, - "path": "crates/weft-core" + "path": null } ], "sources": [ { - "path": "crates/weft-postgresql/src/access.rs", + "path": "crates/weft-model/src/catalog.rs", "publicDeclarations": [ - "struct:ScalarAccess", + "struct:ModuleInput", + "field:document_json", + "field:pin", + "field:selected_module_ids", + "struct:Catalog", + "struct:Admission", + "field:exact_numbers", + "field:field_projection_decimal38", + "field:sdk_scalar_presence", + "field:security_inputs", + "field:identity_name_fallback", + "field:raw_read_opaque_extensions", + "field:native_operation_domains", + "field:logical_defaults020", + "field:exact_scalars", + "field:exact_dates", + "field:bounded_current_strings", + "field:nullable_fields", + "fn:scalar_rules", + "struct:Record", + "field:identity", + "field:pin", "field:value", - "field:present", - "field:native_null", - "field:integrity", - "field:joins", - "struct:ObjectProperty", - "field:namespace", - "field:owner_alias", - "field:type_id", - "field:property_id", - "field:logical_type", - "fn:props", - "fn:row", - "fn:require_scalar_domain" + "field:document", + "fn:selected010", + "fn:selected010_raw_read", + "fn:selected010_table_operation", + "fn:selected010_table_operation020", + "fn:sdk_scalar_presence", + "fn:admission", + "fn:admit", + "fn:prepare_with", + "fn:documents", + "fn:verify_retained_sources", + "fn:query_name", + "fn:validate_selected010", + "fn:inputs", + "fn:prepare_exact_numbers", + "fn:prepare", + "fn:prepare_truss_runtime", + "fn:prepare_security", + "fn:prepare_raw_read010", + "fn:prepare_raw_read010_finite_current_aggregate", + "fn:prepare_table_operation010", + "fn:prepare_table_operation020", + "fn:prepare_security_bounded_strings", + "fn:local_element_by_ids", + "fn:pins", + "fn:record_by_identity", + "fn:record", + "fn:field", + "fn:field_as", + "fn:field03", + "struct:ScalarRules", + "field:mathematical_integer", + "field:mathematical_decimal", + "field:bounded_strings", + "field:decimal38", + "field:nullable_fields", + "fn:scalar_type", + "fn:scalar_type_projected", + "fn:scalar_type_with" ], "pathReferences": [] }, { - "path": "crates/weft-postgresql/src/binding.rs", + "path": "crates/weft-model/src/core_literal.rs", "publicDeclarations": [ - "struct:Admission", - "field:value", - "field:original_json", - "field:artifacts", - "fn:parse", - "fn:decoded_json", - "enum:PropertyHome", - "fn:original_home_definition", - "fn:property_home" + "const:MAX_TEXT", + "enum:ScalarLiteral", + "struct:PreparedLiteral", + "fn:new", + "fn:from_validated", + "fn:check", + "fn:generated_query_refinements", + "fn:coefficient", + "fn:unsigned", + "fn:normalized_facets", + "fn:check_literal", + "fn:validate_field", + "fn:normalized_literal", + "fn:field_domain" ], "pathReferences": [] }, { - "path": "crates/weft-postgresql/src/candidate.rs", + "path": "crates/weft-model/src/descriptor.rs", "publicDeclarations": [ - "const:PROFILE", - "struct:Candidate", - "struct:Mapping", - "struct:TargetPlan" + "struct:Member", + "field:name", + "field:identity", + "enum:Shape", + "enum:ExactScalarType", + "struct:Descriptor", + "field:identity", + "field:availability", + "field:shape", + "struct:EntityDescriptor", + "field:members", + "field:graph", + "struct:AuthoredKey", + "field:id", + "field:fields", + "field:types", + "fn:local_element", + "fn:member_name_matches", + "fn:record_members", + "fn:entity_descriptor", + "fn:entity_descriptor_as", + "fn:member_descriptor", + "fn:member_descriptor_as", + "fn:member_descriptor_by_identity", + "fn:member_descriptor_by_identity_as", + "fn:authored_key", + "fn:authored_key_as", + "struct:RelationshipIdentity", + "field:document_id", + "field:revision", + "field:module", + "field:relationship", + "struct:RelationshipRead", + "field:identity", + "field:inverse", + "field:from", + "field:to", + "field:source_key", + "field:target_key", + "field:source_multiplicity", + "field:target_multiplicity", + "field:target_lifecycle", + "fn:entity_relationships", + "fn:relationship_read", + "fn:relationship_read_identity", + "fn:scalar_descriptor_by_identity", + "fn:scalar_descriptor_by_identity_as" ], "pathReferences": [] }, { - "path": "crates/weft-postgresql/src/candidate/application.rs", - "publicDeclarations": [], - "pathReferences": [] - }, - { - "path": "crates/weft-postgresql/src/candidate/relationships.rs", - "publicDeclarations": [], + "path": "crates/weft-model/src/diagnostic.rs", + "publicDeclarations": [ + "enum:ResolutionReason", + "fn:code", + "struct:Diagnostic", + "field:code", + "field:severity", + "field:message", + "field:phase", + "field:source_span", + "field:resolution_reason", + "fn:new", + "fn:at", + "fn:column", + "fn:unavailable_source", + "type:Result" + ], "pathReferences": [] }, { - "path": "crates/weft-postgresql/src/collection.rs", + "path": "crates/weft-model/src/exact.rs", "publicDeclarations": [ - "enum:Kind", - "struct:CollectionProperty", - "field:namespace", - "field:owner", - "field:type_id", - "field:property_id", - "field:item", + "enum:LiteralKind", + "struct:Literal", + "field:value", "field:kind", - "fn:props", - "fn:row" + "field:span", + "const:MAX_TOKEN", + "fn:check_literal", + "fn:validate_float_token", + "fn:coefficient", + "fn:validate_mathematical_decimal", + "fn:validate_binary_token", + "fn:validate_date_token" ], "pathReferences": [] }, { - "path": "crates/weft-postgresql/src/comparator_requirements.rs", + "path": "crates/weft-model/src/exact_instant.rs", "publicDeclarations": [ - "struct:Requirement", - "field:owner", - "field:identity", - "field:logical_type", - "field:operations", - "fn:registration_key", - "fn:collect", - "fn:admit", - "fn:collect_reads", - "fn:admit_context", - "fn:admit_properties" + "const:PROFILE", + "fn:key" ], "pathReferences": [] }, { - "path": "crates/weft-postgresql/src/conformance_original.rs", + "path": "crates/weft-model/src/json.rs", "publicDeclarations": [ - "fn:configuration", - "fn:registry", - "fn:registry_with_configuration" + "fn:sha256", + "fn:checked_json", + "fn:checked_truss_runtime_catalog_json", + "fn:checked_json_bounded", + "fn:checked_json_bounded_charged" ], "pathReferences": [] }, { - "path": "crates/weft-postgresql/src/current_presence.rs", + "path": "crates/weft-model/src/lib.rs", "publicDeclarations": [ - "struct:CurrentPresenceSql", - "fn:context", - "fn:sql", - "fn:json", - "fn:lower", - "fn:check" + "mod:catalog", + "mod:core_literal", + "mod:descriptor", + "mod:diagnostic", + "mod:exact", + "mod:exact_instant", + "mod:json", + "mod:name", + "mod:string_domain", + "mod:types", + "const:VERSION" ], "pathReferences": [] }, { - "path": "crates/weft-postgresql/src/current_scalar.rs", + "path": "crates/weft-model/src/name.rs", "publicDeclarations": [ - "struct:Parameter", - "field:ordinal", - "field:source_path", - "field:name", + "struct:Name", "field:value", - "field:logical_type", - "field:native_type", - "struct:ResultColumn", - "field:position", - "field:name", - "field:path", - "field:source_field", - "field:effective_domain", - "field:logical_type", - "field:exposure", - "field:encoding", - "struct:CurrentScalarSql", - "fn:sql", - "fn:parameters", - "fn:columns", - "fn:json_string", - "fn:json", - "fn:lower", - "fn:lower_rows", - "fn:lower_contributors", - "fn:lower_original_sum", - "fn:lower_bounded_rows", - "fn:lower_bounded_contributors", - "fn:check_rows", - "fn:check_contributors", - "fn:check_original_sum", - "fn:check_bounded_rows", - "fn:check_bounded_contributors" + "field:quoted", + "field:span", + "fn:matches", + "fn:key", + "fn:quoted" ], "pathReferences": [] }, { - "path": "crates/weft-postgresql/src/dbapi.rs", - "publicDeclarations": [], - "pathReferences": [] - }, - { - "path": "crates/weft-postgresql/src/expression.rs", + "path": "crates/weft-model/src/string_domain.rs", "publicDeclarations": [ - "fn:render" + "struct:Length", + "fn:min", + "fn:max", + "fn:bounds", + "fn:supported", + "fn:validate", + "fn:validate_facets", + "fn:same_domain", + "fn:literal_compatible" ], "pathReferences": [] }, { - "path": "crates/weft-postgresql/src/json_codec.rs", + "path": "crates/weft-model/src/types.rs", "publicDeclarations": [ - "struct:Encoded", - "field:value", - "field:integrity", - "fn:encode" + "struct:Span", + "field:start", + "field:end", + "struct:Identity", + "field:document_id", + "field:revision", + "field:module", + "field:element", + "struct:ModelPin", + "field:document_id", + "field:revision", + "field:umf_version", + "field:sha256", + "enum:Family", + "enum:ScalarFamily", + "fn:from_umf", + "fn:umf_name", + "trait:FamilySet", + "struct:LogicalType", + "field:family", + "field:facets", + "field:nullable", + "type:ScalarType", + "fn:plain", + "fn:widen", + "fn:narrow" + ], + "pathReferences": [] + } + ] + }, + { + "name": "weft-expr", + "manifest": "crates/weft-expr/Cargo.toml", + "responsibility": "Shared typed expression and predicate core (own semver; see crates/weft-expr/README.md)", + "ownedTypes": "Field and Value operands, CompareOp and ComparisonOperator, exact arithmetic Domain/Expression/Kind, weft.update-selection/1 Predicate", + "constructionAndIntegration": "Pure library over weft-model; weft-core and backends build and decode these values, other compilers reuse them", + "dependencies": [ + { + "name": "serde", + "kind": "normal", + "optional": false, + "source": "registry+https://github.com/rust-lang/crates.io-index", + "req": "=1.0.228", + "rename": null, + "target": null, + "features": [ + "derive" + ], + "defaultFeatures": true, + "path": null + }, + { + "name": "serde_json", + "kind": "normal", + "optional": false, + "source": "registry+https://github.com/rust-lang/crates.io-index", + "req": "=1.0.145", + "rename": null, + "target": null, + "features": [ + "arbitrary_precision", + "raw_value" + ], + "defaultFeatures": true, + "path": null + }, + { + "name": "weft-model", + "kind": "normal", + "optional": false, + "source": null, + "req": "*", + "rename": null, + "target": null, + "features": [], + "defaultFeatures": true, + "path": "crates/weft-model" + } + ], + "sources": [ + { + "path": "crates/weft-expr/src/arithmetic.rs", + "publicDeclarations": [ + "const:MAX_LITERAL_BYTES", + "enum:Domain", + "fn:scale", + "fn:combine", + "fn:of_field", + "struct:Expression", + "field:domain", + "field:span", + "field:kind", + "enum:Kind", + "fn:token_domain" + ], + "pathReferences": [] + }, + { + "path": "crates/weft-expr/src/compare.rs", + "publicDeclarations": [ + "enum:CompareOp", + "const:ALL", + "fn:parse", + "fn:name", + "fn:sql", + "fn:orders", + "fn:admits", + "fn:check", + "enum:ComparisonOperator", + "fn:sql", + "fn:capability" + ], + "pathReferences": [] + }, + { + "path": "crates/weft-expr/src/lib.rs", + "publicDeclarations": [ + "mod:arithmetic", + "mod:compare", + "mod:operand", + "mod:selection", + "const:VERSION" + ], + "pathReferences": [] + }, + { + "path": "crates/weft-expr/src/operand.rs", + "publicDeclarations": [ + "struct:Field", + "field:scan", + "field:identity", + "field:logical_type", + "field:span", + "enum:Value", + "fn:logical_type" + ], + "pathReferences": [] + }, + { + "path": "crates/weft-expr/src/selection.rs", + "publicDeclarations": [ + "const:PROTOCOL", + "const:MAX_DEPTH", + "const:MIN_OPERANDS", + "const:MAX_OPERANDS", + "const:MAX_COMPARISONS", + "struct:Compare", + "field:field", + "field:op", + "field:value", + "enum:Predicate", + "enum:Refusal", + "fn:walk", + "fn:to_json", + "fn:comparisons", + "fn:dependencies", + "fn:splice" + ], + "pathReferences": [] + } + ] + }, + { + "name": "weft-backend", + "manifest": "crates/weft-backend/Cargo.toml", + "responsibility": "Shared backend boundary (CONTRACT-002; own semver; see crates/weft-backend/README.md)", + "ownedTypes": "Manifest types and validation, Frontend/Backend/TargetArtifact traits, checked compile pipeline, Registry, SQL Emission carrier", + "constructionAndIntegration": "Pure library over weft-model; weft-core supplies the WeftPlans frontend and registers weft backends through it; other compilers supply their own frontend", + "dependencies": [ + { + "name": "serde", + "kind": "normal", + "optional": false, + "source": "registry+https://github.com/rust-lang/crates.io-index", + "req": "=1.0.228", + "rename": null, + "target": null, + "features": [ + "derive" + ], + "defaultFeatures": true, + "path": null + }, + { + "name": "serde_json", + "kind": "normal", + "optional": false, + "source": "registry+https://github.com/rust-lang/crates.io-index", + "req": "=1.0.145", + "rename": null, + "target": null, + "features": [ + "arbitrary_precision", + "raw_value" + ], + "defaultFeatures": true, + "path": null + }, + { + "name": "weft-model", + "kind": "normal", + "optional": false, + "source": null, + "req": "*", + "rename": null, + "target": null, + "features": [], + "defaultFeatures": true, + "path": "crates/weft-model" + } + ], + "sources": [ + { + "path": "crates/weft-backend/src/boundary.rs", + "publicDeclarations": [ + "trait:Frontend", + "trait:TargetArtifact", + "struct:NoArtifact", + "field:obligations", + "trait:Backend", + "struct:BindingInput", + "field:profile", + "field:json", + "field:sha256", + "struct:Target", + "field:backend_id", + "field:backend_version", + "field:profile_id", + "field:allow_candidate", + "struct:Selection", + "field:records", + "field:fields", + "field:types", + "field:relationships", + "struct:Context", + "field:catalog", + "field:plan", + "field:target", + "field:binding", + "field:binding_value", + "field:selection", + "struct:Validated", + "field:mapping", + "field:additional_capabilities", + "field:coverage", + "field:obligations", + "struct:Assessment", + "field:id", + "field:status", + "field:evidence", + "field:obligations", + "struct:Qualification", + "field:assessment", + "field:declaration", + "struct:Compilation", + "field:backend_id", + "field:backend_version", + "field:target_profile", + "field:binding_profile", + "field:binding_sha256", + "field:qualifications", + "field:emission", + "fn:merge_obligations", + "fn:coverage_contains", + "struct:Registry", + "fn:register", + "fn:manifest", + "fn:compile" + ], + "pathReferences": [] + }, + { + "path": "crates/weft-backend/src/lib.rs", + "publicDeclarations": [ + "mod:boundary", + "mod:manifest", + "mod:sql", + "const:VERSION" + ], + "pathReferences": [] + }, + { + "path": "crates/weft-backend/src/manifest.rs", + "publicDeclarations": [ + "struct:LanguageProfile", + "field:dialect_profile", + "field:ir_version", + "struct:TargetProfile", + "field:id", + "field:engine", + "field:engine_version", + "field:session_settings", + "field:storage_layout_revision", + "field:publication_revision", + "enum:Status", + "enum:ObligationOwner", + "struct:Obligation", + "field:id", + "field:parameters", + "field:owner", + "field:failure_code", + "struct:Capability", + "field:id", + "field:target_profiles", + "field:language_profiles", + "field:logical_domain", + "field:result_domain", + "field:constraints", + "field:obligations", + "field:status", + "field:evidence", + "struct:Manifest", + "field:backend_id", + "field:backend_version", + "field:interface_version", + "field:language_profiles", + "field:binding_profile", + "field:target_profiles", + "field:capabilities", + "field:evidence", + "fn:failure", + "fn:nonempty", + "fn:unique_strings", + "trait:ManifestRules", + "struct:OpaqueManifest", + "fn:take_manifest_opaque", + "fn:restore_manifest_opaque", + "fn:validate_manifest_json", + "fn:validate_manifest" + ], + "pathReferences": [] + }, + { + "path": "crates/weft-backend/src/sql.rs", + "publicDeclarations": [ + "enum:NativeMetadata", + "enum:ParameterType", + "enum:ExactParameterKind", + "fn:atomic", + "struct:ParameterSlot", + "field:position", + "field:logical_type", + "field:value", + "field:origin", + "enum:ScalarCarrier", + "enum:ScalarDecoder", + "fn:exact", + "struct:OuterJoin", + "field:scan", + "field:record", + "enum:Representation", + "struct:Column", + "field:position", + "field:output_name", + "field:carrier_name", + "field:representation", + "field:source_identities", + "field:nullable", + "struct:Emission", + "field:sql", + "field:parameters", + "field:columns", + "field:obligations", + "trait:SqlChecks" + ], + "pathReferences": [] + } + ] + }, + { + "name": "weft-postgresql-emit", + "manifest": "crates/weft-postgresql-emit/Cargo.toml", + "responsibility": "Shared PostgreSQL emission primitives (own semver; see crates/weft-postgresql-emit/README.md)", + "ownedTypes": "Identifier, Parameters slots, string literals, typed casts, C collation, result decoders", + "constructionAndIntegration": "Pure library over weft-model and weft-backend; weft-postgresql binds the Truss layout over it, other compilers bind their own layouts", + "dependencies": [ + { + "name": "base64", + "kind": "normal", + "optional": false, + "source": "registry+https://github.com/rust-lang/crates.io-index", + "req": "=0.22.1", + "rename": null, + "target": null, + "features": [], + "defaultFeatures": true, + "path": null + }, + { + "name": "serde_json", + "kind": "normal", + "optional": false, + "source": "registry+https://github.com/rust-lang/crates.io-index", + "req": "=1.0.145", + "rename": null, + "target": null, + "features": [ + "arbitrary_precision", + "raw_value" + ], + "defaultFeatures": true, + "path": null + }, + { + "name": "weft-backend", + "kind": "normal", + "optional": false, + "source": null, + "req": "*", + "rename": null, + "target": null, + "features": [], + "defaultFeatures": true, + "path": "crates/weft-backend" + }, + { + "name": "weft-model", + "kind": "normal", + "optional": false, + "source": null, + "req": "*", + "rename": null, + "target": null, + "features": [], + "defaultFeatures": true, + "path": "crates/weft-model" + } + ], + "sources": [ + { + "path": "crates/weft-postgresql-emit/src/lib.rs", + "publicDeclarations": [ + "const:VERSION", + "struct:Identifier", + "fn:new", + "fn:sql", + "fn:qualified", + "struct:Parameters", + "fn:push", + "fn:push_binary", + "fn:push_timestamp", + "fn:push_date", + "fn:into_slots", + "fn:slots", + "fn:set_origin", + "enum:CatalogDomain", + "fn:catalog", + "const:COLLATE_C", + "fn:collate_c", + "fn:string_literal", + "fn:escape_string_literal", + "fn:cast_type", + "fn:typed", + "fn:decoder", + "fn:decoder_for", + "fn:decodable" + ], + "pathReferences": [] + } + ] + }, + { + "name": "weft-postgresql", + "manifest": "crates/weft-postgresql/Cargo.toml", + "responsibility": "PostgreSQL backend", + "ownedTypes": "PostgreSQL lowering and receipt replay", + "constructionAndIntegration": "runtime chooses this backend; no Python/WASM dependency", + "dependencies": [ + { + "name": "base64", + "kind": "normal", + "optional": false, + "source": "registry+https://github.com/rust-lang/crates.io-index", + "req": "=0.22.1", + "rename": null, + "target": null, + "features": [], + "defaultFeatures": true, + "path": null + }, + { + "name": "jsonschema", + "kind": "normal", + "optional": false, + "source": "registry+https://github.com/rust-lang/crates.io-index", + "req": "=0.56.0", + "rename": null, + "target": null, + "features": [ + "arbitrary-precision", + "macros" + ], + "defaultFeatures": false, + "path": null + }, + { + "name": "serde", + "kind": "normal", + "optional": false, + "source": "registry+https://github.com/rust-lang/crates.io-index", + "req": "=1.0.228", + "rename": null, + "target": null, + "features": [ + "derive" + ], + "defaultFeatures": true, + "path": null + }, + { + "name": "serde_json", + "kind": "normal", + "optional": false, + "source": "registry+https://github.com/rust-lang/crates.io-index", + "req": "=1.0.145", + "rename": null, + "target": null, + "features": [ + "arbitrary_precision", + "raw_value" + ], + "defaultFeatures": true, + "path": null + }, + { + "name": "sqlparser", + "kind": "normal", + "optional": false, + "source": "registry+https://github.com/rust-lang/crates.io-index", + "req": "=0.63.0", + "rename": null, + "target": null, + "features": [ + "std", + "visitor" + ], + "defaultFeatures": false, + "path": null + }, + { + "name": "weft-core", + "kind": "normal", + "optional": false, + "source": null, + "req": "*", + "rename": null, + "target": null, + "features": [], + "defaultFeatures": true, + "path": "crates/weft-core" + }, + { + "name": "weft-expr", + "kind": "normal", + "optional": false, + "source": null, + "req": "*", + "rename": null, + "target": null, + "features": [], + "defaultFeatures": true, + "path": "crates/weft-expr" + }, + { + "name": "weft-postgresql-emit", + "kind": "normal", + "optional": false, + "source": null, + "req": "*", + "rename": null, + "target": null, + "features": [], + "defaultFeatures": true, + "path": "crates/weft-postgresql-emit" + } + ], + "sources": [ + { + "path": "crates/weft-postgresql/src/access.rs", + "publicDeclarations": [ + "struct:ScalarAccess", + "field:value", + "field:present", + "field:native_null", + "field:integrity", + "field:joins", + "struct:ObjectProperty", + "field:namespace", + "field:owner_alias", + "field:type_id", + "field:property_id", + "field:logical_type", + "fn:props", + "fn:row", + "fn:require_scalar_domain" + ], + "pathReferences": [] + }, + { + "path": "crates/weft-postgresql/src/binding.rs", + "publicDeclarations": [ + "struct:Admission", + "field:value", + "field:original_json", + "field:artifacts", + "fn:parse", + "fn:decoded_json", + "enum:PropertyHome", + "fn:original_home_definition", + "fn:property_home" + ], + "pathReferences": [] + }, + { + "path": "crates/weft-postgresql/src/candidate.rs", + "publicDeclarations": [ + "const:PROFILE", + "struct:Candidate", + "struct:Mapping", + "struct:TargetPlan" + ], + "pathReferences": [] + }, + { + "path": "crates/weft-postgresql/src/candidate/application.rs", + "publicDeclarations": [], + "pathReferences": [] + }, + { + "path": "crates/weft-postgresql/src/candidate/relationships.rs", + "publicDeclarations": [], + "pathReferences": [] + }, + { + "path": "crates/weft-postgresql/src/collection.rs", + "publicDeclarations": [ + "enum:Kind", + "struct:CollectionProperty", + "field:namespace", + "field:owner", + "field:type_id", + "field:property_id", + "field:item", + "field:kind", + "fn:props", + "fn:row" + ], + "pathReferences": [] + }, + { + "path": "crates/weft-postgresql/src/comparator_requirements.rs", + "publicDeclarations": [ + "struct:Requirement", + "field:owner", + "field:identity", + "field:logical_type", + "field:operations", + "fn:registration_key", + "fn:collect", + "fn:admit", + "fn:collect_reads", + "fn:admit_context", + "fn:admit_properties" + ], + "pathReferences": [] + }, + { + "path": "crates/weft-postgresql/src/conformance_original.rs", + "publicDeclarations": [ + "fn:configuration", + "fn:registry", + "fn:registry_with_configuration" + ], + "pathReferences": [] + }, + { + "path": "crates/weft-postgresql/src/current_presence.rs", + "publicDeclarations": [ + "struct:CurrentPresenceSql", + "fn:context", + "fn:sql", + "fn:json", + "fn:lower", + "fn:check" + ], + "pathReferences": [] + }, + { + "path": "crates/weft-postgresql/src/current_scalar.rs", + "publicDeclarations": [ + "struct:Parameter", + "field:ordinal", + "field:source_path", + "field:name", + "field:value", + "field:logical_type", + "field:native_type", + "struct:ResultColumn", + "field:position", + "field:name", + "field:path", + "field:source_field", + "field:effective_domain", + "field:logical_type", + "field:exposure", + "field:encoding", + "struct:CurrentScalarSql", + "fn:sql", + "fn:parameters", + "fn:columns", + "fn:json_string", + "fn:json", + "fn:lower", + "fn:lower_rows", + "fn:lower_contributors", + "fn:lower_original_sum", + "fn:lower_bounded_rows", + "fn:lower_bounded_contributors", + "fn:check_rows", + "fn:check_contributors", + "fn:check_original_sum", + "fn:check_bounded_rows", + "fn:check_bounded_contributors" + ], + "pathReferences": [] + }, + { + "path": "crates/weft-postgresql/src/dbapi.rs", + "publicDeclarations": [], + "pathReferences": [] + }, + { + "path": "crates/weft-postgresql/src/expression.rs", + "publicDeclarations": [ + "fn:render" + ], + "pathReferences": [] + }, + { + "path": "crates/weft-postgresql/src/json_codec.rs", + "publicDeclarations": [ + "struct:Encoded", + "field:value", + "field:integrity", + "fn:encode" ], "pathReferences": [] }, @@ -3502,17 +4144,7 @@ "mod:truss_mutation", "mod:truss_runtime", "mod:value_definition", - "struct:Identifier", - "fn:new", - "fn:sql", - "fn:qualified", - "struct:Parameters", - "fn:push", - "fn:push_binary", - "fn:push_timestamp", - "fn:into_slots", - "enum:CatalogDomain", - "fn:catalog", + "use:weft_postgresql_emit : : { qualified , CatalogDomain , Identifier , Parameters }", "mod:leaf_codec_definition", "mod:native_comparator_definition", "mod:comparator_requirements", @@ -5181,6 +5813,7 @@ "scripts/check-specs.ts", "scripts/check-tree-size.py", "scripts/checks/check-module-boundaries.py", + "scripts/module-boundaries-update.ts", "scripts/module-boundaries.test.ts", "scripts/module-boundaries.ts", "scripts/python-release/licenses.py", diff --git a/scripts/check-release-version.py b/scripts/check-release-version.py index 3cf28a1b..2d68ba7c 100755 --- a/scripts/check-release-version.py +++ b/scripts/check-release-version.py @@ -3,10 +3,12 @@ """Fail unless a release tag matches every version field in the repository. Usage: check-release-version.py vX.Y.Z + check-release-version.py --shared-crates (only the shared-crate check; CI runs it on every PR) Checks that the tag, both pyproject.toml files and crates/weft-python/Cargo.toml -carry the same version, that no Cargo.toml pins UMF by commit (`rev =`), and -that CHANGELOG.md has a section for the version. +carry the same version, that no Cargo.toml pins UMF by commit (`rev =`), that +CHANGELOG.md has a section for the version, and that each shared compiler crate +(SHARED_CRATES, versioned on its own) states its Cargo version in its README title. """ import re import sys @@ -14,6 +16,20 @@ from pathlib import Path ROOT = Path(__file__).resolve().parents[1] +# Crates other compilers depend on. Each has its own semver and a README whose +# first line is "# ". +SHARED_CRATES = ("weft-model", "weft-expr", "weft-backend", "weft-postgresql-emit") + + +def shared_crate_errors() -> list[str]: + errors = [] + for name in SHARED_CRATES: + crate = ROOT / "crates" / name + version = tomllib.loads((crate / "Cargo.toml").read_text())["package"]["version"] + title = (crate / "README.md").read_text().splitlines()[0] + if title != f"# {name} {version}": + errors.append(f"crates/{name}/README.md title {title!r} does not state Cargo version {version}") + return errors def main(tag: str) -> int: @@ -39,6 +55,7 @@ def main(tag: str) -> int: for number, line in enumerate(manifest.read_text().splitlines(), 1): if "umf.git" in line and re.search(r'\brev\s*=\s*"', line): errors.append(f"{manifest.relative_to(ROOT)}:{number} pins UMF by rev; use tag = \"umf-vX.Y.Z\"") + errors.extend(shared_crate_errors()) changelog = (ROOT / "CHANGELOG.md").read_text() if not re.search(rf"^## {re.escape(version)}(\s|$)", changelog, re.M): errors.append(f"CHANGELOG.md has no '## {version}' section") @@ -49,7 +66,16 @@ def main(tag: str) -> int: return 1 if errors else 0 +def shared_only() -> int: + errors = shared_crate_errors() + for error in errors: + print(f"error: {error}", file=sys.stderr) + if not errors: + print("shared crates: " + ", ".join(SHARED_CRATES) + " README titles match Cargo versions") + return 1 if errors else 0 + + if __name__ == "__main__": if len(sys.argv) != 2: sys.exit(__doc__) - sys.exit(main(sys.argv[1])) + sys.exit(shared_only() if sys.argv[1] == "--shared-crates" else main(sys.argv[1])) diff --git a/scripts/checks/module-boundaries.json b/scripts/checks/module-boundaries.json index 621f9abd..1b189f57 100644 --- a/scripts/checks/module-boundaries.json +++ b/scripts/checks/module-boundaries.json @@ -1,9 +1,13 @@ { "format": "weft-cargo-boundaries/0.1", "modules": { - "weft-core": [], + "weft-model": [], + "weft-expr": ["weft-model"], + "weft-backend": ["weft-model"], + "weft-postgresql-emit": ["weft-model", "weft-backend"], + "weft-core": ["weft-model", "weft-expr", "weft-backend"], "weft-databricks": ["weft-core"], - "weft-postgresql": ["weft-core"], + "weft-postgresql": ["weft-core", "weft-expr", "weft-postgresql-emit"], "weft-backend-probe": ["weft-core"], "weft-frontend-probe": ["weft-core"], "weft-runtime": ["weft-core", "weft-databricks", "weft-postgresql", "weft-backend-probe"], diff --git a/scripts/module-boundaries-update.ts b/scripts/module-boundaries-update.ts new file mode 100644 index 00000000..3ec552d6 --- /dev/null +++ b/scripts/module-boundaries-update.ts @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +// Refresh the generated parts of docs/helix/02-design/module-boundaries.json: +// each package's dependency edges and Rust source/public-declaration inventory, +// and each host area's source list. Narrative fields (responsibility, ownedTypes, +// constructionAndIntegration, coverage) stay human-owned: a workspace package +// without an entry refuses until one is written. Review the resulting diff; the +// checker (scripts/module-boundaries.ts) then enforces it. +// +// Usage: WEFT_CARGO="$(command -v cargo)" bun scripts/module-boundaries-update.ts +import {readFile, writeFile, readdir} from 'node:fs/promises'; +import {resolve} from 'node:path'; +import {edgeIdentity, scanSurface} from './module-boundaries'; + +async function walk(path: string): Promise { + const entries = await readdir(path, {withFileTypes: true}); + return (await Promise.all(entries.map(e => e.isDirectory() ? walk(resolve(path, e.name)) : [resolve(path, e.name)]))).flat(); +} + +const root = resolve(import.meta.dir, '..'); +const mapPath = resolve(root, 'docs/helix/02-design/module-boundaries.json'); +const map = JSON.parse(await readFile(mapPath, 'utf8')); +const cargo = Bun.spawn([process.env.WEFT_CARGO || 'cargo', 'metadata', '--no-deps', '--format-version', '1', '--locked', '--offline'], {cwd: root, stdout: 'pipe', stderr: 'inherit'}); +const [raw, code] = await Promise.all([new Response(cargo.stdout).text(), cargo.exited]); +if (code !== 0) throw Error('cargo metadata failed'); +const metadata = JSON.parse(raw); +const actual = metadata.packages.filter((p: any) => metadata.workspace_members.includes(p.id)); +const missing = actual.filter((p: any) => !map.packages.some((m: any) => m.name === p.name)).map((p: any) => p.name); +if (missing.length) throw Error(`Write a narrative entry first for: ${missing.join(', ')}`); +map.packages = map.packages.filter((m: any) => actual.some((p: any) => p.name === m.name)); +for (const entry of map.packages) { + const pkg = actual.find((p: any) => p.name === entry.name); + entry.manifest = pkg.manifest_path.slice(root.length + 1); + entry.dependencies = pkg.dependencies.map((d: any) => edgeIdentity(d, root)) + .sort((a: any, b: any) => a.name.localeCompare(b.name) || a.kind.localeCompare(b.kind)); + entry.sources = await scanSurface(root, entry.manifest); +} +for (const area of map.hostAreas) { + area.sources = (await Promise.all(area.roots.map((path: string) => walk(resolve(root, path))))).flat() + .filter((f: string) => area.extensions.some((e: string) => f.endsWith(e))) + .map((f: string) => f.slice(root.length + 1)).concat(area.configs ?? []).sort(); +} +await writeFile(mapPath, JSON.stringify(map, null, 2) + '\n'); +console.log(`Updated ${map.packages.length} package inventories and ${map.hostAreas.length} host areas.`);