Skip to main content

Core Query Functions

chdb.query

Execute SQL query using chDB engine. This is the main query function that executes SQL statements using the embedded ClickHouse engine. Supports various output formats and can work with in-memory or file-based databases. Syntax
Parameters Returns Returns the query result in the specified format: Raises Examples

chdb.sql

Execute SQL query using chDB engine. This is the main query function that executes SQL statements using the embedded ClickHouse engine. Supports various output formats and can work with in-memory or file-based databases. Syntax
Parameters Returns Returns the query result in the specified format: Raises Examples

chdb.to_arrowTable

Convert query result to PyArrow Table. Converts a chDB query result to a PyArrow Table for efficient columnar data processing. Returns an empty table if the result is empty. Syntax
Parameters Returns Raises Example

chdb.to_df

Convert query result to pandas DataFrame. Converts a chDB query result to a pandas DataFrame by first converting to PyArrow Table and then to pandas using multi-threading for better performance. Syntax
Parameters Returns Raises Example

Connection and Session Management

The following Session Functions are available:

chdb.connect

Create a connection to chDB background server. This function establishes a Connection to the chDB (ClickHouse) database engine. Only one open connection is allowed per process. Multiple calls with the same connection string will return the same connection object.
Parameters: Basic formats With query parameters Query parameter handling Query parameters are passed to ClickHouse engine as startup arguments. Special parameter handling: For a complete parameter list, see clickhouse local --help --verbose Returns Raises
Only one connection per process is supported. Creating a new connection will close any existing connection.
Examples
See also
  • Connection - Database connection class
  • Cursor - Database cursor for DB-API 2.0 operations

Exception Handling

class chdb.ChdbError

Bases: Exception Base exception class for chDB-related errors. This exception is raised when chDB query execution fails or encounters an error. It inherits from the standard Python Exception class and provides error information from the underlying ClickHouse engine.

class chdb.session.Session

Bases: object Session will keep the state of query. If path is None, it will create a temporary directory and use it as the database path and the temporary directory will be removed when the session is closed. You can also pass in a path to create a database at that path where will keep your data. You can also use a connection string to pass in the path and other parameters.
Examples
Connection string args handlingConnection strings containing query params like “file:test.db?param1=value1&param2=value2” “param1=value1” will be passed to ClickHouse engine as start up args.For more details, see clickhouse local –help –verboseSome special args handling:
  • “mode=ro” would be “–readonly=1” for clickhouse (read-only mode)
Important
  • There can be only one session at a time. If you want to create a new session, you need to close the existing one.
  • Creating a new session will close the existing one.

cleanup

Cleanup session resources with exception handling. This method attempts to close the session while suppressing any exceptions that might occur during the cleanup process. It’s particularly useful in error handling scenarios or when you need to ensure cleanup happens regardless of the session state. Syntax
This method will never raise an exception, making it safe to call in finally blocks or destructors.
Examples
See also
  • close() - For explicit session closing with error propagation

close

Close the session and cleanup resources. This method closes the underlying connection and resets the global session state. After calling this method, the session becomes invalid and can’t be used for further queries. Syntax
This method is automatically called when the session is used as a context manager or when the session object is destroyed.
ImportantAny attempt to use the session after calling close() will result in an error.
Examples

query

Execute a SQL query and return the results. This method executes a SQL query against the session’s database and returns the results in the specified format. The method supports various output formats and maintains session state between queries. Syntax
Parameters Returns Returns query results in the specified format. The exact return type depends on the format parameter:
  • String formats (CSV, JSON, etc.) return str
  • Binary formats (Arrow, Parquet) return bytes
Raises
The “Debug” format isn’t supported and will be automatically converted to “CSV” with a warning. For debugging, use connection string parameters instead.
WarningThis method executes the query synchronously and loads all results into memory. For large result sets, consider using send_query() for streaming results.
Examples
See also

send_query

