Search in sources :

Example 11 with SourceTable

use of org.apache.hadoop.hive.metastore.api.SourceTable in project hive by apache.

the class AlterMaterializedViewRewriteAnalyzer method analyzeInternal.

@Override
public void analyzeInternal(ASTNode root) throws SemanticException {
    TableName tableName = getQualifiedTableName((ASTNode) root.getChild(0));
    // Value for the flag
    boolean rewriteEnable;
    switch(root.getChild(1).getType()) {
        case HiveParser.TOK_REWRITE_ENABLED:
            rewriteEnable = true;
            break;
        case HiveParser.TOK_REWRITE_DISABLED:
            rewriteEnable = false;
            break;
        default:
            throw new SemanticException("Invalid alter materialized view expression");
    }
    // It can be fully qualified name or use default database
    Table materializedViewTable = getTable(tableName, true);
    // only uses transactional (MM and ACID) tables
    if (rewriteEnable) {
        for (SourceTable sourceTable : materializedViewTable.getMVMetadata().getSourceTables()) {
            if (!AcidUtils.isTransactionalTable(sourceTable.getTable())) {
                throw new SemanticException("Automatic rewriting for materialized view cannot be enabled if the " + "materialized view uses non-transactional tables");
            }
        }
    }
    AlterMaterializedViewRewriteDesc desc = new AlterMaterializedViewRewriteDesc(tableName.getNotEmptyDbTable(), rewriteEnable);
    if (AcidUtils.isTransactionalTable(materializedViewTable)) {
        ddlDescWithWriteId = desc;
    }
    inputs.add(new ReadEntity(materializedViewTable));
    outputs.add(new WriteEntity(materializedViewTable, WriteEntity.WriteType.DDL_EXCLUSIVE));
    // Create task for alterMVRewriteDesc
    DDLWork work = new DDLWork(getInputs(), getOutputs(), desc);
    Task<?> targetTask = TaskFactory.get(work);
    // Create task to update rewrite flag as dependant of previous one
    MaterializedViewUpdateDesc materializedViewUpdateDesc = new MaterializedViewUpdateDesc(tableName.getNotEmptyDbTable(), rewriteEnable, !rewriteEnable, false);
    DDLWork updateDdlWork = new DDLWork(getInputs(), getOutputs(), materializedViewUpdateDesc);
    targetTask.addDependentTask(TaskFactory.get(updateDdlWork, conf));
    // Add root task
    rootTasks.add(targetTask);
}
Also used : ReadEntity(org.apache.hadoop.hive.ql.hooks.ReadEntity) TableName(org.apache.hadoop.hive.common.TableName) Table(org.apache.hadoop.hive.ql.metadata.Table) SourceTable(org.apache.hadoop.hive.metastore.api.SourceTable) MaterializedViewUpdateDesc(org.apache.hadoop.hive.ql.ddl.view.materialized.update.MaterializedViewUpdateDesc) DDLWork(org.apache.hadoop.hive.ql.ddl.DDLWork) SourceTable(org.apache.hadoop.hive.metastore.api.SourceTable) WriteEntity(org.apache.hadoop.hive.ql.hooks.WriteEntity) SemanticException(org.apache.hadoop.hive.ql.parse.SemanticException)

Example 12 with SourceTable

use of org.apache.hadoop.hive.metastore.api.SourceTable in project hive by apache.

the class SemanticAnalyzer method walkASTMarkTABREF.

