-
Notifications
You must be signed in to change notification settings - Fork 762
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
functions: Add lower support #3521
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
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.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2021 Datafuse Labs. | ||
// | ||
// 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 bstr::ByteSlice; | ||
|
||
use super::string2string::String2StringFunction; | ||
use super::string2string::StringOperator; | ||
|
||
#[derive(Clone, Default)] | ||
pub struct Lower; | ||
|
||
impl StringOperator for Lower { | ||
#[inline] | ||
fn apply_with_no_null<'a>(&'a mut self, s: &'a [u8], buffer: &mut [u8]) -> usize { | ||
buffer.copy_from_slice(&s.to_lowercase()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, there is an extra copy. Maybe we can use https://docs.rs/bstr/latest/bstr/trait.ByteSlice.html#method.to_lowercase_into There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After some research, I found:
One possible solution is to implement the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just use the codes inside
|
||
|
||
s.len() | ||
} | ||
} | ||
|
||
pub type LowerFunction = String2StringFunction<Lower>; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright 2021 Datafuse Labs. | ||
// | ||
// 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 common_datavalues::prelude::*; | ||
use common_exception::Result; | ||
use common_functions::scalars::LowerFunction; | ||
|
||
use super::run_tests; | ||
use super::Test; | ||
|
||
#[test] | ||
fn test_lower_function() -> Result<()> { | ||
let schema = DataSchemaRefExt::create(vec![DataField::new("a", DataType::String, false)]); | ||
|
||
let tests = vec![ | ||
Test { | ||
name: "lower-abc-passed", | ||
display: "lower", | ||
nullable: true, | ||
arg_names: vec!["a"], | ||
columns: vec![Series::new(vec!["Abc"]).into()], | ||
func: LowerFunction::try_create("lower")?, | ||
expect: DataColumn::Constant(DataValue::String(Some("abc".as_bytes().to_vec())), 1), | ||
error: "", | ||
}, | ||
Test { | ||
name: "lower-utf8-passed", | ||
display: "lower", | ||
nullable: true, | ||
arg_names: vec!["a"], | ||
columns: vec![Series::new(vec!["Dobrý den"]).into()], | ||
func: LowerFunction::try_create("lower")?, | ||
expect: DataColumn::Constant( | ||
DataValue::String(Some("dobrý den".as_bytes().to_vec())), | ||
1, | ||
), | ||
error: "", | ||
}, | ||
Test { | ||
name: "lcase-utf8-passed", | ||
display: "lcase", | ||
nullable: true, | ||
arg_names: vec!["a"], | ||
columns: vec![Series::new(vec!["Dobrý den"]).into()], | ||
func: LowerFunction::try_create("lcase")?, | ||
expect: DataColumn::Constant( | ||
DataValue::String(Some("dobrý den".as_bytes().to_vec())), | ||
1, | ||
), | ||
error: "", | ||
}, | ||
Test { | ||
name: "lcase-null-passed", | ||
display: "lcase", | ||
nullable: true, | ||
arg_names: vec!["a"], | ||
columns: vec![Series::new(vec![Option::<Vec<u8>>::None]).into()], | ||
func: LowerFunction::try_create("lcase")?, | ||
expect: DataColumn::Constant(DataValue::String(None), 1), | ||
error: "", | ||
}, | ||
]; | ||
run_tests(tests, schema) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,8 +13,10 @@ | |
// limitations under the License. | ||
|
||
mod locate; | ||
mod lower; | ||
mod substring; | ||
mod trim; | ||
|
||
mod utils; | ||
|
||
pub use utils::*; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
hello,world! | ||
здравствуйте | ||
NULL |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
SELECT LOWER('Hello,World!'); | ||
SELECT LOWER('Здравствуйте'); | ||
SELECT LOWER(NULL); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
title: LCASE | ||
--- | ||
|
||
Synonym for LOWER(str). | ||
|
||
## Syntax | ||
|
||
```sql | ||
LCASE(str); | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
title: LOWER | ||
--- | ||
|
||
Returns the string str with all characters changed to lowercase. | ||
|
||
## Syntax | ||
|
||
```sql | ||
LOWER(str); | ||
``` | ||
|
||
## Arguments | ||
|
||
| Arguments | Description | | ||
|-----------|----------------------------| | ||
| str | The string to be lowercase | | ||
|
||
|
||
## Return Type | ||
|
||
A string data type value. | ||
|
||
## Examples | ||
|
||
```txt | ||
SELECT LOWER('Hello, World!') | ||
+----------------------------+ | ||
| substring('Hello, World!') | | ||
+----------------------------+ | ||
| hello, world! | | ||
+----------------------------+ | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's this crate for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bstr
is a byte string library. I use theByteSlice
trait so that we don't need to implements.char_indices()
on[u8]
again.