Time series functions

**Syntax**

```
timeSeriesRemoveAllTagsExcept(group, tags_to_keep)
```

**Arguments**

- `group` — A group of tags. [`UInt64`](/docs/sql-reference/data-types/int-uint)
- `tags_to_keep` — The names of tags to keep in the group. [`Array(String)`](/docs/sql-reference/data-types/array)

**Returned value**

A new group of tags with only the specified tags kept. [`UInt64`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Example**

```
SELECT timeSeriesTagsToGroup([('region', 'eu'), ('env', 'dev')], '__name__', 'http_requests_count') AS group,
       timeSeriesRemoveAllTagsExcept(group, ['env']) AS result_group,
       timeSeriesGroupToTags(result_group)
```

```
┌─group─┬─result_group─┬─timeSeriesGroupToTags(result_group)─┐
│     1 │            2 │ [('env','dev')]                     │
└───────┴──────────────┴─────────────────────────────────────┘
```

## timeSeriesRemoveTag[​](#timeSeriesRemoveTag "Direct link to timeSeriesRemoveTag")

Introduced in: v26.1.0

Removes a specified tag from a group of tags.
If there is no such tag in the group then the group is returned unchanged.
See also function [timeSeriesRemoveTags()](/docs/sql-reference/functions/time-series-functions#timeSeriesRemoveTags),
[timeSeriesRemoveAllTagsExcept()](/docs/sql-reference/functions/time-series-functions#timeSeriesRemoveAllTagsExcept).

**Syntax**

```
timeSeriesRemoveTag(group, tag_to_remove)
```

**Arguments**

- `group` — A group of tags. [`UInt64`](/docs/sql-reference/data-types/int-uint)
- `tag_to_remove` — The name of a tag to remove from the group. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

A new group of tags without the specified tag. [`UInt64`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Example**

```
SELECT timeSeriesTagsToGroup([('region', 'eu'), ('env', 'dev')], '__name__', 'http_requests_count') AS group_of_3,
       timeSeriesRemoveTag(group_of_3, '__name__') AS group_of_2,
       timeSeriesGroupToTags(group_of_2),
       timeSeriesRemoveTag(group_of_2, 'env') AS group_of_1,
       timeSeriesGroupToTags(group_of_1),
       timeSeriesRemoveTag(group_of_1, 'region') AS empty_group,
       timeSeriesGroupToTags(empty_group)
```

```
┌─group_of_3─┬─group_of_2─┬─timeSeriesGroupToTags(group_of_2)─┬─group_of_1─┬─timeSeriesGroupToTags(group_of_1)─┬─empty_group─┬─timeSeriesGroupToTags(empty_group)─┐
│          1 │          2 │ [('env','dev'),('region','eu')]   │          3 │ [('region','eu')]                 │           0 │ []                                 │
└────────────┴────────────┴───────────────────────────────────┴────────────┴───────────────────────────────────┴─────────────┴────────────────────────────────────┘
```

## timeSeriesRemoveTags[​](#timeSeriesRemoveTags "Direct link to timeSeriesRemoveTags")

Introduced in: v26.1.0

Removes specified tags from a group of tags.
If some of the specified tags are not in the group of tags the function ignores them.
See also function [timeSeriesRemoveTag()](/docs/sql-reference/functions/time-series-functions#timeSeriesRemoveTag),
[timeSeriesRemoveAllTagsExcept()](/docs/sql-reference/functions/time-series-functions#timeSeriesRemoveAllTagsExcept).

**Syntax**

```
timeSeriesRemoveTags(group, tags_to_remove)
```

**Arguments**

- `group` — A group of tags. [`UInt64`](/docs/sql-reference/data-types/int-uint)
- `tags_to_remove` — The names of tags to remove from the group. [`Array(String)`](/docs/sql-reference/data-types/array)

**Returned value**

A new group of tags without the specified tags. [`UInt64`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Example**

```
SELECT timeSeriesTagsToGroup([('region', 'eu'), ('env', 'dev')], '__name__', 'http_requests_count') AS group_of_3,
       timeSeriesRemoveTags(group_of_3, ['env', 'region']) AS group_of_1,
       timeSeriesGroupToTags(group_of_1),
       timeSeriesRemoveTags(group_of_1, ['__name__', 'nonexistent']) AS empty_group,
       timeSeriesGroupToTags(empty_group)
```

```
┌─group_of_3─┬─group_of_1─┬─timeSeriesGroupToTags(group_of_1)────┬─empty_group─┬─timeSeriesGroupToTags(empty_group)─┐
│          1 │          2 │ [('__name__','http_requests_count')] │           0 │ []                                 │
└────────────┴────────────┴──────────────────────────────────────┴─────────────┴────────────────────────────────────┘
```

## timeSeriesReplaceTag[​](#timeSeriesReplaceTag "Direct link to timeSeriesReplaceTag")

Introduced in: v26.1.0

Matches the regular expression `regex` against the value of the tag `src_tag`.
If it matches, the value of the tag `dest_tag` in the returned group will be the expansion of `replacement`,
together with the original tags in the input.
This function mimics the logic of the prometheus function
[label\_replace()](https://prometheus.io/docs/prometheus/latest/querying/functions/#label_replace).

**Syntax**

```
timeSeriesReplaceTag(group, dest_tag, replacement, src_tag, regex)
```

**Arguments**

- `group` — A group of tags. [`UInt64`](/docs/sql-reference/data-types/int-uint)
- `dest_tag` — The name of a destination tag to get the result group. [`String`](/docs/sql-reference/data-types/string)
- `replacement` — A replacement pattern, can contain 1,1, 1,2 or $name to refer capturing groups in the regular expression 'regex'. [`String`](/docs/sql-reference/data-types/string)
- `src_tag` — The name of a tag which value is used to match the regular expression 'regex'. [`String`](/docs/sql-reference/data-types/string)
- `regex` — A regular expression. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

A new group of tags with maybe `dest_tag` added. [`UInt64`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Example**

```
SELECT timeSeriesTagsToGroup([('__name__', 'up'), ('job', 'api-server'), ('service', 'a:c')]) AS group,
       timeSeriesReplaceTag(group, 'foo', '$1', 'service', '(.*):.*') AS result_group,
       timeSeriesGroupToTags(result_group)
```

```
┌─group─┬─result_group─┬─timeSeriesGroupToTags(result_group)────────────────────────────────────┐
│     1 │            2 │ [('__name__','up'),('foo','a'),('job','api-server'),('service','a:c')] │
└───────┴──────────────┴────────────────────────────────────────────────────────────────────────┘
```

## timeSeriesStoreTags[​](#timeSeriesStoreTags "Direct link to timeSeriesStoreTags")

Introduced in: v25.8.0

Stores in the query context a mapping between a specified identifier of a time series and a set of tags.
Functions [timeSeriesIdToTags()](/docs/sql-reference/functions/time-series-functions#timeSeriesIdToTags)
and [timeSeriesIdToGroup()](/docs/sql-reference/functions/time-series-functions#timeSeriesIdToGroup)
can be used to access this mapping later during the query execution.

**Syntax**

```
timeSeriesStoreTags(id, tags_array, separate_tag_name_1, separate_tag_value_1, ...)
```

**Arguments**

- `id` — Identifier of a time series. [`UInt64`](/docs/sql-reference/data-types/int-uint) or [`UInt128`](/docs/sql-reference/data-types/int-uint) or [`UUID`](/docs/sql-reference/data-types/uuid) or [`FixedString(16)`](/docs/sql-reference/data-types/fixedstring)
- `tags_array` — Array of pairs (tag\_name, tag\_value). [`Array(Tuple(String, String))`](/docs/sql-reference/data-types/array) or [`NULL`](/docs/sql-reference/syntax#null)
- `separate_tag_name_i` — The name of a tag. [`String`](/docs/sql-reference/data-types/string) or [`FixedString`](/docs/sql-reference/data-types/fixedstring)
- `separate_tag_value_i` — The value of a tag. [`String`](/docs/sql-reference/data-types/string) or [`FixedString`](/docs/sql-reference/data-types/fixedstring) or [`Nullable(String)`](/docs/sql-reference/data-types/nullable)

**Returned value**

Returns the identifier of a time series (i.e. just the first argument).

**Examples**

**Example**

```
SELECT 8374283493092 AS id,
       timeSeriesStoreTags(id, [('region', 'eu'), ('env', 'dev')], '__name__', 'http_requests_count') AS same_id,
       throwIf(same_id != id),
       timeSeriesIdToTags(same_id),
       timeSeriesGroupToTags(timeSeriesIdToGroup(same_id))
```