Skip to content
Merged
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
139 changes: 139 additions & 0 deletions datafusion/core/tests/sql/arrow_typeof.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// 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 super::*;

#[tokio::test]
async fn arrow_typeof_null() -> Result<()> {
let ctx = SessionContext::new();
let sql = "SELECT arrow_typeof(null)";
let actual = execute(&ctx, sql).await;
let expected = "Null";
assert_eq!(expected, &actual[0][0]);

Ok(())
}

#[tokio::test]
async fn arrow_typeof_boolean() -> Result<()> {
let ctx = SessionContext::new();
let sql = "SELECT arrow_typeof(true)";
let actual = execute(&ctx, sql).await;
let expected = "Boolean";
assert_eq!(expected, &actual[0][0]);

Ok(())
}

#[tokio::test]
async fn arrow_typeof_i64() -> Result<()> {
let ctx = SessionContext::new();
let sql = "SELECT arrow_typeof(1)";
let actual = execute(&ctx, sql).await;
let expected = "Int64";
assert_eq!(expected, &actual[0][0]);

Ok(())
}

#[tokio::test]
async fn arrow_typeof_i32() -> Result<()> {
let ctx = SessionContext::new();
let sql = "SELECT arrow_typeof(1::int)";
let actual = execute(&ctx, sql).await;
let expected = "Int32";
assert_eq!(expected, &actual[0][0]);

Ok(())
}

#[tokio::test]
async fn arrow_typeof_f64() -> Result<()> {
let ctx = SessionContext::new();
let sql = "SELECT arrow_typeof(1.0)";
let actual = execute(&ctx, sql).await;
let expected = "Float64";
assert_eq!(expected, &actual[0][0]);

Ok(())
}

#[tokio::test]
async fn arrow_typeof_f32() -> Result<()> {
let ctx = SessionContext::new();
let sql = "SELECT arrow_typeof(1.0::float)";
let actual = execute(&ctx, sql).await;
let expected = "Float32";
assert_eq!(expected, &actual[0][0]);

Ok(())
}

#[tokio::test]
async fn arrow_typeof_decimal() -> Result<()> {
let ctx = SessionContext::new();
let sql = "SELECT arrow_typeof(1::Decimal)";
let actual = execute(&ctx, sql).await;
let expected = "Decimal128(38, 10)";
assert_eq!(expected, &actual[0][0]);

Ok(())
}

#[tokio::test]
async fn arrow_typeof_timestamp() -> Result<()> {
let ctx = SessionContext::new();
let sql = "SELECT arrow_typeof(now()::timestamp)";
let actual = execute(&ctx, sql).await;
let expected = "Timestamp(Nanosecond, None)";
assert_eq!(expected, &actual[0][0]);

Ok(())
}

#[tokio::test]
async fn arrow_typeof_timestamp_utc() -> Result<()> {
let ctx = SessionContext::new();
let sql = "SELECT arrow_typeof(now())";
let actual = execute(&ctx, sql).await;
let expected = "Timestamp(Nanosecond, Some(\"UTC\"))";
assert_eq!(expected, &actual[0][0]);

Ok(())
}

#[tokio::test]
async fn arrow_typeof_timestamp_date32() -> Result<()> {
let ctx = SessionContext::new();
let sql = "SELECT arrow_typeof(now()::date)";
let actual = execute(&ctx, sql).await;
let expected = "Date32";
assert_eq!(expected, &actual[0][0]);

Ok(())
}

#[tokio::test]
async fn arrow_typeof_utf8() -> Result<()> {
let ctx = SessionContext::new();
let sql = "SELECT arrow_typeof('1')";
let actual = execute(&ctx, sql).await;
let expected = "Utf8";
assert_eq!(expected, &actual[0][0]);

Ok(())
}
1 change: 1 addition & 0 deletions datafusion/core/tests/sql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ pub mod union;
pub mod wildcard;
pub mod window;

pub mod arrow_typeof;
pub mod decimal;
mod explain;
mod idenfifers;
Expand Down
6 changes: 5 additions & 1 deletion datafusion/expr/src/built_in_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,10 @@ pub enum BuiltinScalarFunction {
Upper,
/// regexp_match
RegexpMatch,
///struct
/// struct
Struct,
/// arrow_typeof
ArrowTypeof,
}

impl BuiltinScalarFunction {
Expand Down Expand Up @@ -248,6 +250,7 @@ impl BuiltinScalarFunction {
BuiltinScalarFunction::RegexpMatch => Volatility::Immutable,
BuiltinScalarFunction::Struct => Volatility::Immutable,
BuiltinScalarFunction::FromUnixtime => Volatility::Immutable,
BuiltinScalarFunction::ArrowTypeof => Volatility::Immutable,

// Stable builtin functions
BuiltinScalarFunction::Now => Volatility::Stable,
Expand Down Expand Up @@ -345,6 +348,7 @@ impl FromStr for BuiltinScalarFunction {
"regexp_match" => BuiltinScalarFunction::RegexpMatch,
"struct" => BuiltinScalarFunction::Struct,
"from_unixtime" => BuiltinScalarFunction::FromUnixtime,
"arrow_typeof" => BuiltinScalarFunction::ArrowTypeof,
_ => {
return Err(DataFusionError::Plan(format!(
"There is no built-in function named {}",
Expand Down
4 changes: 4 additions & 0 deletions datafusion/expr/src/expr_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,8 @@ scalar_expr!(ToTimestampMicros, to_timestamp_micros, date);
scalar_expr!(ToTimestampSeconds, to_timestamp_seconds, date);
scalar_expr!(FromUnixtime, from_unixtime, unixtime);

unary_scalar_expr!(ArrowTypeof, arrow_typeof, "data type");

/// Returns an array of fixed size with each argument on it.
pub fn array(args: Vec<Expr>) -> Expr {
Expr::ScalarFunction {
Expand Down Expand Up @@ -630,6 +632,8 @@ mod test {
test_scalar_expr!(DateTrunc, date_trunc, part, date);
test_scalar_expr!(DateBin, date_bin, stride, source, origin);
test_scalar_expr!(FromUnixtime, from_unixtime, unixtime);

test_unary_scalar_expr!(ArrowTypeof, arrow_typeof);
}

#[test]
Expand Down
3 changes: 3 additions & 0 deletions datafusion/expr/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ pub fn return_type(
_ => Ok(DataType::Float64),
},

BuiltinScalarFunction::ArrowTypeof => Ok(DataType::Utf8),

BuiltinScalarFunction::Abs
| BuiltinScalarFunction::Acos
| BuiltinScalarFunction::Asin
Expand Down Expand Up @@ -567,6 +569,7 @@ pub fn signature(fun: &BuiltinScalarFunction) -> Signature {
],
fun.volatility(),
),
BuiltinScalarFunction::ArrowTypeof => Signature::any(1, fun.volatility()),
// math expressions expect 1 argument of type f64 or f32
// priority is given to f64 because e.g. `sqrt(1i32)` is in IR (real numbers) and thus we
// return the best approximation for it (in f64).
Expand Down
9 changes: 9 additions & 0 deletions datafusion/physical-expr/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@ pub fn create_physical_expr(
}
}
}),
BuiltinScalarFunction::ArrowTypeof => {
let input_data_type = coerced_phy_exprs[0].data_type(input_schema)?;
Arc::new(move |_| {
Ok(ColumnarValue::Scalar(ScalarValue::Utf8(Some(format!(
"{}",
input_data_type
)))))
})
}
// These don't need args and input schema
_ => create_physical_fun(fun, execution_props)?,
};
Expand Down
1 change: 1 addition & 0 deletions datafusion/proto/proto/datafusion.proto
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ enum ScalarFunction {
FromUnixtime=66;
Atan2=67;
DateBin=68;
ArrowTypeof=69;
}

message ScalarFunctionNode {
Expand Down
1 change: 1 addition & 0 deletions datafusion/proto/src/from_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ impl From<&protobuf::ScalarFunction> for BuiltinScalarFunction {
ScalarFunction::StructFun => Self::Struct,
ScalarFunction::FromUnixtime => Self::FromUnixtime,
ScalarFunction::Atan2 => Self::Atan2,
ScalarFunction::ArrowTypeof => Self::ArrowTypeof,
}
}
}
Expand Down
1 change: 1 addition & 0 deletions datafusion/proto/src/to_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,7 @@ impl TryFrom<&BuiltinScalarFunction> for protobuf::ScalarFunction {
BuiltinScalarFunction::Struct => Self::StructFun,
BuiltinScalarFunction::FromUnixtime => Self::FromUnixtime,
BuiltinScalarFunction::Atan2 => Self::Atan2,
BuiltinScalarFunction::ArrowTypeof => Self::ArrowTypeof,
};

Ok(scalar_function)
Expand Down