Skip to content
This repository has been archived by the owner on Feb 5, 2020. It is now read-only.

Commit

Permalink
Remaining features for 2.3.0 release.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmewes committed May 31, 2016
1 parent dd2bbf4 commit 393a49a
Show file tree
Hide file tree
Showing 14 changed files with 177 additions and 10 deletions.
2 changes: 2 additions & 0 deletions pb4php/ql2.proto
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ message Term {
// The arity of the function should be
// Sequence..., Function(sizeof...(Sequence)) -> Sequence

FOLD = 187; // Sequence, Datum, Function(2), {Function(3), Function(1)

// Filter a sequence with either a function or a shortcut
// object (see API docs for details). The body of FILTER is
// wrapped in an implicit `.default(false)`, and you can
Expand Down
10 changes: 7 additions & 3 deletions rdb/DatumConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public function nativeToFunction($f)
if (!isset($result)) {
// In case of null, assume that the user forgot to add a return.
// If null is the intended value, r\expr() should be wrapped around the return value.
throw new RqlDriverError("The function did not evaluate to a query (missing return?).");
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);`.");
} else {
$result = $this->nativeToDatum($result);
}
Expand All @@ -197,7 +197,7 @@ public function nativeToFunction($f)
return new RFunction($args, $result);
}

public function nativeToDatumOrFunction($f)
public function nativeToDatumOrFunction($f, $wrapImplicit = true)
{
if (!(is_object($f) && is_subclass_of($f, '\r\Query'))) {
try {
Expand All @@ -210,6 +210,10 @@ public function nativeToDatumOrFunction($f)
$f = $this->nativeToFunction($f);
}
}
return $this->wrapImplicitVar($f);
if ($wrapImplicit) {
return $this->wrapImplicitVar($f);
} else {
return $f;
}
}
}
1 change: 1 addition & 0 deletions rdb/ProtocolBuffer/TermTermType.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class TermTermType
const PB_FILL = 167;
const PB_FILTER = 39;
const PB_FLOOR = 183;
const PB_FOLD = 187;
const PB_FOR_EACH = 68;
const PB_FRIDAY = 111;
const PB_FUNC = 69;
Expand Down
27 changes: 27 additions & 0 deletions rdb/Queries/Aggregations/Fold.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace r\Queries\Aggregations;

use r\ValuedQuery\ValuedQuery;
use r\ProtocolBuffer\TermTermType;

class Fold extends ValuedQuery
{
public function __construct(ValuedQuery $sequence, $base, $fun, $opts = null)
{
$this->setPositionalArg(0, $sequence);
$this->setPositionalArg(1, $this->nativeToDatum($base));
$this->setPositionalArg(2, $this->nativeToFunction($fun));

if (isset($opts)) {
foreach ($opts as $opt => $val) {
$this->setOptionalArg($opt, $this->nativeToDatumOrFunction($val));
}
}
}

protected function getTermType()
{
return TermTermType::PB_FOLD;
}
}
19 changes: 19 additions & 0 deletions rdb/Queries/Control/Args.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace r\Queries\Control;

use r\ValuedQuery\ValuedQuery;
use r\ProtocolBuffer\TermTermType;

class Args extends ValuedQuery
{
public function __construct($args)
{
$this->setPositionalArg(0, $this->nativeToDatum($args));
}

protected function getTermType()
{
return TermTermType::PB_ARGS;
}
}
4 changes: 2 additions & 2 deletions rdb/Queries/Control/Branch.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class Branch extends ValuedQuery
{
public function __construct(Query $test, $trueBranch, $falseBranch)
{
$trueBranch = $this->nativeToDatumOrFunction($trueBranch);
$falseBranch = $this->nativeToDatumOrFunction($falseBranch);
$trueBranch = $this->nativeToDatumOrFunction($trueBranch, false);
$falseBranch = $this->nativeToDatumOrFunction($falseBranch, false);

$this->setPositionalArg(0, $test);
$this->setPositionalArg(1, $trueBranch);
Expand Down
7 changes: 6 additions & 1 deletion rdb/Queries/Transformations/Union.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@

class Union extends ValuedQuery
{
public function __construct(ValuedQuery $sequence, ValuedQuery $otherSequence)
public function __construct(ValuedQuery $sequence, ValuedQuery $otherSequence, $opts = null)
{
$this->setPositionalArg(0, $sequence);
$this->setPositionalArg(1, $otherSequence);
if (isset($opts)) {
foreach ($opts as $opt => $val) {
$this->setOptionalArg($opt, $this->nativeToDatum($val));
}
}
}

protected function getTermType()
Expand Down
2 changes: 1 addition & 1 deletion rdb/Queries/Writing/Insert.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(Table $table, $document, $opts = null)
$this->setPositionalArg(1, $document);
if (isset($opts)) {
foreach ($opts as $opt => $val) {
$this->setOptionalArg($opt, $this->nativeToDatum($val));
$this->setOptionalArg($opt, $this->nativeToDatumOrFunction($val));
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions rdb/ValuedQuery/ValuedQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use r\Queries\Aggregations\Contains;
use r\Queries\Aggregations\Count;
use r\Queries\Aggregations\Distinct;
use r\Queries\Aggregations\Fold;
use r\Queries\Aggregations\Group;
use r\Queries\Aggregations\Max;
use r\Queries\Aggregations\Min;
Expand Down Expand Up @@ -189,9 +190,9 @@ public function isEmpty()
{
return new IsEmpty($this);
}
public function union(ValuedQuery $otherSequence)
public function union(ValuedQuery $otherSequence, $opts = null)
{
return new Union($this, $otherSequence);
return new Union($this, $otherSequence, $opts);
}
public function sample($n)
{
Expand All @@ -201,6 +202,10 @@ public function reduce($reductionFunction)
{
return new Reduce($this, $reductionFunction);
}
public function fold($base, $fun, $opts = null)
{
return new Fold($this, $base, $fun, $opts);
}
public function count($filter = null)
{
return new Count($this, $filter);
Expand Down
12 changes: 12 additions & 0 deletions rdb/global.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use r\Exceptions\RqlDriverError;
use r\Ordering\Asc;
use r\Ordering\Desc;
use r\Queries\Control\Args;
use r\Queries\Control\Branch;
use r\Queries\Control\Error;
use r\Queries\Control\Http;
Expand Down Expand Up @@ -72,6 +73,7 @@
use r\Queries\Tables\TableDrop;
use r\Queries\Tables\TableList;
use r\Queries\Transformations\MapMultiple;
use r\Queries\Transformations\Union;
use r\ValuedQuery\ImplicitVar;
use r\Queries\Control\Js;
use r\ValuedQuery\Json;
Expand Down Expand Up @@ -128,6 +130,11 @@ function rDo($args, $inExpr)
return new RDo($args, $inExpr);
}

function args($args)
{
return new Args($args);
}

function branch(Query $test, $trueBranch, $falseBranch)
{
return new Branch($test, $trueBranch, $falseBranch);
Expand Down Expand Up @@ -439,6 +446,11 @@ function mapMultiple($sequences, $mappingFunction)
return new MapMultiple($sequences[0], array_slice($sequences, 1), $mappingFunction);
}

function union($sequence, $otherSequence, $opts = null)
{
return new Union($sequence, $otherSequence, $opts);
}

function ceil($value)
{
return new Ceil($value);
Expand Down
2 changes: 1 addition & 1 deletion rdb/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

namespace r;

define('PHP_RQL_VERSION', '2.2.0');
define('PHP_RQL_VERSION', '2.3.0');
36 changes: 36 additions & 0 deletions tests/Functional/FoldTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace r\Tests\Functional;

use r\Tests\TestCase;

class FoldTest extends TestCase
{
public function testFoldReduction()
{
$this->assertEquals(
15.0,
\r\expr(array(1, 2, 3, 4))
->fold(5, function ($acc, $v) {
return $acc->add($v);
})
->run($this->conn)
);
}

public function testFoldEmit()
{
$this->assertEquals(
array(5, 6, 8, 11, 15),
\r\expr(array(1, 2, 3, 4))
->fold(5, function ($acc, $v) {
return $acc->add($v);
},
array(
'emit' => function($o, $c, $n) { return array($o); },
'final_emit' => function($a) { return array($a); }
))
->run($this->conn)
);
}
}
35 changes: 35 additions & 0 deletions tests/Functional/InsertTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace r\Tests\Functional;

use r\Tests\TestCase;

class InsertTest extends TestCase
{
public function setUp()
{
$this->conn = $this->getConnection();
$this->data = $this->useDataset('Heroes');
$this->data->populate();
$this->opts = array();
}

public function tearDown()
{
$this->data->truncate();
}

public function testCustomConflict()
{
$res = $this->db()->table('marvel')->insert(
array(
'superhero' => 'Iron Man',
),
array(
'conflict' => function($x, $k, $o) { return \r\expr(null); }
)
)->run($this->conn, $this->opts);

$this->assertObStatus(array('deleted' => 1), $res);
}
}
21 changes: 21 additions & 0 deletions tests/Functional/TransformationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,27 @@ public function testPluckUnion()
);
}

public function testGlobalUnion()
{
$this->assertEquals(
array(1, 3, 2, 4),
\r\union(\r\expr(array(1, 3)), \r\expr(array(2, 4)), array('interleave' => false))
->run($this->conn)
);
}

public function testGlobalUnionInterleave()
{
$this->assertEquals(
array(1, 2, 3, 4),
\r\union(\r\expr(array(array('a' => 1), array('a' => 3))),
\r\expr(array(array('a' => 2), array('a' => 4))),
array('interleave' => 'a'))
->getField('a')
->run($this->conn)
);
}

public function testWithFields()
{
$res = $this->db()->table('marvel')
Expand Down

0 comments on commit 393a49a

Please sign in to comment.