Search in sources :

Example 6 with QueryParserException

use of org.teiid.api.exception.query.QueryParserException in project teiid by teiid.

the class TestEnginePerformance method helpTestLike.

private void helpTestLike(int iterations, int threads) throws QueryParserException, InterruptedException, Exception {
    final Expression ex = QueryParser.getQueryParser().parseExpression("'abcdefg' like 'a%g'");
    runTask(iterations, threads, new Task() {

        @Override
        public Void call() throws Exception {
            Evaluator.evaluate(ex);
            return null;
        }
    });
}
Also used : Expression(org.teiid.query.sql.symbol.Expression) TeiidComponentException(org.teiid.core.TeiidComponentException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) TeiidException(org.teiid.core.TeiidException) BlockedException(org.teiid.common.buffer.BlockedException) QueryParserException(org.teiid.api.exception.query.QueryParserException) IOException(java.io.IOException)

Example 7 with QueryParserException

use of org.teiid.api.exception.query.QueryParserException in project teiid by teiid.

the class SourceTriggerActionPlanner method optimize.

@Override
public ProcessorPlan optimize(Command command, IDGenerator idGenerator, QueryMetadataInterface metadata, CapabilitiesFinder capFinder, AnalysisRecord analysisRecord, CommandContext context) throws QueryPlannerException, QueryMetadataException, TeiidComponentException {
    SourceEventCommand sec = (SourceEventCommand) command;
    Map<Expression, Integer> lookup = new HashMap<Expression, Integer>();
    Map<ElementSymbol, Expression> params = new HashMap<ElementSymbol, Expression>();
    List<Object> tuple = new ArrayList<Object>();
    Map<String, Integer> map = null;
    if (sec.getColumnNames() != null) {
        map = new TreeMap<String, Integer>(String.CASE_INSENSITIVE_ORDER);
        for (String name : sec.getColumnNames()) {
            map.put(name, map.size());
        }
    }
    GroupSymbol changingGroup = new GroupSymbol(ProcedureReservedWords.CHANGING);
    if (sec.newValues != null) {
        GroupSymbol newGroup = new GroupSymbol(SQLConstants.Reserved.NEW);
        newGroup.setMetadataID(sec.table);
        for (int i = 0; i < sec.getTable().getColumns().size(); i++) {
            Column c = sec.getTable().getColumns().get(i);
            Integer index = null;
            if (map != null) {
                index = map.get(c.getName());
            } else {
                index = i;
            }
            ElementSymbol newElement = new ElementSymbol(c.getName(), newGroup);
            newElement.setMetadataID(c);
            ElementSymbol changingElement = new ElementSymbol(c.getName(), changingGroup);
            lookup.put(newElement, tuple.size());
            lookup.put(changingElement, tuple.size() + 1);
            params.put(newElement, newElement);
            params.put(changingElement, changingElement);
            if (index == null) {
                // not changing
                tuple.add(new Constant(null));
                tuple.add(new Constant(Boolean.FALSE));
            } else {
                // changing
                tuple.add(new Constant(DataTypeManager.convertToRuntimeType(sec.newValues[index], true)));
                tuple.add(new Constant(Boolean.TRUE));
            }
        }
    }
    if (sec.oldValues != null) {
        GroupSymbol oldGroup = new GroupSymbol(SQLConstants.Reserved.OLD);
        oldGroup.setMetadataID(sec.table);
        for (int i = 0; i < sec.getTable().getColumns().size(); i++) {
            Column c = sec.getTable().getColumns().get(i);
            Integer index = null;
            if (map != null) {
                index = map.get(c.getName());
            } else {
                index = i;
            }
            ElementSymbol oldElement = new ElementSymbol(c.getName(), oldGroup);
            oldElement.setMetadataID(c);
            lookup.put(oldElement, tuple.size());
            params.put(oldElement, oldElement);
            if (index != null) {
                tuple.add(new Constant(DataTypeManager.convertToRuntimeType(sec.oldValues[index], true)));
            }
        }
    }
    List<ProcessorPlan> plans = new ArrayList<ProcessorPlan>();
    List<String> names = new ArrayList<String>();
    for (Trigger tr : sec.getTable().getTriggers().values()) {
        int updateType = Command.TYPE_UPDATE;
        switch(tr.getEvent()) {
            case DELETE:
                updateType = Command.TYPE_DELETE;
                if (sec.newValues != null) {
                    continue;
                }
                break;
            case INSERT:
                updateType = Command.TYPE_INSERT;
                if (sec.oldValues != null) {
                    continue;
                }
                break;
            case UPDATE:
                if (sec.oldValues == null || sec.newValues == null) {
                    continue;
                }
                break;
        }
        // create plan
        ForEachRowPlan result = new ForEachRowPlan();
        result.setSingleRow(true);
        result.setParams(params);
        TriggerAction parseProcedure;
        GroupSymbol gs = new GroupSymbol(sec.table.getFullName());
        try {
            parseProcedure = (TriggerAction) QueryParser.getQueryParser().parseProcedure(tr.getPlan(), true);
            QueryResolver.resolveCommand(parseProcedure, gs, updateType, metadata.getDesignTimeMetadata(), false);
        } catch (QueryParserException e) {
            // should have been validated
            throw new TeiidComponentException(e);
        } catch (QueryResolverException e) {
            // should have been validated
            throw new TeiidComponentException(e);
        }
        CreateProcedureCommand cpc = new CreateProcedureCommand(parseProcedure.getBlock());
        gs.setMetadataID(sec.table);
        cpc.setVirtualGroup(gs);
        cpc.setUpdateType(updateType);
        ProcedurePlan rowProcedure = (ProcedurePlan) QueryOptimizer.optimizePlan(cpc, metadata, idGenerator, capFinder, analysisRecord, context);
        rowProcedure.setRunInContext(false);
        result.setRowProcedure(rowProcedure);
        result.setLookupMap(lookup);
        result.setTupleSource(new CollectionTupleSource(Arrays.asList(tuple).iterator()));
        plans.add(result);
        names.add(tr.getName());
    }
    return new CompositeProcessorPlan(plans, names, sec.table);
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) QueryParserException(org.teiid.api.exception.query.QueryParserException) TriggerAction(org.teiid.query.sql.proc.TriggerAction) CreateProcedureCommand(org.teiid.query.sql.proc.CreateProcedureCommand) HashMap(java.util.HashMap) Constant(org.teiid.query.sql.symbol.Constant) ArrayList(java.util.ArrayList) CollectionTupleSource(org.teiid.query.processor.CollectionTupleSource) QueryResolverException(org.teiid.api.exception.query.QueryResolverException) Trigger(org.teiid.metadata.Trigger) Column(org.teiid.metadata.Column) ForEachRowPlan(org.teiid.query.processor.proc.ForEachRowPlan) Expression(org.teiid.query.sql.symbol.Expression) ProcedurePlan(org.teiid.query.processor.proc.ProcedurePlan) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) TeiidComponentException(org.teiid.core.TeiidComponentException) ProcessorPlan(org.teiid.query.processor.ProcessorPlan)

