Search in sources :

Example 1 with FullQualifiedNameFieldProvider

use of io.crate.analyze.relations.FullQualifiedNameFieldProvider in project crate by crate.

the class DeleteAnalyzer method analyze.

public AnalyzedDeleteStatement analyze(Delete delete, ParamTypeHints typeHints, CoordinatorTxnCtx txnContext) {
    StatementAnalysisContext stmtCtx = new StatementAnalysisContext(typeHints, Operation.DELETE, txnContext);
    final RelationAnalysisContext relationCtx = stmtCtx.startRelation();
    AnalyzedRelation relation = relationAnalyzer.analyze(delete.getRelation(), stmtCtx);
    stmtCtx.endRelation();
    MaybeAliasedStatement maybeAliasedStatement = MaybeAliasedStatement.analyze(relation);
    relation = maybeAliasedStatement.nonAliasedRelation();
    if (!(relation instanceof DocTableRelation)) {
        throw new UnsupportedOperationException("Cannot delete from relations other than base tables");
    }
    DocTableRelation table = (DocTableRelation) relation;
    EvaluatingNormalizer normalizer = new EvaluatingNormalizer(nodeCtx, RowGranularity.CLUSTER, null, table);
    ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(txnContext, nodeCtx, typeHints, new FullQualifiedNameFieldProvider(relationCtx.sources(), relationCtx.parentSources(), txnContext.sessionContext().searchPath().currentSchema()), new SubqueryAnalyzer(relationAnalyzer, new StatementAnalysisContext(typeHints, Operation.READ, txnContext)));
    Symbol query = Objects.requireNonNullElse(expressionAnalyzer.generateQuerySymbol(delete.getWhere(), new ExpressionAnalysisContext(txnContext.sessionContext())), Literal.BOOLEAN_TRUE);
    query = maybeAliasedStatement.maybeMapFields(query);
    Symbol normalizedQuery = normalizer.normalize(query, txnContext);
    return new AnalyzedDeleteStatement(table, normalizedQuery);
}
Also used : RelationAnalysisContext(io.crate.analyze.relations.RelationAnalysisContext) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) EvaluatingNormalizer(io.crate.expression.eval.EvaluatingNormalizer) Symbol(io.crate.expression.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) StatementAnalysisContext(io.crate.analyze.relations.StatementAnalysisContext) DocTableRelation(io.crate.analyze.relations.DocTableRelation) SubqueryAnalyzer(io.crate.analyze.expressions.SubqueryAnalyzer) FullQualifiedNameFieldProvider(io.crate.analyze.relations.FullQualifiedNameFieldProvider)

Example 2 with FullQualifiedNameFieldProvider

use of io.crate.analyze.relations.FullQualifiedNameFieldProvider 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);
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) StatementAnalysisContext(io.crate.analyze.relations.StatementAnalysisContext) RelationName(io.crate.metadata.RelationName) DocTableRelation(io.crate.analyze.relations.DocTableRelation) TableInfo(io.crate.metadata.table.TableInfo) DocTableInfo(io.crate.metadata.doc.DocTableInfo) SubqueryAnalyzer(io.crate.analyze.expressions.SubqueryAnalyzer) PgCatalogSchemaInfo(io.crate.metadata.pgcatalog.PgCatalogSchemaInfo) SysSchemaInfo(io.crate.metadata.sys.SysSchemaInfo) BlobSchemaInfo(io.crate.metadata.blob.BlobSchemaInfo) InformationSchemaInfo(io.crate.metadata.information.InformationSchemaInfo) SchemaInfo(io.crate.metadata.table.SchemaInfo) FullQualifiedNameFieldProvider(io.crate.analyze.relations.FullQualifiedNameFieldProvider)

Example 3 with FullQualifiedNameFieldProvider

use of io.crate.analyze.relations.FullQualifiedNameFieldProvider in project crate by crate.

the class ExpressionAnalyzerTest method testInSelfJoinCaseFunctionsThatLookTheSameMustNotReuseFunctionAllocation.

@Test
public void testInSelfJoinCaseFunctionsThatLookTheSameMustNotReuseFunctionAllocation() throws Exception {
    TableInfo t1 = executor.resolveTableInfo("t1");
    TableRelation relation = new TableRelation(t1);
    RelationName a1 = new RelationName(null, "a1");
    RelationName a2 = new RelationName(null, "a2");
    Map<RelationName, AnalyzedRelation> sources = Map.of(a1, new AliasedAnalyzedRelation(relation, a1), a2, new AliasedAnalyzedRelation(relation, a2));
    SessionContext sessionContext = SessionContext.systemSessionContext();
    CoordinatorTxnCtx coordinatorTxnCtx = new CoordinatorTxnCtx(sessionContext);
    ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(coordinatorTxnCtx, expressions.nodeCtx, paramTypeHints, new FullQualifiedNameFieldProvider(sources, ParentRelations.NO_PARENTS, sessionContext.searchPath().currentSchema()), null);
    Function andFunction = (Function) expressionAnalyzer.convert(SqlParser.createExpression("not a1.x = 1 and not a2.x = 1"), context);
    ScopedSymbol t1Id = ((ScopedSymbol) ((Function) ((Function) andFunction.arguments().get(0)).arguments().get(0)).arguments().get(0));
    ScopedSymbol t2Id = ((ScopedSymbol) ((Function) ((Function) andFunction.arguments().get(1)).arguments().get(0)).arguments().get(0));
    assertThat(t1Id.relation(), is(not(t2Id.relation())));
}
Also used : CoalesceFunction(io.crate.expression.scalar.conditional.CoalesceFunction) SymbolMatchers.isFunction(io.crate.testing.SymbolMatchers.isFunction) ArraySliceFunction(io.crate.expression.scalar.ArraySliceFunction) ImplicitCastFunction(io.crate.expression.scalar.cast.ImplicitCastFunction) Function(io.crate.expression.symbol.Function) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) RelationName(io.crate.metadata.RelationName) SessionContext(io.crate.action.sql.SessionContext) TableInfo(io.crate.metadata.table.TableInfo) AliasedAnalyzedRelation(io.crate.analyze.relations.AliasedAnalyzedRelation) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) TableRelation(io.crate.analyze.relations.TableRelation) AliasedAnalyzedRelation(io.crate.analyze.relations.AliasedAnalyzedRelation) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol) FullQualifiedNameFieldProvider(io.crate.analyze.relations.FullQualifiedNameFieldProvider) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 4 with FullQualifiedNameFieldProvider

use of io.crate.analyze.relations.FullQualifiedNameFieldProvider in project crate by crate.

the class UpdateAnalyzer method analyze.

