diff --git a/src/common/building/src/lib.rs b/src/common/building/src/lib.rs index b4be7ba0f8d4b..41eaeb9d5619d 100644 --- a/src/common/building/src/lib.rs +++ b/src/common/building/src/lib.rs @@ -61,6 +61,8 @@ pub fn add_building_env_vars() { set_env_config().expect("Unable to generate build envs"); add_env_credits_info(); add_target_features(); + add_build_profile(); + add_opt_level(); add_env_version(); add_env_license(); add_license_public_key(); @@ -185,3 +187,35 @@ pub fn add_target_features() { } }; } + +pub fn add_build_profile() { + match env::var_os("PROFILE") { + Some(var) => match var.into_string() { + Ok(s) => println!("cargo:rustc-env=DATABEND_BUILD_PROFILE={}", s), + Err(_) => { + println!("cargo:warning=PROFILE was not valid utf-8"); + println!("cargo:rustc-env=DATABEND_BUILD_PROFILE=unknown"); + } + }, + None => { + println!("cargo:warning=PROFILE was not set"); + println!("cargo:rustc-env=DATABEND_BUILD_PROFILE=unknown"); + } + }; +} + +pub fn add_opt_level() { + match env::var_os("OPT_LEVEL") { + Some(var) => match var.into_string() { + Ok(s) => println!("cargo:rustc-env=DATABEND_OPT_LEVEL={}", s), + Err(_) => { + println!("cargo:warning=OPT_LEVEL was not valid utf-8"); + println!("cargo:rustc-env=DATABEND_OPT_LEVEL=unknown"); + } + }, + None => { + println!("cargo:warning=OPT_LEVEL was not set"); + println!("cargo:rustc-env=DATABEND_OPT_LEVEL=unknown"); + } + }; +} diff --git a/src/common/version/src/lib.rs b/src/common/version/src/lib.rs index 0bbe6cdf6afd2..21616cfc3b586 100644 --- a/src/common/version/src/lib.rs +++ b/src/common/version/src/lib.rs @@ -45,6 +45,10 @@ pub const DATABEND_ENTERPRISE_LICENSE_PUBLIC_KEY: &str = pub const DATABEND_CARGO_CFG_TARGET_FEATURE: &str = env!("DATABEND_CARGO_CFG_TARGET_FEATURE"); +pub const DATABEND_BUILD_PROFILE: &str = env!("DATABEND_BUILD_PROFILE"); + +pub const DATABEND_OPT_LEVEL: &str = env!("DATABEND_OPT_LEVEL"); + pub const DATABEND_TELEMETRY_ENDPOINT: &str = env!("DATABEND_TELEMETRY_ENDPOINT"); pub const DATABEND_TELEMETRY_API_KEY: &str = env!("DATABEND_TELEMETRY_API_KEY"); diff --git a/src/query/service/build.rs b/src/query/service/build.rs new file mode 100644 index 0000000000000..aaf4c53f857c6 --- /dev/null +++ b/src/query/service/build.rs @@ -0,0 +1,52 @@ +// Copyright 2021 Datafuse Labs +// +// 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::env; + +fn main() { + // Keep build script rerun behavior explicit for the feature list we expose. + println!("cargo:rerun-if-env-changed=PROFILE"); + println!("cargo:rerun-if-env-changed=OPT_LEVEL"); + + // Features declared in `src/query/service/Cargo.toml`. + println!("cargo:rerun-if-env-changed=CARGO_FEATURE_SIMD"); + println!("cargo:rerun-if-env-changed=CARGO_FEATURE_PYTHON_UDF"); + println!("cargo:rerun-if-env-changed=CARGO_FEATURE_DISABLE_INITIAL_EXEC_TLS"); + println!("cargo:rerun-if-env-changed=CARGO_FEATURE_JEMALLOC"); + println!("cargo:rerun-if-env-changed=CARGO_FEATURE_MEMORY_PROFILING"); + println!("cargo:rerun-if-env-changed=CARGO_FEATURE_STORAGE_HDFS"); + println!("cargo:rerun-if-env-changed=CARGO_FEATURE_IO_URING"); + + let mut features = env::vars() + .filter_map(|(k, v)| { + if !k.starts_with("CARGO_FEATURE_") { + return None; + } + if v != "1" { + return None; + } + let name = k + .trim_start_matches("CARGO_FEATURE_") + .to_ascii_lowercase() + .replace('_', "-"); + Some(name) + }) + .collect::>(); + + features.sort(); + features.dedup(); + + let features = features.join(","); + println!("cargo:rustc-env=DATABEND_QUERY_CARGO_FEATURES={features}"); +} diff --git a/src/query/service/src/databases/system/system_database.rs b/src/query/service/src/databases/system/system_database.rs index 0eeac29acf42f..0198d1e978d6a 100644 --- a/src/query/service/src/databases/system/system_database.rs +++ b/src/query/service/src/databases/system/system_database.rs @@ -73,12 +73,13 @@ use databend_common_storages_system::ViewsTableWithHistory; use databend_common_storages_system::ViewsTableWithoutHistory; use databend_common_storages_system::VirtualColumnsTable; use databend_common_storages_system::ZeroTable; +use databend_common_version::DATABEND_BUILD_PROFILE; use databend_common_version::DATABEND_CARGO_CFG_TARGET_FEATURE; use databend_common_version::DATABEND_COMMIT_AUTHORS; use databend_common_version::DATABEND_CREDITS_LICENSES; use databend_common_version::DATABEND_CREDITS_NAMES; use databend_common_version::DATABEND_CREDITS_VERSIONS; -use databend_common_version::VERGEN_CARGO_FEATURES; +use databend_common_version::DATABEND_OPT_LEVEL; use crate::catalogs::InMemoryMetas; use crate::databases::Database; @@ -89,6 +90,9 @@ pub struct SystemDatabase { db_info: DatabaseInfo, } +const DATABEND_QUERY_CARGO_FEATURES: Option<&'static str> = + option_env!("DATABEND_QUERY_CARGO_FEATURES"); + impl SystemDatabase { /// These tables may disabled to the sql users. fn disable_system_tables() -> HashMap { @@ -152,8 +156,10 @@ impl SystemDatabase { DatabasesTableWithHistory::create(sys_db_meta.next_table_id(), ctl_name), BuildOptionsTable::create( sys_db_meta.next_table_id(), - VERGEN_CARGO_FEATURES, + DATABEND_QUERY_CARGO_FEATURES, DATABEND_CARGO_CFG_TARGET_FEATURE, + DATABEND_BUILD_PROFILE, + DATABEND_OPT_LEVEL, ), QueryCacheTable::create(sys_db_meta.next_table_id()), TableFunctionsTable::create(sys_db_meta.next_table_id()), diff --git a/src/query/service/tests/it/storages/system.rs b/src/query/service/tests/it/storages/system.rs index bb22fce3c1cb8..d6d7bd7f00eaa 100644 --- a/src/query/service/tests/it/storages/system.rs +++ b/src/query/service/tests/it/storages/system.rs @@ -50,12 +50,13 @@ use databend_common_storages_system::MetricsTable; use databend_common_storages_system::RolesTable; use databend_common_storages_system::UsersTable; use databend_common_users::UserApiProvider; +use databend_common_version::DATABEND_BUILD_PROFILE; use databend_common_version::DATABEND_CARGO_CFG_TARGET_FEATURE; use databend_common_version::DATABEND_COMMIT_AUTHORS; use databend_common_version::DATABEND_CREDITS_LICENSES; use databend_common_version::DATABEND_CREDITS_NAMES; use databend_common_version::DATABEND_CREDITS_VERSIONS; -use databend_common_version::VERGEN_CARGO_FEATURES; +use databend_common_version::DATABEND_OPT_LEVEL; use databend_query::sessions::QueryContext; use databend_query::sessions::TableContext; use databend_query::stream::ReadDataBlockStream; @@ -113,8 +114,13 @@ async fn test_build_options_table() -> Result<()> { let fixture = TestFixture::setup().await?; let ctx = fixture.new_query_ctx().await?; - let table = - BuildOptionsTable::create(1, VERGEN_CARGO_FEATURES, DATABEND_CARGO_CFG_TARGET_FEATURE); + let table = BuildOptionsTable::create( + 1, + option_env!("DATABEND_QUERY_CARGO_FEATURES"), + DATABEND_CARGO_CFG_TARGET_FEATURE, + DATABEND_BUILD_PROFILE, + DATABEND_OPT_LEVEL, + ); let source_plan = table .read_plan(ctx.clone(), None, None, false, true) .await?; @@ -122,8 +128,8 @@ async fn test_build_options_table() -> Result<()> { let stream = table.read_data_block_stream(ctx, &source_plan).await?; let result = stream.try_collect::>().await?; let block = &result[0]; - assert_eq!(block.num_columns(), 2); - assert!(block.num_rows() > 0); + assert_eq!(block.num_columns(), 3); + assert!(block.num_rows() >= 4); Ok(()) } diff --git a/src/query/service/tests/it/storages/testdata/columns_table.txt b/src/query/service/tests/it/storages/testdata/columns_table.txt index 868d524673052..c146627997f53 100644 --- a/src/query/service/tests/it/storages/testdata/columns_table.txt +++ b/src/query/service/tests/it/storages/testdata/columns_table.txt @@ -24,7 +24,6 @@ DB.Table: 'system'.'columns', Table: columns-table_id:1, ver:0, Engine: SystemCo | 'byte_size' | 'system' | 'clustering_history' | 'UInt64' | 'BIGINT UNSIGNED' | '' | '' | 'NO' | '' | | 'capacity' | 'system' | 'caches' | 'UInt64' | 'BIGINT UNSIGNED' | '' | '' | 'NO' | '' | | 'cardinality' | 'information_schema' | 'statistics' | 'NULL' | 'NULL' | '' | '' | 'NO' | '' | -| 'cargo_features' | 'system' | 'build_options' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' | | 'catalog' | 'system' | 'databases' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' | | 'catalog' | 'system' | 'databases_with_history' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' | | 'catalog' | 'system' | 'streams' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' | @@ -34,6 +33,7 @@ DB.Table: 'system'.'columns', Table: columns-table_id:1, ver:0, Engine: SystemCo | 'catalog' | 'system' | 'views' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' | | 'catalog' | 'system' | 'views_with_history' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' | | 'catalog_name' | 'information_schema' | 'schemata' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' | +| 'category' | 'system' | 'build_options' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' | | 'character_maximum_length' | 'information_schema' | 'columns' | 'Nullable(UInt16)' | 'SMALLINT UNSIGNED' | '' | '' | 'YES' | '' | | 'character_octet_length' | 'information_schema' | 'columns' | 'Nullable(UInt16)' | 'SMALLINT UNSIGNED' | '' | '' | 'YES' | '' | | 'character_set_catalog' | 'information_schema' | 'columns' | 'NULL' | 'NULL' | '' | '' | 'NO' | '' | @@ -265,6 +265,7 @@ DB.Table: 'system'.'columns', Table: columns-table_id:1, ver:0, Engine: SystemCo | 'mode' | 'system' | 'streams_terse' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' | | 'must_change_password' | 'system' | 'users' | 'Nullable(Boolean)' | 'BOOLEAN' | '' | '' | 'YES' | '' | | 'mysql_connection_id' | 'system' | 'processes' | 'Nullable(UInt32)' | 'INT UNSIGNED' | '' | '' | 'YES' | '' | +| 'name' | 'system' | 'build_options' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' | | 'name' | 'system' | 'caches' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' | | 'name' | 'system' | 'catalogs' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' | | 'name' | 'system' | 'clusters' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' | @@ -445,7 +446,6 @@ DB.Table: 'system'.'columns', Table: columns-table_id:1, ver:0, Engine: SystemCo | 'table_type' | 'system' | 'tables' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' | | 'table_type' | 'system' | 'tables_with_history' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' | | 'table_version' | 'system' | 'streams' | 'Nullable(UInt64)' | 'BIGINT UNSIGNED' | '' | '' | 'YES' | '' | -| 'target_features' | 'system' | 'build_options' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' | | 'time' | 'system' | 'processes' | 'UInt64' | 'BIGINT UNSIGNED' | '' | '' | 'NO' | '' | | 'timestamp' | 'system' | 'query_execution' | 'Timestamp' | 'TIMESTAMP' | '' | '' | 'NO' | '' | | 'total_columns' | 'system' | 'tables' | 'UInt64' | 'BIGINT UNSIGNED' | '' | '' | 'NO' | '' | @@ -478,6 +478,7 @@ DB.Table: 'system'.'columns', Table: columns-table_id:1, ver:0, Engine: SystemCo | 'user' | 'system' | 'locks' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' | | 'user' | 'system' | 'processes' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' | | 'user' | 'system' | 'temporary_tables' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' | +| 'value' | 'system' | 'build_options' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' | | 'value' | 'system' | 'configs' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' | | 'value' | 'system' | 'malloc_stats_totals' | 'UInt64' | 'BIGINT UNSIGNED' | '' | '' | 'NO' | '' | | 'value' | 'system' | 'metrics' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' | @@ -499,5 +500,3 @@ DB.Table: 'system'.'columns', Table: columns-table_id:1, ver:0, Engine: SystemCo | 'webhook_options' | 'system' | 'notifications' | 'Nullable(Variant)' | 'VARIANT' | '' | '' | 'YES' | '' | | 'workload_groups' | 'system' | 'users' | 'Nullable(String)' | 'VARCHAR' | '' | '' | 'YES' | '' | +-----------------------------------+----------------------+---------------------------+-----------------------+---------------------+----------+----------+----------+----------+ - - diff --git a/src/query/storages/system/src/build_options_table.rs b/src/query/storages/system/src/build_options_table.rs index 798e8a61935c9..f4698a3f289c3 100644 --- a/src/query/storages/system/src/build_options_table.rs +++ b/src/query/storages/system/src/build_options_table.rs @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::cmp::max; use std::sync::Arc; use databend_common_catalog::table::Table; @@ -53,10 +52,13 @@ impl BuildOptionsTable { table_id: u64, cargo_features: Option<&str>, target_features: &str, + build_profile: &str, + opt_level: &str, ) -> Arc { let schema = TableSchemaRefExt::create(vec![ - TableField::new("cargo_features", TableDataType::String), - TableField::new("target_features", TableDataType::String), + TableField::new("category", TableDataType::String), + TableField::new("name", TableDataType::String), + TableField::new("value", TableDataType::String), ]); let table_info = TableInfo { @@ -71,28 +73,42 @@ impl BuildOptionsTable { ..Default::default() }; - let mut cargo_features = if let Some(features) = cargo_features { - features - .split_terminator(',') - .map(|x| x.trim().to_string()) - .collect::>() + let cargo_features = cargo_features + .map(|s| s.trim()) + .filter(|s| !s.is_empty()) + .unwrap_or("not available"); + + let target_features = target_features.trim(); + let target_features = if target_features.is_empty() { + "not available" } else { - vec!["not available".to_string()] + target_features }; - let mut target_features: Vec = target_features - .split_terminator(',') - .map(|x| x.trim().to_string()) - .collect(); + let mut category = Vec::with_capacity(4); + let mut name = Vec::with_capacity(4); + let mut value = Vec::with_capacity(4); + + category.push("cargo".to_string()); + name.push("features".to_string()); + value.push(cargo_features.to_string()); + + category.push("target".to_string()); + name.push("features".to_string()); + value.push(target_features.to_string()); - let length = max(cargo_features.len(), target_features.len()); + category.push("cargo".to_string()); + name.push("build_profile".to_string()); + value.push(build_profile.to_string()); - cargo_features.resize(length, "".to_string()); - target_features.resize(length, "".to_string()); + category.push("cargo".to_string()); + name.push("opt_level".to_string()); + value.push(opt_level.to_string()); let data = DataBlock::new_from_columns(vec![ - StringType::from_data(cargo_features), - StringType::from_data(target_features), + StringType::from_data(category), + StringType::from_data(name), + StringType::from_data(value), ]); SyncOneBlockSystemTable::create(BuildOptionsTable { table_info, data })