Search code examples
clickhouse

How to convert array to tuple in ClickHouse


I want to use IN operator with tuples:

WHERE (a, b) IN (('my', 'tuple'))

The problem is that I don't have ('my', 'tuple) as a tuple but array. So I have this:

WHERE (a, b) IN (['my', 'tuple'])

(the array is passed by my templating engine that is not very flexible. Actually, what I do is WHERE (a, b) IN (splitByChar('|', 'my|tuple'])).

How can I convert array to tuple? I'm looking for something like toTuple()

WHERE (a, b) IN (toTuple(['my', 'tuple']))

or more precisely:

WHERE (a, b) IN (toTuple(splitByChar('|', 'my|tuple'])))

Solution

  • Try this way:

    SELECT
        ['my', 'tuple'] AS arr,
        arrayMap(x -> (x[1], x[2]), [arr])[1] AS tuple
    
    /*
    ┌─arr────────────┬─tuple──────────┐
    │ ['my','tuple'] │ ('my','tuple') │
    └────────────────┴────────────────┘
    */
    

    or

    select ['my', 'tuple'] AS arr, tupleConcat(tuple(arr[1]), tuple(arr[2])) AS tuple