datafusion_sql/
values.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 crate::planner::{ContextProvider, PlannerContext, SqlToRel};
21use datafusion_common::{DFSchema, Result};
22use datafusion_expr::{LogicalPlan, LogicalPlanBuilder};
23use sqlparser::ast::Values as SQLValues;
24
25impl<S: ContextProvider> SqlToRel<'_, S> {
26    pub(super) fn sql_values_to_plan(
27        &self,
28        values: SQLValues,
29        planner_context: &mut PlannerContext,
30    ) -> Result<LogicalPlan> {
31        let SQLValues {
32            explicit_row: _,
33            rows,
34        } = values;
35
36        let empty_schema = Arc::new(DFSchema::empty());
37        let values = rows
38            .into_iter()
39            .map(|row| {
40                row.into_iter()
41                    .map(|v| self.sql_to_expr(v, &empty_schema, planner_context))
42                    .collect::<Result<Vec<_>>>()
43            })
44            .collect::<Result<Vec<_>>>()?;
45
46        let schema = planner_context.table_schema().unwrap_or(empty_schema);
47        if schema.fields().is_empty() {
48            LogicalPlanBuilder::values(values)?.build()
49        } else {
50            LogicalPlanBuilder::values_with_schema(values, &schema)?.build()
51        }
52    }
53}