use of org.teiid.api.exception.query.QueryValidatorException in project teiid by teiid.
the class QueryRewriter method createPkCriteria.
private List<Criteria> createPkCriteria(GroupSymbol viewGroup, GroupSymbol group, String correlationName, Query query, GroupSymbol varGroup) throws TeiidComponentException, QueryMetadataException, QueryValidatorException {
Object pk = metadata.getPrimaryKey(group.getMetadataID());
if (pk == null) {
Collection uniqueKeysInGroup = metadata.getUniqueKeysInGroup(group.getMetadataID());
if (uniqueKeysInGroup.isEmpty()) {
throw new QueryValidatorException(QueryPlugin.Event.TEIID31267, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31267, viewGroup, group));
}
pk = uniqueKeysInGroup.iterator().next();
}
int i = query.getSelect().getSymbols().size();
List<Object> ids = metadata.getElementIDsInKey(pk);
List<Criteria> pkCriteria = new ArrayList<Criteria>(ids.size());
for (Object object : ids) {
ElementSymbol es = new ElementSymbol(correlationName + Symbol.SEPARATOR + metadata.getName(object));
// $NON-NLS-1$
query.getSelect().addSymbol(new AliasSymbol("s_" + i, es));
es = new ElementSymbol(group.getName() + Symbol.SEPARATOR + metadata.getName(object));
// $NON-NLS-1$
pkCriteria.add(new CompareCriteria(es, CompareCriteria.EQ, new ElementSymbol("s_" + i, varGroup.clone())));
i++;
}
return pkCriteria;
}
use of org.teiid.api.exception.query.QueryValidatorException in project teiid by teiid.
the class QueryRewriter method rewriteAsUpsertProcedure.
public static Command rewriteAsUpsertProcedure(Insert insert, QueryMetadataInterface metadata, CommandContext context) throws TeiidComponentException, QueryMetadataException, QueryValidatorException, QueryResolverException, TeiidProcessingException {
QueryRewriter rewriter = new QueryRewriter(metadata, context);
Collection<?> keys = metadata.getUniqueKeysInGroup(insert.getGroup().getMetadataID());
if (keys.isEmpty()) {
throw new QueryValidatorException(QueryPlugin.Event.TEIID31132, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31132, insert.getGroup()));
}
Object key = keys.iterator().next();
Set<Object> keyCols = new LinkedHashSet<Object>(metadata.getElementIDsInKey(key));
Insert newInsert = new Insert();
newInsert.setGroup(insert.getGroup().clone());
newInsert.setVariables(LanguageObject.Util.deepClone(insert.getVariables(), ElementSymbol.class));
ArrayList<Expression> values = new ArrayList<Expression>();
IfStatement ifStatement = new IfStatement();
Query exists = new Query();
exists.setSelect(new Select(Arrays.asList(new Constant(1))));
exists.setFrom(new From(Arrays.asList(new UnaryFromClause(insert.getGroup().clone()))));
ifStatement.setCondition(new ExistsCriteria(exists));
Update update = new Update();
update.setGroup(insert.getGroup().clone());
SetClauseList setClauses = new SetClauseList();
update.setChangeList(setClauses);
List<Criteria> crits = new ArrayList<Criteria>();
GroupSymbol varGroup = getVarGroup(insert);
for (ElementSymbol es : insert.getVariables()) {
ElementSymbol var = new ElementSymbol(es.getShortName(), varGroup.clone());
values.add(var.clone());
if (keyCols.contains(es.getMetadataID())) {
CompareCriteria cc = new CompareCriteria(es.clone(), CompareCriteria.EQ, var.clone());
crits.add(cc);
} else {
setClauses.addClause(new SetClause(es.clone(), var.clone()));
}
}
newInsert.setValues(values);
update.setCriteria((Criteria) Criteria.combineCriteria(crits).clone());
exists.setCriteria((Criteria) Criteria.combineCriteria(crits).clone());
ifStatement.setIfBlock(new Block(new CommandStatement(update)));
ifStatement.setElseBlock(new Block(new CommandStatement(newInsert)));
// construct the value query
QueryCommand query = insert.getQueryExpression();
if (query == null) {
Query q = new Query();
Select s = new Select();
s.addSymbols(LanguageObject.Util.deepClone(insert.getValues(), Expression.class));
q.setSelect(s);
query = q;
}
// $NON-NLS-1$
query = createInlineViewQuery(new GroupSymbol("X"), query, metadata, insert.getVariables());
return rewriter.asLoopProcedure(insert.getGroup(), query, ifStatement, varGroup, Command.TYPE_INSERT);
}
use of org.teiid.api.exception.query.QueryValidatorException in project teiid by teiid.
the class QueryRewriter method rewriteExpressionDirect.
private Expression rewriteExpressionDirect(Expression expression) throws TeiidComponentException, TeiidProcessingException {
if (expression instanceof Constant) {
return expression;
}
if (expression instanceof ElementSymbol) {
ElementSymbol es = (ElementSymbol) expression;
Class<?> type = es.getType();
if (!processing && es.isExternalReference()) {
if (variables == null) {
return new Reference(es);
}
Expression value = variables.get(es);
if (value == null) {
if (es.getGroupSymbol().getSchema() == null) {
String grpName = es.getGroupSymbol().getName();
if (grpName.equals(ProcedureReservedWords.CHANGING)) {
// $NON-NLS-1$
Assertion.failed("Changing value should not be null");
}
}
} else if (value instanceof Constant) {
if (value.getType() == type) {
return value;
}
try {
return new Constant(FunctionMethods.convert(context, ((Constant) value).getValue(), DataTypeManager.getDataTypeName(type)), es.getType());
} catch (FunctionExecutionException e) {
throw new QueryValidatorException(e);
}
}
return new Reference(es);
}
return expression;
}
boolean isBindEligible = true;
if (expression instanceof AggregateSymbol) {
expression = rewriteExpression((AggregateSymbol) expression);
} else if (expression instanceof Function) {
isBindEligible = !isConstantConvert(expression);
expression = rewriteFunction((Function) expression);
} else if (expression instanceof CaseExpression) {
expression = rewriteCaseExpression((CaseExpression) expression);
} else if (expression instanceof SearchedCaseExpression) {
expression = rewriteCaseExpression((SearchedCaseExpression) expression);
} else if (expression instanceof ScalarSubquery) {
ScalarSubquery subquery = (ScalarSubquery) expression;
if (subquery.shouldEvaluate() && processing) {
return new Constant(evaluator.evaluate(subquery, null), subquery.getType());
}
rewriteSubqueryContainer(subquery, true);
if (!RelationalNodeUtil.shouldExecute(subquery.getCommand(), false, true)) {
return new Constant(null, subquery.getType());
}
if (subquery.getCommand().getProcessorPlan() == null) {
addImplicitLimit(subquery, 2);
}
return expression;
} else if (expression instanceof ExpressionSymbol) {
expression = rewriteExpressionDirect(((ExpressionSymbol) expression).getExpression());
} else if (expression instanceof Criteria) {
expression = rewriteCriteria((Criteria) expression);
} else if (expression instanceof XMLSerialize) {
rewriteExpressions(expression);
XMLSerialize serialize = (XMLSerialize) expression;
if (isNull(serialize.getExpression())) {
return new Constant(null, serialize.getType());
}
if (serialize.getDeclaration() == null && serialize.isDocument()) {
if ((serialize.getVersion() != null && !serialize.getVersion().equals("1.0"))) {
// $NON-NLS-1$
serialize.setDeclaration(true);
} else if (serialize.getEncoding() != null) {
Charset encoding = Charset.forName(serialize.getEncoding());
if (!encoding.equals(Charset.forName("UTF-8")) && !encoding.equals(Charset.forName("UTF-16"))) {
// $NON-NLS-1$ //$NON-NLS-2$
serialize.setDeclaration(true);
}
}
}
} else if (expression instanceof XMLCast) {
XMLCast cast = (XMLCast) expression;
if (cast.getType() == DefaultDataClasses.XML) {
XMLQuery xmlQuery = new XMLQuery();
// $NON-NLS-1$
xmlQuery.setXquery("$i");
// $NON-NLS-1$
xmlQuery.setPassing(Arrays.asList(new DerivedColumn("i", cast.getExpression())));
xmlQuery.compileXqueryExpression();
return xmlQuery;
}
} else {
rewriteExpressions(expression);
}
if (!processing) {
if (!EvaluatableVisitor.isFullyEvaluatable(expression, true)) {
return expression;
}
} else if (!(expression instanceof Reference) && !EvaluatableVisitor.isEvaluatable(expression, EvaluationLevel.PROCESSING)) {
return expression;
}
return evaluate(expression, isBindEligible);
}
use of org.teiid.api.exception.query.QueryValidatorException in project teiid by teiid.
the class QueryRewriter method rewriteInsert.
private Command rewriteInsert(Insert insert) throws TeiidComponentException, TeiidProcessingException {
Command c = rewriteInsertForWriteThrough(insert);
if (c != null) {
return c;
}
UpdateInfo info = insert.getUpdateInfo();
if (info != null && info.isInherentInsert()) {
// TODO: update error messages
UpdateMapping mapping = info.findInsertUpdateMapping(insert, true);
if (mapping == null) {
throw new QueryValidatorException(QueryPlugin.Event.TEIID30375, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30375, insert.getVariables()));
}
Map<ElementSymbol, ElementSymbol> symbolMap = mapping.getUpdatableViewSymbols();
List<ElementSymbol> mappedSymbols = new ArrayList<ElementSymbol>(insert.getVariables().size());
for (ElementSymbol symbol : insert.getVariables()) {
mappedSymbols.add(symbolMap.get(symbol));
}
insert.setVariables(mappedSymbols);
insert.setGroup(mapping.getGroup().clone());
insert.setUpdateInfo(ProcedureContainerResolver.getUpdateInfo(insert.getGroup(), metadata, Command.TYPE_INSERT, true));
return rewriteInsert(insert);
}
if (insert.getQueryExpression() != null) {
insert.setQueryExpression((QueryCommand) rewriteCommand(insert.getQueryExpression(), true));
return correctDatatypes(insert);
}
// Evaluate any function / constant trees in the insert values
List expressions = insert.getValues();
List evalExpressions = new ArrayList(expressions.size());
Iterator expIter = expressions.iterator();
boolean preserveUnknownOld = preserveUnknown;
preserveUnknown = true;
while (expIter.hasNext()) {
Expression exp = (Expression) expIter.next();
if (processing && exp instanceof ExpressionSymbol) {
// expression symbols that were created in the PlanToProcessesConverter
evalExpressions.add(evaluate(exp, true));
} else {
evalExpressions.add(rewriteExpressionDirect(exp));
}
}
preserveUnknown = preserveUnknownOld;
insert.setValues(evalExpressions);
return insert;
}
use of org.teiid.api.exception.query.QueryValidatorException in project teiid by teiid.
the class QueryRewriter method rewriteInherentUpdate.
private Command rewriteInherentUpdate(Update update, UpdateInfo info) throws QueryValidatorException, QueryMetadataException, TeiidComponentException, QueryResolverException, TeiidProcessingException {
UpdateMapping mapping = info.findUpdateMapping(update.getChangeList().getClauseMap().keySet(), false);
if (mapping == null) {
throw new QueryValidatorException(QueryPlugin.Event.TEIID30376, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30376, update.getChangeList().getClauseMap().keySet()));
}
Map<ElementSymbol, ElementSymbol> symbolMap = mapping.getUpdatableViewSymbols();
if (info.isSimple()) {
Collection<ElementSymbol> elements = getAllElementsUsed(update, update.getGroup());
UpdateMapping fullMapping = info.findUpdateMapping(elements, false);
if (fullMapping != null) {
update.setGroup(mapping.getGroup().clone());
for (SetClause clause : update.getChangeList().getClauses()) {
clause.setSymbol(symbolMap.get(clause.getSymbol()));
}
// TODO: properly handle correlated references
DeepPostOrderNavigator.doVisit(update, new ExpressionMappingVisitor(symbolMap, true));
if (info.getViewDefinition().getCriteria() != null) {
update.setCriteria(Criteria.combineCriteria(update.getCriteria(), (Criteria) info.getViewDefinition().getCriteria().clone()));
}
// resolve
update.setUpdateInfo(ProcedureContainerResolver.getUpdateInfo(update.getGroup(), metadata, Command.TYPE_UPDATE, true));
return rewriteUpdate(update);
}
}
Query query = (Query) info.getViewDefinition().clone();
query.setOrderBy(null);
SymbolMap expressionMapping = SymbolMap.createSymbolMap(update.getGroup(), query.getProjectedSymbols(), metadata);
SetClauseList setClauseList = (SetClauseList) update.getChangeList().clone();
GroupSymbol varGroup = getVarGroup(update);
ArrayList<Expression> selectSymbols = mapChangeList(setClauseList, symbolMap, varGroup);
query.setSelect(new Select(selectSymbols));
ExpressionMappingVisitor emv = new ExpressionMappingVisitor(expressionMapping.asMap(), true);
PostOrderNavigator.doVisit(query.getSelect(), emv);
Criteria crit = update.getCriteria();
if (crit != null) {
PostOrderNavigator.doVisit(crit, emv);
query.setCriteria(Criteria.combineCriteria(query.getCriteria(), crit));
}
GroupSymbol group = mapping.getGroup();
String correlationName = mapping.getCorrelatedName().getName();
return createUpdateProcedure(update, query, group, correlationName, setClauseList, varGroup, null);
}
Aggregations