Skip to content

Commit cebc59f

Browse files
committed
Map count() to BIGINT
When parsing the type of materialized view column, detect `AggregateFunction(count)` and return its data type, `BIGINT`. Without this change, the import would fail with the error, `clickhouse_fdw: expected comma in AggregateFunction`. But since `count()` is valid just return its type.
1 parent fe2d232 commit cebc59f

4 files changed

Lines changed: 14 additions & 4 deletions

File tree

src/pglink.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,8 +817,12 @@ parse_type(char *colname, char *typepart, bool *is_nullable, List **options)
817817
strncmp(typepart, "SimpleAggregateFunction", strlen("SimpleAggregateFunction")) == 0)
818818
{
819819
char *pos2 = strstr(pos, ",");
820-
if (pos2 == NULL)
820+
if (pos2 == NULL) {
821+
// Detect COUNT with no params.
822+
if (strncmp(insidebr, "count", strlen("count")) == 0)
823+
return "BIGINT";
821824
elog(ERROR, "clickhouse_fdw: expected comma in AggregateFunction");
825+
}
822826

823827
char *func = pnstrdup(pos + 1, strstr(pos + 1, ",") - pos - 1);
824828

test/expected/engines.out

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ SELECT clickhouse_raw_query('
8383
create table engines_test.t4 (a int,
8484
b AggregateFunction(sum, Int32),
8585
c AggregateFunction(sumMap, Array(Int32), Array(Int32)),
86-
d SimpleAggregateFunction(sum, Int64))
86+
d SimpleAggregateFunction(sum, Int64),
87+
e AggregateFunction(count))
8788
engine = AggregatingMergeTree()
8889
order by a');
8990
clickhouse_raw_query
@@ -162,11 +163,13 @@ FDW options: (database 'engines_test', table_name 't3_aggr', engine 'Materialize
162163
b | integer | | not null | | (aggregatefunction 'sum') | plain | |
163164
c | integer | | not null | | (aggregatefunction '1') | plain | |
164165
d | bigint | | not null | | (simpleaggregatefunction 'sum') | plain | |
166+
e | bigint | | not null | | | plain | |
165167
Not-null constraints:
166168
"t4_a_not_null" NOT NULL "a"
167169
"t4_b_not_null" NOT NULL "b"
168170
"t4_c_not_null" NOT NULL "c"
169171
"t4_d_not_null" NOT NULL "d"
172+
"t4_e_not_null" NOT NULL "e"
170173
Server: engines_loopback
171174
FDW options: (database 'engines_test', table_name 't4', engine 'AggregatingMergeTree')
172175

test/expected/engines_1.out

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ SELECT clickhouse_raw_query('
8383
create table engines_test.t4 (a int,
8484
b AggregateFunction(sum, Int32),
8585
c AggregateFunction(sumMap, Array(Int32), Array(Int32)),
86-
d SimpleAggregateFunction(sum, Int64))
86+
d SimpleAggregateFunction(sum, Int64),
87+
e AggregateFunction(count))
8788
engine = AggregatingMergeTree()
8889
order by a');
8990
clickhouse_raw_query
@@ -146,6 +147,7 @@ FDW options: (database 'engines_test', table_name 't3_aggr', engine 'Materialize
146147
b | integer | | not null | | (aggregatefunction 'sum') | plain | |
147148
c | integer | | not null | | (aggregatefunction '1') | plain | |
148149
d | bigint | | not null | | (simpleaggregatefunction 'sum') | plain | |
150+
e | bigint | | not null | | | plain | |
149151
Server: engines_loopback
150152
FDW options: (database 'engines_test', table_name 't4', engine 'AggregatingMergeTree')
151153

test/sql/engines.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ SELECT clickhouse_raw_query('
4242
create table engines_test.t4 (a int,
4343
b AggregateFunction(sum, Int32),
4444
c AggregateFunction(sumMap, Array(Int32), Array(Int32)),
45-
d SimpleAggregateFunction(sum, Int64))
45+
d SimpleAggregateFunction(sum, Int64),
46+
e AggregateFunction(count))
4647
engine = AggregatingMergeTree()
4748
order by a');
4849

0 commit comments

Comments
 (0)