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 proto for null treatment
305            match window_function {
306                window_expr_node::WindowFunction::Udaf(udaf_name) => {
307                    let udaf_function = match &expr.fun_definition {
308                        Some(buf) => codec.try_decode_udaf(udaf_name, buf)?,
309                        None => registry
310                            .udaf(udaf_name)
311                            .or_else(|_| codec.try_decode_udaf(udaf_name, &[]))?,
312                    };
313
314                    let args = parse_exprs(&expr.exprs, registry, codec)?;
315                    Expr::from(WindowFunction::new(
316                        expr::WindowFunctionDefinition::AggregateUDF(udaf_function),
317                        args,
318                    ))
319                    .partition_by(partition_by)
320                    .order_by(order_by)
321                    .window_frame(window_frame)
322                    .build()
323                    .map_err(Error::DataFusionError)
324                }
325                window_expr_node::WindowFunction::Udwf(udwf_name) => {
326                    let udwf_function = match &expr.fun_definition {
327                        Some(buf) => codec.try_decode_udwf(udwf_name, buf)?,
328                        None => registry
329                            .udwf(udwf_name)
330                            .or_else(|_| codec.try_decode_udwf(udwf_name, &[]))?,
331                    };
332
333                    let args = parse_exprs(&expr.exprs, registry, codec)?;
334                    Expr::from(WindowFunction::new(
335                        expr::WindowFunctionDefinition::WindowUDF(udwf_function),
336                        args,
337                    ))
338                    .partition_by(partition_by)
339                    .order_by(order_by)
340                    .window_frame(window_frame)
341                    .build()
342                    .map_err(Error::DataFusionError)
343                }
344            }
345        }
346        ExprType::Alias(alias) => Ok(Expr::Alias(Alias::new(
347            parse_required_expr(alias.expr.as_deref(), registry, "expr", codec)?,
348            alias
349                .relation
350                .first()
351                .map(|r| TableReference::try_from(r.clone()))
352                .transpose()?,
353            alias.alias.clone(),
354        ))),
355        ExprType::IsNullExpr(is_null) => Ok(Expr::IsNull(Box::new(parse_required_expr(
356            is_null.expr.as_deref(),
357            registry,
358            "expr",
359            codec,
360        )?))),
361        ExprType::IsNotNullExpr(is_not_null) => Ok(Expr::IsNotNull(Box::new(
362            parse_required_expr(is_not_null.expr.as_deref(), registry, "expr", codec)?,
363        ))),
364        ExprType::NotExpr(not) => Ok(Expr::Not(Box::new(parse_required_expr(
365            not.expr.as_deref(),
366            registry,
367            "expr",
368            codec,
369        )?))),
370        ExprType::IsTrue(msg) => Ok(Expr::IsTrue(Box::new(parse_required_expr(
371            msg.expr.as_deref(),
372            registry,
373            "expr",
374            codec,
375        )?))),
376        ExprType::IsFalse(msg) => Ok(Expr::IsFalse(Box::new(parse_required_expr(
377            msg.expr.as_deref(),
378            registry,
379            "expr",
380            codec,
381        )?))),
382        ExprType::IsUnknown(msg) => Ok(Expr::IsUnknown(Box::new(parse_required_expr(
383            msg.expr.as_deref(),
384            registry,
385            "expr",
386            codec,
387        )?))),
388        ExprType::IsNotTrue(msg) => Ok(Expr::IsNotTrue(Box::new(parse_required_expr(
389            msg.expr.as_deref(),
390            registry,
391            "expr",
392            codec,
393        )?))),
394        ExprType::IsNotFalse(msg) => Ok(Expr::IsNotFalse(Box::new(parse_required_expr(
395            msg.expr.as_deref(),
396            registry,
397            "expr",
398            codec,
399        )?))),
400        ExprType::IsNotUnknown(msg) => Ok(Expr::IsNotUnknown(Box::new(
401            parse_required_expr(msg.expr.as_deref(), registry, "expr", codec)?,
402        ))),
403        ExprType::Between(between) => Ok(Expr::Between(Between::new(
404            Box::new(parse_required_expr(
405                between.expr.as_deref(),
406                registry,
407                "expr",
408                codec,
409            )?),
410            between.negated,
411            Box::new(parse_required_expr(
412                between.low.as_deref(),
413                registry,
414                "expr",
415                codec,
416            )?),
417            Box::new(parse_required_expr(
418                between.high.as_deref(),
419                registry,
420                "expr",
421                codec,
422            )?),
423        ))),
424        ExprType::Like(like) => Ok(Expr::Like(Like::new(
425            like.negated,
426            Box::new(parse_required_expr(
427                like.expr.as_deref(),
428                registry,
429                "expr",
430                codec,
431            )?),
432            Box::new(parse_required_expr(
433                like.pattern.as_deref(),
434                registry,
435                "pattern",
436                codec,
437            )?),
438            parse_escape_char(&like.escape_char)?,
439            false,
440        ))),
441        ExprType::Ilike(like) => Ok(Expr::Like(Like::new(
442            like.negated,
443            Box::new(parse_required_expr(
444                like.expr.as_deref(),
445                registry,
446                "expr",
447                codec,
448            )?),
449            Box::new(parse_required_expr(
450                like.pattern.as_deref(),
451                registry,
452                "pattern",
453                codec,
454            )?),
455            parse_escape_char(&like.escape_char)?,
456            true,
457        ))),
458        ExprType::SimilarTo(like) => Ok(Expr::SimilarTo(Like::new(
459            like.negated,
460            Box::new(parse_required_expr(
461                like.expr.as_deref(),
462                registry,
463                "expr",
464                codec,
465            )?),
466            Box::new(parse_required_expr(
467                like.pattern.as_deref(),
468                registry,
469                "pattern",
470                codec,
471            )?),
472            parse_escape_char(&like.escape_char)?,
473            false,
474        ))),
475        ExprType::Case(case) => {
476            let when_then_expr = case
477                .when_then_expr
478                .iter()
479                .map(|e| {
480                    let when_expr = parse_required_expr(
481                        e.when_expr.as_ref(),
482                        registry,
483                        "when_expr",
484                        codec,
485                    )?;
486                    let then_expr = parse_required_expr(
487                        e.then_expr.as_ref(),
488                        registry,
489                        "then_expr",
490                        codec,
491                    )?;
492                    Ok((Box::new(when_expr), Box::new(then_expr)))
493                })
494                .collect::<Result<Vec<(Box<Expr>, Box<Expr>)>, Error>>()?;
495            Ok(Expr::Case(Case::new(
496                parse_optional_expr(case.expr.as_deref(), registry, codec)?.map(Box::new),
497                when_then_expr,
498                parse_optional_expr(case.else_expr.as_deref(), registry, codec)?
499                    .map(Box::new),
500            )))
501        }
502        ExprType::Cast(cast) => {
503            let expr = Box::new(parse_required_expr(
504                cast.expr.as_deref(),
505                registry,
506                "expr",
507                codec,
508            )?);
509            let data_type = cast.arrow_type.as_ref().required("arrow_type")?;
510            Ok(Expr::Cast(Cast::new(expr, data_type)))
511        }
512        ExprType::TryCast(cast) => {
513            let expr = Box::new(parse_required_expr(
514                cast.expr.as_deref(),
515                registry,
516                "expr",
517                codec,
518            )?);
519            let data_type = cast.arrow_type.as_ref().required("arrow_type")?;
520            Ok(Expr::TryCast(TryCast::new(expr, data_type)))
521        }
522        ExprType::Negative(negative) => Ok(Expr::Negative(Box::new(
523            parse_required_expr(negative.expr.as_deref(), registry, "expr", codec)?,
524        ))),
525        ExprType::Unnest(unnest) => {
526            let mut exprs = parse_exprs(&unnest.exprs, registry, codec)?;
527            if exprs.len() != 1 {
528                return Err(proto_error("Unnest must have exactly one expression"));
529            }
530            Ok(Expr::Unnest(Unnest::new(exprs.swap_remove(0))))
531        }
532        ExprType::InList(in_list) => Ok(Expr::InList(InList::new(
533            Box::new(parse_required_expr(
534                in_list.expr.as_deref(),
535                registry,
536                "expr",
537                codec,
538            )?),
539            parse_exprs(&in_list.list, registry, codec)?,
540            in_list.negated,
541        ))),
542        ExprType::Wildcard(protobuf::Wildcard { qualifier }) => {
543            let qualifier = qualifier.to_owned().map(|x| x.try_into()).transpose()?;
544            #[expect(deprecated)]
545            Ok(Expr::Wildcard {
546                qualifier,
547                options: Box::new(WildcardOptions::default()),
548            })
549        }
550        ExprType::ScalarUdfExpr(protobuf::ScalarUdfExprNode {
551            fun_name,
552            args,
553            fun_definition,
554        }) => {
555            let scalar_fn = match fun_definition {
556                Some(buf) => codec.try_decode_udf(fun_name, buf)?,
557                None => registry
558                    .udf(fun_name.as_str())
559                    .or_else(|_| codec.try_decode_udf(fun_name, &[]))?,
560            };
561            Ok(Expr::ScalarFunction(expr::ScalarFunction::new_udf(
562                scalar_fn,
563                parse_exprs(args, registry, codec)?,
564            )))
565        }
566        ExprType::AggregateUdfExpr(pb) => {
567            let agg_fn = match &pb.fun_definition {
568                Some(buf) => codec.try_decode_udaf(&pb.fun_name, buf)?,
569                None => registry
570                    .udaf(&pb.fun_name)
571                    .or_else(|_| codec.try_decode_udaf(&pb.fun_name, &[]))?,
572            };
573
574            Ok(Expr::AggregateFunction(expr::AggregateFunction::new_udf(
575                agg_fn,
576                parse_exprs(&pb.args, registry, codec)?,
577                pb.distinct,
578                parse_optional_expr(pb.filter.as_deref(), registry, codec)?.map(Box::new),
579                parse_sorts(&pb.order_by, registry, codec)?,
580                None,
581            )))
582        }
583
584        ExprType::GroupingSet(GroupingSetNode { expr }) => {
585            Ok(Expr::GroupingSet(GroupingSets(
586                expr.iter()
587                    .map(|expr_list| parse_exprs(&expr_list.expr, registry, codec))
588                    .collect::<Result<Vec<_>, Error>>()?,
589            )))
590        }
591        ExprType::Cube(CubeNode { expr }) => Ok(Expr::GroupingSet(GroupingSet::Cube(
592            parse_exprs(expr, registry, codec)?,
593        ))),
594        ExprType::Rollup(RollupNode { expr }) => Ok(Expr::GroupingSet(
595            GroupingSet::Rollup(parse_exprs(expr, registry, codec)?),
596        )),
597        ExprType::Placeholder(PlaceholderNode { id, data_type }) => match data_type {
598            None => Ok(Expr::Placeholder(Placeholder::new(id.clone(), None))),
599            Some(data_type) => Ok(Expr::Placeholder(Placeholder::new(
600                id.clone(),
601                Some(data_type.try_into()?),
602            ))),
603        },
604    }
605}
606
607/// Parse a vector of `protobuf::LogicalExprNode`s.
608pub fn parse_exprs<'a, I>(
609    protos: I,
610    registry: &dyn FunctionRegistry,
611    codec: &dyn LogicalExtensionCodec,
612) -> Result<Vec<Expr>, Error>
613where
614    I: IntoIterator<Item = &'a protobuf::LogicalExprNode>,
615{
616    let res = protos
617        .into_iter()
618        .map(|elem| {
619            parse_expr(elem, registry, codec).map_err(|e| plan_datafusion_err!("{}", e))
620        })
621        .collect::<Result<Vec<_>>>()?;
622    Ok(res)
623}
624
625pub fn parse_sorts<'a, I>(
626    protos: I,
627    registry: &dyn FunctionRegistry,
628    codec: &dyn LogicalExtensionCodec,
629) -> Result<Vec<Sort>, Error>
630where
631    I: IntoIterator<Item = &'a protobuf::SortExprNode>,
632{
633    protos
634        .into_iter()
635        .map(|sort| parse_sort(sort, registry, codec))
636        .collect::<Result<Vec<Sort>, Error>>()
637}
638
639pub fn parse_sort(
640    sort: &protobuf::SortExprNode,
641    registry: &dyn FunctionRegistry,
642    codec: &dyn LogicalExtensionCodec,
643) -> Result<Sort, Error> {
644    Ok(Sort::new(
645        parse_required_expr(sort.expr.as_ref(), registry, "expr", codec)?,
646        sort.asc,
647        sort.nulls_first,
648    ))
649}
650
651/// Parse an optional escape_char for Like, ILike, SimilarTo
652fn parse_escape_char(s: &str) -> Result<Option<char>> {
653    match s.len() {
654        0 => Ok(None),
655        1 => Ok(s.chars().next()),
656        _ => internal_err!("Invalid length for escape char"),
657    }
658}
659
660pub fn from_proto_binary_op(op: &str) -> Result<Operator, Error> {
661    match op {
662        "And" => Ok(Operator::And),
663        "Or" => Ok(Operator::Or),
664        "Eq" => Ok(Operator::Eq),
665        "NotEq" => Ok(Operator::NotEq),
666        "LtEq" => Ok(Operator::LtEq),
667        "Lt" => Ok(Operator::Lt),
668        "Gt" => Ok(Operator::Gt),
669        "GtEq" => Ok(Operator::GtEq),
670        "Plus" => Ok(Operator::Plus),
671        "Minus" => Ok(Operator::Minus),
672        "Multiply" => Ok(Operator::Multiply),
673        "Divide" => Ok(Operator::Divide),
674        "Modulo" => Ok(Operator::Modulo),
675        "IsDistinctFrom" => Ok(Operator::IsDistinctFrom),
676        "IsNotDistinctFrom" => Ok(Operator::IsNotDistinctFrom),
677        "BitwiseAnd" => Ok(Operator::BitwiseAnd),
678        "BitwiseOr" => Ok(Operator::BitwiseOr),
679        "BitwiseXor" => Ok(Operator::BitwiseXor),
680        "BitwiseShiftLeft" => Ok(Operator::BitwiseShiftLeft),
681        "BitwiseShiftRight" => Ok(Operator::BitwiseShiftRight),
682        "RegexIMatch" => Ok(Operator::RegexIMatch),
683        "RegexMatch" => Ok(Operator::RegexMatch),
684        "RegexNotIMatch" => Ok(Operator::RegexNotIMatch),
685        "RegexNotMatch" => Ok(Operator::RegexNotMatch),
686        "StringConcat" => Ok(Operator::StringConcat),
687        "AtArrow" => Ok(Operator::AtArrow),
688        "ArrowAt" => Ok(Operator::ArrowAt),
689        other => Err(proto_error(format!(
690            "Unsupported binary operator '{other:?}'"
691        ))),
692    }
693}
694
695fn parse_optional_expr(
696    p: Option<&protobuf::LogicalExprNode>,
697    registry: &dyn FunctionRegistry,
698    codec: &dyn LogicalExtensionCodec,
699) -> Result<Option<Expr>, Error> {
700    match p {
701        Some(expr) => parse_expr(expr, registry, codec).map(Some),
702        None => Ok(None),
703    }
704}
705
706fn parse_required_expr(
707    p: Option<&protobuf::LogicalExprNode>,
708    registry: &dyn FunctionRegistry,
709    field: impl Into<String>,
710    codec: &dyn LogicalExtensionCodec,
711) -> Result<Expr, Error> {
712    match p {
713        Some(expr) => parse_expr(expr, registry, codec),
714        None => Err(Error::required(field)),
715    }
716}
717
718fn proto_error<S: Into<String>>(message: S) -> Error {
719    Error::General(message.into())
720}