hamelin_legacy 0.3.10

Legacy AST translation code for Hamelin (to be deprecated)
Documentation
use hamelin_lib::{
    antlr::hamelinparser::{NestCommandContext, NestCommandContextAttrs},
    err::{TranslationError, TranslationErrors},
    sql::expression::identifier::HamelinIdentifier,
};

use crate::env::Environment;
use crate::translation::{
    projection_builder::{ProjectionBuilder, ProjectionBuilderExt},
    PendingQuery,
};
use crate::{ast::pipeline::HamelinPipeline, translation::sql_query_helpers::prepend_projections};

pub fn translate(
    ctx: &NestCommandContext<'static>,
    _pipeline: &HamelinPipeline,
    previous: &PendingQuery,
) -> Result<PendingQuery, TranslationErrors> {
    let into =
        HamelinIdentifier::new(TranslationErrors::expect(ctx, ctx.identifier())?).to_sql()?;
    let nested_projection = ProjectionBuilder::deep_initialize_from_environment(&previous.env);
    let mut new_projections = ProjectionBuilder::default();
    new_projections.bind(
        into,
        nested_projection
            .build_cast()
            .map_err(|e| TranslationError::wrap_box(ctx, e.into()))?
            .into(),
        nested_projection.build_hamelin_type().into(),
    );

    let env = Environment::new(new_projections.clone().build_hamelin_type());
    let res = PendingQuery::new(
        prepend_projections(
            &previous.query.clone().push_down(),
            new_projections
                .build_projections()
                .map_err(|e| TranslationError::wrap_box(ctx, e.into()))?,
            &env,
        )
        .into(),
        env,
    );

    Ok(res)
}