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: bson should handle nulls #2485

Merged
merged 9 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions crates/datasources/src/bson/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,16 @@ macro_rules! append_scalar {
fn append_value(val: RawBsonRef, typ: &DataType, col: &mut dyn ArrayBuilder) -> Result<()> {
// So robust
match (val, typ) {
// null
(RawBsonRef::Null, _) => append_null(&typ, col)?,
(RawBsonRef::Undefined, _) => append_null(&typ, col)?,

// Boolean
(RawBsonRef::Boolean(v), DataType::Boolean) => append_scalar!(BooleanBuilder, col, v),
(RawBsonRef::Boolean(v), DataType::Utf8) => {
append_scalar!(StringBuilder, col, v.to_string())
}

// Double
(RawBsonRef::Double(v), DataType::Int32) => append_scalar!(Int32Builder, col, v as i32),
(RawBsonRef::Double(v), DataType::Int64) => append_scalar!(Int64Builder, col, v as i64),
Expand Down
19 changes: 19 additions & 0 deletions tests/tests/test_bson.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,22 @@ def test_read_bson(
assert len(row) == 5
assert row["beatle_name"] in beatles
assert beatles.index(row["beatle_name"]) == row["beatle_idx"] - 1

def test_null_handling(
glaredb_connection: psycopg2.extensions.connection,
tmp_path_factory: pytest.TempPathFactory,
):
tmp_dir = tmp_path_factory.mktemp(basename="null_handling", numbered=True)
data_path = tmp_dir.joinpath("mixed.bson")

with open(data_path, "wb") as f:
for i in range(100):
f.write(bson.encode({"a": 1}))

for i in range(10):
f.write(bson.encode({"a": None}))

with glaredb_connection.cursor() as curr:
curr.execute(f"select count(*) from '{data_path}'")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the count(*) query was previously working, We should explicitly check the values

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran this test without the patch and it failed.

Because this uses the streaming bson table/file code, which uses the same bson-to-recordbatch as mongodb, we would expect the count to break here too.

r = curr.fetchone()
assert r[0] == 110
Loading