This is an automated email from the ASF dual-hosted git repository.
JingsongLi pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/paimon-rust.git
The following commit(s) were added to refs/heads/main by this push:
new d483f33a feat(datafusion): add common scalar compatibility functions
(#1291)
d483f33a is described below
commit d483f33ad48dbd712f9f3a9c497959222b10e046
Author: Jingsong Lee <[email protected]>
AuthorDate: Mon Nov 2 09:02:27 2026 +0800
feat(datafusion): add common scalar compatibility functions (#1291)
---
.../datafusion/src/compat_functions.rs | 1 +
crates/integrations/datafusion/src/lib.rs | 1 +
.../integrations/datafusion/src/scalar_compat.rs | 438 +++++++++++++++++++++
crates/integrations/datafusion/src/sql_context.rs | 1 +
.../integrations/datafusion/tests/scalar_compat.rs | 159 ++++++++
docs/src/sql.md | 19 +
6 files changed, 619 insertions(+)
diff --git a/crates/integrations/datafusion/src/compat_functions.rs
b/crates/integrations/datafusion/src/compat_functions.rs
index 3e9f5365..b049c200 100644
--- a/crates/integrations/datafusion/src/compat_functions.rs
+++ b/crates/integrations/datafusion/src/compat_functions.rs
@@ -40,6 +40,7 @@ use sha2::{Digest, Sha224, Sha256, Sha384, Sha512};
pub(crate) fn register_compat_functions(ctx: &SessionContext) {
for (name, alias) in [
("current_date", "curdate"),
+ ("to_date", "date"),
("to_timestamp_millis", "timestamp_millis"),
("to_timestamp", "to_timestamp_ltz"),
] {
diff --git a/crates/integrations/datafusion/src/lib.rs
b/crates/integrations/datafusion/src/lib.rs
index 6718045d..6809f9bd 100644
--- a/crates/integrations/datafusion/src/lib.rs
+++ b/crates/integrations/datafusion/src/lib.rs
@@ -63,6 +63,7 @@ mod physical_plan;
mod procedures;
mod relation_planner;
pub mod runtime;
+mod scalar_compat;
mod show_metadata;
mod sql_context;
mod sql_function;
diff --git a/crates/integrations/datafusion/src/scalar_compat.rs
b/crates/integrations/datafusion/src/scalar_compat.rs
new file mode 100644
index 00000000..cdea053a
--- /dev/null
+++ b/crates/integrations/datafusion/src/scalar_compat.rs
@@ -0,0 +1,438 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use datafusion::arrow::array::{
+ Array, ArrayRef, BinaryBuilder, FixedSizeListArray, Int32Array,
Int64Array, LargeListArray,
+ ListArray, ListBuilder, MapArray, StringArray, StringBuilder,
+};
+use datafusion::arrow::compute::{
+ cast,
+ kernels::temporal::{date_part, DatePart},
+};
+use datafusion::arrow::datatypes::{DataType, Field, TimeUnit};
+use datafusion::common::{DataFusionError, Result, ScalarValue};
+use datafusion::logical_expr::{
+ ColumnarValue, ScalarFunctionArgs, ScalarUDF, ScalarUDFImpl, Signature,
TypeSignature,
+ Volatility,
+};
+use datafusion::prelude::SessionContext;
+use regex::Regex;
+use std::collections::HashMap;
+use std::sync::Arc;
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
+enum Kind {
+ Split,
+ Locate,
+ Hex,
+ Unhex,
+ Typeof,
+ Size,
+ Hour,
+}
+#[derive(Debug, PartialEq, Eq, Hash)]
+struct Function {
+ kind: Kind,
+ signature: Signature,
+}
+pub(crate) fn register(ctx: &SessionContext) {
+ for kind in [
+ Kind::Split,
+ Kind::Locate,
+ Kind::Hex,
+ Kind::Unhex,
+ Kind::Typeof,
+ Kind::Size,
+ Kind::Hour,
+ ] {
+ let signature = match kind {
+ Kind::Split | Kind::Locate => Signature::one_of(
+ vec![
+ TypeSignature::Exact(vec![DataType::Utf8, DataType::Utf8]),
+ TypeSignature::Exact(vec![DataType::Utf8, DataType::Utf8,
DataType::Int64]),
+ ],
+ Volatility::Immutable,
+ ),
+ Kind::Unhex => Signature::exact(vec![DataType::Utf8],
Volatility::Immutable),
+ Kind::Typeof => Signature::any(1, Volatility::Immutable),
+ _ => Signature::user_defined(Volatility::Immutable),
+ };
+ ctx.register_udf(ScalarUDF::from(Function { kind, signature }));
+ }
+}
+fn decoded_type(mut ty: &DataType) -> &DataType {
+ while let DataType::Dictionary(_, value) = ty {
+ ty = value;
+ }
+ ty
+}
+fn error(message: &str) -> DataFusionError {
+ DataFusionError::Execution(message.into())
+}
+impl ScalarUDFImpl for Function {
+ fn name(&self) -> &str {
+ match self.kind {
+ Kind::Split => "split",
+ Kind::Locate => "locate",
+ Kind::Hex => "hex",
+ Kind::Unhex => "unhex",
+ Kind::Typeof => "typeof",
+ Kind::Size => "size",
+ Kind::Hour => "hour",
+ }
+ }
+ fn signature(&self) -> &Signature {
+ &self.signature
+ }
+ fn coerce_types(&self, types: &[DataType]) -> Result<Vec<DataType>> {
+ let [ty] = types else {
+ return Err(error("Expected one argument"));
+ };
+ let ty = decoded_type(ty);
+ let output = match self.kind {
+ Kind::Hex
+ if matches!(
+ ty,
+ DataType::Utf8 | DataType::LargeUtf8 | DataType::Utf8View
| DataType::Null
+ ) =>
+ {
+ DataType::Utf8
+ }
+ Kind::Hex if ty.is_binary() => DataType::Binary,
+ Kind::Hex if ty.is_integer() => ty.clone(),
+ Kind::Size
+ if matches!(
+ ty,
+ DataType::List(_)
+ | DataType::LargeList(_)
+ | DataType::FixedSizeList(_, _)
+ | DataType::Map(_, _)
+ | DataType::Null
+ ) =>
+ {
+ ty.clone()
+ }
+ Kind::Hour
+ if matches!(
+ ty,
+ DataType::Date32
+ | DataType::Date64
+ | DataType::Timestamp(_, _)
+ | DataType::Time32(_)
+ | DataType::Time64(_)
+ ) =>
+ {
+ ty.clone()
+ }
+ Kind::Hour
+ if matches!(
+ ty,
+ DataType::Utf8 | DataType::LargeUtf8 | DataType::Utf8View
| DataType::Null
+ ) =>
+ {
+ DataType::Timestamp(TimeUnit::Microsecond, None)
+ }
+ _ => {
+ return Err(DataFusionError::Plan(format!(
+ "Unsupported {} input type: {ty}",
+ self.name()
+ )))
+ }
+ };
+ Ok(vec![output])
+ }
+ fn return_type(&self, _: &[DataType]) -> Result<DataType> {
+ Ok(match self.kind {
+ Kind::Split => DataType::List(Arc::new(Field::new("item",
DataType::Utf8, true))),
+ Kind::Locate | Kind::Size | Kind::Hour => DataType::Int32,
+ Kind::Unhex => DataType::Binary,
+ _ => DataType::Utf8,
+ })
+ }
+ fn invoke_with_args(&self, args: ScalarFunctionArgs) ->
Result<ColumnarValue> {
+ if self.kind == Kind::Typeof {
+ return Ok(ColumnarValue::Scalar(ScalarValue::Utf8(Some(sql_type(
+ &args.args[0].data_type(),
+ )))));
+ }
+ let arrays = ColumnarValue::values_to_arrays(&args.args)?;
+ let output: ArrayRef = match self.kind {
+ Kind::Hour => {
+ if matches!(arrays[0].data_type(), DataType::Date32 |
DataType::Date64) {
+ Arc::new(Int32Array::from_iter(
+ (0..arrays[0].len()).map(|i|
(!arrays[0].is_null(i)).then_some(0)),
+ ))
+ } else {
+ Arc::new(date_part(arrays[0].as_ref(), DatePart::Hour)?)
+ }
+ }
+ Kind::Size => {
+ let a = &arrays[0];
+ let values = (0..a.len())
+ .map(|i| {
+ if a.is_null(i) {
+ return Ok(None);
+ }
+ let n = match a.data_type() {
+ DataType::List(_) => {
+ a.as_any()
+ .downcast_ref::<ListArray>()
+ .unwrap()
+ .value_length(i) as i64
+ }
+ DataType::LargeList(_) => a
+ .as_any()
+ .downcast_ref::<LargeListArray>()
+ .unwrap()
+ .value_length(i),
+ DataType::FixedSizeList(_, _) => {
+ a.as_any()
+ .downcast_ref::<FixedSizeListArray>()
+ .unwrap()
+ .value_length() as i64
+ }
+ DataType::Map(_, _) => {
+ a.as_any()
+ .downcast_ref::<MapArray>()
+ .unwrap()
+ .value_length(i) as i64
+ }
+ DataType::Null => return Ok(None),
+ _ => return Err(error("size expects an array or
map")),
+ };
+ i32::try_from(n)
+ .map(Some)
+ .map_err(|_| error("Collection size exceeds INT
range"))
+ })
+ .collect::<Result<Vec<_>>>()?;
+ Arc::new(Int32Array::from(values))
+ }
+ Kind::Split => {
+ let text =
arrays[0].as_any().downcast_ref::<StringArray>().unwrap();
+ let patterns =
arrays[1].as_any().downcast_ref::<StringArray>().unwrap();
+ let mut output = ListBuilder::new(StringBuilder::new());
+ let mut cache = HashMap::new();
+ for i in 0..text.len() {
+ if arrays.iter().any(|a| a.is_null(i)) {
+ output.append(false);
+ continue;
+ }
+ let pattern = patterns.value(i);
+ if !cache.contains_key(pattern) {
+ cache.insert(
+ pattern,
+ Regex::new(pattern)
+ .map_err(|e| error(&format!("Invalid split
regex: {e}")))?,
+ );
+ }
+ let limit = arrays
+ .get(2)
+ .map(|a|
a.as_any().downcast_ref::<Int64Array>().unwrap().value(i))
+ .unwrap_or(-1);
+ let value = text.value(i);
+ if pattern.is_empty() && !value.is_empty() {
+ let mut start = 0;
+ for (count, (offset, ch)) in
value.char_indices().enumerate() {
+ if limit > 0 && count as i64 >= limit - 1 {
+ break;
+ }
+ output.values().append_value(ch.to_string());
+ start = offset + ch.len_utf8();
+ }
+ if start < value.len() {
+ output.values().append_value(&value[start..]);
+ }
+ } else {
+ let mut start = 0;
+ let mut count = 0;
+ for m in cache[pattern].find_iter(value) {
+ if m.start() == 0 && m.end() == 0 {
+ continue;
+ }
+ if limit > 0 && count >= limit - 1 {
+ break;
+ }
+
output.values().append_value(&value[start..m.start()]);
+ start = m.end();
+ count += 1;
+ }
+ output.values().append_value(&value[start..]);
+ }
+ output.append(true);
+ }
+ Arc::new(output.finish())
+ }
+ Kind::Locate => {
+ let needle =
arrays[0].as_any().downcast_ref::<StringArray>().unwrap();
+ let haystack =
arrays[1].as_any().downcast_ref::<StringArray>().unwrap();
+ let mut values = Vec::new();
+ for i in 0..needle.len() {
+ if arrays.iter().any(|a| a.is_null(i)) {
+ values.push(None);
+ continue;
+ }
+ let start = arrays
+ .get(2)
+ .map(|a|
a.as_any().downcast_ref::<Int64Array>().unwrap().value(i))
+ .unwrap_or(1);
+ let text = haystack.value(i);
+ let offset = if start > 0 {
+ text.char_indices()
+ .map(|(i, _)| i)
+ .chain(std::iter::once(text.len()))
+ .nth((start - 1) as usize)
+ } else {
+ None
+ };
+ let position = offset
+ .and_then(|offset| {
+ text[offset..]
+ .find(needle.value(i))
+ .map(|p| text[..offset + p].chars().count() +
1)
+ })
+ .unwrap_or(0);
+ values.push(Some(
+ i32::try_from(position)
+ .map_err(|_| error("locate result exceeds INT
range"))?,
+ ));
+ }
+ Arc::new(Int32Array::from(values))
+ }
+ Kind::Unhex => {
+ let text =
arrays[0].as_any().downcast_ref::<StringArray>().unwrap();
+ let mut output = BinaryBuilder::new();
+ for value in text.iter() {
+ let Some(value) = value else {
+ output.append_null();
+ continue;
+ };
+ if !value.bytes().all(|byte| byte.is_ascii_hexdigit()) {
+ output.append_null();
+ continue;
+ }
+ let value = if value.len() % 2 == 1 {
+ format!("0{value}")
+ } else {
+ value.to_string()
+ };
+ let decoded = (0..value.len())
+ .step_by(2)
+ .map(|i| u8::from_str_radix(&value[i..i + 2], 16))
+ .collect::<std::result::Result<Vec<_>, _>>();
+ match decoded {
+ Ok(bytes) => output.append_value(bytes),
+ Err(_) => output.append_null(),
+ }
+ }
+ Arc::new(output.finish())
+ }
+ Kind::Hex => {
+ let mut output = StringBuilder::new();
+ for i in 0..arrays[0].len() {
+ if arrays[0].is_null(i) {
+ output.append_null();
+ continue;
+ }
+ let scalar =
ScalarValue::try_from_array(arrays[0].as_ref(), i)?;
+ let text = if let Some(Some(s)) = scalar.try_as_str() {
+ s.as_bytes()
+ .iter()
+ .map(|b| format!("{b:02X}"))
+ .collect::<String>()
+ } else if let ScalarValue::Binary(Some(bytes)) = scalar {
+ bytes.iter().map(|b|
format!("{b:02X}")).collect::<String>()
+ } else {
+ let value = cast(&arrays[0].slice(i, 1),
&DataType::UInt64)?;
+ let unsigned = value
+ .as_any()
+
.downcast_ref::<datafusion::arrow::array::UInt64Array>()
+ .unwrap();
+ if unsigned.is_null(0) {
+ let value = cast(&arrays[0].slice(i, 1),
&DataType::Int64)?;
+ let signed =
value.as_any().downcast_ref::<Int64Array>().unwrap();
+ format!("{:X}", signed.value(0))
+ } else {
+ format!("{:X}", unsigned.value(0))
+ }
+ };
+ output.append_value(text);
+ }
+ Arc::new(output.finish())
+ }
+ Kind::Typeof => unreachable!(),
+ };
+ if args
+ .args
+ .iter()
+ .all(|a| matches!(a, ColumnarValue::Scalar(_)))
+ {
+ Ok(ColumnarValue::Scalar(ScalarValue::try_from_array(
+ output.as_ref(),
+ 0,
+ )?))
+ } else {
+ Ok(ColumnarValue::Array(output))
+ }
+ }
+}
+fn sql_type(ty: &DataType) -> String {
+ if decoded_type(ty) == &paimon::arrow::variant_arrow_type() {
+ return "variant".into();
+ }
+ match decoded_type(ty) {
+ DataType::Null => "void".into(),
+ DataType::Boolean => "boolean".into(),
+ DataType::Int8 => "tinyint".into(),
+ DataType::Int16 => "smallint".into(),
+ DataType::Int32 => "int".into(),
+ DataType::Int64 => "bigint".into(),
+ DataType::Float32 => "float".into(),
+ DataType::Float64 => "double".into(),
+ DataType::Utf8 | DataType::LargeUtf8 | DataType::Utf8View =>
"string".into(),
+ ty if ty.is_binary() => "binary".into(),
+ DataType::Date32 | DataType::Date64 => "date".into(),
+ DataType::Timestamp(_, None) => "timestamp_ntz".into(),
+ DataType::Timestamp(_, Some(_)) => "timestamp".into(),
+ DataType::Decimal32(p, s)
+ | DataType::Decimal64(p, s)
+ | DataType::Decimal128(p, s)
+ | DataType::Decimal256(p, s) => format!("decimal({p},{s})"),
+ DataType::List(f) | DataType::LargeList(f) |
DataType::FixedSizeList(f, _) => {
+ format!("array<{}>", sql_type(f.data_type()))
+ }
+ DataType::Map(f, _) => {
+ if let DataType::Struct(fields) = f.data_type() {
+ format!(
+ "map<{},{}>",
+ sql_type(fields[0].data_type()),
+ sql_type(fields[1].data_type())
+ )
+ } else {
+ ty.to_string()
+ }
+ }
+ DataType::Struct(fields) => format!(
+ "struct<{}>",
+ fields
+ .iter()
+ .map(|f| format!("{}:{}", f.name(), sql_type(f.data_type())))
+ .collect::<Vec<_>>()
+ .join(",")
+ ),
+ other => other.to_string().to_lowercase(),
+ }
+}
diff --git a/crates/integrations/datafusion/src/sql_context.rs
b/crates/integrations/datafusion/src/sql_context.rs
index 38594167..c36c9f90 100644
--- a/crates/integrations/datafusion/src/sql_context.rs
+++ b/crates/integrations/datafusion/src/sql_context.rs
@@ -170,6 +170,7 @@ impl SQLContextBuilder {
let state = state_builder.build();
let ctx = SessionContext::new_with_state(state);
crate::compat_functions::register_compat_functions(&ctx);
+ crate::scalar_compat::register(&ctx);
crate::blob_descriptor_functions::register_blob_descriptor_functions(&ctx);
crate::variant_functions::register_variant_functions(&ctx);
crate::json_functions::register_json_functions(&ctx);
diff --git a/crates/integrations/datafusion/tests/scalar_compat.rs
b/crates/integrations/datafusion/tests/scalar_compat.rs
new file mode 100644
index 00000000..11e5f7a7
--- /dev/null
+++ b/crates/integrations/datafusion/tests/scalar_compat.rs
@@ -0,0 +1,159 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use datafusion::common::ScalarValue;
+use paimon_datafusion::SQLContext;
+#[tokio::test]
+async fn common_scalar_names_have_explicit_semantics() {
+ let ctx = SQLContext::new();
+ for expression in [
+ "split('a,b,c', ',', 2) = ARRAY['a', 'b,c']",
+ "split('a b', ' +') = ARRAY['a','b']",
+ "locate('汉', 'a汉字汉', 3) = 4",
+ "locate('x', 'abc') = 0",
+ "date('2026-10-29') = DATE '2026-10-29'",
+ "typeof(CAST(1 AS BIGINT)) = 'bigint'",
+ "hex('汉') = 'E6B189'",
+ "unhex('f') = decode('0f','hex')",
+ "unhex('zz') IS NULL",
+ "size(ARRAY[1,NULL,3]) = 3",
+ "size(CAST(NULL AS INT[])) IS NULL",
+ "hour(TIMESTAMP '2026-10-29 12:34:56') = 12",
+ ] {
+ let batches = ctx
+ .sql(&format!("SELECT {expression}"))
+ .await
+ .unwrap()
+ .collect()
+ .await
+ .unwrap();
+ assert_eq!(
+ ScalarValue::try_from_array(batches[0].column(0), 0).unwrap(),
+ ScalarValue::Boolean(Some(true)),
+ "{expression}"
+ );
+ }
+}
+
+#[tokio::test]
+async fn scalar_boundaries_cover_nulls_encoding_and_temporal_types() {
+ let ctx = SQLContext::new();
+ for expression in [
+ "split('a,b,', ',', 0) = ARRAY['a','b','']",
+ "split('汉字', '') = ARRAY['汉','字']",
+ "split('abc', '', 2) = ARRAY['a','bc']",
+ "split('abc', '^') = ARRAY['abc']",
+ "split(NULL, ',') IS NULL",
+ "locate('', 'abc', 4) = 4",
+ "locate('a', 'abc', 0) = 0",
+ "locate('a', 'abc', 9223372036854775807) = 0",
+ "locate(NULL, 'abc') IS NULL",
+ "hex(-1) = 'FFFFFFFFFFFFFFFF'",
+ "hex(decode('00ff','hex')) = '00FF'",
+ "hex(NULL) IS NULL",
+ "unhex('+f') IS NULL",
+ "unhex('0+f') IS NULL",
+ "unhex('aBc') = decode('0abc','hex')",
+ "unhex('') = decode('','hex')",
+ "unhex(NULL) IS NULL",
+ "typeof(parse_json('{}')) = 'variant'",
+ "typeof(ARRAY[CAST(1 AS INT)]) = 'array<int>'",
+ "typeof(CAST(NULL AS DECIMAL(10,2))) = 'decimal(10,2)'",
+ "size(map(ARRAY[1,2], ARRAY['a',NULL])) = 2",
+ "size(ARRAY[]) = 0",
+ "size(NULL) IS NULL",
+ "hour(TIME '12:34:56') = 12",
+ "hour(DATE '2026-10-29') = 0",
+ "hour(NULL) IS NULL",
+ ] {
+ let batches = ctx
+ .sql(&format!("SELECT {expression}"))
+ .await
+ .unwrap_or_else(|e| panic!("{expression}: {e}"))
+ .collect()
+ .await
+ .unwrap_or_else(|e| panic!("{expression}: {e}"));
+ assert_eq!(
+ ScalarValue::try_from_array(batches[0].column(0), 0).unwrap(),
+ ScalarValue::Boolean(Some(true)),
+ "{expression}"
+ );
+ }
+}
+#[tokio::test]
+async fn scalar_functions_evaluate_row_inputs() {
+ let ctx = SQLContext::new();
+ let sql="SELECT split(s,p,n) = expected FROM (VALUES ('a,b,c',
',',2,ARRAY['a','b,c']), ('a b',' +',0,ARRAY['a','b'])) t(s,p,n,expected)";
+ for batch in ctx.sql(sql).await.unwrap().collect().await.unwrap() {
+ for row in 0..batch.num_rows() {
+ assert_eq!(
+ ScalarValue::try_from_array(batch.column(0), row).unwrap(),
+ ScalarValue::Boolean(Some(true))
+ );
+ }
+ }
+ for sql in [
+ "SELECT split('abc','[')",
+ "SELECT size('abc')",
+ "SELECT hour(42)",
+ ] {
+ match ctx.sql(sql).await {
+ Err(_) => {}
+ Ok(frame) => assert!(frame.collect().await.is_err(), "{sql}"),
+ }
+ }
+}
+
+#[tokio::test]
+async fn
hour_uses_timestamp_timezone_metadata_and_hex_preserves_unsigned_values() {
+ use datafusion::arrow::{
+ array::{Array, ArrayRef, RecordBatch, TimestampMillisecondArray,
UInt64Array},
+ datatypes::{Field, Schema},
+ };
+ use std::sync::Arc;
+ let ts = Arc::new(
+ TimestampMillisecondArray::from(vec![Some(0),
None]).with_timezone("Asia/Shanghai"),
+ ) as ArrayRef;
+ let nums = Arc::new(UInt64Array::from(vec![Some(u64::MAX), None])) as
ArrayRef;
+ let batch = RecordBatch::try_new(
+ Arc::new(Schema::new(vec![
+ Field::new("ts", ts.data_type().clone(), true),
+ Field::new("n", nums.data_type().clone(), true),
+ ])),
+ vec![ts, nums],
+ )
+ .unwrap();
+ let ctx = SQLContext::new();
+ ctx.ctx().register_batch("inputs", batch).unwrap();
+ let batches = ctx
+ .sql("SELECT hour(ts), hex(n) FROM inputs")
+ .await
+ .unwrap()
+ .collect()
+ .await
+ .unwrap();
+ assert_eq!(
+ ScalarValue::try_from_array(batches[0].column(0), 0).unwrap(),
+ ScalarValue::Int32(Some(8))
+ );
+ assert_eq!(
+ ScalarValue::try_from_array(batches[0].column(1), 0).unwrap(),
+ ScalarValue::Utf8(Some("FFFFFFFFFFFFFFFF".into()))
+ );
+ assert!(batches[0].column(0).is_null(1));
+ assert!(batches[0].column(1).is_null(1));
+}
diff --git a/docs/src/sql.md b/docs/src/sql.md
index 974286ba..25637300 100644
--- a/docs/src/sql.md
+++ b/docs/src/sql.md
@@ -1957,6 +1957,25 @@ The timestamp aliases retain DataFusion semantics:
optional formats are Chrono
strftime strings, not Spark/Java patterns; numeric `to_timestamp_ltz` inputs
are epoch seconds. The Flink `(number, precision)` overload is not implemented.
+### Additional scalar compatibility functions
+
+| Function | Contract |
+|----------|----------|
+| `split(text, regex[, limit])` | Returns a string array using Rust regex
syntax. A positive limit caps the number of parts and retains the remaining
suffix in the last part; a nonpositive limit is unlimited. An empty pattern
splits Unicode characters. |
+| `locate(needle, text[, start])` | Returns a one-based Unicode-character
position, or zero if no match exists or start is nonpositive/out of range.
Start defaults to one. |
+| `date(value)` | Alias of native `to_date`, retaining its accepted inputs and
conversion errors. |
+| `typeof(value)` | Returns SQL-style names for common scalar, array, map,
struct and Variant types, including typed NULLs. Other Arrow types retain their
Arrow type labels. |
+| `hex(value)` | Uppercase hexadecimal UTF-8/string or raw binary bytes;
integer inputs use hexadecimal digits, with negative signed values represented
as 64-bit two's complement. |
+| `unhex(text)` | Decodes case-insensitive hex. An odd number of digits is
left-padded with zero; invalid hex returns NULL. |
+| `size(collection)` | Counts array elements or map entries, including NULL
values, and returns INT. |
+| `hour(value)` | Extracts an INT hour from timestamps, times or timestamp
strings. Dates produce zero; timestamp timezone metadata is honored. |
+
+Except for `typeof`, NULL inputs produce NULL. Empty collections have size
zero.
+These functions are registered locally and do not require REST function
metadata.
+Split patterns follow the same Rust regex restrictions as the other regex
+compatibility functions. SQLContext's `date` alias follows native DataFusion
+behavior when session configuration changes.
+
### Column Projection
Only the required columns are read, reducing I/O: