> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# cast_* 会话级设置

> cast_* 自动生成分组中的 ClickHouse 会话级设置。

export const SettingsInfoBlock = ({type, default_value, changeable_without_restart}) => {
  return <div className="not-prose" style={{
    display: "flex",
    flexWrap: "wrap",
    alignItems: "baseline",
    columnGap: "0.5rem",
    rowGap: "0.125rem",
    margin: "0.375rem 0",
    fontSize: "0.8125rem",
    lineHeight: "1.125rem"
  }}>
      <div style={{
    fontWeight: 600,
    opacity: 0.72
  }}>类型</div>
      <div style={{
    overflowWrap: "anywhere"
  }}>{type}</div>
      <div style={{
    fontWeight: 600,
    opacity: 0.72,
    marginInlineStart: "0.5rem"
  }}>默认值</div>
      <div style={{
    overflowWrap: "anywhere"
  }}>{default_value}</div>
      {changeable_without_restart && <div style={{
    fontWeight: 600,
    opacity: 0.72,
    marginInlineStart: "0.5rem"
  }}>
          无需重启即可更改
        </div>}
      {changeable_without_restart && <div style={{
    overflowWrap: "anywhere"
  }}>
          {changeable_without_restart}
        </div>}
    </div>;
};

这些设置可在 [system.settings](/docs/zh/reference/system-tables/settings) 中查看，并由 [源代码](https://github.com/ClickHouse/ClickHouse/blob/master/src/Core/Settings.cpp) 自动生成。

<div id="cast_ipv4_ipv6_default_on_conversion_error">
  ## cast\_ipv4\_ipv6\_default\_on\_conversion\_error
</div>

<SettingsInfoBlock type="Bool" default_value="0" />

<VersionHistory rows={[{"id": "row-1","items": [{"label": "22.3"},{"label": "0"},{"label": "使函数 cast(value, 'IPv4') 和 cast(value, 'IPv6') 的行为与 toIPv4 和 toIPv6 函数相同"}]}]} />

发生转换错误时，转换为 IPv4 的 CAST 运算符、转换为 IPv6 类型的 CAST 运算符，以及 toIPv4、toIPv6 函数将返回默认值，而不是抛出异常。

<div id="cast_keep_nullable">
  ## cast\_keep\_nullable
</div>

<SettingsInfoBlock type="Bool" default_value="0" />

启用或禁用在 [CAST](/docs/zh/reference/functions/regular-functions/type-conversion-functions#CAST) 操作中保留 `Nullable` 数据类型。

当该设置启用时，如果 `CAST` 函数的参数为 `Nullable`，则结果也会转换为 `Nullable` 类型。当该设置禁用时，结果始终严格为目标数据类型。

可能的值：

* 0 — `CAST` 结果严格为指定的目标数据类型。
* 1 — 如果参数类型为 `Nullable`，则 `CAST` 结果会转换为 `Nullable(DestinationDataType)`。

**示例**

以下查询的结果严格为目标数据类型：

```sql theme={null}
SET cast_keep_nullable = 0;
SELECT CAST(toNullable(toInt32(0)) AS Int32) as x, toTypeName(x);
```

结果：

```text theme={null}
┌─x─┬─toTypeName(CAST(toNullable(toInt32(0)), 'Int32'))─┐
│ 0 │ Int32                                             │
└───┴───────────────────────────────────────────────────┘
```

以下查询会使目标端数据类型带有 `Nullable` 修饰：

```sql theme={null}
SET cast_keep_nullable = 1;
SELECT CAST(toNullable(toInt32(0)) AS Int32) as x, toTypeName(x);
```

结果：

```text theme={null}
┌─x─┬─toTypeName(CAST(toNullable(toInt32(0)), 'Int32'))─┐
│ 0 │ Nullable(Int32)                                   │
└───┴───────────────────────────────────────────────────┘
```

**另请参阅**

* [CAST](/docs/zh/reference/functions/regular-functions/type-conversion-functions#CAST) 函数
