Skip to content

Commit

Permalink
Support Decimal{32,64}(x) column type specifiers
Browse files Browse the repository at this point in the history
(cherry picked from commit 78423ef)
  • Loading branch information
polachok authored and suharev7 committed Feb 5, 2020
1 parent f546252 commit 8b79c04
Showing 1 changed file with 68 additions and 25 deletions.
93 changes: 68 additions & 25 deletions src/types/column/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ macro_rules! match_str {
}) => {
$(
$(
if ($arg.eq_ignore_ascii_case($var)) {
if $arg.eq_ignore_ascii_case($var) {
$doit
} else
)*
Expand Down Expand Up @@ -174,39 +174,81 @@ fn parse_decimal(source: &str) -> Option<(u8, u8, NoBits)> {
return None;
}

if source.chars().nth(7) != Some('(') {
return None;
}
let mut nobits = None;
let mut precision = None;
let mut scale = None;

if !source.ends_with(')') {
return None;
}
let mut params_indexes = (None, None);

let mut params: Vec<u8> = Vec::with_capacity(2);
for cell in source[8..source.len() - 1].split(',').map(|s| s.trim()) {
if let Ok(value) = cell.parse() {
params.push(value)
} else {
return None;
for (idx, byte) in source.as_bytes().iter().enumerate() {
if *byte == b'(' {
match &source.as_bytes()[..idx] {
b"Decimal" => {}
b"Decimal32" => {
nobits = Some(NoBits::N32);
}
b"Decimal64" => {
nobits = Some(NoBits::N64);
}
_ => return None,
}
params_indexes.0 = Some(idx);
}
if *byte == b')' {
params_indexes.1 = Some(idx);
}
}

if params.len() != 2 {
return None;
}

let precision = params[0];
let scale = params[1];
let params_indexes = match params_indexes {
(Some(start), Some(end)) => (start, end),
_ => return None,
};

if scale > precision {
return None;
match nobits {
Some(_) => {
scale = std::str::from_utf8(&source.as_bytes()[params_indexes.0 + 1..params_indexes.1])
.unwrap()
.parse()
.ok()
}
None => {
for (idx, cell) in
std::str::from_utf8(&source.as_bytes()[params_indexes.0 + 1..params_indexes.1])
.unwrap()
.split(',')
.map(|s| s.trim())
.enumerate()
{
match idx {
0 => precision = cell.parse().ok(),
1 => scale = cell.parse().ok(),
_ => return None,
}
}
}
}

if let Some(nobits) = NoBits::from_precision(precision) {
return Some((precision, scale, nobits));
}
match (precision, scale, nobits) {
(Some(precision), Some(scale), None) => {
if scale > precision {
return None;
}

None
if let Some(nobits) = NoBits::from_precision(precision) {
Some((precision, scale, nobits))
} else {
None
}
}
(None, Some(scale), Some(bits)) => {
let precision = match bits {
NoBits::N32 => 9,
NoBits::N64 => 18,
};
Some((precision, scale, bits))
}
_ => None,
}
}

#[cfg(test)]
Expand All @@ -223,6 +265,7 @@ mod test {
assert_eq!(parse_decimal("Decimal(20, -4)"), None);
assert_eq!(parse_decimal("Decimal(0)"), None);
assert_eq!(parse_decimal("Decimal(1, 2, 3)"), None);
assert_eq!(parse_decimal("Decimal64(9)"), Some((18, 9, NoBits::N64)));
}

#[test]
Expand Down

0 comments on commit 8b79c04

Please sign in to comment.