Skip to content

Commit

Permalink
Break many docs examples across multiple lines (#5248)
Browse files Browse the repository at this point in the history
  • Loading branch information
philrz authored Aug 28, 2024
1 parent 598911c commit b6d853f
Show file tree
Hide file tree
Showing 53 changed files with 449 additions and 157 deletions.
53 changes: 28 additions & 25 deletions docs/commands/zq.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,8 @@ the previously-printed header line, a new header line will be output.
For example:

```mdtest-command
echo '{word:"one",digit: 1} {word:"hello",style:"greeting"}' | zq -f table -
echo '{word:"one",digit: 1} {word:"hello",style:"greeting"}' |
zq -f table -
```
produces
```mdtest-output
Expand All @@ -421,7 +422,8 @@ may prove useful to unify the input stream under a single record type that can
be described with a single header line. Doing this to our last example, we find

```mdtest-command
echo '{word:"one",digit:1} {word:"hello",style:"greeting"}' | zq -f table 'fuse' -
echo '{word:"one",digit:1} {word:"hello",style:"greeting"}' |
zq -f table 'fuse' -
```
now produces
```mdtest-output
Expand Down Expand Up @@ -517,22 +519,22 @@ alternative to the traditional technique of looking through logs for errors
or trying to debug a halted program with a vague error message.

For example, this query
```
echo '1 2 0 3' | zq '10.0/this' -
```mdtest-command
echo '1 2 0 3' | zq -z '10.0/this' -
```
produces
```
```mdtest-output
10.
5.
error("divide by zero")
3.3333333333333335
```
and
```
echo '1 2 0 3' | zq '10.0/this' - | zq 'is_error(this)' -
```mdtest-command
echo '1 2 0 3' | zq '10.0/this' - | zq -z 'is_error(this)' -
```
produces just
```
```mdtest-output
error("divide by zero")
```

Expand All @@ -550,70 +552,71 @@ The language documentation and [tutorials directory](../tutorials/README.md)
have many examples, but here are a few more simple `zq` use cases.

_Hello, world_
```
```mdtest-command
echo '"hello, world"' | zq -z 'yield this' -
```
produces this ZSON output
```
```mdtest-output
"hello, world"
```

_Some values of available [data types](../language/data-types.md)_
```
```mdtest-command
echo '1 1.5 [1,"foo"] |["apple","banana"]|' | zq -z 'yield this' -
```
produces
```
```mdtest-output
1
1.5
[1,"foo"]
|["apple","banana"]|
```
_The types of various data_
```
```mdtest-command
echo '1 1.5 [1,"foo"] |["apple","banana"]|' | zq -z 'yield typeof(this)' -
```
produces
```
```mdtest-output
<int64>
<float64>
<[(int64,string)]>
<|[string]|>
```
_A simple [aggregation](../language/aggregates/README.md)_
```
echo '{key:"foo",val:1}{key:"bar",val:2}{key:"foo",val:3}' | zq -z 'sum(val) by key | sort key' -
```mdtest-command
echo '{key:"foo",val:1}{key:"bar",val:2}{key:"foo",val:3}' |
zq -z 'sum(val) by key | sort key' -
```
produces
```
```mdtest-output
{key:"bar",sum:2}
{key:"foo",sum:4}
```
_Convert CSV to Zed and [cast](../language/functions/cast.md) a to an integer from default float_
```
printf "a,b\n1,foo\n2,bar\n" | zq 'a:=int64(a)' -
```mdtest-command
printf "a,b\n1,foo\n2,bar\n" | zq -z 'a:=int64(a)' -
```
produces
```
```mdtest-output
{a:1,b:"foo"}
{a:2,b:"bar"}
```
_Convert JSON to Zed and cast to an integer from default float_
```
echo '{"a":1,"b":"foo"}{"a":2,"b":"bar"}' | zq 'a:=int64(a)' -
```mdtest-command
echo '{"a":1,"b":"foo"}{"a":2,"b":"bar"}' | zq -z 'a:=int64(a)' -
```
produces
```
```mdtest-output
{a:1,b:"foo"}
{a:2,b:"bar"}
```
_Make a schema-rigid Parquet file using fuse and turn it back into Zed_
```
```mdtest-command
echo '{a:1}{a:2}{b:3}' | zq -f parquet -o tmp.parquet fuse -
zq -z tmp.parquet
```
produces
```
```mdtest-output
{a:1,b:null(int64)}
{a:2,b:null(int64)}
{a:null(int64),b:3}
Expand Down
3 changes: 2 additions & 1 deletion docs/language/aggregates/and.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ false

AND of values grouped by key:
```mdtest-command
echo '{a:true,k:1} {a:true,k:1} {a:true,k:2} {a:false,k:2}' | zq -z 'and(a) by k | sort' -
echo '{a:true,k:1} {a:true,k:1} {a:true,k:2} {a:false,k:2}' |
zq -z 'and(a) by k | sort' -
```
=>
```mdtest-output
Expand Down
3 changes: 2 additions & 1 deletion docs/language/aggregates/any.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ echo '"foo" 1 2 3 ' | zq -z 'any(this)' -

Pick from groups bucketed by key:
```mdtest-command
echo '{a:1,k:1} {a:2,k:1} {a:3,k:2} {a:4,k:2}' | zq -z 'any(a) by k | sort' -
echo '{a:1,k:1} {a:2,k:1} {a:3,k:2} {a:4,k:2}' |
zq -z 'any(a) by k | sort' -
```
=>
```mdtest-output
Expand Down
3 changes: 2 additions & 1 deletion docs/language/aggregates/avg.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ echo '1 2 3 4 "foo"' | zq -z 'avg(this)' -

Average of values bucketed by key:
```mdtest-command
echo '{a:1,k:1} {a:2,k:1} {a:3,k:2} {a:4,k:2}' | zq -z 'avg(a) by k | sort' -
echo '{a:1,k:1} {a:2,k:1} {a:3,k:2} {a:4,k:2}' |
zq -z 'avg(a) by k | sort' -
```
=>
```mdtest-output
Expand Down
3 changes: 2 additions & 1 deletion docs/language/aggregates/collect.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ echo '1 2 3 4 "foo"' | zq -z 'collect(this)' -

Create arrays of values bucketed by key:
```mdtest-command
echo '{a:1,k:1} {a:2,k:1} {a:3,k:2} {a:4,k:2}' | zq -z 'collect(a) by k | sort' -
echo '{a:1,k:1} {a:2,k:1} {a:3,k:2} {a:4,k:2}' |
zq -z 'collect(a) by k | sort' -
```
=>
```mdtest-output
Expand Down
12 changes: 9 additions & 3 deletions docs/language/aggregates/collect_map.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ of union of those types.

Combine a sequence of records into a map:
```mdtest-command
echo '{stock:"APPL",price:145.03} {stock:"GOOG",price:87.07}' | zq -z 'collect_map(|{stock:price}|)' -
echo '{stock:"APPL",price:145.03} {stock:"GOOG",price:87.07}' |
zq -z 'collect_map(|{stock:price}|)' -
```
=>
```mdtest-output
Expand All @@ -27,7 +28,8 @@ echo '{stock:"APPL",price:145.03} {stock:"GOOG",price:87.07}' | zq -z 'collect_m

Continuous collection over a simple sequence:
```mdtest-command
echo '|{"APPL":145.03}| |{"GOOG":87.07}| |{"APPL":150.13}|' | zq -z 'yield collect_map(this)' -
echo '|{"APPL":145.03}| |{"GOOG":87.07}| |{"APPL":150.13}|' |
zq -z 'yield collect_map(this)' -
```
=>
```mdtest-output
Expand All @@ -38,7 +40,11 @@ echo '|{"APPL":145.03}| |{"GOOG":87.07}| |{"APPL":150.13}|' | zq -z 'yield colle

Create maps by key:
```mdtest-command
echo '{stock:"APPL",price:145.03,day:0} {stock:"GOOG",price:87.07,day:0} {stock:"APPL",price:150.13,day:1} {stock:"GOOG",price:89.15,day:1}' | zq -z 'collect_map(|{stock:price}|) by day | sort' -
echo '{stock:"APPL",price:145.03,day:0}
{stock:"GOOG",price:87.07,day:0}
{stock:"APPL",price:150.13,day:1}
{stock:"GOOG",price:89.15,day:1}' |
zq -z 'collect_map(|{stock:price}|) by day | sort' -
```
=>
```mdtest-output
Expand Down
3 changes: 2 additions & 1 deletion docs/language/aggregates/fuse.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ echo '{a:1,b:2}{a:2,b:"foo"}' | zq -z 'fuse(this)' -
```
Fuse records with a group-by key:
```mdtest-command
echo '{a:1,b:"bar"}{a:2.1,b:"foo"}{a:3,b:"bar"}' | zq -z 'fuse(this) by b | sort' -
echo '{a:1,b:"bar"}{a:2.1,b:"foo"}{a:3,b:"bar"}' |
zq -z 'fuse(this) by b | sort' -
```
=>
```mdtest-output
Expand Down
3 changes: 2 additions & 1 deletion docs/language/aggregates/max.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ echo '1 2 3 4 "foo"' | zq -z 'max(this)' -

Maximum value within buckets grouped by key:
```mdtest-command
echo '{a:1,k:1} {a:2,k:1} {a:3,k:2} {a:4,k:2}' | zq -z 'max(a) by k | sort' -
echo '{a:1,k:1} {a:2,k:1} {a:3,k:2} {a:4,k:2}' |
zq -z 'max(a) by k | sort' -
```
=>
```mdtest-output
Expand Down
3 changes: 2 additions & 1 deletion docs/language/aggregates/min.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ echo '1 2 3 4 "foo"' | zq -z 'min(this)' -

Minimum value within buckets grouped by key:
```mdtest-command
echo '{a:1,k:1} {a:2,k:1} {a:3,k:2} {a:4,k:2}' | zq -z 'min(a) by k | sort' -
echo '{a:1,k:1} {a:2,k:1} {a:3,k:2} {a:4,k:2}' |
zq -z 'min(a) by k | sort' -
```
=>
```mdtest-output
Expand Down
3 changes: 2 additions & 1 deletion docs/language/aggregates/or.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ true

OR of values grouped by key:
```mdtest-command
echo '{a:true,k:1} {a:false,k:1} {a:false,k:2} {a:false,k:2}' | zq -z 'or(a) by k | sort' -
echo '{a:true,k:1} {a:false,k:1} {a:false,k:2} {a:false,k:2}' |
zq -z 'or(a) by k | sort' -
```
=>
```mdtest-output
Expand Down
3 changes: 2 additions & 1 deletion docs/language/aggregates/sum.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ echo '1 2 3 4 "foo"' | zq -z 'sum(this)' -

Sum of values bucketed by key:
```mdtest-command
echo '{a:1,k:1} {a:2,k:1} {a:3,k:2} {a:4,k:2}' | zq -z 'sum(a) by k | sort' -
echo '{a:1,k:1} {a:2,k:1} {a:3,k:2} {a:4,k:2}' |
zq -z 'sum(a) by k | sort' -
```
=>
```mdtest-output
Expand Down
7 changes: 4 additions & 3 deletions docs/language/aggregates/union.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,19 @@ echo '1 2 3 3' | zq -z 'yield union(this)' -
```

Mixed types create a union type for the set elements:
```mdtest-command-issue-3610
```mdtest-command
echo '1 2 3 "foo"' | zq -z 'set:=union(this) | yield this,typeof(set)' -
```
=>
```mdtest-output-issue-3610
```mdtest-output
{set:|[1,2,3,"foo"]|}
<|[(int64,string)]|>
```

Create sets of values bucketed by key:
```mdtest-command
echo '{a:1,k:1} {a:2,k:1} {a:3,k:2} {a:4,k:2}' | zq -z 'union(a) by k | sort' -
echo '{a:1,k:1} {a:2,k:1} {a:3,k:2} {a:4,k:2}' |
zq -z 'union(a) by k | sort' -
```
=>
```mdtest-output
Expand Down
18 changes: 14 additions & 4 deletions docs/language/data-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ search ... | count() by typeof(this)
```
For example,
```mdtest-command
echo '1 2 "foo" 10.0.0.1 <string>' | zq -z 'count() by typeof(this) | sort this' -
echo '1 2 "foo" 10.0.0.1 <string>' |
zq -z 'count() by typeof(this) | sort this' -
```
produces
```mdtest-output
Expand Down Expand Up @@ -142,7 +143,8 @@ Each value that references a named type retains its local definition of the
named type retaining the proper type binding while accommodating changes in a
particular named type. For example,
```mdtest-command
echo '1(=foo) 2(=bar) "hello"(=foo) 3(=foo)' | zq -z 'count() by typeof(this) | sort this' -
echo '1(=foo) 2(=bar) "hello"(=foo) 3(=foo)' |
zq -z 'count() by typeof(this) | sort this' -
```
results in
```mdtest-output
Expand Down Expand Up @@ -176,12 +178,20 @@ LIMIT 5
```
In Zed, you would say
```
from anywhere | typeof(this)==<employee> | cut last,salary | sort salary | head 5
from anywhere
| typeof(this)==<employee>
| cut last,salary
| sort salary
| head 5
```
and since type comparisons are so useful and common, the [`is` function](functions/is.md)
can be used to perform the type match:
```
from anywhere | is(<employee>) | cut last,salary | sort salary | head 5
from anywhere
| is(<employee>)
| cut last,salary
| sort salary
| head 5
```
The power of Zed is that you can interpret data on the fly as belonging to
a certain schema, in this case "employee", and those records can be intermixed
Expand Down
3 changes: 2 additions & 1 deletion docs/language/dataflow-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ but as an example, you might use the `get` form of `from` to fetch data from an
HTTP endpoint and process it with Zed, in this case, to extract the description
and license of a GitHub repository:
```
zq -f text "get https://api.github.com/repos/brimdata/zed | yield description,license.name"
zq -f text 'get https://api.github.com/repos/brimdata/zed
| yield description,license.name'
```
When a Zed query is run on the command-line with `zq`, the `from` source is
typically omitted and implied instead by the command-line file arguments.
Expand Down
18 changes: 12 additions & 6 deletions docs/language/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ produces
```
You can also use this operator with a static array:
```mdtest-command
echo '{accounts:[{id:1},{id:2},{id:3}]}' | zq -z 'over accounts | where id in [1,2]' -
echo '{accounts:[{id:1},{id:2},{id:3}]}' |
zq -z 'over accounts | where id in [1,2]' -
```
produces
```mdtest-output
Expand Down Expand Up @@ -200,7 +201,8 @@ will be evaluated.

For example,
```mdtest-command
echo '"foo" "bar" "foo"' | zq -z 'yield this=="foo" ? {foocount:count()} : {barcount:count()}' -
echo '"foo" "bar" "foo"' |
zq -z 'yield this=="foo" ? {foocount:count()} : {barcount:count()}' -
```
produces
```mdtest-output
Expand Down Expand Up @@ -283,7 +285,8 @@ a string, it is implicitly cast to a string.

For example,
```mdtest-command
echo '{numerator:22.0, denominator:7.0}' | zq -z 'yield f"pi is approximately {numerator / denominator}"' -
echo '{numerator:22.0, denominator:7.0}' |
zq -z 'yield f"pi is approximately {numerator / denominator}"' -
```
produces
```mdtest-output
Expand All @@ -301,7 +304,8 @@ F-strings may be nested, where a child `<expr>` may contain f-strings.

For example,
```mdtest-command
echo '{foo:"hello", bar:"world", HELLOWORLD:"hi!"}' | zq -z 'yield f"oh {this[upper(f"{foo + bar}")]}"' -
echo '{foo:"hello", bar:"world", HELLOWORLD:"hi!"}' |
zq -z 'yield f"oh {this[upper(f"{foo + bar}")]}"' -
```
produces
```mdtest-output
Expand Down Expand Up @@ -347,7 +351,8 @@ field's value.

For example,
```mdtest-command
echo '{x:1,y:2,r:{a:1,b:2}}' | zq -z 'yield {a:0},{x}, {...r}, {a:0,...r,b:3}' -
echo '{x:1,y:2,r:{a:1,b:2}}' |
zq -z 'yield {a:0},{x}, {...r}, {a:0,...r,b:3}' -
```
produces
```mdtest-output
Expand Down Expand Up @@ -563,7 +568,8 @@ produces
```
and
```mdtest-command
echo '{ts:"1/1/2022",r:{x:"1",y:"2"}} {ts:"1/2/2022",r:{x:3,y:4}}' | zq -z 'cast(this,<{ts:time,r:{x:float64,y:float64}}>)' -
echo '{ts:"1/1/2022",r:{x:"1",y:"2"}} {ts:"1/2/2022",r:{x:3,y:4}}' |
zq -z 'cast(this,<{ts:time,r:{x:float64,y:float64}}>)' -
```
produces
```mdtest-output
Expand Down
Loading

0 comments on commit b6d853f

Please sign in to comment.