Tuesday, 3 June 2025

Introduction to Auto Schema Detection in BigQuery

BigQuery provides an auto schema detection feature that simplifies loading data by automatically inferring the schema from CSV and JSON files. This feature helps users to quickly import data without manually defining column types.

How BigQuery auto detect the Schema?

BigQuery automatically detects the schema by analyzing the first up to 100 rows of the file. It examines each field and assigns a data type based on sample values. Occasionally, BigQuery might modify field names to ensure compatibility with its SQL syntax.

 

Supported File Formats

·      Auto schema detection is only available for CSV and JSON files.

·      It supports GZip-compatible compression when opening files.

 

How Schema Detection Works

·      Delimiters: Detects common delimiters such as comma (,), pipe (|), and tab (\t).

·      Header Detection: If the first row contains only strings while other rows do not, BigQuery assumes it is a header row and ignores it.

·      Date Format: Dates must follow the YYYY-MM-DD format. Otherwise, the column is treated as a string.

·      Timestamp Formats: Recognizes multiple timestamp formats like:

o   YYYY-MM-DD HH:MM:SS

o   YYYY-MM-DD HH:MM:SS.sss

 

Limitations of Auto Schema Detection

·      Schema inference is based only on sample data and may not always be accurate.

·      If values in a column vary in format, the detected type may be inconsistent.

·      It does not support schema detection for file formats other than CSV and JSON.

 

Best Practices

·      Manually define schemas when data consistency is crucial.

·      Preprocess data to match expected formats before loading it.

·      Verify inferred schemas before using the dataset in queries.

 

Auto schema detection is a useful feature, but for production workloads, relying solely on it may lead to issues. Always validate your schema before loading large datasets into BigQuery.

 

Let's create a basic employee CSV file, upload it, and see how BigQuery detects the schema.

 

employees.csv

id,name,age,salary,hire_date,last_login,custom_date
1,Arjun Patel,30,55000.50,2022-05-15,2024-03-18 14:30:45,15/05/2022
2,Ananya Sharma,25,62000.75,2021-08-20,2024-03-18 09:15:30,20-08-2021
3,Ravi Kumar,40,75000.00,2019-11-10,2024-03-17 21:45:00,11.10.2019
4,Priya Reddy,35,68000.25,2020-07-05,2024-03-18 16:05:10,07/05/2020

Columns summary

·      id: Integer

·      name: String (Indian names)

·      age: Integer

·      salary: Float

·      hire_date: Standard Date Format (YYYY-MM-DD)

·      last_login: Timestamp Format (YYYY-MM-DD HH:MM:SS)

·      custom_date: Non-Standard Date Format (DD/MM/YYYY, DD-MM-YYYY, DD.MM.YYYY)

 

Follow below step-by-step procedure to upload employees.csv file.

 

Step 1: Open BigQuery Explorer

·      Open the Google Cloud Console.

·      Navigate to BigQuery by selecting BigQuery from the menu or using the search bar.

·      In the BigQuery Explorer, locate your project and dataset.

 

Step 2: Click on the Dataset

In the BigQuery Explorer, expand your project and click on the dataset where you want to upload the employees.csv file. You will be taken to the Dataset Information page.

 


Step 3: Click on 'CREATE TABLE'

On the dataset information page, click on the Create Table button to start the process of creating a new table.

 

Step 4: Fill in the Table Creation Form

Once you click Create Table, a form will open where you can specify details about the table you're creating.

 

Step 5: Choose 'Upload' as the Source

Under Source, select the Upload option. 

 


Click on the Browse button and select your employees.csv file from your local system.  

Step 6: Provide the Table Name

·      In the Table Name field, enter the name of the table, for example: auto_detect_employees.

·      This will be the name of the table in BigQuery that will hold the uploaded data.

 

Step 7: Auto-Detect Schema

In the Schema section, check the box next to Auto Detect. This allows BigQuery to automatically detect the schema based on the data in your CSV file.


BigQuery will infer the data types (e.g., string, integer, float, date, timestamp) by looking at the sample rows in the file.

 

Step 8: Click on 'Create Table'

After confirming all the details, click the Create Table button.

 

Step 9: Review the Table

After the table is created, you will be able to see it in your dataset. You can now run queries on the table and BigQuery will use the schema it auto-detected from the employees.csv file.

 


Since the custom_date column in employees.csv uses a non-standard date format (e.g., DD/MM/YYYY), BigQuery treat that column as a string rather than a date. It's good to verify the schema after the upload.

 

By following this procedure, you'll be able to upload the employees.csv file into BigQuery, and BigQuery will automatically infer the schema for your table based on the data in the file.

  

Previous                                                    Next                                                    Home

Saving and Sharing Queries in BigQuery

BigQuery allows users to save queries for future use and share them with collaborators. This guide explains how to save queries, manage permissions, and share them effectively.

 

1. Saving a Query in BigQuery

Open the BigQuery Query Editor and type your query.

 


Execute the query to ensure it runs successfully (It is optional step).

Click on the "Save query" button -> select Save query option.

 


In the "Save query" popup, enter:

 

·      Name: A meaningful name for your query.

·      Region: Choose the region where the query should be stored.

 


