atoti.Column.default_value#

property Column.default_value: bool | int | float | date | datetime | time | Sequence[bool] | Sequence[int] | Sequence[float] | Sequence[str] | str | None#

Value used to replace None inserted values.

If not None, the default value must match the column’s data_type. For instance, a LocalDate column cannot use the string "N/A" as its default value.

Each data type has its own default default_value value:

>>> from pprint import pprint
>>> table = session.create_table(
...     "Main data types",
...     data_types={
...         data_type: data_type
...         for data_type in [
...             "boolean",
...             "double",
...             "double[]",
...             "float",
...             "float[]",
...             "int",
...             "int[]",
...             "LocalDate",
...             "LocalDateTime",
...             "LocalTime",
...             "long",
...             "long[]",
...             "String",
...             "ZonedDateTime",
...         ]
...     },
... )
>>> pprint(
...     {
...         column_name: table[column_name].default_value
...         for column_name in table
...     },
...     sort_dicts=False,
... )
{'boolean': False,
 'double': None,
 'double[]': None,
 'float': None,
 'float[]': None,
 'int': None,
 'int[]': None,
 'LocalDate': datetime.date(1970, 1, 1),
 'LocalDateTime': datetime.datetime(1970, 1, 1, 0, 0),
 'LocalTime': datetime.time(0, 0),
 'long': None,
 'long[]': None,
 'String': 'N/A',
 'ZonedDateTime': datetime.datetime(1970, 1, 1, 0, 0, tzinfo=TzInfo(UTC))}

Key columns cannot have None as their default value so it is forced to something else. For numeric scalar columns, this is zero:

>>> table = session.create_table(
...     "Numeric",
...     data_types={
...         data_type: data_type
...         for data_type in [
...             "int",
...             "float",
...             "long",
...             "double",
...         ]
...     },
...     keys={"int", "float"},
... )
>>> {column_name: table[column_name].default_value for column_name in table}
{'int': 0, 'float': 0.0, 'long': None, 'double': None}
>>> table += (None, None, None, None)
>>> table.head()
           long  double
int float
0   0.0    <NA>    <NA>

The default value of array columns is None and cannot be changed:

>>> session.create_table(  
...     "Array",
...     data_types={"long[]": "long[]"},
...     default_values={"long[]": [0, 0]},
... )
Traceback (most recent call last):
    ...
py4j.protocol.Py4JJavaError: ... there is no global default value defined for this type. ...

Changing the default value from None to something else affects both the previously inserted None values and the upcoming ones:

>>> table["long"].default_value = 42
>>> table["long"].default_value
42
>>> table.head()
           long  double
int float
0   0.0      42    <NA>
>>> table += (1, None, None, None)
>>> table.head().sort_index()
           long  double
int float
0   0.0      42    <NA>
1   0.0      42    <NA>

Changing the default value of a column with a non-None default value does not affect the existing rows:

>>> table["long"].default_value = 1337
>>> table["long"].default_value
1337
>>> table += (2, None, None, None)
>>> table.head().sort_index()
           long  double
int float
0   0.0      42    <NA>
1   0.0      42    <NA>
2   0.0    1337    <NA>
>>> del session.tables["Numeric"]
>>> table = session.create_table(
...     "Numeric",
...     keys={"int", "float"},
...     data_types={
...         data_type: data_type
...         for data_type in [
...             "int",
...             "float",
...             "long",
...             "double",
...         ]
...     },
...     default_values={"long": 1337},
... )
>>> table["long"].default_value
1337

The default value can also not be changed to None:

>>> table = session.create_table("Stringly", data_types={"String": "String"})
>>> table["String"].default_value = None
Traceback (most recent call last):
    ...
atoti._graphql_client.exceptions.GraphQLClientGraphQLMultiError: Cannot define a null default value for a non-nullable type.
>>> table["String"].default_value
'N/A'
>>> del session.tables["Stringly"]
>>> table = session.create_table(
...     "Stringly",
...     data_types={"String": "String"},
...     default_values={"String": None},
... )
>>> print(table["String"].default_value)
None