tajo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jyp...@apache.org
Subject tajo git commit: TAJO-2156: Create GeoIP functions taking various types instead of INET4 type.
Date Wed, 15 Jun 2016 09:31:17 GMT
Repository: tajo
Updated Branches:
  refs/heads/master 83e8e343a -> 740720a2f


TAJO-2156: Create GeoIP functions taking various types instead of INET4 type.


Project: http://git-wip-us.apache.org/repos/asf/tajo/repo
Commit: http://git-wip-us.apache.org/repos/asf/tajo/commit/740720a2
Tree: http://git-wip-us.apache.org/repos/asf/tajo/tree/740720a2
Diff: http://git-wip-us.apache.org/repos/asf/tajo/diff/740720a2

Branch: refs/heads/master
Commit: 740720a2f5d170a0aacf867e1bb9d7242c54043b
Parents: 83e8e34
Author: Jongyoung Park <jypark@apache.org>
Authored: Wed Jun 15 18:25:53 2016 +0900
Committer: Jongyoung Park <jypark@apache.org>
Committed: Wed Jun 15 18:25:53 2016 +0900

----------------------------------------------------------------------
 CHANGES                                         |  2 +
 .../org/apache/tajo/util/TestIPconvertUtil.java | 48 ++++++++++++
 .../tajo/engine/function/geoip/BlobToIPstr.java | 57 ++++++++++++++
 .../engine/function/geoip/GeoIPCountryBlob.java | 55 ++++++++++++++
 .../engine/function/geoip/GeoIPCountryInt.java  | 53 +++++++++++++
 .../function/geoip/GeoIPInCountryBlob.java      | 61 +++++++++++++++
 .../function/geoip/GeoIPInCountryInt.java       | 61 +++++++++++++++
 .../tajo/engine/function/geoip/IPstrToBlob.java | 57 ++++++++++++++
 .../tajo/engine/function/geoip/IPstrToInt.java  | 57 ++++++++++++++
 .../tajo/engine/function/geoip/IntToIPstr.java  | 57 ++++++++++++++
 .../java/org/apache/tajo/util/GeoIPUtil.java    | 28 +++++++
 .../org/apache/tajo/util/IPconvertUtil.java     | 78 ++++++++++++++++++++
 12 files changed, 614 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tajo/blob/740720a2/CHANGES
----------------------------------------------------------------------
diff --git a/CHANGES b/CHANGES
index 155aad0..7c27422 100644
--- a/CHANGES
+++ b/CHANGES
@@ -383,6 +383,8 @@ Release 0.12.0 - unreleased
 
   SUB TASKS
 
+    TAJO-2156: Create GeoIP functions taking various types instead of INET4 type. (jongyoung)
+
     TAJO-2130: Remove inet4 data type.
     (Contributed by Jongyoung Park. Committed by jihoon)
 

