Skip to content

Commit ffb844e

Browse files
Add ANTLR parse tree caching with version validation
Add cross-session ANTLR parse tree caching for T-SQL stored procedures. Serialized parse trees and datums are stored in babelfish_function_ext using nodeToString/stringToNode. On procedure execution, cached results are restored to skip ANTLR re-parsing. Cache reads validate the stored bbf_version and modify_date before deserializing, skipping stale entries from different Babelfish versions or procedures modified with the GUC disabled. Changes: - Add antlr_parse_tree_text, antlr_parse_tree_datums, antlr_parse_tree_modify_date, and antlr_parse_tree_bbf_version columns to sys.babelfish_function_ext - Store serialized parse tree and version in pltsql_store_func_default_positions - Restore and validate cached parse tree in new function pltsql_restore_func_parse_result invoked prior to ANTLR parse Task: BABEL-6037 Signed-off-by: Manisha Deshpande <mmdeshp@amazon.com>
1 parent 6055b7b commit ffb844e

File tree

18 files changed

+489
-3822
lines changed

18 files changed

+489
-3822
lines changed

contrib/babelfishpg_tsql/Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ MODULE_big = $(EXTENSION)
2121
OBJS = src/pl_gram.o src/pl_handler.o src/pl_comp.o src/pl_exec.o
2222
OBJS += src/pl_funcs.o src/pl_scanner.o $(WIN32RES)
2323
OBJS += src/pl_comp-2.o
24-
OBJS += src/pltsql_serialize.o
25-
OBJS += src/pltsql_deserialize.o
2624
OBJS += src/pl_explain.o
2725
OBJS += src/properties.o
2826
OBJS += src/databasepropertyex.o

contrib/babelfishpg_tsql/sql/ownership.sql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ CREATE TABLE sys.babelfish_function_ext (
4141
create_date SYS.DATETIME NOT NULL,
4242
modify_date SYS.DATETIME NOT NULL,
4343
definition sys.NTEXT DEFAULT NULL,
44-
antlr_parse_tree JSONB DEFAULT NULL, -- JSONB serialized ANTLR parse tree for caching
44+
antlr_parse_tree_text TEXT DEFAULT NULL, -- Native PG nodeToString() serialized parse tree [TODO: convert to BINARY TEXT]
45+
antlr_parse_tree_datums TEXT DEFAULT NULL, -- Native PG nodeToString() serialized datums array [TODO: convert to BINARY TEXT]
46+
antlr_parse_tree_modify_date SYS.DATETIME DEFAULT NULL,
47+
antlr_parse_tree_bbf_version TEXT DEFAULT NULL,
4548
PRIMARY KEY(funcname, nspname, funcsignature)
4649
);
4750
GRANT SELECT ON sys.babelfish_function_ext TO PUBLIC;

contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--5.5.0--5.6.0.sql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,10 @@ CREATE TABLE sys.babelfish_function_ext (
245245
create_date SYS.DATETIME NOT NULL,
246246
modify_date SYS.DATETIME NOT NULL,
247247
definition sys.NTEXT DEFAULT NULL,
248-
antlr_parse_tree JSONB DEFAULT NULL, -- JSONB serialized ANTLR parse tree for caching
248+
antlr_parse_tree_text TEXT DEFAULT NULL, -- Native PG nodeToString() serialized parse tree
249+
antlr_parse_tree_datums TEXT DEFAULT NULL, -- Native PG nodeToString() serialized datums array
250+
antlr_parse_tree_modify_date SYS.DATETIME DEFAULT NULL,
251+
antlr_parse_tree_bbf_version TEXT DEFAULT NULL,
249252
PRIMARY KEY(funcname, nspname, funcsignature)
250253
);
251254
GRANT SELECT ON sys.babelfish_function_ext TO PUBLIC;

contrib/babelfishpg_tsql/src/catalog.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,11 @@ typedef FormData_bbf_servers_def *Form_bbf_servers_def;
277277
#define Anum_bbf_function_ext_create_date 8
278278
#define Anum_bbf_function_ext_modify_date 9
279279
#define Anum_bbf_function_ext_definition 10
280-
#define Anum_bbf_function_ext_antlr_parse_tree 11
281-
#define BBF_FUNCTION_EXT_NUM_COLS 11
280+
#define Anum_bbf_function_ext_antlr_parse_tree_text 11
281+
#define Anum_bbf_function_ext_antlr_parse_tree_datums 12
282+
#define Anum_bbf_function_ext_antlr_parse_tree_modify_date 13
283+
#define Anum_bbf_function_ext_antlr_parse_tree_bbf_version 14
284+
#define BBF_FUNCTION_EXT_NUM_COLS 14
282285
#define FLAG_IS_ANSI_NULLS_ON (1<<0)
283286
#define FLAG_USES_QUOTED_IDENTIFIER (1<<1)
284287
#define FLAG_CREATED_WITH_RECOMPILE (1<<2)

contrib/babelfishpg_tsql/src/codegen.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ create_goto(int lineno)
147147
{
148148
PLtsql_stmt_goto *stmt_goto;
149149

150-
stmt_goto = palloc(sizeof(PLtsql_stmt_goto));
150+
stmt_goto = makeNode(PLtsql_stmt_goto);
151151
stmt_goto->cmd_type = PLTSQL_STMT_GOTO;
152152
stmt_goto->lineno = lineno;
153153
stmt_goto->cond = NULL; /* unconditional goto */
@@ -159,7 +159,7 @@ create_goto(int lineno)
159159
static PLtsql_stmt_save_ctx *
160160
create_save_ctx(int lineno)
161161
{
162-
PLtsql_stmt_save_ctx *save_ctx = palloc(sizeof(PLtsql_stmt_save_ctx));
162+
PLtsql_stmt_save_ctx *save_ctx = makeNode(PLtsql_stmt_save_ctx);
163163

164164
save_ctx->cmd_type = PLTSQL_STMT_SAVE_CTX;
165165
save_ctx->lineno = lineno;
@@ -171,7 +171,7 @@ create_save_ctx(int lineno)
171171
static PLtsql_stmt_restore_ctx_full *
172172
create_restore_ctx_full(int lineno)
173173
{
174-
PLtsql_stmt_restore_ctx_full *restore_ctx = palloc(sizeof(PLtsql_stmt_restore_ctx_full));
174+
PLtsql_stmt_restore_ctx_full *restore_ctx = makeNode(PLtsql_stmt_restore_ctx_full);
175175

176176
restore_ctx->cmd_type = PLTSQL_STMT_RESTORE_CTX_FULL;
177177
restore_ctx->lineno = lineno;
@@ -181,7 +181,7 @@ create_restore_ctx_full(int lineno)
181181
static PLtsql_stmt_restore_ctx_partial *
182182
create_restore_ctx_partial(int lineno)
183183
{
184-
PLtsql_stmt_restore_ctx_partial *restore_ctx = palloc(sizeof(PLtsql_stmt_restore_ctx_partial));
184+
PLtsql_stmt_restore_ctx_partial *restore_ctx = makeNode(PLtsql_stmt_restore_ctx_partial);
185185

186186
restore_ctx->cmd_type = PLTSQL_STMT_RESTORE_CTX_PARTIAL;
187187
restore_ctx->lineno = lineno;

0 commit comments

Comments
 (0)