Search in sources :

Example 1 with ValidationVisitor

use of org.teiid.query.validator.ValidationVisitor in project teiid by teiid.

the class QueryResolver method resolveView.

public static QueryNode resolveView(GroupSymbol virtualGroup, QueryNode qnode, String cacheString, QueryMetadataInterface qmi, boolean logValidation) throws TeiidComponentException, QueryMetadataException, QueryResolverException, QueryValidatorException {
    qmi = qmi.getDesignTimeMetadata();
    // $NON-NLS-1$
    cacheString = "transformation/" + cacheString;
    QueryNode cachedNode = (QueryNode) qmi.getFromMetadataCache(virtualGroup.getMetadataID(), cacheString);
    if (cachedNode == null) {
        Command result = qnode.getCommand();
        List<String> bindings = null;
        if (result == null) {
            try {
                result = QueryParser.getQueryParser().parseCommand(qnode.getQuery());
            } catch (QueryParserException e) {
                throw new QueryResolverException(QueryPlugin.Event.TEIID30065, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30065, virtualGroup));
            }
            bindings = qnode.getBindings();
        } else {
            result = (Command) result.clone();
        }
        if (bindings != null && !bindings.isEmpty()) {
            QueryResolver.resolveWithBindingMetadata(result, qmi, qnode, true);
        } else {
            QueryResolver.resolveCommand(result, qmi, false);
        }
        Request.validateWithVisitor(new ValidationVisitor(), qmi, result);
        validateProjectedSymbols(virtualGroup, qmi, result);
        cachedNode = new QueryNode(qnode.getQuery());
        cachedNode.setCommand(result);
        if (isView(virtualGroup, qmi)) {
            String updatePlan = qmi.getUpdatePlan(virtualGroup.getMetadataID());
            String deletePlan = qmi.getDeletePlan(virtualGroup.getMetadataID());
            String insertPlan = qmi.getInsertPlan(virtualGroup.getMetadataID());
            // the elements must be against the view and not the alias
            if (virtualGroup.getDefinition() != null) {
                GroupSymbol group = new GroupSymbol(virtualGroup.getNonCorrelationName());
                group.setMetadataID(virtualGroup.getMetadataID());
                virtualGroup = group;
            }
            List<ElementSymbol> elements = ResolverUtil.resolveElementsInGroup(virtualGroup, qmi);
            UpdateValidator validator = new UpdateValidator(qmi, determineType(insertPlan), determineType(updatePlan), determineType(deletePlan));
            validator.validate(result, elements);
            UpdateInfo info = validator.getUpdateInfo();
            if (logValidation && qmi.groupSupports(virtualGroup.getMetadataID(), SupportConstants.Group.UPDATE)) {
                if (info.isInherentInsert() && validator.getInsertReport().hasItems()) {
                    LogManager.logDetail(LogConstants.CTX_QUERY_RESOLVER, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31173, validator.getInsertReport().getFailureMessage(), SQLConstants.Reserved.INSERT, qmi.getFullName(virtualGroup.getMetadataID())));
                }
                if (info.isInherentUpdate() && validator.getUpdateReport().hasItems()) {
                    LogManager.logDetail(LogConstants.CTX_QUERY_RESOLVER, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31173, validator.getUpdateReport().getFailureMessage(), SQLConstants.Reserved.UPDATE, qmi.getFullName(virtualGroup.getMetadataID())));
                }
                if (info.isInherentDelete() && validator.getDeleteReport().hasItems()) {
                    LogManager.logDetail(LogConstants.CTX_QUERY_RESOLVER, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31173, validator.getDeleteReport().getFailureMessage(), SQLConstants.Reserved.DELETE, qmi.getFullName(virtualGroup.getMetadataID())));
                }
            }
            cachedNode.setUpdateInfo(info);
        }
        qmi.addToMetadataCache(virtualGroup.getMetadataID(), cacheString, cachedNode);
    }
    return cachedNode;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) ValidationVisitor(org.teiid.query.validator.ValidationVisitor) QueryParserException(org.teiid.api.exception.query.QueryParserException) Command(org.teiid.query.sql.lang.Command) QueryNode(org.teiid.query.mapping.relational.QueryNode) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) QueryResolverException(org.teiid.api.exception.query.QueryResolverException) UpdateValidator(org.teiid.query.validator.UpdateValidator) UpdateInfo(org.teiid.query.validator.UpdateValidator.UpdateInfo)

