-
Notifications
You must be signed in to change notification settings - Fork 2.1k
feat(tesseract): Support boolean and numeric filter values #11135
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
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,11 +9,142 @@ use cubenativeutils::wrappers::serializer::{ | |
| }; | ||
| use cubenativeutils::wrappers::{NativeArray, NativeContextHolder, NativeObjectHandle}; | ||
| use cubenativeutils::CubeError; | ||
| use serde::{Deserialize, Serialize}; | ||
| use serde::de::Visitor; | ||
| use serde::{Deserialize, Deserializer, Serialize, Serializer}; | ||
| use std::any::Any; | ||
| use std::collections::HashMap; | ||
| use std::fmt; | ||
| use std::rc::Rc; | ||
|
|
||
| /// A single value of a filter (`equals`, `in`, `gt`, …). | ||
| #[derive(Debug, Clone, PartialEq)] | ||
| pub enum FilterValue { | ||
| Str(String), | ||
| Bool(bool), | ||
| Num(f64), | ||
| Null, | ||
| } | ||
|
|
||
| impl FilterValue { | ||
| pub fn is_null(&self) -> bool { | ||
| matches!(self, FilterValue::Null) | ||
| } | ||
|
|
||
| pub fn as_str(&self) -> Option<&str> { | ||
| match self { | ||
| FilterValue::Str(s) => Some(s.as_str()), | ||
| _ => None, | ||
| } | ||
| } | ||
|
|
||
| /// Canonical string representation bound as a SQL parameter. `Null` yields | ||
| /// `None` (the value is dropped / handled as `IS NULL`). Whole numbers are | ||
| /// rendered without a trailing `.0` (`42.0` → `"42"`). | ||
| pub fn to_param_string(&self) -> Option<String> { | ||
|
Comment on lines
+32
to
+36
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two small things on
|
||
| match self { | ||
| FilterValue::Str(s) => Some(s.clone()), | ||
| FilterValue::Bool(b) => Some(b.to_string()), | ||
| FilterValue::Num(n) => Some(Self::format_number(*n)), | ||
| FilterValue::Null => None, | ||
| } | ||
| } | ||
|
|
||
| fn format_number(n: f64) -> String { | ||
| if n.is_finite() && n.fract() == 0.0 && n.abs() < 1e15 { | ||
| format!("{}", n as i64) | ||
| } else { | ||
| format!("{}", n) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl From<Option<String>> for FilterValue { | ||
| fn from(value: Option<String>) -> Self { | ||
| match value { | ||
| Some(s) => FilterValue::Str(s), | ||
| None => FilterValue::Null, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl From<String> for FilterValue { | ||
| fn from(value: String) -> Self { | ||
| FilterValue::Str(value) | ||
| } | ||
| } | ||
|
|
||
| impl Serialize for FilterValue { | ||
| fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
| where | ||
| S: Serializer, | ||
| { | ||
| match self { | ||
| FilterValue::Str(s) => serializer.serialize_str(s), | ||
| FilterValue::Bool(b) => serializer.serialize_bool(*b), | ||
| FilterValue::Num(n) => serializer.serialize_f64(*n), | ||
| FilterValue::Null => serializer.serialize_none(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<'de> Deserialize<'de> for FilterValue { | ||
| fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
| where | ||
| D: Deserializer<'de>, | ||
| { | ||
| struct FilterValueVisitor; | ||
|
|
||
| impl<'de> Visitor<'de> for FilterValueVisitor { | ||
| type Value = FilterValue; | ||
|
|
||
| fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | ||
| formatter.write_str("a string, boolean, number, or null") | ||
| } | ||
|
|
||
| fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E> { | ||
| Ok(FilterValue::Bool(v)) | ||
| } | ||
|
|
||
| fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E> { | ||
| Ok(FilterValue::Num(v as f64)) | ||
| } | ||
|
|
||
| fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E> { | ||
| Ok(FilterValue::Num(v as f64)) | ||
| } | ||
|
|
||
| fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E> { | ||
| Ok(FilterValue::Num(v)) | ||
| } | ||
|
|
||
| fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> { | ||
| Ok(FilterValue::Str(v.to_string())) | ||
| } | ||
|
|
||
| fn visit_string<E>(self, v: String) -> Result<Self::Value, E> { | ||
| Ok(FilterValue::Str(v)) | ||
| } | ||
|
|
||
| fn visit_unit<E>(self) -> Result<Self::Value, E> { | ||
| Ok(FilterValue::Null) | ||
| } | ||
|
|
||
| fn visit_none<E>(self) -> Result<Self::Value, E> { | ||
| Ok(FilterValue::Null) | ||
| } | ||
|
|
||
| fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error> | ||
| where | ||
| D: Deserializer<'de>, | ||
| { | ||
| deserializer.deserialize_any(self) | ||
| } | ||
|
claude[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| deserializer.deserialize_any(FilterValueVisitor) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Serialize, Deserialize, Debug, Clone)] | ||
| pub struct MaskedMemberItem { | ||
| pub member: String, | ||
|
|
@@ -35,7 +166,7 @@ pub struct FilterItem { | |
| pub member: Option<String>, | ||
| pub dimension: Option<String>, | ||
| pub operator: Option<String>, | ||
| pub values: Option<Vec<Option<String>>>, | ||
| pub values: Option<Vec<FilterValue>>, | ||
| } | ||
|
|
||
| #[derive(Serialize, Deserialize, Debug, Clone)] | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This narrowing applies to every call to the native
deserialize_any(not justFilterValue). Previously every JS number was forwarded asi64and serde coerced it to whatever the target type expected; now integral values go tovisit_i64and fractional values tovisit_f64.Two latent risks worth a quick check:
f64. That's strictly an improvement for correct callers but could surface previously-silent data loss in callers that targetedi64/u64and relied on the truncation.f64::NANis!is_finite(), so it goes tovisit_f64(NaN)— most serde targets reject this, which is probably desired but worth noting.Adding a unit test in
deserializer.rscovering the four shapes (int, float, NaN, very large integral) would lock the contract down.