datafusion_physical_expr_common/sort_expr.rs
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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//! Sort expressions
use crate::physical_expr::PhysicalExpr;
use std::fmt;
use std::fmt::{Display, Formatter};
use std::hash::{Hash, Hasher};
use std::ops::{Deref, Index, Range, RangeFrom, RangeTo};
use std::sync::Arc;
use std::vec::IntoIter;
use arrow::compute::kernels::sort::{SortColumn, SortOptions};
use arrow::datatypes::Schema;
use arrow::record_batch::RecordBatch;
use datafusion_common::Result;
use datafusion_expr_common::columnar_value::ColumnarValue;
/// Represents Sort operation for a column in a RecordBatch
///
/// Example:
/// ```
/// # use std::any::Any;
/// # use std::fmt::Display;
/// # use std::hash::Hasher;
/// # use std::sync::Arc;
/// # use arrow::array::RecordBatch;
/// # use datafusion_common::Result;
/// # use arrow::compute::SortOptions;
/// # use arrow::datatypes::{DataType, Schema};
/// # use datafusion_expr_common::columnar_value::ColumnarValue;
/// # use datafusion_physical_expr_common::physical_expr::PhysicalExpr;
/// # use datafusion_physical_expr_common::sort_expr::PhysicalSortExpr;
/// # // this crate doesn't have a physical expression implementation
/// # // so make a really simple one
/// # #[derive(Clone, Debug, PartialEq, Eq, Hash)]
/// # struct MyPhysicalExpr;
/// # impl PhysicalExpr for MyPhysicalExpr {
/// # fn as_any(&self) -> &dyn Any {todo!() }
/// # fn data_type(&self, input_schema: &Schema) -> Result<DataType> {todo!()}
/// # fn nullable(&self, input_schema: &Schema) -> Result<bool> {todo!() }
/// # fn evaluate(&self, batch: &RecordBatch) -> Result<ColumnarValue> {todo!() }
/// # fn children(&self) -> Vec<&Arc<dyn PhysicalExpr>> {todo!()}
/// # fn with_new_children(self: Arc<Self>, children: Vec<Arc<dyn PhysicalExpr>>) -> Result<Arc<dyn PhysicalExpr>> {todo!()}
/// # fn dyn_hash(&self, _state: &mut dyn Hasher) {todo!()}
/// # }
/// # impl Display for MyPhysicalExpr {
/// # fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "a") }
/// # }
/// # impl PartialEq<dyn Any> for MyPhysicalExpr {
/// # fn eq(&self, _other: &dyn Any) -> bool { true }
/// # }
/// # fn col(name: &str) -> Arc<dyn PhysicalExpr> { Arc::new(MyPhysicalExpr) }
/// // Sort by a ASC
/// let options = SortOptions::default();
/// let sort_expr = PhysicalSortExpr::new(col("a"), options);
/// assert_eq!(sort_expr.to_string(), "a ASC");
///
/// // Sort by a DESC NULLS LAST
/// let sort_expr = PhysicalSortExpr::new_default(col("a"))
/// .desc()
/// .nulls_last();
/// assert_eq!(sort_expr.to_string(), "a DESC NULLS LAST");
/// ```
#[derive(Clone, Debug)]
pub struct PhysicalSortExpr {
/// Physical expression representing the column to sort
pub expr: Arc<dyn PhysicalExpr>,
/// Option to specify how the given column should be sorted
pub options: SortOptions,
}
impl PhysicalSortExpr {
/// Create a new PhysicalSortExpr
pub fn new(expr: Arc<dyn PhysicalExpr>, options: SortOptions) -> Self {
Self { expr, options }
}
/// Create a new PhysicalSortExpr with default [`SortOptions`]
pub fn new_default(expr: Arc<dyn PhysicalExpr>) -> Self {
Self::new(expr, SortOptions::default())
}
/// Set the sort sort options to ASC
pub fn asc(mut self) -> Self {
self.options.descending = false;
self
}
/// Set the sort sort options to DESC
pub fn desc(mut self) -> Self {
self.options.descending = true;
self
}
/// Set the sort sort options to NULLS FIRST
pub fn nulls_first(mut self) -> Self {
self.options.nulls_first = true;
self
}
/// Set the sort sort options to NULLS LAST
pub fn nulls_last(mut self) -> Self {
self.options.nulls_first = false;
self
}
}
/// Access the PhysicalSortExpr as a PhysicalExpr
impl AsRef<dyn PhysicalExpr> for PhysicalSortExpr {
fn as_ref(&self) -> &(dyn PhysicalExpr + 'static) {
self.expr.as_ref()
}
}
impl PartialEq for PhysicalSortExpr {
fn eq(&self, other: &PhysicalSortExpr) -> bool {
self.options == other.options && self.expr.eq(&other.expr)
}
}
impl Eq for PhysicalSortExpr {}
impl Hash for PhysicalSortExpr {
fn hash<H: Hasher>(&self, state: &mut H) {
self.expr.hash(state);
self.options.hash(state);
}
}
impl Display for PhysicalSortExpr {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{} {}", self.expr, to_str(&self.options))
}
}
impl PhysicalSortExpr {
/// evaluate the sort expression into SortColumn that can be passed into arrow sort kernel
pub fn evaluate_to_sort_column(&self, batch: &RecordBatch) -> Result<SortColumn> {
let value_to_sort = self.expr.evaluate(batch)?;
let array_to_sort = match value_to_sort {
ColumnarValue::Array(array) => array,
ColumnarValue::Scalar(scalar) => scalar.to_array_of_size(batch.num_rows())?,
};
Ok(SortColumn {
values: array_to_sort,
options: Some(self.options),
})
}
/// Checks whether this sort expression satisfies the given `requirement`.
/// If sort options are unspecified in `requirement`, only expressions are
/// compared for inequality.
pub fn satisfy(
&self,
requirement: &PhysicalSortRequirement,
schema: &Schema,
) -> bool {
// If the column is not nullable, NULLS FIRST/LAST is not important.
let nullable = self.expr.nullable(schema).unwrap_or(true);
self.expr.eq(&requirement.expr)
&& if nullable {
requirement
.options
.map_or(true, |opts| self.options == opts)
} else {
requirement
.options
.map_or(true, |opts| self.options.descending == opts.descending)
}
}
}
/// Represents sort requirement associated with a plan
///
/// If the requirement includes [`SortOptions`] then both the
/// expression *and* the sort options must match.
///
/// If the requirement does not include [`SortOptions`]) then only the
/// expressions must match.
///
/// # Examples
///
/// With sort options (`A`, `DESC NULLS FIRST`):
/// * `ORDER BY A DESC NULLS FIRST` matches
/// * `ORDER BY A ASC NULLS FIRST` does not match (`ASC` vs `DESC`)
/// * `ORDER BY B DESC NULLS FIRST` does not match (different expr)
///
/// Without sort options (`A`, None):
/// * `ORDER BY A DESC NULLS FIRST` matches
/// * `ORDER BY A ASC NULLS FIRST` matches (`ASC` and `NULL` options ignored)
/// * `ORDER BY B DESC NULLS FIRST` does not match (different expr)
#[derive(Clone, Debug)]
pub struct PhysicalSortRequirement {
/// Physical expression representing the column to sort
pub expr: Arc<dyn PhysicalExpr>,
/// Option to specify how the given column should be sorted.
/// If unspecified, there are no constraints on sort options.
pub options: Option<SortOptions>,
}
impl From<PhysicalSortRequirement> for PhysicalSortExpr {
/// If options is `None`, the default sort options `ASC, NULLS LAST` is used.
///
/// The default is picked to be consistent with
/// PostgreSQL: <https://www.postgresql.org/docs/current/queries-order.html>
fn from(value: PhysicalSortRequirement) -> Self {
let options = value.options.unwrap_or(SortOptions {
descending: false,
nulls_first: false,
});
PhysicalSortExpr::new(value.expr, options)
}
}
impl From<PhysicalSortExpr> for PhysicalSortRequirement {
fn from(value: PhysicalSortExpr) -> Self {
PhysicalSortRequirement::new(value.expr, Some(value.options))
}
}
impl PartialEq for PhysicalSortRequirement {
fn eq(&self, other: &PhysicalSortRequirement) -> bool {
self.options == other.options && self.expr.eq(&other.expr)
}
}
impl Display for PhysicalSortRequirement {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let opts_string = self.options.as_ref().map_or("NA", to_str);
write!(f, "{} {}", self.expr, opts_string)
}
}
/// Writes a list of [`PhysicalSortRequirement`]s to a `std::fmt::Formatter`.
///
/// Example output: `[a + 1, b]`
pub fn format_physical_sort_requirement_list(
exprs: &[PhysicalSortRequirement],
) -> impl Display + '_ {
struct DisplayWrapper<'a>(&'a [PhysicalSortRequirement]);
impl<'a> Display for DisplayWrapper<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let mut iter = self.0.iter();
write!(f, "[")?;
if let Some(expr) = iter.next() {
write!(f, "{}", expr)?;
}
for expr in iter {
write!(f, ", {}", expr)?;
}
write!(f, "]")?;
Ok(())
}
}
DisplayWrapper(exprs)
}
impl PhysicalSortRequirement {
/// Creates a new requirement.
///
/// If `options` is `Some(..)`, creates an `exact` requirement,
/// which must match both `options` and `expr`.
///
/// If `options` is `None`, Creates a new `expr_only` requirement,
/// which must match only `expr`.
///
/// See [`PhysicalSortRequirement`] for examples.
pub fn new(expr: Arc<dyn PhysicalExpr>, options: Option<SortOptions>) -> Self {
Self { expr, options }
}
/// Replace the required expression for this requirement with the new one
pub fn with_expr(mut self, expr: Arc<dyn PhysicalExpr>) -> Self {
self.expr = expr;
self
}
/// Returns whether this requirement is equal or more specific than `other`.
pub fn compatible(&self, other: &PhysicalSortRequirement) -> bool {
self.expr.eq(&other.expr)
&& other.options.map_or(true, |other_opts| {
self.options.map_or(false, |opts| opts == other_opts)
})
}
/// Returns [`PhysicalSortRequirement`] that requires the exact
/// sort of the [`PhysicalSortExpr`]s in `ordering`
///
/// This method takes `&'a PhysicalSortExpr` to make it easy to
/// use implementing [`ExecutionPlan::required_input_ordering`].
///
/// [`ExecutionPlan::required_input_ordering`]: https://docs.rs/datafusion/latest/datafusion/physical_plan/trait.ExecutionPlan.html#method.required_input_ordering
pub fn from_sort_exprs<'a>(
ordering: impl IntoIterator<Item = &'a PhysicalSortExpr>,
) -> LexRequirement {
LexRequirement::new(
ordering
.into_iter()
.cloned()
.map(PhysicalSortRequirement::from)
.collect(),
)
}
/// Converts an iterator of [`PhysicalSortRequirement`] into a Vec
/// of [`PhysicalSortExpr`]s.
///
/// This function converts `PhysicalSortRequirement` to `PhysicalSortExpr`
/// for each entry in the input. If required ordering is None for an entry
/// default ordering `ASC, NULLS LAST` if given (see the `PhysicalSortExpr::from`).
pub fn to_sort_exprs(
requirements: impl IntoIterator<Item = PhysicalSortRequirement>,
) -> LexOrdering {
requirements
.into_iter()
.map(PhysicalSortExpr::from)
.collect()
}
}
/// Returns the SQL string representation of the given [SortOptions] object.
#[inline]
fn to_str(options: &SortOptions) -> &str {
match (options.descending, options.nulls_first) {
(true, true) => "DESC",
(true, false) => "DESC NULLS LAST",
(false, true) => "ASC",
(false, false) => "ASC NULLS LAST",
}
}
///`LexOrdering` contains a `Vec<PhysicalSortExpr>`, which represents
/// a lexicographical ordering.
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
pub struct LexOrdering {
pub inner: Vec<PhysicalSortExpr>,
}
impl LexOrdering {
// Creates a new [`LexOrdering`] from a vector
pub fn new(inner: Vec<PhysicalSortExpr>) -> Self {
Self { inner }
}
pub fn as_ref(&self) -> LexOrderingRef {
&self.inner
}
pub fn capacity(&self) -> usize {
self.inner.capacity()
}
pub fn clear(&mut self) {
self.inner.clear()
}
pub fn contains(&self, expr: &PhysicalSortExpr) -> bool {
self.inner.contains(expr)
}
pub fn extend<I: IntoIterator<Item = PhysicalSortExpr>>(&mut self, iter: I) {
self.inner.extend(iter)
}
pub fn from_ref(lex_ordering_ref: LexOrderingRef) -> Self {
Self::new(lex_ordering_ref.to_vec())
}
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
pub fn iter(&self) -> impl Iterator<Item = &PhysicalSortExpr> {
self.inner.iter()
}
pub fn len(&self) -> usize {
self.inner.len()
}
pub fn pop(&mut self) -> Option<PhysicalSortExpr> {
self.inner.pop()
}
pub fn push(&mut self, physical_sort_expr: PhysicalSortExpr) {
self.inner.push(physical_sort_expr)
}
pub fn retain(&mut self, f: impl FnMut(&PhysicalSortExpr) -> bool) {
self.inner.retain(f)
}
pub fn truncate(&mut self, len: usize) {
self.inner.truncate(len)
}
}
impl Deref for LexOrdering {
type Target = [PhysicalSortExpr];
fn deref(&self) -> &Self::Target {
self.inner.as_slice()
}
}
impl Display for LexOrdering {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let mut first = true;
for sort_expr in &self.inner {
if first {
first = false;
} else {
write!(f, ", ")?;
}
write!(f, "{}", sort_expr)?;
}
Ok(())
}
}
impl FromIterator<PhysicalSortExpr> for LexOrdering {
fn from_iter<T: IntoIterator<Item = PhysicalSortExpr>>(iter: T) -> Self {
let mut lex_ordering = LexOrdering::default();
for i in iter {
lex_ordering.push(i);
}
lex_ordering
}
}
impl Index<usize> for LexOrdering {
type Output = PhysicalSortExpr;
fn index(&self, index: usize) -> &Self::Output {
&self.inner[index]
}
}
impl Index<Range<usize>> for LexOrdering {
type Output = [PhysicalSortExpr];
fn index(&self, range: Range<usize>) -> &Self::Output {
&self.inner[range]
}
}
impl Index<RangeFrom<usize>> for LexOrdering {
type Output = [PhysicalSortExpr];
fn index(&self, range_from: RangeFrom<usize>) -> &Self::Output {
&self.inner[range_from]
}
}
impl Index<RangeTo<usize>> for LexOrdering {
type Output = [PhysicalSortExpr];
fn index(&self, range_to: RangeTo<usize>) -> &Self::Output {
&self.inner[range_to]
}
}
impl IntoIterator for LexOrdering {
type Item = PhysicalSortExpr;
type IntoIter = IntoIter<PhysicalSortExpr>;
fn into_iter(self) -> Self::IntoIter {
self.inner.into_iter()
}
}
///`LexOrderingRef` is an alias for the type &`[PhysicalSortExpr]`, which represents
/// a reference to a lexicographical ordering.
pub type LexOrderingRef<'a> = &'a [PhysicalSortExpr];
///`LexRequirement` is an struct containing a `Vec<PhysicalSortRequirement>`, which
/// represents a lexicographical ordering requirement.
#[derive(Debug, Default, Clone, PartialEq)]
pub struct LexRequirement {
pub inner: Vec<PhysicalSortRequirement>,
}
impl LexRequirement {
pub fn new(inner: Vec<PhysicalSortRequirement>) -> Self {
Self { inner }
}
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
pub fn iter(&self) -> impl Iterator<Item = &PhysicalSortRequirement> {
self.inner.iter()
}
pub fn push(&mut self, physical_sort_requirement: PhysicalSortRequirement) {
self.inner.push(physical_sort_requirement)
}
}
impl Deref for LexRequirement {
type Target = [PhysicalSortRequirement];
fn deref(&self) -> &Self::Target {
self.inner.as_slice()
}
}
impl FromIterator<PhysicalSortRequirement> for LexRequirement {
fn from_iter<T: IntoIterator<Item = PhysicalSortRequirement>>(iter: T) -> Self {
let mut lex_requirement = LexRequirement::new(vec![]);
for i in iter {
lex_requirement.inner.push(i);
}
lex_requirement
}
}
impl IntoIterator for LexRequirement {
type Item = PhysicalSortRequirement;
type IntoIter = IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.inner.into_iter()
}
}
///`LexRequirementRef` is an alias for the type &`[PhysicalSortRequirement]`, which
/// represents a reference to a lexicographical ordering requirement.
pub type LexRequirementRef<'a> = &'a [PhysicalSortRequirement];