Skip to content

Commit 0f75cfe

Browse files
committed
chore(function): migrate left, right, substr and space to new expression framework
1 parent 6dfea15 commit 0f75cfe

File tree

13 files changed

+513
-113
lines changed

13 files changed

+513
-113
lines changed

docs/doc/30-reference/20-functions/40-string-functions/substring.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ SUBSTRING(str FROM pos FOR len)
2424
| Arguments | Description |
2525
| ----------- | ----------- |
2626
| str | The main string from where the character to be extracted |
27-
| pos | The one-indexed position expression to start at. If negative, counts from the end |
28-
| len | The number expression of characters to extract |
27+
| pos | The position (starting from 1) the substring starting at. If negative, counts from the end |
28+
| len | The maximun length of the substring to extract |
2929

3030
## Return Type
3131

src/query/ast/src/ast/expr.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ pub enum Expr<'a> {
111111
Substring {
112112
span: &'a [Token<'a>],
113113
expr: Box<Expr<'a>>,
114-
substring_from: Option<Box<Expr<'a>>>,
114+
substring_from: Box<Expr<'a>>,
115115
substring_for: Option<Box<Expr<'a>>>,
116116
},
117117
/// TRIM([[BOTH | LEADING | TRAILING] <expr> FROM] <expr>)
@@ -728,10 +728,7 @@ impl<'a> Display for Expr<'a> {
728728
substring_for,
729729
..
730730
} => {
731-
write!(f, "SUBSTRING({expr}")?;
732-
if let Some(substring_from) = substring_from {
733-
write!(f, " FROM {substring_from}")?;
734-
}
731+
write!(f, "SUBSTRING({expr} FROM {substring_from}")?;
735732
if let Some(substring_for) = substring_for {
736733
write!(f, " FOR {substring_for}")?;
737734
}

src/query/ast/src/ast/format/ast_format.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -387,16 +387,14 @@ impl<'ast> Visitor<'ast> for AstFormatVisitor {
387387
&mut self,
388388
_span: &'ast [Token<'ast>],
389389
expr: &'ast Expr<'ast>,
390-
substring_from: &'ast Option<Box<Expr<'ast>>>,
390+
substring_from: &'ast Expr<'ast>,
391391
substring_for: &'ast Option<Box<Expr<'ast>>>,
392392
) {
393393
let mut children = Vec::with_capacity(1);
394394
self.visit_expr(expr);
395395
children.push(self.children.pop().unwrap());
396-
if let Some(substring_from) = substring_from {
397-
self.visit_expr(substring_from);
398-
children.push(self.children.pop().unwrap());
399-
}
396+
self.visit_expr(substring_from);
397+
children.push(self.children.pop().unwrap());
400398
if let Some(substring_for) = substring_for {
401399
self.visit_expr(substring_for);
402400
children.push(self.children.pop().unwrap());

src/query/ast/src/ast/format/syntax/expr.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,10 @@ pub(crate) fn pretty_expr(expr: Expr) -> RcDoc {
183183
..
184184
} => RcDoc::text("SUBSTRING(")
185185
.append(pretty_expr(*expr))
186-
.append(if let Some(substring_from) = substring_from {
187-
RcDoc::space()
188-
.append(RcDoc::text("FROM"))
189-
.append(RcDoc::space())
190-
.append(pretty_expr(*substring_from))
191-
} else {
192-
RcDoc::nil()
193-
})
186+
.append(RcDoc::space())
187+
.append(RcDoc::text("FROM"))
188+
.append(RcDoc::space())
189+
.append(pretty_expr(*substring_from))
194190
.append(if let Some(substring_for) = substring_for {
195191
RcDoc::space()
196192
.append(RcDoc::text("FOR"))

src/query/ast/src/parser/expr.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ pub enum ExprElement<'a> {
206206
/// SUBSTRING(<expr> [FROM <expr>] [FOR <expr>])
207207
SubString {
208208
expr: Box<Expr<'a>>,
209-
substring_from: Option<Box<Expr<'a>>>,
209+
substring_from: Box<Expr<'a>>,
210210
substring_for: Option<Box<Expr<'a>>>,
211211
},
212212
/// TRIM([[BOTH | LEADING | TRAILING] <expr> FROM] <expr>)
@@ -667,13 +667,14 @@ pub fn expr_element(i: Input) -> IResult<WithSpan<ExprElement>> {
667667
SUBSTRING
668668
~ ^"("
669669
~ ^#subexpr(0)
670-
~ ( ( FROM | "," ) ~ ^#subexpr(0) )?
670+
~ ( FROM | "," )
671+
~ ^#subexpr(0)
671672
~ ( ( FOR | "," ) ~ ^#subexpr(0) )?
672673
~ ^")"
673674
},
674-
|(_, _, expr, opt_substring_from, opt_substring_for, _)| ExprElement::SubString {
675+
|(_, _, expr, _, substring_from, opt_substring_for, _)| ExprElement::SubString {
675676
expr: Box::new(expr),
676-
substring_from: opt_substring_from.map(|(_, expr)| Box::new(expr)),
677+
substring_from: Box::new(substring_from),
677678
substring_for: opt_substring_for.map(|(_, expr)| Box::new(expr)),
678679
},
679680
);

src/query/ast/src/visitors/visitor.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,11 @@ pub trait Visitor<'ast>: Sized {
197197
&mut self,
198198
_span: &'ast [Token<'ast>],
199199
expr: &'ast Expr<'ast>,
200-
substring_from: &'ast Option<Box<Expr<'ast>>>,
200+
substring_from: &'ast Expr<'ast>,
201201
substring_for: &'ast Option<Box<Expr<'ast>>>,
202202
) {
203203
walk_expr(self, expr);
204-
if let Some(substring_from) = substring_from {
205-
walk_expr(self, substring_from);
206-
}
204+
walk_expr(self, substring_from);
207205
if let Some(substring_for) = substring_for {
208206
walk_expr(self, substring_for);
209207
}

src/query/ast/src/visitors/visitor_mut.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,11 @@ pub trait VisitorMut: Sized {
197197
&mut self,
198198
_span: &mut &[Token<'_>],
199199
expr: &mut Expr<'_>,
200-
substring_from: &mut Option<Box<Expr<'_>>>,
200+
substring_from: &mut Box<Expr<'_>>,
201201
substring_for: &mut Option<Box<Expr<'_>>>,
202202
) {
203203
walk_expr_mut(self, expr);
204-
205-
if let Some(substring_from) = substring_from {
206-
walk_expr_mut(self, substring_from);
207-
}
204+
walk_expr_mut(self, substring_from);
208205

209206
if let Some(substring_for) = substring_for {
210207
walk_expr_mut(self, substring_for);

src/query/ast/tests/it/testdata/expr.txt

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,20 +1582,18 @@ Substring {
15821582
span: Ident(10..11),
15831583
},
15841584
},
1585-
substring_from: Some(
1586-
ColumnRef {
1587-
span: [
1588-
Ident(17..18),
1589-
],
1590-
database: None,
1591-
table: None,
1592-
column: Identifier {
1593-
name: "b",
1594-
quote: None,
1595-
span: Ident(17..18),
1596-
},
1585+
substring_from: ColumnRef {
1586+
span: [
1587+
Ident(17..18),
1588+
],
1589+
database: None,
1590+
table: None,
1591+
column: Identifier {
1592+
name: "b",
1593+
quote: None,
1594+
span: Ident(17..18),
15971595
},
1598-
),
1596+
},
15991597
substring_for: Some(
16001598
ColumnRef {
16011599
span: [
@@ -1641,20 +1639,18 @@ Substring {
16411639
span: Ident(10..11),
16421640
},
16431641
},
1644-
substring_from: Some(
1645-
ColumnRef {
1646-
span: [
1647-
Ident(13..14),
1648-
],
1649-
database: None,
1650-
table: None,
1651-
column: Identifier {
1652-
name: "b",
1653-
quote: None,
1654-
span: Ident(13..14),
1655-
},
1642+
substring_from: ColumnRef {
1643+
span: [
1644+
Ident(13..14),
1645+
],
1646+
database: None,
1647+
table: None,
1648+
column: Identifier {
1649+
name: "b",
1650+
quote: None,
1651+
span: Ident(13..14),
16561652
},
1657-
),
1653+
},
16581654
substring_for: Some(
16591655
ColumnRef {
16601656
span: [

0 commit comments

Comments
 (0)