Skip to content
Myr

Add SQL-style operators (#1322)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Nicolas Williams · committed by Nicolas Williams · parent 3965431 · Browse files · 4 files+125 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+125 added−0 removed

  • tests:+29 added−0 removed
  • other:+96 added−0 removed

4 files+125 added−0 removed

modified:docs/content/3.manual/manual.yml+40 added
1833 unmodified lines
1834 - program: 'strptime("%Y-%m-%dT%H:%M:%SZ")|mktime'1835 input: '"2015-03-05T23:51:47Z"'1836 output: ['1425599507']18371838 - title: "SQL-Style Operators"1839 body: |18401841 jq provides a few SQL-style operators.18421843 * INDEX(stream; index_expression):18441845 This builtin produces an object whose keys are computed by1846 the given index expression applied to each value from the1847 given stream.18481849 * JOIN($idx; stream; idx_expr; join_expr):18501851 This builtin joins the values from the given stream to the1852 given index. The index's keys are computed by applying the1853 given index expression to each value from the given stream.1854 An array of the value in the stream and the corresponding1855 value from the index is fed to the given join expression to1856 produce each result.18571858 * JOIN($idx; stream; idx_expr):18591860 Same as `JOIN($idx; stream; idx_expr; .)`.18611862 * JOIN($idx; idx_expr):18631864 This builtin joins the input `.` to the given index, applying1865 the given index expression to `.` to compute the index key.1866 The join operation is as described above.18671868 * IN(s):18691870 This builtin outputs `true` if `.` appears in the given1871 stream, otherwise it outputs `false`.18721873 * IN(source; s):18741875 This builtin outputs `true` if any value in the source stream1876 appears in the second stream, otherwise it outputs `false`.18771878 - title: Conditionals and Comparisons1879 entries:1880 - title: "`==`, `!=`"1881 body: |
1120 unmodified lines
modified:src/builtin.jq+17 added
285 unmodified lines
286 ( {}; . + { ($key): ($in[$key] | walk(f)) } ) | f287 elif type == "array" then map( walk(f) ) | f288 else f289 end;290291# SQL-ish operators here:292def INDEX(stream; idx_expr):293 reduce stream as $row ({};294 .[$row|idx_expr|295 if type != "string" then tojson296 else .297 end] |= $row);298def INDEX(idx_expr): INDEX(.[]; idx_expr);299def JOIN($idx; idx_expr):300 [.[] | [., $idx[idx_expr]]];301def JOIN($idx; stream; idx_expr):302 stream | [., $idx[idx_expr]];303def JOIN($idx; stream; idx_expr; join_expr):304 stream | [., $idx[idx_expr]] | join_expr;305def IN(s): reduce (first(select(. == s)) | true) as $v (false; if . or $v then true else false end);306def IN(src; s): reduce (src|IN(s)) as $v (false; if . or $v then true else false end);
modified:tests/jq.test+29 added
1320 unmodified lines
13211322(.[{}] = 0)?1323null13241325INDEX(range(5)|[., "foo\(.)"]; .[0])1326null1327{"0":[0,"foo0"],"1":[1,"foo1"],"2":[2,"foo2"],"3":[3,"foo3"],"4":[4,"foo4"]}13281329JOIN({"0":[0,"abc"],"1":[1,"bcd"],"2":[2,"def"],"3":[3,"efg"],"4":[4,"fgh"]}; .[0]|tostring)1330[[5,"foo"],[3,"bar"],[1,"foobar"]]1331[[[5,"foo"],null],[[3,"bar"],[3,"efg"]],[[1,"foobar"],[1,"bcd"]]]13321333range(5;10)|IN(range(10))1334null1335true1336true1337true1338true1339true13401341range(10;12)|IN(range(10))1342null1343false1344false13451346IN(range(10;20); range(10))1347null1348false13491350IN(range(5;20); range(10))1351null1352true1353
modified:jq.1.prebuilt+39 added
2003 unmodified lines
2004.fi2005.2006.IP "" 02007.2008.SS "SQL\-Style Operators"2009jq provides a few SQL\-style operators\.2010.2011.TP2012INDEX(stream; index_expression):2013.2014.IP2015This builtin produces an object whose keys are computed by the given index expression applied to each value from the given stream\.2016.2017.TP2018JOIN($idx; stream; idx_expr; join_expr):2019.2020.IP2021This builtin joins the values from the given stream to the given index\. The index\'s keys are computed by applying the given index expression to each value from the given stream\. An array of the value in the stream and the corresponding value from the index is fed to the given join expression to produce each result\.2022.2023.TP2024JOIN($idx; stream; idx_expr):2025.2026.IP2027Same as \fBJOIN($idx; stream; idx_expr; \.)\fR\.2028.2029.TP2030JOIN($idx; idx_expr):2031.2032.IP2033This builtin joins the input \fB\.\fR to the given index, applying the given index expression to \fB\.\fR to compute the index key\. The join operation is as described above\.2034.2035.TP2036IN(s):2037.2038.IP2039This builtin outputs \fBtrue\fR if \fB\.\fR appears in the given stream, otherwise it outputs \fBfalse\fR\.2040.2041.TP2042IN(source; s):2043.2044.IP2045This builtin outputs \fBtrue\fR if any value in the source stream appears in the second stream, otherwise it outputs \fBfalse\fR\.2046.2047.SH "CONDITIONALS AND COMPARISONS"2048.2049.SS "==, !="2050The expression \'a == b\' will produce \'true\' if the result of a and b are equal (that is, if they represent equivalent JSON documents) and \'false\' otherwise\. In particular, strings are never considered equal to numbers\. If you\'re coming from Javascript, jq\'s == is like Javascript\'s === \- considering values equal only when they have the same type as well as the same value\.
1163 unmodified lines