Min, max: Support more than two arguments

Moved from GitHub dgraph/1050

Posted by sungwoncho:

Version: 0.7.7


When aggregating using min or max, as a user I would like to do the following:

max(v1, v2, v3)

rather than

max(max(v1, v2), v3)

Therefore it might be useful if min and max supported more than two arguments.

Use case: https://gist.github.com/sungwoncho/ddba3a287ad87288331c493c2de5a5cb#file-get_active_answers


Dataset:

dgraph-1-2017-07-10-18-51.rdf.gz
dgraph-schema-1-2017-07-10-18-51.rdf.gz

Query:

# You might need to change the value of id
{
  var (id: 0xd18499a5ac49c3a6) {
    Has.Answer {
      answerTs as Timestamp
      ~Post {
        historyTs as Timestamp
      }
      Comment {
        commentTs as Timestamp
      }
      commentTsMax as max(var(commentTs))
      historyTsMax as max(var(historyTs))
      lastActive as math(max(max(answerTs, commentTsMax), historyTsMax))
      # The following line should work, if replaced with the above line
      # lastActive as math(max(answerTs, commentTsMax, historyTsMax))
    }
  }

  question(id: 0xd18499a5ac49c3a6) {
    Has.Answer(orderdesc: var(lastActive)) {
      Body {
        Text
      }
    }
  }
}

ashwin95r commented :

Ping, @abhishekgahlot . Are you still on it?

abhishekgahlot commented :

@ashwin95r Yes, I am still working on this. Will give update soon.

kshitij10496 commented :

@pawanrawal Would it be fine if I give this a try?

pawanrawal commented :

Sure, go ahead and give it a try @kshitij10496.

martinmr commented :

Update: I tried working on this and I got some basic math expressions to work with min and max using more than two arguments. However, there are some bugs in the parser that are caused by the need of knowing exactly how many arguments a function needs. Since this is a very basic assumption, I would need to rework the entire parser, which requires a lot more work than I initially thought.

srfrog commented :

@sungwoncho and @martinmr see the attached PR for a possible solution. Thx

martinmr commented :

Assigning to @pawanrawal to see if he has better luck with this than Gus and I did.