http://git-wip-us.apache.org/repos/asf/tajo/blob/740720a2/tajo-core-tests/src/test/java/org/apache/tajo/util/TestIPconvertUtil.java
----------------------------------------------------------------------
diff --git a/tajo-core-tests/src/test/java/org/apache/tajo/util/TestIPconvertUtil.java b/tajo-core-tests/src/test/java/org/apache/tajo/util/TestIPconvertUtil.java
new file mode 100644
index 0000000..886beb7
--- /dev/null
+++ b/tajo-core-tests/src/test/java/org/apache/tajo/util/TestIPconvertUtil.java
@@ -0,0 +1,48 @@
+/*
+ *  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.tajo.util;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertArrayEquals;
+
+public class TestIPconvertUtil {
+  @Test
+  public void testIntConvertMethods() {
+    assertEquals(16909060, IPconvertUtil.ipstr2int("1.2.3.4"));
+    assertEquals(-1062731699, IPconvertUtil.ipstr2int("192.168.0.77"));
+
+    assertEquals("1.2.3.4", IPconvertUtil.int2ipstr(16909060));
+    assertEquals("192.168.0.77", IPconvertUtil.int2ipstr(-1062731699));
+  }
+
+  @Test
+  public void testBytesConvertMethods() {
+    byte [] addr = new byte[4];
+
+    addr[0] = 1; addr[1] = 2; addr[2] = 3; addr[3] = 4;
+    assertArrayEquals(addr, IPconvertUtil.ipstr2bytes("1.2.3.4"));
+    assertEquals("1.2.3.4", IPconvertUtil.bytes2ipstr(addr));
+
+    addr[0] = (byte)192; addr[1] = (byte)168; addr[2] = 0; addr[3] = 77;
+    assertArrayEquals(addr, IPconvertUtil.ipstr2bytes("192.168.0.77"));
+    assertEquals("192.168.0.77", IPconvertUtil.bytes2ipstr(addr));
+  }
+}

http://git-wip-us.apache.org/repos/asf/tajo/blob/740720a2/tajo-core/src/main/java/org/apache/tajo/engine/function/geoip/BlobToIPstr.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/geoip/BlobToIPstr.java
b/tajo-core/src/main/java/org/apache/tajo/engine/function/geoip/BlobToIPstr.java
new file mode 100644
index 0000000..3a3f6e5
--- /dev/null
+++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/geoip/BlobToIPstr.java
@@ -0,0 +1,57 @@
+/*
+ *  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.tajo.engine.function.geoip;
+
+import org.apache.tajo.catalog.Column;
+import org.apache.tajo.datum.Datum;
+import org.apache.tajo.datum.NullDatum;
+import org.apache.tajo.datum.TextDatum;
+import org.apache.tajo.engine.function.annotation.Description;
+import org.apache.tajo.engine.function.annotation.ParamTypes;
+import org.apache.tajo.plan.function.GeneralFunction;
+import org.apache.tajo.storage.Tuple;
+import org.apache.tajo.util.IPconvertUtil;
+
+import static org.apache.tajo.common.TajoDataTypes.Type.*;
+
+@Description(
+    functionName = "bin_to_ipstr",
+    description = "Convert a binary type value to ipv4 string",
+    example = "> SELECT bin_to_ipstr(ipstr_to_blob('1.2.3.4'));\n"
+        + "1.2.3.4",
+    returnType = TEXT,
+    paramTypes = {@ParamTypes(paramTypes = {BLOB}),
+                  @ParamTypes(paramTypes = {BINARY})}
+)
+public class BlobToIPstr extends GeneralFunction {
+  public BlobToIPstr() {
+    super(new Column[] { new Column("ip_binvalue", BLOB)});
+  }
+
+  @Override
+  public Datum eval(Tuple params) {
+    if (params.isBlankOrNull(0)) {
+      return NullDatum.get();
+    }
+
+    byte [] byteAddr = params.getBytes(0);
+
+    return new TextDatum(IPconvertUtil.bytes2ipstr(byteAddr));
+  }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/740720a2/tajo-core/src/main/java/org/apache/tajo/engine/function/geoip/GeoIPCountryBlob.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/geoip/GeoIPCountryBlob.java
b/tajo-core/src/main/java/org/apache/tajo/engine/function/geoip/GeoIPCountryBlob.java
new file mode 100644
index 0000000..73706d3
--- /dev/null
+++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/geoip/GeoIPCountryBlob.java
@@ -0,0 +1,55 @@
+/*
+ *  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.tajo.engine.function.geoip;
+
+import org.apache.tajo.catalog.Column;
+import org.apache.tajo.common.TajoDataTypes.Type;
+import org.apache.tajo.datum.Datum;
+import org.apache.tajo.datum.NullDatum;
+import org.apache.tajo.datum.TextDatum;
+import org.apache.tajo.engine.function.annotation.Description;
+import org.apache.tajo.engine.function.annotation.ParamTypes;
+import org.apache.tajo.plan.function.GeneralFunction;
+import org.apache.tajo.storage.Tuple;
+import org.apache.tajo.util.GeoIPUtil;
+
+@Description(
+    functionName = "geoip_country_code",
+    description = "Convert an ip address to a geoip country code.",
+    example = "> SELECT geoip_country_code(134744072);\n"
+        + "US",
+    returnType = Type.TEXT,
+    paramTypes = {@ParamTypes(paramTypes = {Type.BINARY}),
+        @ParamTypes(paramTypes = {Type.BLOB})}
+)
+
+public class GeoIPCountryBlob extends GeneralFunction {
+  public GeoIPCountryBlob() {
+    super(new Column[] {new Column("ip_address", Type.BLOB)});
+  }
+
+  @Override
+  public Datum eval(Tuple params) {
+    if (params.isBlankOrNull(0)) {
+      return NullDatum.get();
+    }
+
+    return new TextDatum(GeoIPUtil.getCountryCode(params.getBytes(0)));
+  }
+}

http://git-wip-us.apache.org/repos/asf/tajo/blob/740720a2/tajo-core/src/main/java/org/apache/tajo/engine/function/geoip/GeoIPCountryInt.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/geoip/GeoIPCountryInt.java
b/tajo-core/src/main/java/org/apache/tajo/engine/function/geoip/GeoIPCountryInt.java
new file mode 100644
index 0000000..7dc6a13
--- /dev/null
+++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/geoip/GeoIPCountryInt.java
@@ -0,0 +1,53 @@
+/*
+ *  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.tajo.engine.function.geoip;
+
+import org.apache.tajo.catalog.Column;
+import org.apache.tajo.common.TajoDataTypes.Type;
+import org.apache.tajo.datum.*;
+import org.apache.tajo.engine.function.annotation.Description;
+import org.apache.tajo.engine.function.annotation.ParamTypes;
+import org.apache.tajo.plan.function.GeneralFunction;
+import org.apache.tajo.storage.Tuple;
+import org.apache.tajo.util.GeoIPUtil;
+
+@Description(
+    functionName = "geoip_country_code",
+    description = "Convert an ipv4 address to a geoip country code.",
+    example = "> SELECT geoip_country_code(134744072);\n"
+        + "US",
+    returnType = Type.TEXT,
+    paramTypes = {@ParamTypes(paramTypes = {Type.INT4}),
+        @ParamTypes(paramTypes = {Type.INT8})}
+)
+
+public class GeoIPCountryInt extends GeneralFunction {
+  public GeoIPCountryInt() {
+    super(new Column[] {new Column("ipv4_address", Type.INT8)});
+  }
+
+  @Override
+  public Datum eval(Tuple params) {
+    if (params.isBlankOrNull(0)) {
+      return NullDatum.get();
+    }
+
+    return new TextDatum(GeoIPUtil.getCountryCode(params.getInt4(0)));
+  }
+}

http://git-wip-us.apache.org/repos/asf/tajo/blob/740720a2/tajo-core/src/main/java/org/apache/tajo/engine/function/geoip/GeoIPInCountryBlob.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/geoip/GeoIPInCountryBlob.java
b/tajo-core/src/main/java/org/apache/tajo/engine/function/geoip/GeoIPInCountryBlob.java
new file mode 100644
index 0000000..35a197b
--- /dev/null
+++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/geoip/GeoIPInCountryBlob.java
@@ -0,0 +1,61 @@
+/*
+ *  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.tajo.engine.function.geoip;
+
+import org.apache.tajo.catalog.Column;
+import org.apache.tajo.common.TajoDataTypes.Type;
+import org.apache.tajo.datum.Datum;
+import org.apache.tajo.datum.DatumFactory;
+import org.apache.tajo.datum.NullDatum;
+import org.apache.tajo.engine.function.annotation.Description;
+import org.apache.tajo.engine.function.annotation.ParamTypes;
+import org.apache.tajo.plan.function.GeneralFunction;
+import org.apache.tajo.storage.Tuple;
+import org.apache.tajo.util.GeoIPUtil;
+
+@Description(
+    functionName = "geoip_in_country",
+    description = "If the given country code is same with the country code of the given address,
it returns true. "
+        + "Otherwise, returns false",
+    example = "geoip_in_country(8.8.8.8, 'US')"
+        + "true",
+    returnType = Type.BOOLEAN,
+    paramTypes = {@ParamTypes(paramTypes = {Type.BINARY, Type.TEXT}),
+        @ParamTypes(paramTypes = {Type.BLOB, Type.TEXT})
+    }
+)
+public class GeoIPInCountryBlob extends GeneralFunction {
+  public GeoIPInCountryBlob() {
+    super(new Column[] {new Column("ipv4_address", Type.BLOB),
+        new Column("country_code", Type.TEXT)});
+  }
+
+  @Override
+  public Datum eval(Tuple params) {
+    if (params.isBlankOrNull(0) || params.isBlankOrNull(1)) {
+      return NullDatum.get();
+    }
+
+    byte [] addr = params.getBytes(0);
+    String otherCode = params.getText(1);
+    String thisCode = GeoIPUtil.getCountryCode(addr);
+
+    return DatumFactory.createBool(thisCode.equals(otherCode));
+  }
+}

http://git-wip-us.apache.org/repos/asf/tajo/blob/740720a2/tajo-core/src/main/java/org/apache/tajo/engine/function/geoip/GeoIPInCountryInt.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/geoip/GeoIPInCountryInt.java
b/tajo-core/src/main/java/org/apache/tajo/engine/function/geoip/GeoIPInCountryInt.java
new file mode 100644
index 0000000..a890d42
--- /dev/null
+++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/geoip/GeoIPInCountryInt.java
@@ -0,0 +1,61 @@
+/*
+ *  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.tajo.engine.function.geoip;
+
+import org.apache.tajo.catalog.Column;
+import org.apache.tajo.common.TajoDataTypes.Type;
+import org.apache.tajo.datum.Datum;
+import org.apache.tajo.datum.DatumFactory;
+import org.apache.tajo.datum.NullDatum;
+import org.apache.tajo.engine.function.annotation.Description;
+import org.apache.tajo.engine.function.annotation.ParamTypes;
+import org.apache.tajo.plan.function.GeneralFunction;
+import org.apache.tajo.storage.Tuple;
+import org.apache.tajo.util.GeoIPUtil;
+
+@Description(
+    functionName = "geoip_in_country",
+    description = "If the given country code is same with the country code of the given address,
it returns true. "
+        + "Otherwise, returns false",
+    example = "geoip_in_country(8.8.8.8, 'US')"
+        + "true",
+    returnType = Type.BOOLEAN,
+    paramTypes = {@ParamTypes(paramTypes = {Type.INT4, Type.TEXT}),
+        @ParamTypes(paramTypes = {Type.INT8, Type.TEXT})
+    }
+)
+public class GeoIPInCountryInt extends GeneralFunction {
+  public GeoIPInCountryInt() {
+    super(new Column[] {new Column("ipv4_address", Type.INT8),
+        new Column("country_code", Type.TEXT)});
+  }
+
+  @Override
+  public Datum eval(Tuple params) {
+    if (params.isBlankOrNull(0) || params.isBlankOrNull(1)) {
+      return NullDatum.get();
+    }
+
+    int addr = params.getInt4(0);
+    String otherCode = params.getText(1);
+    String thisCode = GeoIPUtil.getCountryCode(addr);
+
+    return DatumFactory.createBool(thisCode.equals(otherCode));
+  }
+}

http://git-wip-us.apache.org/repos/asf/tajo/blob/740720a2/tajo-core/src/main/java/org/apache/tajo/engine/function/geoip/IPstrToBlob.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/geoip/IPstrToBlob.java
b/tajo-core/src/main/java/org/apache/tajo/engine/function/geoip/IPstrToBlob.java
new file mode 100644
index 0000000..ae13acb
--- /dev/null
+++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/geoip/IPstrToBlob.java
@@ -0,0 +1,57 @@
+/*
+ *  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.tajo.engine.function.geoip;
+
+import org.apache.tajo.catalog.Column;
+import org.apache.tajo.datum.BlobDatum;
+import org.apache.tajo.datum.Datum;
+import org.apache.tajo.datum.NullDatum;
+import org.apache.tajo.engine.function.annotation.Description;
+import org.apache.tajo.engine.function.annotation.ParamTypes;
+import org.apache.tajo.plan.function.GeneralFunction;
+import org.apache.tajo.storage.Tuple;
+import org.apache.tajo.util.IPconvertUtil;
+
+import static org.apache.tajo.common.TajoDataTypes.Type.BINARY;
+import static org.apache.tajo.common.TajoDataTypes.Type.TEXT;
+
+@Description(
+    functionName = "ipstr_to_blob",
+    description = "Convert an ipv4 address string to INT4 type",
+    example = "> SELECT ipstr_to_int('1.2.3.4');\n"
+        + "16909060",
+    returnType = BINARY,
+    paramTypes = {@ParamTypes(paramTypes = {TEXT})}
+)
+public class IPstrToBlob extends GeneralFunction {
+  public IPstrToBlob() {
+    super(new Column[] { new Column("ipstring", TEXT)});
+  }
+
+  @Override
+  public Datum eval(Tuple params) {
+    if (params.isBlankOrNull(0)) {
+      return NullDatum.get();
+    }
+
+    String ipstr = params.getText(0);
+
+    return new BlobDatum(IPconvertUtil.ipstr2bytes(ipstr));
+  }
+}

http://git-wip-us.apache.org/repos/asf/tajo/blob/740720a2/tajo-core/src/main/java/org/apache/tajo/engine/function/geoip/IPstrToInt.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/geoip/IPstrToInt.java
b/tajo-core/src/main/java/org/apache/tajo/engine/function/geoip/IPstrToInt.java
new file mode 100644
index 0000000..43b4ab0
--- /dev/null
+++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/geoip/IPstrToInt.java
@@ -0,0 +1,57 @@
+/*
+ *  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.tajo.engine.function.geoip;
+
+import org.apache.tajo.catalog.Column;
+import org.apache.tajo.datum.Datum;
+import org.apache.tajo.datum.Int4Datum;
+import org.apache.tajo.datum.NullDatum;
+import org.apache.tajo.engine.function.annotation.Description;
+import org.apache.tajo.engine.function.annotation.ParamTypes;
+import org.apache.tajo.plan.function.GeneralFunction;
+import org.apache.tajo.storage.Tuple;
+import org.apache.tajo.util.IPconvertUtil;
+
+import static org.apache.tajo.common.TajoDataTypes.Type.INT4;
+import static org.apache.tajo.common.TajoDataTypes.Type.TEXT;
+
+@Description(
+    functionName = "ipstr_to_int",
+    description = "Convert an ipv4 address string to INT4 type",
+    example = "> SELECT ipstr_to_int('1.2.3.4');\n"
+        + "16909060",
+    returnType = INT4,
+    paramTypes = {@ParamTypes(paramTypes = {TEXT})}
+)
+public class IPstrToInt extends GeneralFunction {
+  public IPstrToInt() {
+    super(new Column[] { new Column("ipstring", TEXT)});
+  }
+
+  @Override
+  public Datum eval(Tuple params) {
+    if (params.isBlankOrNull(0)) {
+      return NullDatum.get();
+    }
+
+    String ipstr = params.getText(0);
+
+    return new Int4Datum(IPconvertUtil.ipstr2int(ipstr));
+  }
+}

http://git-wip-us.apache.org/repos/asf/tajo/blob/740720a2/tajo-core/src/main/java/org/apache/tajo/engine/function/geoip/IntToIPstr.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/geoip/IntToIPstr.java
b/tajo-core/src/main/java/org/apache/tajo/engine/function/geoip/IntToIPstr.java
new file mode 100644
index 0000000..6276b3e
--- /dev/null
+++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/geoip/IntToIPstr.java
@@ -0,0 +1,57 @@
+/*
+ *  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.tajo.engine.function.geoip;
+
+import org.apache.tajo.catalog.Column;
+import org.apache.tajo.datum.Datum;
+import org.apache.tajo.datum.NullDatum;
+import org.apache.tajo.datum.TextDatum;
+import org.apache.tajo.engine.function.annotation.Description;
+import org.apache.tajo.engine.function.annotation.ParamTypes;
+import org.apache.tajo.plan.function.GeneralFunction;
+import org.apache.tajo.storage.Tuple;
+import org.apache.tajo.util.IPconvertUtil;
+
+import static org.apache.tajo.common.TajoDataTypes.Type.INT4;
+import static org.apache.tajo.common.TajoDataTypes.Type.TEXT;
+
+@Description(
+    functionName = "int_to_ipstr",
+    description = "Convert an INT4 type value to ipv4 string",
+    example = "> SELECT int_to_ipstr(16909060);\n"
+        + "1.2.3.4",
+    returnType = TEXT,
+    paramTypes = {@ParamTypes(paramTypes = {INT4})}
+)
+public class IntToIPstr extends GeneralFunction {
+  public IntToIPstr() {
+    super(new Column[] { new Column("ip_intvalue", INT4)});
+  }
+
+  @Override
+  public Datum eval(Tuple params) {
+    if (params.isBlankOrNull(0)) {
+      return NullDatum.get();
+    }
+
+    int ipaddr = params.getInt4(0);
+
+    return new TextDatum(IPconvertUtil.int2ipstr(ipaddr));
+  }
+}

http://git-wip-us.apache.org/repos/asf/tajo/blob/740720a2/tajo-core/src/main/java/org/apache/tajo/util/GeoIPUtil.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/main/java/org/apache/tajo/util/GeoIPUtil.java b/tajo-core/src/main/java/org/apache/tajo/util/GeoIPUtil.java
index 859b37d..e9cc8d6 100644
--- a/tajo-core/src/main/java/org/apache/tajo/util/GeoIPUtil.java
+++ b/tajo-core/src/main/java/org/apache/tajo/util/GeoIPUtil.java
@@ -25,6 +25,8 @@ import org.apache.tajo.conf.TajoConf;
 import org.apache.tajo.conf.TajoConf.ConfVars;
 
 import java.io.IOException;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
 
 public class GeoIPUtil {
   private static final Log LOG = LogFactory.getLog(GeoIPUtil.class);
@@ -43,4 +45,30 @@ public class GeoIPUtil {
   public static String getCountryCode(String host) {
     return lookup.getCountry(host).getCode();
   }
+
+  /** Converts int value to country code.
+   *
+   *  @param host ip address as int type by network byte order(big endian)
+   */
+  public static String getCountryCode(int host) {
+      return lookup.getCountry(host).getCode();
+  }
+
+  /** Converts binary(byte array) to country code.
+   *  In case of IPv4, it is 4 bytes length.
+   *
+   *  @param host ip address as byte array type by network byte order(big endian)
+   */
+  public static String getCountryCode(byte [] host) {
+    InetAddress addr;
+
+    try {
+      addr = InetAddress.getByAddress(host);
+    } catch (UnknownHostException e) {
+      LOG.error("Unknown host: " + IPconvertUtil.bytes2ipstr(host));
+      return "";
+    }
+
+    return lookup.getCountry(addr).getCode();
+  }
 }

