Search in sources :

Example 21 with DocTableRelation

use of io.crate.analyze.relations.DocTableRelation 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 22 with DocTableRelation

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

the class MultiSourceFetchPushDown method process.

void process() {
    remainingOutputs = statement.querySpec().outputs();
    statement.querySpec().outputs(new ArrayList<>());
    HashMap<Symbol, Symbol> topLevelOutputMap = new HashMap<>(statement.canBeFetched().size());
    HashMap<Symbol, Symbol> mssOutputMap = new HashMap<>(statement.querySpec().outputs().size() + 2);
    ArrayList<Symbol> mssOutputs = new ArrayList<>(statement.sources().size() + statement.requiredForQuery().size());
    for (Map.Entry<QualifiedName, RelationSource> entry : statement.sources().entrySet()) {
        RelationSource source = entry.getValue();
        if (!(source.relation() instanceof DocTableRelation)) {
            int index = 0;
            for (Symbol output : source.querySpec().outputs()) {
                RelationColumn rc = new RelationColumn(entry.getKey(), index++, output.valueType());
                mssOutputs.add(rc);
                mssOutputMap.put(output, rc);
                topLevelOutputMap.put(output, new InputColumn(mssOutputs.size() - 1, output.valueType()));
            }
            continue;
        }
        DocTableRelation rel = (DocTableRelation) source.relation();
        HashSet<Field> canBeFetched = filterByRelation(statement.canBeFetched(), rel);
        if (!canBeFetched.isEmpty()) {
            RelationColumn fetchIdColumn = new RelationColumn(entry.getKey(), 0, DataTypes.LONG);
            mssOutputs.add(fetchIdColumn);
            InputColumn fetchIdInput = new InputColumn(mssOutputs.size() - 1);
            ArrayList<Symbol> qtOutputs = new ArrayList<>(source.querySpec().outputs().size() - canBeFetched.size() + 1);
            Reference fetchId = rel.tableInfo().getReference(DocSysColumns.FETCHID);
            qtOutputs.add(fetchId);
            for (Symbol output : source.querySpec().outputs()) {
                if (!canBeFetched.contains(output)) {
                    qtOutputs.add(output);
                    RelationColumn rc = new RelationColumn(entry.getKey(), qtOutputs.size() - 1, output.valueType());
                    mssOutputs.add(rc);
                    mssOutputMap.put(output, rc);
                    topLevelOutputMap.put(output, new InputColumn(mssOutputs.size() - 1, output.valueType()));
                }
            }
            for (Field field : canBeFetched) {
                FetchReference fr = new FetchReference(fetchIdInput, DocReferenceConverter.toSourceLookup(rel.resolveField(field)));
                allocateFetchedReference(fr, rel);
                topLevelOutputMap.put(field, fr);
            }
            source.querySpec().outputs(qtOutputs);
        } else {
            int index = 0;
            for (Symbol output : source.querySpec().outputs()) {
                RelationColumn rc = new RelationColumn(entry.getKey(), index++, output.valueType());
                mssOutputs.add(rc);
                mssOutputMap.put(output, rc);
                topLevelOutputMap.put(output, new InputColumn(mssOutputs.size() - 1, output.valueType()));
            }
        }
    }
    statement.querySpec().outputs(mssOutputs);
    MappingSymbolVisitor.inPlace().processInplace(remainingOutputs, topLevelOutputMap);
    if (statement.querySpec().orderBy().isPresent()) {
        MappingSymbolVisitor.inPlace().processInplace(statement.querySpec().orderBy().get().orderBySymbols(), mssOutputMap);
    }
}
Also used : RelationSource(io.crate.analyze.RelationSource) Reference(io.crate.metadata.Reference) QualifiedName(io.crate.sql.tree.QualifiedName) DocTableRelation(io.crate.analyze.relations.DocTableRelation)

Example 23 with DocTableRelation

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

the class DeleteStatementPlanner method planDelete.

