common_function/system/pg_catalog/
pg_get_userbyid.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 std::fmt::{self};
16use std::sync::Arc;
17
18use common_query::error::Result;
19use common_query::prelude::{Signature, Volatility};
20use datatypes::prelude::{ConcreteDataType, DataType, VectorRef};
21use datatypes::types::LogicalPrimitiveType;
22use datatypes::with_match_primitive_type_id;
23use num_traits::AsPrimitive;
24
25use crate::function::{Function, FunctionContext};
26use crate::scalars::expression::{scalar_unary_op, EvalContext};
27
28#[derive(Clone, Debug, Default)]
29pub struct PGGetUserByIdFunction;
30
31const NAME: &str = crate::pg_catalog_func_fullname!("pg_get_userbyid");
32
33impl fmt::Display for PGGetUserByIdFunction {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        write!(f, crate::pg_catalog_func_fullname!("PG_GET_USERBYID"))
36    }
37}
38
39impl Function for PGGetUserByIdFunction {
40    fn name(&self) -> &str {
41        NAME
42    }
43
44    fn return_type(&self, _input_types: &[ConcreteDataType]) -> Result<ConcreteDataType> {
45        Ok(ConcreteDataType::string_datatype())
46    }
47
48    fn signature(&self) -> Signature {
49        Signature::uniform(
50            1,
51            vec![ConcreteDataType::uint32_datatype()],
52            Volatility::Immutable,
53        )
54    }
55
56    fn eval(&self, _func_ctx: &FunctionContext, columns: &[VectorRef]) -> Result<VectorRef> {
57        with_match_primitive_type_id!(columns[0].data_type().logical_type_id(), |$T| {
58            let col = scalar_unary_op::<<$T as LogicalPrimitiveType>::Native, String, _>(&columns[0], pg_get_user_by_id, &mut EvalContext::default())?;
59            Ok(Arc::new(col))
60        }, {
61            unreachable!()
62        })
63    }
64}
65
66fn pg_get_user_by_id<I>(table_oid: Option<I>, _ctx: &mut EvalContext) -> Option<String>
67where
68    I: AsPrimitive<u32>,
69{
70    // TODO(J0HN50N133): We lack way to get the user_info by a numeric value. Once we have it, we can implement this function.
71    table_oid.map(|_| "".to_string())
72}