You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "ShiKaiWi (via GitHub)" <gi...@apache.org> on 2023/06/26 09:47:46 UTC

[GitHub] [arrow-datafusion] ShiKaiWi opened a new pull request, #6767: Support hex string literal

ShiKaiWi opened a new pull request, #6767:
URL: https://github.com/apache/arrow-datafusion/pull/6767

   # Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Closes #6764 .
   
   # Rationale for this change
   Datafusion supports the binary data type, but there is no way to insert binary data or query with binary data in a predicate through a sql. And this is caused by missing conversion from `sqlparser::ast::Value::HexStringLiteral` to `datafusion_expr::Expr::Literal(ScalarValue::Binary)`.
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   
   # What changes are included in this PR?
   Support the conversion from `sqlparser::ast::Value::HexStringLiteral` to `datafusion_expr::Expr::Literal(ScalarValue::Binary)`.
   <!--
   There is no need to duplicate the description in the issue here, but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   # Are these changes tested?
   Add a unit test for hex string literal decoding.
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   -->
   
   # Are there any user-facing changes?
   Now, the sql including hex string literal, e.g. `X'FF01'`, can be processed and treated as a binary data by datafusion.
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!--
   If there are any breaking changes to public APIs, please add the `api change` label.
   -->


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] alamb merged pull request #6767: Support hex string literal

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #6767:
URL: https://github.com/apache/arrow-datafusion/pull/6767


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] alamb commented on pull request #6767: Support hex string literal

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #6767:
URL: https://github.com/apache/arrow-datafusion/pull/6767#issuecomment-1609204971

   Something about this PR appears to be problematic:
   
   <img width="229" alt="Screenshot 2023-06-27 at 6 11 11 AM" src="https://github.com/apache/arrow-datafusion/assets/490673/2c8a1dbd-53b7-4e47-9c39-5de5820b1d97">
   
   @tustvold  is that what you were trying to fix?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] jackwener commented on a diff in pull request #6767: Support hex string literal

Posted by "jackwener (via GitHub)" <gi...@apache.org>.
jackwener commented on code in PR #6767:
URL: https://github.com/apache/arrow-datafusion/pull/6767#discussion_r1242172439


##########
datafusion/sql/src/expr/value.rs:
##########
@@ -357,3 +366,63 @@ fn has_units(val: &str) -> bool {
         || val.ends_with("nanosecond")
         || val.ends_with("nanoseconds")
 }
