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 1qeOv4-006hem-Di for pgsql-hackers@arkaria.postgresql.org; Fri, 08 Sep 2023 00:00:18 +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 1qeOv2-005MMM-4q for pgsql-hackers@arkaria.postgresql.org; Fri, 08 Sep 2023 00:00:15 +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 1qeOv1-005MLm-9J for pgsql-hackers@lists.postgresql.org; Fri, 08 Sep 2023 00:00:15 +0000 Received: from mail-ot1-x32e.google.com ([2607:f8b0:4864:20::32e]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qeOuw-003llx-P6 for pgsql-hackers@postgresql.org; Fri, 08 Sep 2023 00:00:14 +0000 Received: by mail-ot1-x32e.google.com with SMTP id 46e09a7af769-6b9e478e122so1070448a34.1 for ; Thu, 07 Sep 2023 17:00:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=timescale.com; s=google; t=1694131209; x=1694736009; darn=postgresql.org; h=in-reply-to:cc:to:from:references:content-language:subject :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=m1xNatrWLpfU692BjtbGgzI9Cg6oHTbm+t3OEDMK6TI=; b=jjO9ePFtY4vHz1R7PZBWPjTaOEC70brJMO/YDy+cJhjlb35SpgCGOsXu/YDMXiTcSc xUIuFHzu61+OCX86ZkY/WNFq1/zYxsqd/l2BMrmn3zo0Vae5B+hsS2FV5zcChcYenmJC LsrfrcuPBOQiG8SeGzpBEttyBZUUZWrD6ZGxIy4FkK97cDdgS/JjtxYIG1Wz9WZIspZT 1pQMHKQO6uT3pO2jHPmqeK0R6lUU6rLErBVLM3uNhBiaR6otqHr2+Z0lF7ImWx21Mx7p QYfMuyrB2a1W2qa6fAV1jPBbJicz/4pFUa6j9cBkpSv2pO8Uf4bQbD1KlQZv+2aoj2Td GmSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694131209; x=1694736009; h=in-reply-to:cc:to:from:references:content-language:subject :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=m1xNatrWLpfU692BjtbGgzI9Cg6oHTbm+t3OEDMK6TI=; b=MkP0p+74s42cAvv2k7hvpNkk6xC1al25FJ3ALgmBS37StXFRqpwdJweuUgAc5mdrcD eB5O5swaj9KlGjnKBhUwShd49ByViAQ1CTwZ9zGBFCdYUE/2KLvEWKgtPi5Tm77fdQoD BSeZJYz7VLVtkqxKrxZTN9XS7zDYbGbQj8cXsw/+gqHrJIJnOUKd3TI0CCRrEDOErPxP oCNik46CkR3QppW2ERbeTerXvJkY8jUYd9EJ/dtR5Hg1YYcf6G2CJkvGPy7VzYRBZQwG Hc1WuCCCeiswKl+PN5iW/d46pF1xL53q8R5xYwJw/aLHP7Xj6wOJy59KXl9M/CVmk4+J HRHw== X-Gm-Message-State: AOJu0YyFQBPdFDv4DDs1aBhSSxvQ0/VymonlEn0CudibMoiFZS/pkn5b Uj4dJYiF/NB69vlZlEWU/JLSMA== X-Google-Smtp-Source: AGHT+IHex5b6J8L55YPy4F8YrUsuloLngHh9hymMjW2nwi2yAnam2a47SpteHI31P7N27OF+A38JXQ== X-Received: by 2002:a9d:69cc:0:b0:6b7:5687:8a9e with SMTP id v12-20020a9d69cc000000b006b756878a9emr1008590oto.15.1694131208796; Thu, 07 Sep 2023 17:00:08 -0700 (PDT) Received: from [192.168.1.27] ([50.53.8.205]) by smtp.gmail.com with ESMTPSA id i8-20020a63a848000000b0056428865aadsm154803pgp.82.2023.09.07.17.00.07 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 07 Sep 2023 17:00:08 -0700 (PDT) Content-Type: multipart/mixed; boundary="------------YlsZCQR21Gfdi74kbPCppQcS" Message-ID: Date: Thu, 7 Sep 2023 17:00:07 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.15.0 Subject: Re: Row pattern recognition Content-Language: en-US References: <20230728.200230.1800661389729335298.t-ishii@sranhm.sra.co.jp> <9e135bd6-252a-e39a-bf7d-5a9726427769@postgresfriends.org> <20230729.120508.992131963092683632.t-ishii@sranhm.sra.co.jp> <20230809.174112.1806434641666895071.t-ishii@sranhm.sra.co.jp> From: Jacob Champion To: Tatsuo Ishii , Erik Rijkers Cc: Vik Fearing , PostgreSQL-development In-Reply-To: <20230809.174112.1806434641666895071.t-ishii@sranhm.sra.co.jp> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------YlsZCQR21Gfdi74kbPCppQcS Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Hello! > (1) I completely changed the pattern matching engine so that it > performs backtracking. Now the engine evaluates all pattern elements > defined in PATTER against each row, saving matched pattern variables > in a string per row. For example if the pattern element A and B > evaluated to true, a string "AB" is created for current row. If I understand correctly, this strategy assumes that one row's membership in a pattern variable is independent of the other rows' membership. But I don't think that's necessarily true: DEFINE A AS PREV(CLASSIFIER()) IS DISTINCT FROM 'A', ... > See row_is_in_reduced_frame, search_str_set and search_str_set_recurse > in nodeWindowAggs.c for more details. For now I use a naive depth > first search and probably there is a lot of rooms for optimization > (for example rewriting it without using > recursion). The depth-first match is doing a lot of subtle work here. For example, with PATTERN ( A+ B+ ) DEFINE A AS TRUE, B AS TRUE (i.e. all rows match both variables), and three rows in the partition, our candidates will be tried in the order aaa aab aba abb ... bbb The only possible matches against our regex `^a+b+` are "aab" and "abb", and that order matches the preferment order, so it's fine. But it's easy to come up with a pattern where that's the wrong order, like PATTERN ( A+ (B|A)+ ) Now "aaa" will be considered before "aab", which isn't correct. Similarly, the assumption that we want to match the longest string only works because we don't allow alternation yet. > Suggestions/patches are welcome. Cool, I will give this piece some more thought. Do you mind if I try to add some more complicated pattern quantifiers to stress the architecture, or would you prefer to tackle that later? Just alternation by itself will open up a world of corner cases. > With the new engine, cases above do not fail anymore. See new > regression test cases. Thanks for providing valuable test cases! You're very welcome! On 8/9/23 01:41, Tatsuo Ishii wrote: > - I found a bug with pattern matching code. It creates a string for > subsequent regular expression matching. It uses the initial letter > of each define variable name. For example, if the varname is "foo", > then "f" is used. Obviously this makes trouble if we have two or > more variables starting with same "f" (e.g. "food"). To fix this, I > assign [a-z] to each variable instead of its initial letter. However > this way limits us not to have more than 26 variables. I hope 26 is > enough for most use cases. There are still plenty of alphanumerics left that could be assigned... But I'm wondering if we might want to just implement the NFA directly? The current implementation's Cartesian explosion can probably be pruned aggressively, but replaying the entire regex match once for every backtracked step will still duplicate a lot of work. -- I've attached another test case; it looks like last_value() is depending on some sort of side effect from either first_value() or nth_value(). I know the window frame itself is still under construction, so apologies if this is an expected failure. Thanks! --Jacob --------------YlsZCQR21Gfdi74kbPCppQcS Content-Type: text/plain; charset=UTF-8; name="test-last_value.diff.txt" Content-Disposition: attachment; filename="test-last_value.diff.txt" Content-Transfer-Encoding: base64 ZGlmZiAtLWdpdCBhL3NyYy90ZXN0L3JlZ3Jlc3MvZXhwZWN0ZWQvcnByLm91dCBiL3NyYy90 ZXN0L3JlZ3Jlc3MvZXhwZWN0ZWQvcnByLm91dAppbmRleCAzNDBjY2YyNDJjLi5iZDM1ZTgz ODllIDEwMDY0NAotLS0gYS9zcmMvdGVzdC9yZWdyZXNzL2V4cGVjdGVkL3Jwci5vdXQKKysr IGIvc3JjL3Rlc3QvcmVncmVzcy9leHBl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