Wolfram Research

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

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[1]=

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[2]:=
ResourceFunction[
 "QueryTreeForm"][{Select[f] /* GroupBy[g] /* h, All, i}]
Out[2]=

Scope

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

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

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

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

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

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

Options

All the options of TreeForm are available to this function:

In[6]:=
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[6]=

Applications

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

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

Properties and Relations

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

In[8]:=
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[8]=

Possible Issues

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

In[9]:=
ResourceFunction["QueryTreeForm"][
 Query[GroupBy[#class &], ResourceFunction[
ResourceObject[
Association[
     "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[9]=

Neat Examples

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

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

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

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

Resource History

License Information