Search in sources :

Example 11 with GroupSymbol

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

the class TestJoinOptimization method helpTestNullDependent.

private void helpTestNullDependent(String expressionSQL, boolean dependent) throws QueryParserException, QueryResolverException, QueryMetadataException, TeiidComponentException {
    List<GroupSymbol> innerGroups = new ArrayList<GroupSymbol>();
    // $NON-NLS-1$
    innerGroups.add(new GroupSymbol("pm1.g1"));
    Expression expr = QueryParser.getQueryParser().parseExpression(expressionSQL);
    ResolverVisitor.resolveLanguageObject(expr, RealMetadataFactory.example1Cached());
    assertEquals(dependent, JoinUtil.isNullDependent(RealMetadataFactory.example1Cached(), innerGroups, expr));
}
Also used : Expression(org.teiid.query.sql.symbol.Expression) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) ArrayList(java.util.ArrayList)

Example 12 with GroupSymbol

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

the class DynamicCommandResolver method resolveCommand.

/**
 * @see org.teiid.query.resolver.CommandResolver#resolveCommand(org.teiid.query.sql.lang.Command, TempMetadataAdapter, boolean)
 */
public void resolveCommand(Command command, TempMetadataAdapter metadata, boolean resolveNullLiterals) throws QueryMetadataException, QueryResolverException, TeiidComponentException {
    DynamicCommand dynamicCmd = (DynamicCommand) command;
    Iterator columns = dynamicCmd.getAsColumns().iterator();
    Set groups = new HashSet();
    boolean resolvedColumns = false;
    // if there is no into group, just create temp metadata ids
    if (dynamicCmd.getIntoGroup() == null) {
        while (columns.hasNext()) {
            ElementSymbol column = (ElementSymbol) columns.next();
            column.setMetadataID(new TempMetadataID(column.getShortName(), column.getType()));
        }
    } else if (dynamicCmd.getIntoGroup().isTempGroupSymbol()) {
        resolvedColumns = true;
        while (columns.hasNext()) {
            ElementSymbol column = (ElementSymbol) columns.next();
            column.setGroupSymbol(new GroupSymbol(dynamicCmd.getIntoGroup().getName()));
        }
    }
    ResolverVisitor.resolveLanguageObject(dynamicCmd, groups, dynamicCmd.getExternalGroupContexts(), metadata);
    String sqlType = DataTypeManager.getDataTypeName(dynamicCmd.getSql().getType());
    String targetType = DataTypeManager.DefaultDataTypes.CLOB;
    if (!targetType.equals(sqlType) && !DataTypeManager.isImplicitConversion(sqlType, targetType)) {
        throw new QueryResolverException(QueryPlugin.Event.TEIID30100, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30100, sqlType));
    }
    dynamicCmd.setSql(ResolverUtil.convertExpression(dynamicCmd.getSql(), targetType, metadata));
    if (dynamicCmd.getUsing() != null && !dynamicCmd.getUsing().isEmpty()) {
        for (SetClause clause : dynamicCmd.getUsing().getClauses()) {
            ElementSymbol id = clause.getSymbol();
            id.setGroupSymbol(new GroupSymbol(ProcedureReservedWords.DVARS));
            id.setType(clause.getValue().getType());
            id.setMetadataID(new TempMetadataID(id.getName(), id.getType()));
        }
    }
    GroupSymbol intoSymbol = dynamicCmd.getIntoGroup();
    if (intoSymbol != null) {
        if (!intoSymbol.isImplicitTempGroupSymbol()) {
            ResolverUtil.resolveGroup(intoSymbol, metadata);
            if (!resolvedColumns) {
                // must be a temp table from a higher scope
                for (ElementSymbol column : (List<ElementSymbol>) dynamicCmd.getAsColumns()) {
                    column.setGroupSymbol(dynamicCmd.getIntoGroup().clone());
                }
            }
        } else {
            List symbols = dynamicCmd.getAsColumns();
            ResolverUtil.resolveImplicitTempGroup(metadata, intoSymbol, symbols);
        }
    }
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) DynamicCommand(org.teiid.query.sql.lang.DynamicCommand) Set(java.util.Set) HashSet(java.util.HashSet) Iterator(java.util.Iterator) TempMetadataID(org.teiid.query.metadata.TempMetadataID) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) List(java.util.List) QueryResolverException(org.teiid.api.exception.query.QueryResolverException) HashSet(java.util.HashSet) SetClause(org.teiid.query.sql.lang.SetClause)

Example 13 with GroupSymbol

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

the class ExecResolver method findCommandMetadata.

/**
 * @see org.teiid.query.resolver.CommandResolver#findCommandMetadata(org.teiid.query.sql.lang.Command,
 * org.teiid.query.metadata.QueryMetadataInterface)
 */
private void findCommandMetadata(Command command, TempMetadataStore discoveredMetadata, QueryMetadataInterface metadata) throws QueryMetadataException, QueryResolverException, TeiidComponentException {
    StoredProcedure storedProcedureCommand = (StoredProcedure) command;
    StoredProcedureInfo storedProcedureInfo = null;
    try {
        storedProcedureInfo = metadata.getStoredProcedureInfoForProcedure(storedProcedureCommand.getProcedureName());
    } catch (QueryMetadataException e) {
        // $NON-NLS-1$
        String[] parts = storedProcedureCommand.getProcedureName().split("\\.", 2);
        if (parts.length > 1 && parts[0].equalsIgnoreCase(metadata.getVirtualDatabaseName())) {
            try {
                storedProcedureInfo = metadata.getStoredProcedureInfoForProcedure(parts[1]);
                storedProcedureCommand.setProcedureName(parts[1]);
            } catch (QueryMetadataException e1) {
            }
        }
        if (storedProcedureInfo == null) {
            throw e;
        }
    }
    storedProcedureCommand.setUpdateCount(storedProcedureInfo.getUpdateCount());
    storedProcedureCommand.setModelID(storedProcedureInfo.getModelID());
    storedProcedureCommand.setProcedureID(storedProcedureInfo.getProcedureID());
    storedProcedureCommand.setProcedureCallableName(storedProcedureInfo.getProcedureCallableName());
    // Get old parameters as they may have expressions set on them - collect
    // those expressions to copy later into the resolved parameters
    Collection<SPParameter> oldParams = storedProcedureCommand.getParameters();
    boolean namedParameters = storedProcedureCommand.displayNamedParameters();
    // of relying on all default values of all optional parameters.
    if (oldParams.size() == 0 || (oldParams.size() == 1 && storedProcedureCommand.isCalledWithReturn())) {
        storedProcedureCommand.setDisplayNamedParameters(true);
        namedParameters = true;
    }
    // Cache original input parameter expressions.  Depending on whether
    // the procedure was parsed with named or unnamed parameters, the keys
    // for this map will either be the String names of the parameters or
    // the Integer indices, as entered in the user query
    Map<Integer, Expression> positionalExpressions = new TreeMap<Integer, Expression>();
    Map<String, Expression> namedExpressions = new TreeMap<String, Expression>(String.CASE_INSENSITIVE_ORDER);
    int adjustIndex = 0;
    for (SPParameter param : oldParams) {
        if (param.getExpression() == null) {
            if (param.getParameterType() == SPParameter.RESULT_SET) {
                // If this was already resolved, just pretend the result set param doesn't exist
                adjustIndex--;
            }
            continue;
        }
        if (namedParameters && param.getParameterType() != SPParameter.RETURN_VALUE) {
            if (namedExpressions.put(param.getParameterSymbol().getShortName(), param.getExpression()) != null) {
                throw new QueryResolverException(QueryPlugin.Event.TEIID30138, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30138, param.getName()));
            }
        } else {
            positionalExpressions.put(param.getIndex() + adjustIndex, param.getExpression());
        }
    }
    storedProcedureCommand.clearParameters();
    int origInputs = positionalExpressions.size() + namedExpressions.size();
    /*
         * Take the values set from the stored procedure implementation, and match up with the
         * types of parameter it is from the metadata and then reset the newly joined parameters
         * into the stored procedure command.  If it is a result set get those columns and place
         * them into the stored procedure command as well.
         */
    List<SPParameter> metadataParams = storedProcedureInfo.getParameters();
    List<SPParameter> clonedMetadataParams = new ArrayList<SPParameter>(metadataParams.size());
    int inputParams = 0;
    int optionalParams = 0;
    int outParams = 0;
    boolean hasReturnValue = false;
    boolean optional = false;
    boolean varargs = false;
    for (int i = 0; i < metadataParams.size(); i++) {
        SPParameter metadataParameter = metadataParams.get(i);
        if ((metadataParameter.getParameterType() == ParameterInfo.IN) || (metadataParameter.getParameterType() == ParameterInfo.INOUT)) {
            if (ResolverUtil.hasDefault(metadataParameter.getMetadataID(), metadata) || metadataParameter.isVarArg()) {
                optional = true;
                optionalParams++;
            } else {
                inputParams++;
                if (optional) {
                    optional = false;
                    inputParams += optionalParams;
                    optionalParams = 0;
                }
            }
            if (metadataParameter.isVarArg()) {
                varargs = true;
            }
        } else if (metadataParameter.getParameterType() == ParameterInfo.OUT) {
            outParams++;
        /*
            	 * TODO: it would consistent to do the following, but it is a breaking change for procedures that have intermixed out params with in.
            	 * we may need to revisit this later
            	 */
        // optional = true;
        // optionalParams++;
        } else if (metadataParameter.getParameterType() == ParameterInfo.RETURN_VALUE) {
            hasReturnValue = true;
        }
        SPParameter clonedParam = (SPParameter) metadataParameter.clone();
        clonedMetadataParams.add(clonedParam);
        storedProcedureCommand.setParameter(clonedParam);
    }
    if (storedProcedureCommand.isCalledWithReturn() && !hasReturnValue) {
        throw new QueryResolverException(QueryPlugin.Event.TEIID30139, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30139, storedProcedureCommand.getGroup()));
    }
    if (!namedParameters && (inputParams > positionalExpressions.size())) {
        // $NON-NLS-1$ //$NON-NLS-2$
        throw new QueryResolverException(QueryPlugin.Event.TEIID30140, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30140, inputParams, inputParams + optionalParams + (varargs ? "+" : ""), origInputs, storedProcedureCommand.getGroup()));
    }
    // Walk through the resolved parameters and set the expressions from the
    // input parameters
    int exprIndex = 1;
    HashSet<String> expected = new HashSet<String>();
    if (storedProcedureCommand.isCalledWithReturn() && hasReturnValue) {
        for (SPParameter param : clonedMetadataParams) {
            if (param.getParameterType() == SPParameter.RETURN_VALUE) {
                Expression expr = positionalExpressions.remove(exprIndex++);
                param.setExpression(expr);
                break;
            }
        }
    }
    for (SPParameter param : clonedMetadataParams) {
        if (param.getParameterType() == SPParameter.RESULT_SET || param.getParameterType() == SPParameter.RETURN_VALUE) {
            continue;
        }
        if (namedParameters) {
            String nameKey = param.getParameterSymbol().getShortName();
            Expression expr = namedExpressions.remove(nameKey);
            // With named parameters, have to check on optional params and default values
            if (expr == null) {
                if (param.getParameterType() != ParameterInfo.OUT) {
                    param.setUsingDefault(true);
                    expected.add(nameKey);
                    if (!param.isVarArg()) {
                        expr = ResolverUtil.getDefault(param.getParameterSymbol(), metadata);
                    } else {
                        // zero length array
                        List<Expression> exprs = new ArrayList<Expression>(0);
                        Array array = new Array(exprs);
                        array.setImplicit(true);
                        array.setType(param.getClassType());
                        expr = array;
                    }
                }
            }
            param.setExpression(expr);
        } else {
            Expression expr = positionalExpressions.remove(exprIndex++);
            if (param.getParameterType() == SPParameter.OUT) {
                if (expr != null) {
                    boolean isRef = expr instanceof Reference;
                    if (!isRef || exprIndex <= inputParams + 1) {
                        // for backwards compatibility, this should be treated instead as an input
                        exprIndex--;
                        positionalExpressions.put(exprIndex, expr);
                    } else if (isRef) {
                        // mimics the hack that was in PreparedStatementRequest.
                        Reference ref = (Reference) expr;
                        // may be an out
                        ref.setOptional(true);
                    /*
	                		 * Note that there is a corner case here with out parameters intermixed with optional parameters
	                		 * there's not a good way around this.
	                		 */
                    }
                }
                continue;
            }
            if (expr == null) {
                if (!param.isVarArg()) {
                    expr = ResolverUtil.getDefault(param.getParameterSymbol(), metadata);
                }
                param.setUsingDefault(true);
            }
            if (param.isVarArg()) {
                List<Expression> exprs = new ArrayList<Expression>(positionalExpressions.size() + 1);
                if (expr != null) {
                    exprs.add(expr);
                }
                exprs.addAll(positionalExpressions.values());
                positionalExpressions.clear();
                Array array = new Array(exprs);
                array.setImplicit(true);
                array.setType(param.getClassType());
                expr = array;
            }
            param.setExpression(expr);
        }
    }
    // Check for leftovers, i.e. params entered by user w/ wrong/unknown names
    if (!namedExpressions.isEmpty()) {
        throw new QueryResolverException(QueryPlugin.Event.TEIID30141, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30141, namedExpressions.keySet(), expected));
    }
    if (!positionalExpressions.isEmpty()) {
        throw new QueryResolverException(QueryPlugin.Event.TEIID31113, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31113, positionalExpressions.size(), origInputs, storedProcedureCommand.getGroup().toString()));
    }
    // Create temporary metadata that defines a group based on either the stored proc
    // name or the stored query name - this will be used later during planning
    String procName = storedProcedureCommand.getProcedureName();
    List tempElements = storedProcedureCommand.getProjectedSymbols();
    boolean isVirtual = storedProcedureInfo.getQueryPlan() != null;
    discoveredMetadata.addTempGroup(procName, tempElements, isVirtual);
    // Resolve tempElements against new metadata
    GroupSymbol procGroup = new GroupSymbol(storedProcedureInfo.getProcedureCallableName());
    procGroup.setProcedure(true);
    TempMetadataID tid = discoveredMetadata.getTempGroupID(procName);
    tid.setOriginalMetadataID(storedProcedureCommand.getProcedureID());
    procGroup.setMetadataID(tid);
    storedProcedureCommand.setGroup(procGroup);
}
Also used : SPParameter(org.teiid.query.sql.lang.SPParameter) Reference(org.teiid.query.sql.symbol.Reference) ArrayList(java.util.ArrayList) TempMetadataID(org.teiid.query.metadata.TempMetadataID) QueryMetadataException(org.teiid.api.exception.query.QueryMetadataException) TreeMap(java.util.TreeMap) QueryResolverException(org.teiid.api.exception.query.QueryResolverException) StoredProcedureInfo(org.teiid.query.metadata.StoredProcedureInfo) Array(org.teiid.query.sql.symbol.Array) StoredProcedure(org.teiid.query.sql.lang.StoredProcedure) Expression(org.teiid.query.sql.symbol.Expression) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 14 with GroupSymbol

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

