Search in sources :

Example 1 with SearchedCaseExpression

use of org.teiid.query.sql.symbol.SearchedCaseExpression in project teiid by teiid.

the class TestSearchedCaseExpressionImpl method testGetElseExpression.

public void testGetElseExpression() throws Exception {
    assertNotNull(example().getElseExpression());
    SearchedCaseExpression expr = helpExample();
    expr.setElseExpression(null);
    assertNull(TstLanguageBridgeFactory.factory.translate(expr).getElseExpression());
}
Also used : SearchedCaseExpression(org.teiid.query.sql.symbol.SearchedCaseExpression)

Example 2 with SearchedCaseExpression

use of org.teiid.query.sql.symbol.SearchedCaseExpression in project teiid by teiid.

the class TestSearchedCaseExpressionImpl method helpExample.

public static SearchedCaseExpression helpExample() {
    SearchedCaseExpression caseExpr = new SearchedCaseExpression(getWhenCriteria(3), TestCaseExpression.getThenExpressions(3));
    caseExpr.setElseExpression(new Constant(new Integer(9999)));
    return caseExpr;
}
Also used : SearchedCaseExpression(org.teiid.query.sql.symbol.SearchedCaseExpression) Constant(org.teiid.query.sql.symbol.Constant)

Example 3 with SearchedCaseExpression

use of org.teiid.query.sql.symbol.SearchedCaseExpression in project teiid by teiid.

the class LanguageBridgeFactory method translate.

org.teiid.language.Expression translate(Function function) {
    Expression[] args = function.getArgs();
    List<org.teiid.language.Expression> params = new ArrayList<org.teiid.language.Expression>(args.length);
    for (int i = 0; i < args.length; i++) {
        params.add(translate(args[i]));
    }
    String name = function.getName();
    if (function.getFunctionDescriptor() != null) {
        name = function.getFunctionDescriptor().getName();
        if (!supportsConcat2 && function.getFunctionDescriptor().getMethod().getParent() == null && name.equalsIgnoreCase(SourceSystemFunctions.CONCAT2)) {
            Expression[] newArgs = new Expression[args.length];
            boolean useCase = true;
            for (int i = 0; i < args.length; i++) {
                if (args[i] instanceof Constant) {
                    newArgs[i] = args[i];
                    useCase = false;
                } else {
                    // $NON-NLS-1$
                    Function f = new Function(SourceSystemFunctions.IFNULL, new Expression[] { args[i], new Constant("") });
                    newArgs[i] = f;
                    f.setType(args[i].getType());
                    FunctionDescriptor descriptor = metadataFactory.getMetadata().getFunctionLibrary().findFunction(SourceSystemFunctions.IFNULL, new Class[] { args[i].getType(), DataTypeManager.DefaultDataClasses.STRING });
                    f.setFunctionDescriptor(descriptor);
                }
            }
            Function concat = new Function(SourceSystemFunctions.CONCAT, newArgs);
            concat.setType(DataTypeManager.DefaultDataClasses.STRING);
            if (!useCase) {
                return translate(concat);
            }
            FunctionDescriptor descriptor = metadataFactory.getMetadata().getFunctionLibrary().findFunction(SourceSystemFunctions.CONCAT, new Class[] { DataTypeManager.DefaultDataClasses.STRING, DataTypeManager.DefaultDataClasses.STRING });
            concat.setFunctionDescriptor(descriptor);
            List<CompoundCriteria> when = Arrays.asList(new CompoundCriteria(CompoundCriteria.AND, new IsNullCriteria(args[0]), new IsNullCriteria(args[1])));
            Constant nullConstant = new Constant(null, DataTypeManager.DefaultDataClasses.STRING);
            List<Constant> then = Arrays.asList(nullConstant);
            SearchedCaseExpression caseExpr = new SearchedCaseExpression(when, then);
            caseExpr.setElseExpression(concat);
            caseExpr.setType(DataTypeManager.DefaultDataClasses.STRING);
            return translate(caseExpr);
        }
        // check for translator pushdown functions, and use the name in source if possible
        if (function.getFunctionDescriptor().getMethod().getNameInSource() != null && (CoreConstants.SYSTEM_MODEL.equals(function.getFunctionDescriptor().getSchema()) || (function.getFunctionDescriptor().getMethod().getParent() != null && function.getFunctionDescriptor().getMethod().getParent().isPhysical()))) {
            name = function.getFunctionDescriptor().getMethod().getNameInSource();
        }
    } else {
        name = Symbol.getShortName(name);
    }
    // if there is any ambiguity in the function name it will be up to the translator logic to check the
    // metadata
    org.teiid.language.Function result = new org.teiid.language.Function(name, params, function.getType());
    if (function.getFunctionDescriptor() != null) {
        result.setMetadataObject(function.getFunctionDescriptor().getMethod());
    }
    return result;
}
Also used : Constant(org.teiid.query.sql.symbol.Constant) org.teiid.language(org.teiid.language) FunctionDescriptor(org.teiid.query.function.FunctionDescriptor) WindowFunction(org.teiid.query.sql.symbol.WindowFunction) Function(org.teiid.query.sql.symbol.Function) SearchedCaseExpression(org.teiid.query.sql.symbol.SearchedCaseExpression) SearchedCaseExpression(org.teiid.query.sql.symbol.SearchedCaseExpression) Expression(org.teiid.query.sql.symbol.Expression)

