Skip to content

Commit 007c39c

Browse files
committed
fix examples so that they all work with scylla
1 parent 3776a48 commit 007c39c

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ wasm2wat target/wasm32-wasi/debug/abc.wasm > target/wasm32/wasi/debug/abc.wat
4040
### CQL Statement
4141

4242
The resulting `target/wasm32/wasi/debug/abc.wat` code can now be used directly in a `CREATE FUNCTION` statement. The resulting code will most likely
43-
contain `'` characters, so it's recommended to use a `LANGUAGE xwasm AS $$ (module ...) $$` clause instead of `LANGUAGE xwasm AS ' (module ...) '`.
43+
contain `'` characters, so it may be necessary to first replace them with `''`, so that they're usable in a CQL string.
4444

45-
For example, if you have an [Rust UDF](examples/commas.rs) that joins a list of words using commas you can create a Scylla UDF using the following statement:
45+
For example, if you have an [Rust UDF](examples/commas.rs) that joins a list of words using commas, you can create a Scylla UDF using the following statement:
4646
```
47-
CREATE FUNCTION commas(string list<text>) CALLED ON NULL INPUT RETURNS text AS $$ (module ...) $$
47+
CREATE FUNCTION commas(string list<text>) CALLED ON NULL INPUT RETURNS text AS ' (module ...) '
4848
```
4949

5050

examples/combine.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
use scylla_udf::{export_udf, Counter, CqlDuration, Time, Timestamp};
1+
use scylla_udf::{export_udf, CqlDuration, Time, Timestamp};
22

33
#[allow(clippy::too_many_arguments, clippy::type_complexity)]
44
#[export_udf]
55
fn combine(
66
b: bool,
77
blob: Vec<u8>,
8-
cnt: Counter,
98
date: chrono::NaiveDate,
109
bd: bigdecimal::BigDecimal,
1110
dbl: f64,
@@ -25,7 +24,6 @@ fn combine(
2524
(
2625
bool,
2726
Vec<u8>,
28-
Counter,
2927
chrono::NaiveDate,
3028
bigdecimal::BigDecimal,
3129
f64,
@@ -46,7 +44,7 @@ fn combine(
4644
),
4745
) {
4846
(
49-
(b, blob, cnt, date, bd, dbl, cqldur, flt, int32, int64),
47+
(b, blob, date, bd, dbl, cqldur, flt, int32, int64),
5048
(s, tstamp, ip, int16, int8, tim, uid, bi),
5149
)
5250
}

examples/topn.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,45 @@ struct StringLen(String);
66

77
impl std::cmp::PartialEq for StringLen {
88
fn eq(&self, other: &Self) -> bool {
9-
self.0.len() == other.0.len()
9+
self.0 == other.0
1010
}
1111
}
1212

1313
impl std::cmp::Eq for StringLen {}
1414

1515
impl std::cmp::PartialOrd for StringLen {
1616
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
17-
Some(self.0.len().cmp(&other.0.len()))
17+
Some(self.cmp(other))
1818
}
1919
}
2020

2121
impl std::cmp::Ord for StringLen {
2222
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
23-
self.0.len().cmp(&other.0.len())
23+
if self.0.len().cmp(&other.0.len()) == std::cmp::Ordering::Equal {
24+
self.0.cmp(&other.0)
25+
} else {
26+
self.0.len().cmp(&other.0.len())
27+
}
2428
}
2529
}
2630

2731
// Store the top N strings by length, without repetitions.
2832
#[export_udf]
29-
fn topn_row((n, mut acc): (i32, BTreeSet<StringLen>), v: StringLen) -> (i32, BTreeSet<StringLen>) {
30-
acc.insert(v);
31-
while acc.len() > n as usize {
32-
acc.pop_first();
33+
fn topn_row(
34+
acc_tup: Option<(i32, BTreeSet<StringLen>)>,
35+
v: Option<StringLen>,
36+
) -> Option<(i32, BTreeSet<StringLen>)> {
37+
if let Some((n, mut acc)) = acc_tup {
38+
if let Some(v) = v {
39+
acc.insert(v);
40+
while acc.len() > n as usize {
41+
acc.pop_first();
42+
}
43+
}
44+
Some((n, acc))
45+
} else {
46+
None
3347
}
34-
35-
(n, acc)
3648
}
3749

3850
#[export_udf]

0 commit comments

Comments
 (0)