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