Search in sources :

Example 6 with ExpressionAnalysisContext

use of io.crate.analyze.expressions.ExpressionAnalysisContext in project crate by crate.

the class CopyAnalyzer method convertCopyFrom.

CopyFromAnalyzedStatement convertCopyFrom(CopyFrom node, Analysis analysis) {
    DocTableInfo tableInfo = schemas.getWritableTable(TableIdent.of(node.table(), analysis.sessionContext().defaultSchema()));
    DocTableRelation tableRelation = new DocTableRelation(tableInfo);
    Operation.blockedRaiseException(tableInfo, Operation.INSERT);
    String partitionIdent = null;
    if (!node.table().partitionProperties().isEmpty()) {
        partitionIdent = PartitionPropertiesAnalyzer.toPartitionIdent(tableInfo, node.table().partitionProperties(), analysis.parameterContext().parameters());
    }
    EvaluatingNormalizer normalizer = new EvaluatingNormalizer(functions, RowGranularity.CLUSTER, ReplaceMode.MUTATE, null, tableRelation);
    ExpressionAnalyzer expressionAnalyzer = createExpressionAnalyzer(analysis, tableRelation);
    expressionAnalyzer.setResolveFieldsOperation(Operation.INSERT);
    ExpressionAnalysisContext expressionAnalysisContext = new ExpressionAnalysisContext();
    Predicate<DiscoveryNode> nodeFilters = Predicates.alwaysTrue();
    Settings settings = Settings.EMPTY;
    if (node.genericProperties().isPresent()) {
        // copy map as items are removed. The GenericProperties map is cached in the query cache and removing
        // items would cause subsequent queries that hit the cache to have different genericProperties
        Map<String, Expression> properties = new HashMap<>(node.genericProperties().get().properties());
        nodeFilters = discoveryNodePredicate(analysis.parameterContext().parameters(), properties.remove(NodeFilters.NAME));
        settings = settingsFromProperties(properties, expressionAnalyzer, expressionAnalysisContext);
    }
    Symbol uri = expressionAnalyzer.convert(node.path(), expressionAnalysisContext);
    uri = normalizer.normalize(uri, analysis.transactionContext());
    if (!(uri.valueType() == DataTypes.STRING || uri.valueType() instanceof CollectionType && ((CollectionType) uri.valueType()).innerType() == DataTypes.STRING)) {
        throw CopyFromAnalyzedStatement.raiseInvalidType(uri.valueType());
    }
    return new CopyFromAnalyzedStatement(tableInfo, settings, uri, partitionIdent, nodeFilters);
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) Symbol(io.crate.analyze.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) CollectionType(io.crate.types.CollectionType) DocTableRelation(io.crate.analyze.relations.DocTableRelation) Settings(org.elasticsearch.common.settings.Settings)

Example 7 with ExpressionAnalysisContext

use of io.crate.analyze.expressions.ExpressionAnalysisContext in project crate by crate.

the class CopyAnalyzer method convertCopyTo.

