use of io.crate.common.collections.Lists2.mapTail in project crate by crate.
the class WhereClauseAnalyzer method resolvePartitions.
public static PartitionResult resolvePartitions(Symbol query, DocTableInfo tableInfo, CoordinatorTxnCtx coordinatorTxnCtx, NodeContext nodeCtx) {
assert tableInfo.isPartitioned() : "table must be partitioned in order to resolve partitions";
assert !tableInfo.partitions().isEmpty() : "table must have at least one partition";
PartitionReferenceResolver partitionReferenceResolver = preparePartitionResolver(tableInfo.partitionedByColumns());
EvaluatingNormalizer normalizer = new EvaluatingNormalizer(nodeCtx, RowGranularity.PARTITION, partitionReferenceResolver, null);
Symbol normalized;
Map<Symbol, List<Literal>> queryPartitionMap = new HashMap<>();
for (PartitionName partitionName : tableInfo.partitions()) {
for (PartitionExpression partitionExpression : partitionReferenceResolver.expressions()) {
partitionExpression.setNextRow(partitionName);
}
normalized = normalizer.normalize(query, coordinatorTxnCtx);
assert normalized != null : "normalizing a query must not return null";
if (normalized.equals(query)) {
// no partition columns inside the where clause
return new PartitionResult(query, Collections.emptyList());
}
boolean canMatch = WhereClause.canMatch(normalized);
if (canMatch) {
List<Literal> partitions = queryPartitionMap.get(normalized);
if (partitions == null) {
partitions = new ArrayList<>();
queryPartitionMap.put(normalized, partitions);
}
partitions.add(Literal.of(partitionName.asIndexName()));
}
}
if (queryPartitionMap.size() == 1) {
Map.Entry<Symbol, List<Literal>> entry = Iterables.getOnlyElement(queryPartitionMap.entrySet());
return new PartitionResult(entry.getKey(), Lists2.map(entry.getValue(), literal -> nullOrString(literal.value())));
} else if (queryPartitionMap.size() > 0) {
PartitionResult partitionResult = tieBreakPartitionQueries(normalizer, queryPartitionMap, coordinatorTxnCtx);
return partitionResult == null ? // the query will then be evaluated correctly within each partition to see whether it matches or not
new PartitionResult(query, Lists2.map(tableInfo.partitions(), PartitionName::asIndexName)) : partitionResult;
} else {
return new PartitionResult(Literal.BOOLEAN_FALSE, Collections.emptyList());
}
}
use of io.crate.common.collections.Lists2.mapTail in project crate by crate.
the class CreateSnapshotAnalyzer method analyze.
public AnalyzedCreateSnapshot analyze(CreateSnapshot<Expression> createSnapshot, ParamTypeHints paramTypeHints, CoordinatorTxnCtx txnCtx) {
String repositoryName = createSnapshot.name().getPrefix().map(name -> {
validateRepository(name);
return name.toString();
}).orElseThrow(() -> new IllegalArgumentException("Snapshot must be specified by \"<repository_name>\".\"<snapshot_name>\""));
String snapshotName = createSnapshot.name().getSuffix();
Snapshot snapshot = new Snapshot(repositoryName, new SnapshotId(snapshotName, UUIDs.dirtyUUID().toString()));
var exprCtx = new ExpressionAnalysisContext(txnCtx.sessionContext());
var exprAnalyzerWithoutFields = new ExpressionAnalyzer(txnCtx, nodeCtx, paramTypeHints, FieldProvider.UNSUPPORTED, null);
var exprAnalyzerWithFieldsAsString = new ExpressionAnalyzer(txnCtx, nodeCtx, paramTypeHints, FieldProvider.FIELDS_AS_LITERAL, null);
List<Table<Symbol>> tables = Lists2.map(createSnapshot.tables(), (table) -> table.map(x -> exprAnalyzerWithFieldsAsString.convert(x, exprCtx)));
GenericProperties<Symbol> properties = createSnapshot.properties().map(x -> exprAnalyzerWithoutFields.convert(x, exprCtx));
return new AnalyzedCreateSnapshot(snapshot, tables, properties);
}
use of io.crate.common.collections.Lists2.mapTail in project crate by crate.
the class DocIndexMetadataTest method testExtractColumnDefinitions.
@Test
public void testExtractColumnDefinitions() throws Exception {
// @formatter:off
XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("_meta").field("primary_keys", "integerIndexed").endObject().startObject("properties").startObject("integerIndexed").field("type", "integer").endObject().startObject("integerIndexedBWC").field("type", "integer").field("index", "not_analyzed").endObject().startObject("integerNotIndexed").field("type", "integer").field("index", "false").endObject().startObject("integerNotIndexedBWC").field("type", "integer").field("index", "no").endObject().startObject("stringNotIndexed").field("type", "string").field("index", "false").endObject().startObject("stringNotIndexedBWC").field("type", "string").field("index", "no").endObject().startObject("stringNotAnalyzed").field("type", "keyword").endObject().startObject("stringNotAnalyzedBWC").field("type", "string").field("index", "not_analyzed").endObject().startObject("stringAnalyzed").field("type", "text").field("analyzer", "standard").endObject().startObject("stringAnalyzedBWC").field("type", "string").field("index", "analyzed").field("analyzer", "standard").endObject().startObject("person").startObject("properties").startObject("first_name").field("type", "string").endObject().startObject("birthday").field("type", "date").endObject().endObject().endObject().endObject().endObject();
// @formatter:on
IndexMetadata metadata = getIndexMetadata("test1", builder);
DocIndexMetadata md = newMeta(metadata, "test1");
assertThat(md.columns().size(), is(11));
assertThat(md.references().size(), is(23));
Reference birthday = md.references().get(new ColumnIdent("person", "birthday"));
assertThat(birthday.valueType(), is(DataTypes.TIMESTAMPZ));
assertThat(birthday.indexType(), is(Reference.IndexType.PLAIN));
assertThat(birthday.defaultExpression(), is(nullValue()));
Reference integerIndexed = md.references().get(new ColumnIdent("integerIndexed"));
assertThat(integerIndexed.indexType(), is(Reference.IndexType.PLAIN));
assertThat(integerIndexed.defaultExpression(), is(nullValue()));
Reference integerIndexedBWC = md.references().get(new ColumnIdent("integerIndexedBWC"));
assertThat(integerIndexedBWC.indexType(), is(Reference.IndexType.PLAIN));
assertThat(integerIndexedBWC.defaultExpression(), is(nullValue()));
Reference integerNotIndexed = md.references().get(new ColumnIdent("integerNotIndexed"));
assertThat(integerNotIndexed.indexType(), is(Reference.IndexType.NONE));
assertThat(integerNotIndexed.defaultExpression(), is(nullValue()));
Reference integerNotIndexedBWC = md.references().get(new ColumnIdent("integerNotIndexedBWC"));
assertThat(integerNotIndexedBWC.indexType(), is(Reference.IndexType.NONE));
assertThat(integerNotIndexedBWC.defaultExpression(), is(nullValue()));
Reference stringNotIndexed = md.references().get(new ColumnIdent("stringNotIndexed"));
assertThat(stringNotIndexed.indexType(), is(Reference.IndexType.NONE));
assertThat(stringNotIndexed.defaultExpression(), is(nullValue()));
Reference stringNotIndexedBWC = md.references().get(new ColumnIdent("stringNotIndexedBWC"));
assertThat(stringNotIndexedBWC.indexType(), is(Reference.IndexType.NONE));
assertThat(stringNotIndexedBWC.defaultExpression(), is(nullValue()));
Reference stringNotAnalyzed = md.references().get(new ColumnIdent("stringNotAnalyzed"));
assertThat(stringNotAnalyzed.indexType(), is(Reference.IndexType.PLAIN));
assertThat(stringNotAnalyzed.defaultExpression(), is(nullValue()));
Reference stringNotAnalyzedBWC = md.references().get(new ColumnIdent("stringNotAnalyzedBWC"));
assertThat(stringNotAnalyzedBWC.indexType(), is(Reference.IndexType.PLAIN));
assertThat(stringNotAnalyzedBWC.defaultExpression(), is(nullValue()));
Reference stringAnalyzed = md.references().get(new ColumnIdent("stringAnalyzed"));
assertThat(stringAnalyzed.indexType(), is(Reference.IndexType.FULLTEXT));
assertThat(stringAnalyzed.defaultExpression(), is(nullValue()));
Reference stringAnalyzedBWC = md.references().get(new ColumnIdent("stringAnalyzedBWC"));
assertThat(stringAnalyzedBWC.indexType(), is(Reference.IndexType.FULLTEXT));
assertThat(stringAnalyzedBWC.defaultExpression(), is(nullValue()));
assertThat(Lists2.map(md.references().values(), r -> r.column().fqn()), containsInAnyOrder("_doc", "_fetchid", "_id", "_raw", "_score", "_uid", "_version", "_docid", "_seq_no", "_primary_term", "integerIndexed", "integerIndexedBWC", "integerNotIndexed", "integerNotIndexedBWC", "person", "person.birthday", "person.first_name", "stringAnalyzed", "stringAnalyzedBWC", "stringNotAnalyzed", "stringNotAnalyzedBWC", "stringNotIndexed", "stringNotIndexedBWC"));
}
use of io.crate.common.collections.Lists2.mapTail in project crate by crate.
the class SQLPrinter method print.
public static String print(QueriedSelectRelation relation) {
StringBuilder sb = new StringBuilder();
sb.append("SELECT ");
sb.append(Lists2.joinOn(", ", relation.outputs(), x -> x.toString(Style.QUALIFIED)));
if (relation.where() != Literal.BOOLEAN_TRUE) {
sb.append(" WHERE ");
sb.append(relation.where().toString(Style.QUALIFIED));
}
if (!relation.groupBy().isEmpty()) {
sb.append(" GROUP BY ");
sb.append(Lists2.joinOn(", ", relation.groupBy(), x -> x.toString(Style.QUALIFIED)));
}
Symbol having = relation.having();
if (having != null) {
sb.append(" HAVING ");
sb.append(having.toString(Style.QUALIFIED));
}
OrderBy orderBy = relation.orderBy();
if (orderBy != null) {
sb.append(" ORDER BY ");
process(orderBy, sb);
}
Symbol limit = relation.limit();
if (limit != null) {
sb.append(" LIMIT ");
sb.append(print(limit));
}
Symbol offset = relation.offset();
if (offset != null) {
sb.append(" OFFSET ");
sb.append(print(offset));
}
return sb.toString();
}
use of io.crate.common.collections.Lists2.mapTail in project crate by crate.
the class AbstractWindowFunctionTest method assertEvaluate.
@SuppressWarnings("unchecked")
protected <T> void assertEvaluate(String functionExpression, Matcher<T> expectedValue, List<ColumnIdent> rowsColumnDescription, Object[]... inputRows) throws Throwable {
performInputSanityChecks(inputRows);
Symbol normalizedFunctionSymbol = sqlExpressions.normalize(sqlExpressions.asSymbol(functionExpression));
assertThat(normalizedFunctionSymbol, instanceOf(io.crate.expression.symbol.WindowFunction.class));
var windowFunctionSymbol = (io.crate.expression.symbol.WindowFunction) normalizedFunctionSymbol;
ReferenceResolver<InputCollectExpression> referenceResolver = r -> new InputCollectExpression(rowsColumnDescription.indexOf(r.column()));
var sourceSymbols = Lists2.map(rowsColumnDescription, x -> sqlExpressions.normalize(sqlExpressions.asSymbol(x.sqlFqn())));
ensureInputRowsHaveCorrectType(sourceSymbols, inputRows);
var argsCtx = inputFactory.ctxForRefs(txnCtx, referenceResolver);
argsCtx.add(windowFunctionSymbol.arguments());
FunctionImplementation impl = sqlExpressions.nodeCtx.functions().getQualified(windowFunctionSymbol, txnCtx.sessionSettings().searchPath());
assert impl instanceof WindowFunction || impl instanceof AggregationFunction : "Got " + impl + " but expected a window function";
WindowFunction windowFunctionImpl;
if (impl instanceof AggregationFunction) {
windowFunctionImpl = new AggregateToWindowFunctionAdapter((AggregationFunction) impl, new ExpressionsInput<>(Literal.BOOLEAN_TRUE, List.of()), Version.CURRENT, RamAccounting.NO_ACCOUNTING, memoryManager, Version.CURRENT);
} else {
windowFunctionImpl = (WindowFunction) impl;
}
int numCellsInSourceRows = inputRows[0].length;
var windowDef = windowFunctionSymbol.windowDefinition();
var partitionOrderBy = windowDef.partitions().isEmpty() ? null : new OrderBy(windowDef.partitions());
Comparator<Object[]> cmpOrderBy = createComparator(() -> inputFactory.ctxForRefs(txnCtx, referenceResolver), windowDef.orderBy());
InputColumns.SourceSymbols inputColSources = new InputColumns.SourceSymbols(sourceSymbols);
var mappedWindowDef = windowDef.map(s -> InputColumns.create(s, inputColSources));
BatchIterator<Row> iterator = WindowFunctionBatchIterator.of(InMemoryBatchIterator.of(Arrays.stream(inputRows).map(RowN::new).collect(Collectors.toList()), SENTINEL, true), new IgnoreRowAccounting(), WindowProjector.createComputeStartFrameBoundary(numCellsInSourceRows, txnCtx, sqlExpressions.nodeCtx, mappedWindowDef, cmpOrderBy), WindowProjector.createComputeEndFrameBoundary(numCellsInSourceRows, txnCtx, sqlExpressions.nodeCtx, mappedWindowDef, cmpOrderBy), createComparator(() -> inputFactory.ctxForRefs(txnCtx, referenceResolver), partitionOrderBy), cmpOrderBy, numCellsInSourceRows, () -> 1, Runnable::run, List.of(windowFunctionImpl), argsCtx.expressions(), new Boolean[] { windowFunctionSymbol.ignoreNulls() }, argsCtx.topLevelInputs().toArray(new Input[0]));
List<Object> actualResult;
try {
actualResult = BatchIterators.collect(iterator, Collectors.mapping(row -> row.get(numCellsInSourceRows), Collectors.toList())).get(5, TimeUnit.SECONDS);
} catch (ExecutionException e) {
throw e.getCause();
}
assertThat((T) actualResult, expectedValue);
}
Aggregations