Skip to content

Commit ce7523a

Browse files
authored
added new grammer, parse node and hook for top N Percent (#624)
Description This PR implements comprehensive support for SELECT TOP N PERCENT syntax in Babelfish, addressing JIRA-BABEL-1358. The implementation adds percentage-based row limiting functionality to align with SQL Server behavior. Problem Babelfish previously only supported SELECT TOP N (fixed count) but lacked support for SELECT TOP N PERCENT (percentage-based limiting), which is a commonly used SQL Server feature for selecting a percentage of rows from result sets. Related PR - Extension Link - babelfish-for-postgresql/babelfish_extensions#4111 Issue: BABEL-1358 Signed-off-by: Herambh Shah <herambhs@amazon.com>
1 parent c7667f4 commit ce7523a

3 files changed

Lines changed: 10 additions & 7 deletions

File tree

src/backend/parser/analyze.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,9 @@ pre_transform_setop_sort_clause_hook_type pre_transform_setop_sort_clause_hook =
9292
/* Hook to transform TSQL pivot clause in select stmt */
9393
transform_pivot_clause_hook_type transform_pivot_clause_hook = NULL;
9494

95-
/* Hook to transform TSQL unpivot clauses in select stmt */
96-
transform_unpivot_clause_hook_type transform_unpivot_clause_hook = NULL;
95+
/* Hook to transform TSQL unpivot, top N percent clauses in select stmt */
96+
transform_tsql_select_stmt_hook_type transform_tsql_select_stmt_hook = NULL;
97+
9798

9899
static Query *transformOptionalSelectInto(ParseState *pstate, Node *parseTree);
99100
static Query *transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt);
@@ -1442,10 +1443,10 @@ transformSelectStmt(ParseState *pstate, SelectStmt *stmt)
14421443

14431444
qry->commandType = CMD_SELECT;
14441445

1445-
/* Unpack and process TSQL UNPIVOT nodes in stmt->fromClause, if present */
1446-
if(transform_unpivot_clause_hook)
1446+
/* Unpack and process TSQL UNPIVOT nodes in stmt->fromClause, Process Top N Percent, if present */
1447+
if(transform_tsql_select_stmt_hook)
14471448
{
1448-
(*transform_unpivot_clause_hook)(pstate, stmt);
1449+
(*transform_tsql_select_stmt_hook)(pstate, stmt);
14491450
}
14501451

14511452
/* process the WITH clause independently of all else */

src/include/nodes/nodes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ typedef enum LimitOption
441441
LIMIT_OPTION_COUNT, /* FETCH FIRST... ONLY */
442442
LIMIT_OPTION_WITH_TIES, /* FETCH FIRST... WITH TIES */
443443
LIMIT_OPTION_DEFAULT, /* No limit present */
444+
LIMIT_OPTION_PERCENT, /* FETCH FIRST... PERCENT FOR TSQL */
444445
} LimitOption;
445446

446447
#endif /* NODES_H */

src/include/parser/analyze.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ typedef void (*transform_pivot_clause_hook_type)(ParseState *pstate, SelectStmt
6969
extern PGDLLEXPORT transform_pivot_clause_hook_type transform_pivot_clause_hook;
7070

7171
/* Hook for transform unpivot clause in tsql select stmt */
72-
typedef void (*transform_unpivot_clause_hook_type)(ParseState *pstate, SelectStmt *stmt);
73-
extern PGDLLEXPORT transform_unpivot_clause_hook_type transform_unpivot_clause_hook;
72+
typedef void (*transform_tsql_select_stmt_hook_type)(ParseState *pstate, SelectStmt *stmt);
73+
extern PGDLLEXPORT transform_tsql_select_stmt_hook_type transform_tsql_select_stmt_hook;
74+
7475

7576
extern Query *parse_analyze_fixedparams(RawStmt *parseTree, const char *sourceText,
7677
const Oid *paramTypes, int numParams, QueryEnvironment *queryEnv);

0 commit comments

Comments
 (0)