Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1vBPJ7-00G9SA-5M for pgsql-hackers@arkaria.postgresql.org; Wed, 22 Oct 2025 03:14:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1vBPJ6-00D0zX-2J for pgsql-hackers@arkaria.postgresql.org; Wed, 22 Oct 2025 03:14:35 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1vBPJ5-00D0zP-Ot for pgsql-hackers@lists.postgresql.org; Wed, 22 Oct 2025 03:14:34 +0000 Received: from meldrar.postgresql.org ([2a02:c0:301:0:ffff::31]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vBPJ2-003Xdi-13 for pgsql-hackers@lists.postgresql.org; Wed, 22 Oct 2025 03:14:34 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=postgresql.org; s=20171124; h=Content-Transfer-Encoding:Content-Type: Mime-Version:References:In-Reply-To:From:Subject:Cc:To:Message-Id:Date:Sender :Reply-To:Content-ID:Content-Description; bh=8ozuQANU1WZYlFwfbSCCkZY1cxMT0PMvux9DrNkoBf0=; b=tawQSXAfl+WJE2uh1z3Gr8Sfv0 CoFcaCL8cDCvOniv8B2Mfag9j45HhOLM9I857mvubDpXd7lCZb/MfChXcBIgWAKB4GiTfFcqGxiGF 8onU4DC1MFmPoDr0124wNbHEmMfsf5fm3PJ/J7XUORClUj5gxArPEt1fE8OV9WYyLjaDB0vyj59RF LmCUJDP1FFGeyjNKo8ybPDqnyczalsEFxlXh/y8A95JkCN4SYgiX2OkYqDLUPMLpHuUYFBqefkSsR bBXuDoWgjCSt0tHJ+AWJZsFrYqy8s+oEPf9I+FSO6Br4ra1pYs+893wtnIfodyo0yXCq5FbZaR4qk HTaRl1XA==; Received: from [2409:11:4120:300:9829:ee1f:67fb:48ee] (helo=localhost) by meldrar.postgresql.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vBPIy-008NnC-0y; Wed, 22 Oct 2025 03:14:30 +0000 Date: Wed, 22 Oct 2025 12:14:11 +0900 (JST) Message-Id: <20251022.121411.928830956906039697.ishii@postgresql.org> To: tgl@sss.pgh.pa.us Cc: pgsql-hackers@lists.postgresql.org, pramsey@cleverelephant.ca, ojford@gmail.com, peter@eisentraut.org, li.evan.chao@gmail.com, krasiyan@gmail.com, vik@postgresfriends.org, andrew@tao11.riddles.org.uk, david@fetter.org Subject: Re: Add RESPECT/IGNORE NULLS and FROM FIRST/LAST options From: Tatsuo Ishii In-Reply-To: <20251019.185323.1616138750375579950.ishii@postgresql.org> References: <20251011.090731.1451293026979559494.ishii@postgresql.org> <20251013.134332.1917267590891336373.ishii@postgresql.org> <20251019.185323.1616138750375579950.ishii@postgresql.org> X-Mailer: Mew version 6.8 on Emacs 29.3 Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Host-Lookup-Failed: Reverse DNS lookup failed for 2409:11:4120:300:9829:ee1f:67fb:48ee (failed) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk >>>> 2. AFAICS there is only one notnull_info array, which amounts to >>>> assuming that the window function will have only one argument position >>>> that it calls WinGetFuncArgInFrame or WinGetFuncArgInPartition for. >>>> That may be true for the built-in functions but it seems mighty >>>> restrictive for extensions. Worse yet, there's no check, so that >>>> you'd just get silently wrong answers if two or more arguments are >>>> evaluated. I think there ought to be a separate array for each argno; >>>> of course only created if the window function actually asks for >>>> evaluations of a particular argno. >>> >>> I missed that. Thank you for pointed it out. I agree it would be >>> better allow to use multiple argument positions that calls >>> WinGetFuncArgInFrame or WinGetFuncArgInPartition in >>> extensions. Attached is a PoC patch for that. >>> >>> Currently there's an issue with the patch, however. >>> >>> SELECT x, y, mywindowfunc2(x, y, 2) IGNORE NULLS OVER w FROM g >>> WINDOW w AS (ORDER BY y); >>> psql:test2.sql:9: ERROR: cannot fetch row before WindowObject's mark position >>> >>> mywindowfunc2 is a user defined window function, taking 3 arguments. x >>> and y are expected to be evaluated to integer. The third argument is >>> relative offset to current row. In the query above x and y are >>> retrieved using two WinGetFuncArgInPartition() calls. The data set >>> (table "g") looks like below. >>> >>> x | y >>> ----+--- >>> | 1 >>> | 2 >>> 10 | 3 >>> 20 | 4 >>> (4 rows) >>> >>> I think the cause of the error is: >>> >>> (1) WinGetFuncArgInPartition keep on fetching column x until it's >>> evalued to not null and placed in the second row (in this case that's >>> x==20). In WinGetFuncArgInPartition WinSetMarkPosition is called at >>> abs_pos==3. >>> >>> (2) WinGetFuncArgInPartition tries to fetch column y at row 0. Since >>> the mark was set to at row 3, the error occurred. >>> >>> To avoid the error, we could call WinGetFuncArgInPartition with >>> set_mark = false (and call WinSetMarkPosition separately) but I am not >>> sure if it's an acceptable solution. >> >> Attached is a v2 patch to fix the "cannot fetch row before >> WindowObject's mark position" error, by tweaking the logic to >> calculate the set mark position in WinGetFuncArgInPartition. > > Attached is a v3 patch which is ready for commit IMO. Major > difference from v2 patch is, now the patch satisfies the request > below. > >>>> of course only created if the window function actually asks for >>>> evaluations of a particular argno. > > The NOT NULL information array is allocated only when the window > function actually asks for evaluations of a particular argno using > WinGetFuncArgInFrame or WinGetFuncArgInPartition. > > If there's no objection, I am going to commit in a few days. Patch pushed. Thanks. Best regards, -- Tatsuo Ishii SRA OSS K.K. English: http://www.sraoss.co.jp/index_en/ Japanese:http://www.sraoss.co.jp