Skip to content

feat(rust): add support to u64 and u256 #100

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions questdb-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ rustls = { version = "0.23.25", default-features = false, features = ["logging",
rustls-native-certs = { version = "0.8.1", optional = true }
webpki-roots = { version = "0.26.8", default-features = false, optional = true }
chrono = { version = "0.4.40", optional = true }
hex = "0.4.3"
Copy link
Collaborator

@amunra amunra May 6, 2025

Choose a reason for hiding this comment

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

Don't need the external dep.
Explained faster alternative later.


# We need to limit the `ureq` version to 3.0.x since we use
# the `ureq::unversioned` module which does not respect semantic versioning.
Expand Down
51 changes: 51 additions & 0 deletions questdb-rs/src/ingress/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,57 @@ impl Buffer {
self.output.push(b'i');
Ok(self)
}
/// Record an integer value for the given column.
///
/// ```
/// # use questdb::Result;
/// # use questdb::ingress::Buffer;
/// # fn main() -> Result<()> {
/// # let mut buffer = Buffer::new();
/// # buffer.table("x")?;
/// buffer.column_u64("col_name", 42)?;
/// # Ok(())
/// # }
/// ```
///
/// or
///
/// ```
/// # use questdb::Result;
/// # use questdb::ingress::Buffer;
/// use questdb::ingress::ColumnName;
///
/// # fn main() -> Result<()> {
/// # let mut buffer = Buffer::new();
/// # buffer.table("x")?;
/// let col_name = ColumnName::new("col_name")?;
/// buffer.column_u64(col_name, 42);
/// # Ok(())
/// # }
/// ```
pub fn column_u64<'a, N>(&mut self, name: N, value: u64) -> Result<&mut Self>
where
N: TryInto<ColumnName<'a>>,
Error: From<N::Error>,
{
self.write_column_key(name)?;
let mut buf = itoa::Buffer::new();
let printed = buf.format(value);
self.output.extend_from_slice(printed.as_bytes());
self.output.push(b'i');
Ok(self)
}
Comment on lines +940 to +951
Copy link
Collaborator

Choose a reason for hiding this comment

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

It would be misleading for the API to accept a datatype that the server does not.
If you need to serialize a u64, best do a checked cast and then call column_i64.


pub fn column_long256<'a, N>(&mut self, name: N, value: [u8; 32]) -> Result<&mut Self>
where
N: TryInto<ColumnName<'a>>,
Error: From<N::Error>,
{
self.write_column_key(name)?;
let ser = format!("0x{}i", hex::encode(value));
self.output.extend_from_slice(ser.as_bytes());
Ok(self)
}
Comment on lines +953 to +962
Copy link
Collaborator

@amunra amunra May 6, 2025

Choose a reason for hiding this comment

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

Good start! This is indeed a missing feature and a very welcome contribution.

The long256 encoding looks correct.
https://questdb.com/docs/reference/api/ilp/columnset-types/#long256

For us to consider the PR, it should include tests and a C and C++ API and tests against a live DB (system_test dir). Happy to help if you need assistance with anything here.

As per this function itself, it would need minor tweaking to avoid allocating memory.
In other words, calling format! here isn't really something we'd want to do.

Given that the number is a fixed-size slice, you should be able to just call write!(&mut self.output, "0x{:x}{:x}{:x}{:x}i", .., .., .., ..). This also drops the dependency on hex and the additional dynamic slice size conditional that carries with it.

API docs should include any byte-order considerations.
The representation should align with bigint's H256: https://docs.rs/ethereum-bigint/latest/bigint/struct.H256.html - The questdb-rs crate should not depend on bigint but it should ideally link to it in docs and provide a basic example.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Just remembered.. might want to reserve the exact additional needed bytes here too.


/// Record a floating point value for the given column.
///
Expand Down
Loading