You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@drill.apache.org by cgivre <gi...@git.apache.org> on 2017/10/03 17:23:26 UTC

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

GitHub user cgivre opened a pull request:

    https://github.com/apache/drill/pull/971

    Drill-5834 Add Networking Functions

    This is a collection of Networking Functions to facilitate network analysis.  The functions include:
    
    - **inet_aton(`<ip>`)**:  Converts an IPv4 address into an integer.
    - **inet_ntoa( `<int>`)**:  Converts an integer IP into dotted decimal notation
    - **in_network( `<ip>`,`<cidr>` )**: Returns true if the IP address is in the given CIDR block
    - **getAddressCount( `<cidr>` )**:  Returns the number of IPs in a given CIDR block
    - **getBroadcastAddress( `<cidr>` )**: Returns the broadcast address for a given CIDR block
    - **getNetmask(`<cidr>` )**: Returns the netmask for a given CIDR block. 
    - **getLowAddress(`<cidr>`)**:  Returns the first address in a given CIDR block.
    - **getHighAddress(`<cidr>`)**:  Returns the last address in a given CIDR block.
    - **urlencode( `<url>` )**:  Returns a URL encoded string.
    - **urldecode( `<url>` )**:   Decodes a URL encoded string.
    - **is_valid_IP(`<ip>`)**: Returns true if the IP is a valid IP address
    - **is_private_ip(`<ip>`)**: Returns true if the IP is a private IPv4 address
    - **is_valid_IPv4(`<ip>`)**: Returns true if the IP is a valid IPv4 address
    - **is_valid_IPv6(`<ip>`)**: Returns true if the IP is a valid IPv6 address
    
    
    


You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/cgivre/drill network-functions-v2

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/drill/pull/971.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #971
    
----
commit 8a50040391d9d47d0f64c09f0c060162458ed840
Author: cgivre <cg...@gmail.com>
Date:   2017-10-03T14:31:38Z

    Added Network Analyis Functions

commit 3545362895cd182f73ac4e2f409eed8a7c1d454a
Author: cgivre <cg...@gmail.com>
Date:   2017-10-03T17:06:48Z

    Fixed Tests

commit 24c1b66a402c6418506dab74c28a929bff21e58a
Author: cgivre <cg...@gmail.com>
Date:   2017-10-03T17:07:39Z

    Removed backup file

