pepijnve commented on code in PR #26057: URL: https://github.com/apache/datafusion/pull/26057#discussion_r4394436194
########## datafusion/common/src/information_schema.rs: ########## @@ -0,0 +1,1046 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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. + +//! Schema and [`RecordBatch`] builders for the `information_schema` virtual +//! tables and `SHOW` statements. + +use crate::Result; +use crate::TableType; +use crate::config::ConfigEntry; +use arrow::array::ArrayRef; +use arrow::array::builder::{BooleanBuilder, StringBuilder, UInt8Builder, UInt64Builder}; +use arrow::datatypes::{DataType, Field, Schema, SchemaRef}; +use arrow::record_batch::RecordBatch; +use std::sync::Arc; + +/// Returns the Arrow schema of `information_schema.tables` rows. +/// +/// Columns are based on <https://www.postgresql.org/docs/current/infoschema-tables.html>. +pub fn tables_schema() -> Schema { + Schema::new(vec![ + Field::new("table_catalog", DataType::Utf8, false), + Field::new("table_schema", DataType::Utf8, false), + Field::new("table_name", DataType::Utf8, false), + Field::new("table_type", DataType::Utf8, false), + ]) +} + +/// Builds `information_schema.tables` rows. +#[derive(Debug)] +pub struct InformationSchemaTablesBuilder { + schema: SchemaRef, + catalog_names: StringBuilder, + schema_names: StringBuilder, + table_names: StringBuilder, + table_types: StringBuilder, +} + +impl Default for InformationSchemaTablesBuilder { + fn default() -> Self { + Self::new() + } +} + +impl InformationSchemaTablesBuilder { + /// Construct an empty builder. + pub fn new() -> Self { + Self { + schema: Arc::new(tables_schema()), + catalog_names: StringBuilder::new(), + schema_names: StringBuilder::new(), + table_names: StringBuilder::new(), + table_types: StringBuilder::new(), + } + } + + /// Append one row. + pub fn add_table( + &mut self, + catalog_name: impl AsRef<str>, + schema_name: impl AsRef<str>, + table_name: impl AsRef<str>, + table_type: TableType, + ) { + // Note: append_value is actually infallible. + self.catalog_names.append_value(catalog_name.as_ref()); + self.schema_names.append_value(schema_name.as_ref()); + self.table_names.append_value(table_name.as_ref()); + self.table_types.append_value(match table_type { + TableType::Base => "BASE TABLE", + TableType::View => "VIEW", + TableType::Temporary => "LOCAL TEMPORARY", + }); + } + + /// Finalize the builder into a [`RecordBatch`]. + pub fn finish(&mut self) -> RecordBatch { + RecordBatch::try_new( + Arc::clone(&self.schema), + vec![ + Arc::new(self.catalog_names.finish()), + Arc::new(self.schema_names.finish()), + Arc::new(self.table_names.finish()), + Arc::new(self.table_types.finish()), + ], + ) + .unwrap() + } +} + +/// Returns the Arrow schema of `information_schema.views` rows. +/// +/// Columns are based on <https://www.postgresql.org/docs/current/infoschema-views.html>. +pub fn views_schema() -> Schema { + Schema::new(vec![ + Field::new("table_catalog", DataType::Utf8, false), + Field::new("table_schema", DataType::Utf8, false), + Field::new("table_name", DataType::Utf8, false), + Field::new("definition", DataType::Utf8, true), + ]) +} + +/// Builds `information_schema.views` rows. +#[derive(Debug)] +pub struct InformationSchemaViewBuilder { Review Comment: Indeed. I wanted to avoid duplicating the schema in multiple places. By keeping the schema itself and builders for the arrays together it's easier to modify these going forward. Looking at this again, I think we could move it from `common` to `expr`. Everything else that needs this is dependent on that crate already as well. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