public AnalyzedUpdateStatement analyze(Update update, ParamTypeHints typeHints, CoordinatorTxnCtx txnCtx) {
    /* UPDATE t1 SET col1 = ?, col2 = ? WHERE id = ?`
         *               ^^^^^^^^^^^^^^^^^^       ^^^^^^
         *               assignments               whereClause
         *
         *               col1 = ?
         *               |      |
         *               |     source
         *             columnName/target
         */
    StatementAnalysisContext stmtCtx = new StatementAnalysisContext(typeHints, Operation.UPDATE, txnCtx);
    final RelationAnalysisContext relCtx = stmtCtx.startRelation();
    AnalyzedRelation relation = relationAnalyzer.analyze(update.relation(), stmtCtx);
    stmtCtx.endRelation();
    MaybeAliasedStatement maybeAliasedStatement = MaybeAliasedStatement.analyze(relation);
    relation = maybeAliasedStatement.nonAliasedRelation();
    if (!(relation instanceof AbstractTableRelation)) {
        throw new UnsupportedOperationException("UPDATE is only supported on base-tables");
    }
    AbstractTableRelation<?> table = (AbstractTableRelation<?>) relation;
    EvaluatingNormalizer normalizer = new EvaluatingNormalizer(nodeCtx, RowGranularity.CLUSTER, null, table);
    SubqueryAnalyzer subqueryAnalyzer = new SubqueryAnalyzer(relationAnalyzer, new StatementAnalysisContext(typeHints, Operation.READ, txnCtx));
    ExpressionAnalyzer sourceExprAnalyzer = new ExpressionAnalyzer(txnCtx, nodeCtx, typeHints, new FullQualifiedNameFieldProvider(relCtx.sources(), relCtx.parentSources(), txnCtx.sessionContext().searchPath().currentSchema()), subqueryAnalyzer);
    ExpressionAnalysisContext exprCtx = new ExpressionAnalysisContext(txnCtx.sessionContext());
    Map<Reference, Symbol> assignmentByTargetCol = getAssignments(update.assignments(), typeHints, txnCtx, table, normalizer, subqueryAnalyzer, sourceExprAnalyzer, exprCtx);
    Symbol query = Objects.requireNonNullElse(sourceExprAnalyzer.generateQuerySymbol(update.whereClause(), exprCtx), Literal.BOOLEAN_TRUE);
    query = maybeAliasedStatement.maybeMapFields(query);
    Symbol normalizedQuery = normalizer.normalize(query, txnCtx);
    SelectAnalysis selectAnalysis = SelectAnalyzer.analyzeSelectItems(update.returningClause(), relCtx.sources(), sourceExprAnalyzer, exprCtx);
    List<Symbol> outputSymbol = Lists2.map(selectAnalysis.outputSymbols(), x -> normalizer.normalize(x, txnCtx));
    return new AnalyzedUpdateStatement(table, assignmentByTargetCol, normalizedQuery, outputSymbol.isEmpty() ? null : outputSymbol);
}
Also used : RelationAnalysisContext(io.crate.analyze.relations.RelationAnalysisContext) SelectAnalysis(io.crate.analyze.relations.select.SelectAnalysis) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) EvaluatingNormalizer(io.crate.expression.eval.EvaluatingNormalizer) Reference(io.crate.metadata.Reference) Symbol(io.crate.expression.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) StatementAnalysisContext(io.crate.analyze.relations.StatementAnalysisContext) SubqueryAnalyzer(io.crate.analyze.expressions.SubqueryAnalyzer) AbstractTableRelation(io.crate.analyze.relations.AbstractTableRelation) FullQualifiedNameFieldProvider(io.crate.analyze.relations.FullQualifiedNameFieldProvider)

Example 5 with FullQualifiedNameFieldProvider

use of io.crate.analyze.relations.FullQualifiedNameFieldProvider in project crate by crate.

the class InsertAnalyzer method analyze.

