Hi all,
I'd like to open a discussion on adding DECLARE variable support to Flink
SQL.

*Motivation*

Flink SQL currently supports SET for session-level configuration, but it is
limited to string key-value pairs with no type information. There is no way
to declare a typed variable and reuse it across statements in a session.

*Proposal*
Add a DECLARE statement aligned with ANSI SQL/PSM:

DECLARE variable_name data_type [ DEFAULT expr ];

-- Examples
DECLARE threshold INT DEFAULT 100;
DECLARE event_time TIMESTAMP;


As an initial step, variables would be session-scoped, consistent with how
TableEnvironment manages session state. Future work could extend this to
block-scoped declarations within compound statements (BEGIN/END), following
SQL/PSM semantics.

*Reference*
This follows the ANSI SQL/PSM standard (<SQL variable declaration>), where
data type is required and DEFAULT is optional. Spark SQL introduced a
similar construct via SPARK-48338 (
https://issues.apache.org/jira/browse/SPARK-48338). On the Calcite side,
DECLARE is already reserved as a lexer token but has
no grammar production, so no compatibility issue is expected.

I'm happy to follow up with a FLIP if there is interest.

Best,
Chanhae Oh

Reply via email to