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#[cfg(test)]
29mod timestamp_tests;
30
31pub fn parse_postgres_sql(input: &str) -> Result<SqlStatement> {
32    if let Some(statement) = branch::parse_branch_statement(input)? {
33        return Ok(statement);
34    }
35    let token = hawdb_sql_syntax::first_token(input)
36        .map_err(|error| HawDBError::Parse(error.to_string()))?;
37    if token.is_keyword(input, "SELECT") || token.is_keyword(input, "EXPLAIN") {
38        return owned::parse(input);
39    }
40    full::parse(input)
41}
42
43#[derive(Clone, Copy)]
44pub(super) enum ExpressionPosition {
45    Predicate,
46    Having,
47    HavingScalar,
48    PredicateScalar,
49    Scalar,
50    Column,
51    Value,
52}
53
54fn lower_number(raw: &str) -> Result<Value> {
55    if !raw.contains(['.', 'e', 'E'])
56        && let Ok(value) = raw.parse::<i64>()
57    {
58        return Ok(Value::Int(value));
59    }
60    raw.parse::<hawdb_core::Decimal>()
61        .map(Value::Decimal)
62        .map_err(|error| {
63            HawDBError::Semantic(format!("invalid exact PostgreSQL number {raw}: {error}"))
64        })
65}