|
|
001782 |
Subject: [PATCH] Prevent aliases of window functions expressions from being
|
|
|
001782 |
used as arguments to aggregate or other window functions.
|
|
|
001782 |
|
|
|
001782 |
---
|
|
|
001782 |
src/resolve.c | 21 ++++++---
|
|
|
001782 |
src/sqliteInt.h | 2 +
|
|
|
001782 |
test/windowerr.tcl | 59 ++++++++++++++++++++++++++
|
|
|
001782 |
test/windowerr.test | 99 ++++++++++++++++++++++++++++++++++++++++++
|
|
|
001782 |
4 files changed, 176 insertions(+), 5 deletions(-)
|
|
|
001782 |
create mode 100644 test/windowerr.tcl
|
|
|
001782 |
create mode 100644 test/windowerr.test
|
|
|
001782 |
|
|
|
001782 |
diff --git a/src/resolve.c b/src/resolve.c
|
|
|
001782 |
index 0c7dfc0..cdcf4d9 100644
|
|
|
001782 |
--- a/src/resolve.c
|
|
|
001782 |
+++ b/src/resolve.c
|
|
|
001782 |
@@ -436,6 +436,10 @@ static int lookupName(
|
|
|
001782 |
sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
|
|
|
001782 |
return WRC_Abort;
|
|
|
001782 |
}
|
|
|
001782 |
+ if( (pNC->ncFlags&NC_AllowWin)==0 && ExprHasProperty(pOrig, EP_Win) ){
|
|
|
001782 |
+ sqlite3ErrorMsg(pParse, "misuse of aliased window function %s",zAs);
|
|
|
001782 |
+ return WRC_Abort;
|
|
|
001782 |
+ }
|
|
|
001782 |
if( sqlite3ExprVectorSize(pOrig)!=1 ){
|
|
|
001782 |
sqlite3ErrorMsg(pParse, "row value misused");
|
|
|
001782 |
return WRC_Abort;
|
|
|
001782 |
@@ -707,6 +711,7 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){
|
|
|
001782 |
const char *zId; /* The function name. */
|
|
|
001782 |
FuncDef *pDef; /* Information about the function */
|
|
|
001782 |
u8 enc = ENC(pParse->db); /* The database encoding */
|
|
|
001782 |
+ int savedAllowFlags = (pNC->ncFlags & (NC_AllowAgg | NC_AllowWin));
|
|
|
001782 |
|
|
|
001782 |
assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
|
|
|
001782 |
zId = pExpr->u.zToken;
|
|
|
001782 |
@@ -828,8 +833,11 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){
|
|
|
001782 |
pNC->nErr++;
|
|
|
001782 |
}
|
|
|
001782 |
if( is_agg ){
|
|
|
001782 |
+ /* Window functions may not be arguments of aggregate functions.
|
|
|
001782 |
+ ** Or arguments of other window functions. But aggregate functions
|
|
|
001782 |
+ ** may be arguments for window functions. */
|
|
|
001782 |
#ifndef SQLITE_OMIT_WINDOWFUNC
|
|
|
001782 |
- pNC->ncFlags &= ~(pExpr->y.pWin ? NC_AllowWin : NC_AllowAgg);
|
|
|
001782 |
+ pNC->ncFlags &= ~(NC_AllowWin | (!pExpr->y.pWin ? NC_AllowAgg : 0));
|
|
|
001782 |
#else
|
|
|
001782 |
pNC->ncFlags &= ~NC_AllowAgg;
|
|
|
001782 |
#endif
|
|
|
001782 |
@@ -850,7 +858,7 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){
|
|
|
001782 |
pExpr->y.pWin->pNextWin = pSel->pWin;
|
|
|
001782 |
pSel->pWin = pExpr->y.pWin;
|
|
|
001782 |
}
|
|
|
001782 |
- pNC->ncFlags |= NC_AllowWin;
|
|
|
001782 |
+ pNC->ncFlags |= NC_HasWin;
|
|
|
001782 |
}else
|
|
|
001782 |
#endif /* SQLITE_OMIT_WINDOWFUNC */
|
|
|
001782 |
{
|
|
|
001782 |
@@ -868,8 +876,8 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){
|
|
|
001782 |
pNC2->ncFlags |= NC_HasAgg | (pDef->funcFlags & SQLITE_FUNC_MINMAX);
|
|
|
001782 |
|
|
|
001782 |
}
|
|
|
001782 |
- pNC->ncFlags |= NC_AllowAgg;
|
|
|
001782 |
}
|
|
|
001782 |
+ pNC->ncFlags |= savedAllowFlags;
|
|
|
001782 |
}
|
|
|
001782 |
/* FIX ME: Compute pExpr->affinity based on the expected return
|
|
|
001782 |
** type of the function
|
|
|
001782 |
@@ -1573,8 +1581,8 @@ int sqlite3ResolveExprNames(
|
|
|
001782 |
Walker w;
|
|
|
001782 |
|
|
|
001782 |
if( pExpr==0 ) return SQLITE_OK;
|
|
|
001782 |
- savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg);
|
|
|
001782 |
- pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg);
|
|
|
001782 |
+ savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin);
|
|
|
001782 |
+ pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin);
|
|
|
001782 |
w.pParse = pNC->pParse;
|
|
|
001782 |
w.xExprCallback = resolveExprStep;
|
|
|
001782 |
w.xSelectCallback = resolveSelectStep;
|
|
|
001782 |
@@ -1593,6 +1601,9 @@ int sqlite3ResolveExprNames(
|
|
|
001782 |
if( pNC->ncFlags & NC_HasAgg ){
|
|
|
001782 |
ExprSetProperty(pExpr, EP_Agg);
|
|
|
001782 |
}
|
|
|
001782 |
+ if( pNC->ncFlags & NC_HasWin ){
|
|
|
001782 |
+ ExprSetProperty(pExpr, EP_Win);
|
|
|
001782 |
+ }
|
|
|
001782 |
pNC->ncFlags |= savedHasAgg;
|
|
|
001782 |
return pNC->nErr>0 || w.pParse->nErr>0;
|
|
|
001782 |
}
|
|
|
001782 |
diff --git a/src/sqliteInt.h b/src/sqliteInt.h
|
|
|
001782 |
index 5f5f3cc..b7d3571 100644
|
|
|
001782 |
--- a/src/sqliteInt.h
|
|
|
001782 |
+++ b/src/sqliteInt.h
|
|
|
001782 |
@@ -2517,6 +2517,7 @@ struct Expr {
|
|
|
001782 |
#define EP_Alias 0x400000 /* Is an alias for a result set column */
|
|
|
001782 |
#define EP_Leaf 0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */
|
|
|
001782 |
#define EP_WinFunc 0x1000000 /* TK_FUNCTION with Expr.y.pWin set */
|
|
|
001782 |
+#define EP_Win 0x8000000 /* Contains window functions */
|
|
|
001782 |
|
|
|
001782 |
/*
|
|
|
001782 |
** The EP_Propagate mask is a set of properties that automatically propagate
|
|
|
001782 |
@@ -2773,6 +2774,7 @@ struct NameContext {
|
|
|
001782 |
#define NC_MinMaxAgg 0x1000 /* min/max aggregates seen. See note above */
|
|
|
001782 |
#define NC_Complex 0x2000 /* True if a function or subquery seen */
|
|
|
001782 |
#define NC_AllowWin 0x4000 /* Window functions are allowed here */
|
|
|
001782 |
+#define NC_HasWin 0x8000 /* One or more window functions seen */
|
|
|
001782 |
|
|
|
001782 |
/*
|
|
|
001782 |
** An instance of the following object describes a single ON CONFLICT
|
|
|
001782 |
diff --git a/test/windowerr.tcl b/test/windowerr.tcl
|
|
|
001782 |
new file mode 100644
|
|
|
001782 |
index 0000000..80f464d
|
|
|
001782 |
--- /dev/null
|
|
|
001782 |
+++ b/test/windowerr.tcl
|
|
|
001782 |
@@ -0,0 +1,59 @@
|
|
|
001782 |
+# 2018 May 19
|
|
|
001782 |
+#
|
|
|
001782 |
+# The author disclaims copyright to this source code. In place of
|
|
|
001782 |
+# a legal notice, here is a blessing:
|
|
|
001782 |
+#
|
|
|
001782 |
+# May you do good and not evil.
|
|
|
001782 |
+# May you find forgiveness for yourself and forgive others.
|
|
|
001782 |
+# May you share freely, never taking more than you give.
|
|
|
001782 |
+#
|
|
|
001782 |
+#***********************************************************************
|
|
|
001782 |
+#
|
|
|
001782 |
+
|
|
|
001782 |
+source [file join [file dirname $argv0] pg_common.tcl]
|
|
|
001782 |
+
|
|
|
001782 |
+#=========================================================================
|
|
|
001782 |
+
|
|
|
001782 |
+start_test windowerr "2019 March 01"
|
|
|
001782 |
+ifcapable !windowfunc
|
|
|
001782 |
+
|
|
|
001782 |
+execsql_test 1.0 {
|
|
|
001782 |
+ DROP TABLE IF EXISTS t1;
|
|
|
001782 |
+ CREATE TABLE t1(a INTEGER, b INTEGER);
|
|
|
001782 |
+ INSERT INTO t1 VALUES(1, 1);
|
|
|
001782 |
+ INSERT INTO t1 VALUES(2, 2);
|
|
|
001782 |
+ INSERT INTO t1 VALUES(3, 3);
|
|
|
001782 |
+ INSERT INTO t1 VALUES(4, 4);
|
|
|
001782 |
+ INSERT INTO t1 VALUES(5, 5);
|
|
|
001782 |
+}
|
|
|
001782 |
+
|
|
|
001782 |
+foreach {tn frame} {
|
|
|
001782 |
+ 1 "ORDER BY a ROWS BETWEEN -1 PRECEDING AND 1 FOLLOWING"
|
|
|
001782 |
+ 2 "ORDER BY a ROWS BETWEEN 1 PRECEDING AND -1 FOLLOWING"
|
|
|
001782 |
+
|
|
|
001782 |
+ 3 "ORDER BY a RANGE BETWEEN -1 PRECEDING AND 1 FOLLOWING"
|
|
|
001782 |
+ 4 "ORDER BY a RANGE BETWEEN 1 PRECEDING AND -1 FOLLOWING"
|
|
|
001782 |
+
|
|
|
001782 |
+ 5 "ORDER BY a GROUPS BETWEEN -1 PRECEDING AND 1 FOLLOWING"
|
|
|
001782 |
+ 6 "ORDER BY a GROUPS BETWEEN 1 PRECEDING AND -1 FOLLOWING"
|
|
|
001782 |
+
|
|
|
001782 |
+ 7 "ORDER BY a,b RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING"
|
|
|
001782 |
+
|
|
|
001782 |
+ 8 "PARTITION BY a RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING"
|
|
|
001782 |
+} {
|
|
|
001782 |
+ errorsql_test 1.$tn "
|
|
|
001782 |
+ SELECT a, sum(b) OVER (
|
|
|
001782 |
+ $frame
|
|
|
001782 |
+ ) FROM t1 ORDER BY 1
|
|
|
001782 |
+ "
|
|
|
001782 |
+}
|
|
|
001782 |
+errorsql_test 2.1 {
|
|
|
001782 |
+ SELECT sum( sum(a) OVER () ) FROM t1;
|
|
|
001782 |
+}
|
|
|
001782 |
+
|
|
|
001782 |
+errorsql_test 2.2 {
|
|
|
001782 |
+ SELECT sum(a) OVER () AS xyz FROM t1 ORDER BY sum(xyz);
|
|
|
001782 |
+}
|
|
|
001782 |
+
|
|
|
001782 |
+
|
|
|
001782 |
+finish_test
|
|
|
001782 |
diff --git a/test/windowerr.test b/test/windowerr.test
|
|
|
001782 |
new file mode 100644
|
|
|
001782 |
index 0000000..97dae64
|
|
|
001782 |
--- /dev/null
|
|
|
001782 |
+++ b/test/windowerr.test
|
|
|
001782 |
@@ -0,0 +1,99 @@
|
|
|
001782 |
+# 2019 March 01
|
|
|
001782 |
+#
|
|
|
001782 |
+# The author disclaims copyright to this source code. In place of
|
|
|
001782 |
+# a legal notice, here is a blessing:
|
|
|
001782 |
+#
|
|
|
001782 |
+# May you do good and not evil.
|
|
|
001782 |
+# May you find forgiveness for yourself and forgive others.
|
|
|
001782 |
+# May you share freely, never taking more than you give.
|
|
|
001782 |
+#
|
|
|
001782 |
+#***********************************************************************
|
|
|
001782 |
+# This file implements regression tests for SQLite library.
|
|
|
001782 |
+#
|
|
|
001782 |
+
|
|
|
001782 |
+####################################################
|
|
|
001782 |
+# DO NOT EDIT! THIS FILE IS AUTOMATICALLY GENERATED!
|
|
|
001782 |
+####################################################
|
|
|
001782 |
+
|
|
|
001782 |
+set testdir [file dirname $argv0]
|
|
|
001782 |
+source $testdir/tester.tcl
|
|
|
001782 |
+set testprefix windowerr
|
|
|
001782 |
+
|
|
|
001782 |
+ifcapable !windowfunc { finish_test ; return }
|
|
|
001782 |
+do_execsql_test 1.0 {
|
|
|
001782 |
+ DROP TABLE IF EXISTS t1;
|
|
|
001782 |
+ CREATE TABLE t1(a INTEGER, b INTEGER);
|
|
|
001782 |
+ INSERT INTO t1 VALUES(1, 1);
|
|
|
001782 |
+ INSERT INTO t1 VALUES(2, 2);
|
|
|
001782 |
+ INSERT INTO t1 VALUES(3, 3);
|
|
|
001782 |
+ INSERT INTO t1 VALUES(4, 4);
|
|
|
001782 |
+ INSERT INTO t1 VALUES(5, 5);
|
|
|
001782 |
+} {}
|
|
|
001782 |
+
|
|
|
001782 |
+# PG says ERROR: frame starting offset must not be negative
|
|
|
001782 |
+do_test 1.1 { catch { execsql {
|
|
|
001782 |
+ SELECT a, sum(b) OVER (
|
|
|
001782 |
+ ORDER BY a ROWS BETWEEN -1 PRECEDING AND 1 FOLLOWING
|
|
|
001782 |
+ ) FROM t1 ORDER BY 1
|
|
|
001782 |
+} } } 1
|
|
|
001782 |
+
|
|
|
001782 |
+# PG says ERROR: frame ending offset must not be negative
|
|
|
001782 |
+do_test 1.2 { catch { execsql {
|
|
|
001782 |
+ SELECT a, sum(b) OVER (
|
|
|
001782 |
+ ORDER BY a ROWS BETWEEN 1 PRECEDING AND -1 FOLLOWING
|
|
|
001782 |
+ ) FROM t1 ORDER BY 1
|
|
|
001782 |
+} } } 1
|
|
|
001782 |
+
|
|
|
001782 |
+# PG says ERROR: invalid preceding or following size in window function
|
|
|
001782 |
+do_test 1.3 { catch { execsql {
|
|
|
001782 |
+ SELECT a, sum(b) OVER (
|
|
|
001782 |
+ ORDER BY a RANGE BETWEEN -1 PRECEDING AND 1 FOLLOWING
|
|
|
001782 |
+ ) FROM t1 ORDER BY 1
|
|
|
001782 |
+} } } 1
|
|
|
001782 |
+
|
|
|
001782 |
+# PG says ERROR: invalid preceding or following size in window function
|
|
|
001782 |
+do_test 1.4 { catch { execsql {
|
|
|
001782 |
+ SELECT a, sum(b) OVER (
|
|
|
001782 |
+ ORDER BY a RANGE BETWEEN 1 PRECEDING AND -1 FOLLOWING
|
|
|
001782 |
+ ) FROM t1 ORDER BY 1
|
|
|
001782 |
+} } } 1
|
|
|
001782 |
+
|
|
|
001782 |
+# PG says ERROR: frame starting offset must not be negative
|
|
|
001782 |
+do_test 1.5 { catch { execsql {
|
|
|
001782 |
+ SELECT a, sum(b) OVER (
|
|
|
001782 |
+ ORDER BY a GROUPS BETWEEN -1 PRECEDING AND 1 FOLLOWING
|
|
|
001782 |
+ ) FROM t1 ORDER BY 1
|
|
|
001782 |
+} } } 1
|
|
|
001782 |
+
|
|
|
001782 |
+# PG says ERROR: frame ending offset must not be negative
|
|
|
001782 |
+do_test 1.6 { catch { execsql {
|
|
|
001782 |
+ SELECT a, sum(b) OVER (
|
|
|
001782 |
+ ORDER BY a GROUPS BETWEEN 1 PRECEDING AND -1 FOLLOWING
|
|
|
001782 |
+ ) FROM t1 ORDER BY 1
|
|
|
001782 |
+} } } 1
|
|
|
001782 |
+
|
|
|
001782 |
+# PG says ERROR: RANGE with offset PRECEDING/FOLLOWING requires exactly one ORDER BY column
|
|
|
001782 |
+do_test 1.7 { catch { execsql {
|
|
|
001782 |
+ SELECT a, sum(b) OVER (
|
|
|
001782 |
+ ORDER BY a,b RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING
|
|
|
001782 |
+ ) FROM t1 ORDER BY 1
|
|
|
001782 |
+} } } 1
|
|
|
001782 |
+
|
|
|
001782 |
+# PG says ERROR: RANGE with offset PRECEDING/FOLLOWING requires exactly one ORDER BY column
|
|
|
001782 |
+do_test 1.8 { catch { execsql {
|
|
|
001782 |
+ SELECT a, sum(b) OVER (
|
|
|
001782 |
+ PARTITION BY a RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING
|
|
|
001782 |
+ ) FROM t1 ORDER BY 1
|
|
|
001782 |
+} } } 1
|
|
|
001782 |
+
|
|
|
001782 |
+# PG says ERROR: aggregate function calls cannot contain window function calls
|
|
|
001782 |
+do_test 2.1 { catch { execsql {
|
|
|
001782 |
+ SELECT sum( sum(a) OVER () ) FROM t1;
|
|
|
001782 |
+} } } 1
|
|
|
001782 |
+
|
|
|
001782 |
+# PG says ERROR: column "xyz" does not exist
|
|
|
001782 |
+do_test 2.2 { catch { execsql {
|
|
|
001782 |
+ SELECT sum(a) OVER () AS xyz FROM t1 ORDER BY sum(xyz);
|
|
|
001782 |
+} } } 1
|
|
|
001782 |
+
|
|
|
001782 |
+finish_test
|
|
|
001782 |
--
|
|
|
001782 |
2.24.1
|
|
|
001782 |
|