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

implement compress,uncompress,uncompressed_length #3856

Merged
merged 8 commits into from Dec 5, 2018
25 changes: 12 additions & 13 deletions src/coprocessor/dag/expr/builtin_encryption.rs
Expand Up @@ -14,7 +14,6 @@
use std::borrow::Cow;

use super::{Error, EvalContext, Result, ScalarFunc};
use byteorder::{ByteOrder, LittleEndian, WriteBytesExt};
use coprocessor::codec::Datum;
use crypto::{
digest::Digest,
Expand Down Expand Up @@ -102,26 +101,24 @@ impl ScalarFunc {
ctx: &mut EvalContext,
row: &[Datum],
) -> Result<Option<Cow<'a, [u8]>>> {
use byteorder::{ByteOrder, LittleEndian};
let input = try_opt!(self.children[0].eval_string(ctx, row));

// according to MySQL doc: Empty strings are stored as empty strings.
if input.is_empty() {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add a comment as TiDB: Empty strings are stored as empty strings

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

return Ok(Some(Cow::Borrowed(b"")));
}
let mut e = ZlibEncoder::new(input.as_ref(), Compression::default());
let mut vec = Vec::with_capacity(1024);
let mut vec = Vec::with_capacity(input.len());
ice1000 marked this conversation as resolved.
Show resolved Hide resolved
vec.resize(4, 0);
LittleEndian::write_u32(&mut vec, input.len() as u32);
match e.read_to_end(&mut vec) {
Ok(len) => {
let mut cap = 4 + len;
// according to MySQL doc: If the string ends with space,
// an extra . character is added to avoid problems with endspace trimming
if vec[len - 1] == 32 {
Ok(_) => {
// according to MySQL doc: append "." if ends with space
if vec[vec.len() - 1] == 32 {
vec.push(b'.');
cap += 1;
}

let mut wtr = Vec::with_capacity(cap);
wtr.write_u32::<LittleEndian>(input.len() as u32).unwrap();
wtr.extend_from_slice(&vec);
Ok(Some(Cow::Owned(wtr)))
Ok(Some(Cow::Owned(vec)))
}
_ => Ok(None),
}
Expand All @@ -133,6 +130,7 @@ impl ScalarFunc {
ctx: &mut EvalContext,
row: &[Datum],
) -> Result<Option<Cow<'a, [u8]>>> {
use byteorder::{ByteOrder, LittleEndian};
let input = try_opt!(self.children[0].eval_string(ctx, row));
if input.is_empty() {
return Ok(Some(Cow::Borrowed(b"")));
Expand Down Expand Up @@ -164,6 +162,7 @@ impl ScalarFunc {

#[inline]
pub fn uncompressed_length(&self, ctx: &mut EvalContext, row: &[Datum]) -> Result<Option<i64>> {
use byteorder::{ByteOrder, LittleEndian};
let input = try_opt!(self.children[0].eval_string(ctx, row));
if input.is_empty() {
return Ok(Some(0));
Expand Down
1 change: 1 addition & 0 deletions src/coprocessor/dag/expr/scalar_function.rs
Expand Up @@ -272,6 +272,7 @@ impl ScalarFunc {
| ScalarFuncSig::Trim1Arg
| ScalarFuncSig::FromBase64
| ScalarFuncSig::ToBase64
| ScalarFuncSig::WeekWithoutMode
| ScalarFuncSig::Space
| ScalarFuncSig::Compress
| ScalarFuncSig::Uncompress
Expand Down