Skip to content

Commit 22d4159

Browse files
committed
perf: keep CASE lazy for string-parsing casts, and give NullShortCircuit its own check
Counting a LEGACY or TRY string-to-integer cast as infallible also put CASE and IF on their eager path, which parses every row for each branch that some row chooses. With a selective predicate, that made IF about 8x slower and a three-branch CASE about 3x slower. Cast::is_infallible keeps its original set: casts that cannot fail and cost little more than reading their input. Cast::cannot_fail adds the LEGACY and TRY string-to-integer casts. case_when.rs exposes both checks. CASE uses is_infallible, so it chooses the path it chose before. NullShortCircuit uses cannot_fail, since it masks an argument only to keep it from failing on rows that Spark skips. The conditional bench gains IF and CASE shapes whose branches parse strings.
1 parent dd6da42 commit 22d4159

4 files changed

Lines changed: 122 additions & 31 deletions

File tree

‎native/spark-expr/benches/conditional.rs‎

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ type Expr = Arc<dyn PhysicalExpr>;
4545
type Shape<'a> = (&'a str, fn() -> Expr, &'a RecordBatch);
4646

4747
/// Columns shaped like the ones in `CometConditionalExpressionBenchmark`:
48-
/// `c1` a random long, `c2` an int in `0..100`, `c3` another random long, and `c4` / `c5` short
49-
/// and `c6` / `c7` long strings. `null_density` applies to every column. With `sorted`, `c1` and
50-
/// `c2` ascend through the batch, so every predicate over them selects one contiguous run of rows.
48+
/// `c1` a random long, `c2` an int in `0..100`, `c3` another random long, `c4` / `c5` short
49+
/// and `c6` / `c7` long strings, and `c8` / `c9` integers written as strings. `null_density`
50+
/// applies to every column. With `sorted`, `c1` and `c2` ascend through the batch, so every
51+
/// predicate over them selects one contiguous run of rows.
5152
fn make_batch(null_density: f32, sorted: bool) -> RecordBatch {
5253
let mut rng = StdRng::seed_from_u64(42);
5354
let mut c1: Vec<i64> = (0..NUM_ROWS).map(|_| rng.random::<i64>()).collect();
@@ -57,7 +58,7 @@ fn make_batch(null_density: f32, sorted: bool) -> RecordBatch {
5758
c2.sort_unstable();
5859
}
5960
let c3: Vec<i64> = (0..NUM_ROWS).map(|_| rng.random::<i64>()).collect();
60-
let valid: Vec<Vec<bool>> = (0..7)
61+
let valid: Vec<Vec<bool>> = (0..9)
6162
.map(|_| {
6263
(0..NUM_ROWS)
6364
.map(|_| rng.random::<f32>() >= null_density)
@@ -84,6 +85,11 @@ fn make_batch(null_density: f32, sorted: bool) -> RecordBatch {
8485
.map(|i| valid[col][i].then(|| format!("{tag}{i}-").repeat(12)))
8586
.collect()
8687
};
88+
let numeric = |col: usize, seed: usize| -> StringArray {
89+
(0..NUM_ROWS)
90+
.map(|i| valid[col][i].then(|| format!("{}", (i * 7919 + seed) % 2_000_000)))
91+
.collect()
92+
};
8793
let columns: Vec<ArrayRef> = vec![
8894
Arc::new(c1),
8995
Arc::new(c2),
@@ -92,6 +98,8 @@ fn make_batch(null_density: f32, sorted: bool) -> RecordBatch {
9298
Arc::new(short(4, "t")),
9399
Arc::new(long(5, "long value ")),
94100
Arc::new(long(6, "other value ")),
101+
Arc::new(numeric(7, 17)),
102+
Arc::new(numeric(8, 4242)),
95103
];
96104
RecordBatch::try_new(Arc::new(schema()), columns).unwrap()
97105
}
@@ -105,6 +113,8 @@ fn schema() -> Schema {
105113
Field::new("c5", DataType::Utf8, true),
106114
Field::new("c6", DataType::Utf8, true),
107115
Field::new("c7", DataType::Utf8, true),
116+
Field::new("c8", DataType::Utf8, true),
117+
Field::new("c9", DataType::Utf8, true),
108118
])
109119
}
110120

@@ -309,13 +319,45 @@ fn case_fallible_branch() -> Expr {
309319
)
310320
}
311321

