Revert "Collect and return query substrings corresponding to each SQL statement"
authorPavan Deolasee <[email protected]>
Wed, 14 Jun 2017 06:12:23 +0000 (11:42 +0530)
committerPavan Deolasee <[email protected]>
Wed, 14 Jun 2017 06:12:23 +0000 (11:42 +0530)
This reverts commit 455ff923454e78d80b77639a381db9b05c776577. Core Postgres has
now added support for extracting query string for each command in a
multi-command SQL. So we can use that facility instead of cooking up something
on our own.

src/backend/commands/tablecmds.c
src/backend/parser/gram.y
src/backend/parser/parse_type.c
src/backend/parser/parser.c
src/backend/parser/scan.l
src/backend/tcop/postgres.c
src/include/parser/gramparse.h
src/include/parser/parser.h
src/include/parser/scanner.h
src/include/tcop/tcopprot.h
src/pl/plpgsql/src/pl_gram.y

index 5be449648ce1107d4371398a30922746e537298c..78ab65f60089b82c91088f155e9026064d663340 100644 (file)
@@ -9813,7 +9813,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd,
         * 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, NULL);
+       raw_parsetree_list = raw_parser(cmd);
        querytree_list = NIL;
        foreach(list_item, raw_parsetree_list)
        {
index 7fa2f21e3f4f71f4362754ca21abf24cb99c90d3..739ff10b07ae684c7cdcea0a9bfa6cd40a6c746c 100644 (file)
@@ -130,14 +130,6 @@ typedef struct ImportQual
        List       *table_names;
 } ImportQual;
 
-typedef struct StmtMulti
-{
-       List    *parsetrees;
-       List    *queries;
-       int             offset;
-       char    *lastQuery;
-} StmtMulti;
-
 /* ConstraintAttributeSpec yields an integer bitmask of these flags: */
 #define CAS_NOT_DEFERRABLE                     0x01
 #define CAS_DEFERRABLE                         0x02
@@ -250,7 +242,6 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
        InsertStmt                      *istmt;
        VariableSetStmt         *vsetstmt;
 /* PGXC_BEGIN */
-       struct StmtMulti                        *stmtmulti;
        DistributeBy            *distby;
        PGXCSubCluster          *subclus;
 /* PGXC_END */
@@ -388,8 +379,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
 %type <ival>   import_qualification_type
 %type <importqual> import_qualification
 
-%type <stmtmulti> stmtmulti
-%type <list>   stmtblock
+%type <list>   stmtblock stmtmulti
                                OptTableElementList TableElementList OptInherit definition
                                OptTypedTableElementList TypedTableElementList
                                reloptions opt_reloptions
@@ -806,8 +796,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
  */
 stmtblock:     stmtmulti
                        {
-                               pg_yyget_extra(yyscanner)->parsetree = $1 ? $1->parsetrees : NIL;
-                               pg_yyget_extra(yyscanner)->queries = $1 ? $1->queries : NIL;
+                               pg_yyget_extra(yyscanner)->parsetree = $1;
                        }
                ;
 
