SQLite

Check-in [45cbd3b449]
Login

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

Overview
Comment:Simplify the window function code generator some more.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | window-functions
Files: files | file ages | folders
SHA3-256: 45cbd3b4498cea8856f189e9d0a192556d4f15212055b8328a1beca6083fc47a
User & Date: dan 2019-03-06 21:04:11.725
Context
2019-03-07
19:26
Modify new window functions function so that cursors are stepped immediately after each operation, instead of immediately before. (check-in: 093d2b25f1 user: dan tags: window-functions)
2019-03-06
21:04
Simplify the window function code generator some more. (check-in: 45cbd3b449 user: dan tags: window-functions)
17:12
Improvements to the way built-in window functions are handled. (check-in: e8eee566df user: dan tags: window-functions)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/window.c.
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865

































































































































1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905

1906



1907


1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924

1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
  VdbeComment((v, "end flush_partition subroutine"));

  /* Jump to here to skip over flush_partition */
  sqlite3VdbeJumpHere(v, addrGoto);
}

/* 
** Return true if the entire partition should be cached in the temp
** table before processing.
*/
static int windowCachePartition(Window *pMWin){
  Window *pWin;
  for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
    FuncDef *pFunc = pWin->pFunc;
    if( (pFunc->funcFlags & SQLITE_FUNC_WINDOW_SIZE)
     || (pFunc->zName==nth_valueName)
     || (pFunc->zName==first_valueName)
     || (pFunc->zName==leadName)
     || (pFunc->zName==lagName)
    ){
      return 1;
    }
  }
  return 0;
}


































































































































static void windowCodeStep(
  Parse *pParse, 
  Select *p,
  WhereInfo *pWInfo,
  int regGosub, 
  int addrGosub
){
  Window *pMWin = p->pWin;
  Vdbe *v = sqlite3GetVdbe(pParse);
  int regFlushPart;               /* Register for "Gosub flush_partition" */

  int regArg;
  int csrCurrent = pMWin->iEphCsr;
  int csrWrite = csrCurrent+1;
  int csrStart = csrCurrent+2;
  int csrEnd = csrCurrent+3;

  int regStart;                    /* Value of <expr> PRECEDING */
  int regEnd;                      /* Value of <expr> FOLLOWING */

  int iSubCsr = p->pSrc->a[0].iCursor;
  int nSub = p->pSrc->a[0].pTab->nCol;
  int k;

  int addrGoto;
  int addrIf;
  int addrIfEnd;
  int addrIfStart;
  int addrGosubFlush;
  int addrInteger;
  int addrCacheRewind;
  int addrCacheNext;

  int addrShortcut = 0;

  int bCache = windowCachePartition(pMWin);

  int reg = pParse->nMem+1;
  int regRecord = reg+nSub;
  int regRowid = regRecord+1;





  bCache = 1;



  pParse->nMem += 1 + nSub + 1;

  regFlushPart = ++pParse->nMem;
  regStart = ++pParse->nMem;
  regEnd = ++pParse->nMem;

  assert( pMWin->eStart==TK_PRECEDING 
       || pMWin->eStart==TK_CURRENT 
       || pMWin->eStart==TK_FOLLOWING 
       || pMWin->eStart==TK_UNBOUNDED 
  );
  assert( pMWin->eEnd==TK_FOLLOWING 
       || pMWin->eEnd==TK_CURRENT 
       || pMWin->eEnd==TK_UNBOUNDED 
       || pMWin->eEnd==TK_PRECEDING 
  );


  /* Load the column values for the row returned by the sub-select
  ** into an array of registers starting at reg. Assemble them into
  ** a record in register regRecord. TODO: An optimization here? */
  for(k=0; k<nSub; k++){
    sqlite3VdbeAddOp3(v, OP_Column, iSubCsr, k, reg+k);
  }
  sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, nSub, regRecord);

  /* An input row has just been read into an array of registers starting
  ** at reg. If the window has a PARTITION clause, this block generates 
  ** VM code to check if the input row is the start of a new partition.
  ** If so, it does an OP_Gosub to an address to be filled in later. The







|
|

















>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




















|
|
|



<
<












>

>
>
>
|
>
>

















>




|
|







1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020


2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
  VdbeComment((v, "end flush_partition subroutine"));

  /* Jump to here to skip over flush_partition */
  sqlite3VdbeJumpHere(v, addrGoto);
}

/* 
** Return true if the entire partition should be cached in the ephemeral
** table before processing any rows.
*/
static int windowCachePartition(Window *pMWin){
  Window *pWin;
  for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
    FuncDef *pFunc = pWin->pFunc;
    if( (pFunc->funcFlags & SQLITE_FUNC_WINDOW_SIZE)
     || (pFunc->zName==nth_valueName)
     || (pFunc->zName==first_valueName)
     || (pFunc->zName==leadName)
     || (pFunc->zName==lagName)
    ){
      return 1;
    }
  }
  return 0;
}

typedef struct WindowCodeArg WindowCodeArg;
struct WindowCodeArg {
  Parse *pParse;
  Window *pMWin;
  Vdbe *pVdbe;
  int regGosub;
  int addrGosub;
  int regArg;
};

#define WINDOW_RETURN_ROW 1
#define WINDOW_AGGINVERSE 2
#define WINDOW_AGGSTEP    3

static int windowCodeOp(
 WindowCodeArg *p,
 int op,
 int csr,
 int regCountdown,
 int jumpOnEof
){
  int ret = 0;
  Vdbe *v = p->pVdbe;
  int addrIf = 0; 

  if( regCountdown>0 ){
    addrIf = sqlite3VdbeAddOp3(v, OP_IfPos, regCountdown, 0, 1);
  }

  if( jumpOnEof ){
    sqlite3VdbeAddOp2(v, OP_Next, csr, sqlite3VdbeCurrentAddr(v)+2);
    ret = sqlite3VdbeAddOp0(v, OP_Goto);
  }else{
    sqlite3VdbeAddOp2(v, OP_Next, csr, sqlite3VdbeCurrentAddr(v)+1);
  }

  switch( op ){
    case WINDOW_RETURN_ROW:
      windowAggFinal(p->pParse, p->pMWin, 0);
      windowReturnOneRow(p->pParse, p->pMWin, p->regGosub, p->addrGosub);
      break;

    case WINDOW_AGGINVERSE:
      windowAggStep(p->pParse, p->pMWin, csr, 1, p->regArg, p->pMWin->regSize);
      break;

    case WINDOW_AGGSTEP:
      windowAggStep(p->pParse, p->pMWin, csr, 0, p->regArg, p->pMWin->regSize);
      break;
  }

  if( ret ){
    sqlite3VdbeJumpHere(v, ret);
  }
  if( regCountdown>0 ){
    sqlite3VdbeJumpHere(v, addrIf);
  }
  return ret;
}



