Skip to content

Default values not initialized when job loading data with the Python API #1495

@blieusong

Description

@blieusong

When issuing a job with load_table_from_json, fields in the Schema with a default values are not populated.

I use the following table for testing purposes:

schema = [
    bigquery.SchemaField("client_id", "INTEGER"),
    bigquery.SchemaField(
        "create_date", "DATETIME", default_value_expression="CURRENT_DATETIME()"
    ),
    bigquery.SchemaField("number", "INTEGER", default_value_expression=2, mode="NULLABLE"),
]

Note that I have tried with mode="REQUIRED" but that the loading only fails in such case.

I have also tried creating the table before launching the job and to let the Python load job handle it: none of that worked.

I don't have this issue when streaming instead of using jobs.

Environment details

  • macOS 10.15.7
  • Python version: 3.11.1
  • pip version: 23.0
  • google-cloud-bigquery version: 3.5.0

Steps to reproduce

  1. Run code below
  2. Query the table. Columns create_date and number are empty

Code example

from google.cloud import bigquery

client = bigquery.Client()

schema = [
    bigquery.SchemaField("client_id", "INTEGER"),
    bigquery.SchemaField(
        "create_date", "DATETIME", default_value_expression="CURRENT_DATETIME()"
    ),
    bigquery.SchemaField("number", "INTEGER", default_value_expression=2, mode="NULLABLE"),
]

lines = [
    {"client_id": 1},
    {"client_id": 2},
    {"client_id": 3},
    {"client_id": 4},
]

job_config = bigquery.LoadJobConfig()
job_config.schema = schema
job_config.create_disposition = "CREATE_IF_NEEDED"
job_config.write_disposition = "WRITE_TRUNCATE"

table = "whatever_table"

try:
    load_job = client.load_table_from_json(
        lines,
        destination=table,
        location="europe-west2",
        job_config=job_config,
    )
    print("Load job: %s [%s]" % (load_job.job_id, table))
except Exception as e:
    print("Oops: %s" % (e))

load_job.result()

Stack trace

None. Insertion happens but without the default columns populated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    api: bigqueryIssues related to the googleapis/python-bigquery API.priority: p3Desirable enhancement or fix. May not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions