datafusion_proto/logical_plan/
from_proto.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::sync::Arc;
19
20use datafusion::execution::registry::FunctionRegistry;
21use datafusion_common::{
22    exec_datafusion_err, internal_err, plan_datafusion_err, NullEquality,
23    RecursionUnnestOption, Result, ScalarValue, TableReference, UnnestOptions,
24};
25use datafusion_expr::dml::InsertOp;
26use datafusion_expr::expr::{Alias, Placeholder, Sort};
27use datafusion_expr::expr::{Unnest, WildcardOptions};
28use datafusion_expr::{
29    expr::{self, InList, WindowFunction},
30    logical_plan::{PlanType, StringifiedPlan},
31    Between, BinaryExpr, Case, Cast, Expr, GroupingSet,
32    GroupingSet::GroupingSets,
33    JoinConstraint, JoinType, Like, Operator, TryCast, WindowFrame, WindowFrameBound,
34    WindowFrameUnits,
35};
36use datafusion_expr::{ExprFunctionExt, WriteOp};
37use datafusion_proto_common::{from_proto::FromOptionalField, FromProtoError as Error};
38
39use crate::protobuf::plan_type::PlanTypeEnum::{
40    FinalPhysicalPlanWithSchema, InitialPhysicalPlanWithSchema,
41};
42use crate::protobuf::{
43    self,
44    plan_type::PlanTypeEnum::{
45        AnalyzedLogicalPlan, FinalAnalyzedLogicalPlan, FinalLogicalPlan,
46        FinalPhysicalPlan, FinalPhysicalPlanWithStats, InitialLogicalPlan,
47        InitialPhysicalPlan, InitialPhysicalPlanWithStats, OptimizedLogicalPlan,
48        OptimizedPhysicalPlan, PhysicalPlanError,
49    },
50    AnalyzedLogicalPlanType, CubeNode, GroupingSetNode, OptimizedLogicalPlanType,
51    OptimizedPhysicalPlanType, PlaceholderNode, RollupNode,
52};
53
54use super::LogicalExtensionCodec;
55
56impl From<&protobuf::UnnestOptions> for UnnestOptions {
57    fn from(opts: &protobuf::UnnestOptions) -> Self {
58        Self {
59            preserve_nulls: opts.preserve_nulls,
60            recursions: opts
61                .recursions
62                .iter()
63                .map(|r| RecursionUnnestOption {
64                    input_column: r.input_column.as_ref().unwrap().into(),
65                    output_column: r.output_column.as_ref().unwrap().into(),
66                    depth: r.depth as usize,
67                })
68                .collect::<Vec<_>>(),
69        }
70    }
71}
72
73impl From<protobuf::WindowFrameUnits> for WindowFrameUnits {
74    fn from(units: protobuf::WindowFrameUnits) -> Self {
75        match units {
76            protobuf::WindowFrameUnits::Rows => Self::Rows,
77            protobuf::WindowFrameUnits::Range => Self::Range,
78            protobuf::WindowFrameUnits::Groups => Self::Groups,
79        }
80    }
81}
82
83impl TryFrom<protobuf::TableReference> for TableReference {
84    type Error = Error;
85
86    fn try_from(value: protobuf::TableReference) -> Result<Self, Self::Error> {
87        use protobuf::table_reference::TableReferenceEnum;
88        let table_reference_enum = value
89            .table_reference_enum
90            .ok_or_else(|| Error::required("table_reference_enum"))?;
91
92        match table_reference_enum {
93            TableReferenceEnum::Bare(protobuf::BareTableReference { table }) => {
94                Ok(TableReference::bare(table))
95            }
96            TableReferenceEnum::Partial(protobuf::PartialTableReference {
97                schema,
98                table,
99            }) => Ok(TableReference::partial(schema, table)),
100            TableReferenceEnum::Full(protobuf::FullTableReference {
101                catalog,
102                schema,
103                table,
104            }) => Ok(TableReference::full(catalog, schema, table)),
105        }
106    }
107}
108
109impl From<&protobuf::StringifiedPlan> for StringifiedPlan {
110    fn from(stringified_plan: &protobuf::StringifiedPlan) -> Self {
111        Self {
112            plan_type: match stringified_plan
113                .plan_type
114                .as_ref()
115                .and_then(|pt| pt.plan_type_enum.as_ref())
116                .unwrap_or_else(|| {
117                    panic!(
118                        "Cannot create protobuf::StringifiedPlan from {stringified_plan:?}"
119                    )
120                }) {
121                InitialLogicalPlan(_) => PlanType::InitialLogicalPlan,
122                AnalyzedLogicalPlan(AnalyzedLogicalPlanType { analyzer_name }) => {
123                    PlanType::AnalyzedLogicalPlan {
124                        analyzer_name:analyzer_name.clone()
125                    }
126                }
127                FinalAnalyzedLogicalPlan(_) => PlanType::FinalAnalyzedLogicalPlan,
128                OptimizedLogicalPlan(OptimizedLogicalPlanType { optimizer_name }) => {
129                    PlanType::OptimizedLogicalPlan {
130                        optimizer_name: optimizer_name.clone(),
131                    }
132                }
133                FinalLogicalPlan(_) => PlanType::FinalLogicalPlan,
134                InitialPhysicalPlan(_) => PlanType::InitialPhysicalPlan,
135                InitialPhysicalPlanWithStats(_) => PlanType::InitialPhysicalPlanWithStats,
136                InitialPhysicalPlanWithSchema(_) => PlanType::InitialPhysicalPlanWithSchema,
137                OptimizedPhysicalPlan(OptimizedPhysicalPlanType { optimizer_name }) => {
138                    PlanType::OptimizedPhysicalPlan {
139                        optimizer_name: optimizer_name.clone(),
140                    }
141                }
142                FinalPhysicalPlan(_) => PlanType::FinalPhysicalPlan,
143                FinalPhysicalPlanWithStats(_) => PlanType::FinalPhysicalPlanWithStats,
144                FinalPhysicalPlanWithSchema(_) => PlanType::FinalPhysicalPlanWithSchema,
145                PhysicalPlanError(_) => PlanType::PhysicalPlanError,
146            },
147            plan: Arc::new(stringified_plan.plan.clone()),
148        }
149    }
150}
151
152impl TryFrom<protobuf::WindowFrame> for WindowFrame {
153    type Error = Error;
154
155    fn try_from(window: protobuf::WindowFrame) -> Result<Self, Self::Error> {
156        let units = protobuf::WindowFrameUnits::try_from(window.window_frame_units)
157            .map_err(|_| Error::unknown("WindowFrameUnits", window.window_frame_units))?
158            .into();
159        let start_bound = window.start_bound.required("start_bound")?;
160        let end_bound = window
161            .end_bound
162            .map(|end_bound| match end_bound {
163                protobuf::window_frame::EndBound::Bound(end_bound) => {
164                    end_bound.try_into()
165                }
166            })
167            .transpose()?
168            .unwrap_or(WindowFrameBound::CurrentRow);
169        Ok(WindowFrame::new_bounds(units, start_bound, end_bound))
170    }
171}
172
173impl TryFrom<protobuf::WindowFrameBound> for WindowFrameBound {
174    type Error = Error;
175
176    fn try_from(bound: protobuf::WindowFrameBound) -> Result<Self, Self::Error> {
177        let bound_type =
178            protobuf::WindowFrameBoundType::try_from(bound.window_frame_bound_type)
179                .map_err(|_| {
180                    Error::unknown("WindowFrameBoundType", bound.window_frame_bound_type)
181                })?;
182        match bound_type {
183            protobuf::WindowFrameBoundType::CurrentRow => Ok(Self::CurrentRow),
184            protobuf::WindowFrameBoundType::Preceding => match bound.bound_value {
185                Some(x) => Ok(Self::Preceding(ScalarValue::try_from(&x)?)),
186                None => Ok(Self::Preceding(ScalarValue::UInt64(None))),
187            },
188            protobuf::WindowFrameBoundType::Following => match bound.bound_value {
189                Some(x) => Ok(Self::Following(ScalarValue::try_from(&x)?)),
190                None => Ok(Self::Following(ScalarValue::UInt64(None))),
191            },
192        }
193    }
194}
195
196impl From<protobuf::JoinType> for JoinType {
197    fn from(t: protobuf::JoinType) -> Self {
198        match t {
199            protobuf::JoinType::Inner => JoinType::Inner,
200            protobuf::JoinType::Left => JoinType::Left,
201            protobuf::JoinType::Right => JoinType::Right,
202            protobuf::JoinType::Full => JoinType::Full,
203            protobuf::JoinType::Leftsemi => JoinType::LeftSemi,
204            protobuf::JoinType::Rightsemi => JoinType::RightSemi,
205            protobuf::JoinType::Leftanti => JoinType::LeftAnti,
206            protobuf::JoinType::Rightanti => JoinType::RightAnti,
207            protobuf::JoinType::Leftmark => JoinType::LeftMark,
208            protobuf::JoinType::Rightmark => JoinType::RightMark,
209        }
210    }
211}
212
213impl From<protobuf::JoinConstraint> for JoinConstraint {
214    fn from(t: protobuf::JoinConstraint) -> Self {
215        match t {
216            protobuf::JoinConstraint::On => JoinConstraint::On,
217            protobuf::JoinConstraint::Using => JoinConstraint::Using,
218        }
219    }
220}
221
222impl From<protobuf::NullEquality> for NullEquality {
223    fn from(t: protobuf::NullEquality) -> Self {
224        match t {
225            protobuf::NullEquality::NullEqualsNothing => NullEquality::NullEqualsNothing,
226            protobuf::NullEquality::NullEqualsNull => NullEquality::NullEqualsNull,
227        }
228    }
229}
230
231impl From<protobuf::dml_node::Type> for WriteOp {
232    fn from(t: protobuf::dml_node::Type) -> Self {
233        match t {
234            protobuf::dml_node::Type::Update => WriteOp::Update,
235            protobuf::dml_node::Type::Delete => WriteOp::Delete,
236            protobuf::dml_node::Type::InsertAppend => WriteOp::Insert(InsertOp::Append),
237            protobuf::dml_node::Type::InsertOverwrite => {
238                WriteOp::Insert(InsertOp::Overwrite)
239            }
240            protobuf::dml_node::Type::InsertReplace => WriteOp::Insert(InsertOp::Replace),
241            protobuf::dml_node::Type::Ctas => WriteOp::Ctas,
242        }
243    }
244}
245
246pub fn parse_expr(
247    proto: &protobuf::LogicalExprNode,
248    registry: &dyn FunctionRegistry,
249    codec: &dyn LogicalExtensionCodec,
250) -> Result<Expr, Error> {
251    use protobuf::{logical_expr_node::ExprType, window_expr_node};
252
253    let expr_type = proto
254        .expr_type
255        .as_ref()
256        .ok_or_else(|| Error::required("expr_type"))?;
257
258    match expr_type {
259        ExprType::BinaryExpr(binary_expr) => {
260            let op = from_proto_binary_op(&binary_expr.op)?;
261            let operands = parse_exprs(&binary_expr.operands, registry, codec)?;
262
263            if operands.len() < 2 {
264                return Err(proto_error(
265                    "A binary expression must always have at least 2 operands",
266                ));
267            }
268
269            // Reduce the linearized operands (ordered by left innermost to right
270            // outermost) into a single expression tree.
271            Ok(operands
272                .into_iter()
273                .reduce(|left, right| {
274                    Expr::BinaryExpr(BinaryExpr::new(Box::new(left), op, Box::new(right)))
275                })
276                .expect("Binary expression could not be reduced to a single expression."))
277        }
278        ExprType::Column(column) => Ok(Expr::Column(column.into())),
279        ExprType::Literal(literal) => {
280            let scalar_value: ScalarValue = literal.try_into()?;
281            Ok(Expr::Literal(scalar_value, None))
282        }
283        ExprType::WindowExpr(expr) => {
284            let window_function = expr
285                .window_function
286                .as_ref()
287                .ok_or_else(|| Error::required("window_function"))?;
288            let partition_by = parse_exprs(&expr.partition_by, registry, codec)?;
289            let mut order_by = parse_sorts(&expr.order_by, registry, codec)?;
290            let window_frame = expr
291                .window_frame
292                .as_ref()
293                .map::<Result<WindowFrame, _>, _>(|window_frame| {
294                    let window_frame: WindowFrame = window_frame.clone().try_into()?;
295                    window_frame
296                        .regularize_order_bys(&mut order_by)
297                        .map(|_| window_frame)
298                })
299                .transpose()?
300                .ok_or_else(|| {
301                    exec_datafusion_err!("missing window frame during deserialization")
302                })?;
303
304            // TODO: support null treatment, distinct, and filter in proto.
305            // See https://github.com/apache/datafusion/issues/17417
306            match window_function {
307                window_expr_node::WindowFunction::Udaf(udaf_name) => {
308                    let udaf_function = match &expr.fun_definition {
309                        Some(buf) => codec.try_decode_udaf(udaf_name, buf)?,
310                        None => registry
311                            .udaf(udaf_name)
312                            .or_else(|_| codec.try_decode_udaf(udaf_name, &[]))?,
313                    };
314
315                    let args = parse_exprs(&expr.exprs, registry, codec)?;
316                    Expr::from(WindowFunction::new(
317                        expr::WindowFunctionDefinition::AggregateUDF(udaf_function),
318                        args,
319                    ))
320                    .partition_by(partition_by)
321                    .order_by(order_by)
322                    .window_frame(window_frame)
323                    .build()
324                    .map_err(Error::DataFusionError)
325                }
326                window_expr_node::WindowFunction::Udwf(udwf_name) => {
327                    let udwf_function = match &expr.fun_definition {
328                        Some(buf) => codec.try_decode_udwf(udwf_name, buf)?,
329                        None => registry
330                            .udwf(udwf_name)
331                            .or_else(|_| codec.try_decode_udwf(udwf_name, &[]))?,
332                    };
333
334                    let args = parse_exprs(&expr.exprs, registry, codec)?;
335                    Expr::from(WindowFunction::new(
336                        expr::WindowFunctionDefinition::WindowUDF(udwf_function),
337                        args,
338                    ))
339                    .partition_by(partition_by)
340                    .order_by(order_by)
341                    .window_frame(window_frame)
342                    .build()
343                    .map_err(Error::DataFusionError)
344                }
345            }
346        }
347        ExprType::Alias(alias) => Ok(Expr::Alias(Alias::new(
348            parse_required_expr(alias.expr.as_deref(), registry, "expr", codec)?,
349            alias
350                .relation
351                .first()
352                .map(|r| TableReference::try_from(r.clone()))
353                .transpose()?,
354            alias.alias.clone(),
355        ))),
356        ExprType::IsNullExpr(is_null) => Ok(Expr::IsNull(Box::new(parse_required_expr(
357            is_null.expr.as_deref(),
358            registry,
359            "expr",
360            codec,
361        )?))),
362        ExprType::IsNotNullExpr(is_not_null) => Ok(Expr::IsNotNull(Box::new(
363            parse_required_expr(is_not_null.expr.as_deref(), registry, "expr", codec)?,
364        ))),
365        ExprType::NotExpr(not) => Ok(Expr::Not(Box::new(parse_required_expr(
366            not.expr.as_deref(),
367            registry,
368            "expr",
369            codec,
370        )?))),
371        ExprType::IsTrue(msg) => Ok(Expr::IsTrue(Box::new(parse_required_expr(
372            msg.expr.as_deref(),
373            registry,
374            "expr",
375            codec,
376        )?))),
377        ExprType::IsFalse(msg) => Ok(Expr::IsFalse(Box::new(parse_required_expr(
378            msg.expr.as_deref(),
379            registry,
380            "expr",
381            codec,
382        )?))),
383        ExprType::IsUnknown(msg) => Ok(Expr::IsUnknown(Box::new(parse_required_expr(
384            msg.expr.as_deref(),
385            registry,
386            "expr",
387            codec,
388        )?))),
389        ExprType::IsNotTrue(msg) => Ok(Expr::IsNotTrue(Box::new(parse_required_expr(
390            msg.expr.as_deref(),
391            registry,
392            "expr",
393            codec,
394        )?))),
395        ExprType::IsNotFalse(msg) => Ok(Expr::IsNotFalse(Box::new(parse_required_expr(
396            msg.expr.as_deref(),
397            registry,
398            "expr",
399            codec,
400        )?))),
401        ExprType::IsNotUnknown(msg) => Ok(Expr::IsNotUnknown(Box::new(
402            parse_required_expr(msg.expr.as_deref(), registry, "expr", codec)?,
403        ))),
404        ExprType::Between(between) => Ok(Expr::Between(Between::new(
405            Box::new(parse_required_expr(
406                between.expr.as_deref(),
407                registry,
408                "expr",
409                codec,
410            )?),
411            between.negated,
412            Box::new(parse_required_expr(
413                between.low.as_deref(),
414                registry,
415                "expr",
416                codec,
417            )?),
418            Box::new(parse_required_expr(
419                between.high.as_deref(),
420                registry,
421                "expr",
422                codec,
423            )?),
424        ))),
425        ExprType::Like(like) => Ok(Expr::Like(Like::new(
426            like.negated,
427            Box::new(parse_required_expr(
428                like.expr.as_deref(),
429                registry,
430                "expr",
431                codec,
432            )?),
433            Box::new(parse_required_expr(
434                like.pattern.as_deref(),
435                registry,
436                "pattern",
437                codec,
438            )?),
439            parse_escape_char(&like.escape_char)?,
440            false,
441        ))),
442        ExprType::Ilike(like) => Ok(Expr::Like(Like::new(
443            like.negated,
444            Box::new(parse_required_expr(
445                like.expr.as_deref(),
446                registry,
447                "expr",
448                codec,
449            )?),
450            Box::new(parse_required_expr(
451                like.pattern.as_deref(),
452                registry,
453                "pattern",
454                codec,
455            )?),
456            parse_escape_char(&like.escape_char)?,
457            true,
458        ))),
459        ExprType::SimilarTo(like) => Ok(Expr::SimilarTo(Like::new(
460            like.negated,
461            Box::new(parse_required_expr(
462                like.expr.as_deref(),
463                registry,
464                "expr",
465                codec,
466            )?),
467            Box::new(parse_required_expr(
468                like.pattern.as_deref(),
469                registry,
470                "pattern",
471                codec,
472            )?),
473            parse_escape_char(&like.escape_char)?,
474            false,
475        ))),
476        ExprType::Case(case) => {
477            let when_then_expr = case
478                .when_then_expr
479                .iter()
480                .map(|e| {
481                    let when_expr = parse_required_expr(
482                        e.when_expr.as_ref(),
483                        registry,
484                        "when_expr",
485                        codec,
486                    )?;
487                    let then_expr = parse_required_expr(
488                        e.then_expr.as_ref(),
489                        registry,
490                        "then_expr",
491                        codec,
492                    )?;
493                    Ok((Box::new(when_expr), Box::new(then_expr)))
494                })
495                .collect::<Result<Vec<(Box<Expr>, Box<Expr>)>, Error>>()?;
496            Ok(Expr::Case(Case::new(
497                parse_optional_expr(case.expr.as_deref(), registry, codec)?.map(Box::new),
498                when_then_expr,
499                parse_optional_expr(case.else_expr.as_deref(), registry, codec)?
500                    .map(Box::new),
501            )))
502        }
503        ExprType::Cast(cast) => {
504            let expr = Box::new(parse_required_expr(
505                cast.expr.as_deref(),
506                registry,
507                "expr",
508                codec,
509            )?);
510            let data_type = cast.arrow_type.as_ref().required("arrow_type")?;
511            Ok(Expr::Cast(Cast::new(expr, data_type)))
512        }
513        ExprType::TryCast(cast) => {
514            let expr = Box::new(parse_required_expr(
515                cast.expr.as_deref(),
516                registry,
517                "expr",
518                codec,
519            )?);
520            let data_type = cast.arrow_type.as_ref().required("arrow_type")?;
521            Ok(Expr::TryCast(TryCast::new(expr, data_type)))
522        }
523        ExprType::Negative(negative) => Ok(Expr::Negative(Box::new(
524            parse_required_expr(negative.expr.as_deref(), registry, "expr", codec)?,
525        ))),
526        ExprType::Unnest(unnest) => {
527            let mut exprs = parse_exprs(&unnest.exprs, registry, codec)?;
528            if exprs.len() != 1 {
529                return Err(proto_error("Unnest must have exactly one expression"));
530            }
531            Ok(Expr::Unnest(Unnest::new(exprs.swap_remove(0))))
532        }
533        ExprType::InList(in_list) => Ok(Expr::InList(InList::new(
534            Box::new(parse_required_expr(
535                in_list.expr.as_deref(),
536                registry,
537                "expr",
538                codec,
539            )?),
540            parse_exprs(&in_list.list, registry, codec)?,
541            in_list.negated,
542        ))),
543        ExprType::Wildcard(protobuf::Wildcard { qualifier }) => {
544            let qualifier = qualifier.to_owned().map(|x| x.try_into()).transpose()?;
545            #[expect(deprecated)]
546            Ok(Expr::Wildcard {
547                qualifier,
548                options: Box::new(WildcardOptions::default()),
549            })
550        }
551        ExprType::ScalarUdfExpr(protobuf::ScalarUdfExprNode {
552            fun_name,
553            args,
554            fun_definition,
555        }) => {
556            let scalar_fn = match fun_definition {
557                Some(buf) => codec.try_decode_udf(fun_name, buf)?,
558                None => registry
559                    .udf(fun_name.as_str())
560                    .or_else(|_| codec.try_decode_udf(fun_name, &[]))?,
561            };
562            Ok(Expr::ScalarFunction(expr::ScalarFunction::new_udf(
563                scalar_fn,
564                parse_exprs(args, registry, codec)?,
565            )))
566        }
567        ExprType::AggregateUdfExpr(pb) => {
568            let agg_fn = match &pb.fun_definition {
569                Some(buf) => codec.try_decode_udaf(&pb.fun_name, buf)?,
570                None => registry
571                    .udaf(&pb.fun_name)
572                    .or_else(|_| codec.try_decode_udaf(&pb.fun_name, &[]))?,
573            };
574
575            Ok(Expr::AggregateFunction(expr::AggregateFunction::new_udf(
576                agg_fn,
577                parse_exprs(&pb.args, registry, codec)?,
578                pb.distinct,
579                parse_optional_expr(pb.filter.as_deref(), registry, codec)?.map(Box::new),
580                parse_sorts(&pb.order_by, registry, codec)?,
581                None,
582            )))
583        }
584
585        ExprType::GroupingSet(GroupingSetNode { expr }) => {
586            Ok(Expr::GroupingSet(GroupingSets(
587                expr.iter()
588                    .map(|expr_list| parse_exprs(&expr_list.expr, registry, codec))
589                    .collect::<Result<Vec<_>, Error>>()?,
590            )))
591        }
592        ExprType::Cube(CubeNode { expr }) => Ok(Expr::GroupingSet(GroupingSet::Cube(
593            parse_exprs(expr, registry, codec)?,
594        ))),
595        ExprType::Rollup(RollupNode { expr }) => Ok(Expr::GroupingSet(
596            GroupingSet::Rollup(parse_exprs(expr, registry, codec)?),
597        )),
598        ExprType::Placeholder(PlaceholderNode { id, data_type }) => match data_type {
599            None => Ok(Expr::Placeholder(Placeholder::new(id.clone(), None))),
600            Some(data_type) => Ok(Expr::Placeholder(Placeholder::new(
601                id.clone(),
602                Some(data_type.try_into()?),
603            ))),
604        },
605    }
606}
607
608/// Parse a vector of `protobuf::LogicalExprNode`s.
609pub fn parse_exprs<'a, I>(
610    protos: I,
611    registry: &dyn FunctionRegistry,
612    codec: &dyn LogicalExtensionCodec,
613) -> Result<Vec<Expr>, Error>
614where
615    I: IntoIterator<Item = &'a protobuf::LogicalExprNode>,
616{
617    let res = protos
618        .into_iter()
619        .map(|elem| {
620            parse_expr(elem, registry, codec).map_err(|e| plan_datafusion_err!("{}", e))
621        })
622        .collect::<Result<Vec<_>>>()?;
623    Ok(res)
624}
625
626pub fn parse_sorts<'a, I>(
627    protos: I,
628    registry: &dyn FunctionRegistry,
629    codec: &dyn LogicalExtensionCodec,
630) -> Result<Vec<Sort>, Error>
631where
632    I: IntoIterator<Item = &'a protobuf::SortExprNode>,
633{
634    protos
635        .into_iter()
636        .map(|sort| parse_sort(sort, registry, codec))
637        .collect::<Result<Vec<Sort>, Error>>()
638}
639
640pub fn parse_sort(
641    sort: &protobuf::SortExprNode,
642    registry: &dyn FunctionRegistry,
643    codec: &dyn LogicalExtensionCodec,
644) -> Result<Sort, Error> {
645    Ok(Sort::new(
646        parse_required_expr(sort.expr.as_ref(), registry, "expr", codec)?,
647        sort.asc,
648        sort.nulls_first,
649    ))
650}
651
652/// Parse an optional escape_char for Like, ILike, SimilarTo
653fn parse_escape_char(s: &str) -> Result<Option<char>> {
654    match s.len() {
655        0 => Ok(None),
656        1 => Ok(s.chars().next()),
657        _ => internal_err!("Invalid length for escape char"),
658    }
659}
660
661pub fn from_proto_binary_op(op: &str) -> Result<Operator, Error> {
662    match op {
663        "And" => Ok(Operator::And),
664        "Or" => Ok(Operator::Or),
665        "Eq" => Ok(Operator::Eq),
666        "NotEq" => Ok(Operator::NotEq),
667        "LtEq" => Ok(Operator::LtEq),
668        "Lt" => Ok(Operator::Lt),
669        "Gt" => Ok(Operator::Gt),
670        "GtEq" => Ok(Operator::GtEq),
671        "Plus" => Ok(Operator::Plus),
672        "Minus" => Ok(Operator::Minus),
673        "Multiply" => Ok(Operator::Multiply),
674        "Divide" => Ok(Operator::Divide),
675        "Modulo" => Ok(Operator::Modulo),
676        "IsDistinctFrom" => Ok(Operator::IsDistinctFrom),
677        "IsNotDistinctFrom" => Ok(Operator::IsNotDistinctFrom),
678        "BitwiseAnd" => Ok(Operator::BitwiseAnd),
679        "BitwiseOr" => Ok(Operator::BitwiseOr),
680        "BitwiseXor" => Ok(Operator::BitwiseXor),
681        "BitwiseShiftLeft" => Ok(Operator::BitwiseShiftLeft),
682        "BitwiseShiftRight" => Ok(Operator::BitwiseShiftRight),
683        "RegexIMatch" => Ok(Operator::RegexIMatch),
684        "RegexMatch" => Ok(Operator::RegexMatch),
685        "RegexNotIMatch" => Ok(Operator::RegexNotIMatch),
686        "RegexNotMatch" => Ok(Operator::RegexNotMatch),
687        "StringConcat" => Ok(Operator::StringConcat),
688        "AtArrow" => Ok(Operator::AtArrow),
689        "ArrowAt" => Ok(Operator::ArrowAt),
690        other => Err(proto_error(format!(
691            "Unsupported binary operator '{other:?}'"
692        ))),
693    }
694}
695
696fn parse_optional_expr(
697    p: Option<&protobuf::LogicalExprNode>,
698    registry: &dyn FunctionRegistry,
699    codec: &dyn LogicalExtensionCodec,
700) -> Result<Option<Expr>, Error> {
701    match p {
702        Some(expr) => parse_expr(expr, registry, codec).map(Some),
703        None => Ok(None),
704    }
705}
706
707fn parse_required_expr(
708    p: Option<&protobuf::LogicalExprNode>,
709    registry: &dyn FunctionRegistry,
710    field: impl Into<String>,
711    codec: &dyn LogicalExtensionCodec,
712) -> Result<Expr, Error> {
713    match p {
714        Some(expr) => parse_expr(expr, registry, codec),
715        None => Err(Error::required(field)),
716    }
717}
718
719fn proto_error<S: Into<String>>(message: S) -> Error {
720    Error::General(message.into())
721}