Search code examples
orientdb

OrientDB date manipulation in OSQL to find a path with nodes on the same day but different years


I have a graph that is structured like this:

A --> B

A has a Date field.

This is still based on the same problem as in my other question, but this differs in that I'm not concerned with trying to do everything in one OSQL call to create edges. In this case, I'm only trying to generate a path that looks like:

A1 (date=2014-01-01) ---> B <--- A2 (date=2013-01-01)

And return a table containing just the A1 and A2 id's.

I've started working up a MATCH query that looks like:

MATCH {CLASS: A, AS: A1} -edgeTypeA->
  {AS: B} <-edgeTypeA-
  {AS: A2, WHERE: {$matched.A1.Date=$currentMatch.Date}
RETURN A1, B, A2

This works but it's only returning matches where basically A1 and A2 are the exact same date (including where A1 and A2 are the same node).

I know I can extract parts of a date field using format()... for example, if I just want a string containing the year I could use $currentMatch.Date.format('yyyy') to get that but I still haven't figured out how to increment that date for the purposes of my query.


Solution

  • I got it working after seeing what Alessandro did in my old question. I'm adding my solution for archival purposes. Hopefully it might help others down the line who need to do date manipulation within OSQL queries.

    Here's what I ended up with:

    MATCH {CLASS: A, AS: A1} -edgeTypeA->
        {AS: B} <-edgeTypeA-
        {AS: A2, WHERE: ($matched.A1.Date.format('yyyy').asInteger()=sum($currentMatch.Date.format('yyyy').asInteger(),-1) AND
                         $matched.A1.Date.format('MM-dd')=$currentMatch.Date.format('MM-dd') ) }
    RETURN A1,A2
    

    and I get the right thing for my data:

    +----+------+------+
    |#   |A1    |A2    |
    +----+------+------+
    |0   |#49:30|#49:32|
    |1   |#55:44|#56:46|
    |2   |#53:0 |#53:2 |
    |3   |#55:20|#55:22|
    |4   |#49:42|#49:44|
    |5   |#50:32|#50:34|
    +----+------+------+
    

    It kind of feels like a lot to have to make the call to sum() and asInteger() but now I understand it and can work with that.