Execute a SQL query and return a streaming result iterator. This method executes a SQL query against the session’s database and returns a streaming result object that allows you to iterate over the results without loading everything into memory at once. This is particularly useful for large result sets. Syntax
Parameters Returns Raises
The “Debug” format isn’t supported and will be automatically converted to “CSV” with a warning. For debugging, use connection string parameters instead.
The returned StreamingResult object should be consumed promptly or stored appropriately, as it maintains a connection to the database.
Examples
See also
  • query() - For non-streaming query execution
  • chdb.state.sqlitelike.StreamingResult - Streaming result iterator

sql

Execute a SQL query and return the results. This method executes a SQL query against the session’s database and returns the results in the specified format. The method supports various output formats and maintains session state between queries. Syntax
Parameters Returns Returns query results in the specified format. The exact return type depends on the format parameter:
  • String formats (CSV, JSON, etc.) return str
  • Binary formats (Arrow, Parquet) return bytes
Raises:
The “Debug” format isn’t supported and will be automatically converted to “CSV” with a warning. For debugging, use connection string parameters instead.
WarningThis method executes the query synchronously and loads all results into memory. For large result sets, consider using send_query() for streaming results.
Examples
See also

State Management

chdb.state.connect

Create a Connection to the chDB background server. This function establishes a connection to the chDB (ClickHouse) database engine. Only one open connection is allowed per process. Multiple calls with the same connection string will return the same connection object. Syntax
Parameters Basic formats Supported connection string formats: With query parameters Query parameter handling Query parameters are passed to ClickHouse engine as startup arguments. Special parameter handling: For a complete parameter list, see clickhouse local --help --verbose Returns Raises
WarningOnly one connection per process is supported. Creating a new connection will close any existing connection.
Examples
See also
  • Connection - Database connection class
  • Cursor - Database cursor for DB-API 2.0 operations

class chdb.state.sqlitelike.Connection

Bases: object Syntax

close

Close the connection and cleanup resources. This method closes the database connection and cleans up any associated resources including active cursors. After calling this method, the connection becomes invalid and can’t be used for further operations. Syntax
This method is idempotent - calling it multiple times is safe.
WarningAny ongoing streaming queries will be cancelled when the connection is closed. Ensure all important data is processed before closing.
Examples

cursor

Create a Cursor object for executing queries. This method creates a database cursor that provides the standard DB-API 2.0 interface for executing queries and fetching results. The cursor allows for fine-grained control over query execution and result retrieval. Syntax
Returns
Creating a new cursor will replace any existing cursor associated with this connection. Only one cursor per connection is supported.
Examples
See also
  • Cursor - Database cursor implementation

query

Execute a SQL query and return the complete results. This method executes a SQL query synchronously and returns the complete result set. It supports various output formats and automatically applies format-specific post-processing. Syntax
Parameters: Returns Raises
WarningThis method loads the entire result set into memory. For large results, consider using send_query() for streaming.
Examples
See also

send_query

Execute a SQL query and return a streaming result iterator. This method executes a SQL query and returns a StreamingResult object that allows you to iterate over the results without loading everything into memory at once. This is ideal for processing large result sets. Syntax
Parameters Returns Raises
Only the “Arrow” format supports the record_batch() method on the returned StreamingResult.
Examples
See also

class chdb.state.sqlitelike.StreamingResult

Bases: object Streaming result iterator for processing large query results. This class provides an iterator interface for streaming query results without loading the entire result set into memory. It supports various output formats and provides methods for manual result fetching and PyArrow RecordBatch streaming.

fetch

Fetch the next chunk of streaming results. This method retrieves the next available chunk of data from the streaming query result. The format of the returned data depends on the format specified when the streaming query was initiated. Syntax
Returns Examples

cancel

Cancel the streaming query and cleanup resources. This method cancels any ongoing streaming query and releases associated resources. It should be called when you want to stop processing results before the stream is exhausted. Syntax
Examples

close

Close the streaming result and cleanup resources. Alias for cancel(). Closes the streaming result iterator and releases any associated resources. Syntax

record_batch

Create a PyArrow RecordBatchReader for efficient batch processing. This method creates a PyArrow RecordBatchReader that allows efficient iteration over the query results in Arrow format. This is the most efficient way to process large result sets when using PyArrow. Syntax
Parameters Returns
This method is only available when the streaming query was initiated with format="Arrow". Using it with other formats will raise an error.
Examples

Iterator Protocol

StreamingResult supports the Python iterator protocol, allowing it to be used directly in for loops:

Context Manager Protocol

StreamingResult supports the context manager protocol for automatic resource cleanup:

class chdb.state.sqlitelike.Cursor

Bases: object

close

Close the cursor and cleanup resources. This method closes the cursor and cleans up any associated resources. After calling this method, the cursor becomes invalid and can’t be used for further operations. Syntax
This method is idempotent - calling it multiple times is safe. The cursor is also automatically closed when the connection is closed.
Examples

column_names

Return a list of column names from the last executed query. This method returns the column names from the most recently executed SELECT query. The names are returned in the same order as they appear in the result set. Syntax
Returns Examples
See also

column_types

Return a list of column types from the last executed query. This method returns the ClickHouse column type names from the most recently executed SELECT query. The types are returned in the same order as they appear in the result set. Syntax
Returns Examples
See also

commit

Commit any pending transaction. This method commits any pending database transaction. In ClickHouse, most operations are auto-committed, but this method is provided for DB-API 2.0 compatibility.
ClickHouse typically auto-commits operations, so explicit commits are usually not necessary. This method is provided for compatibility with standard DB-API 2.0 workflow.
Syntax
Examples

property description : list

Return column description as per DB-API 2.0 specification. This property returns a list of 7-item tuples describing each column in the result set of the last executed SELECT query. Each tuple contains: (name, type_code, display_size, internal_size, precision, scale, null_ok) Currently, only name and type_code are provided, with other fields set to None. Returns
This follows the DB-API 2.0 specification for cursor.description. Only the first two elements (name and type_code) contain meaningful data in this implementation.
Examples
See also

execute

Execute a SQL query and prepare results for fetching. This method executes a SQL query and prepares the results for retrieval using the fetch methods. It handles the parsing of result data and automatic type conversion for ClickHouse data types. Syntax
Parameters: Raises
This method follows DB-API 2.0 specifications for cursor.execute(). After execution, use fetchone(), fetchmany(), or fetchall() to retrieve results.
The method automatically converts ClickHouse data types to appropriate Python types:
  • Int/UInt types → int
  • Float types → float
  • String/FixedString → str
  • DateTime → datetime.datetime
  • Date → datetime.date
  • Bool → bool
Examples
See also

fetchall

Fetch all remaining rows from the query result. This method retrieves all remaining rows from the current query result set starting from the current cursor position. It returns a tuple of row tuples with appropriate Python type conversion applied. Syntax
Returns:
WarningThis method loads all remaining rows into memory at once. For large result sets, consider using fetchmany() to process results in batches.
Examples
See also

fetchmany

Fetch multiple rows from the query result. This method retrieves up to ‘size’ rows from the current query result set. It returns a tuple of row tuples, with each row containing column values with appropriate Python type conversion. Syntax
Parameters Returns
This method follows DB-API 2.0 specifications. It will return fewer than ‘size’ rows if the result set is exhausted.
Examples
See also

fetchone

Fetch the next row from the query result. This method retrieves the next available row from the current query result set. It returns a tuple containing the column values with appropriate Python type conversion applied. Syntax
Returns:
This method follows DB-API 2.0 specifications. Column values are automatically converted to appropriate Python types based on ClickHouse column types.
Examples
See also

chdb.state.sqlitelike

Convert query result to PyArrow Table. This function converts chdb query results to a PyArrow Table format, which provides efficient columnar data access and interoperability with other data processing libraries. Syntax
Parameters: Returns Raises
This function requires both pyarrow and pandas to be installed. Install them with: pip install pyarrow pandas
WarningEmpty results return an empty PyArrow Table with no schema.
Examples

chdb.state.sqlitelike.to_df

Convert query result to Pandas DataFrame. This function converts chdb query results to a Pandas DataFrame format by first converting to PyArrow Table and then to DataFrame. This provides convenient data analysis capabilities with Pandas API. Syntax
Parameters: Returns: Raises
This function uses multi-threading for the Arrow to Pandas conversion to improve performance on large datasets.
See also Examples

DataFrame Integration

class chdb.dataframe.Table

Bases:

Database API (DBAPI) 2.0 Interface

chDB provides a Python DB-API 2.0 compatible interface for database connectivity, allowing you to use chDB with tools and frameworks that expect standard database interfaces. The chDB DB-API 2.0 interface includes:
  • Connections: Database connection management with connection strings
  • Cursors: Query execution and result retrieval
  • Type System: DB-API 2.0 compliant type constants and converters
  • Error Handling: Standard database exception hierarchy
  • Thread Safety: Level 1 thread safety (threads may share modules but not connections)

Core Functions

The Database API (DBAPI) 2.0 Interface implements the following core functions:

chdb.dbapi.connect

Initialize a new database connection. Syntax
Parameters Raises

chdb.dbapi.get_client_info()

Get client version information. Returns the chDB client version as a string for MySQLdb compatibility. Syntax
Returns

Type constructors

chdb.dbapi.Binary(x)

Return x as a binary type. This function converts the input to bytes type for use with binary database fields, following the DB-API 2.0 specification. Syntax
Parameters Returns

Connection Class

class chdb.dbapi.connections.Connection(path=None)

Bases: object DB-API 2.0 compliant connection to chDB database. This class provides a standard DB-API interface for connecting to and interacting with chDB databases. It supports both in-memory and file-based databases. The connection manages the underlying chDB engine and provides methods for executing queries, managing transactions (no-op for ClickHouse), and creating cursors.
Parameters Variables Examples
ClickHouse doesn’t support traditional transactions, so commit() and rollback() operations are no-ops but provided for DB-API compliance.

close

Close the database connection. Closes the underlying chDB connection and marks this connection as closed. Subsequent operations on this connection will raise an Error. Syntax
Raises

commit

Commit the current transaction. Syntax
This is a no-op for chDB/ClickHouse as it doesn’t support traditional transactions. Provided for DB-API 2.0 compliance.

cursor

Create a new cursor for executing queries. Syntax
Parameters Returns Raises Example

escape

Escape a value for safe inclusion in SQL queries. Syntax
Parameters Returns Example

escape_string

Escape a string value for SQL queries. Syntax
Parameters Returns

property open

Check if the connection is open. Returns

query

Execute a SQL query directly and return raw results. This method bypasses the cursor interface and executes queries directly. For standard DB-API usage, prefer using cursor() method. Syntax
Parameters: Returns Raises Example

property resp

Get the last query response. Returns
This property is updated each time query() is called directly. It doesn’t reflect queries executed through cursors.

rollback

Roll back the current transaction. Syntax
This is a no-op for chDB/ClickHouse as it doesn’t support traditional transactions. Provided for DB-API 2.0 compliance.

Cursor Class

class chdb.dbapi.cursors.Cursor

Bases: object DB-API 2.0 cursor for executing queries and fetching results. The cursor provides methods for executing SQL statements, managing query results, and navigating through result sets. It supports parameter binding, bulk operations, and follows DB-API 2.0 specifications. Don’t create Cursor instances directly. Use Connection.cursor() instead.
Examples
See DB-API 2.0 Cursor Objects for complete specification details.

callproc

Execute a stored procedure (placeholder implementation). Syntax
Parameters Returns
chDB/ClickHouse doesn’t support stored procedures in the traditional sense. This method is provided for DB-API 2.0 compliance but doesn’t perform any actual operation. Use execute() for all SQL operations.
CompatibilityThis is a placeholder implementation. Traditional stored procedure features like OUT/INOUT parameters, multiple result sets, and server variables aren’t supported by the underlying ClickHouse engine.

close

Close the cursor and free associated resources. After closing, the cursor becomes unusable and any operation will raise an exception. Closing a cursor exhausts all remaining data and releases the underlying cursor. Syntax

