Skip to main content
Using nulls begins with setting nullability of type fields at creation time. Each database API has a language-specific means for assigning nullability when creating a type. Null values and nullability are also supported for SQL.

Setting Nullability

Setting nullability is possible using three different methods: The process for specifying nullability varies between the different API languages and SQL; the process for each language is outlined below.

SQL

When creating a table in SQL, columns are nullable by default, but can be set as nullable explicitly with the modifier NULL. Non-nullable columns need to be defined explicitly with NOT NULL. In the following example, all columns except for id are nullable:

REST

When creating a type schema via REST, a call to /create/type needs to be made, where nullability of each column is specified in two places:
  1. As a union between the type of the nullable column and the keyword null:
  2. In the properties list, with each nullable column being assigned the nullable property:
In the following example, a type schema named ex_null_rest is created with a nullable column, null_col. To create this type schema, call the /create/type endpoint with the following payload:
The type_definition is passed in as a JSON string, so all quotes within it must be escaped.
An example call using curl might look like this, where the above JSON payload is in a file named create_null_type.json:

C++

C#

Java

JavaScript

Node.js

Python

Creating a Table with Nullable Columns

To create a table with a nullable column:

Creating a Type with Nullable Fields

To create a type, from which multiple tables can be instantiated, either the GPUdbRecordType object or a JSON string can be used.

GPUdbRecordType

JSON

When using a JSON string to create a type, one needs to specify nullability at the column property level but also at the type definition level as a union between the type of the column and null.

Using Nulls

Null values can be used in expressions much like any other value. Null functions can also be used on column names in expressions to test for and evaluate null column values. The native API null functions can be found here; the SQL null functions can be found here.

Examples

Given table employees, you can query to see which employees have not input their phone number into the employee record database using the SQL statement
You can also query for all employees and phone numbers, replacing missing numbers with a note:
Given roll-up view budget, you can create a projection to track if a department’s budget has changed since last year using the NULLIF() function. For example, in Python:

Aggregates and Nulls

There are two points of note when attempting to perform aggregation/grouping operations on a table or view that contains null values.
  • Null values are ignored in every aggregation calculation except for certain instances of COUNT() usage, e.g., counting the amount of records in a table or view (COUNT(*)) will include records with null values even if all the values in a record are null; however, counting the values in a column (COUNT(column_name)) will ignore null values
  • Aggregating against an empty table results in 0 records for the count and null values for the other calculations (e.g., SUM, MIN, VAR_POP)

Example

Given the following table survey_response:
If queried like so:
The following response is returned: