Skip to content
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

fix(c/driver/sqlite): don't rely on double-quoted strings feature #2555

Merged
merged 1 commit into from
Feb 25, 2025
Merged
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
4 changes: 2 additions & 2 deletions c/driver/sqlite/sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ struct SqliteGetObjectsHelper : public driver::GetObjectsHelper {
// XXX: because we're saving the SqliteQuery, we also need to save the string builder
columns_query.Reset();
columns_query.Append(
R"(SELECT cid, name, type, "notnull", dflt_value FROM pragma_table_info("%w" , "%w") WHERE NAME LIKE ?)",
R"(SELECT cid, name, type, 'notnull', dflt_value FROM pragma_table_info(%Q, %Q) WHERE NAME LIKE ?)",
table.data(), catalog.data());
UNWRAP_RESULT(auto query, columns_query.GetString());
assert(!query.empty());
Expand Down Expand Up @@ -343,7 +343,7 @@ struct SqliteGetObjectsHelper : public driver::GetObjectsHelper {
{
SqliteStringBuilder builder;
builder.Append(
R"(SELECT name FROM pragma_table_info("%w" , "%w") WHERE pk > 0 ORDER BY pk ASC)",
R"(SELECT name FROM pragma_table_info(%Q, %Q) WHERE pk > 0 ORDER BY pk ASC)",
table.data(), catalog.data());
UNWRAP_RESULT(auto pk_query, builder.GetString());
std::vector<std::string> pk;
Expand Down
20 changes: 11 additions & 9 deletions c/driver/sqlite/sqlite_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,10 @@ class SqliteReaderTest : public ::testing::Test {
}

void Exec(const std::string& query) {
ASSERT_EQ(SQLITE_OK, sqlite3_prepare_v2(db, query.c_str(), query.size(), &stmt,
/*pzTail=*/nullptr));
SCOPED_TRACE(query);
int rc = sqlite3_prepare_v2(db, query.c_str(), query.size(), &stmt,
/*pzTail=*/nullptr);
ASSERT_EQ(SQLITE_OK, rc) << "Failed to prepare query: " << sqlite3_errmsg(db);
ASSERT_EQ(SQLITE_DONE, sqlite3_step(stmt));
sqlite3_finalize(stmt);
stmt = nullptr;
Expand Down Expand Up @@ -526,7 +528,7 @@ TEST_F(SqliteReaderTest, IntsFloatsNulls) {
TEST_F(SqliteReaderTest, IntsNullsStrsNullsInts) {
adbc_validation::StreamReader reader;
ASSERT_NO_FATAL_FAILURE(ExecSelect(
R"((NULL), (1), (NULL), (-1), ("foo"), (NULL), (""), (24))", kInferRows, &reader));
R"((NULL), (1), (NULL), (-1), ('foo'), (NULL), (''), (24))", kInferRows, &reader));
ASSERT_EQ(NANOARROW_TYPE_STRING, reader.fields[0].type);

ASSERT_NO_FATAL_FAILURE(reader.Next());
Expand All @@ -552,7 +554,7 @@ TEST_F(SqliteReaderTest, IntExtremes) {
TEST_F(SqliteReaderTest, IntExtremesStrs) {
adbc_validation::StreamReader reader;
ASSERT_NO_FATAL_FAILURE(ExecSelect(
R"((NULL), (9223372036854775807), (-9223372036854775808), (""), (9223372036854775807), (-9223372036854775808))",
R"((NULL), (9223372036854775807), (-9223372036854775808), (''), (9223372036854775807), (-9223372036854775808))",
kInferRows, &reader));
ASSERT_EQ(NANOARROW_TYPE_STRING, reader.fields[0].type);

Expand Down Expand Up @@ -587,7 +589,7 @@ TEST_F(SqliteReaderTest, FloatExtremes) {
TEST_F(SqliteReaderTest, IntsFloatsStrs) {
adbc_validation::StreamReader reader;
ASSERT_NO_FATAL_FAILURE(
ExecSelect(R"((1), (1.0), (""), (9e999), (-9e999))", kInferRows, &reader));
ExecSelect(R"((1), (1.0), (''), (9e999), (-9e999))", kInferRows, &reader));
ASSERT_EQ(NANOARROW_TYPE_STRING, reader.fields[0].type);

ASSERT_NO_FATAL_FAILURE(reader.Next());
Expand Down Expand Up @@ -629,7 +631,7 @@ TEST_F(SqliteReaderTest, InferIntRejectFloat) {
TEST_F(SqliteReaderTest, InferIntRejectStr) {
adbc_validation::StreamReader reader;
ASSERT_NO_FATAL_FAILURE(
ExecSelect(R"((1), (NULL), (""), (NULL))", /*infer_rows=*/2, &reader));
ExecSelect(R"((1), (NULL), (''), (NULL))", /*infer_rows=*/2, &reader));
ASSERT_EQ(NANOARROW_TYPE_INT64, reader.fields[0].type);
ASSERT_NO_FATAL_FAILURE(reader.Next());
ASSERT_NO_FATAL_FAILURE(
Expand Down Expand Up @@ -678,7 +680,7 @@ TEST_F(SqliteReaderTest, InferFloatReadIntFloat) {

TEST_F(SqliteReaderTest, InferFloatRejectStr) {
adbc_validation::StreamReader reader;
ASSERT_NO_FATAL_FAILURE(ExecSelect(R"((1E0), (NULL), (2E0), (3), (""), (NULL))",
ASSERT_NO_FATAL_FAILURE(ExecSelect(R"((1E0), (NULL), (2E0), (3), (''), (NULL))",
/*infer_rows=*/2, &reader));
ASSERT_EQ(NANOARROW_TYPE_DOUBLE, reader.fields[0].type);
ASSERT_NO_FATAL_FAILURE(reader.Next());
Expand Down Expand Up @@ -716,7 +718,7 @@ TEST_F(SqliteReaderTest, InferFloatRejectBlob) {

TEST_F(SqliteReaderTest, InferStrReadAll) {
adbc_validation::StreamReader reader;
ASSERT_NO_FATAL_FAILURE(ExecSelect(R"((""), (NULL), (2), (3E0), ("foo"), (NULL))",
ASSERT_NO_FATAL_FAILURE(ExecSelect(R"((''), (NULL), (2), (3E0), ('foo'), (NULL))",
/*infer_rows=*/2, &reader));
ASSERT_EQ(NANOARROW_TYPE_STRING, reader.fields[0].type);
ASSERT_NO_FATAL_FAILURE(reader.Next());
Expand Down Expand Up @@ -799,7 +801,7 @@ TEST_F(SqliteReaderTest, InferTypedParams) {

ASSERT_NO_FATAL_FAILURE(Exec("CREATE TABLE foo (idx, value)"));
ASSERT_NO_FATAL_FAILURE(
Exec(R"(INSERT INTO foo VALUES (0, "foo"), (1, NULL), (2, 4), (3, 1E2))"));
Exec(R"(INSERT INTO foo VALUES (0, 'foo'), (1, NULL), (2, 4), (3, 1E2))"));

ASSERT_THAT(adbc_validation::MakeSchema(&schema.value, {{"", NANOARROW_TYPE_INT64}}),
IsOkErrno());
Expand Down
18 changes: 9 additions & 9 deletions python/adbc_driver_manager/tests/test_dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,37 +201,37 @@ def test_partitions(sqlite):
@pytest.mark.sqlite
def test_query_fetch_py(sqlite):
with sqlite.cursor() as cur:
cur.execute('SELECT 1, "foo", 2.0')
cur.execute("SELECT 1, 'foo' AS foo, 2.0")
assert cur.description == [
("1", dbapi.NUMBER, None, None, None, None, None),
('"foo"', dbapi.STRING, None, None, None, None, None),
("foo", dbapi.STRING, None, None, None, None, None),
("2.0", dbapi.NUMBER, None, None, None, None, None),
]
assert cur.rownumber == 0
assert cur.fetchone() == (1, "foo", 2.0)
assert cur.rownumber == 1
assert cur.fetchone() is None

cur.execute('SELECT 1, "foo", 2.0')
cur.execute("SELECT 1, 'foo', 2.0")
assert cur.fetchmany() == [(1, "foo", 2.0)]
assert cur.fetchmany() == []

cur.execute('SELECT 1, "foo", 2.0')
cur.execute("SELECT 1, 'foo', 2.0")
assert cur.fetchall() == [(1, "foo", 2.0)]
assert cur.fetchall() == []

cur.execute('SELECT 1, "foo", 2.0')
cur.execute("SELECT 1, 'foo', 2.0")
assert list(cur) == [(1, "foo", 2.0)]


@pytest.mark.sqlite
def test_query_fetch_arrow(sqlite):
with sqlite.cursor() as cur:
cur.execute('SELECT 1, "foo", 2.0')
cur.execute("SELECT 1, 'foo' AS foo, 2.0")
assert cur.fetch_arrow_table() == pyarrow.table(
{
"1": [1],
'"foo"': ["foo"],
"foo": ["foo"],
"2.0": [2.0],
}
)
Expand All @@ -240,13 +240,13 @@ def test_query_fetch_arrow(sqlite):
@pytest.mark.sqlite
def test_query_fetch_df(sqlite):
with sqlite.cursor() as cur:
cur.execute('SELECT 1, "foo", 2.0')
cur.execute("SELECT 1, 'foo' AS foo, 2.0")
assert_frame_equal(
cur.fetch_df(),
pandas.DataFrame(
{
"1": [1],
'"foo"': ["foo"],
"foo": ["foo"],
"2.0": [2.0],
}
),
Expand Down
Loading