Search in sources :

Example 51 with Constant

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

the class TestMatchCriteria method example.

// ################################## TEST HELPERS ################################
public static MatchCriteria example(String element, String str) {
    MatchCriteria crit = new MatchCriteria();
    crit.setLeftExpression(new ElementSymbol(element));
    crit.setRightExpression(new Constant(str));
    return crit;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Constant(org.teiid.query.sql.symbol.Constant)

Example 52 with Constant

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

the class TestMatchCriteria method example.

public static MatchCriteria example(String str, char escapeChar) {
    MatchCriteria crit = new MatchCriteria();
    // $NON-NLS-1$
    crit.setLeftExpression(new ElementSymbol("m.g1.e1"));
    crit.setRightExpression(new Constant(str));
    crit.setEscapeChar(escapeChar);
    return crit;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Constant(org.teiid.query.sql.symbol.Constant)

Example 53 with Constant

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

the class DataTierManagerImpl method processSystemQuery.

/**
 * @param command
 * @param workItem
 * @return
 * @throws TeiidComponentException
 * @throws TeiidProcessingException
 */
private TupleSource processSystemQuery(CommandContext context, Command command, DQPWorkContext workContext) throws TeiidComponentException, TeiidProcessingException {
    String vdbName = workContext.getVdbName();
    String vdbVersion = workContext.getVdbVersion();
    final VDBMetaData vdb = workContext.getVDB();
    TransformationMetadata indexMetadata = vdb.getAttachment(TransformationMetadata.class);
    CompositeMetadataStore metadata = indexMetadata.getMetadataStore();
    if (command instanceof Query) {
        Query query = (Query) command;
        UnaryFromClause ufc = (UnaryFromClause) query.getFrom().getClauses().get(0);
        GroupSymbol group = ufc.getGroup();
        if (StringUtil.startsWithIgnoreCase(group.getNonCorrelationName(), CoreConstants.SYSTEM_ADMIN_MODEL)) {
            final SystemAdminTables sysTable = SystemAdminTables.valueOf(group.getNonCorrelationName().substring(CoreConstants.SYSTEM_ADMIN_MODEL.length() + 1).toUpperCase());
            BaseExtractionTable<?> et = systemAdminTables.get(sysTable);
            return et.processQuery(query, vdb, indexMetadata, context);
        }
        final SystemTables sysTable = SystemTables.valueOf(group.getNonCorrelationName().substring(CoreConstants.SYSTEM_MODEL.length() + 1).toUpperCase());
        BaseExtractionTable<?> et = systemTables.get(sysTable);
        return et.processQuery(query, vdb, indexMetadata, context);
    }
    Collection<List<?>> rows = new ArrayList<List<?>>();
    StoredProcedure proc = (StoredProcedure) command;
    if (StringUtil.startsWithIgnoreCase(proc.getProcedureCallableName(), CoreConstants.SYSTEM_ADMIN_MODEL)) {
        final SystemAdminProcs sysProc = SystemAdminProcs.valueOf(proc.getProcedureCallableName().substring(CoreConstants.SYSTEM_ADMIN_MODEL.length() + 1).toUpperCase());
        switch(sysProc) {
            case LOGMSG:
            case ISLOGGABLE:
                String level = (String) ((Constant) proc.getParameter(2).getExpression()).getValue();
                String logContext = (String) ((Constant) proc.getParameter(3).getExpression()).getValue();
                Object message = null;
                if (sysProc == SystemAdminProcs.LOGMSG) {
                    message = ((Constant) proc.getParameter(4).getExpression()).getValue();
                }
                int msgLevel = getLevel(level);
                boolean logged = false;
                if (LogManager.isMessageToBeRecorded(logContext, msgLevel)) {
                    if (message != null) {
                        LogManager.log(msgLevel, logContext, message);
                    }
                    logged = true;
                }
                if (proc.returnParameters()) {
                    rows.add(Arrays.asList(logged));
                }
                return new CollectionTupleSource(rows.iterator());
            case SETPROPERTY:
                try {
                    String uuid = (String) ((Constant) proc.getParameter(2).getExpression()).getValue();
                    String key = (String) ((Constant) proc.getParameter(3).getExpression()).getValue();
                    Clob value = (Clob) ((Constant) proc.getParameter(4).getExpression()).getValue();
                    key = MetadataFactory.resolvePropertyKey(null, key);
                    String strVal = null;
                    String result = null;
                    if (value != null) {
                        if (value.length() > MAX_VALUE_LENGTH) {
                            throw new TeiidProcessingException(QueryPlugin.Event.TEIID30548, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30548, MAX_VALUE_LENGTH));
                        }
                        strVal = ObjectConverterUtil.convertToString(value.getCharacterStream());
                    }
                    final AbstractMetadataRecord target = getByUuid(metadata, uuid);
                    if (target == null) {
                        throw new TeiidProcessingException(QueryPlugin.Event.TEIID30549, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30549, uuid));
                    }
                    AbstractMetadataRecord schema = target;
                    while (!(schema instanceof Schema) && schema.getParent() != null) {
                        schema = schema.getParent();
                    }
                    if (schema instanceof Schema && vdb.getImportedModels().contains(((Schema) schema).getName())) {
                        // $NON-NLS-1$
                        throw new TeiidProcessingException(QueryPlugin.Event.TEIID31098, QueryPlugin.Util.getString("ValidationVisitor.invalid_alter", uuid));
                    }
                    if (getMetadataRepository(target, vdb) != null) {
                        getMetadataRepository(target, vdb).setProperty(vdbName, vdbVersion, target, key, strVal);
                    }
                    result = DdlPlan.setProperty(vdb, target, key, strVal);
                    if (eventDistributor != null) {
                        eventDistributor.setProperty(vdbName, vdbVersion, uuid, key, strVal);
                    }
                    // materialization depends upon the property values
                    // $NON-NLS-1$
                    indexMetadata.addToMetadataCache(target, "transformation/matview", null);
                    if (proc.returnParameters()) {
                        if (result == null) {
                            rows.add(Arrays.asList((Clob) null));
                        } else {
                            rows.add(Arrays.asList(new ClobType(new ClobImpl(result))));
                        }
                    }
                    return new CollectionTupleSource(rows.iterator());
                } catch (SQLException e) {
                    throw new TeiidProcessingException(QueryPlugin.Event.TEIID30550, e);
                } catch (IOException e) {
                    throw new TeiidProcessingException(QueryPlugin.Event.TEIID30551, e);
                }
        }
        final Table table = indexMetadata.getGroupID((String) ((Constant) proc.getParameter(1).getExpression()).getValue());
        switch(sysProc) {
            case SETCOLUMNSTATS:
                final String columnName = (String) ((Constant) proc.getParameter(2).getExpression()).getValue();
                Column c = null;
                for (Column col : table.getColumns()) {
                    if (col.getName().equalsIgnoreCase(columnName)) {
                        c = col;
                        break;
                    }
                }
                if (c == null) {
                    throw new TeiidProcessingException(QueryPlugin.Event.TEIID30552, columnName + TransformationMetadata.NOT_EXISTS_MESSAGE);
                }
                Number distinctVals = (Number) ((Constant) proc.getParameter(3).getExpression()).getValue();
                Number nullVals = (Number) ((Constant) proc.getParameter(4).getExpression()).getValue();
                String max = (String) ((Constant) proc.getParameter(5).getExpression()).getValue();
                String min = (String) ((Constant) proc.getParameter(6).getExpression()).getValue();
                final ColumnStats columnStats = new ColumnStats();
                columnStats.setDistinctValues(distinctVals);
                columnStats.setNullValues(nullVals);
                columnStats.setMaximumValue(max);
                columnStats.setMinimumValue(min);
                if (getMetadataRepository(table, vdb) != null) {
                    getMetadataRepository(table, vdb).setColumnStats(vdbName, vdbVersion, c, columnStats);
                }
                DdlPlan.setColumnStats(vdb, c, columnStats);
                if (eventDistributor != null) {
                    eventDistributor.setColumnStats(vdbName, vdbVersion, table.getParent().getName(), table.getName(), columnName, columnStats);
                }
                break;
            case SETTABLESTATS:
                Constant val = (Constant) proc.getParameter(2).getExpression();
                final Number cardinality = (Number) val.getValue();
                TableStats tableStats = new TableStats();
                tableStats.setCardinality(cardinality);
                if (getMetadataRepository(table, vdb) != null) {
                    getMetadataRepository(table, vdb).setTableStats(vdbName, vdbVersion, table, tableStats);
                }
                DdlPlan.setTableStats(vdb, table, tableStats);
                if (eventDistributor != null) {
                    eventDistributor.setTableStats(vdbName, vdbVersion, table.getParent().getName(), table.getName(), tableStats);
                }
                break;
        }
        return new CollectionTupleSource(rows.iterator());
    }
    final SystemProcs sysTable = SystemProcs.valueOf(proc.getProcedureCallableName().substring(CoreConstants.SYSTEM_MODEL.length() + 1).toUpperCase());
    switch(sysTable) {
        case GETXMLSCHEMAS:
            try {
                Object groupID = indexMetadata.getGroupID((String) ((Constant) proc.getParameter(1).getExpression()).getValue());
                List<SQLXMLImpl> schemas = indexMetadata.getXMLSchemas(groupID);
                for (SQLXMLImpl schema : schemas) {
                    rows.add(Arrays.asList(new XMLType(schema)));
                }
            } catch (QueryMetadataException e) {
                throw new TeiidProcessingException(QueryPlugin.Event.TEIID30553, e);
            }
            break;
        case ARRAYITERATE:
            Object array = ((Constant) proc.getParameter(1).getExpression()).getValue();
            if (array != null) {
                final Object[] vals;
                if (array instanceof Object[]) {
                    vals = (Object[]) array;
                } else {
                    ArrayImpl arrayImpl = (ArrayImpl) array;
                    vals = arrayImpl.getValues();
                }
                return new CollectionTupleSource(new Iterator<List<?>>() {

                    int index = 0;

                    @Override
                    public boolean hasNext() {
                        return index < vals.length;
                    }

                    @Override
                    public List<?> next() {
                        if (!hasNext()) {
                            throw new NoSuchElementException();
                        }
                        return Arrays.asList(vals[index++]);
                    }

                    @Override
                    public void remove() {
                        throw new UnsupportedOperationException();
                    }
                });
            }
    }
    return new CollectionTupleSource(rows.iterator());
}
Also used : Query(org.teiid.query.sql.lang.Query) UnaryFromClause(org.teiid.query.sql.lang.UnaryFromClause) SQLException(java.sql.SQLException) Constant(org.teiid.query.sql.symbol.Constant) ArrayImpl(org.teiid.core.types.ArrayImpl) ArrayList(java.util.ArrayList) CollectionTupleSource(org.teiid.query.processor.CollectionTupleSource) TeiidProcessingException(org.teiid.core.TeiidProcessingException) ArrayList(java.util.ArrayList) List(java.util.List) ClobImpl(org.teiid.core.types.ClobImpl) TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) SQLXMLImpl(org.teiid.core.types.SQLXMLImpl) IOException(java.io.IOException) QueryMetadataException(org.teiid.api.exception.query.QueryMetadataException) ClobType(org.teiid.core.types.ClobType) XMLType(org.teiid.core.types.XMLType) CompositeMetadataStore(org.teiid.query.metadata.CompositeMetadataStore) StoredProcedure(org.teiid.query.sql.lang.StoredProcedure) VDBMetaData(org.teiid.adminapi.impl.VDBMetaData) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) Clob(java.sql.Clob) NoSuchElementException(java.util.NoSuchElementException)

