Next: , Previous: Select Transformations, Up: Basic Transformations



7.1.3 Control Transformations

block
"block" takes a list of transformations, which are each executed in turn.
ifexist
"ifexist" takes two or three arguments. The first is an column name, if this column exists the second argument (which is a transformation) is executed. The optional third argument is the transformation to execute if the column does not exist.

An example of an ifexist transformation is:

          ifexist("pdiff", update("pdiff", "$pdiff - $sys - $user"))
     

This transformation sets pdiff to the CPU time used by everything, except the test, but does not fail if pdiff doesn't exist.

if
"if" is similar to ifexist, but the first argument is an expression that undergoes global replacement and is then passed to Perl's eval function.
eval
"eval" takes a single argument, which undergoes global replacement and is then passed to Perl's eval. This is useful for embedding tiny snippets of Perl code in your getstats commands.
noop
"noop" does nothing. It can be used as a place holder or to "comment" out a transformation by inserting "noop".
foreachcol
"foreachcol" takes one argument, which is another transformation. The transformation is then run once for each column in the relation. Replacements in the context of "foreachcol" do column replacement. If "foreachcol" is nested within "foreachrow", then value replacement is performed. For information about replacement, See Replacements.
foreachrow
"foreachrow" takes one argument, which is another transformation. The transformation is then run once for each row in the relation. Replacements in the context of "foreachrow" do row replacement. If "foreachrow" is nested within "foreachcol", then value replacement is performed.
group
"group" is among the most complicated of transformations. It takes two arguments. The first is a field to group on, and the second is a transformation to run on each of these groups.

The group transformation starts by partitioning the current relation into a set of new relations, such that each unique value of key produces a new relation and all the elements in that relation have the same value of key.

For example, grouping rel9 on epoch creates rel10a, rel10b and rel10c.

epoch thread elapsed cpu
1 1 10 2
1 2 11 2
2 1 9 2
2 2 11 3
3 1 10 3
3 2 9 2

Relation 7.7

epoch thread elapsed cpu
1 1 10 2
1 2 11 2

Relation 7.8

epoch thread elapsed cpu
2 1 9 2
2 2 11 3

Relation 7.9

epoch thread elapsed cpu
3 1 10 3
3 2 9 2

Relation 7.10

The second argument to group is a transformation that is run on each of the new relations individually. A typical example of this transformation would be the aggregate transformation.

Assume we ran remove(thread) on each relation, we would have three new relations. For example, Relation 7.8 would be come:

epoch elapsed cpu
1 10 2
1 11 2

Relation 7.11

We could then use the following aggregate transformation:

          aggregate({
          "elapsed" = "$max",
          "cpu" = "$sum",
          "epoch" = "$mean",
          })
     

Note that because epoch is the key we grouped by we can use mean to get the single value of epoch.

Running this transformation on Relation 7.11 would yield rel11.

epoch elapsed cpu
1 11 4

Relation 7.12

This transformation is repeated on Relation 7.9 and Relation 7.10, and finally the results are concatenated, yielding rel12:

epoch elapsed cpu
1 11 4
2 11 5
3 10 5

Relation 7.13

Relation 7.13 is the final output of this example group command.