Skip to content

Commit 85f63cc

Browse files
unit tests
1 parent da9f861 commit 85f63cc

25 files changed

+787
-93
lines changed

src/Helper/Esql/Branch.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function __construct()
2626
parent::__construct(null);
2727
}
2828

29-
protected function render_internal(): string
29+
protected function renderInternal(): string
3030
{
3131
return "";
3232
}

src/Helper/Esql/ChangePointCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
class ChangePointCommand extends EsqlBase {
2424
private string $value;
2525
private string $key = "";
26-
private string $type_name = "type";
27-
private string $pvalue_name = "value";
26+
private string $type_name = "";
27+
private string $pvalue_name = "";
2828

2929
public function __construct(EsqlBase $parent, string $value)
3030
{
@@ -60,7 +60,7 @@ public function as(string $type_name, string $pvalue_name): ChangePointCommand
6060
return $this;
6161
}
6262

63-
protected function render_internal(): string
63+
protected function renderInternal(): string
6464
{
6565
$key = $this->key ? " ON " . $this->format_id($this->key) : "";
6666
$names = ($this->type_name && $this->pvalue_name) ?

src/Helper/Esql/CompletionCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
*/
2525
class CompletionCommand extends EsqlBase {
2626
private string $prompt;
27-
private array $named_prompt;
28-
private string $inference_id;
27+
private array $named_prompt = [];
28+
private string $inference_id = "";
2929

3030
public function __construct(EsqlBase $parent, array $prompt)
3131
{
@@ -54,7 +54,7 @@ public function with(string $inference_id): CompletionCommand
5454
return $this;
5555
}
5656

57-
protected function render_internal(): string
57+
protected function renderInternal(): string
5858
{
5959
if (!$this->inference_id) {
6060
throw new RuntimeException("The completion command requires an inference ID");

src/Helper/Esql/DissectCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function append_separator(string $separator): DissectCommand
4444
return $this;
4545
}
4646

47-
protected function render_internal(): string
47+
protected function renderInternal(): string
4848
{
4949
$sep = $this->separator ? " APPEND_SEPARATOR=" . json_encode($this->separator) : "";
5050
return "DISSECT " . $this->format_id($this->input) . " " . json_encode($this->pattern) . $sep;

src/Helper/Esql/DropCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct(EsqlBase $parent, array $columns)
2929
$this->columns = $columns;
3030
}
3131

32-
protected function render_internal(): string
32+
protected function renderInternal(): string
3333
{
3434
return "DROP " . implode(
3535
", ", array_map(array($this, "format_id"), $this->columns)

src/Helper/Esql/EnrichCommand.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
*/
2323
class EnrichCommand extends EsqlBase {
2424
private string $policy;
25-
private string $match_field;
26-
private array $fields;
27-
private array $named_fields;
25+
private string $match_field = "";
26+
private array $fields = [];
27+
private array $named_fields = [];
2828

2929
public function __construct(EsqlBase $parent, string $policy)
3030
{
@@ -72,10 +72,12 @@ public function with(string ...$fields): EnrichCommand
7272
return $this;
7373
}
7474

75-
protected function render_internal(): string
75+
protected function renderInternal(): string
7676
{
77-
$on = $this->match_field ? " ON " . $this->format_id($this->match_field) : "";
78-
if ($this->named_fields) {
77+
$on = ($this->match_field != "") ? " ON " . $this->format_id($this->match_field) : "";
78+
$with = "";
79+
$items = [];
80+
if (sizeof($this->named_fields)) {
7981
$items = array_map(
8082
function(string $key): string {
8183
return $this->format_id($key) . " = " .
@@ -84,9 +86,12 @@ function(string $key): string {
8486
array_keys($this->named_fields)
8587
);
8688
}
87-
else {
89+
else if (sizeof($this->fields)) {
8890
$items = array_map(fn($value): string => $this->format_id($value), $this->fields);
8991
}
90-
return "ENRICH " . $this->policy . implode(", ", $items);
92+
if (sizeof($items)) {
93+
$with = " WITH " . implode(", ", $items);
94+
}
95+
return "ENRICH " . $this->policy . $on . $with;
9196
}
9297
}

src/Helper/Esql/EsqlBase.php

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,31 @@
1919
abstract class EsqlBase {
2020
private ?EsqlBase $parent = null;
2121

22+
protected function format_id(string $id, bool $allow_patterns = false): string
23+
{
24+
if ($allow_patterns && str_contains($id, "*")) {
25+
// patterns cannot be escaped
26+
return $id;
27+
}
28+
if (preg_match("/[A-Za-z_@][A-Za-z0-9_\\.]*/", $id)) {
29+
// all safe characters, so no need to escape
30+
return $id;
31+
}
32+
// apply escaping
33+
return "`" . str_replace("`", "``", $id) . "`";
34+
}
35+
2236
protected function format_kv(array $map): string
2337
{
2438
return implode(", ", array_map(
2539
function($k, $v) {
26-
return $k . "=" . json_encode($v);
40+
return $k . " = " . json_encode($v);
2741
},
2842
array_keys($map),
2943
$map,
3044
));
3145
}
3246

33-
public function render(): string
34-
{
35-
$query = "";
36-
if ($this->parent) {
37-
$query .= $this->parent->render() . "\n| ";
38-
}
39-
$query .= $this->render_internal();
40-
return $query;
41-
}
42-
43-
protected abstract function render_internal(): string;
44-
45-
public function __construct(?EsqlBase $parent)
46-
{
47-
$this->parent = $parent;
48-
}
49-
50-
public function __toString(): string
51-
{
52-
return $this->render() . "\n";
53-
}
54-
5547
protected function is_named_argument_list(array $args): bool {
5648
$named_count = array_sum(array_map('is_string', array_keys($args)));
5749
if ($named_count == sizeof($args)) {
@@ -63,20 +55,6 @@ protected function is_named_argument_list(array $args): bool {
6355
return false;
6456
}
6557

66-
protected function format_id(string $id, bool $allow_patterns = false): string
67-
{
68-
if ($allow_patterns && str_contains($id, "*")) {
69-
// patterns cannot be escaped
70-
return $id;
71-
}
72-
if (preg_match("/[A-Za-z_@][A-Za-z0-9_\\.]*/", $id)) {
73-
// all safe characters, so no need to escape
74-
return $id;
75-
}
76-
// apply escaping
77-
return "`" . str_replace("`", "``", $id) . "`";
78-
}
79-
8058
protected function is_forked(): bool
8159
{
8260
if (get_class($this) == "ForkCommand") {
@@ -88,6 +66,28 @@ protected function is_forked(): bool
8866
return false;
8967
}
9068

69+
public function __construct(?EsqlBase $parent)
70+
{
71+
$this->parent = $parent;
72+
}
73+
74+
public function render(): string
75+
{
76+
$query = "";
77+
if ($this->parent) {
78+
$query .= $this->parent->render() . "\n| ";
79+
}
80+
$query .= $this->renderInternal();
81+
return $query;
82+
}
83+
84+
protected abstract function renderInternal(): string;
85+
86+
public function __toString(): string
87+
{
88+
return $this->render() . "\n";
89+
}
90+
9191
/**
9292
* `CHANGE_POINT` detects spikes, dips, and change points in a metric.
9393
*
@@ -105,7 +105,7 @@ protected function is_forked(): bool
105105
* .where("type IS NOT NULL")
106106
* )
107107
*/
108-
public function change_point(string $value): ChangePointCommand
108+
public function changePoint(string $value): ChangePointCommand
109109
{
110110
return new ChangePointCommand($this, $value);
111111
}
@@ -394,7 +394,7 @@ public function limit(int $max_number_of_rows): LimitCommand
394394
* .lookup_join("languages_lookup").on("language_code")
395395
* )
396396
*/
397-
public function lookup_join(string $lookup_index): LookupJoinCommand
397+
public function lookupJoin(string $lookup_index): LookupJoinCommand
398398
{
399399
return new LookupJoinCommand($this, $lookup_index);
400400
}
@@ -409,7 +409,7 @@ public function lookup_join(string $lookup_index): LookupJoinCommand
409409
*
410410
* query = ESQL.row(a=[1, 2, 3], b="b", j=["a", "b"]).mv_expand("a")
411411
*/
412-
public function mv_expand(string $column): MvExpandCommand
412+
public function mvExpand(string $column): MvExpandCommand
413413
{
414414
return new MvExpandCommand($this, $column);
415415
}

src/Helper/Esql/EvalCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
* that belong to an ES|QL query in a single expression.
2222
*/
2323
class EvalCommand extends EsqlBase {
24-
private array $columns;
25-
private array $named_columns;
24+
private array $columns = [];
25+
private array $named_columns = [];
2626

2727
public function __construct(EsqlBase $parent, array $columns)
2828
{
@@ -35,9 +35,9 @@ public function __construct(EsqlBase $parent, array $columns)
3535
}
3636
}
3737

38-
protected function render_internal(): string
38+
protected function renderInternal(): string
3939
{
40-
if ($this->named_columns) {
40+
if (sizeof($this->named_columns)) {
4141
$items = array_map(
4242
function(string $key): string {
4343
return $this->format_id($key) . " = " .

src/Helper/Esql/ForkCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function __construct(
3939
$this->branches = [$fork1, $fork2, $fork3, $fork4, $fork5, $fork6, $fork7, $fork8];
4040
}
4141

42-
protected function render_internal(): string
42+
protected function renderInternal(): string
4343
{
4444
$cmds = "";
4545
foreach ($this->branches as $branch) {
@@ -49,7 +49,7 @@ protected function render_internal(): string
4949
$cmds = "( " . $cmd . " )";
5050
}
5151
else {
52-
$cmds += "\n ( " . $cmd . " )";
52+
$cmds .= "\n ( " . $cmd . " )";
5353
}
5454
}
5555
}

src/Helper/Esql/FromCommand.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,33 @@
1616

1717
class FromCommand extends EsqlBase {
1818
private array $indices;
19+
private array $metadata_fields = [];
1920

2021
public function __construct(array $indices)
2122
{
2223
$this->indices = $indices;
2324
}
2425

25-
protected function render_internal(): string
26+
/**
27+
* Continuation of the `FROM` source command.
28+
*
29+
* *param string ...$metadata_fields Metadata fields to retrieve, given as
30+
* positional arguments.
31+
*/
32+
public function metadata(string ...$metadata_fields): FromCommand
2633
{
27-
return "FROM " . implode(", ", $this->indices);
34+
$this->metadata_fields = $metadata_fields;
35+
return $this;
36+
}
37+
38+
protected function renderInternal(): string
39+
{
40+
$s = "FROM " . implode(", ", $this->indices);
41+
if (sizeof($this->metadata_fields)) {
42+
$s .= " METADATA " . implode(
43+
", ", array_map(array($this, "format_id"), $this->metadata_fields)
44+
);
45+
}
46+
return $s;
2847
}
2948
}

0 commit comments

Comments
 (0)