Skip to content

Commit fd92bcb

Browse files
authored
Minor: Use ScalarValue::from impl for strings (#8429)
* Minor: Use ScalarValue::from impl for strings * fix typo
1 parent 3ddd5eb commit fd92bcb

File tree

28 files changed

+83
-167
lines changed

28 files changed

+83
-167
lines changed

datafusion/common/src/pyarrow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ mod tests {
119119
ScalarValue::Boolean(Some(true)),
120120
ScalarValue::Int32(Some(23)),
121121
ScalarValue::Float64(Some(12.34)),
122-
ScalarValue::Utf8(Some("Hello!".to_string())),
122+
ScalarValue::from("Hello!"),
123123
ScalarValue::Date32(Some(1234)),
124124
];
125125

datafusion/common/src/scalar.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ impl ScalarValue {
774774

775775
/// Returns a [`ScalarValue::Utf8`] representing `val`
776776
pub fn new_utf8(val: impl Into<String>) -> Self {
777-
ScalarValue::Utf8(Some(val.into()))
777+
ScalarValue::from(val.into())
778778
}
779779

780780
/// Returns a [`ScalarValue::IntervalYearMonth`] representing
@@ -2699,7 +2699,7 @@ impl ScalarValue {
26992699

27002700
/// Try to parse `value` into a ScalarValue of type `target_type`
27012701
pub fn try_from_string(value: String, target_type: &DataType) -> Result<Self> {
2702-
let value = ScalarValue::Utf8(Some(value));
2702+
let value = ScalarValue::from(value);
27032703
let cast_options = CastOptions {
27042704
safe: false,
27052705
format_options: Default::default(),
@@ -3581,9 +3581,9 @@ mod tests {
35813581
#[test]
35823582
fn test_list_to_array_string() {
35833583
let scalars = vec![
3584-
ScalarValue::Utf8(Some(String::from("rust"))),
3585-
ScalarValue::Utf8(Some(String::from("arrow"))),
3586-
ScalarValue::Utf8(Some(String::from("data-fusion"))),
3584+
ScalarValue::from("rust"),
3585+
ScalarValue::from("arrow"),
3586+
ScalarValue::from("data-fusion"),
35873587
];
35883588

35893589
let array = ScalarValue::new_list(scalars.as_slice(), &DataType::Utf8);
@@ -4722,7 +4722,7 @@ mod tests {
47224722
Some(vec![
47234723
ScalarValue::Int32(Some(23)),
47244724
ScalarValue::Boolean(Some(false)),
4725-
ScalarValue::Utf8(Some("Hello".to_string())),
4725+
ScalarValue::from("Hello"),
47264726
ScalarValue::from(vec![
47274727
("e", ScalarValue::from(2i16)),
47284728
("f", ScalarValue::from(3i64)),
@@ -4915,17 +4915,17 @@ mod tests {
49154915

49164916
// Define struct scalars
49174917
let s0 = ScalarValue::from(vec![
4918-
("A", ScalarValue::Utf8(Some(String::from("First")))),
4918+
("A", ScalarValue::from("First")),
49194919
("primitive_list", l0),
49204920
]);
49214921

49224922
let s1 = ScalarValue::from(vec![
4923-
("A", ScalarValue::Utf8(Some(String::from("Second")))),
4923+
("A", ScalarValue::from("Second")),
49244924
("primitive_list", l1),
49254925
]);
49264926

49274927
let s2 = ScalarValue::from(vec![
4928-
("A", ScalarValue::Utf8(Some(String::from("Third")))),
4928+
("A", ScalarValue::from("Third")),
49294929
("primitive_list", l2),
49304930
]);
49314931

@@ -5212,7 +5212,7 @@ mod tests {
52125212
check_scalar_cast(ScalarValue::Float64(None), DataType::Int16);
52135213

52145214
check_scalar_cast(
5215-
ScalarValue::Utf8(Some("foo".to_string())),
5215+
ScalarValue::from("foo"),
52165216
DataType::Dictionary(Box::new(DataType::Int32), Box::new(DataType::Utf8)),
52175217
);
52185218

@@ -5493,10 +5493,7 @@ mod tests {
54935493
(ScalarValue::Int8(None), ScalarValue::Int16(Some(1))),
54945494
(ScalarValue::Int8(Some(1)), ScalarValue::Int16(None)),
54955495
// Unsupported types
5496-
(
5497-
ScalarValue::Utf8(Some("foo".to_string())),
5498-
ScalarValue::Utf8(Some("bar".to_string())),
5499-
),
5496+
(ScalarValue::from("foo"), ScalarValue::from("bar")),
55005497
(
55015498
ScalarValue::Boolean(Some(true)),
55025499
ScalarValue::Boolean(Some(false)),

datafusion/core/src/datasource/listing/helpers.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -526,19 +526,13 @@ mod tests {
526526
f1.object_meta.location.as_ref(),
527527
"tablepath/mypartition=val1/file.parquet"
528528
);
529-
assert_eq!(
530-
&f1.partition_values,
531-
&[ScalarValue::Utf8(Some(String::from("val1"))),]
532-
);
529+
assert_eq!(&f1.partition_values, &[ScalarValue::from("val1")]);
533530
let f2 = &pruned[1];
534531
assert_eq!(
535532
f2.object_meta.location.as_ref(),
536533
"tablepath/mypartition=val1/other=val3/file.parquet"
537534
);
538-
assert_eq!(
539-
f2.partition_values,
540-
&[ScalarValue::Utf8(Some(String::from("val1"))),]
541-
);
535+
assert_eq!(f2.partition_values, &[ScalarValue::from("val1"),]);
542536
}
543537

544538
#[tokio::test]
@@ -579,10 +573,7 @@ mod tests {
579573
);
580574
assert_eq!(
581575
&f1.partition_values,
582-
&[
583-
ScalarValue::Utf8(Some(String::from("p1v2"))),
584-
ScalarValue::Utf8(Some(String::from("p2v1")))
585-
]
576+
&[ScalarValue::from("p1v2"), ScalarValue::from("p2v1"),]
586577
);
587578
let f2 = &pruned[1];
588579
assert_eq!(
@@ -591,10 +582,7 @@ mod tests {
591582
);
592583
assert_eq!(
593584
&f2.partition_values,
594-
&[
595-
ScalarValue::Utf8(Some(String::from("p1v2"))),
596-
ScalarValue::Utf8(Some(String::from("p2v1")))
597-
]
585+
&[ScalarValue::from("p1v2"), ScalarValue::from("p2v1")]
598586
);
599587
}
600588

datafusion/core/src/datasource/physical_plan/avro.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,7 @@ mod tests {
406406
.await?;
407407

408408
let mut partitioned_file = PartitionedFile::from(meta);
409-
partitioned_file.partition_values =
410-
vec![ScalarValue::Utf8(Some("2021-10-26".to_owned()))];
409+
partitioned_file.partition_values = vec![ScalarValue::from("2021-10-26")];
411410

412411
let avro_exec = AvroExec::new(FileScanConfig {
413412
// select specific columns of the files as well as the partitioning

datafusion/core/src/datasource/physical_plan/csv.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -872,8 +872,7 @@ mod tests {
872872

873873
// Add partition columns
874874
config.table_partition_cols = vec![Field::new("date", DataType::Utf8, false)];
875-
config.file_groups[0][0].partition_values =
876-
vec![ScalarValue::Utf8(Some("2021-10-26".to_owned()))];
875+
config.file_groups[0][0].partition_values = vec![ScalarValue::from("2021-10-26")];
877876

878877
// We should be able to project on the partition column
879878
// Which is supposed to be after the file fields

datafusion/core/src/datasource/physical_plan/file_scan_config.rs

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -654,15 +654,9 @@ mod tests {
654654
// file_batch is ok here because we kept all the file cols in the projection
655655
file_batch,
656656
&[
657-
wrap_partition_value_in_dict(ScalarValue::Utf8(Some(
658-
"2021".to_owned(),
659-
))),
660-
wrap_partition_value_in_dict(ScalarValue::Utf8(Some(
661-
"10".to_owned(),
662-
))),
663-
wrap_partition_value_in_dict(ScalarValue::Utf8(Some(
664-
"26".to_owned(),
665-
))),
657+
wrap_partition_value_in_dict(ScalarValue::from("2021")),
658+
wrap_partition_value_in_dict(ScalarValue::from("10")),
659+
wrap_partition_value_in_dict(ScalarValue::from("26")),
666660
],
667661
)
668662
.expect("Projection of partition columns into record batch failed");
@@ -688,15 +682,9 @@ mod tests {
688682
// file_batch is ok here because we kept all the file cols in the projection
689683
file_batch,
690684
&[
691-
wrap_partition_value_in_dict(ScalarValue::Utf8(Some(
692-
"2021".to_owned(),
693-
))),
694-
wrap_partition_value_in_dict(ScalarValue::Utf8(Some(
695-
"10".to_owned(),
696-
))),
697-
wrap_partition_value_in_dict(ScalarValue::Utf8(Some(
698-
"27".to_owned(),
699-
))),
685+
wrap_partition_value_in_dict(ScalarValue::from("2021")),
686+
wrap_partition_value_in_dict(ScalarValue::from("10")),
687+
wrap_partition_value_in_dict(ScalarValue::from("27")),
700688
],
701689
)
702690
.expect("Projection of partition columns into record batch failed");
@@ -724,15 +712,9 @@ mod tests {
724712
// file_batch is ok here because we kept all the file cols in the projection
725713
file_batch,
726714
&[
727-
wrap_partition_value_in_dict(ScalarValue::Utf8(Some(
728-
"2021".to_owned(),
729-
))),
730-
wrap_partition_value_in_dict(ScalarValue::Utf8(Some(
731-
"10".to_owned(),
732-
))),
733-
wrap_partition_value_in_dict(ScalarValue::Utf8(Some(
734-
"28".to_owned(),
735-
))),
715+
wrap_partition_value_in_dict(ScalarValue::from("2021")),
716+
wrap_partition_value_in_dict(ScalarValue::from("10")),
717+
wrap_partition_value_in_dict(ScalarValue::from("28")),
736718
],
737719
)
738720
.expect("Projection of partition columns into record batch failed");
@@ -758,9 +740,9 @@ mod tests {
758740
// file_batch is ok here because we kept all the file cols in the projection
759741
file_batch,
760742
&[
761-
ScalarValue::Utf8(Some("2021".to_owned())),
762-
ScalarValue::Utf8(Some("10".to_owned())),
763-
ScalarValue::Utf8(Some("26".to_owned())),
743+
ScalarValue::from("2021"),
744+
ScalarValue::from("10"),
745+
ScalarValue::from("26"),
764746
],
765747
)
766748
.expect("Projection of partition columns into record batch failed");

datafusion/core/src/datasource/physical_plan/parquet/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,11 +1603,11 @@ mod tests {
16031603
let partitioned_file = PartitionedFile {
16041604
object_meta: meta,
16051605
partition_values: vec![
1606-
ScalarValue::Utf8(Some("2021".to_owned())),
1606+
ScalarValue::from("2021"),
16071607
ScalarValue::UInt8(Some(10)),
16081608
ScalarValue::Dictionary(
16091609
Box::new(DataType::UInt16),
1610-
Box::new(ScalarValue::Utf8(Some("26".to_owned()))),
1610+
Box::new(ScalarValue::from("26")),
16111611
),
16121612
],
16131613
range: None,

datafusion/core/src/test/variable.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl VarProvider for SystemVar {
3737
/// get system variable value
3838
fn get_value(&self, var_names: Vec<String>) -> Result<ScalarValue> {
3939
let s = format!("{}-{}", "system-var", var_names.concat());
40-
Ok(ScalarValue::Utf8(Some(s)))
40+
Ok(ScalarValue::from(s))
4141
}
4242

4343
fn get_type(&self, _: &[String]) -> Option<DataType> {
@@ -61,7 +61,7 @@ impl VarProvider for UserDefinedVar {
6161
fn get_value(&self, var_names: Vec<String>) -> Result<ScalarValue> {
6262
if var_names[0] != "@integer" {
6363
let s = format!("{}-{}", "user-defined-var", var_names.concat());
64-
Ok(ScalarValue::Utf8(Some(s)))
64+
Ok(ScalarValue::from(s))
6565
} else {
6666
Ok(ScalarValue::Int32(Some(41)))
6767
}

datafusion/execution/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl SessionConfig {
8686

8787
/// Set a generic `str` configuration option
8888
pub fn set_str(self, key: &str, value: &str) -> Self {
89-
self.set(key, ScalarValue::Utf8(Some(value.to_string())))
89+
self.set(key, ScalarValue::from(value))
9090
}
9191

9292
/// Customize batch size

datafusion/expr/src/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,7 @@ impl Expr {
10441044
Expr::GetIndexedField(GetIndexedField {
10451045
expr: Box::new(self),
10461046
field: GetFieldAccess::NamedStructField {
1047-
name: ScalarValue::Utf8(Some(name.into())),
1047+
name: ScalarValue::from(name.into()),
10481048
},
10491049
})
10501050
}

0 commit comments

Comments
 (0)