Skip to content

Commit 6fb0901

Browse files
Add testing AGGREGATE statements and fix aggregatesTemplate
1 parent fa8da7c commit 6fb0901

File tree

5 files changed

+82
-6
lines changed

5 files changed

+82
-6
lines changed

Diff for: docker-compose.yml

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ services:
44
node_1:
55
image: ${SCYLLA_IMAGE}
66
privileged: true
7-
command: --smp 2 --memory 768M --seeds 192.168.100.11 --overprovisioned 1
7+
command: |
8+
--smp 2
9+
--memory 768M
10+
--seeds 192.168.100.11
11+
--overprovisioned 1
12+
--experimental-features udf
13+
--enable-user-defined-functions true
814
networks:
915
public:
1016
ipv4_address: 192.168.100.11

Diff for: recreate.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ var functionTemplate = template.Must(template.New("functions").
109109
"stripFrozen": cqlHelpers.stripFrozen,
110110
}).
111111
Parse(`
112-
CREATE FUNCTION {{ escape .keyspaceName }}.{{ escape .fm.Name }} (
112+
CREATE FUNCTION {{ .keyspaceName }}.{{ .fm.Name }} (
113113
{{- range $i, $args := zip .fm.ArgumentNames .fm.ArgumentTypes }}
114114
{{- if ne $i 0 }}, {{ end }}
115-
{{- escape (index $args 0) }}
115+
{{- (index $args 0) }}
116116
{{ stripFrozen (index $args 1) }}
117117
{{- end -}})
118118
{{ if .fm.CalledOnNullInput }}CALLED{{ else }}RETURNS NULL{{ end }} ON NULL INPUT
@@ -167,19 +167,19 @@ var aggregatesTemplate = template.Must(template.New("aggregate").
167167
}).
168168
Parse(`
169169
CREATE AGGREGATE {{ .Keyspace }}.{{ .Name }}(
170-
{{- range $arg, $i := .ArgumentTypes }}
170+
{{- range $i, $arg := .ArgumentTypes }}
171171
{{- if ne $i 0 }}, {{ end }}
172172
{{ stripFrozen $arg }}
173173
{{- end -}})
174174
SFUNC {{ .StateFunc.Name }}
175-
STYPE {{ stripFrozen .State }}
175+
STYPE {{ stripFrozen .StateType }}
176176
{{- if ne .FinalFunc.Name "" }}
177177
FINALFUNC {{ .FinalFunc.Name }}
178178
{{- end -}}
179179
{{- if ne .InitCond "" }}
180180
INITCOND {{ .InitCond }}
181181
{{- end -}}
182-
);
182+
;
183183
`))
184184

185185
func (km *KeyspaceMetadata) aggregateToCQL(w io.Writer, am *AggregateMetadata) error {

Diff for: recreate_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ func TestRecreateSchema(t *testing.T) {
6161
Input: "testdata/recreate/udt.cql",
6262
Golden: "testdata/recreate/udt_golden.cql",
6363
},
64+
{
65+
Name: "Aggregates",
66+
Keyspace: "gocqlx_aggregates",
67+
Input: "testdata/recreate/aggregates.cql",
68+
Golden: "testdata/recreate/aggregates_golden.cql",
69+
},
6470
}
6571

6672
for i := range tcs {

Diff for: testdata/recreate/aggregates.cql

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
CREATE KEYSPACE gocqlx_aggregates WITH replication = {
2+
'class': 'SimpleStrategy',
3+
'replication_factor': '2'
4+
};
5+
6+
CREATE FUNCTION gocqlx_aggregates.avgstate(
7+
state tuple<int, double>,
8+
val double)
9+
CALLED ON NULL INPUT
10+
RETURNS frozen<tuple<int, double>>
11+
LANGUAGE lua
12+
AS $$
13+
return { state[1]+1, state[2]+val }
14+
$$;
15+
16+
CREATE FUNCTION gocqlx_aggregates.avgfinal(
17+
state tuple<int, double>)
18+
CALLED ON NULL INPUT
19+
RETURNS double
20+
LANGUAGE lua
21+
as $$
22+
r=0
23+
r=state[2]
24+
r=r/state[1]
25+
return r
26+
$$;
27+
28+
CREATE AGGREGATE gocqlx_aggregates.average(double)
29+
SFUNC avgstate STYPE tuple<int, double>
30+
FINALFUNC avgfinal
31+
INITCOND (0,0.0);

Diff for: testdata/recreate/aggregates_golden.cql

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
CREATE KEYSPACE gocqlx_aggregates WITH replication = {
2+
'class': 'SimpleStrategy',
3+
'replication_factor': '2'
4+
};
5+
6+
CREATE FUNCTION gocqlx_aggregates.avgstate (state
7+
tuple<int, double>, val
8+
double)
9+
CALLED ON NULL INPUT
10+
RETURNS frozen<tuple<int, double>>
11+
LANGUAGE lua
12+
AS $$
13+
return { state[1]+1, state[2]+val }
14+
$$;
15+
16+
CREATE FUNCTION gocqlx_aggregates.avgfinal (state
17+
tuple<int, double>)
18+
CALLED ON NULL INPUT
19+
RETURNS double
20+
LANGUAGE lua
21+
AS $$
22+
r=0
23+
r=state[2]
24+
r=r/state[1]
25+
return r
26+
$$;
27+
28+
CREATE AGGREGATE gocqlx_aggregates.average(
29+
double)
30+
SFUNC avgstate
31+
STYPE tuple<int, double>
32+
FINALFUNC avgfinal
33+
INITCOND (0, 0);

0 commit comments

Comments
 (0)