Example 2 with ValidationVisitor

use of org.teiid.query.validator.ValidationVisitor in project teiid by teiid.

the class RowBasedSecurityHelper method resolveCondition.

static Criteria resolveCondition(QueryMetadataInterface metadata, final GroupSymbol group, String fullName, Map.Entry<String, DataPolicy> entry, PermissionMetaData pmd, String filterString) throws QueryMetadataException {
    Criteria filter = (Criteria) pmd.getResolvedCondition();
    if (filter == null) {
        try {
            filter = QueryParser.getQueryParser().parseCriteria(filterString);
            GroupSymbol gs = group;
            if (group.getDefinition() != null) {
                gs = new GroupSymbol(fullName);
                gs.setMetadataID(group.getMetadataID());
            }
            Collection<GroupSymbol> groups = Arrays.asList(gs);
            for (SubqueryContainer container : ValueIteratorProviderCollectorVisitor.getValueIteratorProviders(filter)) {
                container.getCommand().pushNewResolvingContext(groups);
                QueryResolver.resolveCommand(container.getCommand(), metadata, false);
            }
            ResolverVisitor.resolveLanguageObject(filter, groups, metadata);
            ValidatorReport report = Validator.validate(filter, metadata, new ValidationVisitor());
            if (report.hasItems()) {
                ValidatorFailure firstFailure = report.getItems().iterator().next();
                // $NON-NLS-1$
                throw new QueryMetadataException(QueryPlugin.Event.TEIID31129, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31129, entry.getKey(), fullName) + " " + firstFailure);
            }
            pmd.setResolvedCondition(filter.clone());
        } catch (QueryMetadataException e) {
            throw e;
        } catch (TeiidException e) {
            throw new QueryMetadataException(QueryPlugin.Event.TEIID31129, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31129, entry.getKey(), fullName));
        }
    } else {
        filter = (Criteria) filter.clone();
    }
    return filter;
}
Also used : ValidationVisitor(org.teiid.query.validator.ValidationVisitor) ValidatorFailure(org.teiid.query.validator.ValidatorFailure) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) QueryMetadataException(org.teiid.api.exception.query.QueryMetadataException) ValidatorReport(org.teiid.query.validator.ValidatorReport) TeiidException(org.teiid.core.TeiidException)

Example 3 with ValidationVisitor

use of org.teiid.query.validator.ValidationVisitor in project teiid by teiid.

the class ColumnMaskingHelper method maskColumn.

