Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected behavior for multiple 'with' calls separated by 'order' #1642

Open
mvitale opened this issue Jan 7, 2021 · 5 comments
Open

Unexpected behavior for multiple 'with' calls separated by 'order' #1642

mvitale opened this issue Jan 7, 2021 · 5 comments

Comments

@mvitale
Copy link

mvitale commented Jan 7, 2021

I'm trying to produce a query similar to the following via method chaining. It gets the top five friends, ordered by name, of each user.

MATCH (user:User)-[:friend_of]->(friend:User)
WITH user, friend
ORDER BY user.id, friend.name ASC
WITH user, collect(friend)[0..5] AS top_friends_alphabetically
RETURN user, top_friends_alphabetically

I would expect something like this to work:

User.as(:user).friends.query_as(:friend)
  .with(:user, :friend)
  .order('user.id', 'friend.name ASC')
  .with(:user, 'collect(friend)[0..5] AS top_friends_alphabetically')
  .return(:user, :top_friends_alphabetically)

However, this results in the following invalid cypher query, lumping both .with calls into a single WITH before the ORDER BY:

MATCH (user:User)
MATCH (user)-[:friend_of]->(friend:User)
WITH user, friend, user, collect(friend)[0..5] AS top_friends_alphabetically
ORDER BY user.id, friend.name ASC
RETURN user, top_friends_alphabetically

Is this expected behavior? If so, is there an alternate way to accomplish what I'm trying to do without resorting to ActiveGraph::Base.query? Thanks!

@mrhardikjoshi
Copy link
Member

Hello @mvitale , following should work fine and it also avoids additional with clause.

User.as(:user).friends.query_as(:friend)
  .with(:user, :friend)
  .order('user.id', 'friend.name ASC')
  .return(:user, 'collect(friend)[0..5] AS top_friends_alphabetically')

It generates following cypher, which returns correct results:

  MATCH (user:`User`)
  MATCH (user)-[rel1:`friend_of`]->(friend:`User`)
  WITH 
    user, 
    friend
  ORDER BY 
    user.id, 
    friend.name ASC
  RETURN 
    user, 
    collect(friend)[0..5] AS top_friends_alphabetically 

@mvitale
Copy link
Author

mvitale commented Jan 8, 2021

Thanks! Actually, my example here is contrived and much simpler than the actual query I'm trying to write. In the real one, there's a bunch of matching that happens after the with calls. So, I think my question/bug report still stands: it seems like one should be able to call with multiple times without them getting lumped into a single WITH in the query.

@klobuczek
Copy link
Member

@mvitale have you tried the break method? It usually prevents such unwanted shuffle.

@mvitale
Copy link
Author

mvitale commented Jan 8, 2021

@klobuczek I haven't -- I'll give that a shot. Thanks for pointing it out. I'm new to the API.

@mvitale
Copy link
Author

mvitale commented Jan 8, 2021

@klobuczek That works, thanks. I'll leave the decision to close this issue or leave it open up to you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants