Starburst IBM Db2 connector#

The IBM Db2 connector allows querying and creating tables in an external Db2 database.

Requirements#

To connect to IBM Db2, you need:

  • Db2 for Linux, Unix, and Windows (LUW) 11.5 or higher. Db2 for z/OS and Db2 for IBM i are not supported.

  • Network access from the coordinator and workers to the Db2 server. Port 51000 is the default port.

  • A valid Starburst Enterprise license.

Configuration#

Any of the below installation and configuration methods require the following artifacts:

  • Db2 JDBC driver file db2jcc-db2jcc4.jar obtained from IBM

  • JDBC connection details to connect to Db2 in a catalog properties file (e.g. example.properties for a catalog named example). File should contain the following contents, replacing the connection properties as appropriate for your setup:

connector.name=db2
connection-url=jdbc:db2://HOST:PORT/DATABASE
connection-user=USERNAME
connection-password=PASSWORD

The Db2 JDBC driver documentation contains information about format and parameters of the JDBC URL.

To install the Db2 Database connector, use the following steps:

  1. Add the Db2 JDBC driver JAR files to the plugin/db2 directory.

  2. Add a catalog properties file (e.g. example.properties for a catalog named example).

  3. Perform the above steps on every node.

  4. Restart SEP on every node.

General configuration properties#

The following table describes general catalog configuration properties for the connector:

Property name

Description

case-insensitive-name-matching

Support case insensitive schema and table names. Defaults to false.

case-insensitive-name-matching.cache-ttl

Duration for which case insensitive schema and table names are cached. Defaults to 1m.

case-insensitive-name-matching.config-file

Path to a name mapping configuration file in JSON format that allows Trino to disambiguate between schemas and tables with similar names in different cases. Defaults to null.

case-insensitive-name-matching.config-file.refresh-period

Frequency with which Trino checks the name matching configuration file for changes. The duration value defaults to 0s (refresh disabled).

metadata.cache-ttl

Duration for which metadata, including table and column statistics, is cached. Defaults to 0s (caching disabled).

metadata.cache-missing

Cache the fact that metadata, including table and column statistics, is not available. Defaults to false.

metadata.schemas.cache-ttl

Duration for which schema metadata is cached. Defaults to the value of metadata.cache-ttl.

metadata.tables.cache-ttl

Duration for which table metadata is cached. Defaults to the value of metadata.cache-ttl.

metadata.statistics.cache-ttl

Duration for which tables statistics are cached. Defaults to the value of metadata.cache-ttl.

metadata.cache-maximum-size

Maximum number of objects stored in the metadata cache. Defaults to 10000.

write.batch-size

Maximum number of statements in a batched execution. Do not change this setting from the default. Non-default values may negatively impact performance. Defaults to 1000.

dynamic-filtering.enabled

Push down dynamic filters into JDBC queries. Defaults to true.

dynamic-filtering.wait-timeout

Maximum duration for which Trino waits for dynamic filters to be collected from the build side of joins before starting a JDBC query. Using a large timeout can potentially result in more detailed dynamic filters. However, it can also increase latency for some queries. Defaults to 20s.

Type mapping#

Because Trino and Db2 each support types that the other does not, this connector modifies some types when reading or writing data. Data types may not map the same way in both directions between Trino and the data source. Refer to the following sections for type mapping in each direction.

Db2 to Trino type mapping#

The connector maps Db2 types to the corresponding Trino types according to the following table:

Db2 to Trino type mapping#

Db2 type

Trino type

Notes

NUMERIC

DECIMAL(p, s)

DECIMAL(p, s)

DECIMAL(p, s)

DECFLOAT(p)

DECIMAL(p, s)

FLOAT[(p)]

DOUBLE

REAL

REAL

DOUBLE

DOUBLE

DOUBLE PRECISION

DOUBLE

TINYINT

SMALLINT

SMALLINT

SMALLINT

INT

INT

BIGINT

BIGINT

DOUBLE PRECISION

DOUBLE

VARCHAR(n)

VARCHAR(n)

CHAR VARYING(n)

VARCHAR(n)

BINARY(n)

VARBINARY

BINARY VARYING

VARBINARY

VARBINARY(n)

VARBINARY

CHAR(n)

CHAR(n)

CLOB

VARCHAR

BLOB

VARBINARY

ROWID

VARCHAR

DATE

DATE

See Mapping datetime types

TIMESTAMP(p)

TIMESTAMP(9)

See Mapping datetime types

TIME

TIME

See Mapping datetime types

No other types are supported.

Trino to Db2 type mapping#

The connector maps Trino types to the corresponding Db2 types according to the following table:

Trino to Db2 type mapping#

Trino type

Db2 type

Notes

BOOLEAN

BOOLEAN

TINYINT

SMALLINT

SMALLINT

SMALLINT

INTEGER

INTEGER

BIGINT

BIGINT

REAL

FLOAT(23)

DOUBLE

FLOAT(53)

DECIMAL(p, s)

DECIMAL(p, s)

VARCHAR(n)

VARCHAR(n)

For n up to 32704.

VARCHAR(n)

CLOB

For n above 32704.

VARCHAR

CLOB

When no bound is given.

CHAR(n)

CHAR(n)

For n up to 255. n above 255 is not supported.

VARBINARY

BLOB

TIMESTAMP

TIMESTAMP

See Mapping datetime types

TIME

TIME

See Mapping datetime types

DATE

DATE

See Mapping datetime types

No other types are supported.

Mapping datetime types#

Db2 TIMESTAMP values with fractional precision greater than 9 must be explicitly cast to a precision of nine digits for SEP to handle them properly.

Inserting a TIME value with fractional seconds into Db2 truncates (not rounds) the time to second precision, as Db2 does not support fractional seconds in TIME values.

Warning

Because of differences in date and time representation in Db2 and JDBC, attempting to insert or select a datetime value earlier than 1582-10-15 results in an incorrect date being stored/retrieved.

Type mapping configuration properties#

The following properties can be used to configure how data types from the connected data source are mapped to Trino data types and how the metadata is cached in Trino.

Property name

Description

Default value

unsupported-type-handling

Configure how unsupported column data types are handled:

  • IGNORE, column is not accessible.

  • CONVERT_TO_VARCHAR, column is converted to unbounded VARCHAR.

The respective catalog session property is unsupported_type_handling.

IGNORE

jdbc-types-mapped-to-varchar

Allow forced mapping of comma separated lists of data types to convert to unbounded VARCHAR

SQL support#

The connector provides read and write access to data and metadata in the Db2 database. In addition to the globally available and read operation statements, the connector supports the following features:

SQL DELETE#

If a WHERE clause is specified, the DELETE operation only works if the predicate in the clause can be fully pushed down to the data source.

ALTER TABLE RENAME TO#

The connector does not support renaming tables across multiple schemas. For example, the following statement is supported:

ALTER TABLE example.schema_one.table_one RENAME TO example.schema_one.table_two

The following statement attempts to rename a table across schemas, and therefore is not supported:

ALTER TABLE example.schema_one.table_one RENAME TO example.schema_two.table_two

ALTER TABLE EXECUTE#

The connector supports the following commands for use with ALTER TABLE EXECUTE:

collect_statistics#

The collect_statistics command is used with Managed statistics to collect statistics for a table and its columns.

The following statement collects statistics for the example_table table and all of its columns:

ALTER TABLE example_table EXECUTE collect_statistics;

Collecting statistics for all columns in a table may be unnecessarily performance-intensive, especially for wide tables. To only collect statistics for a subset of columns, you can include the columns parameter with an array of column names. For example:

ALTER TABLE example_table
    EXECUTE collect_statistics(columns => ARRAY['customer','line_item']);

Table functions#

The connector provides specific table functions to access Db2.

query(VARCHAR) -> table#

The query function allows you to query the underlying database directly. It requires syntax native to the data source, because the full query is pushed down and processed in the data source. This can be useful for accessing native features or for improving query performance in situations where running a query natively may be faster.

The query table function is available in the system schema of any catalog that uses the Db2 connector, such as example. The following example passes myQuery to the data source. myQuery has to be a valid query for the data source, and is required to return a table as a result:

SELECT
  *
FROM
  TABLE(
    example.system.query(
      query => 'myQuery'
    )
  );

Fault-tolerant execution support#

The connector supports Fault-tolerant execution of query processing. Read and write operations are both supported with any retry policy.

Performance#

The connector includes a number of performance improvements, detailed in the following sections.

Table statistics#

The Db2 connector can use table and column statistics for cost based optimizations, to improve query processing performance based on the actual data in the data source.

The statistics are collected by Db2 and retrieved by the connector.

To collect statistics for a table, execute the following statements in Db2.

CALL SYSPROC.ADMIN_CMD('RUNSTATS ON TABLE table_name');

Managed statistics#

The connector supports Managed statistics allowing SEP to collect and store its own table and column statistics that can then be used for performance optimizations in query planning.

Statistics must be collected manually using the built-in collect_statistics command, see collect_statistics for details and examples.

Pushdown#

The connector supports pushdown for a number of operations:

Aggregate pushdown for the following functions:

Cost-based join pushdown#