private static Expression maskColumn(ElementSymbol col, GroupSymbol unaliased, QueryMetadataInterface metadata, ExpressionMappingVisitor emv, Map<String, DataPolicy> policies, CommandContext cc) throws TeiidComponentException, TeiidProcessingException {
    Object metadataID = col.getMetadataID();
    String fullName = metadata.getFullName(metadataID);
    final GroupSymbol group = col.getGroupSymbol();
    String elementType = metadata.getElementRuntimeTypeName(col.getMetadataID());
    Class<?> expectedType = DataTypeManager.getDataTypeClass(elementType);
    List<WhenThen> cases = null;
    Collection<GroupSymbol> groups = Arrays.asList(unaliased);
    for (Map.Entry<String, DataPolicy> entry : policies.entrySet()) {
        DataPolicyMetadata dpm = (DataPolicyMetadata) entry.getValue();
        PermissionMetaData pmd = dpm.getPermissionMap().get(fullName);
        if (pmd == null) {
            continue;
        }
        String maskString = pmd.getMask();
        if (maskString == null) {
            continue;
        }
        Criteria condition = null;
        if (pmd.getCondition() != null) {
            condition = RowBasedSecurityHelper.resolveCondition(metadata, group, metadata.getFullName(group.getMetadataID()), entry, pmd, pmd.getCondition());
        } else {
            condition = QueryRewriter.TRUE_CRITERIA;
        }
        Expression mask = (Expression) pmd.getResolvedMask();
        if (mask == null) {
            try {
                mask = QueryParser.getQueryParser().parseExpression(pmd.getMask());
                for (SubqueryContainer container : ValueIteratorProviderCollectorVisitor.getValueIteratorProviders(mask)) {
                    container.getCommand().pushNewResolvingContext(groups);
                    QueryResolver.resolveCommand(container.getCommand(), metadata, false);
                }
                ResolverVisitor.resolveLanguageObject(mask, groups, metadata);
                ValidatorReport report = Validator.validate(mask, metadata, new ValidationVisitor());
                if (report.hasItems()) {
                    ValidatorFailure firstFailure = report.getItems().iterator().next();
                    // $NON-NLS-1$
                    throw new QueryMetadataException(QueryPlugin.Event.TEIID31139, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31139, dpm.getName(), fullName) + " " + firstFailure);
                }
                if (mask.getType() != expectedType) {
                    mask = ResolverUtil.convertExpression(mask, elementType, metadata);
                }
                pmd.setResolvedMask(mask.clone());
                if (!dpm.isAnyAuthenticated()) {
                    // we treat this as user deterministic since the data roles won't change.  this may change if the logic becomes dynamic
                    // TODO: this condition may not even be used
                    cc.setDeterminismLevel(Determinism.USER_DETERMINISTIC);
                }
            } catch (QueryMetadataException e) {
                throw e;
            } catch (TeiidException e) {
                throw new QueryMetadataException(QueryPlugin.Event.TEIID31129, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31129, dpm.getName(), fullName));
            }
        } else {
            mask = (Expression) mask.clone();
        }
        if (group.getDefinition() != null) {
            PreOrPostOrderNavigator.doVisit(mask, emv, PreOrPostOrderNavigator.PRE_ORDER, true);
        }
        if (cases == null) {
            cases = new ArrayList<ColumnMaskingHelper.WhenThen>();
        }
        cases.add(new WhenThen(pmd.getOrder(), condition, mask));
    }
    if (cases == null) {
        return col;
    }
    Collections.sort(cases);
    List<Criteria> whens = new ArrayList<Criteria>();
    List<Expression> thens = new ArrayList<Expression>();
    for (WhenThen whenThen : cases) {
        whens.add(whenThen.when);
        thens.add(whenThen.then);
    }
    SearchedCaseExpression sce = new SearchedCaseExpression(whens, thens);
    sce.setElseExpression(col);
    sce.setType(expectedType);
    Expression mask = QueryRewriter.rewriteExpression(sce, cc, metadata, true);
    return mask;
}
Also used : ValidationVisitor(org.teiid.query.validator.ValidationVisitor) ArrayList(java.util.ArrayList) Criteria(org.teiid.query.sql.lang.Criteria) ValidatorFailure(org.teiid.query.validator.ValidatorFailure) DataPolicyMetadata(org.teiid.adminapi.impl.DataPolicyMetadata) DataPolicy(org.teiid.adminapi.DataPolicy) SubqueryContainer(org.teiid.query.sql.lang.SubqueryContainer) QueryMetadataException(org.teiid.api.exception.query.QueryMetadataException) ValidatorReport(org.teiid.query.validator.ValidatorReport) TeiidException(org.teiid.core.TeiidException) SearchedCaseExpression(org.teiid.query.sql.symbol.SearchedCaseExpression) SearchedCaseExpression(org.teiid.query.sql.symbol.SearchedCaseExpression) Expression(org.teiid.query.sql.symbol.Expression) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) Map(java.util.Map) PermissionMetaData(org.teiid.adminapi.impl.DataPolicyMetadata.PermissionMetaData)

