> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-fix-nav-issues.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> Documentação do formato BSONEachRow

# BSONEachRow

| Entrada | Saída | Alias |
| ------- | ----- | ----- |
| ✔       | ✔     |       |

<div id="description">
  ## Descrição
</div>

O formato `BSONEachRow` interpreta os dados como uma sequência de documentos Binary JSON (BSON), sem nenhum separador entre eles.
Cada linha é formatada como um único documento, e cada coluna é formatada como um único campo do documento BSON, com o nome da coluna como chave.

<div id="data-types-matching">
  ## Correspondência entre tipos de dados
</div>

Para a saída, é usada a seguinte correspondência entre os tipos ClickHouse e os tipos BSON:

| Tipo ClickHouse                                                                                     | Tipo BSON                                                                                                                       |
| --------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
| [Bool](/pt-BR/reference/data-types/boolean)                                                         | `\x08` boolean                                                                                                                  |
| [Int8/UInt8](/pt-BR/reference/data-types/int-uint)/[Enum8](/pt-BR/reference/data-types/enum)        | `\x10` int32                                                                                                                    |
| [Int16/UInt16](/pt-BR/reference/data-types/int-uint)/[Enum16](/pt-BR/reference/data-types/enum)     | `\x10` int32                                                                                                                    |
| [Int32](/pt-BR/reference/data-types/int-uint)                                                       | `\x10` int32                                                                                                                    |
| [UInt32](/pt-BR/reference/data-types/int-uint)                                                      | `\x12` int64                                                                                                                    |
| [Int64/UInt64](/pt-BR/reference/data-types/int-uint)                                                | `\x12` int64                                                                                                                    |
| [Float32/Float64](/pt-BR/reference/data-types/float)                                                | `\x01` double                                                                                                                   |
| [Date](/pt-BR/reference/data-types/date)/[Date32](/pt-BR/reference/data-types/date32)               | `\x10` int32                                                                                                                    |
| [DateTime](/pt-BR/reference/data-types/datetime)                                                    | `\x12` int64                                                                                                                    |
| [DateTime64](/pt-BR/reference/data-types/datetime64)                                                | `\x09` datetime                                                                                                                 |
| [Decimal32](/pt-BR/reference/data-types/decimal)                                                    | `\x10` int32                                                                                                                    |
| [Decimal64](/pt-BR/reference/data-types/decimal)                                                    | `\x12` int64                                                                                                                    |
| [Decimal128](/pt-BR/reference/data-types/decimal)                                                   | `\x05` binary, `\x00` subtipo binário, tamanho = 16                                                                             |
| [Decimal256](/pt-BR/reference/data-types/decimal)                                                   | `\x05` binary, `\x00` subtipo binário, tamanho = 32                                                                             |
| [Int128/UInt128](/pt-BR/reference/data-types/int-uint)                                              | `\x05` binary, `\x00` subtipo binário, tamanho = 16                                                                             |
| [Int256/UInt256](/pt-BR/reference/data-types/int-uint)                                              | `\x05` binary, `\x00` subtipo binário, tamanho = 32                                                                             |
| [String](/pt-BR/reference/data-types/string)/[FixedString](/pt-BR/reference/data-types/fixedstring) | `\x05` binary, `\x00` subtipo binário ou \x02 string se a configuração output\_format\_bson\_string\_as\_string estiver ativada |
| [UUID](/pt-BR/reference/data-types/uuid)                                                            | `\x05` binary, `\x04` uuid subtipo binário, tamanho = 16                                                                        |
| [Array](/pt-BR/reference/data-types/array)                                                          | `\x04` array                                                                                                                    |
| [Tuple](/pt-BR/reference/data-types/tuple)                                                          | `\x04` array                                                                                                                    |
| [Named Tuple](/pt-BR/reference/data-types/tuple)                                                    | `\x03` documento                                                                                                                |
| [Map](/pt-BR/reference/data-types/map)                                                              | `\x03` documento                                                                                                                |
| [IPv4](/pt-BR/reference/data-types/ipv4)                                                            | `\x10` int32                                                                                                                    |
| [IPv6](/pt-BR/reference/data-types/ipv6)                                                            | `\x05` binary, `\x00` subtipo binário                                                                                           |

Para a entrada, é usada a seguinte correspondência entre os tipos BSON e os tipos ClickHouse:

| Tipo BSON                                     | Tipo do ClickHouse                                                                                                                                                                              |
| --------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `\x01` double                                 | [Float32/Float64](/pt-BR/reference/data-types/float)                                                                                                                                            |
| `\x02` string                                 | [String](/pt-BR/reference/data-types/string)/[FixedString](/pt-BR/reference/data-types/fixedstring)                                                                                             |
| `\x03` documento                              | [Map](/pt-BR/reference/data-types/map)/[Named Tuple](/pt-BR/reference/data-types/tuple)                                                                                                         |
| `\x04` array                                  | [Array](/pt-BR/reference/data-types/array)/[Tuple](/pt-BR/reference/data-types/tuple)                                                                                                           |
| `\x05` binário, subtipo binário `\x00`        | [String](/pt-BR/reference/data-types/string)/[FixedString](/pt-BR/reference/data-types/fixedstring)/[IPv6](/pt-BR/reference/data-types/ipv6)                                                    |
| `\x05` binário, subtipo binário antigo `\x02` | [String](/pt-BR/reference/data-types/string)/[FixedString](/pt-BR/reference/data-types/fixedstring)                                                                                             |
| `\x05` binário, subtipo UUID antigo `\x03`    | [UUID](/pt-BR/reference/data-types/uuid)                                                                                                                                                        |
| `\x05` binário, subtipo UUID `\x04`           | [UUID](/pt-BR/reference/data-types/uuid)                                                                                                                                                        |
| `\x07` ObjectId                               | [String](/pt-BR/reference/data-types/string)/[FixedString](/pt-BR/reference/data-types/fixedstring)                                                                                             |
| `\x08` boolean                                | [Bool](/pt-BR/reference/data-types/boolean)                                                                                                                                                     |
| `\x09` datetime                               | [DateTime64](/pt-BR/reference/data-types/datetime64)                                                                                                                                            |
| `\x0A` valor NULL                             | [NULL](/pt-BR/reference/data-types/nullable)                                                                                                                                                    |
| `\x0D` código JavaScript                      | [String](/pt-BR/reference/data-types/string)/[FixedString](/pt-BR/reference/data-types/fixedstring)                                                                                             |
| `\x0E` símbolo                                | [String](/pt-BR/reference/data-types/string)/[FixedString](/pt-BR/reference/data-types/fixedstring)                                                                                             |
| `\x10` int32                                  | [Int32/UInt32](/pt-BR/reference/data-types/int-uint)/[Decimal32](/pt-BR/reference/data-types/decimal)/[IPv4](/pt-BR/reference/data-types/ipv4)/[Enum8/Enum16](/pt-BR/reference/data-types/enum) |
| `\x12` int64                                  | [Int64/UInt64](/pt-BR/reference/data-types/int-uint)/[Decimal64](/pt-BR/reference/data-types/decimal)/[DateTime64](/pt-BR/reference/data-types/datetime64)                                      |

Outros tipos BSON não são suportados. Além disso, o formato faz conversão entre diferentes tipos inteiros.
Por exemplo, é possível inserir um valor BSON `int32` no ClickHouse como [`UInt8`](/pt-BR/reference/data-types/int-uint).

Inteiros grandes e decimais, como `Int128`/`UInt128`/`Int256`/`UInt256`/`Decimal128`/`Decimal256`, podem ser convertidos de um valor BSON do tipo Binary com o subtipo binário `\x00`.
Nesse caso, o formato valida se o tamanho dos dados binários é igual ao tamanho do valor esperado.

<Note>
  Este formato não funciona corretamente em plataformas Big-Endian.
</Note>

<div id="example-usage">
  ## Exemplo de uso
</div>

<div id="inserting-data">
  ### Inserção de dados
</div>

Usando um arquivo BSON com os dados a seguir, chamado `football.bson`:

```text theme={null}
    ┌───────date─┬─season─┬─home_team─────────────┬─away_team───────────┬─home_team_goals─┬─away_team_goals─┐
 1. │ 2022-04-30 │   2021 │ Sutton United         │ Bradford City       │               1 │               4 │
 2. │ 2022-04-30 │   2021 │ Swindon Town          │ Barrow              │               2 │               1 │
 3. │ 2022-04-30 │   2021 │ Tranmere Rovers       │ Oldham Athletic     │               2 │               0 │
 4. │ 2022-05-02 │   2021 │ Port Vale             │ Newport County      │               1 │               2 │
 5. │ 2022-05-02 │   2021 │ Salford City          │ Mansfield Town      │               2 │               2 │
 6. │ 2022-05-07 │   2021 │ Barrow                │ Northampton Town    │               1 │               3 │
 7. │ 2022-05-07 │   2021 │ Bradford City         │ Carlisle United     │               2 │               0 │
 8. │ 2022-05-07 │   2021 │ Bristol Rovers        │ Scunthorpe United   │               7 │               0 │
 9. │ 2022-05-07 │   2021 │ Exeter City           │ Port Vale           │               0 │               1 │
10. │ 2022-05-07 │   2021 │ Harrogate Town A.F.C. │ Sutton United       │               0 │               2 │
11. │ 2022-05-07 │   2021 │ Hartlepool United     │ Colchester United   │               0 │               2 │
12. │ 2022-05-07 │   2021 │ Leyton Orient         │ Tranmere Rovers     │               0 │               1 │
13. │ 2022-05-07 │   2021 │ Mansfield Town        │ Forest Green Rovers │               2 │               2 │
14. │ 2022-05-07 │   2021 │ Newport County        │ Rochdale            │               0 │               2 │
15. │ 2022-05-07 │   2021 │ Oldham Athletic       │ Crawley Town        │               3 │               3 │
16. │ 2022-05-07 │   2021 │ Stevenage Borough     │ Salford City        │               4 │               2 │
17. │ 2022-05-07 │   2021 │ Walsall               │ Swindon Town        │               0 │               3 │
    └────────────┴────────┴───────────────────────┴─────────────────────┴─────────────────┴─────────────────┘
```

Insira os dados:

```sql theme={null}
INSERT INTO football FROM INFILE 'football.bson' FORMAT BSONEachRow;
```

<div id="reading-data">
  ### Leitura de dados
</div>

Leia os dados usando o formato `BSONEachRow`:

```sql theme={null}
SELECT *
FROM football INTO OUTFILE 'docs_data/bson/football.bson'
FORMAT BSONEachRow
```

<Tip>
  BSON é um formato binário que não é exibido no terminal em um formato legível por humanos. Use `INTO OUTFILE` para gerar arquivos BSON.
</Tip>

<div id="format-settings">
  ## Configurações de formato
</div>

| Configuração                                                                                                                                                                             | Descrição                                                                                                 | Padrão  |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------- | ------- |
| [`output_format_bson_string_as_string`](/pt-BR/reference/settings/formats#output_format_bson_string_as_string)                                                                           | Usa o tipo String do BSON em vez de Binary para colunas String.                                           | `false` |
| [`input_format_bson_skip_fields_with_unsupported_types_in_schema_inference`](/pt-BR/reference/settings/formats#input_format_bson_skip_fields_with_unsupported_types_in_schema_inference) | Permite ignorar colunas com tipos não compatíveis durante a inferência de esquema do formato BSONEachRow. | `false` |
