Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Framework for generating function docs from embedded code documentation #12668

Merged
merged 16 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,21 @@ jobs:
# If you encounter an error, run './dev/update_config_docs.sh' and commit
./dev/update_config_docs.sh
git diff --exit-code
- name: Check if aggregate_functions.md has been modified
run: |
alamb marked this conversation as resolved.
Show resolved Hide resolved
# If you encounter an error, run './dev/update_aggregate_docs.sh' and commit
./dev/update_aggregate_docs.sh
git diff --exit-code
- name: Check if scalar_functions.md has been modified
run: |
# If you encounter an error, run './dev/update_scalar_docs.sh' and commit
./dev/update_scalar_docs.sh
git diff --exit-code
- name: Check if window_functions.md has been modified
run: |
# If you encounter an error, run './dev/update_window_docs.sh' and commit
./dev/update_window_docs.sh
git diff --exit-code

# Verify MSRV for the crates which are directly used by other projects:
# - datafusion
Expand Down
1 change: 1 addition & 0 deletions datafusion-cli/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

152 changes: 152 additions & 0 deletions datafusion/core/src/bin/print_aggregate_functions_docs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

use datafusion::execution::SessionStateDefaults;
use datafusion_expr::aggregate_doc_sections::doc_sections;
use datafusion_expr::AggregateUDF;
use itertools::Itertools;
use std::fmt::Write as _;
use std::sync::Arc;

fn main() {
let functions = SessionStateDefaults::default_aggregate_functions();
let mut docs = "".to_string();

// doc sections only includes sections that have 'include' == true
for doc_section in doc_sections() {
// make sure there is a function that is in this doc section
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we could change the signature of documentation() to return Option<&Documentation> so that returning None could signal that there was no documentation for the specified function

That would also make it easy to print out the list of functions that don't have documentation (and thus need to be ported here): "INFO: function 'cos' does not have any documentation"

Once the porting is complete, we could change the info to an error which would ensure that all newly added functions were also documented 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Signature updated.

if !functions
.iter()
.any(|f| f.documentation().doc_section == doc_section)
{
continue;
}

// write out section header
let _ = writeln!(&mut docs, "## {} ", doc_section.label);

if let Some(description) = doc_section.description {
let _ = writeln!(&mut docs, "{description}");
}

let filtered = functions
.clone()
.into_iter()
.filter(|f| f.documentation().doc_section == doc_section)
.collect_vec();

// names is a sorted list of function names and aliases since we display
// both in the documentation
let names = get_names_and_aliases(&filtered);

// write out the list of function names and aliases
names.iter().for_each(|name| {
let _ = writeln!(&mut docs, "- [{name}](#{name})");
});

// write out each function and alias in the order of the sorted name list
for name in names {
let f = filtered
.iter()
.find(|f| f.name() == name || f.aliases().contains(&name))
.unwrap();
let documentation = f.documentation();

// if this name is an alias we need to display what it's an alias of
if f.aliases().contains(&name) {
let _ = write!(&mut docs, "_Alias of [{name}](#{name})._");
continue;
}

// otherwise display the documentation for the function

// first, the name, description and syntax example
let _ = write!(
&mut docs,
r#"
### `{}`

{}

```
{}
```
"#,
f.name(),
documentation.description,
documentation.syntax_example
);

// next, arguments
if let Some(args) = documentation.arguments {
let _ = writeln!(&mut docs, "#### Arguments\n");
for (arg_name, arg_desc) in args {
let _ = writeln!(&mut docs, "- **{arg_name}**: {arg_desc}");
}
}

// next, sql example if provided
if let Some(example) = documentation.sql_example {
let _ = writeln!(
&mut docs,
r#"
#### Example

{}
"#,
example
);
}

// next, aliases
if !f.aliases().is_empty() {
let _ = write!(&mut docs, "#### Aliases");

for alias in f.aliases() {
let _ = writeln!(&mut docs, "- {alias}");
}
}

// finally, any related udfs
if let Some(related_udfs) = documentation.related_udfs {
let _ = writeln!(&mut docs, "\n**Related functions**:");

for related in related_udfs {
let _ = writeln!(&mut docs, "- [{related}](#{related})");
}
}
}
}

println!("{docs}");
}