The connector supports cost-based Join pushdown to make intelligent decisions about whether to push down a join operation to the data source.

When cost-based join pushdown is enabled, the connector only pushes down join operations if the available Table statistics suggest that doing so improves performance. Note that if no table statistics are available, join operation pushdown does not occur to avoid a potential decrease in query performance.

The following table describes catalog configuration properties for join pushdown:

Property name

Description

Default value

join-pushdown.enabled

Enable join pushdown. Equivalent catalog session property is join_pushdown_enabled.

true

join-pushdown.strategy

Strategy used to evaluate whether join operations are pushed down. Set to AUTOMATIC to enable cost-based join pushdown, or EAGER to push down joins whenever possible. Note that EAGER can push down joins even when table statistics are unavailable, which may result in degraded query performance. Because of this, EAGER is only recommended for testing and troubleshooting purposes.

AUTOMATIC

Dynamic filtering#

Dynamic filtering is enabled by default. It causes the connector to wait for dynamic filtering to complete before starting a JDBC query.

You can disable dynamic filtering by setting the dynamic-filtering.enabled property in your catalog configuration file to false.

Wait timeout#

By default, table scans on the connector are delayed up to 20 seconds until dynamic filters are collected from the build side of joins. Using a large timeout can potentially result in more detailed dynamic filters. However, it can also increase latency for some queries.

You can configure the dynamic-filtering.wait-timeout property in your catalog properties file:

dynamic-filtering.wait-timeout=1m

You can use the dynamic_filtering_wait_timeout catalog session property in a specific session:

SET SESSION example.dynamic_filtering_wait_timeout = 1s;

Compaction#

The maximum size of dynamic filter predicate, that is pushed down to the connector during table scan for a column, is configured using the domain-compaction-threshold property in the catalog properties file:

domain-compaction-threshold=100

You can use the domain_compaction_threshold catalog session property:

SET SESSION domain_compaction_threshold = 10;

By default, domain-compaction-threshold is set to 32. When the dynamic predicate for a column exceeds this threshold, it is compacted into a single range predicate.

For example, if the dynamic filter collected for a date column dt on the fact table selects more than 32 days, the filtering condition is simplified from dt IN ('2020-01-10', '2020-01-12',..., '2020-05-30') to dt BETWEEN '2020-01-10' AND '2020-05-30'. Using a large threshold can result in increased table scan overhead due to a large IN list getting pushed down to the data source.

Metrics#

Metrics about dynamic filtering are reported in a JMX table for each catalog:

jmx.current."io.trino.plugin.jdbc:name=example,type=dynamicfilteringstats"

Metrics include information about the total number of dynamic filters, the number of completed dynamic filters, the number of available dynamic filters and the time spent waiting for dynamic filters.

JDBC connection pooling#

When JDBC connection pooling is enabled, each node creates and maintains a connection pool instead of opening and closing separate connections to the data source. Each connection is available to connect to the data source and retrieve data. After completion of an operation, the connection is returned to the pool and can be reused. This improves performance by a small amount, reduces the load on any required authentication system used for establishing the connection, and helps avoid running into connection limits on data sources.

JDBC connection pooling is disabled by default. You can enable JDBC connection pooling by setting the connection-pool.enabled property to true in your catalog configuration file:

connection-pool.enabled=true

The following catalog configuration properties can be used to tune connection pooling:

JDBC connection pooling catalog configuration properties#

Property name

Description

Default value

connection-pool.enabled

Enable connection pooling for the catalog.

false

connection-pool.max-size

The maximum number of idle and active connections in the pool.

10

connection-pool.max-connection-lifetime

The maximum lifetime of a connection. When a connection reaches this lifetime it is removed, regardless of how recently it has been active.

30m

connection-pool.pool-cache-max-size

The maximum size of the JDBC data source cache.

1000

connection-pool.pool-cache-ttl

The expiration time of a cached data source when it is no longer accessed.

30m

Starburst Cached Views#

The connectors supports table scan redirection to improve performance and reduce load on the data source.

Security#

The connector includes a number of security-related features, detailed in the following sections.

User impersonation#

Db2 connector supports user impersonation.

User impersonation can be enabled in your catalog file:

db2.impersonation.enabled=true

User impersonation is based on SET SESSION_USER detailed in the IBM documentation.

Note

Running SET SESSION_USER in Db2 requires the user to have a SETSESSIONUSER privilege.

Password credential pass-through#

The connector supports password credential pass-through. To enable it, edit the catalog properties file to include the authentication type:

db2.authentication.type=PASSWORD_PASS_THROUGH

For more information about configurations and limitations, see Password credential pass-through.