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
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

use mz_sql_parser::ast::{
    AvroSchema, CreateSourceConnector, CreateSourceFormat, CreateSourceStatement, CsrConnector,
    CsrConnectorAvro, CsrConnectorProto, Format, KafkaConnector, KafkaSourceConnector,
    ProtobufSchema,
};

use crate::catalog::SessionCatalog;
use crate::names::{Aug, ResolvedObjectName};
use crate::plan::StatementContext;

/// Uses the provided catalog to populate all Connector references with the values of the connector
/// it references allowing it to be used as if there was no indirection
pub fn populate_connectors(
    mut stmt: CreateSourceStatement<Aug>,
    catalog: &dyn SessionCatalog,
) -> Result<CreateSourceStatement<Aug>, anyhow::Error> {
    let scx = StatementContext::new(None, catalog);

    if let CreateSourceStatement {
        connector:
            CreateSourceConnector::Kafka(
                KafkaSourceConnector {
                    connector: kafka_connector @ KafkaConnector::Reference { .. },
                    ..
                },
                ..,
            ),
        ..
    } = &mut stmt
    {
        let name = match kafka_connector {
            KafkaConnector::Reference { connector, .. } => connector,
            _ => unreachable!(),
        };
        let conn = scx.get_item_by_resolved_name(&name)?;
        let resolved_source_connector = conn.catalog_connector()?;
        *kafka_connector = KafkaConnector::Reference {
            connector: name.to_owned(),
            broker: Some(resolved_source_connector.uri()),
            with_options: Some(resolved_source_connector.options()),
        };
    };

    if let CreateSourceStatement {
        format: CreateSourceFormat::Bare(format),
        ..
    } = &mut stmt
    {
        populate_connector_for_format(&scx, format)?;
        return Ok(stmt);
    };
    if let CreateSourceStatement {
        format: CreateSourceFormat::KeyValue { key, value },
        ..
    } = &mut stmt
    {
        populate_connector_for_format(&scx, key)?;
        populate_connector_for_format(&scx, value)?;
    };
    Ok(stmt)
}

/// Helper function which reifies any connectors in a single [`Format`]
fn populate_connector_for_format(
    scx: &StatementContext,
    format: &mut Format<Aug>,
) -> Result<(), anyhow::Error> {
    Ok(match format {
        Format::Avro(avro_schema) => match avro_schema {
            AvroSchema::Csr {
                csr_connector:
                    CsrConnectorAvro {
                        connector: csr_connector @ CsrConnector::Reference { .. },
                        ..
                    },
            } => {
                let name = match csr_connector {
                    CsrConnector::Reference { connector, .. } => connector,
                    _ => unreachable!(),
                };
                *csr_connector = populate_csr_connector(scx, name.clone())?;
            }
            _ => {}
        },
        Format::Protobuf(proto_schema) => match proto_schema {
            ProtobufSchema::Csr {
                csr_connector:
                    CsrConnectorProto {
                        connector: csr_connector @ CsrConnector::Reference { .. },
                        ..
                    },
            } => {
                let name = match csr_connector {
                    CsrConnector::Reference { connector, .. } => connector,
                    _ => unreachable!(),
                };
                *csr_connector = populate_csr_connector(scx, name.clone())?;
            }
            _ => {}
        },
        _ => {}
    })
}

/// Helper function which reifies individual [`CsrConnector::Reference`] instances
fn populate_csr_connector(
    scx: &StatementContext,
    name: ResolvedObjectName,
) -> Result<CsrConnector<Aug>, anyhow::Error> {
    let conn = scx.get_item_by_resolved_name(&name)?;
    let resolved_csr_connector = conn.catalog_connector()?;
    Ok(CsrConnector::Reference {
        connector: name,
        url: Some(resolved_csr_connector.uri()),
        with_options: Some(resolved_csr_connector.options()),
    })
}