Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
:sectnums:
:sectnumlevels: 5

= TO_BINARY_DOUBLE 函数设计

== 目的

`TO_BINARY_DOUBLE` 复用 BINARY_DOUBLE 类型的输入函数、数值转换和 TO_NUMBER 的格式模型解析。直接输入浮点文本时保留科学计数法与特殊值支持。

使用示例见 xref:master/oracle_compatibility/compat_to_binary_double_function.adoc[TO_BINARY_DOUBLE 函数].

== 实现说明

=== 代码组织

函数注册及 C 入口位于 `contrib/ivorysql_ora/src/builtin_functions/`,类型转换位于扩展的 `src/datatype/`。格式解析位于 `src/backend/utils/adt/`。

[cols="3,4",options="header"]
|===
|文件 |职责
|`+builtin_functions--1.0.sql+` |注册文本与数值重载,声明返回类型
|`numeric_datatype_functions.c` |实现 C 入口与两条文本转换路径
|`binary_double.c` |提供类型输入及 NUMBER 转换
|`formatting.c` |提供共享格式模型解析和前导空格处理
|===

=== 重载与转换路径

`sys.to_binary_double` 注册了 `(text)`、`(text, text)` 和三个数值重载。文本重载共用 C 入口 `ora_to_binary_double()`,入口按实参数量判断是否提供格式。数值重载使用 SQL 实现,NUMBER 和另一种二进制浮点类型通过已有类型转换得到结果,同类型直接返回。所有重载均为 STRICT。

[source,text]
----
text -> binary_double_in() -> BINARY_DOUBLE
text + format -> ora_to_number_internal() -> number_binary_double() -> BINARY_DOUBLE
----

单参数路径将文本转为 C 字符串,再交给 `binary_double_in()`。格式路径先用 `ora_to_number_internal()` 得到 Numeric,再调用 `number_binary_double()`。若格式解析返回空结果,C 入口返回 SQL NULL。

=== 前导空格处理

共享解析器在尚未读入数字内容时跳过前导空格,避免空格占用格式模型中由 9 指定的数字位置。这样,带前导空格的 1234.5 仍能按 9999.9 读入完整的小数部分。进入数字内容后继续按格式处理输入。

== 测试验证

扩展的 `sql/ora_binary_double.sql` 和 `expected/ora_binary_double.out` 覆盖普通文本、科学计数法、格式模型、前导空格、空格式、特殊值及数值重载。可在已构建并安装对应扩展的环境中运行以下回归组。

[source,bash]
----
make -C contrib/ivorysql_ora oracle-installcheck ORA_REGRESS=ora_binary_double
----
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
:sectnums:
:sectnumlevels: 5

= TO_BINARY_FLOAT 函数设计

== 目的

`TO_BINARY_FLOAT` 复用 BINARY_FLOAT 类型的输入函数、数值转换和 TO_NUMBER 的格式模型解析。直接输入浮点文本时保留科学计数法与特殊值支持。

使用示例见 xref:master/oracle_compatibility/compat_to_binary_float_function.adoc[TO_BINARY_FLOAT 函数].

== 实现说明

=== 代码组织

函数注册及 C 入口位于 `contrib/ivorysql_ora/src/builtin_functions/`,类型转换位于扩展的 `src/datatype/`。格式解析位于 `src/backend/utils/adt/`。

[cols="3,4",options="header"]
|===
|文件 |职责
|`+builtin_functions--1.0.sql+` |注册文本与数值重载,声明返回类型
|`numeric_datatype_functions.c` |实现 C 入口与两条文本转换路径
|`binary_float.c` |提供类型输入及 NUMBER 转换
|`formatting.c` |提供共享格式模型解析和前导空格处理
|===

=== 重载与转换路径

`sys.to_binary_float` 注册了 `(text)`、`(text, text)` 和三个数值重载。文本重载共用 C 入口 `ora_to_binary_float()`,入口按实参数量判断是否提供格式。数值重载使用 SQL 实现,NUMBER 和另一种二进制浮点类型通过已有类型转换得到结果,同类型直接返回。所有重载均为 STRICT。

