-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Minor: Consolidate UDF tests #7704
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a52f5a4
Minor: Consolidate user defined functions
alamb 20d7da5
cleanup
alamb 8a6dca7
move more tests
alamb fb140eb
more
alamb 7e52e9e
cleanup use
alamb 9ef794c
Merge remote-tracking branch 'apache/main' into alamb/consolidate_udf…
alamb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -616,7 +616,7 @@ async fn test_array_cast_expressions() -> Result<()> { | |
|
||
#[tokio::test] | ||
async fn test_random_expression() -> Result<()> { | ||
let ctx = create_ctx(); | ||
let ctx = SessionContext::new(); | ||
let sql = "SELECT random() r1"; | ||
let actual = execute(&ctx, sql).await; | ||
let r1 = actual[0][0].parse::<f64>().unwrap(); | ||
|
@@ -627,7 +627,7 @@ async fn test_random_expression() -> Result<()> { | |
|
||
#[tokio::test] | ||
async fn test_uuid_expression() -> Result<()> { | ||
let ctx = create_ctx(); | ||
let ctx = SessionContext::new(); | ||
let sql = "SELECT uuid()"; | ||
let actual = execute(&ctx, sql).await; | ||
let uuid = actual[0][0].parse::<uuid::Uuid>().unwrap(); | ||
|
@@ -886,18 +886,6 @@ async fn csv_query_nullif_divide_by_0() -> Result<()> { | |
Ok(()) | ||
} | ||
|
||
#[tokio::test] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was randomly in the file for testing exprs when it is actually an (user defined) aggregate query 😕 |
||
async fn csv_query_avg_sqrt() -> Result<()> { | ||
let ctx = create_ctx(); | ||
register_aggregate_csv(&ctx).await?; | ||
let sql = "SELECT avg(custom_sqrt(c12)) FROM aggregate_test_100"; | ||
let mut actual = execute(&ctx, sql).await; | ||
actual.sort(); | ||
let expected = vec![vec!["0.6706002946036462"]]; | ||
assert_float_eq(&expected, &actual); | ||
Ok(()) | ||
} | ||
|
||
#[tokio::test] | ||
async fn nested_subquery() -> Result<()> { | ||
let ctx = SessionContext::new(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ use chrono::prelude::*; | |
use chrono::Duration; | ||
|
||
use datafusion::datasource::TableProvider; | ||
use datafusion::error::{DataFusionError, Result}; | ||
use datafusion::logical_expr::{Aggregate, LogicalPlan, TableScan}; | ||
use datafusion::physical_plan::metrics::MetricValue; | ||
use datafusion::physical_plan::ExecutionPlan; | ||
|
@@ -34,15 +35,9 @@ use datafusion::prelude::*; | |
use datafusion::test_util; | ||
use datafusion::{assert_batches_eq, assert_batches_sorted_eq}; | ||
use datafusion::{datasource::MemTable, physical_plan::collect}; | ||
use datafusion::{ | ||
error::{DataFusionError, Result}, | ||
physical_plan::ColumnarValue, | ||
}; | ||
use datafusion::{execution::context::SessionContext, physical_plan::displayable}; | ||
use datafusion_common::cast::as_float64_array; | ||
use datafusion_common::plan_err; | ||
use datafusion_common::{assert_contains, assert_not_contains}; | ||
use datafusion_expr::Volatility; | ||
use object_store::path::Path; | ||
use std::fs::File; | ||
use std::io::Write; | ||
|
@@ -101,54 +96,6 @@ pub mod select; | |
mod sql_api; | ||
pub mod subqueries; | ||
pub mod timestamp; | ||
pub mod udf; | ||
|
||
fn assert_float_eq<T>(expected: &[Vec<T>], received: &[Vec<String>]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not needed |
||
where | ||
T: AsRef<str>, | ||
{ | ||
expected | ||
.iter() | ||
.flatten() | ||
.zip(received.iter().flatten()) | ||
.for_each(|(l, r)| { | ||
let (l, r) = ( | ||
l.as_ref().parse::<f64>().unwrap(), | ||
r.as_str().parse::<f64>().unwrap(), | ||
); | ||
if l.is_nan() || r.is_nan() { | ||
assert!(l.is_nan() && r.is_nan()); | ||
} else if (l - r).abs() > 2.0 * f64::EPSILON { | ||
panic!("{l} != {r}") | ||
} | ||
}); | ||
} | ||
|
||
fn create_ctx() -> SessionContext { | ||
let ctx = SessionContext::new(); | ||
|
||
// register a custom UDF | ||
ctx.register_udf(create_udf( | ||
"custom_sqrt", | ||
vec![DataType::Float64], | ||
Arc::new(DataType::Float64), | ||
Volatility::Immutable, | ||
Arc::new(custom_sqrt), | ||
)); | ||
|
||
ctx | ||
} | ||
|
||
fn custom_sqrt(args: &[ColumnarValue]) -> Result<ColumnarValue> { | ||
let arg = &args[0]; | ||
if let ColumnarValue::Array(v) = arg { | ||
let input = as_float64_array(v).expect("cast failed"); | ||
let array: Float64Array = input.iter().map(|v| v.map(|x| x.sqrt())).collect(); | ||
Ok(ColumnarValue::Array(Arc::new(array))) | ||
} else { | ||
unimplemented!() | ||
} | ||
} | ||
|
||
fn create_join_context( | ||
column_left: &str, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved to user defined tests