Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IPAddress Presto type #10596

Closed
wants to merge 21 commits into from
1 change: 1 addition & 0 deletions velox/docs/develop/types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ HYPERLOGLOG VARBINARY
JSON VARCHAR
TIMESTAMP WITH TIME ZONE BIGINT
UUID HUGEINT
IPADDRESS HUGEINT
mohsaka marked this conversation as resolved.
Show resolved Hide resolved
======================== =====================

TIMESTAMP WITH TIME ZONE represents a time point in milliseconds precision
Expand Down
110 changes: 109 additions & 1 deletion velox/docs/functions/presto/conversion.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ are supported if the conversion of their element types are supported. In additio
supported conversions to/from JSON are listed in :doc:`json`.

.. list-table::
:widths: 25 25 25 25 25 25 25 25 25 25 25 25 25 25
:widths: 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25
:header-rows: 1

* -
Expand All @@ -42,11 +42,13 @@ supported conversions to/from JSON are listed in :doc:`json`.
- real
- double
- varchar
- varbinary
- timestamp
- timestamp with time zone
- date
- interval day to second
- decimal
- ipaddress
* - tinyint
- Y
- Y
Expand All @@ -56,11 +58,13 @@ supported conversions to/from JSON are listed in :doc:`json`.
- Y
- Y
- Y
-
-
-
-
-
- Y
-
* - smallint
- Y
- Y
Expand All @@ -70,11 +74,13 @@ supported conversions to/from JSON are listed in :doc:`json`.
- Y
- Y
- Y
-
-
-
-
-
- Y
-
* - integer
- Y
- Y
Expand All @@ -84,11 +90,13 @@ supported conversions to/from JSON are listed in :doc:`json`.
- Y
- Y
- Y
-
-
-
-
-
- Y
-
* - bigint
- Y
- Y
Expand All @@ -98,11 +106,13 @@ supported conversions to/from JSON are listed in :doc:`json`.
- Y
- Y
- Y
-
-
-
-
-
- Y
-
* - boolean
- Y
- Y
Expand All @@ -112,11 +122,13 @@ supported conversions to/from JSON are listed in :doc:`json`.
- Y
- Y
- Y
-
-
-
-
-
- Y
-
* - real
- Y
- Y
Expand All @@ -126,11 +138,13 @@ supported conversions to/from JSON are listed in :doc:`json`.
- Y
- Y
- Y
-
-
-
-
-
- Y
-
* - double
- Y
- Y
Expand All @@ -140,11 +154,13 @@ supported conversions to/from JSON are listed in :doc:`json`.
- Y
- Y
- Y
-
-
-
-
-
- Y
-
* - varchar
- Y
- Y
Expand All @@ -154,11 +170,29 @@ supported conversions to/from JSON are listed in :doc:`json`.
- Y
- Y
- Y
-
- Y
- Y
- Y
-
- Y
- Y
* - varbinary
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Y
* - timestamp
-
-
Expand All @@ -168,11 +202,13 @@ supported conversions to/from JSON are listed in :doc:`json`.
-
-
- Y
-
- Y
- Y
- Y
-
-
-
* - timestamp with time zone
-
-
Expand All @@ -182,11 +218,13 @@ supported conversions to/from JSON are listed in :doc:`json`.
-
-
- Y
-
- Y
-
- Y
-
-
-
* - date
-
-
Expand All @@ -196,11 +234,13 @@ supported conversions to/from JSON are listed in :doc:`json`.
-
-
- Y
-
- Y
- Y
-
-
-
-
* - interval day to second
-
-
Expand All @@ -210,6 +250,8 @@ supported conversions to/from JSON are listed in :doc:`json`.
-
-
- Y
-
-
-
-
-
Expand All @@ -224,11 +266,29 @@ supported conversions to/from JSON are listed in :doc:`json`.
- Y
- Y
- Y
-
-
-
-
-
- Y
-
* - ipaddress
-
-
-
-
-
-
-
- Y
- Y
-
-
-
-
-
-

Cast to Integral Types
----------------------
Expand Down Expand Up @@ -919,6 +979,54 @@ Invalid example
SELECT cast('-3E+2.1' as decimal(12, 2)); -- Value is not a number
SELECT cast('3E+' as decimal(12, 2)); -- Value is not a number

Cast to IPAddress
mohsaka marked this conversation as resolved.
Show resolved Hide resolved
---------------

From varchar type
mohsaka marked this conversation as resolved.
Show resolved Hide resolved
^^^^^^^^^^^^^^^^^

To cast a varchar to IPAddress it must be in the form of either
mohsaka marked this conversation as resolved.
Show resolved Hide resolved
mohsaka marked this conversation as resolved.
Show resolved Hide resolved
IPV4 or IPV6. It can be written in full form or shortened form.
mohsaka marked this conversation as resolved.
Show resolved Hide resolved
All IPV6 addresses that have the form of an IPV4 mapped IPV6
mohsaka marked this conversation as resolved.
Show resolved Hide resolved
mohsaka marked this conversation as resolved.
Show resolved Hide resolved
address will be assumed to be IPV4 addresses.
mohsaka marked this conversation as resolved.
Show resolved Hide resolved

Valid examples

::

SELECT cast('2001:0db8:0000:0000:0000:ff00:0042:8329' as ipaddress); -- ipaddress '2001:db8::ff00:42:8329'
mohsaka marked this conversation as resolved.
Show resolved Hide resolved
SELECT cast('1.2.3.4' as ipaddress); -- ipaddress '1.2.3.4'
SELECT cast('::ffff:ffff:ffff' as ipaddress); -- ipaddress '255.255.255.255'

Invalid examples

::

SELECT cast('2001:db8::1::1' as ipaddress); -- Invalid IP address
mohsaka marked this conversation as resolved.
Show resolved Hide resolved
SELECT cast('789.1.1.1' as ipaddress); -- Invalid IP address

From varbinary type
mohsaka marked this conversation as resolved.
Show resolved Hide resolved
^^^^^^^^^^^^^^^^^

To cast a varbinary to IPAddress it must be either IPV4(4 Bytes)
mohsaka marked this conversation as resolved.
Show resolved Hide resolved
or IPV6(16 Bytes) in network byte order.
All IPV6 addresses that have the form of an IPV4 mapped IPV6
mohsaka marked this conversation as resolved.
Show resolved Hide resolved
address will be assumed to be IPV4 addresses.

Valid examples

::

SELECT cast(from_hex('20010db8000000000000ff0000428329') as ipaddress); -- ipaddress '2001:db8::ff00:42:8329'
SELECT cast(from_hex('01020304') as ipaddress); -- ipaddress '1.2.3.4'
SELECT cast(from_hex('00000000000000000000ffff01020304') as ipaddress); -- ipaddress '1.2.3.4'

Invalid examples

::

SELECT cast(from_hex('f000001100') as ipaddress); -- Varbinary length 5, must be IPV4(4), or IPV6(16)
mohsaka marked this conversation as resolved.
Show resolved Hide resolved

Miscellaneous
-------------

Expand Down
2 changes: 2 additions & 0 deletions velox/expression/tests/CustomTypeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ TEST_F(CustomTypeTest, getCustomTypeNames) {
"HYPERLOGLOG",
"TIMESTAMP WITH TIME ZONE",
"UUID",
"IPADDRESS",
}),
names);

Expand All @@ -229,6 +230,7 @@ TEST_F(CustomTypeTest, getCustomTypeNames) {
"HYPERLOGLOG",
"TIMESTAMP WITH TIME ZONE",
"UUID",
"IPADDRESS",
"FANCY_INT",
}),
names);
Expand Down
26 changes: 26 additions & 0 deletions velox/functions/prestosql/IPAddressFunctions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed 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.
*/
#pragma once

#include "velox/functions/prestosql/types/IPAddressType.h"

namespace facebook::velox::functions {

void registerIPAddressFunctions(const std::string& prefix) {
registerIPAddressType();
}

} // namespace facebook::velox::functions
3 changes: 3 additions & 0 deletions velox/functions/prestosql/TypeOf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
#include "velox/expression/VectorFunction.h"
#include "velox/functions/prestosql/types/HyperLogLogType.h"
#include "velox/functions/prestosql/types/IPAddressType.h"
#include "velox/functions/prestosql/types/JsonType.h"
#include "velox/functions/prestosql/types/TimestampWithTimeZoneType.h"
#include "velox/functions/prestosql/types/UuidType.h"
Expand Down Expand Up @@ -54,6 +55,8 @@ std::string typeName(const TypePtr& type) {
case TypeKind::HUGEINT: {
if (isUuidType(type)) {
return "uuid";
} else if (isIPAddressType(type)) {
return "ipaddress";
}
VELOX_USER_CHECK(
type->isDecimal(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/
#include <string>
#include "velox/functions/prestosql/IPAddressFunctions.h"
#include "velox/functions/prestosql/UuidFunctions.h"

namespace facebook::velox::functions {
Expand Down Expand Up @@ -108,6 +109,7 @@ void registerAllScalarFunctions(const std::string& prefix) {
registerBinaryFunctions(prefix);
registerBitwiseFunctions(prefix);
registerUuidFunctions(prefix);
registerIPAddressFunctions(prefix);
}

void registerMapAllowingDuplicates(
Expand Down
1 change: 1 addition & 0 deletions velox/functions/prestosql/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ add_executable(
HyperLogLogCastTest.cpp
HyperLogLogFunctionsTest.cpp
InPredicateTest.cpp
IPAddressCastTest.cpp
JsonCastTest.cpp
JsonExtractScalarTest.cpp
JsonFunctionsTest.cpp
Expand Down
Loading
Loading