Skip to content

Commit

Permalink
Write documentation for AST Builder lpad & rpad (gluesql#1434)
Browse files Browse the repository at this point in the history
  • Loading branch information
devgony committed Oct 29, 2023
1 parent 42119a8 commit e248963
Showing 1 changed file with 78 additions and 4 deletions.
82 changes: 78 additions & 4 deletions docs/docs/ast-builder/functions/text/padding.md
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 |
```

0 comments on commit e248963

Please sign in to comment.