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

refactor: Remove dependency to ordered-float #263

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ mod helper;
use std::convert::identity;

use anyhow::{anyhow, Context};
use ordered_float::OrderedFloat;
use pest::{iterators::Pairs, Parser};

use crate::{
Expand Down Expand Up @@ -151,7 +150,7 @@ impl PestParserImpl {
let s = self_as_str(&mut params);

s.parse::<f32>()
.map(|f| SqlValue::NotNull(NnSqlValue::Float(OrderedFloat(f))))
.map(|f| SqlValue::NotNull(NnSqlValue::Float(f)))
.map_err(|_e| {
SpringError::Sql(anyhow!(
"float value `{}` could not be parsed as f32 (max supported size)",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// This file is part of https://github.com/SpringQL/SpringQL which is licensed under MIT OR Apache-2.0. See file LICENSE-MIT or LICENSE-APACHE for full license details.

use ordered_float::OrderedFloat;

use crate::{
api::error::Result,
stream_engine::autonomous_executor::row::value::{
Expand All @@ -27,7 +25,7 @@ impl SpringValue for f32 {

impl ToNnSqlValue for f32 {
fn into_nn_sql_value(self) -> NnSqlValue {
NnSqlValue::Float(OrderedFloat(self))
NnSqlValue::Float(self)
}
}

Expand All @@ -45,7 +43,7 @@ mod tests_f32 {
let rust_values = vec![0f32, 1., -1., f32::MAX, f32::MIN, f32::NAN];

for v in rust_values {
let sql_value = NnSqlValue::Float(OrderedFloat(v));
let sql_value = NnSqlValue::Float(v);
let unpacked: f32 = sql_value.unpack()?;
if v.is_nan() {
assert!(unpacked.is_nan());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use std::{
};

use anyhow::anyhow;
use ordered_float::OrderedFloat;

use crate::{
api::error::{Result, SpringError},
Expand Down Expand Up @@ -187,7 +186,7 @@ impl TryFrom<&serde_json::Value> for SqlValue {

serde_json::Value::Number(n) => {
if let Some(f) = n.as_f64() {
Ok(SqlValue::NotNull(NnSqlValue::Float(OrderedFloat(f as f32))))
Ok(SqlValue::NotNull(NnSqlValue::Float(f as f32)))
} else if let Some(i) = n.as_i64() {
Ok(SqlValue::NotNull(NnSqlValue::BigInt(i)))
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use std::{
};

use anyhow::anyhow;
use ordered_float::OrderedFloat;

use crate::{
api::error::{Result, SpringError},
Expand Down Expand Up @@ -42,10 +41,7 @@ pub enum NnSqlValue {
UnsignedBigInt(u64),

/// FLOAT
Float(
// to implement Hash
OrderedFloat<f32>,
),
Float(f32),

/// TEXT
Text(String),
Expand Down Expand Up @@ -114,7 +110,7 @@ macro_rules! for_all_loose_types {
}
NnSqlValue::Float(_) => {
let v = $nn_sql_value.unpack::<f32>().unwrap();
$closure_ordered_float(OrderedFloat(v))
$closure_ordered_float(v)
}
NnSqlValue::Text(s) => $closure_string(s.to_string()),
NnSqlValue::Blob(v) => $closure_blob(v.to_owned()),
Expand All @@ -132,6 +128,24 @@ impl PartialEq for NnSqlValue {
}
impl Eq for NnSqlValue {}

use std::hash::{Hash, Hasher};

impl Hash for f32 {
fn hash<H: Hasher>(&self, state: &mut H) {
let bits = if self.is_nan() {
CANONICAL_NAN_BITS
} else if self.is_zero() {
CANONICAL_ZERO_BITS
} else {
raw_double_bits(&self.0)
};

bits.hash(state)
}
}

use ordered_float::OrderedFloat;

impl Hash for NnSqlValue {
/// Although raw format are different between two NnSqlValue, this hash function must return the same value if loosely typed values are the same.
/// E.g. `42 SMALLINT`'s hash value must be equal to that of `42 INTEGER`.
Expand All @@ -144,7 +158,7 @@ impl Hash for NnSqlValue {
|u: u64| {
u.hash(state);
},
|f: OrderedFloat<f32>| {
|f: f32| {
f.hash(state);
},
|s: String| {
Expand All @@ -166,7 +180,7 @@ impl Display for NnSqlValue {
self,
|i: i64| i.to_string(),
|u: u64| u.to_string(),
|f: OrderedFloat<f32>| f.to_string(),
|f: f32| f.to_string(),
|s: String| format!(r#""{}""#, s),
|v: Vec<u8>| format!("{:?}", v),
|b: bool| (if b { "TRUE" } else { "FALSE" }).to_string(),
Expand Down Expand Up @@ -379,7 +393,7 @@ impl Add for NnSqlValue {
}
(NumericComparableType::F32Loose(_), NumericComparableType::F32Loose(_)) => {
let (self_f32, rhs_f32) = (self.unpack::<f32>()?, rhs.unpack::<f32>()?);
Ok(Self::Float(OrderedFloat(self_f32 + rhs_f32)))
Ok(Self::Float(self_f32 + rhs_f32))
}
_ => Err(SpringError::Sql(anyhow!(
"Cannot add {:?} and {:?}",
Expand Down Expand Up @@ -413,7 +427,7 @@ impl Mul for NnSqlValue {
}
(NumericComparableType::F32Loose(_), NumericComparableType::F32Loose(_)) => {
let (self_f32, rhs_f32) = (self.unpack::<f32>()?, rhs.unpack::<f32>()?);
Ok(Self::Float(OrderedFloat(self_f32 * rhs_f32)))
Ok(Self::Float(self_f32 * rhs_f32))
}
_ => Err(SpringError::Sql(anyhow!(
"Cannot multiply {:?} by {:?}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ mod aggregate_state;

use std::collections::HashMap;

use ordered_float::OrderedFloat;

use crate::{
api::error::Result,
expr_resolver::ExprResolver,
Expand Down Expand Up @@ -127,8 +125,7 @@ impl Pane for AggrPane {
let aggregated_and_grouping_values_seq = states
.into_iter()
.map(|(group_by_values, state)| {
let aggr_value =
SqlValue::NotNull(NnSqlValue::Float(OrderedFloat(state.finalize())));
let aggr_value = SqlValue::NotNull(NnSqlValue::Float(state.finalize()));

let group_bys = group_by_labels
.as_labels()
Expand Down