forked from datastax/php-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
function_and_aggregate_metadata.feature
139 lines (131 loc) · 5.11 KB
/
function_and_aggregate_metadata.feature
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
@cassandra-version-2.2
Feature: User-defined Function (UDF) and Aggregate Metadata (UDA)
PHP Driver exposes the Cassandra Schema Metadata for UDFs and UDAs.
Background:
Given a running cassandra cluster
And the following schema:
"""cql
CREATE KEYSPACE simplex WITH replication = {
'class': 'SimpleStrategy',
'replication_factor': 1
} AND DURABLE_WRITES = false;
USE simplex;
CREATE OR REPLACE FUNCTION fLog (input double) CALLED ON NULL INPUT RETURNS double LANGUAGE java AS 'return Double.valueOf(Math.log(input.doubleValue()));';
CREATE OR REPLACE FUNCTION avgState ( state tuple<int,bigint>, val int ) CALLED ON NULL INPUT RETURNS tuple<int,bigint> LANGUAGE java AS 'if (val !=null) { state.setInt(0, state.getInt(0)+1); state.setLong(1, state.getLong(1)+val.intValue()); } return state;';
CREATE OR REPLACE FUNCTION avgFinal ( state tuple<int,bigint> ) CALLED ON NULL INPUT RETURNS double LANGUAGE java AS 'double r = 0; if (state.getInt(0) == 0) return null; r = state.getLong(1); r/= state.getInt(0); return Double.valueOf(r);';
CREATE AGGREGATE IF NOT EXISTS average ( int ) SFUNC avgState STYPE tuple<int,bigint> FINALFUNC avgFinal INITCOND (0,0);
"""
Scenario: Getting a function's metadata
Given the following example:
"""php
<?php
$cluster = Cassandra::cluster()
->withContactPoints('127.0.0.1')
->build();
$session = $cluster->connect("simplex");
$schema = $session->schema();
$function = $schema->keyspace("simplex")->function("flog", Cassandra\Type::double());
echo "Name: " . $function->simpleName() . "\n";
echo "Signature: " . $function->signature() . "\n";
echo "Language: " . $function->language() . "\n";
echo "Body: " . $function->body() . "\n";
echo "Arguments: " . var_export($function->arguments(), true) . "\n";
echo "ReturnType: " . var_export($function->returnType(), true) . "\n";
echo "IsCalledOnNullInput: " . ($function->isCalledOnNullInput() ? "true" : "false") . "\n";
"""
When it is executed
Then its output should contain:
"""
Name: flog
Signature: flog(double)
Language: java
Body: return Double.valueOf(Math.log(input.doubleValue()));
Arguments: array (
'input' =>
Cassandra\Type\Scalar::__set_state(array(
'name' => 'double',
)),
)
ReturnType: Cassandra\Type\Scalar::__set_state(array(
'name' => 'double',
))
IsCalledOnNullInput: true
"""
@skip-ci
Scenario: Getting an aggregates's metadata
Given the following example:
"""php
<?php
$cluster = Cassandra::cluster()
->withContactPoints('127.0.0.1')
->build();
$session = $cluster->connect("simplex");
$schema = $session->schema();
$aggregate = $schema->keyspace("simplex")->aggregate("average", Cassandra\Type::int());
echo "Name: " . $aggregate->simpleName() . "\n";
echo "Signature: " . $aggregate->signature() . "\n";
echo "ArgumentTypes: " . var_export($aggregate->argumentTypes(), true) . "\n";
echo "StateType: " . var_export($aggregate->stateType(), true) . "\n";
echo "ReturnType: " . var_export($aggregate->returnType(), true) . "\n";
echo "InitialCondition: " . var_export($aggregate->initialCondition(), true) . "\n";
echo "StateFunction: " . $aggregate->stateFunction()->signature() . "\n";
echo "FinalFunction: " . $aggregate->finalFunction()->signature() . "\n";
"""
When it is executed
Then its output should contain:
"""
Name: average
Signature: average(int)
ArgumentTypes: array (
0 =>
Cassandra\Type\Scalar::__set_state(array(
'name' => 'int',
)),
)
StateType: Cassandra\Type\Tuple::__set_state(array(
'types' =>
array (
0 =>
Cassandra\Type\Scalar::__set_state(array(
'name' => 'int',
)),
1 =>
Cassandra\Type\Scalar::__set_state(array(
'name' => 'bigint',
)),
),
))
ReturnType: Cassandra\Type\Scalar::__set_state(array(
'name' => 'double',
))
InitialCondition: Cassandra\Tuple::__set_state(array(
'type' =>
Cassandra\Type\Tuple::__set_state(array(
'types' =>
array (
0 =>
Cassandra\Type\Scalar::__set_state(array(
'name' => 'int',
)),
1 =>
Cassandra\Type\Scalar::__set_state(array(
'name' => 'bigint',
)),
),
)),
'values' =>
array (
0 => 0,
1 =>
Cassandra\Bigint::__set_state(array(
'type' =>
Cassandra\Type\Scalar::__set_state(array(
'name' => 'bigint',
)),
'value' => '0',
)),
),
))
StateFunction: avgstate(frozen<tuple<int,bigint>>,int)
FinalFunction: avgfinal(frozen<tuple<int,bigint>>)
"""