rluvaton commented on code in PR #18183: URL: https://github.com/apache/datafusion/pull/18183#discussion_r2524462330
########## datafusion/physical-expr/src/expressions/case/literal_lookup_table/mod.rs: ########## @@ -0,0 +1,440 @@ +// 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. + +mod boolean_lookup_table; +mod bytes_like_lookup_table; +mod primitive_lookup_table; + +use crate::expressions::case::literal_lookup_table::boolean_lookup_table::BooleanIndexMap; +use crate::expressions::case::literal_lookup_table::bytes_like_lookup_table::{ + BytesDictionaryHelper, BytesLikeIndexMap, BytesViewDictionaryHelper, + FixedBinaryHelper, FixedBytesDictionaryHelper, GenericBytesHelper, + GenericBytesViewHelper, +}; +use crate::expressions::case::literal_lookup_table::primitive_lookup_table::PrimitiveArrayMapHolder; +use crate::expressions::case::WhenThen; +use crate::expressions::Literal; +use arrow::array::{downcast_integer, downcast_primitive, ArrayRef, Int32Array}; +use arrow::datatypes::{ + ArrowDictionaryKeyType, BinaryViewType, DataType, GenericBinaryType, + GenericStringType, StringViewType, +}; +use datafusion_common::DataFusionError; +use datafusion_common::{arrow_datafusion_err, plan_datafusion_err, ScalarValue}; +use datafusion_physical_expr_common::physical_expr::PhysicalExpr; +use indexmap::IndexMap; +use std::fmt::Debug; +use std::sync::Arc; + +/// Optimization for CASE expressions with literal WHEN and THEN clauses +/// +/// for this form: +/// ```sql +/// CASE <expr_a> +/// WHEN <literal_a> THEN <literal_e> +/// WHEN <literal_b> THEN <literal_f> +/// WHEN <literal_c> THEN <literal_g> +/// WHEN <literal_d> THEN <literal_h> +/// ELSE <optional-fallback_literal> +/// END +/// ``` +/// +/// # Improvement idea +/// TODO - we should think of unwrapping the `IN` expressions into multiple equality comparisons +/// so it will use this optimization as well, e.g. +/// ```sql +/// -- Before +/// CASE +/// WHEN (<expr_a> = <literal_a>) THEN <literal_e> +/// WHEN (<expr_a> in (<literal_b>, <literal_c>) THEN <literal_f> +/// WHEN (<expr_a> = <literal_d>) THEN <literal_g> +/// ELSE <optional-fallback_literal> +/// +/// -- After +/// CASE +/// WHEN (<expr_a> = <literal_a>) THEN <literal_e> +/// WHEN (<expr_a> = <literal_b>) THEN <literal_f> +/// WHEN (<expr_a> = <literal_c>) THEN <literal_g> +/// WHEN (<expr_a> = <literal_d>) THEN <literal_h> +/// ELSE <optional-fallback_literal> +/// END +/// ``` +/// +#[derive(Debug)] +pub(in super::super) struct LiteralLookupTable { + /// The lookup table to use for evaluating the CASE expression + lookup: Arc<dyn WhenLiteralIndexMap>, + + /// [`ArrayRef`] where `array[i] = then_literals[i]` + /// the last value in the array is the else_expr + values_to_take_from: ArrayRef, Review Comment: updated -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
