Syntax errors (Groovy)
Groovy syntax errors occur when code violates the language's structural rules, such as missing braces, semicolons, mismatched quotes, or referencing undefined variables. These errors prevent your Deephaven queries from executing and must be fixed before your code can run successfully.
Identifying and fixing syntax errors is a fundamental skill for developing Deephaven Persistent Queries written in Groovy. Within these queries, operations like select, update, updateView, view, and where use formulas that must also follow proper syntax rules.
The key to resolving Groovy syntax errors is understanding how to read a Java exception stack trace. For example, the following code assigns a value to the variable named val but then incorrectly references value:
When executed from a Code Studio or Notebook, the snippet produces the following stack trace:
Each command entered into a console or the script of a Persistent Query is assigned a unique class name by the Groovy interpreter, in this case io.deephaven.dynamic.Script_7. The line number referenced is 4, but the line numbers in Groovy stack traces are inaccurate. In this case, the actual error is on the third line of the script.
Similarly, Groovy can raise syntax errors for problems such as missing braces, as in the example below:
This results in the following stack trace:
Again, the line numbers are shifted by 1, but the stack trace includes a pointer to the ending brace }, which is the unexpected token. To correct this error, the missing brace on line 5 must be added or the brace on line 7 removed. Using an editor with syntax highlighting like the Deephaven Code Studio or IntelliJ IDEA can help identify these kinds of errors.
Formula syntax errors
Deephaven formulas and filters use a Java-like syntax to express operations on table columns, variables from your session, and globally imported functions. As in any programming language, proper syntax is necessary. In the following example, the Groovy syntax is correct, but the closing parenthesis inside the formula is missing:
This results in a stack trace of:
The summary information is on line 1, indicating an error with the formula 2 * 3 ( + 4.
The stack traces indicate where the error occurred, but the relevant details begin on line 47 and continue until line 87:
In this case, the message indicates that the <EOF> or end of file token was encountered at column 10, but another token was expected. Many options are listed, including the correct closing parenthesis on line 61. In practice, with so many options, you must carefully examine the formula around the indicated position to determine what syntax is incorrect.
Invalid operator overloads
You may also have errors related to operators. The Deephaven formula parser converts operators to method calls to permit users to naturally express mathematic operations between various types unsupported by Java (for example, Deephaven permits you to add two BigIntegers together using the plus (+) operator). This is similar to operator overloading in many programming languages. In the example below, the formula attempts to multiply an integer and a string, which is not supported by Deephaven formulas:
The following error is produced:
The relevant detail is that no method is named multiply for int and Strings. The following operators may also be present in your stack trace, corresponding to an operator used in your formula:
| Operator | Method |
|---|---|
+ | plus |
- | minus |
* | multiply |
/ | divide |
% | remainder |
= or == | eq |
< | less |
<= | lessEquals |
> | greater |
>= | greaterEquals |
^ | xor |
| | binaryOr |
^ | binaryAnd |
|| | or |
&& | and |
Invalid use of in
The in keyword can only be used in a standalone expression and cannot be combined with other operators or formula elements (even if surrounded in parenthesis). For example, this is valid syntax:
However, in this example in is combined with another expression:
This results in an error:
When the in operator begins the combined expression, then an alternative error is produced indicating that the expression is not a valid long value: