Github user sachouche commented on a diff in the pull request: https://github.com/apache/drill/pull/1072#discussion_r162124388 --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/SqlPatternContainsMatcher.java --- @@ -19,44 +19,286 @@ 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 == 0) { + matcherFcn = new MatcherZero(); + } else if (patternLength == 1) { + matcherFcn = new MatcherOne(); + } else if (patternLength == 2) { + matcherFcn = new MatcherTwo(); + } else if (patternLength == 3) { + matcherFcn = new MatcherThree(); + } 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 MatcherZero extends MatcherFcn { - if (patternLength == 0) { // Everything should match for null pattern string + private MatcherZero() { + } + + /** {@inheritDoc} */ + @Override + protected final int match(int start, int end, DrillBuf drillBuf) { return 1; } + } + + /** Handles patterns with length one */ + private final class MatcherOne extends MatcherFcn { + + private MatcherOne() { + super(); + } + + /** {@inheritDoc} */ + @Override + protected final int match(int start, int end, DrillBuf drillBuf) { + final int lengthToProcess = end - start; + final byte firstPattByte = patternArray[0]; + + // 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 MatcherTwo extends MatcherFcn { + + private MatcherTwo() { + } + + /** {@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 idx = 0; idx < lengthToProcess; idx++) { + final byte firstInByte = drillBuf.getByte(start + idx); - final int txtLength = end - start; + if (firstPattByte != firstInByte) { + continue; + } else { + final byte secondInByte = drillBuf.getByte(start + idx +1); - // no match if input string length is less than pattern length - if (txtLength < patternLength) { + if (secondInByte == secondPattByte) { + return 1; + } + } + } return 0; } + } + /** Handles patterns with length three */ + private final class MatcherThree extends MatcherFcn { - final int outerEnd = txtLength - patternLength; + private MatcherThree() { + } - 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 -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 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 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); --- End diff -- fixed
---