Function Repository Resource:

QueryTreeForm

Source Notebook

Present a query as a tree

Contributed by: Seth J. Chandler

ResourceFunction["QueryTreeForm"][q]

presents a Query q as a tree in which the order of operations is the depth-first descent of the tree.

ResourceFunction["QueryTreeForm"][{op1,op2,,opn}]

presents a list of operations for Query as a tree with depth-first descent.

Details and Options

ResourceFunction["QueryTreeForm"] takes the same options as TreeForm.

Examples

Basic Examples (2) 

Show the order of operations of a Query that is constructed of an ascending operator f and an ascending operator g as its arguments:

In[1]:=
ResourceFunction["QueryTreeForm"][Query[f, g]]
Out[48]=

Show the order of operations of a Query that is constructed of: (1) a right composition of two descending operators and an ascending operator; (2) the All operator; and (3) the ascending operator i:

In[49]:=
ResourceFunction["QueryTreeForm"][{Select[f]/*GroupBy[g]/*h, All, i}]
Out[97]=

Scope (3) 

Visualize a Query composed of an ascending and a descending operator to aggregate values of a column after filtering the rows:

In[98]:=
ResourceFunction["QueryTreeForm"][
 Query[Select[#b == "z" &]/*Total, "a"]]
Out[173]=

Visualize the order of operations of a Query that applies functions to each column independently:

In[174]:=
ResourceFunction["QueryTreeForm"][
 Query[All, {"a" -> f, "b" -> g, "c" -> h}]]
Out[250]=

Visualize a Query that constructs a new table by specifying operators that will compute each column:

In[251]:=
ResourceFunction[
 "QueryTreeForm"][{All, Key["c"]/*<|"ctotal" -> Total, "clength" -> Length|>}, ImageSize -> 450]
Out[331]=

Options (1) 

All the options of TreeForm are available to this function:

In[332]:=
ResourceFunction["QueryTreeForm"][{f, g}, ImageSize -> 500, VertexLabeling -> False, EdgeRenderingFunction -> ({RGBColor[
     0.7843137254901961, 0.06274509803921569, 0.1803921568627451], Thickness[0.05], Arrow[#1, 0.1]} &), VertexRenderingFunction -> ({RGBColor[
     1, 0.9764705882352941, 0.8509803921568627], EdgeForm[
      Directive[{RGBColor[0, 0.7019607843137254, 0.5333333333333333], Thickness[0.02]}]], Disk[#, 0.3], RGBColor[
     0.5333333333333333, 0.5450980392156862, 0.5529411764705883], Text[Style[#2, FontFamily -> "Damascus"], #1]} &), Background -> RGBColor[
Rational[82, 85], 0.7450980392156863, 0]]
Out[469]=

Applications (1) 

QueryTreeForm is useful in thinking through the order of operations in complex queries:

In[470]:=
With[{ops = {SelectFirst[f]/*KeySelect[g], DeleteDuplicatesBy[g], All,
     Counts}}, ResourceFunction["QueryTreeForm"][ops, ImageSize -> 580, PlotLabel -> Normal[Query @@ ops]]]
Out[556]=

Properties and Relations (1) 

QueryTreeForm can be combined with other visualization functions to present additional information:

In[557]:=
With[{ops = {SelectFirst[f]/*KeySelect[g], DeleteDuplicatesBy[g], All,
     Counts}}, Framed[Labeled[
   ResourceFunction["QueryTreeForm"][ops, ImageSize -> 400, VertexRenderingFunction -> (Text[
        Framed[Style[#2, 11], Background -> RGBColor[
          0.6862745098039216, 0.7215686274509804, 0.8117647058823529]], #1] &)], {Query @@ ops, Normal[Query @@ ops]}, {Top, Bottom}]
  ]]
Out[557]=

Possible Issues (1) 

Any ResourceFunction that does not format well within TreeForm does not format well in QueryTreeForm either:

In[558]:=
ResourceFunction["QueryTreeForm"][
 Query[GroupBy[#class &], ResourceFunction[
ResourceObject[<|{"Name" -> "ItemCounts", "ShortName" -> "ItemCounts",
        "UUID" -> "6f0e7df7-5f7c-4aa6-82f9-5ef19898e7c0", "ResourceType" -> "Function", "Version" -> "1.0.0", "Description" -> "A two argument form of Counts that gives an association between a set of provided keys and the number of times those keys appear in a list", "RepositoryLocation" -> URL[
        "https://www.wolframcloud.com/objects/resourcesystem/api/1.0"], "SymbolName" -> "FunctionRepository`$7a3a61c711cf4c5e90b66949a03cb158`ItemCounts", "FunctionLocation" -> CloudObject[
        "https://www.wolframcloud.com/obj/2b78e33b-78bd-4d4e-a640-a836b8c05b2f"]}|>, ResourceSystemBase -> "https://www.wolframcloud.com/objects/resourcesystem/api/1.0"]][{"male", "female"}], #sex &], ImageSize -> 600]
Out[668]=

Neat Examples (2) 

Determine the order of operations in a Query designed to produce several statistics of a dataset involving age, gender and class, broken down by gender:

In[669]:=
ResourceFunction["QueryTreeForm"][
 Query[GroupBy[#sex &], Merge[Identity]/*(a |-> Association["count" -> Length[a["age"]], "1st" -> Count[a["class"], "1st"], "mean age" -> Mean[DeleteMissing[a["age"]]]]), KeyTake[{"age", "class"}]], ImageSize -> 500
 ]
Out[784]=

A function that, if applied to the Titanic dataset, will yield a breakdown of survival by gender and class in an attractive format:

In[785]:=
defaultAssociation = Association["male" -> 0, "female" -> 0];
In[786]:=
ResourceFunction["QueryTreeForm"][
 Query[GroupBy[#class &], Counts/*(Join[defaultAssociation, #] &), #sex &], ImageSize -> 500]
Out[922]=

Publisher

Seth J. Chandler

Version History

  • 1.0.0 – 15 July 2019

License Information