Jq 涉及左侧变量的管道的奇怪行为

发布于 2025-01-09 09:06:30 字数 1386 浏览 1 评论 0原文

我遇到了 jq 的奇怪行为,涉及管道左侧的变量。

供您参考,这个问题的灵感来自 jq 手册:在 Scoping 下 (https://stedolan.github.io/jq/manual/#Advancedfeatures) 其中提到了一个示例过滤器 ... | .*3 为 $times_ Three | [。 + $times_ Three] | ...。我相信正确的版本是 ... | (.*3) 作为 $times_ Three | [。 + $times_ Three] | ...

第一个 (https://jqplay.org/s/ffMPsqmsmt)

filter:
. * 3 as $times_three | .
input:
3
output:
9

第二个 (https://jqplay.org/s/yOFcjRAMLL)

filter:
. * 4 as $times_four | .
input:
3
output:
9

发生了什么 这里?

但是(https://jqplay.org/s/IKrTNZjKI8

filter:
(. * 3) as $times_three | .
input:
3
output:
3

并且(https://jqplay.org/s/8zoq2-HN1G)

filter:
(. * 4) as $times_four | .
input:
3
output:
3

因此,如果括号 (.*3)(.*4) 是在声明变量时使用,然后过滤器的行为可预测。

但如果不使用括号 .*3.*4 ,那么奇怪的是,两者的输出都是 9。

你能解释一下吗?

I came across a weird behavior of jq involving a variable on the left hand side of a pipe.

For your information, this question was inspired by the jq manual: under Scoping (https://stedolan.github.io/jq/manual/#Advancedfeatures) where it mentions an example filter ... | .*3 as $times_three | [. + $times_three] | .... I believe the correct version is ... | (.*3) as $times_three | [. + $times_three] | ....

First (https://jqplay.org/s/ffMPsqmsmt)

filter:
. * 3 as $times_three | .
input:
3
output:
9

Second (https://jqplay.org/s/yOFcjRAMLL)

filter:
. * 4 as $times_four | .
input:
3
output:
9

What is happening here?

But (https://jqplay.org/s/IKrTNZjKI8)

filter:
(. * 3) as $times_three | .
input:
3
output:
3

And (https://jqplay.org/s/8zoq2-HN1G)

filter:
(. * 4) as $times_four | .
input:
3
output:
3

So if parenthesis (.*3) or (.*4) is used when the variable is declared then filter behaves predictably.

But if parenthesis is not used .*3 or .*4 then strangely the output is 9 for both.

Can you explain?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乱了心跳 2025-01-16 09:06:30

范围界定部分中的示例所假设的相反,。 * 4 为 $times_four | 。 相当于。 * ( 4 as $times_four | . ) 因此对其输入进行平方。


您可能期望

. * 4 as $times_four | .

等同于

( . * 4 ) as $times_four | .

正如您所指出的,一些示例甚至表明情况确实如此。然而,第一个片段实际上相当于以下内容:

. * ( 4 as $times_four | . )

相同

. * ( . | . )

并且由于 ... as $x 生成其上下文[1],因此与or

. * .

jq 的运算符优先级不一致和/或古怪。

“定义”| “abc”+“def”| length 表示
"def" | (“abc”+“def”) |长度,但是
"def" | “abc”+“def”为 $x | length 表示
"def" | “abc”+(“def”为 $x | 长度)

这种行为表明 as 并不是人们所期望的 X as $Y 形式的二元运算符,而是 X as 形式的三元运算符$Y | Z。

事实上,它的记录方式是这样的:

变量/符号绑定运算符:... as $identifier | ...

这会带来惊喜,特别是因为它的结合比预期的要紧密得多。看起来范围界定部分中示例的作者都落入了陷阱。


  1. 它可能会多次生成它,例如 .[] as $x

Contrary to what the examples in the Scoping section assume, . * 4 as $times_four | . is equivalent to . * ( 4 as $times_four | . ) and therefore squares its input.


You might expect

. * 4 as $times_four | .

to be equivalent to

( . * 4 ) as $times_four | .

And as you point out, some example even suggest this is the case. However, the first snippet is actually equivalent to the following:

. * ( 4 as $times_four | . )

And since … as $x produces its context[1], that's the same as

. * ( . | . )

or

. * .

jq's operator precedence is inconsistent and/or quirky.

"def" | "abc" + "def" | length means
"def" | ( "abc" + "def" ) | length, but
"def" | "abc" + "def" as $x | length means
"def" | "abc" + ( "def" as $x | length ).

This behaviour suggests that that as isn't a binary operator of the form X as $Y as one might expect, but a ternary operator of the form X as $Y | Z.

And, in fact, this is how it's documented:

Variable / Symbolic Binding Operator: ... as $identifier | ...

This leads to surprises, especially since it binds a lot more tightly than expected. And it looks like whomever authored the examples in the Scoping section fell into the trap.


  1. It might produce it multiple times e.g. .[] as $x.
z祗昰~ 2025-01-16 09:06:30

确实,说明书上好像有错误。在 范围界定 部分中,它对比了(错误的)示例

... | .*3 as $times_three | [. + $times_three] | ...         # faulty!

... | (.*3 as $times_three | [. + $times_three]) | ...       # faulty!

虽然总体声明保持不变有效,两个示例都缺少 .*3 周围的附加括号。因此,它实际上应该分别读取

... | (.*3) as $times_three | [. + $times_three] | ...

... | ((.*3) as $times_three | [. + $times_three]) | ...


从手册下的部分 变量/ 符号绑定运算符

表达式exp as $x | ... 表示:对于表达式 exp 的每个值,使用整个原始输入运行管道的其余部分,并将 $x 设置为该值。因此 as 的功能类似于 foreach 循环。

这意味着变量赋值采用 as 左侧的 one 表达式,并将其计算值分配给 as 右侧定义的变量(这发生为exp 产生输出时多次)。但是,由于 jq 中的所有内容都是过滤器,因此赋值本身也是过滤器,因此它本身需要有一个输出。如果你仔细观察,该部分的完整标题

变量/符号绑定运算符:... as $identifier | ...

旁边还有一个管道符号,表示它属于分配的结构。尝试只运行 。如 $x。您将收到错误,因为 | ... 部分丢失。因此,为了简单地保持输入上下文不变(除了可能将其复制与 as 左侧的表达式产生输出一样多的次数),完整的赋值宁愿看起来像 ... as $ x| .,或者,如果输入上下文是您想要在变量 中捕获的内容。作为 $x | .

也就是说,让我们通过在赋值周围放置显式括号来澄清示例中发生的情况:

3 | . * 3 as $times_three | .
3 | . * (3 as $times_three | .)
3 | . * .                           # with $times_three set to 3
3 * 3                               # with $times_three set to 3
9                                   # with $times_three set to 3
3 | . * 4 as $times_four | .
3 | . * (4 as $times_four | .)
3 | . * .                           # with $times_four set to 4
3 * 3                               # with $times_four set to 4
9                                   # with $times_four set to 4
3 | (. * 3) as $times_three | .
3 | ((. * 3) as $times_three | .)
3 | ((3 * 3) as $times_three | .)
3 | (9 as $times_three | .)
3 | .                               # with $times_three set to 9
3                                   # with $times_three set to 9
3 | (. * 4) as $times_four | .
3 | ((. * 4) as $times_four | .)
3 | ((3 * 4) as $times_four | .)
3 | (12 as $times_four | .)
3 | .                               # with $times_four set to 12
3                                   # with $times_four set to 12

Indeed, there seems to be a mistake in the manual. In section Scoping it is contrasting the (faulty) examples

... | .*3 as $times_three | [. + $times_three] | ...         # faulty!

and

... | (.*3 as $times_three | [. + $times_three]) | ...       # faulty!

While the overall statement stays valid, both examples are missing additional parentheses around .*3. Thus, it should actually read

... | (.*3) as $times_three | [. + $times_three] | ...

and

... | ((.*3) as $times_three | [. + $times_three]) | ...

respectively.


From the manual under section Variable / Symbolic Binding Operator:

The expression exp as $x | ... means: for each value of expression exp, run the rest of the pipeline with the entire original input, and with $x set to that value. Thus as functions as something of a foreach loop.

This means that a variable assignment takes the one expression left of as and assigns its evaluation to the defined variable right of as (and this happens as many times as exp produces an output). But, as everything in jq is a filter, the assignment itself also is, and as such it needs to have an output itself. If you look closely, the full title of that section

Variable / Symbolic Binding Operator: ... as $identifier | ...

also features a pipe symbol next to it, which indicates that it belongs to the assignment's structure. Try just running . as $x. You will get an error because the | ... part is missing. Thus, to simply keep the input context as is (apart from maybe duplicating it as many times as the expression left of as produced an output), a complete assignment would rather look like … as $x | ., or, if the input context is what you wanted to capture in the variable, . as $x | .

That said, let's clarify what happens with your examples by putting explicit parentheses around the assignments:

3 | . * 3 as $times_three | .
3 | . * (3 as $times_three | .)
3 | . * .                           # with $times_three set to 3
3 * 3                               # with $times_three set to 3
9                                   # with $times_three set to 3
3 | . * 4 as $times_four | .
3 | . * (4 as $times_four | .)
3 | . * .                           # with $times_four set to 4
3 * 3                               # with $times_four set to 4
9                                   # with $times_four set to 4
3 | (. * 3) as $times_three | .
3 | ((. * 3) as $times_three | .)
3 | ((3 * 3) as $times_three | .)
3 | (9 as $times_three | .)
3 | .                               # with $times_three set to 9
3                                   # with $times_three set to 9
3 | (. * 4) as $times_four | .
3 | ((. * 4) as $times_four | .)
3 | ((3 * 4) as $times_four | .)
3 | (12 as $times_four | .)
3 | .                               # with $times_four set to 12
3                                   # with $times_four set to 12
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文