use of io.crate.metadata.CoordinatorTxnCtx 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.metadata.CoordinatorTxnCtx in project crate by crate.
the class LogicalPlanner method planSubSelect.
public LogicalPlan planSubSelect(SelectSymbol selectSymbol, PlannerContext plannerContext) {
CoordinatorTxnCtx txnCtx = plannerContext.transactionContext();
AnalyzedRelation relation = selectSymbol.relation();
final int fetchSize;
final java.util.function.Function<LogicalPlan, LogicalPlan> maybeApplySoftLimit;
if (selectSymbol.getResultType() == SINGLE_COLUMN_SINGLE_VALUE) {
// SELECT (SELECT foo FROM t)
// ^^^^^^^^^^^^^^^^^
// The subquery must return at most 1 row, if more than 1 row is returned semantics require us to throw an error.
// So we limit the query to 2 if there is no limit to avoid retrieval of many rows while being able to validate max1row
fetchSize = 2;
maybeApplySoftLimit = plan -> new Limit(plan, Literal.of(2L), Literal.of(0L));
} else {
fetchSize = 0;
maybeApplySoftLimit = plan -> plan;
}
PlannerContext subSelectPlannerContext = PlannerContext.forSubPlan(plannerContext, fetchSize);
SubqueryPlanner subqueryPlanner = new SubqueryPlanner(s -> planSubSelect(s, subSelectPlannerContext));
var planBuilder = new PlanBuilder(subqueryPlanner, txnCtx, tableStats, subSelectPlannerContext.params());
LogicalPlan plan = relation.accept(planBuilder, relation.outputs());
plan = tryOptimizeForInSubquery(selectSymbol, relation, plan);
LogicalPlan optimizedPlan = optimizer.optimize(maybeApplySoftLimit.apply(plan), tableStats, txnCtx);
return new RootRelationBoundary(optimizedPlan);
}
use of io.crate.metadata.CoordinatorTxnCtx in project crate by crate.
the class PreExecutionBenchmark method measure_parse_and_analyze_simple_select.
@Benchmark
public AnalyzedStatement measure_parse_and_analyze_simple_select() throws Exception {
String sql = "select name from users";
CoordinatorTxnCtx systemTransactionContext = CoordinatorTxnCtx.systemTransactionContext();
Analysis analysis = new Analysis(systemTransactionContext, ParamTypeHints.EMPTY);
return analyzer.analyzedStatement(SqlParser.createStatement(sql), analysis);
}
use of io.crate.metadata.CoordinatorTxnCtx in project crate by crate.
the class PreExecutionBenchmark method measure_parse_analyze_and_plan_simple_select.
@Benchmark
public Plan measure_parse_analyze_and_plan_simple_select() throws Exception {
String sql = "select name from users";
CoordinatorTxnCtx systemTransactionContext = CoordinatorTxnCtx.systemTransactionContext();
Analysis analysis = new Analysis(systemTransactionContext, ParamTypeHints.EMPTY);
AnalyzedStatement analyzedStatement = analyzer.analyzedStatement(SqlParser.createStatement(sql), analysis);
var jobId = UUID.randomUUID();
var routingProvider = new RoutingProvider(Randomness.get().nextInt(), planner.getAwarenessAttributes());
var clusterState = clusterService.state();
var txnCtx = CoordinatorTxnCtx.systemTransactionContext();
var plannerContext = new PlannerContext(clusterState, routingProvider, jobId, txnCtx, nodeCtx, 0, null);
return planner.plan(analyzedStatement, plannerContext);
}
use of io.crate.metadata.CoordinatorTxnCtx in project crate by crate.
the class CopyToPlan method bind.
@VisibleForTesting
public static BoundCopyTo bind(AnalyzedCopyTo copyTo, CoordinatorTxnCtx txnCtx, NodeContext nodeCtx, Row parameters, SubQueryResults subQueryResults) {
Function<? super Symbol, Object> eval = x -> SymbolEvaluator.evaluate(txnCtx, nodeCtx, x, parameters, subQueryResults);
DocTableInfo table = (DocTableInfo) copyTo.tableInfo();
List<String> partitions = resolvePartitions(Lists2.map(copyTo.table().partitionProperties(), x -> x.map(eval)), table);
List<Symbol> outputs = new ArrayList<>();
Map<ColumnIdent, Symbol> overwrites = null;
boolean columnsDefined = false;
final List<String> outputNames = new ArrayList<>(copyTo.columns().size());
if (!copyTo.columns().isEmpty()) {
// TODO: remove outputNames?
for (Symbol symbol : copyTo.columns()) {
assert symbol instanceof Reference : "Only references are expected here";
RefVisitor.visitRefs(symbol, r -> outputNames.add(r.column().sqlFqn()));
outputs.add(DocReferences.toSourceLookup(symbol));
}
columnsDefined = true;
} else {
Reference sourceRef;
if (table.isPartitioned() && partitions.isEmpty()) {
// table is partitioned, insert partitioned columns into the output
overwrites = new HashMap<>();
for (Reference reference : table.partitionedByColumns()) {
if (!(reference instanceof GeneratedReference)) {
overwrites.put(reference.column(), reference);
}
}
if (overwrites.size() > 0) {
sourceRef = table.getReference(DocSysColumns.DOC);
} else {
sourceRef = table.getReference(DocSysColumns.RAW);
}
} else {
sourceRef = table.getReference(DocSysColumns.RAW);
}
outputs = List.of(sourceRef);
}
Settings settings = genericPropertiesToSettings(copyTo.properties().map(eval));
WriterProjection.CompressionType compressionType = settingAsEnum(WriterProjection.CompressionType.class, COMPRESSION_SETTING.get(settings));
WriterProjection.OutputFormat outputFormat = settingAsEnum(WriterProjection.OutputFormat.class, OUTPUT_FORMAT_SETTING.get(settings));
if (!columnsDefined && outputFormat == WriterProjection.OutputFormat.JSON_ARRAY) {
throw new UnsupportedFeatureException("Output format not supported without specifying columns.");
}
WhereClause whereClause = new WhereClause(copyTo.whereClause(), partitions, Collections.emptySet());
return new BoundCopyTo(outputs, table, whereClause, Literal.of(DataTypes.STRING.sanitizeValue(eval.apply(copyTo.uri()))), compressionType, outputFormat, outputNames.isEmpty() ? null : outputNames, columnsDefined, overwrites, settings);
}
Aggregations