|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use arrow_array::Int64Array; |
| 19 | +use arrow_schema::{DataType, Field}; |
| 20 | +use datafusion::execution::session_state::SessionStateBuilder; |
| 21 | +use datafusion_common::{Column, DFSchema, Result, ScalarValue}; |
| 22 | +use datafusion_execution::TaskContext; |
| 23 | +use datafusion_expr::expr::AggregateFunction; |
| 24 | +use datafusion_expr::logical_plan::{LogicalPlan, Values}; |
| 25 | +use datafusion_expr::{Aggregate, AggregateUDF, Expr}; |
| 26 | +use datafusion_functions_aggregate::count::Count; |
| 27 | +use datafusion_physical_plan::collect; |
| 28 | +use std::collections::HashMap; |
| 29 | +use std::fmt::Debug; |
| 30 | +use std::ops::Deref; |
| 31 | +use std::sync::Arc; |
| 32 | + |
| 33 | +///! Logical plans need to provide stable semantics, as downstream projects |
| 34 | +///! create them and depend on them. Test executable semantics of logical plans. |
| 35 | +
|
| 36 | +#[tokio::test] |
| 37 | +async fn count_only_nulls() -> Result<()> { |
| 38 | + // Input: VALUES (NULL), (NULL), (NULL) AS _(col) |
| 39 | + let input_schema = Arc::new(DFSchema::from_unqualified_fields( |
| 40 | + vec![Field::new("col", DataType::Null, true)].into(), |
| 41 | + HashMap::new(), |
| 42 | + )?); |
| 43 | + let input = Arc::new(LogicalPlan::Values(Values { |
| 44 | + schema: input_schema, |
| 45 | + values: vec![ |
| 46 | + vec![Expr::Literal(ScalarValue::Null)], |
| 47 | + vec![Expr::Literal(ScalarValue::Null)], |
| 48 | + vec![Expr::Literal(ScalarValue::Null)], |
| 49 | + ], |
| 50 | + })); |
| 51 | + let input_col_ref = Expr::Column(Column { |
| 52 | + relation: None, |
| 53 | + name: "col".to_string(), |
| 54 | + }); |
| 55 | + |
| 56 | + // Aggregation: count(col) AS count |
| 57 | + let aggregate = LogicalPlan::Aggregate(Aggregate::try_new( |
| 58 | + input, |
| 59 | + vec![], |
| 60 | + vec![Expr::AggregateFunction(AggregateFunction { |
| 61 | + func: Arc::new(AggregateUDF::new_from_impl(Count::new())), |
| 62 | + args: vec![input_col_ref], |
| 63 | + distinct: false, |
| 64 | + filter: None, |
| 65 | + order_by: None, |
| 66 | + null_treatment: None, |
| 67 | + })], |
| 68 | + )?); |
| 69 | + |
| 70 | + // Execute and verify results |
| 71 | + let session_state = SessionStateBuilder::new().build(); |
| 72 | + let physical_plan = session_state.create_physical_plan(&aggregate).await?; |
| 73 | + let result = |
| 74 | + collect(physical_plan, Arc::new(TaskContext::from(&session_state))).await?; |
| 75 | + |
| 76 | + let result = only(result.as_slice()); |
| 77 | + let result_schema = result.schema(); |
| 78 | + let field = only(result_schema.fields().deref()); |
| 79 | + let column = only(result.columns()); |
| 80 | + |
| 81 | + assert_eq!(field.data_type(), &DataType::Int64); // TODO should be UInt64 |
| 82 | + assert_eq!(column.deref(), &Int64Array::from(vec![0])); |
| 83 | + |
| 84 | + Ok(()) |
| 85 | +} |
| 86 | + |
| 87 | +fn only<T>(elements: &[T]) -> &T |
| 88 | +where |
| 89 | + T: Debug, |
| 90 | +{ |
| 91 | + let [element] = elements else { |
| 92 | + panic!("Expected exactly one element, got {:?}", elements); |
| 93 | + }; |
| 94 | + element |
| 95 | +} |
0 commit comments