etseidl commented on code in PR #8524:
URL: https://github.com/apache/arrow-rs/pull/8524#discussion_r2415026920


##########
parquet/src/geospatial/accumulator.rs:
##########
@@ -0,0 +1,385 @@
+// 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.
+
+//! This module provides implementations and traits for building 
[`GeospatialStatistics`]
+
+use std::sync::{Arc, OnceLock};
+
+use crate::{
+    basic::LogicalType, errors::ParquetError, 
geospatial::statistics::GeospatialStatistics,
+    schema::types::ColumnDescPtr,
+};
+
+/// Create a new [`GeoStatsAccumulator`] instance if `descr` represents a 
Geometry or
+/// Geography [`LogicalType`]
+///
+/// Returns a suitable [`GeoStatsAccumulator`] if `descr` represents a 
non-geospatial type
+/// or `None` otherwise.
+pub fn try_new_geo_stats_accumulator(
+    descr: &ColumnDescPtr,
+) -> Option<Box<dyn GeoStatsAccumulator>> {
+    if !matches!(
+        descr.logical_type(),
+        Some(LogicalType::Geometry { .. }) | Some(LogicalType::Geography { .. 
})
+    ) {
+        return None;
+    }
+
+    Some(
+        ACCUMULATOR_FACTORY
+            .get_or_init(|| 
Arc::new(DefaultGeoStatsAccumulatorFactory::default()))
+            .new_accumulator(descr),
+    )
+}
+
+/// Initialize the global [`GeoStatsAccumulatorFactory`]
+///
+/// This may only be done once before any calls to 
[`try_new_geo_stats_accumulator`].
+/// Clients may use this to implement support for builds of the Parquet crate 
without
+/// geospatial support or to implement support for Geography bounding using 
external
+/// dependencies.
+pub fn init_geo_stats_accumulator_factory(
+    factory: Arc<dyn GeoStatsAccumulatorFactory>,
+) -> Result<(), ParquetError> {
+    if ACCUMULATOR_FACTORY.set(factory).is_err() {
+        Err(ParquetError::General(
+            "Global GeoStatsAccumulatorFactory already set".to_string(),
+        ))
+    } else {
+        Ok(())
+    }
+}
+
+/// Global accumulator factory instance
+static ACCUMULATOR_FACTORY: OnceLock<Arc<dyn GeoStatsAccumulatorFactory>> = 
OnceLock::new();
+
+/// Factory for [`GeospatialStatistics`] accumulators
+///
+/// The GeoStatsAccumulatorFactory is a trait implemented by the global 
factory that
+/// generates new instances of a [GeoStatsAccumulator] when constructing new
+/// encoders for a Geometry or Geography logical type.
+pub trait GeoStatsAccumulatorFactory: Send + Sync {
+    /// Create a new [GeoStatsAccumulator] appropriate for the logical type of 
a given
+    /// [ColumnDescPtr]
+    fn new_accumulator(&self, descr: &ColumnDescPtr) -> Box<dyn 
GeoStatsAccumulator>;
+}
+
+/// Dynamic [`GeospatialStatistics``] accumulator
+///
+/// The GeoStatsAccumulator is a trait whose implementors can ingest the 
(non-null)
+/// elements of a column and return compliant [`GeospatialStatistics`] (or 
`None`).
+/// When built with geospatial support this will usually be the
+/// [ParquetGeoStatsAccumulator]
+pub trait GeoStatsAccumulator: Send {
+    /// Returns true if this instance can return [`GeospatialStatistics`] from
+    /// [GeoStatsAccumulator::finish].
+    ///
+    /// This method returns false when this crate was built without geospatial 
support
+    /// (i.e., from the [VoidGeoStatsAccumulator]) or if the accumulator 
encountered

Review Comment:
   ```suggestion
       /// This method returns false when this crate is built without 
geospatial support
       /// (i.e., from the [`VoidGeoStatsAccumulator`]) or if the accumulator 
encountered
   ```



-- 
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