lidavidm commented on code in PR #4878:
URL: https://github.com/apache/arrow-adbc/pull/4878#discussion_r4266054139
##########
c/driver/sqlite/sqlite_test.cc:
##########
@@ -1013,9 +1015,116 @@ TEST_F(SqliteReaderTest, IntsFloatsStrs) {
ASSERT_EQ(NANOARROW_TYPE_STRING, reader.fields[0].type);
ASSERT_NO_FATAL_FAILURE(reader.Next());
+
ASSERT_NO_FATAL_FAILURE(CompareArray<std::string>(reader.array_view->children[0],
+ {"1.0", "1.0", "", "Inf",
"-Inf"}));
+}
+
+TEST_F(SqliteReaderTest, InferFloatToStringPreservesRoundTripPrecision) {
+ ASSERT_NO_FATAL_FAILURE(Exec("CREATE TABLE foo (upcast_col, append_col)"));
+ ASSERT_NO_FATAL_FAILURE(
+ Exec("INSERT INTO foo VALUES "
+ "(123456789.123, 'start'), "
+ "(3.141592653589793, 123456789.123), "
+ "(0.1, 3.141592653589793), "
+ "('end', 0.1)"));
+
+ struct ExpectedValue {
+ std::optional<double> number;
+ std::string text;
+ };
+ std::vector<ExpectedValue> expected_upcast;
+ std::vector<ExpectedValue> expected_append;
+ sqlite3_stmt* expected_stmt = nullptr;
+ ASSERT_EQ(SQLITE_OK, sqlite3_prepare_v2(db, "SELECT upcast_col, append_col
FROM foo",
+ -1, &expected_stmt, nullptr));
+ int rc = SQLITE_OK;
+ while ((rc = sqlite3_step(expected_stmt)) == SQLITE_ROW) {
+ auto read_expected = [expected_stmt](int column) {
+ ExpectedValue value;
+ const int type = sqlite3_column_type(expected_stmt, column);
+ if (type == SQLITE_INTEGER || type == SQLITE_FLOAT) {
+ value.number = sqlite3_column_double(expected_stmt, column);
+ } else {
+ value.text = std::string(
+ reinterpret_cast<const char*>(sqlite3_column_text(expected_stmt,
column)),
+ sqlite3_column_bytes(expected_stmt, column));
+ }
+ return value;
+ };
+ expected_upcast.emplace_back(read_expected(0));
+ expected_append.emplace_back(read_expected(1));
+ }
+ EXPECT_EQ(SQLITE_DONE, rc);
+ sqlite3_finalize(expected_stmt);
+
+ adbc_validation::StreamReader reader;
+ ASSERT_NO_FATAL_FAILURE(Exec("SELECT * FROM foo", kInferRows, &reader));
+ ASSERT_EQ(NANOARROW_TYPE_STRING, reader.fields[0].type);
+ ASSERT_EQ(NANOARROW_TYPE_STRING, reader.fields[1].type);
+ ASSERT_NO_FATAL_FAILURE(reader.Next());
+ auto expect_round_trip = [](ArrowArrayView* actual,
+ const std::vector<ExpectedValue>& expected) {
+ ASSERT_EQ(actual->length, static_cast<int64_t>(expected.size()));
+ for (int64_t i = 0; i < actual->length; i++) {
+ const ArrowStringView actual_view =
ArrowArrayViewGetStringUnsafe(actual, i);
+ const std::string actual_value(actual_view.data, actual_view.size_bytes);
+
+ if (expected[i].number.has_value()) {
+ char* actual_end = nullptr;
+ const double actual_number = std::strtod(actual_value.c_str(),
&actual_end);
Review Comment:
Why compare the doubles? If the string representations match then it should
be fine?
##########
c/driver/sqlite/sqlite_test.cc:
##########
@@ -1013,9 +1015,116 @@ TEST_F(SqliteReaderTest, IntsFloatsStrs) {
ASSERT_EQ(NANOARROW_TYPE_STRING, reader.fields[0].type);
ASSERT_NO_FATAL_FAILURE(reader.Next());
+
ASSERT_NO_FATAL_FAILURE(CompareArray<std::string>(reader.array_view->children[0],
+ {"1.0", "1.0", "", "Inf",
"-Inf"}));
+}
+
+TEST_F(SqliteReaderTest, InferFloatToStringPreservesRoundTripPrecision) {
+ ASSERT_NO_FATAL_FAILURE(Exec("CREATE TABLE foo (upcast_col, append_col)"));
+ ASSERT_NO_FATAL_FAILURE(
+ Exec("INSERT INTO foo VALUES "
+ "(123456789.123, 'start'), "
+ "(3.141592653589793, 123456789.123), "
+ "(0.1, 3.141592653589793), "
+ "('end', 0.1)"));
+
+ struct ExpectedValue {
+ std::optional<double> number;
+ std::string text;
+ };
+ std::vector<ExpectedValue> expected_upcast;
+ std::vector<ExpectedValue> expected_append;
+ sqlite3_stmt* expected_stmt = nullptr;
+ ASSERT_EQ(SQLITE_OK, sqlite3_prepare_v2(db, "SELECT upcast_col, append_col
FROM foo",
+ -1, &expected_stmt, nullptr));
+ int rc = SQLITE_OK;
+ while ((rc = sqlite3_step(expected_stmt)) == SQLITE_ROW) {
+ auto read_expected = [expected_stmt](int column) {
+ ExpectedValue value;
+ const int type = sqlite3_column_type(expected_stmt, column);
+ if (type == SQLITE_INTEGER || type == SQLITE_FLOAT) {
+ value.number = sqlite3_column_double(expected_stmt, column);
+ } else {
+ value.text = std::string(
+ reinterpret_cast<const char*>(sqlite3_column_text(expected_stmt,
column)),
+ sqlite3_column_bytes(expected_stmt, column));
+ }
+ return value;
+ };
+ expected_upcast.emplace_back(read_expected(0));
+ expected_append.emplace_back(read_expected(1));
+ }
+ EXPECT_EQ(SQLITE_DONE, rc);
+ sqlite3_finalize(expected_stmt);
Review Comment:
Why are we doing all this?
##########
c/driver/sqlite/statement_reader.c:
##########
@@ -953,27 +952,16 @@ AdbcStatusCode
InternalSqliteStatementReaderAppendDoubleToBinary(
struct ArrowBuffer* offsets, struct ArrowBuffer* binary, double value,
int32_t* offset, struct AdbcError* error) {
static const size_t kReserve = 64;
- size_t buffer_size = kReserve;
- CHECK_NA(INTERNAL, ArrowBufferReserve(binary, buffer_size), error);
+ CHECK_NA(INTERNAL, ArrowBufferReserve(binary, kReserve), error);
char* output = (char*)(binary->data + binary->size_bytes);
- int written = 0;
- while (1) {
- written = snprintf(output, buffer_size, "%e", value);
- if (written < 0) {
- InternalAdbcSetError(error, "Encoding error when upcasting double to
string");
- return ADBC_STATUS_INTERNAL;
- } else if (((size_t)written) >= buffer_size) {
- // Truncated, resize and try again
- // Check for overflow - presumably this can never happen...?
- if (UINT_MAX - buffer_size < buffer_size) {
- InternalAdbcSetError(error, "Overflow when upcasting double to
string");
- return ADBC_STATUS_INTERNAL;
- }
- CHECK_NA(INTERNAL, ArrowBufferReserve(binary, buffer_size), error);
- buffer_size += buffer_size;
- continue;
- }
- break;
+ // SQLite's alternate-form-2 formatter is locale-independent, keeps enough
+ // significant digits to round-trip binary64, and preserves REAL values as
+ // SQL-compatible text (including a decimal point for integral values).
Review Comment:
```suggestion
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]