From 33db5b73e50b62a52b37e3ccca5dce0fb2fbf705 Mon Sep 17 00:00:00 2001 From: drh <> Date: Sun, 25 Jan 2026 20:15:00 +0000 Subject: [PATCH] The Select.addrOpenEphm field is no longer needed. Remove it and simplify related code. FossilOrigin-Name: 1fe5a1ca72892987716b71b9419265f25c8c426e52320acf56724fd64b838be6 --- manifest | 16 +++++------ manifest.uuid | 2 +- src/expr.c | 3 +-- src/select.c | 72 +++++++++++++++---------------------------------- src/sqliteInt.h | 13 +-------- 5 files changed, 33 insertions(+), 73 deletions(-) diff --git a/manifest b/manifest index 9b93a36c5a..eed39d646a 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Refactor\sone\sfunction\sname.\s\sComment\simprovements.\s\sNo\sfunctional\schanges. -D 2026-01-25T18:21:53.279 +C The\sSelect.addrOpenEphm\sfield\sis\sno\slonger\sneeded.\s\sRemove\sit\sand\nsimplify\srelated\scode. +D 2026-01-25T20:15:00.758 F .fossil-settings/binary-glob 61195414528fb3ea9693577e1980230d78a1f8b0a54c78cf1b9b24d0a409ed6a x F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea @@ -689,7 +689,7 @@ F src/date.c e19e0cfff9a41bfdd884c655755f6f00bca4c1a22272b56e0dd6667b7ea893a2 F src/dbpage.c c9ea81c11727f27e02874611e92773e68e2a90a875ef2404b084564c235fd91f F src/dbstat.c 73362c0df0f40ad5523a6f5501224959d0976757b511299bf892313e79d14f5c F src/delete.c 901499bed747c3b4b2be45be1abe912ba50a3f6a40ba88cc006ccf279f2d0e97 -F src/expr.c 20010bd64989c1e4665b50e8c447589004c462c310cb16eec82c9d3abc29879e +F src/expr.c 2857569c0f81c6b5bb6a5de824f6f8a6fcf974ff86604d5f43a7a7e71678e563 F src/fault.c 460f3e55994363812d9d60844b2a6de88826e007 F src/fkey.c c065da737307a29e4d240ac727758dbf4102cb3218a1f651eb689b6a6fa12531 F src/func.c efbcfe7cb7fc92fe5299c9aaa141075eb60d2108253e99bc235384ed6a90d937 @@ -738,12 +738,12 @@ F src/printf.c b1b29b5e58e1530d5daeee5963d3c318d8ab2d7e38437580e28755753e0c1ded F src/random.c 606b00941a1d7dd09c381d3279a058d771f406c5213c9932bbd93d5587be4b9c F src/resolve.c 47aa7fdc9ec4c19b103ac5e79d7887d30119b5675309facf5eed1118391c868b F src/rowset.c 8432130e6c344b3401a8874c3cb49fefe6873fec593294de077afea2dce5ec97 -F src/select.c c43a16c436d342260c6d872c7d0c9afa1fa90705c83f18fbbc0a74d312cd6d08 +F src/select.c 0b01aa2c1a53d46fd4bda0498e98fac7c8b998d549827d441bdb7c3e1bd214b7 F src/shell.c.in e8818572acd50464bc00426fe0d755e98239f73d531437c3dc7721d1fecb1231 F src/sqlite.h.in d463dcdd67d4865991cd62dc8d3f678086b38365593861f77c09c3401551d59f F src/sqlite3.rc 015537e6ac1eec6c7050e17b616c2ffe6f70fca241835a84a4f0d5937383c479 F src/sqlite3ext.h f590cd8cb4c36fc727632c9b5fbbafc85f7efe2c6890f9958d7e711dc26ec01e -F src/sqliteInt.h 2d5c66fde59032f5caa1dd2a910c9ecbbe6c452c3d2f68a0e5758a1086681ef5 +F src/sqliteInt.h f4b50f8c287b858bb23dd51daee29ff424697be9a0753cf37df98fbe1806125b F src/sqliteLimit.h 904a3f520362c7065c18165aaabd504fb13cc1b76cb411f38bd41ac219e4af1e F src/status.c 7565d63a79aa2f326339a24a0461a60096d0bd2bce711fefb50b5c89335f3592 F src/table.c 0f141b58a16de7e2fbe81c308379e7279f4c6b50eb08efeec5892794a0ba30d1 @@ -2193,8 +2193,8 @@ F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee F tool/warnings.sh d924598cf2f55a4ecbc2aeb055c10bd5f48114793e7ba25f9585435da29e7e98 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f F tool/winmain.c 00c8fb88e365c9017db14c73d3c78af62194d9644feaf60e220ab0f411f3604c -P 781b7ec0bcaa5d94e8c5fd31b4fe0203393d5209a5f518848f057ec3d2b8d7ac -R 12674c2f8b312dc84fc35da4be0993ed +P 5b4c0a5e8609e16a4b08b7f95d55e513ee4300b22d044ac8f42e60b96cff5be6 +R 4436094fd125f3cfae4fcd4df218545b U drh -Z 1132ccc2797672409401e1e1fbceab99 +Z 6d1da1c40ab70849b9cdb5b6a86c63ef # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 1e46ac2c6d..480c476a76 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -5b4c0a5e8609e16a4b08b7f95d55e513ee4300b22d044ac8f42e60b96cff5be6 +1fe5a1ca72892987716b71b9419265f25c8c426e52320acf56724fd64b838be6 diff --git a/src/expr.c b/src/expr.c index c8f5c57e9f..1cd95f89b1 100644 --- a/src/expr.c +++ b/src/expr.c @@ -1978,8 +1978,7 @@ Select *sqlite3SelectDup(sqlite3 *db, const Select *pDup, int flags){ pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags); pNew->iLimit = 0; pNew->iOffset = 0; - pNew->selFlags = p->selFlags & ~(u32)SF_UsesEphemeral; - pNew->addrOpenEphm = -1; + pNew->selFlags = p->selFlags; pNew->nSelectRow = p->nSelectRow; pNew->pWith = sqlite3WithDup(db, p->pWith); #ifndef SQLITE_OMIT_WINDOWFUNC diff --git a/src/select.c b/src/select.c index b823f3ed3a..6a055064d7 100644 --- a/src/select.c +++ b/src/select.c @@ -151,7 +151,6 @@ Select *sqlite3SelectNew( pNew->iLimit = 0; pNew->iOffset = 0; pNew->selId = ++pParse->nSelect; - pNew->addrOpenEphm = -1; pNew->nSelectRow = 0; if( pSrc==0 ) pSrc = sqlite3DbMallocZero(pParse->db, SZ_SRCLIST_1); pNew->pSrc = pSrc; @@ -2729,8 +2728,28 @@ static void generateWithRecursiveQuery( } VdbeComment((v, "Queue table")); if( iDistinct ){ - p->addrOpenEphm = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iDistinct, 0); - p->selFlags |= SF_UsesEphemeral; + /* Generate an ephemeral table used to enforce distinctness on the + ** output of the recursive part of the CTE. + */ + KeyInfo *pKeyInfo; /* Collating sequence for the result set */ + CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */ + + assert( p->pNext==0 ); + assert( p->pEList!=0 ); + nCol = p->pEList->nExpr; + pKeyInfo = sqlite3KeyInfoAlloc(pParse->db, nCol, 1); + if( !pKeyInfo ){ + rc = SQLITE_NOMEM_BKPT; + goto end_of_recursive_query; + } + for(i=0, apColl=pKeyInfo->aColl; idb->pDfltColl; + } + } + sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iDistinct, nCol, 0, + (void*)pKeyInfo, P4_KEYINFO); } /* Detach the ORDER BY clause from the compound SELECT */ @@ -2954,9 +2973,6 @@ static int multiSelect( generateWithRecursiveQuery(pParse, p, &dest); }else #endif - - /* Compound SELECTs that have an ORDER BY clause are handled separately. - */ if( p->pOrderBy ){ /* If the compound has an ORDER BY clause, then always use the merge ** algorithm. */ @@ -3028,50 +3044,6 @@ static int multiSelect( } #endif } - if( pParse->nErr ) goto multi_select_end; - - /* Compute collating sequences used by - ** temporary table needed to implement the compound select. - ** Attach the KeyInfo structure to all temporary tables. - ** - ** This section is run by the right-most SELECT statement only. - ** SELECT statements to the left always skip this part. The right-most - ** SELECT might also skip this part if it has no ORDER BY clause and - ** no temp tables are required. - */ - if( p->selFlags & SF_UsesEphemeral ){ - int i; /* Loop counter */ - KeyInfo *pKeyInfo; /* Collating sequence for the result set */ - Select *pLoop; /* For looping through SELECT statements */ - CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */ - int nCol; /* Number of columns in result set */ - - assert( p->pNext==0 ); - assert( p->pEList!=0 ); - nCol = p->pEList->nExpr; - pKeyInfo = sqlite3KeyInfoAlloc(db, nCol, 1); - if( !pKeyInfo ){ - rc = SQLITE_NOMEM_BKPT; - goto multi_select_end; - } - for(i=0, apColl=pKeyInfo->aColl; ipDfltColl; - } - } - - for(pLoop=p; pLoop; pLoop=pLoop->pPrior){ - int addr = pLoop->addrOpenEphm; - if( addr>=0 ){ - sqlite3VdbeChangeP2(v, addr, nCol); - sqlite3VdbeChangeP4(v, addr, (char*)sqlite3KeyInfoRef(pKeyInfo), - P4_KEYINFO); - pLoop->addrOpenEphm = -1; - } - } - sqlite3KeyInfoUnref(pKeyInfo); - } multi_select_end: pDest->iSdst = dest.iSdst; diff --git a/src/sqliteInt.h b/src/sqliteInt.h index eaeaa14eaf..12dd0f8fe6 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -3585,16 +3585,6 @@ struct Upsert { /* ** An instance of the following structure contains all information ** needed to generate code for a single SELECT statement. -** -** See the header comment on the computeLimitRegisters() routine for a -** detailed description of the meaning of the iLimit and iOffset fields. -** -** addrOpenEphm entries contain the address of an OP_OpenEphemeral opcode. -** This address must be stored so that we can go back and fill in -** the P4_KEYINFO and P2 parameters later. Neither the KeyInfo nor -** the number of columns in P2 can be computed at the same time -** as the OP_OpenEphm instruction is coded because not -** enough information about the compound query is known at that point. */ struct Select { u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */ @@ -3602,7 +3592,6 @@ struct Select { u32 selFlags; /* Various SF_* values */ int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */ u32 selId; /* Unique identifier number for this SELECT */ - int addrOpenEphm; /* OP_OpenEphem opcodes related to this select */ ExprList *pEList; /* The fields of the result */ SrcList *pSrc; /* The FROM clause */ Expr *pWhere; /* The WHERE clause */ @@ -3634,7 +3623,7 @@ struct Select { #define SF_Resolved 0x0000004 /* Identifiers have been resolved */ #define SF_Aggregate 0x0000008 /* Contains agg functions or a GROUP BY */ #define SF_HasAgg 0x0000010 /* Contains aggregate functions */ -#define SF_UsesEphemeral 0x0000020 /* Uses the OpenEphemeral opcode */ +/* 0x0000020 // available for reuse */ #define SF_Expanded 0x0000040 /* sqlite3SelectExpand() called on this */ #define SF_HasTypeInfo 0x0000080 /* FROM subqueries have Table metadata */ #define SF_Compound 0x0000100 /* Part of a compound query */ -- 2.47.3