Skip to content

Commit 95a88f7

Browse files
committed
test
1 parent 88e32b3 commit 95a88f7

File tree

1 file changed

+95
-36
lines changed

1 file changed

+95
-36
lines changed

src/max_min_by.rs

Lines changed: 95 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,15 @@ mod tests {
325325

326326
#[cfg(test)]
327327
mod max_by {
328+
328329
use super::*;
329330
async fn extract_string(df: prelude::DataFrame) -> error::Result<String> {
330331
let results = df.collect().await?;
331332
let col = results[0].column(0);
332-
let arr = col.as_any().downcast_ref::<arrow::array::StringArray>().unwrap();
333+
let arr = col
334+
.as_any()
335+
.downcast_ref::<arrow::array::StringArray>()
336+
.unwrap();
333337
Ok(arr.value(0).to_string())
334338
}
335339

@@ -338,6 +342,7 @@ mod tests {
338342
ctx.register_udaf(MaxByFunction::new().into());
339343
Ok(ctx)
340344
}
345+
341346
#[tokio::test]
342347
async fn test_max_by_string_int() -> error::Result<()> {
343348
let query = format!(
@@ -350,41 +355,6 @@ mod tests {
350355
Ok(())
351356
}
352357

353-
#[tokio::test]
354-
async fn test_max_by_ignores_nulls_in_ok() -> error::Result<()> {
355-
let ctx = ctx_max()?;
356-
let sql = r#"
357-
SELECT max_by(v, k)
358-
FROM (
359-
VALUES
360-
('a', 1),
361-
('b', CAST(NULL AS INT)),
362-
('c', 2)
363-
) AS t(v, k)
364-
"#;
365-
let df = ctx.sql(sql).await?;
366-
let got = extract_string(df).await?;
367-
assert_eq!(got, "c", "max_by should ignore NULLs");
368-
Ok(())
369-
}
370-
#[tokio::test]
371-
async fn test_max_by_ignores_nulls_in_ko() -> error::Result<()> {
372-
let ctx = ctx_max()?;
373-
let sql = r#"
374-
SELECT max_by(v, k)
375-
FROM (
376-
VALUES
377-
('a', 1),
378-
('b', CAST(NULL AS INT)),
379-
('c', 2)
380-
) AS t(v, k)
381-
"#;
382-
let df = ctx.sql(sql).await?;
383-
let got = extract_string(df).await?;
384-
assert_eq!(got, "b", "max_by should ignore NULLs");
385-
Ok(())
386-
}
387-
388358
#[tokio::test]
389359
async fn test_max_by_string_float() -> error::Result<()> {
390360
let query = format!(
@@ -433,6 +403,43 @@ mod tests {
433403
Ok(())
434404
}
435405

406+
#[tokio::test]
407+
async fn test_max_by_ignores_nulls() -> error::Result<()> {
408+
let ctx = ctx_max()?;
409+
let sql = r#"
410+
SELECT max_by(v, k)
411+
FROM (
412+
VALUES
413+
('a', 1),
414+
('b', CAST(NULL AS INT)),
415+
('c', 2)
416+
) AS t(v, k)
417+
"#;
418+
let df = ctx.sql(sql).await?;
419+
let got = extract_string(df).await?;
420+
assert_eq!(got, "c", "max_by should ignore NULLs");
421+
Ok(())
422+
}
423+
424+
#[tokio::test]
425+
async fn test_max_like_main_test() -> error::Result<()> {
426+
let ctx = ctx_max()?;
427+
let sql = r#"
428+
SELECT max_by(v, k)
429+
FROM (
430+
VALUES
431+
('a', 10),
432+
('b', 5),
433+
('c', 15),
434+
('d', 8)
435+
) AS t(v, k)
436+
"#;
437+
let df = ctx.sql(sql).await?;
438+
let got = extract_string(df).await?;
439+
assert_eq!(got, "c");
440+
Ok(())
441+
}
442+
436443
fn ctx() -> error::Result<prelude::SessionContext> {
437444
let ctx = test_ctx()?;
438445
let max_by_udaf = MaxByFunction::new();
@@ -445,6 +452,21 @@ mod tests {
445452
mod min_by {
446453

447454
use super::*;
455+
async fn extract_string(df: prelude::DataFrame) -> error::Result<String> {
456+
let results = df.collect().await?;
457+
let col = results[0].column(0);
458+
let arr = col
459+
.as_any()
460+
.downcast_ref::<arrow::array::StringArray>()
461+
.unwrap();
462+
Ok(arr.value(0).to_string())
463+
}
464+
465+
fn ctx_min() -> error::Result<prelude::SessionContext> {
466+
let ctx = prelude::SessionContext::new();
467+
ctx.register_udaf(MinByFunction::new().into());
468+
Ok(ctx)
469+
}
448470

449471
#[tokio::test]
450472
async fn test_min_by_string_int() -> error::Result<()> {
@@ -506,6 +528,43 @@ mod tests {
506528
Ok(())
507529
}
508530

531+
#[tokio::test]
532+
async fn test_min_by_ignores_nulls() -> error::Result<()> {
533+
let ctx = ctx_min()?;
534+
let sql = r#"
535+
SELECT min_by(v, k)
536+
FROM (
537+
VALUES
538+
('a', 1),
539+
('b', CAST(NULL AS INT)),
540+
('c', 2)
541+
) AS t(v, k)
542+
"#;
543+
let df = ctx.sql(sql).await?;
544+
let got = extract_string(df).await?;
545+
assert_eq!(got, "a", "max_by should ignore NULLs");
546+
Ok(())
547+
}
548+
549+
#[tokio::test]
550+
async fn test_min_like_main_test() -> error::Result<()> {
551+
let ctx = ctx_min()?;
552+
let sql = r#"
553+
SELECT min_by(v, k)
554+
FROM (
555+
VALUES
556+
('a', 10),
557+
('b', 5),
558+
('c', 15),
559+
('d', 8)
560+
) AS t(v, k)
561+
"#;
562+
let df = ctx.sql(sql).await?;
563+
let got = extract_string(df).await?;
564+
assert_eq!(got, "b");
565+
Ok(())
566+
}
567+
509568
fn ctx() -> error::Result<prelude::SessionContext> {
510569
let ctx = test_ctx()?;
511570
let min_by_udaf = MinByFunction::new();

0 commit comments

Comments
 (0)