0

I have a Gremlin query where I need to check almost in every step if the passed param values are found, if yes then proceed to the next step otherwise exit the query with an error message which will be different in each step.

My sample data can be found here https://gremlify.com/wczryzotbf/1

An employee id, manager name, and designation(s) can be passed as params. My query is not taking designation param yet which I am yet to write.

The following query does not return any error message if an invalid employee id like 100 is passed. If I pass a valid employee id like 1 in this case it works as expected.

However I pass the correct employee id but the incorrect manager name, an error message Manager not found is returned as expected.

g.V().
  has('emp', 'emp_id', 100).as('empview').
  choose(
    select('empview').count(local).is(eq(0)),
    constant("Employee Id not found"),
    choose(
      outE().
      hasLabel('emp-mgr').
      has('start_date', gte(1643702400)).
      inV().
      has('manager', 'manager_name', 'manager2'),
      __.outE().
      hasLabel('emp-mgr').as('edgeview').
      has('start_date', gte(1643702400)).
      inV().
      has('manager', 'manager_name', 'manager2').as('managerview').
      select('empview', 'edgeview', 'managerview').
        by(
          valueMap(
            'emp_id',
            'emp_name',
            'start_date',
            'end_date',
            'manager_name')),
      constant("Manager not found")))

Now if I change the query to write has clause in the 2nd line within choose step it shows the error message for an invalid id but the query is executed for each employee vertex.

  g.V()
  choose(not(has('emp','emp_id', 1)),
    constant("Employee Id not found"),......

How I can show an error message if an employee id is not found?

Also once I traverse from an employee vertex to a manager vertex I need to collect data from the manager vertex and then I need to go back to the employee vertex to traverse the employee to the designation vertex using "emp-desig" edge and do the same validation step there. How i can go back to a node and traverse different path from there?

I am using the AWS Neptune database. My actual query will be written in gremlin python.

user2026504
  • 69
  • 1
  • 9

1 Answers1

0

The behavior you are seeing is expected. Consider the first part of your traversal with a bit of modification for demonstration purposes (i.e. I added a second constant() to indicate the else condition):

g.V().
  has('emp', 'emp_id', 100).as('empview').
  choose(
    select('empview').count(local).is(eq(0)),
    constant("Employee Id not found"),
    constant("Found!!"))

If you run that you will find that neither the if or the else condition are executed:

gremlin> g.V().has('emp', 'emp_id', 100).as('empview').
......1>   choose(select('empview').count(local).is(eq(0)),
......2>          constant("Employee Id not found"),
......3>          constant("Found!!"))
gremlin> 

The reason for that is if the initial has() filter fails to find anything there is no traverser to trigger the execution of choose(). That rest of the traversal will do nothing.

Instead of choose() I think you should make use of coalesce():

g.V().has('emp', 'emp_id', 3).fold().
  coalesce(unfold().as('empview').
           coalesce(outE('emp-mgr').as('edgeview').
                    has('start_date', gte(2483257600)).
                    inV().
                    has('manager', 'manager_name', 'manager2').as('managerview').
                    select('empview','edgeview','managerview').
                      by(valueMap('emp_id','emp_name','start_date','end_date','manager_name')),
                    constant('Manager not found')),
           constant("Employee Id not found"))
stephen mallette
  • 45,298
  • 5
  • 67
  • 135