common_macro/
aggr_func.rs

1// Copyright 2023 Greptime Team
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 proc_macro::TokenStream;
16use quote::{quote, quote_spanned};
17use syn::parse::Parser;
18use syn::spanned::Spanned;
19use syn::{parse_macro_input, DeriveInput, ItemStruct};
20
21pub(crate) fn impl_aggr_func_type_store(ast: &DeriveInput) -> TokenStream {
22    let name = &ast.ident;
23    let gen = quote! {
24        impl common_query::logical_plan::accumulator::AggrFuncTypeStore for #name {
25            fn input_types(&self) -> std::result::Result<Vec<datatypes::prelude::ConcreteDataType>, common_query::error::Error> {
26                let input_types = self.input_types.load();
27                snafu::ensure!(input_types.is_some(), common_query::error::InvalidInputStateSnafu);
28                Ok(input_types.as_ref().unwrap().as_ref().clone())
29            }
30
31            fn set_input_types(&self, input_types: Vec<datatypes::prelude::ConcreteDataType>) -> std::result::Result<(), common_query::error::Error> {
32                let old = self.input_types.swap(Some(std::sync::Arc::new(input_types.clone())));
33                if let Some(old) = old {
34                    snafu::ensure!(old.len() == input_types.len(), common_query::error::InvalidInputStateSnafu);
35                    for (x, y) in old.iter().zip(input_types.iter()) {
36                        snafu::ensure!(x == y, common_query::error::InvalidInputStateSnafu);
37                    }
38                }
39                Ok(())
40            }
41        }
42    };
43    gen.into()
44}
45
46pub(crate) fn impl_as_aggr_func_creator(_args: TokenStream, input: TokenStream) -> TokenStream {
47    let mut item_struct = parse_macro_input!(input as ItemStruct);
48    if let syn::Fields::Named(ref mut fields) = item_struct.fields {
49        let result = syn::Field::parse_named.parse2(quote! {
50            input_types: arc_swap::ArcSwapOption<Vec<datatypes::prelude::ConcreteDataType>>
51        });
52        match result {
53            Ok(field) => fields.named.push(field),
54            Err(e) => return e.into_compile_error().into(),
55        }
56    } else {
57        return quote_spanned!(
58            item_struct.fields.span() => compile_error!(
59                "This attribute macro needs to add fields to the its annotated struct, \
60                so the struct must have \"{}\".")
61        )
62        .into();
63    }
64    quote! {
65        #item_struct
66    }
67    .into()
68}