the class InsertResolver method resolveProceduralCommand.

/**
 * Resolve an INSERT.  Need to resolve elements, constants, types, etc.
 * @see org.teiid.query.resolver.ProcedureContainerResolver#resolveProceduralCommand(org.teiid.query.sql.lang.Command, org.teiid.query.metadata.TempMetadataAdapter)
 */
public void resolveProceduralCommand(Command command, TempMetadataAdapter metadata) throws QueryMetadataException, QueryResolverException, TeiidComponentException {
    // Cast to known type
    Insert insert = (Insert) command;
    if (insert.getValues() != null) {
        QueryResolver.resolveSubqueries(command, metadata, null);
        // variables and values must be resolved separately to account for implicitly defined temp groups
        resolveList(insert.getValues(), metadata, insert.getExternalGroupContexts(), null);
    }
    boolean usingQuery = insert.getQueryExpression() != null;
    // resolve subquery if there
    if (usingQuery) {
        QueryResolver.setChildMetadata(insert.getQueryExpression(), command);
        QueryResolver.resolveCommand(insert.getQueryExpression(), metadata.getMetadata(), false);
    }
    Set<GroupSymbol> groups = new HashSet<GroupSymbol>();
    groups.add(insert.getGroup());
    // resolve any functions in the values
    List values = insert.getValues();
    if (usingQuery) {
        values = insert.getQueryExpression().getProjectedSymbols();
    }
    if (insert.getVariables().isEmpty()) {
        if (insert.getGroup().isResolved()) {
            List<ElementSymbol> variables = ResolverUtil.resolveElementsInGroup(insert.getGroup(), metadata);
            for (Iterator<ElementSymbol> i = variables.iterator(); i.hasNext(); ) {
                insert.addVariable(i.next().clone());
            }
        } else {
            for (int i = 0; i < values.size(); i++) {
                if (usingQuery) {
                    Expression ses = (Expression) values.get(i);
                    ElementSymbol es = new ElementSymbol(Symbol.getShortName(ses));
                    es.setType(ses.getType());
                    insert.addVariable(es);
                } else {
                    // $NON-NLS-1$
                    insert.addVariable(new ElementSymbol("expr" + i));
                }
            }
        }
    } else if (insert.getGroup().isResolved()) {
        resolveVariables(metadata, insert, groups);
    }
    resolveTypes(insert, metadata, values, usingQuery);
    if (usingQuery && insert.getQueryExpression() instanceof SetQuery) {
        // now that the first branch is set, we need to make sure that all branches conform
        QueryResolver.resolveCommand(insert.getQueryExpression(), metadata.getMetadata(), false);
        resolveTypes(insert, metadata, values, usingQuery);
    }
    if (!insert.getGroup().isResolved()) {
        // define the implicit temp group
        ResolverUtil.resolveImplicitTempGroup(metadata, insert.getGroup(), insert.getVariables());
        resolveVariables(metadata, insert, groups);
        // ensure that the types match
        resolveTypes(insert, metadata, values, usingQuery);
    }
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) SetQuery(org.teiid.query.sql.lang.SetQuery) Expression(org.teiid.query.sql.symbol.Expression) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) ArrayList(java.util.ArrayList) List(java.util.List) Insert(org.teiid.query.sql.lang.Insert) HashSet(java.util.HashSet)

