forked from gluesql/gluesql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Write documentation for AST Builder lpad & rpad (gluesql#1434)
- Loading branch information
Showing
1 changed file
with
78 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,5 +1,79 @@ | ||
# Padding | ||
## Todo | ||
- LPAD: Left-pads a string with a specified string up to a certain length. | ||
- RPAD: Right-pads a string with a specified string up to a certain length. | ||
# Padding | ||
|
||
The AST Builder API in GlueSQL allows you to execute lpad and rpad functions for text padding. | ||
|
||
## lpad | ||
|
||
`lpad` returns the string with leading space if the length of the string is less than the specified length. | ||
|
||
```rs | ||
lpad<'a, T: Into<ExprNode<'a>>>(expr: T, len: usize, fill: Option<String>) -> ExprNode<'a> | ||
``` | ||
|
||
## rpad | ||
|
||
`rpad` returns the string with trailing space if the length of the string is less than the specified length. | ||
|
||
```rs | ||
rpad<'a, T: Into<ExprNode<'a>>>(expr: T, len: usize, fill: Option<String>) -> ExprNode<'a> | ||
``` | ||
|
||
## Examples | ||
|
||
In these examples, the LPAD and RPAD functions should return matching values. | ||
|
||
```rs | ||
use { | ||
crate::*, | ||
gluesql_core::{ | ||
ast_builder::{function as f, *}, | ||
prelude::Value::{Null, Str}, | ||
}, | ||
}; | ||
|
||
test_case!(padding, { | ||
let glue = get_glue!(); | ||
|
||
let actual = values(vec![ | ||
vec![f::lpad("'hello'", 10, None), f::rpad("'hello'", 10, None)], | ||
vec![ | ||
f::lpad("'hello'", 10, Some("'ab'".into())), | ||
f::rpad("'hello'", 10, Some("'ab'".into())), | ||
], | ||
vec![f::lpad("'hello'", 3, None), f::rpad("'hello'", 3, None)], | ||
vec![ | ||
f::lpad("'hello'", 3, Some("'ab'".into())), | ||
f::rpad("'hello'", 3, Some("'ab'".into())), | ||
], | ||
vec![f::lpad("NULL", 5, None), f::rpad("NULL", 5, None)], | ||
]) | ||
.alias_as("Sub") | ||
.select() | ||
.project("column1 AS lpaded") | ||
.project("column2 AS rpaded") | ||
.execute(glue) | ||
.await; | ||
let expected = Ok(select_with_null!( | ||
lpaded | rpaded; | ||
Str(" hello".to_owned()) Str("hello ".to_owned()); | ||
Str("ababahello".to_owned()) Str("helloababa".to_owned()); | ||
Str("hel".to_owned()) Str("hel".to_owned()); | ||
Str("hel".to_owned()) Str("hel".to_owned()); | ||
Null Null | ||
)); | ||
assert_eq!( | ||
actual, expected, | ||
"lpad and rpad should pad the string with given length" | ||
); | ||
}); | ||
``` | ||
|
||
``` | ||
| lpaded | rpaded | | ||
| ------------ | ------------ | | ||
| ' hello' | 'hello ' | | ||
| 'ababahello' | 'helloababa' | | ||
| 'hel' | 'hel' | | ||
| 'hel' | 'hel' | | ||
| Null | Null | | ||
``` |