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

Adding array of geo gives "Input for predicate example.Loc of type scalar is uid" #2482

Closed
commesan opened this issue Jul 10, 2018 · 2 comments
Labels
kind/bug Something is broken.

Comments

@commesan
Copy link
Contributor

  • What version of Dgraph are you using?
    1.06

When using:


     type loc struct {
		Type   string    `json:"type,omitempty"`
		Coords []float64 `json:"coordinates,omitempty"`
	}

	type Tracker struct {
		Uid  string `json:"uid,omitempty"`
		Name string `json:"example.Name,omitempty"`
		Loc  []loc  `json:"example.Loc,omitempty"`
	}

	t := Tracker{
		Name: "Tom Baker",
		Loc: []loc{
			{
				Type:   "Point",
				Coords: []float64{1.1, 2},
			},
			{
				Type:   "Point",
				Coords: []float64{1.9, 5},
			},
		},
	}

And mapping:

        example.Name: string @index(exact) .
	example.Loc: [geo] @index(geo) .

I get Input for predicate example.Loc of type scalar is uid

When using geo as a list the SetJson parser seems to view the geojson types as nodes #instead of scalars.

@commesan
Copy link
Contributor Author

Here's a runnable example.

package main

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/dgraph-io/dgo"
	"github.com/dgraph-io/dgo/protos/api"
	"google.golang.org/grpc"
	"log"
)

type loc struct {
	Type   string    `json:"type,omitempty"`
	Coords []float64 `json:"coordinates,omitempty"`
}

type Tracker struct {
	Uid  string `json:"uid,omitempty"`
	Name string `json:"example.Name,omitempty"`
	Loc  []loc  `json:"example.Loc,omitempty"`
}

func main() {
	conn, err := grpc.Dial("127.0.0.1:9080", grpc.WithInsecure())
	if err != nil {
		log.Fatal("While trying to dial gRPC")
	}
	defer conn.Close()

	dc := api.NewDgraphClient(conn)
	dg := dgo.NewDgraphClient(dc)

	op := &api.Operation{}
	op.Schema = `
	example.Name: string @index(exact) .
	example.Loc: [geo] @index(geo) .
`

	ctx := context.Background()
	err = dg.Alter(ctx, op)
	if err != nil {
		log.Fatal(err)
	}

	t := Tracker{
		Name: "Tom Baker",
		Loc: []loc{
			{
				Type:   "Point",
				Coords: []float64{1.1, 2},
			},
			{
				Type:   "Point",
				Coords: []float64{1.9, 5},
			},
		},
	}

	mu := &api.Mutation{
		CommitNow: true,
	}
	tb, err := json.Marshal(t)
	if err != nil {
		log.Fatal(err)
	}

	mu.SetJson = tb
	_, err = dg.NewTxn().Mutate(ctx, mu)
	if err != nil {
		log.Fatal(err)
	}

	// Assigned uids for nodes which were created would be returned in the resp.AssignedUids map.
	variables := map[string]string{"$name": "Tom Baker"}
	q := `query GetTom($name: string){
	        me(func: eq(example.Name, $name)) {
		        example.Name
		        example.Loc		
	        }
       }`

	resp, err := dg.NewTxn().QueryWithVars(ctx, q, variables)
	if err != nil {
		log.Fatal(err)
	}

	type Root struct {
		Me []Tracker `json:"me"`
	}

	var r Root
	err = json.Unmarshal(resp.Json, &r)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(string(resp.Json))
}

This gives: pc error: code = Unknown desc = Input for predicate example.Loc of type scalar is uid

Adding the array of geopoints like this:

{
  set {
    _:tracker0 <example.Name> "Tom Baker" .
    _:tracker0 <example.Loc> "{'type':'Point', 'coordinates':[1.1, 2.0]}"^^<geo:geojson> .
    _:tracker0 <example.Loc> "{'type':'Point', 'coordinates':[1.1, 3.0]}"^^<geo:geojson> .
  }
}

does add them as a list of points.

@commesan
Copy link
Contributor Author

I tracked the problem down.

Handling the []interface{} case in nquads_from_json.go doesn't check for geotypes and assumes all non basic types should be handled as uid.

I'll write a fix for this and submit a pull request if that's ok?

commesan added a commit to commesan/dgraph that referenced this issue Jul 12, 2018
manishrjain pushed a commit that referenced this issue Aug 14, 2018
* Correctly handle list of type geo in json (#2482)

* Add handleGeoType function for handling a geotype or list of geotype (#2482)
@danielmai danielmai added the kind/bug Something is broken. label Oct 12, 2018
dna2github pushed a commit to dna2fork/dgraph that referenced this issue Jul 19, 2019
…rmodeinc#2485)

* Correctly handle list of type geo in json (hypermodeinc#2482)

* Add handleGeoType function for handling a geotype or list of geotype (hypermodeinc#2482)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Something is broken.
Development

No branches or pull requests

2 participants