Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
16 changes: 16 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ members = [
"datafusion/ffi",
"datafusion/functions",
"datafusion/functions-aggregate",
"datafusion/functions-json",
"datafusion/functions-aggregate-common",
"datafusion/functions-table",
"datafusion/functions-nested",
Expand Down Expand Up @@ -135,6 +136,7 @@ datafusion-ffi = { path = "datafusion/ffi", version = "53.0.0" }
datafusion-functions = { path = "datafusion/functions", version = "53.0.0" }
datafusion-functions-aggregate = { path = "datafusion/functions-aggregate", version = "53.0.0" }
datafusion-functions-aggregate-common = { path = "datafusion/functions-aggregate-common", version = "53.0.0" }
datafusion-functions-json = { path = "datafusion/functions-json", version = "53.0.0" }
datafusion-functions-nested = { path = "datafusion/functions-nested", version = "53.0.0", default-features = false }
datafusion-functions-table = { path = "datafusion/functions-table", version = "53.0.0" }
datafusion-functions-window = { path = "datafusion/functions-window", version = "53.0.0" }
Expand Down
2 changes: 2 additions & 0 deletions datafusion/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ all-features = true
workspace = true

[features]
json_expressions = ["datafusion-functions-json"]
nested_expressions = ["datafusion-functions-nested"]
# This feature is deprecated. Use the `nested_expressions` feature instead.
array_expressions = ["nested_expressions"]
Expand Down Expand Up @@ -131,6 +132,7 @@ datafusion-expr = { workspace = true, default-features = false }
datafusion-expr-common = { workspace = true }
datafusion-functions = { workspace = true }
datafusion-functions-aggregate = { workspace = true }
datafusion-functions-json = { workspace = true, optional = true }
datafusion-functions-nested = { workspace = true, default-features = false, optional = true }
datafusion-functions-table = { workspace = true }
datafusion-functions-window = { workspace = true }
Expand Down
22 changes: 20 additions & 2 deletions datafusion/core/src/execution/session_state_defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ use crate::datasource::file_format::json::JsonFormatFactory;
use crate::datasource::file_format::parquet::ParquetFormatFactory;
use crate::datasource::provider::DefaultTableFactory;
use crate::execution::context::SessionState;
#[cfg(feature = "json_expressions")]
use crate::functions_json;
#[cfg(feature = "nested_expressions")]
use crate::functions_nested;
use crate::{functions, functions_aggregate, functions_table, functions_window};
Expand Down Expand Up @@ -104,12 +106,18 @@ impl SessionStateDefaults {

/// returns the list of default [`ScalarUDF`]s
pub fn default_scalar_functions() -> Vec<Arc<ScalarUDF>> {
#[cfg_attr(not(feature = "nested_expressions"), expect(unused_mut))]
#[cfg_attr(
not(any(feature = "nested_expressions", feature = "json_expressions")),
expect(unused_mut)
)]
let mut functions: Vec<Arc<ScalarUDF>> = functions::all_default_functions();

#[cfg(feature = "nested_expressions")]
functions.append(&mut functions_nested::all_default_nested_functions());

#[cfg(feature = "json_expressions")]
functions.append(&mut functions_json::all_default_json_functions());

functions
}

Expand Down Expand Up @@ -150,9 +158,10 @@ impl SessionStateDefaults {
file_formats
}

/// registers all builtin functions - scalar, array and aggregate
/// registers all builtin functions - scalar, array, json, and aggregate
pub fn register_builtin_functions(state: &mut SessionState) {
Self::register_scalar_functions(state);
Self::register_json_functions(state);
Self::register_array_functions(state);
Self::register_aggregate_functions(state);
}
Expand All @@ -162,6 +171,15 @@ impl SessionStateDefaults {
functions::register_all(state).expect("can not register built in functions");
}

/// registers all the builtin JSON functions
#[cfg_attr(not(feature = "json_expressions"), expect(unused_variables))]
pub fn register_json_functions(state: &mut SessionState) {
// register crate of JSON expressions (if enabled)
#[cfg(feature = "json_expressions")]
functions_json::register_all(state)
.expect("can not register JSON functions");
}

/// registers all the builtin array functions
#[cfg_attr(not(feature = "nested_expressions"), expect(unused_variables))]
pub fn register_array_functions(state: &mut SessionState) {
Expand Down
7 changes: 7 additions & 0 deletions datafusion/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,7 @@
//! * [datafusion_expr]: [`LogicalPlan`], [`Expr`] and related logical planning structure
//! * [datafusion_functions]: Scalar function packages
//! * [datafusion_functions_aggregate]: Aggregate functions such as `MIN`, `MAX`, `SUM`, etc
//! * [datafusion_functions_json]: JSON scalar functions such as `json_get_str`
//! * [datafusion_functions_nested]: Scalar function packages for `ARRAY`s, `MAP`s and `STRUCT`s
//! * [datafusion_functions_table]: Table Functions such as `GENERATE_SERIES`
//! * [datafusion_functions_window]: Window functions such as `ROW_NUMBER`, `RANK`, etc
Expand Down Expand Up @@ -865,6 +866,12 @@ pub mod functions {
pub use datafusion_functions::*;
}

/// re-export of [`datafusion_functions_json`] crate, if "json_expressions" feature is enabled
pub mod functions_json {
#[cfg(feature = "json_expressions")]
pub use datafusion_functions_json::*;
}

/// re-export of [`datafusion_functions_nested`] crate, if "nested_expressions" feature is enabled
pub mod functions_nested {
#[cfg(feature = "nested_expressions")]
Expand Down
58 changes: 58 additions & 0 deletions datafusion/functions-json/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# 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.

[package]
name = "datafusion-functions-json"
description = "JSON function package for the DataFusion query engine"
keywords = ["datafusion", "json", "functions"]
readme = "README.md"
version = { workspace = true }
edition = { workspace = true }
homepage = { workspace = true }
repository = { workspace = true }
license = { workspace = true }
authors = { workspace = true }
rust-version = { workspace = true }

[package.metadata.docs.rs]
all-features = true

# Note: add additional linter rules in lib.rs.
# Rust does not support workspace + new linter rules in subcrates yet
# https://github.com/rust-lang/cargo/issues/13157
[lints]
workspace = true

[lib]
name = "datafusion_functions_json"

[features]
default = []

[dependencies]
arrow = { workspace = true }
datafusion-common = { workspace = true }
datafusion-doc = { workspace = true }
datafusion-execution = { workspace = true }
datafusion-expr = { workspace = true, default-features = false }
datafusion-macros = { workspace = true }
log = { workspace = true }
serde_json = { workspace = true }

[dev-dependencies]
arrow = { workspace = true, features = ["test_utils"] }
tokio = { workspace = true, features = ["macros", "rt"] }
43 changes: 43 additions & 0 deletions datafusion/functions-json/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!---
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.
-->

# datafusion-functions-json

JSON scalar functions for [DataFusion](https://datafusion.apache.org/).

This crate provides JSON manipulation functions operating on JSON-encoded strings.
Based on the [datafusion-functions-json](https://github.com/datafusion-contrib/datafusion-functions-json)
community crate.

## Functions

| Function | Description |
|----------|-------------|
| `json_get_str(json, key1, ...)` | Extract a string value from a JSON string at the given path |

## Usage

These functions are registered automatically when the `json_expressions` feature
is enabled on the `datafusion` crate. They can also be registered manually:

```rust
use datafusion_functions_json;
// registry is a FunctionRegistry, e.g. SessionState
datafusion_functions_json::register_all(&mut registry)?;
```
Loading