Search in sources :

Example 51 with WhereClause

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

the class WhereClauseAnalyzerTest method test1ColPrimaryKey.

@Test
public void test1ColPrimaryKey() throws Exception {
    WhereClause whereClause = analyzeSelectWhere("select name from users where id='jalla'");
    assertThat(whereClause.docKeys().get(), contains(isDocKey("jalla")));
    whereClause = analyzeSelectWhere("select name from users where 'jalla'=id");
    assertThat(whereClause.docKeys().get(), contains(isDocKey("jalla")));
    whereClause = analyzeSelectWhere("select name from users where id='jalla' and id='jalla'");
    assertThat(whereClause.docKeys().get(), contains(isDocKey("jalla")));
    whereClause = analyzeSelectWhere("select name from users where id='jalla' and (id='jalla' or 1=1)");
    assertThat(whereClause.docKeys().get(), contains(isDocKey("jalla")));
    // since the id is unique it is not possible to have a result here, this is not optimized and just results in
    // no found primary keys
    whereClause = analyzeSelectWhere("select name from users where id='jalla' and id='kelle'");
    assertFalse(whereClause.docKeys().isPresent());
    whereClause = analyzeSelectWhere("select name from users where id='jalla' or name = 'something'");
    assertFalse(whereClause.docKeys().isPresent());
    assertFalse(whereClause.noMatch());
    whereClause = analyzeSelectWhere("select name from users where name = 'something'");
    assertFalse(whereClause.docKeys().isPresent());
    assertFalse(whereClause.noMatch());
}
Also used : WhereClause(io.crate.analyze.WhereClause) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 52 with WhereClause

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

the class LuceneQueryBuilderTest method testEqOnArrayWithTooManyClauses.

@Test
public void testEqOnArrayWithTooManyClauses() throws Exception {
    // should trigger the TooManyClauses exception
    Object[] values = new Object[2000];
    Arrays.fill(values, 10L);
    SqlExpressions sqlExpressions = new SqlExpressions(sources, new Object[] { values });
    Query query = convert(new WhereClause(expressions.normalize(sqlExpressions.asSymbol("y_array = ?"))));
    assertThat(query, instanceOf(BooleanQuery.class));
    BooleanQuery booleanQuery = (BooleanQuery) query;
    assertThat(booleanQuery.clauses().get(0).getQuery(), instanceOf(TermsQuery.class));
    assertThat(booleanQuery.clauses().get(1).getQuery(), instanceOf(GenericFunctionQuery.class));
}
Also used : TermsQuery(org.apache.lucene.queries.TermsQuery) CrateRegexQuery(io.crate.lucene.match.CrateRegexQuery) IntersectsPrefixTreeQuery(org.apache.lucene.spatial.prefix.IntersectsPrefixTreeQuery) WithinPrefixTreeQuery(org.apache.lucene.spatial.prefix.WithinPrefixTreeQuery) WhereClause(io.crate.analyze.WhereClause) SqlExpressions(io.crate.testing.SqlExpressions) TermsQuery(org.apache.lucene.queries.TermsQuery) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 53 with WhereClause

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

the class RoutedCollectPhase method replaceSymbols.

@Override
public void replaceSymbols(Function<Symbol, Symbol> replaceFunction) {
    super.replaceSymbols(replaceFunction);
    if (whereClause.hasQuery()) {
        Symbol query = whereClause.query();
        Symbol newQuery = replaceFunction.apply(query);
        if (query != newQuery) {
            whereClause = new WhereClause(newQuery, whereClause.docKeys().orElse(null), whereClause.partitions());
        }
    }
    Lists2.replaceItems(toCollect, replaceFunction);
    if (orderBy != null) {
        orderBy.replace(replaceFunction);
    }
}
Also used : Symbol(io.crate.analyze.symbol.Symbol) WhereClause(io.crate.analyze.WhereClause)

Example 54 with WhereClause

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

the class LuceneQueryBuilderTest method testEqOnTwoArraysBecomesGenericFunctionQueryAllValuesNull.

@Test
public void testEqOnTwoArraysBecomesGenericFunctionQueryAllValuesNull() throws Exception {
    SqlExpressions sqlExpressions = new SqlExpressions(sources, new Object[] { new Object[] { null, null, null } });
    Query query = convert(new WhereClause(expressions.normalize(sqlExpressions.asSymbol("y_array = ?"))));
    assertThat(query, instanceOf(GenericFunctionQuery.class));
}
Also used : TermsQuery(org.apache.lucene.queries.TermsQuery) CrateRegexQuery(io.crate.lucene.match.CrateRegexQuery) IntersectsPrefixTreeQuery(org.apache.lucene.spatial.prefix.IntersectsPrefixTreeQuery) WithinPrefixTreeQuery(org.apache.lucene.spatial.prefix.WithinPrefixTreeQuery) WhereClause(io.crate.analyze.WhereClause) SqlExpressions(io.crate.testing.SqlExpressions) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 55 with WhereClause

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

the class PlannerTest method testAllocateRouting.

@Test
public void testAllocateRouting() throws Exception {
    TableIdent custom = new TableIdent("custom", "t1");
    TableInfo tableInfo1 = TestingTableInfo.builder(custom, shardRouting("t1")).add("id", DataTypes.INTEGER, null).build();
    TableInfo tableInfo2 = TestingTableInfo.builder(custom, shardRoutingForReplicas("t1")).add("id", DataTypes.INTEGER, null).build();
    Planner.Context plannerContext = new Planner.Context(e.planner, clusterService, UUID.randomUUID(), null, normalizer, new TransactionContext(SessionContext.SYSTEM_SESSION), 0, 0);
    WhereClause whereClause = new WhereClause(new Function(new FunctionInfo(new FunctionIdent(EqOperator.NAME, Arrays.<DataType>asList(DataTypes.INTEGER, DataTypes.INTEGER)), DataTypes.BOOLEAN), Arrays.asList(tableInfo1.getReference(new ColumnIdent("id")), Literal.of(2))));
    plannerContext.allocateRouting(tableInfo1, WhereClause.MATCH_ALL, null);
    plannerContext.allocateRouting(tableInfo2, whereClause, null);
    // 2 routing allocations with different where clause must result in 2 allocated routings
    java.lang.reflect.Field tableRoutings = Planner.Context.class.getDeclaredField("tableRoutings");
    tableRoutings.setAccessible(true);
    Multimap<TableIdent, Planner.TableRouting> routing = (Multimap<TableIdent, Planner.TableRouting>) tableRoutings.get(plannerContext);
    assertThat(routing.size(), is(2));
    // The routings must be the same after merging the locations
    Iterator<Planner.TableRouting> iterator = routing.values().iterator();
    Routing routing1 = iterator.next().routing;
    Routing routing2 = iterator.next().routing;
    assertThat(routing1, is(routing2));
}
Also used : SessionContext(io.crate.action.sql.SessionContext) WhereClause(io.crate.analyze.WhereClause) TableDefinitions.shardRouting(io.crate.analyze.TableDefinitions.shardRouting) Function(io.crate.analyze.symbol.Function) Multimap(com.google.common.collect.Multimap) DataType(io.crate.types.DataType) TableInfo(io.crate.metadata.table.TableInfo) TestingTableInfo(io.crate.metadata.table.TestingTableInfo) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

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