Skip to content
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
7 changes: 5 additions & 2 deletions src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,10 +459,13 @@ mod tests {
}

#[test]
fn arithmetic_scalar(){
fn arithmetic_scalar() {
let qs = "56";
let res = arithmetic(qs.as_bytes());
assert!(res.is_err());
assert_eq!(nom::Err::Error(nom::error::Error::new(qs.as_bytes(), ErrorKind::Tag)), res.err().unwrap());
assert_eq!(
nom::Err::Error(nom::error::Error::new(qs.as_bytes(), ErrorKind::Tag)),
res.err().unwrap()
);
}
}
91 changes: 56 additions & 35 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ where
let (inp, _) = first.parse(inp)?;
let (inp, o2) = second.parse(inp)?;
third.parse(inp).map(|(i, _)| (i, o2))
},
}
}
}
}
Expand Down Expand Up @@ -641,7 +641,8 @@ pub fn function_argument_parser(i: &[u8]) -> IResult<&[u8], FunctionArgument> {
// present.
pub fn function_arguments(i: &[u8]) -> IResult<&[u8], (FunctionArgument, bool)> {
let distinct_parser = opt(tuple((tag_no_case("distinct"), multispace1)));
let (remaining_input, (distinct, args)) = tuple((distinct_parser, function_argument_parser))(i)?;
let (remaining_input, (distinct, args)) =
tuple((distinct_parser, function_argument_parser))(i)?;
Ok((remaining_input, (args, distinct.is_some())))
}

Expand Down Expand Up @@ -695,12 +696,25 @@ pub fn column_function(i: &[u8]) -> IResult<&[u8], FunctionExpression> {
FunctionExpression::GroupConcat(FunctionArgument::Column(col.clone()), sep)
},
),
map(tuple((sql_identifier, multispace0, tag("("), separated_list0(tag(","), delimited(multispace0, function_argument_parser, multispace0)), tag(")"))), |tuple| {
let (name, _, _, arguments, _) = tuple;
FunctionExpression::Generic(
str::from_utf8(name).unwrap().to_string(),
FunctionArguments::from(arguments))
})
map(
tuple((
sql_identifier,
multispace0,
tag("("),
separated_list0(
tag(","),
delimited(multispace0, function_argument_parser, multispace0),
),
tag(")"),
)),
|tuple| {
let (name, _, _, arguments, _) = tuple;
FunctionExpression::Generic(
str::from_utf8(name).unwrap().to_string(),
FunctionArguments::from(arguments),
)
},
),
))(i)
}

Expand Down Expand Up @@ -1021,22 +1035,23 @@ pub fn value_list(i: &[u8]) -> IResult<&[u8], Vec<Literal>> {
// Parse a reference to a named schema.table, with an optional alias
pub fn schema_table_reference(i: &[u8]) -> IResult<&[u8], Table> {
map(
tuple((
opt(pair(sql_identifier, tag("."))),
sql_identifier,
opt(as_alias)
)),
|tup| Table {
name: String::from(str::from_utf8(tup.1).unwrap()),
alias: match tup.2 {
Some(a) => Some(String::from(a)),
None => None,
},
schema: match tup.0 {
Some((schema, _)) => Some(String::from(str::from_utf8(schema).unwrap())),
None => None,
tuple((
opt(pair(sql_identifier, tag("."))),
sql_identifier,
opt(as_alias),
)),
|tup| Table {
name: String::from(str::from_utf8(tup.1).unwrap()),
alias: match tup.2 {
Some(a) => Some(String::from(a)),
None => None,
},
schema: match tup.0 {
Some((schema, _)) => Some(String::from(str::from_utf8(schema).unwrap())),
None => None,
},
},
})(i)
)(i)
}

// Parse a reference to a named table, with an optional alias
Expand All @@ -1047,7 +1062,7 @@ pub fn table_reference(i: &[u8]) -> IResult<&[u8], Table> {
Some(a) => Some(String::from(a)),
None => None,
},
schema: None,
schema: None,
})(i)
}

Expand Down Expand Up @@ -1137,25 +1152,31 @@ mod tests {
name: String::from("max(addr_id)"),
alias: None,
table: None,
function: Some(Box::new(FunctionExpression::Max(
FunctionArgument::Column(Column::from("addr_id")),
))),
function: Some(Box::new(FunctionExpression::Max(FunctionArgument::Column(
Column::from("addr_id"),
)))),
};
assert_eq!(res.unwrap().1, expected);
}

#[test]
fn simple_generic_function() {
let qlist = ["coalesce(a,b,c)".as_bytes(), "coalesce (a,b,c)".as_bytes(), "coalesce(a ,b,c)".as_bytes(), "coalesce(a, b,c)".as_bytes()];
let qlist = [
"coalesce(a,b,c)".as_bytes(),
"coalesce (a,b,c)".as_bytes(),
"coalesce(a ,b,c)".as_bytes(),
"coalesce(a, b,c)".as_bytes(),
];
for q in qlist.iter() {
let res = column_function(q);
let expected = FunctionExpression::Generic("coalesce".to_string(),
FunctionArguments::from(
vec!(
FunctionArgument::Column(Column::from("a")),
FunctionArgument::Column(Column::from("b")),
FunctionArgument::Column(Column::from("c"))
)));
let expected = FunctionExpression::Generic(
"coalesce".to_string(),
FunctionArguments::from(vec![
FunctionArgument::Column(Column::from("a")),
FunctionArgument::Column(Column::from("b")),
FunctionArgument::Column(Column::from("c")),
]),
);
assert_eq!(res, Ok((&b""[..], expected)));
}
}
Expand Down
Loading