-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Implement support for 1-dim arrays for PostgreSQL #110
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c9c87b6
Implement support for 1-dim arrays for PostgreSQL
oeb25 521570d
Remember `use std::marker::PhantomData`
oeb25 11b36fc
Remove unused method `extend` on ArrayEncoder
oeb25 0de4b51
Add the array types to the query! macro
oeb25 b51b4d3
Report errors in decoding, change types in macros
oeb25 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
/// Encoding and decoding of Postgres arrays. Documentation of the byte format can be found [here](https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/include/utils/array.h;h=7f7e744cb12bc872f628f90dad99dfdf074eb314;hb=master#l6) | ||
use crate::decode::Decode; | ||
use crate::decode::DecodeError; | ||
use crate::encode::Encode; | ||
use crate::io::{Buf, BufMut}; | ||
use crate::postgres::database::Postgres; | ||
use crate::types::HasSqlType; | ||
use std::marker::PhantomData; | ||
|
||
impl<T> Encode<Postgres> for [T] | ||
where | ||
T: Encode<Postgres>, | ||
Postgres: HasSqlType<T>, | ||
{ | ||
fn encode(&self, buf: &mut Vec<u8>) { | ||
let mut encoder = ArrayEncoder::new(buf); | ||
for item in self { | ||
encoder.push(item); | ||
} | ||
} | ||
} | ||
impl<T> Encode<Postgres> for Vec<T> | ||
where | ||
[T]: Encode<Postgres>, | ||
Postgres: HasSqlType<T>, | ||
{ | ||
fn encode(&self, buf: &mut Vec<u8>) { | ||
self.as_slice().encode(buf) | ||
} | ||
} | ||
|
||
impl<T> Decode<Postgres> for Vec<T> | ||
where | ||
T: Decode<Postgres>, | ||
Postgres: HasSqlType<T>, | ||
{ | ||
fn decode(buf: &[u8]) -> Result<Self, DecodeError> { | ||
let decoder = ArrayDecoder::<T>::new(buf)?; | ||
decoder.collect() | ||
} | ||
} | ||
|
||
type Order = byteorder::BigEndian; | ||
|
||
struct ArrayDecoder<'a, T> | ||
where | ||
T: Decode<Postgres>, | ||
Postgres: HasSqlType<T>, | ||
{ | ||
left: usize, | ||
did_error: bool, | ||
|
||
buf: &'a [u8], | ||
|
||
phantom: PhantomData<T>, | ||
} | ||
|
||
impl<T> ArrayDecoder<'_, T> | ||
where | ||
T: Decode<Postgres>, | ||
Postgres: HasSqlType<T>, | ||
{ | ||
fn new(mut buf: &[u8]) -> Result<ArrayDecoder<T>, DecodeError> { | ||
let ndim = buf.get_i32::<Order>()?; | ||
let dataoffset = buf.get_i32::<Order>()?; | ||
let elemtype = buf.get_i32::<Order>()?; | ||
|
||
if ndim == 0 { | ||
return Ok(ArrayDecoder { | ||
left: 0, | ||
did_error: false, | ||
buf, | ||
phantom: PhantomData, | ||
}); | ||
} | ||
|
||
if ndim != 1 { | ||
return Err(decode_err!( | ||
"only arrays of dimension 1 is supported, found array of dimension {}", | ||
ndim | ||
)); | ||
} | ||
|
||
let dimensions = buf.get_i32::<Order>()?; | ||
let lower_bnds = buf.get_i32::<Order>()?; | ||
|
||
if dataoffset != 0 { | ||
// arrays with [null bitmap] is not supported | ||
return Err(DecodeError::UnexpectedNull); | ||
} | ||
if elemtype != <Postgres as HasSqlType<T>>::type_info().id.0 as i32 { | ||
return Err(decode_err!("mismatched array element type")); | ||
} | ||
if lower_bnds != 1 { | ||
return Err(decode_err!( | ||
"expected lower_bnds of array to be 1, but found {}", | ||
lower_bnds | ||
)); | ||
} | ||
|
||
Ok(ArrayDecoder { | ||
left: dimensions as usize, | ||
did_error: false, | ||
buf, | ||
|
||
phantom: PhantomData, | ||
}) | ||
} | ||
|
||
/// Decodes the next element without worring how many are left, or if it previously errored | ||
fn decode_next_element(&mut self) -> Result<T, DecodeError> { | ||
let len = self.buf.get_i32::<Order>()?; | ||
let bytes = self.buf.get_bytes(len as usize)?; | ||
Decode::decode(bytes) | ||
} | ||
} | ||
|
||
impl<T> Iterator for ArrayDecoder<'_, T> | ||
where | ||
T: Decode<Postgres>, | ||
Postgres: HasSqlType<T>, | ||
{ | ||
type Item = Result<T, DecodeError>; | ||
|
||
fn next(&mut self) -> Option<Result<T, DecodeError>> { | ||
if self.did_error || self.left == 0 { | ||
return None; | ||
} | ||
|
||
self.left -= 1; | ||
|
||
let decoded = self.decode_next_element(); | ||
self.did_error = decoded.is_err(); | ||
Some(decoded) | ||
} | ||
} | ||
|
||
struct ArrayEncoder<'a, T> | ||
where | ||
T: Encode<Postgres>, | ||
Postgres: HasSqlType<T>, | ||
{ | ||
count: usize, | ||
len_start_index: usize, | ||
buf: &'a mut Vec<u8>, | ||
|
||
phantom: PhantomData<T>, | ||
} | ||
|
||
impl<T> ArrayEncoder<'_, T> | ||
where | ||
T: Encode<Postgres>, | ||
Postgres: HasSqlType<T>, | ||
{ | ||
fn new(buf: &mut Vec<u8>) -> ArrayEncoder<T> { | ||
let ty = <Postgres as HasSqlType<T>>::type_info(); | ||
|
||
// ndim | ||
buf.put_i32::<Order>(1); | ||
// dataoffset | ||
buf.put_i32::<Order>(0); | ||
// elemtype | ||
buf.put_i32::<Order>(ty.id.0 as i32); | ||
let len_start_index = buf.len(); | ||
// dimensions | ||
buf.put_i32::<Order>(0); | ||
// lower_bnds | ||
buf.put_i32::<Order>(1); | ||
|
||
ArrayEncoder { | ||
count: 0, | ||
len_start_index, | ||
buf, | ||
|
||
phantom: PhantomData, | ||
} | ||
} | ||
fn push(&mut self, item: &T) { | ||
// Allocate space for the length of the encoded elemement up front | ||
let el_len_index = self.buf.len(); | ||
self.buf.put_i32::<Order>(0); | ||
|
||
// Allocate and encode the element it self | ||
let el_start = self.buf.len(); | ||
Encode::encode(item, self.buf); | ||
let el_end = self.buf.len(); | ||
|
||
// Now we know the actual length of the encoded element | ||
let el_len = el_end - el_start; | ||
|
||
// And we can now go back and update the length | ||
self.buf[el_len_index..el_start].copy_from_slice(&(el_len as i32).to_be_bytes()); | ||
|
||
self.count += 1; | ||
} | ||
fn update_len(&mut self) { | ||
const I32_SIZE: usize = std::mem::size_of::<i32>(); | ||
|
||
let size_bytes = (self.count as i32).to_be_bytes(); | ||
|
||
self.buf[self.len_start_index..self.len_start_index + I32_SIZE] | ||
.copy_from_slice(&size_bytes); | ||
} | ||
} | ||
impl<T> Drop for ArrayEncoder<'_, T> | ||
where | ||
T: Encode<Postgres>, | ||
Postgres: HasSqlType<T>, | ||
{ | ||
fn drop(&mut self) { | ||
self.update_len(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod array; | ||
mod bool; | ||
mod bytes; | ||
mod float; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this is actually the description of the in-memory format. The binary wire format appears to be different based on the implementation of
array_send
here: https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/utils/adt/arrayfuncs.c;h=7a4a5aaa86dc1c8cffa2d899c89511dc317d485b;hb=master#l1547