Skip to main content
Skip to main content

groupArrayInsertAt

groupArrayInsertAt

Introduced in: v1.1

Inserts a value into the array at the specified position.

If in one query several values are inserted into the same position, the function behaves in the following ways:

  • If a query is executed in a single thread, the first one of the inserted values is used.
  • If a query is executed in multiple threads, the resulting value is an undetermined one of the inserted values.

Syntax

groupArrayInsertAt(default_x, size)([x, pos])

Parameters

  • default_x — Optional. Default value for substituting in empty positions. Any
  • size — Optional. Length of the resulting array. When using this parameter, the default value default_x must be specified. UInt32

Arguments

  • x — Value to be inserted. Any
  • pos — Position at which the specified element x is to be inserted. Index numbering in the array starts from zero. UInt32

Returned value

Returns an array with inserted values. Array

Examples

Basic usage without parameters

SELECT groupArrayInsertAt(toString(number), number * 2) FROM numbers(5);
┌─groupArrayInsertAt(toString(number), multiply(number, 2))─┐
│ ['0','','1','','2','','3','','4']                         │
└───────────────────────────────────────────────────────────┘

Usage with default value parameter

SELECT groupArrayInsertAt('-')(toString(number), number * 2) FROM numbers(5);
┌─groupArrayInsertAt('-')(toString(number), multiply(number, 2))─┐
│ ['0','-','1','-','2','-','3','-','4']                          │
└────────────────────────────────────────────────────────────────┘

Usage with default value and size parameters

SELECT groupArrayInsertAt('-', 5)(toString(number), number * 2) FROM numbers(5);
┌─groupArrayInsertAt('-', 5)(toString(number), multiply(number, 2))─┐
│ ['0','-','1','-','2']                                             │
└───────────────────────────────────────────────────────────────────┘

Multi-threaded insertion into same position

SELECT groupArrayInsertAt(number, 0) FROM numbers_mt(10) SETTINGS max_block_size = 1;
┌─groupArrayInsertAt(number, 0)─┐
│ [7]                           │
└───────────────────────────────┘