Skip to content

Commit 95f429d

Browse files
authored
Remove needless fmt_sql (#7409)
Remove bespoke `fmt_sql` implementations for scalar functions that do not need special syntax. These overrides only duplicated the generic call formatting and could drift from the registered `ScalarFnId`; relying on the default keeps expression display consistent, preserves qualified function names/options, and reduces boilerplate for new scalar functions. Signed-off-by: Nicholas Gates <nick@nickgates.com> Signed-off-by: "Nicholas Gates" <nick@nickgates.com>
1 parent 7668bef commit 95f429d

14 files changed

Lines changed: 29 additions & 178 deletions

File tree

vortex-array/src/expr/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ mod tests {
207207
"(($.col1 < $.col2) or ($.col1 != $.col2))"
208208
);
209209

210-
assert_eq!(not(col1).to_string(), "not($.col1)");
210+
assert_eq!(not(col1).to_string(), "vortex.not($.col1)");
211211

212212
assert_eq!(
213213
select(vec![FieldName::from("col1")], root()).to_string(),

vortex-array/src/scalar_fn/fns/fill_null/mod.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
mod kernel;
55

6-
use std::fmt::Formatter;
7-
86
pub use kernel::*;
97
use vortex_error::VortexResult;
108
use vortex_error::vortex_bail;
@@ -66,19 +64,6 @@ impl ScalarFnVTable for FillNull {
6664
}
6765
}
6866

69-
fn fmt_sql(
70-
&self,
71-
_options: &Self::Options,
72-
expr: &Expression,
73-
f: &mut Formatter<'_>,
74-
) -> std::fmt::Result {
75-
write!(f, "fill_null(")?;
76-
expr.child(0).fmt_sql(f)?;
77-
write!(f, ", ")?;
78-
expr.child(1).fmt_sql(f)?;
79-
write!(f, ")")
80-
}
81-
8267
fn return_dtype(&self, _options: &Self::Options, arg_dtypes: &[DType]) -> VortexResult<DType> {
8368
vortex_ensure!(
8469
arg_dtypes[0].eq_ignore_nullability(&arg_dtypes[1]),
@@ -265,6 +250,6 @@ mod tests {
265250
#[test]
266251
fn test_display() {
267252
let expr = fill_null(get_item("value", root()), lit(0i32));
268-
assert_eq!(expr.to_string(), "fill_null($.value, 0i32)");
253+
assert_eq!(expr.to_string(), "vortex.fill_null($.value, 0i32)");
269254
}
270255
}

vortex-array/src/scalar_fn/fns/is_null.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
33

4-
use std::fmt::Formatter;
5-
64
use vortex_error::VortexResult;
75
use vortex_session::VortexSession;
86

@@ -60,17 +58,6 @@ impl ScalarFnVTable for IsNull {
6058
}
6159
}
6260

63-
fn fmt_sql(
64-
&self,
65-
_options: &Self::Options,
66-
expr: &Expression,
67-
f: &mut Formatter<'_>,
68-
) -> std::fmt::Result {
69-
write!(f, "is_null(")?;
70-
expr.child(0).fmt_sql(f)?;
71-
write!(f, ")")
72-
}
73-
7461
fn return_dtype(&self, _options: &Self::Options, _arg_dtypes: &[DType]) -> VortexResult<DType> {
7562
Ok(DType::Bool(Nullability::NonNullable))
7663
}
@@ -250,10 +237,10 @@ mod tests {
250237
#[test]
251238
fn test_display() {
252239
let expr = is_null(get_item("name", root()));
253-
assert_eq!(expr.to_string(), "is_null($.name)");
240+
assert_eq!(expr.to_string(), "vortex.is_null($.name)");
254241

255242
let expr2 = is_null(root());
256-
assert_eq!(expr2.to_string(), "is_null($)");
243+
assert_eq!(expr2.to_string(), "vortex.is_null($)");
257244
}
258245

259246
#[test]

vortex-array/src/scalar_fn/fns/list_contains/mod.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
mod kernel;
55

6-
use std::fmt::Formatter;
76
use std::ops::BitOr;
87

98
use arrow_buffer::bit_iterator::BitIndexIterator;
@@ -89,19 +88,6 @@ impl ScalarFnVTable for ListContains {
8988
),
9089
}
9190
}
92-
fn fmt_sql(
93-
&self,
94-
_options: &Self::Options,
95-
expr: &Expression,
96-
f: &mut Formatter<'_>,
97-
) -> std::fmt::Result {
98-
write!(f, "contains(")?;
99-
expr.child(0).fmt_sql(f)?;
100-
write!(f, ", ")?;
101-
expr.child(1).fmt_sql(f)?;
102-
write!(f, ")")
103-
}
104-
10591
fn return_dtype(&self, _options: &Self::Options, arg_dtypes: &[DType]) -> VortexResult<DType> {
10692
let list_dtype = &arg_dtypes[0];
10793
let needle_dtype = &arg_dtypes[1];
@@ -670,10 +656,10 @@ mod tests {
670656
#[test]
671657
pub fn test_display() {
672658
let expr = list_contains(get_item("tags", root()), lit("urgent"));
673-
assert_eq!(expr.to_string(), "contains($.tags, \"urgent\")");
659+
assert_eq!(expr.to_string(), "vortex.list.contains($.tags, \"urgent\")");
674660

675661
let expr2 = list_contains(root(), lit(42));
676-
assert_eq!(expr2.to_string(), "contains($, 42i32)");
662+
assert_eq!(expr2.to_string(), "vortex.list.contains($, 42i32)");
677663
}
678664

679665
#[test]

vortex-array/src/scalar_fn/fns/mask/mod.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
33

44
mod kernel;
5-
use std::fmt::Formatter;
65

76
pub use kernel::*;
87
use vortex_error::VortexExpect;
@@ -72,19 +71,6 @@ impl ScalarFnVTable for Mask {
7271
}
7372
}
7473

75-
fn fmt_sql(
76-
&self,
77-
_options: &Self::Options,
78-
expr: &Expression,
79-
f: &mut Formatter<'_>,
80-
) -> std::fmt::Result {
81-
write!(f, "mask(")?;
82-
expr.child(0).fmt_sql(f)?;
83-
write!(f, ", ")?;
84-
expr.child(1).fmt_sql(f)?;
85-
write!(f, ")")
86-
}
87-
8874
fn return_dtype(&self, _options: &Self::Options, arg_dtypes: &[DType]) -> VortexResult<DType> {
8975
vortex_ensure!(
9076
arg_dtypes[1] == DType::Bool(Nullability::NonNullable),

vortex-array/src/scalar_fn/fns/merge.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,6 @@ impl ScalarFnVTable for Merge {
8484
ChildName::from(Arc::from(format!("{}", child_idx)))
8585
}
8686

87-
fn fmt_sql(
88-
&self,
89-
_options: &Self::Options,
90-
expr: &Expression,
91-
f: &mut Formatter<'_>,
92-
) -> std::fmt::Result {
93-
write!(f, "merge(")?;
94-
for (i, child) in expr.children().iter().enumerate() {
95-
child.fmt_sql(f)?;
96-
if i + 1 < expr.children().len() {
97-
write!(f, ", ")?;
98-
}
99-
}
100-
write!(f, ")")
101-
}
102-
10387
fn return_dtype(&self, options: &Self::Options, arg_dtypes: &[DType]) -> VortexResult<DType> {
10488
let mut field_names = Vec::new();
10589
let mut arrays = Vec::new();
@@ -550,10 +534,13 @@ mod tests {
550534
#[test]
551535
pub fn test_display() {
552536
let expr = merge([get_item("struct1", root()), get_item("struct2", root())]);
553-
assert_eq!(expr.to_string(), "merge($.struct1, $.struct2)");
537+
assert_eq!(
538+
expr.to_string(),
539+
"vortex.merge($.struct1, $.struct2, opts=Error)"
540+
);
554541

555542
let expr2 = merge(vec![get_item("a", root())]);
556-
assert_eq!(expr2.to_string(), "merge($.a)");
543+
assert_eq!(expr2.to_string(), "vortex.merge($.a, opts=Error)");
557544
}
558545

559546
#[test]

vortex-array/src/scalar_fn/fns/not/mod.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
mod kernel;
55

6-
use std::fmt::Formatter;
7-
86
pub use kernel::*;
97
use vortex_error::VortexResult;
108
use vortex_error::vortex_bail;
@@ -19,7 +17,6 @@ use crate::arrays::ConstantArray;
1917
use crate::arrays::bool::BoolArrayExt;
2018
use crate::builtins::ArrayBuiltins;
2119
use crate::dtype::DType;
22-
use crate::expr::Expression;
2320
use crate::scalar::Scalar;
2421
use crate::scalar_fn::Arity;
2522
use crate::scalar_fn::ChildName;
@@ -62,17 +59,6 @@ impl ScalarFnVTable for Not {
6259
}
6360
}
6461

65-
fn fmt_sql(
66-
&self,
67-
_options: &Self::Options,
68-
expr: &Expression,
69-
f: &mut Formatter<'_>,
70-
) -> std::fmt::Result {
71-
write!(f, "not(")?;
72-
expr.child(0).fmt_sql(f)?;
73-
write!(f, ")")
74-
}
75-
7662
fn return_dtype(&self, _options: &Self::Options, arg_dtypes: &[DType]) -> VortexResult<DType> {
7763
let child_dtype = &arg_dtypes[0];
7864
if !matches!(child_dtype, DType::Bool(_)) {
@@ -151,8 +137,8 @@ mod tests {
151137
let a = not(get_item("a", root()));
152138
let b = get_item("a", not(root()));
153139
assert_ne!(a.to_string(), b.to_string());
154-
assert_eq!(a.to_string(), "not($.a)");
155-
assert_eq!(b.to_string(), "not($).a");
140+
assert_eq!(a.to_string(), "vortex.not($.a)");
141+
assert_eq!(b.to_string(), "vortex.not($).a");
156142
}
157143

158144
#[test]

vortex-array/src/scalar_fn/vtable.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use crate::dtype::DType;
2222
use crate::expr::Expression;
2323
use crate::expr::StatsCatalog;
2424
use crate::expr::stats::Stat;
25+
use crate::expr::traversal::Node;
2526
use crate::scalar_fn::ScalarFnId;
2627
use crate::scalar_fn::ScalarFnRef;
2728
use crate::scalar_fn::TypedScalarFnInstance;
@@ -77,7 +78,21 @@ pub trait ScalarFnVTable: 'static + Sized + Clone + Send + Sync {
7778
options: &Self::Options,
7879
expr: &Expression,
7980
f: &mut Formatter<'_>,
80-
) -> fmt::Result;
81+
) -> fmt::Result {
82+
write!(f, "{}(", self.id())?;
83+
let nchildren = expr.children_count();
84+
for (i, child) in expr.children().iter().enumerate() {
85+
child.fmt_sql(f)?;
86+
if i + 1 < nchildren {
87+
write!(f, ", ")?;
88+
}
89+
}
90+
let opts = format!("{}", options);
91+
if !opts.is_empty() {
92+
write!(f, ", opts={}", opts)?;
93+
}
94+
write!(f, ")")
95+
}
8196

8297
/// Coerce the arguments of this function.
8398
///

vortex-tensor/public-api.lock

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,6 @@ pub fn vortex_tensor::scalar_fns::cosine_similarity::CosineSimilarity::child_nam
284284

285285
pub fn vortex_tensor::scalar_fns::cosine_similarity::CosineSimilarity::execute(&self, &Self::Options, &dyn vortex_array::scalar_fn::vtable::ExecutionArgs, &mut vortex_array::executor::ExecutionCtx) -> vortex_error::VortexResult<vortex_array::array::erased::ArrayRef>
286286

287-
pub fn vortex_tensor::scalar_fns::cosine_similarity::CosineSimilarity::fmt_sql(&self, &Self::Options, &vortex_array::expr::expression::Expression, &mut core::fmt::Formatter<'_>) -> core::fmt::Result
288-
289287
pub fn vortex_tensor::scalar_fns::cosine_similarity::CosineSimilarity::id(&self) -> vortex_array::scalar_fn::ScalarFnId
290288

291289
pub fn vortex_tensor::scalar_fns::cosine_similarity::CosineSimilarity::is_fallible(&self, &Self::Options) -> bool
@@ -326,8 +324,6 @@ pub fn vortex_tensor::scalar_fns::inner_product::InnerProduct::child_name(&self,
326324

327325
pub fn vortex_tensor::scalar_fns::inner_product::InnerProduct::execute(&self, &Self::Options, &dyn vortex_array::scalar_fn::vtable::ExecutionArgs, &mut vortex_array::executor::ExecutionCtx) -> vortex_error::VortexResult<vortex_array::array::erased::ArrayRef>
328326

329-
pub fn vortex_tensor::scalar_fns::inner_product::InnerProduct::fmt_sql(&self, &Self::Options, &vortex_array::expr::expression::Expression, &mut core::fmt::Formatter<'_>) -> core::fmt::Result
330-
331327
pub fn vortex_tensor::scalar_fns::inner_product::InnerProduct::id(&self) -> vortex_array::scalar_fn::ScalarFnId
332328

333329
pub fn vortex_tensor::scalar_fns::inner_product::InnerProduct::is_fallible(&self, &Self::Options) -> bool
@@ -370,8 +366,6 @@ pub fn vortex_tensor::scalar_fns::l2_denorm::L2Denorm::child_name(&self, &Self::
370366

371367
pub fn vortex_tensor::scalar_fns::l2_denorm::L2Denorm::execute(&self, &Self::Options, &dyn vortex_array::scalar_fn::vtable::ExecutionArgs, &mut vortex_array::executor::ExecutionCtx) -> vortex_error::VortexResult<vortex_array::array::erased::ArrayRef>
372368

373-
pub fn vortex_tensor::scalar_fns::l2_denorm::L2Denorm::fmt_sql(&self, &Self::Options, &vortex_array::expr::expression::Expression, &mut core::fmt::Formatter<'_>) -> core::fmt::Result
374-
375369
pub fn vortex_tensor::scalar_fns::l2_denorm::L2Denorm::id(&self) -> vortex_array::scalar_fn::ScalarFnId
376370

377371
pub fn vortex_tensor::scalar_fns::l2_denorm::L2Denorm::is_fallible(&self, &Self::Options) -> bool
@@ -416,8 +410,6 @@ pub fn vortex_tensor::scalar_fns::l2_norm::L2Norm::child_name(&self, &Self::Opti
416410

417411
pub fn vortex_tensor::scalar_fns::l2_norm::L2Norm::execute(&self, &Self::Options, &dyn vortex_array::scalar_fn::vtable::ExecutionArgs, &mut vortex_array::executor::ExecutionCtx) -> vortex_error::VortexResult<vortex_array::array::erased::ArrayRef>
418412

419-
pub fn vortex_tensor::scalar_fns::l2_norm::L2Norm::fmt_sql(&self, &Self::Options, &vortex_array::expr::expression::Expression, &mut core::fmt::Formatter<'_>) -> core::fmt::Result
420-
421413
pub fn vortex_tensor::scalar_fns::l2_norm::L2Norm::id(&self) -> vortex_array::scalar_fn::ScalarFnId
422414

423415
pub fn vortex_tensor::scalar_fns::l2_norm::L2Norm::is_fallible(&self, &Self::Options) -> bool
@@ -504,8 +496,6 @@ pub fn vortex_tensor::scalar_fns::sorf_transform::SorfTransform::child_name(&sel
504496

505497
pub fn vortex_tensor::scalar_fns::sorf_transform::SorfTransform::execute(&self, &Self::Options, &dyn vortex_array::scalar_fn::vtable::ExecutionArgs, &mut vortex_array::executor::ExecutionCtx) -> vortex_error::VortexResult<vortex_array::array::erased::ArrayRef>
506498

507-
pub fn vortex_tensor::scalar_fns::sorf_transform::SorfTransform::fmt_sql(&self, &Self::Options, &vortex_array::expr::expression::Expression, &mut core::fmt::Formatter<'_>) -> core::fmt::Result
508-
509499
pub fn vortex_tensor::scalar_fns::sorf_transform::SorfTransform::id(&self) -> vortex_array::scalar_fn::ScalarFnId
510500

511501
pub fn vortex_tensor::scalar_fns::sorf_transform::SorfTransform::is_fallible(&self, &Self::Options) -> bool

vortex-tensor/src/scalar_fns/cosine_similarity.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
//! Cosine similarity expression for tensor-like types.
55
6-
use std::fmt::Formatter;
7-
86
use num_traits::Zero;
97
use vortex_array::ArrayRef;
108
use vortex_array::ExecutionCtx;
@@ -96,19 +94,6 @@ impl ScalarFnVTable for CosineSimilarity {
9694
}
9795
}
9896

99-
fn fmt_sql(
100-
&self,
101-
_options: &Self::Options,
102-
expr: &Expression,
103-
f: &mut Formatter<'_>,
104-
) -> std::fmt::Result {
105-
write!(f, "cosine_similarity(")?;
106-
expr.child(0).fmt_sql(f)?;
107-
write!(f, ", ")?;
108-
expr.child(1).fmt_sql(f)?;
109-
write!(f, ")")
110-
}
111-
11297
fn return_dtype(&self, _options: &Self::Options, arg_dtypes: &[DType]) -> VortexResult<DType> {
11398
let lhs = &arg_dtypes[0];
11499
let rhs = &arg_dtypes[1];

0 commit comments

Comments
 (0)