You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "nathanielc (via GitHub)" <gi...@apache.org> on 2024/09/04 14:40:20 UTC

[I] Converting binary data to utf8 string [datafusion]

nathanielc opened a new issue, #12326:
URL: https://github.com/apache/datafusion/issues/12326

   ### Is your feature request related to a problem or challenge?
   
   I often work with a column with a binary type however its known that the binary data is a valid utf8 string. I'd like a mechanism to easily work with such data so that I can use other string scalar functions on the data.
   
   ### Describe the solution you'd like
   
   Add a scalar function to interpret a binary column as a utf8 string. This allows for explicit conversion between the data types.  The function could be named `str_from_utf8` or similar.
   
   How should non valid utf8 be handled. I see two options: 
   
   * Report an error thus causing the entire query to fail.
   * Return null for that row.
   
   I can see both being useful, does this mean we want two functions or a single function with a flag for its error behavior. Or is there a convention to follow for failable scalar functions?
   
   ### Describe alternatives you've considered
   
   We could also add an encoding format to the `encode` and `decode` functions as they already have the function signature of binary <-> utf8. However the existing formats are about encoding arbitrary bytes not about interpreting bytes as another format.
   
   
   
   ### Additional context
   
   I'd be happy to contribute this scalar function if we decide its a good solution to the problem.


-- 
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@datafusion.apache.org.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Converting binary data to utf8 string [datafusion]

Posted by "nathanielc (via GitHub)" <gi...@apache.org>.
nathanielc closed issue #12326: Converting binary data to utf8 string
URL: https://github.com/apache/datafusion/issues/12326


-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Converting binary data to utf8 string [datafusion]

Posted by "nathanielc (via GitHub)" <gi...@apache.org>.
nathanielc commented on issue #12326:
URL: https://github.com/apache/datafusion/issues/12326#issuecomment-2331907199

   @alamb Thanks I was expecting it to be builtin but didn't find it. Thanks for the docs. For now I am fine with the safe mode of returning nulls. 


-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Converting binary data to utf8 string [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on issue #12326:
URL: https://github.com/apache/datafusion/issues/12326#issuecomment-2331959083

   (note there is also the `CAST` syntax)
   ```sql
   > select cast(1 as VARCHAR);
   +----------+
   | Int64(1) |
   +----------+
   | 1        |
   +----------+
   1 row(s) fetched.
   Elapsed 0.011 seconds.
   ```
   
   Also if you want to cast to a specific arrow type you can use https://datafusion.apache.org/user-guide/sql/scalar_functions.html#arrow-cast


-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Converting binary data to utf8 string [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on issue #12326:
URL: https://github.com/apache/datafusion/issues/12326#issuecomment-2331287085

   > Add a scalar function to interpret a binary column as a utf8 string. This allows for explicit conversion between the data types. The function could be named str_from_utf8 or similar.
   
   I think you can convert binary to utf8 using a cast like `col::varchar`:
   
   ```sql
   > create table foo as values (X'123');
   0 row(s) fetched.
   Elapsed 0.011 seconds.
   
   > select * from foo;
   +---------+
   | column1 |
   +---------+
   | 0123    |
   +---------+
   1 row(s) fetched.
   Elapsed 0.004 seconds.
   
   > select arrow_typeof(column1) from foo;
   +---------------------------+
   | arrow_typeof(foo.column1) |
   +---------------------------+
   | Binary                    |
   +---------------------------+
   1 row(s) fetched.
   Elapsed 0.003 seconds.
   
   > select column1::varchar from foo;
   +-------------+
   | foo.column1 |
   +-------------+
   | #          |
   +-------------+
   1 row(s) fetched.
   Elapsed 0.002 seconds.
   ```
   
   However, I don't think this mechanism allows you to control the casting behavior (error or null), though the underlying arrow cast kernels do:
   
   https://docs.rs/arrow/latest/arrow/compute/fn.cast_with_options.html
   
   Specifically [CastOptions::safe](https://docs.rs/arrow/latest/arrow/compute/struct.CastOptions.html)


-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org