Example 15 with GroupSymbol

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

the class TempTableResolver method resolveCommand.

/**
 * @see org.teiid.query.resolver.CommandResolver#resolveCommand(org.teiid.query.sql.lang.Command, org.teiid.query.metadata.TempMetadataAdapter, boolean)
 */
public void resolveCommand(Command command, TempMetadataAdapter metadata, boolean resolveNullLiterals) throws QueryMetadataException, QueryResolverException, TeiidComponentException {
    if (command.getType() == Command.TYPE_CREATE) {
        Create create = (Create) command;
        GroupSymbol group = create.getTable();
        // assuming that all temp table creates are local, the user must use a local name
        if (group.getName().indexOf(Symbol.SEPARATOR) != -1) {
            throw new QueryResolverException(QueryPlugin.Event.TEIID30117, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30117, group.getName()));
        }
        // this will only check non-temp groups
        Collection exitsingGroups = metadata.getMetadata().getGroupsForPartialName(group.getName());
        if (!exitsingGroups.isEmpty()) {
            throw new QueryResolverException(QueryPlugin.Event.TEIID30118, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30118, group.getName()));
        }
        if (metadata.getMetadata().hasProcedure(group.getName())) {
            throw new QueryResolverException(QueryPlugin.Event.TEIID30118, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30118, group.getName()));
        }
        // now we will be more specific for temp groups
        TempMetadataID id = metadata.getMetadataStore().getTempGroupID(group.getName());
        if (id != null && !metadata.isTemporaryTable(id)) {
            throw new QueryResolverException(QueryPlugin.Event.TEIID30118, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30118, group.getName()));
        }
        // if we get here then either the group does not exist or has already been defined as a temp table
        // if it has been defined as a temp table, that's ok we'll use this as the new definition and throw an
        // exception at runtime if the user has not dropped the previous table yet
        TempMetadataID tempTable = ResolverUtil.addTempTable(metadata, group, create.getColumnSymbols());
        ResolverUtil.resolveGroup(create.getTable(), metadata);
        Set<GroupSymbol> groups = new HashSet<GroupSymbol>();
        groups.add(create.getTable());
        ResolverVisitor.resolveLanguageObject(command, groups, metadata);
        addAdditionalMetadata(create, tempTable);
        tempTable.setOriginalMetadataID(create.getTableMetadata());
        if (create.getOn() != null) {
            Object mid = null;
            try {
                mid = metadata.getModelID(create.getOn());
            } catch (QueryMetadataException e) {
                throw new QueryResolverException(QueryPlugin.Event.TEIID31134, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31134, create.getOn()));
            }
            if (mid != null && (metadata.isVirtualModel(mid) || !(mid instanceof Schema))) {
                throw new QueryResolverException(QueryPlugin.Event.TEIID31135, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31135, create.getOn()));
            }
            create.getTableMetadata().setParent((Schema) mid);
            tempTable.getTableData().setModel(mid);
        }
    } else if (command.getType() == Command.TYPE_DROP) {
        ResolverUtil.resolveGroup(((Drop) command).getTable(), metadata);
    }
}
Also used : Create(org.teiid.query.sql.lang.Create) Schema(org.teiid.metadata.Schema) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) TempMetadataID(org.teiid.query.metadata.TempMetadataID) Collection(java.util.Collection) QueryMetadataException(org.teiid.api.exception.query.QueryMetadataException) QueryResolverException(org.teiid.api.exception.query.QueryResolverException) HashSet(java.util.HashSet) Drop(org.teiid.query.sql.lang.Drop)

Aggregations

GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)299 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)146 Test (org.junit.Test)108 ArrayList (java.util.ArrayList)92 MultipleElementSymbol (org.teiid.query.sql.symbol.MultipleElementSymbol)59 Expression (org.teiid.query.sql.symbol.Expression)52 PlanNode (org.teiid.query.optimizer.relational.plantree.PlanNode)50 Constant (org.teiid.query.sql.symbol.Constant)48 List (java.util.List)43 HashSet (java.util.HashSet)32 Query (org.teiid.query.sql.lang.Query)31 From (org.teiid.query.sql.lang.From)29 SymbolMap (org.teiid.query.sql.util.SymbolMap)29 Select (org.teiid.query.sql.lang.Select)26 Criteria (org.teiid.query.sql.lang.Criteria)22 TempMetadataID (org.teiid.query.metadata.TempMetadataID)21 LinkedList (java.util.LinkedList)20 Command (org.teiid.query.sql.lang.Command)20 Set (java.util.Set)17 CompareCriteria (org.teiid.query.sql.lang.CompareCriteria)17