Skip to content

Commit

Permalink
feat: use thrift to generate hms client stub code
Browse files Browse the repository at this point in the history
  • Loading branch information
dantengsky committed Apr 27, 2022
1 parent 9ee5ed4 commit a1cf11e
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 74,839 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ members = [
"common/codegen",
"common/tracing",
"common/metrics",
"common/hive-meta-store",

# Query
"query",
Expand Down
24 changes: 24 additions & 0 deletions common/hive-meta-store/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "common-hive-meta-store"
version = "0.1.0"
authors = ["Databend Authors <[email protected]>"]
license = "Apache-2.0"
publish = false
edition = "2021"

[lib]
doctest = false
test = false

[dependencies] # In alphabetical order
# Workspace dependencies
common-base = { path = "../base" }
common-exception = { path = "../exception" }
common-tracing = { path = "../tracing" }

# Github dependencies

# Crates.io dependencies
thrift = "0.15"

[build-dependencies]
69 changes: 69 additions & 0 deletions common/hive-meta-store/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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;
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use std::io::Write;
use std::path::Path;
use std::process::Command;

#[allow(clippy::expect_fun_call)]
fn main() {
let out_dir = env::var_os("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir);

// thrift -out my_rust_program/src --gen rs -r Tutorial.thrift
Command::new("thrift")
.args(&[
"-out",
&dest_path.as_os_str().to_string_lossy(),
"-gen",
"rs",
"-r",
"src/idl/hms.thrift",
])
.status()
.unwrap();

// unfortunately, the code that thrift generated contains attributes attributes
// which will prevent us from using `include!` macro to embed the codes.
//
// If we `include!` the code generated directly, rustc will reports errors like this:
//
// "error: an inner attribute is not permitted following an outer attribute"
//
// see also:
// 1. https://github.com/rust-lang/rfcs/issues/752
// 2. https://github.com/google/flatbuffers/pull/6410
//
// thus, we have to "patch" the code that thrift generated.

let input_file_path = dest_path.join("hms.rs");
let output_file_path = dest_path.join("hms_patched.rs");
let input = BufReader::new(
File::open(&input_file_path)
.expect(format!("open generated file failure: {:?}", input_file_path).as_str()),
);
let mut output = File::create(output_file_path).expect("create output patch file failure");
for line in input.lines() {
let line = line.expect("readline failure");
if !line.starts_with("#![") {
std::writeln!(output, "{}", line).expect("write line to patched file failure");
}
}

println!("cargo:rerun-if-changed=build.rs");
}
15 changes: 15 additions & 0 deletions common/hive-meta-store/src/hive_meta_store.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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.

include!(concat!(env!("OUT_DIR"), "/hms_patched.rs"));
29 changes: 29 additions & 0 deletions common/hive-meta-store/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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.

#![allow(clippy::all)]
#![allow(dead_code)]
#![allow(unreachable_patterns)]
#![allow(unused_imports)]
#![allow(unused_variables)]
#![allow(unused_extern_crates)]
#![allow(deprecated)]
#![allow(clippy::too_many_arguments, clippy::type_complexity, clippy::vec_box)]
#![cfg_attr(rustfmt, rustfmt_skip)]
mod hive_meta_store;

pub use hive_meta_store::TThriftHiveMetastoreSyncClient;
pub use hive_meta_store::ThriftHiveMetastoreSyncClient;
pub use hive_meta_store::*;
pub use thrift;
1 change: 1 addition & 0 deletions query/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ common-datavalues = { path = "../common/datavalues" }
common-exception = { path = "../common/exception" }
common-functions = { path = "../common/functions" }
common-grpc = { path = "../common/grpc" }
common-hive-meta-store = { path = "../common/hive-meta-store" }
common-infallible = { path = "../common/infallible" }
common-io = { path = "../common/io" }
common-macros = { path = "../common/macros" }
Expand Down
2 changes: 1 addition & 1 deletion query/src/catalogs/hive/converters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ use common_datavalues::DataField;
use common_datavalues::DataSchema;
use common_datavalues::UInt8Type;
use common_exception::Result;
use common_hive_meta_store as hms;
use common_meta_types::*;

use super::hive_database::HiveDatabase;
use super::hive_database::HIVE_DATABASE_ENGIE;
use super::hive_meta_store as hms;
use super::hive_table::HIVE_TABLE_ENGIE;

impl From<hms::Database> for HiveDatabase {
Expand Down
Loading

0 comments on commit a1cf11e

Please sign in to comment.