Skip to content

Commit 0087fbe

Browse files
committed
refactor: use static Signatures where applicable
We cannot allocate in `static` (see [E0010]), but `Signature`s with empty `vec![]`s totally work. [E0010]: https://doc.rust-lang.org/error_codes/E0010.html
1 parent 63b0bd1 commit 0087fbe

File tree

7 files changed

+74
-106
lines changed

7 files changed

+74
-106
lines changed

guests/evil/src/env.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use std::{hash::Hash, io::Read, sync::Arc};
33

44
use arrow::datatypes::DataType;
55
use datafusion_common::{Result as DataFusionResult, ScalarValue};
6-
use datafusion_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility};
6+
use datafusion_expr::{
7+
ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, TypeSignature, Volatility,
8+
};
79

810
use crate::common::DynBox;
911

@@ -15,11 +17,6 @@ struct StringUdf {
1517

1618
/// String producer.
1719
effect: DynBox<dyn Fn() -> Option<String> + Send + Sync>,
18-
19-
/// Signature of the UDF.
20-
///
21-
/// We store this here because [`ScalarUDFImpl::signature`] requires us to return a reference.
22-
signature: Signature,
2320
}
2421

2522
impl StringUdf {
@@ -31,7 +28,6 @@ impl StringUdf {
3128
Self {
3229
name,
3330
effect: DynBox(Box::new(effect)),
34-
signature: Signature::uniform(0, vec![], Volatility::Immutable),
3531
}
3632
}
3733
}
@@ -46,7 +42,12 @@ impl ScalarUDFImpl for StringUdf {
4642
}
4743

4844
fn signature(&self) -> &Signature {
49-
&self.signature
45+
static S: Signature = Signature {
46+
type_signature: TypeSignature::Uniform(0, vec![]),
47+
volatility: Volatility::Immutable,
48+
};
49+
50+
&S
5051
}
5152

5253
fn return_type(&self, _arg_types: &[DataType]) -> DataFusionResult<DataType> {

guests/evil/src/runtime.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use std::{hash::Hash, sync::Arc};
33

44
use arrow::datatypes::DataType;
55
use datafusion_common::{Result as DataFusionResult, ScalarValue};
6-
use datafusion_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility};
6+
use datafusion_expr::{
7+
ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, TypeSignature, Volatility,
8+
};
79

810
use crate::common::DynBox;
911

@@ -15,11 +17,6 @@ struct SideEffect {
1517

1618
/// Side effect.
1719
effect: DynBox<dyn Fn() + Send + Sync>,
18-
19-
/// Signature of the UDF.
20-
///
21-
/// We store this here because [`ScalarUDFImpl::signature`] requires us to return a reference.
22-
signature: Signature,
2320
}
2421

2522
impl SideEffect {
@@ -31,7 +28,6 @@ impl SideEffect {
3128
Self {
3229
name,
3330
effect: DynBox(Box::new(effect)),
34-
signature: Signature::uniform(0, vec![], Volatility::Immutable),
3531
}
3632
}
3733
}
@@ -46,7 +42,12 @@ impl ScalarUDFImpl for SideEffect {
4642
}
4743

4844
fn signature(&self) -> &Signature {
49-
&self.signature
45+
static S: Signature = Signature {
46+
type_signature: TypeSignature::Uniform(0, vec![]),
47+
volatility: Volatility::Immutable,
48+
};
49+
50+
&S
5051
}
5152

5253
fn return_type(&self, _arg_types: &[DataType]) -> DataFusionResult<DataType> {

guests/evil/src/spin/udf_invoke.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,15 @@ use std::sync::Arc;
33

44
use arrow::datatypes::DataType;
55
use datafusion_common::{Result as DataFusionResult, ScalarValue};
6-
use datafusion_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility};
6+
use datafusion_expr::{
7+
ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, TypeSignature, Volatility,
8+
};
79

810
use crate::spin::spin;
911

1012
/// UDF that spins.
1113
#[derive(Debug, PartialEq, Eq, Hash)]
12-
struct SpinUdf {
13-
/// Signature of the UDF.
14-
///
15-
/// We store this here because [`ScalarUDFImpl::signature`] requires us to return a reference.
16-
signature: Signature,
17-
}
18-
19-
impl SpinUdf {
20-
/// Create new UDF.
21-
fn new() -> Self {
22-
Self {
23-
signature: Signature::uniform(0, vec![], Volatility::Immutable),
24-
}
25-
}
26-
}
14+
struct SpinUdf;
2715

