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
Provenance
- Commit
7bdf3a3- Tree
dba867b- Author
Nicolas Williams <nico@cryptonector.com>- Committer
Nicolas Williams <nico@cryptonector.com>- Parent
3965431
4 files changed+125 added−0 removed
modified:docs/content/3.manual/manual.yml+40 added
1833 unmodified lines1834 - program: 'strptime("%Y-%m-%dT%H:%M:%SZ")|mktime'1835 input: '"2015-03-05T23:51:47Z"'1836 output: ['1425599507']1837 1838+ - title: "SQL-Style Operators"1839+ body: |1840+1841+ jq provides a few SQL-style operators.1842+1843+ * INDEX(stream; index_expression):1844+1845+ This builtin produces an object whose keys are computed by1846+ the given index expression applied to each value from the1847+ given stream.1848+1849+ * JOIN($idx; stream; idx_expr; join_expr):1850+1851+ 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.1857+1858+ * JOIN($idx; stream; idx_expr):1859+1860+ Same as `JOIN($idx; stream; idx_expr; .)`.1861+1862+ * JOIN($idx; idx_expr):1863+1864+ 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.1867+1868+ * IN(s):1869+1870+ This builtin outputs `true` if `.` appears in the given1871+ stream, otherwise it outputs `false`.1872+1873+ * IN(source; s):1874+1875+ This builtin outputs `true` if any value in the source stream1876+ appears in the second stream, otherwise it outputs `false`.1877+1878 - title: Conditionals and Comparisons1879 entries:1880 - title: "`==`, `!=`"1881 body: |1120 unmodified linesmodified:src/builtin.jq+17 added
285 unmodified lines286 ( {}; . + { ($key): ($in[$key] | walk(f)) } ) | f287 elif type == "array" then map( walk(f) ) | f288 else f289 end;290+291+# SQL-ish operators here:292+def INDEX(stream; idx_expr):293+ reduce stream as $row ({};294+ .[$row|idx_expr|295+ if type != "string" then tojson296+ else .297+ end] |= $row);298+def INDEX(idx_expr): INDEX(.[]; idx_expr);299+def JOIN($idx; idx_expr):300+ [.[] | [., $idx[idx_expr]]];301+def JOIN($idx; stream; idx_expr):302+ stream | [., $idx[idx_expr]];303+def JOIN($idx; stream; idx_expr; join_expr):304+ stream | [., $idx[idx_expr]] | join_expr;305+def IN(s): reduce (first(select(. == s)) | true) as $v (false; if . or $v then true else false end);306+def 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 lines1321 1322 (.[{}] = 0)?1323 null1324 1325+INDEX(range(5)|[., "foo\(.)"]; .[0])1326+null1327+{"0":[0,"foo0"],"1":[1,"foo1"],"2":[2,"foo2"],"3":[3,"foo3"],"4":[4,"foo4"]}1328+1329+JOIN({"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"]]]1332+1333+range(5;10)|IN(range(10))1334+null1335+true1336+true1337+true1338+true1339+true1340+1341+range(10;12)|IN(range(10))1342+null1343+false1344+false1345+1346+IN(range(10;20); range(10))1347+null1348+false1349+1350+IN(range(5;20); range(10))1351+null1352+true1353+modified:jq.1.prebuilt+39 added
2003 unmodified lines2004 .fi2005 .2006 .IP "" 02007 .2008+.SS "SQL\-Style Operators"2009+jq provides a few SQL\-style operators\.2010+.2011+.TP2012+INDEX(stream; index_expression):2013+.2014+.IP2015+This builtin produces an object whose keys are computed by the given index expression applied to each value from the given stream\.2016+.2017+.TP2018+JOIN($idx; stream; idx_expr; join_expr):2019+.2020+.IP2021+This 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+.TP2024+JOIN($idx; stream; idx_expr):2025+.2026+.IP2027+Same as \fBJOIN($idx; stream; idx_expr; \.)\fR\.2028+.2029+.TP2030+JOIN($idx; idx_expr):2031+.2032+.IP2033+This 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+.TP2036+IN(s):2037+.2038+.IP2039+This builtin outputs \fBtrue\fR if \fB\.\fR appears in the given stream, otherwise it outputs \fBfalse\fR\.2040+.2041+.TP2042+IN(source; s):2043+.2044+.IP2045+This 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 "==, !="2050 The 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