Skip to content

Commit 70e2ab3

Browse files
alambtobixdev
authored andcommitted
Easier construction of ScalarAndMetadata (apache#18272)
## Which issue does this PR close? - Follow on to apache#17986 from @paleolimbot ## Rationale for this change As we thread Field through more of the DataFusion APs, making it easy to convert back and forth will be increasingly important. We added `ScalarAndMetadata` and I think it is a good idea to add some helper methods to make it easy to create `ScalarAndMetadata`. ## What changes are included in this PR? Add some From impls that make conversions easier ## Are these changes tested? By CI ## Are there any user-facing changes? SOme new APIs
1 parent 7ca2a5c commit 70e2ab3

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

datafusion/common/src/metadata.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,15 @@ impl ScalarAndMetadata {
6060
target_type: &DataType,
6161
) -> Result<Self, DataFusionError> {
6262
let new_value = self.value().cast_to(target_type)?;
63-
Ok(ScalarAndMetadata::new(new_value, self.metadata.clone()))
63+
Ok(Self::new(new_value, self.metadata.clone()))
64+
}
65+
}
66+
67+
/// create a new ScalarAndMetadata from a ScalarValue without
68+
/// any metadata
69+
impl From<ScalarValue> for ScalarAndMetadata {
70+
fn from(value: ScalarValue) -> Self {
71+
Self::new(value, None)
6472
}
6573
}
6674

datafusion/common/src/param_value.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,7 @@ impl ParamValues {
115115

116116
impl From<Vec<ScalarValue>> for ParamValues {
117117
fn from(value: Vec<ScalarValue>) -> Self {
118-
Self::List(
119-
value
120-
.into_iter()
121-
.map(|v| ScalarAndMetadata::new(v, None))
122-
.collect(),
123-
)
118+
Self::List(value.into_iter().map(ScalarAndMetadata::from).collect())
124119
}
125120
}
126121

@@ -131,7 +126,7 @@ where
131126
fn from(value: Vec<(K, ScalarValue)>) -> Self {
132127
let value: HashMap<String, ScalarAndMetadata> = value
133128
.into_iter()
134-
.map(|(k, v)| (k.into(), ScalarAndMetadata::new(v, None)))
129+
.map(|(k, v)| (k.into(), ScalarAndMetadata::from(v)))
135130
.collect();
136131
Self::Map(value)
137132
}
@@ -144,7 +139,7 @@ where
144139
fn from(value: HashMap<K, ScalarValue>) -> Self {
145140
let value: HashMap<String, ScalarAndMetadata> = value
146141
.into_iter()
147-
.map(|(k, v)| (k.into(), ScalarAndMetadata::new(v, None)))
142+
.map(|(k, v)| (k.into(), ScalarAndMetadata::from(v)))
148143
.collect();
149144
Self::Map(value)
150145
}

datafusion/expr/src/expr.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ use sqlparser::ast::{
4747

4848
// Moved in 51.0.0 to datafusion_common
4949
pub use datafusion_common::metadata::FieldMetadata;
50+
use datafusion_common::metadata::ScalarAndMetadata;
5051

5152
// This mirrors sqlparser::ast::NullTreatment but we need our own variant
5253
// for when the sql feature is disabled.
@@ -424,6 +425,14 @@ impl From<WindowFunction> for Expr {
424425
}
425426
}
426427

428+
/// Create an [`Expr`] from an [`ScalarAndMetadata`]
429+
impl From<ScalarAndMetadata> for Expr {
430+
fn from(value: ScalarAndMetadata) -> Self {
431+
let (value, metadata) = value.into_inner();
432+
Expr::Literal(value, metadata)
433+
}
434+
}
435+
427436
/// Create an [`Expr`] from an optional qualifier and a [`FieldRef`]. This is
428437
/// useful for creating [`Expr`] from a [`DFSchema`].
429438
///

datafusion/sql/tests/cases/params.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ fn test_update_infer_with_metadata() {
754754
("$2", Some(uuid_field.clone().with_name("id").into())),
755755
];
756756
let param_values = vec![
757-
ScalarAndMetadata::new(ScalarValue::from("Turing"), None),
757+
ScalarAndMetadata::from(ScalarValue::from("Turing")),
758758
ScalarAndMetadata::new(
759759
ScalarValue::FixedSizeBinary(16, Some(uuid_bytes)),
760760
Some(uuid_field.metadata().into()),
@@ -831,8 +831,8 @@ fn test_insert_infer_with_metadata() {
831831
ScalarValue::FixedSizeBinary(16, Some(uuid_bytes)),
832832
Some(uuid_field.metadata().into()),
833833
),
834-
ScalarAndMetadata::new(ScalarValue::from("Alan"), None),
835-
ScalarAndMetadata::new(ScalarValue::from("Turing"), None),
834+
ScalarAndMetadata::from(ScalarValue::from("Alan")),
835+
ScalarAndMetadata::from(ScalarValue::from("Turing")),
836836
];
837837

838838
// Check a normal insert

0 commit comments

Comments
 (0)