Skip to content

Commit 76e72a3

Browse files
muhmuhtennicowilliams
authored andcommitted
Simplify and optimize SQLish builtins
1 parent a1626ad commit 76e72a3

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

src/builtin.jq

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,17 +263,13 @@ def walk(f):
263263

264264
# SQL-ish operators here:
265265
def INDEX(stream; idx_expr):
266-
reduce stream as $row ({};
267-
.[$row|idx_expr|
268-
if type != "string" then tojson
269-
else .
270-
end] |= $row);
266+
reduce stream as $row ({}; .[$row|idx_expr|tostring] = $row);
271267
def INDEX(idx_expr): INDEX(.[]; idx_expr);
272268
def JOIN($idx; idx_expr):
273269
[.[] | [., $idx[idx_expr]]];
274270
def JOIN($idx; stream; idx_expr):
275271
stream | [., $idx[idx_expr]];
276272
def JOIN($idx; stream; idx_expr; join_expr):
277273
stream | [., $idx[idx_expr]] | join_expr;
278-
def IN(s): reduce (first(select(. == s)) | true) as $v (false; if . or $v then true else false end);
279-
def IN(src; s): reduce (src|IN(s)) as $v (false; if . or $v then true else false end);
274+
def IN(s): any(s == .; .);
275+
def IN(src; s): any(src == s; .);

tests/jq.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,6 +1575,17 @@ true
15751575
true
15761576
true
15771577

1578+
range(5;13)|IN(range(0;10;3))
1579+
null
1580+
false
1581+
true
1582+
false
1583+
false
1584+
true
1585+
false
1586+
false
1587+
false
1588+
15781589
range(10;12)|IN(range(10))
15791590
null
15801591
false

0 commit comments

Comments
 (0)