Example 4 with ValidationVisitor

use of org.teiid.query.validator.ValidationVisitor in project teiid by teiid.

the class QueryProcessorFactoryImpl method getPreparedPlan.

@Override
public PreparedPlan getPreparedPlan(String query, String recursionGroup, CommandContext commandContext, QueryMetadataInterface metadata) throws TeiidComponentException, TeiidProcessingException {
    if (recursionGroup != null) {
        commandContext.pushCall(recursionGroup);
    }
    PreparedPlan pp = commandContext.getPlan(query);
    if (pp == null) {
        ParseInfo parseInfo = new ParseInfo();
        Command newCommand = QueryParser.getQueryParser().parseCommand(query, parseInfo);
        QueryResolver.resolveCommand(newCommand, metadata);
        List<Reference> references = ReferenceCollectorVisitor.getReferences(newCommand);
        AbstractValidationVisitor visitor = new ValidationVisitor();
        Request.validateWithVisitor(visitor, metadata, newCommand);
        newCommand = QueryRewriter.rewrite(newCommand, metadata, commandContext);
        AnalysisRecord record = new AnalysisRecord(false, false);
        ProcessorPlan plan = QueryOptimizer.optimizePlan(newCommand, metadata, idGenerator, finder, record, commandContext);
        pp = new PreparedPlan();
        pp.setPlan(plan, commandContext);
        pp.setReferences(references);
        pp.setAnalysisRecord(record);
        pp.setCommand(newCommand);
        commandContext.putPlan(query, pp, commandContext.getDeterminismLevel());
    }
    return pp;
}
Also used : AbstractValidationVisitor(org.teiid.query.validator.AbstractValidationVisitor) ValidationVisitor(org.teiid.query.validator.ValidationVisitor) AnalysisRecord(org.teiid.query.analysis.AnalysisRecord) Command(org.teiid.query.sql.lang.Command) Reference(org.teiid.query.sql.symbol.Reference) ParseInfo(org.teiid.query.parser.ParseInfo) AbstractValidationVisitor(org.teiid.query.validator.AbstractValidationVisitor) ProcessorPlan(org.teiid.query.processor.ProcessorPlan)

Example 5 with ValidationVisitor

use of org.teiid.query.validator.ValidationVisitor in project teiid by teiid.

the class Request method validateQuery.

private void validateQuery(Command command) throws QueryValidatorException, TeiidComponentException {
    // Create generic sql validation visitor
    AbstractValidationVisitor visitor = new ValidationVisitor();
    validateWithVisitor(visitor, metadata, command);
}
Also used : AbstractValidationVisitor(org.teiid.query.validator.AbstractValidationVisitor) ValidationVisitor(org.teiid.query.validator.ValidationVisitor) AbstractValidationVisitor(org.teiid.query.validator.AbstractValidationVisitor)

Aggregations

ValidationVisitor (org.teiid.query.validator.ValidationVisitor)7 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)4 ProcessorPlan (org.teiid.query.processor.ProcessorPlan)3 Command (org.teiid.query.sql.lang.Command)3 QueryMetadataException (org.teiid.api.exception.query.QueryMetadataException)2 TeiidException (org.teiid.core.TeiidException)2 CreateProcedureCommand (org.teiid.query.sql.proc.CreateProcedureCommand)2 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)2 Expression (org.teiid.query.sql.symbol.Expression)2 AbstractValidationVisitor (org.teiid.query.validator.AbstractValidationVisitor)2 ValidatorFailure (org.teiid.query.validator.ValidatorFailure)2 ValidatorReport (org.teiid.query.validator.ValidatorReport)2 Clob (java.sql.Clob)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 DataPolicy (org.teiid.adminapi.DataPolicy)1 DataPolicyMetadata (org.teiid.adminapi.impl.DataPolicyMetadata)1