Return-Path: X-Original-To: apmail-phoenix-dev-archive@minotaur.apache.org Delivered-To: apmail-phoenix-dev-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 6C58810293 for ; Wed, 3 Jun 2015 02:23:51 +0000 (UTC) Received: (qmail 37272 invoked by uid 500); 3 Jun 2015 02:23:51 -0000 Delivered-To: apmail-phoenix-dev-archive@phoenix.apache.org Received: (qmail 37199 invoked by uid 500); 3 Jun 2015 02:23:51 -0000 Mailing-List: contact dev-help@phoenix.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@phoenix.apache.org Delivered-To: mailing list dev@phoenix.apache.org Received: (qmail 37162 invoked by uid 99); 3 Jun 2015 02:23:51 -0000 Received: from Unknown (HELO spamd4-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 03 Jun 2015 02:23:51 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd4-us-west.apache.org (ASF Mail Server at spamd4-us-west.apache.org) with ESMTP id D1B8BC0940 for ; Wed, 3 Jun 2015 02:23:50 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd4-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 1.77 X-Spam-Level: * X-Spam-Status: No, score=1.77 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, T_RP_MATCHES_RCVD=-0.01] autolearn=disabled Received: from mx1-us-west.apache.org ([10.40.0.8]) by localhost (spamd4-us-west.apache.org [10.40.0.11]) (amavisd-new, port 10024) with ESMTP id no0jFE28hM10 for ; Wed, 3 Jun 2015 02:23:49 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-us-west.apache.org (ASF Mail Server at mx1-us-west.apache.org) with SMTP id 90E2E275EA for ; Wed, 3 Jun 2015 02:23:49 +0000 (UTC) Received: (qmail 37084 invoked by uid 99); 3 Jun 2015 02:23:49 -0000 Received: from arcas.apache.org (HELO arcas.apache.org) (140.211.11.28) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 03 Jun 2015 02:23:49 +0000 Date: Wed, 3 Jun 2015 02:23:49 +0000 (UTC) From: "James Taylor (JIRA)" To: dev@phoenix.incubator.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Comment Edited] (PHOENIX-2018) Implement math build-in function SQRT MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 [ https://issues.apache.org/jira/browse/PHOENIX-2018?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14570149#comment-14570149 ] James Taylor edited comment on PHOENIX-2018 at 6/3/15 2:22 AM: --------------------------------------------------------------- Thanks for the patch, [~shuxi0ng]. It looks very good. Here's some minor feedback: - In your evaluate method, check for the child evaluating to null (which you can check with ptr.getLength() == 0), and in that case, just return true. - Remove that if (ptr.getLength() < returnType.getByteSize()) statement. You definitely always need to allocate a new buffer for the return value, otherwise you're potentially overriding some random memory location (based on what the child returned). - Don't bother coercing the value after the Math.sqrt call. Instead, just don't specialize the getSortOrder() method (which will end up defaulting to SortOrder.ASC). {code} + @Override + public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) { + Expression childExpr = children.get(0); + PDataType returnType = getDataType(); + if (childExpr.evaluate(tuple, ptr)) { + double result; + if (childExpr.getDataType() == PDecimal.INSTANCE) { + result = + ((BigDecimal) childExpr.getDataType().toObject(ptr, + childExpr.getSortOrder())).doubleValue(); + } else { + result = + childExpr.getDataType().getCodec() + .decodeDouble(ptr, childExpr.getSortOrder()); + } + if (ptr.getLength() < returnType.getByteSize()) { + ptr.set(new byte[returnType.getByteSize()]); + } + returnType.getCodec().encodeDouble(Math.sqrt(result), ptr); + returnType.coerceBytes(ptr, returnType, SortOrder.ASC, childExpr.getSortOrder()); + return true; + } + return false; + } + + @Override + public PDataType getDataType() { + return PDouble.INSTANCE; + } + + @Override + public String getName() { + return NAME; + } + + @Override + public OrderPreserving preservesOrder() { + return OrderPreserving.YES; + } + + @Override + public SortOrder getSortOrder() { + return children.get(0).getSortOrder(); + } {code} was (Author: jamestaylor): Thanks for the patch, [~shuxi0ng]. It looks very good. Here's some minor feedback: - In your evaluate method, check for the child evaluating to null (which you can check with ptr.getLength() == 0), and in that case, just return true. - Remove that if (ptr.getLength() < returnType.getByteSize()) statement. You definitely always need to allocate a new buffer for the return value, otherwise you're potentially overriding some random memory location (based on what the child returned). - Don't bother coercing the value after the Math.sqrt call. Instead, just don't specialize the getSortOrder() method (which will end up defaulting to SortOrder.ASC). {code} + @Override + public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) { + Expression childExpr = children.get(0); + PDataType returnType = getDataType(); + if (childExpr.evaluate(tuple, ptr)) { + double result; + if (childExpr.getDataType() == PDecimal.INSTANCE) { + result = + ((BigDecimal) childExpr.getDataType().toObject(ptr, + childExpr.getSortOrder())).doubleValue(); + } else { + result = + childExpr.getDataType().getCodec() + .decodeDouble(ptr, childExpr.getSortOrder()); + } + if (ptr.getLength() < returnType.getByteSize()) { + ptr.set(new byte[returnType.getByteSize()]); + } + returnType.getCodec().encodeDouble(Math.sqrt(result), ptr); + returnType.coerceBytes(ptr, returnType, SortOrder.ASC, childExpr.getSortOrder()); + return true; + } + return false; + } {code} + + @Override + public PDataType getDataType() { + return PDouble.INSTANCE; + } + + @Override + public String getName() { + return NAME; + } + + @Override + public OrderPreserving preservesOrder() { + return OrderPreserving.YES; + } + + @Override + public SortOrder getSortOrder() { + return children.get(0).getSortOrder(); + } {code} > Implement math build-in function SQRT > ------------------------------------- > > Key: PHOENIX-2018 > URL: https://issues.apache.org/jira/browse/PHOENIX-2018 > Project: Phoenix > Issue Type: Sub-task > Reporter: Shuxiong Ye > Assignee: Shuxiong Ye > Fix For: 5.0.0, 4.5.0, 4.4.1 > > Attachments: 0001-PHOENIX-2018-Implement-math-build-in-function-SQRT_v3.patch > > > # SQRT means square root. > # Return type will be PDouble > # OrderPreserving -- This message was sent by Atlassian JIRA (v6.3.4#6332)