use crate::ast::expression::HamelinExpression;
use crate::ast::pipeline::HamelinPipeline;
use crate::translation::sql_query_helpers::apply_limit;
use crate::translation::PendingQuery;
use hamelin_lib::antlr::hamelinparser::{LimitCommandContext, LimitCommandContextAttrs};
use hamelin_lib::err::{TranslationError, TranslationErrors};
use hamelin_lib::types::INT;
pub fn translate(
ctx: &LimitCommandContext<'static>,
pipeline: &HamelinPipeline,
previous: &PendingQuery,
) -> Result<PendingQuery, TranslationErrors> {
let exptree = TranslationErrors::expect(ctx, ctx.expression())?;
let exp = HamelinExpression::new(
exptree.clone(),
pipeline
.context
.default_expression_translation_context(&previous.env),
)
.translate()?;
if exp.typ != INT {
return Err(
TranslationError::msg(exptree.as_ref(), "limit expression must be an integer").single(),
);
}
Ok(PendingQuery::new(
apply_limit(previous.query.clone(), exp.sql, &previous.env),
previous.env.clone(),
))
}