2816
impl ScalarUDFImpl for SpinUdf {
2917
fn as_any(&self) -> &dyn std::any::Any {
@@ -35,7 +23,12 @@ impl ScalarUDFImpl for SpinUdf {
3523
}
3624

3725
fn signature(&self) -> &Signature {
38-
&self.signature
26+
static S: Signature = Signature {
27+
type_signature: TypeSignature::Uniform(0, vec![]),
28+
volatility: Volatility::Immutable,
29+
};
30+
31+
&S
3932
}
4033

4134
fn return_type(&self, _arg_types: &[DataType]) -> DataFusionResult<DataType> {
@@ -53,5 +46,5 @@ impl ScalarUDFImpl for SpinUdf {
5346
/// The passed `source` is ignored.
5447
#[expect(clippy::unnecessary_wraps, reason = "public API through export! macro")]
5548
pub(crate) fn udfs(_source: String) -> DataFusionResult<Vec<Arc<dyn ScalarUDFImpl>>> {
56-
Ok(vec![Arc::new(SpinUdf::new())])
49+
Ok(vec![Arc::new(SpinUdf)])
5750
}

guests/evil/src/spin/udf_name.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,15 @@ use std::sync::Arc;
33

44
use arrow::datatypes::DataType;
55
use datafusion_common::{Result as DataFusionResult, ScalarValue};
6-
use datafusion_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility};
6+
use datafusion_expr::{
7+
ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, TypeSignature, Volatility,
8+
};
79

810
use crate::spin::spin;
911

1012
/// UDF that spins.
1113
#[derive(Debug, PartialEq, Eq, Hash)]
12-
struct SpinUdf {
13-
/// Signature of the UDF.
14-
///
15-
/// We store this here because [`ScalarUDFImpl::signature`] requires us to return a reference.
16-
signature: Signature,
17-
}
18-
19-
impl SpinUdf {
20-
/// Create new UDF.
21-
fn new() -> Self {
22-
Self {
23-
signature: Signature::uniform(0, vec![], Volatility::Immutable),
24-
}
25-
}
26-
}
14+
struct SpinUdf;
2715

2816
impl ScalarUDFImpl for SpinUdf {
2917
fn as_any(&self) -> &dyn std::any::Any {
@@ -36,7 +24,12 @@ impl ScalarUDFImpl for SpinUdf {
3624
}
3725

3826
fn signature(&self) -> &Signature {
39-
&self.signature
27+
static S: Signature = Signature {
28+
type_signature: TypeSignature::Uniform(0, vec![]),
29+
volatility: Volatility::Immutable,
30+
};
31+
32+
&S
4033
}
4134

4235
fn return_type(&self, _arg_types: &[DataType]) -> DataFusionResult<DataType> {
@@ -53,5 +46,5 @@ impl ScalarUDFImpl for SpinUdf {
5346
/// The passed `source` is ignored.
5447
#[expect(clippy::unnecessary_wraps, reason = "public API through export! macro")]
5548
pub(crate) fn udfs(_source: String) -> DataFusionResult<Vec<Arc<dyn ScalarUDFImpl>>> {
56-
Ok(vec![Arc::new(SpinUdf::new())])
49+
Ok(vec![Arc::new(SpinUdf)])
5750
}

guests/evil/src/spin/udf_return_type_exact.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,15 @@ use std::sync::Arc;
33

44
use arrow::datatypes::DataType;
55
use datafusion_common::{Result as DataFusionResult, ScalarValue};
6-
use datafusion_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility};
6+
use datafusion_expr::{
7+
ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, TypeSignature, Volatility,
8+
};
79

810
use crate::spin::spin;
911

1012
/// UDF that spins.
1113
#[derive(Debug, PartialEq, Eq, Hash)]
12-
struct SpinUdf {
13-
/// Signature of the UDF.
14-
///
15-
/// We store this here because [`ScalarUDFImpl::signature`] requires us to return a reference.
16-
signature: Signature,
17-
}
18-
19-
impl SpinUdf {
20-
/// Create new UDF.
21-
fn new() -> Self {
22-
Self {
23-
signature: Signature::exact(vec![], Volatility::Immutable),
24-
}
25-
}
26-
}
14+
struct SpinUdf;
2715

2816
impl ScalarUDFImpl for SpinUdf {
2917
fn as_any(&self) -> &dyn std::any::Any {
@@ -35,7 +23,12 @@ impl ScalarUDFImpl for SpinUdf {
3523
}
3624

3725
fn signature(&self) -> &Signature {
38-
&self.signature
26+
static S: Signature = Signature {
27+
type_signature: TypeSignature::Exact(vec![]),
28+
volatility: Volatility::Immutable,
29+
};
30+
31+
&S
3932
}
4033

4134
fn return_type(&self, _arg_types: &[DataType]) -> DataFusionResult<DataType> {
@@ -53,5 +46,5 @@ impl ScalarUDFImpl for SpinUdf {
5346
/// The passed `source` is ignored.
5447
#[expect(clippy::unnecessary_wraps, reason = "public API through export! macro")]
5548
pub(crate) fn udfs(_source: String) -> DataFusionResult<Vec<Arc<dyn ScalarUDFImpl>>> {
56-
Ok(vec![Arc::new(SpinUdf::new())])
49+
Ok(vec![Arc::new(SpinUdf)])
5750
}

guests/evil/src/spin/udf_return_type_other.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,15 @@ use std::sync::Arc;
33

44
use arrow::datatypes::DataType;
55
use datafusion_common::{Result as DataFusionResult, ScalarValue};
6-
use datafusion_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility};
6+
use datafusion_expr::{
7+
ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, TypeSignature, Volatility,
8+
};
79

810
use crate::spin::spin;
911

1012
/// UDF that spins.
1113
#[derive(Debug, PartialEq, Eq, Hash)]
12-
struct SpinUdf {
13-
/// Signature of the UDF.
14-
///
15-
/// We store this here because [`ScalarUDFImpl::signature`] requires us to return a reference.
16-
signature: Signature,
17-
}
18-
19-
impl SpinUdf {
20-
/// Create new UDF.
21-
fn new() -> Self {
22-
Self {
23-
signature: Signature::uniform(0, vec![], Volatility::Immutable),
24-
}
25-
}
26-
}
14+
struct SpinUdf;
2715

2816
impl ScalarUDFImpl for SpinUdf {
2917
fn as_any(&self) -> &dyn std::any::Any {
@@ -35,7 +23,12 @@ impl ScalarUDFImpl for SpinUdf {
3523
}
3624

3725
fn signature(&self) -> &Signature {
38-
&self.signature
26+
static S: Signature = Signature {
27+
type_signature: TypeSignature::Uniform(0, vec![]),
28+
volatility: Volatility::Immutable,
29+
};
30+
31+
&S
3932
}
4033

4134
fn return_type(&self, _arg_types: &[DataType]) -> DataFusionResult<DataType> {
@@ -53,5 +46,5 @@ impl ScalarUDFImpl for SpinUdf {
5346
/// The passed `source` is ignored.
5447
#[expect(clippy::unnecessary_wraps, reason = "public API through export! macro")]
5548
pub(crate) fn udfs(_source: String) -> DataFusionResult<Vec<Arc<dyn ScalarUDFImpl>>> {
56-
Ok(vec![Arc::new(SpinUdf::new())])
49+
Ok(vec![Arc::new(SpinUdf)])
5750
}

guests/evil/src/spin/udf_signature.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,15 @@ use std::sync::Arc;
33

44
use arrow::datatypes::DataType;
55
use datafusion_common::{Result as DataFusionResult, ScalarValue};
6-
use datafusion_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility};
6+
use datafusion_expr::{
7+
ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, TypeSignature, Volatility,
8+
};
79

810
use crate::spin::spin;
911

1012
/// UDF that spins.
1113
#[derive(Debug, PartialEq, Eq, Hash)]
12-
struct SpinUdf {
13-
/// Signature of the UDF.
14-
///
15-
/// We store this here because [`ScalarUDFImpl::signature`] requires us to return a reference.
16-
signature: Signature,
17-
}
18-
19-
impl SpinUdf {
20-
/// Create new UDF.
21-
fn new() -> Self {
22-
Self {
23-
signature: Signature::uniform(0, vec![], Volatility::Immutable),
24-
}
25-
}
26-
}
14+
struct SpinUdf;
2715

2816
impl ScalarUDFImpl for SpinUdf {
2917
fn as_any(&self) -> &dyn std::any::Any {
@@ -35,8 +23,14 @@ impl ScalarUDFImpl for SpinUdf {
3523
}
3624

3725
fn signature(&self) -> &Signature {
26+
static S: Signature = Signature {
27+
type_signature: TypeSignature::Uniform(0, vec![]),
28+
volatility: Volatility::Immutable,
29+
};
30+
3831
spin();
39-
&self.signature
32+
33+
&S
4034
}
4135

4236
fn return_type(&self, _arg_types: &[DataType]) -> DataFusionResult<DataType> {
@@ -53,5 +47,5 @@ impl ScalarUDFImpl for SpinUdf {
5347
/// The passed `source` is ignored.
5448
#[expect(clippy::unnecessary_wraps, reason = "public API through export! macro")]
5549
pub(crate) fn udfs(_source: String) -> DataFusionResult<Vec<Arc<dyn ScalarUDFImpl>>> {
56-
Ok(vec![Arc::new(SpinUdf::new())])
50+
Ok(vec![Arc::new(SpinUdf)])
5751
}

0 commit comments

Comments
 (0)