/ Check-in [25643a01]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Eliminate the OP_SortMakeRec and OP_SortCallback opcodes. Sort using the standard record format. (CVS 1426)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 25643a0137d395572f16cfec3ab3327d913138ba
User & Date: drh 2004-05-21 03:01:59
Context
2004-05-21
10:08
Further work on the new API. All the functions to execute queries are there now. (CVS 1427) check-in: fc94575d user: danielk1977 tags: trunk
03:01
Eliminate the OP_SortMakeRec and OP_SortCallback opcodes. Sort using the standard record format. (CVS 1426) check-in: 25643a01 user: drh tags: trunk
02:14
Most sorting problems are fixed. Dead code has been removed. 3 test failures remain but will be fixed by the new function API once it gets implemented. (CVS 1425) check-in: 3b55095e user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/select.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that are called by the parser
    13     13   ** to handle SELECT statements in SQLite.
    14     14   **
    15         -** $Id: select.c,v 1.173 2004/05/21 02:14:25 drh Exp $
           15  +** $Id: select.c,v 1.174 2004/05/21 03:01:59 drh Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   
    20     20   /*
    21     21   ** Allocate a new Select structure and return a pointer to that
    22     22   ** structure.
................................................................................
   482    482       }
   483    483   
   484    484       /* Send the data to the callback function.
   485    485       */
   486    486       case SRT_Callback:
   487    487       case SRT_Sorter: {
   488    488         if( pOrderBy ){
   489         -        sqlite3VdbeAddOp(v, OP_SortMakeRec, nColumn, 0);
          489  +        sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
   490    490           pushOntoSorter(pParse, v, pOrderBy);
   491    491         }else{
   492    492           assert( eDest==SRT_Callback );
   493    493           sqlite3VdbeAddOp(v, OP_Callback, nColumn, 0);
   494    494         }
   495    495         break;
   496    496       }
................................................................................
   562    562       sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
   563    563       sqlite3VdbeAddOp(v, OP_Goto, 0, addr);
   564    564     }
   565    565     if( p->iLimit>=0 ){
   566    566       sqlite3VdbeAddOp(v, OP_MemIncr, p->iLimit, end2);
   567    567     }
   568    568     switch( eDest ){
   569         -    case SRT_Callback: {
   570         -      sqlite3VdbeAddOp(v, OP_SortCallback, nColumn, 0);
   571         -      break;
   572         -    }
   573    569       case SRT_Table:
   574    570       case SRT_TempTable: {
   575    571         sqlite3VdbeAddOp(v, OP_NewRecno, iParm, 0);
   576    572         sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
   577    573         sqlite3VdbeAddOp(v, OP_PutIntKey, iParm, 0);
   578    574         break;
   579    575       }
................................................................................
   589    585       }
   590    586       case SRT_Mem: {
   591    587         assert( nColumn==1 );
   592    588         sqlite3VdbeAddOp(v, OP_MemStore, iParm, 1);
   593    589         sqlite3VdbeAddOp(v, OP_Goto, 0, end1);
   594    590         break;
   595    591       }
          592  +    case SRT_Callback:
   596    593       case SRT_Subroutine: {
   597    594         int i;
   598    595         sqlite3VdbeAddOp(v, OP_Integer, p->pEList->nExpr, 0);
   599    596         sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
   600    597         for(i=0; i<nColumn; i++){
   601    598           sqlite3VdbeAddOp(v, OP_Column, -1-i, i);
   602    599         }
   603         -      sqlite3VdbeAddOp(v, OP_Gosub, 0, iParm);
          600  +      if( eDest==SRT_Callback ){
          601  +        sqlite3VdbeAddOp(v, OP_Callback, nColumn, 0);
          602  +      }else{
          603  +        sqlite3VdbeAddOp(v, OP_Gosub, 0, iParm);
          604  +      }
   604    605         sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
   605    606         break;
   606    607       }
   607    608       default: {
   608    609         /* Do nothing */
   609    610         break;
   610    611       }

