Skip to content

Commit 78066d7

Browse files
committed
Add length() perturbation to translate_table_ref AUTO_INCREMENT subquery
testInformationSchemaTablesFilterByAutoIncrement fails on Turso mid-suite: WHERE on the AUTO_INCREMENT alias of a correlated subquery in the derived-table SELECT list returns 0 rows. CI probe of the same shape on a fresh PDO works, so the bug is state-dependent — likely Turso's optimizer caches the subquery result across rows after some prior tests run. The probe also tested adding `AND length(<alias>.table_name) > 0` to the correlated subquery's WHERE — same result on isolation but it forces a fresh outer-column reference at each evaluation, which should defeat any cross-row caching. Apply that perturbation here. The shape of translate_table_ref's output is otherwise unchanged, so we hope this avoids the testReconstructTable pager fragility that the JOIN-form rewrite triggered.
1 parent 656f12e commit 78066d7

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

.github/workflows/phpunit-tests-turso.yml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,6 +1905,66 @@ jobs:
19051905
# The two temp-table tests stay failing under Turso. No
19061906
# driver-side workaround found that doesn't break a
19071907
# currently-passing test.
1908+
1909+
# 21. testInformationSchemaTablesFilterByAutoIncrement fails on
1910+
# Turso mid-suite because the WHERE filter on the AUTO_INCREMENT
1911+
# alias of a correlated subquery in a derived-table SELECT
1912+
# list returns 0 rows. CI probe of the exact same query
1913+
# shape on a fresh PDO returns the correct rows, so the bug
1914+
# is state-dependent.
1915+
#
1916+
# The probe also showed that adding `AND length(<alias>.table_name) > 0`
1917+
# to the correlated subquery's WHERE clause works (forces a
1918+
# fresh outer-column reference at each evaluation). Apply
1919+
# that perturbation here. This change keeps the overall
1920+
# translate_table_ref shape identical except for one extra
1921+
# redundant predicate in the WHERE — which we hope doesn't
1922+
# trigger Turso's testReconstructTable pager fragility the
1923+
# way restructuring to a JOIN form did.
1924+
path = 'src/sqlite/class-wp-pdo-mysql-on-sqlite.php'
1925+
src = open(path).read()
1926+
old_corr = (
1927+
"\t\t\t\t\t$auto_increment_subquery = sprintf(\n"
1928+
"\t\t\t\t\t\t\"(\n"
1929+
"\t\t\t\t\t\t\tSELECT COALESCE(s.seq + 1, 1)\n"
1930+
"\t\t\t\t\t\t\tFROM %s AS c\n"
1931+
"\t\t\t\t\t\t\t%s\n"
1932+
"\t\t\t\t\t\t\tWHERE c.extra = 'auto_increment'\n"
1933+
"\t\t\t\t\t\t\tAND c.table_schema = %s.table_schema\n"
1934+
"\t\t\t\t\t\t\tAND c.table_name = %s.table_name\n"
1935+
"\t\t\t\t\t\t)\",\n"
1936+
"\t\t\t\t\t\t$this->quote_sqlite_identifier( $columns_table ),\n"
1937+
"\t\t\t\t\t\t$has_sequence_table\n"
1938+
"\t\t\t\t\t\t\t? 'LEFT JOIN main.sqlite_sequence AS s ON s.name = c.table_name'\n"
1939+
"\t\t\t\t\t\t\t: 'LEFT JOIN (SELECT 0 AS seq) AS s',\n"
1940+
"\t\t\t\t\t\t$this->quote_sqlite_identifier( $table_name ),\n"
1941+
"\t\t\t\t\t\t$this->quote_sqlite_identifier( $table_name )\n"
1942+
"\t\t\t\t\t);\n"
1943+
)
1944+
new_corr = (
1945+
"\t\t\t\t\t$auto_increment_subquery = sprintf(\n"
1946+
"\t\t\t\t\t\t\"(\n"
1947+
"\t\t\t\t\t\t\tSELECT COALESCE(s.seq + 1, 1)\n"
1948+
"\t\t\t\t\t\t\tFROM %s AS c\n"
1949+
"\t\t\t\t\t\t\t%s\n"
1950+
"\t\t\t\t\t\t\tWHERE c.extra = 'auto_increment'\n"
1951+
"\t\t\t\t\t\t\tAND c.table_schema = %s.table_schema\n"
1952+
"\t\t\t\t\t\t\tAND c.table_name = %s.table_name\n"
1953+
"\t\t\t\t\t\t\tAND length(%s.table_name) > 0\n"
1954+
"\t\t\t\t\t\t)\",\n"
1955+
"\t\t\t\t\t\t$this->quote_sqlite_identifier( $columns_table ),\n"
1956+
"\t\t\t\t\t\t$has_sequence_table\n"
1957+
"\t\t\t\t\t\t\t? 'LEFT JOIN main.sqlite_sequence AS s ON s.name = c.table_name'\n"
1958+
"\t\t\t\t\t\t\t: 'LEFT JOIN (SELECT 0 AS seq) AS s',\n"
1959+
"\t\t\t\t\t\t$this->quote_sqlite_identifier( $table_name ),\n"
1960+
"\t\t\t\t\t\t$this->quote_sqlite_identifier( $table_name ),\n"
1961+
"\t\t\t\t\t\t$this->quote_sqlite_identifier( $table_name )\n"
1962+
"\t\t\t\t\t);\n"
1963+
)
1964+
assert old_corr in src, 'translate_table_ref AUTO_INCREMENT correlated subquery not found'
1965+
src = src.replace(old_corr, new_corr, 1)
1966+
open(path, 'w').write(src)
1967+
print('patched translate_table_ref AUTO_INCREMENT correlated subquery with length() perturbation')
19081968
PY
19091969
19101970
- name: Run PHPUnit tests against Turso DB

0 commit comments

Comments
 (0)