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 09A67200D87 for ; Thu, 21 Dec 2017 00:23:10 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 058B1160C18; Wed, 20 Dec 2017 23:23:10 +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 2A02B160C2C for ; Thu, 21 Dec 2017 00:23:09 +0100 (CET) Received: (qmail 6592 invoked by uid 500); 20 Dec 2017 23:23:08 -0000 Mailing-List: contact issues-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 issues@drill.apache.org Received: (qmail 6550 invoked by uid 99); 20 Dec 2017 23:23:08 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd3-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 20 Dec 2017 23:23:08 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd3-us-west.apache.org (ASF Mail Server at spamd3-us-west.apache.org) with ESMTP id E870E180985 for ; Wed, 20 Dec 2017 23:23:07 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd3-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -99.201 X-Spam-Level: X-Spam-Status: No, score=-99.201 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, RCVD_IN_DNSWL_NONE=-0.0001, RP_MATCHES_RCVD=-0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001, USER_IN_WHITELIST=-100] autolearn=disabled Received: from mx1-lw-us.apache.org ([10.40.0.8]) by localhost (spamd3-us-west.apache.org [10.40.0.10]) (amavisd-new, port 10024) with ESMTP id 2baCeSzhBMI2 for ; Wed, 20 Dec 2017 23:23:05 +0000 (UTC) Received: from mailrelay1-us-west.apache.org (mailrelay1-us-west.apache.org [209.188.14.139]) by mx1-lw-us.apache.org (ASF Mail Server at mx1-lw-us.apache.org) with ESMTP id 62EBD5FB29 for ; Wed, 20 Dec 2017 23:23:04 +0000 (UTC) Received: from jira-lw-us.apache.org (unknown [207.244.88.139]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id 26131E0F06 for ; Wed, 20 Dec 2017 23:23:03 +0000 (UTC) Received: from jira-lw-us.apache.org (localhost [127.0.0.1]) by jira-lw-us.apache.org (ASF Mail Server at jira-lw-us.apache.org) with ESMTP id CF6A024133 for ; Wed, 20 Dec 2017 23:23:01 +0000 (UTC) Date: Wed, 20 Dec 2017 23:23:01 +0000 (UTC) From: "ASF GitHub Bot (JIRA)" To: issues@drill.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Commented] (DRILL-5879) Optimize "Like" operator MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 archived-at: Wed, 20 Dec 2017 23:23:10 -0000 [ https://issues.apache.org/jira/browse/DRILL-5879?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16299243#comment-16299243 ] ASF GitHub Bot commented on DRILL-5879: --------------------------------------- Github user paul-rogers commented on a diff in the pull request: https://github.com/apache/drill/pull/1072#discussion_r158159524 --- 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); --- End diff -- Is it faster to say `secondInByte = thirdInByte`; shuffle values between variables in each iteration rather than diving into direct memory multiple types for the same byte? > Optimize "Like" operator > ------------------------ > > Key: DRILL-5879 > URL: https://issues.apache.org/jira/browse/DRILL-5879 > Project: Apache Drill > Issue Type: Improvement > Components: Execution - Relational Operators > Environment: * > Reporter: salim achouche > Assignee: salim achouche > Priority: Minor > Fix For: 1.13.0 > > > Query: select from where colA like '%a%' or colA like '%xyz%'; > Improvement Opportunities > # Avoid isAscii computation (full access of the input string) since we're dealing with the same column twice > # Optimize the "contains" for-loop > Implementation Details > 1) > * Added a new integer variable "asciiMode" to the VarCharHolder class > * The default value is -1 which indicates this info is not known > * Otherwise this value will be set to either 1 or 0 based on the string being in ASCII mode or Unicode > * The execution plan already shares the same VarCharHolder instance for all evaluations of the same column value > * The asciiMode will be correctly set during the first LIKE evaluation and will be reused across other LIKE evaluations > 2) > * The "Contains" LIKE operation is quite expensive as the code needs to access the input string to perform character based comparisons > * Created 4 versions of the same for-loop to a) make the loop simpler to optimize (Vectorization) and b) minimize comparisons > Benchmarks > * Lineitem table 100GB > * Query: select l_returnflag, count(*) from dfs.`` where l_comment not like '%a%' or l_comment like '%the%' group by l_returnflag > * Before changes: 33sec > * After changes : 27sec -- This message was sent by Atlassian JIRA (v6.4.14#64029)