Tengri’s own syntax

Tengri implements its own syntactic constructs and keywords for easy multilingual (SQL and Python) operations in a single environment.

Keyword cell_output

The cell_output keyword is used in cells of type Python to refer to the result of the previous cell of type SQL. With this keyword it is convenient to switch from SQL to Python when working with data inside one notebook.

Data in cell_output is written as Pandas DataFrame.

The cell_output keyword is interpreted on a per-cell basis, not for the entire notebook. This means that the values written to cell_output may be different in different cells of type Python within the same notebook depending on the previous cells of type SQL. Therefore, it is recommended that the contents of the cell_output variable be written to a new variable if you intend to work with this data in multiple Python cells (as shown in the example below).

Example

Let’s run a cell of type SQL with a SELECT query to output numbers from 1 to 5. For this purpose we will use the functions unnest and generate_series:

SELECT unnest(generate_series(1,5)) as numbers;
+---------+
| numbers |
+---------+
| 1       |
+---------+
| 2       |
+---------+
| 3       |
+---------+
| 4       |
+---------+
| 5       |
+---------+

In the next cell of type Python of the same notebook, we refer to the table output in the previous cell via the cell_output keyword:

# Print the value of the cell_output variable
print(cell_output)
print()

# Print the type of the cell_output variable
print(type(cell_output))
print()

# Write the value of the cell_output variable to a new variable
df = cell_output

# Filter values in DataFrame by column condition using the new variable
print(df.loc[df['numbers'] > 2])
numbers
0        1
1        2
2        3
3        4
4        5

<class 'pandas.core.frame.DataFrame'>

   numbers
2        3
3        4
4        5

We can see that the content of the cell_output variable in this cell is a DataFrame with data taken from the result of the previous cell. You can work with this DataFrame using any standard Python tools.


Real examples of using the cell_output keyword can be seen in the scripts:

Cross-language variables

Tengri implements a special syntax to uniformly set variables in SQL and in Python. These variables are set at the notebook level and can be used in any cells regardless of the language.

Variable values are set through the GUI in the Variables tab. Variable names appear in the list in this tab the moment a construct with them is written to a code cell. To create a variable, you simply write it in the code — no special actions are required to initialise it.

Variables can be used in code in two ways: as string values and as raw values.

String values

Syntax for SQL and Python:

{@ variable_name @}

Raw values

Syntax SQL and Python:

{@ raw variable_name @}

Specifying the default value

If necessary, you can specify the default value that a variable will take if you leave the field for the value of this variable in the Variables tab (Variables) empty.

Syntax SQL and Python:

{@ variable_name | default_value @}
{@ raw variable_name | default_value @}

Examples

Let’s write the value my_value into the variable my_var through the GUI.

Let’s run the code in a cell of type Python, using string values:

print({@ my_var @})
my_value

Let’s run the code in a cell of type SQL, using string values:

SELECT {@ my_var @} AS my_column
+-----------+
| my_column |
+-----------+
| my_value  |
+-----------+

Let’s run the code in a cell of type Python, using raw values:

print({@ raw my_var @}) # Error expected
NameError: name 'my_value' is not defined
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[12], line 1
----> 1 print(my_value)
NameError: name 'my_value' is not defined

Expectedly, we get the NameError error because the variable named my_value has not been set.


Let’s set this variable and print its value:

{@ raw my_var @} = 2025

print({@ raw my_var @})
2025

Let’s run the code in a cell of type SQL, using raw values:

SELECT {@ raw my_var @} AS my_column -- Error expected
ERROR: BinderException: Binder Error: Referenced column "my_value" not found in FROM clause!

Expectedly, we get a BinderException error, because the query has a my_value column, but it is not in the table where the SELECT is made from.


Let’s run the code in a cell of type SQL, using raw values, but so that the raw variable value specifies the column name:

SELECT 2025 AS {@ raw my_var @}
+----------+
| my_value |
+----------+
| 2025     |
+----------+

Let’s leave the field for the variable value empty and run the code in a cell of type SQL:

SELECT {@ my_var | my_default_value @} AS my_column
+------------------+
| my_column        |
+------------------+
| my_default_value |
+------------------+

The use of cross-language variables is also possible in the case of string interpolation in Python:

print(f'my_var: {{@ my_var @}}')

print('my_var: {}'.format({@ my_var @}))
my_var: my_value
my_var: my_value