|
| 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 std::sync::Arc; |
| 19 | + |
| 20 | +use datafusion::arrow::array::Int32Array; |
| 21 | +use datafusion::arrow::datatypes::{DataType, Field, Schema}; |
| 22 | +use datafusion::arrow::record_batch::RecordBatch; |
| 23 | +use datafusion::error::Result; |
| 24 | +use datafusion::prelude::*; |
| 25 | +use datafusion_common::assert_contains; |
| 26 | + |
| 27 | +/// This example demonstrates how to use the make_date |
| 28 | +/// function in the DataFrame API as well as via sql. |
| 29 | +#[tokio::main] |
| 30 | +async fn main() -> Result<()> { |
| 31 | + // define a schema. |
| 32 | + let schema = Arc::new(Schema::new(vec![ |
| 33 | + Field::new("y", DataType::Int32, false), |
| 34 | + Field::new("m", DataType::Int32, false), |
| 35 | + Field::new("d", DataType::Int32, false), |
| 36 | + ])); |
| 37 | + |
| 38 | + // define data. |
| 39 | + let batch = RecordBatch::try_new( |
| 40 | + schema, |
| 41 | + vec![ |
| 42 | + Arc::new(Int32Array::from(vec![2020, 2021, 2022, 2023, 2024])), |
| 43 | + Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5])), |
| 44 | + Arc::new(Int32Array::from(vec![15, 16, 17, 18, 19])), |
| 45 | + ], |
| 46 | + )?; |
| 47 | + |
| 48 | + // declare a new context. In spark API, this corresponds to a new spark SQLsession |
| 49 | + let ctx = SessionContext::new(); |
| 50 | + |
| 51 | + // declare a table in memory. In spark API, this corresponds to createDataFrame(...). |
| 52 | + ctx.register_batch("t", batch)?; |
| 53 | + let df = ctx.table("t").await?; |
| 54 | + |
| 55 | + // use make_date function to convert col 'y', 'm' & 'd' to a date |
| 56 | + let df = df.with_column("a", make_date(col("y"), col("m"), col("d")))?; |
| 57 | + // use make_date function to convert col 'y' & 'm' with a static day to a date |
| 58 | + let df = df.with_column("b", make_date(col("y"), col("m"), lit(22)))?; |
| 59 | + |
| 60 | + let df = df.select_columns(&["a", "b"])?; |
| 61 | + |
| 62 | + // print the results |
| 63 | + df.show().await?; |
| 64 | + |
| 65 | + // use sql to convert col 'y', 'm' & 'd' to a date |
| 66 | + let df = ctx.sql("select make_date(y, m, d) from t").await?; |
| 67 | + |
| 68 | + // print the results |
| 69 | + df.show().await?; |
| 70 | + |
| 71 | + // use sql to convert col 'y' & 'm' with a static string day to a date |
| 72 | + let df = ctx.sql("select make_date(y, m, '22') from t").await?; |
| 73 | + |
| 74 | + // print the results |
| 75 | + df.show().await?; |
| 76 | + |
| 77 | + // math expressions work |
| 78 | + let df = ctx.sql("select make_date(y + 1, m, d) from t").await?; |
| 79 | + |
| 80 | + // print the results |
| 81 | + df.show().await?; |
| 82 | + |
| 83 | + // you can cast to supported types (int, bigint, varchar) if required |
| 84 | + let df = ctx |
| 85 | + .sql("select make_date(2024::bigint, 01::bigint, 27::varchar(3))") |
| 86 | + .await?; |
| 87 | + |
| 88 | + // print the results |
| 89 | + df.show().await?; |
| 90 | + |
| 91 | + // arrow casts also work |
| 92 | + let df = ctx |
| 93 | + .sql("select make_date(arrow_cast(2024, 'Int64'), arrow_cast(1, 'Int64'), arrow_cast(27, 'Int64'))") |
| 94 | + .await?; |
| 95 | + |
| 96 | + // print the results |
| 97 | + df.show().await?; |
| 98 | + |
| 99 | + // invalid column values will result in an error |
| 100 | + let result = ctx |
| 101 | + .sql("select make_date(2024, null, 23)") |
| 102 | + .await? |
| 103 | + .collect() |
| 104 | + .await; |
| 105 | + |
| 106 | + let expected = "Execution error: Unable to parse date from null/empty value"; |
| 107 | + assert_contains!(result.unwrap_err().to_string(), expected); |
| 108 | + |
| 109 | + // invalid date values will also result in an error |
| 110 | + let result = ctx |
| 111 | + .sql("select make_date(2024, 01, 32)") |
| 112 | + .await? |
| 113 | + .collect() |
| 114 | + .await; |
| 115 | + |
| 116 | + let expected = "Execution error: Unable to parse date from 2024, 1, 32"; |
| 117 | + assert_contains!(result.unwrap_err().to_string(), expected); |
| 118 | + |
| 119 | + Ok(()) |
| 120 | +} |
0 commit comments