From 43d8164c0014dee25a42bd7c6f4ab0afa981f6d0 Mon Sep 17 00:00:00 2001 From: belong326 Date: Wed, 9 Oct 2024 09:48:55 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E8=93=9D=E9=BB=84=E4=B8=80=E8=87=B4?= =?UTF-8?q?=E6=80=A7=E3=80=91=E5=90=8C=E6=AD=A5=E9=BB=84=E5=8C=BA=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: belong326 Change-Id: Icc2049e7174fd30ab0a1533f411b38d3c7669739 --- amalgamation/rename_exports.h | 2 +- amalgamation/sqlite3.c | 13 ++++++-- amalgamation/sqlite3.h | 2 +- amalgamation_dev/rename_exports.h | 2 +- amalgamation_dev/sqlite3.c | 31 ++++++++++++------ amalgamation_dev/sqlite3.h | 2 +- src/resolve.c | 7 ++-- src/sqliteInt.h | 1 + src/where.c | 5 ++- test/aggnested.test | 54 +++++++++++++++++++++++++++++++ test/window1.test | 2 +- 11 files changed, 100 insertions(+), 21 deletions(-) diff --git a/amalgamation/rename_exports.h b/amalgamation/rename_exports.h index 52274ef..36b6c9d 100644 --- a/amalgamation/rename_exports.h +++ b/amalgamation/rename_exports.h @@ -1,4 +1,4 @@ -// Copyright 2023 The Chromium Authors. All rights reserved. +// Copyright 2024 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. diff --git a/amalgamation/sqlite3.c b/amalgamation/sqlite3.c index 560ea7a..df9391c 100644 --- a/amalgamation/sqlite3.c +++ b/amalgamation/sqlite3.c @@ -454,7 +454,7 @@ extern "C" { */ #define SQLITE_VERSION "3.41.2" #define SQLITE_VERSION_NUMBER 3041002 -#define SQLITE_SOURCE_ID "2023-03-22 11:56:21 c5a8ed5442668c558f8330bc68beb9e9e6396526f364a63f7a6cb812639aff78" +#define SQLITE_SOURCE_ID "2023-03-22 11:56:21 c5a8ed5442668c558f8330bc68beb9e9e6396526f364a63f7a6cb812639aalt1" /* ** CAPI3REF: Run-Time Library Version Numbers @@ -18804,6 +18804,7 @@ struct NameContext { int nRef; /* Number of names resolved by this context */ int nNcErr; /* Number of errors encountered while resolving names */ int ncFlags; /* Zero or more NC_* flags defined below */ + int nNestedSelect; /* Number of nested selects using this NC */ Select *pWinSelect; /* SELECT statement for any window functions */ }; @@ -104749,11 +104750,12 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){ while( pNC2 && sqlite3ReferencesSrcList(pParse, pExpr, pNC2->pSrcList)==0 ){ - pExpr->op2++; + pExpr->op2 += (1 + pNC2->nNestedSelect); pNC2 = pNC2->pNext; } assert( pDef!=0 || IN_RENAME_OBJECT ); if( pNC2 && pDef ){ + pExpr->op2 += pNC2->nNestedSelect; assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg ); assert( SQLITE_FUNC_ANYORDER==NC_OrderAgg ); testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 ); @@ -105314,6 +105316,7 @@ static int resolveSelectStep(Walker *pWalker, Select *p){ /* Recursively resolve names in all subqueries in the FROM clause */ + if( pOuterNC ) pOuterNC->nNestedSelect++; for(i=0; ipSrc->nSrc; i++){ SrcItem *pItem = &p->pSrc->a[i]; if( pItem->pSelect && (pItem->pSelect->selFlags & SF_Resolved)==0 ){ @@ -105338,6 +105341,7 @@ static int resolveSelectStep(Walker *pWalker, Select *p){ } } } + if( pOuterNC ) pOuterNC->nNestedSelect--; /* Set up the local name-context to pass to sqlite3ResolveExprNames() to ** resolve the result-set expression list. @@ -162770,7 +162774,10 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin( /* An ORDER/GROUP BY clause of more than 63 terms cannot be optimized */ testcase( pOrderBy && pOrderBy->nExpr==BMS-1 ); - if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0; + if( pOrderBy && pOrderBy->nExpr>=BMS ){ + pOrderBy = 0; + wctrlFlags &= ~WHERE_WANT_DISTINCT; + } /* The number of tables in the FROM clause is limited by the number of ** bits in a Bitmask diff --git a/amalgamation/sqlite3.h b/amalgamation/sqlite3.h index e967249..c87819b 100644 --- a/amalgamation/sqlite3.h +++ b/amalgamation/sqlite3.h @@ -148,7 +148,7 @@ extern "C" { */ #define SQLITE_VERSION "3.41.2" #define SQLITE_VERSION_NUMBER 3041002 -#define SQLITE_SOURCE_ID "2023-03-22 11:56:21 c5a8ed5442668c558f8330bc68beb9e9e6396526f364a63f7a6cb812639aff78" +#define SQLITE_SOURCE_ID "2023-03-22 11:56:21 c5a8ed5442668c558f8330bc68beb9e9e6396526f364a63f7a6cb812639aalt1" /* ** CAPI3REF: Run-Time Library Version Numbers diff --git a/amalgamation_dev/rename_exports.h b/amalgamation_dev/rename_exports.h index 52274ef..36b6c9d 100644 --- a/amalgamation_dev/rename_exports.h +++ b/amalgamation_dev/rename_exports.h @@ -1,4 +1,4 @@ -// Copyright 2023 The Chromium Authors. All rights reserved. +// Copyright 2024 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. diff --git a/amalgamation_dev/sqlite3.c b/amalgamation_dev/sqlite3.c index 0819ea6..1455fa5 100644 --- a/amalgamation_dev/sqlite3.c +++ b/amalgamation_dev/sqlite3.c @@ -454,7 +454,7 @@ extern "C" { */ #define SQLITE_VERSION "3.41.2" #define SQLITE_VERSION_NUMBER 3041002 -#define SQLITE_SOURCE_ID "2023-03-22 11:56:21 c5a8ed5442668c558f8330bc68beb9e9e6396526f364a63f7a6cb812639aff78" +#define SQLITE_SOURCE_ID "2023-03-22 11:56:21 c5a8ed5442668c558f8330bc68beb9e9e6396526f364a63f7a6cb812639aalt1" /* ** CAPI3REF: Run-Time Library Version Numbers @@ -18817,6 +18817,7 @@ struct NameContext { int nRef; /* Number of names resolved by this context */ int nNcErr; /* Number of errors encountered while resolving names */ int ncFlags; /* Zero or more NC_* flags defined below */ + int nNestedSelect; /* Number of nested selects using this NC */ Select *pWinSelect; /* SELECT statement for any window functions */ }; @@ -104762,11 +104763,12 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){ while( pNC2 && sqlite3ReferencesSrcList(pParse, pExpr, pNC2->pSrcList)==0 ){ - pExpr->op2++; + pExpr->op2 += (1 + pNC2->nNestedSelect); pNC2 = pNC2->pNext; } assert( pDef!=0 || IN_RENAME_OBJECT ); if( pNC2 && pDef ){ + pExpr->op2 += pNC2->nNestedSelect; assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg ); assert( SQLITE_FUNC_ANYORDER==NC_OrderAgg ); testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 ); @@ -105327,6 +105329,7 @@ static int resolveSelectStep(Walker *pWalker, Select *p){ /* Recursively resolve names in all subqueries in the FROM clause */ + if( pOuterNC ) pOuterNC->nNestedSelect++; for(i=0; ipSrc->nSrc; i++){ SrcItem *pItem = &p->pSrc->a[i]; if( pItem->pSelect && (pItem->pSelect->selFlags & SF_Resolved)==0 ){ @@ -105351,6 +105354,7 @@ static int resolveSelectStep(Walker *pWalker, Select *p){ } } } + if( pOuterNC ) pOuterNC->nNestedSelect--; /* Set up the local name-context to pass to sqlite3ResolveExprNames() to ** resolve the result-set expression list. @@ -162783,7 +162787,10 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin( /* An ORDER/GROUP BY clause of more than 63 terms cannot be optimized */ testcase( pOrderBy && pOrderBy->nExpr==BMS-1 ); - if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0; + if( pOrderBy && pOrderBy->nExpr>=BMS ){ + pOrderBy = 0; + wctrlFlags &= ~WHERE_WANT_DISTINCT; + } /* The number of tables in the FROM clause is limited by the number of ** bits in a Bitmask @@ -219195,15 +219202,19 @@ static int sessionReadRecord( } } if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){ - sqlite3_int64 v = sessionGetI64(aVal); - if( eType==SQLITE_INTEGER ){ - sqlite3VdbeMemSetInt64(apOut[i], v); + if( (pIn->nData-pIn->iNext)<8 ){ + rc = SQLITE_CORRUPT_BKPT; }else{ - double d; - memcpy(&d, &v, 8); - sqlite3VdbeMemSetDouble(apOut[i], d); + sqlite3_int64 v = sessionGetI64(aVal); + if( eType==SQLITE_INTEGER ){ + sqlite3VdbeMemSetInt64(apOut[i], v); + }else{ + double d; + memcpy(&d, &v, 8); + sqlite3VdbeMemSetDouble(apOut[i], d); + } + pIn->iNext += 8; } - pIn->iNext += 8; } } } diff --git a/amalgamation_dev/sqlite3.h b/amalgamation_dev/sqlite3.h index e967249..c87819b 100644 --- a/amalgamation_dev/sqlite3.h +++ b/amalgamation_dev/sqlite3.h @@ -148,7 +148,7 @@ extern "C" { */ #define SQLITE_VERSION "3.41.2" #define SQLITE_VERSION_NUMBER 3041002 -#define SQLITE_SOURCE_ID "2023-03-22 11:56:21 c5a8ed5442668c558f8330bc68beb9e9e6396526f364a63f7a6cb812639aff78" +#define SQLITE_SOURCE_ID "2023-03-22 11:56:21 c5a8ed5442668c558f8330bc68beb9e9e6396526f364a63f7a6cb812639aalt1" /* ** CAPI3REF: Run-Time Library Version Numbers diff --git a/src/resolve.c b/src/resolve.c index 4b36ecc..c5228a7 100644 --- a/src/resolve.c +++ b/src/resolve.c @@ -1211,11 +1211,12 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){ while( pNC2 && sqlite3ReferencesSrcList(pParse, pExpr, pNC2->pSrcList)==0 ){ - pExpr->op2++; + pExpr->op2 += (1 + pNC2->nNestedSelect); pNC2 = pNC2->pNext; } assert( pDef!=0 || IN_RENAME_OBJECT ); if( pNC2 && pDef ){ + pExpr->op2 += pNC2->nNestedSelect; assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg ); assert( SQLITE_FUNC_ANYORDER==NC_OrderAgg ); testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 ); @@ -1776,6 +1777,7 @@ static int resolveSelectStep(Walker *pWalker, Select *p){ /* Recursively resolve names in all subqueries in the FROM clause */ + if( pOuterNC ) pOuterNC->nNestedSelect++; for(i=0; ipSrc->nSrc; i++){ SrcItem *pItem = &p->pSrc->a[i]; if( pItem->pSelect && (pItem->pSelect->selFlags & SF_Resolved)==0 ){ @@ -1800,7 +1802,8 @@ static int resolveSelectStep(Walker *pWalker, Select *p){ } } } - + if( pOuterNC ) pOuterNC->nNestedSelect--; + /* Set up the local name-context to pass to sqlite3ResolveExprNames() to ** resolve the result-set expression list. */ diff --git a/src/sqliteInt.h b/src/sqliteInt.h index 2614f4b..07bc4de 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -3321,6 +3321,7 @@ struct NameContext { int nRef; /* Number of names resolved by this context */ int nNcErr; /* Number of errors encountered while resolving names */ int ncFlags; /* Zero or more NC_* flags defined below */ + int nNestedSelect; /* Number of nested selects using this NC */ Select *pWinSelect; /* SELECT statement for any window functions */ }; diff --git a/src/where.c b/src/where.c index 448c955..423fa37 100644 --- a/src/where.c +++ b/src/where.c @@ -5849,7 +5849,10 @@ WhereInfo *sqlite3WhereBegin( /* An ORDER/GROUP BY clause of more than 63 terms cannot be optimized */ testcase( pOrderBy && pOrderBy->nExpr==BMS-1 ); - if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0; + if( pOrderBy && pOrderBy->nExpr>=BMS ){ + pOrderBy = 0; + wctrlFlags &= ~WHERE_WANT_DISTINCT; + } /* The number of tables in the FROM clause is limited by the number of ** bits in a Bitmask diff --git a/test/aggnested.test b/test/aggnested.test index 1b8b608..c53ea50 100644 --- a/test/aggnested.test +++ b/test/aggnested.test @@ -358,6 +358,60 @@ do_execsql_test 6.2.2 { FROM t2 GROUP BY 'constant_string'; } {{}} +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 7.0 { + CREATE TABLE invoice ( + id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + amount DOUBLE PRECISION DEFAULT NULL, + name VARCHAR(100) DEFAULT NULL + ); + + INSERT INTO invoice (amount, name) VALUES + (4.0, 'Michael'), (15.0, 'Bara'), (4.0, 'Michael'), (6.0, 'John'); +} + +do_execsql_test 7.1 { + SELECT sum(amount), name + from invoice + group by name + having (select v > 6 from (select sum(amount) v) t) +} { + 15.0 Bara + 8.0 Michael +} + +do_execsql_test 7.2 { + SELECT (select 1 from (select sum(amount))) FROM invoice +} {1} + +do_execsql_test 8.0 { + CREATE TABLE t1(x INT); + INSERT INTO t1 VALUES(100); + INSERT INTO t1 VALUES(20); + INSERT INTO t1 VALUES(3); + SELECT (SELECT y FROM (SELECT sum(x) AS y) AS t2 ) FROM t1; +} {123} + +do_execsql_test 8.1 { + SELECT ( + SELECT y FROM ( + SELECT z AS y FROM (SELECT sum(x) AS z) AS t2 + ) + ) FROM t1; +} {123} + +do_execsql_test 8.2 { + SELECT ( + SELECT a FROM ( + SELECT y AS a FROM ( + SELECT z AS y FROM (SELECT sum(x) AS z) AS t2 + ) + ) + ) FROM t1; +} {123} + diff --git a/test/window1.test b/test/window1.test index 471ac2d..23e76cb 100644 --- a/test/window1.test +++ b/test/window1.test @@ -1881,7 +1881,7 @@ do_catchsql_test 57.3 { SELECT max(y) OVER( ORDER BY (SELECT x FROM (SELECT sum(y) AS x FROM t1))) ) FROM t3; -} {1 {misuse of aggregate: sum()}} +} {0 5} # 2020-06-06 ticket 1f6f353b684fc708 reset_db -- Gitee