Skip to main content

hawdb_sql/
parser.rs

1// Copyright 2026 Nowledge
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::ast::*;
16use hawdb_core::{HawDBError, Result, Value};
17
18mod branch;
19#[cfg(test)]
20mod clause_tests;
21mod clauses;
22#[cfg(test)]
23mod compatibility_tests;
24mod full;
25#[cfg(test)]
26mod numeric_tests;
27mod owned;
28
29pub fn parse_postgres_sql(input: &str) -> Result<SqlStatement> {
30    if let Some(statement) = branch::parse_branch_statement(input)? {
31        return Ok(statement);
32    }
33    let token = hawdb_sql_syntax::first_token(input)
34        .map_err(|error| HawDBError::Parse(error.to_string()))?;
35    if token.is_keyword(input, "SELECT") || token.is_keyword(input, "EXPLAIN") {
36        return owned::parse(input);
37    }
38    full::parse(input)
39}
40
41#[derive(Clone, Copy)]
42pub(super) enum ExpressionPosition {
43    Predicate,
44    Having,
45    HavingScalar,
46    PredicateScalar,
47    Scalar,
48    Column,
49    Value,
50}
51
52fn lower_number(raw: &str) -> Result<Value> {
53    if !raw.contains(['.', 'e', 'E'])
54        && let Ok(value) = raw.parse::<i64>()
55    {
56        return Ok(Value::Int(value));
57    }
58    raw.parse::<hawdb_core::Decimal>()
59        .map(Value::Decimal)
60        .map_err(|error| {
61            HawDBError::Semantic(format!("invalid exact PostgreSQL number {raw}: {error}"))
62        })
63}