@@ -823,62 +812,11 @@ stmtblock:        stmtmulti
  */
 stmtmulti:     stmtmulti ';' stmt
                                {
-                                       /* 
-                                        * XXX PG10MERGE: Looks like support for obtaining raw
-                                        * query string for individual commands is added in PG10.
-                                        * If so, we can make use of the same infrastructure.
-                                        *
-                                        * XXX The following gives a compilation WARNING because
-                                        * stmtmulti is defined as a List in PG10, but we have our
-                                        * own definition.
-                                        */
                                        if ($1 != NIL)
                                        {
                                                /* update length of previous stmt */
                                                updateRawStmtEnd(llast_node(RawStmt, $1), @2);
                                        }
-                                       if ($3 != NULL)
-                                       {
-                                               char *query;
-                                               ListCell *last;
-                                               /*
-                                                * Because of the way multi-commands are parsed by the
-                                                * parser, when the earlier command was parsed and
-                                                * reduced to a 'stmtmulti', we did not have the
-                                                * end-of-the-query marker. But now that we have seen
-                                                * the ';' token, add '\0' at the corresponding offset
-                                                * to get a separated command.
-                                                */
-                                               if ($1 != NULL)
-                                               {
-                                                       last = list_tail($1->queries);
-                                                       query = palloc(@2 - $1->offset + 1);
-                                                       memcpy(query, lfirst(last), @2 - $1->offset);
-                                                       query[@2 - $1->offset] = '\0';
-                                                       lfirst(last) = query;
-
-                                                       query = scanner_get_query(@3, -1, yyscanner);
-                                                       $1->offset = @2;
-                                                       $1->parsetrees = lappend($1->parsetrees, $3);
-                                                       $1->queries = lappend($1->queries, query);
-                                                       $$ = $1;
-                                               }
-                                               /*
-                                                *
-                                                * If the earlier statements were all null, then we
-                                                * must initialise the StmtMulti structure and make
-                                                * singleton lists
-                                                */
-                                               else
-                                               {
-                                                       StmtMulti *n = (StmtMulti *) palloc0(sizeof (StmtMulti));
-                                                       query = scanner_get_query(@3, -1, yyscanner);
-                                                       n->offset = @2;
-                                                       n->parsetrees = list_make1($3);
-                                                       n->queries = list_make1(query);
-                                                       $$ = n;
-                                               }
-                                       }
                                        if ($3 != NULL)
                                                $$ = lappend($1, makeRawStmt($3, @2 + 1));
                                        else
@@ -886,37 +824,10 @@ stmtmulti:        stmtmulti ';' stmt
                                }
                        | stmt
                                {
-                                       if ($1 != NULL)
-                                       {
-                                               StmtMulti *n = (StmtMulti *) palloc0(sizeof (StmtMulti));
-                                               char *query = scanner_get_query(@1, -1, yyscanner);
-
-                                               /*
-                                                * Keep track of the offset where $1 started. We don't
-                                                * have the offset where it ends so we copy the entire
-                                                * query to the end. If later, we find a ';' followed
-                                                * by another command, we'll add the '\0' at the
-                                                * appropriate offset
-                                                *
-                                                * XXX May be there is a better way to get the matching  
-                                                * portion of the query string, but this does the trick
-                                                * for regression as well as the problem we are trying
-                                                * to solve with multi-command queries
-                                                */
-                                               n->offset = @1;
-
-                                               /*
-                                                * Collect both parsetree as well as the original query
-                                                * that resulted in the parsetree
-                                                */
-                                               n->parsetrees = list_make1($1);
-                                               n->queries = list_make1(query);
-                                               $$ = n;
-                                       }
                                        if ($1 != NULL)
                                                $$ = list_make1(makeRawStmt($1, 0));
                                        else
-                                               $$ = NULL;
+                                               $$ = NIL;
                                }
                ;
 
index a723ea15ad64ab372763988ad6564261b0e4f7b0..b71b17bd2a6c7c4b33e135352bfcb8c5e69ce546 100644 (file)
@@ -709,7 +709,7 @@ typeStringToTypeName(const char *str)
        ptserrcontext.previous = error_context_stack;
        error_context_stack = &ptserrcontext;
 
-       raw_parsetree_list = raw_parser(buf.data, NULL);
+       raw_parsetree_list = raw_parser(buf.data);
 
        error_context_stack = ptserrcontext.previous;
 
index 522d7ec2035bdebd86a4768ff19530780b679c2c..245b4cda3b9b3a4ebe585cad639f5c1fe3b62d02 100644 (file)
@@ -33,7 +33,7 @@
  * of the list are always RawStmt nodes.
  */
 List *
-raw_parser(const char *str, List **queries)
+raw_parser(const char *str)
 {
        core_yyscan_t yyscanner;
        base_yy_extra_type yyextra;
@@ -58,9 +58,6 @@ raw_parser(const char *str, List **queries)
        if (yyresult)                           /* error */
                return NIL;
 
-       if (queries)
-               *queries = yyextra.queries;
-
        return yyextra.parsetree;
 }
 
