Skip to content

Commit

Permalink
Add UDFs to catalog; Add extension UDFs for TUPLEUNION/TUPLECONCAT (#496
Browse files Browse the repository at this point in the history
)

Extensions and scalar UDFs
---
Adds ability to create and register scalar User Defined Functions (UDFs)

TUPLEUNION & TUPLECONCAT
---
 
Adds `TUPLEUNION` and `TUPLECONCAT` functions via an extension crate under the `extension` crate namespace as scalar UDFs.

`TUPLEUNION` is as per 6.3.2 of the spec (https://partiql.org/assets/PartiQL-Specification.pdf#subsubsection.6.3.2)

`TUPLECONCAT` is inspired by description of concatenating binding environments as per section 3.3 of the spec (https://partiql.org/assets/PartiQL-Specification.pdf#subsection.3.3) and as requested in #495.

---

`tupleunion({ 'bob': 1, 'sally': 'error' }, { 'sally': 1 }, { 'sally': 2 }, { 'sally': 3 }, { 'sally': 4 })`
->
`{ 'bob': 1, 'sally': 'error', 'sally': 1, 'sally': 2, 'sally': 3, 'sally': 4 }`

---

`tupleconcat({ 'bob': 1, 'sally': 'error' }, { 'sally': 1 }, { 'sally': 2 }, { 'sally': 3 }, { 'sally': 4 })`
->
`{ 'sally': 4, 'bob': 1 }`
  • Loading branch information
jpschorr authored Oct 2, 2024
1 parent 70b5ea2 commit 104a7d1
Show file tree
Hide file tree
Showing 45 changed files with 1,339 additions and 502 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- partiql-ast: fixed pretty-printing of `PIVOT`
- partiql-ast: improved pretty-printing of `CASE` and various clauses
- *BREAKING* partiql-catalog: refactored structure of crate; module paths have changes

### Added
- Added `partiql-common`.
- Added `NodeId` to `StaticType`.
- *BREAKING* Added thread-safe `PartiqlShapeBuilder` and automatic `NodeId` generation for the `StaticType`.
- Added a static thread safe `shape_builder` function that provides a convenient way for using `PartiqlShapeBuilder` for creating new shapes.
- Added `partiql_common::meta::PartiqlMetadata`
- Added ability for crate importers to add scalar *U*ser *D*efined *F*unctions (UDFs) to the catalog
- Added `extension/partiql-extension-value-functions` crate demonstrating use of scalar UDFs
- Added `TUPLEUNION` and `TUPLECONCAT` functions in the `extension/partiql-extension-value-functions` crate

### Removed
- *BREAKING* Removed `partiql-source-map`.
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ members = [
"extension/partiql-extension-ddl",
"extension/partiql-extension-ion",
"extension/partiql-extension-ion-functions",
"extension/partiql-extension-value-functions",
"extension/partiql-extension-visualize",
]

Expand Down
5 changes: 4 additions & 1 deletion deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ ignore = [
# Advisory: https://rustsec.org/advisories/RUSTSEC-2020-0071
# `chrono` uses an old version of `time`, but `chrono` >= 4.2 doesn't use the code path with the issue
"RUSTSEC-2020-0071",
# Advisory: https://rustsec.org/advisories/RUSTSEC-2024-0375
# `atty` is unmaintained, but we still use a version of criterion that brings it in
"RUSTSEC-2024-0375"
]


Expand Down Expand Up @@ -103,7 +106,7 @@ deny = [
{ name = "time", version = "<0.2.23", wrappers = ["chrono"] },
# Advisory: https://rustsec.org/advisories/RUSTSEC-2020-0071
# `chrono` uses an old version of `time`, but `chrono` >= 4.2 doesn't use the code path with the issue
{ name = "chrono", version = "<0.4.20" }
{ name = "chrono", version = "<0.4.20" },
]


Expand Down
20 changes: 11 additions & 9 deletions extension/partiql-extension-ion-functions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

use ion_rs_old::data_source::ToIonDataSource;
use partiql_catalog::call_defs::{CallDef, CallSpec, CallSpecArg};
use partiql_catalog::TableFunction;
use partiql_catalog::{
BaseTableExpr, BaseTableExprResult, BaseTableExprResultError, BaseTableExprResultValueIter,
BaseTableFunctionInfo, Catalog,
use partiql_catalog::catalog::Catalog;
use partiql_catalog::table_fn::{
BaseTableExpr, BaseTableExprResult, BaseTableExprResultValueIter, BaseTableFunctionInfo,
TableFunction,
};
use partiql_extension_ion::decode::{IonDecoderBuilder, IonDecoderConfig};
use partiql_extension_ion::Encoding;
Expand All @@ -15,6 +15,7 @@ use partiql_value::Value;
use std::borrow::Cow;

use partiql_catalog::context::SessionContext;
use partiql_catalog::extension::ExtensionResultError;
use std::error::Error;
use std::fmt::Debug;
use std::fs::File;
Expand Down Expand Up @@ -51,7 +52,7 @@ impl From<std::io::Error> for IonExtensionError {
#[derive(Debug)]
pub struct IonExtension {}

impl partiql_catalog::Extension for IonExtension {
impl partiql_catalog::extension::Extension for IonExtension {
fn name(&self) -> String {
"ion".into()
}
Expand Down Expand Up @@ -114,12 +115,12 @@ impl BaseTableExpr for EvalFnReadIon {
let error = IonExtensionError::FunctionError(
"expected string path argument".to_string(),
);
Err(Box::new(error) as BaseTableExprResultError)
Err(Box::new(error) as ExtensionResultError)
}
}
} else {
let error = IonExtensionError::FunctionError("expected path argument".to_string());
Err(Box::new(error) as BaseTableExprResultError)
Err(Box::new(error) as ExtensionResultError)
}
}
}
Expand Down Expand Up @@ -151,7 +152,7 @@ fn parse_ion_read<'a>(mut reader: impl 'a + Read + Seek) -> BaseTableExprResult<
}

