@@ -2582,11 +2582,10 @@ get_next_wbucket(pgsmSharedState *pgsm)
25822582}
25832583
25842584/*
2585- * This function expects a NORMALIZED query as the input.
2586- * It iterates over the normalized query skipping comments and
2587- * multiple spaces. All spaces are converted to ' ' so that we
2588- * the calculation is independent of the space type whether
2589- * newline, tab, or any other type. Trailing and leading spaces
2585+ * This function expects a NORMALIZED query as the input. It iterates over the
2586+ * normalized query skipping comments and multiple spaces. All spaces are
2587+ * converted to ' ' so that we the calculation is independent of the space
2588+ * type whether newline, tab, or any other type. Trailing and leading spaces
25902589 * are also removed before calculating the hash.
25912590 */
25922591static int64
@@ -2595,82 +2594,71 @@ get_pgsm_query_id_hash(const char *norm_query, int norm_len)
25952594 char * query ;
25962595 char * q_iter ;
25972596 const char * norm_q_iter = norm_query ;
2598- int64 pgsm_query_id = 0 ;
2597+ bool space = false;
2598+ int64 pgsm_query_id ;
2599+
2600+ Assert (norm_query != NULL );
25992601
26002602 if (!pgsm_enable_pgsm_query_id )
26012603 return 0 ;
26022604
26032605 query = palloc (norm_len + 1 );
26042606 q_iter = query ;
26052607
2606- while (norm_q_iter && * norm_q_iter && norm_q_iter < norm_query + norm_len )
2608+ while (* norm_q_iter && norm_q_iter < norm_query + norm_len )
26072609 {
2608- /*
2609- * Skip multiline comments, + 1 is safe even if we've reach end of
2610- * string
2611- */
26122610 if (* norm_q_iter == '/' && * (norm_q_iter + 1 ) == '*' )
26132611 {
2614- while (* norm_q_iter && * (norm_q_iter + 1 ) && (* norm_q_iter != '*' || * (norm_q_iter + 1 ) != '/' ))
2615- norm_q_iter ++ ;
2616-
2617- /*
2618- * Skip the end if the current character is valid. norm_q_iter
2619- * points to the *, we have to skip 2 characters
2620- */
2621- if (* norm_q_iter )
2622- norm_q_iter ++ ;
2623- if (* norm_q_iter )
2624- norm_q_iter ++ ;
2612+ /* Skip multiline comments */
2613+ norm_q_iter ++ ;
2614+ norm_q_iter ++ ;
26252615
2626- continue ;
2616+ while (* norm_q_iter )
2617+ {
2618+ if (* norm_q_iter == '*' && * (norm_q_iter + 1 ) == '/' )
2619+ {
2620+ norm_q_iter ++ ;
2621+ norm_q_iter ++ ;
2622+ break ;
2623+ }
2624+ else
2625+ norm_q_iter ++ ;
2626+ }
26272627 }
2628-
2629- /*
2630- * Skip single line comments, + 1 is safe even if we've reach end of
2631- * string
2632- */
2633- if (* norm_q_iter == '-' && * (norm_q_iter + 1 ) == '-' )
2628+ else if (* norm_q_iter == '-' && * (norm_q_iter + 1 ) == '-' )
26342629 {
2630+ /* Skip single line comments */
2631+ norm_q_iter ++ ;
2632+ norm_q_iter ++ ;
2633+
26352634 while (* norm_q_iter && * norm_q_iter != '\n' )
26362635 norm_q_iter ++ ;
26372636 }
2638-
2639- /* Skip white spaces */
2640- if (scanner_isspace (* norm_q_iter ))
2637+ else if (scanner_isspace (* norm_q_iter ))
26412638 {
2642- while (scanner_isspace (* ++ norm_q_iter ));
2639+ /* Convert white spaces into single space */
2640+ norm_q_iter ++ ;
26432641
2644- /*
2645- * Let's replace it with a simple space. -1 is safe as we are
2646- * making sure we are not at the start of the string.
2647- */
2648- if (q_iter != query && !scanner_isspace (* (q_iter - 1 )))
2649- * q_iter ++ = ' ' ;
2642+ while (scanner_isspace (* norm_q_iter ))
2643+ norm_q_iter ++ ;
26502644
2651- continue ;
2645+ space = true;
2646+ }
2647+ else
2648+ {
2649+ if (space && q_iter != query )
2650+ * q_iter ++ = ' ' ;
2651+ space = false;
2652+ * q_iter ++ = * norm_q_iter ++ ;
26522653 }
2653-
2654- * q_iter ++ = * norm_q_iter ++ ;
26552654 }
26562655
2657- /* Ensure we have a terminating zero at the end */
26582656 * q_iter = '\0' ;
26592657
2660- /* Get rid of trailing spaces */
2661- while (q_iter > query && * q_iter == '\0' )
2662- {
2663- q_iter -- ;
2664-
2665- /* Continue reducing the string size if space is found. */
2666- if (scanner_isspace (* q_iter ))
2667- * q_iter = '\0' ;
2668- }
2669-
2670- /* Calculate the hash. */
2671- pgsm_query_id = pgsm_hash_string (query , strlen (query ));
2658+ pgsm_query_id = pgsm_hash_string (query , q_iter - query );
26722659
26732660 pfree (query );
2661+
26742662 return pgsm_query_id ;
26752663}
26762664
0 commit comments