Skip to content

Commit

Permalink
Add testing AGGREGATE statements and fix aggregatesTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
sylwiaszunejko committed Oct 31, 2023
1 parent fa8da7c commit 6fb0901
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 6 deletions.
8 changes: 7 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ services:
node_1:
image: ${SCYLLA_IMAGE}
privileged: true
command: --smp 2 --memory 768M --seeds 192.168.100.11 --overprovisioned 1
command: |
--smp 2
--memory 768M
--seeds 192.168.100.11
--overprovisioned 1
--experimental-features udf
--enable-user-defined-functions true
networks:
public:
ipv4_address: 192.168.100.11
Expand Down
10 changes: 5 additions & 5 deletions recreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ var functionTemplate = template.Must(template.New("functions").
"stripFrozen": cqlHelpers.stripFrozen,
}).
Parse(`
CREATE FUNCTION {{ escape .keyspaceName }}.{{ escape .fm.Name }} (
CREATE FUNCTION {{ .keyspaceName }}.{{ .fm.Name }} (
{{- range $i, $args := zip .fm.ArgumentNames .fm.ArgumentTypes }}
{{- if ne $i 0 }}, {{ end }}
{{- escape (index $args 0) }}
{{- (index $args 0) }}
{{ stripFrozen (index $args 1) }}
{{- end -}})
{{ if .fm.CalledOnNullInput }}CALLED{{ else }}RETURNS NULL{{ end }} ON NULL INPUT
Expand Down Expand Up @@ -167,19 +167,19 @@ var aggregatesTemplate = template.Must(template.New("aggregate").
}).
Parse(`
CREATE AGGREGATE {{ .Keyspace }}.{{ .Name }}(
{{- range $arg, $i := .ArgumentTypes }}
{{- range $i, $arg := .ArgumentTypes }}
{{- if ne $i 0 }}, {{ end }}
{{ stripFrozen $arg }}
{{- end -}})
SFUNC {{ .StateFunc.Name }}
STYPE {{ stripFrozen .State }}
STYPE {{ stripFrozen .StateType }}
{{- if ne .FinalFunc.Name "" }}
FINALFUNC {{ .FinalFunc.Name }}
{{- end -}}
{{- if ne .InitCond "" }}
INITCOND {{ .InitCond }}
{{- end -}}
);
;
`))

func (km *KeyspaceMetadata) aggregateToCQL(w io.Writer, am *AggregateMetadata) error {
Expand Down
6 changes: 6 additions & 0 deletions recreate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ func TestRecreateSchema(t *testing.T) {
Input: "testdata/recreate/udt.cql",
Golden: "testdata/recreate/udt_golden.cql",
},
{
Name: "Aggregates",
Keyspace: "gocqlx_aggregates",
Input: "testdata/recreate/aggregates.cql",
Golden: "testdata/recreate/aggregates_golden.cql",
},
}

for i := range tcs {
Expand Down
31 changes: 31 additions & 0 deletions testdata/recreate/aggregates.cql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
CREATE KEYSPACE gocqlx_aggregates WITH replication = {
'class': 'SimpleStrategy',
'replication_factor': '2'
};

CREATE FUNCTION gocqlx_aggregates.avgstate(
state tuple<int, double>,
val double)
CALLED ON NULL INPUT
RETURNS frozen<tuple<int, double>>
LANGUAGE lua
AS $$
return { state[1]+1, state[2]+val }
$$;

CREATE FUNCTION gocqlx_aggregates.avgfinal(
state tuple<int, double>)
CALLED ON NULL INPUT
RETURNS double
LANGUAGE lua
as $$
r=0
r=state[2]
r=r/state[1]
return r
$$;

CREATE AGGREGATE gocqlx_aggregates.average(double)
SFUNC avgstate STYPE tuple<int, double>
FINALFUNC avgfinal
INITCOND (0,0.0);
33 changes: 33 additions & 0 deletions testdata/recreate/aggregates_golden.cql
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
CREATE KEYSPACE gocqlx_aggregates WITH replication = {
'class': 'SimpleStrategy',
'replication_factor': '2'
};

CREATE FUNCTION gocqlx_aggregates.avgstate (state
tuple<int, double>, val
double)
CALLED ON NULL INPUT
RETURNS frozen<tuple<int, double>>
LANGUAGE lua
AS $$
return { state[1]+1, state[2]+val }
$$;

CREATE FUNCTION gocqlx_aggregates.avgfinal (state
tuple<int, double>)
CALLED ON NULL INPUT
RETURNS double
LANGUAGE lua
AS $$
r=0
r=state[2]
r=r/state[1]
return r
$$;

CREATE AGGREGATE gocqlx_aggregates.average(
double)
SFUNC avgstate
STYPE tuple<int, double>
FINALFUNC avgfinal
INITCOND (0, 0);

0 comments on commit 6fb0901

Please sign in to comment.