WordPress.org

Make WordPress Core

Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#32763 closed defect (bug) (fixed)

SQL query results depend on the number of blanks in query (due to error from "wpdb->get_table_from_query")

Reported by: klaus-b Owned by: pento
Milestone: 4.2.3 Priority: normal
Severity: normal Version: 4.2
Component: Database Keywords: fixed-major
Focuses: Cc:

Description

A (non ASCII) query of form

  SELECT <long fieldlist> from
    (SELECT <complex subquery>) as sq1
    LEFT JOIN
    (SELECT <simple subquery>) as sq2

didnt deliver a result tough some was expected.

Modifying the query to:

  SELECT <1000 spaces> <long fieldlist> from
    (SELECT <complex subquery>) as sq1
    LEFT JOIN
    (SELECT <simple subquery>) as sq2

Fixed the problem.

I tracked it down to wpdb->get_table_from_query.

  • In first case (for reasons I didn't investigate on) wpdb->get_table_from_query returns "SELECT"
  • In second case (for reasons mentioned below) wpdb->get_table_from_query returns FALSE

A reason for the different results is that wpdb->get_table_from_query only works on the first 1000 characters of the $query. Statement 3 of wpdb->get_table_from_query:

$query = preg_replace( '/\((?!\s*select)[^(]*?\)/is', '()', substr( $query, 0, 1000 ) );

But this (of course) doesn't fix the problem. The correct query result is delivered in case 2 when wpdb->get_table_from_query due to enough blanks gives up (returning FALSE) instead of delivering a wrong result "SELECT" as in case 1.

Change History (5)

#1 @dd32
2 years ago

  • Version changed from 4.2.2 to 4.2

A query that fails, for example purposes: ($wpdb::get_table_from_query() will return 'SELECT')

SELECT aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa FROM (SELECT * FROM a_test_table_name) as subquery
Last edited 2 years ago by dd32 (previous) (diff)

#2 @pento
2 years ago

  • Owner set to pento
  • Resolution set to fixed
  • Status changed from new to closed

In 33259:

WPDB: When extracting the table name from a query, we had a 1000 character limit on the SQL string that would be searched. This was a hangover from when the code was imported from HyperDB, and isn't appropriate for Core, where a wider range of queries are likely to be run.

Fixes #32763

#3 @pento
2 years ago

In 33260:

WPDB: When extracting the table name from a query, we had a 1000 character limit on the SQL string that would be searched. This was a hangover from when the code was imported from HyperDB, and isn't appropriate for Core, where a wider range of queries are likely to be run.

Merges [33259] to the 4.2 branch.

Fixes #32763

#4 @pento
2 years ago

  • Milestone changed from Awaiting Review to 4.2.3

#5 @samuelsidler
2 years ago

  • Keywords fixed-major added
Note: See TracTickets for help on using tickets.