fn get_names_and_aliases(functions: &[Arc<AggregateUDF>]) -> Vec<String> {
functions
.iter()
.flat_map(|f| {
if f.aliases().is_empty() {
vec![f.name().to_string()]
} else {
let mut names = vec![f.name().to_string()];
names.extend(f.aliases().iter().cloned());
names
}
})
.sorted()
.collect_vec()
}
152 changes: 152 additions & 0 deletions datafusion/core/src/bin/print_scalar_functions_docs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

use datafusion::execution::SessionStateDefaults;
use datafusion_expr::scalar_doc_sections::doc_sections;
use datafusion_expr::ScalarUDF;
use itertools::Itertools;
use std::fmt::Write as _;
use std::sync::Arc;

fn main() {
let functions = SessionStateDefaults::default_scalar_functions();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

let mut docs = "".to_string();

// doc sections only includes sections that have 'include' == true
for doc_section in doc_sections() {
// make sure there is a function that is in this doc section
if !functions
.iter()
.any(|f| f.documentation().doc_section == doc_section)
{
continue;
}

// write out section header
let _ = writeln!(&mut docs, "## {} ", doc_section.label);

if let Some(description) = doc_section.description {
let _ = writeln!(&mut docs, "{description}");
}

let filtered = functions
.clone()
.into_iter()
.filter(|f| f.documentation().doc_section == doc_section)
.collect_vec();

// names is a sorted list of function names and aliases since we display
// both in the documentation
let names = get_names_and_aliases(&filtered);

// write out the list of function names and aliases
names.iter().for_each(|name| {
let _ = writeln!(&mut docs, "- [{name}](#{name})");
});

// write out each function and alias in the order of the sorted name list
for name in names {
let f = filtered
.iter()
.find(|f| f.name() == name || f.aliases().contains(&name))
.unwrap();
let documentation = f.documentation();

// if this name is an alias we need to display what it's an alias of
if f.aliases().contains(&name) {
let _ = write!(&mut docs, "_Alias of [{name}](#{name})._");
continue;
}

// otherwise display the documentation for the function

// first, the name, description and syntax example
let _ = write!(
&mut docs,
r#"
### `{}`

{}

```
{}
```
"#,
f.name(),
documentation.description,
documentation.syntax_example
);

// next, arguments
if let Some(args) = documentation.arguments {
let _ = writeln!(&mut docs, "#### Arguments\n");
for (arg_name, arg_desc) in args {
let _ = writeln!(&mut docs, "- **{arg_name}**: {arg_desc}");
}
}

// next, sql example if provided
if let Some(example) = documentation.sql_example {
let _ = writeln!(
&mut docs,
r#"
#### Example

{}
"#,
example
);
}

// next, aliases
if !f.aliases().is_empty() {
let _ = write!(&mut docs, "#### Aliases");

for alias in f.aliases() {
let _ = writeln!(&mut docs, "- {alias}");
}
}

// finally, any related udfs
if let Some(related_udfs) = documentation.related_udfs {
let _ = writeln!(&mut docs, "\n**Related functions**:");

for related in related_udfs {
let _ = writeln!(&mut docs, "- [{related}](#{related})");
}
}
}
}

println!("{docs}");
}

fn get_names_and_aliases(functions: &[Arc<ScalarUDF>]) -> Vec<String> {
functions
.iter()
.flat_map(|f| {
if f.aliases().is_empty() {
vec![f.name().to_string()]
} else {
let mut names = vec![f.name().to_string()];
names.extend(f.aliases().iter().cloned());
names
}
})
.sorted()
.collect_vec()
}
Loading