execute

Execute a SQL query with optional parameter binding. This method executes a single SQL statement with optional parameter substitution. It supports multiple parameter placeholder styles for flexibility. Syntax
Parameters Returns Parameter Styles Examples
Raises

executemany(query, args)

Execute a query multiple times with different parameter sets. This method efficiently executes the same SQL query multiple times with different parameter values. It’s particularly useful for bulk INSERT operations. Syntax
Parameters Returns Examples
This method improves performance for multiple-row INSERT and UPDATE operations by optimizing the query execution process.

fetchall()

Fetch all remaining rows from the query result. Syntax
Returns Raises
WarningThis method can consume large amounts of memory for big result sets. Consider using fetchmany() for large datasets.
Example

fetchmany

Fetch multiple rows from the query result. Syntax
Parameters Returns Raises Example

fetchone

Fetch the next row from the query result. Syntax
Returns Raises Example

max_stmt_length = 1024000

Max statement size which executemany() generates. Default value is 1024000.

mogrify

Return the exact query string that would be sent to the database. This method shows the final SQL query after parameter substitution, which is useful for debugging and logging purposes. Syntax
Parameters Returns Example
This method follows the extension to DB-API 2.0 used by Psycopg.

nextset

Move to the next result set (not supported). Syntax
Returns
chDB/ClickHouse doesn’t support multiple result sets from a single query. This method is provided for DB-API 2.0 compliance but always returns None.

setinputsizes

Set input sizes for parameters (no-op implementation). Syntax
Parameters
This method does nothing but is required by DB-API 2.0 specification. chDB automatically handles parameter sizing internally.

setoutputsizes

Set output column sizes (no-op implementation). Syntax
Parameters
This method does nothing but is required by DB-API 2.0 specification. chDB automatically handles output sizing internally.

Error Classes

Exception classes for chdb database operations. This module provides a complete hierarchy of exception classes for handling database-related errors in chdb, following the Python Database API Specification v2.0. The exception hierarchy is structured as follows:
Each exception class represents a specific category of database errors:
These exception classes are compliant with Python DB API 2.0 specification and provide consistent error handling across different database operations.
See also Examples

exception chdb.dbapi.err.DataError

Bases: DatabaseError Exception raised for errors that are due to problems with the processed data. This exception is raised when database operations fail due to issues with the data being processed, such as:
  • Division by zero operations
  • Numeric values out of range
  • Invalid date/time values
  • String truncation errors
  • Type conversion failures
  • Invalid data format for column type
Raises Examples

exception chdb.dbapi.err.DatabaseError

Bases: Error Exception raised for errors that are related to the database. This is the base class for all database-related errors. It encompasses all errors that occur during database operations and are related to the database itself rather than the interface. Common scenarios include:
  • SQL execution errors
  • Database connectivity issues
  • Transaction-related problems
  • Database-specific constraints violations
This serves as the parent class for more specific database error types such as DataError, OperationalError, etc.

exception chdb.dbapi.err.Error

Bases: StandardError Exception that is the base class of all other error exceptions (not Warning). This is the base class for all error exceptions in chdb, excluding warnings. It serves as the parent class for all database error conditions that prevent successful completion of operations.
This exception hierarchy follows the Python DB API 2.0 specification.
See also
  • Warning - For non-fatal warnings that don’t prevent operation completion

exception chdb.dbapi.err.IntegrityError

Bases: DatabaseError Exception raised when the relational integrity of the database is affected. This exception is raised when database operations violate integrity constraints, including:
  • Foreign key constraint violations
  • Primary key or unique constraint violations (duplicate keys)
  • Check constraint violations
  • NOT NULL constraint violations
  • Referential integrity violations
Raises Examples

exception chdb.dbapi.err.InterfaceError

Bases: Error Exception raised for errors that are related to the database interface rather than the database itself. This exception is raised when there are problems with the database interface implementation, such as:
  • Invalid connection parameters
  • API misuse (calling methods on closed connections)
  • Interface-level protocol errors
  • Module import or initialization failures