+
+/// Try to decode bytes from hex literal string.
+///
+/// None will be returned if the input literal is hex-invalid.
+fn try_decode_hex_literal(s: &str) -> Option<Vec<u8>> {
+    let hex_bytes = s.as_bytes();
+
+    let mut decoded_bytes = Vec::with_capacity((hex_bytes.len() + 1) / 2);
+
+    let start_idx = hex_bytes.len() % 2;
+    if start_idx > 0 {
+        // The first byte is formed of only one char.
+        decoded_bytes.push(try_decode_hex_char(hex_bytes[0])?);
+    }
+
+    for i in (start_idx..hex_bytes.len()).step_by(2) {
+        let high = try_decode_hex_char(hex_bytes[i])?;
+        let low = try_decode_hex_char(hex_bytes[i + 1])?;
+        decoded_bytes.push(high << 4 | low);
+    }
+
+    Some(decoded_bytes)

Review Comment:
   ```suggestion
       Ok(decoded_bytes)
   ```



##########
datafusion/sql/src/expr/value.rs:
##########
@@ -357,3 +366,63 @@ fn has_units(val: &str) -> bool {
         || val.ends_with("nanosecond")
         || val.ends_with("nanoseconds")
 }
+
+/// Try to decode bytes from hex literal string.
+///
+/// None will be returned if the input literal is hex-invalid.
+fn try_decode_hex_literal(s: &str) -> Option<Vec<u8>> {

Review Comment:
   ```suggestion
   fn try_decode_hex_literal(s: &str) -> Result<Vec<u8>> {
   ```



##########
datafusion/sql/src/expr/value.rs:
##########
@@ -357,3 +366,63 @@ fn has_units(val: &str) -> bool {
         || val.ends_with("nanosecond")
         || val.ends_with("nanoseconds")
 }
+
+/// Try to decode bytes from hex literal string.
+///
+/// None will be returned if the input literal is hex-invalid.
+fn try_decode_hex_literal(s: &str) -> Option<Vec<u8>> {
+    let hex_bytes = s.as_bytes();
+
+    let mut decoded_bytes = Vec::with_capacity((hex_bytes.len() + 1) / 2);
+
+    let start_idx = hex_bytes.len() % 2;
+    if start_idx > 0 {
+        // The first byte is formed of only one char.
+        decoded_bytes.push(try_decode_hex_char(hex_bytes[0])?);
+    }
+
+    for i in (start_idx..hex_bytes.len()).step_by(2) {
+        let high = try_decode_hex_char(hex_bytes[i])?;
+        let low = try_decode_hex_char(hex_bytes[i + 1])?;
+        decoded_bytes.push(high << 4 | low);
+    }
+
+    Some(decoded_bytes)
+}
+
+/// Try to decode a byte from a hex char.
+///
+/// None will be returned if the input char is hex-invalid.
+const fn try_decode_hex_char(c: u8) -> Option<u8> {
+    match c {
+        b'A'..=b'F' => Some(c - b'A' + 10),
+        b'a'..=b'f' => Some(c - b'a' + 10),
+        b'0'..=b'9' => Some(c - b'0'),
+        _ => None,
+    }
+}

Review Comment:
   ```suggestion
   fn try_decode_hex_char(c: u8) -> Result<u8> {
       match c {
           b'A'..=b'F' => Ok(c - b'A' + 10),
           b'a'..=b'f' => Ok(c - b'a' + 10),
           b'0'..=b'9' => Ok(c - b'0'),
           _ => Err(DataFusionError::Plan(format!(
               "Invalid hex character: {}",
               c as char
           ))),
       }
   }
   ```



##########
datafusion/sql/src/expr/value.rs:
##########
@@ -357,3 +366,63 @@ fn has_units(val: &str) -> bool {
         || val.ends_with("nanosecond")
         || val.ends_with("nanoseconds")
 }
+
+/// Try to decode bytes from hex literal string.
+///
+/// None will be returned if the input literal is hex-invalid.
+fn try_decode_hex_literal(s: &str) -> Option<Vec<u8>> {
+    let hex_bytes = s.as_bytes();
+
+    let mut decoded_bytes = Vec::with_capacity((hex_bytes.len() + 1) / 2);
+
+    let start_idx = hex_bytes.len() % 2;
+    if start_idx > 0 {
+        // The first byte is formed of only one char.
+        decoded_bytes.push(try_decode_hex_char(hex_bytes[0])?);
+    }
+
+    for i in (start_idx..hex_bytes.len()).step_by(2) {
+        let high = try_decode_hex_char(hex_bytes[i])?;
+        let low = try_decode_hex_char(hex_bytes[i + 1])?;
+        decoded_bytes.push(high << 4 | low);
+    }
+
+    Some(decoded_bytes)
+}
+
+/// Try to decode a byte from a hex char.
+///
+/// None will be returned if the input char is hex-invalid.
+const fn try_decode_hex_char(c: u8) -> Option<u8> {
+    match c {
+        b'A'..=b'F' => Some(c - b'A' + 10),
+        b'a'..=b'f' => Some(c - b'a' + 10),
+        b'0'..=b'9' => Some(c - b'0'),
+        _ => None,
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_decode_hex_literal() {
+        let cases = [
+            ("", Some(vec![])),
+            ("FF00", Some(vec![255, 0])),
+            ("a00a", Some(vec![160, 10])),
+            ("FF0", Some(vec![15, 240])),
+            ("f", Some(vec![15])),
+            ("FF0X", None),
+            ("X0", None),
+            ("XX", None),
+            ("x", None),
+        ];
+
+        for (input, expect) in cases {
+            let output = try_decode_hex_literal(input);

Review Comment:
   ```suggestion
               let output = try_decode_hex_literal(input).map_or(None, |v| Some(v));
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] ShiKaiWi commented on a diff in pull request #6767: Support hex string literal

Posted by "ShiKaiWi (via GitHub)" <gi...@apache.org>.
ShiKaiWi commented on code in PR #6767:
URL: https://github.com/apache/arrow-datafusion/pull/6767#discussion_r1243079994


##########
datafusion/sql/src/expr/value.rs:
##########
@@ -357,3 +366,63 @@ fn has_units(val: &str) -> bool {
         || val.ends_with("nanosecond")
         || val.ends_with("nanoseconds")
 }
+
+/// Try to decode bytes from hex literal string.
+///
+/// None will be returned if the input literal is hex-invalid.
+fn try_decode_hex_literal(s: &str) -> Option<Vec<u8>> {

Review Comment:
   I guess the error detail of the decode failure is not necessary because the original invalid hex string literal is enough for troubleshooting if decode fails.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] tustvold commented on pull request #6767: Support hex string literal

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on PR #6767:
URL: https://github.com/apache/arrow-datafusion/pull/6767#issuecomment-1609281311

   > @tustvold is that what you were trying to fix?
   
   Yes, I accidentally force pushed main, it should be reverted now and #6775 should prevent this in future, but the PR dialog appears to be a tad confused by this


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] tustvold closed pull request #6767: Support hex string literal

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold closed pull request #6767: Support hex string literal
URL: https://github.com/apache/arrow-datafusion/pull/6767


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] jackwener commented on pull request #6767: Support hex string literal

Posted by "jackwener (via GitHub)" <gi...@apache.org>.
jackwener commented on PR #6767:
URL: https://github.com/apache/arrow-datafusion/pull/6767#issuecomment-1607453304

   Can you add a `sqllogicaltest` for this case? @ShiKaiWi 
   You can see directory `datafusion/core/tests/sqllogictests`


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] alamb commented on pull request #6767: Support hex string literal

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #6767:
URL: https://github.com/apache/arrow-datafusion/pull/6767#issuecomment-1610012922

   https://github.com/apache/arrow-datafusion/pull/6770 is now ready for review too (with some basic .slt tests)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org