Consider the following dataset.

titanic = ExampleData[{"Dataset", "Titanic"}]

If I use the lookup syntax of the dataset like following it creates an equivalent Query syntax which is compiled and then applied to the dataset.

titanic[Select[#survived == True && #age > 60 &], {"class", "sex"}] // InputForm

If you look at the input syntax above you can see the TypeSystem being used for storage and also the Query used to arrive at the current dataset from the previous one.

I want to extract the Query being used. I tried the following with no success.

query=First@Cases[%, x_Query :> x, \[Infinity], Heads -> True]

Once the Query is extracted I can just use Normal[query] to get the optimized version.

Can someone help me get out the Query from the Dataset. I know it is trivial in this case but I want to use this as a base to work with more complex queries and have a way to track previous datasets using their "ID"s and connecting Query like a Graph with ID as Vertices and Query as Edges in a workflow diagram.


There is a probably an internal function that can extract this information, but if not, you could use the following function to do so:

extractDatasetInformation[ds_Dataset] := Block[{Dataset = Inactive[Dataset]},

Your example:

q = titanic[Select[#survived == True && #age > 60 &], {"class", "sex"}];


<|"Origin" -> HoldComplete[ Query[Select[#survived == True && #age > 60 &], {"class", "sex"}][ Dataset`DatasetHandle[263616672941144]]], "ID" -> 278717777953890|>

HoldComplete[ Query[Select[#survived == True && #age > 60 &], {"class", "sex"}][ Dataset`DatasetHandle[263616672941144]]]

  • $\begingroup$ Thank you. Can you please explain why did you compress the inactive Dataset expression and uncompress it before extracting the part? $\endgroup$ – user13892 May 25 at 18:18

We can extract the query from the dataset object through pattern-matching:

datasetQuery[HoldPattern[Dataset[_,_,KeyValuePattern["Origin"->HoldComplete[query_[___]]]]]] := query


titanic = ExampleData[{"Dataset", "Titanic"}];

ds = titanic[Select[#survived == True && #age > 60 &], {"class", "sex"}];


(* Query[Select[#survived == True && #age > 60 &], {"class", "sex"}] *)

Beware that this relies upon undocumented internal structure for Dataset objects which may change between releases. The exhibited pattern is current for Version 12.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.