Skip to content

Commit

Permalink
feat: support hex string literal (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiKaiWi authored Jun 26, 2023
1 parent 13314c3 commit 5215fb6
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions datafusion/sql/src/expr/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use log::debug;
use sqlparser::ast::{DateTimeField, Expr as SQLExpr, Value};
use sqlparser::parser::ParserError::ParserError;
use std::collections::HashSet;
use std::num::ParseIntError;

impl<'a, S: ContextProvider> SqlToRel<'a, S> {
pub(crate) fn parse_value(
Expand All @@ -39,6 +40,12 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
Value::Placeholder(param) => {
Self::create_placeholder_expr(param, param_data_types)
}
Value::HexStringLiteral(s) => {
let bs = decode_hex(&s).map_err(|_| {
DataFusionError::Plan(format!("Invalid hex string literal:{s}"))
})?;
Ok(lit(bs))
}
_ => Err(DataFusionError::Plan(format!(
"Unsupported Value '{value:?}'",
))),
Expand Down Expand Up @@ -250,3 +257,10 @@ fn has_units(val: &str) -> bool {
|| val.ends_with("nanosecond")
|| val.ends_with("nanoseconds")
}

fn decode_hex(s: &str) -> Result<Vec<u8>, ParseIntError> {
(0..s.len())
.step_by(2)
.map(|i| u8::from_str_radix(&s[i..i + 2], 16))
.collect()
}

0 comments on commit 5215fb6

Please sign in to comment.