Example 4 with SearchedCaseExpression

use of org.teiid.query.sql.symbol.SearchedCaseExpression in project teiid by teiid.

the class RulePushLimit method getMinValue.

/**
 * @param limitNode
 * @param child
 * @throws TeiidComponentException
 * @throws BlockedException
 * @throws ExpressionEvaluationException
 */
static Expression getMinValue(Expression expr1, Expression expr2) {
    if (expr1 == null) {
        return expr2;
    }
    if (expr2 == null) {
        return expr1;
    }
    Criteria crit = new CompareCriteria(expr1, CompareCriteria.LT, expr2);
    SearchedCaseExpression sce = new SearchedCaseExpression(Arrays.asList(new Object[] { crit }), Arrays.asList(new Object[] { expr1 }));
    sce.setElseExpression(expr2);
    sce.setType(expr1.getType());
    return evaluateIfPossible(sce);
}
Also used : SearchedCaseExpression(org.teiid.query.sql.symbol.SearchedCaseExpression) Criteria(org.teiid.query.sql.lang.Criteria) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria)

Example 5 with SearchedCaseExpression

use of org.teiid.query.sql.symbol.SearchedCaseExpression 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)

Aggregations

SearchedCaseExpression (org.teiid.query.sql.symbol.SearchedCaseExpression)6 Constant (org.teiid.query.sql.symbol.Constant)3 Criteria (org.teiid.query.sql.lang.Criteria)2 Expression (org.teiid.query.sql.symbol.Expression)2 Function (org.teiid.query.sql.symbol.Function)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 DataPolicy (org.teiid.adminapi.DataPolicy)1 DataPolicyMetadata (org.teiid.adminapi.impl.DataPolicyMetadata)1 PermissionMetaData (org.teiid.adminapi.impl.DataPolicyMetadata.PermissionMetaData)1 QueryMetadataException (org.teiid.api.exception.query.QueryMetadataException)1 TeiidException (org.teiid.core.TeiidException)1 org.teiid.language (org.teiid.language)1 FunctionDescriptor (org.teiid.query.function.FunctionDescriptor)1 CompareCriteria (org.teiid.query.sql.lang.CompareCriteria)1 SubqueryContainer (org.teiid.query.sql.lang.SubqueryContainer)1 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)1 ScalarSubquery (org.teiid.query.sql.symbol.ScalarSubquery)1 WindowFunction (org.teiid.query.sql.symbol.WindowFunction)1 ValidationVisitor (org.teiid.query.validator.ValidationVisitor)1