Skip to main content

datafusion_app/catalog/
mod.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::{
21    arrow::{
22        array::StringArray,
23        datatypes::{DataType, Field, Schema},
24        record_batch::RecordBatch,
25    },
26    catalog::{CatalogProvider, MemoryCatalogProvider, MemorySchemaProvider, SchemaProvider},
27    common::Result,
28    datasource::MemTable,
29    DATAFUSION_VERSION,
30};
31
32use crate::config::ExecutionConfig;
33
34pub fn create_app_catalog(
35    _config: &ExecutionConfig,
36    app_name: &str,
37    app_version: &str,
38) -> Result<Arc<dyn CatalogProvider>> {
39    let catalog = MemoryCatalogProvider::new();
40    let meta_schema = Arc::new(MemorySchemaProvider::new());
41    catalog.register_schema("meta", Arc::<MemorySchemaProvider>::clone(&meta_schema))?;
42    let versions_table = try_create_meta_versions_table(app_name, app_version)?;
43    meta_schema.register_table("versions".to_string(), versions_table)?;
44    Ok(Arc::new(catalog))
45}
46
47fn try_create_meta_versions_table(app_name: &str, app_version: &str) -> Result<Arc<MemTable>> {
48    let fields = vec![
49        Field::new(app_name, DataType::Utf8, false),
50        Field::new("datafusion", DataType::Utf8, false),
51        Field::new("datafusion-app", DataType::Utf8, false),
52    ];
53    let schema = Arc::new(Schema::new(fields));
54
55    let app_version_arr = StringArray::from(vec![app_version]);
56    let datafusion_version_arr = StringArray::from(vec![DATAFUSION_VERSION]);
57    let datafusion_app_version_arr = StringArray::from(vec![env!("CARGO_PKG_VERSION")]);
58    let batches = RecordBatch::try_new(
59        Arc::<Schema>::clone(&schema),
60        vec![
61            Arc::new(app_version_arr),
62            Arc::new(datafusion_version_arr),
63            Arc::new(datafusion_app_version_arr),
64        ],
65    )?;
66
67    Ok(Arc::new(MemTable::try_new(schema, vec![vec![batches]])?))
68}