babelfish-for-postgresql / babelfish_extensions

Babelfish for PostgreSQL provides the capability for PostgreSQL to work with applications written for Microsoft SQL Server. Babelfish understands the SQL Server wire-protocol and T-SQL, the Microsoft SQL Server query and procedural language, so you don’t have to switch database drivers or rewrite all of your application queries.
https://babelfishpg.org/
Apache License 2.0
277 stars 93 forks source link

Make local variable to evaluate dynamically (only if it appears on TargetList) #3139

Open Deepesh125 opened 5 days ago

Deepesh125 commented 5 days ago

Currently, declared local variables are assumed to be static by analyser and hence it is replaced with const node by optimiser. Currently, the parameter value will be supplied through pltsql_param_fetch(..) which is setup during pltsql_estate_setup as part of ParamListInfo setup. This hook is purely used during planning phase and is not used during query execution.

If parameter value is required during execution then values will be read from estate->ndatums through pltsql_param_eval_var callback for EEOP_PARAM_CALLBACK step. This estate->ndatums setup during pltsql_estate_setup and values are copied through copy_pltsql_datums(…). And currently, any assignments to declare variables are implemented using "select target" which will be executed at the end of executor. Hence, any use of variables will read original (value copied during pltsql_estate_setup(...)) and it would not reflect any dynamic change in the value of any variable.

In order to make this behaviour dynamic, this commit introduces internal function sys.pltsql_assign_var(dno, expr). Argument dno is item number of declared variable and expr could be anything whose value will be assigned to declared variable pointed by dno during execution. We will use this function to rewrite any variables assignment operation during ANTLR parsing as stated in following example,

select @a = expr

Will be written to

select @a = sys.pltsql_assign_var(dno, expr)

Internally, sys.pltsql_assign_var invokes exec_assign_value(…) to assign appropriate value directly to variable by updating estate->ndatums array and hence any subsequent read will read latest value.

Engine PR: babelfish-for-postgresql/postgresql_modified_for_babelfish#469 Task: BABEL-5325 Signed-off-by: Dipesh Dhameliya dddhamel@amazon.com

Check List

By submitting this pull request, I confirm that my contribution is under the terms of the Apache 2.0 and PostgreSQL licenses, and grant any person obtaining a copy of the contribution permission to relicense all or a portion of my contribution to the PostgreSQL License solely to contribute all or a portion of my contribution to the PostgreSQL open source project.

For more information on following Developer Certificate of Origin and signing off your commits, please check here.