From 2d51a0b21bec3c55bc48f228dc1ecb146834afe0 Mon Sep 17 00:00:00 2001 From: gitccl Date: Wed, 10 May 2023 11:33:49 +0800 Subject: [PATCH] Port tests in `json.rs` to sqllogictest --- datafusion/core/tests/sql/json.rs | 99 ------------------- datafusion/core/tests/sql/mod.rs | 1 - .../tests/sqllogictests/test_files/json.slt | 68 +++++++++++++ 3 files changed, 68 insertions(+), 100 deletions(-) delete mode 100644 datafusion/core/tests/sql/json.rs create mode 100644 datafusion/core/tests/sqllogictests/test_files/json.slt diff --git a/datafusion/core/tests/sql/json.rs b/datafusion/core/tests/sql/json.rs deleted file mode 100644 index 8608305f152d0..0000000000000 --- a/datafusion/core/tests/sql/json.rs +++ /dev/null @@ -1,99 +0,0 @@ -// 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::*; - -const TEST_DATA_BASE: &str = "tests/data"; - -#[tokio::test] -async fn json_query() { - let ctx = SessionContext::new(); - let path = format!("{TEST_DATA_BASE}/2.json"); - ctx.register_json("t1", &path, NdJsonReadOptions::default()) - .await - .unwrap(); - - let sql = "SELECT a, b FROM t1"; - let actual = execute_to_batches(&ctx, sql).await; - let expected = vec![ - "+-----------------+------+", - "| a | b |", - "+-----------------+------+", - "| 1 | 2.0 |", - "| -10 | -3.5 |", - "| 2 | 0.6 |", - "| 1 | 2.0 |", - "| 7 | -3.5 |", - "| 1 | 0.6 |", - "| 1 | 2.0 |", - "| 5 | -3.5 |", - "| 1 | 0.6 |", - "| 1 | 2.0 |", - "| 1 | -3.5 |", - "| 100000000000000 | 0.6 |", - "+-----------------+------+", - ]; - - assert_batches_eq!(expected, &actual); -} - -#[tokio::test] -#[should_panic] -async fn json_single_nan_schema() { - let ctx = SessionContext::new(); - let path = format!("{TEST_DATA_BASE}/3.json"); - ctx.register_json("single_nan", &path, NdJsonReadOptions::default()) - .await - .unwrap(); - let sql = "SELECT mycol FROM single_nan"; - let dataframe = ctx.sql(sql).await.unwrap(); - let results = dataframe.collect().await.unwrap(); - for batch in results { - assert_eq!(1, batch.num_rows()); - assert_eq!(1, batch.num_columns()); - } -} - -#[tokio::test] -#[cfg_attr(tarpaulin, ignore)] -async fn json_explain() { - let ctx = SessionContext::new(); - let path = format!("{TEST_DATA_BASE}/2.json"); - ctx.register_json("t1", &path, NdJsonReadOptions::default()) - .await - .unwrap(); - - let sql = "EXPLAIN SELECT count(*) from t1"; - let actual = execute(&ctx, sql).await; - let actual = normalize_vec_for_explain(actual); - let expected = vec![ - vec![ - "logical_plan", - "Aggregate: groupBy=[[]], aggr=[[COUNT(UInt8(1))]]\ - \n TableScan: t1 projection=[a]", - ], - vec![ - "physical_plan", - "AggregateExec: mode=Final, gby=[], aggr=[COUNT(UInt8(1))]\ - \n CoalescePartitionsExec\ - \n AggregateExec: mode=Partial, gby=[], aggr=[COUNT(UInt8(1))]\ - \n RepartitionExec: partitioning=RoundRobinBatch(NUM_CORES), input_partitions=1\ - \n JsonExec: file_groups={1 group: [[WORKING_DIR/tests/data/2.json]]}, projection=[a]\n", - ], - ]; - assert_eq!(expected, actual); -} diff --git a/datafusion/core/tests/sql/mod.rs b/datafusion/core/tests/sql/mod.rs index fba64e8b01248..8b33a888a53eb 100644 --- a/datafusion/core/tests/sql/mod.rs +++ b/datafusion/core/tests/sql/mod.rs @@ -88,7 +88,6 @@ pub mod expr; pub mod functions; pub mod group_by; pub mod joins; -pub mod json; pub mod limit; pub mod order; pub mod parquet; diff --git a/datafusion/core/tests/sqllogictests/test_files/json.slt b/datafusion/core/tests/sqllogictests/test_files/json.slt new file mode 100644 index 0000000000000..dc0dd39b2c0ea --- /dev/null +++ b/datafusion/core/tests/sqllogictests/test_files/json.slt @@ -0,0 +1,68 @@ +# 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. + +########## +## Json Tests +########## + +statement ok +CREATE EXTERNAL TABLE json_test +STORED AS JSON +LOCATION 'tests/data/2.json'; + +statement ok +CREATE EXTERNAL TABLE single_nan +STORED AS JSON +LOCATION 'tests/data/3.json'; + +query IR rowsort +SELECT a, b FROM json_test +---- +-10 -3.5 +1 -3.5 +1 0.6 +1 0.6 +1 2 +1 2 +1 2 +1 2 +100000000000000 0.6 +2 0.6 +5 -3.5 +7 -3.5 + +query TT +EXPLAIN SELECT count(*) from json_test +---- +logical_plan +Aggregate: groupBy=[[]], aggr=[[COUNT(UInt8(1))]] + TableScan: json_test projection=[a] +physical_plan +AggregateExec: mode=Final, gby=[], aggr=[COUNT(UInt8(1))] + CoalescePartitionsExec + AggregateExec: mode=Partial, gby=[], aggr=[COUNT(UInt8(1))] + RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1 + JsonExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/core/tests/data/2.json]]}, projection=[a] + +query error DataFusion error: Schema error: No field named mycol\. +SELECT mycol FROM single_nan + +statement ok +DROP TABLE json_test + +statement ok +DROP TABLE single_nan