Comparing the value of an accumulator before and after an ACCUM clause

Sometimes you want to know if an ACCUM clause actually modified an accumulator or by how much. We have a handy, tiny(!) operator for that: the tick operator ( ’ ). It’s been a part of GSQL since before v1.0, but it’s so tiny it often escapes notice:

From https://docs.tigergraph.com/dev/gsql-ref/querying/operators-functions-and-expressions#accumulator-functions:
The tick operator ( ’ ) can be used to read the value of an accumulator as it was at the start an ACCUM clause, before any changes that took place within the ACCUM clause. It can only be used in the POST-ACCUM clause. A typical use is to compare the value of the accumulator before and after the ACCUM clause. The PageRank algorithm provides a good example:

v = SELECT s
    FROM start:s - (e_type:e) -> :t
    ACCUM t.@received_score += s.@score/(s.outdegree(e_type))
    POST-ACCUM s.@score = (1.0 - damping) + damping * s.@received_score,
           s.@received_score = 0,
           @@max_diff += abs(s.@score - s.@score'); 

In the last line, we compute @@max_diff as the absolute value of the difference between the post-ACCUM score (s.@score) and the pre-ACCUM score (s.@score').

3 Likes

Very cool -

to be precise - do you actually mean the difference between the post-POST-ACCUM score and the pre-POST-ACCUM score?

1 Like

I mean pre-ACCUM and post-ACCUM. Here’s a timeline:

Stage 1: SELECT…FROM…WHERE clauses

  • We select which vertices and edges we want to work with, and assign aliases to the groups (source vertices, edges, and target vertices).
  • No change to accumulator values yet

Stage 2A: Inside the ACCUM clause

  • += accumulation is queued up, but the changes don’t take place yet (a necessity of sane parallel processing).
  • alias.@myAccum : no change yet

Stage 2B: Exiting the ACCUM clause

  • Accumulation takes place. alias.@myAccum will now show the cumulative effects of the ACCUM clause.

Stage 3A: Inside a POST-ACCUM clause

  • Second change accumulation, those only based on vertex aliases. Edge aliases are not available.
  • Again, POST-ACCUM += operations are queued up, but do not take place yet.
  • alias@myAccum is the value at Stage 2B
  • alias@myAccum’ is the value at Stage 2A

Stage 3B: Exiting the POST-ACCUM clause

  • Accumulation takes place. alias.@myAccum will now show the cumulative effects of POST-ACCUM clause.
1 Like

Perfect explanation, thank you. But in your original example, the @score accumulator is not modified in the ACCUM clause. Hence my question.

1 Like