3

So I'm trying to run the following query against a clickhouse db using a go echo api.

SELECT (SELECT  round(sum(amount),2)  
                FROM activity
                WHERE created_at >= FROM_UNIXTIME(?) AND created_at <= FROM_UNIXTIME(?)
                         AND account_id = ?  AND funnel_id=?  AND event_id=2   ) AS value, (SELECT  round(sum(amount),2)
                FROM activity
                WHERE created_at >= FROM_UNIXTIME(?) AND created_at <= FROM_UNIXTIME(?)
                         AND account_id = ?  AND funnel_id=?  AND event_id=2   ) AS old_value;

The statement runs fine in DataGrip which I was using to test but when running in live code I get the following error: PrepareContext error: cannot get table name from query

Code I'm using to call the query is:

func ExecuteQuery(query string, values []interface{}) *sql.Rows {
    stmt, err := conn.Prepare(query)
    if err != nil {
        log.Println(err)
    }

    rows, err := stmt.Query(values...)
    if err != nil {
        log.Println(err)
    }

    stmt.Close()

    return rows
}

No idea what the issue is.

Tried changing using FROM activity LIMIT 1; at the end of the query, that didn't work - same error. No other ideas

liam2682
  • 41
  • 2
  • The clickhouse driver's `Prepare` method seems to be intended for INSERT queries ONLY (see [issue 960](https://github.com/ClickHouse/clickhouse-go/issues/960)), and this [piece of code](https://github.com/ClickHouse/clickhouse-go/blob/main/conn_http_batch.go#L38-L40) shows that that specific error is returned when the query doesn't match the expected INSERT regexp. – mkopriva Aug 02 '23 at 12:50
  • @mkopriva jesus thats annoying, thanks for the heads up – liam2682 Aug 02 '23 at 13:08

0 Answers0