Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Improvements to the way built-in window functions are handled. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | window-functions |
Files: | files | file ages | folders |
SHA3-256: |
e8eee566dfca6f4c8af074731dfe91f7 |
User & Date: | dan 2019-03-06 17:12:32.566 |
Context
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) | |
2019-03-05
| ||
19:29 | Extend windowCodeStep() to handle any ROWS PRECEDING/FOLLOWING frame specification. (check-in: af0ea13635 user: dan tags: window-functions) | |
Changes
Changes to src/sqliteInt.h.
︙ | ︙ | |||
3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 | int regPart; /* First in a set of registers holding PARTITION BY ** and ORDER BY values for the window */ Expr *pOwner; /* Expression object this window is attached to */ int nBufferCol; /* Number of columns in buffer table */ int iArgCol; /* Offset of first argument for this function */ int regFirst; }; #ifndef SQLITE_OMIT_WINDOWFUNC void sqlite3WindowDelete(sqlite3*, Window*); void sqlite3WindowListDelete(sqlite3 *db, Window *p); Window *sqlite3WindowAlloc(Parse*, int, int, Expr*, int , Expr*); void sqlite3WindowAttach(Parse*, Expr*, Window*); | > | 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 | int regPart; /* First in a set of registers holding PARTITION BY ** and ORDER BY values for the window */ Expr *pOwner; /* Expression object this window is attached to */ int nBufferCol; /* Number of columns in buffer table */ int iArgCol; /* Offset of first argument for this function */ int regFirst; int regSize; }; #ifndef SQLITE_OMIT_WINDOWFUNC void sqlite3WindowDelete(sqlite3*, Window*); void sqlite3WindowListDelete(sqlite3 *db, Window *p); Window *sqlite3WindowAlloc(Parse*, int, int, Expr*, int , Expr*); void sqlite3WindowAttach(Parse*, Expr*, Window*); |
︙ | ︙ |
Changes to src/window.c.
︙ | ︙ | |||
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 | pMWin->regPart = pParse->nMem+1; pParse->nMem += nPart; sqlite3VdbeAddOp3(v, OP_Null, 0, pMWin->regPart, pMWin->regPart+nPart-1); } pMWin->regFirst = ++pParse->nMem; sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regFirst); for(pWin=pMWin; pWin; pWin=pWin->pNextWin){ FuncDef *p = pWin->pFunc; if( (p->funcFlags & SQLITE_FUNC_MINMAX) && pWin->eStart!=TK_UNBOUNDED ){ /* The inline versions of min() and max() require a single ephemeral ** table and 3 registers. The registers are used as follows: ** | > > | 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 | pMWin->regPart = pParse->nMem+1; pParse->nMem += nPart; sqlite3VdbeAddOp3(v, OP_Null, 0, pMWin->regPart, pMWin->regPart+nPart-1); } pMWin->regFirst = ++pParse->nMem; sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regFirst); pMWin->regSize = ++pParse->nMem; sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regSize); for(pWin=pMWin; pWin; pWin=pWin->pNextWin){ FuncDef *p = pWin->pFunc; if( (p->funcFlags & SQLITE_FUNC_MINMAX) && pWin->eStart!=TK_UNBOUNDED ){ /* The inline versions of min() and max() require a single ephemeral ** table and 3 registers. The registers are used as follows: ** |
︙ | ︙ | |||
1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 | sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr); sqlite3VdbeAddOp1(v, OP_Return, regFlushPart); VdbeComment((v, "end flush_partition subroutine")); /* Jump to here to skip over flush_partition */ sqlite3VdbeJumpHere(v, addrGoto); } static void windowCodeStep( Parse *pParse, Select *p, WhereInfo *pWInfo, int regGosub, int addrGosub | > > > > > > > > > > > > > > > > > > > > | 1838 1839 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 | sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr); sqlite3VdbeAddOp1(v, OP_Return, regFlushPart); 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 |
︙ | ︙ | |||
1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 | int addrGoto; int addrIf; int addrIfEnd; int addrIfStart; int addrGosubFlush; int addrInteger; int addrShortcut = 0; int reg = pParse->nMem+1; int regRecord = reg+nSub; int regRowid = regRecord+1; pParse->nMem += 1 + nSub + 1; regFlushPart = ++pParse->nMem; regStart = ++pParse->nMem; regEnd = ++pParse->nMem; | > > > > > > | 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 | 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; |
︙ | ︙ | |||
1899 1900 1901 1902 1903 1904 1905 | ** 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); | > > | > > > | > > < > > > | | > > | > | < > > > > > > > | | | > > > > | > | | | | | > > > > | | > | | | | > | > | 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 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 | ** 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 ** address of the OP_Gosub is stored in local variable addrGosubFlush. */ if( pMWin->pPartition ){ int addr; ExprList *pPart = pMWin->pPartition; int nPart = pPart->nExpr; int regNewPart = reg + pMWin->nBufferCol; KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0); addrIf = sqlite3VdbeAddOp1(v, OP_If, pMWin->regFirst); addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart, nPart); sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO); sqlite3VdbeAddOp3(v, OP_Jump, addr+2, addr+4, addr+2); VdbeCoverageEqNe(v); addrGosubFlush = sqlite3VdbeAddOp1(v, OP_Gosub, regFlushPart); VdbeComment((v, "call flush_partition")); sqlite3VdbeJumpHere(v, addrIf); sqlite3VdbeAddOp3(v, OP_Copy, regNewPart, pMWin->regPart, nPart-1); } /* Insert the new row into the ephemeral table */ sqlite3VdbeAddOp2(v, OP_NewRowid, csrWrite, regRowid); sqlite3VdbeAddOp3(v, OP_Insert, csrWrite, regRecord, regRowid); sqlite3VdbeAddOp2(v, OP_AddImm, pMWin->regSize, 1); if( bCache ){ sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regFirst); sqlite3WhereEnd(pWInfo); addrInteger = sqlite3VdbeAddOp2(v, OP_Integer, 0, regFlushPart); if( pMWin->pPartition ){ sqlite3VdbeJumpHere(v, addrGosubFlush); } 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); sqlite3VdbeAddOp2(v, OP_Next, csrWrite, addrCacheRewind+1); }else{ sqlite3VdbeAddOp2(v, OP_Rewind, csrCurrent, 1); windowReturnOneRow(pParse, pMWin, regGosub, addrGosub); sqlite3VdbeAddOp1(v, OP_ResetSorter, csrCurrent); } addrShortcut = sqlite3VdbeAddOp0(v, OP_Goto); sqlite3VdbeJumpHere(v, addrGe); } if( pMWin->eStart==TK_FOLLOWING ){ sqlite3VdbeAddOp3(v, OP_Subtract, regStart, regEnd, regStart); } 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); } } /* |
︙ | ︙ | |||
2526 2527 2528 2529 2530 2531 2532 | ** windowCodeDefaultStep() is the only one of the three functions that ** does not cache each partition in a temp table before beginning to ** return rows. */ if( pMWin->eType==TK_ROWS && (pMWin->eStart!=TK_UNBOUNDED||pMWin->eEnd!=TK_CURRENT||!pMWin->pOrderBy) ){ | < < < < < < < < < < < < < < < | | | 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 | ** windowCodeDefaultStep() is the only one of the three functions that ** does not cache each partition in a temp table before beginning to ** return rows. */ if( pMWin->eType==TK_ROWS && (pMWin->eStart!=TK_UNBOUNDED||pMWin->eEnd!=TK_CURRENT||!pMWin->pOrderBy) ){ if( (pMWin->eStart!=TK_PRECEDING && pMWin->eStart!=TK_FOLLOWING) || (pMWin->eEnd!=TK_FOLLOWING && pMWin->eEnd!=TK_PRECEDING) ){ VdbeModuleComment((pParse->pVdbe, "Begin RowExprStep()")); windowCodeRowExprStep(pParse, p, pWInfo, regGosub, addrGosub); VdbeModuleComment((pParse->pVdbe, "End RowExprStep()")); }else{ VdbeModuleComment((pParse->pVdbe, "Begin windowCodeStep()")); windowCodeStep(pParse, p, pWInfo, regGosub, addrGosub); |
︙ | ︙ |
Changes to test/window4.tcl.
|
| | | 1 2 3 4 5 6 7 8 | ## 2018 May 19 # # The author disclaims copyright to this source code. In place of # a legal notice, here is a blessing: # # May you do good and not evil. # May you find forgiveness for yourself and forgive others. # May you share freely, never taking more than you give. |
︙ | ︙ |