From 740860590838eccefe9affdef531d2bcf73bd1d3 Mon Sep 17 00:00:00 2001 From: Muh Muhten Date: Tue, 26 Feb 2019 05:54:58 -0500 Subject: [PATCH] Simplify and optimize SQLish builtins --- src/builtin.jq | 10 +++------- tests/jq.test | 11 +++++++++++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/builtin.jq b/src/builtin.jq index b782e8ad7a..1d394efd0b 100644 --- a/src/builtin.jq +++ b/src/builtin.jq @@ -258,11 +258,7 @@ 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]]]; @@ -270,5 +266,5 @@ 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; .); diff --git a/tests/jq.test b/tests/jq.test index b85f897d93..28b325f997 100644 --- a/tests/jq.test +++ b/tests/jq.test @@ -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