Search in sources :

Example 6 with WhereClause

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

the class WhereClauseAnalyzerTest method testSelectFromPartitionedTableWithoutPKInWhereClause.

@Test
public void testSelectFromPartitionedTableWithoutPKInWhereClause() throws Exception {
    WhereClause whereClause = analyzeSelectWhere("select id from parted where match(name, 'name')");
    assertThat(whereClause.docKeys().isPresent(), is(false));
    assertThat(whereClause.partitions(), empty());
}
Also used : WhereClause(io.crate.analyze.WhereClause) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 7 with WhereClause

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

the class WhereClauseAnalyzerTest method testMultiplePrimaryKeysAndInvalidColumn.

@Test
public void testMultiplePrimaryKeysAndInvalidColumn() throws Exception {
    WhereClause whereClause = analyzeSelectWhere("select name from users where id = 2 or id = 1 and name = 'foo'");
    assertFalse(whereClause.docKeys().isPresent());
}
Also used : WhereClause(io.crate.analyze.WhereClause) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 8 with WhereClause

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

the class WhereClauseAnalyzerTest method testSelectFromPartitionedTable.

@Test
public void testSelectFromPartitionedTable() throws Exception {
    String partition1 = new PartitionName("parted", Arrays.asList(new BytesRef("1395874800000"))).asIndexName();
    String partition2 = new PartitionName("parted", Arrays.asList(new BytesRef("1395961200000"))).asIndexName();
    String partition3 = new PartitionName("parted", new ArrayList<BytesRef>() {

        {
            add(null);
        }
    }).asIndexName();
    WhereClause whereClause = analyzeSelectWhere("select id, name from parted where date = 1395874800000");
    assertEquals(ImmutableList.of(partition1), whereClause.partitions());
    assertFalse(whereClause.hasQuery());
    assertFalse(whereClause.noMatch());
    whereClause = analyzeSelectWhere("select id, name from parted where date = 1395874800000 " + "and substr(name, 0, 4) = 'this'");
    assertEquals(ImmutableList.of(partition1), whereClause.partitions());
    assertThat(whereClause.hasQuery(), is(true));
    assertThat(whereClause.noMatch(), is(false));
    whereClause = analyzeSelectWhere("select id, name from parted where date >= 1395874800000");
    assertThat(whereClause.partitions(), containsInAnyOrder(partition1, partition2));
    assertFalse(whereClause.hasQuery());
    assertFalse(whereClause.noMatch());
    whereClause = analyzeSelectWhere("select id, name from parted where date < 1395874800000");
    assertEquals(ImmutableList.of(), whereClause.partitions());
    assertTrue(whereClause.noMatch());
    whereClause = analyzeSelectWhere("select id, name from parted where date = 1395874800000 and date = 1395961200000");
    assertEquals(ImmutableList.of(), whereClause.partitions());
    assertTrue(whereClause.noMatch());
    whereClause = analyzeSelectWhere("select id, name from parted where date = 1395874800000 or date = 1395961200000");
    assertThat(whereClause.partitions(), containsInAnyOrder(partition1, partition2));
    assertFalse(whereClause.hasQuery());
    assertFalse(whereClause.noMatch());
    whereClause = analyzeSelectWhere("select id, name from parted where date < 1395874800000 or date > 1395874800000");
    assertEquals(ImmutableList.of(partition2), whereClause.partitions());
    assertFalse(whereClause.hasQuery());
    assertFalse(whereClause.noMatch());
    whereClause = analyzeSelectWhere("select id, name from parted where date in (1395874800000, 1395961200000)");
    assertThat(whereClause.partitions(), containsInAnyOrder(partition1, partition2));
    assertFalse(whereClause.hasQuery());
    assertFalse(whereClause.noMatch());
    whereClause = analyzeSelectWhere("select id, name from parted where date in (1395874800000, 1395961200000) and id = 1");
    assertThat(whereClause.partitions(), containsInAnyOrder(partition1, partition2));
    assertTrue(whereClause.hasQuery());
    assertFalse(whereClause.noMatch());
    /**
         *
         * obj['col'] = 'undefined' => null as col doesn't exist
         *
         *  partition1: not (true  and null) -> not (null)  -> null -> no match
         *  partition2: not (false and null) -> not (false) -> true -> match
         *  partition3: not (null  and null) -> not (null)  -> null -> no match
         */
    whereClause = analyzeSelectWhere("select id, name from parted where not (date = 1395874800000 and obj['col'] = 'undefined')");
    assertThat(whereClause.partitions(), containsInAnyOrder(partition2));
    assertThat(whereClause.hasQuery(), is(false));
    assertThat(whereClause.noMatch(), is(false));
    whereClause = analyzeSelectWhere("select id, name from parted where date in (1395874800000) or date in (1395961200000)");
    assertThat(whereClause.partitions(), containsInAnyOrder(partition1, partition2));
    assertFalse(whereClause.hasQuery());
    assertFalse(whereClause.noMatch());
    whereClause = analyzeSelectWhere("select id, name from parted where date = 1395961200000 and id = 1");
    assertEquals(ImmutableList.of(partition2), whereClause.partitions());
    assertTrue(whereClause.hasQuery());
    assertFalse(whereClause.noMatch());
    whereClause = analyzeSelectWhere("select id, name from parted where (date =1395874800000 or date = 1395961200000) and id = 1");
    assertThat(whereClause.partitions(), containsInAnyOrder(partition1, partition2));
    assertTrue(whereClause.hasQuery());
    assertFalse(whereClause.noMatch());
    whereClause = analyzeSelectWhere("select id, name from parted where date = 1395874800000 and id is null");
    assertEquals(ImmutableList.of(partition1), whereClause.partitions());
    assertTrue(whereClause.hasQuery());
    assertFalse(whereClause.noMatch());
    whereClause = analyzeSelectWhere("select id, name from parted where date is null and id = 1");
    assertEquals(ImmutableList.of(partition3), whereClause.partitions());
    assertTrue(whereClause.hasQuery());
    assertFalse(whereClause.noMatch());
    whereClause = analyzeSelectWhere("select id, name from parted where 1395874700000 < date and date < 1395961200001");
    assertThat(whereClause.partitions(), containsInAnyOrder(partition1, partition2));
    assertFalse(whereClause.hasQuery());
    assertFalse(whereClause.noMatch());
    whereClause = analyzeSelectWhere("select id, name from parted where '2014-03-16T22:58:20' < date and date < '2014-03-27T23:00:01'");
    assertThat(whereClause.partitions(), containsInAnyOrder(partition1, partition2));
    assertFalse(whereClause.hasQuery());
    assertFalse(whereClause.noMatch());
}
Also used : PartitionName(io.crate.metadata.PartitionName) ArrayList(java.util.ArrayList) WhereClause(io.crate.analyze.WhereClause) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 9 with WhereClause

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