index 83a73410b831d4acd5f9a8b213c356b30c81eba6..634bfa512f4fff37e3010dfedc68fd5e49fb6b3b 100644 (file)
@@ -1108,10 +1108,6 @@ scanner_init(const char *str,
        yyext->keywords = keywords;
        yyext->num_keywords = num_keywords;
 
-#ifdef XCP
-       yyext->query = pstrdup(str);
-#endif
-
        yyext->backslash_quote = backslash_quote;
        yyext->escape_string_warning = escape_string_warning;
        yyext->standard_conforming_strings = standard_conforming_strings;
@@ -1546,33 +1542,3 @@ core_yyfree(void *ptr, core_yyscan_t yyscanner)
        if (ptr)
                pfree(ptr);
 }
-
-/*
- * Return a copy of a substring of the original query string, starting at
- * 'start' offset and 'len' bytes long, Be mindful of the invalid arguments
- * being passed by the caller
- */
-char *
-scanner_get_query(int start, int len, core_yyscan_t yyscanner)
-{
-       char *query;
-
-       /*
-        * If the caller passes a wrong offset, just assume 0
-        */
-       if (start == -1)
-               start = 0; 
-       if (start > strlen(yyextra->query))
-               return NULL;
-       /*
-        * Similarly, if the passed-in length is more than remaining
-        * bytes in the string, just return whatever is available
-        */
-       if (len == -1)
-               len = strlen(yyextra->query) - start;
-       else if (len + start > strlen(yyextra->query))
-               return NULL;
-
-       query = yyextra->query + start;
-       return query; 
-}
index a4f4884372813825856cd83394824e92bf8875b8..b10e0e44d9361a50343e2be7d4bdbf7ce93c06ca 100644 (file)
@@ -742,8 +742,8 @@ ProcessClientWriteInterrupt(bool blocked)
  * we've seen a COMMIT or ABORT command; when we are in abort state, other
  * commands are not processed any further than the raw parse stage.
  */
-static List *
-pg_parse_query_internal(const char *query_string, List **querysource_list)
+List *
+pg_parse_query(const char *query_string)
 {
        List       *raw_parsetree_list;
 
@@ -752,7 +752,7 @@ pg_parse_query_internal(const char *query_string, List **querysource_list)
        if (log_parser_stats)
                ResetUsage();
 
-       raw_parsetree_list = raw_parser(query_string, querysource_list);
+       raw_parsetree_list = raw_parser(query_string);
 
        if (log_parser_stats)
                ShowUsage("PARSER STATISTICS");
@@ -775,18 +775,6 @@ pg_parse_query_internal(const char *query_string, List **querysource_list)
        return raw_parsetree_list;
 }
 
-List *
-pg_parse_query(const char *query_string)
-{
-       return pg_parse_query_internal(query_string, NULL);
-}
-
-List *
-pg_parse_query_get_source(const char *query_string, List **querysource_list)
-{
-       return pg_parse_query_internal(query_string, querysource_list);
-}
-
 /*
  * Given a raw parsetree (gram.y output), and optionally information about
  * types of parameter symbols ($n), perform parse analysis and rule rewriting.
@@ -1056,8 +1044,6 @@ exec_simple_query(const char *query_string)
        MemoryContext oldcontext;
        List       *parsetree_list;
        ListCell   *parsetree_item;
-       List       *querysource_list;
-       ListCell   *querysource_item;
        bool            save_log_statement_stats = log_statement_stats;
        bool            was_logged = false;
        bool            isTopLevel;
@@ -1106,7 +1092,7 @@ exec_simple_query(const char *query_string)
         * Do basic parsing of the query or queries (this should be safe even if
         * we are in aborted transaction state!)
         */
-       parsetree_list = pg_parse_query_get_source(query_string, &querysource_list);
+       parsetree_list = pg_parse_query(query_string);
 
 #ifdef XCP
        if (IS_PGXC_LOCAL_COORDINATOR && list_length(parsetree_list) > 1)
@@ -1173,10 +1159,9 @@ exec_simple_query(const char *query_string)
        /*
         * Run through the raw parsetree(s) and process each one.
         */
