-
Notifications
You must be signed in to change notification settings - Fork 39
feat(sql): Custom SQL udf are supported #244
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3e4bbeb
feat(sql): Custom SQL udf are supported
chenquan 14187a3
x
chenquan addd963
x
chenquan 774e4c6
x
chenquan 7cb2ca1
x
chenquan 40d6960
x
chenquan a2fd659
x
chenquan 3be1ee9
x
chenquan ed4c51d
x
chenquan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /* | ||
| * Licensed 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 arkflow_core::Error; | ||
| use datafusion::execution::FunctionRegistry; | ||
| use datafusion::logical_expr::AggregateUDF; | ||
| use std::sync::{Arc, RwLock}; | ||
| use tracing::debug; | ||
|
|
||
| lazy_static::lazy_static! { | ||
| static ref UDFS: RwLock<Vec<Arc<AggregateUDF>>> = RwLock::new(Vec::new()); | ||
| } | ||
|
|
||
| /// Register a new aggregate UDF (User Defined Function). | ||
| /// | ||
| /// This function wraps the provided AggregateUDF instance in an Arc and stores it in the global UDFS list, | ||
| /// so it can later be registered with the FunctionRegistry. | ||
| /// | ||
| /// # Arguments | ||
| /// * `udf` - The AggregateUDF instance to register. | ||
| pub fn register(udf: AggregateUDF) { | ||
| let mut udfs = UDFS.write().expect("Failed to acquire write lock for UDFS"); | ||
| udfs.push(Arc::new(udf)); | ||
| } | ||
|
|
||
| pub(crate) fn init<T: FunctionRegistry>(registry: &mut T) -> Result<(), Error> { | ||
| let aggregate_udfs = UDFS | ||
| .read() | ||
| .expect("Failed to acquire read lock for aggregate UDFS"); | ||
| aggregate_udfs | ||
| .iter() | ||
| .try_for_each(|udf| { | ||
| let existing_udf = registry.register_udaf(Arc::clone(udf))?; | ||
| if let Some(existing_udf) = existing_udf { | ||
| debug!("Overwrite existing aggregate UDF: {}", existing_udf.name()); | ||
| } | ||
| Ok(()) as datafusion::common::Result<()> | ||
| }) | ||
| .map_err(|e| Error::Config(format!("Failed to register aggregate UDFs: {}", e))) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /* | ||
| * Licensed 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 arkflow_core::Error; | ||
| use datafusion::execution::FunctionRegistry; | ||
| use datafusion::logical_expr::ScalarUDF; | ||
| use std::sync::{Arc, RwLock}; | ||
| use tracing::debug; | ||
|
|
||
| lazy_static::lazy_static! { | ||
| static ref UDFS: RwLock<Vec<Arc<ScalarUDF>>> = RwLock::new(Vec::new()); | ||
| } | ||
|
|
||
| /// Register a new scalar UDF. | ||
| /// | ||
| /// This function adds a UDF to the global registry. The UDF will be available for use | ||
| /// in SQL queries after the next call to `init`. | ||
| /// | ||
| /// # Arguments | ||
| /// | ||
| /// * `udf` - The UDF to register, wrapped in an Arc for shared ownership. | ||
| pub fn register(udf: ScalarUDF) { | ||
| let mut udfs = UDFS.write().expect("Failed to acquire write lock for UDFS"); | ||
| udfs.push(Arc::new(udf)); | ||
| } | ||
|
|
||
| pub(crate) fn init<T: FunctionRegistry>(registry: &mut T) -> Result<(), Error> { | ||
| let scalar_udfs = UDFS | ||
| .read() | ||
| .expect("Failed to acquire read lock for scalar UDFS"); | ||
| scalar_udfs | ||
| .iter() | ||
| .try_for_each(|udf| { | ||
| let existing_udf = registry.register_udf(Arc::clone(udf))?; | ||
| if let Some(existing_udf) = existing_udf { | ||
| debug!("Overwrite existing scalar UDF: {}", existing_udf.name()); | ||
| } | ||
| Ok(()) as datafusion::common::Result<()> | ||
| }) | ||
| .map_err(|e| Error::Config(format!("Failed to register scalar UDFs: {}", e))) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /* | ||
| * Licensed 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 arkflow_core::Error; | ||
| use datafusion::execution::FunctionRegistry; | ||
| use datafusion::logical_expr::WindowUDF; | ||
| use std::sync::{Arc, RwLock}; | ||
| use tracing::debug; | ||
|
|
||
| lazy_static::lazy_static! { | ||
| static ref UDFS: RwLock<Vec<Arc<WindowUDF>>> = RwLock::new(Vec::new()); | ||
| } | ||
|
|
||
| /// Register a new window UDF (User Defined Function). | ||
| /// | ||
| /// This function wraps the provided WindowUDF instance in an Arc and stores it in the global UDFS list, | ||
| /// so it can later be registered with the FunctionRegistry. | ||
| /// | ||
| /// # Arguments | ||
| /// * `udf` - The WindowUDF instance to register. | ||
| pub fn register(udf: WindowUDF) { | ||
| let mut udfs = UDFS.write().expect("Failed to acquire write lock for UDFS"); | ||
| udfs.push(Arc::new(udf)); | ||
| } | ||
|
|
||
| pub(crate) fn init<T: FunctionRegistry>(registry: &mut T) -> Result<(), Error> { | ||
| let window_udfs = UDFS | ||
| .read() | ||
| .expect("Failed to acquire read lock for window UDFS"); | ||
| window_udfs | ||
| .iter() | ||
| .try_for_each(|udf| { | ||
| let existing_udf = registry.register_udwf(Arc::clone(udf))?; | ||
| if let Some(existing_udf) = existing_udf { | ||
| debug!("Overwrite existing window UDF: {}", existing_udf.name()); | ||
| } | ||
| Ok(()) as datafusion::common::Result<()> | ||
| }) | ||
| .map_err(|e| Error::Config(format!("Failed to register window UDFs: {}", e))) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.