322+
/// A string-to-integer cast with ANSI off, which returns NULL for a string it cannot parse.
323+
fn parse_int(column: &str) -> Expr {
324+
spark_cast(col(column), DataType::Int32)
325+
}
326+
327+
/// A selective predicate whose branch parses a string: 5% of the rows choose it.
328+
fn if_selective_parse() -> Expr {
329+
if_expr(c2_below(5), parse_int("c8"), lit(ScalarValue::Int32(None)))
330+
}
331+
332+
/// A predicate that most rows match, with a branch that parses a string.
333+
fn if_mostly_parse() -> Expr {
334+
if_expr(c2_below(95), parse_int("c8"), lit(ScalarValue::Int32(None)))
335+
}
336+
337+
/// Both branches parse a string, and half of the rows choose each.
338+
fn if_parse_either() -> Expr {
339+
if_expr(c2_below(50), parse_int("c8"), parse_int("c9"))
340+
}
341+
342+
/// Three selective branches, each parsing a string for 2% of the rows.
343+
fn case_selective_parse_3_branches() -> Expr {
344+
case_when(
345+
vec![
346+
(c2_below(2), parse_int("c8")),
347+
(c2_below(4), parse_int("c9")),
348+
(c2_below(6), parse_int("c8")),
349+
],
350+
None,
351+
)
352+
}
353+
312354
fn criterion_benchmark(c: &mut Criterion) {
313355
let no_nulls = make_batch(0.0, false);
314356
let sparse_nulls = make_batch(0.1, false);
315357
let dense_nulls = make_batch(0.9, false);
316358
let sorted = make_batch(0.0, true);
317359

318-
let shapes: [Shape; 26] = [
360+
let shapes: [Shape; 30] = [
319361
(
320362
"case literal 3 branches",
321363
case_literal_3_branches,
@@ -374,6 +416,14 @@ fn criterion_benchmark(c: &mut Criterion) {
374416
case_column_10_branches,
375417
&sorted,
376418
),
419+
("if selective parse", if_selective_parse, &no_nulls),
420+
("if mostly parse", if_mostly_parse, &no_nulls),
421+
("if parse either", if_parse_either, &no_nulls),
422+
(
423+
"case selective parse 3 branches",
424+
case_selective_parse_3_branches,
425+
&no_nulls,
426+
),
377427
];
378428
let mut group = c.benchmark_group("conditional");
379429
group.throughput(Throughput::Elements(NUM_ROWS as u64));

‎native/spark-expr/src/conditional_funcs/case_when.rs‎

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -385,12 +385,27 @@ impl PhysicalExpr for CaseWhenExpr {
385385
}
386386
}
387387

388-
/// Whether `expr` can be evaluated for rows that Spark would not evaluate it for.
388+
/// Whether `expr` can be evaluated for rows that Spark would not evaluate it for, at little cost.
389389
///
390-
/// That holds when it can neither fail nor return something different for seeing more rows: a
391-
/// column, a literal, and comparisons, boolean logic, null checks, widening casts and wrapping
392-
/// arithmetic over them. Anything else, including every function, is assumed to be able to fail.
390+
/// That holds when it can neither fail nor return something different for seeing more rows, and is
391+
/// cheap: a column, a literal, and comparisons, boolean logic, null checks, widening casts and
392+
/// wrapping arithmetic over them. Anything else, including every function and a cast that parses a
393+
/// string, is treated as able to fail. `CASE` uses this to decide whether to evaluate every branch
394+
/// over the whole batch, which pays off only when that costs little more than selecting rows.
393395
pub(super) fn is_infallible(expr: &Arc<dyn PhysicalExpr>, input_schema: &Schema) -> bool {
396+
infallible(expr, input_schema, false)
397+
}
398+
399+
/// Whether `expr` can be evaluated for rows that Spark would not evaluate it for, at any cost: what
400+
/// [`is_infallible`] accepts, and casts that [`Cast::cannot_fail`] accepts beyond it, such as a
401+
/// LEGACY or TRY cast from a string to an integer, which returns NULL for a string it cannot parse.
402+
/// `NullShortCircuit` uses this, since it masks an argument only to keep it from failing on rows
403+
/// that Spark skips.
404+
pub(super) fn cannot_fail(expr: &Arc<dyn PhysicalExpr>, input_schema: &Schema) -> bool {
405+
infallible(expr, input_schema, true)
406+
}
407+
408+
fn infallible(expr: &Arc<dyn PhysicalExpr>, input_schema: &Schema, parsing: bool) -> bool {
394409
if expr.is::<Column>() || expr.is::<Literal>() {
395410
return true;
396411
}
@@ -399,7 +414,11 @@ pub(super) fn is_infallible(expr: &Arc<dyn PhysicalExpr>, input_schema: &Schema)
399414
} else if let Some(comparison) = expr.downcast_ref::<SparkComparison>() {
400415
comparison.is_infallible(input_schema)
401416
} else if let Some(cast) = expr.downcast_ref::<Cast>() {
402-
cast.is_infallible(input_schema)
417+
if parsing {
418+
cast.cannot_fail(input_schema)
419+
} else {
420+
cast.is_infallible(input_schema)
421+
}
403422
} else if let Some(normalize) = expr.downcast_ref::<NormalizeNaNAndZero>() {
404423
normalize.is_infallible()
405424
} else if let Some(not) = expr.downcast_ref::<NotExpr>() {
@@ -417,7 +436,7 @@ pub(super) fn is_infallible(expr: &Arc<dyn PhysicalExpr>, input_schema: &Schema)
417436
&& expr
418437
.children()
419438
.into_iter()
420-
.all(|child| is_infallible(child, input_schema))
439+
.all(|child| infallible(child, input_schema, parsing))
421440
}
422441

423442
fn binary_is_infallible(binary: &BinaryExpr, input_schema: &Schema) -> bool {
@@ -1231,29 +1250,28 @@ mod tests {
12311250
// Narrowing and parsing casts can fail under ANSI
12321251
assert!(!infallible(cast(c("i"), DataType::Int32)));
12331252
assert!(!infallible(cast(c("s"), DataType::Int32)));
1234-
// A string that LEGACY or TRY mode cannot parse as an integer becomes NULL instead
1253+
// A string that LEGACY or TRY mode cannot parse as an integer becomes NULL, so the cast
1254+
// cannot fail. Parsing costs too much for CASE to do it for rows it would skip, though.
12351255
for mode in [EvalMode::Legacy, EvalMode::Try] {
12361256
for to in [
12371257
DataType::Int8,
12381258
DataType::Int16,
12391259
DataType::Int32,
12401260
DataType::Int64,
12411261
] {
1242-
assert!(
1243-
infallible(cast_in(mode, c("s"), to.clone())),
1244-
"{mode:?} {to}"
1245-
);
1246-
assert!(
1247-
infallible(cast_in(mode, c("ls"), to.clone())),
1248-
"{mode:?} {to}"
1249-
);
1262+
for column in ["s", "ls"] {
1263+
let parse = cast_in(mode, c(column), to.clone());
1264+
assert!(!infallible(Arc::clone(&parse)), "{mode:?} {column} {to}");
1265+
assert!(cannot_fail(&parse, &schema), "{mode:?} {column} {to}");
1266+
}
12501267
}
12511268
// Other string casts are not claimed
12521269
assert!(
1253-
!infallible(cast_in(mode, c("s"), DataType::Float64)),
1270+
!cannot_fail(&cast_in(mode, c("s"), DataType::Float64), &schema),
12541271
"{mode:?}"
12551272
);
12561273
}
1274+
assert!(!cannot_fail(&cast(c("s"), DataType::Int32), &schema));
12571275
// So can anything built from a part that can fail
12581276
assert!(!infallible(binary(
12591277
binary(c("i"), Operator::Divide, c("i")),

‎native/spark-expr/src/conditional_funcs/null_short_circuit.rs‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
use super::case_when::is_infallible;
18+
use super::case_when::cannot_fail;
1919
use arrow::array::{new_null_array, Array, BooleanArray};
2020
use arrow::buffer::NullBuffer;
2121
use arrow::datatypes::{DataType, FieldRef, Schema};
@@ -66,7 +66,7 @@ impl NullShortCircuit {
6666
.children()
6767
.iter()
6868
.enumerate()
69-
.map(|(i, arg)| i > 0 && !is_infallible(arg, input_schema))
69+
.map(|(i, arg)| i > 0 && !cannot_fail(arg, input_schema))
7070
.collect::<Vec<_>>();
7171
if !masked.contains(&true) {
7272
return Ok(inner);
@@ -487,6 +487,19 @@ mod tests {
487487
);
488488
let wrapped = NullShortCircuit::wrap(Arc::clone(&widened), &schema).unwrap();
489489
assert!(Arc::ptr_eq(&widened, &wrapped));
490+
// With ANSI off, a string that the cast cannot parse becomes NULL, so it cannot fail either
491+
let parsed = array_position(
492+
col("a", &schema).unwrap(),
493+
Arc::new(Cast::new(
494+
col("s", &schema).unwrap(),
495+
DataType::Int32,
496+
SparkCastOptions::new_without_timezone(EvalMode::Legacy, false),
497+
None,
498+
None,
499+
)),
500+
);
501+
let wrapped = NullShortCircuit::wrap(Arc::clone(&parsed), &schema).unwrap();
502+
assert!(Arc::ptr_eq(&parsed, &wrapped));
490503
}
491504

492505
#[test]

‎native/spark-expr/src/conversion_funcs/cast.rs‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,9 @@ impl Cast {
119119
}
120120
}
121121

122-
/// Whether the cast cannot fail for any value: a cast to the child's own type, from NULL, or to
123-
/// a wider number, in any eval mode, or a cast from a string to an integer in LEGACY or TRY
124-
/// mode, which returns NULL for a string it cannot parse. `cast_array` rejects a dictionary
125-
/// target first.
122+
/// Whether the cast cannot fail for any value, in any eval mode, and costs little more than
123+
/// reading its input: a cast to the child's own type, from NULL, or to a wider number.
124+
/// `cast_array` rejects a dictionary target first.
126125
pub(crate) fn is_infallible(&self, input_schema: &Schema) -> bool {
127126
use DataType::*;
128127
let Ok(from) = self.child.data_type(input_schema) else {
@@ -138,10 +137,21 @@ impl Cast {
138137
| (Int32, Int64 | Float32 | Float64)
139138
| (Int64, Float32 | Float64)
140139
| (Float32, Float64)
141-
)
142-
|| (matches!(from, Utf8 | LargeUtf8)
143-
&& matches!(self.data_type, Int8 | Int16 | Int32 | Int64)
144-
&& self.cast_options.eval_mode != EvalMode::Ansi))
140+
))
141+
}
142+
143+
/// Whether the cast cannot fail for any value: what [`Self::is_infallible`] accepts, and a cast
144+
/// from a string to an integer in LEGACY or TRY mode, which returns NULL for a string it cannot
145+
/// parse. Parsing costs far more per row than those casts.
146+
pub(crate) fn cannot_fail(&self, input_schema: &Schema) -> bool {
147+
use DataType::*;
148+
self.is_infallible(input_schema)
149+
|| (self
150+
.child
151+
.data_type(input_schema)
152+
.is_ok_and(|from| matches!(from, Utf8 | LargeUtf8))
153+
&& matches!(self.data_type, Int8 | Int16 | Int32 | Int64)
154+
&& self.cast_options.eval_mode != EvalMode::Ansi)
145155
}
146156
}
147157

0 commit comments

Comments
 (0)