|
| 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 datafusion::error::Result; |
| 19 | +use datafusion::functions_aggregate::count::count; |
| 20 | +use datafusion::prelude::*; |
| 21 | + |
| 22 | +/// This example demonstrates executing a DataFrame operation against an Arrow data source (CSV) and |
| 23 | +/// fetching results. See `csv_sql.rs` for a SQL version of this example. |
| 24 | +#[tokio::main] |
| 25 | +async fn main() -> Result<()> { |
| 26 | + // create local execution context |
| 27 | + let ctx = SessionContext::new(); |
| 28 | + |
| 29 | + let testdata = datafusion::test_util::arrow_test_data(); |
| 30 | + |
| 31 | + // execute the query |
| 32 | + let df = ctx |
| 33 | + .read_csv( |
| 34 | + &format!("{testdata}/csv/aggregate_test_100.csv"), |
| 35 | + CsvReadOptions::new(), |
| 36 | + ) |
| 37 | + .await? |
| 38 | + .filter(col("c11").gt(lit(0.1)).and(col("c11").lt(lit(0.9))))? |
| 39 | + .aggregate( |
| 40 | + vec![col("c1")], |
| 41 | + vec![ |
| 42 | + min(col("c12")), |
| 43 | + max(col("c12")), |
| 44 | + count(col("c12")), |
| 45 | + ], |
| 46 | + )?; |
| 47 | + |
| 48 | + // print the results |
| 49 | + df.show().await?; |
| 50 | + |
| 51 | + Ok(()) |
| 52 | +} |
0 commit comments