Example 8 with QueryParserException

use of org.teiid.api.exception.query.QueryParserException in project teiid by teiid.

the class MaterializationMetadataRepository method fixScript.

/**
 * Rather than require a script to be tokenized directly
 * we expect it to be wrapped in an anon block
 * @param property
 * @param table
 * @return
 */
private String fixScript(String property, Table table) {
    String script = table.getProperty(property, false);
    if (script == null) {
        return null;
    }
    if (!script.contains(";")) {
        // $NON-NLS-1$
        return script;
    }
    QueryParser parser = QueryParser.getQueryParser();
    try {
        parser.parseCommand(script);
        return script;
    } catch (QueryParserException e) {
    }
    // $NON-NLS-1$ //$NON-NLS-2$
    String wrapped = "begin " + script + "; end";
    try {
        parser.parseCommand(wrapped);
        table.setProperty(property, wrapped);
        return wrapped;
    } catch (QueryParserException e) {
    }
    // because we don't handle empty ; in scripts also try without
    // $NON-NLS-1$ //$NON-NLS-2$
    wrapped = "begin " + script + " end";
    try {
        parser.parseCommand(wrapped);
        table.setProperty(property, wrapped);
        return wrapped;
    } catch (QueryParserException e) {
    }
    // give up
    return script;
}
Also used : QueryParser(org.teiid.query.parser.QueryParser) QueryParserException(org.teiid.api.exception.query.QueryParserException)

Example 9 with QueryParserException

use of org.teiid.api.exception.query.QueryParserException in project teiid by teiid.

the class ResolverUtil method getDefault.

/**
 * Get the default value for the parameter, which could be null
 * if the parameter is set to NULLABLE.  If no default is available,
 * a QueryResolverException will be thrown.
 *
 * @param symbol ElementSymbol retrieved from metadata, fully-resolved
 * @param metadata QueryMetadataInterface
 * @return expr param (if it is non-null) or default value (if there is one)
 * or null Constant (if parameter is optional and therefore allows this)
 * @throws QueryResolverException if expr is null, parameter is required and no
 * default value is defined
 * @throws QueryMetadataException for error retrieving metadata
 * @throws TeiidComponentException
 * @throws QueryParserException
 * @since 4.3
 */
