go-driver icon indicating copy to clipboard operation
go-driver copied to clipboard

Query on a view - the cursor returns an empty document

Open sereeena opened this issue 5 years ago • 2 comments

Is it supported to do AQL queries on views with the driver? I have a simple collection with a view defined on it with includeAllFields set to true, no other options set.

Environment:

  • github.com/arangodb/go-driver v0.0.0-20200811070917-cc2b983cf602
  • go version go1.13.4 linux/amd64
  • ArangoDB v3.7.1

If I do the following in the query editor: RETURN (FOR v IN vvariants SEARCH v.chr == 5 RETURN v._key)

The result is

[
  [
    "14681832"
  ]
]

If in code:

        var variants interface{} // or []string, either returns empty document
        query := `RETURN (FOR v IN vvariants SEARCH v.chr == @chr  RETURN v._key)`
        bindVars := map[string]interface{}{"chr": 5}
        cursor, err := db.Query(ctx, query, bindVars)
        if err != nil {
		fmt.Println("Query failed:", err)
		return variants, err
	}
	defer cursor.Close()
	
        if cursor.HasMore() {
		_, err = cursor.ReadDocument(ctx, &variants)
		if err != nil {
			fmt.Println("Failed to read document:", err)
			return variants, err
		}
		fmt.Println(variants)  // prints []
	} else {
		fmt.Println("No documents found")
	}
	return variants, nil

I expect to see the same result as the query editor, but the document read from the cursor is empty? I can't see what I am doing wrong, is it supposed to be supported? Thanks!

sereeena avatar Aug 27 '20 06:08 sereeena

Can anyone confirm? I believe this is a bug in the driver, where documents can't be returned from Database.Query if the AQL query is returning a document or projection from a view.

The same AQL query will work in the arangoDB query editor, and I also don't think it is a problem with the view updating itself as I have added time.Sleep before executing the query to no effect, so it is definitely driver specific.

I have found a workaround, which is to use AQL to query the view and return the results in a variable, which can then be used to filter and return documents on the actual collections rather than the view, which still allows me to use views.

sereeena avatar Sep 09 '20 02:09 sereeena

Out of curiosity I gave this a shot. I swear I was able to reproduce it, even though the response over the wire was identical to an identical FILTER query. But I can no longer reproduce it, even after bouncing the server...

My best guess is that A) it's some kind of time delay after creating a View, where the Coordinator (or similar) hasn't caught up, or B) some kind of state was knocked loose when I tried setting AllowDirtyReads on the query, ala:

...
t := true
newCtx := godriver.WithAllowDirtyReads(ctx, &t)
cursor, err := db.Query(newCtx, query, bindVars)
...

But as of now your example works for me (on [email protected] and go-driver@master, FWIW).

irridia avatar Oct 01 '20 03:10 irridia

Closing since we can not reproduce it anymore

jwierzbo avatar Feb 27 '24 08:02 jwierzbo