Jimexist commented on a change in pull request #1141:
URL: https://github.com/apache/arrow-datafusion/pull/1141#discussion_r740232427



##########
File path: datafusion/src/datasource/listing/helpers.rs
##########
@@ -0,0 +1,722 @@
+// 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.
+
+//! Helper functions for the table implementation
+
+use std::sync::Arc;
+
+use arrow::{
+    array::{
+        Array, ArrayBuilder, ArrayRef, Date64Array, Date64Builder, StringArray,
+        StringBuilder, UInt64Array, UInt64Builder,
+    },
+    datatypes::{DataType, Field, Schema},
+    record_batch::RecordBatch,
+};
+use chrono::{TimeZone, Utc};
+use futures::{
+    stream::{self},
+    StreamExt, TryStreamExt,
+};
+use log::debug;
+
+use crate::{
+    error::Result,
+    execution::context::ExecutionContext,
+    logical_plan::{self, Expr, ExpressionVisitor, Recursion},
+    physical_plan::functions::Volatility,
+    scalar::ScalarValue,
+};
+
+use crate::datasource::{
+    object_store::{FileMeta, ObjectStore, SizedFile},
+    MemTable, PartitionedFile, PartitionedFileStream,
+};
+
+const FILE_SIZE_COLUMN_NAME: &str = "_df_part_file_size_";
+const FILE_PATH_COLUMN_NAME: &str = "_df_part_file_path_";
+const FILE_MODIFIED_COLUMN_NAME: &str = "_df_part_file_modified_";
+
+/// The `ExpressionVisitor` for `expr_applicable_for_cols`. Walks the tree to
+/// validate that the given expression is applicable with only the `col_names`
+/// set of columns.
+struct ApplicabilityVisitor<'a> {
+    col_names: &'a [String],
+    is_applicable: &'a mut bool,
+}
+
+impl ApplicabilityVisitor<'_> {
+    fn visit_volatility(self, volatility: Volatility) -> Recursion<Self> {
+        match volatility {
+            Volatility::Immutable => Recursion::Continue(self),
+            // TODO: Stable functions could be `applicable`, but that would 
require access to the context
+            Volatility::Stable | Volatility::Volatile => {
+                *self.is_applicable = false;
+                Recursion::Stop(self)
+            }
+        }
+    }
+}
+
+impl ExpressionVisitor for ApplicabilityVisitor<'_> {
+    fn pre_visit(self, expr: &Expr) -> Result<Recursion<Self>> {
+        let rec = match expr {
+            Expr::Column(logical_plan::Column { ref name, .. }) => {
+                *self.is_applicable &= self.col_names.contains(name);
+                Recursion::Stop(self) // leaf node anyway
+            }
+            Expr::Literal(_)
+            | Expr::Alias(_, _)
+            | Expr::ScalarVariable(_)
+            | Expr::Not(_)
+            | Expr::IsNotNull(_)
+            | Expr::IsNull(_)
+            | Expr::Negative(_)
+            | Expr::Cast { .. }
+            | Expr::TryCast { .. }
+            | Expr::BinaryExpr { .. }
+            | Expr::Between { .. }
+            | Expr::InList { .. }
+            | Expr::GetIndexedField { .. }
+            | Expr::Case { .. } => Recursion::Continue(self),
+
+            Expr::ScalarFunction { fun, .. } => 
self.visit_volatility(fun.volatility()),
+            Expr::ScalarUDF { fun, .. } => {
+                self.visit_volatility(fun.signature.volatility)
+            }
+
+            // TODO other expressions are not handled yet:
+            // - AGGREGATE, WINDOW and SORT should not end up in filter 
conditions, except maybe in some edge cases
+            // - Can `Wildcard` be considered as a `Literal`?
+            // - ScalarVariable could be `applicable`, but that would require 
access to the context
+            Expr::AggregateUDF { .. }
+            | Expr::AggregateFunction { .. }
+            | Expr::Sort { .. }
+            | Expr::WindowFunction { .. }
+            | Expr::Wildcard => {
+                *self.is_applicable = false;
+                Recursion::Stop(self)
+            }
+        };
+        Ok(rec)
+    }
+}
+
+/// Check whether the given expression can be resolved using only the columns 
`col_names`.
+/// This means that if this function returns true:
+/// - the table provider can filter the table partition values with this 
expression
+/// - the expression can be marked as `TableProviderFilterPushDown::Exact` 
once this filtering
+/// was performed
+pub fn expr_applicable_for_cols(col_names: &[String], expr: &Expr) -> bool {
+    let mut is_applicable = true;
+    expr.accept(ApplicabilityVisitor {
+        col_names,
+        is_applicable: &mut is_applicable,
+    })
+    .unwrap();
+    is_applicable
+}
+
+/// Partition the list of files into `n` groups
+pub fn split_files(
+    partitioned_files: Vec<PartitionedFile>,
+    n: usize,
+) -> Vec<Vec<PartitionedFile>> {
+    if partitioned_files.is_empty() {
+        return vec![];
+    }
+    let chunk_size = (partitioned_files.len() + n - 1) / n;

Review comment:
       ```suggestion
       // effectively this is div with rounding up instead of truncating
       let chunk_size = (partitioned_files.len() + n - 1) / n;
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to