[source,text]
----
text -> binary_float_in() -> BINARY_FLOAT
text + format -> ora_to_number_internal() -> number_binary_float() -> BINARY_FLOAT
----

单参数路径将文本转为 C 字符串,再交给 `binary_float_in()`。格式路径先用 `ora_to_number_internal()` 得到 Numeric,再调用 `number_binary_float()`。若格式解析返回空结果,C 入口返回 SQL NULL。

=== 前导空格处理

共享解析器在尚未读入数字内容时跳过前导空格,避免空格占用格式模型中由 9 指定的数字位置。这样,带前导空格的 1234.5 仍能按 9999.9 读入完整的小数部分。进入数字内容后继续按格式处理输入。

== 测试验证

扩展的 `sql/ora_binary_float.sql` 和 `expected/ora_binary_float.out` 覆盖普通文本、科学计数法、格式模型、前导空格、空格式、特殊值及数值重载。可在已构建并安装对应扩展的环境中运行以下回归组。

[source,bash]
----
make -C contrib/ivorysql_ora oracle-installcheck ORA_REGRESS=ora_binary_float
----
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
:sectnums:
:sectnumlevels: 5

= TO_BINARY_DOUBLE 函数

== 目的

`TO_BINARY_DOUBLE` 将文本或数值转换为 `BINARY_DOUBLE`,即 64 位双精度二进制浮点数。

== 功能说明

[source,text]
----
TO_BINARY_DOUBLE(value)
TO_BINARY_DOUBLE(text_value, format_model)
----

[cols="2,4",options="header"]
|===
|调用方式 |行为
|`TO_BINARY_DOUBLE(text_value)` |直接解析浮点文本,支持正负号、前后空格和科学计数法
|`TO_BINARY_DOUBLE(text_value, format_model)` |按数字格式模型解析文本,再转换为目标精度
|`TO_BINARY_DOUBLE(value)` |接受 NUMBER、BINARY_FLOAT 和 BINARY_DOUBLE 数值。同类型输入直接返回
|===

任一实参为 NULL 时返回 NULL。空格式模型也返回 NULL。需要直接解析文本时,使用单参数形式。

== 使用示例

在已安装 ivorysql_ora 扩展的 Oracle 兼容会话中执行以下示例。

[source,sql]
----
SHOW ivorysql.compatible_mode;
----

确认结果为 `oracle`。

=== 直接解析文本

[source,sql]
----
SELECT sys.to_binary_double('1.2345678901234e+200') AS scientific,
sys.to_binary_double('16777217') AS rounded_value;
----

在 IvorySQL 6.0 中,结果分别显示为 `1.2345678901234E+200` 和 `1.6777217E+007`。二进制浮点值按目标类型的精度存储。

=== 按格式模型解析

[source,sql]
----
SELECT sys.to_binary_double('1,234.5', '9,999.9') AS grouped_value,
sys.to_binary_double(' 1234.5', '9999.9') AS padded_value;
----

两列的数值均为 1234.5。第二列的前导空格不占用格式模型中的数字位置。

=== 转换数值与处理特殊值

[source,sql]
----
SELECT sys.to_binary_double(42::sys.number) AS from_number,
sys.to_binary_double(42::sys.binary_float) AS from_binary;

SELECT sys.to_binary_double('NaN') AS nan_value,
sys.to_binary_double('inf') AS positive_infinity,
sys.to_binary_double('-Infinity') AS negative_infinity;

SELECT sys.to_binary_double(NULL::text) IS NULL AS null_input,
sys.to_binary_double('123', '') IS NULL AS empty_format,
sys.to_binary_double('123', NULL::text) IS NULL AS null_format;
----

第一条查询的两列均为 42。第二条查询依次显示 `Nan`、`Inf` 和 `-Inf`。最后一条查询的三列均为真。

== 使用说明

