synalog 2.5.0__tar.gz → 2.5.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {synalog-2.5.0 → synalog-2.5.1}/Cargo.lock +2 -2
- {synalog-2.5.0 → synalog-2.5.1}/Cargo.toml +1 -1
- {synalog-2.5.0 → synalog-2.5.1}/PKG-INFO +1 -1
- {synalog-2.5.0 → synalog-2.5.1}/docs/verification.md +4 -1
- {synalog-2.5.0 → synalog-2.5.1}/pyproject.toml +1 -1
- {synalog-2.5.0 → synalog-2.5.1}/src/compiler/annotations.rs +7 -18
- synalog-2.5.1/src/compiler/home_guard.rs +536 -0
- synalog-2.5.1/src/compiler/home_guard_test.rs +116 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/compiler/mod.rs +1 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/compiler/universe.rs +36 -0
- {synalog-2.5.0 → synalog-2.5.1}/uv.lock +1 -1
- {synalog-2.5.0 → synalog-2.5.1}/verified/Cargo.toml +1 -1
- synalog-2.5.1/verified/src/home.rs +212 -0
- {synalog-2.5.0 → synalog-2.5.1}/verified/src/lib.rs +3 -0
- {synalog-2.5.0 → synalog-2.5.1}/.gitignore +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/LICENSE +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/NOTICE +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/README.md +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/assertions.md +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/assets/synalog-dark.svg +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/assets/synalog-light.svg +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/assets/synalog_favicon.png +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/benchmark/results.json +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/benchmark/speedup_by_engine.png +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/benchmark/speedup_distribution.png +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/benchmark/summary.md +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/benchmark/summary.png +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/benchmark/time_comparison.png +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/benchmark/verify_by_engine.png +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/benchmark.md +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/cli.md +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/development.md +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/differences-datalog.md +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/differences.md +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/engines.md +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/aggregation.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/aggregation.log +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/directives.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/directives.log +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/functions.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/functions.log +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/functors.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/functors.log +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/getting_started.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/getting_started.log +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/knowledge_graphs.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/knowledge_graphs.log +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/recursion.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/recursion.log +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/run.py +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/shop/concepts/customer.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/shop/concepts/order_status.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/shop/data/orders.csv +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/shop/layer.toml +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/shop/rules/customer_revenue.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/shop/rules/orders_by_status.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/shop/rules/top_customers.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/shop/tables/orders.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/shop.log +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/smoke/concepts/device.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/smoke/concepts/test_status.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/smoke/data/smoke_tests.csv +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/smoke/layer.toml +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/smoke/rules/failures_by_device.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/smoke/rules/runs_per_day.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/smoke/tables/smoke_tests.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/smoke.log +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/syntax.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/syntax.log +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/temporal.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/temporal.log +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/temporal_graph.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/temporal_graph.log +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/verification.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/examples/verification.log +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/getting-started.md +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/index.md +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/javascripts/playground.js +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/javascripts/summarize.js +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/javascripts/synalog-highlight.js +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/knowledge-graphs.md +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/language/aggregation.md +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/language/directives.md +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/language/functions.md +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/language/functors.md +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/language/index.md +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/language/recursion.md +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/language/syntax.md +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/language/temporal.md +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/playground/vendor/codemirror.js +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/playground.md +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/python-api.md +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/stylesheets/extra.css +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/docs/why.md +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/img/synalog-dark.svg +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/img/synalog-light.svg +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/python/synalog/__init__.py +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/python/synalog/__main__.py +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/python/synalog/checking.py +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/python/synalog/cli.py +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/python/synalog/config.py +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/python/synalog/execution.py +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/python/synalog/introspect.py +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/python/synalog/project.py +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/python/synalog/runners.py +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/python/synalog/writes.py +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/skills/synalog/SKILL.md +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/skills/synalog/examples/company/concepts/department.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/skills/synalog/examples/company/concepts/person.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/skills/synalog/examples/company/concepts/works_in.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/skills/synalog/examples/company/data/departments.csv +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/skills/synalog/examples/company/data/employees.csv +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/skills/synalog/examples/company/layer.toml +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/skills/synalog/examples/company/rules/team_mate.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/skills/synalog/examples/company/tables/departments.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/skills/synalog/examples/company/tables/employees.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/skills/synalog/examples/org/concepts/all_managers.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/skills/synalog/examples/org/data/employees.csv +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/skills/synalog/examples/org/layer.toml +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/skills/synalog/examples/org/tables/employees.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/skills/synalog/examples/sales/concepts/region.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/skills/synalog/examples/sales/data/sales.csv +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/skills/synalog/examples/sales/layer.toml +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/skills/synalog/examples/sales/rules/monthly_sales.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/skills/synalog/examples/sales/rules/top_region.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/skills/synalog/examples/sales/rules/total_by_region.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/skills/synalog/examples/sales/tables/sales.l +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/assertion/mod.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/assertion/parse.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/assertion/report.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/assertion/translate.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/compiler/annotations_test.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/compiler/dialects.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/compiler/dialects_test.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/compiler/expr_translate.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/compiler/expr_translate_test.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/compiler/functors.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/compiler/functors_test.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/compiler/rule_translate.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/compiler/rule_translate_test.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/compiler/type_inference/builder.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/compiler/type_inference/built_in.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/compiler/type_inference/edge.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/compiler/type_inference/expression.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/compiler/type_inference/graph.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/compiler/type_inference/inference.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/compiler/type_inference/intersection.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/compiler/type_inference/mod.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/compiler/type_inference/types.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/compiler/universe_test.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/errors.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/lib.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/parser/json.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/parser/json_test.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/parser/mod.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/parser/names.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/parser/parse.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/parser/parse_test.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/parser/rewrite.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/parser/rewrite_test.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/parser/span.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/parser/span_test.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/parser/traverse.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/parser/traverse_test.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/python.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/verifier/arity.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/verifier/assertions.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/verifier/cast.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/verifier/contradiction.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/verifier/dependencies.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/verifier/directives.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/verifier/front_matter.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/verifier/functors.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/verifier/mod.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/verifier/orderby.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/verifier/positional.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/verifier/recursion.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/verifier/reserved.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/verifier/safety.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/verifier/sqlexpr.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/verifier/stratification.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/verifier/undefined.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/verifier/vars.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/src/wasm.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/verified/src/arity.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/verified/src/cast.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/verified/src/contradiction.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/verified/src/convergence.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/verified/src/membership.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/verified/src/page.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/verified/src/plan.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/verified/src/quote.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/verified/src/recursion.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/verified/src/safety.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/verified/src/strata.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/verified/src/types.rs +0 -0
- {synalog-2.5.0 → synalog-2.5.1}/zensical.toml +0 -0
|
@@ -300,7 +300,7 @@ dependencies = [
|
|
|
300
300
|
|
|
301
301
|
[[package]]
|
|
302
302
|
name = "synalog"
|
|
303
|
-
version = "2.5.
|
|
303
|
+
version = "2.5.1"
|
|
304
304
|
dependencies = [
|
|
305
305
|
"anyhow",
|
|
306
306
|
"indexmap",
|
|
@@ -316,7 +316,7 @@ dependencies = [
|
|
|
316
316
|
|
|
317
317
|
[[package]]
|
|
318
318
|
name = "synalog-verified"
|
|
319
|
-
version = "2.5.
|
|
319
|
+
version = "2.5.1"
|
|
320
320
|
dependencies = [
|
|
321
321
|
"vstd",
|
|
322
322
|
]
|
|
@@ -74,6 +74,8 @@ A program's text reaches SQL only as values or as checked names, so no string ca
|
|
|
74
74
|
- a program reaches no file, process or service: there is no `ReadFile`, `WriteFile` or `Intelligence`, no `copy_to_file`, and a table name opens none: once the files given to `execute` are loaded, its DuckDB session reads no file, installs no extension and takes no Python variable for a table (`secret.csv`, `"secret"."parquet"`), which DuckDB otherwise does (`tests/cli/test_file_access.py`);
|
|
75
75
|
- a program writes in `synalog_home` only, on every engine (on Trino and Presto, in a catalog of the same name: `synalog_home.synalog_home`): its tables (also when `@Ground` names one), and the types and sequence the PostgreSQL and DuckDB sessions need. There is no directive to write elsewhere (`@Dataset` and `@AttachDatabase` are not directives), so a program cannot create, drop or replace anything outside it.
|
|
76
76
|
|
|
77
|
+
Before a plan leaves the compiler, every statement of it is read again as the engine reads it, and the plan is refused unless they write in `synalog_home` only: a bug in the compiler that named a table elsewhere is a compile error (`synalog refused to return SQL that would write outside synalog_home`), never a statement sent to a database. The reading accepts the statements the compiler writes, and nothing else: a query, `SET` (not of a role or a password), `CREATE` or `DROP` of a table, type or sequence, `INSERT INTO`, the making of `synalog_home`, PostgreSQL's `DO` blocks of its setup and SQLite's attaching of its in-memory `synalog_home`. It refuses any other statement (`UPDATE`, `DELETE`, `ALTER`, `GRANT`, `COPY`, `CALL`, dynamic SQL), a write inside a query (`SELECT ... INTO`, `WITH d AS (DELETE ...)`), a table stored elsewhere though named in `synalog_home` (`LOCATION`, a ClickHouse `ENGINE` but `Memory`), and text an engine might read otherwise than it does: an unclosed literal or comment, a comment nested in one, MySQL's `/*!` and `--` before no space, a `#`, Spark's `${`, BigQuery's raw and triple-quoted strings. The decision over what it reads is proved (see [Proved algorithms](#proved-algorithms)); `src/compiler/home_guard.rs` reads the statements for it.
|
|
78
|
+
|
|
77
79
|
`tests/programs/injection` holds the attempts this is checked against, on every engine. The quoting of strings and identifiers is also proved (see [Proved algorithms](#proved-algorithms)): for any text, the engine's lexer reads it back exactly and ends the literal at its closing quote.
|
|
78
80
|
|
|
79
81
|
### Contradictions
|
|
@@ -102,7 +104,7 @@ TopCustomers(customer_id:) :- customer_id in Range(3);
|
|
|
102
104
|
|
|
103
105
|
## Proved algorithms
|
|
104
106
|
|
|
105
|
-
The algorithms behind the stratification and recursion checks, the compiler's choice of how to unfold a mutual recursion, the order a compiled program's tables run in, the contradiction warning, pagination, quoting
|
|
107
|
+
The algorithms behind the stratification and recursion checks, the compiler's choice of how to unfold a mutual recursion, the order a compiled program's tables run in, the contradiction warning, pagination, quoting, the `Cast` type-name check and the check that a plan writes in `synalog_home` only are written in [Verus](https://github.com/verus-lang/verus): each algorithm carries a specification that Verus proves it meets, for every input:
|
|
106
108
|
|
|
107
109
|
| Algorithm | Proved |
|
|
108
110
|
|-----------|--------|
|
|
@@ -115,6 +117,7 @@ The algorithms behind the stratification and recursion checks, the compiler's ch
|
|
|
115
117
|
| **Contradictions** | A rule reported as contradictory has no values satisfying its comparisons: the warning is never wrong |
|
|
116
118
|
| **Pagination** | A page keeps exactly the rows asked for (`limit` rows after `offset`, no more than `@Limit`), whatever the numbers, and never writes one a `BIGINT` cannot hold |
|
|
117
119
|
| **Cast type** | A `Cast`/`TryCast` type name the validator accepts holds no character that could end the `CAST(...)` or start a statement after it, and keeps its brackets balanced, so it stays inside the cast |
|
|
120
|
+
| **Writes in `synalog_home` only** | The statements of a plan are returned exactly when none is one the compiler never writes and every write names a table directly in `synalog_home` (`synalog_home.synalog_home` on Trino and Presto; PostgreSQL's session tables in `pg_temp`), every schema made being `synalog_home` itself: a name with a part too few or too many, or no name at all, is refused |
|
|
118
121
|
| **Quoting** | A string or an identifier quoted for any engine is read back by the engine's lexer exactly, ending at its closing quote whatever SQL follows: doubled quotes (`'it''s'`), backslash escapes (BigQuery, Spark), PostgreSQL's `E'...'` read alike whatever `standard_conforming_strings` says; on BigQuery and Spark a string holds no quote but its own two, and on Spark no `$` |
|
|
119
122
|
|
|
120
123
|
The type inference's intersection of two types runs too often to convert its types for a proved version: Verus proves a reference intersection the greatest common refinement of two types (it refines both, every type refining both refines it, and when there is none no type refines both), and a test checks the type inference agrees with it on 20,000 random pairs of types (`cargo test --test type_intersection`).
|
|
@@ -637,27 +637,16 @@ impl Annotations {
|
|
|
637
637
|
let v = a.get("ground")?;
|
|
638
638
|
let raw = v.as_str().to_string();
|
|
639
639
|
|
|
640
|
-
//
|
|
641
|
-
//
|
|
642
|
-
//
|
|
640
|
+
// Named after the predicate, after the table another grounded
|
|
641
|
+
// predicate named here is stored in, or as given: in Synalog's
|
|
642
|
+
// dataset each time, so grounding never drops or replaces a table
|
|
643
|
+
// elsewhere.
|
|
643
644
|
let table_name = if raw == pred_name {
|
|
644
645
|
self.default_table(&raw)
|
|
645
646
|
} else {
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
.
|
|
649
|
-
.and_then(|ann| ann.get("ground"))
|
|
650
|
-
{
|
|
651
|
-
let other_raw = other_ground.as_str().to_string();
|
|
652
|
-
if other_raw == raw {
|
|
653
|
-
self.default_table(&other_raw)
|
|
654
|
-
} else {
|
|
655
|
-
other_raw
|
|
656
|
-
}
|
|
657
|
-
} else {
|
|
658
|
-
// A table named in @Ground lives in Synalog's dataset too:
|
|
659
|
-
// grounding never drops or replaces a table elsewhere.
|
|
660
|
-
self.default_table(&raw)
|
|
647
|
+
match self.annotations.get(&raw).and_then(|ann| ann.get("ground")) {
|
|
648
|
+
Some(other) => self.default_table(other.as_str()),
|
|
649
|
+
None => self.default_table(&raw),
|
|
661
650
|
}
|
|
662
651
|
};
|
|
663
652
|
|
|
@@ -0,0 +1,536 @@
|
|
|
1
|
+
// License Apache 2.0: (c) 2025-2026 Yoan Sallami (Synalinks Team)
|
|
2
|
+
|
|
3
|
+
//! The last check before a plan leaves the compiler: its statements write in
|
|
4
|
+
//! `synalog_home` only.
|
|
5
|
+
//!
|
|
6
|
+
//! Every statement of every step is read as the engine reads it (string
|
|
7
|
+
//! literals and comments skipped, with the engine's escapes) into the tokens
|
|
8
|
+
//! `synalog_verified::home::writes_in_home` decides on, proved to accept them
|
|
9
|
+
//! exactly when no statement is one the compiler never writes and every write
|
|
10
|
+
//! names a table directly in `synalog_home` (on Trino and Presto
|
|
11
|
+
//! `synalog_home.synalog_home`; PostgreSQL's session tables in `pg_temp`).
|
|
12
|
+
//!
|
|
13
|
+
//! The reading fails closed: a statement is one of the forms the compiler
|
|
14
|
+
//! writes (a query, `SET`, `CREATE`/`DROP` of a table, type, sequence or the
|
|
15
|
+
//! schema, `INSERT INTO`, PostgreSQL's `DO` blocks of its setup, SQLite's
|
|
16
|
+
//! attaching of its in-memory `synalog_home`), and anything else is refused:
|
|
17
|
+
//! another statement, a clause that would store a table elsewhere (`LOCATION`,
|
|
18
|
+
//! an `ENGINE` other than `Memory`), a write inside a query (`SELECT ...
|
|
19
|
+
//! INTO`, PostgreSQL's `WITH d AS (DELETE ...)`), and any text an engine
|
|
20
|
+
//! might read otherwise than this reader does (an unterminated literal, a
|
|
21
|
+
//! nested comment, MySQL's `/*!` and `--x`, a `#`, Spark's `${`).
|
|
22
|
+
|
|
23
|
+
use std::collections::HashMap;
|
|
24
|
+
|
|
25
|
+
use synalog_verified::home::{writes_in_home, DOT, FORBIDDEN, MAKE_HOME, NAME, OTHER, WRITE};
|
|
26
|
+
|
|
27
|
+
use super::home_schema;
|
|
28
|
+
|
|
29
|
+
/// A token as the engine reads it.
|
|
30
|
+
#[derive(Debug, Clone, PartialEq)]
|
|
31
|
+
enum Raw {
|
|
32
|
+
/// An unquoted word: a keyword or a name, as written.
|
|
33
|
+
Word(String),
|
|
34
|
+
/// A quoted name, its quotes and escapes removed.
|
|
35
|
+
Quoted(String),
|
|
36
|
+
/// A string literal, as it reads.
|
|
37
|
+
Str(String),
|
|
38
|
+
Dot,
|
|
39
|
+
Semi,
|
|
40
|
+
Open,
|
|
41
|
+
/// PostgreSQL's dollar quote (`$$`) around a `DO` block's body.
|
|
42
|
+
Dollar,
|
|
43
|
+
Sym(char),
|
|
44
|
+
/// Text an engine may read otherwise than this reader.
|
|
45
|
+
Bad,
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
fn backslash_strings(engine: &str) -> bool {
|
|
49
|
+
matches!(engine, "databricks" | "bigquery" | "clickhouse")
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
fn double_quoted_strings(engine: &str) -> bool {
|
|
53
|
+
matches!(engine, "databricks" | "bigquery")
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/// Engines where `#` starts a comment.
|
|
57
|
+
fn hash_comments(engine: &str) -> bool {
|
|
58
|
+
matches!(engine, "mysql" | "clickhouse" | "bigquery")
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
fn is_word_char(c: char) -> bool {
|
|
62
|
+
c.is_alphanumeric() || c == '_' || c == '$'
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/// The quoted text from `chars[start]` (an opening `quote`) to its closing
|
|
66
|
+
/// quote: its content and the index after it, or None when it never closes.
|
|
67
|
+
fn quoted(chars: &[char], start: usize, quote: char, backslash: bool) -> Option<(String, usize)> {
|
|
68
|
+
let mut out = String::new();
|
|
69
|
+
let mut i = start + 1;
|
|
70
|
+
while i < chars.len() {
|
|
71
|
+
let c = chars[i];
|
|
72
|
+
if backslash && c == '\\' {
|
|
73
|
+
let next = *chars.get(i + 1)?;
|
|
74
|
+
out.push(next);
|
|
75
|
+
i += 2;
|
|
76
|
+
} else if c == quote {
|
|
77
|
+
if chars.get(i + 1) == Some("e) {
|
|
78
|
+
out.push(quote);
|
|
79
|
+
i += 2;
|
|
80
|
+
} else {
|
|
81
|
+
return Some((out, i + 1));
|
|
82
|
+
}
|
|
83
|
+
} else {
|
|
84
|
+
out.push(c);
|
|
85
|
+
i += 1;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
None
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
fn lex(sql: &str, engine: &str) -> Vec<Raw> {
|
|
92
|
+
let chars: Vec<char> = sql.chars().collect();
|
|
93
|
+
let mut out = Vec::new();
|
|
94
|
+
let mut i = 0;
|
|
95
|
+
while i < chars.len() {
|
|
96
|
+
let c = chars[i];
|
|
97
|
+
let next = chars.get(i + 1).copied();
|
|
98
|
+
if c.is_whitespace() {
|
|
99
|
+
i += 1;
|
|
100
|
+
} else if c == '-' && next == Some('-') {
|
|
101
|
+
// MySQL reads `--` as a comment only before a space.
|
|
102
|
+
if engine == "mysql" && !chars.get(i + 2).is_none_or(|c| c.is_whitespace() || c.is_control()) {
|
|
103
|
+
out.push(Raw::Bad);
|
|
104
|
+
return out;
|
|
105
|
+
}
|
|
106
|
+
while i < chars.len() && chars[i] != '\n' {
|
|
107
|
+
i += 1;
|
|
108
|
+
}
|
|
109
|
+
} else if c == '/' && next == Some('*') {
|
|
110
|
+
// MySQL runs what `/*!` holds; PostgreSQL nests comments.
|
|
111
|
+
if chars.get(i + 2) == Some(&'!') {
|
|
112
|
+
out.push(Raw::Bad);
|
|
113
|
+
return out;
|
|
114
|
+
}
|
|
115
|
+
let mut j = i + 2;
|
|
116
|
+
loop {
|
|
117
|
+
if j + 1 >= chars.len() || (chars[j] == '/' && chars[j + 1] == '*') {
|
|
118
|
+
out.push(Raw::Bad);
|
|
119
|
+
return out;
|
|
120
|
+
}
|
|
121
|
+
if chars[j] == '*' && chars[j + 1] == '/' {
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
j += 1;
|
|
125
|
+
}
|
|
126
|
+
i = j + 2;
|
|
127
|
+
} else if c == '#' && hash_comments(engine) {
|
|
128
|
+
out.push(Raw::Bad);
|
|
129
|
+
return out;
|
|
130
|
+
} else if c == '\'' || (c == '"' && double_quoted_strings(engine)) {
|
|
131
|
+
// BigQuery's and Spark's triple-quoted and raw strings read
|
|
132
|
+
// otherwise: not something the compiler writes.
|
|
133
|
+
let triple = chars.get(i + 1) == Some(&c) && chars.get(i + 2) == Some(&c);
|
|
134
|
+
let raw_prefix = double_quoted_strings(engine)
|
|
135
|
+
&& matches!(out.last(), Some(Raw::Word(w)) if w.eq_ignore_ascii_case("r") || w.eq_ignore_ascii_case("b") || w.eq_ignore_ascii_case("rb") || w.eq_ignore_ascii_case("br"))
|
|
136
|
+
&& i > 0
|
|
137
|
+
&& is_word_char(chars[i - 1]);
|
|
138
|
+
if (triple && double_quoted_strings(engine)) || raw_prefix {
|
|
139
|
+
out.push(Raw::Bad);
|
|
140
|
+
return out;
|
|
141
|
+
}
|
|
142
|
+
// PostgreSQL's `E'...'` takes backslash escapes.
|
|
143
|
+
let e_string = engine == "psql"
|
|
144
|
+
&& i > 0
|
|
145
|
+
&& matches!(out.last(), Some(Raw::Word(w)) if w.eq_ignore_ascii_case("e"))
|
|
146
|
+
&& is_word_char(chars[i - 1]);
|
|
147
|
+
if e_string {
|
|
148
|
+
out.pop();
|
|
149
|
+
}
|
|
150
|
+
match quoted(&chars, i, c, backslash_strings(engine) || e_string) {
|
|
151
|
+
Some((text, end)) => {
|
|
152
|
+
out.push(Raw::Str(text));
|
|
153
|
+
i = end;
|
|
154
|
+
}
|
|
155
|
+
None => {
|
|
156
|
+
out.push(Raw::Bad);
|
|
157
|
+
return out;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
} else if c == '"' || c == '`' {
|
|
161
|
+
let backslash = matches!(engine, "clickhouse") || (c == '`' && engine == "bigquery");
|
|
162
|
+
match quoted(&chars, i, c, backslash) {
|
|
163
|
+
Some((text, end)) => {
|
|
164
|
+
out.push(Raw::Quoted(text));
|
|
165
|
+
i = end;
|
|
166
|
+
}
|
|
167
|
+
None => {
|
|
168
|
+
out.push(Raw::Bad);
|
|
169
|
+
return out;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
} else if c == '$' && engine == "databricks" && next == Some('{') {
|
|
173
|
+
out.push(Raw::Bad);
|
|
174
|
+
return out;
|
|
175
|
+
} else if c == '$' && engine == "psql" && next == Some('$') {
|
|
176
|
+
out.push(Raw::Dollar);
|
|
177
|
+
i += 2;
|
|
178
|
+
} else if is_word_char(c) {
|
|
179
|
+
let start = i;
|
|
180
|
+
while i < chars.len() && is_word_char(chars[i]) {
|
|
181
|
+
i += 1;
|
|
182
|
+
}
|
|
183
|
+
out.push(Raw::Word(chars[start..i].iter().collect()));
|
|
184
|
+
} else {
|
|
185
|
+
out.push(match c {
|
|
186
|
+
'.' => Raw::Dot,
|
|
187
|
+
';' => Raw::Semi,
|
|
188
|
+
'(' => Raw::Open,
|
|
189
|
+
_ => Raw::Sym(c),
|
|
190
|
+
});
|
|
191
|
+
i += 1;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
out
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/// Reads one script's statements into `synalog_verified::home`'s tokens.
|
|
198
|
+
struct Reader<'a> {
|
|
199
|
+
engine: &'a str,
|
|
200
|
+
names: HashMap<String, u64>,
|
|
201
|
+
tokens: Vec<(u8, u64)>,
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
fn is_word(raw: Option<&Raw>, word: &str) -> bool {
|
|
205
|
+
matches!(raw, Some(Raw::Word(w)) if w.eq_ignore_ascii_case(word))
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
fn word_of(raw: Option<&Raw>) -> Option<String> {
|
|
209
|
+
match raw {
|
|
210
|
+
Some(Raw::Word(w)) => Some(w.to_ascii_lowercase()),
|
|
211
|
+
_ => None,
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/// Words that write, refused inside a query: `SELECT ... INTO`, a
|
|
216
|
+
/// data-modifying `WITH d AS (DELETE ...)`.
|
|
217
|
+
const WRITES_IN_A_QUERY: &[&str] = &["insert", "update", "delete", "merge", "into", "copy", "upsert", "replace"];
|
|
218
|
+
|
|
219
|
+
/// Statements of a PostgreSQL `DO` block the compiler never writes there.
|
|
220
|
+
const NOT_IN_A_BLOCK: &[&str] = &[
|
|
221
|
+
"insert", "update", "delete", "merge", "into", "alter", "execute", "perform", "call", "copy",
|
|
222
|
+
"grant", "revoke", "truncate", "comment", "rename", "lock", "set", "reset",
|
|
223
|
+
];
|
|
224
|
+
|
|
225
|
+
impl<'a> Reader<'a> {
|
|
226
|
+
fn new(engine: &'a str) -> Self {
|
|
227
|
+
Reader { engine, names: HashMap::new(), tokens: Vec::new() }
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
fn id(&mut self, name: &str) -> u64 {
|
|
231
|
+
let next = self.names.len() as u64;
|
|
232
|
+
*self.names.entry(name.to_string()).or_insert(next)
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
fn push(&mut self, kind: u8) {
|
|
236
|
+
self.tokens.push((kind, 0));
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/// One token as it is: a name, a dot, or anything else.
|
|
240
|
+
fn plain(&mut self, raw: &Raw) {
|
|
241
|
+
match raw {
|
|
242
|
+
Raw::Word(w) | Raw::Quoted(w) => {
|
|
243
|
+
let id = self.id(w);
|
|
244
|
+
self.tokens.push((NAME, id));
|
|
245
|
+
}
|
|
246
|
+
Raw::Dot => self.push(DOT),
|
|
247
|
+
Raw::Bad => self.push(FORBIDDEN),
|
|
248
|
+
_ => self.push(OTHER),
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/// The dotted name at `s[i..]`, after a `verb` token: the index after it.
|
|
253
|
+
fn target(&mut self, verb: u8, s: &[Raw], mut i: usize) -> usize {
|
|
254
|
+
self.push(verb);
|
|
255
|
+
loop {
|
|
256
|
+
match s.get(i) {
|
|
257
|
+
Some(raw @ (Raw::Word(_) | Raw::Quoted(_))) => {
|
|
258
|
+
self.plain(raw);
|
|
259
|
+
i += 1;
|
|
260
|
+
if matches!(s.get(i), Some(Raw::Dot)) {
|
|
261
|
+
self.push(DOT);
|
|
262
|
+
i += 1;
|
|
263
|
+
} else {
|
|
264
|
+
return i;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
// No name, or a dot before none: the target is what was read.
|
|
268
|
+
_ => return i,
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/// A query from `s[i..]` to the end of the statement: read, any write in
|
|
274
|
+
/// it refused.
|
|
275
|
+
fn query(&mut self, s: &[Raw], i: usize) {
|
|
276
|
+
for (k, raw) in s.iter().enumerate().skip(i) {
|
|
277
|
+
let after_dot = k > 0 && matches!(s[k - 1], Raw::Dot);
|
|
278
|
+
let before_open = matches!(s.get(k + 1), Some(Raw::Open));
|
|
279
|
+
let writes = word_of(Some(raw)).is_some_and(|w| WRITES_IN_A_QUERY.contains(&w.as_str()));
|
|
280
|
+
// A word after a dot is a column; one before `(` a function
|
|
281
|
+
// (`REPLACE(s, 'a', 'b')`). A write in a query follows `(` or a
|
|
282
|
+
// query's own keyword, never `AS`: `AS update` is a name.
|
|
283
|
+
let named = k > 0 && is_word(s.get(k - 1), "as");
|
|
284
|
+
if writes && !after_dot && !before_open && !named {
|
|
285
|
+
self.push(FORBIDDEN);
|
|
286
|
+
} else {
|
|
287
|
+
self.plain(raw);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/// Words from `s[i..]` that must all be among `allowed`, to the end.
|
|
293
|
+
fn only(&mut self, s: &[Raw], i: usize, allowed: &[&str]) {
|
|
294
|
+
for raw in &s[i..] {
|
|
295
|
+
match raw {
|
|
296
|
+
Raw::Word(w) if allowed.contains(&w.to_ascii_lowercase().as_str()) => self.push(OTHER),
|
|
297
|
+
Raw::Sym('=') => self.push(OTHER),
|
|
298
|
+
_ => self.push(FORBIDDEN),
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/// `CREATE ...` from `s[i..]` (after `CREATE`).
|
|
304
|
+
fn create(&mut self, s: &[Raw], mut i: usize) {
|
|
305
|
+
if is_word(s.get(i), "or") && is_word(s.get(i + 1), "replace") {
|
|
306
|
+
i += 2;
|
|
307
|
+
}
|
|
308
|
+
if is_word(s.get(i), "temp") || is_word(s.get(i), "temporary") {
|
|
309
|
+
i += 1;
|
|
310
|
+
}
|
|
311
|
+
let kind = word_of(s.get(i)).unwrap_or_default();
|
|
312
|
+
i += 1;
|
|
313
|
+
if is_word(s.get(i), "if") && is_word(s.get(i + 1), "not") && is_word(s.get(i + 2), "exists") {
|
|
314
|
+
i += 3;
|
|
315
|
+
}
|
|
316
|
+
match kind.as_str() {
|
|
317
|
+
"schema" | "database" => {
|
|
318
|
+
let end = self.target(MAKE_HOME, s, i);
|
|
319
|
+
// MySQL's collation for it.
|
|
320
|
+
self.only(s, end, &["default", "collate", "character", "set", "charset", "utf8mb4_0900_bin", "utf8mb4"]);
|
|
321
|
+
}
|
|
322
|
+
"table" => {
|
|
323
|
+
let mut end = self.target(WRITE, s, i);
|
|
324
|
+
// ClickHouse keeps the table in memory, nowhere else.
|
|
325
|
+
if self.engine == "clickhouse"
|
|
326
|
+
&& is_word(s.get(end), "engine")
|
|
327
|
+
&& matches!(s.get(end + 1), Some(Raw::Sym('=')))
|
|
328
|
+
&& is_word(s.get(end + 2), "memory")
|
|
329
|
+
{
|
|
330
|
+
for _ in 0..3 {
|
|
331
|
+
self.push(OTHER);
|
|
332
|
+
}
|
|
333
|
+
end += 3;
|
|
334
|
+
}
|
|
335
|
+
if is_word(s.get(end), "as") {
|
|
336
|
+
self.push(OTHER);
|
|
337
|
+
self.query(s, end + 1);
|
|
338
|
+
} else {
|
|
339
|
+
self.only(s, end, &[]);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
"type" => {
|
|
343
|
+
let end = self.target(WRITE, s, i);
|
|
344
|
+
if is_word(s.get(end), "as") {
|
|
345
|
+
self.push(OTHER);
|
|
346
|
+
self.query(s, end + 1);
|
|
347
|
+
} else {
|
|
348
|
+
self.only(s, end, &[]);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
"sequence" => {
|
|
352
|
+
let end = self.target(WRITE, s, i);
|
|
353
|
+
self.only(s, end, &[]);
|
|
354
|
+
}
|
|
355
|
+
_ => self.push(FORBIDDEN),
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/// `DROP ...` from `s[i..]` (after `DROP`).
|
|
360
|
+
fn drop(&mut self, s: &[Raw], mut i: usize) {
|
|
361
|
+
let kind = word_of(s.get(i)).unwrap_or_default();
|
|
362
|
+
i += 1;
|
|
363
|
+
if is_word(s.get(i), "if") && is_word(s.get(i + 1), "exists") {
|
|
364
|
+
i += 2;
|
|
365
|
+
}
|
|
366
|
+
if matches!(kind.as_str(), "table" | "type" | "sequence") {
|
|
367
|
+
let end = self.target(WRITE, s, i);
|
|
368
|
+
self.only(s, end, &["cascade"]);
|
|
369
|
+
} else {
|
|
370
|
+
self.push(FORBIDDEN);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/// A PostgreSQL `DO $$ ... $$` block of the compiler's setup: its own
|
|
375
|
+
/// statements read as the others are; no other write.
|
|
376
|
+
fn block(&mut self, s: &[Raw], i: usize) {
|
|
377
|
+
if !matches!(s.get(i), Some(Raw::Dollar)) || !matches!(s.last(), Some(Raw::Dollar)) || s.len() < i + 2 {
|
|
378
|
+
self.push(FORBIDDEN);
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
let body = &s[i + 1..s.len() - 1];
|
|
382
|
+
if body.iter().any(|r| matches!(r, Raw::Dollar)) {
|
|
383
|
+
self.push(FORBIDDEN);
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
for part in body.split(|r| matches!(r, Raw::Semi)) {
|
|
387
|
+
let mut k = 0;
|
|
388
|
+
while k < part.len() {
|
|
389
|
+
let after_dot = k > 0 && matches!(part[k - 1], Raw::Dot);
|
|
390
|
+
match word_of(part.get(k)) {
|
|
391
|
+
Some(w) if !after_dot && w == "create" => {
|
|
392
|
+
self.create(part, k + 1);
|
|
393
|
+
k = part.len();
|
|
394
|
+
}
|
|
395
|
+
Some(w) if !after_dot && w == "drop" => {
|
|
396
|
+
self.drop(part, k + 1);
|
|
397
|
+
k = part.len();
|
|
398
|
+
}
|
|
399
|
+
Some(w) if !after_dot && NOT_IN_A_BLOCK.contains(&w.as_str()) => {
|
|
400
|
+
self.push(FORBIDDEN);
|
|
401
|
+
k += 1;
|
|
402
|
+
}
|
|
403
|
+
_ => {
|
|
404
|
+
self.plain(&part[k]);
|
|
405
|
+
k += 1;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
self.push(OTHER);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/// One statement.
|
|
414
|
+
fn statement(&mut self, s: &[Raw]) {
|
|
415
|
+
let Some(first) = s.first() else { return };
|
|
416
|
+
if matches!(first, Raw::Open) {
|
|
417
|
+
return self.query(s, 0);
|
|
418
|
+
}
|
|
419
|
+
match word_of(Some(first)).as_deref() {
|
|
420
|
+
Some("select" | "with" | "values") => self.query(s, 0),
|
|
421
|
+
Some("create") => self.create(s, 1),
|
|
422
|
+
Some("drop") => self.drop(s, 1),
|
|
423
|
+
Some("insert") if is_word(s.get(1), "into") => {
|
|
424
|
+
let end = self.target(WRITE, s, 2);
|
|
425
|
+
self.query(s, end);
|
|
426
|
+
}
|
|
427
|
+
// A session setting; never whose role or rights.
|
|
428
|
+
Some("set") => {
|
|
429
|
+
let refused = ["role", "authorization", "password", "global", "persist", "persist_only"];
|
|
430
|
+
if s.iter().skip(1).any(|r| word_of(Some(r)).is_some_and(|w| refused.contains(&w.as_str()))) {
|
|
431
|
+
self.push(FORBIDDEN);
|
|
432
|
+
} else {
|
|
433
|
+
s.iter().for_each(|r| self.plain(r));
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
Some("do") if self.engine == "psql" => self.block(s, 1),
|
|
437
|
+
// SQLite's in-memory database for synalog's tables, and no other.
|
|
438
|
+
Some("attach") if self.engine == "sqlite" => {
|
|
439
|
+
let rest: Vec<&Raw> = s.iter().skip(1).filter(|r| !is_word(Some(r), "database")).collect();
|
|
440
|
+
let memory = matches!(rest.as_slice(), [Raw::Str(path), as_, Raw::Word(name)]
|
|
441
|
+
if path == ":memory:" && is_word(Some(as_), "as") && name == home_schema());
|
|
442
|
+
self.push(if memory { OTHER } else { FORBIDDEN });
|
|
443
|
+
}
|
|
444
|
+
_ => self.push(FORBIDDEN),
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/// The statements of `sql`.
|
|
449
|
+
fn script(&mut self, sql: &str) {
|
|
450
|
+
let raws = lex(sql, self.engine);
|
|
451
|
+
let (parts, unclosed) = statements(&raws);
|
|
452
|
+
for part in parts {
|
|
453
|
+
self.statement(part);
|
|
454
|
+
self.push(OTHER);
|
|
455
|
+
}
|
|
456
|
+
if unclosed {
|
|
457
|
+
self.push(FORBIDDEN);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/// The statements of a script: split at each `;` outside a `DO` block, and
|
|
463
|
+
/// whether a block is left open.
|
|
464
|
+
fn statements(raws: &[Raw]) -> (Vec<&[Raw]>, bool) {
|
|
465
|
+
let mut parts = Vec::new();
|
|
466
|
+
let mut start = 0;
|
|
467
|
+
let mut in_block = false;
|
|
468
|
+
for (k, raw) in raws.iter().enumerate() {
|
|
469
|
+
match raw {
|
|
470
|
+
Raw::Dollar => in_block = !in_block,
|
|
471
|
+
Raw::Semi if !in_block => {
|
|
472
|
+
parts.push(&raws[start..k]);
|
|
473
|
+
start = k + 1;
|
|
474
|
+
}
|
|
475
|
+
_ => {}
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
parts.push(&raws[start..]);
|
|
479
|
+
(parts, in_block)
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/// Where synalog's tables are on `engine`, and its session's own temporary
|
|
483
|
+
/// schema, as `writes_in_home` takes them.
|
|
484
|
+
fn home_of(reader: &mut Reader) -> (Vec<u64>, Option<u64>) {
|
|
485
|
+
let home = home_schema();
|
|
486
|
+
let parts = if matches!(reader.engine, "trino" | "presto") { vec![home, home] } else { vec![home] };
|
|
487
|
+
let ids = parts.iter().map(|p| reader.id(p)).collect();
|
|
488
|
+
let scratch = (reader.engine == "psql").then(|| reader.id("pg_temp"));
|
|
489
|
+
(ids, scratch)
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/// Whether every statement of `scripts` writes in synalog_home only, as
|
|
493
|
+
/// `engine` reads it.
|
|
494
|
+
pub fn writes_only_in_home<'s>(scripts: impl IntoIterator<Item = &'s str>, engine: &str) -> bool {
|
|
495
|
+
let mut reader = Reader::new(engine);
|
|
496
|
+
let (home, scratch) = home_of(&mut reader);
|
|
497
|
+
for sql in scripts {
|
|
498
|
+
reader.script(sql);
|
|
499
|
+
reader.push(OTHER);
|
|
500
|
+
}
|
|
501
|
+
writes_in_home(&reader.tokens, &home, scratch)
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/// The first statement of `sql` that writes outside synalog_home, as the
|
|
505
|
+
/// reader saw it, for the error that refuses it.
|
|
506
|
+
pub fn first_outside(sql: &str, engine: &str) -> Option<String> {
|
|
507
|
+
let raws = lex(sql, engine);
|
|
508
|
+
let (parts, unclosed) = statements(&raws);
|
|
509
|
+
for part in parts {
|
|
510
|
+
let mut reader = Reader::new(engine);
|
|
511
|
+
let (home, scratch) = home_of(&mut reader);
|
|
512
|
+
reader.statement(part);
|
|
513
|
+
if !writes_in_home(&reader.tokens, &home, scratch) {
|
|
514
|
+
let text: Vec<String> = part
|
|
515
|
+
.iter()
|
|
516
|
+
.map(|raw| match raw {
|
|
517
|
+
Raw::Word(w) => w.clone(),
|
|
518
|
+
Raw::Quoted(w) => format!("<{w}>"),
|
|
519
|
+
Raw::Str(_) => "'…'".to_string(),
|
|
520
|
+
Raw::Dot => ".".to_string(),
|
|
521
|
+
Raw::Open => "(".to_string(),
|
|
522
|
+
Raw::Dollar => "$$".to_string(),
|
|
523
|
+
Raw::Sym(c) => c.to_string(),
|
|
524
|
+
Raw::Semi => ";".to_string(),
|
|
525
|
+
Raw::Bad => "…".to_string(),
|
|
526
|
+
})
|
|
527
|
+
.collect();
|
|
528
|
+
return Some(text.join(" ").replace(" . ", ".").chars().take(160).collect());
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
unclosed.then(|| "an unclosed $$ block".to_string())
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
#[cfg(test)]
|
|
535
|
+
#[path = "home_guard_test.rs"]
|
|
536
|
+
mod home_guard_test;
|