drill-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From paul-rogers <...@git.apache.org>
Subject [GitHub] drill pull request #1015: DRILL-5899: Simple pattern matchers can work with ...
Date Wed, 08 Nov 2017 01:37:38 GMT
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.


---

Mime
View raw message