Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce tipb_helper #4634

Merged
merged 7 commits into from May 8, 2019

Some generated files are not rendered by default. Learn more.

@@ -119,6 +119,7 @@ twoway = "0.2.0"
profiler = { path = "components/profiler" }
cop_datatype = { path = "components/cop_datatype" }
cop_codegen = { path = "components/cop_codegen" }
tipb_helper = { path = "components/tipb_helper" }
panic_hook = { path = "components/panic_hook" }
tipb = { git = "https://github.com/pingcap/tipb.git" }
kvproto = { git = "https://github.com/pingcap/kvproto.git" }
@@ -169,6 +170,7 @@ members = [
"components/panic_hook",
"components/cop_datatype",
"components/cop_codegen",
"components/tipb_helper",
"components/log_wrappers",
"components/tikv_util",
]
@@ -0,0 +1,10 @@
[package]
name = "tipb_helper"
version = "0.0.1"
edition = "2018"
publish = false

[dependencies]
tipb = { git = "https://github.com/pingcap/tipb.git" }
codec = { path = "../codec" }
cop_datatype = { path = "../cop_datatype" }
@@ -0,0 +1,97 @@
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.

use codec::prelude::NumberEncoder;
use cop_datatype::{FieldTypeAccessor, FieldTypeFlag, FieldTypeTp};
use tipb::expression::{Expr, ExprType, FieldType, ScalarFuncSig};

/// A helper utility to build `tipb::expression::Expr` (a.k.a. expression definition) easily.
pub struct ExprDefinitionBuilder(Expr);

impl ExprDefinitionBuilder {
pub fn constant_int(v: i64) -> Self {
let mut expr = Expr::new();
expr.set_tp(ExprType::Int64);
expr.mut_val().write_i64(v).unwrap();
expr.mut_field_type()
.as_mut_accessor()
.set_tp(FieldTypeTp::LongLong);
Self(expr)
}

pub fn constant_uint(v: u64) -> Self {
let mut expr = Expr::new();
expr.set_tp(ExprType::Uint64);
expr.mut_val().write_u64(v).unwrap();
expr.mut_field_type()
.as_mut_accessor()
.set_tp(FieldTypeTp::LongLong)
.set_flag(FieldTypeFlag::UNSIGNED);
Self(expr)
}

pub fn constant_real(v: f64) -> Self {
let mut expr = Expr::new();
expr.set_tp(ExprType::Float64);
expr.mut_val().write_f64(v).unwrap();
expr.mut_field_type()
.as_mut_accessor()
.set_tp(FieldTypeTp::Double);
Self(expr)
}

pub fn constant_bytes(v: Vec<u8>) -> Self {
let mut expr = Expr::new();
expr.set_tp(ExprType::String);
expr.set_val(v);
expr.mut_field_type()
.as_mut_accessor()
.set_tp(FieldTypeTp::VarChar);
Self(expr)
}

pub fn constant_null(field_type: impl Into<FieldType>) -> Self {
let mut expr = Expr::new();
expr.set_tp(ExprType::Null);
expr.set_field_type(field_type.into());
Self(expr)
}

pub fn column_ref(offset: usize, field_type: impl Into<FieldType>) -> Self {
let mut expr = Expr::new();
expr.set_tp(ExprType::ColumnRef);
expr.mut_val().write_i64(offset as i64).unwrap();
expr.set_field_type(field_type.into());
Self(expr)
}

pub fn scalar_func(sig: ScalarFuncSig, field_type: impl Into<FieldType>) -> Self {
let mut expr = Expr::new();
expr.set_tp(ExprType::ScalarFunc);
expr.set_sig(sig);
expr.set_field_type(field_type.into());
Self(expr)
}

pub fn aggr_func(tp: ExprType, field_type: impl Into<FieldType>) -> Self {
let mut expr = Expr::new();
expr.set_tp(tp);
expr.set_field_type(field_type.into());
Self(expr)
}

pub fn push(mut self, child: impl Into<Expr>) -> Self {
This conversation was marked as resolved by breeswish

This comment has been minimized.

Copy link
@AndreMouche

AndreMouche May 7, 2019

Member

add_child?

This comment has been minimized.

Copy link
@breeswish

breeswish May 7, 2019

Author Member

How about push_child?

self.0.mut_children().push(child.into());
self
}

/// Builds the expression definition.
pub fn build(self) -> Expr {
self.0
}
}

impl Into<Expr> for ExprDefinitionBuilder {
fn into(self) -> Expr {
self.build()
}
}
@@ -0,0 +1,5 @@
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.

mod expr_def_builder;

pub use self::expr_def_builder::ExprDefinitionBuilder;
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.