public AnalyzedInsertStatement analyze(Insert<Expression> insert, ParamTypeHints typeHints, CoordinatorTxnCtx txnCtx) {
    DocTableInfo tableInfo = (DocTableInfo) schemas.resolveTableInfo(insert.table().getName(), Operation.INSERT, txnCtx.sessionContext().sessionUser(), txnCtx.sessionContext().searchPath());
    List<Reference> targetColumns = new ArrayList<>(resolveTargetColumns(insert.columns(), tableInfo));
    AnalyzedRelation subQueryRelation = relationAnalyzer.analyze(insert.insertSource(), new StatementAnalysisContext(typeHints, Operation.READ, txnCtx, targetColumns));
    ensureClusteredByPresentOrNotRequired(targetColumns, tableInfo);
    checkSourceAndTargetColsForLengthAndTypesCompatibility(targetColumns, subQueryRelation.outputs());
    DocTableRelation tableRelation = new DocTableRelation(tableInfo);
    NameFieldProvider fieldProvider = new NameFieldProvider(tableRelation);
    ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(txnCtx, nodeCtx, typeHints, fieldProvider, null, Operation.READ);
    verifyOnConflictTargets(txnCtx, expressionAnalyzer, tableInfo, insert.duplicateKeyContext());
    Map<Reference, Symbol> onDuplicateKeyAssignments = processUpdateAssignments(tableRelation, targetColumns, typeHints, txnCtx, nodeCtx, fieldProvider, insert.duplicateKeyContext());
    final boolean ignoreDuplicateKeys = insert.duplicateKeyContext().getType() == Insert.DuplicateKeyContext.Type.ON_CONFLICT_DO_NOTHING;
    List<Symbol> returnValues;
    if (insert.returningClause().isEmpty()) {
        returnValues = null;
    } else {
        var exprCtx = new ExpressionAnalysisContext(txnCtx.sessionContext());
        Map<RelationName, AnalyzedRelation> sources = Map.of(tableRelation.relationName(), tableRelation);
        var sourceExprAnalyzer = new ExpressionAnalyzer(txnCtx, nodeCtx, typeHints, new FullQualifiedNameFieldProvider(sources, ParentRelations.NO_PARENTS, txnCtx.sessionContext().searchPath().currentSchema()), null);
        var selectAnalysis = SelectAnalyzer.analyzeSelectItems(insert.returningClause(), sources, sourceExprAnalyzer, exprCtx);
        returnValues = selectAnalysis.outputSymbols();
    }
    return new AnalyzedInsertStatement(subQueryRelation, tableInfo, targetColumns, ignoreDuplicateKeys, onDuplicateKeyAssignments, returnValues);
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) QualifiedNameReference(io.crate.sql.tree.QualifiedNameReference) DynamicReference(io.crate.expression.symbol.DynamicReference) GeneratedReference(io.crate.metadata.GeneratedReference) Reference(io.crate.metadata.Reference) Symbol(io.crate.expression.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) ArrayList(java.util.ArrayList) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) StatementAnalysisContext(io.crate.analyze.relations.StatementAnalysisContext) FullQualifiedNameFieldProvider(io.crate.analyze.relations.FullQualifiedNameFieldProvider) NameFieldProvider(io.crate.analyze.relations.NameFieldProvider) DocTableRelation(io.crate.analyze.relations.DocTableRelation) RelationName(io.crate.metadata.RelationName) FullQualifiedNameFieldProvider(io.crate.analyze.relations.FullQualifiedNameFieldProvider)

Aggregations

AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)5 FullQualifiedNameFieldProvider (io.crate.analyze.relations.FullQualifiedNameFieldProvider)5 ExpressionAnalysisContext (io.crate.analyze.expressions.ExpressionAnalysisContext)4 ExpressionAnalyzer (io.crate.analyze.expressions.ExpressionAnalyzer)4 StatementAnalysisContext (io.crate.analyze.relations.StatementAnalysisContext)4 SubqueryAnalyzer (io.crate.analyze.expressions.SubqueryAnalyzer)3 DocTableRelation (io.crate.analyze.relations.DocTableRelation)3 Symbol (io.crate.expression.symbol.Symbol)3 RelationName (io.crate.metadata.RelationName)3 RelationAnalysisContext (io.crate.analyze.relations.RelationAnalysisContext)2 EvaluatingNormalizer (io.crate.expression.eval.EvaluatingNormalizer)2 CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)2 Reference (io.crate.metadata.Reference)2 DocTableInfo (io.crate.metadata.doc.DocTableInfo)2 TableInfo (io.crate.metadata.table.TableInfo)2 SessionContext (io.crate.action.sql.SessionContext)1 AbstractTableRelation (io.crate.analyze.relations.AbstractTableRelation)1 AliasedAnalyzedRelation (io.crate.analyze.relations.AliasedAnalyzedRelation)1 NameFieldProvider (io.crate.analyze.relations.NameFieldProvider)1 TableRelation (io.crate.analyze.relations.TableRelation)1