Expressions

Updated at:

Syntax rules

logical_attribute_expression := attribute_expression logical_operator attribute_expression 
logical_operator := AND | OR
relational_attribute_expression := attribute_expression relational_operator attribute_expression 
relational_operator := '=' | '>' | '<' | '<=' | '>=' | '!='
arithmatic_attribute_expression := attribute_expression arithmatic_operator attribute_expression | atomic_attribute_expression
arithmatic_operator := '+' | '-'|'*'|'/' 
atomic_attribute_expression := attribute_name | number_literal 
attribute_name := IDENTIFIER
number_literal := NUMBER
  • An attribute_expression supports numbers, strings, and multi-value types.

  • Strings support all relational operations but do not support arithmetic operations.

  • Multi-value types support only the relational operators "=" and "!=".

    • The semantics of the = operator for multi-value types is that the attribute contains the value of the filter condition.

Operators

+    -   *  /    >    <    =   !=   >=    <=    AND    OR    ()    @
  • @ is the bitwise AND operator.

  • AND is the logical AND operator.

Expressions

  • This function encapsulates the evaluation logic for the query process.

  • Note: All expressions support operations only between fields of the same type.

Arithmetic expressions

  • The result of an arithmetic expression is a non-Boolean type.

  • Simple variable: a

  • Basic operations: a + b, a + b - c, and so on.

  • Constant: 119

Logical expressions

  • The result of a logical expression is a Boolean type.

  • Logical operations AND, OR

    • a AND b, a OR b

    • Both a and b can be expressions, such as uid=1000 OR uid=2000.

  • Relational operations > < = <= >= !=

    • a > b, a < b, a = b, a <= b, a >= b

Optimization suggestions:

To avoid significant performance issues, do not use more than 10 filter conditions in a single query.

You can merge homogeneous filter conditions using functions such as in, notin, in_string, or notin_string.

Mathematical function expressions

  • Trigonometric functions sin, cos, tan, cot, asin, acos, atan, sinh, cosh, tanh

    • Semantics: These functions are consistent with the C++ math library and are not described in more detail.

    • Example: sin(a), where a is a numeric expression.

  • Type conversion functions to_double, to_int

    • to_double(a)

    • Semantics: Returns the value of a as a double type.

    • Note: a can be a string expression that contains only numbers.

    • to_int(a)

    • Semantics: Returns the value of a as a long type.

    • Note: a can be a string expression that contains only numbers.

  • Mathematical functions abs, ln, log10, exp, sqrt, trunc, ceil, floor, round, pow(a, b)

    • Semantics: These functions are consistent with the C++ math library.

  • Conditional function if

    • Format: if(bool_expression, a, b)

    • Semantics: If bool_expression is true, the function returns the value of a. Otherwise, the function returns the value of b.

    • Notes

    • The result of bool_expression must be a Boolean type.

    • The data types of a and b must be the same. The return value of the function has the same data type as a.

    • Example: if(a > 10, b, c). If the value of a is greater than 10, the function returns the value of b. Otherwise, it returns the value of c.

  • Conditional function case

    • Format: case(bool_expression, a, bool_expression, b, c, ...)

    • Semantics: The function sequentially evaluates each bool_expression. If a bool_expression is true, the function returns the value of the parameter that follows it. If no bool_expression is true, the function returns the last value.

    • Notes

    • The case expression must have an odd number of parameters.

    • The values of a, b, and c must have the same data type.

    • Example: case(a > 20, b, a > 10, c, a > 5, d, e). If the value of a is greater than 20, the function returns the value of b. If the value of a is greater than 10, the function returns the value of c. If the value of a is greater than 5, the function returns the value of d. Otherwise, the function returns the value of e.

  • Range functions in, notin, in_string, notin_string

    • in(a, b) or notin(a, b)

    • Notes

    • The parameter a can be any numeric expression, and can be a multi-value or single-value field. For multi-value fields, the expression is true if any value in the field is a match.

    • The parameter b must be a list of constant numeric values separated by pipe characters (|).

    • The data types of the constant values in a and b must be the same.

    • This function returns a Boolean value.

    • Example: in(nid, "10|12|13"), notin(tag_id, "101|203|405")

    • This syntax provides significantly better performance than nid=10 OR nid=20 OR nid=30.

    • in_string(field_name, group_condition, separator)

    • Notes

    • The field_name parameter must be a string expression.

    • The group_condition parameter must be a list of string expression values.

    • The separator parameter specifies the separator character.

    • This expression returns a Boolean value.

    • Example: in_string(name, "abc|cde|fgh", "|"), notin_string(tag, "aaa,bbb,ccc", ",")

  • Range count function count_in

    • Format: count_in(a, b)

    • Notes

    • The parameter a can be any numeric expression, and can be a multi-value or single-value field. For multi-value fields, each match is counted.

    • The parameter b must be a list of constant numeric values separated by pipe characters (|).

    • The data types of the constant values in a and b must be the same.

    • This expression returns an integer.

    • Example: count_in(nid, "10|13|12|13")

    • If nid=10, the result is 1.

    • If nid=13, the result is 2.

    • If the value of nid is 12 13, the result is 3. This is because 12 is hit once and 13 is hit twice.

    • If the value of nid is 13 12 13, the result is 5. This is because the first 13 is hit twice, 12 is hit once, and the second 13 is hit twice.

Example

Gremlin

  • Expressions support logical, relational, and arithmetic operations on the properties of an entity.

  • You can use expressions in the filter() step and in any single step that can be modified by by().

g("thinkerpop").V("1;2;3;4;5;6").hasLabel("thinkerpop_modern_person").filter("age<30")
==> {"label":"thinkerpop_modern_person","age":29,"name":"marko","pk":"1"}
==> {"label":"thinkerpop_modern_person","age":27,"name":"vadas","pk":"2"}