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

> توثيق نوع البيانات Tuple في ClickHouse

# Tuple(T1, T2, ...)

Tuple هو مجموعة من العناصر، لكل عنصر فيها [نوع](/ar/reference/data-types/index) خاص به. ويجب أن يحتوي Tuple على عنصر واحد على الأقل.

تُستخدم Tuples للتجميع المؤقت للأعمدة. ويمكن تجميع الأعمدة عند استخدام تعبير IN في استعلام، وكذلك لتحديد بعض المعلمات الصورية لدوال لامبدا. لمزيد من المعلومات، راجع قسمي [عوامل التشغيل IN](/ar/reference/statements/in) و[الدوال عالية الرتبة](/ar/reference/functions/regular-functions/overview#higher-order-functions).

يمكن أن تكون Tuples ناتج استعلام. في هذه الحالة، في التنسيقات النصية بخلاف JSON، تُفصل القيم بفواصل داخل `()`. أما في تنسيقات JSON، فتُخرَج Tuples على هيئة مصفوفات (داخل `[]`).

<div id="creating-tuples">
  ## إنشاء Tuples
</div>

يمكنك استخدام دالة لإنشاء tuple:

```sql theme={null}
tuple(T1, T2, ...)
```

مثال على إنشاء Tuple:

```sql theme={null}
SELECT tuple(1, 'a') AS x, toTypeName(x)
```

```text theme={null}
┌─x───────┬─toTypeName(tuple(1, 'a'))─┐
│ (1,'a') │ Tuple(UInt8, String)      │
└─────────┴───────────────────────────┘
```

يمكن أن يتضمن Tuple عنصرًا واحدًا

مثال:

```sql theme={null}
SELECT tuple('a') AS x;
```

```text theme={null}
┌─x─────┐
│ ('a') │
└───────┘
```

يمكن استخدام الصيغة `(tuple_element1, tuple_element2)` لإنشاء tuple يتكوّن من عدة عناصر دون استدعاء الدالة `tuple()`.

مثال:

```sql theme={null}
SELECT (1, 'a') AS x, (today(), rand(), 'someString') AS y, ('a') AS not_a_tuple;
```

```text theme={null}
┌─x───────┬─y──────────────────────────────────────┬─not_a_tuple─┐
│ (1,'a') │ ('2022-09-21',2006973416,'someString') │ a           │
└─────────┴────────────────────────────────────────┴─────────────┘
```

<div id="data-type-detection">
  ## اكتشاف نوع البيانات
</div>

عند إنشاء قيم Tuple أثناء التنفيذ، يستنتج ClickHouse أنواع وسائط Tuple على أنها أصغر أنواع يمكنها احتواء قيمة الوسيطة المقدَّمة. إذا كانت القيمة هي [NULL](/ar/reference/settings/formats#input_format_null_as_default)، فسيكون النوع المستنتَج هو [Nullable](/ar/reference/data-types/nullable).

مثال على الاكتشاف التلقائي لنوع البيانات:

```sql theme={null}
SELECT tuple(1, NULL) AS x, toTypeName(x)
```

```text theme={null}
┌─x─────────┬─toTypeName(tuple(1, NULL))──────┐
│ (1, NULL) │ Tuple(UInt8, Nullable(Nothing)) │
└───────────┴─────────────────────────────────┘
```

<div id="referring-to-tuple-elements">
  ## الإشارة إلى عناصر Tuple
</div>

يمكن الإشارة إلى عناصر Tuple بالاسم أو برقم الفهرس:

```sql title="Query" theme={null}
CREATE TABLE named_tuples (`a` Tuple(s String, i Int64)) ENGINE = Memory;
INSERT INTO named_tuples VALUES (('y', 10)), (('x',-10));

SELECT a.s FROM named_tuples; -- by name
SELECT a.2 FROM named_tuples; -- by index
```

```text title="Response" theme={null}
┌─a.s─┐
│ y   │
│ x   │
└─────┘

┌─tupleElement(a, 2)─┐
│                 10 │
│                -10 │
└────────────────────┘
```

<div id="comparison-operations-with-tuple">
  ## عمليات المقارنة مع Tuple
</div>

تُقارَن قيمتا Tuple بمقارنة عناصرهما بالتتابع من اليسار إلى اليمين. فإذا كان العنصر الأول في Tuple الأولى أكبر (أصغر) من العنصر المناظر له في Tuple الثانية، عُدَّت Tuple الأولى أكبر (أصغر) من الثانية. وإلا، إذا كان العنصران متساويين، فتُقارَن القيمة التالية.

مثال:

```sql theme={null}
SELECT (1, 'z') > (1, 'a') c1, (2022, 01, 02) > (2023, 04, 02) c2, (1,2,3) = (3,2,1) c3;
```

```text theme={null}
┌─c1─┬─c2─┬─c3─┐
│  1 │  0 │  0 │
└────┴────┴────┘
```

أمثلة واقعية:

```sql theme={null}
CREATE TABLE test
(
    `year` Int16,
    `month` Int8,
    `day` Int8
)
ENGINE = Memory AS
SELECT *
FROM values((2022, 12, 31), (2000, 1, 1));

SELECT * FROM test;

┌─year─┬─month─┬─day─┐
│ 2022 │    12 │  31 │
│ 2000 │     1 │   1 │
└──────┴───────┴─────┘

SELECT *
FROM test
WHERE (year, month, day) > (2010, 1, 1);

┌─year─┬─month─┬─day─┐
│ 2022 │    12 │  31 │
└──────┴───────┴─────┘
CREATE TABLE test
(
    `key` Int64,
    `duration` UInt32,
    `value` Float64
)
ENGINE = Memory AS
SELECT *
FROM values((1, 42, 66.5), (1, 42, 70), (2, 1, 10), (2, 2, 0));

SELECT * FROM test;

┌─key─┬─duration─┬─value─┐
│   1 │       42 │  66.5 │
│   1 │       42 │    70 │
│   2 │        1 │    10 │
│   2 │        2 │     0 │
└─────┴──────────┴───────┘

-- Let's find a value for each key with the biggest duration, if durations are equal, select the biggest value

SELECT
    key,
    max(duration),
    argMax(value, (duration, value))
FROM test
GROUP BY key
ORDER BY key ASC;

┌─key─┬─max(duration)─┬─argMax(value, tuple(duration, value))─┐
│   1 │            42 │                                    70 │
│   2 │             2 │                                     0 │
└─────┴───────────────┴───────────────────────────────────────┘
```

<div id="nullable-tuple">
  ## Nullable(Tuple(T1, T2, ...))
</div>

<Info>
  **ميزة تجريبية**

  يتطلب `SET enable_nullable_tuple_type = 1`
  هذه ميزة تجريبية.
</Info>

يسمح بأن تكون الـ Tuple بأكملها `NULL`، بخلاف `Tuple(Nullable(T1), Nullable(T2), ...)` حيث يمكن فقط للعناصر الفردية أن تكون `NULL`.

| النوع                                      | يمكن أن تكون Tuple بقيمة NULL | يمكن أن تكون العناصر بقيمة NULL |
| ------------------------------------------ | ----------------------------- | ------------------------------- |
| `Nullable(Tuple(String, Int64))`           | ✅                             | ❌                               |
| `Tuple(Nullable(String), Nullable(Int64))` | ❌                             | ✅                               |

مثال:

```sql theme={null}
SET enable_nullable_tuple_type = 1;

CREATE TABLE test (
    id UInt32,
    data Nullable(Tuple(String, Int64))
) ENGINE = Memory;

INSERT INTO test VALUES (1, ('hello', 42)), (2, NULL);

SELECT * FROM test WHERE data IS NULL;
```

```txt theme={null}
 ┌─id─┬─data─┐
 │  2 │ ᴺᵁᴸᴸ │
 └────┴──────┘
```