/*
** This function - windowCodeStep() - generates the VM code that reads data
** from the sub-select and returns rows to the consumer. For the simplest
** case:
**
**     ROWS BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
**
** The VM code generated is equivalent in spirit to the following:
**
**     while( !eof ){
**       if( new partition ){
**         Gosub flush
**       }    
**       Insert new row into eph table.
**     
**       if( first row of partition ){
**         Rewind(csrEnd, skipNext=1)
**         Rewind(csrStart, skipNext=1)
**         Rewind(csrCurrent, skipNext=1)
**     
**         regEnd = <expr2>          // FOLLOWING expression
**         regStart = <expr1>        // PRECEDING expression
**       }else{
**         if( (regEnd--)<=0 ){
**           Next(csrCurrent)
**           Return one row.
**           if( (regStart--)<0 ){
**             Next(csrStart)
**             AggInverse(csrStart)
**           }
**         }
**       }    
**     
**       Next(csrEnd)
**       AggStep(csrEnd)
**     }    
**     flush:
**       while( 1 ){ 
**         Next(csrCurrent)
**         if( eof ) break
**         Return one row.
**         if( (regStart--)<0 ){
**           Next(csrStart)
**           AggInverse(csrStart)
**         }
**       }    
**       Empty eph table.
**
** More generally, the pattern used for all window types is:
**
**     while( !eof ){
**       if( new partition ){
**         Gosub flush
**       }    
**       Insert new row into eph table.
**       if( first row of partition ){
**         FIRST_ROW_CODE
**       }else{
**         SECOND_ROW_CODE
**       }    
**       ALL_ROW_CODE
**     }    
**     flush:
**       FLUSH_CODE
**       Empty eph table.
**
*/
static void windowCodeStep(
  Parse *pParse, 
  Select *p,
  WhereInfo *pWInfo,
  int regGosub, 
  int addrGosub
){
  Window *pMWin = p->pWin;
  Vdbe *v = sqlite3GetVdbe(pParse);
  int regFlushPart;               /* Register for "Gosub flush_partition" */

  int regArg;
  int csrCurrent = pMWin->iEphCsr;
  int csrWrite = csrCurrent+1;
  int csrStart = csrCurrent+2;
  int csrEnd = csrCurrent+3;

  int regStart;                    /* Value of <expr> PRECEDING */
  int regEnd;                      /* Value of <expr> FOLLOWING */

  int iSubCsr = p->pSrc->a[0].iCursor;      /* Cursor of sub-select */
  int nSub = p->pSrc->a[0].pTab->nCol;      /* Number of cols returned by sub */
  int iCol;                                 /* To iterate through sub cols */

  int addrGoto;
  int addrIf;


  int addrGosubFlush;
  int addrInteger;
  int addrCacheRewind;
  int addrCacheNext;

  int addrShortcut = 0;

  int bCache = windowCachePartition(pMWin);

  int reg = pParse->nMem+1;
  int regRecord = reg+nSub;
  int regRowid = regRecord+1;
  WindowCodeArg s;

  memset(&s, 0, sizeof(WindowCodeArg));
  s.pParse = pParse;
  s.pMWin = pMWin;
  s.pVdbe = v;
  s.regGosub = regGosub;
  s.addrGosub = addrGosub;

  pParse->nMem += 1 + nSub + 1;

  regFlushPart = ++pParse->nMem;
  regStart = ++pParse->nMem;
  regEnd = ++pParse->nMem;

  assert( pMWin->eStart==TK_PRECEDING 
       || pMWin->eStart==TK_CURRENT 
       || pMWin->eStart==TK_FOLLOWING 
       || pMWin->eStart==TK_UNBOUNDED 
  );
  assert( pMWin->eEnd==TK_FOLLOWING 
       || pMWin->eEnd==TK_CURRENT 
       || pMWin->eEnd==TK_UNBOUNDED 
       || pMWin->eEnd==TK_PRECEDING 
  );


  /* Load the column values for the row returned by the sub-select
  ** into an array of registers starting at reg. Assemble them into
  ** a record in register regRecord. TODO: An optimization here? */
  for(iCol=0; iCol<nSub; iCol++){
    sqlite3VdbeAddOp3(v, OP_Column, iSubCsr, iCol, reg+iCol);
  }
  sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, nSub, regRecord);

  /* An input row has just been read into an array of registers starting
  ** at reg. If the window has a PARTITION clause, this block generates 
  ** VM code to check if the input row is the start of a new partition.
  ** If so, it does an OP_Gosub to an address to be filled in later. The
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983


1984
1985
1986
1987
1988
1989
1990
    }
    addrCacheRewind = sqlite3VdbeAddOp1(v, OP_Rewind, csrWrite);
  }else{
    addrIf = sqlite3VdbeAddOp1(v, OP_IfNot, pMWin->regFirst);
  }

  /* This block is run for the first row of each partition */
  regArg = windowInitAccum(pParse, pMWin);

  sqlite3ExprCode(pParse, pMWin->pStart, regStart);
  windowCheckIntValue(pParse, regStart, 0);
  sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
  windowCheckIntValue(pParse, regEnd, 1);

  if( pMWin->eStart==TK_FOLLOWING || pMWin->eEnd==TK_PRECEDING ){


    int op = ((pMWin->eStart==TK_FOLLOWING) ? OP_Ge : OP_Le);
    int addrGe = sqlite3VdbeAddOp3(v, op, regStart, 0, regEnd);
    windowAggFinal(pParse, pMWin, 0);
    if( bCache ){
      sqlite3VdbeAddOp2(v, OP_Rowid, csrWrite, regRowid);
      sqlite3VdbeAddOp3(v, OP_NotExists, csrCurrent, 0, regRowid);
      windowReturnOneRow(pParse, pMWin, regGosub, addrGosub);







|






|
>
>







2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
    }
    addrCacheRewind = sqlite3VdbeAddOp1(v, OP_Rewind, csrWrite);
  }else{
    addrIf = sqlite3VdbeAddOp1(v, OP_IfNot, pMWin->regFirst);
  }

  /* This block is run for the first row of each partition */
  s.regArg = regArg = windowInitAccum(pParse, pMWin);

  sqlite3ExprCode(pParse, pMWin->pStart, regStart);
  windowCheckIntValue(pParse, regStart, 0);
  sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
  windowCheckIntValue(pParse, regEnd, 1);

  if( pMWin->eStart==pMWin->eEnd 
   && pMWin->eStart!=TK_CURRENT && pMWin->eStart!=TK_UNBOUNDED 
  ){
    int op = ((pMWin->eStart==TK_FOLLOWING) ? OP_Ge : OP_Le);
    int addrGe = sqlite3VdbeAddOp3(v, op, regStart, 0, regEnd);
    windowAggFinal(pParse, pMWin, 0);
    if( bCache ){
      sqlite3VdbeAddOp2(v, OP_Rowid, csrWrite, regRowid);
      sqlite3VdbeAddOp3(v, OP_NotExists, csrCurrent, 0, regRowid);
      windowReturnOneRow(pParse, pMWin, regGosub, addrGosub);
2004
2005
2006
2007
2008
2009
2010
2011

2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054

2055

2056
2057
2058
2059
2060

2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072

2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096

2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114

2115
2116
2117
2118
2119
2120
2121
2122

2123
2124
2125
2126
2127
2128
2129
  sqlite3VdbeAddOp2(v, OP_Rewind, csrStart, 1);   sqlite3VdbeChangeP5(v, 1);
  sqlite3VdbeAddOp2(v, OP_Rewind, csrCurrent, 1); sqlite3VdbeChangeP5(v, 1);
  sqlite3VdbeAddOp2(v, OP_Rewind, csrEnd, 1); sqlite3VdbeChangeP5(v, 1);

  sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regFirst);
  addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);

  /* This block is run for the second and subsequent rows of each partition */

  if( bCache ){
    addrCacheNext = sqlite3VdbeCurrentAddr(v);
  }else{
    sqlite3VdbeJumpHere(v, addrIf);
  }

  if( pMWin->eStart==TK_FOLLOWING ){
    addrIfEnd = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0, 1);
    windowAggFinal(pParse, pMWin, 0);
    sqlite3VdbeAddOp2(v, OP_Next, csrCurrent, sqlite3VdbeCurrentAddr(v)+1);
    windowReturnOneRow(pParse, pMWin, regGosub, addrGosub);
    sqlite3VdbeJumpHere(v, addrIfEnd);

    addrIfStart = sqlite3VdbeAddOp3(v, OP_IfPos, regStart, 0, 1);
    sqlite3VdbeAddOp2(v, OP_Next, csrStart, sqlite3VdbeCurrentAddr(v)+1);
    windowAggStep(pParse, pMWin, csrStart, 1, regArg, pMWin->regSize);
    sqlite3VdbeJumpHere(v, addrIfStart);
  }else
  if( pMWin->eEnd==TK_PRECEDING ){
    addrIfEnd = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0, 1);
    sqlite3VdbeAddOp2(v, OP_Next, csrEnd, sqlite3VdbeCurrentAddr(v)+1);
    windowAggStep(pParse, pMWin, csrEnd, 0, regArg, pMWin->regSize);
    sqlite3VdbeJumpHere(v, addrIfEnd);

    windowAggFinal(pParse, pMWin, 0);
    sqlite3VdbeAddOp2(v, OP_Next, csrCurrent, sqlite3VdbeCurrentAddr(v)+1);
    windowReturnOneRow(pParse, pMWin, regGosub, addrGosub);

    addrIfStart = sqlite3VdbeAddOp3(v, OP_IfPos, regStart, 0, 1);
    sqlite3VdbeAddOp2(v, OP_Next, csrStart, sqlite3VdbeCurrentAddr(v)+1);
    windowAggStep(pParse, pMWin, csrStart, 1, regArg, pMWin->regSize);
    sqlite3VdbeJumpHere(v, addrIfStart);
  }else{
    addrIfEnd = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0, 1);
    windowAggFinal(pParse, pMWin, 0);
    sqlite3VdbeAddOp2(v, OP_Next, csrCurrent, sqlite3VdbeCurrentAddr(v)+1);
    windowReturnOneRow(pParse, pMWin, regGosub, addrGosub);
    addrIfStart = sqlite3VdbeAddOp3(v, OP_IfPos, regStart, 0, 1);
    sqlite3VdbeAddOp2(v, OP_Next, csrStart, sqlite3VdbeCurrentAddr(v)+1);
    windowAggStep(pParse, pMWin, csrStart, 1, regArg, pMWin->regSize);
    sqlite3VdbeJumpHere(v, addrIfStart);
    sqlite3VdbeJumpHere(v, addrIfEnd);
  }



  sqlite3VdbeJumpHere(v, addrGoto);
  if( pMWin->eEnd!=TK_PRECEDING ){
    sqlite3VdbeAddOp2(v, OP_Next, csrEnd, sqlite3VdbeCurrentAddr(v)+1);
    windowAggStep(pParse, pMWin, csrEnd, 0, regArg, pMWin->regSize);
  }


  /* End of the main input loop */
  if( bCache ){
    sqlite3VdbeAddOp2(v, OP_Next, csrWrite, addrCacheNext);
    sqlite3VdbeJumpHere(v, addrCacheRewind); 
  }else{
    if( addrShortcut>0 ) sqlite3VdbeJumpHere(v, addrShortcut);
    sqlite3WhereEnd(pWInfo);
  }

  /* Fall through */


  if( pMWin->pPartition && bCache==0 ){
    addrInteger = sqlite3VdbeAddOp2(v, OP_Integer, 0, regFlushPart);
    sqlite3VdbeJumpHere(v, addrGosubFlush);
  }

  if( pMWin->eStart==TK_FOLLOWING ){
    int addrBreak;
    addrIfEnd = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0, 1);
    sqlite3VdbeAddOp2(v, OP_Next, csrCurrent, sqlite3VdbeCurrentAddr(v)+2);
    addrBreak = sqlite3VdbeAddOp0(v, OP_Goto);
    windowAggFinal(pParse, pMWin, 0);
    windowReturnOneRow(pParse, pMWin, regGosub, addrGosub);
    sqlite3VdbeJumpHere(v, addrIfEnd);

    addrIfStart = sqlite3VdbeAddOp3(v, OP_IfPos, regStart, 0, 1);
    sqlite3VdbeAddOp2(v, OP_Next, csrStart, sqlite3VdbeCurrentAddr(v)+2);
    sqlite3VdbeAddOp0(v, OP_Goto);
    windowAggStep(pParse, pMWin, csrStart, 1, regArg, pMWin->regSize);
    sqlite3VdbeJumpHere(v, addrIfStart);
    sqlite3VdbeJumpHere(v, addrIfStart+2);

    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrIfEnd);
    sqlite3VdbeJumpHere(v, addrBreak);
  }else{

    sqlite3VdbeAddOp2(v, OP_Next, csrCurrent, sqlite3VdbeCurrentAddr(v)+2);
    addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
    if( pMWin->eEnd==TK_PRECEDING ){
      addrIfEnd = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0, 1);
      sqlite3VdbeAddOp2(v, OP_Next, csrEnd, sqlite3VdbeCurrentAddr(v)+1);
      windowAggStep(pParse, pMWin, csrEnd, 0, regArg, pMWin->regSize);
      sqlite3VdbeJumpHere(v, addrIfEnd);
      windowAggFinal(pParse, pMWin, 0);
      windowReturnOneRow(pParse, pMWin, regGosub, addrGosub);
    }else{
      windowAggFinal(pParse, pMWin, 0);
      windowReturnOneRow(pParse, pMWin, regGosub, addrGosub);
      addrIfStart = sqlite3VdbeAddOp3(v, OP_IfPos, regStart, 0, 1);
      sqlite3VdbeAddOp2(v, OP_Next, csrStart, sqlite3VdbeCurrentAddr(v)+1);
      windowAggStep(pParse, pMWin, csrStart, 1, regArg, pMWin->regSize);
      sqlite3VdbeJumpHere(v, addrIfStart);
      sqlite3VdbeAddOp2(v, OP_Goto, 0, addrGoto-1);
    }

    sqlite3VdbeJumpHere(v, addrGoto);
  }


  if( bCache && addrShortcut>0 ) sqlite3VdbeJumpHere(v, addrShortcut);
  sqlite3VdbeAddOp1(v, OP_ResetSorter, csrCurrent);
  sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regSize);
  if( bCache==0 ) sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regFirst);

  if( pMWin->pPartition ){
    sqlite3VdbeChangeP1(v, addrInteger, sqlite3VdbeCurrentAddr(v));
    sqlite3VdbeAddOp1(v, OP_Return, regFlushPart);
  }
}

