How can I determine if two queries return the same results?

Hi to everyone!

I ran two queries which - I believe, however I might as well be completely wrong - return the same results:

<>MATCH (src:Address)-[:REDEEMED]->(ip:IncomingPayment)-[:INCOMING_PAYMENT]->(tx:Transaction)-[:SENT_COINS]->(op:OutgoingPayment)-[:WAS_SENT_TO]->(dst:Address {AddressID: 'xxxxxxxxxxxxxxxxx'})
RETURN distinct(src.AddressID) as first_order_txs
LIMIT 5;</>

<>MATCH (src:Address)-[:REDEEMED]->(ip:IncomingPayment)-[:INCOMING_PAYMENT]->(tx:Transaction)-[:SENT_COINS]->(op:OutgoingPayment)-[:WAS_SENT_TO]->(dst:Address) WHERE dst.AddressID IN
['xxxxxxxxxxxxxxxxx']
RETURN distinct(src.AddressID) as first_order_txs
LIMIT 5;</>

The result in both cases is a table with 5 entries; the entries are not the same. My expectation was that they would be the same.

  • What am I missing? Are the two queries equivalent?
  • In the general case, how can I compare if two queries return the same result?

  • neo4j desktop 3.5.16
  • Linux 19.10

Thanks!
Andreas

Hello @and_manousakis,

First of all, in case you have only one AddressID, use your first request:

MATCH (src:Address)-[:REDEEMED]->(ip:IncomingPayment)-[:INCOMING_PAYMENT]->(tx:Transaction)-[:SENT_COINS]->(op:OutgoingPayment)-[:WAS_SENT_TO]->(dst:Address {AddressID: 'xxxxxxxxxxxxxxxxx'})
RETURN distinct(src.AddressID) AS first_order_txs
LIMIT 5

But if you have multiple AddressID, use your second request:

MATCH (src:Address)-[:REDEEMED]->(ip:IncomingPayment)-[:INCOMING_PAYMENT]->(tx:Transaction)-[:SENT_COINS]->(op:OutgoingPayment)-[:WAS_SENT_TO]->(dst:Address)
WHERE dst.AddressID IN ['xxxxxxxxxxxxxxxxx']
RETURN distinct(src.AddressID) AS first_order_txs
LIMIT 5

Now if you want to get the same results, you should try to add an ORDER BY clause before the LIMIT, for example:

MATCH (src:Address)-[:REDEEMED]->(ip:IncomingPayment)-[:INCOMING_PAYMENT]->(tx:Transaction)-[:SENT_COINS]->(op:OutgoingPayment)-[:WAS_SENT_TO]->(dst:Address {AddressID: 'xxxxxxxxxxxxxxxxx'})
RETURN distinct(src.AddressID) AS first_order_txs
ORDER BY first_order_txs
LIMIT 5

Regards,
Cobra

Interesting finding, Andreas. I also expected the Neo4j internal parser to translate exact matches into similar queries and return the same result.

If, as Cobra said, only their result order is different, then it behaves just like in SQL: the results are indeed guaranteed to be the same, but not necessarily in the same order. Unless you use ORDER BY.

Cheers,
-C

Hi @cobra @cristiscu, thank you for the replies! I used ORDER BY src.AddressID DESC LIMIT 5; and the results were indeed the same.

Best
Andreas

1 Like