Index: src/select.c ================================================================== --- src/select.c +++ src/select.c @@ -10,11 +10,11 @@ ** ************************************************************************* ** This file contains C code routines that are called by the parser ** to handle SELECT statements in SQLite. ** -** $Id: select.c,v 1.466 2008/08/20 16:35:10 drh Exp $ +** $Id: select.c,v 1.467 2008/08/21 14:15:59 drh Exp $ */ #include "sqliteInt.h" /* @@ -3771,24 +3771,18 @@ j = nGroupBy+1; for(i=0; iiSorterColumn>=j ){ int r1 = j + regBase; -#ifndef NDEBUG - int r2 = -#endif - sqlite3ExprCodeGetColumn(pParse, + int r2; + + r2 = sqlite3ExprCodeGetColumn(pParse, pCol->pTab, pCol->iColumn, pCol->iTable, r1, 0); + if( r1!=r2 ){ + sqlite3VdbeAddOp2(v, OP_SCopy, r2, r1); + } j++; - - /* sAggInfo.aCol[] only contains one entry per column. So - ** The reference to pCol->iColumn,pCol->iTable must have been - ** the first reference to that column. Hence, - ** sqliteExprCodeGetColumn is guaranteed to put the result in - ** the column requested. - */ - assert( r1==r2 ); } } regRecord = sqlite3GetTempReg(pParse); sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord); sqlite3VdbeAddOp2(v, OP_IdxInsert, sAggInfo.sortingIdx, regRecord); Index: test/select5.test ================================================================== --- test/select5.test +++ test/select5.test @@ -10,11 +10,11 @@ #*********************************************************************** # This file implements regression tests for SQLite library. The # focus of this file is testing aggregate functions and the # GROUP BY and HAVING clauses of SELECT statements. # -# $Id: select5.test,v 1.19 2008/08/20 16:35:10 drh Exp $ +# $Id: select5.test,v 1.20 2008/08/21 14:15:59 drh Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl # Build some test data @@ -189,12 +189,69 @@ INSERT INTO t4 VALUES(6,7,8); SELECT max(x), count(x), y, z FROM t4 GROUP BY y, z ORDER BY 1 } } {1 1 2 {} 2 1 3 {} 3 1 {} 5 4 2 {} 6 5 2 {} {} 6 1 7 8} -do_test select5.7.2 { +do_test select5-7.2 { execsql { SELECT count(*), count(x) as cnt FROM t4 GROUP BY y ORDER BY cnt; } } {1 1 1 1 1 1 5 5} + +# See ticket #3324. +# +do_test select5-8.1 { + execsql { + CREATE TABLE t8a(a,b); + CREATE TABLE t8b(x); + INSERT INTO t8a VALUES('one', 1); + INSERT INTO t8a VALUES('one', 2); + INSERT INTO t8a VALUES('two', 3); + INSERT INTO t8a VALUES('one', NULL); + INSERT INTO t8b(rowid,x) VALUES(1,111); + INSERT INTO t8b(rowid,x) VALUES(2,222); + INSERT INTO t8b(rowid,x) VALUES(3,333); + SELECT a, count(b) FROM t8a, t8b WHERE b=t8b.rowid GROUP BY a ORDER BY a; + } +} {one 2 two 1} +do_test select5-8.2 { + execsql { + SELECT a, count(b) FROM t8a, t8b WHERE b=+t8b.rowid GROUP BY a ORDER BY a; + } +} {one 2 two 1} +do_test select5-8.3 { + execsql { + SELECT t8a.a, count(t8a.b) FROM t8a, t8b WHERE t8a.b=t8b.rowid + GROUP BY 1 ORDER BY 1; + } +} {one 2 two 1} +do_test select5-8.4 { + execsql { + SELECT a, count(*) FROM t8a, t8b WHERE b=+t8b.rowid GROUP BY a ORDER BY a; + } +} {one 2 two 1} +do_test select5-8.5 { + execsql { + SELECT a, count(b) FROM t8a, t8b WHERE b