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