# Alerts trying to change integers to toDate() Using Clickhouse DB

**URL:** <https://community.grafana.com/t/alerts-trying-to-change-integers-to-todate-using-clickhouse-db/123633>\
**Category:** Grafana\
**Tags:** alert-rule, clickhouse\
**Created:** [June 10, 2024, 1:29pm UTC](https://community.grafana.com/t/alerts-trying-to-change-integers-to-todate-using-clickhouse-db/123633 "2024-06-10T13:29:43Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![torque](https://avatars.discourse-cdn.com/v4/letter/t/da6949/32.png) [@torque](https://community.grafana.com/u/torque)\
**Post date:** [June 10, 2024, 1:29pm UTC](https://community.grafana.com/t/alerts-trying-to-change-integers-to-todate-using-clickhouse-db/123633/1 "2024-06-10T13:29:43Z")

</div>

I recently upgraded my grafana from v8.5.6-1 to v11.0.0-1 and lost my alert rules.  
I am now re-creating them and I am having some problems adding an alert rule, where I get the following error:

```auto
[sse.dataQueryError] failed to execute query [A]: Code: 46. DB::Exception: Unknown function toDATE. Maybe you meant: ['toDate','toDate32']: While processing SELECT (intDiv(toUInt32(record_datetime), 1800) * 1800) * 1000 AS t, avg(mos) FROM hepic_data.sip_transaction_call WHERE (record_datetime >= toDateTime(1717420888)) AND (record_datetime <= toDateTime(1718025688)) AND (captid IN (9174, 9174)) AND (mos > toDATE(1718025688)) AND (duration > toDATE(1718025688)) GROUP BY t ORDER BY t ASC LIMIT 5000. (UNKNOWN_FUNCTION) (version 21.9.4.35 (official build))

```

Here is the original query:

```auto
SELECT
    $timeSeries as t,
    avg(mos)
FROM $table

WHERE
    $timeFilter
    AND captid in (9174,9174) AND mos > 1 AND duration > 1
GROUP BY
    t
ORDER BY t

LIMIT 5000

```

It seems to be trying to convert certain integers to date using ‘toDATE()’  
The Data source is a Clickhouse Database and I am using Grafana version 11.0.0

Here is a screen shot if it helps with context:

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/3/8/38531ca22e46eb9c64a2f577d416e5d046e03c84.png)

I have confirmed the query works when the ‘mos’ and ‘duration’ WHERE clauses are integers:  
 ![image](https://us1.discourse-cdn.com/grafana/original/3X/6/a/6ac32c34467d337420a76b46102ebc6de441019a.png)

Please let me know if any further information is needed.  
Any assistance with this will be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![yosiasz](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/yosiasz/32/45847_2.png) [@yosiasz](https://community.grafana.com/u/yosiasz)\
**Post date:** [June 10, 2024, 1:33pm UTC](https://community.grafana.com/t/alerts-trying-to-change-integers-to-todate-using-clickhouse-db/123633/2 "2024-06-10T13:33:00Z")

</div>

what happens if you change things to

```auto
SELECT $timeSeries as time,
       avg(mos) as value
 FROM $table
WHERE $timeFilter
  AND captid in (9174,9174) 
  AND mos > 1 
  AND duration > 1
GROUP BY 1
ORDER BY 1

LIMIT 5000
```

---

<div class="post-metadata">

**Author:** ![torque](https://avatars.discourse-cdn.com/v4/letter/t/da6949/32.png) [@torque](https://community.grafana.com/u/torque)\
**Post date:** [June 10, 2024, 1:38pm UTC](https://community.grafana.com/t/alerts-trying-to-change-integers-to-todate-using-clickhouse-db/123633/3 "2024-06-10T13:38:58Z")

</div>

Hi yosiasz, I am busy testing. It seems to not update correctly when I make changes.

---

<div class="post-metadata">

**Author:** ![torque](https://avatars.discourse-cdn.com/v4/letter/t/da6949/32.png) [@torque](https://community.grafana.com/u/torque)\
**Post date:** [June 10, 2024, 1:54pm UTC](https://community.grafana.com/t/alerts-trying-to-change-integers-to-todate-using-clickhouse-db/123633/4 "2024-06-10T13:54:10Z")

</div>

I still seem to get the “Unknown function toDATE” error:

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/8/1/81a68a7758f83b45db2cb073194de24a6221417c.png)

---

<div class="post-metadata">

**Author:** ![yosiasz](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/yosiasz/32/45847_2.png) [@yosiasz](https://community.grafana.com/u/yosiasz)\
**Post date:** [June 10, 2024, 2:03pm UTC](https://community.grafana.com/t/alerts-trying-to-change-integers-to-todate-using-clickhouse-db/123633/5 "2024-06-10T14:03:49Z")

</div>

look at the generated sql in that error.

`intDiv(toUINt32` part for `$timeSeries` what is going on there? Can you copy and run that whole sql statement in ClickHouse and see what you get?

---

<div class="post-metadata">

**Author:** ![torque](https://avatars.discourse-cdn.com/v4/letter/t/da6949/32.png) [@torque](https://community.grafana.com/u/torque)\
**Post date:** [June 10, 2024, 2:14pm UTC](https://community.grafana.com/t/alerts-trying-to-change-integers-to-todate-using-clickhouse-db/123633/6 "2024-06-10T14:14:17Z")

</div>

if I run the generated query ‘as is’ I get the same error:

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/a/8/a81fb57b6a95ba8f5212a939823c96792fb47f00.png)

But if I correct the ‘mos’ and ‘duration’ WHERE clauses it works fine:  
 ![image](https://us1.discourse-cdn.com/grafana/original/3X/3/1/318d7a7291730f83ce87afdf12859f5a9fbfef67.png)

---

<div class="post-metadata">

**Author:** ![yosiasz](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/yosiasz/32/45847_2.png) [@yosiasz](https://community.grafana.com/u/yosiasz)\
**Post date:** [June 10, 2024, 2:18pm UTC](https://community.grafana.com/t/alerts-trying-to-change-integers-to-todate-using-clickhouse-db/123633/7 "2024-06-10T14:18:51Z")

</div>

So in your grafana query you do have it as

`mos > 1` but somehow it is being converted to `mos > toDate(1718028616)`

1718028616 = Mon Jun 10 2024 14:10:16 GMT+0000

Why is that? What data type is `mos` ?

---

<div class="post-metadata">

**Author:** ![torque](https://avatars.discourse-cdn.com/v4/letter/t/da6949/32.png) [@torque](https://community.grafana.com/u/torque)\
**Post date:** [June 10, 2024, 2:28pm UTC](https://community.grafana.com/t/alerts-trying-to-change-integers-to-todate-using-clickhouse-db/123633/8 "2024-06-10T14:28:08Z")

</div>

In the DB  
mos=Int16  
duration=Int32

Output of working query  
mos=Float64 #this is due to avg()

---

<div class="post-metadata">

**Author:** ![yosiasz](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/yosiasz/32/45847_2.png) [@yosiasz](https://community.grafana.com/u/yosiasz)\
**Post date:** [June 10, 2024, 2:30pm UTC](https://community.grafana.com/t/alerts-trying-to-change-integers-to-todate-using-clickhouse-db/123633/9 "2024-06-10T14:30:18Z")

</div>

> [@torque](#):
>
> mos=Int16  
> duration=Int32

are there functions to make `mos > int16(1)` otherwise as you can see it is converting that 1 to `toDate()` you need to fix that so that it does not convert it to date, that is your issue. could be a bug in the clickhouse plugin. you have not yet explained why it does that odd conversion

---

<div class="post-metadata">

**Author:** ![torque](https://avatars.discourse-cdn.com/v4/letter/t/da6949/32.png) [@torque](https://community.grafana.com/u/torque)\
**Post date:** [June 10, 2024, 3:08pm UTC](https://community.grafana.com/t/alerts-trying-to-change-integers-to-todate-using-clickhouse-db/123633/10 "2024-06-10T15:08:01Z")

</div>

Yeah, I have no idea why it is trying to convert to toDate(). I will see if I can get some support on the Clickhouse plugin.
