Index: src/vdbe.c ================================================================== --- src/vdbe.c +++ src/vdbe.c @@ -41,11 +41,11 @@ ** documentation, headers files, or other derived files. The formatting ** of the code in this file is, therefore, important. See other comments ** in this file for details. If in doubt, do not deviate from existing ** commenting and indentation practices when changing or adding code. ** -** $Id: vdbe.c,v 1.418 2004/10/06 15:41:17 drh Exp $ +** $Id: vdbe.c,v 1.419 2004/10/19 16:40:59 drh Exp $ */ #include "sqliteInt.h" #include "os.h" #include #include "vdbeInt.h" @@ -4235,11 +4235,24 @@ */ case OP_AggGet: { AggElem *pFocus; int i = pOp->p2; pFocus = p->agg.pCurrent; - if( pFocus==0 ) goto no_mem; + if( pFocus==0 ){ + int res; + if( sqlite3_malloc_failed ) goto no_mem; + rc = sqlite3BtreeFirst(p->agg.pCsr, &res); + if( rc!=SQLITE_OK ){ + return rc; + } + if( res!=0 ){ + rc = AggInsert(&p->agg,"",1); + pFocus = p->agg.pCurrent; + }else{ + rc = sqlite3BtreeData(p->agg.pCsr, 0, 4, (char *)&pFocus); + } + } assert( i>=0 && iagg.nMem ); pTos++; sqlite3VdbeMemShallowCopy(pTos, &pFocus->aMem[i], MEM_Ephem); if( pTos->flags&MEM_Str ){ sqlite3VdbeChangeEncoding(pTos, db->enc); Index: test/misc4.test ================================================================== --- test/misc4.test +++ test/misc4.test @@ -11,11 +11,11 @@ # This file implements regression tests for SQLite library. # # This file implements tests for miscellanous features that were # left out of other test files. # -# $Id: misc4.test,v 1.6 2004/07/24 17:38:30 drh Exp $ +# $Id: misc4.test,v 1.7 2004/10/19 16:40:59 drh Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl # Prepare a statement that will create a temporary table. Then do @@ -80,7 +80,22 @@ catchsql { INSERT INTO t3 VALUES(1); } } {0 {}} +# Ticket #966 +# +do_test misc4-3.1 { + execsql { + CREATE TABLE Table1(ID integer primary key, Value TEXT); + INSERT INTO Table1 VALUES(1, 'x'); + CREATE TABLE Table2(ID integer NOT NULL, Value TEXT); + INSERT INTO Table2 VALUES(1, 'z'); + INSERT INTO Table2 VALUES (1, 'a'); + SELECT ID, Value FROM Table1 + UNION SELECT ID, max(Value) FROM Table2 GROUP BY 1,2 + ORDER BY 1, 2; + } +} {{} {} 1 x 1 z} + finish_test