Search in sources :

Example 96 with Symbol

use of io.crate.analyze.symbol.Symbol in project crate by crate.

the class OrderBy method merge.

public OrderBy merge(@Nullable OrderBy otherOrderBy) {
    if (otherOrderBy != null) {
        List<Symbol> newOrderBySymbols = otherOrderBy.orderBySymbols();
        List<Boolean> newReverseFlags = new ArrayList<>(Booleans.asList(otherOrderBy.reverseFlags()));
        List<Boolean> newNullsFirst = new ArrayList<>(Arrays.asList(otherOrderBy.nullsFirst()));
        for (int i = 0; i < orderBySymbols.size(); i++) {
            Symbol orderBySymbol = orderBySymbols.get(i);
            int idx = newOrderBySymbols.indexOf(orderBySymbol);
            if (idx == -1) {
                newOrderBySymbols.add(orderBySymbol);
                newReverseFlags.add(reverseFlags[i]);
                newNullsFirst.add(nullsFirst[i]);
            } else {
                if (newReverseFlags.get(idx) != reverseFlags[i]) {
                    throw new AmbiguousOrderByException(orderBySymbol);
                }
                if (newNullsFirst.get(idx) != nullsFirst[i]) {
                    throw new AmbiguousOrderByException(orderBySymbol);
                }
            }
        }
        this.orderBySymbols = newOrderBySymbols;
        this.reverseFlags = Booleans.toArray(newReverseFlags);
        this.nullsFirst = newNullsFirst.toArray(new Boolean[0]);
    }
    return this;
}
Also used : Symbol(io.crate.analyze.symbol.Symbol) AmbiguousOrderByException(io.crate.exceptions.AmbiguousOrderByException)

Example 97 with Symbol

use of io.crate.analyze.symbol.Symbol in project crate by crate.

the class ColumnIndexWriterProjection method writeTo.

@Override
public void writeTo(StreamOutput out) throws IOException {
    super.writeTo(out);
    if (columnSymbols == null) {
        out.writeBoolean(false);
    } else {
        out.writeBoolean(true);
        Symbols.toStream(columnSymbols, out);
    }
    if (columnReferences == null) {
        out.writeBoolean(false);
    } else {
        out.writeBoolean(true);
        out.writeVInt(columnReferences.size());
        for (Reference columnIdent : columnReferences) {
            Reference.toStream(columnIdent, out);
        }
    }
    if (onDuplicateKeyAssignments == null) {
        out.writeBoolean(false);
    } else {
        out.writeBoolean(true);
        out.writeVInt(onDuplicateKeyAssignments.size());
        for (Map.Entry<Reference, Symbol> entry : onDuplicateKeyAssignments.entrySet()) {
            Reference.toStream(entry.getKey(), out);
            Symbols.toStream(entry.getValue(), out);
        }
    }
}
Also used : Reference(io.crate.metadata.Reference) Symbol(io.crate.analyze.symbol.Symbol) HashMap(java.util.HashMap) Map(java.util.Map)

Example 98 with Symbol

use of io.crate.analyze.symbol.Symbol in project crate by crate.

the class SplitPointVisitor method visitFunction.

@Override
public Void visitFunction(Function symbol, Context context) {
    if (symbol.info().type() == FunctionInfo.Type.AGGREGATE) {
        context.allocateAggregate(symbol);
        context.aggregateSeen = true;
        for (Symbol arg : symbol.arguments()) {
            context.allocateCollectSymbol(arg);
        }
        return null;
    }
    return super.visitFunction(symbol, context);
}
Also used : Symbol(io.crate.analyze.symbol.Symbol)

Example 99 with Symbol

use of io.crate.analyze.symbol.Symbol in project crate by crate.

the class CopyStatementPlanner method planCopyFrom.

