Parse/analyze function renaming

There are three parallel ways to call parse/analyze: with fixed
parameters, with variable parameters, and by supplying your own parser
callback.  Some of the involved functions were confusingly named and
made this API structure more confusing.  This patch renames some
functions to make this clearer:

parse_analyze() -> parse_analyze_fixedparams()
pg_analyze_and_rewrite() -> pg_analyze_and_rewrite_fixedparams()

(Otherwise one might think this variant doesn't accept parameters, but
in fact all three ways accept parameters.)

pg_analyze_and_rewrite_params() -> pg_analyze_and_rewrite_withcb()

(Before, and also when considering pg_analyze_and_rewrite(), one might
think this is the only way to pass parameters.  Moreover, the parser
callback doesn't necessarily need to parse only parameters, it's just
one of the things it could do.)

parse_fixed_parameters() -> setup_parse_fixed_parameters()
parse_variable_parameters() -> setup_parse_variable_parameters()

(These functions don't actually do any parsing, they just set up
callbacks to use during parsing later.)

This patch also adds some const decorations to the fixed-parameters
API, so the distinction from the variable-parameters API is more
clear.

Reviewed-by: Nathan Bossart <bossartn@amazon.com>
Discussion: https://www.postgresql.org/message-id/flat/c67ce276-52b4-0239-dc0e-39875bf81840@enterprisedb.com
This commit is contained in:
Peter Eisentraut 2022-03-04 14:49:37 +01:00
parent d816f366bc
commit 791b1b71da
17 changed files with 41 additions and 40 deletions

View File

