liveBook · Manning

Type inference

As explained in DuckDB in Action

Type inference is the automatic determination of data types from available program or expression information.

Type inference automatically determines data types from available information. DuckDB infers Python user-defined function parameter and return types from annotations, and can infer table column types while reading CSV files. section 6.4 section 10.1.4

Use inference when the input provides enough reliable type information, but make the contract explicit when correctness or portability matters:

  • Registering remove_spaces with annotations lets DuckDB infer a VARCHAR parameter and VARCHAR return type. section 6.4
  • Reading Stack Overflow CSV files with auto_detect=true infers the table schema directly from the files. section 10.1.4
  • Explicit types prevent inference pitfalls when annotations are absent, inaccurate, or unavailable in third-party libraries. section 6.4
  • Defining a schema beforehand provides control but requires updates when file structure or column types change. section 10.1.4

Infer or declare a DuckDB function contract

A typed Python function such as def remove_spaces(field: str) -> str: ... can be registered directly. DuckDB exposes the inferred VARCHAR parameter and return type; alternatively, registration can explicitly declare [(VARCHAR)] and VARCHAR. Querying the function against Region confirms the cleaned values and their changed lengths. section 6.4

Listing 10.5 Creating or replacing the Stack Overflow users table
CREATE OR REPLACE TABLE users AS
SELECT *
FROM read_csv(
    'Users.csv.gz',
    auto_detect=true,
    column_names=[
        'Id', 'Reputation', 'CreationDate', 'DisplayName',
        'LastAccessDate', 'AboutMe', 'Views', 'UpVotes', 'DownVotes'
    ]
);

Inspect a complete auto-detected CSV table definition for Users, including selected column names and inferred types. section 10.1.4

How DuckDB in Action covers it

How deep it goes
Explains annotation-based and CSV-based inference, then contrasts them with explicit declarations and schema maintenance.
Examples
Demonstrates remove_spaces registration, Region cleanup, and Stack Overflow Users and Posts imports using automatic CSV detection.
What you can do after
Apply it via DuckDB to register Python functions and load CSV tables, while recognizing when explicit types are safer.
How it connects
Connects type inference with Python type annotations, user-defined functions, schemas, CSV files, and data types.

Also explained in

Compare how 16 books explain type inference →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of DuckDB in Action. The section links point to the parts of the book it draws on.