单参数文本形式沿用 BINARY_DOUBLE 的输入规则。无效的浮点文本会报错。超出范围的有限值可能转换为无穷大,极小值可能舍入为零。需要保留十进制精度时,请使用 NUMBER。
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
:sectnums:
:sectnumlevels: 5

= TO_BINARY_FLOAT 函数

== 目的

`TO_BINARY_FLOAT` 将文本或数值转换为 `BINARY_FLOAT`,即 32 位单精度二进制浮点数。

== 功能说明

[source,text]
----
TO_BINARY_FLOAT(value)
TO_BINARY_FLOAT(text_value, format_model)
----

[cols="2,4",options="header"]
|===
|调用方式 |行为
|`TO_BINARY_FLOAT(text_value)` |直接解析浮点文本,支持正负号、前后空格和科学计数法
|`TO_BINARY_FLOAT(text_value, format_model)` |按数字格式模型解析文本,再转换为目标精度
|`TO_BINARY_FLOAT(value)` |接受 NUMBER、BINARY_FLOAT 和 BINARY_DOUBLE 数值。同类型输入直接返回
|===

任一实参为 NULL 时返回 NULL。空格式模型也返回 NULL。需要直接解析文本时,使用单参数形式。

== 使用示例

在已安装 ivorysql_ora 扩展的 Oracle 兼容会话中执行以下示例。

[source,sql]
----
SHOW ivorysql.compatible_mode;
----

确认结果为 `oracle`。

=== 直接解析文本

[source,sql]
----
SELECT sys.to_binary_float('1.2345678901234e+20') AS scientific,
sys.to_binary_float('16777217') AS rounded_value;
----

在 IvorySQL 6.0 中,结果分别显示为 `1.2345679E+020` 和 `1.6777216E+007`。二进制浮点值按目标类型的精度存储。

=== 按格式模型解析

[source,sql]
----
SELECT sys.to_binary_float('1,234.5', '9,999.9') AS grouped_value,
sys.to_binary_float(' 1234.5', '9999.9') AS padded_value;
----

两列的数值均为 1234.5。第二列的前导空格不占用格式模型中的数字位置。

=== 转换数值与处理特殊值

[source,sql]
----
SELECT sys.to_binary_float(42::sys.number) AS from_number,
sys.to_binary_float(42::sys.binary_double) AS from_binary;

SELECT sys.to_binary_float('NaN') AS nan_value,
sys.to_binary_float('inf') AS positive_infinity,
sys.to_binary_float('-Infinity') AS negative_infinity;

SELECT sys.to_binary_float(NULL::text) IS NULL AS null_input,
sys.to_binary_float('123', '') IS NULL AS empty_format,
sys.to_binary_float('123', NULL::text) IS NULL AS null_format;
----

第一条查询的两列均为 42。第二条查询依次显示 `Nan`、`Inf` 和 `-Inf`。最后一条查询的三列均为真。

== 使用说明

单参数文本形式沿用 BINARY_FLOAT 的输入规则。无效的浮点文本会报错。超出范围的有限值可能转换为无穷大,极小值可能舍入为零。需要保留十进制精度时,请使用 NUMBER。
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
:sectnums:
:sectnumlevels: 5

= TO_BINARY_DOUBLE Function Design

== Purpose

`TO_BINARY_DOUBLE` reuses BINARY_DOUBLE input and numeric conversion routines and the TO_NUMBER format parser. Direct floating-point text retains support for scientific notation and special values.

For usage examples, see xref:master/oracle_compatibility/compat_to_binary_double_function.adoc[TO_BINARY_DOUBLE Function].

== Implementation

=== Code Organization

Function registration and the C entry point reside in `contrib/ivorysql_ora/src/builtin_functions/`. Type conversion resides in the extension’s `src/datatype/`. Format parsing resides in `src/backend/utils/adt/`.

[cols="3,4",options="header"]
|===
|File |Responsibility
|`+builtin_functions--1.0.sql+` |Registers text and numeric overloads and their return type
|`numeric_datatype_functions.c` |Implements the C entry point and both text conversion paths
|`binary_double.c` |Provides type input and NUMBER conversion
|`formatting.c` |Provides shared format parsing and leading-space handling
|===

