Search in sources :

Example 56 with WhereClause

use of io.crate.analyze.WhereClause in project crate by crate.

the class WhereClauseAnalyzer method analyze.

public WhereClause analyze(WhereClause whereClause, TransactionContext transactionContext) {
    if (!whereClause.hasQuery()) {
        return whereClause;
    }
    Set<Symbol> clusteredBy = null;
    if (whereClause.hasQuery()) {
        WhereClauseValidator.validate(whereClause);
        Symbol query = GENERATED_COLUMN_COMPARISON_REPLACER.replaceIfPossible(whereClause.query(), tableInfo);
        if (!whereClause.query().equals(query)) {
            whereClause = new WhereClause(normalizer.normalize(query, transactionContext));
        }
    }
    List<ColumnIdent> pkCols;
    boolean versionInQuery = Symbols.containsColumn(whereClause.query(), DocSysColumns.VERSION);
    if (versionInQuery) {
        pkCols = new ArrayList<>(tableInfo.primaryKey().size() + 1);
        pkCols.addAll(tableInfo.primaryKey());
        pkCols.add(DocSysColumns.VERSION);
    } else {
        pkCols = tableInfo.primaryKey();
    }
    List<List<Symbol>> pkValues = eqExtractor.extractExactMatches(pkCols, whereClause.query(), transactionContext);
    if (!pkCols.isEmpty() && pkValues != null) {
        int clusterdIdx = -1;
        if (tableInfo.clusteredBy() != null) {
            clusterdIdx = tableInfo.primaryKey().indexOf(tableInfo.clusteredBy());
            clusteredBy = new HashSet<>(pkValues.size());
        }
        List<Integer> partitionsIdx = null;
        if (tableInfo.isPartitioned()) {
            partitionsIdx = new ArrayList<>(tableInfo.partitionedByColumns().size());
            for (ColumnIdent columnIdent : tableInfo.partitionedBy()) {
                int posPartitionColumn = tableInfo.primaryKey().indexOf(columnIdent);
                if (posPartitionColumn >= 0) {
                    partitionsIdx.add(posPartitionColumn);
                }
            }
        }
        whereClause.docKeys(new DocKeys(pkValues, versionInQuery, clusterdIdx, partitionsIdx));
        if (clusterdIdx >= 0) {
            for (List<Symbol> row : pkValues) {
                clusteredBy.add(row.get(clusterdIdx));
            }
            whereClause.clusteredBy(clusteredBy);
        }
    } else {
        clusteredBy = getClusteredByLiterals(whereClause, eqExtractor, transactionContext);
    }
    if (clusteredBy != null) {
        whereClause.clusteredBy(clusteredBy);
    }
    if (tableInfo.isPartitioned() && !whereClause.docKeys().isPresent()) {
        whereClause = resolvePartitions(new WhereClause(normalizer.normalize(whereClause.query(), transactionContext)), tableInfo, functions, transactionContext);
    }
    return whereClause;
}
Also used : Symbol(io.crate.analyze.symbol.Symbol) WhereClause(io.crate.analyze.WhereClause) ImmutableList(com.google.common.collect.ImmutableList)

Example 57 with WhereClause

use of io.crate.analyze.WhereClause in project crate by crate.

the class RelationSplitter method processWhere.

private void processWhere() {
    if (!querySpec.where().hasQuery()) {
        return;
    }
    Symbol query = querySpec.where().query();
    assert query != null : "query must not be null";
    QuerySplittingVisitor.Context context = QuerySplittingVisitor.INSTANCE.process(querySpec.where().query(), joinPairs);
    JoinConditionValidator.INSTANCE.process(context.query(), null);
    querySpec.where(new WhereClause(context.query()));
    for (Map.Entry<QualifiedName, Collection<Symbol>> entry : context.queries().asMap().entrySet()) {
        getSpec(entry.getKey()).where(new WhereClause(AndOperator.join(entry.getValue())));
    }
}
Also used : QualifiedName(io.crate.sql.tree.QualifiedName) WhereClause(io.crate.analyze.WhereClause)

Example 58 with WhereClause

use of io.crate.analyze.WhereClause 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 59 with WhereClause

use of io.crate.analyze.WhereClause in project crate by crate.

the class InternalCountOperationTest method testCount.