----


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143955475
  
    --- Diff: exec/java-exec/src/test/java/org/apache/drill/exec/fn/impl/TestNetworkFunctions.java ---
    @@ -0,0 +1,93 @@
    +/**
    --- End diff --
    
    Apache header should be in a form of comment.


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143947777
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    +        result = 1;
    +      }
    +      else{
    +        result = 0;
    +      }
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getAddressCount",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getAddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      out.value = utils.getInfo().getAddressCount();
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function returns the broadcast address of a given CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getBroadcastAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getBroadcastAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getBroadcastAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the netmask of the input CIDR block.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "getNetmask",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getNetmaskFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getNetmask();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically lowest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getLowAddress",
    --- End diff --
    
    `low_address`?


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r144037246
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    +        result = 1;
    +      }
    +      else{
    +        result = 0;
    +      }
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getAddressCount",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getAddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      out.value = utils.getInfo().getAddressCount();
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function returns the broadcast address of a given CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getBroadcastAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getBroadcastAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getBroadcastAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the netmask of the input CIDR block.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "getNetmask",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getNetmaskFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getNetmask();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically lowest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getLowAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getLowAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getLowAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically highest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getHighAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getHighddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getHighAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function encodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "urlencode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urlencodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLEncoder.encode(url, "UTF-8");
    +      }catch (Exception e){
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function decodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "urldecode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urldecodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLDecoder.decode(url, "UTF-8");
    +      }catch (Exception e){
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +
    +  /**
    +   * This function converts a BigInt IPv4 into dotted decimal notation.  The opposite of inet_aton.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "inet_ntoa",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InetNtoaFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    BigIntHolder in1;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +      StringBuilder result = new StringBuilder(15);
    +
    +      long inputInt = in1.value;
    +
    +      for (int i = 0; i < 4; i++) {
    +
    +        result.insert(0,Long.toString(inputInt & 0xff));
    +
    +        if (i < 3) {
    +          result.insert(0,'.');
    +        }
    +
    +        inputInt = inputInt >> 8;
    +      }
    +
    +      String outputValue = result.toString();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +    }
    +
    +
    +  }
    +
    +  /**
    +   * This function returns true if a given IPv4 address is private, false if not.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "is_private_ip",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class IsPrivateIP implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputTextA;
    +
    +    @Output BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputTextA.start, inputTextA.end, inputTextA.buffer);
    +
    +      String[] ipAddressInArray = ipString.split("\\.");
    +
    +      int result = 0;
    +
    +      int[] octets = new int[3];
    +
    +      for( int i = 0; i < 3; i++ ){
    +        octets[i] = Integer.parseInt( ipAddressInArray[i] );
    +        if( octets[i] > 255 || octets[i] < 0 ) {
    +          result = 0;
    +        }
    +      }
    +
    +      if( octets[0] == 192 && octets[1] == 168 ) {
    +        result = 1;
    +      }
    +      else if (octets[0] == 172 && octets [1] >= 16 && octets[1] <= 31 ){
    +        result = 1;
    +      }
    +      else if( octets[0] == 10 ) {
    +        result = 1;
    +      }
    +      else {
    +        result = 0;
    +      }
    +
    +      out.value = result;
    +    }
    +  }
    +  /**
    +   *  This function converts an IPv4 address into a BigInt.  Useful for sorting IPs, or looking for IP ranges.
    +   *  IE:
    +   *  SELECT *
    +   *  FROM <data>
    +   *  ORDER BY inet_aton( ip ) ASC
    +   */
    +  @FunctionTemplate(
    +    name = "inet_aton",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InetAtonFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputTextA;
    +
    +    @Output BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputTextA.start, inputTextA.end, inputTextA.buffer);
    +      if( ipString == null || ipString.isEmpty() || ipString.length() == 0 ){
    +        out.value = 0;
    +      } else {
    +        String[] ipAddressInArray = ipString.split("\\.");
    +
    +        long result = 0;
    +        for (int i = 0; i < ipAddressInArray.length; i++) {
    +          int power = 3 - i;
    +          int ip = Integer.parseInt(ipAddressInArray[i]);
    +          result += ip * Math.pow(256, power);
    +        }
    +
    +        out.value = result;
    +      }
    +    }
    +  }
    +
    +  /**
    +   * Returns true if the input string is a valid IP address
    +   */
    +  @FunctionTemplate(
    +    name = "is_valid_IP",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class IsValidIPFunction implements DrillSimpleFunc {
    --- End diff --
    
    Also you may ask on dev list regarding `commons-logging` usage on Drill dev list.


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143949784
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    +        result = 1;
    +      }
    +      else{
    +        result = 0;
    +      }
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getAddressCount",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getAddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      out.value = utils.getInfo().getAddressCount();
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function returns the broadcast address of a given CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getBroadcastAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getBroadcastAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getBroadcastAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the netmask of the input CIDR block.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "getNetmask",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getNetmaskFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getNetmask();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically lowest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getLowAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getLowAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getLowAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically highest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getHighAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getHighddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getHighAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function encodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "urlencode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urlencodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLEncoder.encode(url, "UTF-8");
    +      }catch (Exception e){
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function decodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "urldecode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urldecodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLDecoder.decode(url, "UTF-8");
    +      }catch (Exception e){
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +
    +  /**
    +   * This function converts a BigInt IPv4 into dotted decimal notation.  The opposite of inet_aton.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "inet_ntoa",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InetNtoaFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    BigIntHolder in1;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +      StringBuilder result = new StringBuilder(15);
    +
    +      long inputInt = in1.value;
    +
    +      for (int i = 0; i < 4; i++) {
    +
    +        result.insert(0,Long.toString(inputInt & 0xff));
    +
    +        if (i < 3) {
    +          result.insert(0,'.');
    +        }
    +
    +        inputInt = inputInt >> 8;
    +      }
    +
    +      String outputValue = result.toString();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +    }
    +
    +
    +  }
    +
    +  /**
    +   * This function returns true if a given IPv4 address is private, false if not.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "is_private_ip",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class IsPrivateIP implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputTextA;
    +
    +    @Output BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputTextA.start, inputTextA.end, inputTextA.buffer);
    +
    +      String[] ipAddressInArray = ipString.split("\\.");
    +
    +      int result = 0;
    +
    +      int[] octets = new int[3];
    +
    +      for( int i = 0; i < 3; i++ ){
    +        octets[i] = Integer.parseInt( ipAddressInArray[i] );
    +        if( octets[i] > 255 || octets[i] < 0 ) {
    +          result = 0;
    +        }
    +      }
    +
    +      if( octets[0] == 192 && octets[1] == 168 ) {
    +        result = 1;
    +      }
    +      else if (octets[0] == 172 && octets [1] >= 16 && octets[1] <= 31 ){
    +        result = 1;
    +      }
    +      else if( octets[0] == 10 ) {
    +        result = 1;
    +      }
    +      else {
    +        result = 0;
    +      }
    +
    +      out.value = result;
    +    }
    +  }
    +  /**
    +   *  This function converts an IPv4 address into a BigInt.  Useful for sorting IPs, or looking for IP ranges.
    +   *  IE:
    +   *  SELECT *
    +   *  FROM <data>
    +   *  ORDER BY inet_aton( ip ) ASC
    +   */
    +  @FunctionTemplate(
    +    name = "inet_aton",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InetAtonFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputTextA;
    +
    +    @Output BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputTextA.start, inputTextA.end, inputTextA.buffer);
    +      if( ipString == null || ipString.isEmpty() || ipString.length() == 0 ){
    +        out.value = 0;
    +      } else {
    +        String[] ipAddressInArray = ipString.split("\\.");
    +
    +        long result = 0;
    +        for (int i = 0; i < ipAddressInArray.length; i++) {
    +          int power = 3 - i;
    +          int ip = Integer.parseInt(ipAddressInArray[i]);
    +          result += ip * Math.pow(256, power);
    +        }
    +
    +        out.value = result;
    +      }
    +    }
    +  }
    +
    +  /**
    +   * Returns true if the input string is a valid IP address
    +   */
    +  @FunctionTemplate(
    +    name = "is_valid_IP",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class IsValidIPFunction implements DrillSimpleFunc {
    --- End diff --
    
    May we use existing lib, for example, Apache commons instead of writing regexp (here and in two more functions below)?
    
    [1] http://commons.apache.org/proper/commons-validator/apidocs/org/apache/commons/validator/routines/InetAddressValidator.html


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by cgivre <gi...@git.apache.org>.
Github user cgivre closed the pull request at:

    https://github.com/apache/drill/pull/971


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by cgivre <gi...@git.apache.org>.
Github user cgivre commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r146263421
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,566 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions {
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {
    +  }
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   */
    +  @FunctionTemplate(name = "in_network", scope = FunctionTemplate.FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.NULL_IF_NULL)
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      org.apache.commons.net.util.SubnetUtils utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if (utils.getInfo().isInRange(ipString)) {
    +        result = 1;
    +      }
    +
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(name = "address_count", scope = FunctionTemplate.FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.NULL_IF_NULL)
    +  public static class AddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      org.apache.commons.net.util.SubnetUtils utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      out.value = utils.getInfo().getAddressCount();
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function returns the broadcast address of a given CIDR block.
    +   */
    +  @FunctionTemplate(name = "broadcast_address", scope = FunctionTemplate.FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.NULL_IF_NULL)
    +  public static class BroadcastAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      org.apache.commons.net.util.SubnetUtils utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getBroadcastAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the netmask of the input CIDR block.
    +   */
    +
    +  @FunctionTemplate(name = "netmask", scope = FunctionTemplate.FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.NULL_IF_NULL)
    +  public static class NetmaskFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      org.apache.commons.net.util.SubnetUtils utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getNetmask();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically lowest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(name = "low_address", scope = FunctionTemplate.FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.NULL_IF_NULL)
    +  public static class LowAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      org.apache.commons.net.util.SubnetUtils utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getLowAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically highest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(name = "high_address", scope = FunctionTemplate.FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.NULL_IF_NULL)
    +  public static class HighAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      org.apache.commons.net.util.SubnetUtils utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getHighAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function encodes URL strings.
    +   */
    +  @FunctionTemplate(name = "url_encode", scope = FunctionTemplate.FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.NULL_IF_NULL)
    +  public static class urlencodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLEncoder.encode(url, "UTF-8");
    +      } catch (Exception e) {
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function decodes URL strings.
    +   */
    +  @FunctionTemplate(name = "url_decode", scope = FunctionTemplate.FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.NULL_IF_NULL)
    +  public static class urldecodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLDecoder.decode(url, "UTF-8");
    +      } catch (Exception e) {
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +
    +  /**
    +   * This function converts a BigInt IPv4 into dotted decimal notation.  The opposite of inet_aton.
    +   */
    +
    +  @FunctionTemplate(name = "inet_ntoa", scope = FunctionTemplate.FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.NULL_IF_NULL)
    +  public static class InetNtoaFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    BigIntHolder in;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +      StringBuilder result = new StringBuilder(15);
    +
    +      long inputInt = in.value;
    +
    +      for (int i = 0; i < 4; i++) {
    +
    +        result.insert(0, Long.toString(inputInt & 0xff));
    +
    +        if (i < 3) {
    +          result.insert(0, '.');
    +        }
    +
    +        inputInt = inputInt >> 8;
    +      }
    +
    +      String outputValue = result.toString();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +    }
    +
    +
    +  }
    +
    +  /**
    +   * This function returns true if a given IPv4 address is private, false if not.
    +   */
    +
    +  @FunctionTemplate(name = "is_private_ip", scope = FunctionTemplate.FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.NULL_IF_NULL)
    +  public static class IsPrivateIP implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputTextA;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputTextA.start, inputTextA.end, inputTextA.buffer);
    +
    +      String[] ipAddressInArray = ipString.split("\\.");
    +
    +      int result = 0;
    +
    +      int[] octets = new int[3];
    +
    +      for (int i = 0; i < 3; i++) {
    +        octets[i] = Integer.parseInt(ipAddressInArray[i]);
    +        if (octets[i] > 255 || octets[i] < 0) {
    --- End diff --
    
    I put that in there to detect invalid IP addresses.  I guess we can remove it. 


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143950296
  
    --- Diff: exec/java-exec/src/test/java/org/apache/drill/exec/fn/impl/TestNetworkFunctions.java ---
    @@ -0,0 +1,93 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.fn.impl;
    +
    +import org.apache.drill.BaseTestQuery;
    +import org.junit.Test;
    +
    +public class TestNetworkFunctions extends BaseTestQuery {
    +
    +  @Test
    +  public void testInetAton() throws Exception {
    +    final String query = "select inet_aton( '192.168.0.1') as inet from (values(1))";
    +    testBuilder().sqlQuery(query).ordered().baselineColumns("inet").baselineValues( Long.parseLong("3232235521") ).go();
    +  }
    +
    +  @Test
    +  public void testInetNtoa() throws Exception {
    +    final String query = "select inet_ntoa( 3232235521 ) as inet from (values(1))";
    +    testBuilder().sqlQuery(query).ordered().baselineColumns("inet").baselineValues("192.168.0.1").go();
    +  }
    +
    +  @Test
    +  public void testInNetwork() throws Exception {
    +    final String query = "select in_network( '192.168.0.1', '192.168.0.0/28' ) as in_net FROM (values(1))";
    +    testBuilder().sqlQuery(query).ordered().baselineColumns("in_net").baselineValues(true).go();
    +  }
    +
    +  @Test
    +  public void testBroadcastAddress() throws Exception {
    +    final String query = "select getBroadcastAddress( '192.168.0.0/28' ) AS broadcastAddress FROM (values(1))";
    +    testBuilder().sqlQuery(query).ordered().baselineColumns("broadcastAddress").baselineValues("192.168.0.15").go();
    +  }
    +  @Test
    +  public void testNetmask() throws Exception {
    +    final String query = "select getNetmask( '192.168.0.0/28' ) AS netmask FROM (values(1))";
    +    testBuilder().sqlQuery(query).ordered().baselineColumns("netmask").baselineValues("255.255.255.240").go();
    +  }
    +  @Test
    +  public void testFunctions() throws Exception {
    --- End diff --
    
    Well, it is still good to split test into multiple for each function.


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143947900
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    +        result = 1;
    +      }
    +      else{
    +        result = 0;
    +      }
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getAddressCount",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getAddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      out.value = utils.getInfo().getAddressCount();
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function returns the broadcast address of a given CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getBroadcastAddress",
    --- End diff --
    
    `broadcast_address`?


---

[GitHub] drill issue #971: Drill-5834 Add Networking Functions

Posted by cgivre <gi...@git.apache.org>.
Github user cgivre commented on the issue:

    https://github.com/apache/drill/pull/971
  
    Thanks!  I’ll resubmit this weekend using commons-validator
    
    
    > On Oct 11, 2017, at 11:26, Arina Ielchiieva <no...@github.com> wrote:
    > 
    > @arina-ielchiieva commented on this pull request.
    > 
    > In exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java <https://github.com/apache/drill/pull/971#discussion_r144047807>:
    > 
    > > +        }
    > +
    > +        out.value = result;
    > +      }
    > +    }
    > +  }
    > +
    > +  /**
    > +   * Returns true if the input string is a valid IP address
    > +   */
    > +  @FunctionTemplate(
    > +    name = "is_valid_IP",
    > +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    > +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    > +  )
    > +  public static class IsValidIPFunction implements DrillSimpleFunc {
    > We exclude commons-logging because Drill uses logback for logging.
    > Excluding it from commons-validator should work perfectly fine (I have tried).
    > 
    >     <dependency>
    >       <groupId>commons-validator</groupId>
    >       <artifactId>commons-validator</artifactId>
    >       <version>1.4.0</version>
    >       <exclusions>
    >       <exclusion>
    >         <groupId>commons-logging</groupId>
    >         <artifactId>commons-logging</artifactId>
    >       </exclusion>
    >       </exclusions>
    >     </dependency>
    > —
    > You are receiving this because you were mentioned.
    > Reply to this email directly, view it on GitHub <https://github.com/apache/drill/pull/971#discussion_r144047807>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AFQfvnNVrqNWjpBNlz61SDhXT9nhnJxcks5srN4ngaJpZM4Pseni>.
    > 
    



---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r145080845
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,619 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions {
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      org.apache.commons.net.util.SubnetUtils utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if(utils.getInfo().isInRange(ipString) ){
    +        result = 1;
    +      }
    +
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "address_count",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class AddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      org.apache.commons.net.util.SubnetUtils utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      out.value = utils.getInfo().getAddressCount();
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function returns the broadcast address of a given CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "broadcast_address",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class BroadcastAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      org.apache.commons.net.util.SubnetUtils utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getBroadcastAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the netmask of the input CIDR block.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "netmask",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class NetmaskFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      org.apache.commons.net.util.SubnetUtils utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getNetmask();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically lowest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "low_address",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class LowAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      org.apache.commons.net.util.SubnetUtils utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getLowAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically highest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "high_address",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class HighAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      org.apache.commons.net.util.SubnetUtils utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getHighAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function encodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "url_encode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urlencodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLEncoder.encode(url, "UTF-8");
    +      }catch (Exception e){
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function decodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "url_decode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urldecodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLDecoder.decode(url, "UTF-8");
    +      }catch (Exception e){
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +
    +  /**
    +   * This function converts a BigInt IPv4 into dotted decimal notation.  The opposite of inet_aton.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "inet_ntoa",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InetNtoaFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    BigIntHolder in;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +      StringBuilder result = new StringBuilder(15);
    +
    +      long inputInt = in.value;
    +
    +      for (int i = 0; i < 4; i++) {
    +
    +        result.insert(0,Long.toString(inputInt & 0xff));
    +
    +        if (i < 3) {
    +          result.insert(0,'.');
    +        }
    +
    +        inputInt = inputInt >> 8;
    +      }
    +
    +      String outputValue = result.toString();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +    }
    +
    +
    +  }
    +
    +  /**
    +   * This function returns true if a given IPv4 address is private, false if not.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "is_private_ip",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class IsPrivateIP implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputTextA;
    +
    +    @Output BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputTextA.start, inputTextA.end, inputTextA.buffer);
    +
    +      String[] ipAddressInArray = ipString.split("\\.");
    +
    +      int result = 0;
    +
    +      int[] octets = new int[3];
    +
    +      for( int i = 0; i < 3; i++ ){
    +        octets[i] = Integer.parseInt( ipAddressInArray[i] );
    +        if( octets[i] > 255 || octets[i] < 0 ) {
    +          result = 0;
    +        }
    +      }
    +
    +      if( octets[0] == 192 && octets[1] == 168 ) {
    +        result = 1;
    +      }
    +      else if (octets[0] == 172 && octets [1] >= 16 && octets[1] <= 31 ){
    +        result = 1;
    +      }
    +      else if( octets[0] == 10 ) {
    +        result = 1;
    +      }
    +      else {
    +        result = 0;
    +      }
    +
    +      out.value = result;
    +    }
    +  }
    +  /**
    +   *  This function converts an IPv4 address into a BigInt.  Useful for sorting IPs, or looking for IP ranges.
    +   *  IE:
    +   *  SELECT *
    +   *  FROM <data>
    +   *  ORDER BY inet_aton( ip ) ASC
    +   */
    +  @FunctionTemplate(
    +    name = "inet_aton",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InetAtonFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputTextA;
    +
    +    @Output BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputTextA.start, inputTextA.end, inputTextA.buffer);
    +      if( ipString == null || ipString.isEmpty() || ipString.length() == 0 ){
    +        out.value = 0;
    +      } else {
    +        String[] ipAddressInArray = ipString.split("\\.");
    +
    +        long result = 0;
    +        for (int i = 0; i < ipAddressInArray.length; i++) {
    +          int power = 3 - i;
    +          int ip = Integer.parseInt(ipAddressInArray[i]);
    +          result += ip * Math.pow(256, power);
    +        }
    +
    +        out.value = result;
    +      }
    +    }
    +  }
    +
    +  /**
    +   * Returns true if the input string is a valid IP address
    +   */
    +  @FunctionTemplate(
    +    name = "is_valid_IP",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class IsValidIPFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Output BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      if( ipString == null || ipString.isEmpty() || ipString.length() == 0 ){
    --- End diff --
    
    @cgivre regarding coding convention. I have mentioned this multiple time in your previous pull request and in this as well. You do fix some parts but still there are many places where code conventions are not followed. I suggest you follow guidelines from the link below [1], import formatter and format code using your IDE. It's really tedious for the code reviewer to point to all places where simple formatting are not observed which can be done by the developer with simple keyboard shortcut but following code conventions a common rule here and we all try to follow it!
    
    [1] https://drill.apache.org/docs/apache-drill-contribution-guidelines/ 


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143945768
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    +        result = 1;
    +      }
    +      else{
    --- End diff --
    
    Why you need else if you have already defined result as 0?


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r144032079
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    +        result = 1;
    +      }
    +      else{
    +        result = 0;
    +      }
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getAddressCount",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getAddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      out.value = utils.getInfo().getAddressCount();
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function returns the broadcast address of a given CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getBroadcastAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getBroadcastAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getBroadcastAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the netmask of the input CIDR block.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "getNetmask",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getNetmaskFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getNetmask();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically lowest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getLowAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getLowAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getLowAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically highest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getHighAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getHighddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getHighAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function encodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "urlencode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urlencodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLEncoder.encode(url, "UTF-8");
    +      }catch (Exception e){
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function decodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "urldecode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urldecodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLDecoder.decode(url, "UTF-8");
    +      }catch (Exception e){
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +
    +  /**
    +   * This function converts a BigInt IPv4 into dotted decimal notation.  The opposite of inet_aton.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "inet_ntoa",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InetNtoaFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    BigIntHolder in1;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +      StringBuilder result = new StringBuilder(15);
    +
    +      long inputInt = in1.value;
    +
    +      for (int i = 0; i < 4; i++) {
    +
    +        result.insert(0,Long.toString(inputInt & 0xff));
    +
    +        if (i < 3) {
    +          result.insert(0,'.');
    +        }
    +
    +        inputInt = inputInt >> 8;
    +      }
    +
    +      String outputValue = result.toString();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +    }
    +
    +
    +  }
    +
    +  /**
    +   * This function returns true if a given IPv4 address is private, false if not.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "is_private_ip",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class IsPrivateIP implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputTextA;
    +
    +    @Output BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputTextA.start, inputTextA.end, inputTextA.buffer);
    +
    +      String[] ipAddressInArray = ipString.split("\\.");
    +
    +      int result = 0;
    +
    +      int[] octets = new int[3];
    +
    +      for( int i = 0; i < 3; i++ ){
    +        octets[i] = Integer.parseInt( ipAddressInArray[i] );
    +        if( octets[i] > 255 || octets[i] < 0 ) {
    +          result = 0;
    +        }
    +      }
    +
    +      if( octets[0] == 192 && octets[1] == 168 ) {
    +        result = 1;
    +      }
    +      else if (octets[0] == 172 && octets [1] >= 16 && octets[1] <= 31 ){
    +        result = 1;
    +      }
    +      else if( octets[0] == 10 ) {
    +        result = 1;
    +      }
    +      else {
    +        result = 0;
    +      }
    +
    +      out.value = result;
    +    }
    +  }
    +  /**
    +   *  This function converts an IPv4 address into a BigInt.  Useful for sorting IPs, or looking for IP ranges.
    +   *  IE:
    +   *  SELECT *
    +   *  FROM <data>
    +   *  ORDER BY inet_aton( ip ) ASC
    +   */
    +  @FunctionTemplate(
    +    name = "inet_aton",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InetAtonFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputTextA;
    +
    +    @Output BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputTextA.start, inputTextA.end, inputTextA.buffer);
    +      if( ipString == null || ipString.isEmpty() || ipString.length() == 0 ){
    +        out.value = 0;
    +      } else {
    +        String[] ipAddressInArray = ipString.split("\\.");
    +
    +        long result = 0;
    +        for (int i = 0; i < ipAddressInArray.length; i++) {
    +          int power = 3 - i;
    +          int ip = Integer.parseInt(ipAddressInArray[i]);
    +          result += ip * Math.pow(256, power);
    +        }
    +
    +        out.value = result;
    +      }
    +    }
    +  }
    +
    +  /**
    +   * Returns true if the input string is a valid IP address
    +   */
    +  @FunctionTemplate(
    +    name = "is_valid_IP",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class IsValidIPFunction implements DrillSimpleFunc {
    --- End diff --
    
    Why it's banned in Drill? Can we exclude it, for example?


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143944908
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    --- End diff --
    
    space -> `NetworkFunctions {`


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143947835
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    +        result = 1;
    +      }
    +      else{
    +        result = 0;
    +      }
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getAddressCount",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getAddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      out.value = utils.getInfo().getAddressCount();
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function returns the broadcast address of a given CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getBroadcastAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getBroadcastAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getBroadcastAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the netmask of the input CIDR block.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "getNetmask",
    --- End diff --
    
    `netmask`?


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by cgivre <gi...@git.apache.org>.
Github user cgivre commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r144722146
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    +        result = 1;
    +      }
    +      else{
    +        result = 0;
    +      }
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getAddressCount",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getAddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      out.value = utils.getInfo().getAddressCount();
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function returns the broadcast address of a given CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getBroadcastAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getBroadcastAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getBroadcastAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the netmask of the input CIDR block.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "getNetmask",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getNetmaskFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getNetmask();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically lowest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getLowAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getLowAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getLowAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically highest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getHighAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getHighddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getHighAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function encodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "urlencode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urlencodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLEncoder.encode(url, "UTF-8");
    +      }catch (Exception e){
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function decodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "urldecode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urldecodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLDecoder.decode(url, "UTF-8");
    +      }catch (Exception e){
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +
    +  /**
    +   * This function converts a BigInt IPv4 into dotted decimal notation.  The opposite of inet_aton.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "inet_ntoa",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InetNtoaFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    BigIntHolder in1;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +      StringBuilder result = new StringBuilder(15);
    +
    +      long inputInt = in1.value;
    +
    +      for (int i = 0; i < 4; i++) {
    +
    +        result.insert(0,Long.toString(inputInt & 0xff));
    +
    +        if (i < 3) {
    +          result.insert(0,'.');
    +        }
    +
    +        inputInt = inputInt >> 8;
    +      }
    +
    +      String outputValue = result.toString();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +    }
    +
    +
    +  }
    +
    +  /**
    +   * This function returns true if a given IPv4 address is private, false if not.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "is_private_ip",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class IsPrivateIP implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputTextA;
    +
    +    @Output BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputTextA.start, inputTextA.end, inputTextA.buffer);
    +
    +      String[] ipAddressInArray = ipString.split("\\.");
    +
    +      int result = 0;
    +
    +      int[] octets = new int[3];
    +
    +      for( int i = 0; i < 3; i++ ){
    +        octets[i] = Integer.parseInt( ipAddressInArray[i] );
    +        if( octets[i] > 255 || octets[i] < 0 ) {
    +          result = 0;
    +        }
    +      }
    +
    +      if( octets[0] == 192 && octets[1] == 168 ) {
    +        result = 1;
    +      }
    +      else if (octets[0] == 172 && octets [1] >= 16 && octets[1] <= 31 ){
    +        result = 1;
    +      }
    +      else if( octets[0] == 10 ) {
    +        result = 1;
    +      }
    +      else {
    +        result = 0;
    +      }
    +
    +      out.value = result;
    +    }
    +  }
    +  /**
    +   *  This function converts an IPv4 address into a BigInt.  Useful for sorting IPs, or looking for IP ranges.
    +   *  IE:
    +   *  SELECT *
    +   *  FROM <data>
    +   *  ORDER BY inet_aton( ip ) ASC
    +   */
    +  @FunctionTemplate(
    +    name = "inet_aton",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InetAtonFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputTextA;
    +
    +    @Output BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputTextA.start, inputTextA.end, inputTextA.buffer);
    +      if( ipString == null || ipString.isEmpty() || ipString.length() == 0 ){
    +        out.value = 0;
    +      } else {
    +        String[] ipAddressInArray = ipString.split("\\.");
    +
    +        long result = 0;
    +        for (int i = 0; i < ipAddressInArray.length; i++) {
    +          int power = 3 - i;
    +          int ip = Integer.parseInt(ipAddressInArray[i]);
    +          result += ip * Math.pow(256, power);
    +        }
    +
    +        out.value = result;
    +      }
    +    }
    +  }
    +
    +  /**
    +   * Returns true if the input string is a valid IP address
    +   */
    +  @FunctionTemplate(
    +    name = "is_valid_IP",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class IsValidIPFunction implements DrillSimpleFunc {
    --- End diff --
    
    Fixed


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r146232191
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,566 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions {
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {
    +  }
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   */
    +  @FunctionTemplate(name = "in_network", scope = FunctionTemplate.FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.NULL_IF_NULL)
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      org.apache.commons.net.util.SubnetUtils utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if (utils.getInfo().isInRange(ipString)) {
    +        result = 1;
    +      }
    +
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(name = "address_count", scope = FunctionTemplate.FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.NULL_IF_NULL)
    +  public static class AddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      org.apache.commons.net.util.SubnetUtils utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      out.value = utils.getInfo().getAddressCount();
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function returns the broadcast address of a given CIDR block.
    +   */
    +  @FunctionTemplate(name = "broadcast_address", scope = FunctionTemplate.FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.NULL_IF_NULL)
    +  public static class BroadcastAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      org.apache.commons.net.util.SubnetUtils utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getBroadcastAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the netmask of the input CIDR block.
    +   */
    +
    +  @FunctionTemplate(name = "netmask", scope = FunctionTemplate.FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.NULL_IF_NULL)
    +  public static class NetmaskFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      org.apache.commons.net.util.SubnetUtils utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getNetmask();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically lowest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(name = "low_address", scope = FunctionTemplate.FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.NULL_IF_NULL)
    +  public static class LowAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      org.apache.commons.net.util.SubnetUtils utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getLowAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically highest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(name = "high_address", scope = FunctionTemplate.FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.NULL_IF_NULL)
    +  public static class HighAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      org.apache.commons.net.util.SubnetUtils utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getHighAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function encodes URL strings.
    +   */
    +  @FunctionTemplate(name = "url_encode", scope = FunctionTemplate.FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.NULL_IF_NULL)
    +  public static class urlencodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLEncoder.encode(url, "UTF-8");
    +      } catch (Exception e) {
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function decodes URL strings.
    +   */
    +  @FunctionTemplate(name = "url_decode", scope = FunctionTemplate.FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.NULL_IF_NULL)
    +  public static class urldecodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLDecoder.decode(url, "UTF-8");
    +      } catch (Exception e) {
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +
    +  /**
    +   * This function converts a BigInt IPv4 into dotted decimal notation.  The opposite of inet_aton.
    +   */
    +
    +  @FunctionTemplate(name = "inet_ntoa", scope = FunctionTemplate.FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.NULL_IF_NULL)
    +  public static class InetNtoaFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    BigIntHolder in;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +      StringBuilder result = new StringBuilder(15);
    +
    +      long inputInt = in.value;
    +
    +      for (int i = 0; i < 4; i++) {
    +
    +        result.insert(0, Long.toString(inputInt & 0xff));
    +
    +        if (i < 3) {
    +          result.insert(0, '.');
    +        }
    +
    +        inputInt = inputInt >> 8;
    +      }
    +
    +      String outputValue = result.toString();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +    }
    +
    +
    +  }
    +
    +  /**
    +   * This function returns true if a given IPv4 address is private, false if not.
    +   */
    +
    +  @FunctionTemplate(name = "is_private_ip", scope = FunctionTemplate.FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.NULL_IF_NULL)
    +  public static class IsPrivateIP implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputTextA;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputTextA.start, inputTextA.end, inputTextA.buffer);
    +
    +      String[] ipAddressInArray = ipString.split("\\.");
    +
    +      int result = 0;
    +
    +      int[] octets = new int[3];
    +
    +      for (int i = 0; i < 3; i++) {
    +        octets[i] = Integer.parseInt(ipAddressInArray[i]);
    +        if (octets[i] > 255 || octets[i] < 0) {
    --- End diff --
    
    Why do we need this `if`?  It seems it can be removed since the subsequent if will take precedence.


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143948455
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    +        result = 1;
    +      }
    +      else{
    +        result = 0;
    +      }
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getAddressCount",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getAddressCountFunction implements DrillSimpleFunc {
    --- End diff --
    
    `AddressCountFunction`


---

[GitHub] drill issue #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on the issue:

    https://github.com/apache/drill/pull/971
  
    +1, LGTM.


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r146223741
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -523,13 +476,13 @@ public void setup() {
     
         public void eval() {
           String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    -      if( ipString == null || ipString.isEmpty() || ipString.length() == 0 ){
    +      if (ipString == null || ipString.isEmpty()) {
             out.value = 0;
           } else {
             org.apache.commons.validator.routines.InetAddressValidator validator = org.apache.commons.validator.routines.InetAddressValidator.getInstance();
     
             boolean result = validator.isValid(ipString);
    -        if( result == true ){
    +        if (result == true) {
    --- End diff --
    
    1. `if (result) {`
    2. we can rename the variable to `valid` -> `if (valid) {`


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by cgivre <gi...@git.apache.org>.
Github user cgivre commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r144722144
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    +        result = 1;
    +      }
    +      else{
    +        result = 0;
    +      }
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getAddressCount",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getAddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      out.value = utils.getInfo().getAddressCount();
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function returns the broadcast address of a given CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getBroadcastAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getBroadcastAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getBroadcastAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the netmask of the input CIDR block.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "getNetmask",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getNetmaskFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getNetmask();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically lowest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getLowAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getLowAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getLowAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically highest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getHighAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getHighddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getHighAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function encodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "urlencode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urlencodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLEncoder.encode(url, "UTF-8");
    +      }catch (Exception e){
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function decodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "urldecode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urldecodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLDecoder.decode(url, "UTF-8");
    +      }catch (Exception e){
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +
    +  /**
    +   * This function converts a BigInt IPv4 into dotted decimal notation.  The opposite of inet_aton.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "inet_ntoa",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InetNtoaFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    BigIntHolder in1;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    --- End diff --
    
    Is there a preference to using an existing library?


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r144047807
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    +        result = 1;
    +      }
    +      else{
    +        result = 0;
    +      }
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getAddressCount",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getAddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      out.value = utils.getInfo().getAddressCount();
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function returns the broadcast address of a given CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getBroadcastAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getBroadcastAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getBroadcastAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the netmask of the input CIDR block.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "getNetmask",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getNetmaskFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getNetmask();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically lowest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getLowAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getLowAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getLowAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically highest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getHighAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getHighddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getHighAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function encodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "urlencode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urlencodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLEncoder.encode(url, "UTF-8");
    +      }catch (Exception e){
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function decodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "urldecode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urldecodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLDecoder.decode(url, "UTF-8");
    +      }catch (Exception e){
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +
    +  /**
    +   * This function converts a BigInt IPv4 into dotted decimal notation.  The opposite of inet_aton.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "inet_ntoa",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InetNtoaFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    BigIntHolder in1;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +      StringBuilder result = new StringBuilder(15);
    +
    +      long inputInt = in1.value;
    +
    +      for (int i = 0; i < 4; i++) {
    +
    +        result.insert(0,Long.toString(inputInt & 0xff));
    +
    +        if (i < 3) {
    +          result.insert(0,'.');
    +        }
    +
    +        inputInt = inputInt >> 8;
    +      }
    +
    +      String outputValue = result.toString();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +    }
    +
    +
    +  }
    +
    +  /**
    +   * This function returns true if a given IPv4 address is private, false if not.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "is_private_ip",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class IsPrivateIP implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputTextA;
    +
    +    @Output BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputTextA.start, inputTextA.end, inputTextA.buffer);
    +
    +      String[] ipAddressInArray = ipString.split("\\.");
    +
    +      int result = 0;
    +
    +      int[] octets = new int[3];
    +
    +      for( int i = 0; i < 3; i++ ){
    +        octets[i] = Integer.parseInt( ipAddressInArray[i] );
    +        if( octets[i] > 255 || octets[i] < 0 ) {
    +          result = 0;
    +        }
    +      }
    +
    +      if( octets[0] == 192 && octets[1] == 168 ) {
    +        result = 1;
    +      }
    +      else if (octets[0] == 172 && octets [1] >= 16 && octets[1] <= 31 ){
    +        result = 1;
    +      }
    +      else if( octets[0] == 10 ) {
    +        result = 1;
    +      }
    +      else {
    +        result = 0;
    +      }
    +
    +      out.value = result;
    +    }
    +  }
    +  /**
    +   *  This function converts an IPv4 address into a BigInt.  Useful for sorting IPs, or looking for IP ranges.
    +   *  IE:
    +   *  SELECT *
    +   *  FROM <data>
    +   *  ORDER BY inet_aton( ip ) ASC
    +   */
    +  @FunctionTemplate(
    +    name = "inet_aton",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InetAtonFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputTextA;
    +
    +    @Output BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputTextA.start, inputTextA.end, inputTextA.buffer);
    +      if( ipString == null || ipString.isEmpty() || ipString.length() == 0 ){
    +        out.value = 0;
    +      } else {
    +        String[] ipAddressInArray = ipString.split("\\.");
    +
    +        long result = 0;
    +        for (int i = 0; i < ipAddressInArray.length; i++) {
    +          int power = 3 - i;
    +          int ip = Integer.parseInt(ipAddressInArray[i]);
    +          result += ip * Math.pow(256, power);
    +        }
    +
    +        out.value = result;
    +      }
    +    }
    +  }
    +
    +  /**
    +   * Returns true if the input string is a valid IP address
    +   */
    +  @FunctionTemplate(
    +    name = "is_valid_IP",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class IsValidIPFunction implements DrillSimpleFunc {
    --- End diff --
    
    We exclude `commons-logging` because Drill uses `logback` for logging.
    Excluding it from `commons-validator` should work perfectly fine (I have tried).
    
    ```
        <dependency>
          <groupId>commons-validator</groupId>
          <artifactId>commons-validator</artifactId>
          <version>1.4.0</version>
          <exclusions>
          <exclusion>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
          </exclusion>
          </exclusions>
        </dependency>
    ```


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143946313
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    +        result = 1;
    +      }
    +      else{
    +        result = 0;
    +      }
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getAddressCount",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getAddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      out.value = utils.getInfo().getAddressCount();
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function returns the broadcast address of a given CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getBroadcastAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getBroadcastAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getBroadcastAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the netmask of the input CIDR block.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "getNetmask",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getNetmaskFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    --- End diff --
    
    Same as above.


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143947725
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    +        result = 1;
    +      }
    +      else{
    +        result = 0;
    +      }
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getAddressCount",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getAddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      out.value = utils.getInfo().getAddressCount();
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function returns the broadcast address of a given CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getBroadcastAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getBroadcastAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getBroadcastAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the netmask of the input CIDR block.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "getNetmask",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getNetmaskFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getNetmask();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically lowest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getLowAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getLowAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getLowAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically highest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getHighAddress",
    --- End diff --
    
    `high_address`?


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143944633
  
    --- Diff: exec/java-exec/pom.xml ---
    @@ -434,7 +434,11 @@
           <artifactId>joda-time</artifactId>
           <version>2.9</version>
         </dependency>
    -
    +    <dependency>
    +      <groupId>commons-net</groupId>
    +      <artifactId>commons-net</artifactId>
    +      <version>LATEST</version>
    --- End diff --
    
    Please use exact lib version instead of `LATEST`.


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by cgivre <gi...@git.apache.org>.
Github user cgivre commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r144030560
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    +        result = 1;
    +      }
    +      else{
    +        result = 0;
    +      }
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getAddressCount",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getAddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      out.value = utils.getInfo().getAddressCount();
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function returns the broadcast address of a given CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getBroadcastAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getBroadcastAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getBroadcastAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the netmask of the input CIDR block.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "getNetmask",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getNetmaskFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getNetmask();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically lowest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getLowAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getLowAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getLowAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically highest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getHighAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getHighddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getHighAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function encodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "urlencode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urlencodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLEncoder.encode(url, "UTF-8");
    +      }catch (Exception e){
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function decodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "urldecode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urldecodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLDecoder.decode(url, "UTF-8");
    +      }catch (Exception e){
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +
    +  /**
    +   * This function converts a BigInt IPv4 into dotted decimal notation.  The opposite of inet_aton.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "inet_ntoa",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InetNtoaFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    BigIntHolder in1;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +      StringBuilder result = new StringBuilder(15);
    +
    +      long inputInt = in1.value;
    +
    +      for (int i = 0; i < 4; i++) {
    +
    +        result.insert(0,Long.toString(inputInt & 0xff));
    +
    +        if (i < 3) {
    +          result.insert(0,'.');
    +        }
    +
    +        inputInt = inputInt >> 8;
    +      }
    +
    +      String outputValue = result.toString();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +    }
    +
    +
    +  }
    +
    +  /**
    +   * This function returns true if a given IPv4 address is private, false if not.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "is_private_ip",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class IsPrivateIP implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputTextA;
    +
    +    @Output BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputTextA.start, inputTextA.end, inputTextA.buffer);
    +
    +      String[] ipAddressInArray = ipString.split("\\.");
    +
    +      int result = 0;
    +
    +      int[] octets = new int[3];
    +
    +      for( int i = 0; i < 3; i++ ){
    +        octets[i] = Integer.parseInt( ipAddressInArray[i] );
    +        if( octets[i] > 255 || octets[i] < 0 ) {
    +          result = 0;
    +        }
    +      }
    +
    +      if( octets[0] == 192 && octets[1] == 168 ) {
    +        result = 1;
    +      }
    +      else if (octets[0] == 172 && octets [1] >= 16 && octets[1] <= 31 ){
    +        result = 1;
    +      }
    +      else if( octets[0] == 10 ) {
    +        result = 1;
    +      }
    +      else {
    +        result = 0;
    +      }
    +
    +      out.value = result;
    +    }
    +  }
    +  /**
    +   *  This function converts an IPv4 address into a BigInt.  Useful for sorting IPs, or looking for IP ranges.
    +   *  IE:
    +   *  SELECT *
    +   *  FROM <data>
    +   *  ORDER BY inet_aton( ip ) ASC
    +   */
    +  @FunctionTemplate(
    +    name = "inet_aton",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InetAtonFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputTextA;
    +
    +    @Output BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputTextA.start, inputTextA.end, inputTextA.buffer);
    +      if( ipString == null || ipString.isEmpty() || ipString.length() == 0 ){
    +        out.value = 0;
    +      } else {
    +        String[] ipAddressInArray = ipString.split("\\.");
    +
    +        long result = 0;
    +        for (int i = 0; i < ipAddressInArray.length; i++) {
    +          int power = 3 - i;
    +          int ip = Integer.parseInt(ipAddressInArray[i]);
    +          result += ip * Math.pow(256, power);
    +        }
    +
    +        out.value = result;
    +      }
    +    }
    +  }
    +
    +  /**
    +   * Returns true if the input string is a valid IP address
    +   */
    +  @FunctionTemplate(
    +    name = "is_valid_IP",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class IsValidIPFunction implements DrillSimpleFunc {
    --- End diff --
    
    That was my original plan, however the commons validator has a dependency on commons-logging which is banned in Drill.  I wasn't sure why that's the case, but if we can fix that somehow, I can reinstate the code using commons-validator.


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143946390
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    +        result = 1;
    +      }
    +      else{
    +        result = 0;
    +      }
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getAddressCount",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getAddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      out.value = utils.getInfo().getAddressCount();
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function returns the broadcast address of a given CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getBroadcastAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getBroadcastAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getBroadcastAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the netmask of the input CIDR block.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "getNetmask",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getNetmaskFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getNetmask();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically lowest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getLowAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getLowAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    --- End diff --
    
    Same as above.


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143944750
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    --- End diff --
    
    Apache header should be in a form of comment, not Java doc.


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r145078865
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,619 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions {
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      org.apache.commons.net.util.SubnetUtils utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if(utils.getInfo().isInRange(ipString) ){
    +        result = 1;
    +      }
    +
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "address_count",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class AddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      org.apache.commons.net.util.SubnetUtils utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      out.value = utils.getInfo().getAddressCount();
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function returns the broadcast address of a given CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "broadcast_address",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class BroadcastAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      org.apache.commons.net.util.SubnetUtils utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getBroadcastAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the netmask of the input CIDR block.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "netmask",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class NetmaskFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      org.apache.commons.net.util.SubnetUtils utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getNetmask();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically lowest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "low_address",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class LowAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      org.apache.commons.net.util.SubnetUtils utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getLowAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically highest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "high_address",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class HighAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      org.apache.commons.net.util.SubnetUtils utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getHighAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function encodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "url_encode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urlencodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLEncoder.encode(url, "UTF-8");
    +      }catch (Exception e){
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function decodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "url_decode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urldecodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLDecoder.decode(url, "UTF-8");
    +      }catch (Exception e){
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +
    +  /**
    +   * This function converts a BigInt IPv4 into dotted decimal notation.  The opposite of inet_aton.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "inet_ntoa",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InetNtoaFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    BigIntHolder in;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +      StringBuilder result = new StringBuilder(15);
    +
    +      long inputInt = in.value;
    +
    +      for (int i = 0; i < 4; i++) {
    +
    +        result.insert(0,Long.toString(inputInt & 0xff));
    +
    +        if (i < 3) {
    +          result.insert(0,'.');
    +        }
    +
    +        inputInt = inputInt >> 8;
    +      }
    +
    +      String outputValue = result.toString();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +    }
    +
    +
    +  }
    +
    +  /**
    +   * This function returns true if a given IPv4 address is private, false if not.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "is_private_ip",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class IsPrivateIP implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputTextA;
    +
    +    @Output BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputTextA.start, inputTextA.end, inputTextA.buffer);
    +
    +      String[] ipAddressInArray = ipString.split("\\.");
    +
    +      int result = 0;
    +
    +      int[] octets = new int[3];
    +
    +      for( int i = 0; i < 3; i++ ){
    +        octets[i] = Integer.parseInt( ipAddressInArray[i] );
    +        if( octets[i] > 255 || octets[i] < 0 ) {
    +          result = 0;
    +        }
    +      }
    +
    +      if( octets[0] == 192 && octets[1] == 168 ) {
    +        result = 1;
    +      }
    +      else if (octets[0] == 172 && octets [1] >= 16 && octets[1] <= 31 ){
    +        result = 1;
    +      }
    +      else if( octets[0] == 10 ) {
    +        result = 1;
    +      }
    +      else {
    +        result = 0;
    +      }
    +
    +      out.value = result;
    +    }
    +  }
    +  /**
    +   *  This function converts an IPv4 address into a BigInt.  Useful for sorting IPs, or looking for IP ranges.
    +   *  IE:
    +   *  SELECT *
    +   *  FROM <data>
    +   *  ORDER BY inet_aton( ip ) ASC
    +   */
    +  @FunctionTemplate(
    +    name = "inet_aton",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InetAtonFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputTextA;
    +
    +    @Output BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputTextA.start, inputTextA.end, inputTextA.buffer);
    +      if( ipString == null || ipString.isEmpty() || ipString.length() == 0 ){
    --- End diff --
    
    `isEmpty()` is the same as `length() == 0`. Please remove excess check.
    Please fix here and in other functions


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143948743
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    +        result = 1;
    +      }
    +      else{
    +        result = 0;
    +      }
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getAddressCount",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getAddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      out.value = utils.getInfo().getAddressCount();
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function returns the broadcast address of a given CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getBroadcastAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getBroadcastAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getBroadcastAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the netmask of the input CIDR block.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "getNetmask",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getNetmaskFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getNetmask();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically lowest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getLowAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getLowAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getLowAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically highest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getHighAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getHighddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getHighAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function encodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "urlencode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urlencodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLEncoder.encode(url, "UTF-8");
    +      }catch (Exception e){
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function decodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "urldecode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urldecodeFunction implements DrillSimpleFunc {
    --- End diff --
    
    `UrlDecodeFunction`


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143946222
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    +        result = 1;
    +      }
    +      else{
    +        result = 0;
    +      }
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getAddressCount",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getAddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      out.value = utils.getInfo().getAddressCount();
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function returns the broadcast address of a given CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getBroadcastAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getBroadcastAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    --- End diff --
    
    Same as above.


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143948634
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    +        result = 1;
    +      }
    +      else{
    +        result = 0;
    +      }
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getAddressCount",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getAddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      out.value = utils.getInfo().getAddressCount();
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function returns the broadcast address of a given CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getBroadcastAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getBroadcastAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getBroadcastAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the netmask of the input CIDR block.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "getNetmask",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getNetmaskFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getNetmask();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically lowest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getLowAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getLowAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getLowAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically highest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getHighAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getHighddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getHighAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function encodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "urlencode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urlencodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLEncoder.encode(url, "UTF-8");
    +      }catch (Exception e){
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function decodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "urldecode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urldecodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLDecoder.decode(url, "UTF-8");
    +      }catch (Exception e){
    --- End diff --
    
    spaces 


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143948692
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    +        result = 1;
    +      }
    +      else{
    +        result = 0;
    +      }
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getAddressCount",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getAddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      out.value = utils.getInfo().getAddressCount();
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function returns the broadcast address of a given CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getBroadcastAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getBroadcastAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getBroadcastAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the netmask of the input CIDR block.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "getNetmask",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getNetmaskFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getNetmask();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically lowest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getLowAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getLowAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getLowAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically highest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getHighAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getHighddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getHighAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function encodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "urlencode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urlencodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLEncoder.encode(url, "UTF-8");
    +      }catch (Exception e){
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function decodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "urldecode",
    --- End diff --
    
    `url_decode`


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143947986
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    +        result = 1;
    +      }
    +      else{
    +        result = 0;
    +      }
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getAddressCount",
    --- End diff --
    
    `address_count`?


