| 495 | |
| 496 | |
| 497 | void registerDataTypeTuple(DataTypeFactory & factory) |
| 498 | { |
| 499 | factory.registerDataType("Tuple", create, DataTypeFactory::Case::Sensitive, Documentation{ |
| 500 | .description = R"DOCS_MD( |
| 501 | A tuple of elements, each having an individual [type](/sql-reference/data-types). Tuple must contain at least one element. |
| 502 | |
| 503 | Tuples are used for temporary column grouping. Columns can be grouped when an IN expression is used in a query, and for specifying certain formal parameters of lambda functions. For more information, see the sections [IN operators](../../sql-reference/operators/in.md) and [Higher order functions](/sql-reference/functions/overview#higher-order-functions). |
| 504 | |
| 505 | Tuples can be the result of a query. In this case, for text formats other than JSON, values are comma-separated in `()`. In JSON formats, tuples are output as arrays (in `[]`). |
| 506 | |
| 507 | ## Creating Tuples {#creating-tuples} |
| 508 | |
| 509 | You can use a function to create a tuple: |
| 510 | |
| 511 | ```sql |
| 512 | tuple(T1, T2, ...) |
| 513 | ``` |
| 514 | |
| 515 | Example of creating a tuple: |
| 516 | |
| 517 | ```sql |
| 518 | SELECT tuple(1, 'a') AS x, toTypeName(x) |
| 519 | ``` |
| 520 | |
| 521 | ```text |
| 522 | ┌─x───────┬─toTypeName(tuple(1, 'a'))─┐ |
| 523 | │ (1,'a') │ Tuple(UInt8, String) │ |
| 524 | └─────────┴───────────────────────────┘ |
| 525 | ``` |
| 526 | |
| 527 | A Tuple can contain a single element |
| 528 | |
| 529 | Example: |
| 530 | |
| 531 | ```sql |
| 532 | SELECT tuple('a') AS x; |
| 533 | ``` |
| 534 | |
| 535 | ```text |
| 536 | ┌─x─────┐ |
| 537 | │ ('a') │ |
| 538 | └───────┘ |
| 539 | ``` |
| 540 | |
| 541 | Syntax `(tuple_element1, tuple_element2)` may be used to create a tuple of several elements without calling the `tuple()` function. |
| 542 | |
| 543 | Example: |
| 544 | |
| 545 | ```sql |
| 546 | SELECT (1, 'a') AS x, (today(), rand(), 'someString') AS y, ('a') AS not_a_tuple; |
| 547 | ``` |
| 548 | |
| 549 | ```text |
| 550 | ┌─x───────┬─y──────────────────────────────────────┬─not_a_tuple─┐ |
| 551 | │ (1,'a') │ ('2022-09-21',2006973416,'someString') │ a │ |
| 552 | └─────────┴────────────────────────────────────────┴─────────────┘ |
| 553 | ``` |
| 554 |
no test coverage detected