Click the "Save" button.

 

The saved query will now be available under the Queries section.

 


2. Sharing a Saved Query

Click on the three vertical dots next to the saved query.

 

Select "Share" from the dropdown menu. 


 

You will see two sharing options:

a.   Manage Permissions

b.   Copy link

 

2.1 Manage Permissions

Click "Manage Permissions" to open a sharing form.

 


Click on the button ‘Add User/Group’ to add users or groups who need access.

 


Assign roles:

·      Code Owner – Full access, including sharing.

·      Code Editor – Edit access.

·      Code Viewer – Read-only access.

Click "Save" to apply changes.

 

b. Copy Link

Click "Copy Link" to copy the query's shareable link. You can send this link to collaborators for quick access.

 

Previous                                                    Next                                                    Home

Prometheus: Ignore specific labels while matching

When writing PromQL queries, you often need to compare or perform operations on metrics that differ only by one or two labels. But what if some labels should be excluded from comparison? That’s where the ignoring keyword comes in!

 

Using ignoring, you can tell Prometheus to ignore specific labels while matching series during binary operations like and, or, /, +, -, *, etc.

 

What is the ignoring Keyword in Prometheus?

Prometheus metrics are made up of a metric name and a set of labels. Labels make metrics more specific, but sometimes you want to compare or combine metrics while ignoring one or more labels. That’s where ignoring comes into play.

 

Let’s take two metrics:

 

node_cpu_seconds_total{mode="idle"}

node_cpu_seconds_total{mode="system"}

 

They both track CPU time but for different modes. They share labels like cpu, instance, and job, but differ by mode.

 

If you try this:

node_cpu_seconds_total{mode="idle"} / node_cpu_seconds_total{mode="system"}

 

It won’t work, because Prometheus requires all labels to match unless told otherwise.


 

 

Using ignoring(mode)

node_cpu_seconds_total{mode="idle"}

/ ignoring(mode)

node_cpu_seconds_total{mode="system"}

 

Now, Prometheus will ignore the mode label and match the remaining labels like cpu, instance, and job.

 

Instead of per-mode results, you now get the ratio of idle time to system time per CPU.

 

You can now see, for each CPU, how much more time it spends idle compared to system time — ignoring the mode difference!


 

 

Previous                                                    Next                                                    Home

Logical/Set Binary Operators in Prometheus (AND, OR, UNLESS)

In Prometheus, logical/set binary operators are used to perform set-based operations (like intersection, union, or complement) on instant vectors only. These operations are based on matching labels rather than actual metric values.

Following table summarizes the Logical Operators.

Operator

Meaning

Description

and

Intersection 

Returns time series present in both sides

or

Union

Returns time series present in either side

unless 

Complement 

Returns time series on left side only, not on right

 

1. and (Intersection)

For example, 'up' metric return following response.

up{instance="localhost:9090", job="prometheus"}

up{instance="localhost:9100", job="node_exporter"} 

 


'node_memory_active_bytes' return following response.

node_memory_active_bytes{instance="localhost:9100", job="node_exporter"}

 


up and node_memory_active_bytes will return following response.

up{instance="localhost:9100", job="node_exporter"}

 


If you want the value of node_memory_active_bytes after performing and operation, make the metric ‘node_memory_active_bytes’ as left operand.

 

node_memory_active_bytes and up

 


(node_memory_active_bytes and up) vs  (up and node_memory_active_bytes)

For logical/set binary operators like and, Prometheus returns the time series from the left-hand side that have matching labels on the right-hand side.

 

node_memory_active_bytes and up

·      Keeps time series from node_memory_active_bytes

·      Filters to include only those series that have matching labels in up

 

up and node_memory_active_bytes

·      Keeps time series from up

·      Filters to include only those series that have matching labels in node_memory_active_bytes

 

2. or (union)

Returns all-time series that exist in either up or node_memory_MemAvailable_bytes.

 

For example, the metric node_boot_time_seconds return following response.

node_boot_time_seconds{instance="localhost:9100", job="node_exporter"}  1744263605.246621

 


The metric 'go_gc_heap_goal_bytes' return following response.

go_gc_heap_goal_bytes{instance="localhost:9090", job="prometheus"}  40831809

 


node_boot_time_seconds or go_gc_heap_goal_bytes return below response.

node_boot_time_seconds{instance="localhost:9100", job="node_exporter"}  1744263605.246621

go_gc_heap_goal_bytes{instance="localhost:9090", job="prometheus"}  40831809

 


3. unless — Complement

up unless go_gc_heap_goal_bytes

 

Returns time series from “up” that do not exist in “go_gc_heap_goal_bytes”.

 

How matching works?

Logical operators compare labels (like job, instance, etc.) to determine whether time series are the same.

 

If you want to control how the matching happens (e.g., only by cpu), you can use the on() or ignoring() clauses:

 

node_cpu_seconds_total{mode="idle"}

and

on(cpu)

node_cpu_seconds_total{mode="system"}

 

Following statement compare by ignoring mode label.

 

node_cpu_seconds_total{mode="idle"}

/

ignoring(mode)

node_cpu_seconds_total{mode="system"}

 

 

Previous                                                    Next                                                    Home