public Plan planCopyFrom(CopyFromAnalyzedStatement analysis, Planner.Context context) {
    /**
         * copy from has two "modes":
         *
         * 1: non-partitioned tables or partitioned tables with partition ident --> import into single es index
         *    -> collect raw source and import as is
         *
         * 2: partitioned table without partition ident
         *    -> collect document and partition by values
         *    -> exclude partitioned by columns from document
         *    -> insert into es index (partition determined by partition by value)
         */
    DocTableInfo table = analysis.table();
    int clusteredByPrimaryKeyIdx = table.primaryKey().indexOf(analysis.table().clusteredBy());
    List<String> partitionedByNames;
    String partitionIdent = null;
    List<BytesRef> partitionValues;
    if (analysis.partitionIdent() == null) {
        if (table.isPartitioned()) {
            partitionedByNames = Lists.newArrayList(Lists.transform(table.partitionedBy(), ColumnIdent::fqn));
        } else {
            partitionedByNames = Collections.emptyList();
        }
        partitionValues = ImmutableList.of();
    } else {
        assert table.isPartitioned() : "table must be partitioned if partitionIdent is set";
        // partitionIdent is present -> possible to index raw source into concrete es index
        partitionValues = PartitionName.decodeIdent(analysis.partitionIdent());
        partitionIdent = analysis.partitionIdent();
        partitionedByNames = Collections.emptyList();
    }
    SourceIndexWriterProjection sourceIndexWriterProjection = new SourceIndexWriterProjection(table.ident(), partitionIdent, table.getReference(DocSysColumns.RAW), table.primaryKey(), table.partitionedBy(), partitionValues, table.clusteredBy(), clusteredByPrimaryKeyIdx, analysis.settings(), null, partitionedByNames.size() > 0 ? partitionedByNames.toArray(new String[partitionedByNames.size()]) : null, // autoCreateIndices
    table.isPartitioned());
    List<Projection> projections = Collections.<Projection>singletonList(sourceIndexWriterProjection);
    partitionedByNames.removeAll(Lists.transform(table.primaryKey(), ColumnIdent::fqn));
    int referencesSize = table.primaryKey().size() + partitionedByNames.size() + 1;
    referencesSize = clusteredByPrimaryKeyIdx == -1 ? referencesSize + 1 : referencesSize;
    List<Symbol> toCollect = new ArrayList<>(referencesSize);
    // add primaryKey columns
    for (ColumnIdent primaryKey : table.primaryKey()) {
        toCollect.add(table.getReference(primaryKey));
    }
    // add partitioned columns (if not part of primaryKey)
    Set<Reference> referencedReferences = new HashSet<>();
    for (String partitionedColumn : partitionedByNames) {
        Reference reference = table.getReference(ColumnIdent.fromPath(partitionedColumn));
        Symbol symbol;
        if (reference instanceof GeneratedReference) {
            symbol = ((GeneratedReference) reference).generatedExpression();
            referencedReferences.addAll(((GeneratedReference) reference).referencedReferences());
        } else {
            symbol = reference;
        }
        toCollect.add(symbol);
    }
    // add clusteredBy column (if not part of primaryKey)
    if (clusteredByPrimaryKeyIdx == -1 && table.clusteredBy() != null && !DocSysColumns.ID.equals(table.clusteredBy())) {
        toCollect.add(table.getReference(table.clusteredBy()));
    }
    // add _raw or _doc
    if (table.isPartitioned() && analysis.partitionIdent() == null) {
        toCollect.add(table.getReference(DocSysColumns.DOC));
    } else {
        toCollect.add(table.getReference(DocSysColumns.RAW));
    }
    // add columns referenced by generated columns which are used as partitioned by column
    for (Reference reference : referencedReferences) {
        if (!toCollect.contains(reference)) {
            toCollect.add(reference);
        }
    }
    DiscoveryNodes allNodes = clusterService.state().nodes();
    FileUriCollectPhase collectPhase = new FileUriCollectPhase(context.jobId(), context.nextExecutionPhaseId(), "copyFrom", getExecutionNodes(allNodes, analysis.settings().getAsInt("num_readers", allNodes.getSize()), analysis.nodePredicate()), analysis.uri(), toCollect, projections, analysis.settings().get("compression", null), analysis.settings().getAsBoolean("shared", null));
    Collect collect = new Collect(collectPhase, TopN.NO_LIMIT, 0, 1, 1, null);
    return Merge.ensureOnHandler(collect, context, Collections.singletonList(MergeCountProjection.INSTANCE));
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) GeneratedReference(io.crate.metadata.GeneratedReference) Collect(io.crate.planner.node.dql.Collect) Symbol(io.crate.analyze.symbol.Symbol) GeneratedReference(io.crate.metadata.GeneratedReference) Reference(io.crate.metadata.Reference) SourceIndexWriterProjection(io.crate.planner.projection.SourceIndexWriterProjection) WriterProjection(io.crate.planner.projection.WriterProjection) MergeCountProjection(io.crate.planner.projection.MergeCountProjection) SourceIndexWriterProjection(io.crate.planner.projection.SourceIndexWriterProjection) Projection(io.crate.planner.projection.Projection) FileUriCollectPhase(io.crate.planner.node.dql.FileUriCollectPhase) ColumnIdent(io.crate.metadata.ColumnIdent) BytesRef(org.apache.lucene.util.BytesRef) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes)

