risingwave_frontend/handler/
create_table_as.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2024 RisingWave Labs
//
// Licensed 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.

use either::Either;
use pgwire::pg_response::StatementType;
use risingwave_common::catalog::{ColumnCatalog, ColumnDesc};
use risingwave_pb::ddl_service::TableJobType;
use risingwave_sqlparser::ast::{ColumnDef, ObjectName, OnConflict, Query, Statement};

use super::{HandlerArgs, RwPgResponse};
use crate::binder::BoundStatement;
use crate::error::{ErrorCode, Result};
use crate::handler::create_table::{gen_create_table_plan_without_source, ColumnIdGenerator};
use crate::handler::query::handle_query;
use crate::{build_graph, Binder, OptimizerContext};
pub async fn handle_create_as(
    handler_args: HandlerArgs,
    table_name: ObjectName,
    if_not_exists: bool,
    query: Box<Query>,
    column_defs: Vec<ColumnDef>,
    append_only: bool,
    on_conflict: Option<OnConflict>,
    with_version_column: Option<String>,
) -> Result<RwPgResponse> {
    if column_defs.iter().any(|column| column.data_type.is_some()) {
        return Err(ErrorCode::InvalidInputSyntax(
            "Should not specify data type in CREATE TABLE AS".into(),
        )
        .into());
    }
    let session = handler_args.session.clone();

    if let Either::Right(resp) = session.check_relation_name_duplicated(
        table_name.clone(),
        StatementType::CREATE_TABLE,
        if_not_exists,
    )? {
        return Ok(resp);
    }

    let mut col_id_gen = ColumnIdGenerator::new_initial();

    // Generate catalog descs from query
    let mut columns: Vec<_> = {
        let mut binder = Binder::new(&session);
        let bound = binder.bind(Statement::Query(query.clone()))?;
        if let BoundStatement::Query(query) = bound {
            // Create ColumnCatelog by Field
            query
                .schema()
                .fields()
                .iter()
                .map(|field| {
                    let id = col_id_gen.generate(&field.name);
                    ColumnCatalog {
                        column_desc: ColumnDesc::from_field_with_column_id(field, id.get_id()),
                        is_hidden: false,
                    }
                })
                .collect()
        } else {
            unreachable!()
        }
    };

    if column_defs.len() > columns.len() {
        return Err(ErrorCode::InvalidInputSyntax(
            "too many column names were specified".to_string(),
        )
        .into());
    }

    // Override column name if it specified in creaet statement.
    column_defs.iter().enumerate().for_each(|(idx, column)| {
        columns[idx].column_desc.name = column.name.real_value();
    });

    let (graph, source, table) = {
        let context = OptimizerContext::from_handler_args(handler_args.clone());
        let (_, secret_refs) = context.with_options().clone().into_parts();
        if !secret_refs.is_empty() {
            return Err(crate::error::ErrorCode::InvalidParameterValue(
                "Secret reference is not allowed in options for CREATE TABLE AS".to_string(),
            )
            .into());
        }
        let (plan, table) = gen_create_table_plan_without_source(
            context,
            table_name.clone(),
            columns,
            vec![],
            vec![],
            "".to_owned(), // TODO: support `SHOW CREATE TABLE` for `CREATE TABLE AS`
            vec![],        // No watermark should be defined in for `CREATE TABLE AS`
            append_only,
            on_conflict,
            with_version_column,
            Some(col_id_gen.into_version()),
        )?;
        let graph = build_graph(plan)?;

        (graph, None, table)
    };

    tracing::trace!(
        "name={}, graph=\n{}",
        table_name,
        serde_json::to_string_pretty(&graph).unwrap()
    );

    let catalog_writer = session.catalog_writer()?;
    catalog_writer
        .create_table(source, table, graph, TableJobType::Unspecified)
        .await?;

    // Generate insert
    let insert = Statement::Insert {
        table_name,
        columns: vec![],
        source: query,
        returning: vec![],
    };

    handle_query(handler_args, insert, vec![]).await
}