-
Notifications
You must be signed in to change notification settings - Fork 484
feat: add json data type #4619
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
feat: add json data type #4619
Changes from 13 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
7836e03
feat: add json type and vector
CookiePieWw ed2f07d
fix: allow to create and insert json data
CookiePieWw f910b37
feat: udf to query json as string
CookiePieWw c334919
refactor: remove JsonbValue and JsonVector
CookiePieWw b2585ac
feat: show json value as strings
CookiePieWw 45c853b
chore: make ci happy
CookiePieWw 5e9e417
test: adunit test and sqlness test
CookiePieWw 092ee0c
refactor: use binary as grpc value of json
CookiePieWw 6d5241d
Merge branch 'main' into json
CookiePieWw cb88312
fix: use non-preserve-order jsonb
CookiePieWw 7ceddd0
test: revert changed test
CookiePieWw 26e8d1a
refactor: change udf get_by_path to jq
CookiePieWw d3752b3
chore: make ci happy
CookiePieWw 601f647
fix: distinguish binary and json in proto
CookiePieWw 13c370c
chore: delete udf for future pr
CookiePieWw 619f490
refactor: remove Value(Json)
CookiePieWw 9f07158
chore: follow review comments
CookiePieWw 1c279e4
test: some tests and checks
CookiePieWw ae85538
test: fix unit tests
CookiePieWw 411cf8f
chore: follow review comments
CookiePieWw 3ca3d42
chore: corresponding changes to proto
CookiePieWw 1f2d751
fix: change grpc and pgsql server behavior alongside with sqlness/cru…
CookiePieWw 5645c63
chore: follow review comments
CookiePieWw d908a46
feat: udf of conversions between json and strings, used for grpc server
CookiePieWw 34c15b0
refactor: rename to_string to json_to_string
CookiePieWw 94e57be
chore: resolve merge conflict
CookiePieWw 315b1e6
test: add more sqlness test for json
CookiePieWw ac3f9e1
chore: thanks for review :)
CookiePieWw 65b71ce
chore: merge main into json
CookiePieWw 9141eaa
Apply suggestions from code review
WenyXu 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,28 @@ | ||
| // Copyright 2023 Greptime Team | ||
| // | ||
| // 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 std::sync::Arc; | ||
| mod jq; | ||
|
|
||
| use jq::JqFunction; | ||
|
|
||
| use crate::function_registry::FunctionRegistry; | ||
|
|
||
| pub(crate) struct JsonFunction; | ||
|
|
||
| impl JsonFunction { | ||
| pub fn register(registry: &FunctionRegistry) { | ||
| registry.register(Arc::new(JqFunction)); | ||
| } | ||
| } |
|
WenyXu marked this conversation as resolved.
|
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,174 @@ | ||
| // Copyright 2023 Greptime Team | ||
| // | ||
| // 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 std::fmt::{self, Display}; | ||
|
|
||
| use common_query::error::{InvalidFuncArgsSnafu, Result, UnsupportedInputDataTypeSnafu}; | ||
| use common_query::prelude::Signature; | ||
| use datafusion::logical_expr::Volatility; | ||
| use datatypes::data_type::ConcreteDataType; | ||
| use datatypes::prelude::VectorRef; | ||
| use datatypes::scalars::ScalarVectorBuilder; | ||
| use datatypes::vectors::{MutableVector, StringVectorBuilder}; | ||
|
|
||
| use crate::function::{Function, FunctionContext}; | ||
|
|
||
| #[derive(Clone, Debug, Default)] | ||
|
WenyXu marked this conversation as resolved.
|
||
| pub struct JqFunction; | ||
|
|
||
| const NAME: &str = "jq"; | ||
|
|
||
| impl Function for JqFunction { | ||
| fn name(&self) -> &str { | ||
| "jq" | ||
| } | ||
|
|
||
| fn return_type(&self, _input_types: &[ConcreteDataType]) -> Result<ConcreteDataType> { | ||
| Ok(ConcreteDataType::string_datatype()) | ||
| } | ||
|
|
||
| fn signature(&self) -> Signature { | ||
| Signature::exact( | ||
| vec![ | ||
| ConcreteDataType::string_datatype(), | ||
| ConcreteDataType::json_datatype(), | ||
| ], | ||
| Volatility::Immutable, | ||
| ) | ||
| } | ||
|
|
||
| fn eval(&self, _func_ctx: FunctionContext, columns: &[VectorRef]) -> Result<VectorRef> { | ||
| let jsons = &columns[1]; | ||
| let paths = &columns[0]; | ||
|
|
||
| let size = jsons.len(); | ||
| let datatype = jsons.data_type(); | ||
| let mut results = StringVectorBuilder::with_capacity(size); | ||
|
|
||
| match datatype { | ||
| ConcreteDataType::Binary(_) | ConcreteDataType::Json(_) => { | ||
| for i in 0..size { | ||
| let json = jsons.get_ref(i); | ||
|
WenyXu marked this conversation as resolved.
|
||
| let json = json.as_binary().unwrap(); | ||
| let path = paths.get_ref(i); | ||
| let path = path.as_string().unwrap(); | ||
| let result = match (json, path) { | ||
| (Some(json), Some(path)) => { | ||
| let json_path = match jsonb::jsonpath::parse_json_path(path.as_bytes()) | ||
| { | ||
| Ok(json_path) => json_path, | ||
| Err(_) => { | ||
| return InvalidFuncArgsSnafu { | ||
| err_msg: format!("Invalid JSON path: {}", path), | ||
| } | ||
| .fail(); | ||
| } | ||
| }; | ||
| let mut sub_jsonb = Vec::new(); | ||
| let mut sub_offsets = Vec::new(); | ||
| match jsonb::get_by_path( | ||
| json, | ||
| json_path, | ||
| &mut sub_jsonb, | ||
| &mut sub_offsets, | ||
| ) { | ||
| Ok(_) => Some(jsonb::to_string(&sub_jsonb)), | ||
| Err(_) => None, | ||
| } | ||
| } | ||
| _ => None, | ||
| }; | ||
|
|
||
| results.push(result.as_deref()); | ||
| } | ||
| } | ||
| _ => { | ||
| return UnsupportedInputDataTypeSnafu { | ||
| function: NAME, | ||
| datatypes: columns.iter().map(|c| c.data_type()).collect::<Vec<_>>(), | ||
| } | ||
| .fail(); | ||
| } | ||
| } | ||
|
|
||
| Ok(results.to_vector()) | ||
| } | ||
| } | ||
|
|
||
| impl Display for JqFunction { | ||
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| write!(f, "JQ") | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use std::sync::Arc; | ||
|
|
||
| use common_query::prelude::TypeSignature; | ||
| use datatypes::scalars::ScalarVector; | ||
| use datatypes::vectors::{BinaryVector, StringVector}; | ||
|
|
||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_jq_function() { | ||
| let jq = JqFunction; | ||
|
|
||
| assert_eq!("jq", jq.name()); | ||
| assert_eq!( | ||
| ConcreteDataType::string_datatype(), | ||
| jq.return_type(&[ | ||
| ConcreteDataType::json_datatype(), | ||
| ConcreteDataType::string_datatype() | ||
| ]) | ||
| .unwrap() | ||
| ); | ||
|
|
||
| assert!(matches!(jq.signature(), | ||
| Signature { | ||
| type_signature: TypeSignature::Exact(valid_types), | ||
| volatility: Volatility::Immutable | ||
| } if valid_types == vec![ConcreteDataType::json_datatype(), ConcreteDataType::string_datatype()] | ||
| )); | ||
|
|
||
| let json_strings = [ | ||
| r#"{"a": {"b": 2}, "b": 2, "c": 3}"#, | ||
| r#"{"a": 4, "b": {"c": 6}, "c": 6}"#, | ||
| r#"{"a": 7, "b": 8, "c": {"a": 7}}"#, | ||
| ]; | ||
| let paths = vec!["a", "b", "c"]; | ||
| let results = [r#"{"b":2}"#, r#"{"c":6}"#, r#"{"a":7}"#]; | ||
|
|
||
| let jsonbs = json_strings | ||
| .iter() | ||
| .map(|s| { | ||
| let value = jsonb::parse_value(s.as_bytes()).unwrap(); | ||
| value.to_vec() | ||
| }) | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| let json_vector = BinaryVector::from_vec(jsonbs); | ||
| let path_vector = StringVector::from_vec(paths); | ||
| let args: Vec<VectorRef> = vec![Arc::new(json_vector), Arc::new(path_vector)]; | ||
| let vector = jq.eval(FunctionContext::default(), &args).unwrap(); | ||
|
|
||
| assert_eq!(3, vector.len()); | ||
| for (i, gt) in results.iter().enumerate() { | ||
| let result = vector.get_ref(i); | ||
| let result = result.as_string().unwrap().unwrap(); | ||
| assert_eq!(*gt, result); | ||
| } | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.