From 07c2b093ed0981a9819f86d2fc970324d96fb86c Mon Sep 17 00:00:00 2001 From: alrocar Date: Tue, 16 Nov 2021 16:24:16 +0100 Subject: [PATCH 1/4] rebase upstream --- src/adjust.c | 6 ++++++ src/deparse.c | 10 ++++++++++ src/include/clickhousedb_fdw.h | 1 + tests/expected/functions.out | 16 ++++++++++++++++ tests/sql/functions.sql | 3 +++ 5 files changed, 36 insertions(+) diff --git a/src/adjust.c b/src/adjust.c index 05322ce..a4517dc 100644 --- a/src/adjust.c +++ b/src/adjust.c @@ -93,6 +93,7 @@ CustomObjectDef *chfdw_check_for_custom_function(Oid funcid) case 868: // strpos case F_BTRIM: case F_BTRIM1: + case F_TO_TIMESTAMP: special_builtin = true; break; default: @@ -151,6 +152,11 @@ CustomObjectDef *chfdw_check_for_custom_function(Oid funcid) case 868: { strcpy(entry->custom_name, "position"); + } + case F_TO_TIMESTAMP: + { + entry->cf_type = CF_TO_TIMESTAMP; + entry->custom_name[0] = '\1'; break; } } diff --git a/src/deparse.c b/src/deparse.c index 2c3c52a..c9e101b 100644 --- a/src/deparse.c +++ b/src/deparse.c @@ -2359,6 +2359,16 @@ deparseFuncExpr(FuncExpr *node, deparse_expr_cxt *context) * Normal function: display as proname(args). */ cdef = appendFunctionName(node->funcid, context); + + if (cdef && cdef->cf_type == CF_TO_TIMESTAMP) + { + appendStringInfoString(buf, "parseDateTimeBestEffortOrNull"); + appendStringInfoChar(buf, '('); + deparseExpr(list_nth(node->args, 0), context); + appendStringInfoChar(buf, ')'); + return; + } + if (cdef && cdef->cf_type == CF_DATE_TRUNC) { Const *arg = (Const *) linitial(node->args); diff --git a/src/include/clickhousedb_fdw.h b/src/include/clickhousedb_fdw.h index 24145f3..3bc437e 100644 --- a/src/include/clickhousedb_fdw.h +++ b/src/include/clickhousedb_fdw.h @@ -268,6 +268,7 @@ typedef enum { CF_DATE_PART, /* date_part function */ CF_TIMESTAMPTZ_PL_INTERVAL, /* timestamptz + interval */ CF_TIMEZONE, /* timezone */ + CF_TO_TIMESTAMP, CF_COUNTRY_TYPE, CF_AJTIME_PL_INTERVAL, CF_AJTIME_MI_INTERVAL, diff --git a/tests/expected/functions.out b/tests/expected/functions.out index 2450b6d..999597f 100644 --- a/tests/expected/functions.out +++ b/tests/expected/functions.out @@ -233,6 +233,22 @@ SELECT uniq_exact(a) FILTER(WHERE b>1) FROM t1; 1 (1 row) +EXPLAIN (VERBOSE, COSTS OFF) SELECT c as d1 FROM t1 WHERE c >= to_timestamp('2019-01-01 00:00:00.000000', 'YYYY-MM-DD HH24:MI:SS.US') GROUP BY d1 ORDER BY d1; + QUERY PLAN +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + Foreign Scan + Output: c + Relations: Aggregate on (t1) + Remote SQL: SELECT c FROM test.tt WHERE ((c >= parseDateTimeBestEffortOrNull('2019-01-01 00:00:00.000000'))) GROUP BY c ORDER BY c ASC +(4 rows) + +SELECT c as d1 FROM t1 WHERE c >= to_timestamp('2019-01-01 00:00:00.000000', 'YYYY-MM-DD HH24:MI:SS.US') GROUP BY d1 ORDER BY d1; + d1 +------------------------ + 2019-01-01 10:00:00 + 2019-01-02 10:00:00 +(2 rows) + EXPLAIN (VERBOSE, COSTS OFF) SELECT date_trunc('dAy', c at time zone 'UTC') as d1 FROM t1 GROUP BY d1 ORDER BY d1; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- diff --git a/tests/sql/functions.sql b/tests/sql/functions.sql index d59f0f9..9898f56 100644 --- a/tests/sql/functions.sql +++ b/tests/sql/functions.sql @@ -78,6 +78,9 @@ SELECT uniq_exact(a) FROM t1; EXPLAIN (VERBOSE, COSTS OFF) SELECT uniq_exact(a) FILTER(WHERE b>1) FROM t1; SELECT uniq_exact(a) FILTER(WHERE b>1) FROM t1; +EXPLAIN (VERBOSE, COSTS OFF) SELECT c as d1 FROM t1 WHERE c >= to_timestamp('2019-01-01 00:00:00.000000', 'YYYY-MM-DD HH24:MI:SS.US') GROUP BY d1 ORDER BY d1; +SELECT c as d1 FROM t1 WHERE c >= to_timestamp('2019-01-01 00:00:00.000000', 'YYYY-MM-DD HH24:MI:SS.US') GROUP BY d1 ORDER BY d1; + EXPLAIN (VERBOSE, COSTS OFF) SELECT date_trunc('dAy', c at time zone 'UTC') as d1 FROM t1 GROUP BY d1 ORDER BY d1; SELECT date_trunc('day', c at time zone 'UTC') as d1 FROM t1 GROUP BY d1 ORDER BY d1; From accd915fe53893c16a4d51a051a50f0b08b6cee0 Mon Sep 17 00:00:00 2001 From: alrocar Date: Tue, 21 Sep 2021 23:44:04 +0200 Subject: [PATCH 2/4] fix out --- tests/expected/functions.out | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/expected/functions.out b/tests/expected/functions.out index 999597f..667e86d 100644 --- a/tests/expected/functions.out +++ b/tests/expected/functions.out @@ -239,7 +239,7 @@ EXPLAIN (VERBOSE, COSTS OFF) SELECT c as d1 FROM t1 WHERE c >= to_timestamp('201 Foreign Scan Output: c Relations: Aggregate on (t1) - Remote SQL: SELECT c FROM test.tt WHERE ((c >= parseDateTimeBestEffortOrNull('2019-01-01 00:00:00.000000'))) GROUP BY c ORDER BY c ASC + Remote SQL: SELECT c FROM regression.t1 WHERE ((c >= parseDateTimeBestEffortOrNull('2019-01-01 00:00:00.000000'))) GROUP BY c ORDER BY c ASC (4 rows) SELECT c as d1 FROM t1 WHERE c >= to_timestamp('2019-01-01 00:00:00.000000', 'YYYY-MM-DD HH24:MI:SS.US') GROUP BY d1 ORDER BY d1; From 1b6ad94f9dbc47c0d73791266f02d45a228f2580 Mon Sep 17 00:00:00 2001 From: alrocar Date: Wed, 22 Sep 2021 11:07:24 +0200 Subject: [PATCH 3/4] support older versions of CH --- src/deparse.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/deparse.c b/src/deparse.c index c9e101b..72ca941 100644 --- a/src/deparse.c +++ b/src/deparse.c @@ -2419,7 +2419,7 @@ deparseFuncExpr(FuncExpr *node, deparse_expr_cxt *context) { appendStringInfoString(buf, "(toDateTime64("); deparseExpr(list_nth(node->args, 1), context); - appendStringInfoString(buf, ", 1))"); + appendStringInfoString(buf, ", 1, 'UTC'))"); } else { From bc2e7ffc2af79a18f0945da1f829b49c24497df8 Mon Sep 17 00:00:00 2001 From: alrocar Date: Tue, 16 Nov 2021 20:12:46 +0100 Subject: [PATCH 4/4] fix merge --- src/adjust.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/adjust.c b/src/adjust.c index a4517dc..a83bbf7 100644 --- a/src/adjust.c +++ b/src/adjust.c @@ -152,6 +152,7 @@ CustomObjectDef *chfdw_check_for_custom_function(Oid funcid) case 868: { strcpy(entry->custom_name, "position"); + break; } case F_TO_TIMESTAMP: {