1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
use std::convert::TryFrom;
use std::sync::Arc;
use polars_core::frame::groupby::GroupsProxy;
use polars_core::prelude::*;
use polars_core::series::unstable::UnstableSeries;
use polars_core::POOL;
use rayon::prelude::*;
use crate::physical_plan::state::{ExecutionState, StateFlags};
use crate::prelude::*;
pub struct BinaryExpr {
pub(crate) left: Arc<dyn PhysicalExpr>,
pub(crate) op: Operator,
pub(crate) right: Arc<dyn PhysicalExpr>,
expr: Expr,
}
impl BinaryExpr {
pub fn new(
left: Arc<dyn PhysicalExpr>,
op: Operator,
right: Arc<dyn PhysicalExpr>,
expr: Expr,
) -> Self {
Self {
left,
op,
right,
expr,
}
}
}
/// Can partially do operations in place.
fn apply_operator_owned(left: Series, right: Series, op: Operator) -> PolarsResult<Series> {
match op {
Operator::Gt => ChunkCompare::<&Series>::gt(&left, &right).map(|ca| ca.into_series()),
Operator::GtEq => ChunkCompare::<&Series>::gt_eq(&left, &right).map(|ca| ca.into_series()),
Operator::Lt => ChunkCompare::<&Series>::lt(&left, &right).map(|ca| ca.into_series()),
Operator::LtEq => ChunkCompare::<&Series>::lt_eq(&left, &right).map(|ca| ca.into_series()),
Operator::Eq => ChunkCompare::<&Series>::equal(&left, &right).map(|ca| ca.into_series()),
Operator::NotEq => {
ChunkCompare::<&Series>::not_equal(&left, &right).map(|ca| ca.into_series())
}
Operator::Plus => Ok(left + right),
Operator::Minus => Ok(left - right),
Operator::Multiply => Ok(left * right),
Operator::Divide => Ok(&left / &right),
Operator::TrueDivide => apply_operator(&left, &right, op),
Operator::FloorDivide => apply_operator(&left, &right, op),
Operator::And => left.bitand(&right),
Operator::Or => left.bitor(&right),
Operator::Xor => left.bitxor(&right),
Operator::Modulus => Ok(&left % &right),
}
}
pub fn apply_operator(left: &Series, right: &Series, op: Operator) -> PolarsResult<Series> {
match op {
Operator::Gt => ChunkCompare::<&Series>::gt(left, right).map(|ca| ca.into_series()),
Operator::GtEq => ChunkCompare::<&Series>::gt_eq(left, right).map(|ca| ca.into_series()),
Operator::Lt => ChunkCompare::<&Series>::lt(left, right).map(|ca| ca.into_series()),
Operator::LtEq => ChunkCompare::<&Series>::lt_eq(left, right).map(|ca| ca.into_series()),
Operator::Eq => ChunkCompare::<&Series>::equal(left, right).map(|ca| ca.into_series()),
Operator::NotEq => {
ChunkCompare::<&Series>::not_equal(left, right).map(|ca| ca.into_series())
}
Operator::Plus => Ok(left + right),
Operator::Minus => Ok(left - right),
Operator::Multiply => Ok(left * right),
Operator::Divide => Ok(left / right),
Operator::TrueDivide => match left.dtype() {
DataType::Date | DataType::Datetime(_, _) | DataType::Float32 | DataType::Float64 => {
Ok(left / right)
}
_ => Ok(&left.cast(&DataType::Float64)? / &right.cast(&DataType::Float64)?),
},
Operator::FloorDivide => match left.dtype() {
#[cfg(feature = "round_series")]
DataType::Float32 | DataType::Float64 => (left / right).floor(),
_ => Ok(left / right),
},
Operator::And => left.bitand(right),
Operator::Or => left.bitor(right),
Operator::Xor => left.bitxor(right),
Operator::Modulus => Ok(left % right),
}
}
impl PhysicalExpr for BinaryExpr {
fn as_expression(&self) -> Option<&Expr> {
Some(&self.expr)
}
fn evaluate(&self, df: &DataFrame, state: &ExecutionState) -> PolarsResult<Series> {
let mut state = state.split();
// don't cache window functions as they run in parallel
state.flags.remove(StateFlags::CACHE_WINDOW_EXPR);
let (lhs, rhs) = POOL.install(|| {
rayon::join(
|| self.left.evaluate(df, &state),
|| self.right.evaluate(df, &state),
)
});
apply_operator_owned(lhs?, rhs?, self.op)
}
#[allow(clippy::ptr_arg)]
fn evaluate_on_groups<'a>(
&self,
df: &DataFrame,
groups: &'a GroupsProxy,
state: &ExecutionState,
) -> PolarsResult<AggregationContext<'a>> {
let (result_a, result_b) = POOL.install(|| {
rayon::join(
|| self.left.evaluate_on_groups(df, groups, state),
|| self.right.evaluate_on_groups(df, groups, state),
)
});
let mut ac_l = result_a?;
let mut ac_r = result_b?;
match (
ac_l.agg_state(),
ac_r.agg_state(),
state.overlapping_groups(),
) {
// Some aggregations must return boolean masks that fit the group. That's why not all literals can take this path.
// only literals that are used in arithmetic
(
AggState::AggregatedFlat(lhs) | AggState::Literal(lhs),
AggState::AggregatedFlat(rhs) | AggState::Literal(rhs),
_,
) => {
// we want to be able to mutate in place
// so we take the lhs to make sure that we drop
let lhs = lhs.clone();
let rhs = rhs.clone();
// drop lhs so that we might operate in place
{
let _ = ac_l.take();
}
let out = apply_operator_owned(lhs, rhs, self.op)?;
ac_l.with_series(out, true);
Ok(ac_l)
}
// One of the two exprs is aggregated with flat aggregation, e.g. `e.min(), e.max(), e.first()`
// if the groups_len == df.len we can just apply all flat.
// within an aggregation a `col().first() - lit(0)` must still produce a boolean array of group length,
// that's why a literal also takes this branch
(AggState::AggregatedFlat(s), AggState::NotAggregated(_), _overlapping_groups)
if s.len() != df.height() =>
{
// this is a flat series of len eq to group tuples
let l = ac_l.aggregated_arity_operation();
let l = l.as_ref();
let arr_l = &l.chunks()[0];
// we create a dummy Series that is not cloned nor moved
// so we can swap the ArrayRef during the hot loop
// this prevents a series Arc alloc and a vec alloc per iteration
let dummy = Series::try_from(("dummy", vec![arr_l.clone()])).unwrap();
// keep logical type info
let mut dummy = dummy.cast(l.dtype()).unwrap();
let mut us = UnstableSeries::new(&mut dummy);
// this is now a list
let r = ac_r.aggregated_arity_operation();
let r = r.list().unwrap();
let mut ca: ListChunked = r
.amortized_iter()
.enumerate()
.map(|(idx, opt_s)| {
opt_s
.map(|s| {
let r = s.as_ref();
// TODO: optimize this?
// Safety:
// we are in bounds
let mut arr = unsafe { arr_l.slice_unchecked(idx, 1) };
us.swap(&mut arr);
let l = us.as_ref();
apply_operator(l, r, self.op)
})
.transpose()
})
.collect::<PolarsResult<_>>()?;
ca.rename(l.name());
ac_l.with_series(ca.into_series(), true);
ac_l.with_update_groups(UpdateGroups::WithGroupsLen);
Ok(ac_l)
}
// if the groups_len == df.len we can just apply all flat.
(
AggState::AggregatedList(_) | AggState::NotAggregated(_),
AggState::AggregatedFlat(s),
_overlapping_groups,
) if s.len() != df.height() => {
// this is now a list
let l = ac_l.aggregated_arity_operation();
let l = l.list().unwrap();
// this is a flat series of len eq to group tuples
let r = ac_r.aggregated_arity_operation();
assert_eq!(l.len(), groups.len());
let r = r.as_ref();
let arr_r = &r.chunks()[0];
// we create a dummy Series that is not cloned nor moved
// so we can swap the ArrayRef during the hot loop
// this prevents a series Arc alloc and a vec alloc per iteration
let dummy = Series::try_from(("dummy", vec![arr_r.clone()])).unwrap();
// keep logical type info
let mut dummy = dummy.cast(r.dtype()).unwrap();
let mut us = UnstableSeries::new(&mut dummy);
let mut ca: ListChunked = l
.amortized_iter()
.enumerate()
.map(|(idx, opt_s)| {
opt_s
.map(|s| {
let l = s.as_ref();
// TODO: optimize this? Its slow.
// Safety:
// we are in bounds
let mut arr = unsafe { arr_r.slice_unchecked(idx, 1) };
us.swap(&mut arr);
let r = us.as_ref();
apply_operator(l, r, self.op)
})
.transpose()
})
.collect::<PolarsResult<_>>()?;
ca.rename(l.name());
ac_l.with_series(ca.into_series(), true);
// Todo! maybe always update with groups len here?
if matches!(ac_l.update_groups, UpdateGroups::WithSeriesLen)
|| matches!(ac_r.update_groups, UpdateGroups::WithSeriesLen)
{
ac_l.with_update_groups(UpdateGroups::WithSeriesLen);
} else {
ac_l.with_update_groups(UpdateGroups::WithGroupsLen);
}
Ok(ac_l)
}
// # Align data in sort order and apply flattened.
// 1 we sort/aggregate by groups
// 2 then we flatten/explode and do the binary operation.
// 3 then we use the original groups length to restore the groups
//
// Overlapping groups may not take this branch.
// when groups overlap, step 2 creates more values than rows
// and the original group lengths will be incorrect
(
AggState::AggregatedList(_) | AggState::AggregatedFlat(_),
AggState::NotAggregated(_) | AggState::Literal(_),
false,
)
| (
AggState::NotAggregated(_) | AggState::Literal(_),
AggState::AggregatedList(_) | AggState::AggregatedFlat(_),
false,
) => {
ac_l.sort_by_groups();
ac_r.sort_by_groups();
let lhs = ac_l.flat_naive().as_ref().clone();
let rhs = ac_r.flat_naive().as_ref().clone();
// drop lhs so that we might operate in place
{
let _ = ac_l.take();
}
let out = apply_operator_owned(lhs, rhs, self.op)?;
// we flattened the series, so that sorts by group
ac_l.with_update_groups(UpdateGroups::WithGroupsLen);
ac_l.with_series(out, false);
Ok(ac_l)
}
// # Flatten the Series and apply the operators.
//
// Overlapping groups may not take this branch.
// the explode call would create more data and is expensive
(AggState::AggregatedList(_), AggState::AggregatedList(_), false) => {
let lhs = ac_l.flat_naive().as_ref().clone();
let rhs = ac_r.flat_naive().as_ref().clone();
// drop lhs so that we might operate in place
{
let _ = ac_l.take();
}
let out = apply_operator_owned(lhs, rhs, self.op)?;
ac_l.combine_groups(ac_r).with_series(out, false);
ac_l.with_update_groups(UpdateGroups::WithGroupsLen);
Ok(ac_l)
}
// Both are or a flat series (if groups do not overlap)
// so we can flatten the Series and apply the operators
(_l, _r, false) => {
// Check if the group state of `ac_a` differs from the original `GroupTuples`.
// If this is the case we might need to align the groups. But only if `ac_b` is not a
// `Literal` as literals don't have any groups, the changed group order does not matter
// in that case
let different_group_state =
|ac_a: &AggregationContext, ac_b: &AggregationContext| {
(ac_a.update_groups != UpdateGroups::No)
&& !matches!(ac_b.agg_state(), AggState::Literal(_))
};
// the groups state differs, so we aggregate both and flatten again to make them align
if different_group_state(&ac_l, &ac_r) || different_group_state(&ac_r, &ac_l) {
// use the aggregated state to determine the new groups
let lhs = ac_l.aggregated();
ac_l.with_update_groups(UpdateGroups::WithSeriesLenOwned(lhs.clone()));
// we should only explode lists
// not aggregated flat states
let flatten = |s: Series| match s.dtype() {
DataType::List(_) => s.explode(),
_ => Ok(s),
};
let out =
apply_operator(&flatten(lhs)?, &flatten(ac_r.aggregated())?, self.op)?;
ac_l.with_series(out, false);
Ok(ac_l)
} else {
let lhs = ac_l.flat_naive().as_ref().clone();
let rhs = ac_r.flat_naive().as_ref().clone();
// drop lhs so that we might operate in place
{
let _ = ac_l.take();
}
let out = apply_operator_owned(lhs, rhs, self.op)?;
ac_l.combine_groups(ac_r).with_series(out, false);
Ok(ac_l)
}
}
// overlapping groups, we iterate the separate groups, so that we don't have to explode
// If both sides are aggregated to a list, we can apply in parallel
(AggState::AggregatedList(_), AggState::AggregatedList(_), true) => {
let l = ac_l.aggregated();
let r = ac_r.aggregated();
let mut l = l.list()?.clone();
let mut r = r.list()?.clone();
let mut out = POOL.install(|| {
l.par_iter_indexed()
.zip(r.par_iter_indexed())
.map(|(opt_l, opt_r)| match (opt_l, opt_r) {
(Some(l), Some(r)) => apply_operator(&l, &r, self.op).map(Some),
_ => Ok(None),
})
.collect::<PolarsResult<ListChunked>>()
})?;
out.rename(ac_l.series().name());
ac_l.with_series(out.into_series(), true);
Ok(ac_l)
}
// overlapping groups, we iterate the separate groups, so that we don't have to explode
(_l, _r, true) => {
let mut out = ac_l
.iter_groups()
.zip(ac_r.iter_groups())
.map(|(opt_l, opt_r)| match (opt_l, opt_r) {
(Some(l), Some(r)) => {
apply_operator(l.as_ref(), r.as_ref(), self.op).map(Some)
}
_ => Ok(None),
})
.collect::<PolarsResult<ListChunked>>()?;
out.rename(ac_l.series().name());
ac_l.with_series(out.into_series(), true);
Ok(ac_l)
}
}
}
fn to_field(&self, input_schema: &Schema) -> PolarsResult<Field> {
self.expr.to_field(input_schema, Context::Default)
}
fn as_partitioned_aggregator(&self) -> Option<&dyn PartitionedAggregation> {
Some(self)
}
#[cfg(feature = "parquet")]
fn as_stats_evaluator(&self) -> Option<&dyn polars_io::predicates::StatsEvaluator> {
Some(self)
}
fn is_valid_aggregation(&self) -> bool {
// we don't want:
// col(a) == lit(1)
// we do want
// col(a).sum() == lit(1)
(!self.left.is_literal() && self.left.is_valid_aggregation())
| (!self.right.is_literal() && self.right.is_valid_aggregation())
}
}
#[cfg(feature = "parquet")]
mod stats {
use polars_io::parquet::predicates::BatchStats;
use polars_io::predicates::StatsEvaluator;
use super::*;
fn apply_operator_stats_rhs_lit(min_max: &Series, literal: &Series, op: Operator) -> bool {
match op {
// col > lit
// e.g.
// [min,
// max] > 0
//
// [-1,
// 2] > 0
//
// [false, true] -> true -> read
Operator::Gt => {
// literal is bigger than max value
// selection needs all rows
ChunkCompare::<&Series>::gt(min_max, literal)
.ok()
.map(|s| s.any())
== Some(true)
}
// col >= lit
Operator::GtEq => {
// literal is bigger than max value
// selection needs all rows
ChunkCompare::<&Series>::gt_eq(min_max, literal)
.ok()
.map(|ca| ca.any())
== Some(true)
}
// col < lit
Operator::Lt => {
// literal is smaller than min value
// selection needs all rows
ChunkCompare::<&Series>::lt(min_max, literal)
.ok()
.map(|ca| ca.any())
== Some(true)
}
// col <= lit
Operator::LtEq => {
// literal is smaller than min value
// selection needs all rows
ChunkCompare::<&Series>::lt_eq(min_max, literal)
.ok()
.map(|ca| ca.any())
== Some(true)
}
// default: read the file
_ => true,
}
}
fn apply_operator_stats_lhs_lit(literal: &Series, min_max: &Series, op: Operator) -> bool {
match op {
Operator::Gt => {
// literal is bigger than max value
// selection needs all rows
ChunkCompare::<&Series>::gt(literal, min_max)
.ok()
.map(|ca| ca.any())
== Some(true)
}
Operator::GtEq => {
// literal is bigger than max value
// selection needs all rows
ChunkCompare::<&Series>::gt_eq(literal, min_max)
.ok()
.map(|ca| ca.any())
== Some(true)
}
Operator::Lt => {
// literal is smaller than min value
// selection needs all rows
ChunkCompare::<&Series>::lt(literal, min_max)
.ok()
.map(|ca| ca.any())
== Some(true)
}
Operator::LtEq => {
// literal is smaller than min value
// selection needs all rows
ChunkCompare::<&Series>::lt_eq(literal, min_max)
.ok()
.map(|ca| ca.any())
== Some(true)
}
// default: read the file
_ => true,
}
}
impl BinaryExpr {
fn impl_should_read(&self, stats: &BatchStats) -> PolarsResult<bool> {
// See: #5864 for the rationale behind this.
use Expr::*;
use Operator::*;
if !self.expr.into_iter().all(|e| match e {
BinaryExpr { op, .. } => !matches!(
op,
Multiply | Divide | TrueDivide | FloorDivide | Modulus | Eq | NotEq
),
Column(_) | Literal(_) | Alias(_, _) => true,
_ => false,
}) {
return Ok(true);
}
let schema = stats.schema();
let fld_l = self.left.to_field(schema)?;
let fld_r = self.right.to_field(schema)?;
#[cfg(debug_assertions)]
{
match (fld_l.data_type(), fld_r.data_type()) {
#[cfg(feature = "dtype-categorical")]
(DataType::Utf8, DataType::Categorical(_)) => {}
#[cfg(feature = "dtype-categorical")]
(DataType::Categorical(_), DataType::Utf8) => {}
(l, r) if l != r => panic!("implementation error: {l:?}, {r:?}"),
_ => {}
}
}
let dummy = DataFrame::new_no_checks(vec![]);
let state = ExecutionState::new();
let out = match (self.left.is_literal(), self.right.is_literal()) {
(false, true) => {
let l = stats.get_stats(fld_l.name())?;
match l.to_min_max() {
None => Ok(true),
Some(min_max_s) => {
// will be incorrect if not
debug_assert_eq!(min_max_s.null_count(), 0);
let lit_s = self.right.evaluate(&dummy, &state).unwrap();
Ok(apply_operator_stats_rhs_lit(&min_max_s, &lit_s, self.op))
}
}
}
(true, false) => {
let r = stats.get_stats(fld_r.name())?;
match r.to_min_max() {
None => Ok(true),
Some(min_max_s) => {
// will be incorrect if not
debug_assert_eq!(min_max_s.null_count(), 0);
let lit_s = self.left.evaluate(&dummy, &state).unwrap();
Ok(apply_operator_stats_lhs_lit(&lit_s, &min_max_s, self.op))
}
}
}
// default: read the file
_ => Ok(true),
};
out.map(|read| {
if state.verbose() && read {
eprintln!("parquet file must be read, statistics not sufficient for predicate.")
} else if state.verbose() && !read {
eprintln!("parquet file can be skipped, the statistics were sufficient to apply the predicate.")
};
read
})
}
}
impl StatsEvaluator for BinaryExpr {
fn should_read(&self, stats: &BatchStats) -> PolarsResult<bool> {
if std::env::var("POLARS_NO_PARQUET_STATISTICS").is_ok() {
return Ok(true);
}
match (
self.left.as_stats_evaluator(),
self.right.as_stats_evaluator(),
) {
(Some(l), Some(r)) => match self.op {
Operator::And => Ok(l.should_read(stats)? && r.should_read(stats)?),
Operator::Or => Ok(l.should_read(stats)? || r.should_read(stats)?),
_ => Ok(true),
},
_ => self.impl_should_read(stats),
}
}
}
}
impl PartitionedAggregation for BinaryExpr {
fn evaluate_partitioned(
&self,
df: &DataFrame,
groups: &GroupsProxy,
state: &ExecutionState,
) -> PolarsResult<Series> {
let left = self.left.as_partitioned_aggregator().unwrap();
let right = self.right.as_partitioned_aggregator().unwrap();
let left = left.evaluate_partitioned(df, groups, state)?;
let right = right.evaluate_partitioned(df, groups, state)?;
apply_operator(&left, &right, self.op)
}
fn finalize(
&self,
partitioned: Series,
_groups: &GroupsProxy,
_state: &ExecutionState,
) -> PolarsResult<Series> {
Ok(partitioned)
}
}