Example 100 with Symbol

use of io.crate.analyze.symbol.Symbol in project crate by crate.

the class InsertFromSubQueryAnalyzerTest method testImplicitTypeCasting.

@Test
public void testImplicitTypeCasting() throws Exception {
    InsertFromSubQueryAnalyzedStatement statement = e.analyze("insert into users (id, name, shape) (" + "  select id, other_id, name from users " + "  where name = 'Trillian'" + ")");
    List<Symbol> outputSymbols = statement.subQueryRelation().querySpec().outputs();
    assertThat(statement.columns().size(), is(outputSymbols.size()));
    assertThat(outputSymbols.get(1), instanceOf(Function.class));
    Function castFunction = (Function) outputSymbols.get(1);
    assertThat(castFunction, isFunction(CastFunctionResolver.FunctionNames.TO_STRING));
    Function geoCastFunction = (Function) outputSymbols.get(2);
    assertThat(geoCastFunction, isFunction(CastFunctionResolver.FunctionNames.TO_GEO_SHAPE));
}
Also used : Function(io.crate.analyze.symbol.Function) SubstrFunction(io.crate.operation.scalar.SubstrFunction) Symbol(io.crate.analyze.symbol.Symbol) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Aggregations

Symbol (io.crate.analyze.symbol.Symbol)109 Test (org.junit.Test)51 CrateUnitTest (io.crate.test.integration.CrateUnitTest)40 Function (io.crate.analyze.symbol.Function)14 Input (io.crate.data.Input)14 Reference (io.crate.metadata.Reference)11 WhereClause (io.crate.analyze.WhereClause)10 InputColumn (io.crate.analyze.symbol.InputColumn)8 InputFactory (io.crate.operation.InputFactory)8 OrderBy (io.crate.analyze.OrderBy)7 ExpressionAnalysisContext (io.crate.analyze.expressions.ExpressionAnalysisContext)6 ExpressionAnalyzer (io.crate.analyze.expressions.ExpressionAnalyzer)6 AbstractScalarFunctionsTest (io.crate.operation.scalar.AbstractScalarFunctionsTest)6 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)6 StreamInput (org.elasticsearch.common.io.stream.StreamInput)6 ImmutableList (com.google.common.collect.ImmutableList)5 DocTableInfo (io.crate.metadata.doc.DocTableInfo)5 TableInfo (io.crate.metadata.table.TableInfo)5 QuerySpec (io.crate.analyze.QuerySpec)4 Literal (io.crate.analyze.symbol.Literal)4