[ 
https://issues.apache.org/jira/browse/DRILL-5899?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16243238#comment-16243238
 ] 

ASF GitHub Bot commented on DRILL-5899:
---------------------------------------

Github user paul-rogers commented on a diff in the pull request:

    https://github.com/apache/drill/pull/1015#discussion_r149554195
  
    --- Diff: 
exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/SqlPatternContainsMatcher.java
 ---
    @@ -17,37 +17,48 @@
      */
     package org.apache.drill.exec.expr.fn.impl;
     
    -public class SqlPatternContainsMatcher implements SqlPatternMatcher {
    -  final String patternString;
    -  CharSequence charSequenceWrapper;
    -  final int patternLength;
    -
    -  public SqlPatternContainsMatcher(String patternString, CharSequence 
charSequenceWrapper) {
    -    this.patternString = patternString;
    -    this.charSequenceWrapper = charSequenceWrapper;
    -    patternLength = patternString.length();
    +import io.netty.buffer.DrillBuf;
    +
    +public class SqlPatternContainsMatcher extends AbstractSqlPatternMatcher {
    +
    +  public SqlPatternContainsMatcher(String patternString) {
    +    super(patternString);
       }
     
       @Override
    -  public int match() {
    -    final int txtLength = charSequenceWrapper.length();
    -    int patternIndex = 0;
    -    int txtIndex = 0;
    -
    -    // simplePattern string has meta characters i.e % and _ and escape 
characters removed.
    -    // so, we can just directly compare.
    -    while (patternIndex < patternLength && txtIndex < txtLength) {
    -      if (patternString.charAt(patternIndex) != 
charSequenceWrapper.charAt(txtIndex)) {
    -        // Go back if there is no match
    -        txtIndex = txtIndex - patternIndex;
    -        patternIndex = 0;
    -      } else {
    -        patternIndex++;
    +  public int match(int start, int end, DrillBuf drillBuf) {
    +
    +    if (patternLength == 0) { // Everything should match for null pattern 
string
    +      return 1;
    +    }
    +
    +    final int txtLength = end - start;
    +
    +    // no match if input string length is less than pattern length
    +    if (txtLength < patternLength) {
    +      return 0;
    +    }
    +
    +    outer:
    +    for (int txtIndex = 0; txtIndex < txtLength; txtIndex++) {
    +
    +      // boundary check
    +      if (txtIndex + patternLength > txtLength) {
    --- End diff --
    
    Better:
    ```
    int end = txtLength - patternLength;
    for (int txtIndex = 0; txtIndex < end; txtIndex++) {
    ```
    And omit the boundary check on every iteration. That is, no reason to 
iterate past the last possible match, then use an if-statement to shorten the 
loop. Just shorten the loop.


> Simple pattern matchers can work with DrillBuf directly
> -------------------------------------------------------
>
>                 Key: DRILL-5899
>                 URL: https://issues.apache.org/jira/browse/DRILL-5899
>             Project: Apache Drill
>          Issue Type: Improvement
>          Components: Execution - Flow
>            Reporter: Padma Penumarthy
>            Assignee: Padma Penumarthy
>            Priority: Critical
>
> For the 4 simple patterns we have i.e. startsWith, endsWith, contains and 
> constant,, we do not need the overhead of charSequenceWrapper. We can work 
> with DrillBuf directly. This will save us from doing isAscii check and UTF8 
> decoding for each row.
> UTF-8 encoding ensures that no UTF-8 character is a prefix of any other valid 
> character. So, instead of decoding varChar from each row we are processing, 
> encode the patternString once during setup and do raw byte comparison. 
> Instead of bounds checking and reading one byte at a time, we get the whole 
> buffer in one shot and use that for comparison.
> This improved overall performance for filter operator by around 20%. 



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to