Raises
These errors are typically programming errors or configuration issues that can be resolved by fixing the client code or configuration.

exception chdb.dbapi.err.InternalError

Bases: DatabaseError Exception raised when the database encounters an internal error. This exception is raised when the database system encounters internal errors that aren’t caused by the application, such as:
  • Invalid cursor state (cursor isn’t valid anymore)
  • Transaction state inconsistencies (transaction is out of sync)
  • Database corruption issues
  • Internal data structure corruption
  • System-level database errors
Raises
WarningInternal errors may indicate serious database problems that require database administrator attention. These errors are typically not recoverable through application-level retry logic.
These errors are generally outside the control of the application and may require database restart or repair operations.

exception chdb.dbapi.err.NotSupportedError

Bases: DatabaseError Exception raised when a method or database API isn’t supported. This exception is raised when the application attempts to use database features or API methods that aren’t supported by the current database configuration or version, such as:
  • Requesting rollback() on connections without transaction support
  • Using advanced SQL features not supported by the database version
  • Calling methods not implemented by the current driver
  • Attempting to use disabled database features
Raises Examples
Check database documentation and driver capabilities to avoid these errors. Consider graceful fallbacks where possible.

exception chdb.dbapi.err.OperationalError

Bases: DatabaseError Exception raised for errors that are related to the database’s operation. This exception is raised for errors that occur during database operation and aren’t necessarily under the control of the programmer, including:
  • Unexpected disconnection from database
  • Database server not found or unreachable
  • Transaction processing failures
  • Memory allocation errors during processing
  • Disk space or resource exhaustion
  • Database server internal errors
  • Authentication or authorization failures
Raises
These errors are typically transient and may be resolved by retrying the operation or addressing system-level issues.
WarningSome operational errors may indicate serious system problems that require administrative intervention.

exception chdb.dbapi.err.ProgrammingError

Bases: DatabaseError Exception raised for programming errors in database operations. This exception is raised when there are programming errors in the application’s database usage, including:
  • Table or column not found
  • Table or index already exists when creating
  • SQL syntax errors in statements
  • Wrong number of parameters specified in prepared statements
  • Invalid SQL operations (e.g., DROP on non-existent objects)
  • Incorrect usage of database API methods
Raises Examples

exception chdb.dbapi.err.StandardError

Bases: Exception Exception related to operation with chdb. This is the base class for all chdb-related exceptions. It inherits from Python’s built-in Exception class and serves as the root of the exception hierarchy for database operations.
This exception class follows the Python DB API 2.0 specification for database exception handling.

exception chdb.dbapi.err.Warning

Bases: StandardError Exception raised for important warnings like data truncations while inserting, etc. This exception is raised when the database operation completes but with important warnings that should be brought to the attention of the application. Common scenarios include:
  • Data truncation during insertion
  • Precision loss in numeric conversions
  • Character set conversion warnings
This follows the Python DB API 2.0 specification for warning exceptions.

Module Constants

chdb.dbapi.apilevel = '2.0'

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object._\_str_\_() (if defined) or repr(object).
  • encoding defaults to ‘utf-8’.
  • errors defaults to ‘strict’.

chdb.dbapi.threadsafety = 1

Convert a number or string to an integer, or return 0 if no arguments are given. If x is a number, return x._int_(). For floating-point numbers, this truncates towards zero. If x isn’t a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in the given base. The literal can be preceded by ‘+’ or ‘-’ and be surrounded by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to interpret the base from the string as an integer literal.

chdb.dbapi.paramstyle = 'format'

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object._str_() (if defined) or repr(object). encoding defaults to ‘utf-8’. errors defaults to ‘strict’.

Type Constants

chdb.dbapi.STRING = frozenset({247, 253, 254})

Extended frozenset for DB-API 2.0 type comparison. This class extends frozenset to support DB-API 2.0 type comparison semantics. It allows for flexible type checking where individual items can be compared against the set using both equality and inequality operators. This is used for type constants like STRING, BINARY, NUMBER, etc. to enable comparisons like “field_type == STRING” where field_type is a single type value. Examples