---

[GitHub] drill issue #971: Drill-5834 Add Networking Functions

Posted by cgivre <gi...@git.apache.org>.
Github user cgivre commented on the issue:

    https://github.com/apache/drill/pull/971
  
    I applied the code formatting template from the Drill site in IntelliJ and resubmitted.   Regarding converting an IP to an int, there are tons of examples of this online and when I implemented it, I reviewed a few of them and adapted it to Drill.   I don't remember which one I used at this point, but here is a good explanation of it. 
    https://www.mkyong.com/java/java-convert-ip-address-to-decimal-number/
    
    Other examples:
    https://stackoverflow.com/questions/12057853/how-to-convert-string-ip-numbers-to-integer-in-java
    
    If you'd prefer, I can resubmit using a pre-existing library. 
    
    For `is_private`, it really isn't that complicated.  Assuming the IP is valid to start with, there are three cases:
    1.  If the IP address starts with 192.168 it is private
    2.  If the IP starts with 172. and the second octet is between 16-31 (inclusive) it is private
    3.  If the IP starts with 10. it is private. 
    
    Anything else is a public IP. 
    
    
    



---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r146223706
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -563,13 +513,13 @@ public void setup() {
     
         public void eval() {
           String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    -      if( ipString == null || ipString.isEmpty() || ipString.length() == 0 ){
    +      if (ipString == null || ipString.isEmpty()) {
             out.value = 0;
           } else {
             org.apache.commons.validator.routines.InetAddressValidator validator = org.apache.commons.validator.routines.InetAddressValidator.getInstance();
     
             boolean result = validator.isValidInet4Address(ipString);
    -        if( result == true ){
    +        if (result == true) {
    --- End diff --
    
    1. `if (result) {`
    2. we can rename the variable to `valid` -> `if (valid) {`


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143950087
  
    --- Diff: exec/java-exec/src/test/java/org/apache/drill/exec/fn/impl/TestNetworkFunctions.java ---
    @@ -0,0 +1,93 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.fn.impl;
    +
    +import org.apache.drill.BaseTestQuery;
    +import org.junit.Test;
    +
    +public class TestNetworkFunctions extends BaseTestQuery {
    +
    +  @Test
    +  public void testInetAton() throws Exception {
    +    final String query = "select inet_aton( '192.168.0.1') as inet from (values(1))";
    +    testBuilder().sqlQuery(query).ordered().baselineColumns("inet").baselineValues( Long.parseLong("3232235521") ).go();
    +  }
    +
    +  @Test
    +  public void testInetNtoa() throws Exception {
    +    final String query = "select inet_ntoa( 3232235521 ) as inet from (values(1))";
    +    testBuilder().sqlQuery(query).ordered().baselineColumns("inet").baselineValues("192.168.0.1").go();
    +  }
    +
    +  @Test
    +  public void testInNetwork() throws Exception {
    +    final String query = "select in_network( '192.168.0.1', '192.168.0.0/28' ) as in_net FROM (values(1))";
    --- End diff --
    
    What about negative test case?


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143948135
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    +        result = 1;
    +      }
    +      else{
    +        result = 0;
    +      }
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getAddressCount",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getAddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      out.value = utils.getInfo().getAddressCount();
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function returns the broadcast address of a given CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getBroadcastAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getBroadcastAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getBroadcastAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the netmask of the input CIDR block.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "getNetmask",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getNetmaskFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getNetmask();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically lowest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getLowAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getLowAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getLowAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically highest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getHighAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getHighddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getHighAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function encodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "urlencode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urlencodeFunction implements DrillSimpleFunc {
    --- End diff --
    
    `UrlEncodeFunction`


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by cgivre <gi...@git.apache.org>.
Github user cgivre commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r144036847
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    +        result = 1;
    +      }
    +      else{
    +        result = 0;
    +      }
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getAddressCount",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getAddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      out.value = utils.getInfo().getAddressCount();
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function returns the broadcast address of a given CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getBroadcastAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getBroadcastAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getBroadcastAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the netmask of the input CIDR block.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "getNetmask",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getNetmaskFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getNetmask();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically lowest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getLowAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getLowAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getLowAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically highest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getHighAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getHighddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getHighAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function encodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "urlencode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urlencodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLEncoder.encode(url, "UTF-8");
    +      }catch (Exception e){
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function decodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "urldecode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urldecodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLDecoder.decode(url, "UTF-8");
    +      }catch (Exception e){
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +
    +  /**
    +   * This function converts a BigInt IPv4 into dotted decimal notation.  The opposite of inet_aton.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "inet_ntoa",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InetNtoaFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    BigIntHolder in1;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +      StringBuilder result = new StringBuilder(15);
    +
    +      long inputInt = in1.value;
    +
    +      for (int i = 0; i < 4; i++) {
    +
    +        result.insert(0,Long.toString(inputInt & 0xff));
    +
    +        if (i < 3) {
    +          result.insert(0,'.');
    +        }
    +
    +        inputInt = inputInt >> 8;
    +      }
    +
    +      String outputValue = result.toString();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +    }
    +
    +
    +  }
    +
    +  /**
    +   * This function returns true if a given IPv4 address is private, false if not.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "is_private_ip",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class IsPrivateIP implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputTextA;
    +
    +    @Output BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputTextA.start, inputTextA.end, inputTextA.buffer);
    +
    +      String[] ipAddressInArray = ipString.split("\\.");
    +
    +      int result = 0;
    +
    +      int[] octets = new int[3];
    +
    +      for( int i = 0; i < 3; i++ ){
    +        octets[i] = Integer.parseInt( ipAddressInArray[i] );
    +        if( octets[i] > 255 || octets[i] < 0 ) {
    +          result = 0;
    +        }
    +      }
    +
    +      if( octets[0] == 192 && octets[1] == 168 ) {
    +        result = 1;
    +      }
    +      else if (octets[0] == 172 && octets [1] >= 16 && octets[1] <= 31 ){
    +        result = 1;
    +      }
    +      else if( octets[0] == 10 ) {
    +        result = 1;
    +      }
    +      else {
    +        result = 0;
    +      }
    +
    +      out.value = result;
    +    }
    +  }
    +  /**
    +   *  This function converts an IPv4 address into a BigInt.  Useful for sorting IPs, or looking for IP ranges.
    +   *  IE:
    +   *  SELECT *
    +   *  FROM <data>
    +   *  ORDER BY inet_aton( ip ) ASC
    +   */
    +  @FunctionTemplate(
    +    name = "inet_aton",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InetAtonFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputTextA;
    +
    +    @Output BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputTextA.start, inputTextA.end, inputTextA.buffer);
    +      if( ipString == null || ipString.isEmpty() || ipString.length() == 0 ){
    +        out.value = 0;
    +      } else {
    +        String[] ipAddressInArray = ipString.split("\\.");
    +
    +        long result = 0;
    +        for (int i = 0; i < ipAddressInArray.length; i++) {
    +          int power = 3 - i;
    +          int ip = Integer.parseInt(ipAddressInArray[i]);
    +          result += ip * Math.pow(256, power);
    +        }
    +
    +        out.value = result;
    +      }
    +    }
    +  }
    +
    +  /**
    +   * Returns true if the input string is a valid IP address
    +   */
    +  @FunctionTemplate(
    +    name = "is_valid_IP",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class IsValidIPFunction implements DrillSimpleFunc {
    --- End diff --
    
    I'm not really sure why it's banned.  I'd love to exclude it, but I couldn't seem to get Drill to build when I tried putting that exclusion in the java-exec pom file.  Any assistance would be greatly appreciated. 


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143945315
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    --- End diff --
    
    There is not need to define `utils` as `Workspace`, it can be defined inside of class.


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143945471
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    --- End diff --
    
    Please mind code style: `if (utils.getInfo().isInRange( ipString)) {`


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143946776
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    +        result = 1;
    +      }
    +      else{
    +        result = 0;
    +      }
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getAddressCount",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getAddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      out.value = utils.getInfo().getAddressCount();
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function returns the broadcast address of a given CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getBroadcastAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getBroadcastAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getBroadcastAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the netmask of the input CIDR block.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "getNetmask",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getNetmaskFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getNetmask();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically lowest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getLowAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getLowAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getLowAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically highest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getHighAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getHighddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getHighAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function encodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "urlencode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urlencodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLEncoder.encode(url, "UTF-8");
    +      }catch (Exception e){
    --- End diff --
    
    `} catch (Exception e) {` (please mind to https://drill.apache.org/docs/apache-drill-contribution-guidelines/)


---

Re: [GitHub] drill issue #971: Drill-5834 Add Networking Functions

Posted by Charles Givre <cg...@gmail.com>.
I *think* I just did this correctly, but let me know if I didn’t.
—C


> On Oct 30, 2017, at 12:54, paul-rogers <gi...@git.apache.org> wrote:
> 
> Github user paul-rogers commented on the issue:
> 
>    https://github.com/apache/drill/pull/971
> 
>    Please squash commits.
> 
> 
> ---


[GitHub] drill issue #971: Drill-5834 Add Networking Functions

Posted by paul-rogers <gi...@git.apache.org>.
Github user paul-rogers commented on the issue:

    https://github.com/apache/drill/pull/971
  
    Please squash commits.


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143949930
  
    --- Diff: exec/java-exec/src/test/java/org/apache/drill/exec/fn/impl/TestNetworkFunctions.java ---
    @@ -0,0 +1,93 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.fn.impl;
    +
    +import org.apache.drill.BaseTestQuery;
    +import org.junit.Test;
    +
    +public class TestNetworkFunctions extends BaseTestQuery {
    +
    +  @Test
    +  public void testInetAton() throws Exception {
    +    final String query = "select inet_aton( '192.168.0.1') as inet from (values(1))";
    +    testBuilder().sqlQuery(query).ordered().baselineColumns("inet").baselineValues( Long.parseLong("3232235521") ).go();
    --- End diff --
    
    Please remove spaces...


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143948382
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    +        result = 1;
    +      }
    +      else{
    +        result = 0;
    +      }
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getAddressCount",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getAddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      out.value = utils.getInfo().getAddressCount();
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function returns the broadcast address of a given CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getBroadcastAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getBroadcastAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getBroadcastAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the netmask of the input CIDR block.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "getNetmask",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getNetmaskFunction implements DrillSimpleFunc {
    --- End diff --
    
    `NetmaskFunction`


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143955360
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    +        result = 1;
    +      }
    +      else{
    +        result = 0;
    +      }
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getAddressCount",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getAddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      out.value = utils.getInfo().getAddressCount();
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function returns the broadcast address of a given CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getBroadcastAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getBroadcastAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getBroadcastAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the netmask of the input CIDR block.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "getNetmask",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getNetmaskFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getNetmask();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically lowest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getLowAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getLowAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getLowAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically highest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getHighAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getHighddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getHighAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function encodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "urlencode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urlencodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLEncoder.encode(url, "UTF-8");
    +      }catch (Exception e){
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function decodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "urldecode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urldecodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLDecoder.decode(url, "UTF-8");
    +      }catch (Exception e){
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +
    +  /**
    +   * This function converts a BigInt IPv4 into dotted decimal notation.  The opposite of inet_aton.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "inet_ntoa",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InetNtoaFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    BigIntHolder in1;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    --- End diff --
    
    For `ntoa` and `aton` may we use guava lib [1, 2]?
    
    [1] https://google.github.io/guava/releases/19.0/api/docs/com/google/common/net/InetAddresses.html
    [2] https://stackoverflow.com/questions/2241229/going-from-127-0-0-1-to-2130706433-and-back-again


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143948412
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    +        result = 1;
    +      }
    +      else{
    +        result = 0;
    +      }
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getAddressCount",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getAddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      out.value = utils.getInfo().getAddressCount();
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function returns the broadcast address of a given CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getBroadcastAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getBroadcastAddressFunction implements DrillSimpleFunc {
    --- End diff --
    
    `BroadcastAddressFunction`


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143947620
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    +        result = 1;
    +      }
    +      else{
    +        result = 0;
    +      }
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getAddressCount",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getAddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      out.value = utils.getInfo().getAddressCount();
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function returns the broadcast address of a given CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getBroadcastAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getBroadcastAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getBroadcastAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the netmask of the input CIDR block.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "getNetmask",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getNetmaskFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getNetmask();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically lowest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getLowAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getLowAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getLowAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically highest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getHighAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getHighddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getHighAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function encodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "urlencode",
    --- End diff --
    
    `ulr_encode` (like SQL functions naming)?


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143945974
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    +        result = 1;
    +      }
    +      else{
    +        result = 0;
    +      }
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getAddressCount",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getAddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    --- End diff --
    
    Can be defined inside of method instead of as workspace variable.


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r145078505
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    +        result = 1;
    +      }
    +      else{
    +        result = 0;
    +      }
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getAddressCount",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getAddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      out.value = utils.getInfo().getAddressCount();
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function returns the broadcast address of a given CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getBroadcastAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getBroadcastAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getBroadcastAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the netmask of the input CIDR block.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "getNetmask",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getNetmaskFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getNetmask();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically lowest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getLowAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getLowAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getLowAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically highest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getHighAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getHighddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getHighAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function encodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "urlencode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urlencodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLEncoder.encode(url, "UTF-8");
    +      }catch (Exception e){
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function decodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "urldecode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urldecodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLDecoder.decode(url, "UTF-8");
    +      }catch (Exception e){
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +
    +  /**
    +   * This function converts a BigInt IPv4 into dotted decimal notation.  The opposite of inet_aton.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "inet_ntoa",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InetNtoaFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    BigIntHolder in1;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    --- End diff --
    
    Well, it depends but I would call it's Java way to reuse already defined logic from different libraries rather then repeating it. Of course if it's worse adding new lib. In our case logic for `ntoa` , `aton` and `private_ip` does not seem trivial. Did you write it yourself or copied from somewhere else? If first case according to which references algorithm was written? Were all cases considered, tested etc? If second, is source is well-trusted? Does source owner mind his code will be reused etc? Maybe it's better to use well-known lib instead which mostly likely has all these functions tested and followed common standards?


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143953962
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    +        result = 1;
    +      }
    +      else{
    +        result = 0;
    +      }
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getAddressCount",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getAddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      out.value = utils.getInfo().getAddressCount();
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function returns the broadcast address of a given CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getBroadcastAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getBroadcastAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getBroadcastAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the netmask of the input CIDR block.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "getNetmask",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getNetmaskFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getNetmask();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically lowest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getLowAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getLowAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getLowAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically highest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getHighAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getHighddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getHighAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function encodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "urlencode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urlencodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLEncoder.encode(url, "UTF-8");
    +      }catch (Exception e){
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function decodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "urldecode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urldecodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLDecoder.decode(url, "UTF-8");
    +      }catch (Exception e){
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +
    +  /**
    +   * This function converts a BigInt IPv4 into dotted decimal notation.  The opposite of inet_aton.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "inet_ntoa",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InetNtoaFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    BigIntHolder in1;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +      StringBuilder result = new StringBuilder(15);
    +
    +      long inputInt = in1.value;
    +
    +      for (int i = 0; i < 4; i++) {
    +
    +        result.insert(0,Long.toString(inputInt & 0xff));
    +
    +        if (i < 3) {
    +          result.insert(0,'.');
    +        }
    +
    +        inputInt = inputInt >> 8;
    +      }
    +
    +      String outputValue = result.toString();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +    }
    +
    +
    +  }
    +
    +  /**
    +   * This function returns true if a given IPv4 address is private, false if not.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "is_private_ip",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class IsPrivateIP implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputTextA;
    +
    +    @Output BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    --- End diff --
    
    May we use `InetAddress.html#isSiteLocalAddress` instead [1, 2]?
    
    [1] http://docs.oracle.com/javase/1.5.0/docs/api/java/net/InetAddress.html#isSiteLocalAddress%28%29
    [2] https://stackoverflow.com/questions/9729378/check-whether-the-ipaddress-is-in-private-range/9729432


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143948906
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    +        result = 1;
    +      }
    +      else{
    +        result = 0;
    +      }
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getAddressCount",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getAddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      out.value = utils.getInfo().getAddressCount();
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function returns the broadcast address of a given CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getBroadcastAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getBroadcastAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getBroadcastAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the netmask of the input CIDR block.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "getNetmask",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getNetmaskFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getNetmask();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically lowest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getLowAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getLowAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getLowAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically highest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getHighAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getHighddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getHighAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function encodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "urlencode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urlencodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLEncoder.encode(url, "UTF-8");
    +      }catch (Exception e){
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +  /**
    +   * This function decodes URL strings.
    +   */
    +  @FunctionTemplate(
    +    name = "urldecode",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class urldecodeFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputString;
    +
    +    @Output
    +    VarCharHolder outputString;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String url = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputString.start, inputString.end, inputString.buffer);
    +
    +      String outputValue = "";
    +      try {
    +        outputValue = java.net.URLDecoder.decode(url, "UTF-8");
    +      }catch (Exception e){
    +
    +      }
    +      outputString.buffer = buffer;
    +      outputString.start = 0;
    +      outputString.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +  }
    +
    +
    +  /**
    +   * This function converts a BigInt IPv4 into dotted decimal notation.  The opposite of inet_aton.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "inet_ntoa",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InetNtoaFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    BigIntHolder in1;
    --- End diff --
    
    `in`


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143950000
  
    --- Diff: exec/java-exec/src/test/java/org/apache/drill/exec/fn/impl/TestNetworkFunctions.java ---
    @@ -0,0 +1,93 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.fn.impl;
    +
    +import org.apache.drill.BaseTestQuery;
    +import org.junit.Test;
    +
    +public class TestNetworkFunctions extends BaseTestQuery {
    +
    +  @Test
    +  public void testInetAton() throws Exception {
    +    final String query = "select inet_aton( '192.168.0.1') as inet from (values(1))";
    +    testBuilder().sqlQuery(query).ordered().baselineColumns("inet").baselineValues( Long.parseLong("3232235521") ).go();
    +  }
    +
    +  @Test
    +  public void testInetNtoa() throws Exception {
    +    final String query = "select inet_ntoa( 3232235521 ) as inet from (values(1))";
    --- End diff --
    
    spaces


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143948213
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    +        result = 1;
    +      }
    +      else{
    +        result = 0;
    +      }
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getAddressCount",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getAddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      out.value = utils.getInfo().getAddressCount();
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function returns the broadcast address of a given CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getBroadcastAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getBroadcastAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getBroadcastAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the netmask of the input CIDR block.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "getNetmask",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getNetmaskFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getNetmask();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically lowest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getLowAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getLowAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getLowAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically highest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getHighAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getHighddressFunction implements DrillSimpleFunc {
    --- End diff --
    
    `HighAddressFunction`


---

[GitHub] drill pull request #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/971#discussion_r143948324
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/NetworkFunctions.java ---
    @@ -0,0 +1,668 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.drill.exec.expr.fn.impl;
    +
    +import io.netty.buffer.DrillBuf;
    +import org.apache.commons.net.util.SubnetUtils;
    +import org.apache.drill.exec.expr.DrillSimpleFunc;
    +import org.apache.drill.exec.expr.annotations.FunctionTemplate;
    +import org.apache.drill.exec.expr.annotations.Output;
    +import org.apache.drill.exec.expr.annotations.Param;
    +import org.apache.drill.exec.expr.annotations.Workspace;
    +import org.apache.drill.exec.expr.holders.BigIntHolder;
    +import org.apache.drill.exec.expr.holders.BitHolder;
    +import org.apache.drill.exec.expr.holders.VarCharHolder;
    +
    +import javax.inject.Inject;
    +
    +public class NetworkFunctions{
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(NetworkFunctions.class);
    +
    +  private NetworkFunctions() {}
    +
    +  /**
    +   * This function takes two arguments, an input IPv4 and a CIDR, and returns true if the IP is in the given CIDR block
    +   *
    +   */
    +  @FunctionTemplate(
    +    name = "in_network",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class InNetworkFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputIP;
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BitHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +
    +    public void eval() {
    +
    +      String ipString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputIP.start, inputIP.end, inputIP.buffer);
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +
    +      int result = 0;
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      if( utils.getInfo().isInRange( ipString ) ){
    +        result = 1;
    +      }
    +      else{
    +        result = 0;
    +      }
    +      out.value = result;
    +    }
    +  }
    +
    +
    +  /**
    +   * This function retunrs the number of IP addresses in the input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getAddressCount",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getAddressCountFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    BigIntHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      out.value = utils.getInfo().getAddressCount();
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function returns the broadcast address of a given CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getBroadcastAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getBroadcastAddressFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getBroadcastAddress();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the netmask of the input CIDR block.
    +   */
    +
    +  @FunctionTemplate(
    +    name = "getNetmask",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getNetmaskFunction implements DrillSimpleFunc {
    +
    +    @Param
    +    VarCharHolder inputCIDR;
    +
    +    @Output
    +    VarCharHolder out;
    +
    +    @Inject
    +    DrillBuf buffer;
    +
    +    @Workspace
    +    SubnetUtils utils;
    +
    +    public void setup() {
    +    }
    +
    +    public void eval() {
    +
    +      String cidrString = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(inputCIDR.start, inputCIDR.end, inputCIDR.buffer);
    +      utils = new org.apache.commons.net.util.SubnetUtils(cidrString);
    +
    +      String outputValue = utils.getInfo().getNetmask();
    +
    +      out.buffer = buffer;
    +      out.start = 0;
    +      out.end = outputValue.getBytes().length;
    +      buffer.setBytes(0, outputValue.getBytes());
    +
    +    }
    +
    +  }
    +
    +  /**
    +   * This function gets the numerically lowest IP address in an input CIDR block.
    +   */
    +  @FunctionTemplate(
    +    name = "getLowAddress",
    +    scope = FunctionTemplate.FunctionScope.SIMPLE,
    +    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
    +  )
    +  public static class getLowAddressFunction implements DrillSimpleFunc {
    --- End diff --
    
    `LowAddressFunction ` (please follow Java class naming standards)


---

[GitHub] drill issue #971: Drill-5834 Add Networking Functions

Posted by arina-ielchiieva <gi...@git.apache.org>.
Github user arina-ielchiieva commented on the issue:

    https://github.com/apache/drill/pull/971
  
    @cgivre under squash commits @paul-rogers meant that you should end up with one commit (now you have 13). You might consider using rebase and then do force push in your branch to overwrite this 13 commits. Also please make sure your commit message has proper naming: DRILL-XXX: <message>. In your case, `DRILL-5834: Add networking functions`.


---