public static Plan planDelete(DeleteAnalyzedStatement analyzedStatement, Planner.Context context) {
    DocTableRelation tableRelation = analyzedStatement.analyzedRelation();
    List<WhereClause> whereClauses = new ArrayList<>(analyzedStatement.whereClauses().size());
    List<DocKeys.DocKey> docKeys = new ArrayList<>(analyzedStatement.whereClauses().size());
    Map<Integer, Integer> itemToBulkIdx = new HashMap<>();
    int bulkIdx = -1;
    int itemIdx = 0;
    for (WhereClause whereClause : analyzedStatement.whereClauses()) {
        bulkIdx++;
        if (whereClause.noMatch()) {
            continue;
        }
        if (whereClause.docKeys().isPresent() && whereClause.docKeys().get().size() == 1) {
            DocKeys.DocKey docKey = whereClause.docKeys().get().getOnlyKey();
            if (docKey.id() != null) {
                docKeys.add(docKey);
                itemToBulkIdx.put(itemIdx, bulkIdx);
                itemIdx++;
            }
        } else if (!whereClause.noMatch()) {
            whereClauses.add(whereClause);
        }
    }
    if (!docKeys.isEmpty()) {
        return new ESDelete(context.jobId(), context.nextExecutionPhaseId(), tableRelation.tableInfo(), docKeys, itemToBulkIdx, analyzedStatement.whereClauses().size());
    } else if (!whereClauses.isEmpty()) {
        return deleteByQuery(tableRelation.tableInfo(), whereClauses, context);
    }
    return new NoopPlan(context.jobId());
}
Also used : NoopPlan(io.crate.planner.NoopPlan) DocKeys(io.crate.analyze.where.DocKeys) WhereClause(io.crate.analyze.WhereClause) ESDelete(io.crate.planner.node.dml.ESDelete) DocTableRelation(io.crate.analyze.relations.DocTableRelation)

Example 24 with DocTableRelation

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

the class DeleteAnalyzerTest method testDeleteWhere.

@Test
public void testDeleteWhere() throws Exception {
    AnalyzedDeleteStatement delete = e.analyze("delete from users where name='Trillian'");
    DocTableRelation tableRelation = delete.relation();
    TableInfo tableInfo = tableRelation.tableInfo();
    assertThat(USER_TABLE_IDENT, equalTo(tableInfo.ident()));
    assertThat(tableInfo.rowGranularity(), is(RowGranularity.DOC));
    assertThat(delete.query(), isFunction(EqOperator.NAME, isReference("name"), isLiteral("Trillian")));
}
Also used : DocTableRelation(io.crate.analyze.relations.DocTableRelation) TableInfo(io.crate.metadata.table.TableInfo) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 25 with DocTableRelation

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

the class AbstractTableFunctionsTest method prepareFunctions.

@Before
public void prepareFunctions() {
    DocTableRelation relation = mock(DocTableRelation.class);
    RelationName relationName = new RelationName(null, "t");
    when(relation.getField(any(ColumnIdent.class), any(Operation.class), any(Boolean.class))).thenReturn(new Reference(new ReferenceIdent(relationName, "name"), RowGranularity.NODE, DataTypes.STRING, 0, null));
    sqlExpressions = new SqlExpressions(Map.of(relationName, relation));
}
Also used : ColumnIdent(io.crate.metadata.ColumnIdent) Reference(io.crate.metadata.Reference) DocTableRelation(io.crate.analyze.relations.DocTableRelation) RelationName(io.crate.metadata.RelationName) Operation(io.crate.metadata.table.Operation) SqlExpressions(io.crate.testing.SqlExpressions) ReferenceIdent(io.crate.metadata.ReferenceIdent) Before(org.junit.Before)

Aggregations

DocTableRelation (io.crate.analyze.relations.DocTableRelation)36 DocTableInfo (io.crate.metadata.doc.DocTableInfo)17 RelationName (io.crate.metadata.RelationName)13 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)10 Symbol (io.crate.expression.symbol.Symbol)10 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)10 SqlExpressions (io.crate.testing.SqlExpressions)10 Test (org.junit.Test)10 Before (org.junit.Before)9 ExpressionAnalysisContext (io.crate.analyze.expressions.ExpressionAnalysisContext)7 ExpressionAnalyzer (io.crate.analyze.expressions.ExpressionAnalyzer)7 EvaluatingNormalizer (io.crate.expression.eval.EvaluatingNormalizer)7 Reference (io.crate.metadata.Reference)6 SQLExecutor (io.crate.testing.SQLExecutor)6 WhereClause (io.crate.analyze.WhereClause)5 TableInfo (io.crate.metadata.table.TableInfo)4 QueriedSelectRelation (io.crate.analyze.QueriedSelectRelation)3 FullQualifiedNameFieldProvider (io.crate.analyze.relations.FullQualifiedNameFieldProvider)3 NameFieldProvider (io.crate.analyze.relations.NameFieldProvider)3 StatementAnalysisContext (io.crate.analyze.relations.StatementAnalysisContext)3