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 initialize 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 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  |
+-----------+
variables EN
An example of using variables in the Tengri interface

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