Skip to content

[WIP] Allow MySQL table engine and function to take a user's subsquery - #79652

Closed
the-devid wants to merge 23 commits into
ClickHouse:masterfrom
the-devid:improvements-for-table-functions
Closed

the-devid wants to merge 23 commits into
ClickHouse:masterfrom
the-devid:improvements-for-table-functions

Conversation

@the-devid

@the-devid the-devid commented Apr 28, 2025 •

Copy link
Copy Markdown

Changelog category (leave one):

  • New Feature

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):

Add suport for table function mysql and table engine MySQL to take a user's query as an argument and pass it to the MySQL as is.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

@CLAassistant

CLAassistant commented Apr 28, 2025 •

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
0 out of 2 committers have signed the CLA.

❌ Denis Vidyaev
❌ the-devid


Denis Vidyaev seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Apr 28, 2025
@clickhouse-gh

clickhouse-gh Bot commented Apr 28, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [456a081]

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Apr 28, 2025
@the-devid the-devid changed the title [WIP] Improvements for table functions (pass SELECT query to external DB as is; push down LIMIT clause) [WIP] Allow mysql table function to take user's SELECT subquery as an argument May 2, 2025
@the-devid the-devid changed the title [WIP] Allow mysql table function to take user's SELECT subquery as an argument [WIP] Allow MySQL table engine and function to take a user's subsquery May 18, 2025
@clickhouse-gh clickhouse-gh Bot added pr-feature Pull request with new product feature and removed pr-improvement Pull request with some product improvements labels May 18, 2025
@clickhouse-gh

clickhouse-gh Bot commented Jul 22, 2025

Copy link
Copy Markdown
Contributor

Dear @the-devid, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

7 similar comments
@clickhouse-gh

clickhouse-gh Bot commented Jul 29, 2025

Copy link
Copy Markdown
Contributor

Dear @the-devid, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Aug 5, 2025

Copy link
Copy Markdown
Contributor

Dear @the-devid, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Oct 7, 2025

Copy link
Copy Markdown
Contributor

Dear @the-devid, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Dec 9, 2025

Copy link
Copy Markdown
Contributor

Dear @the-devid, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Feb 10, 2026

Copy link
Copy Markdown
Contributor

Dear @the-devid, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Apr 14, 2026

Copy link
Copy Markdown
Contributor

Dear @the-devid, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Dear @the-devid, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

pull Bot pushed a commit to sowelswl/ClickHouse that referenced this pull request Jul 1, 2026
…e integrations

Generalizes the idea of ClickHouse#79652
(which added it for `mysql`) to all native SQL external-database integrations:
`mysql`, `postgresql` and `sqlite`, both as table functions and as table engines.

Instead of a remote table name, the table argument can now be a `SELECT` query
that is passed to the external database as is, written either as a subquery
`(SELECT ...)` or wrapped into `query('SELECT ...')`:

    SELECT * FROM mysql('host:3306', 'db', query('SELECT a, b FROM t WHERE a > 0'), 'user', 'pw');
    SELECT * FROM postgresql('host:5432', 'db', (SELECT a, b FROM t WHERE a > 0), 'user', 'pw');
    SELECT * FROM sqlite('db.sqlite', query('SELECT a, b FROM t'));

This is useful to push down joins, aggregations and arbitrary processing to the
external database. The structure of such a table is inferred from the query result;
the table is read-only and `INSERT` into it throws `INCORRECT_QUERY`.

Implementation:
- New shared `TableNameOrQuery` abstraction (`src/Storages/TableNameOrQuery.h/.cpp`)
  holding either a table name or a query, plus the argument parser
  `tryGetExternalDatabaseQuery` and the read-projection builder
  `buildQueryForExternalDatabaseSubquery`.
- MySQL: new `convertMySQLDataType(MYSQL_FIELD &)` overload and `mysqlxx::ResultBase::getField`;
  the structure is inferred by running the query with `LIMIT 0`.
- PostgreSQL: `convertPostgreSQLDataType` is exposed; the structure is inferred from
  the result column OIDs of `SELECT * FROM (<query>) AS __subquery LIMIT 0` executed in a
  read-only transaction (works against read-only replicas as well).
- SQLite: `convertSQLiteDataType` is exposed; the structure is inferred from the prepared
  statement's column metadata.

Tests: a stateless test for `sqlite` and integration tests for `mysql` and `postgresql`.
Docs updated for the three table functions.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
alexey-milovidov added a commit that referenced this pull request Jul 23, 2026
Query forwarding to external databases (#107740) is based on Denis
Vidiaev's #79652, the GeoJSON output format (#108065) on Mark Needham's
#98124, and SZ3 (#108788) revives Konstantin Vedernikov's #83088.

Co-Authored-By: Claude Fable 5 <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Query forwarding to external databases (ClickHouse#107740) is based on Denis
Vidiaev's ClickHouse#79652, the GeoJSON output format (ClickHouse#108065) on Mark Needham's
ClickHouse#98124, and SZ3 (ClickHouse#108788) revives Konstantin Vedernikov's ClickHouse#83088.

Co-Authored-By: Claude Fable 5 <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…e integrations

Generalizes the idea of ClickHouse#79652
(which added it for `mysql`) to all native SQL external-database integrations:
`mysql`, `postgresql` and `sqlite`, both as table functions and as table engines.

Instead of a remote table name, the table argument can now be a `SELECT` query
that is passed to the external database as is, written either as a subquery
`(SELECT ...)` or wrapped into `query('SELECT ...')`:

    SELECT * FROM mysql('host:3306', 'db', query('SELECT a, b FROM t WHERE a > 0'), 'user', 'pw');
    SELECT * FROM postgresql('host:5432', 'db', (SELECT a, b FROM t WHERE a > 0), 'user', 'pw');
    SELECT * FROM sqlite('db.sqlite', query('SELECT a, b FROM t'));

This is useful to push down joins, aggregations and arbitrary processing to the
external database. The structure of such a table is inferred from the query result;
the table is read-only and `INSERT` into it throws `INCORRECT_QUERY`.

Implementation:
- New shared `TableNameOrQuery` abstraction (`src/Storages/TableNameOrQuery.h/.cpp`)
  holding either a table name or a query, plus the argument parser
  `tryGetExternalDatabaseQuery` and the read-projection builder
  `buildQueryForExternalDatabaseSubquery`.
- MySQL: new `convertMySQLDataType(MYSQL_FIELD &)` overload and `mysqlxx::ResultBase::getField`;
  the structure is inferred by running the query with `LIMIT 0`.
- PostgreSQL: `convertPostgreSQLDataType` is exposed; the structure is inferred from
  the result column OIDs of `SELECT * FROM (<query>) AS __subquery LIMIT 0` executed in a
  read-only transaction (works against read-only replicas as well).
- SQLite: `convertSQLiteDataType` is exposed; the structure is inferred from the prepared
  statement's column metadata.

Tests: a stateless test for `sqlite` and integration tests for `mysql` and `postgresql`.
Docs updated for the three table functions.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-feature Pull request with new product feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants