# Is there a documented meaning for the following Query syntax?

Consider this example:

a = <|"x" -> 1, "y" -> 2|>;

a // Query[Association["q" -> (# &)]]
(* <|"q" -> <|"x" -> 1, "y" -> 2|>|> *)

It returns what I expect according to the documentation (see Dataset, under Details):

Now consider the following:

a // Query[Association[("q" -> # &)]]
(* <|"q" -> <|"x" -> 1, "y" -> 2|>|> *)

It returns the same thing. But what does it actually do? Is this documented syntax?

To phrase it another way: Should I treat this as valid usage or as undefined behaviour?

I typed something similar to this by accident. If I saw this code without the possibility to run it, I would say that it is nonsense and it should ideally trigger an error.

Here are the first two things that seemed reasonable to try:

Clear[fun1]
a // Query[Association[fun1]]
(* Association[(GeneralUtilitiesSafeConstruct[#1, <|"x" -> 1, "y" -> 2|>] &)[DatasetCompileSubquery[fun1]]] *)

fun2[val_] := "key" -> val
a // Query[Association[fun2]]
(* <|"key" -> <|"x" -> 1, "y" -> 2|>|> *)

One triggers a bug (??) the other works just like my first example.

• I probably fail to grasp the full the full extend of the problem, but I would interpret a\\Query[Association[("q" -> # &)]] as Query[Association[Function[{x}, "q" -> x]]][a] (this makes it easier for me to read). That form (as far as I can see) is documented as Query[operator1,...][expr] in the help section under details of Query. I would interpret Function as the operator here. Which would be indentical to your last definition (fun2).
– Max1
Apr 30 '20 at 17:01
• Mhm sorry seems like I was mixing Association[Function[...]] with Function[Association[...]]. Trace output is also insanely long.
– Max1
Apr 30 '20 at 17:07
• @Max1 Yes, what's written there is not a function, f, but a function wrapped in Association, Association[f]. Having Association here is important. Other heads don't behave this way. Also, while this thing has head Association, it is not an association (AssociationQ). It may become one after the contents are evaluated. If it does, it behaves just just the <|"key" -> operator|> form. If not, it returns some internal ugliness. Apr 30 '20 at 17:17
• Yes I absolutely overlooked that. One thing which looked interesting to me is the very last part of the Trace of Query[Association[Function[{x}, "q" -> x]]][a]. One of the very last expressions is (GeneralUtilitiesSafeConstruct[#1, <|"x" -> 1, "y" ->2|>] &) /@ Association[DatasetCompileSubquery[Function[{x}, "q" -> x]]]. Since I'm not a mathematica wizard that looks like #[<|"x" -> 1, "y" -> 2|>] & /@ Association[Function[{x}, "q" -> x]] to me with some internal mumbo jumbo. I realize tho that your main point is if this is documented or some kind of undefined behaviour.
– Max1
Apr 30 '20 at 17:48
• I think this is something that works accidentally.
– jose
May 9 '20 at 1:52