GSQL V2 Error - a possible Bug

Question about an error in GSQL V2 - see the picture below.
Running on TG-Cloud v. 3 in the Studio.
The ClassCode is a UINT attribute of RegionClass.
Why am I getting an error:
(52, 12) Error: no type can be inferred for s.ClassCode

Trying to declare the type in the seed like that didn’t work:
start(RegionClass) = {RegionClass.*};

The complete query code follows:

CREATE QUERY insertGradeClass(VERTEX<Country> keyCountry, VERTEX<Region> keyRegion,VERTEX<Grade> keyGrade, UINT gradeClassIndex) FOR GRAPH SkillBlasterDev syntax v2 { 
  
  STRING keyGradeClass;
  STRING firstClass;
  STRING nextClass;
  
  EXCEPTION gradeClassExists (40001);
  
  keyGradeClass = "grade-class-" + to_string(keyCountry.CountryCode) + "-" + to_string(keyGrade.GradeIndex) + "-" + to_string(gradeClassIndex);
  
  //Does grade class exists
  start = {GradeClass.*};
  vGradeClass = SELECT s FROM start:s WHERE s == to_vertex(keyGradeClass, "GradeClass");
                  
  IF vGradeClass.size() > 0 THEN
      RAISE gradeClassExists ("Grade Class already exists for this Grade");
  END;
  
  //If first grade class - add it as the first in the chain
  start = {Grade.*};
  vFirstGradeClass = SELECT t FROM start:s-()-GradeClass:t WHERE s == keyGrade
      POST-ACCUM firstClass = t.id;
  
  IF vFirstGradeClass.size() > 0 THEN
    //Traverse chain to find the last regionClass
    WHILE (vFirstGradeClass.size() > 0) DO
      nextClass = firstClass;
      vFirstGradeClass = SELECT t FROM vFirstGradeClass:s-(nextGradeClass>)-GradeClass:t
        POST-ACCUM firstClass = t.id; 
    END;
    //nextClass is the last in the chain - insert new vertex after it with edge
    INSERT INTO GradeClass (PRIMARY_ID, GradeClassIndex) VALUES (
      keyGradeClass, gradeClassIndex);
    INSERT INTO nextGradeClass (FROM, TO) VALUES (
    nextClass,
    keyGradeClass
  );
  ELSE
    //First grade class of grade to be inserted
    INSERT INTO GradeClass (PRIMARY_ID, GradeClassIndex) VALUES (
      keyGradeClass, gradeClassIndex);
    INSERT INTO firstGradeClass (FROM, TO) VALUES (
    keyGrade,
    keyGradeClass
  );
  END;
  
  //Add edge from newly inserted GradeClass to the matching RegionClass
  start(RegionClass) = {RegionClass.*}; //Hardcoding the type for start as a workaround for V2 failing here.
  //vRegionClass = SELECT s FROM start:s-(firstRegionClass:e)-Region:t WHERE s == keyGrade
  vRegionClass = SELECT s FROM start:s-(*1..6)-Region:r-()-Country:t WHERE t == keyCountry AND r == keyRegion 
        AND s.ClassCode == gradeClassIndex;
  
  PRINT keyGradeClass;
}

Hello @morsagmon

Thank you for reporting this issue. Based on the query you provided above, I created the following sample schema to try reproducing the issue:

DROP ALL

CREATE VERTEX Country (id STRING PRIMARY KEY, CountryCde UINT)
CREATE VERTEX Grade (id STRING PRIMARY KEY, GradeIndex UINT)
CREATE VERTEX Region (id STRING PRIMARY KEY)
CREATE VERTEX GradeClass (id STRING PRIMARY KEY, GradeClassIndex UINT)
CREATE VERTEX RegionClass (id STRING PRIMARY KEY, ClassCode UINT)

CREATE DIRECTED EDGE firstGradeClass(FROM Grade, TO GradeClass)
CREATE DIRECTED EDGE nextGradeClass(FROM GradeClass, TO GradeClass)

CREATE DIRECTED EDGE firstRegionClass(FROM Region, TO RegionClass)
CREATE DIRECTED EDGE nextRegionClass(FROM RegionClass, TO RegionClass)

CREATE DIRECTED EDGE hasRegion(FROM Country, TO Region)


CREATE GRAPH SkillBlasterDev(*)

However I was unable to reproduce the issue on product version 3.1.

We could consider the following workaround:
Separate the condition s.ClassCode == gradeClassIndex from the last SELECT block. One option is to do the filtering in the line defining vertex set variable start

