Skip to main content

risingwave_frontend/handler/create_source/external_schema/
adbc_snowflake.rs

1// Copyright 2025 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use anyhow::anyhow;
16use risingwave_common::array::arrow::from_arrow58_field;
17use risingwave_common::catalog::{ColumnCatalog, ColumnDesc, ColumnId};
18use risingwave_connector::source::ConnectorProperties;
19
20use crate::WithOptionsSecResolved;
21
22/// Extract column schema from ADBC Snowflake query result.
23/// This function infers the schema by executing the query and examining the result schema.
24pub async fn extract_adbc_snowflake_columns(
25    with_properties: &WithOptionsSecResolved,
26) -> anyhow::Result<Vec<ColumnCatalog>> {
27    let props = ConnectorProperties::extract(with_properties.clone(), true)?;
28    if let ConnectorProperties::AdbcSnowflake(properties) = props {
29        // Get the Arrow schema from Snowflake
30        let arrow_schema = properties.get_arrow_schema()?;
31
32        // Convert Arrow schema to RisingWave columns
33        let columns: Vec<ColumnCatalog> = arrow_schema
34            .fields()
35            .iter()
36            .enumerate()
37            .map(|(i, field)| {
38                let column_desc = ColumnDesc::named(
39                    field.name(),
40                    ColumnId::new((i + 1).try_into().unwrap()),
41                    from_arrow58_field(field).unwrap(),
42                );
43                ColumnCatalog {
44                    column_desc,
45                    is_hidden: false,
46                }
47            })
48            .collect();
49
50        tracing::info!(
51            "ADBC Snowflake inferred {} columns from table {}",
52            columns.len(),
53            properties.table
54        );
55
56        Ok(columns)
57    } else {
58        Err(anyhow!(format!(
59            "Invalid properties for ADBC Snowflake source: {:?}",
60            props
61        )))
62    }
63}