=== Overloads and Conversion Paths

`sys.to_binary_double` registers `(text)`, `(text, text)`, and three numeric overloads. The text overloads share the C entry point `ora_to_binary_double()`, which checks the argument count for a format model. SQL wrappers use existing casts from NUMBER and the other binary floating-point type, and return same-type input directly. All overloads are STRICT.

[source,text]
----
text -> binary_double_in() -> BINARY_DOUBLE
text + format -> ora_to_number_internal() -> number_binary_double() -> BINARY_DOUBLE
----

The one-argument path converts the text to a C string and calls `binary_double_in()`. The formatted path obtains a Numeric from `ora_to_number_internal()` and calls `number_binary_double()`. The C entry point returns SQL NULL when the format parser returns a null result.

=== Leading Spaces in Formatted Input

The shared parser skips leading spaces before any numeric content is read, so those spaces do not consume digit positions marked by 9. This preserves the fractional part of 1234.5 with leading spaces under the model 9999.9. After numeric content begins, parsing continues according to the model.

== Validation

The extension’s `sql/ora_binary_double.sql` and `expected/ora_binary_double.out` cover plain text, scientific notation, format models, leading spaces, empty formats, special values, and numeric overloads. Run the following regression group against an instance with the matching built and installed extension.

[source,bash]
----
make -C contrib/ivorysql_ora oracle-installcheck ORA_REGRESS=ora_binary_double
----
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
:sectnums:
:sectnumlevels: 5

= TO_BINARY_FLOAT Function Design

== Purpose

`TO_BINARY_FLOAT` reuses BINARY_FLOAT input and numeric conversion routines and the TO_NUMBER format parser. Direct floating-point text retains support for scientific notation and special values.

For usage examples, see xref:master/oracle_compatibility/compat_to_binary_float_function.adoc[TO_BINARY_FLOAT Function].

== Implementation

=== Code Organization

Function registration and the C entry point reside in `contrib/ivorysql_ora/src/builtin_functions/`. Type conversion resides in the extension’s `src/datatype/`. Format parsing resides in `src/backend/utils/adt/`.

[cols="3,4",options="header"]
|===
|File |Responsibility
|`+builtin_functions--1.0.sql+` |Registers text and numeric overloads and their return type
|`numeric_datatype_functions.c` |Implements the C entry point and both text conversion paths
|`binary_float.c` |Provides type input and NUMBER conversion
|`formatting.c` |Provides shared format parsing and leading-space handling
|===

=== Overloads and Conversion Paths

`sys.to_binary_float` registers `(text)`, `(text, text)`, and three numeric overloads. The text overloads share the C entry point `ora_to_binary_float()`, which checks the argument count for a format model. SQL wrappers use existing casts from NUMBER and the other binary floating-point type, and return same-type input directly. All overloads are STRICT.

[source,text]
----
text -> binary_float_in() -> BINARY_FLOAT
text + format -> ora_to_number_internal() -> number_binary_float() -> BINARY_FLOAT
----

The one-argument path converts the text to a C string and calls `binary_float_in()`. The formatted path obtains a Numeric from `ora_to_number_internal()` and calls `number_binary_float()`. The C entry point returns SQL NULL when the format parser returns a null result.

=== Leading Spaces in Formatted Input

The shared parser skips leading spaces before any numeric content is read, so those spaces do not consume digit positions marked by 9. This preserves the fractional part of 1234.5 with leading spaces under the model 9999.9. After numeric content begins, parsing continues according to the model.

== Validation

The extension’s `sql/ora_binary_float.sql` and `expected/ora_binary_float.out` cover plain text, scientific notation, format models, leading spaces, empty formats, special values, and numeric overloads. Run the following regression group against an instance with the matching built and installed extension.

[source,bash]
----
make -C contrib/ivorysql_ora oracle-installcheck ORA_REGRESS=ora_binary_float
----
Loading
Loading