use of io.crate.metadata.CoordinatorTxnCtx in project crate by crate.
the class InternalCountOperationTest method testCount.
@Test
public void testCount() throws Exception {
execute("create table t (name string) clustered into 1 shards with (number_of_replicas = 0)");
ensureYellow();
execute("insert into t (name) values ('Marvin'), ('Arthur'), ('Trillian')");
execute("refresh table t");
CountOperation countOperation = internalCluster().getDataNodeInstance(CountOperation.class);
ClusterService clusterService = internalCluster().getDataNodeInstance(ClusterService.class);
CoordinatorTxnCtx txnCtx = CoordinatorTxnCtx.systemTransactionContext();
Metadata metadata = clusterService.state().getMetadata();
Index index = metadata.index(getFqn("t")).getIndex();
IntArrayList shards = new IntArrayList(1);
shards.add(0);
Map<String, IntIndexedContainer> indexShards = Map.of(index.getName(), shards);
{
CompletableFuture<Long> count = countOperation.count(txnCtx, indexShards, Literal.BOOLEAN_TRUE);
assertThat(count.get(5, TimeUnit.SECONDS), is(3L));
}
Schemas schemas = internalCluster().getInstance(Schemas.class);
TableInfo tableInfo = schemas.getTableInfo(new RelationName(sqlExecutor.getCurrentSchema(), "t"));
TableRelation tableRelation = new TableRelation(tableInfo);
Map<RelationName, AnalyzedRelation> tableSources = Map.of(tableInfo.ident(), tableRelation);
SqlExpressions sqlExpressions = new SqlExpressions(tableSources, tableRelation);
Symbol filter = sqlExpressions.normalize(sqlExpressions.asSymbol("name = 'Marvin'"));
{
CompletableFuture<Long> count = countOperation.count(txnCtx, indexShards, filter);
assertThat(count.get(5, TimeUnit.SECONDS), is(1L));
}
}
use of io.crate.metadata.CoordinatorTxnCtx in project crate by crate.
the class SystemCollectSourceTest method testReadIsolation.
@Test
public void testReadIsolation() throws Exception {
SystemCollectSource systemCollectSource = internalCluster().getDataNodeInstance(SystemCollectSource.class);
RoutedCollectPhase collectPhase = new RoutedCollectPhase(UUID.randomUUID(), 1, "collect", new Routing(Map.of()), RowGranularity.SHARD, List.of(), List.of(), WhereClause.MATCH_ALL.queryOrFallback(), DistributionInfo.DEFAULT_BROADCAST);
// No read isolation
List<String> noReadIsolationIterable = new ArrayList<>();
noReadIsolationIterable.add("a");
noReadIsolationIterable.add("b");
CoordinatorTxnCtx txnCtx = CoordinatorTxnCtx.systemTransactionContext();
Iterable<? extends Row> rows = systemCollectSource.toRowsIterableTransformation(collectPhase, txnCtx, unassignedShardRefResolver(), false).apply(noReadIsolationIterable);
assertThat(StreamSupport.stream(rows.spliterator(), false).count(), is(2L));
noReadIsolationIterable.add("c");
assertThat(StreamSupport.stream(rows.spliterator(), false).count(), is(3L));
// Read isolation
List<String> readIsolationIterable = new ArrayList<>();
readIsolationIterable.add("a");
readIsolationIterable.add("b");
rows = systemCollectSource.toRowsIterableTransformation(collectPhase, txnCtx, unassignedShardRefResolver(), true).apply(readIsolationIterable);
assertThat(StreamSupport.stream(rows.spliterator(), false).count(), is(2L));
readIsolationIterable.add("c");
assertThat(StreamSupport.stream(rows.spliterator(), false).count(), is(2L));
}
use of io.crate.metadata.CoordinatorTxnCtx in project crate by crate.
the class RelationAnalyzer method visitQuerySpecification.
@Override
protected AnalyzedRelation visitQuerySpecification(QuerySpecification node, StatementAnalysisContext statementContext) {
List<Relation> from = node.getFrom().isEmpty() ? EMPTY_ROW_TABLE_RELATION : node.getFrom();
RelationAnalysisContext currentRelationContext = statementContext.startRelation();
for (Relation relation : from) {
// different from relations have to be isolated from each other
RelationAnalysisContext innerContext = statementContext.startRelation();
relation.accept(this, statementContext);
statementContext.endRelation();
for (Map.Entry<RelationName, AnalyzedRelation> entry : innerContext.sources().entrySet()) {
currentRelationContext.addSourceRelation(entry.getValue());
}
for (JoinPair joinPair : innerContext.joinPairs()) {
currentRelationContext.addJoinPair(joinPair);
}
}
RelationAnalysisContext context = statementContext.currentRelationContext();
CoordinatorTxnCtx coordinatorTxnCtx = statementContext.transactionContext();
ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(coordinatorTxnCtx, nodeCtx, statementContext.paramTyeHints(), new FullQualifiedNameFieldProvider(context.sources(), context.parentSources(), coordinatorTxnCtx.sessionContext().searchPath().currentSchema()), new SubqueryAnalyzer(this, statementContext));
ExpressionAnalysisContext expressionAnalysisContext = context.expressionAnalysisContext();
expressionAnalysisContext.windows(node.getWindows());
SelectAnalysis selectAnalysis = SelectAnalyzer.analyzeSelectItems(node.getSelect().getSelectItems(), context.sources(), expressionAnalyzer, expressionAnalysisContext);
List<Symbol> groupBy = analyzeGroupBy(selectAnalysis, node.getGroupBy(), expressionAnalyzer, expressionAnalysisContext);
if (!node.getGroupBy().isEmpty() || expressionAnalysisContext.hasAggregates()) {
GroupAndAggregateSemantics.validate(selectAnalysis.outputSymbols(), groupBy);
}
boolean isDistinct = node.getSelect().isDistinct();
Symbol where = expressionAnalyzer.generateQuerySymbol(node.getWhere(), expressionAnalysisContext);
WhereClauseValidator.validate(where);
var normalizer = EvaluatingNormalizer.functionOnlyNormalizer(nodeCtx, f -> expressionAnalysisContext.isEagerNormalizationAllowed() && f.isDeterministic());
QueriedSelectRelation relation = new QueriedSelectRelation(isDistinct, List.copyOf(context.sources().values()), context.joinPairs(), selectAnalysis.outputSymbols(), where, groupBy, analyzeHaving(node.getHaving(), groupBy, expressionAnalyzer, context.expressionAnalysisContext()), analyzeOrderBy(selectAnalysis, node.getOrderBy(), expressionAnalyzer, expressionAnalysisContext, expressionAnalysisContext.hasAggregates() || !groupBy.isEmpty(), isDistinct), longSymbolOrNull(node.getLimit(), expressionAnalyzer, expressionAnalysisContext, normalizer, coordinatorTxnCtx), longSymbolOrNull(node.getOffset(), expressionAnalyzer, expressionAnalysisContext, normalizer, coordinatorTxnCtx));
statementContext.endRelation();
return relation;
}
use of io.crate.metadata.CoordinatorTxnCtx in project crate by crate.
the class RelationAnalyzer method visitJoin.
@Override
protected AnalyzedRelation visitJoin(Join node, StatementAnalysisContext statementContext) {
AnalyzedRelation leftRel = node.getLeft().accept(this, statementContext);
AnalyzedRelation rightRel = node.getRight().accept(this, statementContext);
RelationAnalysisContext relationContext = statementContext.currentRelationContext();
Optional<JoinCriteria> optCriteria = node.getCriteria();
Symbol joinCondition = null;
if (optCriteria.isPresent()) {
JoinCriteria joinCriteria = optCriteria.get();
if (joinCriteria instanceof JoinOn || joinCriteria instanceof JoinUsing) {
final CoordinatorTxnCtx coordinatorTxnCtx = statementContext.transactionContext();
ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(coordinatorTxnCtx, nodeCtx, statementContext.paramTyeHints(), new FullQualifiedNameFieldProvider(relationContext.sources(), relationContext.parentSources(), coordinatorTxnCtx.sessionContext().searchPath().currentSchema()), new SubqueryAnalyzer(this, statementContext));
Expression expr;
if (joinCriteria instanceof JoinOn) {
expr = ((JoinOn) joinCriteria).getExpression();
} else {
expr = JoinUsing.toExpression(leftRel.relationName().toQualifiedName(), rightRel.relationName().toQualifiedName(), ((JoinUsing) joinCriteria).getColumns());
}
try {
joinCondition = expressionAnalyzer.convert(expr, relationContext.expressionAnalysisContext());
} catch (RelationUnknown e) {
throw new RelationValidationException(e.getTableIdents(), String.format(Locale.ENGLISH, "missing FROM-clause entry for relation '%s'", e.getTableIdents()));
}
} else {
throw new UnsupportedOperationException(String.format(Locale.ENGLISH, "join criteria %s not supported", joinCriteria.getClass().getSimpleName()));
}
}
relationContext.addJoinType(JoinType.values()[node.getType().ordinal()], joinCondition);
return null;
}
use of io.crate.metadata.CoordinatorTxnCtx in project crate by crate.
the class ExpressionAnalyzer method convertFunctionCall.
private Symbol convertFunctionCall(FunctionCall node, ExpressionAnalysisContext context) {
List<Symbol> arguments = new ArrayList<>(node.getArguments().size());
for (Expression expression : node.getArguments()) {
Symbol argSymbol = expression.accept(innerAnalyzer, context);
arguments.add(argSymbol);
}
List<String> parts = node.getName().getParts();
// We don't set a default schema here because no supplied schema
// means that we first try to lookup builtin functions, followed
// by a lookup in the default schema for UDFs.
String schema = null;
String name;
if (parts.size() == 1) {
name = parts.get(0);
} else {
schema = parts.get(0);
name = parts.get(1);
}
Symbol filter = node.filter().map(expression -> convert(expression, context)).orElse(null);
WindowDefinition windowDefinition = getWindowDefinition(node.getWindow(), context);
if (node.isDistinct()) {
if (arguments.size() > 1) {
throw new UnsupportedOperationException(String.format(Locale.ENGLISH, "%s(DISTINCT x) does not accept more than one argument", node.getName()));
}
Symbol collectSetFunction = allocateFunction(CollectSetAggregation.NAME, arguments, filter, context, coordinatorTxnCtx, nodeCtx);
// define the outer function which contains the inner function as argument.
String nodeName = "collection_" + name;
List<Symbol> outerArguments = List.of(collectSetFunction);
try {
return allocateBuiltinOrUdfFunction(schema, nodeName, outerArguments, null, node.ignoreNulls(), windowDefinition, context);
} catch (UnsupportedOperationException ex) {
throw new UnsupportedOperationException(String.format(Locale.ENGLISH, "unknown function %s(DISTINCT %s)", name, arguments.get(0).valueType()), ex);
}
} else {
return allocateBuiltinOrUdfFunction(schema, name, arguments, filter, node.ignoreNulls(), windowDefinition, context);
}
}
Aggregations