-
Notifications
You must be signed in to change notification settings - Fork 23
add usage and implementation document for oracle compatible feature empty string to null #185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| :sectnums: | ||
| :sectnumlevels: 5 | ||
|
|
||
| :imagesdir: ./_images | ||
|
|
||
| = 空字符串转NULL功能 | ||
|
|
||
| == 目的 | ||
|
|
||
| 在Oracle数据库中,空字符串('')被视为NULL值,这是Oracle的一个重要特性。为了兼容Oracle的这一行为,IvorySQL提供了空字符串转NULL功能。当启用该功能后,SQL语句中的空字符串将自动转换为NULL值,从而保证Oracle应用程序在IvorySQL上的行为一致性。 | ||
|
|
||
| == 实现说明 | ||
|
|
||
| 参数 `ivorysql.enable_emptystring_to_NULL` 对应的GUC变量为 `enable_emptystring_to_NULL`。 | ||
|
|
||
| 在文件 `ora_scan.l` 中可以看到对这个变量的使用: | ||
|
|
||
| [source,c] | ||
| ---- | ||
| case xe: | ||
| yylval->str = litbufdup(yyscanner); | ||
| if (strcmp(yylval->str, "") == 0 && | ||
| ORA_PARSER == compatible_db && | ||
| enable_emptystring_to_NULL) | ||
| { | ||
| return NULL_P; | ||
| } | ||
| return SCONST; | ||
| ---- | ||
|
|
||
| 其中 `xe` 代表被引号包围的字符串: | ||
|
|
||
| ---- | ||
| <xe> extended quoted strings (support backslash escape sequences) | ||
| ---- | ||
|
|
||
| 上面代码的逻辑是,在词法分析时如果遇到空字符串,在空转NULL功能启用的情况下,返回 `NULL_P`,否则返回 `SCONST`。 | ||
|
|
||
| 在语法分析文件 `ora_gram.y` 里,对语句 `insert into abc values('');` 是这样解析的: | ||
|
|
||
| [source,c] | ||
| ---- | ||
| values_clause: | ||
| VALUES '(' expr_list ')' | ||
| { | ||
| SelectStmt *n = makeNode(SelectStmt); | ||
|
|
||
| n->valuesLists = list_make1($3); | ||
| $$ = (Node *) n; | ||
| } | ||
|
|
||
| expr_list: a_expr | ||
| { | ||
| $$ = list_make1($1); | ||
| } | ||
|
|
||
| a_expr: c_expr { $$ = $1; } | ||
|
|
||
| c_expr: AexprConst { $$ = $1; } | ||
|
|
||
| AexprConst: Iconst | ||
| | Sconst | ||
| { | ||
| $$ = makeStringConst($1, @1); | ||
| } | ||
| | NULL_P | ||
| { | ||
| $$ = makeNullAConst(@1); | ||
| } | ||
| ---- | ||
|
|
||
| 以上代码对词法分析时返回的 `NULL_P` 或者 `SCONST` 分别构建对应的节点进行处理。 | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| :sectnums: | ||
| :sectnumlevels: 5 | ||
|
|
||
| :imagesdir: ./_images | ||
|
|
||
| = 兼容Oracle 空字符串转NULL | ||
|
|
||
| == 目的 | ||
|
|
||
| - 在 IvorySQL 的 Oracle 兼容模式下,支持将空字符串转换成NULL进行存储,提供与Oracle数据库一致的行为。 | ||
|
|
||
| == 功能说明 | ||
|
|
||
| - Oracle兼容模式下支持将空字符串转换成NULL进行存储。 | ||
| - 通过参数 `ivorysql.enable_emptystring_to_null` 控制该特性,默认值为 `on`。 | ||
| - 当该参数开启时,插入空字符串会自动转换为NULL值存储。 | ||
| - 可以通过 `IS NULL` 条件查询到转换后的NULL值。 | ||
|
|
||
| == 测试用例 | ||
|
|
||
| [literal] | ||
| ---- | ||
| -- 创建测试表 | ||
| ivorysql=# create table abc (id int); | ||
| CREATE TABLE | ||
|
|
||
| -- 查看空字符串转NULL参数状态 | ||
| ivorysql=# show ivorysql.enable_emptystring_to_null; | ||
| ivorysql.enable_emptystring_to_NULL | ||
| ------------------------------------- | ||
| on | ||
| (1 row) | ||
|
|
||
| -- 插入空字符串 | ||
| ivorysql=# insert into abc values(''); | ||
| INSERT 0 1 | ||
|
|
||
| -- 查询表数据,显示为NULL | ||
| ivorysql=# select * from abc; | ||
| id | ||
| \---- | ||
|
|
||
| (1 row) | ||
|
|
||
| -- 使用IS NULL条件查询 | ||
| ivorysql=# select * from abc where id is null; | ||
| id | ||
| \---- | ||
|
|
||
| (1 row) | ||
| ---- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| :sectnums: | ||
| :sectnumlevels: 5 | ||
|
|
||
| :imagesdir: ./_images | ||
|
|
||
| = Empty String to NULL Conversion | ||
|
|
||
| == Purpose | ||
|
|
||
| In Oracle databases, empty strings ('') are treated as NULL values, which is an important Oracle feature. To maintain compatibility with this Oracle behavior, IvorySQL provides the empty string to NULL conversion functionality. When this feature is enabled, empty strings in SQL statements are automatically converted to NULL values, ensuring behavioral consistency for Oracle applications running on IvorySQL. | ||
|
|
||
| == Implementation | ||
|
|
||
| The parameter `ivorysql.enable_emptystring_to_NULL` corresponds to the GUC variable `enable_emptystring_to_NULL`. | ||
|
|
||
| In the file `ora_scan.l`, you can see how this variable is used: | ||
|
|
||
| [source,c] | ||
| ---- | ||
| case xe: | ||
| yylval->str = litbufdup(yyscanner); | ||
| if (strcmp(yylval->str, "") == 0 && | ||
| ORA_PARSER == compatible_db && | ||
| enable_emptystring_to_NULL) | ||
| { | ||
| return NULL_P; | ||
| } | ||
| return SCONST; | ||
| ---- | ||
|
|
||
| Where `xe` represents strings enclosed in quotes: | ||
|
|
||
| ---- | ||
| <xe> extended quoted strings (support backslash escape sequences) | ||
| ---- | ||
|
|
||
| The logic of the above code is that during lexical analysis, if an empty string is encountered and the empty-to-NULL conversion feature is enabled, it returns `NULL_P`; otherwise, it returns `SCONST`. | ||
|
|
||
| In the grammar analysis file `ora_gram.y`, the statement `insert into abc values('');` is parsed as follows: | ||
|
|
||
| [source,c] | ||
| ---- | ||
| values_clause: | ||
| VALUES '(' expr_list ')' | ||
| { | ||
| SelectStmt *n = makeNode(SelectStmt); | ||
|
|
||
| n->valuesLists = list_make1($3); | ||
| $$ = (Node *) n; | ||
| } | ||
|
|
||
| expr_list: a_expr | ||
| { | ||
| $$ = list_make1($1); | ||
| } | ||
|
|
||
| a_expr: c_expr { $$ = $1; } | ||
|
|
||
| c_expr: AexprConst { $$ = $1; } | ||
|
|
||
| AexprConst: Iconst | ||
| | Sconst | ||
| { | ||
| $$ = makeStringConst($1, @1); | ||
| } | ||
| | NULL_P | ||
| { | ||
| $$ = makeNullAConst(@1); | ||
| } | ||
| ---- | ||
|
|
||
| The above code constructs corresponding nodes to handle `NULL_P` or `SCONST` returned from the lexical analysis. | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| :sectnums: | ||
| :sectnumlevels: 5 | ||
|
|
||
| :imagesdir: ./_images | ||
|
|
||
| = Oracle Compatible Empty String to NULL | ||
|
|
||
| == Purpose | ||
|
|
||
| - In IvorySQL's Oracle compatibility mode, support converting empty strings to NULL for storage, providing behavior consistent with Oracle Database. | ||
|
|
||
| == Feature Description | ||
|
|
||
| - Oracle compatibility mode supports converting empty strings to NULL for storage. | ||
| - The feature is controlled by parameter `ivorysql.enable_emptystring_to_null`, with default value `on`. | ||
| - When this parameter is enabled, inserting an empty string will automatically convert it to NULL value for storage. | ||
| - NULL values after conversion can be queried using the `IS NULL` condition. | ||
|
|
||
| == Test Cases | ||
|
|
||
| [literal] | ||
| ---- | ||
| -- Create test table | ||
| ivorysql=# create table abc (id int); | ||
| CREATE TABLE | ||
|
|
||
| -- Check empty string to NULL parameter status | ||
| ivorysql=# show ivorysql.enable_emptystring_to_null; | ||
| ivorysql.enable_emptystring_to_NULL | ||
| ------------------------------------- | ||
| on | ||
| (1 row) | ||
|
|
||
| -- Insert empty string | ||
| ivorysql=# insert into abc values(''); | ||
| INSERT 0 1 | ||
|
|
||
| -- Query table data, displays as NULL | ||
| ivorysql=# select * from abc; | ||
| id | ||
| \---- | ||
|
|
||
| (1 row) | ||
|
|
||
| -- Query using IS NULL condition | ||
| ivorysql=# select * from abc where id is null; | ||
| id | ||
| \---- | ||
|
|
||
| (1 row) | ||
| ---- | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Markdown compatible syntax is an optional feature of the AsciiDoc language.
So you can simply write like this
as an alternative solution of
[source,c]
Both are OK