the class WhereClauseAnalyzerTest method testSelectWherePartitionedByColumn.

@Test
public void testSelectWherePartitionedByColumn() throws Exception {
    WhereClause whereClause = analyzeSelectWhere("select id from parted where date = 1395874800000");
    assertThat(whereClause.hasQuery(), is(false));
    assertThat(whereClause.noMatch(), is(false));
    assertThat(whereClause.partitions(), Matchers.contains(new PartitionName("parted", Arrays.asList(new BytesRef("1395874800000"))).asIndexName()));
}
Also used : PartitionName(io.crate.metadata.PartitionName) WhereClause(io.crate.analyze.WhereClause) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 10 with WhereClause

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

the class WhereClauseAnalyzerTest method testAnyEqConvertableArrayTypeLiterals.

@Test
public void testAnyEqConvertableArrayTypeLiterals() throws Exception {
    WhereClause whereClause = analyzeSelectWhere("select * from users where name = any([1, 2, 3])");
    assertThat(whereClause.query(), isFunction(AnyEqOperator.NAME, ImmutableList.<DataType>of(DataTypes.STRING, new ArrayType(DataTypes.STRING))));
}
Also used : ArrayType(io.crate.types.ArrayType) WhereClause(io.crate.analyze.WhereClause) DataType(io.crate.types.DataType) 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