Skip to content

Commit 393a49a

Browse files
committed
Remaining features for 2.3.0 release.
1 parent dd2bbf4 commit 393a49a

File tree

14 files changed

+177
-10
lines changed

14 files changed

+177
-10
lines changed

pb4php/ql2.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,8 @@ message Term {
367367
// The arity of the function should be
368368
// Sequence..., Function(sizeof...(Sequence)) -> Sequence
369369

370+
FOLD = 187; // Sequence, Datum, Function(2), {Function(3), Function(1)
371+
370372
// Filter a sequence with either a function or a shortcut
371373
// object (see API docs for details). The body of FILTER is
372374
// wrapped in an implicit `.default(false)`, and you can

rdb/DatumConverter.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public function nativeToFunction($f)
188188
if (!isset($result)) {
189189
// In case of null, assume that the user forgot to add a return.
190190
// If null is the intended value, r\expr() should be wrapped around the return value.
191-
throw new RqlDriverError("The function did not evaluate to a query (missing return?).");
191+
throw new RqlDriverError("The function did not evaluate to a value (missing return?). If the function is intended to return `null,` please use `return r\expr(null);`.");
192192
} else {
193193
$result = $this->nativeToDatum($result);
194194
}
@@ -197,7 +197,7 @@ public function nativeToFunction($f)
197197
return new RFunction($args, $result);
198198
}
199199

200-
public function nativeToDatumOrFunction($f)
200+
public function nativeToDatumOrFunction($f, $wrapImplicit = true)
201201
{
202202
if (!(is_object($f) && is_subclass_of($f, '\r\Query'))) {
203203
try {
@@ -210,6 +210,10 @@ public function nativeToDatumOrFunction($f)
210210
$f = $this->nativeToFunction($f);
211211
}
212212
}
213-
return $this->wrapImplicitVar($f);
213+
if ($wrapImplicit) {
214+
return $this->wrapImplicitVar($f);
215+
} else {
216+
return $f;
217+
}
214218
}
215219
}

rdb/ProtocolBuffer/TermTermType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class TermTermType
5454
const PB_FILL = 167;
5555
const PB_FILTER = 39;
5656
const PB_FLOOR = 183;
57+
const PB_FOLD = 187;
5758
const PB_FOR_EACH = 68;
5859
const PB_FRIDAY = 111;
5960
const PB_FUNC = 69;

rdb/Queries/Aggregations/Fold.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace r\Queries\Aggregations;
4+
5+
use r\ValuedQuery\ValuedQuery;
6+
use r\ProtocolBuffer\TermTermType;
7+
8+
class Fold extends ValuedQuery
9+
{
10+
public function __construct(ValuedQuery $sequence, $base, $fun, $opts = null)
11+
{
12+
$this->setPositionalArg(0, $sequence);
13+
$this->setPositionalArg(1, $this->nativeToDatum($base));
14+
$this->setPositionalArg(2, $this->nativeToFunction($fun));
15+
16+
if (isset($opts)) {
17+
foreach ($opts as $opt => $val) {
18+
$this->setOptionalArg($opt, $this->nativeToDatumOrFunction($val));
19+
}
20+
}
21+
}
22+
23+
protected function getTermType()
24+
{
25+
return TermTermType::PB_FOLD;
26+
}
27+
}

rdb/Queries/Control/Args.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace r\Queries\Control;
4+
5+
use r\ValuedQuery\ValuedQuery;
6+
use r\ProtocolBuffer\TermTermType;
7+
8+
class Args extends ValuedQuery
9+
{
10+
public function __construct($args)
11+
{
12+
$this->setPositionalArg(0, $this->nativeToDatum($args));
13+
}
14+
15+
protected function getTermType()
16+
{
17+
return TermTermType::PB_ARGS;
18+
}
19+
}

