All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.flink.table.client.gateway.TableDesc Maven / Gradle / Ivy

/*
 * 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.
 */

package org.apache.flink.table.client.gateway;

import org.apache.flink.table.catalog.CatalogTable;
import org.apache.flink.table.catalog.CatalogView;
import org.apache.flink.util.StringUtils;

import org.apache.flink.shaded.guava18.com.google.common.base.Preconditions;

import java.util.Arrays;
import java.util.Map;
import java.util.TreeMap;

/**
 * Description of a {@link org.apache.flink.table.catalog.CatalogTable}.
 */
public class TableDesc {

	// fully qualified name of format "catalog.db.table"
	private final String[] fullName;
	private final CatalogTable table;
	private final boolean extended;

	public TableDesc(String[] fullName, CatalogTable table, boolean extended) {
		Preconditions.checkArgument(fullName != null && fullName.length == 3,
			"Invalid table name " + Arrays.toString(fullName));
		this.fullName = fullName;
		this.table = table;
		this.extended = extended;
	}

	public String catalogName() {
		return fullName[0];
	}

	public String dbName() {
		return fullName[1];
	}

	public String tableName() {
		return fullName[2];
	}

	public CatalogTable getTable() {
		return table;
	}

	public boolean isExtended() {
		return extended;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder(table.getTableSchema().toString());
		// prints partition columns
		if (table.isPartitioned()) {
			builder.append("\n# Partition Information\n");
			for (String partCol : table.getPartitionColumnNames()) {
				builder.append(DescFormatUtil.colDesc(table.getTableSchema(), partCol));
			}
		}
		if (extended) {
			// print detailed table information
			builder.append("\n# Detailed Table Information\n");
			DescFormatUtil.formatNamedValue("Catalog:", catalogName(), builder);
			DescFormatUtil.formatNamedValue("Database:", dbName(), builder);
			DescFormatUtil.formatNamedValue("Table Type:", table.getTableType(), builder);
			if (!StringUtils.isNullOrWhitespaceOnly(table.getComment())) {
				DescFormatUtil.formatNamedValue("Comment:", table.getComment(), builder);
			}

			// print table properties
			if (!table.getProperties().isEmpty()) {
				builder.append("Table Properties:\n");
				TreeMap treeMap = new TreeMap<>(table.getProperties());
				// TODO: some properties may be redundant, e.g. hive.table.field.names. Need a way to filter them out.
				for (Map.Entry entry : treeMap.entrySet()) {
					builder.append(DescFormatUtil.FIELD_DELIM);
					DescFormatUtil.formatNamedValue(entry.getKey(), entry.getValue(), builder);
				}
			}

			// print view information
			if (table instanceof CatalogView) {
				CatalogView view = (CatalogView) table;
				builder.append("\n# View Information\n");
				DescFormatUtil.formatNamedValue("View Original Text:", view.getOriginalQuery(), builder);
				DescFormatUtil.formatNamedValue("View Expanded Text:", view.getExpandedQuery(), builder);
			}
		}
		return builder.toString();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy