Apache DataFusion SQL Query Engine
Test all examples from library-user-guide & user-guide docs (#14544)
Dawei H.3f900ac5e188
diff --git a/.github/actions/setup-builder/action.yaml b/.github/actions/setup-builder/action.yamlindex 22d2f2187d..5c7ecd3e1e 100644--- a/.github/actions/setup-builder/action.yaml+++ b/.github/actions/setup-builder/action.yaml@@ -30,7 +30,7 @@ runs:run: |RETRY=("ci/scripts/retry" timeout 120)"${RETRY[@]}" apt-get update- "${RETRY[@]}" apt-get install -y protobuf-compiler+ "${RETRY[@]}" apt-get install -y protobuf-compiler cmake- name: Setup Rust toolchainshell: bash# rustfmt is needed for the substrait build scriptdiff --git a/Cargo.lock b/Cargo.lockindex 953f4622be..cd6586180d 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -1730,10 +1730,12 @@ dependencies = ["chrono","criterion","ctor",+ "dashmap","datafusion-catalog","datafusion-catalog-listing","datafusion-common","datafusion-common-runtime",+ "datafusion-doc","datafusion-execution","datafusion-expr","datafusion-functions",@@ -1742,6 +1744,7 @@ dependencies = ["datafusion-functions-table","datafusion-functions-window","datafusion-functions-window-common",+ "datafusion-macros","datafusion-optimizer","datafusion-physical-expr","datafusion-physical-expr-common",diff --git a/datafusion/core/Cargo.toml b/datafusion/core/Cargo.tomlindex c3c764ab14..3c7287b887 100644--- a/datafusion/core/Cargo.toml+++ b/datafusion/core/Cargo.toml@@ -95,6 +95,7 @@ datafusion-catalog = { workspace = true }datafusion-catalog-listing = { workspace = true }datafusion-common = { workspace = true, features = ["object_store"] }datafusion-common-runtime = { workspace = true }+datafusion-doc = { workspace = true }datafusion-execution = { workspace = true }datafusion-expr = { workspace = true }datafusion-functions = { workspace = true }@@ -102,6 +103,7 @@ datafusion-functions-aggregate = { workspace = true }datafusion-functions-nested = { workspace = true, optional = true }datafusion-functions-table = { workspace = true }datafusion-functions-window = { workspace = true }+datafusion-macros = { workspace = true }datafusion-optimizer = { workspace = true }datafusion-physical-expr = { workspace = true }datafusion-physical-expr-common = { workspace = true }@@ -130,6 +132,7 @@ zstd = { version = "0.13", optional = true, default-features = false }async-trait = { workspace = true }criterion = { version = "0.5", features = ["async_tokio"] }ctor = { workspace = true }+dashmap = "6.1.0"datafusion-functions-window-common = { workspace = true }datafusion-physical-optimizer = { workspace = true }doc-comment = { workspace = true }diff --git a/datafusion/core/src/bin/print_functions_docs.rs b/datafusion/core/src/bin/print_functions_docs.rsindex 8b453d5e96..7afb90282a 100644--- a/datafusion/core/src/bin/print_functions_docs.rs+++ b/datafusion/core/src/bin/print_functions_docs.rs@@ -193,7 +193,7 @@ fn print_docs({}-```+```sql{}```"#,diff --git a/datafusion/core/src/lib.rs b/datafusion/core/src/lib.rsindex b256ed3803..48ee8e46bc 100644--- a/datafusion/core/src/lib.rs+++ b/datafusion/core/src/lib.rs@@ -844,11 +844,17 @@ doc_comment::doctest!("../../../README.md", readme_example_test);//// For example, if `user_guide_expressions(line 123)` fails,// go to `docs/source/user-guide/expressions.md` to find the relevant problem.+//+#[cfg(doctest)]+doc_comment::doctest!(+ "../../../docs/source/user-guide/concepts-readings-events.md",+ user_guide_concepts_readings_events+);#[cfg(doctest)]doc_comment::doctest!(- "../../../docs/source/user-guide/example-usage.md",- user_guide_example_usage+ "../../../docs/source/user-guide/configs.md",+ user_guide_configs);#[cfg(doctest)]@@ -859,14 +865,20 @@ doc_comment::doctest!(#[cfg(doctest)]doc_comment::doctest!(- "../../../docs/source/user-guide/configs.md",- user_guide_configs+ "../../../docs/source/user-guide/dataframe.md",+ user_guide_dataframe);#[cfg(doctest)]doc_comment::doctest!(- "../../../docs/source/user-guide/dataframe.md",- user_guide_dataframe+ "../../../docs/source/user-guide/example-usage.md",+ user_guide_example_usage+);++#[cfg(doctest)]+doc_comment::doctest!(+ "../../../docs/source/user-guide/explain-usage.md",+ user_guide_explain_usage);#[cfg(doctest)]@@ -875,16 +887,181 @@ doc_comment::doctest!(user_guide_expressions);+#[cfg(doctest)]+doc_comment::doctest!("../../../docs/source/user-guide/faq.md", user_guide_faq);+#[cfg(doctest)]doc_comment::doctest!(- "../../../docs/source/library-user-guide/using-the-sql-api.md",- library_user_guide_sql_api+ "../../../docs/source/user-guide/introduction.md",+ user_guide_introduction+);++#[cfg(doctest)]+doc_comment::doctest!(+ "../../../docs/source/user-guide/cli/datasources.md",+ user_guide_cli_datasource+);++#[cfg(doctest)]+doc_comment::doctest!(+ "../../../docs/source/user-guide/cli/installation.md",+ user_guide_cli_installation+);++#[cfg(doctest)]+doc_comment::doctest!(+ "../../../docs/source/user-guide/cli/overview.md",+ user_guide_cli_overview+);++#[cfg(doctest)]+doc_comment::doctest!(+ "../../../docs/source/user-guide/cli/usage.md",+ user_guide_cli_usage+);++#[cfg(doctest)]+doc_comment::doctest!(+ "../../../docs/source/user-guide/sql/aggregate_functions.md",+ user_guide_sql_aggregate_functions+);++#[cfg(doctest)]+doc_comment::doctest!(+ "../../../docs/source/user-guide/sql/data_types.md",+ user_guide_sql_data_types+);++#[cfg(doctest)]+doc_comment::doctest!(+ "../../../docs/source/user-guide/sql/ddl.md",+ user_guide_sql_ddl+);++#[cfg(doctest)]+doc_comment::doctest!(+ "../../../docs/source/user-guide/sql/dml.md",+ user_guide_sql_dml+);++#[cfg(doctest)]+doc_comment::doctest!(+ "../../../docs/source/user-guide/sql/explain.md",+ user_guide_sql_exmplain+);++#[cfg(doctest)]+doc_comment::doctest!(+ "../../../docs/source/user-guide/sql/information_schema.md",+ user_guide_sql_information_schema+);++#[cfg(doctest)]+doc_comment::doctest!(+ "../../../docs/source/user-guide/sql/operators.md",+ user_guide_sql_operators+);++#[cfg(doctest)]+doc_comment::doctest!(+ "../../../docs/source/user-guide/sql/scalar_functions.md",+ user_guide_sql_scalar_functions+);++#[cfg(doctest)]+doc_comment::doctest!(+ "../../../docs/source/user-guide/sql/select.md",+ user_guide_sql_select+);++#[cfg(doctest)]+doc_comment::doctest!(+ "../../../docs/source/user-guide/sql/special_functions.md",+ user_guide_sql_special_functions+);++#[cfg(doctest)]+doc_comment::doctest!(+ "../../../docs/source/user-guide/sql/sql_status.md",+ user_guide_sql_status+);++#[cfg(doctest)]+doc_comment::doctest!(+ "../../../docs/source/user-guide/sql/subqueries.md",+ user_guide_sql_subqueries+);++#[cfg(doctest)]+doc_comment::doctest!(+ "../../../docs/source/user-guide/sql/window_functions.md",+ user_guide_sql_window_functions+);++#[cfg(doctest)]+doc_comment::doctest!(+ "../../../docs/source/user-guide/sql/write_options.md",+ user_guide_sql_write_options+);++#[cfg(doctest)]+doc_comment::doctest!(+ "../../../docs/source/library-user-guide/adding-udfs.md",+ library_user_guide_adding_udfs+);++#[cfg(doctest)]+doc_comment::doctest!(+ "../../../docs/source/library-user-guide/api-health.md",+ library_user_guide_api_health);#[cfg(doctest)]doc_comment::doctest!("../../../docs/source/library-user-guide/building-logical-plans.md",- library_user_guide_logical_plans+ library_user_guide_building_logical_plans+);++#[cfg(doctest)]+doc_comment::doctest!(+ "../../../docs/source/library-user-guide/catalogs.md",+ library_user_guide_catalogs+);++#[cfg(doctest)]+doc_comment::doctest!(+ "../../../docs/source/library-user-guide/custom-table-providers.md",+ library_user_guide_custom_table_providers+);++#[cfg(doctest)]+doc_comment::doctest!(+ "../../../docs/source/library-user-guide/extending-operators.md",+ library_user_guide_extending_operators+);++#[cfg(doctest)]+doc_comment::doctest!(+ "../../../docs/source/library-user-guide/extensions.md",+ library_user_guide_extensions+);++#[cfg(doctest)]+doc_comment::doctest!(+ "../../../docs/source/library-user-guide/index.md",+ library_user_guide_index+);++#[cfg(doctest)]+doc_comment::doctest!(+ "../../../docs/source/library-user-guide/profiling.md",+ library_user_guide_profiling+);++#[cfg(doctest)]+doc_comment::doctest!(+ "../../../docs/source/library-user-guide/query-optimizer.md",+ library_user_guide_query_optimizer);#[cfg(doctest)]@@ -892,3 +1069,15 @@ doc_comment::doctest!("../../../docs/source/library-user-guide/using-the-dataframe-api.md",library_user_guide_dataframe_api);++#[cfg(doctest)]+doc_comment::doctest!(+ "../../../docs/source/library-user-guide/using-the-sql-api.md",+ library_user_guide_sql_api+);++#[cfg(doctest)]+doc_comment::doctest!(+ "../../../docs/source/library-user-guide/working-with-exprs.md",+ library_user_guide_working_with_exprs+);diff --git a/dev/update_function_docs.sh b/dev/update_function_docs.shindex 205ab41984..a9e87aacf5 100755--- a/dev/update_function_docs.sh+++ b/dev/update_function_docs.sh@@ -236,7 +236,7 @@ WINDOW w AS (PARTITION BY depname ORDER BY salary DESC);The syntax for the OVER-clause is-```+```sqlfunction([expr])OVER([PARTITION BY expr[, …]]@@ -247,7 +247,7 @@ function([expr])where **frame_clause** is one of:-```+```sql{ RANGE | ROWS | GROUPS } frame_start{ RANGE | ROWS | GROUPS } BETWEEN frame_start AND frame_end```diff --git a/docs/build.sh b/docs/build.shindex 3fdcd03270..14464fab40 100755--- a/docs/build.sh+++ b/docs/build.sh@@ -25,4 +25,7 @@ mkdir tempcp -rf source/* temp/# replace relative URLs with absolute URLssed -i -e 's/\.\.\/\.\.\/\.\.\//https:\/\/github.com\/apache\/arrow-datafusion\/blob\/main\//g' temp/contributor-guide/index.md++python rustdoc_trim.py+make SOURCEDIR=`pwd`/temp htmldiff --git a/docs/rustdoc_trim.py b/docs/rustdoc_trim.pynew file mode 100644index 0000000000..7ea96dbb44--- /dev/null+++ b/docs/rustdoc_trim.py@@ -0,0 +1,75 @@+# 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.++import re++from pathlib import Path++# Regex pattern to match Rust code blocks in Markdown+RUST_CODE_BLOCK_PATTERN = re.compile(r"```rust\s*(.*?)```", re.DOTALL)+++def remove_hashtag_lines_in_rust_blocks(markdown_content):+ """+ Removes lines starting with '# ' in Rust code blocks within a Markdown string.+ """++ def _process_code_block(match):+ # Extract the code block content+ code_block_content = match.group(1).strip()++ # Remove lines starting with '#'+ modified_code_block = "\n".join(+ line+ for line in code_block_content.splitlines()+ if (not line.lstrip().startswith("# ")) and line.strip() != "#"+ )++ # Return the modified code block wrapped in triple backticks+ return f"```rust\n{modified_code_block}\n```"++ # Replace all Rust code blocks using the _process_code_block function+ return RUST_CODE_BLOCK_PATTERN.sub(_process_code_block, markdown_content)+++# Example usage+def process_markdown_file(file_path):+ # Read the Markdown file+ with open(file_path, "r", encoding="utf-8") as file:+ markdown_content = file.read()++ # Remove lines starting with '#' in Rust code blocks+ updated_markdown_content = remove_hashtag_lines_in_rust_blocks(markdown_content)++ # Write the updated content back to the Markdown file+ with open(file_path, "w", encoding="utf-8") as file:+ file.write(updated_markdown_content)++ print(f"Done processing file: {file_path}")+++root_directory = Path("./temp/library-user-guide")+for file_path in root_directory.rglob("*.md"):+ print(f"Processing file: {file_path}")+ process_markdown_file(file_path)++root_directory = Path("./temp/user-guide")+for file_path in root_directory.rglob("*.md"):+ print(f"Processing file: {file_path}")+ process_markdown_file(file_path)++print("All Markdown files processed.")diff --git a/docs/source/library-user-guide/adding-udfs.md b/docs/source/library-user-guide/adding-udfs.mdindex a920297697..a365ef6696 100644--- a/docs/source/library-user-guide/adding-udfs.md+++ b/docs/source/library-user-guide/adding-udfs.md@@ -55,46 +55,62 @@ of arguments.This a lower level API with more functionality but is more complex, also documented in [`advanced_udf.rs`].```rust+use std::sync::Arc;use std::any::Any;+use std::sync::LazyLock;use arrow::datatypes::DataType;+use datafusion_common::cast::as_int64_array;use datafusion_common::{DataFusionError, plan_err, Result};-use datafusion_expr::{col, ColumnarValue, Signature, Volatility};+use datafusion_expr::{col, ColumnarValue, ScalarFunctionArgs, Signature, Volatility};+use datafusion::arrow::array::{ArrayRef, Int64Array};use datafusion_expr::{ScalarUDFImpl, ScalarUDF};-+use datafusion_macros::user_doc;+use datafusion_doc::Documentation;++/// This struct for a simple UDF that adds one to an int32+#[user_doc(+ doc_section(label = "Math Functions"),+ description = "Add one udf",+ syntax_example = "add_one(1)"+)]#[derive(Debug)]struct AddOne {- signature: Signature-};+ signature: Signature,+}impl AddOne {- fn new() -> Self {- Self {- signature: Signature::uniform(1, vec![DataType::Int32], Volatility::Immutable)- }- }+ fn new() -> Self {+ Self {+ signature: Signature::uniform(1, vec![DataType::Int32], Volatility::Immutable),+ }+ }}/// Implement the ScalarUDFImpl trait for AddOneimpl ScalarUDFImpl for AddOne {- fn as_any(&self) -> &dyn Any { self }- fn name(&self) -> &str { "add_one" }- fn signature(&self) -> &Signature { &self.signature }- fn return_type(&self, args: &[DataType]) -> Result<DataType> {- if !matches!(args.get(0), Some(&DataType::Int32)) {- return plan_err!("add_one only accepts Int32 arguments");- }- Ok(DataType::Int32)- }- // The actual implementation would add one to the argument- fn invoke_batch(&self, args: &[ColumnarValue], _number_rows: usize) -> Result<ColumnarValue> {- let args = columnar_values_to_array(args)?;+ fn as_any(&self) -> &dyn Any { self }+ fn name(&self) -> &str { "add_one" }+ fn signature(&self) -> &Signature { &self.signature }+ fn return_type(&self, args: &[DataType]) -> Result<DataType> {+ if !matches!(args.get(0), Some(&DataType::Int32)) {+ return plan_err!("add_one only accepts Int32 arguments");+ }+ Ok(DataType::Int32)+ }+ // The actual implementation would add one to the argument+ fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {+ let args = ColumnarValue::values_to_arrays(&args.args)?;let i64s = as_int64_array(&args[0])?;let new_array = i64s.iter().map(|array_elem| array_elem.map(|value| value + 1)).collect::<Int64Array>();- Ok(Arc::new(new_array))++ Ok(ColumnarValue::from(Arc::new(new_array) as ArrayRef))+ }+ fn documentation(&self) -> Option<&Documentation> {+ self.doc()}}```@@ -102,15 +118,75 @@ impl ScalarUDFImpl for AddOne {We now need to register the function with DataFusion so that it can be used in the context of a query.```rust+# use std::sync::Arc;+# use std::any::Any;+# use std::sync::LazyLock;+# use arrow::datatypes::DataType;+# use datafusion_common::cast::as_int64_array;+# use datafusion_common::{DataFusionError, plan_err, Result};+# use datafusion_expr::{col, ColumnarValue, ScalarFunctionArgs, Signature, Volatility};+# use datafusion::arrow::array::{ArrayRef, Int64Array};+# use datafusion_expr::{ScalarUDFImpl, ScalarUDF};+# use datafusion_macros::user_doc;+# use datafusion_doc::Documentation;+#+# /// This struct for a simple UDF that adds one to an int32+# #[user_doc(+# doc_section(label = "Math Functions"),+# description = "Add one udf",+# syntax_example = "add_one(1)"+# )]+# #[derive(Debug)]+# struct AddOne {+# signature: Signature,+# }+#+# impl AddOne {+# fn new() -> Self {+# Self {+# signature: Signature::uniform(1, vec![DataType::Int32], Volatility::Immutable),+# }+# }+# }+#+# /// Implement the ScalarUDFImpl trait for AddOne+# impl ScalarUDFImpl for AddOne {+# fn as_any(&self) -> &dyn Any { self }+# fn name(&self) -> &str { "add_one" }+# fn signature(&self) -> &Signature { &self.signature }+# fn return_type(&self, args: &[DataType]) -> Result<DataType> {+# if !matches!(args.get(0), Some(&DataType::Int32)) {+# return plan_err!("add_one only accepts Int32 arguments");+# }+# Ok(DataType::Int32)+# }+# // The actual implementation would add one to the argument+# fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {+# let args = ColumnarValue::values_to_arrays(&args.args)?;+# let i64s = as_int64_array(&args[0])?;+#+# let new_array = i64s+# .iter()+# .map(|array_elem| array_elem.map(|value| value + 1))+# .collect::<Int64Array>();+#+# Ok(ColumnarValue::from(Arc::new(new_array) as ArrayRef))+# }+# fn documentation(&self) -> Option<&Documentation> {+# self.doc()+# }+# }+use datafusion::execution::context::SessionContext;+// Create a new ScalarUDF from the implementationlet add_one = ScalarUDF::from(AddOne::new());+// Call the function `add_one(col)`+let expr = add_one.call(vec![col("a")]);+// register the UDF with the context so it can be invoked by name and from SQLlet mut ctx = SessionContext::new();ctx.register_udf(add_one.clone());--// Call the function `add_one(col)`-let expr = add_one.call(vec![col("a")]);```### Adding a Scalar UDF by [`create_udf`]@@ -121,7 +197,6 @@ There is a an older, more concise, but also more limited API [`create_udf`] avai```rustuse std::sync::Arc;-use datafusion::arrow::array::{ArrayRef, Int64Array};use datafusion::common::cast::as_int64_array;use datafusion::common::Result;@@ -145,6 +220,24 @@ This "works" in isolation, i.e. if you have a slice of `ArrayRef`s, you can call`ArrayRef` with 1 added to each value.```rust+# use std::sync::Arc;+# use datafusion::arrow::array::{ArrayRef, Int64Array};+# use datafusion::common::cast::as_int64_array;+# use datafusion::common::Result;+# use datafusion::logical_expr::ColumnarValue;+#+# pub fn add_one(args: &[ColumnarValue]) -> Result<ColumnarValue> {+# // Error handling omitted for brevity+# let args = ColumnarValue::values_to_arrays(args)?;+# let i64s = as_int64_array(&args[0])?;+#+# let new_array = i64s+# .iter()+# .map(|array_elem| array_elem.map(|value| value + 1))+# .collect::<Int64Array>();+#+# Ok(ColumnarValue::from(Arc::new(new_array) as ArrayRef))+# }let input = vec![Some(1), None, Some(3)];let input = ColumnarValue::from(Arc::new(Int64Array::from(input)) as ArrayRef);@@ -165,9 +258,26 @@ with the `SessionContext`.DataFusion provides the [`create_udf`] and helper functions to make this easier.```rust+# use std::sync::Arc;+# use datafusion::arrow::array::{ArrayRef, Int64Array};+# use datafusion::common::cast::as_int64_array;+# use datafusion::common::Result;+# use datafusion::logical_expr::ColumnarValue;+#+# pub fn add_one(args: &[ColumnarValue]) -> Result<ColumnarValue> {+# // Error handling omitted for brevity+# let args = ColumnarValue::values_to_arrays(args)?;+# let i64s = as_int64_array(&args[0])?;+#+# let new_array = i64s+# .iter()+# .map(|array_elem| array_elem.map(|value| value + 1))+# .collect::<Int64Array>();+#+# Ok(ColumnarValue::from(Arc::new(new_array) as ArrayRef))+# }use datafusion::logical_expr::{Volatility, create_udf};use datafusion::arrow::datatypes::DataType;-use std::sync::Arc;let udf = create_udf("add_one",@@ -178,12 +288,7 @@ let udf = create_udf();```-[`scalarudf`]: https://docs.rs/datafusion/latest/datafusion/logical_expr/struct.ScalarUDF.html-[`create_udf`]: https://docs.rs/datafusion/latest/datafusion/logical_expr/fn.create_udf.html-[`process_scalar_func_inputs`]: https://docs.rs/datafusion/latest/datafusion/physical_expr/functions/fn.process_scalar_func_inputs.html-[`advanced_udf.rs`]: https://github.com/apache/datafusion/blob/main/datafusion-examples/examples/advanced_udf.rs--A few things to note:+A few things to note on `create_udf`:- The first argument is the name of the function. This is the name that will be used in SQL queries.- The second argument is a vector of `DataType`s. This is the list of argument types that the function accepts. I.e. in@@ -198,20 +303,51 @@ A few things to note:That gives us a `ScalarUDF` that we can register with the `SessionContext`:```rust+# use std::sync::Arc;+# use datafusion::arrow::array::{ArrayRef, Int64Array};+# use datafusion::common::cast::as_int64_array;+# use datafusion::common::Result;+# use datafusion::logical_expr::ColumnarValue;+#+# pub fn add_one(args: &[ColumnarValue]) -> Result<ColumnarValue> {+# // Error handling omitted for brevity+# let args = ColumnarValue::values_to_arrays(args)?;+# let i64s = as_int64_array(&args[0])?;+#+# let new_array = i64s+# .iter()+# .map(|array_elem| array_elem.map(|value| value + 1))+# .collect::<Int64Array>();+#+# Ok(ColumnarValue::from(Arc::new(new_array) as ArrayRef))+# }+use datafusion::logical_expr::{Volatility, create_udf};+use datafusion::arrow::datatypes::DataType;use datafusion::execution::context::SessionContext;-let mut ctx = SessionContext::new();--ctx.register_udf(udf);+#[tokio::main]+async fn main() {+ let udf = create_udf(+ "add_one",+ vec![DataType::Int64],+ DataType::Int64,+ Volatility::Immutable,+ Arc::new(add_one),+ );++ let mut ctx = SessionContext::new();+ ctx.register_udf(udf);++ // At this point, you can use the `add_one` function in your query:+ let query = "SELECT add_one(1)";+ let df = ctx.sql(&query).await.unwrap();+}```-At this point, you can use the `add_one` function in your query:--```rust-let sql = "SELECT add_one(1)";--let df = ctx.sql( & sql).await.unwrap();-```+[`scalarudf`]: https://docs.rs/datafusion/latest/datafusion/logical_expr/struct.ScalarUDF.html+[`create_udf`]: https://docs.rs/datafusion/latest/datafusion/logical_expr/fn.create_udf.html+[`process_scalar_func_inputs`]: https://docs.rs/datafusion/latest/datafusion/physical_expr/functions/fn.process_scalar_func_inputs.html+[`advanced_udf.rs`]: https://github.com/apache/datafusion/blob/main/datafusion-examples/examples/advanced_udf.rs## Adding a Window UDF@@ -294,17 +430,61 @@ with the `SessionContext`. DataFusion provides the [`create_udwf`] helper functiThere is a lower level API with more functionality but is more complex, that is documented in [`advanced_udwf.rs`].```rust+# use datafusion::arrow::{array::{ArrayRef, Float64Array, AsArray}, datatypes::Float64Type};+# use datafusion::logical_expr::{PartitionEvaluator};+# use datafusion::common::ScalarValue;+# use datafusion::error::Result;+#+# #[derive(Clone, Debug)]+# struct MyPartitionEvaluator {}+#+# impl MyPartitionEvaluator {+# fn new() -> Self {+# Self {}+# }+# }+#+# impl PartitionEvaluator for MyPartitionEvaluator {+# fn uses_window_frame(&self) -> bool {+# true+# }+#+# fn evaluate(+# &mut self,+# values: &[ArrayRef],+# range: &std::ops::Range<usize>,+# ) -> Result<ScalarValue> {+# // Again, the input argument is an array of floating+# // point numbers to calculate a moving average+# let arr: &Float64Array = values[0].as_ref().as_primitive::<Float64Type>();+#+# let range_len = range.end - range.start;+#+# // our smoothing function will average all the values in the+# let output = if range_len > 0 {+# let sum: f64 = arr.values().iter().skip(range.start).take(range_len).sum();+# Some(sum / range_len as f64)+# } else {+# None+# };+#+# Ok(ScalarValue::Float64(output))+# }+# }+# fn make_partition_evaluator() -> Result<Box<dyn PartitionEvaluator>> {+# Ok(Box::new(MyPartitionEvaluator::new()))+# }use datafusion::logical_expr::{Volatility, create_udwf};use datafusion::arrow::datatypes::DataType;use std::sync::Arc;// here is where we define the UDWF. We also declare its signature:let smooth_it = create_udwf(-"smooth_it",-DataType::Float64,-Arc::new(DataType::Float64),-Volatility::Immutable,-Arc::new(make_partition_evaluator),+ "smooth_it",+ DataType::Float64,+ Arc::new(DataType::Float64),+ Volatility::Immutable,+ Arc::new(make_partition_evaluator),);```@@ -327,6 +507,62 @@ The `create_udwf` has five arguments to check:That gives us a `WindowUDF` that we can register with the `SessionContext`:```rust+# use datafusion::arrow::{array::{ArrayRef, Float64Array, AsArray}, datatypes::Float64Type};+# use datafusion::logical_expr::{PartitionEvaluator};+# use datafusion::common::ScalarValue;+# use datafusion::error::Result;+#+# #[derive(Clone, Debug)]+# struct MyPartitionEvaluator {}+#+# impl MyPartitionEvaluator {+# fn new() -> Self {+# Self {}+# }+# }+#+# impl PartitionEvaluator for MyPartitionEvaluator {+# fn uses_window_frame(&self) -> bool {+# true+# }+#+# fn evaluate(+# &mut self,+# values: &[ArrayRef],+# range: &std::ops::Range<usize>,+# ) -> Result<ScalarValue> {+# // Again, the input argument is an array of floating+# // point numbers to calculate a moving average+# let arr: &Float64Array = values[0].as_ref().as_primitive::<Float64Type>();+#+# let range_len = range.end - range.start;+#+# // our smoothing function will average all the values in the+# let output = if range_len > 0 {+# let sum: f64 = arr.values().iter().skip(range.start).take(range_len).sum();+# Some(sum / range_len as f64)+# } else {+# None+# };+#+# Ok(ScalarValue::Float64(output))+# }+# }+# fn make_partition_evaluator() -> Result<Box<dyn PartitionEvaluator>> {+# Ok(Box::new(MyPartitionEvaluator::new()))+# }+# use datafusion::logical_expr::{Volatility, create_udwf};+# use datafusion::arrow::datatypes::DataType;+# use std::sync::Arc;+#+# // here is where we define the UDWF. We also declare its signature:+# let smooth_it = create_udwf(+# "smooth_it",+# DataType::Float64,+# Arc::new(DataType::Float64),+# Volatility::Immutable,+# Arc::new(make_partition_evaluator),+# );use datafusion::execution::context::SessionContext;let ctx = SessionContext::new();@@ -336,10 +572,9 @@ ctx.register_udwf(smooth_it);At this point, you can use the `smooth_it` function in your query:-For example, if we have a [-`cars.csv`](https://github.com/apache/datafusion/blob/main/datafusion/core/tests/data/cars.csv) whose contents like+For example, if we have a [`cars.csv`](https://github.com/apache/datafusion/blob/main/datafusion/core/tests/data/cars.csv) whose contents like-```+```csvcar,speed,timered,20.0,1996-04-12T12:05:03.000000000red,20.3,1996-04-12T12:05:04.000000000@@ -351,30 +586,97 @@ green,10.3,1996-04-12T12:05:04.000000000Then, we can query like below:```rust+# use datafusion::arrow::{array::{ArrayRef, Float64Array, AsArray}, datatypes::Float64Type};+# use datafusion::logical_expr::{PartitionEvaluator};+# use datafusion::common::ScalarValue;+# use datafusion::error::Result;+#+# #[derive(Clone, Debug)]+# struct MyPartitionEvaluator {}+#+# impl MyPartitionEvaluator {+# fn new() -> Self {+# Self {}+# }+# }+#+# impl PartitionEvaluator for MyPartitionEvaluator {+# fn uses_window_frame(&self) -> bool {+# true+# }+#+# fn evaluate(+# &mut self,+# values: &[ArrayRef],+# range: &std::ops::Range<usize>,+# ) -> Result<ScalarValue> {+# // Again, the input argument is an array of floating+# // point numbers to calculate a moving average+# let arr: &Float64Array = values[0].as_ref().as_primitive::<Float64Type>();+#+# let range_len = range.end - range.start;+#+# // our smoothing function will average all the values in the+# let output = if range_len > 0 {+# let sum: f64 = arr.values().iter().skip(range.start).take(range_len).sum();+# Some(sum / range_len as f64)+# } else {+# None+# };+#+# Ok(ScalarValue::Float64(output))+# }+# }+# fn make_partition_evaluator() -> Result<Box<dyn PartitionEvaluator>> {+# Ok(Box::new(MyPartitionEvaluator::new()))+# }+# use datafusion::logical_expr::{Volatility, create_udwf};+# use datafusion::arrow::datatypes::DataType;+# use std::sync::Arc;+# use datafusion::execution::context::SessionContext;+use datafusion::datasource::file_format::options::CsvReadOptions;-// register csv table first-let csv_path = "cars.csv".to_string();-ctx.register_csv("cars", & csv_path, CsvReadOptions::default ().has_header(true)).await?;-// do query with smooth_it-let df = ctx-.sql(-"SELECT \- car, \- speed, \- smooth_it(speed) OVER (PARTITION BY car ORDER BY time) as smooth_speed,\- time \- from cars \- ORDER BY \- car",-)-.await?;-// print the results-df.show().await?;++#[tokio::main]+async fn main() -> Result<()> {++ let ctx = SessionContext::new();++ let smooth_it = create_udwf(+ "smooth_it",+ DataType::Float64,+ Arc::new(DataType::Float64),+ Volatility::Immutable,+ Arc::new(make_partition_evaluator),+ );+ ctx.register_udwf(smooth_it);++ // register csv table first+ let csv_path = "../../datafusion/core/tests/data/cars.csv".to_string();+ ctx.register_csv("cars", &csv_path, CsvReadOptions::default().has_header(true)).await?;++ // do query with smooth_it+ let df = ctx+ .sql(r#"+ SELECT+ car,+ speed,+ smooth_it(speed) OVER (PARTITION BY car ORDER BY time) as smooth_speed,+ time+ FROM cars+ ORDER BY car+ "#)+ .await?;++ // print the results+ df.show().await?;+ Ok(())+}```-the output will be like:+The output will be like:-```+```text+-------+-------+--------------------+---------------------+| car | speed | smooth_speed | time |+-------+-------+--------------------+---------------------+@@ -403,6 +705,7 @@ Aggregate UDFs are functions that take a group of rows and return a single valueFor example, we will declare a single-type, single return type UDAF that computes the geometric mean.```rust+use datafusion::arrow::array::ArrayRef;use datafusion::scalar::ScalarValue;use datafusion::{error::Result, physical_plan::Accumulator};@@ -427,7 +730,7 @@ impl Accumulator for GeometricMean {// This function serializes our state to `ScalarValue`, which DataFusion uses// to pass this state between execution stages.// Note that this can be arbitrary data.- fn state(&self) -> Result<Vec<ScalarValue>> {+ fn state(&mut self) -> Result<Vec<ScalarValue>> {Ok(vec![ScalarValue::from(self.prod),ScalarValue::from(self.n),@@ -436,7 +739,7 @@ impl Accumulator for GeometricMean {// DataFusion expects this function to return the final value of this aggregator.// in this case, this is the formula of the geometric mean- fn evaluate(&self) -> Result<ScalarValue> {+ fn evaluate(&mut self) -> Result<ScalarValue> {let value = self.prod.powf(1.0 / self.n as f64);Ok(ScalarValue::from(value))}@@ -491,37 +794,106 @@ impl Accumulator for GeometricMean {}```-### registering an Aggregate UDF+### Registering an Aggregate UDFTo register a Aggregate UDF, you need to wrap the function implementation in a [`AggregateUDF`] struct and then registerit with the `SessionContext`. DataFusion provides the [`create_udaf`] helper functions to make this easier.There is a lower level API with more functionality but is more complex, that is documented in [`advanced_udaf.rs`].```rust+# use datafusion::arrow::array::ArrayRef;+# use datafusion::scalar::ScalarValue;+# use datafusion::{error::Result, physical_plan::Accumulator};+#+# #[derive(Debug)]+# struct GeometricMean {+# n: u32,+# prod: f64,+# }+#+# impl GeometricMean {+# pub fn new() -> Self {+# GeometricMean { n: 0, prod: 1.0 }+# }+# }+#+# impl Accumulator for GeometricMean {+# fn state(&mut self) -> Result<Vec<ScalarValue>> {+# Ok(vec![+# ScalarValue::from(self.prod),+# ScalarValue::from(self.n),+# ])+# }+#+# fn evaluate(&mut self) -> Result<ScalarValue> {+# let value = self.prod.powf(1.0 / self.n as f64);+# Ok(ScalarValue::from(value))+# }+#+# fn update_batch(&mut self, values: &[ArrayRef]) -> Result<()> {+# if values.is_empty() {+# return Ok(());+# }+# let arr = &values[0];+# (0..arr.len()).try_for_each(|index| {+# let v = ScalarValue::try_from_array(arr, index)?;+#+# if let ScalarValue::Float64(Some(value)) = v {+# self.prod *= value;+# self.n += 1;+# } else {+# unreachable!("")+# }+# Ok(())+# })+# }+#+# fn merge_batch(&mut self, states: &[ArrayRef]) -> Result<()> {+# if states.is_empty() {+# return Ok(());+# }+# let arr = &states[0];+# (0..arr.len()).try_for_each(|index| {+# let v = states+# .iter()+# .map(|array| ScalarValue::try_from_array(array, index))+# .collect::<Result<Vec<_>>>()?;+# if let (ScalarValue::Float64(Some(prod)), ScalarValue::UInt32(Some(n))) = (&v[0], &v[1])+# {+# self.prod *= prod;+# self.n += n;+# } else {+# unreachable!("")+# }+# Ok(())+# })+# }+#+# fn size(&self) -> usize {+# std::mem::size_of_val(self)+# }+# }+use datafusion::logical_expr::{Volatility, create_udaf};use datafusion::arrow::datatypes::DataType;use std::sync::Arc;// here is where we define the UDAF. We also declare its signature:let geometric_mean = create_udaf(-// the name; used to represent it in plan descriptions and in the registry, to use in SQL.-"geo_mean",-// the input type; DataFusion guarantees that the first entry of `values` in `update` has this type.-vec![DataType::Float64],-// the return type; DataFusion expects this to match the type returned by `evaluate`.-Arc::new(DataType::Float64),-Volatility::Immutable,-// This is the accumulator factory; DataFusion uses it to create new accumulators.-Arc::new( | _ | Ok(Box::new(GeometricMean::new()))),-// This is the description of the state. `state()` must match the types here.-Arc::new(vec![DataType::Float64, DataType::UInt32]),+ // the name; used to represent it in plan descriptions and in the registry, to use in SQL.+ "geo_mean",+ // the input type; DataFusion guarantees that the first entry of `values` in `update` has this type.+ vec![DataType::Float64],+ // the return type; DataFusion expects this to match the type returned by `evaluate`.+ Arc::new(DataType::Float64),+ Volatility::Immutable,+ // This is the accumulator factory; DataFusion uses it to create new accumulators.+ Arc::new( | _ | Ok(Box::new(GeometricMean::new()))),+ // This is the description of the state. `state()` must match the types here.+ Arc::new(vec![DataType::Float64, DataType::UInt32]),);```-[`aggregateudf`]: https://docs.rs/datafusion/latest/datafusion/logical_expr/struct.AggregateUDF.html-[`create_udaf`]: https://docs.rs/datafusion/latest/datafusion/logical_expr/fn.create_udaf.html-[`advanced_udaf.rs`]: https://github.com/apache/datafusion/blob/main/datafusion-examples/examples/advanced_udaf.rs-The `create_udaf` has six arguments to check:- The first argument is the name of the function. This is the name that will be used in SQL queries.@@ -535,22 +907,119 @@ The `create_udaf` has six arguments to check:- The fifth argument is the function implementation. This is the function that we defined above.- The sixth argument is the description of the state, which will by passed between execution stages.-That gives us a `AggregateUDF` that we can register with the `SessionContext`:-```rust-use datafusion::execution::context::SessionContext;-let ctx = SessionContext::new();+# use datafusion::arrow::array::ArrayRef;+# use datafusion::scalar::ScalarValue;+# use datafusion::{error::Result, physical_plan::Accumulator};+#+# #[derive(Debug)]+# struct GeometricMean {+# n: u32,+# prod: f64,+# }+#+# impl GeometricMean {+# pub fn new() -> Self {+# GeometricMean { n: 0, prod: 1.0 }+# }+# }+#+# impl Accumulator for GeometricMean {+# fn state(&mut self) -> Result<Vec<ScalarValue>> {+# Ok(vec![+# ScalarValue::from(self.prod),+# ScalarValue::from(self.n),+# ])+# }+#+# fn evaluate(&mut self) -> Result<ScalarValue> {+# let value = self.prod.powf(1.0 / self.n as f64);+# Ok(ScalarValue::from(value))+# }+#+# fn update_batch(&mut self, values: &[ArrayRef]) -> Result<()> {+# if values.is_empty() {+# return Ok(());+# }+# let arr = &values[0];+# (0..arr.len()).try_for_each(|index| {+# let v = ScalarValue::try_from_array(arr, index)?;+#+# if let ScalarValue::Float64(Some(value)) = v {+# self.prod *= value;+# self.n += 1;+# } else {+# unreachable!("")+# }+# Ok(())+# })+# }+#+# fn merge_batch(&mut self, states: &[ArrayRef]) -> Result<()> {+# if states.is_empty() {+# return Ok(());+# }+# let arr = &states[0];+# (0..arr.len()).try_for_each(|index| {+# let v = states+# .iter()+# .map(|array| ScalarValue::try_from_array(array, index))+# .collect::<Result<Vec<_>>>()?;+# if let (ScalarValue::Float64(Some(prod)), ScalarValue::UInt32(Some(n))) = (&v[0], &v[1])+# {+# self.prod *= prod;+# self.n += n;+# } else {+# unreachable!("")+# }+# Ok(())+# })+# }+#+# fn size(&self) -> usize {+# std::mem::size_of_val(self)+# }+# }-ctx.register_udaf(geometric_mean);-```+use datafusion::logical_expr::{Volatility, create_udaf};+use datafusion::arrow::datatypes::DataType;+use std::sync::Arc;+use datafusion::execution::context::SessionContext;+use datafusion::datasource::file_format::options::CsvReadOptions;-Then, we can query like below:+#[tokio::main]+async fn main() -> Result<()> {+ let geometric_mean = create_udaf(+ "geo_mean",+ vec![DataType::Float64],+ Arc::new(DataType::Float64),+ Volatility::Immutable,+ Arc::new( | _ | Ok(Box::new(GeometricMean::new()))),+ Arc::new(vec![DataType::Float64, DataType::UInt32]),+ );++ // That gives us a `AggregateUDF` that we can register with the `SessionContext`:+ use datafusion::execution::context::SessionContext;++ let ctx = SessionContext::new();+ ctx.register_udaf(geometric_mean);++ // register csv table first+ let csv_path = "../../datafusion/core/tests/data/cars.csv".to_string();+ ctx.register_csv("cars", &csv_path, CsvReadOptions::default().has_header(true)).await?;++ // Then, we can query like below:+ let df = ctx.sql("SELECT geo_mean(speed) FROM cars").await?;+ Ok(())+}-```rust-let df = ctx.sql("SELECT geo_mean(a) FROM t").await?;```+[`aggregateudf`]: https://docs.rs/datafusion/latest/datafusion/logical_expr/struct.AggregateUDF.html+[`create_udaf`]: https://docs.rs/datafusion/latest/datafusion/logical_expr/fn.create_udaf.html+[`advanced_udaf.rs`]: https://github.com/apache/datafusion/blob/main/datafusion-examples/examples/advanced_udaf.rs+## Adding a User-Defined Table FunctionA User-Defined Table Function (UDTF) is a function that takes parameters and returns a `TableProvider`.@@ -592,12 +1061,17 @@ In the `call` method, you parse the input `Expr`s and return a `TableProvider`.validation of the input `Expr`s, e.g. checking that the number of arguments is correct.```rust-use datafusion::common::plan_err;-use datafusion::datasource::function::TableFunctionImpl;-// Other imports here+use std::sync::Arc;+use datafusion::common::{plan_err, ScalarValue, Result};+use datafusion::catalog::{TableFunctionImpl, TableProvider};+use datafusion::arrow::array::{ArrayRef, Int64Array};+use datafusion::datasource::memory::MemTable;+use arrow::record_batch::RecordBatch;+use arrow::datatypes::{DataType, Field, Schema};+use datafusion_expr::Expr;/// A table function that returns a table provider with the value as a single column-#[derive(Default)]+#[derive(Debug)]pub struct EchoFunction {}impl TableFunctionImpl for EchoFunction {@@ -628,22 +1102,57 @@ impl TableFunctionImpl for EchoFunction {With the UDTF implemented, you can register it with the `SessionContext`:```rust+# use std::sync::Arc;+# use datafusion::common::{plan_err, ScalarValue, Result};+# use datafusion::catalog::{TableFunctionImpl, TableProvider};+# use datafusion::arrow::array::{ArrayRef, Int64Array};+# use datafusion::datasource::memory::MemTable;+# use arrow::record_batch::RecordBatch;+# use arrow::datatypes::{DataType, Field, Schema};+# use datafusion_expr::Expr;+#+# /// A table function that returns a table provider with the value as a single column+# #[derive(Debug, Default)]+# pub struct EchoFunction {}+#+# impl TableFunctionImpl for EchoFunction {+# fn call(&self, exprs: &[Expr]) -> Result<Arc<dyn TableProvider>> {+# let Some(Expr::Literal(ScalarValue::Int64(Some(value)))) = exprs.get(0) else {+# return plan_err!("First argument must be an integer");+# };+#+# // Create the schema for the table+# let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Int64, false)]));+#+# // Create a single RecordBatch with the value as a single column+# let batch = RecordBatch::try_new(+# schema.clone(),+# vec![Arc::new(Int64Array::from(vec![*value]))],+# )?;+#+# // Create a MemTable plan that returns the RecordBatch+# let provider = MemTable::try_new(schema, vec![vec![batch]])?;+#+# Ok(Arc::new(provider))+# }+# }+use datafusion::execution::context::SessionContext;+use datafusion::arrow::util::pretty;-let ctx = SessionContext::new();+#[tokio::main]+async fn main() -> Result<()> {+ let ctx = SessionContext::new();-ctx.register_udtf("echo", Arc::new(EchoFunction::default ()));-```+ ctx.register_udtf("echo", Arc::new(EchoFunction::default()));-And if all goes well, you can use it in your query:+ // And if all goes well, you can use it in your query:-```rust-use datafusion::arrow::util::pretty;--let df = ctx.sql("SELECT * FROM echo(1)").await?;+ let results = ctx.sql("SELECT * FROM echo(1)").await?.collect().await?;+ pretty::print_batches(&results)?;+ Ok(())+}-let results = df.collect().await?;-pretty::print_batches( & results) ?;// +---+// | a |// +---+diff --git a/docs/source/library-user-guide/api-health.md b/docs/source/library-user-guide/api-health.mdindex b9c6de370e..87d3754b21 100644--- a/docs/source/library-user-guide/api-health.md+++ b/docs/source/library-user-guide/api-health.md@@ -62,8 +62,8 @@ To mark the API as deprecated, use the `#[deprecated(since = "...", note = "..."For example:```rust- #[deprecated(since = "41.0.0", note = "Use SessionStateBuilder")]- pub fn new_with_config_rt(config: SessionConfig, runtime: Arc<RuntimeEnv>) -> Self+#[deprecated(since = "41.0.0", note = "Use new API instead")]+pub fn api_to_deprecated(a: usize, b: usize) {}```Deprecated methods will remain in the codebase for a period of 6 major versions or 6 months, whichever is longer, to provide users ample time to transition away from them.diff --git a/docs/source/library-user-guide/catalogs.md b/docs/source/library-user-guide/catalogs.mdindex 13158d6564..906039ba23 100644--- a/docs/source/library-user-guide/catalogs.md+++ b/docs/source/library-user-guide/catalogs.md@@ -40,6 +40,11 @@ In the following example, we'll implement an in memory catalog, starting with thThe `MemorySchemaProvider` is a simple implementation of the `SchemaProvider` trait. It stores state (i.e. tables) in a `DashMap`, which then underlies the `SchemaProvider` trait.```rust+use std::sync::Arc;+use dashmap::DashMap;+use datafusion::catalog::{TableProvider, SchemaProvider};++#[derive(Debug)]pub struct MemorySchemaProvider {tables: DashMap<String, Arc<dyn TableProvider>>,}@@ -50,6 +55,20 @@ pub struct MemorySchemaProvider {Then we implement the `SchemaProvider` trait for `MemorySchemaProvider`.```rust+# use std::sync::Arc;+# use dashmap::DashMap;+# use datafusion::catalog::TableProvider;+#+# #[derive(Debug)]+# pub struct MemorySchemaProvider {+# tables: DashMap<String, Arc<dyn TableProvider>>,+# }++use std::any::Any;+use datafusion::catalog::SchemaProvider;+use async_trait::async_trait;+use datafusion::common::{Result, exec_err};+#[async_trait]impl SchemaProvider for MemorySchemaProvider {fn as_any(&self) -> &dyn Any {@@ -63,8 +82,8 @@ impl SchemaProvider for MemorySchemaProvider {.collect()}- async fn table(&self, name: &str) -> Option<Arc<dyn TableProvider>> {- self.tables.get(name).map(|table| table.value().clone())+ async fn table(&self, name: &str) -> Result<Option<Arc<dyn TableProvider>>> {+ Ok(self.tables.get(name).map(|table| table.value().clone()))}fn register_table(@@ -93,12 +112,85 @@ impl SchemaProvider for MemorySchemaProvider {Without getting into a `CatalogProvider` implementation, we can create a `MemorySchemaProvider` and register `TableProvider`s with it.```rust+# use std::sync::Arc;+# use dashmap::DashMap;+# use datafusion::catalog::TableProvider;+#+# #[derive(Debug)]+# pub struct MemorySchemaProvider {+# tables: DashMap<String, Arc<dyn TableProvider>>,+# }+#+# use std::any::Any;+# use datafusion::catalog::SchemaProvider;+# use async_trait::async_trait;+# use datafusion::common::{Result, exec_err};+#+# #[async_trait]+# impl SchemaProvider for MemorySchemaProvider {+# fn as_any(&self) -> &dyn Any {+# self+# }+#+# fn table_names(&self) -> Vec<String> {+# self.tables+# .iter()+# .map(|table| table.key().clone())+# .collect()+# }+#+# async fn table(&self, name: &str) -> Result<Option<Arc<dyn TableProvider>>> {+# Ok(self.tables.get(name).map(|table| table.value().clone()))+# }+#+# fn register_table(+# &self,+# name: String,+# table: Arc<dyn TableProvider>,+# ) -> Result<Option<Arc<dyn TableProvider>>> {+# if self.table_exist(name.as_str()) {+# return exec_err!(+# "The table {name} already exists"+# );+# }+# Ok(self.tables.insert(name, table))+# }+#+# fn deregister_table(&self, name: &str) -> Result<Option<Arc<dyn TableProvider>>> {+# Ok(self.tables.remove(name).map(|(_, table)| table))+# }+#+# fn table_exist(&self, name: &str) -> bool {+# self.tables.contains_key(name)+# }+# }++use arrow::datatypes::{DataType, Field, Schema, SchemaRef};+use arrow::record_batch::RecordBatch;+use datafusion::datasource::MemTable;+use arrow::array::{self, Array, ArrayRef, Int32Array};++impl MemorySchemaProvider {+ /// Instantiates a new MemorySchemaProvider with an empty collection of tables.+ pub fn new() -> Self {+ Self {+ tables: DashMap::new(),+ }+ }+}+let schema_provider = Arc::new(MemorySchemaProvider::new());-let table_provider = _; // create a table provider-schema_provider.register_table("table_name".to_string(), table_provider);+let table_provider = {+ let schema = Arc::new(Schema::new(vec![Field::new("i", DataType::Int32, true)]));+ let arr = Arc::new(Int32Array::from((1..=1).collect::<Vec<_>>()));+ let partitions = vec![vec![RecordBatch::try_new(schema.clone(), vec![arr as ArrayRef]).unwrap()]];+ Arc::new(MemTable::try_new(schema, partitions).unwrap())+};++schema_provider.register_table("users".to_string(), table_provider);-let table = schema_provider.table("table_name").unwrap();+let table = schema_provider.table("users");```### Asynchronous `SchemaProvider`@@ -108,27 +200,44 @@ It's often useful to fetch metadata about which tables are in a schema, from a rThe trait is roughly the same except for the `table` method, and the addition of the `#[async_trait]` attribute.```rust+# use async_trait::async_trait;+# use std::sync::Arc;+# use datafusion::catalog::{TableProvider, SchemaProvider};+# use datafusion::common::Result;+#+# type OriginSchema = arrow::datatypes::Schema;+#+# #[derive(Debug)]+# struct Schema(OriginSchema);+#[async_trait]impl SchemaProvider for Schema {- async fn table(&self, name: &str) -> Option<Arc<dyn TableProvider>> {- // fetch metadata from remote source+ async fn table(&self, name: &str) -> Result<Option<Arc<dyn TableProvider>>> {+# todo!();}++# fn as_any(&self) -> &(dyn std::any::Any + 'static) { todo!() }+# fn table_names(&self) -> Vec<std::string::String> { todo!() }+# fn table_exist(&self, _: &str) -> bool { todo!() }}```## Implementing `MemoryCatalogProvider`-As mentioned, the `CatalogProvider` can manage the schemas in a catalog, and the `MemoryCatalogProvider` is a simple implementation of the `CatalogProvider` trait. It stores schemas in a `DashMap`.+As mentioned, the `CatalogProvider` can manage the schemas in a catalog, and the `MemoryCatalogProvider` is a simple implementation of the `CatalogProvider` trait. It stores schemas in a `DashMap`. With that the `CatalogProvider` trait can be implemented.```rust+use std::any::Any;+use std::sync::Arc;+use dashmap::DashMap;+use datafusion::catalog::{CatalogProvider, SchemaProvider};+use datafusion::common::Result;++#[derive(Debug)]pub struct MemoryCatalogProvider {schemas: DashMap<String, Arc<dyn SchemaProvider>>,}-```--With that the `CatalogProvider` trait can be implemented.-```rustimpl CatalogProvider for MemoryCatalogProvider {fn as_any(&self) -> &dyn Any {self@@ -167,20 +276,24 @@ impl CatalogProvider for MemoryCatalogProvider {}```-Again, this is fairly straightforward, as there's an underlying data structure to store the state, via key-value pairs.+Again, this is fairly straightforward, as there's an underlying data structure to store the state, via key-value pairs. With that the `CatalogProviderList` trait can be implemented.## Implementing `MemoryCatalogProviderList````rust++use std::any::Any;+use std::sync::Arc;+use dashmap::DashMap;+use datafusion::catalog::{CatalogProviderList, CatalogProvider};+use datafusion::common::Result;++#[derive(Debug)]pub struct MemoryCatalogProviderList {/// Collection of catalogs containing schemas and ultimately TableProviderspub catalogs: DashMap<String, Arc<dyn CatalogProvider>>,}-```-With that the `CatalogProviderList` trait can be implemented.--```rustimpl CatalogProviderList for MemoryCatalogProviderList {fn as_any(&self) -> &dyn Any {selfdiff --git a/docs/source/library-user-guide/custom-table-providers.md b/docs/source/library-user-guide/custom-table-providers.mdindex a7183fb311..886ac96295 100644--- a/docs/source/library-user-guide/custom-table-providers.md+++ b/docs/source/library-user-guide/custom-table-providers.md@@ -37,19 +37,84 @@ The `ExecutionPlan` trait at its core is a way to get a stream of batches. The aThere are many different types of `SendableRecordBatchStream` implemented in DataFusion -- you can use a pre existing one, such as `MemoryStream` (if your `RecordBatch`es are all in memory) or implement your own custom logic, depending on your usecase.-Looking at the [example in this repo][ex], the execute method:+Looking at the full example below:```rust+use std::any::Any;+use std::sync::{Arc, Mutex};+use std::collections::{BTreeMap, HashMap};+use datafusion::common::Result;+use datafusion::arrow::datatypes::{DataType, Field, Schema, SchemaRef};+use datafusion::physical_plan::expressions::PhysicalSortExpr;+use datafusion::physical_plan::{+ ExecutionPlan, SendableRecordBatchStream, DisplayAs, DisplayFormatType,+ Statistics, PlanProperties+};+use datafusion::execution::context::TaskContext;+use datafusion::arrow::array::{UInt64Builder, UInt8Builder};+use datafusion::physical_plan::memory::MemoryStream;+use datafusion::arrow::record_batch::RecordBatch;++/// A User, with an id and a bank account+#[derive(Clone, Debug)]+struct User {+ id: u8,+ bank_account: u64,+}++/// A custom datasource, used to represent a datastore with a single index+#[derive(Clone, Debug)]+pub struct CustomDataSource {+ inner: Arc<Mutex<CustomDataSourceInner>>,+}++#[derive(Debug)]+struct CustomDataSourceInner {+ data: HashMap<u8, User>,+ bank_account_index: BTreeMap<u64, u8>,+}++#[derive(Debug)]struct CustomExec {db: CustomDataSource,projected_schema: SchemaRef,}+impl DisplayAs for CustomExec {+ fn fmt_as(&self, _t: DisplayFormatType, f: &mut std::fmt::Formatter) -> std::fmt::Result {+ write!(f, "CustomExec")+ }+}+impl ExecutionPlan for CustomExec {- fn name(&self) {+ fn name(&self) -> &str {"CustomExec"}+ fn as_any(&self) -> &dyn Any {+ self+ }++ fn schema(&self) -> SchemaRef {+ self.projected_schema.clone()+ }+++ fn properties(&self) -> &PlanProperties {+ unreachable!()+ }++ fn children(&self) -> Vec<&Arc<dyn ExecutionPlan>> {+ Vec::new()+ }++ fn with_new_children(+ self: Arc<Self>,+ _: Vec<Arc<dyn ExecutionPlan>>,+ ) -> Result<Arc<dyn ExecutionPlan>> {+ Ok(self)+ }+fn execute(&self,_partition: usize,@@ -83,7 +148,7 @@ impl ExecutionPlan for CustomExec {}```-This:+This `execute` method:1. Gets the users from the database2. Constructs the individual output arrays (columns)@@ -98,6 +163,134 @@ With the `ExecutionPlan` implemented, we can now implement the `scan` method ofThe `scan` method of the `TableProvider` returns a `Result<Arc<dyn ExecutionPlan>>`. We can use the `Arc` to return a reference-counted pointer to the `ExecutionPlan` we implemented. In the example, this is done by:```rust++# use std::any::Any;+# use std::sync::{Arc, Mutex};+# use std::collections::{BTreeMap, HashMap};+# use datafusion::common::Result;+# use datafusion::arrow::datatypes::{DataType, Field, Schema, SchemaRef};+# use datafusion::physical_plan::expressions::PhysicalSortExpr;+# use datafusion::physical_plan::{+# ExecutionPlan, SendableRecordBatchStream, DisplayAs, DisplayFormatType,+# Statistics, PlanProperties+# };+# use datafusion::execution::context::TaskContext;+# use datafusion::arrow::array::{UInt64Builder, UInt8Builder};+# use datafusion::physical_plan::memory::MemoryStream;+# use datafusion::arrow::record_batch::RecordBatch;+#+# /// A User, with an id and a bank account+# #[derive(Clone, Debug)]+# struct User {+# id: u8,+# bank_account: u64,+# }+#+# /// A custom datasource, used to represent a datastore with a single index+# #[derive(Clone, Debug)]+# pub struct CustomDataSource {+# inner: Arc<Mutex<CustomDataSourceInner>>,+# }+#+# #[derive(Debug)]+# struct CustomDataSourceInner {+# data: HashMap<u8, User>,+# bank_account_index: BTreeMap<u64, u8>,+# }+#+# #[derive(Debug)]+# struct CustomExec {+# db: CustomDataSource,+# projected_schema: SchemaRef,+# }+#+# impl DisplayAs for CustomExec {+# fn fmt_as(&self, _t: DisplayFormatType, f: &mut std::fmt::Formatter) -> std::fmt::Result {+# write!(f, "CustomExec")+# }+# }+#+# impl ExecutionPlan for CustomExec {+# fn name(&self) -> &str {+# "CustomExec"+# }+#+# fn as_any(&self) -> &dyn Any {+# self+# }+#+# fn schema(&self) -> SchemaRef {+# self.projected_schema.clone()+# }+#+#+# fn properties(&self) -> &PlanProperties {+# unreachable!()+# }+#+# fn children(&self) -> Vec<&Arc<dyn ExecutionPlan>> {+# Vec::new()+# }+#+# fn with_new_children(+# self: Arc<Self>,+# _: Vec<Arc<dyn ExecutionPlan>>,+# ) -> Result<Arc<dyn ExecutionPlan>> {+# Ok(self)+# }+#+# fn execute(+# &self,+# _partition: usize,+# _context: Arc<TaskContext>,+# ) -> Result<SendableRecordBatchStream> {+# let users: Vec<User> = {+# let db = self.db.inner.lock().unwrap();+# db.data.values().cloned().collect()+# };+#+# let mut id_array = UInt8Builder::with_capacity(users.len());+# let mut account_array = UInt64Builder::with_capacity(users.len());+#+# for user in users {+# id_array.append_value(user.id);+# account_array.append_value(user.bank_account);+# }+#+# Ok(Box::pin(MemoryStream::try_new(+# vec![RecordBatch::try_new(+# self.projected_schema.clone(),+# vec![+# Arc::new(id_array.finish()),+# Arc::new(account_array.finish()),+# ],+# )?],+# self.schema(),+# None,+# )?))+# }+# }++use async_trait::async_trait;+use datafusion::logical_expr::expr::Expr;+use datafusion::datasource::{TableProvider, TableType};+use datafusion::physical_plan::project_schema;+use datafusion::catalog::Session;++impl CustomExec {+ fn new(+ projections: Option<&Vec<usize>>,+ schema: SchemaRef,+ db: CustomDataSource,+ ) -> Self {+ let projected_schema = project_schema(&schema, projections).unwrap();+ Self {+ db,+ projected_schema,+ }+ }+}+impl CustomDataSource {pub(crate) async fn create_physical_plan(&self,@@ -110,6 +303,21 @@ impl CustomDataSource {#[async_trait]impl TableProvider for CustomDataSource {+ fn as_any(&self) -> &dyn Any {+ self+ }++ fn schema(&self) -> SchemaRef {+ SchemaRef::new(Schema::new(vec![+ Field::new("id", DataType::UInt8, false),+ Field::new("bank_account", DataType::UInt64, true),+ ]))+ }++ fn table_type(&self) -> TableType {+ TableType::Base+ }+async fn scan(&self,_state: &dyn Session,@@ -145,17 +353,194 @@ For filters that can be pushed down, they'll be passed to the `scan` method as tIn order to use the custom table provider, we need to register it with DataFusion. This is done by creating a `TableProvider` and registering it with the `SessionContext`.-```rust-let ctx = SessionContext::new();--let custom_table_provider = CustomDataSource::new();-ctx.register_table("custom_table", Arc::new(custom_table_provider));-```-This will allow you to use the custom table provider in DataFusion. For example, you could use it in a SQL query to get a `DataFrame`.```rust-let df = ctx.sql("SELECT id, bank_account FROM custom_table")?;+# use std::any::Any;+# use std::sync::{Arc, Mutex};+# use std::collections::{BTreeMap, HashMap};+# use datafusion::common::Result;+# use datafusion::arrow::datatypes::{DataType, Field, Schema, SchemaRef};+# use datafusion::physical_plan::expressions::PhysicalSortExpr;+# use datafusion::physical_plan::{+# ExecutionPlan, SendableRecordBatchStream, DisplayAs, DisplayFormatType,+# Statistics, PlanProperties+# };+# use datafusion::execution::context::TaskContext;+# use datafusion::arrow::array::{UInt64Builder, UInt8Builder};+# use datafusion::physical_plan::memory::MemoryStream;+# use datafusion::arrow::record_batch::RecordBatch;+#+# /// A User, with an id and a bank account+# #[derive(Clone, Debug)]+# struct User {+# id: u8,+# bank_account: u64,+# }+#+# /// A custom datasource, used to represent a datastore with a single index+# #[derive(Clone, Debug)]+# pub struct CustomDataSource {+# inner: Arc<Mutex<CustomDataSourceInner>>,+# }+#+# #[derive(Debug)]+# struct CustomDataSourceInner {+# data: HashMap<u8, User>,+# bank_account_index: BTreeMap<u64, u8>,+# }+#+# #[derive(Debug)]+# struct CustomExec {+# db: CustomDataSource,+# projected_schema: SchemaRef,+# }+#+# impl DisplayAs for CustomExec {+# fn fmt_as(&self, _t: DisplayFormatType, f: &mut std::fmt::Formatter) -> std::fmt::Result {+# write!(f, "CustomExec")+# }+# }+#+# impl ExecutionPlan for CustomExec {+# fn name(&self) -> &str {+# "CustomExec"+# }+#+# fn as_any(&self) -> &dyn Any {+# self+# }+#+# fn schema(&self) -> SchemaRef {+# self.projected_schema.clone()+# }+#+#+# fn properties(&self) -> &PlanProperties {+# unreachable!()+# }+#+# fn children(&self) -> Vec<&Arc<dyn ExecutionPlan>> {+# Vec::new()+# }+#+# fn with_new_children(+# self: Arc<Self>,+# _: Vec<Arc<dyn ExecutionPlan>>,+# ) -> Result<Arc<dyn ExecutionPlan>> {+# Ok(self)+# }+#+# fn execute(+# &self,+# _partition: usize,+# _context: Arc<TaskContext>,+# ) -> Result<SendableRecordBatchStream> {+# let users: Vec<User> = {+# let db = self.db.inner.lock().unwrap();+# db.data.values().cloned().collect()+# };+#+# let mut id_array = UInt8Builder::with_capacity(users.len());+# let mut account_array = UInt64Builder::with_capacity(users.len());+#+# for user in users {+# id_array.append_value(user.id);+# account_array.append_value(user.bank_account);+# }+#+# Ok(Box::pin(MemoryStream::try_new(+# vec![RecordBatch::try_new(+# self.projected_schema.clone(),+# vec![+# Arc::new(id_array.finish()),+# Arc::new(account_array.finish()),+# ],+# )?],+# self.schema(),+# None,+# )?))+# }+# }++# use async_trait::async_trait;+# use datafusion::logical_expr::expr::Expr;+# use datafusion::datasource::{TableProvider, TableType};+# use datafusion::physical_plan::project_schema;+# use datafusion::catalog::Session;+#+# impl CustomExec {+# fn new(+# projections: Option<&Vec<usize>>,+# schema: SchemaRef,+# db: CustomDataSource,+# ) -> Self {+# let projected_schema = project_schema(&schema, projections).unwrap();+# Self {+# db,+# projected_schema,+# }+# }+# }+#+# impl CustomDataSource {+# pub(crate) async fn create_physical_plan(+# &self,+# projections: Option<&Vec<usize>>,+# schema: SchemaRef,+# ) -> Result<Arc<dyn ExecutionPlan>> {+# Ok(Arc::new(CustomExec::new(projections, schema, self.clone())))+# }+# }+#+# #[async_trait]+# impl TableProvider for CustomDataSource {+# fn as_any(&self) -> &dyn Any {+# self+# }+#+# fn schema(&self) -> SchemaRef {+# SchemaRef::new(Schema::new(vec![+# Field::new("id", DataType::UInt8, false),+# Field::new("bank_account", DataType::UInt64, true),+# ]))+# }+#+# fn table_type(&self) -> TableType {+# TableType::Base+# }+#+# async fn scan(+# &self,+# _state: &dyn Session,+# projection: Option<&Vec<usize>>,+# // filters and limit can be used here to inject some push-down operations if needed+# _filters: &[Expr],+# _limit: Option<usize>,+# ) -> Result<Arc<dyn ExecutionPlan>> {+# return self.create_physical_plan(projection, self.schema()).await;+# }+# }++use datafusion::execution::context::SessionContext;++#[tokio::main]+async fn main() -> Result<()> {+ let ctx = SessionContext::new();++ let custom_table_provider = CustomDataSource {+ inner: Arc::new(Mutex::new(CustomDataSourceInner {+ data: Default::default(),+ bank_account_index: Default::default(),+ })),+ };++ ctx.register_table("customers", Arc::new(custom_table_provider));+ let df = ctx.sql("SELECT id, bank_account FROM customers").await?;++ Ok(())+}+```## Recapdiff --git a/docs/source/library-user-guide/query-optimizer.md b/docs/source/library-user-guide/query-optimizer.mdindex c2c60af85f..fad8adf83d 100644--- a/docs/source/library-user-guide/query-optimizer.md+++ b/docs/source/library-user-guide/query-optimizer.md@@ -35,18 +35,28 @@ and applying it to a logical plan to produce an optimized logical plan.```rust+use std::sync::Arc;+use datafusion::logical_expr::{col, lit, LogicalPlan, LogicalPlanBuilder};+use datafusion::optimizer::{OptimizerRule, OptimizerContext, Optimizer};+// We need a logical plan as the starting point. There are many ways to build a logical plan://// The `datafusion-expr` crate provides a LogicalPlanBuilder// The `datafusion-sql` crate provides a SQL query planner that can create a LogicalPlan from SQL// The `datafusion` crate provides a DataFrame API that can create a LogicalPlan-let logical_plan = ...-let mut config = OptimizerContext::default();-let optimizer = Optimizer::new(&config);-let optimized_plan = optimizer.optimize(&logical_plan, &config, observe)?;+let initial_logical_plan = LogicalPlanBuilder::empty(false).build().unwrap();++// use builtin rules or customized rules+let rules: Vec<Arc<dyn OptimizerRule + Send + Sync>> = vec![];++let optimizer = Optimizer::with_rules(rules);++let config = OptimizerContext::new().with_max_passes(16);-fn observe(plan: &LogicalPlan, rule: &dyn OptimizerRule) {+let optimized_plan = optimizer.optimize(initial_logical_plan.clone(), &config, observer);++fn observer(plan: &LogicalPlan, rule: &dyn OptimizerRule) {println!("After applying rule '{}':\n{}",rule.name(),@@ -55,16 +65,6 @@ fn observe(plan: &LogicalPlan, rule: &dyn OptimizerRule) {}```-## Providing Custom Rules--The optimizer can be created with a custom set of rules.--```rust-let optimizer = Optimizer::with_rules(vec![- Arc::new(MyRule {})-]);-```-## Writing Optimization RulesPlease refer to the@@ -72,26 +72,71 @@ Please refer to theexample to learn more about the general approach to writing optimizer rules andthen move onto studying the existing rules.+`OptimizerRule` transforms one ['LogicalPlan'] into another which+computes the same results, but in a potentially more efficient+way. If there are no suitable transformations for the input plan,+the optimizer can simply return it as is.+All rules must implement the `OptimizerRule` trait.```rust-/// `OptimizerRule` transforms one ['LogicalPlan'] into another which-/// computes the same results, but in a potentially more efficient-/// way. If there are no suitable transformations for the input plan,-/// the optimizer can simply return it as is.-pub trait OptimizerRule {- /// Rewrite `plan` to an optimized form- fn optimize(- &self,- plan: &LogicalPlan,- config: &dyn OptimizerConfig,- ) -> Result<LogicalPlan>;+# use datafusion::common::tree_node::Transformed;+# use datafusion::common::Result;+# use datafusion::logical_expr::LogicalPlan;+# use datafusion::optimizer::{OptimizerConfig, OptimizerRule};+#++#[derive(Default, Debug)]+struct MyOptimizerRule {}++impl OptimizerRule for MyOptimizerRule {+ fn name(&self) -> &str {+ "my_optimizer_rule"+ }- /// A human readable name for this optimizer rule- fn name(&self) -> &str;+ fn rewrite(+ &self,+ plan: LogicalPlan,+ _config: &dyn OptimizerConfig,+ ) -> Result<Transformed<LogicalPlan>> {+ unimplemented!()+ }}```+## Providing Custom Rules++The optimizer can be created with a custom set of rules.++```rust+# use std::sync::Arc;+# use datafusion::logical_expr::{col, lit, LogicalPlan, LogicalPlanBuilder};+# use datafusion::optimizer::{OptimizerRule, OptimizerConfig, OptimizerContext, Optimizer};+# use datafusion::common::tree_node::Transformed;+# use datafusion::common::Result;+#+# #[derive(Default, Debug)]+# struct MyOptimizerRule {}+#+# impl OptimizerRule for MyOptimizerRule {+# fn name(&self) -> &str {+# "my_optimizer_rule"+# }+#+# fn rewrite(+# &self,+# plan: LogicalPlan,+# _config: &dyn OptimizerConfig,+# ) -> Result<Transformed<LogicalPlan>> {+# unimplemented!()+# }+# }++let optimizer = Optimizer::with_rules(vec![+ Arc::new(MyOptimizerRule {})+]);+```+### General GuidelinesRules typical walk the logical plan and walk the expression trees inside operators and selectively mutate@@ -168,16 +213,19 @@ and [#3555](https://github.com/apache/datafusion/issues/3555) occur where the exThere are currently two ways to create a name for an expression in the logical plan.```rust+# use datafusion::common::Result;+# struct Expr;+impl Expr {/// Returns the name of this expression as it should appear in a schema. This name/// will not include any CAST expressions.pub fn display_name(&self) -> Result<String> {- create_name(self)+ Ok("display_name".to_string())}/// Returns a full and complete string representation of this expression.pub fn canonical_name(&self) -> String {- format!("{}", self)+ "canonical_name".to_string()}}```@@ -187,93 +235,99 @@ name to be used in a schema, `display_name` should be used.### Utilities-There are a number of utility methods provided that take care of some common tasks.+There are a number of [utility methods][util] provided that take care of some common tasks.-### ExprVisitor+[util]: https://github.com/apache/datafusion/blob/main/datafusion/expr/src/utils.rs-The `ExprVisitor` and `ExprVisitable` traits provide a mechanism for applying a visitor pattern to an expression tree.+### Recursively walk an expression tree-Here is an example that demonstrates this.+The [TreeNode API] provides a convenient way to recursively walk an expression or plan tree.-```rust-fn extract_subquery_filters(expression: &Expr, extracted: &mut Vec<Expr>) -> Result<()> {- struct InSubqueryVisitor<'a> {- accum: &'a mut Vec<Expr>,- }+For example, to find all subquery references in a logical plan, the following code can be used:- impl ExpressionVisitor for InSubqueryVisitor<'_> {- fn pre_visit(self, expr: &Expr) -> Result<Recursion<Self>> {+```rust+# use datafusion::prelude::*;+# use datafusion::common::tree_node::{TreeNode, TreeNodeRecursion};+# use datafusion::common::Result;+// Return all subquery references in an expression+fn extract_subquery_filters(expression: &Expr) -> Result<Vec<&Expr>> {+ let mut extracted = vec![];+ expression.apply(|expr| {if let Expr::InSubquery(_) = expr {- self.accum.push(expr.to_owned());+ extracted.push(expr);}- Ok(Recursion::Continue(self))- }- }+ Ok(TreeNodeRecursion::Continue)+ })?;+ Ok(extracted)+}+```- expression.accept(InSubqueryVisitor { accum: extracted })?;- Ok(())+Likewise you can use the [TreeNode API] to rewrite a `LogicalPlan` or `ExecutionPlan`++```rust+# use datafusion::prelude::*;+# use datafusion::logical_expr::{LogicalPlan, Join};+# use datafusion::common::tree_node::{TreeNode, TreeNodeRecursion};+# use datafusion::common::Result;+// Return all joins in a logical plan+fn find_joins(overall_plan: &LogicalPlan) -> Result<Vec<&Join>> {+ let mut extracted = vec![];+ overall_plan.apply(|plan| {+ if let LogicalPlan::Join(join) = plan {+ extracted.push(join);+ }+ Ok(TreeNodeRecursion::Continue)+ })?;+ Ok(extracted)}```-### Rewriting Expressions+### Rewriting expressions-The `MyExprRewriter` trait can be implemented to provide a way to rewrite expressions. This rule can then be applied-to an expression by calling `Expr::rewrite` (from the `ExprRewritable` trait).+The [TreeNode API] also provides a convenient way to rewrite expressions and+plans as well. For example to rewrite all expressions like-The `rewrite` method will perform a depth first walk of the expression and its children to rewrite an expression,-consuming `self` producing a new expression.+```sql+col BETWEEN x AND y+```-```rust-let mut expr_rewriter = MyExprRewriter {};-let expr = expr.rewrite(&mut expr_rewriter)?;+into++```sql+col >= x AND col <= y```-Here is an example implementation which will rewrite `expr BETWEEN a AND b` as `expr >= a AND expr <= b`. Note that the-implementation does not need to perform any recursion since this is handled by the `rewrite` method.+you can use the following code:```rust-struct MyExprRewriter {}--impl ExprRewriter for MyExprRewriter {- fn mutate(&mut self, expr: Expr) -> Result<Expr> {- match expr {- Expr::Between {+# use datafusion::prelude::*;+# use datafusion::logical_expr::{Between};+# use datafusion::logical_expr::expr_fn::*;+# use datafusion::common::tree_node::{Transformed, TreeNode, TreeNodeRecursion};+# use datafusion::common::Result;+// Recursively rewrite all BETWEEN expressions+// returns Transformed::yes if any changes were made+fn rewrite_between(expr: Expr) -> Result<Transformed<Expr>> {+ // transform_up does a bottom up rewrite+ expr.transform_up(|expr| {+ // only handle BETWEEN expressions+ let Expr::Between(Between {negated,expr,low,high,- } => {- let expr: Expr = expr.as_ref().clone();- let low: Expr = low.as_ref().clone();- let high: Expr = high.as_ref().clone();- if negated {- Ok(expr.clone().lt(low).or(expr.clone().gt(high)))- } else {- Ok(expr.clone().gt_eq(low).and(expr.clone().lt_eq(high)))- }- }- _ => Ok(expr.clone()),- }- }-}-```--### optimize_children--Typically a rule is applied recursively to all operators within a query plan. Rather than duplicate-that logic in each rule, an `optimize_children` method is provided. This recursively invokes the `optimize` method on-the plan's children and then returns a node of the same type.--```rust-fn optimize(- &self,- plan: &LogicalPlan,- _config: &mut OptimizerConfig,-) -> Result<LogicalPlan> {- // recurse down and optimize children first- let plan = utils::optimize_children(self, plan, _config)?;-- ...+ }) = expr else {+ return Ok(Transformed::no(expr))+ };+ let rewritten_expr = if negated {+ // don't rewrite NOT BETWEEN+ Expr::Between(Between::new(expr, negated, low, high))+ } else {+ // rewrite to (expr >= low) AND (expr <= high)+ expr.clone().gt_eq(*low).and(expr.lt_eq(*high))+ };+ Ok(Transformed::yes(rewritten_expr))+ })}```diff --git a/docs/source/library-user-guide/working-with-exprs.md b/docs/source/library-user-guide/working-with-exprs.mdindex e0b6f434a0..1a6e912308 100644--- a/docs/source/library-user-guide/working-with-exprs.md+++ b/docs/source/library-user-guide/working-with-exprs.md@@ -61,12 +61,34 @@ We'll use a `ScalarUDF` expression as our example. This necessitates implementinSo assuming you've written that function, you can use it to create an `Expr`:```rust+# use std::sync::Arc;+# use datafusion::arrow::array::{ArrayRef, Int64Array};+# use datafusion::common::cast::as_int64_array;+# use datafusion::common::Result;+# use datafusion::logical_expr::ColumnarValue;+#+# pub fn add_one(args: &[ColumnarValue]) -> Result<ColumnarValue> {+# // Error handling omitted for brevity+# let args = ColumnarValue::values_to_arrays(args)?;+# let i64s = as_int64_array(&args[0])?;+#+# let new_array = i64s+# .iter()+# .map(|array_elem| array_elem.map(|value| value + 1))+# .collect::<Int64Array>();+#+# Ok(ColumnarValue::from(Arc::new(new_array) as ArrayRef))+# }+use datafusion::logical_expr::{Volatility, create_udf};+use datafusion::arrow::datatypes::DataType;+use datafusion::logical_expr::{col, lit};+let add_one_udf = create_udf("add_one",vec![DataType::Int64],- Arc::new(DataType::Int64),+ DataType::Int64,Volatility::Immutable,- make_scalar_function(add_one), // <-- the function we wrote+ Arc::new(add_one),);// make the expr `add_one(5)`@@ -99,11 +121,16 @@ In our example, we'll use rewriting to update our `add_one` UDF, to be rewrittenTo implement the inlining, we'll need to write a function that takes an `Expr` and returns a `Result<Expr>`. If the expression is _not_ to be rewritten `Transformed::no` is used to wrap the original `Expr`. If the expression _is_ to be rewritten, `Transformed::yes` is used to wrap the new `Expr`.```rust-fn rewrite_add_one(expr: Expr) -> Result<Expr> {+use datafusion::common::Result;+use datafusion::common::tree_node::{Transformed, TreeNode};+use datafusion::logical_expr::{col, lit, Expr};+use datafusion::logical_expr::{ScalarUDF};++fn rewrite_add_one(expr: Expr) -> Result<Transformed<Expr>> {expr.transform(&|expr| {Ok(match expr {- Expr::ScalarUDF(scalar_fun) if scalar_fun.fun.name == "add_one" => {- let input_arg = scalar_fun.args[0].clone();+ Expr::ScalarFunction(scalar_func) if scalar_func.func.inner().name() == "add_one" => {+ let input_arg = scalar_func.args[0].clone();let new_expression = input_arg + lit(1i64);Transformed::yes(new_expression)@@ -124,6 +151,27 @@ We'll call our rule `AddOneInliner` and implement the `OptimizerRule` trait. The- `try_optimize` - takes a `LogicalPlan` and returns an `Option<LogicalPlan>`. If the rule is able to optimize the plan, it returns `Some(LogicalPlan)` with the optimized plan. If the rule is not able to optimize the plan, it returns `None`.```rust+use std::sync::Arc;+use datafusion::common::Result;+use datafusion::common::tree_node::{Transformed, TreeNode};+use datafusion::logical_expr::{col, lit, Expr, LogicalPlan, LogicalPlanBuilder};+use datafusion::optimizer::{OptimizerRule, OptimizerConfig, OptimizerContext, Optimizer};++# fn rewrite_add_one(expr: Expr) -> Result<Transformed<Expr>> {+# expr.transform(&|expr| {+# Ok(match expr {+# Expr::ScalarFunction(scalar_func) if scalar_func.func.inner().name() == "add_one" => {+# let input_arg = scalar_func.args[0].clone();+# let new_expression = input_arg + lit(1i64);+#+# Transformed::yes(new_expression)+# }+# _ => Transformed::no(expr),+# })+# })+# }++#[derive(Default, Debug)]struct AddOneInliner {}impl OptimizerRule for AddOneInliner {@@ -131,23 +179,26 @@ impl OptimizerRule for AddOneInliner {"add_one_inliner"}- fn try_optimize(+ fn rewrite(&self,- plan: &LogicalPlan,- config: &dyn OptimizerConfig,- ) -> Result<Option<LogicalPlan>> {+ plan: LogicalPlan,+ _config: &dyn OptimizerConfig,+ ) -> Result<Transformed<LogicalPlan>> {// Map over the expressions and rewrite them- let new_expressions = plan+ let new_expressions: Vec<Expr> = plan.expressions().into_iter().map(|expr| rewrite_add_one(expr))- .collect::<Result<Vec<_>>>()?;+ .collect::<Result<Vec<_>>>()? // returns Vec<Transformed<Expr>>+ .into_iter()+ .map(|transformed| transformed.data)+ .collect();let inputs = plan.inputs().into_iter().cloned().collect::<Vec<_>>();- let plan = plan.with_new_exprs(&new_expressions, &inputs);+ let plan: Result<LogicalPlan> = plan.with_new_exprs(new_expressions, inputs);- plan.map(Some)+ plan.map(|p| Transformed::yes(p))}}```@@ -161,25 +212,111 @@ We're almost there. Let's just test our rule works properly.Testing the rule is fairly simple, we can create a SessionState with our rule and then create a DataFrame and run a query. The logical plan will be optimized by our rule.```rust-use datafusion::prelude::*;--let rules = Arc::new(AddOneInliner {});-let state = ctx.state().with_optimizer_rules(vec![rules]);--let ctx = SessionContext::with_state(state);-ctx.register_udf(add_one);--let sql = "SELECT add_one(1) AS added_one";-let plan = ctx.sql(sql).await?.logical_plan();--println!("{:?}", plan);+# use std::sync::Arc;+# use datafusion::common::Result;+# use datafusion::common::tree_node::{Transformed, TreeNode};+# use datafusion::logical_expr::{col, lit, Expr, LogicalPlan, LogicalPlanBuilder};+# use datafusion::optimizer::{OptimizerRule, OptimizerConfig, OptimizerContext, Optimizer};+# use datafusion::arrow::array::{ArrayRef, Int64Array};+# use datafusion::common::cast::as_int64_array;+# use datafusion::logical_expr::ColumnarValue;+# use datafusion::logical_expr::{Volatility, create_udf};+# use datafusion::arrow::datatypes::DataType;+#+# fn rewrite_add_one(expr: Expr) -> Result<Transformed<Expr>> {+# expr.transform(&|expr| {+# Ok(match expr {+# Expr::ScalarFunction(scalar_func) if scalar_func.func.inner().name() == "add_one" => {+# let input_arg = scalar_func.args[0].clone();+# let new_expression = input_arg + lit(1i64);+#+# Transformed::yes(new_expression)+# }+# _ => Transformed::no(expr),+# })+# })+# }+#+# #[derive(Default, Debug)]+# struct AddOneInliner {}+#+# impl OptimizerRule for AddOneInliner {+# fn name(&self) -> &str {+# "add_one_inliner"+# }+#+# fn rewrite(+# &self,+# plan: LogicalPlan,+# _config: &dyn OptimizerConfig,+# ) -> Result<Transformed<LogicalPlan>> {+# // Map over the expressions and rewrite them+# let new_expressions: Vec<Expr> = plan+# .expressions()+# .into_iter()+# .map(|expr| rewrite_add_one(expr))+# .collect::<Result<Vec<_>>>()? // returns Vec<Transformed<Expr>>+# .into_iter()+# .map(|transformed| transformed.data)+# .collect();+#+# let inputs = plan.inputs().into_iter().cloned().collect::<Vec<_>>();+#+# let plan: Result<LogicalPlan> = plan.with_new_exprs(new_expressions, inputs);+#+# plan.map(|p| Transformed::yes(p))+# }+# }+#+# pub fn add_one(args: &[ColumnarValue]) -> Result<ColumnarValue> {+# // Error handling omitted for brevity+# let args = ColumnarValue::values_to_arrays(args)?;+# let i64s = as_int64_array(&args[0])?;+#+# let new_array = i64s+# .iter()+# .map(|array_elem| array_elem.map(|value| value + 1))+# .collect::<Int64Array>();+#+# Ok(ColumnarValue::from(Arc::new(new_array) as ArrayRef))+# }++use datafusion::execution::context::SessionContext;++#[tokio::main]+async fn main() -> Result<()> {++ let ctx = SessionContext::new();+ // ctx.add_optimizer_rule(Arc::new(AddOneInliner {}));++ let add_one_udf = create_udf(+ "add_one",+ vec![DataType::Int64],+ DataType::Int64,+ Volatility::Immutable,+ Arc::new(add_one),+ );+ ctx.register_udf(add_one_udf);++ let sql = "SELECT add_one(5) AS added_one";+ // let plan = ctx.sql(sql).await?.into_unoptimized_plan().clone();+ let plan = ctx.sql(sql).await?.into_optimized_plan()?.clone();++ let expected = r#"Projection: Int64(6) AS added_one+ EmptyRelation"#;++ assert_eq!(plan.to_string(), expected);++ Ok(())+}```-This results in the following output:+This plan is optimized as:```text-Projection: Int64(1) + Int64(1) AS added_one- EmptyRelation+Projection: add_one(Int64(5)) AS added_one+ -> Projection: Int64(5) + Int64(1) AS added_one+ -> Projection: Int64(6) AS added_one```I.e. the `add_one` UDF has been inlined into the projection.@@ -189,27 +326,23 @@ I.e. the `add_one` UDF has been inlined into the projection.The `arrow::datatypes::DataType` of the expression can be obtained by calling the `get_type` given something that implements `Expr::Schemable`, for example a `DFschema` object:```rust-use arrow_schema::DataType;-use datafusion::common::{DFField, DFSchema};+use arrow::datatypes::{DataType, Field};+use datafusion::common::DFSchema;use datafusion::logical_expr::{col, ExprSchemable};use std::collections::HashMap;+// Get the type of an expression that adds 2 columns. Adding an Int32+// and Float32 results in Float32 typelet expr = col("c1") + col("c2");-let schema = DFSchema::new_with_metadata(+let schema = DFSchema::from_unqualified_fields(vec![- DFField::new_unqualified("c1", DataType::Int32, true),- DFField::new_unqualified("c2", DataType::Float32, true),- ],+ Field::new("c1", DataType::Int32, true),+ Field::new("c2", DataType::Float32, true),+ ]+ .into(),HashMap::new(),-)-.unwrap();-print!("type = {}", expr.get_type(&schema).unwrap());-```--This results in the following output:--```text-type = Float32+).unwrap();+assert_eq!("Float32", format!("{}", expr.get_type(&schema).unwrap()));```## Conclusiondiff --git a/docs/source/user-guide/cli/usage.md b/docs/source/user-guide/cli/usage.mdindex 6a620fc692..fb238dad10 100644--- a/docs/source/user-guide/cli/usage.md+++ b/docs/source/user-guide/cli/usage.md@@ -127,7 +127,7 @@ supports additional statements and commands:Show configuration options-```SQL+```sql> show all;+-------------------------------------------------+---------+@@ -163,7 +163,7 @@ Show specific configuration option- Set configuration options-```SQL+```sql> SET datafusion.execution.batch_size to 1024;```diff --git a/docs/source/user-guide/crate-configuration.md b/docs/source/user-guide/crate-configuration.mdindex 9d22e34030..f4a1910f5f 100644--- a/docs/source/user-guide/crate-configuration.md+++ b/docs/source/user-guide/crate-configuration.md@@ -68,7 +68,9 @@ codegen-units = 1Then, in `main.rs.` update the memory allocator with the below after your imports:-```rust ,ignore+<!-- Note can't include snmalloc-rs in a runnable example, because it takes over the global allocator -->++```no-runuse datafusion::prelude::*;#[global_allocator]diff --git a/docs/source/user-guide/explain-usage.md b/docs/source/user-guide/explain-usage.mdindex 32a87ae919..d89ed5f0e7 100644--- a/docs/source/user-guide/explain-usage.md+++ b/docs/source/user-guide/explain-usage.md@@ -49,7 +49,7 @@ LIMIT 5;The output will look like-```+```text+---------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+| plan_type | plan |+---------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+@@ -70,7 +70,7 @@ Elapsed 0.060 seconds.There are two sections: logical plan and physical plan-- **Logical Plan:** is a plan generated for a specific SQL query, DataFrame, or other language without the+- **Logical Plan:** is a plan generated for a specific SQL query, DataFrame, or other language without theknowledge of the underlying data organization.- **Physical Plan:** is a plan generated from a logical plan along with consideration of the hardwareconfiguration (e.g number of CPUs) and the underlying data organization (e.g number of files).@@ -87,7 +87,7 @@ query run faster depends on the reason it is slow and beyond the scope of this dA query plan is an upside down tree, and we always read from bottom up. Thephysical plan in Figure 1 in tree format will look like-```+```text▲││@@ -174,7 +174,7 @@ above but with `EXPLAIN ANALYZE` (note the output is edited for clarity)[`executionplan::metrics`]: https://docs.rs/datafusion/latest/datafusion/physical_plan/trait.ExecutionPlan.html#method.metrics-```+```sql> EXPLAIN ANALYZE SELECT "WatchID" AS wid, "hits.parquet"."ClientIP" AS ipFROM 'hits.parquet'WHERE starts_with("URL", 'http://domcheloveplanet.ru/')@@ -267,7 +267,7 @@ LIMIT 10;We can again see the query plan by using `EXPLAIN`:-```+```sql> EXPLAIN SELECT "UserID", COUNT(*) FROM 'hits.parquet' GROUP BY "UserID" ORDER BY COUNT(*) DESC LIMIT 10;+---------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+| plan_type | plan |diff --git a/docs/source/user-guide/sql/aggregate_functions.md b/docs/source/user-guide/sql/aggregate_functions.mdindex 221bb0572e..7d88d3168d 100644--- a/docs/source/user-guide/sql/aggregate_functions.md+++ b/docs/source/user-guide/sql/aggregate_functions.md@@ -58,7 +58,7 @@ Aggregate functions operate on a set of values to compute a single result.Returns an array created from the expression elements. If ordering is required, elements are inserted in the specified order.-```+```sqlarray_agg(expression [ORDER BY expression])```@@ -81,7 +81,7 @@ array_agg(expression [ORDER BY expression])Returns the average of numeric values in the specified column.-```+```sqlavg(expression)```@@ -108,7 +108,7 @@ avg(expression)Computes the bitwise AND of all non-null input values.-```+```sqlbit_and(expression)```@@ -120,7 +120,7 @@ bit_and(expression)Computes the bitwise OR of all non-null input values.-```+```sqlbit_or(expression)```@@ -132,7 +132,7 @@ bit_or(expression)Computes the bitwise exclusive OR of all non-null input values.-```+```sqlbit_xor(expression)```@@ -144,7 +144,7 @@ bit_xor(expression)Returns true if all non-null input values are true, otherwise false.-```+```sqlbool_and(expression)```@@ -167,7 +167,7 @@ bool_and(expression)Returns true if all non-null input values are true, otherwise false.-```+```sqlbool_and(expression)```@@ -190,7 +190,7 @@ bool_and(expression)Returns the number of non-null values in the specified column. To include null values in the total count, use `count(*)`.-```+```sqlcount(expression)```@@ -220,7 +220,7 @@ count(expression)Returns the first element in an aggregation group according to the requested ordering. If no ordering is given, returns an arbitrary element from the group.-```+```sqlfirst_value(expression [ORDER BY expression])```@@ -243,7 +243,7 @@ first_value(expression [ORDER BY expression])Returns 1 if the data is aggregated across the specified column, or 0 if it is not aggregated in the result set.-```+```sqlgrouping(expression)```@@ -270,7 +270,7 @@ grouping(expression)Returns the last element in an aggregation group according to the requested ordering. If no ordering is given, returns an arbitrary element from the group.-```+```sqllast_value(expression [ORDER BY expression])```@@ -293,7 +293,7 @@ last_value(expression [ORDER BY expression])Returns the maximum value in the specified column.-```+```sqlmax(expression)```@@ -320,7 +320,7 @@ _Alias of [avg](#avg)._Returns the median value in the specified column.-```+```sqlmedian(expression)```@@ -343,7 +343,7 @@ median(expression)Returns the minimum value in the specified column.-```+```sqlmin(expression)```@@ -366,7 +366,7 @@ min(expression)Concatenates the values of string expressions and places separator values between them.-```+```sqlstring_agg(expression, delimiter)```@@ -391,7 +391,7 @@ string_agg(expression, delimiter)Returns the sum of all values in the specified column.-```+```sqlsum(expression)```@@ -414,7 +414,7 @@ sum(expression)Returns the statistical sample variance of a set of numbers.-```+```sqlvar(expression)```@@ -431,7 +431,7 @@ var(expression)Returns the statistical population variance of a set of numbers.-```+```sqlvar_pop(expression)```@@ -479,7 +479,7 @@ _Alias of [var](#var)._Returns the coefficient of correlation between two numeric values.-```+```sqlcorr(expression1, expression2)```@@ -507,7 +507,7 @@ _Alias of [covar_samp](#covar_samp)._Returns the sample covariance of a set of number pairs.-```+```sqlcovar_samp(expression1, expression2)```@@ -531,7 +531,7 @@ covar_samp(expression1, expression2)Returns the sample covariance of a set of number pairs.-```+```sqlcovar_samp(expression1, expression2)```@@ -559,7 +559,7 @@ covar_samp(expression1, expression2)Returns the nth value in a group of values.-```+```sqlnth_value(expression, n ORDER BY expression)```@@ -588,7 +588,7 @@ nth_value(expression, n ORDER BY expression)Computes the average of the independent variable (input) expression_x for the non-null paired data points.-```+```sqlregr_avgx(expression_y, expression_x)```@@ -601,7 +601,7 @@ regr_avgx(expression_y, expression_x)Computes the average of the dependent variable (output) expression_y for the non-null paired data points.-```+```sqlregr_avgy(expression_y, expression_x)```@@ -614,7 +614,7 @@ regr_avgy(expression_y, expression_x)Counts the number of non-null paired data points.-```+```sqlregr_count(expression_y, expression_x)```@@ -627,7 +627,7 @@ regr_count(expression_y, expression_x)Computes the y-intercept of the linear regression line. For the equation (y = kx + b), this function returns b.-```+```sqlregr_intercept(expression_y, expression_x)```@@ -640,7 +640,7 @@ regr_intercept(expression_y, expression_x)Computes the square of the correlation coefficient between the independent and dependent variables.-```+```sqlregr_r2(expression_y, expression_x)```@@ -653,7 +653,7 @@ regr_r2(expression_y, expression_x)Returns the slope of the linear regression line for non-null pairs in aggregate columns. Given input column Y and X: regr_slope(Y, X) returns the slope (k in Y = k\*X + b) using minimal RSS fitting.-```+```sqlregr_slope(expression_y, expression_x)```@@ -666,7 +666,7 @@ regr_slope(expression_y, expression_x)Computes the sum of squares of the independent variable.-```+```sqlregr_sxx(expression_y, expression_x)```@@ -679,7 +679,7 @@ regr_sxx(expression_y, expression_x)Computes the sum of products of paired data points.-```+```sqlregr_sxy(expression_y, expression_x)```@@ -692,7 +692,7 @@ regr_sxy(expression_y, expression_x)Computes the sum of squares of the dependent variable.-```+```sqlregr_syy(expression_y, expression_x)```@@ -705,7 +705,7 @@ regr_syy(expression_y, expression_x)Returns the standard deviation of a set of numbers.-```+```sqlstddev(expression)```@@ -732,7 +732,7 @@ stddev(expression)Returns the population standard deviation of a set of numbers.-```+```sqlstddev_pop(expression)```@@ -766,7 +766,7 @@ _Alias of [stddev](#stddev)._Returns the approximate number of distinct input values calculated using the HyperLogLog algorithm.-```+```sqlapprox_distinct(expression)```@@ -789,7 +789,7 @@ approx_distinct(expression)Returns the approximate median (50th percentile) of input values. It is an alias of `approx_percentile_cont(x, 0.5)`.-```+```sqlapprox_median(expression)```@@ -812,7 +812,7 @@ approx_median(expression)Returns the approximate percentile of input values using the t-digest algorithm.-```+```sqlapprox_percentile_cont(expression, percentile, centroids)```@@ -837,7 +837,7 @@ approx_percentile_cont(expression, percentile, centroids)Returns the weighted approximate percentile of input values using the t-digest algorithm.-```+```sqlapprox_percentile_cont_with_weight(expression, weight, percentile)```diff --git a/docs/source/user-guide/sql/ddl.md b/docs/source/user-guide/sql/ddl.mdindex e16b9681eb..71475cff9a 100644--- a/docs/source/user-guide/sql/ddl.md+++ b/docs/source/user-guide/sql/ddl.md@@ -55,7 +55,7 @@ file system or remote object store as a named table which can be queried.The supported syntax is:-```+```sqlCREATE [UNBOUNDED] EXTERNAL TABLE[ IF NOT EXISTS ]<TABLE_NAME>[ (<column_definition>) ]@@ -185,7 +185,7 @@ OPTIONS ('has_header' 'true');Where `WITH ORDER` clause specifies the sort order:-```+```sqlWITH ORDER (sort_expression1 [ASC | DESC] [NULLS { FIRST | LAST }][, sort_expression2 [ASC | DESC] [NULLS { FIRST | LAST }] ...])```@@ -198,7 +198,7 @@ WITH ORDER (sort_expression1 [ASC | DESC] [NULLS { FIRST | LAST }]If data sources are already partitioned in Hive style, `PARTITIONED BY` can be used for partition pruning.-```+```text/mnt/nyctaxi/year=2022/month=01/tripdata.parquet/mnt/nyctaxi/year=2021/month=12/tripdata.parquet/mnt/nyctaxi/year=2021/month=11/tripdata.parquetdiff --git a/docs/source/user-guide/sql/dml.md b/docs/source/user-guide/sql/dml.mdindex dd016cabbf..4eda59d6de 100644--- a/docs/source/user-guide/sql/dml.md+++ b/docs/source/user-guide/sql/dml.md@@ -28,7 +28,7 @@ Copies the contents of a table or query to file(s). Supported fileformats are `parquet`, `csv`, `json`, and `arrow`.<pre>-COPY { <i><b>table_name</i></b> | <i><b>query</i></b> }+COPY { <i><b>table_name</i></b> | <i><b>query</i></b> }TO '<i><b>file_name</i></b>'[ STORED AS <i><b>format</i></b> ][ PARTITIONED BY <i><b>column_name</i></b> [, ...] ]@@ -91,7 +91,7 @@ of hive-style partitioned parquet files:If the the data contains values of `x` and `y` in column1 and only `a` incolumn2, output files will appear in the following directory structure:-```+```textdir_name/column1=x/column2=a/diff --git a/docs/source/user-guide/sql/explain.md b/docs/source/user-guide/sql/explain.mdindex 709e6311c2..3f2c7de43e 100644--- a/docs/source/user-guide/sql/explain.md+++ b/docs/source/user-guide/sql/explain.md@@ -32,8 +32,9 @@ EXPLAIN [ANALYZE] [VERBOSE] statementShows the execution plan of a statement.If you need more detailed output, use `EXPLAIN VERBOSE`.-```+```sqlEXPLAIN SELECT SUM(x) FROM table GROUP BY b;++---------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+| plan_type | plan |+---------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+@@ -56,8 +57,9 @@ EXPLAIN SELECT SUM(x) FROM table GROUP BY b;Shows the execution plan and metrics of a statement.If you need more information output, use `EXPLAIN ANALYZE VERBOSE`.-```+```sqlEXPLAIN ANALYZE SELECT SUM(x) FROM table GROUP BY b;++-------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------+| plan_type | plan |+-------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------+diff --git a/docs/source/user-guide/sql/scalar_functions.md b/docs/source/user-guide/sql/scalar_functions.mdindex b769b8b7bd..b14bf5b2cc 100644--- a/docs/source/user-guide/sql/scalar_functions.md+++ b/docs/source/user-guide/sql/scalar_functions.md@@ -73,7 +73,7 @@ dev/update_function_docs.sh file for updating surrounding text.Returns the absolute value of a number.-```+```sqlabs(numeric_expression)```@@ -85,7 +85,7 @@ abs(numeric_expression)Returns the arc cosine or inverse cosine of a number.-```+```sqlacos(numeric_expression)```@@ -97,7 +97,7 @@ acos(numeric_expression)Returns the area hyperbolic cosine or inverse hyperbolic cosine of a number.-```+```sqlacosh(numeric_expression)```@@ -109,7 +109,7 @@ acosh(numeric_expression)Returns the arc sine or inverse sine of a number.-```+```sqlasin(numeric_expression)```@@ -121,7 +121,7 @@ asin(numeric_expression)Returns the area hyperbolic sine or inverse hyperbolic sine of a number.-```+```sqlasinh(numeric_expression)```@@ -133,7 +133,7 @@ asinh(numeric_expression)Returns the arc tangent or inverse tangent of a number.-```+```sqlatan(numeric_expression)```@@ -145,7 +145,7 @@ atan(numeric_expression)Returns the arc tangent or inverse tangent of `expression_y / expression_x`.-```+```sqlatan2(expression_y, expression_x)```@@ -160,7 +160,7 @@ atan2(expression_y, expression_x)Returns the area hyperbolic tangent or inverse hyperbolic tangent of a number.-```+```sqlatanh(numeric_expression)```@@ -172,7 +172,7 @@ atanh(numeric_expression)Returns the cube root of a number.-```+```sqlcbrt(numeric_expression)```@@ -184,7 +184,7 @@ cbrt(numeric_expression)Returns the nearest integer greater than or equal to a number.-```+```sqlceil(numeric_expression)```@@ -196,7 +196,7 @@ ceil(numeric_expression)Returns the cosine of a number.-```+```sqlcos(numeric_expression)```@@ -208,7 +208,7 @@ cos(numeric_expression)Returns the hyperbolic cosine of a number.-```+```sqlcosh(numeric_expression)```@@ -220,7 +220,7 @@ cosh(numeric_expression)Returns the cotangent of a number.-```+```sqlcot(numeric_expression)```@@ -232,7 +232,7 @@ cot(numeric_expression)Converts radians to degrees.-```+```sqldegrees(numeric_expression)```@@ -244,7 +244,7 @@ degrees(numeric_expression)Returns the base-e exponential of a number.-```+```sqlexp(numeric_expression)```@@ -256,7 +256,7 @@ exp(numeric_expression)Factorial. Returns 1 if value is less than 2.-```+```sqlfactorial(numeric_expression)```@@ -268,7 +268,7 @@ factorial(numeric_expression)Returns the nearest integer less than or equal to a number.-```+```sqlfloor(numeric_expression)```@@ -280,7 +280,7 @@ floor(numeric_expression)Returns the greatest common divisor of `expression_x` and `expression_y`. Returns 0 if both inputs are zero.-```+```sqlgcd(expression_x, expression_y)```@@ -293,7 +293,7 @@ gcd(expression_x, expression_y)Returns true if a given number is +NaN or -NaN otherwise returns false.-```+```sqlisnan(numeric_expression)```@@ -305,7 +305,7 @@ isnan(numeric_expression)Returns true if a given number is +0.0 or -0.0 otherwise returns false.-```+```sqliszero(numeric_expression)```@@ -317,7 +317,7 @@ iszero(numeric_expression)Returns the least common multiple of `expression_x` and `expression_y`. Returns 0 if either input is zero.-```+```sqllcm(expression_x, expression_y)```@@ -330,7 +330,7 @@ lcm(expression_x, expression_y)Returns the natural logarithm of a number.-```+```sqlln(numeric_expression)```@@ -342,7 +342,7 @@ ln(numeric_expression)Returns the base-x logarithm of a number. Can either provide a specified base, or if omitted then takes the base-10 of a number.-```+```sqllog(base, numeric_expression)log(numeric_expression)```@@ -356,7 +356,7 @@ log(numeric_expression)Returns the base-10 logarithm of a number.-```+```sqllog10(numeric_expression)```@@ -368,7 +368,7 @@ log10(numeric_expression)Returns the base-2 logarithm of a number.-```+```sqllog2(numeric_expression)```@@ -381,7 +381,7 @@ log2(numeric_expression)Returns the first argument if it's not _NaN_.Returns the second argument otherwise.-```+```sqlnanvl(expression_x, expression_y)```@@ -394,7 +394,7 @@ nanvl(expression_x, expression_y)Returns an approximate value of π.-```+```sqlpi()```@@ -406,7 +406,7 @@ _Alias of [power](#power)._Returns a base expression raised to the power of an exponent.-```+```sqlpower(base, exponent)```@@ -423,7 +423,7 @@ power(base, exponent)Converts degrees to radians.-```+```sqlradians(numeric_expression)```@@ -436,7 +436,7 @@ radians(numeric_expression)Returns a random float value in the range [0, 1).The random seed is unique to each row.-```+```sqlrandom()```@@ -444,7 +444,7 @@ random()Rounds a number to the nearest integer.-```+```sqlround(numeric_expression[, decimal_places])```@@ -459,7 +459,7 @@ Returns the sign of a number.Negative numbers return `-1`.Zero and positive numbers return `1`.-```+```sqlsignum(numeric_expression)```@@ -471,7 +471,7 @@ signum(numeric_expression)Returns the sine of a number.-```+```sqlsin(numeric_expression)```@@ -483,7 +483,7 @@ sin(numeric_expression)Returns the hyperbolic sine of a number.-```+```sqlsinh(numeric_expression)```@@ -495,7 +495,7 @@ sinh(numeric_expression)Returns the square root of a number.-```+```sqlsqrt(numeric_expression)```@@ -507,7 +507,7 @@ sqrt(numeric_expression)Returns the tangent of a number.-```+```sqltan(numeric_expression)```@@ -519,7 +519,7 @@ tan(numeric_expression)Returns the hyperbolic tangent of a number.-```+```sqltanh(numeric_expression)```@@ -531,7 +531,7 @@ tanh(numeric_expression)Truncates a number to a whole number or truncated to the specified decimal places.-```+```sqltrunc(numeric_expression[, decimal_places])```@@ -558,7 +558,7 @@ trunc(numeric_expression[, decimal_places])Returns the first of its arguments that is not _null_. Returns _null_ if all arguments are _null_. This function is often used to substitute a default value for _null_ values.-```+```sqlcoalesce(expression1[, ..., expression_n])```@@ -581,7 +581,7 @@ coalesce(expression1[, ..., expression_n])Returns the greatest value in a list of expressions. Returns _null_ if all expressions are _null_.-```+```sqlgreatest(expression1[, ..., expression_n])```@@ -608,7 +608,7 @@ _Alias of [nvl](#nvl)._Returns the smallest value in a list of expressions. Returns _null_ if all expressions are _null_.-```+```sqlleast(expression1[, ..., expression_n])```@@ -632,7 +632,7 @@ least(expression1[, ..., expression_n])Returns _null_ if _expression1_ equals _expression2_; otherwise it returns _expression1_.This can be used to perform the inverse operation of [`coalesce`](#coalesce).-```+```sqlnullif(expression1, expression2)```@@ -662,7 +662,7 @@ nullif(expression1, expression2)Returns _expression2_ if _expression1_ is NULL otherwise it returns _expression1_.-```+```sqlnvl(expression1, expression2)```@@ -696,7 +696,7 @@ nvl(expression1, expression2)Returns _expression2_ if _expression1_ is not NULL; otherwise it returns _expression3_.-```+```sqlnvl2(expression1, expression2, expression3)```@@ -769,7 +769,7 @@ nvl2(expression1, expression2, expression3)Returns the Unicode character code of the first character in a string.-```+```sqlascii(str)```@@ -802,7 +802,7 @@ ascii(str)Returns the bit length of a string.-```+```sqlbit_length(str)```@@ -830,7 +830,7 @@ bit_length(str)Trims the specified trim string from the start and end of a string. If no trim string is provided, all whitespace is removed from the start and end of the input string.-```+```sqlbtrim(str[, trim_str])```@@ -877,7 +877,7 @@ _Alias of [character_length](#character_length)._Returns the number of characters in a string.-```+```sqlcharacter_length(str)```@@ -910,7 +910,7 @@ character_length(str)Returns the character with the specified ASCII or Unicode code value.-```+```sqlchr(expression)```@@ -937,7 +937,7 @@ chr(expression)Concatenates multiple strings together.-```+```sqlconcat(str[, ..., str_n])```@@ -965,7 +965,7 @@ concat(str[, ..., str_n])Concatenates multiple strings together with a specified separator.-```+```sqlconcat_ws(separator, str[, ..., str_n])```@@ -994,7 +994,7 @@ concat_ws(separator, str[, ..., str_n])Return true if search_str is found within string (case-sensitive).-```+```sqlcontains(str, search_str)```@@ -1018,7 +1018,7 @@ contains(str, search_str)Tests if a string ends with a substring.-```+```sqlends_with(str, substr)```@@ -1048,7 +1048,7 @@ ends_with(str, substr)Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings.-```+```sqlfind_in_set(str, strlist)```@@ -1072,7 +1072,7 @@ find_in_set(str, strlist)Capitalizes the first character in each word in the input string. Words are delimited by non-alphanumeric characters.-```+```sqlinitcap(str)```@@ -1104,7 +1104,7 @@ _Alias of [strpos](#strpos)._Returns a specified number of characters from the left side of a string.-```+```sqlleft(str, n)```@@ -1136,7 +1136,7 @@ _Alias of [character_length](#character_length)._Returns the [`Levenshtein distance`](https://en.wikipedia.org/wiki/Levenshtein_distance) between the two given strings.-```+```sqllevenshtein(str1, str2)```@@ -1160,7 +1160,7 @@ levenshtein(str1, str2)Converts a string to lower-case.-```+```sqllower(str)```@@ -1188,7 +1188,7 @@ lower(str)Pads the left side of a string with another string to a specified string length.-```+```sqllpad(str, n[, padding_str])```@@ -1217,7 +1217,7 @@ lpad(str, n[, padding_str])Trims the specified trim string from the beginning of a string. If no trim string is provided, all whitespace is removed from the start of the input string.-```+```sqlltrim(str[, trim_str])```@@ -1258,7 +1258,7 @@ trim(LEADING trim_str FROM str)Returns the length of a string in bytes.-```+```sqloctet_length(str)```@@ -1290,7 +1290,7 @@ _Alias of [strpos](#strpos)._Returns a string with an input string repeated a specified number.-```+```sqlrepeat(str, n)```@@ -1314,7 +1314,7 @@ repeat(str, n)Replaces all occurrences of a specified substring in a string with a new substring.-```+```sqlreplace(str, substr, replacement)```@@ -1339,7 +1339,7 @@ replace(str, substr, replacement)Reverses the character order of a string.-```+```sqlreverse(str)```@@ -1362,7 +1362,7 @@ reverse(str)Returns a specified number of characters from the right side of a string.-```+```sqlright(str, n)```@@ -1390,7 +1390,7 @@ right(str, n)Pads the right side of a string with another string to a specified string length.-```+```sqlrpad(str, n[, padding_str])```@@ -1419,7 +1419,7 @@ rpad(str, n[, padding_str])Trims the specified trim string from the end of a string. If no trim string is provided, all whitespace is removed from the end of the input string.-```+```sqlrtrim(str[, trim_str])```@@ -1460,7 +1460,7 @@ trim(TRAILING trim_str FROM str)Splits a string based on a specified delimiter and returns the substring in the specified position.-```+```sqlsplit_part(str, delimiter, pos)```@@ -1485,7 +1485,7 @@ split_part(str, delimiter, pos)Tests if a string starts with a substring.-```+```sqlstarts_with(str, substr)```@@ -1509,7 +1509,7 @@ starts_with(str, substr)Returns the starting position of a specified substring in a string. Positions begin at 1. If the substring does not exist in the string, the function returns 0.-```+```sqlstrpos(str, substr)```@@ -1544,7 +1544,7 @@ position(substr in origstr)Extracts a substring of a specified number of characters from a specific starting position in a string.-```+```sqlsubstr(str, start_pos[, length])```@@ -1581,7 +1581,7 @@ Returns the substring from str before count occurrences of the delimiter delim.If count is positive, everything to the left of the final delimiter (counting from the left) is returned.If count is negative, everything to the right of the final delimiter (counting from the right) is returned.-```+```sqlsubstr_index(str, delim, count)```@@ -1624,7 +1624,7 @@ _Alias of [substr_index](#substr_index)._Converts an integer to a hexadecimal string.-```+```sqlto_hex(int)```@@ -1647,7 +1647,7 @@ to_hex(int)Translates characters in a string to specified translation characters.-```+```sqltranslate(str, chars, translation)```@@ -1676,7 +1676,7 @@ _Alias of [btrim](#btrim)._Converts a string to upper-case.-```+```sqlupper(str)```@@ -1704,7 +1704,7 @@ upper(str)Returns [`UUID v4`](<https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_(random)>) string value which is unique per row.-```+```sqluuid()```@@ -1728,7 +1728,7 @@ uuid()Decode binary data from textual representation in string.-```+```sqldecode(expression, format)```@@ -1745,7 +1745,7 @@ decode(expression, format)Encode binary data into a textual representation.-```+```sqlencode(expression, format)```@@ -1774,7 +1774,7 @@ The following regular expression functions are supported:Returns the number of matches that a [regular expression](https://docs.rs/regex/latest/regex/#syntax) has in a string.-```+```sqlregexp_count(str, regexp[, start, flags])```@@ -1805,7 +1805,7 @@ regexp_count(str, regexp[, start, flags])Returns true if a [regular expression](https://docs.rs/regex/latest/regex/#syntax) has at least one match in a string, false otherwise.-```+```sqlregexp_like(str, regexp[, flags])```@@ -1843,7 +1843,7 @@ Additional examples can be found [here](https://github.com/apache/datafusion/bloReturns the first [regular expression](https://docs.rs/regex/latest/regex/#syntax) matches in a string.-```+```sqlregexp_match(str, regexp[, flags])```@@ -1882,7 +1882,7 @@ Additional examples can be found [here](https://github.com/apache/datafusion/bloReplaces substrings in a string that match a [regular expression](https://docs.rs/regex/latest/regex/#syntax).-```+```sqlregexp_replace(str, regexp, replacement[, flags])```@@ -1950,7 +1950,7 @@ Returns the current UTC date.The `current_date()` return value is determined at query time and will return the same date, no matter when in the query plan the function executes.-```+```sqlcurrent_date()```@@ -1964,7 +1964,7 @@ Returns the current UTC time.The `current_time()` return value is determined at query time and will return the same time, no matter when in the query plan the function executes.-```+```sqlcurrent_time()```@@ -1978,7 +1978,7 @@ Calculates time intervals and returns the start of the interval nearest to the sFor example, if you "bin" or "window" data into 15 minute intervals, an input timestamp of `2023-01-01T18:18:18Z` will be updated to the start time of the 15 minute bin it is in: `2023-01-01T18:15:00Z`.-```+```sqldate_bin(interval, expression, origin-timestamp)```@@ -2034,7 +2034,7 @@ _Alias of [to_char](#to_char)._Returns the specified part of the date as an integer.-```+```sqldate_part(part, expression)```@@ -2073,7 +2073,7 @@ extract(field FROM source)Truncates a timestamp value to a specified precision.-```+```sqldate_trunc(precision, expression)```@@ -2108,7 +2108,7 @@ _Alias of [date_trunc](#date_trunc)._Converts an integer to RFC3339 timestamp format (`YYYY-MM-DDT00:00:00.000000000Z`). Integers and unsigned integers are interpreted as nanoseconds since the unix epoch (`1970-01-01T00:00:00Z`) return the corresponding timestamp.-```+```sqlfrom_unixtime(expression[, timezone])```@@ -2132,7 +2132,7 @@ from_unixtime(expression[, timezone])Make a date from year/month/day component parts.-```+```sqlmake_date(year, month, day)```@@ -2167,7 +2167,7 @@ Returns the current UTC timestamp.The `now()` return value is determined at query time and will return the same timestamp, no matter when in the query plan the function executes.-```+```sqlnow()```@@ -2179,7 +2179,7 @@ now()Returns a string representation of a date, time, timestamp or duration based on a [Chrono format](https://docs.rs/chrono/latest/chrono/format/strftime/index.html). Unlike the PostgreSQL equivalent of this function numerical formatting is not supported.-```+```sqlto_char(expression, format)```@@ -2216,7 +2216,7 @@ Returns the corresponding date.Note: `to_date` returns Date32, which represents its values as the number of days since unix epoch(`1970-01-01`) stored as signed 32 bit value. The largest supported date value is `9999-12-31`.-```+```sqlto_date('2017-05-31', '%Y-%m-%d')```@@ -2250,7 +2250,7 @@ Additional examples can be found [here](https://github.com/apache/datafusion/bloConverts a timestamp with a timezone to a timestamp without a timezone (with no offset or timezone information). This function handles daylight saving time changes.-```+```sqlto_local_time(expression)```@@ -2313,7 +2313,7 @@ Converts a value to a timestamp (`YYYY-MM-DDT00:00:00Z`). Supports strings, inteNote: `to_timestamp` returns `Timestamp(Nanosecond)`. The supported range for integer input is between `-9223372037` and `9223372036`. Supported range for string input is between `1677-09-21T00:12:44.0` and `2262-04-11T23:47:16.0`. Please use `to_timestamp_seconds` for the input outside of supported bounds.-```+```sqlto_timestamp(expression[, ..., format_n])```@@ -2345,7 +2345,7 @@ Additional examples can be found [here](https://github.com/apache/datafusion/bloConverts a value to a timestamp (`YYYY-MM-DDT00:00:00.000000Z`). Supports strings, integer, and unsigned integer types as input. Strings are parsed as RFC3339 (e.g. '2023-07-20T05:44:00') if no [Chrono format](https://docs.rs/chrono/latest/chrono/format/strftime/index.html)s are provided. Integers and unsigned integers are interpreted as microseconds since the unix epoch (`1970-01-01T00:00:00Z`) Returns the corresponding timestamp.-```+```sqlto_timestamp_micros(expression[, ..., format_n])```@@ -2377,7 +2377,7 @@ Additional examples can be found [here](https://github.com/apache/datafusion/bloConverts a value to a timestamp (`YYYY-MM-DDT00:00:00.000Z`). Supports strings, integer, and unsigned integer types as input. Strings are parsed as RFC3339 (e.g. '2023-07-20T05:44:00') if no [Chrono formats](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) are provided. Integers and unsigned integers are interpreted as milliseconds since the unix epoch (`1970-01-01T00:00:00Z`). Returns the corresponding timestamp.-```+```sqlto_timestamp_millis(expression[, ..., format_n])```@@ -2409,7 +2409,7 @@ Additional examples can be found [here](https://github.com/apache/datafusion/bloConverts a value to a timestamp (`YYYY-MM-DDT00:00:00.000000000Z`). Supports strings, integer, and unsigned integer types as input. Strings are parsed as RFC3339 (e.g. '2023-07-20T05:44:00') if no [Chrono format](https://docs.rs/chrono/latest/chrono/format/strftime/index.html)s are provided. Integers and unsigned integers are interpreted as nanoseconds since the unix epoch (`1970-01-01T00:00:00Z`). Returns the corresponding timestamp.-```+```sqlto_timestamp_nanos(expression[, ..., format_n])```@@ -2441,7 +2441,7 @@ Additional examples can be found [here](https://github.com/apache/datafusion/bloConverts a value to a timestamp (`YYYY-MM-DDT00:00:00.000Z`). Supports strings, integer, and unsigned integer types as input. Strings are parsed as RFC3339 (e.g. '2023-07-20T05:44:00') if no [Chrono format](https://docs.rs/chrono/latest/chrono/format/strftime/index.html)s are provided. Integers and unsigned integers are interpreted as seconds since the unix epoch (`1970-01-01T00:00:00Z`). Returns the corresponding timestamp.-```+```sqlto_timestamp_seconds(expression[, ..., format_n])```@@ -2473,7 +2473,7 @@ Additional examples can be found [here](https://github.com/apache/datafusion/bloConverts a value to seconds since the unix epoch (`1970-01-01T00:00:00Z`). Supports strings, dates, timestamps and double types as input. Strings are parsed as RFC3339 (e.g. '2023-07-20T05:44:00') if no [Chrono formats](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) are provided.-```+```sqlto_unixtime(expression[, ..., format_n])```@@ -2600,7 +2600,7 @@ _Alias of [current_date](#current_date)._Returns the first non-null element in the array.-```+```sqlarray_any_value(array)```@@ -2627,7 +2627,7 @@ array_any_value(array)Appends an element to the end of an array.-```+```sqlarray_append(array, element)```@@ -2661,7 +2661,7 @@ _Alias of [array_concat](#array_concat)._Concatenates arrays.-```+```sqlarray_concat(array[, ..., array_n])```@@ -2695,7 +2695,7 @@ _Alias of [array_has](#array_has)._Returns an array of the array's dimensions.-```+```sqlarray_dims(array)```@@ -2722,7 +2722,7 @@ array_dims(array)Returns the Euclidean distance between two input arrays of equal length.-```+```sqlarray_distance(array1, array2)```@@ -2750,7 +2750,7 @@ array_distance(array1, array2)Returns distinct values from the array after removing duplicates.-```+```sqlarray_distinct(array)```@@ -2777,7 +2777,7 @@ array_distinct(array)Extracts the element with the index n from the array.-```+```sqlarray_element(array, index)```@@ -2811,7 +2811,7 @@ _Alias of [empty](#empty)._Returns an array of the elements that appear in the first array but not in the second.-```+```sqlarray_except(array1, array2)```@@ -2849,7 +2849,7 @@ _Alias of [array_element](#array_element)._Returns true if the array contains the element.-```+```sqlarray_has(array, element)```@@ -2879,7 +2879,7 @@ array_has(array, element)Returns true if all elements of sub-array exist in array.-```+```sqlarray_has_all(array, sub-array)```@@ -2907,7 +2907,7 @@ array_has_all(array, sub-array)Returns true if any elements exist in both arrays.-```+```sqlarray_has_any(array, sub-array)```@@ -2940,7 +2940,7 @@ _Alias of [array_position](#array_position)._Returns an array of elements in the intersection of array1 and array2.-```+```sqlarray_intersect(array1, array2)```@@ -2978,7 +2978,7 @@ _Alias of [array_to_string](#array_to_string)._Returns the length of the array dimension.-```+```sqlarray_length(array, dimension)```@@ -3006,7 +3006,7 @@ array_length(array, dimension)Returns the number of dimensions of the array.-```+```sqlarray_ndims(array, element)```@@ -3034,7 +3034,7 @@ array_ndims(array, element)Returns the array without the last element.-```+```sqlarray_pop_back(array)```@@ -3061,7 +3061,7 @@ array_pop_back(array)Returns the array without the first element.-```+```sqlarray_pop_front(array)```@@ -3088,7 +3088,7 @@ array_pop_front(array)Returns the position of the first occurrence of the specified element in the array.-```+```sqlarray_position(array, element)array_position(array, element, index)```@@ -3126,7 +3126,7 @@ array_position(array, element, index)Searches for an element in the array, returns all occurrences.-```+```sqlarray_positions(array, element)```@@ -3154,7 +3154,7 @@ array_positions(array, element)Prepends an element to the beginning of an array.-```+```sqlarray_prepend(element, array)```@@ -3192,7 +3192,7 @@ _Alias of [array_prepend](#array_prepend)._Removes the first element from the array equal to the given value.-```+```sqlarray_remove(array, element)```@@ -3220,7 +3220,7 @@ array_remove(array, element)Removes all elements from the array equal to the given value.-```+```sqlarray_remove_all(array, element)```@@ -3248,7 +3248,7 @@ array_remove_all(array, element)Removes the first `max` elements from the array equal to the given value.-```+```sqlarray_remove_n(array, element, max))```@@ -3277,7 +3277,7 @@ array_remove_n(array, element, max))Returns an array containing element `count` times.-```+```sqlarray_repeat(element, count)```@@ -3311,7 +3311,7 @@ array_repeat(element, count)Replaces the first occurrence of the specified element with another specified element.-```+```sqlarray_replace(array, from, to)```@@ -3340,7 +3340,7 @@ array_replace(array, from, to)Replaces all occurrences of the specified element with another specified element.-```+```sqlarray_replace_all(array, from, to)```@@ -3369,7 +3369,7 @@ array_replace_all(array, from, to)Replaces the first `max` occurrences of the specified element with another specified element.-```+```sqlarray_replace_n(array, from, to, max)```@@ -3399,7 +3399,7 @@ array_replace_n(array, from, to, max)Resizes the list to contain size elements. Initializes new elements with value or empty if value is not set.-```+```sqlarray_resize(array, size, value)```@@ -3428,7 +3428,7 @@ array_resize(array, size, value)Returns the array with the order of the elements reversed.-```+```sqlarray_reverse(array)```@@ -3455,7 +3455,7 @@ array_reverse(array)Returns a slice of the array based on 1-indexed start and end positions.-```+```sqlarray_slice(array, begin, end)```@@ -3485,7 +3485,7 @@ array_slice(array, begin, end)Sort array.-```+```sqlarray_sort(array, desc, nulls_first)```@@ -3514,7 +3514,7 @@ array_sort(array, desc, nulls_first)Converts each element to its text representation.-```+```sqlarray_to_string(array, delimiter[, null_string])```@@ -3545,7 +3545,7 @@ array_to_string(array, delimiter[, null_string])Returns an array of elements that are present in both arrays (all elements from both arrays) with out duplicates.-```+```sqlarray_union(array1, array2)```@@ -3583,7 +3583,7 @@ _Alias of [array_has_any](#array_has_any)._Returns the total number of elements in the array.-```+```sqlcardinality(array)```@@ -3606,7 +3606,7 @@ cardinality(array)Returns 1 for an empty array or 0 for a non-empty array.-```+```sqlempty(array)```@@ -3639,7 +3639,7 @@ Converts an array of arrays to a flat array.The flattened array contains all the elements from all source arrays.-```+```sqlflatten(array)```@@ -3662,7 +3662,7 @@ flatten(array)Similar to the range function, but it includes the upper bound.-```+```sqlgenerate_series(start, stop, step)```@@ -3847,7 +3847,7 @@ _Alias of [array_union](#array_union)._Returns an array using the specified input expressions.-```+```sqlmake_array(expression1[, ..., expression_n])```@@ -3878,7 +3878,7 @@ _Alias of [make_array](#make_array)._Returns an Arrow array between start and stop with step. The range start..end contains all values with start <= x < end. It is empty if start >= end. Step cannot be 0.-```+```sqlrange(start, stop, step)```@@ -3910,7 +3910,7 @@ range(start, stop, step)Splits a string into an array of substrings based on a delimiter. Any substrings matching the optional `null_str` argument are replaced with NULL.-```+```sqlstring_to_array(str, delimiter[, null_str])```@@ -3955,7 +3955,7 @@ _Alias of [string_to_array](#string_to_array)._Returns an Arrow struct using the specified name and input expressions pairs.-```+```sqlnamed_struct(expression1_name, expression1_input[, ..., expression_n_name, expression_n_input])```@@ -3996,7 +3996,7 @@ Returns an Arrow struct using the specified input expressions optionally named.Fields in the returned struct use the optional name or the `cN` naming convention.For example: `c0`, `c1`, `c2`, etc.-```+```sqlstruct(expression1[, ..., expression_n])```@@ -4059,7 +4059,7 @@ Returns an Arrow map with the specified key-value pairs.The `make_map` function creates a map from two lists: one for keys and one for values. Each key must be unique and non-null.-```+```sqlmap(key, value)map(key: value)make_map(['key1', 'key2'], ['value1', 'value2'])@@ -4106,7 +4106,7 @@ SELECT MAKE_MAP(['key1', 'key2'], ['value1', null]);Returns a list containing the value for the given key or an empty list if the key is not present in the map.-```+```sqlmap_extract(map, key)```@@ -4139,7 +4139,7 @@ SELECT map_extract(MAP {'x': 10, 'y': NULL, 'z': 30}, 'y');Returns a list of all keys in the map.-```+```sqlmap_keys(map)```@@ -4163,7 +4163,7 @@ SELECT map_keys(map([100, 5], [42, 43]));Returns a list of all values in the map.-```+```sqlmap_values(map)```@@ -4196,7 +4196,7 @@ SELECT map_values(map([100, 5], [42, 43]));Computes the binary hash of an expression using the specified algorithm.-```+```sqldigest(expression, algorithm)```@@ -4228,7 +4228,7 @@ digest(expression, algorithm)Computes an MD5 128-bit checksum for a string expression.-```+```sqlmd5(expression)```@@ -4251,7 +4251,7 @@ md5(expression)Computes the SHA-224 hash of a binary string.-```+```sqlsha224(expression)```@@ -4274,7 +4274,7 @@ sha224(expression)Computes the SHA-256 hash of a binary string.-```+```sqlsha256(expression)```@@ -4297,7 +4297,7 @@ sha256(expression)Computes the SHA-384 hash of a binary string.-```+```sqlsha384(expression)```@@ -4320,7 +4320,7 @@ sha384(expression)Computes the SHA-512 hash of a binary string.-```+```sqlsha512(expression)```@@ -4350,7 +4350,7 @@ sha512(expression)Casts a value to a specific Arrow data type.-```+```sqlarrow_cast(expression, datatype)```@@ -4378,7 +4378,7 @@ arrow_cast(expression, datatype)Returns the name of the underlying [Arrow data type](https://docs.rs/arrow/latest/arrow/datatypes/enum.DataType.html) of the expression.-```+```sqlarrow_typeof(expression)```@@ -4404,7 +4404,7 @@ Note: most users invoke `get_field` indirectly via field accesssyntax such as `my_struct_col['field_name']` which results in a call to`get_field(my_struct_col, 'field_name')`.-```+```sqlget_field(expression1, expression2)```@@ -4444,7 +4444,7 @@ get_field(expression1, expression2)Returns the version of DataFusion.-```+```sqlversion()```diff --git a/docs/source/user-guide/sql/window_functions.md b/docs/source/user-guide/sql/window_functions.mdindex a68fdbda67..1c02804f0d 100644--- a/docs/source/user-guide/sql/window_functions.md+++ b/docs/source/user-guide/sql/window_functions.md@@ -115,7 +115,7 @@ WINDOW w AS (PARTITION BY depname ORDER BY salary DESC);The syntax for the OVER-clause is-```+```sqlfunction([expr])OVER([PARTITION BY expr[, …]]@@ -126,7 +126,7 @@ function([expr])where **frame_clause** is one of:-```+```sql{ RANGE | ROWS | GROUPS } frame_start{ RANGE | ROWS | GROUPS } BETWEEN frame_start AND frame_end```@@ -162,7 +162,7 @@ All [aggregate functions](aggregate_functions.md) can be used as window functionRelative rank of the current row: (number of rows preceding or peer with current row) / (total rows).-```+```sqlcume_dist()```@@ -170,7 +170,7 @@ cume_dist()Returns the rank of the current row without gaps. This function ranks rows in a dense manner, meaning consecutive ranks are assigned even for identical values.-```+```sqldense_rank()```@@ -178,7 +178,7 @@ dense_rank()Integer ranging from 1 to the argument value, dividing the partition as equally as possible-```+```sqlntile(expression)```@@ -190,7 +190,7 @@ ntile(expression)Returns the percentage rank of the current row within its partition. The value ranges from 0 to 1 and is computed as `(rank - 1) / (total_rows - 1)`.-```+```sqlpercent_rank()```@@ -198,7 +198,7 @@ percent_rank()Returns the rank of the current row within its partition, allowing gaps between ranks. This function provides a ranking similar to `row_number`, but skips ranks for identical values.-```+```sqlrank()```@@ -206,7 +206,7 @@ rank()Number of the current row within its partition, counting from 1.-```+```sqlrow_number()```@@ -222,7 +222,7 @@ row_number()Returns value evaluated at the row that is the first row of the window frame.-```+```sqlfirst_value(expression)```@@ -234,7 +234,7 @@ first_value(expression)Returns value evaluated at the row that is offset rows before the current row within the partition; if there is no such row, instead return default (which must be of the same type as value).-```+```sqllag(expression, offset, default)```@@ -248,7 +248,7 @@ lag(expression, offset, default)Returns value evaluated at the row that is the last row of the window frame.-```+```sqllast_value(expression)```@@ -260,7 +260,7 @@ last_value(expression)Returns value evaluated at the row that is offset rows after the current row within the partition; if there is no such row, instead return default (which must be of the same type as value).-```+```sqllead(expression, offset, default)```@@ -274,7 +274,7 @@ lead(expression, offset, default)Returns value evaluated at the row that is the nth row of the window frame (counting from 1); null if no such row.-```+```sqlnth_value(expression, n)```