Skip to content

Commit

Permalink
do not map empty string sto null
Browse files Browse the repository at this point in the history
  • Loading branch information
pacman82 committed Dec 19, 2023
1 parent 0b3a597 commit ae40547
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/reader/text.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{char::decode_utf16, cmp::min, ffi::CStr, num::NonZeroUsize, sync::Arc};

use arrow::array::{ArrayRef, StringBuilder};
use log::warn;
use odbc_api::{
buffers::{AnySlice, BufferDesc},
DataType as OdbcDataType,
Expand Down Expand Up @@ -59,6 +60,11 @@ fn narrow_text_strategy(
assume_indicators_are_memory_garbage: bool,
) -> Box<dyn ReadStrategy> {
if assume_indicators_are_memory_garbage {
warn!(
"Ignoring indicators, because we expect the ODBC driver of your database to return \
garbage memory. We can not distiguish between empty strings and NULL. Everything is \
empty."
);
Box::new(NarrowUseTerminatingZero::new(octet_len))
} else {
Box::new(NarrowText::new(octet_len))
Expand Down
15 changes: 11 additions & 4 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,11 @@ fn fetch_varchar() {
#[test]
fn fetch_varchar_using_terminating_zeroes_to_indicate_string_length() {
let table_name = function_name!().rsplit_once(':').unwrap().1;
let cursor = cursor_over(table_name, "VARCHAR(50)", "('Hello'),('Bonjour'),(NULL)");
let cursor = cursor_over(
table_name,
"VARCHAR(50)",
"('Hello'),('Bonjour'),(NULL)",
);

let mut quirks = Quirks::new();
quirks.indicators_returned_from_bulk_fetch_are_memory_garbage = true;
Expand All @@ -331,9 +335,12 @@ fn fetch_varchar_using_terminating_zeroes_to_indicate_string_length() {
assert_eq!("Bonjour", array_vals.value(1));

// This workaround is currently only active for UTF-8. Which in turn is only active on Linux
#[cfg(target_os="windows")]
#[cfg(target_os = "windows")]
assert!(array_vals.is_null(2));
#[cfg(not(target_os="windows"))]
// Due to the ambiguity between empty and NULL we map everything to empty. This can not be
// mapped to NULL, due to the fact, that the schema might be a mandatory column. The
// representation is ambigious, because we need to ignore the indicator buffer.
#[cfg(not(target_os = "windows"))]
assert_eq!("", array_vals.value(0));
}

Expand All @@ -345,7 +352,7 @@ fn fetch_varchar_using_terminating_zeroes_to_indicate_string_length() {
#[test]
fn fetch_empty_string_from_non_null_varchar_using_terminating_zeroes_to_indicate_string_length() {
let table_name = function_name!().rsplit_once(':').unwrap().1;
let cursor = cursor_over(table_name, "VARCHAR(50)", "('')");
let cursor = cursor_over(table_name, "VARCHAR(50) NOT NULL", "('')");

let mut quirks = Quirks::new();
quirks.indicators_returned_from_bulk_fetch_are_memory_garbage = true;
Expand Down

0 comments on commit ae40547

Please sign in to comment.