com.facebook.presto.metadata.MetadataUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.facebook.presto.metadata.MetadataUtil.java

Source

/*
 * Licensed 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 com.facebook.presto.metadata;

import com.facebook.presto.Session;
import com.facebook.presto.spi.ColumnMetadata;
import com.facebook.presto.spi.ConnectorTableMetadata;
import com.facebook.presto.spi.SchemaTableName;
import com.facebook.presto.spi.type.Type;
import com.facebook.presto.sql.analyzer.SemanticException;
import com.facebook.presto.sql.tree.Node;
import com.facebook.presto.sql.tree.QualifiedName;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;

import java.util.List;
import java.util.Optional;

import static com.facebook.presto.sql.analyzer.SemanticErrorCode.CATALOG_NOT_SPECIFIED;
import static com.facebook.presto.sql.analyzer.SemanticErrorCode.SCHEMA_NOT_SPECIFIED;
import static com.google.common.base.Preconditions.checkArgument;
import static java.lang.String.format;
import static java.util.Locale.ENGLISH;
import static java.util.Objects.requireNonNull;

public final class MetadataUtil {
    private MetadataUtil() {
    }

    public static void checkTableName(String catalogName, Optional<String> schemaName, Optional<String> tableName) {
        checkCatalogName(catalogName);
        checkSchemaName(schemaName);
        checkTableName(tableName);

        checkArgument(schemaName.isPresent() || !tableName.isPresent(),
                "tableName specified but schemaName is missing");
    }

    public static String checkCatalogName(String catalogName) {
        return checkLowerCase(catalogName, "catalogName");
    }

    public static String checkSchemaName(String schemaName) {
        return checkLowerCase(schemaName, "schemaName");
    }

    public static Optional<String> checkSchemaName(Optional<String> schemaName) {
        return checkLowerCase(schemaName, "schemaName");
    }

    public static String checkTableName(String tableName) {
        return checkLowerCase(tableName, "tableName");
    }

    public static Optional<String> checkTableName(Optional<String> tableName) {
        return checkLowerCase(tableName, "tableName");
    }

    public static String checkColumnName(String catalogName) {
        return checkLowerCase(catalogName, "catalogName");
    }

    public static void checkTableName(String catalogName, String schemaName, String tableName) {
        checkLowerCase(catalogName, "catalogName");
        checkLowerCase(schemaName, "schemaName");
        checkLowerCase(tableName, "tableName");
    }

    public static Optional<String> checkLowerCase(Optional<String> value, String name) {
        if (value.isPresent()) {
            checkLowerCase(value.get(), name);
        }
        return value;
    }

    public static String checkLowerCase(String value, String name) {
        if (value == null) {
            throw new NullPointerException(format("%s is null", name));
        }
        checkArgument(value.equals(value.toLowerCase(ENGLISH)), "%s is not lowercase", name);
        return value;
    }

    public static ColumnMetadata findColumnMetadata(ConnectorTableMetadata tableMetadata, String columnName) {
        for (ColumnMetadata columnMetadata : tableMetadata.getColumns()) {
            if (columnName.equals(columnMetadata.getName())) {
                return columnMetadata;
            }
        }
        return null;
    }

    public static QualifiedTableName createQualifiedTableName(Session session, Node node, QualifiedName name) {
        requireNonNull(session, "session is null");
        requireNonNull(name, "name is null");
        checkArgument(name.getParts().size() <= 3, "Too many dots in table name: %s", name);

        List<String> parts = Lists.reverse(name.getParts());
        String tableName = parts.get(0);
        String schemaName = (parts.size() > 1) ? parts.get(1)
                : session.getSchema().orElseThrow(() -> new SemanticException(CATALOG_NOT_SPECIFIED, node,
                        "Catalog must be specified when session catalog is not set"));
        String catalogName = (parts.size() > 2) ? parts.get(2)
                : session.getCatalog().orElseThrow(() -> new SemanticException(SCHEMA_NOT_SPECIFIED, node,
                        "Schema must be specified when session schema is not set"));

        return new QualifiedTableName(catalogName, schemaName, tableName);
    }

    public static boolean tableExists(Metadata metadata, Session session, String table) {
        if (!session.getCatalog().isPresent() || !session.getSchema().isPresent()) {
            return false;
        }
        QualifiedTableName name = new QualifiedTableName(session.getCatalog().get(), session.getSchema().get(),
                table);
        return metadata.getTableHandle(session, name).isPresent();
    }

    public static class SchemaMetadataBuilder {
        public static SchemaMetadataBuilder schemaMetadataBuilder() {
            return new SchemaMetadataBuilder();
        }

        private final ImmutableMap.Builder<SchemaTableName, ConnectorTableMetadata> tables = ImmutableMap.builder();

        public SchemaMetadataBuilder table(ConnectorTableMetadata tableMetadata) {
            tables.put(tableMetadata.getTable(), tableMetadata);
            return this;
        }

        public ImmutableMap<SchemaTableName, ConnectorTableMetadata> build() {
            return tables.build();
        }
    }

    public static class TableMetadataBuilder {
        public static TableMetadataBuilder tableMetadataBuilder(String schemaName, String tableName) {
            return new TableMetadataBuilder(new SchemaTableName(schemaName, tableName));
        }

        public static TableMetadataBuilder tableMetadataBuilder(SchemaTableName tableName) {
            return new TableMetadataBuilder(tableName);
        }

        private final SchemaTableName tableName;
        private final ImmutableList.Builder<ColumnMetadata> columns = ImmutableList.builder();
        private final ImmutableMap.Builder<String, Object> properties = ImmutableMap.builder();

        private TableMetadataBuilder(SchemaTableName tableName) {
            this.tableName = tableName;
        }

        public TableMetadataBuilder column(String columnName, Type type) {
            columns.add(new ColumnMetadata(columnName, type, false));
            return this;
        }

        public TableMetadataBuilder partitionKeyColumn(String columnName, Type type) {
            columns.add(new ColumnMetadata(columnName, type, true));
            return this;
        }

        public TableMetadataBuilder property(String name, Object value) {
            properties.put(name, value);
            return this;
        }

        public ConnectorTableMetadata build() {
            return new ConnectorTableMetadata(tableName, columns.build(), properties.build());
        }
    }
}