Skip to main content
Question

Conversion of numeric value to IPV6 string.

  • June 15, 2020
  • 1 reply
  • 5 views

sahil_kumar
Forum|alt.badge.img

The vertica functions V6_ATON() converts the IPV6 string to varbinary(16) and reversal (varbinary to IPV6) is achieved by function V6_NTOA().

The customer requirement is converting IPV6 to numeric and it's reverse as well , numeric to IPV6.

IPv6 is achieved by them by following:

dbadmin=> select ('0x'||to_hex(V6_ATON('2601:603:4f81:1b30:a99e:8090:6f23:94e')))::Numeric(39,0);

?column?

50515978093432843058966403658954246478
(1 row)

Now, need your thoughts on how to make reversal work (numeric to IPV6). I have tried various ways as below:

What I have tried:

I thought of to start the reversal of above numeric number by following:

First converting it to hexadecimal, then varbinary and then back to IPV6.

  1. However, the function to_hex doesn't accept numeric.

select to_hex(50515978093432843058966403658954246478);

ERROR 3457: Function to_hex(numeric) does not exist, or permission is denied for to_hex(numeric)

  1. Casting into integer first before converting to hexadecimal, was also not possible because the number is of Numeric(39,0) more than the integer can hold.

dbadmin=> select 50515978093432843058966403658954246478::INTEGER;
ERROR 5411: Value exceeds range of type numeric(18,0)

  1. Casting it into varbinary not possible
    dbadmin=> select 50515978093432843058966403658954246478::varbinary(16);
    ERROR 2366: Cannot cast type numeric to varbinary

I could achieve with below workaround, by using the linux shell for conversion of hexadecimal to decimal as below.

dbadmin=> \set a echo \'$(echo "obase=16; 50515978093432843058966403658954246478" | bc)\'

dbadmin=> \echo :a
'260106034F811B30A99E80906F23094E'

dbadmin=> select V6_NTOA(hex_to_binary(:a));

V6_NTOA

2601:603:4f81:1b30:a99e:8090:6f23:94e
(1 row)

However, not acceptable by customer. Customer's doesn't want to use Linux shell.

Customer'requirement in his words below. They basically want some way to convert numeric to IPV6 or a way to develop a function in vertica, which transforms numeric to IPV6 string.

"
Today we are joining IPv4's numeric value (result of INET_ATON) to range of IPs from third party data and that way we find various metrics about them.
In order to transform it back we are using INET_NTOA function.

In case of IPv6 we use the technique I shared with you, but unfortunately I can't find a way to make the opposite operation.
We need this operation for our production system.
If the option isn't exist today, what needed to develop such a function in Vertica? "

1 reply

SergeB
Forum|alt.badge.img
  • Participating Frequently
  • June 19, 2020

Attached is a Python Vertica SDK Transform function.
CREATE LIBRARY zpylib as '/home/dbadmin/python_udx/decimal2hex.py' LANGUAGE 'Python';

CREATE FUNCTION hexconvert AS LANGUAGE 'Python' NAME 'hexconvert_factory' LIBRARY zpylib FENCED;

select hexconvert( 50515978093432843058966403658954246478);

hexconvert

0x260106034f811b30a99e80906f23094e
(1 row)

select V6_NTOA(hex_to_binary(hexconvert( 50515978093432843058966403658954246478)));

V6_NTOA

2601:603:4f81:1b30:a99e:8090:6f23:94e