fn parse_ion_buff<'a, I: 'a + ToIonDataSource>(input: I) -> BaseTableExprResult<'a> {
let err_map = |e| Box::new(e) as BaseTableExprResultError;
let err_map = |e| Box::new(e) as ExtensionResultError;
let reader = ion_rs_old::ReaderBuilder::new().build(input).unwrap();
let decoder =
IonDecoderBuilder::new(IonDecoderConfig::default().with_mode(Encoding::Ion)).build(reader);
Expand All @@ -163,8 +164,9 @@ fn parse_ion_buff<'a, I: 'a + ToIonDataSource>(input: I) -> BaseTableExprResult<
mod tests {
use super::*;

use partiql_catalog::catalog::{Catalog, PartiqlCatalog};
use partiql_catalog::context::SystemContext;
use partiql_catalog::{Catalog, Extension, PartiqlCatalog};
use partiql_catalog::extension::Extension;
use partiql_eval::env::basic::MapBindings;
use partiql_eval::eval::BasicContext;
use partiql_eval::plan::EvaluationMode;
Expand Down
33 changes: 33 additions & 0 deletions extension/partiql-extension-value-functions/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[package]
name = "partiql-extension-value-functions"
description = "PartiQL Value function extensions"
authors.workspace = true
homepage.workspace = true
repository.workspace = true
license = "Apache-2.0"
readme = "../../README.md"
keywords = ["sql", "query", "compilers", "interpreters"]
categories = ["database", "compilers"]
exclude = [
"**/.git/**",
"**/.github/**",
"**/.travis.yml",
"**/.appveyor.yml",
]
version.workspace = true
edition.workspace = true

[lib]
bench = false

[dependencies]
partiql-value = { path = "../../partiql-value", version = "0.10.*" }
partiql-catalog = { path = "../../partiql-catalog", version = "0.10.*" }
partiql-logical = { path = "../../partiql-logical", version = "0.10.*" }

ordered-float = "3.*"
unicase = "2.6"
time = { version = "0.3", features = ["macros"] }

[features]
default = []
175 changes: 175 additions & 0 deletions extension/partiql-extension-value-functions/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@

Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/

TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION

1. Definitions.

"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.

"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.

"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.

"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.

"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.

"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.

"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).

"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.

"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."

"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.

2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.

3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.

4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:

(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and

(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and

(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and

(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.

You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.

5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.

6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.

7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.

8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.

9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
Loading

1 comment on commit 104a7d1

@github-actions
Copy link

Choose a reason for hiding this comment

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

PartiQL (rust) Benchmark

Benchmark suite Current: 104a7d1 Previous: 70b5ea2 Ratio
arith_agg-avg 763771 ns/iter (± 16741) 759500 ns/iter (± 1408) 1.01
arith_agg-avg_distinct 851786 ns/iter (± 1842) 849462 ns/iter (± 4155) 1.00
arith_agg-count 809835 ns/iter (± 14050) 806498 ns/iter (± 14064) 1.00
arith_agg-count_distinct 848062 ns/iter (± 13483) 843196 ns/iter (± 4176) 1.01
arith_agg-min 812714 ns/iter (± 1506) 813808 ns/iter (± 2458) 1.00
arith_agg-min_distinct 851673 ns/iter (± 7418) 848859 ns/iter (± 16282) 1.00
arith_agg-max 818977 ns/iter (± 3083) 814628 ns/iter (± 2353) 1.01
arith_agg-max_distinct 860550 ns/iter (± 4522) 862783 ns/iter (± 3478) 1.00
arith_agg-sum 814833 ns/iter (± 2186) 813703 ns/iter (± 2335) 1.00
arith_agg-sum_distinct 851388 ns/iter (± 3259) 846973 ns/iter (± 3297) 1.01
arith_agg-avg-count-min-max-sum 967156 ns/iter (± 3874) 943604 ns/iter (± 2933) 1.02
arith_agg-avg-count-min-max-sum-group_by 1234665 ns/iter (± 29328) 1165672 ns/iter (± 16453) 1.06
arith_agg-avg-count-min-max-sum-group_by-group_as 1836036 ns/iter (± 8969) 1762193 ns/iter (± 14659) 1.04
arith_agg-avg_distinct-count_distinct-min_distinct-max_distinct-sum_distinct 1291597 ns/iter (± 20131) 1277611 ns/iter (± 15074) 1.01
arith_agg-avg_distinct-count_distinct-min_distinct-max_distinct-sum_distinct-group_by 1572591 ns/iter (± 7342) 1492997 ns/iter (± 19875) 1.05
arith_agg-avg_distinct-count_distinct-min_distinct-max_distinct-sum_distinct-group_by-group_as 2156366 ns/iter (± 12394) 2123779 ns/iter (± 12249) 1.02
parse-1 6093 ns/iter (± 41) 6113 ns/iter (± 45) 1.00
parse-15 50048 ns/iter (± 93) 49828 ns/iter (± 181) 1.00
parse-30 98554 ns/iter (± 330) 97703 ns/iter (± 288) 1.01
compile-1 4320 ns/iter (± 23) 4307 ns/iter (± 13) 1.00
compile-15 32611 ns/iter (± 325) 32139 ns/iter (± 155) 1.01
compile-30 68061 ns/iter (± 714) 67087 ns/iter (± 205) 1.01
plan-1 67828 ns/iter (± 261) 67118 ns/iter (± 1029) 1.01
plan-15 1064049 ns/iter (± 14178) 1051816 ns/iter (± 9727) 1.01
plan-30 2124398 ns/iter (± 5112) 2104501 ns/iter (± 3002) 1.01
eval-1 13498152 ns/iter (± 233756) 12482615 ns/iter (± 60899) 1.08
eval-15 91551008 ns/iter (± 350907) 84866990 ns/iter (± 802755) 1.08
eval-30 176077656 ns/iter (± 654476) 163210743 ns/iter (± 892385) 1.08
join 10072 ns/iter (± 21) 10122 ns/iter (± 43) 1.00
simple 2635 ns/iter (± 7) 2456 ns/iter (± 15) 1.07
simple-no 476 ns/iter (± 1) 429 ns/iter (± 2) 1.11
numbers 48 ns/iter (± 0) 57 ns/iter (± 0) 0.84
parse-simple 882 ns/iter (± 3) 866 ns/iter (± 4) 1.02
parse-ion 2576 ns/iter (± 35) 2616 ns/iter (± 4) 0.98
parse-group 7564 ns/iter (± 26) 7933 ns/iter (± 24) 0.95
parse-complex 20026 ns/iter (± 62) 20088 ns/iter (± 102) 1.00
parse-complex-fexpr 27626 ns/iter (± 111) 27415 ns/iter (± 84) 1.01

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.