GSQL Style Guide
This page details TigerGraph’s style guide for writing GSQL. The purpose of the style guide is to make code easy to read and achieve lower cost of maintenance.
General
-
Always write keywords and reserved words in all caps. You can find a list of keywords and reserved words in Query Language Keywords & Reserved Words and DDL Keywords & Reserved Words.
-
If you are storing GSQL statements in a separate file, use the file extension
.gsql
.
Naming
Identifiers are categorized into two broad classes:
-
Schema object types (graph, vertex, edge, and tuple type).
-
Instances.
Schema types are analogous to classes in an object-oriented language. Schema object types names have their initial letter capitalized, connected by underscore; instances use snake case, with additional specifications for accumulators.
Detailed identifier naming conventions are described in the following table:
Identifier type | Style | Example |
---|---|---|
Instances (This includes any variable one can declare in a loading job or query not otherwise specified in the table) |
Use snake case: lowercase, letters connected by underscore |
|
Non-collection accumulators ( |
|
|
Collection accumulator |
|
|
Vertex type name |
Capitalize initial letters and connect words with underscores. |
|
Edge type name |
Capitalize initial letters and connect words with underscores. |
|
Vertex and edge attribute name |
Use snake case. |
|
Graph name |
Capitalize initial letters and connect words with underscores. |
|
Tuple type name |
Capitalize initial letters and connect words with underscores. |
|
Query and loading job name |
Use snake case. |
|
Spacing
Block indentation
Indent the body of a block by 4 spaces.
A block is a syntactic structure which may contain multiple statements.
This includes queries, SELECT
, IF/ELSE
, CASE WHEN/THEN
, FOREACH
, and WHILE
.
If the body of ACCUM
or POST-ACCUM
contains multiple clauses, then use indentation also.
Use spaces instead of tabs, because tab size can vary.
Recommended |
|
---|---|
Not recommended |
|
Line length
Lines should be short enough so that they display without scrolling and can be printed without anything being cut off. If a line is more than 80 characters, it should be split into multiple lines. The additional lines should be indented, but by 2 spaces only, to show that they are a continuation of the previous line.
Keyword justification
In a SELECT
block, line up the keywords FROM
, SAMPLE
, WHERE
, ACCUM
, POST-ACCUM
, HAVING
, GROUP BY
, ORDER BY
, and LIMIT
.
Recommended |
|
||||
---|---|---|---|---|---|
Not recommended |
This is not recommended. |
In GSQL, SELECT statements begin with a vertex set variable.
This means SELECT is not in the leftmost column. If you prefer, you can break the first clause (<vertex set variable> = SELECT <vertex alias>
) into two lines, putting an indented SELECT on the second line:
CREATE QUERY activity_align (INT activity_threshold) FOR GRAPH Social_Net {
SumAccum<INT> @activity_amount;
start = {Person.*};
result =
SELECT v
FROM start:v -(:e)- Post:tgt
ACCUM v.@activity_amount +=1;
PRINT result;
}
Comments
-
Use
//
for single line and inline comments. -
Use
/*
at the start and*/
at the end of multiline comments, with the interior comment lines indented. -
Do not use
#
.
Recommended |
|
||||
---|---|---|---|---|---|
Not recommended |
|
num
to refer to a quantity, always put it at the beginning. For example, use num_students
to refer to the number of students instead of students_num