Skip to content

Instantly share code, notes, and snippets.

@dovy
Last active July 25, 2017 21:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dovy/352771830c887570711fad8755746825 to your computer and use it in GitHub Desktop.
Save dovy/352771830c887570711fad8755746825 to your computer and use it in GitHub Desktop.
Use this to get a SQL statement for a BigQuery table. You can run use this in the console of chrome/firefox when the desired BigQuery table is selected. Great for casting specific columns.
if (typeof jQuery == 'undefined') {
var jq = document.createElement('script');
jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(jq);
// ... give time for script to load, then type (or see below for non wait option)
jQuery.noConflict();
}
var $columns = ""
$('.schema-table-field').each(function(e){
$columns = $columns + "\t"+ $(this).text().trim() + ",\n"
});
$columns = $columns.slice(0, -2);
var $project = $('.nav-link-selected:first-child').parent().parent().parent().parent().find('.tables-dataset-id-text').text().trim()
var $table = $('.content-title span').text().trim()
var $sql = "select \n"+$columns+" \nfrom "+$project+"."+$table
console.log($sql);

Recasting a column in a BigQuery Table (EASY WAY)

Recasting a column in BigQuery can sometimes be a pain, but using this code will make it much easier.

1. identify the column ID you wish to request.

2. Identify the new desired "type" from the list below

  • INT64
  • FLOAT64
  • BOOL
  • STRING
  • BYTES
  • DATE
  • DATETIME
  • TIME
  • TIMESTAMP

3. Run the code found in get_table_query.js when the table you want to alter is selected.

Example:

select 
	account_id,
	account_code,
	eff_dt_id
from nyt_pdw_dev.fact_hd_snapshot

4. Update the column id of the item you wish to alter

Add a CAST(COLUMN_NAME AS TYPE) AS COLUMN_NAME in place of your key.

select 
	account_id,
	account_code,
	CAST(eff_dt_id AS INT64) AS eff_date_id,
from nyt_pdw_dev.fact_hd_snapshot

5. Prepare to Query the table.

First, click the Query Table button. One the new view MAKE SURE you are using "standard sql". If you don't know what that is, click on Show Options, and be sure Standard SQL is checked.

6. Test the Query

You may want to add LIMIT 10 to the bottom of your query for testing. Just run the query to make sure there are no mistakes.

7. Replace your Table

  • REMOVE the LIMIT 10 from bottom of your query.
  • Click on Show Options (if not open)
  • Find Destination Table row (top item) and click on Select Table.
  • Choose the Collection and write in the table id. We're going to replace our table. (You can change the table name here to make sure you don't mess up for further testing.)
  • Once selected go to the Write Preference and click on Overwrite Table to replace your table.

Congrats, your table is recasted. :)

@bwalrond
Copy link

@dovy Can you elaborate on "3. Run the code found in get_table_query.js when the table you want to alter is selected." ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment