Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"bad double value" in Google BigQuery

I'm working in Google BigQuery (not using LegacySQL), and I'm currently trying to cast() a string as a float64. Each time I get the error "Bad double value". I've also tried safe_cast() but it completely eliminates some of my id's (Ex: if one customer repeats 3 times for 3 different dates, and only has 'null' for a single "Height" entry, that customer is completely eliminated after I do safe_cast(), not just the row that had the 'null' value). I don't have any weird string value in my data, just whole or rational numbers or null entries.

Here's my current code:

select id, date,        
       cast(height as float64) as height,    
       cast(weight as float64) as weight   
from (select id, date, max(height) as height, max(weight) as weight   
      from table    
      group by 1,2
     )
group by 1, 2
like image 399
cheesetaco Avatar asked May 07 '17 23:05

cheesetaco


People also ask

What is safe cast in BigQuery?

Most data types can be cast from one type to another with the CAST function. When using CAST , a query can fail if BigQuery is unable to perform the cast. If you want to protect your queries from these types of errors, you can use SAFE_CAST .

What is BigQuery good for?

BigQuery is a fully managed enterprise data warehouse that helps you manage and analyze your data with built-in features like machine learning, geospatial analysis, and business intelligence.

Is Google BigQuery fast?

Jordan Tigani. Google BigQuery is a lightning-fast analytics database. Customers find BigQuery performance liberating, allowing them to experiment with enormous datasets without compromise.

Why is Google BigQuery?

Google BigQuery is a cloud-based big data analytics web service for processing very large read-only data sets. BigQuery was designed for analyzing data on the order of billions of rows, using a SQL-like syntax.


1 Answers

Of course safe_cast() returns NULL values. That is because you have inappropriate values in the data.

You can find these by doing:

select height, weight
from table
where safe_cast(height) is null or safe_cast(weight) is null;

Once you understand what the values are, fix the values or adjust the logic of the query.

If you just want the max of values are are properly numeric, then cast before the aggregation:

select id, date,
       max(safe_cast(height as float64)) as height,
       max(safe_cast(weight as float64)) as weight   
from table    
group by 1, 2;

A subquery doesn't seem necessary or desirable for your query.

like image 164
Gordon Linoff Avatar answered Sep 18 '22 20:09

Gordon Linoff