/*







|
>





<

<
<
|
<
<
|
<
<
<
<


<
<
<
<
|
<
|
<
|
<
<
<
<

|
<
|
<
<
|
<
|
<

>

>


<
|

>












>





|
<
<
<
<
<
<
<
|
<
<
<
<
<
<
|
<
<

>
|
<
|
<
<
<
<
|
|

<
<
|
|
<
<
<

>
|

<





>







2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153

2154


2155


2156




2157
2158




2159

2160

2161




2162
2163

2164


2165

2166

2167
2168
2169
2170
2171
2172

2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194







2195






2196


2197
2198
2199

2200




2201
2202
2203


2204
2205



2206
2207
2208
2209

2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
  sqlite3VdbeAddOp2(v, OP_Rewind, csrStart, 1);   sqlite3VdbeChangeP5(v, 1);
  sqlite3VdbeAddOp2(v, OP_Rewind, csrCurrent, 1); sqlite3VdbeChangeP5(v, 1);
  sqlite3VdbeAddOp2(v, OP_Rewind, csrEnd, 1); sqlite3VdbeChangeP5(v, 1);

  sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regFirst);
  addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);

  /* Begin generating SECOND_ROW_CODE */
  VdbeModuleComment((pParse->pVdbe, "Begin windowCodeStep.SECOND_ROW_CODE"));
  if( bCache ){
    addrCacheNext = sqlite3VdbeCurrentAddr(v);
  }else{
    sqlite3VdbeJumpHere(v, addrIf);
  }

  if( pMWin->eStart==TK_FOLLOWING ){


    windowCodeOp(&s, WINDOW_RETURN_ROW, csrCurrent, regEnd, 0);


    windowCodeOp(&s, WINDOW_AGGINVERSE, csrStart, regStart, 0);




  }else
  if( pMWin->eEnd==TK_PRECEDING ){




    windowCodeOp(&s, WINDOW_AGGSTEP, csrEnd, regEnd, 0);

    windowCodeOp(&s, WINDOW_RETURN_ROW, csrCurrent, 0, 0);

    windowCodeOp(&s, WINDOW_AGGINVERSE, csrStart, regStart, 0);




  }else{
    int addr = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0, 1);

    windowCodeOp(&s, WINDOW_RETURN_ROW, csrCurrent, 0, 0);


    windowCodeOp(&s, WINDOW_AGGINVERSE, csrStart, regStart, 0);

    sqlite3VdbeJumpHere(v, addr);

  }
  VdbeModuleComment((pParse->pVdbe, "End windowCodeStep.SECOND_ROW_CODE"));

  VdbeModuleComment((pParse->pVdbe, "Begin windowCodeStep.ALL_ROW_CODE"));
  sqlite3VdbeJumpHere(v, addrGoto);
  if( pMWin->eEnd!=TK_PRECEDING ){

    windowCodeOp(&s, WINDOW_AGGSTEP, csrEnd, 0, 0);
  }
  VdbeModuleComment((pParse->pVdbe, "End windowCodeStep.ALL_ROW_CODE"));

  /* End of the main input loop */
  if( bCache ){
    sqlite3VdbeAddOp2(v, OP_Next, csrWrite, addrCacheNext);
    sqlite3VdbeJumpHere(v, addrCacheRewind); 
  }else{
    if( addrShortcut>0 ) sqlite3VdbeJumpHere(v, addrShortcut);
    sqlite3WhereEnd(pWInfo);
  }

  /* Fall through */

  VdbeModuleComment((pParse->pVdbe, "Begin windowCodeStep.FLUSH_CODE"));
  if( pMWin->pPartition && bCache==0 ){
    addrInteger = sqlite3VdbeAddOp2(v, OP_Integer, 0, regFlushPart);
    sqlite3VdbeJumpHere(v, addrGosubFlush);
  }

  if( pMWin->eEnd==TK_PRECEDING ){







    windowCodeOp(&s, WINDOW_AGGSTEP, csrEnd, regEnd, 1);






    windowCodeOp(&s, WINDOW_RETURN_ROW, csrCurrent, 0, 1);


  }else{
    int addrBreak;
    int addrStart = sqlite3VdbeCurrentAddr(v);

    if( pMWin->eStart==TK_FOLLOWING ){




      addrBreak = windowCodeOp(&s, WINDOW_RETURN_ROW, csrCurrent, regEnd, 1);
      windowCodeOp(&s, WINDOW_AGGINVERSE, csrStart, regStart, 1);
    }else{


      addrBreak = windowCodeOp(&s, WINDOW_RETURN_ROW, csrCurrent, 0, 1);
      windowCodeOp(&s, WINDOW_AGGINVERSE, csrStart, regStart, 0);



    }
    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrStart);
    sqlite3VdbeJumpHere(v, addrBreak);
  }


  if( bCache && addrShortcut>0 ) sqlite3VdbeJumpHere(v, addrShortcut);
  sqlite3VdbeAddOp1(v, OP_ResetSorter, csrCurrent);
  sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regSize);
  if( bCache==0 ) sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regFirst);
  VdbeModuleComment((pParse->pVdbe, "End windowCodeStep.FLUSH_CODE"));
  if( pMWin->pPartition ){
    sqlite3VdbeChangeP1(v, addrInteger, sqlite3VdbeCurrentAddr(v));
    sqlite3VdbeAddOp1(v, OP_Return, regFlushPart);
  }
}

/*