use of io.crate.expression.symbol.Literal in project crate by crate.
the class TableFunction method build.
@Override
public ExecutionPlan build(PlannerContext plannerContext, Set<PlanHint> planHints, ProjectionBuilder projectionBuilder, int limit, int offset, @Nullable OrderBy order, @Nullable Integer pageSizeHint, Row params, SubQueryResults subQueryResults) {
List<Symbol> args = relation.function().arguments();
ArrayList<Literal<?>> functionArguments = new ArrayList<>(args.size());
EvaluatingNormalizer normalizer = new EvaluatingNormalizer(plannerContext.nodeContext(), RowGranularity.CLUSTER, null, relation);
var binder = new SubQueryAndParamBinder(params, subQueryResults).andThen(x -> normalizer.normalize(x, plannerContext.transactionContext()));
for (Symbol arg : args) {
// It's not possible to use columns as argument to a table function, so it's safe to evaluate at this point.
functionArguments.add(Literal.ofUnchecked(arg.valueType(), SymbolEvaluator.evaluate(plannerContext.transactionContext(), plannerContext.nodeContext(), arg, params, subQueryResults)));
}
TableFunctionCollectPhase collectPhase = new TableFunctionCollectPhase(plannerContext.jobId(), plannerContext.nextExecutionPhaseId(), plannerContext.handlerNode(), relation.functionImplementation(), functionArguments, Lists2.map(toCollect, binder), binder.apply(where.queryOrFallback()));
return new Collect(collectPhase, TopN.NO_LIMIT, 0, toCollect.size(), TopN.NO_LIMIT, null);
}
use of io.crate.expression.symbol.Literal in project crate by crate.
the class CompoundLiteralTest method testNestedArrayLiteral.
@Test
public void testNestedArrayLiteral() throws Exception {
Map<String, DataType<?>> expected = Map.of("'string'", DataTypes.STRING, "0", DataTypes.INTEGER, "1.8", DataTypes.DOUBLE, "TRUE", DataTypes.BOOLEAN);
for (Map.Entry<String, DataType<?>> entry : expected.entrySet()) {
Symbol nestedArraySymbol = analyzeExpression("[[" + entry.getKey() + "]]");
assertThat(nestedArraySymbol, Matchers.instanceOf(Literal.class));
Literal<?> nestedArray = (Literal<?>) nestedArraySymbol;
assertThat(nestedArray.valueType(), is(new ArrayType<>(new ArrayType<>(entry.getValue()))));
}
}
use of io.crate.expression.symbol.Literal in project crate by crate.
the class RelationAnalyzer method visitValues.
@Override
public AnalyzedRelation visitValues(Values values, StatementAnalysisContext context) {
var expressionAnalyzer = new ExpressionAnalyzer(context.transactionContext(), nodeCtx, context.paramTyeHints(), FieldProvider.UNSUPPORTED, new SubqueryAnalyzer(this, context));
var expressionAnalysisContext = new ExpressionAnalysisContext(context.sessionContext());
// prevent normalization of the values array, otherwise the value literals are converted to an array literal
// and a special per-value-literal casting logic won't be executed (e.g. FloatLiteral.cast())
expressionAnalysisContext.allowEagerNormalize(false);
java.util.function.Function<Expression, Symbol> expressionToSymbol = e -> expressionAnalyzer.convert(e, expressionAnalysisContext);
// There is a first pass to convert expressions from row oriented format:
// `[[1, a], [2, b]]` to columns `[[1, 2], [a, b]]`
//
// At the same time we determine the column type with the highest precedence,
// so that we don't fail with slight type miss-matches (long vs. int)
List<ValuesList> rows = values.rows();
assert rows.size() > 0 : "Parser grammar enforces at least 1 row";
ValuesList firstRow = rows.get(0);
int numColumns = firstRow.values().size();
ArrayList<List<Symbol>> columns = new ArrayList<>();
ArrayList<DataType<?>> targetTypes = new ArrayList<>(numColumns);
var parentOutputColumns = context.parentOutputColumns();
for (int c = 0; c < numColumns; c++) {
ArrayList<Symbol> columnValues = new ArrayList<>();
DataType<?> targetType;
boolean usePrecedence = true;
if (parentOutputColumns.size() > c) {
targetType = parentOutputColumns.get(c).valueType();
usePrecedence = false;
} else {
targetType = DataTypes.UNDEFINED;
}
for (int r = 0; r < rows.size(); r++) {
List<Expression> row = rows.get(r).values();
if (row.size() != numColumns) {
throw new IllegalArgumentException("VALUES lists must all be the same length. " + "Found row with " + numColumns + " items and another with " + columns.size() + " items");
}
Symbol cell = expressionToSymbol.apply(row.get(c));
columnValues.add(cell);
var cellType = cell.valueType();
if (// skip first cell, we don't have to check for self-conversion
r > 0 && !cellType.isConvertableTo(targetType, false) && targetType.id() != DataTypes.UNDEFINED.id()) {
throw new IllegalArgumentException("The types of the columns within VALUES lists must match. " + "Found `" + targetType + "` and `" + cellType + "` at position: " + c);
}
if (usePrecedence && cellType.precedes(targetType)) {
targetType = cellType;
} else if (targetType == DataTypes.UNDEFINED) {
targetType = cellType;
}
}
targetTypes.add(targetType);
columns.add(columnValues);
}
var normalizer = EvaluatingNormalizer.functionOnlyNormalizer(nodeCtx, f -> f.isDeterministic());
ArrayList<Symbol> arrays = new ArrayList<>(columns.size());
for (int c = 0; c < numColumns; c++) {
DataType<?> targetType = targetTypes.get(c);
ArrayType<?> arrayType = new ArrayType<>(targetType);
List<Symbol> columnValues = Lists2.map(columns.get(c), s -> normalizer.normalize(s.cast(targetType), context.transactionContext()));
arrays.add(new Function(ArrayFunction.SIGNATURE, columnValues, arrayType));
}
FunctionImplementation implementation = nodeCtx.functions().getQualified(ValuesFunction.SIGNATURE, Symbols.typeView(arrays), RowType.EMPTY);
Function function = new Function(implementation.signature(), arrays, RowType.EMPTY);
TableFunctionImplementation<?> tableFunc = TableFunctionFactory.from(implementation);
TableFunctionRelation relation = new TableFunctionRelation(tableFunc, function);
context.startRelation();
context.currentRelationContext().addSourceRelation(relation);
context.endRelation();
return relation;
}
use of io.crate.expression.symbol.Literal in project crate by crate.
the class WhereClauseAnalyzer method tieBreakPartitionQueries.
@Nullable
private static PartitionResult tieBreakPartitionQueries(EvaluatingNormalizer normalizer, Map<Symbol, List<Literal>> queryPartitionMap, CoordinatorTxnCtx coordinatorTxnCtx) throws UnsupportedOperationException {
/*
* Got multiple normalized queries which all could match.
* This might be the case if one partition resolved to null
*
* e.g.
*
* p = 1 and x = 2
*
* might lead to
*
* null and x = 2
* true and x = 2
*
* At this point it is unknown if they really match.
* In order to figure out if they could potentially match all conditions involving references are now set to true
*
* null and true -> can't match
* true and true -> can match, can use this query + partition
*
* If there is still more than 1 query that can match it's not possible to execute the query :(
*/
List<Tuple<Symbol, List<Literal>>> canMatch = new ArrayList<>();
for (Map.Entry<Symbol, List<Literal>> entry : queryPartitionMap.entrySet()) {
Symbol query = entry.getKey();
List<Literal> partitions = entry.getValue();
Symbol normalized = normalizer.normalize(ScalarsAndRefsToTrue.rewrite(query), coordinatorTxnCtx);
assert normalized instanceof Literal : "after normalization and replacing all reference occurrences with true there must only be a literal left";
Object value = ((Literal) normalized).value();
if (value != null && (Boolean) value) {
canMatch.add(new Tuple<>(query, partitions));
}
}
if (canMatch.size() == 1) {
Tuple<Symbol, List<Literal>> symbolListTuple = canMatch.get(0);
return new PartitionResult(symbolListTuple.v1(), Lists2.map(symbolListTuple.v2(), literal -> nullOrString(literal.value())));
}
return null;
}
use of io.crate.expression.symbol.Literal in project crate by crate.
the class SelectivityFunctions method eqSelectivity.
private static double eqSelectivity(Symbol leftArg, Symbol rightArg, Stats stats, @Nullable Row params) {
ColumnIdent column = getColumn(leftArg);
if (column == null) {
return DEFAULT_EQ_SEL;
}
var columnStats = stats.statsByColumn().get(column);
if (columnStats == null) {
return DEFAULT_EQ_SEL;
}
if (rightArg instanceof ParameterSymbol && params != null) {
var value = params.get(((ParameterSymbol) rightArg).index());
return eqSelectivityFromValueAndStats(value, columnStats);
}
if (rightArg instanceof Literal) {
return eqSelectivityFromValueAndStats(((Literal) rightArg).value(), columnStats);
}
return 1.0 / columnStats.approxDistinct();
}
Aggregations