Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 3E78D200D71 for ; Thu, 21 Dec 2017 00:22:23 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 3CAE7160C2C; Wed, 20 Dec 2017 23:22:23 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 5A74E160C15 for ; Thu, 21 Dec 2017 00:22:22 +0100 (CET) Received: (qmail 99863 invoked by uid 500); 20 Dec 2017 23:22:20 -0000 Mailing-List: contact dev-help@drill.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@drill.apache.org Delivered-To: mailing list dev@drill.apache.org Received: (qmail 99632 invoked by uid 99); 20 Dec 2017 23:22:20 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 20 Dec 2017 23:22:20 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 6E577DFF59; Wed, 20 Dec 2017 23:22:19 +0000 (UTC) From: paul-rogers To: dev@drill.apache.org Reply-To: dev@drill.apache.org References: In-Reply-To: Subject: [GitHub] drill pull request #1072: DRILL-5879: Improved SQL Pattern Contains Performa... Content-Type: text/plain Message-Id: <20171220232219.6E577DFF59@git1-us-west.apache.org> Date: Wed, 20 Dec 2017 23:22:19 +0000 (UTC) archived-at: Wed, 20 Dec 2017 23:22:23 -0000 Github user paul-rogers commented on a diff in the pull request: https://github.com/apache/drill/pull/1072#discussion_r158160058 --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/SqlPatternContainsMatcher.java --- @@ -19,44 +19,283 @@ import io.netty.buffer.DrillBuf; -public class SqlPatternContainsMatcher extends AbstractSqlPatternMatcher { +/** SQL Pattern Contains implementation */ +public final class SqlPatternContainsMatcher extends AbstractSqlPatternMatcher { + private final MatcherFcn matcherFcn; public SqlPatternContainsMatcher(String patternString) { super(patternString); + + // Pattern matching is 1) a CPU intensive operation and 2) pattern and input dependent. The conclusion is + // that there is no single implementation that can do it all well. So, we use multiple implementations + // chosen based on the pattern length. + if (patternLength == 1) { + matcherFcn = new Matcher1(); + } else if (patternLength == 2) { + matcherFcn = new Matcher2(); + } else if (patternLength == 3) { + matcherFcn = new Matcher3(); + } else if (patternLength < 10) { + matcherFcn = new MatcherN(); + } else { + matcherFcn = new BoyerMooreMatcher(); + } } @Override public int match(int start, int end, DrillBuf drillBuf) { + return matcherFcn.match(start, end, drillBuf); + } + + //-------------------------------------------------------------------------- + // Inner Data Structure + // -------------------------------------------------------------------------- + + /** Abstract matcher class to allow us pick the most efficient implementation */ + private abstract class MatcherFcn { + protected final byte[] patternArray; + + protected MatcherFcn() { + assert patternByteBuffer.hasArray(); + + patternArray = patternByteBuffer.array(); + } + + /** + * @return 1 if the pattern was matched; 0 otherwise + */ + protected abstract int match(int start, int end, DrillBuf drillBuf); + } + + /** Handles patterns with length one */ + private final class Matcher1 extends MatcherFcn { - if (patternLength == 0) { // Everything should match for null pattern string - return 1; + private Matcher1() { + super(); } - final int txtLength = end - start; + /** {@inheritDoc} */ + @Override + protected final int match(int start, int end, DrillBuf drillBuf) { + final int lengthToProcess = end - start; + final byte firstPattByte = patternArray[0]; - // no match if input string length is less than pattern length - if (txtLength < patternLength) { + // simplePattern string has meta characters i.e % and _ and escape characters removed. + // so, we can just directly compare. + for (int idx = 0; idx < lengthToProcess; idx++) { + byte inputByte = drillBuf.getByte(start + idx); + + if (firstPattByte != inputByte) { + continue; + } + return 1; + } return 0; } + } + /** Handles patterns with length two */ + private final class Matcher2 extends MatcherFcn { - final int outerEnd = txtLength - patternLength; + private Matcher2() { + super(); + } - outer: - for (int txtIndex = 0; txtIndex <= outerEnd; txtIndex++) { + /** {@inheritDoc} */ + @Override + protected final int match(int start, int end, DrillBuf drillBuf) { + final int lengthToProcess = end - start - 1; + final byte firstPattByte = patternArray[0]; + final byte secondPattByte = patternArray[1]; // simplePattern string has meta characters i.e % and _ and escape characters removed. // so, we can just directly compare. - for (int patternIndex = 0; patternIndex < patternLength; patternIndex++) { - if (patternByteBuffer.get(patternIndex) != drillBuf.getByte(start + txtIndex + patternIndex)) { - continue outer; + for (int idx = 0; idx < lengthToProcess; idx++) { + final byte firstInByte = drillBuf.getByte(start + idx); + + if (firstPattByte != firstInByte) { + continue; + } else { + final byte secondInByte = drillBuf.getByte(start + idx +1); + + if (secondInByte == secondPattByte) { + return 1; + } } } + return 0; + } + } + + /** Handles patterns with length three */ + private final class Matcher3 extends MatcherFcn { - return 1; + private Matcher3() { + super(); } - return 0; + /** {@inheritDoc} */ + @Override + protected final int match(int start, int end, DrillBuf drillBuf) { + final int lengthToProcess = end - start -2; + final byte firstPattByte = patternArray[0]; + final byte secondPattByte = patternArray[1]; + final byte thirdPattByte = patternArray[2]; + + // simplePattern string has meta characters i.e % and _ and escape characters removed. + // so, we can just directly compare. + for (int idx = 0; idx < lengthToProcess; idx++) { + final byte inputByte = drillBuf.getByte(start + idx); + + if (firstPattByte != inputByte) { + continue; + } else { + final byte secondInByte = drillBuf.getByte(start + idx +1); + final byte thirdInByte = drillBuf.getByte(start + idx +2); + + if (secondInByte == secondPattByte && thirdInByte == thirdPattByte) { + return 1; + } + } + } + return 0; + } + } + + /** Handles patterns with arbitrary length */ + private final class MatcherN extends MatcherFcn { + + private MatcherN() { + super(); + } + + /** {@inheritDoc} */ + @Override + protected final int match(int start, int end, DrillBuf drillBuf) { + + if (patternLength == 0) { --- End diff -- Should there be a special case for an empty pattern? Just return 1 always. Else, seems odd for this to be the length = 0 || length > n case. ---