Skip to content

Commit

Permalink
cast_f32_to_string impl
Browse files Browse the repository at this point in the history
Signed-off-by: Petros Angelatos <petrosagg@gmail.com>
  • Loading branch information
petrosagg committed Sep 14, 2021
1 parent b42934c commit 88a1443
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
21 changes: 8 additions & 13 deletions src/expr/src/scalar/func.rs
Expand Up @@ -273,12 +273,6 @@ fn cast_int64_to_string<'a>(a: Datum<'a>, temp_storage: &'a RowArena) -> Datum<'
Datum::String(temp_storage.push_string(buf))
}

fn cast_float32_to_string<'a>(a: Datum<'a>, temp_storage: &'a RowArena) -> Datum<'a> {
let mut buf = String::new();
strconv::format_float32(&mut buf, a.unwrap_float32());
Datum::String(temp_storage.push_string(buf))
}

fn cast_float32_to_numeric<'a>(a: Datum<'a>, scale: Option<u8>) -> Result<Datum<'a>, EvalError> {
let a = a.unwrap_float32();
if a.is_infinite() {
Expand Down Expand Up @@ -3451,7 +3445,7 @@ pub enum UnaryFunc {
CastFloat32ToInt32(CastFloat32ToInt32),
CastFloat32ToInt64(CastFloat32ToInt64),
CastFloat32ToFloat64(CastFloat32ToFloat64),
CastFloat32ToString,
CastFloat32ToString(CastFloat32ToString),
CastFloat32ToNumeric(Option<u8>),
CastFloat64ToNumeric(Option<u8>),
CastFloat64ToInt16(CastFloat64ToInt16),
Expand Down Expand Up @@ -3640,7 +3634,8 @@ derive_unary!(
CastFloat64ToInt32,
CastFloat64ToInt64,
CastFloat32ToFloat64,
CastFloat64ToFloat32
CastFloat64ToFloat32,
CastFloat32ToString
);

impl UnaryFunc {
Expand Down Expand Up @@ -3675,6 +3670,7 @@ impl UnaryFunc {
| CastFloat64ToInt32(_)
| CastFloat64ToInt64(_)
| CastFloat64ToFloat32(_)
| CastFloat32ToString(_)
| CastFloat32ToFloat64(_) => unreachable!(),
IsNull => Ok(is_null(a)),
NegInt16 => Ok(neg_int16(a)),
Expand Down Expand Up @@ -3716,7 +3712,6 @@ impl UnaryFunc {
CastInt64ToFloat32 => Ok(cast_int64_to_float32(a)),
CastInt64ToFloat64 => Ok(cast_int64_to_float64(a)),
CastInt64ToString => Ok(cast_int64_to_string(a, temp_storage)),
CastFloat32ToString => Ok(cast_float32_to_string(a, temp_storage)),
CastFloat64ToString => Ok(cast_float64_to_string(a, temp_storage)),
CastStringToBool => cast_string_to_bool(a),
CastStringToBytes => cast_string_to_bytes(a, temp_storage),
Expand Down Expand Up @@ -3872,6 +3867,7 @@ impl UnaryFunc {
| CastFloat64ToInt32(_)
| CastFloat64ToInt64(_)
| CastFloat64ToFloat32(_)
| CastFloat32ToString(_)
| CastFloat32ToFloat64(_) => unreachable!(),
IsNull => ScalarType::Bool.nullable(nullable),

Expand All @@ -3894,7 +3890,6 @@ impl UnaryFunc {
| CastInt16ToString
| CastInt32ToString
| CastInt64ToString
| CastFloat32ToString
| CastFloat64ToString
| CastNumericToString
| CastDateToString
Expand Down Expand Up @@ -4073,6 +4068,7 @@ impl UnaryFunc {
| CastFloat64ToInt32(_)
| CastFloat64ToInt64(_)
| CastFloat64ToFloat32(_)
| CastFloat32ToString(_)
| CastFloat32ToFloat64(_) => unreachable!(),
// These return null when their input is SQL null.
CastJsonbToString | CastJsonbToInt16 | CastJsonbToInt32 | CastJsonbToInt64
Expand Down Expand Up @@ -4105,7 +4101,6 @@ impl UnaryFunc {
| CastInt16ToString
| CastInt32ToString
| CastInt64ToString
| CastFloat32ToString
| CastFloat64ToString
| CastNumericToString
| CastDateToString
Expand Down Expand Up @@ -4196,6 +4191,7 @@ impl UnaryFunc {
| CastFloat64ToInt32(_)
| CastFloat64ToInt64(_)
| CastFloat64ToFloat32(_)
| CastFloat32ToString(_)
| CastFloat32ToFloat64(_) => unreachable!(),
NegInt16
| NegInt32
Expand All @@ -4212,7 +4208,6 @@ impl UnaryFunc {
| CastInt32ToInt64
| CastInt32ToString
| CastInt64ToString
| CastFloat32ToString
| CastFloat64ToString
| CastStringToBytes
| CastDateToTimestamp
Expand Down Expand Up @@ -4245,6 +4240,7 @@ impl UnaryFunc {
| CastFloat64ToInt32(_)
| CastFloat64ToInt64(_)
| CastFloat64ToFloat32(_)
| CastFloat32ToString(_)
| CastFloat32ToFloat64(_) => unreachable!(),
IsNull => f.write_str("isnull"),
NegInt16 => f.write_str("-"),
Expand Down Expand Up @@ -4284,7 +4280,6 @@ impl UnaryFunc {
CastInt64ToFloat32 => f.write_str("i64tof32"),
CastInt64ToFloat64 => f.write_str("i64tof64"),
CastInt64ToString => f.write_str("i64tostr"),
CastFloat32ToString => f.write_str("f32tostr"),
CastFloat32ToNumeric(_) => f.write_str("f32tonumeric"),
CastFloat64ToString => f.write_str("f64tostr"),
CastFloat64ToNumeric(_) => f.write_str("f32tonumeric"),
Expand Down
10 changes: 10 additions & 0 deletions src/expr/src/scalar/func/impls/float.rs
Expand Up @@ -8,6 +8,7 @@
// by the Apache License, Version 2.0.

use crate::EvalError;
use repr::strconv;

sqlfunc!(
#[sqlname = "-"]
Expand Down Expand Up @@ -199,3 +200,12 @@ sqlfunc!(
a.into()
}
);

sqlfunc!(
#[sqlname = "f32tostr"]
fn cast_float32_to_string(a: f32) -> String {
let mut s = String::new();
strconv::format_float32(&mut s, a);
s
}
);
2 changes: 1 addition & 1 deletion src/sql/src/plan/typeconv.rs
Expand Up @@ -183,7 +183,7 @@ lazy_static! {
let s = to_type.unwrap_numeric_scale();
Some(move |e: HirScalarExpr| e.call_unary(CastFloat32ToNumeric(s)))
}),
(Float32, String) => Assignment: CastFloat32ToString,
(Float32, String) => Assignment: CastFloat32ToString(func::CastFloat32ToString),

// FLOAT64
(Float64, Int16) => Assignment: CastFloat64ToInt16(func::CastFloat64ToInt16),
Expand Down

0 comments on commit 88a1443

Please sign in to comment.