CopyToAnalyzedStatement convertCopyTo(CopyTo node, Analysis analysis) {
    if (!node.directoryUri()) {
        throw new UnsupportedOperationException("Using COPY TO without specifying a DIRECTORY is not supported");
    }
    TableInfo tableInfo = schemas.getTableInfo(TableIdent.of(node.table(), analysis.sessionContext().defaultSchema()));
    if (!(tableInfo instanceof DocTableInfo)) {
        throw new UnsupportedOperationException(String.format(Locale.ENGLISH, "Cannot COPY %s TO. COPY TO only supports user tables", tableInfo.ident()));
    }
    Operation.blockedRaiseException(tableInfo, Operation.READ);
    DocTableRelation tableRelation = new DocTableRelation((DocTableInfo) tableInfo);
    EvaluatingNormalizer normalizer = new EvaluatingNormalizer(functions, RowGranularity.CLUSTER, ReplaceMode.MUTATE, null, tableRelation);
    ExpressionAnalyzer expressionAnalyzer = createExpressionAnalyzer(analysis, tableRelation);
    ExpressionAnalysisContext expressionAnalysisContext = new ExpressionAnalysisContext();
    Settings settings = GenericPropertiesConverter.settingsFromProperties(node.genericProperties(), analysis.parameterContext(), SETTINGS_APPLIERS).build();
    WriterProjection.CompressionType compressionType = settingAsEnum(WriterProjection.CompressionType.class, settings.get(COMPRESSION_SETTINGS.name()));
    WriterProjection.OutputFormat outputFormat = settingAsEnum(WriterProjection.OutputFormat.class, settings.get(OUTPUT_FORMAT_SETTINGS.name()));
    Symbol uri = expressionAnalyzer.convert(node.targetUri(), expressionAnalysisContext);
    uri = normalizer.normalize(uri, analysis.transactionContext());
    List<String> partitions = resolvePartitions(node, analysis, tableRelation);
    List<Symbol> outputs = new ArrayList<>();
    QuerySpec querySpec = new QuerySpec();
    WhereClause whereClause = createWhereClause(node.whereClause(), tableRelation, partitions, normalizer, expressionAnalyzer, expressionAnalysisContext, analysis.transactionContext());
    querySpec.where(whereClause);
    Map<ColumnIdent, Symbol> overwrites = null;
    boolean columnsDefined = false;
    List<String> outputNames = null;
    if (!node.columns().isEmpty()) {
        outputNames = new ArrayList<>(node.columns().size());
        for (Expression expression : node.columns()) {
            Symbol symbol = expressionAnalyzer.convert(expression, expressionAnalysisContext);
            symbol = normalizer.normalize(symbol, analysis.transactionContext());
            outputNames.add(SymbolPrinter.INSTANCE.printSimple(symbol));
            outputs.add(DocReferenceConverter.convertIf(symbol));
        }
        columnsDefined = true;
    } else {
        Reference sourceRef;
        if (tableRelation.tableInfo().isPartitioned() && partitions.isEmpty()) {
            // table is partitioned, insert partitioned columns into the output
            overwrites = new HashMap<>();
            for (Reference reference : tableRelation.tableInfo().partitionedByColumns()) {
                if (!(reference instanceof GeneratedReference)) {
                    overwrites.put(reference.ident().columnIdent(), reference);
                }
            }
            if (overwrites.size() > 0) {
                sourceRef = tableRelation.tableInfo().getReference(DocSysColumns.DOC);
            } else {
                sourceRef = tableRelation.tableInfo().getReference(DocSysColumns.RAW);
            }
        } else {
            sourceRef = tableRelation.tableInfo().getReference(DocSysColumns.RAW);
        }
        outputs = ImmutableList.<Symbol>of(sourceRef);
    }
    querySpec.outputs(outputs);
    if (!columnsDefined && outputFormat == WriterProjection.OutputFormat.JSON_ARRAY) {
        throw new UnsupportedFeatureException("Output format not supported without specifying columns.");
    }
    QueriedDocTable subRelation = new QueriedDocTable(tableRelation, querySpec);
    return new CopyToAnalyzedStatement(subRelation, settings, uri, compressionType, outputFormat, outputNames, columnsDefined, overwrites);
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) Symbol(io.crate.analyze.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableInfo(io.crate.metadata.table.TableInfo) Settings(org.elasticsearch.common.settings.Settings) UnsupportedFeatureException(io.crate.exceptions.UnsupportedFeatureException) WriterProjection(io.crate.planner.projection.WriterProjection) QueriedDocTable(io.crate.analyze.relations.QueriedDocTable) DocTableRelation(io.crate.analyze.relations.DocTableRelation)

Example 8 with ExpressionAnalysisContext

use of io.crate.analyze.expressions.ExpressionAnalysisContext in project crate by crate.

the class InsertFromSubQueryAnalyzer method processUpdateAssignments.

private Map<Reference, Symbol> processUpdateAssignments(DocTableRelation tableRelation, List<Reference> targetColumns, SessionContext sessionContext, ParameterContext parameterContext, TransactionContext transactionContext, FieldProvider fieldProvider, List<Assignment> assignments) {
    ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(functions, sessionContext, parameterContext, fieldProvider, null);
    ExpressionAnalysisContext expressionAnalysisContext = new ExpressionAnalysisContext();
    EvaluatingNormalizer normalizer = new EvaluatingNormalizer(functions, RowGranularity.CLUSTER, ReplaceMode.COPY, null, tableRelation);
    ValueNormalizer valuesNormalizer = new ValueNormalizer(schemas);
    ValuesResolver valuesResolver = new ValuesResolver(tableRelation, targetColumns);
    ValuesAwareExpressionAnalyzer valuesAwareExpressionAnalyzer = new ValuesAwareExpressionAnalyzer(functions, sessionContext, parameterContext, fieldProvider, valuesResolver);
    Map<Reference, Symbol> updateAssignments = new HashMap<>(assignments.size());
    for (Assignment assignment : assignments) {
        Reference columnName = tableRelation.resolveField((Field) expressionAnalyzer.convert(assignment.columnName(), expressionAnalysisContext));
        assert columnName != null : "columnName must not be null";
        Symbol valueSymbol = normalizer.normalize(valuesAwareExpressionAnalyzer.convert(assignment.expression(), expressionAnalysisContext), transactionContext);
        Symbol assignmentExpression = valuesNormalizer.normalizeInputForReference(valueSymbol, columnName);
        updateAssignments.put(columnName, assignmentExpression);
    }
    return updateAssignments;
}
Also used : Assignment(io.crate.sql.tree.Assignment) ValueNormalizer(io.crate.analyze.expressions.ValueNormalizer) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) DynamicReference(io.crate.analyze.symbol.DynamicReference) Symbol(io.crate.analyze.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer)

Example 9 with ExpressionAnalysisContext

use of io.crate.analyze.expressions.ExpressionAnalysisContext in project crate by crate.

the class RelationAnalyzer method visitQuerySpecification.