public static Expression getDefault(ElementSymbol symbol, QueryMetadataInterface metadata) throws TeiidComponentException, QueryMetadataException, QueryResolverException {
    // Check if there is a default value, if so use it
    Object mid = symbol.getMetadataID();
    Class<?> type = symbol.getType();
    String defaultValue = metadata.getDefaultValue(mid);
    boolean omit = false;
    String extensionProperty = metadata.getExtensionProperty(mid, BaseColumn.DEFAULT_HANDLING, false);
    if (BaseColumn.EXPRESSION_DEFAULT.equalsIgnoreCase(extensionProperty)) {
        Expression ex = null;
        try {
            ex = QueryParser.getQueryParser().parseExpression(defaultValue);
        } catch (QueryParserException e) {
            // TODO: also validate this at load time
            throw new QueryMetadataException(QueryPlugin.Event.TEIID31170, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31170, symbol));
        }
        List<SubqueryContainer<?>> subqueries = ValueIteratorProviderCollectorVisitor.getValueIteratorProviders(ex);
        ResolverVisitor.resolveLanguageObject(ex, metadata);
        for (SubqueryContainer<?> container : subqueries) {
            QueryResolver.resolveCommand(container.getCommand(), metadata);
        }
        return ResolverUtil.convertExpression(ex, DataTypeManager.getDataTypeName(type), metadata);
    } else if (BaseColumn.OMIT_DEFAULT.equalsIgnoreCase(extensionProperty)) {
        Object id = metadata.getGroupIDForElementID(symbol.getMetadataID());
        if (!metadata.isVirtualGroup(id)) {
            omit = true;
            // for physical procedures we just need a dummy value
            defaultValue = null;
        }
    }
    if (!omit && defaultValue == null && !metadata.elementSupports(mid, SupportConstants.Element.NULL)) {
        throw new QueryResolverException(QueryPlugin.Event.TEIID30089, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30089, symbol.getOutputName()));
    }
    return getProperlyTypedConstant(defaultValue, type);
}
Also used : QueryParserException(org.teiid.api.exception.query.QueryParserException) LanguageObject(org.teiid.query.sql.LanguageObject) QueryMetadataException(org.teiid.api.exception.query.QueryMetadataException) QueryResolverException(org.teiid.api.exception.query.QueryResolverException)

Example 10 with QueryParserException

use of org.teiid.api.exception.query.QueryParserException in project teiid by teiid.

the class ProcedureContainerResolver method expandCommand.

/**
 * Expand a command by finding and attaching all subcommands to the command.  If
 * some initial resolution must be done for this to be accomplished, that is ok,
 * but it should be kept to a minimum.
 * @param command The command to expand
 * @param useMetadataCommands True if resolver should use metadata commands to completely resolve
 * @param metadata Metadata access
 * @param analysis The analysis record that will be filled in if doing annotation.
 *
 * @throws QueryMetadataException If there is a metadata problem
 * @throws QueryResolverException If the query cannot be resolved
 * @throws TeiidComponentException If there is an internal error
 */
public Command expandCommand(ProcedureContainer procCommand, QueryMetadataInterface metadata, AnalysisRecord analysis) throws QueryMetadataException, QueryResolverException, TeiidComponentException {
    // Resolve group so we can tell whether it is an update procedure
    GroupSymbol group = procCommand.getGroup();
    Command subCommand = null;
    String plan = getPlan(metadata, procCommand);
    if (plan == null) {
        return null;
    }
    QueryParser parser = QueryParser.getQueryParser();
    try {
        subCommand = parser.parseProcedure(plan, !(procCommand instanceof StoredProcedure));
    } catch (QueryParserException e) {
        throw new QueryResolverException(QueryPlugin.Event.TEIID30060, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30060, group, procCommand.getClass().getSimpleName()));
    }
    return subCommand;
}
Also used : QueryParser(org.teiid.query.parser.QueryParser) QueryParserException(org.teiid.api.exception.query.QueryParserException) StoredProcedure(org.teiid.query.sql.lang.StoredProcedure) CreateProcedureCommand(org.teiid.query.sql.proc.CreateProcedureCommand) Command(org.teiid.query.sql.lang.Command) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) QueryResolverException(org.teiid.api.exception.query.QueryResolverException)

Aggregations

QueryParserException (org.teiid.api.exception.query.QueryParserException)10 QueryResolverException (org.teiid.api.exception.query.QueryResolverException)5 TeiidComponentException (org.teiid.core.TeiidComponentException)4 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)4 ArrayList (java.util.ArrayList)3 Command (org.teiid.query.sql.lang.Command)3 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)3 Expression (org.teiid.query.sql.symbol.Expression)3 IOException (java.io.IOException)2 QueryParser (org.teiid.query.parser.QueryParser)2 CreateProcedureCommand (org.teiid.query.sql.proc.CreateProcedureCommand)2 CommandContext (org.teiid.query.util.CommandContext)2 HashMap (java.util.HashMap)1 QueryMetadataException (org.teiid.api.exception.query.QueryMetadataException)1 BlockedException (org.teiid.common.buffer.BlockedException)1 TeiidException (org.teiid.core.TeiidException)1 TeiidProcessingException (org.teiid.core.TeiidProcessingException)1 CacheID (org.teiid.dqp.internal.process.SessionAwareCache.CacheID)1 Column (org.teiid.metadata.Column)1 Trigger (org.teiid.metadata.Trigger)1