Add can_pushdown_predicate fn to evaluate ast expressions for constness

This commit is contained in:
PThorpe92 2025-04-06 14:20:51 -04:00
parent 853af16946
commit 0f34a813ff
No known key found for this signature in database
GPG key ID: 66DB3FBACBDD05CC

View file

@ -2,6 +2,7 @@ use limbo_sqlite3_parser::ast::{self, CreateTableBody, Expr, FunctionTail, Liter
use std::{rc::Rc, sync::Arc};
use crate::{
function::Func,
schema::{self, Column, Schema, Type},
types::{OwnedValue, OwnedValueType},
LimboError, OpenFlags, Result, Statement, StepResult, SymbolTable, IO,
@ -565,6 +566,41 @@ pub fn columns_from_create_table_body(body: &ast::CreateTableBody) -> crate::Res
.collect::<Vec<_>>())
}
pub fn can_pushdown_predicate(expr: &Expr) -> bool {
match expr {
Expr::Literal(_) => true,
Expr::Column { .. } => true,
Expr::Binary(lhs, _, rhs) => can_pushdown_predicate(lhs) && can_pushdown_predicate(rhs),
Expr::Parenthesized(exprs) => can_pushdown_predicate(exprs.first().unwrap()),
Expr::Unary(_, expr) => can_pushdown_predicate(expr),
Expr::FunctionCall { args, name, .. } => {
let function = crate::function::Func::resolve_function(
&name.0,
args.as_ref().map_or(0, |a| a.len()),
);
matches!(function, Ok(Func::Scalar(_)))
}
Expr::Like { lhs, rhs, .. } => can_pushdown_predicate(lhs) && can_pushdown_predicate(rhs),
Expr::Between {
lhs, start, end, ..
} => {
can_pushdown_predicate(lhs)
&& can_pushdown_predicate(start)
&& can_pushdown_predicate(end)
}
Expr::Id(_) => true,
Expr::Name(_) => true,
Expr::Qualified(_, _) => true,
Expr::DoublyQualified(_, _, _) => true,
Expr::InTable { lhs, .. } => can_pushdown_predicate(lhs),
_ => false,
}
}
fn is_deterministic(func: &Func) -> bool {
matches!(func, Func::Scalar(_))
}
#[derive(Debug, Default, PartialEq)]
pub struct OpenOptions<'a> {
/// The authority component of the URI. may be 'localhost' or empty