@Test
public void testCount() throws Exception {
    execute("create table t (name string) clustered into 1 shards with (number_of_replicas = 0)");
    ensureYellow();
    execute("insert into t (name) values ('Marvin'), ('Arthur'), ('Trillian')");
    execute("refresh table t");
    CountOperation countOperation = internalCluster().getDataNodeInstance(CountOperation.class);
    assertThat(countOperation.count("t", 0, WhereClause.MATCH_ALL), is(3L));
    Schemas schemas = internalCluster().getInstance(Schemas.class);
    TableInfo tableInfo = schemas.getTableInfo(new TableIdent(null, "t"));
    TableRelation tableRelation = new TableRelation(tableInfo);
    Map<QualifiedName, AnalyzedRelation> tableSources = ImmutableMap.<QualifiedName, AnalyzedRelation>of(new QualifiedName(tableInfo.ident().name()), tableRelation);
    SqlExpressions sqlExpressions = new SqlExpressions(tableSources, tableRelation);
    WhereClause whereClause = new WhereClause(sqlExpressions.normalize(sqlExpressions.asSymbol("name = 'Marvin'")));
    assertThat(countOperation.count("t", 0, whereClause), is(1L));
}
Also used : QualifiedName(io.crate.sql.tree.QualifiedName) WhereClause(io.crate.analyze.WhereClause) TableInfo(io.crate.metadata.table.TableInfo) TableIdent(io.crate.metadata.TableIdent) Schemas(io.crate.metadata.Schemas) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) SqlExpressions(io.crate.testing.SqlExpressions) TableRelation(io.crate.analyze.relations.TableRelation) Test(org.junit.Test) SQLTransportIntegrationTest(io.crate.integrationtests.SQLTransportIntegrationTest)

Example 60 with WhereClause

use of io.crate.analyze.WhereClause in project crate by crate.

the class DocLevelCollectTest method testCollectDocLevelWhereClause.

@Test
public void testCollectDocLevelWhereClause() throws Throwable {
    EqOperator op = (EqOperator) functions.get(new FunctionIdent(EqOperator.NAME, ImmutableList.<DataType>of(DataTypes.INTEGER, DataTypes.INTEGER)));
    List<Symbol> toCollect = Collections.<Symbol>singletonList(testDocLevelReference);
    WhereClause whereClause = new WhereClause(new Function(op.info(), Arrays.<Symbol>asList(testDocLevelReference, Literal.of(2))));
    RoutedCollectPhase collectNode = getCollectNode(toCollect, whereClause);
    Bucket result = collect(collectNode);
    assertThat(result, contains(isRow(2)));
}
Also used : Function(io.crate.analyze.symbol.Function) EqOperator(io.crate.operation.operator.EqOperator) Bucket(io.crate.data.Bucket) Symbol(io.crate.analyze.symbol.Symbol) WhereClause(io.crate.analyze.WhereClause) RoutedCollectPhase(io.crate.planner.node.dql.RoutedCollectPhase) SQLTransportIntegrationTest(io.crate.integrationtests.SQLTransportIntegrationTest) Test(org.junit.Test)

Aggregations

WhereClause (io.crate.analyze.WhereClause)61 Test (org.junit.Test)46 CrateUnitTest (io.crate.test.integration.CrateUnitTest)43 Symbol (io.crate.analyze.symbol.Symbol)10 PartitionName (io.crate.metadata.PartitionName)8 BytesRef (org.apache.lucene.util.BytesRef)8 DataType (io.crate.types.DataType)6 TableInfo (io.crate.metadata.table.TableInfo)5 SqlExpressions (io.crate.testing.SqlExpressions)5 QualifiedName (io.crate.sql.tree.QualifiedName)4 ArrayType (io.crate.types.ArrayType)4 ImmutableList (com.google.common.collect.ImmutableList)3 OrderBy (io.crate.analyze.OrderBy)3 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)3 TableRelation (io.crate.analyze.relations.TableRelation)3 Function (io.crate.analyze.symbol.Function)3 SQLTransportIntegrationTest (io.crate.integrationtests.SQLTransportIntegrationTest)3 CrateRegexQuery (io.crate.lucene.match.CrateRegexQuery)3 Literal (io.crate.analyze.symbol.Literal)2 Bucket (io.crate.data.Bucket)2