use of io.crate.metadata.CoordinatorTxnCtx in project crate by crate.
the class DocIndexMetadata method build.
public DocIndexMetadata build() {
notNullColumns = getNotNullColumns();
columnPolicy = getColumnPolicy();
createColumnDefinitions();
indices = createIndexDefinitions();
references = new LinkedHashMap<>();
DocSysColumns.forTable(ident, references::put);
columns.sort(SORT_REFS_BY_POSTITON_THEN_NAME);
nestedColumns.sort(SORT_REFS_BY_POSTITON_THEN_NAME);
for (Reference ref : columns) {
references.put(ref.column(), ref);
for (Reference nestedColumn : nestedColumns) {
if (nestedColumn.column().getRoot().equals(ref.column())) {
references.put(nestedColumn.column(), nestedColumn);
}
}
}
// Order of the partitionedByColumns is important; Must be the same order as `partitionedBy` is in.
partitionedByColumns = Lists2.map(partitionedBy, references::get);
generatedColumnReferences = List.copyOf(generatedColumnReferencesBuilder);
primaryKey = getPrimaryKey();
routingCol = getRoutingCol();
Collection<Reference> references = this.references.values();
TableReferenceResolver tableReferenceResolver = new TableReferenceResolver(references, ident);
CoordinatorTxnCtx txnCtx = CoordinatorTxnCtx.systemTransactionContext();
ExpressionAnalyzer exprAnalyzer = new ExpressionAnalyzer(txnCtx, nodeCtx, ParamTypeHints.EMPTY, tableReferenceResolver, null);
ExpressionAnalysisContext analysisCtx = new ExpressionAnalysisContext(txnCtx.sessionContext());
ArrayList<CheckConstraint<Symbol>> checkConstraintsBuilder = null;
Map<String, Object> metaMap = Maps.get(mappingMap, "_meta");
if (metaMap != null) {
Map<String, String> checkConstraintsMap = Maps.get(metaMap, "check_constraints");
if (checkConstraintsMap != null) {
checkConstraintsBuilder = new ArrayList<>();
for (Map.Entry<String, String> entry : checkConstraintsMap.entrySet()) {
String name = entry.getKey();
String expressionStr = entry.getValue();
Expression expr = SqlParser.createExpression(expressionStr);
Symbol analyzedExpr = exprAnalyzer.convert(expr, analysisCtx);
checkConstraintsBuilder.add(new CheckConstraint<>(name, null, analyzedExpr, expressionStr));
}
}
}
checkConstraints = checkConstraintsBuilder != null ? List.copyOf(checkConstraintsBuilder) : List.of();
for (Reference reference : generatedColumnReferences) {
GeneratedReference generatedReference = (GeneratedReference) reference;
Expression expression = SqlParser.createExpression(generatedReference.formattedGeneratedExpression());
generatedReference.generatedExpression(exprAnalyzer.convert(expression, analysisCtx));
generatedReference.referencedReferences(List.copyOf(tableReferenceResolver.references()));
tableReferenceResolver.references().clear();
}
return this;
}
use of io.crate.metadata.CoordinatorTxnCtx in project crate by crate.
the class ViewAnalyzer method analyze.
public CreateViewStmt analyze(CreateView createView, CoordinatorTxnCtx txnCtx) {
RelationName name = RelationName.of(createView.name(), txnCtx.sessionContext().searchPath().currentSchema());
name.ensureValidForRelationCreation();
if (BlobSchemaInfo.NAME.equals(name.schema())) {
throw new UnsupportedOperationException("Creating a view in the \"blob\" schema is not supported");
}
AnalyzedRelation query;
String formattedQuery;
try {
formattedQuery = SqlFormatter.formatSql(createView.query());
} catch (Exception e) {
throw new UnsupportedOperationException("Invalid query used in CREATE VIEW. Query: " + createView.query());
}
try {
// Analyze the formatted Query to make sure the formatting didn't mess it up in any way.
query = relationAnalyzer.analyze((Query) SqlParser.createStatement(formattedQuery), txnCtx, ParamTypeHints.EMPTY);
} catch (Exception e) {
throw new UnsupportedOperationException("Invalid query used in CREATE VIEW. " + e.getMessage() + ". Query: " + formattedQuery);
}
if (query.outputs().stream().map(f -> Symbols.pathFromSymbol(f).sqlFqn()).distinct().count() != query.outputs().size()) {
throw new IllegalArgumentException("Query in CREATE VIEW must not have duplicate column names");
}
return new CreateViewStmt(name, query, createView.query(), createView.replaceExisting(), txnCtx.sessionContext().sessionUser());
}
use of io.crate.metadata.CoordinatorTxnCtx 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.metadata.CoordinatorTxnCtx in project crate by crate.
the class DocKeysTest method testDocKeySequeceAndTerm.
@Test
public void testDocKeySequeceAndTerm() {
DocKeys docKeys = new DocKeys(List.of(List.of(Literal.of(1), Literal.of(22), Literal.of(5))), false, true, 1, null);
DocKeys.DocKey key = docKeys.getOnlyKey();
CoordinatorTxnCtx txnCtx = CoordinatorTxnCtx.systemTransactionContext();
Optional<Long> sequenceNo = key.sequenceNo(txnCtx, nodeCtx, Row.EMPTY, SubQueryResults.EMPTY);
assertThat(sequenceNo.isPresent(), is(true));
assertThat(sequenceNo.get(), is(22L));
Optional<Long> primaryTerm = key.primaryTerm(txnCtx, nodeCtx, Row.EMPTY, SubQueryResults.EMPTY);
assertThat(primaryTerm.isPresent(), is(true));
assertThat(primaryTerm.get(), is(5L));
}
use of io.crate.metadata.CoordinatorTxnCtx in project crate by crate.
the class SQLExecutor method asSymbol.
/**
* Convert a expression to a symbol
* If tables are used here they must also be registered in the SQLExecutor having used {@link Builder#addTable(String)}
*/
public Symbol asSymbol(String expression) {
MapBuilder<RelationName, AnalyzedRelation> sources = MapBuilder.newMapBuilder();
for (SchemaInfo schemaInfo : schemas) {
for (TableInfo tableInfo : schemaInfo.getTables()) {
if (tableInfo instanceof DocTableInfo) {
RelationName relationName = tableInfo.ident();
sources.put(relationName, new DocTableRelation(schemas.getTableInfo(relationName)));
}
}
}
CoordinatorTxnCtx coordinatorTxnCtx = new CoordinatorTxnCtx(sessionContext);
ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(coordinatorTxnCtx, nodeCtx, ParamTypeHints.EMPTY, new FullQualifiedNameFieldProvider(sources.immutableMap(), ParentRelations.NO_PARENTS, sessionContext.searchPath().currentSchema()), new SubqueryAnalyzer(relAnalyzer, new StatementAnalysisContext(ParamTypeHints.EMPTY, Operation.READ, coordinatorTxnCtx)));
ExpressionAnalysisContext expressionAnalysisContext = new ExpressionAnalysisContext(coordinatorTxnCtx.sessionContext());
return expressionAnalyzer.convert(SqlParser.createExpression(expression), expressionAnalysisContext);
}
Aggregations