http://git-wip-us.apache.org/repos/asf/tajo/blob/740720a2/tajo-core/src/main/java/org/apache/tajo/util/IPconvertUtil.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/main/java/org/apache/tajo/util/IPconvertUtil.java b/tajo-core/src/main/java/org/apache/tajo/util/IPconvertUtil.java
new file mode 100644
index 0000000..cab9f30
--- /dev/null
+++ b/tajo-core/src/main/java/org/apache/tajo/util/IPconvertUtil.java
@@ -0,0 +1,78 @@
+/*
+ *  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.tajo.util;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import java.util.Arrays;
+
+public class IPconvertUtil {
+  private static final Log LOG = LogFactory.getLog(IPconvertUtil.class);
+
+  public static int ipstr2int(String host) {
+    String[] ips = host.split("\\.");
+
+    if (ips.length != 4) {
+      LOG.error(host + " is not valid ip string");
+      return 0;
+    }
+
+    Integer[] ipAddr = Arrays.stream(ips).map(Integer::parseInt).toArray(Integer[]::new);
+
+    int address = ipAddr[3] & 0xFF;
+    address |= ((ipAddr[2] << 8) & 0xFF00);
+    address |= ((ipAddr[1] << 16) & 0xFF0000);
+    address |= ((ipAddr[0] << 24) & 0xFF000000);
+
+    return address;
+  }
+
+  public static String int2ipstr(int addr) {
+    int[] intAddr = new int[4];
+
+    for (int i = 3; i >= 0; i--) {
+      intAddr[i] = addr & 0xFF;
+      addr >>= 8;
+    }
+
+    return String.format("%d.%d.%d.%d", intAddr[0], intAddr[1], intAddr[2], intAddr[3]);
+  }
+
+  public static byte[] ipstr2bytes(String host) {
+    String[] ips = host.split("\\.");
+
+    if (ips.length != 4) {
+      LOG.error(host + " is not valid ip string");
+      return null;
+    }
+
+    byte[] result = new byte[4];
+    for (int i = 0; i < 4; i++) {
+      result[i] = (byte) (Integer.parseInt(ips[i]) & 0xFF);
+    }
+
+    return result;
+  }
+
+  public static String bytes2ipstr(byte[] addr) {
+    return String.format("%d.%d.%d.%d", addr[0] & 0xFF, addr[1] & 0xFF, addr[2] &
0xFF, addr[3] & 0xFF);
+
+  }
+}


Mime
View raw message