Skip to content

Commit

Permalink
Simplify and optimize SQLish builtins
Browse files Browse the repository at this point in the history
  • Loading branch information
muhmuhten committed Feb 26, 2019
1 parent 79ea998 commit 7408605
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
10 changes: 3 additions & 7 deletions src/builtin.jq
Original file line number Diff line number Diff line change
Expand Up @@ -258,17 +258,13 @@ def walk(f):

# SQL-ish operators here:
def INDEX(stream; idx_expr):
reduce stream as $row ({};
.[$row|idx_expr|
if type != "string" then tojson
else .
end] |= $row);
reduce stream as $row ({}; .[$row|idx_expr|tostring] = $row);
def INDEX(idx_expr): INDEX(.[]; idx_expr);
def JOIN($idx; idx_expr):
[.[] | [., $idx[idx_expr]]];
def JOIN($idx; stream; idx_expr):
stream | [., $idx[idx_expr]];
def JOIN($idx; stream; idx_expr; join_expr):
stream | [., $idx[idx_expr]] | join_expr;
def IN(s): reduce (first(select(. == s)) | true) as $v (false; if . or $v then true else false end);
def IN(src; s): reduce (src|IN(s)) as $v (false; if . or $v then true else false end);
def IN(s): any(s == .; .);
def IN(src; s): any(src == s; .);
11 changes: 11 additions & 0 deletions tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -1575,6 +1575,17 @@ true
true
true

range(5;13)|IN(range(0;10;3))
null
false
true
false
false
true
false
false
false

range(10;12)|IN(range(10))
null
false
Expand Down

0 comments on commit 7408605

Please sign in to comment.