# bytes_concat_agg

Concatenates BYTES chunks in aggregate input order, preserving an aggregate ORDER BY clause when supplied.

Canonical HTML: <https://determica.com/docs/functions/bytes_concat_agg>

- Kind: scalar function
- Category: [EVM utilities](/docs/category/evm-utilities.md)
- Tags: Bytes, Aggregate, Deterministic, Types

Concatenates BYTES chunks in aggregate input order, preserving an aggregate ORDER BY clause when supplied.

## Overload 1: bytes_concat_agg(BYTES)

```sql
bytes_concat_agg(BYTES)
```

Returns the ordered concatenation of BYTES chunks. Empty groups and groups containing NULL return NULL.

- Kind: scalar
- Execution context: Offline
- Behavior: Transform
- Execution modes: Offline
- Input shape: Scalar Arguments
- Planning contract: Runtime
- Cardinality: One Per Input
- Determinism: Deterministic
- Return shape: Scalar
- Schema stability: Fixed
- Stability: Stable
- Side effects: None documented

### Inputs

- `chunk` (BYTES; required; positional) — BYTES fragment. Supply aggregate ORDER BY for deterministic framing; any NULL chunk makes the group result NULL.

### Returns

Concatenated bytes

Returns BYTES in aggregate order. Empty groups and groups containing at least one NULL return NULL.

- `bytes` (BYTES)

Concatenated bytes. Returns BYTES in aggregate order. Empty groups and groups containing at least one NULL return NULL.

## Additional examples

```sql
-- Build a WETH / 0.3% / USDC Uniswap V3 packed path
SELECT hex(bytes_concat_agg(chunk ORDER BY position)) AS path
FROM (
  VALUES
    (1, '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'::ADDRESS::BYTES),
    (2, evm_pack_uint_be(3000::UINT256, 3)),
    (3, '0xA0b86991c6218b36c1d19d4a2e9eb0ce3606eb48'::ADDRESS::BYTES)
) AS pieces(position, chunk);
-- => [{"path":"C02AAA39B223FE8D0A0E5C4F27EAD9083C756CC2000BB8A0B86991C6218B36C1D19D4A2E9EB0CE3606EB48"}]
```

```sql
-- A NULL fragment poisons the aggregate instead of being skipped
SELECT hex(bytes_concat_agg(chunk ORDER BY position)) AS encoded
FROM (
  VALUES (1, '0x01'::BYTES), (2, NULL::BYTES), (3, '0x02'::BYTES)
) AS pieces(position, chunk);
-- => [{"encoded":null}]
```