@Override
protected AnalyzedRelation visitQuerySpecification(QuerySpecification node, StatementAnalysisContext statementContext) {
    List<Relation> from = node.getFrom() != null ? node.getFrom() : SYS_CLUSTER_SOURCE;
    statementContext.startRelation();
    for (Relation relation : from) {
        process(relation, statementContext);
    }
    RelationAnalysisContext context = statementContext.currentRelationContext();
    ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(functions, statementContext.sessionContext(), statementContext.convertParamFunction(), new FullQualifedNameFieldProvider(context.sources()), new SubqueryAnalyzer(this, statementContext));
    ExpressionAnalysisContext expressionAnalysisContext = context.expressionAnalysisContext();
    Symbol querySymbol = expressionAnalyzer.generateQuerySymbol(node.getWhere(), expressionAnalysisContext);
    WhereClause whereClause = new WhereClause(querySymbol);
    SelectAnalysis selectAnalysis = SelectAnalyzer.analyzeSelect(node.getSelect(), context, expressionAnalyzer, expressionAnalysisContext);
    List<Symbol> groupBy = analyzeGroupBy(selectAnalysis, node.getGroupBy(), expressionAnalyzer, expressionAnalysisContext);
    if (!node.getGroupBy().isEmpty() || expressionAnalysisContext.hasAggregates) {
        ensureNonAggregatesInGroupBy(selectAnalysis.outputSymbols(), selectAnalysis.outputNames(), groupBy);
    }
    boolean isDistinct = false;
    if (node.getSelect().isDistinct() && groupBy.isEmpty()) {
        groupBy = rewriteGlobalDistinct(selectAnalysis.outputSymbols());
        isDistinct = true;
    }
    if (groupBy != null && groupBy.isEmpty()) {
        groupBy = null;
    }
    QuerySpec querySpec = new QuerySpec().orderBy(analyzeOrderBy(selectAnalysis, node.getOrderBy(), expressionAnalyzer, expressionAnalysisContext, expressionAnalysisContext.hasAggregates || groupBy != null, isDistinct)).having(analyzeHaving(node.getHaving(), groupBy, expressionAnalyzer, context.expressionAnalysisContext())).limit(optionalLongSymbol(node.getLimit(), expressionAnalyzer, expressionAnalysisContext)).offset(optionalLongSymbol(node.getOffset(), expressionAnalyzer, expressionAnalysisContext)).outputs(selectAnalysis.outputSymbols()).where(whereClause).groupBy(groupBy).hasAggregates(expressionAnalysisContext.hasAggregates);
    QueriedRelation relation;
    if (context.sources().size() == 1) {
        AnalyzedRelation source = Iterables.getOnlyElement(context.sources().values());
        if (source instanceof DocTableRelation) {
            relation = new QueriedDocTable((DocTableRelation) source, selectAnalysis.outputNames(), querySpec);
        } else if (source instanceof TableRelation) {
            relation = new QueriedTable((TableRelation) source, selectAnalysis.outputNames(), querySpec);
        } else {
            assert source instanceof QueriedRelation : "expecting relation to be an instance of QueriedRelation";
            relation = new QueriedSelectRelation((QueriedRelation) source, selectAnalysis.outputNames(), querySpec);
        }
    } else {
        relation = new MultiSourceSelect(context.sources(), selectAnalysis.outputSymbols(), selectAnalysis.outputNames(), querySpec, context.joinPairs());
    }
    statementContext.endRelation();
    return relation;
}
Also used : SelectAnalysis(io.crate.analyze.relations.select.SelectAnalysis) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) Symbol(io.crate.analyze.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) SubqueryAnalyzer(io.crate.analyze.expressions.SubqueryAnalyzer)

Aggregations

ExpressionAnalysisContext (io.crate.analyze.expressions.ExpressionAnalysisContext)9 ExpressionAnalyzer (io.crate.analyze.expressions.ExpressionAnalyzer)9 Symbol (io.crate.analyze.symbol.Symbol)7 DocTableRelation (io.crate.analyze.relations.DocTableRelation)3 DocTableInfo (io.crate.metadata.doc.DocTableInfo)3 TableReferenceResolver (io.crate.analyze.expressions.TableReferenceResolver)2 ValueNormalizer (io.crate.analyze.expressions.ValueNormalizer)2 WhereClauseAnalyzer (io.crate.analyze.where.WhereClauseAnalyzer)2 TableInfo (io.crate.metadata.table.TableInfo)2 Assignment (io.crate.sql.tree.Assignment)2 Settings (org.elasticsearch.common.settings.Settings)2 SubqueryAnalyzer (io.crate.analyze.expressions.SubqueryAnalyzer)1 FieldProvider (io.crate.analyze.relations.FieldProvider)1 NameFieldProvider (io.crate.analyze.relations.NameFieldProvider)1 QueriedDocTable (io.crate.analyze.relations.QueriedDocTable)1 SelectAnalysis (io.crate.analyze.relations.select.SelectAnalysis)1 DynamicReference (io.crate.analyze.symbol.DynamicReference)1 SymbolPrinter (io.crate.analyze.symbol.format.SymbolPrinter)1 UnsupportedFeatureException (io.crate.exceptions.UnsupportedFeatureException)1 TransactionContext (io.crate.metadata.TransactionContext)1