chdb.dbapi.BINARY = frozenset({249, 250, 251, 252})

Extended frozenset for DB-API 2.0 type comparison. This class extends frozenset to support DB-API 2.0 type comparison semantics. It allows for flexible type checking where individual items can be compared against the set using both equality and inequality operators. This is used for type constants like STRING, BINARY, NUMBER, etc. to enable comparisons like “field_type == STRING” where field_type is a single type value. Examples

chdb.dbapi.NUMBER = frozenset({0, 1, 3, 4, 5, 8, 9, 13})

Extended frozenset for DB-API 2.0 type comparison. This class extends frozenset to support DB-API 2.0 type comparison semantics. It allows for flexible type checking where individual items can be compared against the set using both equality and inequality operators. This is used for type constants like STRING, BINARY, NUMBER, etc. to enable comparisons like “field_type == STRING” where field_type is a single type value. Examples

chdb.dbapi.DATE = frozenset({10, 14})

Extended frozenset for DB-API 2.0 type comparison. This class extends frozenset to support DB-API 2.0 type comparison semantics. It allows for flexible type checking where individual items can be compared against the set using both equality and inequality operators. This is used for type constants like STRING, BINARY, NUMBER, etc. to enable comparisons like “field_type == STRING” where field_type is a single type value. Examples

chdb.dbapi.TIME = frozenset({11})

Extended frozenset for DB-API 2.0 type comparison. This class extends frozenset to support DB-API 2.0 type comparison semantics. It allows for flexible type checking where individual items can be compared against the set using both equality and inequality operators. This is used for type constants like STRING, BINARY, NUMBER, etc. to enable comparisons like “field_type == STRING” where field_type is a single type value. Examples

chdb.dbapi.TIMESTAMP = frozenset({7, 12})

Extended frozenset for DB-API 2.0 type comparison. This class extends frozenset to support DB-API 2.0 type comparison semantics. It allows for flexible type checking where individual items can be compared against the set using both equality and inequality operators. This is used for type constants like STRING, BINARY, NUMBER, etc. to enable comparisons like “field_type == STRING” where field_type is a single type value. Examples

chdb.dbapi.DATETIME = frozenset({7, 12})

Extended frozenset for DB-API 2.0 type comparison. This class extends frozenset to support DB-API 2.0 type comparison semantics. It allows for flexible type checking where individual items can be compared against the set using both equality and inequality operators. This is used for type constants like STRING, BINARY, NUMBER, etc. to enable comparisons like “field_type == STRING” where field_type is a single type value. Examples

chdb.dbapi.ROWID = frozenset({})

Extended frozenset for DB-API 2.0 type comparison. This class extends frozenset to support DB-API 2.0 type comparison semantics. It allows for flexible type checking where individual items can be compared against the set using both equality and inequality operators. This is used for type constants like STRING, BINARY, NUMBER, etc. to enable comparisons like “field_type == STRING” where field_type is a single type value. Examples
Usage Examples Basic Query Example:
Working with Data:
Connection Management:
Best Practices
  1. Connection Management: Always close connections and cursors when done
  2. Context Managers: Use with statements for automatic cleanup
  3. Batch Processing: Use fetchmany() for large result sets
  4. Error Handling: Wrap database operations in try-except blocks
  5. Parameter Binding: Use parameterized queries when possible
  6. Memory Management: Avoid fetchall() for very large datasets
  • chDB’s DB-API 2.0 interface is compatible with most Python database tools
  • The interface provides Level 1 thread safety (threads may share modules but not connections)
  • Connection strings support the same parameters as chDB sessions
  • All standard DB-API 2.0 exceptions are supported
Warning
  • Always close cursors and connections to avoid resource leaks
  • Large result sets should be processed in batches
  • Parameter binding syntax follows format style: %s

User-Defined Functions (UDF)

User-defined functions module for chDB. This module provides functionality for creating and managing user-defined functions (UDFs) in chDB. It allows you to extend chDB’s capabilities by writing custom Python functions that can be called from SQL queries.

chdb.udf.chdb_udf

Decorator for chDB Python UDF(User Defined Function). Syntax
Parameters Notes
  1. The function should be stateless. Only UDFs are supported, not UDAFs.
  2. Default return type is String. The return type should be one of the ClickHouse data types.
  3. The function should take in arguments of type String. All arguments are strings.
  4. The function will be called for each line of input.
  5. The function should be pure python function. Import all modules used IN THE FUNCTION.
  6. Python interpreter used is the same as the one used to run the script.
Example

chdb.udf.generate_udf

Generate UDF configuration and executable script files. This function creates the necessary files for a User Defined Function (UDF) in chDB:
  1. A Python executable script that processes input data
  2. An XML configuration file that registers the UDF with ClickHouse
Syntax
Parameters
This function is typically called by the @chdb_udf decorator and shouldn’t be called directly by users.

Utilities

Utility functions and helpers for chDB. This module contains various utility functions for working with chDB, including data type inference, data conversion helpers, and debugging utilities.

chdb.utils.convert_to_columnar

Converts a list of dictionaries into a columnar format. This function takes a list of dictionaries and converts it into a dictionary where each key corresponds to a column and each value is a list of column values. Missing values in the dictionaries are represented as None. Syntax
Parameters Returns Example

chdb.utils.flatten_dict

Flattens a nested dictionary. This function takes a nested dictionary and flattens it, concatenating nested keys with a separator. Lists of dictionaries are serialized to JSON strings. Syntax
Parameters Returns Example

chdb.utils.infer_data_type

Infers the most suitable data type for a list of values. This function examines a list of values and determines the most appropriate data type that can represent all the values in the list. It considers integer, unsigned integer, decimal, and float types, and defaults to “string” if the values can’t be represented by any numeric type or if all values are None. Syntax
Parameters Returns
  • If all values in the list are None, the function returns “string”.
  • If any value in the list is a string, the function immediately returns “string”.
  • The function assumes that numeric values can be represented as integers, decimals, or floats based on their range and precision.

chdb.utils.infer_data_types

Infers data types for each column in a columnar data structure. This function analyzes the values in each column and infers the most suitable data type for each column, based on a sample of the data. Syntax
Parameters Returns

Abstract Base Classes

class chdb.rwabc.PyReader(data: Any)`

Bases: ABC

abstractmethod read

Read a specified number of rows from the given columns and return a list of objects, where each object is a sequence of values for a column.
Parameters Returns

class chdb.rwabc.PyWriter

Bases: ABC

abstractmethod finalize

Assemble and return the final data from blocks. Must be implemented by subclasses.
Returns

abstractmethod write

Save columns of data to blocks. Must be implemented by subclasses.
Parameters

Exception Handling

class chdb.ChdbError

Bases: Exception Base exception class for chDB-related errors. This exception is raised when chDB query execution fails or encounters an error. It inherits from the standard Python Exception class and provides error information from the underlying ClickHouse engine. The exception message typically contains detailed error information from ClickHouse, including syntax errors, type mismatches, missing tables/columns, and other query execution issues. Variables Examples
This exception is automatically raised by chdb.query() and related functions when the underlying ClickHouse engine reports an error. You should catch this exception when handling potentially failing queries to provide appropriate error handling in your application.

Version Information

chdb.chdb_version = ('3', '6', '0')

Built-in immutable sequence. If no argument is given, the constructor returns an empty tuple. If iterable is specified the tuple is initialized from iterable’s items. If the argument is a tuple, the return value is the same object.

chdb.engine_version = '25.5.2.1'

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object._str_() (if defined) or repr(object).
  • encoding defaults to ‘utf-8’.
  • errors defaults to ‘strict’.

chdb.__version__ = '3.6.0'

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object._str_() (if defined) or repr(object).
  • encoding defaults to ‘utf-8’.
  • errors defaults to ‘strict’.
Last modified on July 2, 2026