CREATE SERVER deparse_lookback FOREIGN DATA WRAPPER clickhouse_fdw OPTIONS(dbname 'deparse_test'); CREATE USER MAPPING FOR CURRENT_USER SERVER deparse_lookback; CREATE SERVER deparse_admin FOREIGN DATA WRAPPER clickhouse_fdw; CREATE USER MAPPING FOR CURRENT_USER SERVER deparse_admin; CALL clickhouse_perform('deparse_admin', 'drop database if exists deparse_test'); CALL clickhouse_perform('deparse_admin', 'create database deparse_test'); CALL clickhouse_perform('deparse_admin', ' create table deparse_test.t1 (a int, b Int8) engine = MergeTree() order by a'); CALL clickhouse_perform('deparse_admin', ' insert into deparse_test.t1 select number % 10, number % 10 > 5 from numbers(1, 100);'); CREATE SCHEMA deparse_test; IMPORT FOREIGN SCHEMA deparse_test FROM SERVER deparse_lookback INTO deparse_test; SET search_path = deparse_test, public; \d+ t1 Foreign table "deparse_test.t1" Column | Type | Collation | Nullable | Default | FDW options | Storage | Stats target | Description --------+----------+-----------+----------+---------+-------------+---------+--------------+------------- a | integer | | not null | | | plain | | b | smallint | | not null | | | plain | | Server: deparse_lookback FDW options: (database 'deparse_test', table_name 't1', engine 'MergeTree') ALTER TABLE t1 ALTER COLUMN b SET DATA TYPE bool; EXPLAIN (VERBOSE, COSTS OFF) SELECT (CASE WHEN b THEN 1 ELSE 2 END) as g1, MAX(a) FROM t1 GROUP BY g1; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- Foreign Scan Output: (CASE WHEN b THEN 1 ELSE 2 END), (max(a)) Relations: Aggregate on (t1) Remote SQL: SELECT CASE WHEN b = 1 THEN toInt32(1) ELSE toInt32(2) END, max(a) FROM deparse_test.t1 GROUP BY (CASE WHEN b = 1 THEN toInt32(1) ELSE toInt32(2) END) (4 rows) SELECT (CASE WHEN b THEN 1 ELSE 2 END) as g1, MAX(a) FROM t1 GROUP BY g1; g1 | max ----+----- 2 | 5 1 | 9 (2 rows) EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM t1 ORDER BY a NULLS FIRST, b LIMIT 3; QUERY PLAN ----------------------------------------------------------------------------------------------------- Foreign Scan on deparse_test.t1 Output: a, b Remote SQL: SELECT a, b FROM deparse_test.t1 ORDER BY a ASC NULLS FIRST, b ASC NULLS LAST LIMIT 3 (3 rows) SELECT * FROM t1 ORDER BY a NULLS FIRST, b LIMIT 3; a | b ---+--- 0 | f 0 | f 0 | f (3 rows) DROP USER MAPPING FOR CURRENT_USER SERVER deparse_lookback; CALL clickhouse_perform('deparse_admin', 'DROP DATABASE deparse_test'); DROP SERVER deparse_lookback CASCADE; NOTICE: drop cascades to foreign table t1