Skip to content

Commit 5f07a2a

Browse files
authored
Procmacro to derive unary/binary function definitions (#32083)
Similar to the `sqlfunc!` macro, this proc macro derives the helper traits for unary and binary functions. Here's a binary function for which we generate the `EagerBinaryFunc` trait implementation: ```rust #[sqlfunc(is_monotone = (true, true), output_type = i16, is_infix_op = true, sqlname="+")] fn add_int16<'a>(a: Datum<'a>, b: Datum<'a>) -> Result<Datum<'a>, EvalError> { a.unwrap_int16() .checked_add(b.unwrap_int16()) .ok_or(EvalError::NumericFieldOverflow) .map(Datum::from) } ``` The parameters control the behavior of the code that we generate and allow the function to override the default behavior. ### Checklist - [ ] This PR has adequate test coverage / QA involvement has been duly considered. ([trigger-ci for additional test/nightly runs](https://trigger-ci.dev.materialize.com/)) - [ ] This PR has an associated up-to-date [design doc](https://github.com/MaterializeInc/materialize/blob/main/doc/developer/design/README.md), is a design doc ([template](https://github.com/MaterializeInc/materialize/blob/main/doc/developer/design/00000000_template.md)), or is sufficiently small to not require a design. <!-- Reference the design in the description. --> - [ ] If this PR evolves [an existing `$T ⇔ Proto$T` mapping](https://github.com/MaterializeInc/materialize/blob/main/doc/developer/command-and-response-binary-encoding.md) (possibly in a backwards-incompatible way), then it is tagged with a `T-proto` label. - [ ] If this PR will require changes to cloud orchestration or tests, there is a companion cloud PR to account for those changes that is tagged with the release-blocker label ([example](MaterializeInc/cloud#5021)). <!-- Ask in #team-cloud on Slack if you need help preparing the cloud PR. --> - [ ] If this PR includes major [user-facing behavior changes](https://github.com/MaterializeInc/materialize/blob/main/doc/developer/guide-changes.md#what-changes-require-a-release-note), I have pinged the relevant PM to schedule a changelog post. --------- Signed-off-by: Moritz Hoffmann <mh@materialize.com>
1 parent 2b99ef8 commit 5f07a2a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+3000
-24
lines changed

Cargo.lock

Lines changed: 35 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
[workspace]
22
members = [
3+
"misc/bazel/cargo-gazelle",
34
"src/adapter",
45
"src/adapter-types",
56
"src/alloc",
67
"src/alloc-default",
78
"src/arrow-util",
89
"src/audit-log",
910
"src/avro",
10-
"src/aws-util",
1111
"src/aws-secrets-controller",
12+
"src/aws-util",
1213
"src/balancerd",
1314
"src/build-info",
1415
"src/build-tools",
15-
"src/ccsr",
1616
"src/catalog",
1717
"src/catalog-debug",
1818
"src/catalog-protos",
19+
"src/ccsr",
1920
"src/cloud-api",
2021
"src/cloud-provider",
22+
"src/cloud-resources",
2123
"src/cluster",
22-
"src/clusterd",
2324
"src/cluster-client",
25+
"src/clusterd",
2426
"src/compute",
2527
"src/compute-client",
2628
"src/compute-types",
@@ -32,6 +34,8 @@ members = [
3234
"src/dyncfgs",
3335
"src/environmentd",
3436
"src/expr",
37+
"src/expr-derive",
38+
"src/expr-derive-impl",
3539
"src/expr-parser",
3640
"src/expr-test-util",
3741
"src/fivetran-destination",
@@ -44,14 +48,14 @@ members = [
4448
"src/license-keys",
4549
"src/lowertest",
4650
"src/lowertest-derive",
51+
"src/lsp-server",
52+
"src/materialized",
4753
"src/materialized",
4854
"src/metabase",
55+
"src/metrics",
4956
"src/mysql-util",
5057
"src/mz",
5158
"src/mz-debug",
52-
"src/lsp-server",
53-
"src/materialized",
54-
"src/metrics",
5559
"src/npm",
5660
"src/orchestrator",
5761
"src/orchestrator-kubernetes",
@@ -65,7 +69,6 @@ members = [
6569
"src/persist-cli",
6670
"src/persist-client",
6771
"src/persist-proc",
68-
"src/txn-wal",
6972
"src/persist-types",
7073
"src/pgcopy",
7174
"src/pgrepr",
@@ -79,6 +82,7 @@ members = [
7982
"src/prof",
8083
"src/prof-http",
8184
"src/proto",
85+
"src/regexp",
8286
"src/repr",
8387
"src/repr-test-util",
8488
"src/rocksdb",
@@ -88,13 +92,13 @@ members = [
8892
"src/segment",
8993
"src/server-core",
9094
"src/service",
91-
"src/ssh-util",
9295
"src/sql",
9396
"src/sql-lexer",
9497
"src/sql-parser",
9598
"src/sql-pretty",
9699
"src/sql-server-util",
97100
"src/sqllogictest",
101+
"src/ssh-util",
98102
"src/storage",
99103
"src/storage-client",
100104
"src/storage-controller",
@@ -106,13 +110,11 @@ members = [
106110
"src/tls-util",
107111
"src/tracing",
108112
"src/transform",
109-
"src/cloud-resources",
113+
"src/txn-wal",
110114
"src/walkabout",
111115
"src/workspace-hack",
112116
"test/metabase/smoketest",
113117
"test/test-util",
114-
"src/regexp",
115-
"misc/bazel/cargo-gazelle",
116118
]
117119

118120
# We make all members of the workspace default members, *except* for a few
@@ -155,6 +157,8 @@ default-members = [
155157
"src/dyncfgs",
156158
"src/environmentd",
157159
"src/expr",
160+
"src/expr-derive",
161+
"src/expr-derive-impl",
158162
"src/expr-parser",
159163
"src/expr-test-util",
160164
"src/frontegg-auth",
@@ -269,6 +273,10 @@ differential-dataflow = { opt-level = 3 }
269273
mz-compute = { opt-level = 3 }
270274
mz-transform = { opt-level = 3 }
271275

276+
# Recommended by insta
277+
insta = { opt-level = 3 }
278+
similar = { opt-level = 3 }
279+
272280
[profile.release]
273281
# Compile time seems similar to "lto = false", runtime ~10% faster
274282
lto = "thin"

WORKSPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,8 @@ crates_repository(
558558
"rustc-demangle": [crate.annotation(rustc_flags = ["-Copt-level=3"])],
559559
"timely": [crate.annotation(rustc_flags = ["-Copt-level=3"])],
560560
"differential-dataflow": [crate.annotation(rustc_flags = ["-Copt-level=3"])],
561+
"insta": [crate.annotation(rustc_flags = ["-Copt-level=3"])],
562+
"similar": [crate.annotation(rustc_flags = ["-Copt-level=3"])],
561563
},
562564
cargo_config = "//:.cargo/config.toml",
563565
cargo_lockfile = "//:Cargo.lock",
@@ -611,6 +613,8 @@ crates_repository(
611613
"//:src/dyncfg-launchdarkly/Cargo.toml",
612614
"//:src/dyncfgs/Cargo.toml",
613615
"//:src/environmentd/Cargo.toml",
616+
"//:src/expr-derive/Cargo.toml",
617+
"//:src/expr-derive-impl/Cargo.toml",
614618
"//:src/expr-parser/Cargo.toml",
615619
"//:src/expr-test-util/Cargo.toml",
616620
"//:src/expr/Cargo.toml",

ci/test/lint-main/checks/check-copyright.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ copyright_files=$(grep -vE \
6060
-e '^src/catalog/tests/snapshots/.*\.snap' \
6161
-e '^src/catalog/src/durable/upgrade/snapshots/.*' \
6262
-e '^src/catalog/src/durable/upgrade/persist/snapshots/.*\.snap' \
63+
-e '^src/expr-derive-impl/src/snapshots.*' \
64+
-e '^src/expr/src/scalar/func/snapshots/.*' \
65+
-e '^src/expr/src/scalar/snapshots/.*' \
6366
-e '^src/license-keys/src/license_keys/.*\.pub' \
6467
-e '^src/storage-types/src/snapshots/.*' \
6568
-e '^src/repr/src/adt/snapshots/.*' \

src/expr-derive-impl/BUILD.bazel

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Code generated by cargo-gazelle DO NOT EDIT
2+
3+
# Copyright Materialize, Inc. and contributors. All rights reserved.
4+
#
5+
# Use of this software is governed by the Business Source License
6+
# included in the LICENSE file at the root of this repository.
7+
#
8+
# As of the Change Date specified in that file, in accordance with
9+
# the Business Source License, use of this software will be governed
10+
# by the Apache License, Version 2.0.
11+
12+
load("@crates_io//:defs.bzl", "aliases", "all_crate_deps")
13+
load("@rules_rust//cargo:defs.bzl", "extract_cargo_lints")
14+
load("@rules_rust//rust:defs.bzl", "rust_doc_test", "rust_library", "rust_test")
15+
16+
package(default_visibility = ["//visibility:public"])
17+
18+
rust_library(
19+
name = "mz_expr_derive_impl",
20+
srcs = glob(["src/**/*.rs"]),
21+
aliases = aliases(
22+
normal = True,
23+
proc_macro = True,
24+
),
25+
compile_data = [],
26+
crate_features = [
27+
"default",
28+
"insta",
29+
"prettyplease",
30+
"test",
31+
],
32+
data = [],
33+
lint_config = ":lints",
34+
proc_macro_deps = [] + all_crate_deps(proc_macro = True),
35+
rustc_env = {},
36+
rustc_flags = ["-Copt-level=3"],
37+
version = "0.0.0",
38+
deps = [] + all_crate_deps(normal = True),
39+
)
40+
41+
alias(
42+
name = "expr-derive-impl",
43+
actual = "mz_expr_derive_impl",
44+
)
45+
46+
rust_test(
47+
name = "mz_expr_derive_impl_lib_tests",
48+
size = "medium",
49+
aliases = aliases(
50+
normal = True,
51+
normal_dev = True,
52+
proc_macro = True,
53+
proc_macro_dev = True,
54+
),
55+
compile_data = [],
56+
crate = ":mz_expr_derive_impl",
57+
crate_features = [
58+
"default",
59+
"insta",
60+
"prettyplease",
61+
"test",
62+
],
63+
data = [],
64+
env = {},
65+
lint_config = ":lints",
66+
proc_macro_deps = [] + all_crate_deps(
67+
proc_macro = True,
68+
proc_macro_dev = True,
69+
),
70+
rustc_env = {},
71+
rustc_flags = [],
72+
version = "0.0.0",
73+
deps = ["//src/ore:mz_ore"] + all_crate_deps(
74+
normal = True,
75+
normal_dev = True,
76+
),
77+
)
78+
79+
rust_doc_test(
80+
name = "mz_expr_derive_impl_doc_test",
81+
crate = ":mz_expr_derive_impl",
82+
deps = ["//src/ore:mz_ore"] + all_crate_deps(
83+
normal = True,
84+
normal_dev = True,
85+
),
86+
)
87+
88+
extract_cargo_lints(
89+
name = "lints",
90+
manifest = "Cargo.toml",
91+
workspace = "@//:Cargo.toml",
92+
)

src/expr-derive-impl/Cargo.toml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[package]
2+
name = "mz-expr-derive-impl"
3+
description = "Implementations of derives for the core expression language for Materialize."
4+
version = "0.0.0"
5+
edition.workspace = true
6+
rust-version.workspace = true
7+
publish = false
8+
9+
[lints]
10+
workspace = true
11+
12+
[dependencies]
13+
darling = "0.20.11"
14+
insta = { version = "1.42", optional = true, features = ["serde"] }
15+
prettyplease = { version = "0.2", optional = true }
16+
proc-macro2 = "1.0"
17+
quote = "1.0"
18+
syn = { version = "2.0", features = ["extra-traits"] }
19+
workspace-hack = { version = "0.0.0", path = "../workspace-hack", optional = true }
20+
21+
[dev-dependencies]
22+
insta = { version = "1.42", features = ["serde"] }
23+
mz-ore = { path = "../ore", features = ["test"] }
24+
prettyplease = "0.2"
25+
26+
[package.metadata.cargo-gazelle.lib]
27+
rustc_flags = ["-Copt-level=3"]
28+
29+
[package.metadata.cargo-udeps.ignore]
30+
normal = ["workspace-hack"]
31+
32+
[features]
33+
default = ["workspace-hack"]
34+
test = ["prettyplease", "insta"]

0 commit comments

Comments
 (0)