Skip to content

Commit

Permalink
sqllogicaltest: move union.rs (#5075)
Browse files Browse the repository at this point in the history
* sqllogicaltest: move union.rs

* add order by to confirm the order of result
correct type.

* correct type.
  • Loading branch information
jackwener authored Jan 27, 2023
1 parent fbff8e2 commit dd09212
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 126 deletions.
124 changes: 0 additions & 124 deletions datafusion/core/tests/sql/union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,130 +17,6 @@

use super::*;

#[tokio::test]
async fn union_all() -> Result<()> {
let ctx = SessionContext::new();
let sql = "SELECT 1 as x UNION ALL SELECT 2 as x";
let actual = execute_to_batches(&ctx, sql).await;
#[rustfmt::skip]
let expected = vec![
"+---+",
"| x |",
"+---+",
"| 1 |",
"| 2 |",
"+---+"
];
assert_batches_eq!(expected, &actual);
Ok(())
}

#[tokio::test]
async fn csv_union_all() -> Result<()> {
let ctx = SessionContext::new();
register_aggregate_csv(&ctx).await?;
let sql =
"SELECT c1 FROM aggregate_test_100 UNION ALL SELECT c1 FROM aggregate_test_100";
let actual = execute(&ctx, sql).await;
assert_eq!(actual.len(), 200);
Ok(())
}

#[tokio::test]
async fn union_distinct() -> Result<()> {
let ctx = SessionContext::new();
let sql = "SELECT 1 as x UNION SELECT 1 as x";
let actual = execute_to_batches(&ctx, sql).await;
#[rustfmt::skip]
let expected = vec![
"+---+",
"| x |",
"+---+",
"| 1 |",
"+---+"
];
assert_batches_eq!(expected, &actual);
Ok(())
}

#[tokio::test]
async fn union_all_with_aggregate() -> Result<()> {
let ctx = SessionContext::new();
let sql =
"SELECT SUM(d) FROM (SELECT 1 as c, 2 as d UNION ALL SELECT 1 as c, 3 AS d) as a";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+----------+",
"| SUM(a.d) |",
"+----------+",
"| 5 |",
"+----------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}

#[tokio::test]
async fn union_all_with_count() -> Result<()> {
let ctx = SessionContext::new();
execute_to_batches(&ctx, "CREATE table t as SELECT 1 as a").await;
let sql = "SELECT COUNT(*) FROM (SELECT a from t UNION ALL SELECT a from t)";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+-----------------+",
"| COUNT(UInt8(1)) |",
"+-----------------+",
"| 2 |",
"+-----------------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}

#[tokio::test]
async fn union_schemas() -> Result<()> {
let ctx =
SessionContext::with_config(SessionConfig::new().with_information_schema(true));

let result = ctx
.sql("SELECT 1 A UNION ALL SELECT 2 order by 1")
.await
.unwrap()
.collect()
.await
.unwrap();

#[rustfmt::skip]
let expected = vec![
"+---+",
"| a |",
"+---+",
"| 1 |",
"| 2 |",
"+---+"
];
assert_batches_eq!(expected, &result);

let result = ctx
.sql("SELECT 1 UNION SELECT 2 order by 1")
.await
.unwrap()
.collect()
.await
.unwrap();

let expected = vec![
"+----------+",
"| Int64(1) |",
"+----------+",
"| 1 |",
"| 2 |",
"+----------+",
];
assert_batches_eq!(expected, &result);
Ok(())
}

#[tokio::test]
async fn union_with_except_input() -> Result<()> {
let ctx = create_union_context()?;
Expand Down
122 changes: 120 additions & 2 deletions datafusion/core/tests/sqllogictests/test_files/union.slt
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ ORDER BY name
Alice
John



# union with type coercion
query T
(
Expand All @@ -76,3 +74,123 @@ ORDER BY name
----
3 Alice
3 John

# union all
query I
SELECT 1 as x
UNION ALL
SELECT 2 as x
----
1
2

# csv_union_all
statement ok
CREATE EXTERNAL TABLE aggregate_test_100 (
c1 VARCHAR NOT NULL,
c2 TINYINT NOT NULL,
c3 SMALLINT NOT NULL,
c4 SMALLINT,
c5 INT,
c6 BIGINT NOT NULL,
c7 SMALLINT NOT NULL,
c8 INT NOT NULL,
c9 BIGINT UNSIGNED NOT NULL,
c10 VARCHAR NOT NULL,
c11 FLOAT NOT NULL,
c12 DOUBLE NOT NULL,
c13 VARCHAR NOT NULL
)
STORED AS CSV
WITH HEADER ROW
LOCATION '../../testing/data/csv/aggregate_test_100.csv'

query I
select COUNT(*) from (
SELECT c1 FROM aggregate_test_100
UNION ALL
SELECT c1 FROM aggregate_test_100
)
----
200

# union_distinct
query I
SELECT 1 as x
UNION
SELECT 1 as x
----
1

# union_all_with_aggregate
query I
SELECT SUM(d) FROM (
SELECT 1 as c, 2 as d
UNION ALL
SELECT 1 as c, 3 AS d
) as a
----
5

# union_all_with_count
statement ok
CREATE table t as SELECT 1 as a

query I
SELECT COUNT(*) FROM (
SELECT a from t
UNION ALL
SELECT a from t
)
----
2

# union_schemas
query I
SELECT 1 A UNION ALL SELECT 2 order by 1
----
1
2

# union_schemas
query I
SELECT 1 UNION SELECT 2 order by 1
----
1
2

# union_with_except_input
query T
SELECT * FROM (
(
SELECT name FROM t1
EXCEPT
SELECT name FROM t2
)
UNION ALL
(
SELECT name FROM t2
EXCEPT
SELECT name FROM t1
)
) ORDER BY name
----
Alice
John

# union_with_type_coercion
query IT
(
SELECT id, name FROM t1
EXCEPT
SELECT id, name FROM t2
)
UNION ALL
(
SELECT id, name FROM t2
EXCEPT
SELECT id, name FROM t1
)
----
3 Alice
3 John

0 comments on commit dd09212

Please sign in to comment.