Skip to content

Commit 90a1422

Browse files
Added decay functions
Added distance functions chore: code clean up
1 parent 0f1d729 commit 90a1422

16 files changed

+316
-70
lines changed

composer.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@
3737
"psr-4": {
3838
"LaravelFreelancerNL\\FluentAQL\\": "src",
3939
"Tests\\": "tests"
40-
}
40+
},
41+
"files": [
42+
"src/helpers.php"
43+
]
4144
},
4245
"autoload-dev": {
4346
"classmap": [

docs/api/functions.md

+19-13
Original file line numberDiff line numberDiff line change
@@ -119,19 +119,25 @@ The following functions are directly supported in FluentAql.
119119

120120

121121
### Numeric functions
122-
| Description | AQL Function |
123-
| :-------------------- | :-------------------------------------------------------------------------------------------- |
124-
| average($value) | [AVERAGE(numArray](https://www.arangodb.com/docs/stable/aql/functions-numeric.html#average) |
125-
| avg($value) | [AVG(numArray](https://www.arangodb.com/docs/stable/aql/functions-numeric.html#average) |
126-
| ceil($value) | [CEIL(value)](https://www.arangodb.com/docs/stable/aql/functions-numeric.html#ceil) |
127-
| floor($value) | [FLOOR(value)](https://www.arangodb.com/docs/stable/aql/functions-numeric.html#floor) |
128-
| max($value) | [MAX(anyArray)](https://www.arangodb.com/docs/stable/aql/functions-numeric.html#max) |
129-
| min($value) | [MIN(anyArray)](https://www.arangodb.com/docs/stable/aql/functions-numeric.html#min) |
130-
| product($array) | [PRODUCT(numArray)](https://www.arangodb.com/docs/stable/aql/functions-numeric.html#min) |
131-
| rand() | [RAND()](https://www.arangodb.com/docs/stable/aql/functions-numeric.html#rand) |
132-
| range($start, $stop, $step) | [RANGE(start, stop, step)](https://www.arangodb.com/docs/stable/aql/functions-numeric.html#range) |
133-
| round($value) | [ROUND(value)](https://www.arangodb.com/docs/stable/aql/functions-numeric.html#round) |
134-
| sum($value) | [SUM(numArray)](https://www.arangodb.com/docs/stable/aql/functions-numeric.html#sum) |
122+
| Description | AQL Function |
123+
|:------------------------------------------------------|:----------------------------------------------------------------------------------------------------------------------------------|
124+
| average($value) | [AVERAGE(numArray](https://www.arangodb.com/docs/stable/aql/functions-numeric.html#average) |
125+
| avg($value) | [AVG(numArray](https://www.arangodb.com/docs/stable/aql/functions-numeric.html#average) |
126+
| ceil($value) | [CEIL(value)](https://www.arangodb.com/docs/stable/aql/functions-numeric.html#ceil) |
127+
| cosineSimilarity($x, $y) | [COSINE_SIMILARITY(x, y)](https://www.arangodb.com/docs/stable/aql/functions-numeric.html#cosine_similarity) |
128+
| decayGauss($value, $origin, $scale, $offset, $decay) | [DECAY_GAUSS(value, origin, scale, offset, decay)](https://www.arangodb.com/docs/stable/aql/functions-numeric.html#decay_gauss) |
129+
| decayExp($value, $origin, $scale, $offset, $decay) | [DECAY_EXP(value, origin, scale, offset, decay)](https://www.arangodb.com/docs/stable/aql/functions-numeric.html#decay_exp) |
130+
| decayLinear($value, $origin, $scale, $offset, $decay) | [DECAY_LINEAR(value, origin, scale, offset, decay)](https://www.arangodb.com/docs/stable/aql/functions-numeric.html#decay_linear) |
131+
| floor($value) | [FLOOR(value)](https://www.arangodb.com/docs/stable/aql/functions-numeric.html#floor) |
132+
| l1Distance($x, $y) | [L1_DISTANCE(x, y)](https://www.arangodb.com/docs/stable/aql/functions-numeric.html#l1_distance) |
133+
| l2Distance($x, $y) | [L2_DISTANCE(x, y)](https://www.arangodb.com/docs/stable/aql/functions-numeric.html#l2_distance) |
134+
| max($value) | [MAX(anyArray)](https://www.arangodb.com/docs/stable/aql/functions-numeric.html#max) |
135+
| min($value) | [MIN(anyArray)](https://www.arangodb.com/docs/stable/aql/functions-numeric.html#min) |
136+
| product($array) | [PRODUCT(numArray)](https://www.arangodb.com/docs/stable/aql/functions-numeric.html#min) |
137+
| rand() | [RAND()](https://www.arangodb.com/docs/stable/aql/functions-numeric.html#rand) |
138+
| range($start, $stop, $step) | [RANGE(start, stop, step)](https://www.arangodb.com/docs/stable/aql/functions-numeric.html#range) |
139+
| round($value) | [ROUND(value)](https://www.arangodb.com/docs/stable/aql/functions-numeric.html#round) |
140+
| sum($value) | [SUM(numArray)](https://www.arangodb.com/docs/stable/aql/functions-numeric.html#sum) |
135141

136142

137143
### String functions

phpmd-ruleset.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
<priority>3</priority>
4949
<properties>
5050
<property name="minimum" description="Minimum length for a variable, property or parameter name" value="2"/>
51-
<property name="exceptions" value="b,id,k,qb" />
51+
<property name="exceptions" value="b,id,k,qb,x,y" />
5252
</properties>
5353
</rule>
5454
<rule name="LongVariable"

src/AQL/HasNumericFunctions.php

+96
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,72 @@ public function ceil(
4747
return new FunctionExpression('CEIL', [$value]);
4848
}
4949

50+
/**
51+
* Return the cosine similarity between x and y.
52+
*
53+
* @link https://www.arangodb.com/docs/stable/aql/functions-numeric.html#cosine_similarity
54+
*
55+
* @param array<array<int|float>|int|float>|QueryBuilder|Expression $x
56+
* @param array<array<int|float>|int|float>|QueryBuilder|Expression $y
57+
*/
58+
public function cosineSimilarity(
59+
array|QueryBuilder|Expression $x,
60+
array|QueryBuilder|Expression $y
61+
): FunctionExpression {
62+
return new FunctionExpression('COSINE_SIMILARITY', [$x, $y]);
63+
}
64+
65+
/**
66+
* Calculate the score for one or multiple values with a Gaussian function that decays
67+
* depending on the distance of a numeric value from a user-given origin.
68+
*
69+
* @link https://www.arangodb.com/docs/stable/aql/functions-numeric.html#decay_gauss
70+
*
71+
* @param array<int|float>|int|float $value
72+
*/
73+
public function decayGauss(
74+
array|int|float|QueryBuilder|Expression $value,
75+
int|float|QueryBuilder|Expression $origin,
76+
int|float|QueryBuilder|Expression $scale,
77+
int|float|QueryBuilder|Expression $offset,
78+
int|float|QueryBuilder|Expression $decay,
79+
): FunctionExpression {
80+
return new FunctionExpression('DECAY_GAUSS', [$value, $origin, $scale, $offset, $decay]);
81+
}
82+
83+
/**
84+
* Calculate the score for one or multiple values with an exponential function that decays depending
85+
* on the distance of a numeric value from a user-given origin.
86+
*
87+
* @link https://www.arangodb.com/docs/stable/aql/functions-numeric.html#decay_exp
88+
*/
89+
public function decayExp(
90+
int|float|QueryBuilder|Expression $value,
91+
int|float|QueryBuilder|Expression $origin,
92+
int|float|QueryBuilder|Expression $scale,
93+
int|float|QueryBuilder|Expression $offset,
94+
int|float|QueryBuilder|Expression $decay,
95+
): FunctionExpression {
96+
return new FunctionExpression('DECAY_EXP', [$value, $origin, $scale, $offset, $decay]);
97+
}
98+
99+
/**
100+
* Calculate the score for one or multiple values with a linear function that decays depending
101+
* on the distance of a numeric value from a user-given origin.
102+
*
103+
* @link https://www.arangodb.com/docs/stable/aql/functions-numeric.html#decay_linear
104+
*/
105+
public function decayLinear(
106+
int|float|QueryBuilder|Expression $value,
107+
int|float|QueryBuilder|Expression $origin,
108+
int|float|QueryBuilder|Expression $scale,
109+
int|float|QueryBuilder|Expression $offset,
110+
int|float|QueryBuilder|Expression $decay,
111+
): FunctionExpression {
112+
return new FunctionExpression('DECAY_LINEAR', [$value, $origin, $scale, $offset, $decay]);
113+
}
114+
115+
50116
/**
51117
* Return the integer closest but not greater than value.
52118
*
@@ -58,6 +124,36 @@ public function floor(
58124
return new FunctionExpression('FLOOR', [$value]);
59125
}
60126

127+
/**
128+
* Return the Manhattan distance between x and y.
129+
*
130+
* @link https://www.arangodb.com/docs/stable/aql/functions-numeric.html#l1_distance
131+
*
132+
* @param array<array<int|float>|int|float>|QueryBuilder|Expression $x
133+
* @param array<array<int|float>|int|float>|QueryBuilder|Expression $y
134+
*/
135+
public function l1Distance(
136+
array|QueryBuilder|Expression $x,
137+
array|QueryBuilder|Expression $y
138+
): FunctionExpression {
139+
return new FunctionExpression('L1_DISTANCE', [$x, $y]);
140+
}
141+
142+
/**
143+
* Return the Euclidean distance between x and y.
144+
*
145+
* @link https://www.arangodb.com/docs/stable/aql/functions-numeric.html#l2_distance
146+
*
147+
* @param array<array<int|float>|int|float>|QueryBuilder|Expression $x
148+
* @param array<array<int|float>|int|float>|QueryBuilder|Expression $y
149+
*/
150+
public function l2Distance(
151+
array|QueryBuilder|Expression $x,
152+
array|QueryBuilder|Expression $y
153+
): FunctionExpression {
154+
return new FunctionExpression('L2_DISTANCE', [$x, $y]);
155+
}
156+
61157
/**
62158
* Return the largest element of an array.
63159
*

src/Clauses/UpsertClause.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace LaravelFreelancerNL\FluentAQL\Clauses;
66

7+
use LaravelFreelancerNL\FluentAQL\Exceptions\ExpressionTypeException;
78
use LaravelFreelancerNL\FluentAQL\Expressions\Expression;
89
use LaravelFreelancerNL\FluentAQL\QueryBuilder;
910

@@ -52,6 +53,9 @@ public function __construct(
5253
$this->replace = $replace;
5354
}
5455

56+
/**
57+
* @throws ExpressionTypeException
58+
*/
5559
public function compile(QueryBuilder $queryBuilder): string
5660
{
5761
$this->search = $queryBuilder->normalizeArgument($this->search, ['RegisteredVariable', 'Key', 'Bind']);
@@ -66,7 +70,7 @@ public function compile(QueryBuilder $queryBuilder): string
6670
}
6771

6872
return "UPSERT {$this->search->compile($queryBuilder)} " .
69-
"INSERT {$this->insert->compile($queryBuilder)} {$withClause} {$this->update->compile($queryBuilder)} " .
73+
"INSERT {$this->insert->compile($queryBuilder)} $withClause {$this->update->compile($queryBuilder)} " .
7074
"IN {$this->collection->compile($queryBuilder)}";
7175
}
7276
}

src/Clauses/WindowClause.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
namespace LaravelFreelancerNL\FluentAQL\Clauses;
66

7+
use LaravelFreelancerNL\FluentAQL\Exceptions\ExpressionTypeException;
78
use LaravelFreelancerNL\FluentAQL\Expressions\Expression;
89
use LaravelFreelancerNL\FluentAQL\QueryBuilder;
9-
use phpDocumentor\Reflection\Types\ArrayKey;
1010

1111
class WindowClause extends Clause
1212
{
@@ -25,10 +25,15 @@ public function __construct(
2525
array|object $offsets,
2626
null|string|QueryBuilder|Expression $rangeValue = null
2727
) {
28+
parent::__construct();
29+
2830
$this->offsets = $offsets;
2931
$this->rangeValue = $rangeValue;
3032
}
3133

34+
/**
35+
* @throws ExpressionTypeException
36+
*/
3237
public function compile(QueryBuilder $queryBuilder): string
3338
{
3439
$this->offsets = $queryBuilder->normalizeArgument(

src/Clauses/WithClause.php

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace LaravelFreelancerNL\FluentAQL\Clauses;
66

7+
use LaravelFreelancerNL\FluentAQL\Exceptions\ExpressionTypeException;
78
use LaravelFreelancerNL\FluentAQL\Expressions\Expression;
89
use LaravelFreelancerNL\FluentAQL\QueryBuilder;
910

@@ -27,6 +28,9 @@ public function __construct(
2728
$this->collections = $collections;
2829
}
2930

31+
/**
32+
* @throws ExpressionTypeException
33+
*/
3034
public function compile(QueryBuilder $queryBuilder): string
3135
{
3236
$collections = [];

src/Clauses/WithCountClause.php

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace LaravelFreelancerNL\FluentAQL\Clauses;
66

7+
use LaravelFreelancerNL\FluentAQL\Exceptions\ExpressionTypeException;
78
use LaravelFreelancerNL\FluentAQL\Expressions\Expression;
89
use LaravelFreelancerNL\FluentAQL\QueryBuilder;
910

@@ -14,9 +15,14 @@ class WithCountClause extends Clause
1415
public function __construct(
1516
string|QueryBuilder|Expression $countVariableName
1617
) {
18+
parent::__construct();
19+
1720
$this->countVariableName = $countVariableName;
1821
}
1922

23+
/**
24+
* @throws ExpressionTypeException
25+
*/
2026
public function compile(QueryBuilder $queryBuilder): string
2127
{
2228
$this->countVariableName = $queryBuilder->normalizeArgument(

src/Grammar.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class Grammar
146146
*/
147147
public function getDateFormat()
148148
{
149-
return 'Y-m-d\TH:i:s.v\Z';
149+
return 'Y-m-d\TH:i:s.vp';
150150
}
151151

152152
public function wrap(

0 commit comments

Comments
 (0)