Changes to src/vdbe.c.

    39     39   **
    40     40   ** Various scripts scan this source file in order to generate HTML
    41     41   ** documentation, headers files, or other derived files.  The formatting
    42     42   ** of the code in this file is, therefore, important.  See other comments
    43     43   ** in this file for details.  If in doubt, do not deviate from existing
    44     44   ** commenting and indentation practices when changing or adding code.
    45     45   **
    46         -** $Id: vdbe.c,v 1.311 2004/05/21 02:14:25 drh Exp $
           46  +** $Id: vdbe.c,v 1.312 2004/05/21 03:01:59 drh Exp $
    47     47   */
    48     48   #include "sqliteInt.h"
    49     49   #include "os.h"
    50     50   #include <ctype.h>
    51     51   #include "vdbeInt.h"
    52     52   
    53     53   /*
................................................................................
  4188   4188     assert( pNos->flags & MEM_Dyn );
  4189   4189     pSorter->nData = pNos->n;
  4190   4190     pSorter->pData = pNos->z;
  4191   4191     pTos -= 2;
  4192   4192     break;
  4193   4193   }
  4194   4194   
  4195         -/* Opcode: SortMakeRec P1 * *
  4196         -**
  4197         -** The top P1 elements are the arguments to a callback.  Form these
  4198         -** elements into a single data entry that can be stored on a sorter
  4199         -** using SortPut and later fed to a callback using SortCallback.
  4200         -*/
  4201         -case OP_SortMakeRec: {
  4202         -  char *z;
  4203         -  char **azArg;
  4204         -  int nByte;
  4205         -  int nField;
  4206         -  int i;
  4207         -  Mem *pRec;
  4208         -
  4209         -  nField = pOp->p1;
  4210         -  pRec = &pTos[1-nField];
  4211         -  assert( pRec>=p->aStack );
  4212         -  nByte = 0;
  4213         -  for(i=0; i<nField; i++, pRec++){
  4214         -    if( (pRec->flags & MEM_Null)==0 ){
  4215         -      Stringify(pRec);
  4216         -      nByte += pRec->n;
  4217         -    }
  4218         -  }
  4219         -  nByte += sizeof(char*)*(nField+1);
  4220         -  azArg = sqliteMallocRaw( nByte );
  4221         -  if( azArg==0 ) goto no_mem;
  4222         -  z = (char*)&azArg[nField+1];
  4223         -  for(pRec=&pTos[1-nField], i=0; i<nField; i++, pRec++){
  4224         -    if( pRec->flags & MEM_Null ){
  4225         -      azArg[i] = 0;
  4226         -    }else{
  4227         -      azArg[i] = z;
  4228         -      memcpy(z, pRec->z, pRec->n);
  4229         -      z += pRec->n;
  4230         -    }
  4231         -  }
  4232         -  popStack(&pTos, nField);
  4233         -  pTos++;
  4234         -  pTos->n = nByte;
  4235         -  pTos->z = (char*)azArg;
  4236         -  pTos->flags = MEM_Str | MEM_Dyn;
  4237         -  break;
  4238         -}
  4239         -
  4240   4195   /* Opcode: Sort * * P3
  4241   4196   **
  4242   4197   ** Sort all elements on the sorter.  The algorithm is a
  4243   4198   ** mergesort.  The P3 argument is a pointer to a KeyInfo structure
  4244   4199   ** that describes the keys to be sorted.
  4245   4200   */
  4246   4201   case OP_Sort: {
................................................................................
  4296   4251       sqliteFree(pSorter);
  4297   4252     }else{
  4298   4253       pc = pOp->p2 - 1;
  4299   4254     }
  4300   4255     break;
  4301   4256   }
  4302   4257   
  4303         -/* Opcode: SortCallback P1 * *
  4304         -**
  4305         -** The top of the stack contains a callback record built using
  4306         -** the SortMakeRec operation with the same P1 value as this
  4307         -** instruction.  Pop this record from the stack and invoke the
  4308         -** callback on it.
  4309         -*/
  4310         -case OP_SortCallback: {
  4311         -  assert( pTos>=p->aStack );
  4312         -  assert( pTos->flags & MEM_Str );
  4313         -  p->nCallback++;
  4314         -  p->pc = pc+1;
  4315         -  p->azResColumn = (char**)pTos->z;
  4316         -  assert( p->nResColumn==pOp->p1 );
  4317         -  p->popStack = 1;
  4318         -  p->pTos = pTos;
  4319         -  return SQLITE_ROW;
  4320         -}
  4321         -
  4322   4258   /* Opcode: SortReset * * *
  4323   4259   **
  4324   4260   ** Remove any elements that remain on the sorter.
  4325   4261   */
  4326   4262   case OP_SortReset: {
  4327   4263     sqlite3VdbeSorterReset(p);
  4328   4264     break;

Changes to test/where.test.

     7      7   #    May you find forgiveness for yourself and forgive others.
     8      8   #    May you share freely, never taking more than you give.
     9      9   #
    10     10   #***********************************************************************
    11     11   # This file implements regression tests for SQLite library.  The
    12     12   # focus of this file is testing the use of indices in WHERE clases.
    13     13   #
    14         -# $Id: where.test,v 1.20 2004/05/21 02:14:25 drh Exp $
           14  +# $Id: where.test,v 1.21 2004/05/21 03:01:59 drh Exp $
    15     15   
    16     16   set testdir [file dirname $argv0]
    17     17   source $testdir/tester.tcl
    18     18   
    19     19   # Build some test data
    20     20   #
    21     21   do_test where-1.0 {
................................................................................
   360    360   # for the SQL and appends a "nosort" to the result if the program contains the
   361    361   # SortCallback opcode.  If the program does not contain the SortCallback
   362    362   # opcode it appends "sort"
   363    363   #
   364    364   proc cksort {sql} {
   365    365     set data [execsql $sql]
   366    366     set prog [execsql "EXPLAIN $sql"]
   367         -  if {[regexp SortCallback $prog]} {set x sort} {set x nosort}
          367  +  if {[regexp Sort $prog]} {set x sort} {set x nosort}
   368    368     lappend data $x
   369    369     return $data
   370    370   }
   371    371   # Check out the logic that attempts to implement the ORDER BY clause
   372    372   # using an index rather than by sorting.
   373    373   #
   374    374   do_test where-6.1 {