start =
  SELECT s
  FROM RegionClass:s
  WHERE s.ClassNode == gradeClassIndex;

vRegionClass =
  SELECT s
  FROM start:s -(*1..6)- Region:r -()- Country:r
  WHERE t == keyCountry AND r == keyRegion;

If possible, could you also provide some additional info on the graph schema so that we can look into this issue deeper?

Hi Adil.
Thank you for looking into this and assisting me!

First off, I’m running on 3.0.6, not 3.1.

I tried your workaround, but can’t get past an unknown error flagged by the editor on an empty line between the last statement you offered and the subsequent “PRINT” statement:

  start = SELECT s FROM RegionClass:s WHERE s.ClassCode == gradeClassIndex;

  vRegionClass = SELECT s FROM start:s-(*1..6)-Region:r-()-Country:t WHERE t == keyCountry AND r == keyRegion;
  
  PRINT keyGradeClass;

This is the error:

(57, 9) Error: extraneous input '==' expecting {ANY, AVG, BY, COALESCE, COUNT, DATETIME_ADD, DATETIME_SUB, FALSE, FILE, GROUP, ISEMPTY, LASTHOP, LIST, LOG, MAP, MATCH, MAX, MIN, NOT, NOW, PATH, PER, RANGE, SELECT_VERTEX, SRC, SUM, TGT, TO_DATETIME, TRIM, TRUE, UPDATE, GSQL_INT_MAX, GSQL_INT_MIN, GSQL_UINT_MAX, '__ENGINE__E_ATTR', '__ENGINE__SRC_ATTR', '__ENGINE__TGT_ATTR', '__ENGINE__V_ATTR', '__ENGINE__SRC_VAL', '__ENGINE__TGT_VAL', '__ENGINE__V_VAL', '__ENGINE__MESSAGE', '__ENGINE__CONTEXT', '__ENGINE__REQUEST', '__ENGINE__SERVICEAPI', '(', '[', '-', '.', '_', CONST_INT, CONST_STR, NAME, GACCNAME}

So, I really can’t even test it to run.
Also, I’m now getting a very annoying “error” popup on every move - how do I turn this OFF?

Finally, I hope the below image tells you more about the Graph section involved.

In that case, for the block that defines vRegionClass, consider specifying the edge types explicitly. For example:

vRegionClass = SELECT s FROM start:s -((_>|<_)*1..6)- Region:r ...;

Hi Adil.

Thanks for the suggestion.
I found an earlier issue that required a workaround in lines 24-25. I needed to take the WHERE into the start command here:
start = SELECT s FROM Grade:s WHERE s == keyGrade;
vFirstGradeClass = SELECT t FROM start:s-()-GradeClass:t
POST-ACCUM firstClass = t.id;

Now, the error on the famous line:

vRegionClass = SELECT s FROM start:s-(*1..6)-Region:r-()-Country:t WHERE t == keyCountry AND r == keyRegion;

Reads:

(55, 0) Error: Left set 'VS__6' returns vertex type [GradeClass], but the right set '(SeedVS_66)' returns vertex type [Region]

I can’t figure out what it is.
Here are the two lines together:

start = SELECT s FROM RegionClass:s WHERE s.ClassCode == gradeClassIndex;
  vRegionClass = SELECT s FROM start:s-(*1..6)-Region:r-()-Country:t WHERE t == keyCountry AND r == keyRegion;

Can you spot anything?

Thanks!!
Mor

Really sorry for the delayed response. You are right about the bug.

When we add a new v2 syntax query, GSQL will perform some query-rewriting to optimize the input query. The start:s -(*1..6)- Region:r part is mishandled during this rewriting step, hence leading to the above errors.

For workaround I would suggest breaking the following two lines apart:

// BEFORE
start = SELECT s FROM RegionClass:s WHERE s.ClassCode == gradeClassIndex;
vRegionClass = SELECT s FROM start:s-(*1..6)-Region:r-()-Country:t WHERE t == keyCountry AND r == keyRegion;

// AFTER
start = SELECT s FROM RegionClass:s WHERE s.ClassCode == gradeClassCode;
vRegion = { keyRegion };
vCountry = { keyCountry };
vRegionClass = SELECT s FROM start:s -((_|_>|<_)*1..6)- vRegion:r -()- vCountry:t;

Hi Adil.
Thank you for this - your workaround seems to work - the query is now installed.
I still get a yellow marker (maybe a warning?) next to the “vRegionClass =…” statement, but no text message is found to understand what it is. Anyway, it is now installed.
I hope this bug is fixed in the future to help others.

Take care!

1 Like