|
| 1 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | +// you may not use this file except in compliance with the License. |
| 3 | +// You may obtain a copy of the License at |
| 4 | +// |
| 5 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +// |
| 7 | +// Unless required by applicable law or agreed to in writing, software |
| 8 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +// See the License for the specific language governing permissions and |
| 11 | +// limitations under the License. |
| 12 | + |
| 13 | +use std::sync::Arc; |
| 14 | + |
| 15 | +use arrow_array::{Int32Array, StringArray}; |
| 16 | +use arrow_schema::{DataType, Field, Schema}; |
| 17 | + |
| 18 | +use super::DataSet; |
| 19 | +use crate::sql::schema::{schema_columns_one_line, stream_table_row_detail, StreamTable}; |
| 20 | + |
| 21 | +#[derive(Clone, Debug)] |
| 22 | +pub struct ShowCatalogTablesResult { |
| 23 | + names: Vec<String>, |
| 24 | + kinds: Vec<String>, |
| 25 | + column_counts: Vec<i32>, |
| 26 | + schema_lines: Vec<String>, |
| 27 | + details: Vec<String>, |
| 28 | +} |
| 29 | + |
| 30 | +impl ShowCatalogTablesResult { |
| 31 | + pub fn from_tables(tables: &[Arc<StreamTable>]) -> Self { |
| 32 | + let mut names = Vec::with_capacity(tables.len()); |
| 33 | + let mut kinds = Vec::with_capacity(tables.len()); |
| 34 | + let mut column_counts = Vec::with_capacity(tables.len()); |
| 35 | + let mut schema_lines = Vec::with_capacity(tables.len()); |
| 36 | + let mut details = Vec::with_capacity(tables.len()); |
| 37 | + |
| 38 | + for t in tables { |
| 39 | + let schema = t.schema(); |
| 40 | + let ncols = schema.fields().len() as i32; |
| 41 | + names.push(t.name().to_string()); |
| 42 | + kinds.push(match t.as_ref() { |
| 43 | + StreamTable::Source { .. } => "SOURCE", |
| 44 | + StreamTable::Sink { .. } => "SINK", |
| 45 | + } |
| 46 | + .to_string()); |
| 47 | + column_counts.push(ncols); |
| 48 | + schema_lines.push(schema_columns_one_line(&schema)); |
| 49 | + details.push(stream_table_row_detail(t.as_ref())); |
| 50 | + } |
| 51 | + |
| 52 | + Self { |
| 53 | + names, |
| 54 | + kinds, |
| 55 | + column_counts, |
| 56 | + schema_lines, |
| 57 | + details, |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +impl DataSet for ShowCatalogTablesResult { |
| 63 | + fn to_record_batch(&self) -> arrow_array::RecordBatch { |
| 64 | + let schema = Arc::new(Schema::new(vec![ |
| 65 | + Field::new("table_name", DataType::Utf8, false), |
| 66 | + Field::new("kind", DataType::Utf8, false), |
| 67 | + Field::new("column_count", DataType::Int32, false), |
| 68 | + Field::new("schema_columns", DataType::Utf8, false), |
| 69 | + Field::new("details", DataType::Utf8, false), |
| 70 | + ])); |
| 71 | + |
| 72 | + arrow_array::RecordBatch::try_new( |
| 73 | + schema, |
| 74 | + vec![ |
| 75 | + Arc::new(StringArray::from( |
| 76 | + self.names.iter().map(|s| s.as_str()).collect::<Vec<_>>(), |
| 77 | + )), |
| 78 | + Arc::new(StringArray::from( |
| 79 | + self.kinds.iter().map(|s| s.as_str()).collect::<Vec<_>>(), |
| 80 | + )), |
| 81 | + Arc::new(Int32Array::from(self.column_counts.clone())), |
| 82 | + Arc::new(StringArray::from( |
| 83 | + self.schema_lines.iter().map(|s| s.as_str()).collect::<Vec<_>>(), |
| 84 | + )), |
| 85 | + Arc::new(StringArray::from( |
| 86 | + self.details.iter().map(|s| s.as_str()).collect::<Vec<_>>(), |
| 87 | + )), |
| 88 | + ], |
| 89 | + ) |
| 90 | + .unwrap_or_else(|_| arrow_array::RecordBatch::new_empty(Arc::new(Schema::empty()))) |
| 91 | + } |
| 92 | +} |
0 commit comments