rdb/Queries/Control/Branch.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ class Branch extends ValuedQuery
1010
{
1111
public function __construct(Query $test, $trueBranch, $falseBranch)
1212
{
13-
$trueBranch = $this->nativeToDatumOrFunction($trueBranch);
14-
$falseBranch = $this->nativeToDatumOrFunction($falseBranch);
13+
$trueBranch = $this->nativeToDatumOrFunction($trueBranch, false);
14+
$falseBranch = $this->nativeToDatumOrFunction($falseBranch, false);
1515

1616
$this->setPositionalArg(0, $test);
1717
$this->setPositionalArg(1, $trueBranch);

rdb/Queries/Transformations/Union.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@
77

88
class Union extends ValuedQuery
99
{
10-
public function __construct(ValuedQuery $sequence, ValuedQuery $otherSequence)
10+
public function __construct(ValuedQuery $sequence, ValuedQuery $otherSequence, $opts = null)
1111
{
1212
$this->setPositionalArg(0, $sequence);
1313
$this->setPositionalArg(1, $otherSequence);
14+
if (isset($opts)) {
15+
foreach ($opts as $opt => $val) {
16+
$this->setOptionalArg($opt, $this->nativeToDatum($val));
17+
}
18+
}
1419
}
1520

1621
protected function getTermType()

rdb/Queries/Writing/Insert.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct(Table $table, $document, $opts = null)
2828
$this->setPositionalArg(1, $document);
2929
if (isset($opts)) {
3030
foreach ($opts as $opt => $val) {
31-
$this->setOptionalArg($opt, $this->nativeToDatum($val));
31+
$this->setOptionalArg($opt, $this->nativeToDatumOrFunction($val));
3232
}
3333
}
3434
}

rdb/ValuedQuery/ValuedQuery.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use r\Queries\Aggregations\Contains;
88
use r\Queries\Aggregations\Count;
99
use r\Queries\Aggregations\Distinct;
10+
use r\Queries\Aggregations\Fold;
1011
use r\Queries\Aggregations\Group;
1112
use r\Queries\Aggregations\Max;
1213
use r\Queries\Aggregations\Min;
@@ -189,9 +190,9 @@ public function isEmpty()
189190
{
190191
return new IsEmpty($this);
191192
}
192-
public function union(ValuedQuery $otherSequence)
193+
public function union(ValuedQuery $otherSequence, $opts = null)
193194
{
194-
return new Union($this, $otherSequence);
195+
return new Union($this, $otherSequence, $opts);
195196
}
196197
public function sample($n)
197198
{
@@ -201,6 +202,10 @@ public function reduce($reductionFunction)
201202
{
202203
return new Reduce($this, $reductionFunction);
203204
}
205+
public function fold($base, $fun, $opts = null)
206+
{
207+
return new Fold($this, $base, $fun, $opts);
208+
}
204209
public function count($filter = null)
205210
{
206211
return new Count($this, $filter);

rdb/global.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use r\Exceptions\RqlDriverError;
77
use r\Ordering\Asc;
88
use r\Ordering\Desc;
9+
use r\Queries\Control\Args;
910
use r\Queries\Control\Branch;
1011
use r\Queries\Control\Error;
1112
use r\Queries\Control\Http;
@@ -72,6 +73,7 @@
7273
use r\Queries\Tables\TableDrop;
7374
use r\Queries\Tables\TableList;
7475
use r\Queries\Transformations\MapMultiple;
76+
use r\Queries\Transformations\Union;
7577
use r\ValuedQuery\ImplicitVar;
7678
use r\Queries\Control\Js;
7779
use r\ValuedQuery\Json;
@@ -128,6 +130,11 @@ function rDo($args, $inExpr)
128130
return new RDo($args, $inExpr);
129131
}
130132

133+
function args($args)
134+
{
135+
return new Args($args);
136+
}
137+
131138
function branch(Query $test, $trueBranch, $falseBranch)
132139
{
133140
return new Branch($test, $trueBranch, $falseBranch);
@@ -439,6 +446,11 @@ function mapMultiple($sequences, $mappingFunction)
439446
return new MapMultiple($sequences[0], array_slice($sequences, 1), $mappingFunction);
440447
}
441448

449+
function union($sequence, $otherSequence, $opts = null)
450+
{
451+
return new Union($sequence, $otherSequence, $opts);
452+
}
453+
442454
function ceil($value)
443455
{
444456
return new Ceil($value);

rdb/version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
namespace r;
44

5-
define('PHP_RQL_VERSION', '2.2.0');
5+
define('PHP_RQL_VERSION', '2.3.0');

tests/Functional/FoldTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace r\Tests\Functional;
4+
5+
use r\Tests\TestCase;
6+
7+
class FoldTest extends TestCase
8+
{
9+
public function testFoldReduction()
10+
{
11+
$this->assertEquals(
12+
15.0,
13+
\r\expr(array(1, 2, 3, 4))
14+
->fold(5, function ($acc, $v) {
15+
return $acc->add($v);
16+
})
17+
->run($this->conn)
18+
);
19+
}
20+
21+
public function testFoldEmit()
22+
{
23+
$this->assertEquals(
24+
array(5, 6, 8, 11, 15),
25+
\r\expr(array(1, 2, 3, 4))
26+
->fold(5, function ($acc, $v) {
27+
return $acc->add($v);
28+
},
29+
array(
30+
'emit' => function($o, $c, $n) { return array($o); },
31+
'final_emit' => function($a) { return array($a); }
32+
))
33+
->run($this->conn)
34+
);
35+
}
36+
}

tests/Functional/InsertTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace r\Tests\Functional;
4+
5+
use r\Tests\TestCase;
6+
7+
class InsertTest extends TestCase
8+
{
9+
public function setUp()
10+
{
11+
$this->conn = $this->getConnection();
12+
$this->data = $this->useDataset('Heroes');
13+
$this->data->populate();
14+
$this->opts = array();
15+
}
16+
17+
public function tearDown()
18+
{
19+
$this->data->truncate();
20+
}
21+
22+
public function testCustomConflict()
23+
{
24+
$res = $this->db()->table('marvel')->insert(
25+
array(
26+
'superhero' => 'Iron Man',
27+
),
28+
array(
29+
'conflict' => function($x, $k, $o) { return \r\expr(null); }
30+
)
31+
)->run($this->conn, $this->opts);
32+
33+
$this->assertObStatus(array('deleted' => 1), $res);
34+
}
35+
}

tests/Functional/TransformationsTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,27 @@ public function testPluckUnion()
214214
);
215215
}
216216

217+
public function testGlobalUnion()
218+
{
219+
$this->assertEquals(
220+
array(1, 3, 2, 4),
221+
\r\union(\r\expr(array(1, 3)), \r\expr(array(2, 4)), array('interleave' => false))
222+
->run($this->conn)
223+
);
224+
}
225+
226+
public function testGlobalUnionInterleave()
227+
{
228+
$this->assertEquals(
229+
array(1, 2, 3, 4),
230+
\r\union(\r\expr(array(array('a' => 1), array('a' => 3))),
231+
\r\expr(array(array('a' => 2), array('a' => 4))),
232+
array('interleave' => 'a'))
233+
->getField('a')
234+
->run($this->conn)
235+
);
236+
}
237+
217238
public function testWithFields()
218239
{
219240
$res = $this->db()->table('marvel')

0 commit comments

Comments
 (0)