Example 54 with Constant

use of org.teiid.query.sql.symbol.Constant 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 55 with Constant

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

the class TestCriteriaEvaluator method helpTestMatch.

// ################################## TEST HELPERS ################################
private void helpTestMatch(String value, String pattern, char escape, boolean negated, boolean expectedMatch) throws ExpressionEvaluationException, BlockedException, TeiidComponentException {
    MatchCriteria crit = new MatchCriteria(new Constant(value), new Constant(pattern), escape);
    crit.setNegated(negated);
    boolean actualMatch = Evaluator.evaluate(crit);
    // Compare actual and expected match
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    assertEquals("Match criteria test failed for value=[" + value + "], pattern=[" + pattern + "], hasEscape=" + (escape != MatchCriteria.NULL_ESCAPE_CHAR) + ": ", expectedMatch, actualMatch);
}
Also used : Constant(org.teiid.query.sql.symbol.Constant)

Aggregations

Constant (org.teiid.query.sql.symbol.Constant)203 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)94 Test (org.junit.Test)88 ArrayList (java.util.ArrayList)61 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)48 List (java.util.List)38 Expression (org.teiid.query.sql.symbol.Expression)38 Function (org.teiid.query.sql.symbol.Function)31 CompareCriteria (org.teiid.query.sql.lang.CompareCriteria)25 Query (org.teiid.query.sql.lang.Query)22 Select (org.teiid.query.sql.lang.Select)15 Reference (org.teiid.query.sql.symbol.Reference)14 From (org.teiid.query.sql.lang.From)12 HashMap (java.util.HashMap)11 FunctionDescriptor (org.teiid.query.function.FunctionDescriptor)11 Criteria (org.teiid.query.sql.lang.Criteria)11 SetQuery (org.teiid.query.sql.lang.SetQuery)11 LinkedList (java.util.LinkedList)10 Limit (org.teiid.query.sql.lang.Limit)10 StoredProcedure (org.teiid.query.sql.lang.StoredProcedure)9