Skip to content

Commit

Permalink
register
Browse files Browse the repository at this point in the history
Signed-off-by: coldWater <[email protected]>
  • Loading branch information
forsaken628 committed Sep 24, 2024
1 parent c3925e7 commit 9b6b0ac
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 32 deletions.
57 changes: 25 additions & 32 deletions src/common/io/src/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ enum NumFlag {
Multi,
PlusPost,
MinusPost,
EEEE,
Eeee,
}

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -173,7 +173,7 @@ struct NumDesc {
impl NumDesc {
fn prepare(&mut self, n: &FormatNode) -> std::result::Result<(), &'static str> {
if let FormatNode::Action(key) = n {
if self.flag.contains(NumFlag::EEEE) && !matches!(key.id, NumPoz::TkE) {
if self.flag.contains(NumFlag::Eeee) && !matches!(key.id, NumPoz::TkE) {
return Err("\"EEEE\" must be the last pattern used");
}

Expand All @@ -193,7 +193,7 @@ impl NumDesc {
} else {
self.pre += 1;
}
return Ok(());
Ok(())
}

NumPoz::Tk0 => {
Expand Down Expand Up @@ -341,7 +341,7 @@ impl NumDesc {
}

NumPoz::TkE => {
if self.flag.contains(NumFlag::EEEE) {
if self.flag.contains(NumFlag::Eeee) {
return Err("cannot use \"EEEE\" twice");
}

Expand All @@ -360,7 +360,7 @@ impl NumDesc {
);
}

self.flag.insert(NumFlag::EEEE);
self.flag.insert(NumFlag::Eeee);
Ok(())
}

Expand Down Expand Up @@ -484,35 +484,28 @@ impl NumProc {
} else {
// Write Decimal point
if self.number.get(self.number_p).is_some_and(|c| *c == '.') {
if !self.last_relevant_is_dot() {
self.inout.push_str(&self.decimal) /* Write DEC/D */
}
// Ora 'n' -- FM9.9 --> 'n.'
else if self.desc.flag.contains(NumFlag::FillMode)
&& self.last_relevant_is_dot()
if !self.last_relevant_is_dot()
|| self.desc.flag.contains(NumFlag::FillMode) && self.last_relevant_is_dot()
// Ora 'n' -- FM9.9 --> 'n.'s
{
self.inout.push_str(&self.decimal) /* Write DEC/D */
}
} else {
if self.last_relevant.is_some_and(|(_, i)| self.number_p > i)
&& !matches!(id, NumPoz::Tk0)
{
} else if self.last_relevant.is_some_and(|(_, i)| self.number_p > i)
&& !matches!(id, NumPoz::Tk0)
{
}
// '0.1' -- 9.9 --> ' .1'
else if self.is_predec_space() {
if self.desc.flag.contains(NumFlag::FillMode) {
self.inout.push(' ');
}
// '0.1' -- 9.9 --> ' .1'
else if self.is_predec_space() {
if self.desc.flag.contains(NumFlag::FillMode) {
self.inout.push(' ');
}
// '0' -- FM9.9 --> '0.'
else if self.last_relevant_is_dot() {
self.inout.push('0')
}
} else {
if self.number_p < self.number.len() {
self.inout.push(self.number[self.number_p]); /* Write DIGIT */
self.num_in = true
}
// '0' -- FM9.9 --> '0.'
else if self.last_relevant_is_dot() {
self.inout.push('0')
}
} else if self.number_p < self.number.len() {
self.inout.push(self.number[self.number_p]); /* Write DIGIT */
self.num_in = true
}
if self.number_p < self.number.len() {
self.number_p += 1;
Expand Down Expand Up @@ -567,7 +560,7 @@ impl NumProc {
_ => {}
}
}
self.last_relevant = n.map(|n| (*self.number.iter().nth(n).unwrap(), n));
self.last_relevant = n.map(|n| (*self.number.get(n).unwrap(), n));
}

fn last_relevant_is_dot(&self) -> bool {
Expand Down Expand Up @@ -689,14 +682,14 @@ fn num_processor(
np.inout
}

fn i32_to_char(value: i32, fmt: &str) -> Result<String> {
pub fn i32_to_char(value: i32, fmt: &str) -> Result<String> {
let mut desc = NumDesc::default();
let nodes = parse_format(fmt, &NUM_KEYWORDS, Some(&mut desc))?;

let sign = value >= 0;
let (numstr, out_pre_spaces) = if desc.flag.contains(NumFlag::Roman) {
unimplemented!()
} else if desc.flag.contains(NumFlag::EEEE) {
} else if desc.flag.contains(NumFlag::Eeee) {
// we can do it easily because f32 won't lose any precision
let orgnum = format!("{:+.*e}", desc.post, value as f32);

Expand Down
32 changes: 32 additions & 0 deletions src/query/functions/src/scalars/other.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use databend_common_expression::types::ArgType;
use databend_common_expression::types::DataType;
use databend_common_expression::types::DateType;
use databend_common_expression::types::GenericType;
use databend_common_expression::types::Int32Type;
use databend_common_expression::types::NullType;
use databend_common_expression::types::NullableType;
use databend_common_expression::types::NumberColumn;
Expand All @@ -45,6 +46,7 @@ use databend_common_expression::types::StringType;
use databend_common_expression::types::TimestampType;
use databend_common_expression::types::ValueType;
use databend_common_expression::vectorize_with_builder_1_arg;
use databend_common_expression::vectorize_with_builder_2_arg;
use databend_common_expression::Column;
use databend_common_expression::Domain;
use databend_common_expression::EvalContext;
Expand All @@ -58,6 +60,7 @@ use databend_common_expression::Scalar;
use databend_common_expression::ScalarRef;
use databend_common_expression::Value;
use databend_common_expression::ValueRef;
use databend_common_io::number::i32_to_char;
use rand::Rng;
use rand::SeedableRng;

Expand All @@ -73,6 +76,7 @@ pub fn register(registry: &mut FunctionRegistry) {
register_inet_ntoa(registry);
register_run_diff(registry);
register_grouping(registry);
register_num_to_char(registry);

registry.properties.insert(
"rand".to_string(),
Expand Down Expand Up @@ -384,6 +388,34 @@ fn register_grouping(registry: &mut FunctionRegistry) {
})
}

fn register_num_to_char(registry: &mut FunctionRegistry) {
registry.register_passthrough_nullable_2_arg::<Int32Type, StringType, StringType, _, _>(
"to_char",
|_, _, _| FunctionDomain::MayThrow,
vectorize_with_builder_2_arg::<Int32Type, StringType, StringType>(
|value, fmt, builder, ctx| {
if let Some(validity) = &ctx.validity {
if !validity.get_bit(builder.len()) {
builder.commit_row();
return;
}
}

match i32_to_char(value, fmt) {
Ok(s) => {
builder.put_str(&s);
builder.commit_row()
}
Err(e) => {
ctx.set_error(builder.len(), e.to_string());
builder.commit_row()
}
}
},
),
)
}

/// Compute `grouping` by `grouping_id` and `cols`.
///
/// `cols` are indices of the column represented in `_grouping_id`.
Expand Down

0 comments on commit 9b6b0ac

Please sign in to comment.