Trying to use GSQL like SQL, but "mismatched input '.' expecting FROM"

I’m trying to apply my SQL experience to GSQL but it has failed me (again.)

from the TG 101 example, I got this query to work:

USE GRAPH social
CREATE QUERY hello(VERTEX<person> p) FOR GRAPH social{
  Start = {p};
  Result = SELECT tgt
           FROM Start:s-(friendship:e) ->person:tgt;
  PRINT Result;
}

The result does show a friend does contain age attribute (here’s a portion of the result):

    {'v_id': 'Jenny',
     'attributes': {'gender': 'female',
      'name': 'Jenny',
      'state': 'tx',
      'age': 25},
     'v_type': 'person'}

I wanted to make a small change to return the friends’ age. I would have expected this to work based on my experience with SQL, where I replaced SELECT tgt with SELECT tgt.age.

CREATE QUERY getFriendAge(VERTEX<person> p) FOR GRAPH social{
  Start = {p};
  Result = SELECT tgt.age
           FROM Start:s-(friendship:e) ->person:tgt;
  PRINT Result;
}

I get this error msg:

mismatched input '.' expecting FROM

where GSQL is complaining about the . between tgt and age

What am I missing? How do I get this to work?

Stop trying to use SQL :slight_smile:
Generally start thinking of result sets not in your SQL world of columns and derived columns but rather as vertex sets.

Here is an easy way

 CREATE QUERY getFriendAge(VERTEX<person> p) FOR GRAPH social{
  Start = {p};
  Result = SELECT tgt
           FROM Start:s-(friendship:e) ->person:tgt;
  PRINT Result[Result.age];
}
2 Likes

I don’t quite get the logic of:

Result[Result.age]

I would have expected:

Result["age"]

Of course, the advantage of using an expression as an “index” into result, is you can programmatically specify which field you actually want to use instead of hard coding the value. (sorry for Python thinking…)

Sure - there are programmatic ways such as getAttr

https://docs.tigergraph.com/dev/gsql-ref/querying/operators-functions-and-expressions#vertex-functions

But since the name of your query was getFriendAge I felt it was appropriate to have it hard coded in that instance

2 Likes