private void walkASTMarkTABREF(TableMask tableMask, ASTNode ast, Set<String> cteAlias, Context ctx) throws SemanticException {
    Queue<Node> queue = new LinkedList<>();
    queue.add(ast);
    Map<HivePrivilegeObject, MaskAndFilterInfo> basicInfos = new LinkedHashMap<>();
    while (!queue.isEmpty()) {
        ASTNode astNode = (ASTNode) queue.poll();
        if (astNode.getToken().getType() == HiveParser.TOK_TABREF) {
            int aliasIndex = 0;
            StringBuilder additionalTabInfo = new StringBuilder();
            for (int index = 1; index < astNode.getChildCount(); index++) {
                ASTNode ct = (ASTNode) astNode.getChild(index);
                if (ct.getToken().getType() == HiveParser.TOK_TABLEBUCKETSAMPLE || ct.getToken().getType() == HiveParser.TOK_TABLESPLITSAMPLE || ct.getToken().getType() == HiveParser.TOK_TABLEPROPERTIES) {
                    additionalTabInfo.append(ctx.getTokenRewriteStream().toString(ct.getTokenStartIndex(), ct.getTokenStopIndex()));
                } else {
                    aliasIndex = index;
                }
            }
            ASTNode tableTree = (ASTNode) (astNode.getChild(0));
            String tabIdName = getUnescapedName(tableTree);
            String alias;
            if (aliasIndex != 0) {
                alias = unescapeIdentifier(astNode.getChild(aliasIndex).getText());
            } else {
                alias = getUnescapedUnqualifiedTableName(tableTree);
            }
            // select * from TAB2 [no masking]
            if (cteAlias.contains(tabIdName)) {
                continue;
            }
            Table table = null;
            try {
                table = getTableObjectByName(tabIdName, false);
            } catch (HiveException e) {
                // This should not happen.
                throw new SemanticException("Got exception though getTableObjectByName method should ignore it");
            }
            if (table == null) {
                // Table may not be found when materialization of CTE is on.
                STATIC_LOG.debug("Table " + tabIdName + " is not found in walkASTMarkTABREF.");
                continue;
            }
            if (table.isMaterializedView()) {
                // do not apply any policies.
                for (SourceTable sourceTable : table.getMVMetadata().getSourceTables()) {
                    String qualifiedTableName = TableName.getDbTable(sourceTable.getTable().getDbName(), sourceTable.getTable().getTableName());
                    try {
                        table = getTableObjectByName(qualifiedTableName, true);
                    } catch (HiveException e) {
                        // This should not happen.
                        throw new SemanticException("Table " + qualifiedTableName + " not found when trying to obtain it to check masking/filtering policies");
                    }
                    List<String> colNames = new ArrayList<>();
                    extractColumnInfos(table, colNames, new ArrayList<>());
                    basicInfos.put(new HivePrivilegeObject(table.getDbName(), table.getTableName(), colNames), null);
                }
            } else {
                List<String> colNames;
                List<String> colTypes;
                if (this.ctx.isCboSucceeded() && this.columnAccessInfo != null && (colNames = this.columnAccessInfo.getTableToColumnAllAccessMap().get(table.getCompleteName())) != null) {
                    Map<String, String> colNameToType = table.getAllCols().stream().collect(Collectors.toMap(FieldSchema::getName, FieldSchema::getType));
                    colTypes = colNames.stream().map(colNameToType::get).collect(Collectors.toList());
                } else {
                    colNames = new ArrayList<>();
                    colTypes = new ArrayList<>();
                    extractColumnInfos(table, colNames, colTypes);
                }
                basicInfos.put(new HivePrivilegeObject(table.getDbName(), table.getTableName(), colNames), new MaskAndFilterInfo(colTypes, additionalTabInfo.toString(), alias, astNode, table.isView(), table.isNonNative()));
            }
        }
        if (astNode.getChildCount() > 0 && !IGNORED_TOKENS.contains(astNode.getToken().getType())) {
            for (Node child : astNode.getChildren()) {
                queue.offer(child);
            }
        }
    }
    List<HivePrivilegeObject> basicPrivObjs = new ArrayList<>(basicInfos.keySet());
    List<HivePrivilegeObject> needRewritePrivObjs = tableMask.applyRowFilterAndColumnMasking(basicPrivObjs);
    if (needRewritePrivObjs != null && !needRewritePrivObjs.isEmpty()) {
        for (HivePrivilegeObject privObj : needRewritePrivObjs) {
            MaskAndFilterInfo info = basicInfos.get(privObj);
            // First we check whether entity actually needs masking or filtering
            if (tableMask.needsMaskingOrFiltering(privObj)) {
                if (info == null) {
                    // when mask/filter should be applied on source tables
                    throw new SemanticException(ErrorMsg.MASKING_FILTERING_ON_MATERIALIZED_VIEWS_SOURCES, privObj.getDbname(), privObj.getObjectName());
                } else {
                    String replacementText = tableMask.create(privObj, info);
                    // We don't support masking/filtering against ACID query at the moment
                    if (ctx.getIsUpdateDeleteMerge()) {
                        throw new SemanticException(ErrorMsg.MASKING_FILTERING_ON_ACID_NOT_SUPPORTED, privObj.getDbname(), privObj.getObjectName());
                    }
                    tableMask.setNeedsRewrite(true);
                    tableMask.addTranslation(info.astNode, replacementText);
                }
            }
        }
    }
}
Also used : SourceTable(org.apache.hadoop.hive.metastore.api.SourceTable) Table(org.apache.hadoop.hive.ql.metadata.Table) HiveException(org.apache.hadoop.hive.ql.metadata.HiveException) Node(org.apache.hadoop.hive.ql.lib.Node) ArrayList(java.util.ArrayList) SourceTable(org.apache.hadoop.hive.metastore.api.SourceTable) HivePrivilegeObject(org.apache.hadoop.hive.ql.security.authorization.plugin.HivePrivilegeObject) LinkedList(java.util.LinkedList) SQLUniqueConstraint(org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint) SQLCheckConstraint(org.apache.hadoop.hive.metastore.api.SQLCheckConstraint) SQLDefaultConstraint(org.apache.hadoop.hive.metastore.api.SQLDefaultConstraint) DefaultConstraint(org.apache.hadoop.hive.ql.metadata.DefaultConstraint) SQLNotNullConstraint(org.apache.hadoop.hive.metastore.api.SQLNotNullConstraint) LinkedHashMap(java.util.LinkedHashMap) CalciteSemanticException(org.apache.hadoop.hive.ql.optimizer.calcite.CalciteSemanticException)

Example 13 with SourceTable

use of org.apache.hadoop.hive.metastore.api.SourceTable in project hive by apache.

the class TextDescTableFormatter method getViewInfo.

private void getViewInfo(StringBuilder tableInfo, Table table, boolean isOutputPadded) {
    formatOutput("Original Query:", table.getViewOriginalText(), tableInfo);
    formatOutput("Expanded Query:", table.getViewExpandedText(), tableInfo);
    if (table.isMaterializedView()) {
        formatOutput("Rewrite Enabled:", table.isRewriteEnabled() ? "Yes" : "No", tableInfo);
        formatOutput("Outdated for Rewriting:", table.isOutdatedForRewriting() == null ? "Unknown" : table.isOutdatedForRewriting() ? "Yes" : "No", tableInfo);
        tableInfo.append(LINE_DELIM).append("# Materialized View Source table information").append(LINE_DELIM);
        TextMetaDataTable metaDataTable = new TextMetaDataTable();
        metaDataTable.addRow("Table name", "I/U/D since last rebuild");
        List<SourceTable> sourceTableList = new ArrayList<>(table.getMVMetadata().getSourceTables());
        sourceTableList.sort(Comparator.<SourceTable, String>comparing(sourceTable -> sourceTable.getTable().getDbName()).thenComparing(sourceTable -> sourceTable.getTable().getTableName()));
        for (SourceTable sourceTable : sourceTableList) {
            String qualifiedTableName = TableName.getQualified(sourceTable.getTable().getCatName(), sourceTable.getTable().getDbName(), sourceTable.getTable().getTableName());
            metaDataTable.addRow(qualifiedTableName, String.format("%d/%d/%d", sourceTable.getInsertedCount(), sourceTable.getUpdatedCount(), sourceTable.getDeletedCount()));
        }
        tableInfo.append(metaDataTable.renderTable(isOutputPadded));
    }
}
Also used : CheckConstraint(org.apache.hadoop.hive.ql.metadata.CheckConstraint) Date(java.util.Date) ConfVars(org.apache.hadoop.hive.conf.HiveConf.ConfVars) DefaultConstraintCol(org.apache.hadoop.hive.ql.metadata.DefaultConstraint.DefaultConstraintCol) StatsSetupConst(org.apache.hadoop.hive.common.StatsSetupConst) DataOutputStream(java.io.DataOutputStream) ForeignKeyInfo(org.apache.hadoop.hive.ql.metadata.ForeignKeyInfo) Map(java.util.Map) StorageDescriptor(org.apache.hadoop.hive.metastore.api.StorageDescriptor) LINE_DELIM(org.apache.hadoop.hive.ql.ddl.ShowUtils.LINE_DELIM) NotNullConstraint(org.apache.hadoop.hive.ql.metadata.NotNullConstraint) DefaultConstraint(org.apache.hadoop.hive.ql.metadata.DefaultConstraint) UniqueConstraint(org.apache.hadoop.hive.ql.metadata.UniqueConstraint) ColumnStatisticsObj(org.apache.hadoop.hive.metastore.api.ColumnStatisticsObj) Set(java.util.Set) SessionState(org.apache.hadoop.hive.ql.session.SessionState) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) ForeignKeyCol(org.apache.hadoop.hive.ql.metadata.ForeignKeyInfo.ForeignKeyCol) HiveStringUtils(org.apache.hive.common.util.HiveStringUtils) UniqueConstraintCol(org.apache.hadoop.hive.ql.metadata.UniqueConstraint.UniqueConstraintCol) Entry(java.util.Map.Entry) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HiveException(org.apache.hadoop.hive.ql.metadata.HiveException) DEFAULT_STRINGBUILDER_SIZE(org.apache.hadoop.hive.ql.ddl.ShowUtils.DEFAULT_STRINGBUILDER_SIZE) DescTableDesc(org.apache.hadoop.hive.ql.ddl.table.info.desc.DescTableDesc) FIELD_DELIM(org.apache.hadoop.hive.ql.ddl.ShowUtils.FIELD_DELIM) CollectionUtils(org.apache.commons.collections4.CollectionUtils) TABLE_IS_CTAS(org.apache.hadoop.hive.metastore.api.hive_metastoreConstants.TABLE_IS_CTAS) ArrayList(java.util.ArrayList) Utilities(org.apache.hadoop.hive.ql.exec.Utilities) PrimaryKeyInfo(org.apache.hadoop.hive.ql.metadata.PrimaryKeyInfo) ShowUtils.formatOutput(org.apache.hadoop.hive.ql.ddl.ShowUtils.formatOutput) ShowUtils(org.apache.hadoop.hive.ql.ddl.ShowUtils) SourceTable(org.apache.hadoop.hive.metastore.api.SourceTable) TableName(org.apache.hadoop.hive.common.TableName) TextMetaDataTable(org.apache.hadoop.hive.ql.ddl.ShowUtils.TextMetaDataTable) PlanUtils(org.apache.hadoop.hive.ql.plan.PlanUtils) MapUtils(org.apache.commons.collections4.MapUtils) PartitionTransformSpec(org.apache.hadoop.hive.ql.parse.PartitionTransformSpec) HiveConf(org.apache.hadoop.hive.conf.HiveConf) ALIGNMENT(org.apache.hadoop.hive.ql.ddl.ShowUtils.ALIGNMENT) StringEscapeUtils(org.apache.commons.text.StringEscapeUtils) Table(org.apache.hadoop.hive.ql.metadata.Table) IOException(java.io.IOException) Partition(org.apache.hadoop.hive.ql.metadata.Partition) FieldSchema(org.apache.hadoop.hive.metastore.api.FieldSchema) TreeMap(java.util.TreeMap) TableType(org.apache.hadoop.hive.metastore.TableType) Comparator(java.util.Comparator) CheckConstraintCol(org.apache.hadoop.hive.ql.metadata.CheckConstraint.CheckConstraintCol) Collections(java.util.Collections) ArrayList(java.util.ArrayList) SourceTable(org.apache.hadoop.hive.metastore.api.SourceTable) TextMetaDataTable(org.apache.hadoop.hive.ql.ddl.ShowUtils.TextMetaDataTable)

