krotik / eliasdb

EliasDB a graph-based database.
Mozilla Public License 2.0
994 stars 49 forks source link

Question : why comparaison operators don't work on string ? #12

Closed sapiens-sapide closed 7 years ago

sapiens-sapide commented 7 years ago

Hello, I'm digging into eliasdb for few days and I'm stuck with a 'query where clause' with strings.
I want to query nodes with a date string greater than a given string :
get message where date > 2017-03-21 for example. But I get the error Value of operand is not a number.

Does this mean that it is not possible to make strings comparaison in EQL ?
Thanks in advance for your response.

krotik commented 7 years ago

Hi,

In EQL the "greater than" operation is only defined for numbers (hence the error message). After all what should be the result of "foo" > "bar" be?

What you are asking is that the EQL interpreter can "understand" a date string. Dealing with date strings is something which I wanted to do for some time now and so I take this issue as an opportunity to start with it :-)

As you can imagine date strings can be in all sorts of different formats. There are numerous standards e.g. RFC822, RFC1123 or RFC3339. Go uses the "Parse" function in its time package for this.

I've now extended the EQL interpreter so a client can now use the @parseDate function in a where clause. The function takes a date string (with an optional layout definition) and converts it into an unix time integer (see https://en.wikipedia.org/wiki/Unix_time). Using this integer it is now possible to use the "greater than" operation. An example:

get datetest where @parseDate(date_value, '2006-01-02') > @parseDate('2017-03-21', '2006-01-02')

This query will search all "datetest" nodes and return all entries which have a date after the 21. of March 2017 in the attribute date_value. Assuming that the value in date_value is of the format --.

The layout definition string is the one which is used by Go (see https://golang.org/pkg/time/#Parse).

The code which handles this is in src/devt.de/eliasdb/eql/interpreter/func.go have a look at the unit test in func_test.go for more examples.

Hope this helps!

sapiens-sapide commented 7 years ago

Thanks krotik for your response. It's a good news to have the @parseDate function.
But, I still think it would be very useful to have the string comparaison available. Because all string dates are alphabetically sortable : the string "2006-01-02" is lesser than "2017-03-21", no need to convert to a number. Regards,
Stan.

krotik commented 7 years ago

Hi Stan,

I take your point - doing a string comparison alphabetically is more helpful than an error message. As such I changed now the relational operators > >= <= < to also deal with strings. Each operator will first try to interpret the operands as numbers and, if that fails, it will then compare the operands as strings.

Please reopen if you have more suggestions on this point.

sapiens-sapide commented 7 years ago

Thanks krotik. I'll test it and report any issue. Regards.