Search in sources :

Example 11 with QueriedDocTable

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

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

the class SubSelectAnalyzerTest method testSimpleSubSelect.

@Test
public void testSimpleSubSelect() throws Exception {
    SelectAnalyzedStatement statement = analyze("select aliased_sub.x / aliased_sub.i from (select x, i from t1) as aliased_sub");
    QueriedDocTable relation = (QueriedDocTable) statement.relation();
    assertThat(relation.fields().size(), is(1));
    assertThat(relation.fields().get(0), isField("(x / i)"));
    assertThat(relation.tableRelation().tableInfo(), is(T1_INFO));
}
Also used : QueriedDocTable(io.crate.analyze.relations.QueriedDocTable) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 13 with QueriedDocTable

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

the class QueriedDocTableFetchPushDownTest method testScoreGetsPushedDown.

@Test
public void testScoreGetsPushedDown() throws Exception {
    QuerySpec qs = new QuerySpec();
    qs.outputs(Lists.newArrayList(REF_A, REF_I, REF_SCORE));
    qs.orderBy(new OrderBy(Lists.newArrayList(REF_I), new boolean[] { true }, new Boolean[] { false }));
    QueriedDocTableFetchPushDown pd = new QueriedDocTableFetchPushDown(new QueriedDocTable(TABLE_REL, qs));
    QueriedDocTable sub = pd.pushDown();
    assertThat(qs, isSQL("SELECT FETCH(INPUT(0), s.t._doc['a']), INPUT(1), INPUT(2) ORDER BY INPUT(1) DESC NULLS LAST"));
    assertThat(sub.querySpec(), isSQL("SELECT s.t._fetchid, s.t.i, s.t._score ORDER BY s.t.i DESC NULLS LAST"));
}
Also used : OrderBy(io.crate.analyze.OrderBy) QueriedDocTable(io.crate.analyze.relations.QueriedDocTable) QuerySpec(io.crate.analyze.QuerySpec) Test(org.junit.Test)

Example 14 with QueriedDocTable

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

the class QueriedDocTableFetchPushDownTest method testPushDownOrderRefUsedInFunction.

@Test
public void testPushDownOrderRefUsedInFunction() throws Exception {
    QuerySpec qs = new QuerySpec();
    Function funcOfI = abs(REF_I);
    qs.outputs(Lists.newArrayList(REF_A, REF_I, funcOfI));
    qs.orderBy(new OrderBy(Lists.newArrayList(REF_I), new boolean[] { true }, new Boolean[] { false }));
    QueriedDocTableFetchPushDown pd = new QueriedDocTableFetchPushDown(new QueriedDocTable(TABLE_REL, qs));
    QueriedDocTable sub = pd.pushDown();
    assertThat(qs, isSQL("SELECT FETCH(INPUT(0), s.t._doc['a']), INPUT(1), abs(INPUT(1)) ORDER BY INPUT(1) DESC NULLS LAST"));
    assertThat(sub.querySpec(), isSQL("SELECT s.t._fetchid, s.t.i ORDER BY s.t.i DESC NULLS LAST"));
}
Also used : OrderBy(io.crate.analyze.OrderBy) Function(io.crate.analyze.symbol.Function) AbsFunction(io.crate.operation.scalar.arithmetic.AbsFunction) QueriedDocTable(io.crate.analyze.relations.QueriedDocTable) QuerySpec(io.crate.analyze.QuerySpec) Test(org.junit.Test)

Example 15 with QueriedDocTable

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

the class QueriedDocTableFetchPushDownTest method testPushDownWithNestedOrder.

@Test
public void testPushDownWithNestedOrder() throws Exception {
    QuerySpec qs = new QuerySpec();
    qs.outputs(Lists.newArrayList(REF_A, REF_I));
    qs.orderBy(new OrderBy(Lists.newArrayList(abs(REF_I)), new boolean[] { true }, new Boolean[] { false }));
    QueriedDocTableFetchPushDown pd = new QueriedDocTableFetchPushDown(new QueriedDocTable(TABLE_REL, qs));
    QueriedDocTable sub = pd.pushDown();
    assertThat(qs, isSQL("SELECT FETCH(INPUT(0), s.t._doc['a']), FETCH(INPUT(0), s.t._doc['i']) ORDER BY INPUT(1) DESC NULLS LAST"));
    assertThat(sub.querySpec(), isSQL("SELECT s.t._fetchid, abs(s.t.i) ORDER BY abs(s.t.i) DESC NULLS LAST"));
}
Also used : OrderBy(io.crate.analyze.OrderBy) QueriedDocTable(io.crate.analyze.relations.QueriedDocTable) QuerySpec(io.crate.analyze.QuerySpec) Test(org.junit.Test)

Aggregations

QueriedDocTable (io.crate.analyze.relations.QueriedDocTable)16 Test (org.junit.Test)14 QuerySpec (io.crate.analyze.QuerySpec)11 OrderBy (io.crate.analyze.OrderBy)8 CrateUnitTest (io.crate.test.integration.CrateUnitTest)4 Function (io.crate.analyze.symbol.Function)2 AbsFunction (io.crate.operation.scalar.arithmetic.AbsFunction)2 ExpressionAnalysisContext (io.crate.analyze.expressions.ExpressionAnalysisContext)1 ExpressionAnalyzer (io.crate.analyze.expressions.ExpressionAnalyzer)1 DocTableRelation (io.crate.analyze.relations.DocTableRelation)1 Symbol (io.crate.analyze.symbol.Symbol)1 UnsupportedFeatureException (io.crate.exceptions.UnsupportedFeatureException)1 Reference (io.crate.metadata.Reference)1 DocTableInfo (io.crate.metadata.doc.DocTableInfo)1 TableInfo (io.crate.metadata.table.TableInfo)1 WriterProjection (io.crate.planner.projection.WriterProjection)1 Nullable (javax.annotation.Nullable)1 Settings (org.elasticsearch.common.settings.Settings)1