Example 14 with SourceTable

use of org.apache.hadoop.hive.metastore.api.SourceTable in project hive by apache.

the class MaterializedViewMetadata method from.

private SourceTable from(SourceTable sourceTable) {
    SourceTable newSourceTable = new SourceTable();
    newSourceTable.setTable(sourceTable.getTable());
    newSourceTable.setInsertedCount(0L);
    newSourceTable.setUpdatedCount(0L);
    newSourceTable.setDeletedCount(0L);
    return newSourceTable;
}
Also used : SourceTable(org.apache.hadoop.hive.metastore.api.SourceTable)

Example 15 with SourceTable

use of org.apache.hadoop.hive.metastore.api.SourceTable in project hive by apache.

the class TxnHandler method wasCompacted.

private Boolean wasCompacted(CreationMetadata creationMetadata) throws MetaException {
    Set<String> insertOnlyTables = new HashSet<>();
    for (SourceTable sourceTable : creationMetadata.getSourceTables()) {
        Table table = sourceTable.getTable();
        String transactionalProp = table.getParameters().get(hive_metastoreConstants.TABLE_TRANSACTIONAL_PROPERTIES);
        if (!"insert_only".equalsIgnoreCase(transactionalProp)) {
            continue;
        }
        insertOnlyTables.add(TableName.getDbTable(sourceTable.getTable().getDbName(), sourceTable.getTable().getTableName()));
    }
    if (insertOnlyTables.isEmpty()) {
        return false;
    }
    // We are composing a query that returns a single row if a compaction happened after
    // the materialization was created. Otherwise, query returns 0 rows.
    // Parse validReaderWriteIdList from creation metadata
    final ValidTxnWriteIdList validReaderWriteIdList = new ValidTxnWriteIdList(creationMetadata.getValidTxnList());
    List<String> params = new ArrayList<>();
    StringBuilder queryCompletedCompactions = new StringBuilder();
    StringBuilder queryCompactionQueue = new StringBuilder();
    // compose a query that select transactions containing an update...
    queryCompletedCompactions.append("SELECT 1 FROM \"COMPLETED_COMPACTIONS\" WHERE (");
    queryCompactionQueue.append("SELECT 1 FROM \"COMPACTION_QUEUE\" WHERE (");
    int i = 0;
    for (String fullyQualifiedName : insertOnlyTables) {
        ValidWriteIdList tblValidWriteIdList = validReaderWriteIdList.getTableValidWriteIdList(fullyQualifiedName);
        if (tblValidWriteIdList == null) {
            LOG.warn("ValidWriteIdList for table {} not present in creation metadata, this should not happen", fullyQualifiedName);
            return null;
        }
        // where the transaction had to be committed after the materialization was created...
        if (i != 0) {
            queryCompletedCompactions.append("OR");
            queryCompactionQueue.append("OR");
        }
        String[] names = TxnUtils.getDbTableName(fullyQualifiedName);
        assert (names.length == 2);
        queryCompletedCompactions.append(" (\"CC_DATABASE\"=? AND \"CC_TABLE\"=?");
        queryCompactionQueue.append(" (\"CQ_DATABASE\"=? AND \"CQ_TABLE\"=?");
        params.add(names[0]);
        params.add(names[1]);
        queryCompletedCompactions.append(" AND (\"CC_HIGHEST_WRITE_ID\" > ");
        queryCompletedCompactions.append(tblValidWriteIdList.getHighWatermark());
        queryCompletedCompactions.append(tblValidWriteIdList.getInvalidWriteIds().length == 0 ? ") " : " OR \"CC_HIGHEST_WRITE_ID\" IN(" + StringUtils.join(",", Arrays.asList(ArrayUtils.toObject(tblValidWriteIdList.getInvalidWriteIds()))) + ") ) ");
        queryCompletedCompactions.append(") ");
        queryCompactionQueue.append(") ");
        i++;
    }
    // ... and where the transaction has already been committed as per snapshot taken
    // when we are running current query
    queryCompletedCompactions.append(")");
    queryCompactionQueue.append(") ");
    // Execute query
    queryCompletedCompactions.append(" UNION ");
    queryCompletedCompactions.append(queryCompactionQueue);
    List<String> paramsTwice = new ArrayList<>(params);
    paramsTwice.addAll(params);
    return executeBoolean(queryCompletedCompactions.toString(), paramsTwice, "Unable to retrieve materialization invalidation information: compactions");
}
Also used : Table(org.apache.hadoop.hive.metastore.api.Table) SourceTable(org.apache.hadoop.hive.metastore.api.SourceTable) ValidWriteIdList(org.apache.hadoop.hive.common.ValidWriteIdList) ArrayList(java.util.ArrayList) SourceTable(org.apache.hadoop.hive.metastore.api.SourceTable) ValidTxnWriteIdList(org.apache.hadoop.hive.common.ValidTxnWriteIdList) Savepoint(java.sql.Savepoint) HashSet(java.util.HashSet)

Aggregations

SourceTable (org.apache.hadoop.hive.metastore.api.SourceTable)18 Table (org.apache.hadoop.hive.metastore.api.Table)8 HashSet (java.util.HashSet)7 ArrayList (java.util.ArrayList)6 TableName (org.apache.hadoop.hive.common.TableName)4 CreationMetadata (org.apache.hadoop.hive.metastore.api.CreationMetadata)4 Table (org.apache.hadoop.hive.ql.metadata.Table)4 HashMap (java.util.HashMap)3 HiveException (org.apache.hadoop.hive.ql.metadata.HiveException)3 IOException (java.io.IOException)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 ValidTxnWriteIdList (org.apache.hadoop.hive.common.ValidTxnWriteIdList)2 TableType (org.apache.hadoop.hive.metastore.TableType)2 Warehouse (org.apache.hadoop.hive.metastore.Warehouse)2 Materialization (org.apache.hadoop.hive.metastore.api.Materialization)2 MTable (org.apache.hadoop.hive.metastore.model.MTable)2 MVersionTable (org.apache.hadoop.hive.metastore.model.MVersionTable)2