use of org.teiid.query.metadata.TempMetadataStore in project teiid by teiid.
the class ProcedureContainerResolver method findChildCommandMetadata.
/**
* Set the appropriate "external" metadata for the given command
* @param inferProcedureResultSetColumns
* @throws QueryResolverException
*/
public static void findChildCommandMetadata(Command currentCommand, GroupSymbol container, int type, QueryMetadataInterface metadata, boolean inferProcedureResultSetColumns) throws QueryMetadataException, TeiidComponentException, QueryResolverException {
// find the childMetadata using a clean metadata store
TempMetadataStore childMetadata = new TempMetadataStore();
TempMetadataAdapter tma = new TempMetadataAdapter(metadata, childMetadata);
GroupContext externalGroups = new GroupContext();
if (currentCommand instanceof TriggerAction) {
TriggerAction ta = (TriggerAction) currentCommand;
ta.setView(container);
// TODO: it seems easier to just inline the handling here rather than have each of the resolvers check for trigger actions
List<ElementSymbol> viewElements = ResolverUtil.resolveElementsInGroup(ta.getView(), metadata);
if (type == Command.TYPE_UPDATE || type == Command.TYPE_INSERT) {
ProcedureContainerResolver.addChanging(tma.getMetadataStore(), externalGroups, viewElements);
ProcedureContainerResolver.addScalarGroup(SQLConstants.Reserved.NEW, tma.getMetadataStore(), externalGroups, viewElements, false);
if (type == Command.TYPE_INSERT) {
List<ElementSymbol> key = InsertResolver.getAutoIncrementKey(ta.getView().getMetadataID(), viewElements, metadata);
if (key != null) {
ProcedureContainerResolver.addScalarGroup(SQLConstants.NonReserved.KEY, tma.getMetadataStore(), externalGroups, key, true);
}
}
}
if (type == Command.TYPE_UPDATE || type == Command.TYPE_DELETE) {
ProcedureContainerResolver.addScalarGroup(SQLConstants.Reserved.OLD, tma.getMetadataStore(), externalGroups, viewElements, false);
}
} else if (currentCommand instanceof CreateProcedureCommand) {
CreateProcedureCommand cupc = (CreateProcedureCommand) currentCommand;
cupc.setVirtualGroup(container);
if (type == Command.TYPE_STORED_PROCEDURE) {
StoredProcedureInfo info = metadata.getStoredProcedureInfoForProcedure(container.getName());
// 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 = info.getProcedureCallableName();
// Look through parameters to find input elements - these become child metadata
List<ElementSymbol> tempElements = new ArrayList<ElementSymbol>(info.getParameters().size());
boolean[] updatable = new boolean[info.getParameters().size()];
int i = 0;
List<ElementSymbol> rsColumns = Collections.emptyList();
for (SPParameter param : info.getParameters()) {
if (param.getParameterType() != ParameterInfo.RESULT_SET) {
ElementSymbol symbol = param.getParameterSymbol();
tempElements.add(symbol);
updatable[i++] = param.getParameterType() != ParameterInfo.IN;
if (param.getParameterType() == ParameterInfo.RETURN_VALUE) {
cupc.setReturnVariable(symbol);
}
} else {
rsColumns = param.getResultSetColumns();
}
}
if (inferProcedureResultSetColumns) {
rsColumns = null;
}
GroupSymbol gs = ProcedureContainerResolver.addScalarGroup(procName, childMetadata, externalGroups, tempElements, updatable);
if (cupc.getReturnVariable() != null) {
ResolverVisitor.resolveLanguageObject(cupc.getReturnVariable(), Arrays.asList(gs), metadata);
}
cupc.setResultSetColumns(rsColumns);
// the relational planner will override this with the appropriate value
cupc.setProjectedSymbols(rsColumns);
} else {
cupc.setUpdateType(type);
}
}
QueryResolver.setChildMetadata(currentCommand, childMetadata, externalGroups);
}
use of org.teiid.query.metadata.TempMetadataStore in project teiid by teiid.
the class QueryResolver method resolveWithBindingMetadata.
/**
* Bindings are a poor mans input parameters. They are represented in legacy metadata
* by ElementSymbols and placed positionally into the command or by alias symbols
* and matched by names. After resolving bindings will be replaced with their
* referenced symbols (input names will not be used) and those symbols will
* be marked as external references.
*/
public static TempMetadataStore resolveWithBindingMetadata(Command currentCommand, QueryMetadataInterface metadata, QueryNode queryNode, boolean replaceBindings) throws TeiidComponentException, QueryResolverException {
Map<ElementSymbol, ElementSymbol> symbolMap = null;
if (queryNode.getBindings() != null && queryNode.getBindings().size() > 0) {
symbolMap = new HashMap<ElementSymbol, ElementSymbol>();
// Create ElementSymbols for each InputParameter
final List<ElementSymbol> elements = new ArrayList<ElementSymbol>(queryNode.getBindings().size());
boolean positional = true;
for (Expression ses : parseBindings(queryNode)) {
String name = Symbol.getShortName(ses);
if (ses instanceof AliasSymbol) {
ses = ((AliasSymbol) ses).getSymbol();
positional = false;
}
ElementSymbol elementSymbol = (ElementSymbol) ses;
ResolverVisitor.resolveLanguageObject(elementSymbol, metadata);
elementSymbol.setIsExternalReference(true);
if (!positional) {
symbolMap.put(new ElementSymbol("INPUT" + Symbol.SEPARATOR + name), elementSymbol.clone());
symbolMap.put(new ElementSymbol(BINDING_GROUP + Symbol.SEPARATOR + name), elementSymbol.clone());
elementSymbol.setShortName(name);
}
elements.add(elementSymbol);
}
if (positional) {
ExpressionMappingVisitor emv = new ExpressionMappingVisitor(null) {
@Override
public Expression replaceExpression(Expression element) {
if (!(element instanceof Reference)) {
return element;
}
Reference ref = (Reference) element;
if (!ref.isPositional()) {
return ref;
}
return elements.get(ref.getIndex()).clone();
}
};
DeepPostOrderNavigator.doVisit(currentCommand, emv);
} else {
TempMetadataStore rootExternalStore = new TempMetadataStore();
GroupContext externalGroups = new GroupContext();
// $NON-NLS-1$
ProcedureContainerResolver.addScalarGroup("INPUT", rootExternalStore, externalGroups, elements);
ProcedureContainerResolver.addScalarGroup(BINDING_GROUP, rootExternalStore, externalGroups, elements);
QueryResolver.setChildMetadata(currentCommand, rootExternalStore, externalGroups);
}
}
TempMetadataStore result = resolveCommand(currentCommand, metadata, false);
if (replaceBindings && symbolMap != null && !symbolMap.isEmpty()) {
ExpressionMappingVisitor emv = new ExpressionMappingVisitor(symbolMap);
DeepPostOrderNavigator.doVisit(currentCommand, emv);
}
return result;
}
use of org.teiid.query.metadata.TempMetadataStore in project teiid by teiid.
the class QueryResolver method setChildMetadata.
public static void setChildMetadata(Command subCommand, Command parent) {
TempMetadataStore childMetadata = parent.getTemporaryMetadata();
GroupContext parentContext = parent.getExternalGroupContexts();
setChildMetadata(subCommand, childMetadata, parentContext);
}
use of org.teiid.query.metadata.TempMetadataStore in project teiid by teiid.
the class QueryResolver method resolveCommand.
public static TempMetadataStore resolveCommand(Command currentCommand, QueryMetadataInterface metadata, boolean resolveNullLiterals) throws QueryResolverException, TeiidComponentException {
// $NON-NLS-1$
LogManager.logTrace(org.teiid.logging.LogConstants.CTX_QUERY_RESOLVER, new Object[] { "Resolving command", currentCommand });
TempMetadataAdapter resolverMetadata = null;
try {
TempMetadataStore discoveredMetadata = currentCommand.getTemporaryMetadata();
if (discoveredMetadata == null) {
discoveredMetadata = new TempMetadataStore();
currentCommand.setTemporaryMetadata(discoveredMetadata);
}
resolverMetadata = new TempMetadataAdapter(metadata, discoveredMetadata);
// Resolve external groups for command
Collection<GroupSymbol> externalGroups = currentCommand.getAllExternalGroups();
for (GroupSymbol extGroup : externalGroups) {
Object metadataID = extGroup.getMetadataID();
// TODO: this is mainly for XML resolving since it sends external groups in unresolved
if (metadataID == null || (!(extGroup.getMetadataID() instanceof TempMetadataID) && discoveredMetadata.getTempGroupID(extGroup.getName()) != null)) {
boolean missing = metadataID == null;
metadataID = resolverMetadata.getGroupID(extGroup.getName());
if (missing) {
extGroup.setMetadataID(metadataID);
} else {
// we shouldn't modify the existing, just add a shadow group
GroupSymbol gs = extGroup.clone();
gs.setMetadataID(metadataID);
currentCommand.getExternalGroupContexts().addGroup(gs);
}
}
}
CommandResolver resolver = chooseResolver(currentCommand, resolverMetadata);
// Resolve this command
resolver.resolveCommand(currentCommand, resolverMetadata, resolveNullLiterals);
} catch (QueryMetadataException e) {
throw new QueryResolverException(e);
}
// Flag that this command has been resolved.
currentCommand.setIsResolved(true);
return resolverMetadata.getMetadataStore();
}
use of org.teiid.query.metadata.TempMetadataStore in project teiid by teiid.
the class QueryResolver method setChildMetadata.
public static void setChildMetadata(Command subCommand, TempMetadataStore parentTempMetadata, GroupContext parentContext) {
TempMetadataStore tempMetadata = subCommand.getTemporaryMetadata();
if (tempMetadata == null) {
subCommand.setTemporaryMetadata(parentTempMetadata.clone());
} else {
tempMetadata.getData().putAll(parentTempMetadata.getData());
}
subCommand.setExternalGroupContexts(parentContext);
}
Aggregations