@ -947,7 +947,7 @@ fmgr_sql_validator(PG_FUNCTION_ARGS)
RawStmt *parsetree = lfirst_node(RawStmt, lc);
List *querytree_sublist;
querytree_sublist = pg_analyze_and_rewrite_params(parsetree,
querytree_sublist = pg_analyze_and_rewrite_withcb(parsetree,
prosrc,
(ParserSetupHook) sql_fn_parser_setup,
pinfo,

View File

@ -439,7 +439,7 @@ BeginCopyTo(ParseState *pstate,
* Run parse analysis and rewrite. Note this also acquires sufficient
* locks on the source table(s).
*/
rewritten = pg_analyze_and_rewrite(raw_query,
rewritten = pg_analyze_and_rewrite_fixedparams(raw_query,
pstate->p_sourcetext, NULL, 0,
NULL);

View File

@ -757,7 +757,7 @@ execute_sql_string(const char *sql)
/* Be sure parser can see any DDL done so far */
CommandCounterIncrement();
stmt_list = pg_analyze_and_rewrite(parsetree,
stmt_list = pg_analyze_and_rewrite_fixedparams(parsetree,
sql,
NULL,
0,

View File

@ -172,7 +172,7 @@ CreateSchemaCommand(CreateSchemaStmt *stmt, const char *queryString,
/*
* Execute each command contained in the CREATE SCHEMA. Since the grammar
* allows only utility commands in CREATE SCHEMA, there is no need to pass
* them through parse_analyze() or the rewriter; we can just hand them
* them through parse_analyze_*() or the rewriter; we can just hand them
* straight to ProcessUtility.
*/
foreach(parsetree_item, parsetree_list)

View File

@ -13181,7 +13181,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd,
/*
* We expect that we will get only ALTER TABLE and CREATE INDEX
* statements. Hence, there is no need to pass them through
* parse_analyze() or the rewriter, but instead we need to pass them
* parse_analyze_*() or the rewriter, but instead we need to pass them
* through parse_utilcmd.c to make them ready for execution.
*/
raw_parsetree_list = raw_parser(cmd, RAW_PARSE_DEFAULT);

View File

@ -439,7 +439,7 @@ DefineView(ViewStmt *stmt, const char *queryString,
rawstmt->stmt_location = stmt_location;
rawstmt->stmt_len = stmt_len;
viewParse = parse_analyze(rawstmt, queryString, NULL, 0, NULL);
viewParse = parse_analyze_fixedparams(rawstmt, queryString, NULL, 0, NULL);
/*
* The grammar should ensure that the result is a single SELECT Query.

View File

@ -718,7 +718,7 @@ init_sql_fcache(FunctionCallInfo fcinfo, Oid collation, bool lazyEvalOK)
RawStmt *parsetree = lfirst_node(RawStmt, lc);
List *queryTree_sublist;
queryTree_sublist = pg_analyze_and_rewrite_params(parsetree,
queryTree_sublist = pg_analyze_and_rewrite_withcb(parsetree,
fcache->src,
(ParserSetupHook) sql_fn_parser_setup,
fcache->pinfo,

View File

@ -2258,7 +2258,7 @@ _SPI_prepare_plan(const char *src, SPIPlanPtr plan)
if (plan->parserSetup != NULL)
{
Assert(plan->nargs == 0);
stmt_list = pg_analyze_and_rewrite_params(parsetree,
stmt_list = pg_analyze_and_rewrite_withcb(parsetree,
src,
plan->parserSetup,
plan->parserSetupArg,
@ -2266,7 +2266,7 @@ _SPI_prepare_plan(const char *src, SPIPlanPtr plan)
}
else
{
stmt_list = pg_analyze_and_rewrite(parsetree,
stmt_list = pg_analyze_and_rewrite_fixedparams(parsetree,
src,
plan->argtypes,
plan->nargs,
@ -2495,7 +2495,7 @@ _SPI_execute_plan(SPIPlanPtr plan, const SPIExecuteOptions *options,
else if (plan->parserSetup != NULL)
{
Assert(plan->nargs == 0);
stmt_list = pg_analyze_and_rewrite_params(parsetree,
stmt_list = pg_analyze_and_rewrite_withcb(parsetree,
src,
plan->parserSetup,
plan->parserSetupArg,
@ -2503,7 +2503,7 @@ _SPI_execute_plan(SPIPlanPtr plan, const SPIExecuteOptions *options,
}
else
{
stmt_list = pg_analyze_and_rewrite(parsetree,
stmt_list = pg_analyze_and_rewrite_fixedparams(parsetree,
src,
plan->argtypes,
plan->nargs,

View File

@ -5057,7 +5057,7 @@ inline_set_returning_function(PlannerInfo *root, RangeTblEntry *rte)
if (list_length(raw_parsetree_list) != 1)
goto fail;
querytree_list = pg_analyze_and_rewrite_params(linitial(raw_parsetree_list),
querytree_list = pg_analyze_and_rewrite_withcb(linitial(raw_parsetree_list),
src,
(ParserSetupHook) sql_fn_parser_setup,
pinfo, NULL);

View File

@ -96,7 +96,7 @@ static bool test_raw_expression_coverage(Node *node, void *context);
/*
* parse_analyze
* parse_analyze_fixedparams
* Analyze a raw parse tree and transform it to Query form.
*
* Optionally, information about $n parameter types can be supplied.
@ -107,8 +107,8 @@ static bool test_raw_expression_coverage(Node *node, void *context);
* a dummy CMD_UTILITY Query node.
*/
Query *
parse_analyze(RawStmt *parseTree, const char *sourceText,
Oid *paramTypes, int numParams,
parse_analyze_fixedparams(RawStmt *parseTree, const char *sourceText,
const Oid *paramTypes, int numParams,
QueryEnvironment *queryEnv)
{
ParseState *pstate = make_parsestate(NULL);
@ -120,7 +120,7 @@ parse_analyze(RawStmt *parseTree, const char *sourceText,
pstate->p_sourcetext = sourceText;
if (numParams > 0)
parse_fixed_parameters(pstate, paramTypes, numParams);
setup_parse_fixed_parameters(pstate, paramTypes, numParams);
pstate->p_queryEnv = queryEnv;
@ -158,7 +158,7 @@ parse_analyze_varparams(RawStmt *parseTree, const char *sourceText,
pstate->p_sourcetext = sourceText;
parse_variable_parameters(pstate, paramTypes, numParams);
setup_parse_variable_parameters(pstate, paramTypes, numParams);
query = transformTopLevelStmt(pstate, parseTree);

View File

@ -35,7 +35,7 @@
typedef struct FixedParamState
{
Oid *paramTypes; /* array of parameter type OIDs */
const Oid *paramTypes; /* array of parameter type OIDs */
int numParams; /* number of array entries */
} FixedParamState;
@ -64,8 +64,8 @@ static bool query_contains_extern_params_walker(Node *node, void *context);
* Set up to process a query containing references to fixed parameters.
*/
void
parse_fixed_parameters(ParseState *pstate,
Oid *paramTypes, int numParams)
setup_parse_fixed_parameters(ParseState *pstate,
const Oid *paramTypes, int numParams)
{
FixedParamState *parstate = palloc(sizeof(FixedParamState));
@ -80,7 +80,7 @@ parse_fixed_parameters(ParseState *pstate,
* Set up to process a query containing references to variable parameters.
*/
void
parse_variable_parameters(ParseState *pstate,
setup_parse_variable_parameters(ParseState *pstate,
Oid **paramTypes, int *numParams)
{
VarParamState *parstate = palloc(sizeof(VarParamState));

View File

@ -3,7 +3,7 @@
* parse_utilcmd.c
* Perform parse analysis work for various utility commands
*
* Formerly we did this work during parse_analyze() in analyze.c. However
* Formerly we did this work during parse_analyze_*() in analyze.c. However
* that is fairly unsafe in the presence of querytree caching, since any
* database state that we depend on in making the transformations might be
* obsolete by the time the utility command is executed; and utility commands

View File

@ -637,8 +637,8 @@ pg_parse_query(const char *query_string)
* NOTE: for reasons mentioned above, this must be separate from raw parsing.
*/
List *
pg_analyze_and_rewrite(RawStmt *parsetree, const char *query_string,
Oid *paramTypes, int numParams,
pg_analyze_and_rewrite_fixedparams(RawStmt *parsetree, const char *query_string,
const Oid *paramTypes, int numParams,
QueryEnvironment *queryEnv)
{
Query *query;
@ -652,7 +652,7 @@ pg_analyze_and_rewrite(RawStmt *parsetree, const char *query_string,
if (log_parser_stats)
ResetUsage();
query = parse_analyze(parsetree, query_string, paramTypes, numParams,
query = parse_analyze_fixedparams(parsetree, query_string, paramTypes, numParams,
queryEnv);
if (log_parser_stats)
@ -669,12 +669,13 @@ pg_analyze_and_rewrite(RawStmt *parsetree, const char *query_string,
}
/*
* Do parse analysis and rewriting. This is the same as pg_analyze_and_rewrite
* except that external-parameter resolution is determined by parser callback
* hooks instead of a fixed list of parameter datatypes.
* Do parse analysis and rewriting. This is the same as
* pg_analyze_and_rewrite_fixedparams except that, instead of a fixed list of
* parameter datatypes, a parser callback is supplied that can do
* external-parameter resolution and possibly other things.
*/
List *
pg_analyze_and_rewrite_params(RawStmt *parsetree,
pg_analyze_and_rewrite_withcb(RawStmt *parsetree,
const char *query_string,
ParserSetupHook parserSetup,
void *parserSetupArg,
@ -1125,7 +1126,7 @@ exec_simple_query(const char *query_string)
else
oldcontext = MemoryContextSwitchTo(MessageContext);
querytree_list = pg_analyze_and_rewrite(parsetree, query_string,
querytree_list = pg_analyze_and_rewrite_fixedparams(parsetree, query_string,
NULL, 0, NULL);
plantree_list = pg_plan_queries(querytree_list, query_string,

View File

@ -682,13 +682,13 @@ RevalidateCachedQuery(CachedPlanSource *plansource,
if (rawtree == NULL)
tlist = NIL;
else if (plansource->parserSetup != NULL)
tlist = pg_analyze_and_rewrite_params(rawtree,
tlist = pg_analyze_and_rewrite_withcb(rawtree,
plansource->query_string,
plansource->parserSetup,
plansource->parserSetupArg,
queryEnv);
else
tlist = pg_analyze_and_rewrite(rawtree,
tlist = pg_analyze_and_rewrite_fixedparams(rawtree,
plansource->query_string,
plansource->param_types,
plansource->num_params,

View File

@ -24,8 +24,8 @@ typedef void (*post_parse_analyze_hook_type) (ParseState *pstate,
extern PGDLLIMPORT post_parse_analyze_hook_type post_parse_analyze_hook;
extern Query *parse_analyze(RawStmt *parseTree, const char *sourceText,
Oid *paramTypes, int numParams, QueryEnvironment *queryEnv);
extern Query *parse_analyze_fixedparams(RawStmt *parseTree, const char *sourceText,
const Oid *paramTypes, int numParams, QueryEnvironment *queryEnv);
extern Query *parse_analyze_varparams(RawStmt *parseTree, const char *sourceText,
Oid **paramTypes, int *numParams);

View File

@ -15,9 +15,9 @@
#include "parser/parse_node.h"
extern void parse_fixed_parameters(ParseState *pstate,
Oid *paramTypes, int numParams);
extern void parse_variable_parameters(ParseState *pstate,
extern void setup_parse_fixed_parameters(ParseState *pstate,
const Oid *paramTypes, int numParams);
extern void setup_parse_variable_parameters(ParseState *pstate,
Oid **paramTypes, int *numParams);
extern void check_variable_parameters(ParseState *pstate, Query *query);
extern bool query_contains_extern_params(Query *query);

View File

@ -45,11 +45,11 @@ extern PGDLLIMPORT int log_statement;
extern List *pg_parse_query(const char *query_string);
extern List *pg_rewrite_query(Query *query);
extern List *pg_analyze_and_rewrite(RawStmt *parsetree,
extern List *pg_analyze_and_rewrite_fixedparams(RawStmt *parsetree,
const char *query_string,
Oid *paramTypes, int numParams,
const Oid *paramTypes, int numParams,
QueryEnvironment *queryEnv);
extern List *pg_analyze_and_rewrite_params(RawStmt *parsetree,
extern List *pg_analyze_and_rewrite_withcb(RawStmt *parsetree,
const char *query_string,
ParserSetupHook parserSetup,
void *parserSetupArg,