Search in sources :

Example 26 with ElementSymbol

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

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

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

the class InsertResolver method getAutoIncrementKey.

public static List<ElementSymbol> getAutoIncrementKey(Object metadataId, List<ElementSymbol> columns, QueryMetadataInterface metadata) throws QueryMetadataException, TeiidComponentException {
    Object pk = metadata.getPrimaryKey(metadataId);
    if (pk == null) {
        return null;
    }
    List<?> cols = metadata.getElementIDsInKey(pk);
    List<ElementSymbol> key = null;
    for (ElementSymbol es : columns) {
        if (cols.contains(es.getMetadataID())) {
            if (!metadata.elementSupports(es.getMetadataID(), SupportConstants.Element.AUTO_INCREMENT) && !metadata.elementSupports(es.getMetadataID(), SupportConstants.Element.NULL)) {
                return null;
            }
            if (key == null) {
                key = new ArrayList<ElementSymbol>();
            }
            key.add(es);
        }
    }
    return key;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol)

Example 29 with ElementSymbol

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

the class TempTableResolver method addAdditionalMetadata.

public static void addAdditionalMetadata(Create create, TempMetadataID tempTable) {
    if (!create.getPrimaryKey().isEmpty()) {
        ArrayList<TempMetadataID> primaryKey = new ArrayList<TempMetadataID>(create.getPrimaryKey().size());
        for (ElementSymbol symbol : create.getPrimaryKey()) {
            Object mid = symbol.getMetadataID();
            if (mid instanceof TempMetadataID) {
                primaryKey.add((TempMetadataID) mid);
            } else if (mid instanceof Column) {
                // TODO: this breaks our normal metadata usage
                primaryKey.add(tempTable.getElements().get(((Column) mid).getPosition() - 1));
            }
        }
        tempTable.setPrimaryKey(primaryKey);
    }
    for (int i = 0; i < create.getColumns().size(); i++) {
        Column column = create.getColumns().get(i);
        TempMetadataID tid = tempTable.getElements().get(i);
        if (column.isAutoIncremented()) {
            tid.setAutoIncrement(true);
        }
        if (column.getNullType() == NullType.No_Nulls) {
            tid.setNotNull(true);
        }
    }
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Column(org.teiid.metadata.Column) TempMetadataID(org.teiid.query.metadata.TempMetadataID) ArrayList(java.util.ArrayList)

Example 30 with ElementSymbol

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

the class UpdateProcedureResolver method collectDeclareVariable.

private void collectDeclareVariable(DeclareStatement obj, GroupSymbol variables, TempMetadataAdapter metadata, GroupContext externalGroups) throws QueryResolverException, TeiidComponentException {
    ElementSymbol variable = obj.getVariable();
    String typeName = obj.getVariableType();
    GroupSymbol gs = variable.getGroupSymbol();
    if (gs == null) {
        String outputName = variable.getShortName();
        variable.setGroupSymbol(new GroupSymbol(ProcedureReservedWords.VARIABLES));
        variable.setOutputName(outputName);
    } else {
        if (gs.getSchema() != null || !gs.getShortName().equalsIgnoreCase(ProcedureReservedWords.VARIABLES)) {
            // $NON-NLS-1$
            handleUnresolvableDeclaration(variable, QueryPlugin.Util.getString("ERR.015.010.0031", new Object[] { ProcedureReservedWords.VARIABLES, variable }));
        }
    }
    boolean exists = false;
    try {
        ResolverVisitor.resolveLanguageObject(variable, null, externalGroups, metadata);
        exists = true;
    } catch (QueryResolverException e) {
    // ignore, not already defined
    }
    if (exists) {
        // $NON-NLS-1$
        handleUnresolvableDeclaration(variable, QueryPlugin.Util.getString("ERR.015.010.0032", variable.getOutputName()));
    }
    boolean exception = typeName.equalsIgnoreCase(SQLConstants.NonReserved.EXCEPTION);
    variable.setType(exception ? DataTypeManager.DefaultDataClasses.OBJECT : DataTypeManager.getDataTypeClass(typeName));
    variable.setGroupSymbol(variables);
    TempMetadataID id = new TempMetadataID(variable.getName(), exception ? Exception.class : variable.getType());
    id.setUpdatable(true);
    variable.setMetadataID(id);
    // TODO: this will cause the variables group to loose it's cache of resolved symbols
    metadata.getMetadataStore().addElementToTempGroup(ProcedureReservedWords.VARIABLES, variable.clone());
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) TempMetadataID(org.teiid.query.metadata.TempMetadataID) QueryResolverException(org.teiid.api.exception.query.QueryResolverException) TeiidComponentException(org.teiid.core.TeiidComponentException) QueryMetadataException(org.teiid.api.exception.query.QueryMetadataException) QueryResolverException(org.teiid.api.exception.query.QueryResolverException)

Aggregations

ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)417 ArrayList (java.util.ArrayList)165 Test (org.junit.Test)157 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)148 Expression (org.teiid.query.sql.symbol.Expression)104 List (java.util.List)103 Constant (org.teiid.query.sql.symbol.Constant)94 MultipleElementSymbol (org.teiid.query.sql.symbol.MultipleElementSymbol)41 SymbolMap (org.teiid.query.sql.util.SymbolMap)40 PlanNode (org.teiid.query.optimizer.relational.plantree.PlanNode)36 CompareCriteria (org.teiid.query.sql.lang.CompareCriteria)29 Map (java.util.Map)28 AggregateSymbol (org.teiid.query.sql.symbol.AggregateSymbol)28 Query (org.teiid.query.sql.lang.Query)26 HashMap (java.util.HashMap)25 Select (org.teiid.query.sql.lang.Select)24 BufferManager (org.teiid.common.buffer.BufferManager)22 Criteria (org.teiid.query.sql.lang.Criteria)22 LinkedList (java.util.LinkedList)20 TupleBuffer (org.teiid.common.buffer.TupleBuffer)19