-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add build() function to return the database build info (#2919)
* feat: add build function and register it build() function to return the database build info #2909 * refactor: fix typos and change code structure * test: add test for build() * refactor: cargo fmt and eliminate warnings * Apply suggestions from code review Co-authored-by: Weny Xu <[email protected]> * refactor: move system.sql to a new directory --------- Co-authored-by: Weny Xu <[email protected]>
- Loading branch information
1 parent
fec3fcf
commit 3d65152
Showing
30 changed files
with
187 additions
and
41 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright 2023 Greptime Team | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
pub mod build; | ||
|
||
use std::sync::Arc; | ||
|
||
use build::BuildFunction; | ||
|
||
use crate::function_registry::FunctionRegistry; | ||
|
||
pub(crate) struct SystemFunction; | ||
|
||
impl SystemFunction { | ||
pub fn register(registry: &FunctionRegistry) { | ||
registry.register(Arc::new(BuildFunction)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Copyright 2023 Greptime Team | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::fmt; | ||
use std::sync::Arc; | ||
|
||
use common_query::error::Result; | ||
use common_query::prelude::{Signature, Volatility}; | ||
use datatypes::prelude::*; | ||
use datatypes::vectors::{StringVector, VectorRef}; | ||
|
||
use crate::function::{Function, FunctionContext}; | ||
|
||
const DEFAULT_VALUE: &str = "unknown"; | ||
|
||
/// Generates build information | ||
#[derive(Clone, Debug, Default)] | ||
pub struct BuildFunction; | ||
|
||
impl fmt::Display for BuildFunction { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "BUILD") | ||
} | ||
} | ||
|
||
impl Function for BuildFunction { | ||
fn name(&self) -> &str { | ||
"build" | ||
} | ||
|
||
fn return_type(&self, _input_types: &[ConcreteDataType]) -> Result<ConcreteDataType> { | ||
Ok(ConcreteDataType::string_datatype()) | ||
} | ||
|
||
fn signature(&self) -> Signature { | ||
Signature::uniform( | ||
0, | ||
vec![ConcreteDataType::string_datatype()], | ||
Volatility::Immutable, | ||
) | ||
} | ||
|
||
fn eval(&self, _func_ctx: FunctionContext, _columns: &[VectorRef]) -> Result<VectorRef> { | ||
let build_info = format!( | ||
"branch: {}\ncommit: {}\ncommit short: {}\ndirty: {}\nversion: {}", | ||
build_data::get_git_branch().unwrap_or_else(|_| DEFAULT_VALUE.to_string()), | ||
build_data::get_git_commit().unwrap_or_else(|_| DEFAULT_VALUE.to_string()), | ||
build_data::get_git_commit_short().unwrap_or_else(|_| DEFAULT_VALUE.to_string()), | ||
build_data::get_git_dirty().map_or(DEFAULT_VALUE.to_string(), |v| v.to_string()), | ||
env!("CARGO_PKG_VERSION") | ||
); | ||
|
||
let v = Arc::new(StringVector::from(vec![build_info])); | ||
Ok(v) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::sync::Arc; | ||
|
||
use common_query::prelude::TypeSignature; | ||
|
||
use super::*; | ||
#[test] | ||
fn test_build_function() { | ||
let build = BuildFunction; | ||
assert_eq!("build", build.name()); | ||
assert_eq!( | ||
ConcreteDataType::string_datatype(), | ||
build.return_type(&[]).unwrap() | ||
); | ||
assert!(matches!(build.signature(), | ||
Signature { | ||
type_signature: TypeSignature::Uniform(0, valid_types), | ||
volatility: Volatility::Immutable | ||
} if valid_types == vec![ConcreteDataType::string_datatype()] | ||
)); | ||
let build_info = format!( | ||
"branch: {}\ncommit: {}\ncommit short: {}\ndirty: {}\nversion: {}", | ||
build_data::get_git_branch().unwrap_or_else(|_| DEFAULT_VALUE.to_string()), | ||
build_data::get_git_commit().unwrap_or_else(|_| DEFAULT_VALUE.to_string()), | ||
build_data::get_git_commit_short().unwrap_or_else(|_| DEFAULT_VALUE.to_string()), | ||
build_data::get_git_dirty().map_or(DEFAULT_VALUE.to_string(), |v| v.to_string()), | ||
env!("CARGO_PKG_VERSION") | ||
); | ||
let vector = build.eval(FunctionContext::default(), &[]).unwrap(); | ||
let expect: VectorRef = Arc::new(StringVector::from(vec![build_info])); | ||
assert_eq!(expect, vector); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.