@@ -1094,14 +1094,22 @@ jobs:
10941094 // Reproduce testInformationSchemaTablesFilterByAutoIncrement's exact
10951095 // shape — info_schema-style tables and the *exact* translate_table_ref
10961096 // correlated subquery (LEFT JOIN inside subquery + COALESCE).
1097+ // Also use Turso's REAL `sqlite_sequence` (a special table) by
1098+ // creating tables with INTEGER PRIMARY KEY AUTOINCREMENT, which
1099+ // is what the driver sets up for AUTO_INCREMENT columns.
10971100 $pdo = new PDO('sqlite::memory:');
10981101 $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
10991102 // Mirror the driver's information schema layout and seed it.
11001103 $pdo->exec("CREATE TABLE _ist (table_schema TEXT, table_name TEXT, engine TEXT)");
11011104 $pdo->exec("CREATE TABLE _isc (table_schema TEXT, table_name TEXT, column_name TEXT, extra TEXT)");
11021105 $pdo->exec("INSERT INTO _ist VALUES ('main','low','InnoDB'), ('main','high','InnoDB'), ('main','plain','InnoDB')");
11031106 $pdo->exec("INSERT INTO _isc VALUES ('main','low','id','auto_increment'), ('main','high','id','auto_increment'), ('main','plain','id','')");
1104- // sqlite_sequence is auto-managed; emulate with a regular table called sqlseq.
1107+ // Real sqlite_sequence: create AUTOINCREMENT tables and insert.
1108+ $pdo->exec("CREATE TABLE low (id INTEGER PRIMARY KEY AUTOINCREMENT, n TEXT)");
1109+ $pdo->exec("CREATE TABLE high (id INTEGER PRIMARY KEY AUTOINCREMENT, n TEXT)");
1110+ $pdo->exec("INSERT INTO low (n) VALUES ('a')");
1111+ $pdo->exec("INSERT INTO high (n) VALUES ('a'),('b'),('c'),('d'),('e')");
1112+ // (Also keep the fake table for comparison — used in the cases below.)
11051113 $pdo->exec("CREATE TABLE sqlseq (name TEXT, seq INT)");
11061114 $pdo->exec("INSERT INTO sqlseq VALUES ('low',1), ('high',5)");
11071115
@@ -1115,23 +1123,35 @@ jobs:
11151123
11161124 $derived = "(SELECT table_name AS NAME, $corr AS AI FROM _ist AS t)";
11171125
1126+ // Variants using REAL sqlite_sequence (special table populated by AUTOINCREMENT)
1127+ $real_corr = "(SELECT COALESCE(s.seq + 1, 1)
1128+ FROM _isc AS c
1129+ LEFT JOIN sqlite_sequence AS s ON s.name = c.table_name
1130+ WHERE c.extra = 'auto_increment'
1131+ AND c.table_schema = t.table_schema
1132+ AND c.table_name = t.table_name)";
1133+ $real_derived = "(SELECT table_name AS NAME, $real_corr AS AI FROM _ist AS t)";
1134+
11181135 $cases = [
1119- 'inline correlated subq value' =>
1136+ 'fake-seq inline correlated subq value' =>
11201137 "SELECT table_name, $corr AS AI FROM _ist AS t",
1121- 'derived-table + WHERE AI > 3 (the bug)' =>
1138+ 'fake-seq derived-table + WHERE AI > 3 (the bug)' =>
11221139 "SELECT NAME FROM $derived WHERE AI > 3",
1123- 'derived-table + WHERE AI IS NULL' =>
1140+ 'fake-seq derived-table + WHERE AI IS NULL' =>
11241141 "SELECT NAME FROM $derived WHERE AI IS NULL",
1125- 'derived-table + plain SELECT' =>
1142+ 'fake-seq derived-table + plain SELECT' =>
11261143 "SELECT NAME, AI FROM $derived",
1127- 'derived-table + WHERE on AI with length(t.name) > 0 forced' =>
1128- ("SELECT NAME FROM (SELECT table_name AS NAME, "
1129- . "(SELECT COALESCE(s.seq + 1, 1) FROM _isc AS c "
1130- . "LEFT JOIN sqlseq AS s ON s.name = c.table_name "
1131- . "WHERE c.extra='auto_increment' "
1132- . "AND c.table_schema = t.table_schema "
1133- . "AND c.table_name = t.table_name "
1134- . "AND length(t.table_name) > 0) AS AI FROM _ist AS t) WHERE AI > 3"),
1144+ 'real-seq inline correlated subq value' =>
1145+ "SELECT table_name, $real_corr AS AI FROM _ist AS t",
1146+ 'real-seq derived-table + WHERE AI > 3 (the bug)' =>
1147+ "SELECT NAME FROM $real_derived WHERE AI > 3",
1148+ 'real-seq derived-table + WHERE AI IS NULL' =>
1149+ "SELECT NAME FROM $real_derived WHERE AI IS NULL",
1150+ 'real-seq derived-table + plain SELECT' =>
1151+ "SELECT NAME, AI FROM $real_derived",
1152+ // Just dump real sqlite_sequence to confirm it has the right values
1153+ 'real sqlite_sequence dump' =>
1154+ "SELECT name, seq FROM sqlite_sequence ORDER BY name",
11351155 ];
11361156 foreach ($cases as $label => $sql) {
11371157 try {
0 commit comments