String¶
Fixed-length strings and variable-length strings are supported.
Declaration and literal representation¶
The string type is declared with the keywords of:
STRING
: Variable-length strings.FIXED_STRING(<length>)
: Fixed-length strings.<length>
is the length of the string, such asFIXED_STRING(32)
.
A string type is used to store a sequence of characters (text). The literal constant is a sequence of characters of any length surrounded by double or single quotes. For example, "Hello, Cooper"
or 'Hello, Cooper'
.
String reading and writing¶
Nebula Graph supports using string types in the following ways:
- Define the data type of VID as a fixed-length string.
- Set the variable-length string as the Schema name, including the names of the graph space, tag, edge type, and property.
- Define the data type of the property as a fixed-length or variable-length string.
For example:
- Define the data type of the property as a fixed-length string
nebula> CREATE TAG IF NOT EXISTS t1 (p1 FIXED_STRING(10));
- Define the data type of the property as a variable-length string
nebula> CREATE TAG IF NOT EXISTS t2 (p2 STRING);
When the fixed-length string you try to write exceeds the length limit:
- If the fixed-length string is a property, the writing will succeed, and NebulaGraph will truncate the string and only store the part that meets the length limit.
- If the fixed-length string is a VID, the writing will fail and NebulaGraph will return an error.
Escape Characters¶
In strings, the backslash (\
) serves as an escape character used to denote special characters.
For example, to include a double quote ("
) within a string, you cannot directly write "Hello "world""
as it leads to a syntax error. Instead, use the backslash (\
) to escape the double quote, such as "Hello \"world\""
.
nebula> RETURN "Hello \"world\""
+-----------------+
| "Hello "world"" |
+-----------------+
| "Hello "world"" |
+-----------------+
The backslash itself needs to be escaped as it's a special character. For example, to include a backslash in a string, you need to write "Hello \\ world"
.
nebula> RETURN "Hello \\ world"
+-----------------+
| "Hello \ world" |
+-----------------+
| "Hello \ world" |
+-----------------+
For more examples of escape characters, see Escape character examples.
OpenCypher compatibility¶
There are some tiny differences between openCypher and Cypher, as well as nGQL. The following is what openCypher requires. Single quotes cannot be converted to double quotes.
# File: Literals.feature
Feature: Literals
Background:
Given any graph
Scenario: Return a single-quoted string
When executing query:
"""
RETURN '' AS literal
"""
Then the result should be, in any order:
| literal |
| '' | # Note: it should return single-quotes as openCypher required.
And no side effects
While Cypher accepts both single quotes and double quotes as the return results. nGQL follows the Cypher way.
nebula > YIELD '' AS quote1, "" AS quote2, "'" AS quote3, '"' AS quote4
+--------+--------+--------+--------+
| quote1 | quote2 | quote3 | quote4 |
+--------+--------+--------+--------+
| "" | "" | "'" | """ |
+--------+--------+--------+--------+