-       forboth(parsetree_item, parsetree_list, querysource_item, querysource_list)
+       foreach(parsetree_item, parsetree_list)
        {
                RawStmt    *parsetree = lfirst_node(RawStmt, parsetree_item);
-               char       *querysource = (char *) lfirst(querysource_item);
                bool            snapshot_set = false;
                const char *commandTag;
                char            completionTag[COMPLETION_TAG_BUFSIZE];
@@ -1290,23 +1275,10 @@ exec_simple_query(const char *query_string)
                 * We don't have to copy anything into the portal, because everything
                 * we are passing here is in MessageContext, which will outlive the
                 * portal anyway.
-                *
-                * The query_string may contain multiple commands separated by ';' and
-                * we have a separate parsetree corresponding to each such command.
-                * Since we later may send down the query to the remote nodes
-                * (especially for utility queries), using the query_string is a
-                * problem because the same query will be sent out multiple times, one
-                * for each command processed. So we taught the parser to return the
-                * portion of the query_string along with the parsetree and use that
-                * while defining a portal below.
-                *
-                * XXX Since the portal expects to see a valid query_string, if the
-                * substring is available, use the original query_string. Not elegant,
-                * but far better than what we were doing earlier
                 */
                PortalDefineQuery(portal,
                                                  NULL,
-                                                 querysource ? querysource : query_string,
+                                                 query_string,
                                                  commandTag,
                                                  plantree_list,
                                                  NULL);
index a8c267a28cbb79164a92f698c3934b0a83777700..2da98f67f319a739087475727c60e6ce11c02db5 100644 (file)
@@ -53,7 +53,6 @@ typedef struct base_yy_extra_type
         * State variables that belong to the grammar.
         */
        List       *parsetree;          /* final parse result is delivered here */
-       List       *queries;
 } base_yy_extra_type;
 
 /*
index cce78444571d77c558f9f31008b246a6b9ed4780..c8ad710dac0a738c67353d836a816e1baa53f7c0 100644 (file)
@@ -32,7 +32,7 @@ extern PGDLLIMPORT bool standard_conforming_strings;
 
 
 /* Primary entry point for the raw parsing functions */
-extern List *raw_parser(const char *str, List **queries);
+extern List *raw_parser(const char *str);
 
 /* Utility functions exported by gram.y (perhaps these should be elsewhere) */
 extern List *SystemFuncName(char *name);
index 597ff17078e9831565598c0821d0e245f6222f08..74f1cc897f50a54562539a2e93908ba34eccc074 100644 (file)
@@ -72,13 +72,6 @@ typedef struct core_yy_extra_type
        char       *scanbuf;
        Size            scanbuflen;
 
-#ifdef XCP
-       /*
-        * Pointer to the original query string
-        */
-       char       *query;
-#endif
-
        /*
         * The keyword list to use.
         */
@@ -133,6 +126,5 @@ extern int core_yylex(core_YYSTYPE *lvalp, YYLTYPE *llocp,
                   core_yyscan_t yyscanner);
 extern int     scanner_errposition(int location, core_yyscan_t yyscanner);
 extern void scanner_yyerror(const char *message, core_yyscan_t yyscanner) pg_attribute_noreturn();
-extern char *scanner_get_query(int start, int len, core_yyscan_t yyscanner);
 
 #endif   /* SCANNER_H */
index a32735bd3fe036d72c5e6c41e07c1f366761d342..8700e5d88e5dcc948022afde648fc7e30a1f1d0d 100644 (file)
@@ -51,7 +51,6 @@ typedef enum
 extern int     log_statement;
 
 extern List *pg_parse_query(const char *query_string);
-extern List *pg_parse_query_get_source(const char *query_string, List **queries);
 extern List *pg_analyze_and_rewrite(RawStmt *parsetree,
                                           const char *query_string,
                                           Oid *paramTypes, int numParams,
index c2e64957a57e4a430dd638c7ff245b5f34c16814..94f1f5859301939a6551cc6502b10948d767ee22 100644 (file)
@@ -3557,7 +3557,7 @@ check_sql_expr(const char *stmt, int location, int leaderlen)
        error_context_stack = &syntax_errcontext;
 
        oldCxt = MemoryContextSwitchTo(plpgsql_compile_tmp_cxt);
-       (void) raw_parser(stmt, NULL);
+       (void) raw_parser(stmt);
        MemoryContextSwitchTo(oldCxt);
 
        /* Restore former ereport callback */