My primary goal is to filter out a range of IP addresses in IPv4 format by converting them to integers. So, for example, I'd like to filter an IP range from 192.168.1.1 to 192.168.1.100, but I first need to convert them to integers: 3232235777 to 3232235876.
Is there a way to do this in BigQuery (standard SQL)? I've looked at the BigQuery documentation but my combination of the NET.IP_FROM_STRING() and NET.IPV4_TO_INT64() functions keeps throwing this error: No matching signature for function NET.IPV4_TO_INT64 for argument types: STRING.
SELECT addr_str, ip_from_string, FORMAT("0x%X",
NET.IPV4_TO_INT64(ip_from_string)) AS ipv4_to_int64
FROM
(
SELECT addr_str, FORMAT("%T", NET.IP_FROM_STRING(addr_str)) AS ip_from_string
FROM UNNEST(['192.168.1.1','192.168.1.100']) AS addr_str
)
NET.IPV4_TO_INT64 function expects binary (BYTES) as an input, but you "accidentally" cast ip_from_string to string by applying FORMAT function.
Just remove it!
Why are you calling FORMAT on the result of NET.IP_FROM_STRING? NET.IP_FROM_STRING returns the IP address as bytes, and using that you can convert to an INT64 using NET.IPV4_TO_INT64, for example.
#standardSQL
SELECT
addr_str,
ip_from_string,
NET.IPV4_TO_INT64(ip_from_string) AS ipv4_to_int64
FROM (
SELECT
addr_str,
NET.IP_FROM_STRING(addr_str) AS ip_from_string
FROM UNNEST(['192.168.1.1','192.168.1.100']) AS addr_str
);
This answer covers the math of converting an IPv4 to an integer. Are there mathematical operators available to you in BigQuery?
Related
I'm trying to calculate a hash from a string for best-effort ordering and partioning purposes in Athena. There is no String to hashCode() similar in Athena, so as a best effort, I try to get the 2nd character and calculate its codepoint and get the modulus. (As I said, best effort, maybe a nice effort)
Consider the query:
SELECT
doc_id,
substring(doc_id, 2, 1),
typeof(split(substring(doc_id, 2, 1)))
FROM events LIMIT 100
The 3rd row returns a varchar but the codepoint function expects a varchar(1) and casting it does not work as cast(substring(doc_id, 2, 1) as varchar(1)).
FUNCTION_NOT_FOUND: line 6:5: Unexpected parameters (varchar) for function codepoint. Expected: codepoint(varchar(1))
How can I accomplish this task without modifiying the data source? I'm open to ideas.
You can compute a hash code with the xxhash64 function. It takes a varbinary as input, so first cast the string to that type. Since the function also returns a 64-bit varbinary value, you can convert it to a bigint via the from_big_endian_64 function
WITH t(x) AS (VALUES 'hello')
SELECT from_big_endian_64(xxhash64(cast(x AS varbinary)))
FROM t
output:
_col0
---------------------
2794345569481354659
(1 row)
I would like is possible to divide the column A by the column B as column C, in Bigquery.
For example SELECT prix / surface as prixmcarre FROM 'appartement', is not possible in bigquery.
Error: No matching signature for operator / for argument types: STRING, STRING. Supported signatures: FLOAT64 / FLOAT64; NUMERIC / NUMERIC at [1:8]
In the Google Cloud Document, I don't see the simple solution.
Thank's for your help :)
You must be using strings that look like integers or floats. You need to cast them. For example:
select cast('10' as INT64) / cast('5' as INT64)
Is there a way in BigQuery to convert a hex string to a decimal value?
Something like:
select hex("ff")
CAST now supports converting hexadecimal strings to INT64 or FLOAT64 values, even though it's not specified in their reference
Here's how you use it:
SELECT
CAST(columnA as FLOAT64) as float,
CAST(columnB as INT64) as int
FROM table
This should work, but it doesn't (I'm filing a feature request):
SELECT INTEGER('0xffff')
In the meantime, this does work:
SELECT FLOAT('0xffff')
255.0
For integer results:
SELECT INTEGER(FLOAT('0xffff'))
255
Looking into the query reference, I'd say no.
You have "HEX_STRING()" which does the opposite, but all the string to number functions seem to not take hex.
I am trying to extract the length of an integer column but I get this error as below. Can someone share their thoughts on this? Thanks.
Error: Argument type mismatch in function LENGTH: first argument is type int64
LENGTH is a function which operates on strings. If you want the string length of an integer, you could run, e.g.:
SELECT LENGTH(STRING(17));
You can also see the BigQuery query reference for string functions for more information.
If you have a GBQ Integer type (INT64) then most likely you have to do this nowadays:
SELECT LENGTH(CAST(17 AS STRING));
I have PostgreSQL 9.4 installed on my laptop and my database contains a versioned-number which has this format : A.B.C.D ( example : 1.2.13.6 ). How can i apply MAX aggregation to my column "version" which is text. Thank you very much
If the numbers are always numeric, you can do something like this:
select max(string_to_array(version, '.')::int[])
from your_table;
By converting the string into an array of integers, the comparison will be done correctly [1,12,1] is bigger than [1,1,1]
This will however fail if you have values like 1.2.13.6a in that column
SQLFiddle: http://sqlfiddle.com/#!15/d41d8/4608