The eval table function evaluates its argument to a query string, then executes that string as a single SELECT query.
This table function is experimental and disabled by default. Enable it with the allow_experimental_eval_table_function setting:
SET allow_experimental_eval_table_function = 1;It also requires the analyzer (enable_analyzer, enabled by default).
Syntax
eval(expression)
eval(SELECT ...)Arguments
expression— A constant expression that returns a query string. Query parameters and scalar subqueries are allowed.SELECT ...— ASELECTquery that returns a query string. This form is syntactic sugar for a scalar subquery:eval(SELECT ...)is equivalent toeval((SELECT ...)), so the query must return exactly one row and one column.
The value of the expression must have one of these types:
StringNullable(String)LowCardinality(String)LowCardinality(Nullable(String))
If the value is NULL, eval throws an exception.
Returned Value
Returns the result of the generated SELECT query as a table.
The output schema is determined when the eval table function is analyzed, so outer queries can refer to the generated query’s real column names and types.
Examples
Evaluate a constant expression:
SELECT * FROM eval('SEL' || 'ECT 1 AS x');Result:
┌─x─┐
│ 1 │
└───┘Use a query parameter:
SET param_q = 'SELECT 2 AS y';
SELECT * FROM eval({q:String});Result:
┌─y─┐
│ 2 │
└───┘Evaluate an input SELECT query that returns query text:
SELECT * FROM eval(SELECT 'SELECT 3 AS z');Result:
┌─z─┐
│ 3 │
└───┘Use the generated schema in the outer query:
SELECT x + 1 FROM eval('SELECT 4 AS x');Result:
┌─plus(x, 1)─┐
│ 5 │
└────────────┘Restrictions
evalworks only with query analyzer (enable_analyzer = 1).- The generated query must be a single
SELECTquery without output options such asINTO OUTFILEorFORMAT.evaldoes not execute multiple statements. - The generated query must be self-contained: it is resolved in its own scope and cannot reference
WITHaliases or columns of the outer query. - The generated query cannot use the
evaltable function again. evalcannot be used as an argument of another table function, such asremote('host', eval(...)). To execute the generated query on a remote server, wrap it intoview:remote('host', view(SELECT * FROM eval(...))).- The argument is evaluated once while the query is analyzed, not once per row or block.
- The outer query log records the original query that contains
eval; the generatedSELECTquery is not logged as a separate user query.
Related
viewtable functionallow_experimental_eval_table_functionsetting