use of io.crate.analyze.symbol.Symbol in project crate by crate.
the class InsertFromValuesAnalyzer method analyze.
public AnalyzedStatement analyze(InsertFromValues node, Analysis analysis) {
DocTableInfo tableInfo = schemas.getWritableTable(TableIdent.of(node.table(), analysis.sessionContext().defaultSchema()));
Operation.blockedRaiseException(tableInfo, Operation.INSERT);
DocTableRelation tableRelation = new DocTableRelation(tableInfo);
FieldProvider fieldProvider = new NameFieldProvider(tableRelation);
Function<ParameterExpression, Symbol> convertParamFunction = analysis.parameterContext();
ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(functions, analysis.sessionContext(), convertParamFunction, fieldProvider, null);
ExpressionAnalysisContext expressionAnalysisContext = new ExpressionAnalysisContext();
expressionAnalyzer.setResolveFieldsOperation(Operation.INSERT);
ValuesResolver valuesResolver = new ValuesResolver(tableRelation);
ExpressionAnalyzer valuesAwareExpressionAnalyzer = new ValuesAwareExpressionAnalyzer(functions, analysis.sessionContext(), convertParamFunction, fieldProvider, valuesResolver);
InsertFromValuesAnalyzedStatement statement = new InsertFromValuesAnalyzedStatement(tableInfo, analysis.parameterContext().numBulkParams());
handleInsertColumns(node, node.maxValuesLength(), statement);
Set<Reference> allReferencedReferences = new HashSet<>();
for (GeneratedReference reference : tableInfo.generatedColumns()) {
allReferencedReferences.addAll(reference.referencedReferences());
}
ReferenceToLiteralConverter.Context referenceToLiteralContext = new ReferenceToLiteralConverter.Context(statement.columns(), allReferencedReferences);
ValueNormalizer valuesNormalizer = new ValueNormalizer(schemas);
EvaluatingNormalizer normalizer = new EvaluatingNormalizer(functions, RowGranularity.CLUSTER, ReplaceMode.COPY, null, tableRelation);
analyzeColumns(statement.tableInfo(), statement.columns());
for (ValuesList valuesList : node.valuesLists()) {
analyzeValues(tableRelation, valuesNormalizer, normalizer, expressionAnalyzer, expressionAnalysisContext, analysis.transactionContext(), valuesResolver, valuesAwareExpressionAnalyzer, valuesList, node.onDuplicateKeyAssignments(), statement, analysis.parameterContext(), referenceToLiteralContext);
}
return statement;
}
use of io.crate.analyze.symbol.Symbol in project crate by crate.
the class InsertFromValuesAnalyzer method processGeneratedExpressions.
private void processGeneratedExpressions(GeneratedExpressionContext context) {
List<ColumnIdent> primaryKey = context.analyzedStatement.tableInfo().primaryKey();
for (GeneratedReference reference : context.tableRelation.tableInfo().generatedColumns()) {
Symbol valueSymbol = TO_LITERAL_CONVERTER.process(reference.generatedExpression(), context.referenceToLiteralContext);
valueSymbol = context.normalizer.normalize(valueSymbol, context.transactionContext);
if (valueSymbol.symbolType() == SymbolType.LITERAL) {
Object value = ((Input) valueSymbol).value();
if (primaryKey.contains(reference.ident().columnIdent()) && context.analyzedStatement.columns().indexOf(reference) == -1) {
int idx = primaryKey.indexOf(reference.ident().columnIdent());
addPrimaryKeyValue(idx, value, context.primaryKeyValues);
}
ColumnIdent routingColumn = context.analyzedStatement.tableInfo().clusteredBy();
if (routingColumn != null && routingColumn.equals(reference.ident().columnIdent())) {
context.routingValue = extractRoutingValue(routingColumn, value, context.analyzedStatement);
}
if (context.tableRelation.tableInfo().isPartitioned() && context.tableRelation.tableInfo().partitionedByColumns().contains(reference)) {
addGeneratedPartitionedColumnValue(reference.ident().columnIdent(), value, context.analyzedStatement.currentPartitionMap());
} else {
context.insertValues = addGeneratedColumnValue(context.analyzedStatement, reference, value, context.insertValues);
}
}
}
}
use of io.crate.analyze.symbol.Symbol in project crate by crate.
the class OrderBy method subset.
/**
* Create a new OrderBy with symbols that match the predicate
*/
@Nullable
public OrderBy subset(Predicate<? super Symbol> predicate) {
List<Integer> subSet = new ArrayList<>();
Integer i = 0;
for (Symbol orderBySymbol : orderBySymbols) {
if (predicate.apply(orderBySymbol)) {
subSet.add(i);
}
i++;
}
if (subSet.isEmpty()) {
return null;
}
return subset(subSet);
}
use of io.crate.analyze.symbol.Symbol in project crate by crate.
the class QuerySpec method castOutputs.
/**
* Tries to cast the outputs to the given types. Types might contain Null values, which indicates to skip that
* position. The iterator needs to return exactly the same number of types as there are outputs.
*
* @param types an iterable providing the types
* @return -1 if all casts where successfully applied or the position of the failed cast
*/
public int castOutputs(Iterator<DataType> types) {
int i = 0;
ListIterator<Symbol> outputsIt = outputs.listIterator();
while (types.hasNext() && outputsIt.hasNext()) {
DataType targetType = types.next();
assert targetType != null : "targetType must not be null";
Symbol output = outputsIt.next();
DataType sourceType = output.valueType();
if (!sourceType.equals(targetType)) {
if (sourceType.isConvertableTo(targetType)) {
Symbol castFunction = CastFunctionResolver.generateCastFunction(output, targetType, false);
if (groupBy.isPresent()) {
Collections.replaceAll(groupBy.get(), output, castFunction);
}
if (orderBy.isPresent()) {
Collections.replaceAll(orderBy.get().orderBySymbols(), output, castFunction);
}
outputsIt.set(castFunction);
} else if (!targetType.equals(DataTypes.UNDEFINED)) {
return i;
}
}
i++;
}
assert i == outputs.size() : "i must be equal to outputs.size()";
return -1;
}
use of io.crate.analyze.symbol.Symbol in project crate by crate.
the class RelationAnalyzer method symbolFromSelectOutputReferenceOrExpression.
/**
* <h2>resolve expression by also taking alias and ordinal-reference into account</h2>
* <p>
* <p>
* in group by or order by clauses it is possible to reference anything in the
* select list by using a number or alias
* </p>
* <p>
* These are allowed:
* <pre>
* select name as n ... order by n
* select name ... order by 1
* select name ... order by other_column
* </pre>
*/
private Symbol symbolFromSelectOutputReferenceOrExpression(Expression expression, SelectAnalysis selectAnalysis, String clause, ExpressionAnalyzer expressionAnalyzer, ExpressionAnalysisContext expressionAnalysisContext) {
Symbol symbol;
if (expression instanceof QualifiedNameReference) {
List<String> parts = ((QualifiedNameReference) expression).getName().getParts();
if (parts.size() == 1) {
symbol = getOneOrAmbiguous(selectAnalysis.outputMultiMap(), Iterables.getOnlyElement(parts));
if (symbol != null) {
return symbol;
}
}
}
symbol = expressionAnalyzer.convert(expression, expressionAnalysisContext);
if (symbol.symbolType().isValueSymbol()) {
Literal longLiteral;
try {
longLiteral = io.crate.analyze.symbol.Literal.convert(symbol, DataTypes.LONG);
} catch (ClassCastException | IllegalArgumentException e) {
throw new UnsupportedOperationException(String.format(Locale.ENGLISH, "Cannot use %s in %s clause", SymbolPrinter.INSTANCE.printSimple(symbol), clause));
}
symbol = ordinalOutputReference(selectAnalysis.outputSymbols(), longLiteral, clause);
}
return symbol;
}
Aggregations