Skip to main content

nodedb_sql/resolver/expr/convert/
builtins.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! Built-in SQL constructs that parse to dedicated AST nodes and lower
4//! to ordinary function calls.
5
6use sqlparser::ast::Expr;
7
8use crate::error::Result;
9use crate::resolver::ColumnScope;
10use crate::types::*;
11
12use super::entry::convert_expr_depth;
13
14/// TRIM([BOTH|LEADING|TRAILING] [what FROM] expr)
15pub(super) fn convert_trim(
16    expr: &Expr,
17    depth: &mut usize,
18    scope: &ColumnScope<'_>,
19) -> Result<SqlExpr> {
20    Ok(SqlExpr::Function {
21        name: "trim".into(),
22        args: vec![convert_expr_depth(expr, depth, scope)?],
23        distinct: false,
24    })
25}
26
27/// CEIL(expr)
28pub(super) fn convert_ceil(
29    expr: &Expr,
30    depth: &mut usize,
31    scope: &ColumnScope<'_>,
32) -> Result<SqlExpr> {
33    Ok(SqlExpr::Function {
34        name: "ceil".into(),
35        args: vec![convert_expr_depth(expr, depth, scope)?],
36        distinct: false,
37    })
38}
39
40/// FLOOR(expr)
41pub(super) fn convert_floor(
42    expr: &Expr,
43    depth: &mut usize,
44    scope: &ColumnScope<'_>,
45) -> Result<SqlExpr> {
46    Ok(SqlExpr::Function {
47        name: "floor".into(),
48        args: vec![convert_expr_depth(expr, depth, scope)?],
49        distinct: false,
50    })
51}
52
53/// SUBSTRING(expr FROM start FOR len)
54pub(super) fn convert_substring(
55    expr: &Expr,
56    substring_from: Option<&Expr>,
57    substring_for: Option<&Expr>,
58    depth: &mut usize,
59    scope: &ColumnScope<'_>,
60) -> Result<SqlExpr> {
61    let mut args = vec![convert_expr_depth(expr, depth, scope)?];
62    if let Some(from) = substring_from {
63        args.push(convert_expr_depth(from, depth, scope)?);
64    }
65    if let Some(len) = substring_for {
66        args.push(convert_expr_depth(len, depth, scope)?);
67    }
68    Ok(SqlExpr::Function {
69        name: "substring".into(),
70        args,
71        distinct: false,
72    })
73}