Search in sources :

Example 1 with SqlExpressions

use of io.crate.testing.SqlExpressions in project crate by crate.

the class FilterProjectionTest method testStreaming.

@Test
public void testStreaming() throws Exception {
    SqlExpressions sqlExpressions = new SqlExpressions(T3.SOURCES, T3.TR_1);
    FilterProjection p = new FilterProjection(sqlExpressions.normalize(sqlExpressions.asSymbol("a = 'foo'")), ImmutableList.of(new InputColumn(1)));
    p.requiredGranularity(RowGranularity.SHARD);
    BytesStreamOutput out = new BytesStreamOutput();
    Projection.toStream(p, out);
    StreamInput in = StreamInput.wrap(out.bytes());
    FilterProjection p2 = (FilterProjection) Projection.fromStream(in);
    assertEquals(p, p2);
}
Also used : InputColumn(io.crate.analyze.symbol.InputColumn) StreamInput(org.elasticsearch.common.io.stream.StreamInput) SqlExpressions(io.crate.testing.SqlExpressions) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 2 with SqlExpressions

use of io.crate.testing.SqlExpressions in project crate by crate.

the class CurrentSchemaFunctionTest method testNormalizeCurrentSchemaCustomSchema.

@Test
public void testNormalizeCurrentSchemaCustomSchema() throws Exception {
    sqlExpressions = new SqlExpressions(tableSources, new SessionContext(0, Option.NONE, "custom_schema"));
    functions = sqlExpressions.getInstance(Functions.class);
    assertNormalize("current_schema()", isLiteral("custom_schema"), false);
}
Also used : SessionContext(io.crate.action.sql.SessionContext) Functions(io.crate.metadata.Functions) SqlExpressions(io.crate.testing.SqlExpressions) AbstractScalarFunctionsTest(io.crate.operation.scalar.AbstractScalarFunctionsTest) Test(org.junit.Test)

Example 3 with SqlExpressions

use of io.crate.testing.SqlExpressions in project crate by crate.

the class LuceneQueryBuilderTest method prepare.

@Before
public void prepare() throws Exception {
    DocTableInfo users = TestingTableInfo.builder(new TableIdent(null, "users"), null).add("name", DataTypes.STRING).add("x", DataTypes.INTEGER).add("d", DataTypes.DOUBLE).add("d_array", new ArrayType(DataTypes.DOUBLE)).add("y_array", new ArrayType(DataTypes.LONG)).add("shape", DataTypes.GEO_SHAPE).add("point", DataTypes.GEO_POINT).build();
    TableRelation usersTr = new TableRelation(users);
    sources = ImmutableMap.of(new QualifiedName("users"), usersTr);
    expressions = new SqlExpressions(sources, usersTr);
    builder = new LuceneQueryBuilder(expressions.getInstance(Functions.class));
    indexCache = mock(IndexCache.class, Answers.RETURNS_MOCKS.get());
    Path tempDir = createTempDir();
    Settings indexSettings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).put("path.home", tempDir).build();
    Index index = new Index(users.ident().indexName());
    when(indexCache.indexSettings()).thenReturn(indexSettings);
    AnalysisService analysisService = createAnalysisService(indexSettings, index);
    mapperService = createMapperService(index, indexSettings, analysisService);
    // @formatter:off
    XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().startObject().startObject("default").startObject("properties").startObject("name").field("type", "string").endObject().startObject("x").field("type", "integer").endObject().startObject("d").field("type", "double").endObject().startObject("point").field("type", "geo_point").endObject().startObject("shape").field("type", "geo_shape").endObject().startObject("d_array").field("type", "array").startObject("inner").field("type", "double").endObject().endObject().startObject("y_array").field("type", "array").startObject("inner").field("type", "integer").endObject().endObject().endObject().endObject().endObject();
    // @formatter:on
    mapperService.merge("default", new CompressedXContent(xContentBuilder.bytes()), MapperService.MergeReason.MAPPING_UPDATE, true);
    indexFieldDataService = mock(IndexFieldDataService.class);
    IndexFieldData geoFieldData = mock(IndexGeoPointFieldData.class);
    when(geoFieldData.getFieldNames()).thenReturn(new MappedFieldType.Names("point"));
    when(indexFieldDataService.getForField(mapperService.smartNameFieldType("point"))).thenReturn(geoFieldData);
}
Also used : Path(java.nio.file.Path) DocTableInfo(io.crate.metadata.doc.DocTableInfo) QualifiedName(io.crate.sql.tree.QualifiedName) TableIdent(io.crate.metadata.TableIdent) Index(org.elasticsearch.index.Index) TableRelation(io.crate.analyze.relations.TableRelation) ArrayType(io.crate.types.ArrayType) IndexFieldDataService(org.elasticsearch.index.fielddata.IndexFieldDataService) IndexCache(org.elasticsearch.index.cache.IndexCache) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) IndexFieldData(org.elasticsearch.index.fielddata.IndexFieldData) IndicesAnalysisService(org.elasticsearch.indices.analysis.IndicesAnalysisService) AnalysisService(org.elasticsearch.index.analysis.AnalysisService) SqlExpressions(io.crate.testing.SqlExpressions) Settings(org.elasticsearch.common.settings.Settings) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) Before(org.junit.Before)

Example 4 with SqlExpressions

use of io.crate.testing.SqlExpressions in project crate by crate.

the class LuceneQueryBuilderTest method testWhereRefEqNullWithDifferentTypes.

@Test
public void testWhereRefEqNullWithDifferentTypes() throws Exception {
    for (DataType type : DataTypes.PRIMITIVE_TYPES) {
        DocTableInfo tableInfo = TestingTableInfo.builder(new TableIdent(null, "test_primitive"), null).add("x", type).build();
        TableRelation tableRelation = new TableRelation(tableInfo);
        Map<QualifiedName, AnalyzedRelation> tableSources = ImmutableMap.of(new QualifiedName(tableInfo.ident().name()), tableRelation);
        SqlExpressions sqlExpressions = new SqlExpressions(tableSources, tableRelation, new Object[] { null }, SessionContext.SYSTEM_SESSION);
        Query query = convert(new WhereClause(sqlExpressions.normalize(sqlExpressions.asSymbol("x = ?"))));
        // must always become a MatchNoDocsQuery (empty BooleanQuery)
        // string: term query with null would cause NPE
        // int/numeric: rangeQuery from null to null would match all
        // bool:  term would match false too because of the condition in the eq query builder
        assertThat(query, instanceOf(BooleanQuery.class));
        assertThat(((BooleanQuery) query).clauses().size(), is(0));
    }
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) 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) QualifiedName(io.crate.sql.tree.QualifiedName) WhereClause(io.crate.analyze.WhereClause) DataType(io.crate.types.DataType) TableIdent(io.crate.metadata.TableIdent) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) SqlExpressions(io.crate.testing.SqlExpressions) TableRelation(io.crate.analyze.relations.TableRelation) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 5 with SqlExpressions

use of io.crate.testing.SqlExpressions in project crate by crate.

the class SymbolToTrueVisitorTest method prepare.

@Before
public void prepare() throws Exception {
    visitor = new SymbolToTrueVisitor();
    DummyRelation dummyRelation = new DummyRelation("clustered_by", "number_of_shards", "table_name", "number_of_replicas", "schema_name");
    ImmutableMap<QualifiedName, AnalyzedRelation> sources = ImmutableMap.<QualifiedName, AnalyzedRelation>of(new QualifiedName("dummy"), dummyRelation);
    expressions = new SqlExpressions(sources);
}
Also used : QualifiedName(io.crate.sql.tree.QualifiedName) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) SqlExpressions(io.crate.testing.SqlExpressions) DummyRelation(io.crate.testing.DummyRelation) Before(org.junit.Before)

Aggregations

SqlExpressions (io.crate.testing.SqlExpressions)18 Test (org.junit.Test)12 CrateUnitTest (io.crate.test.integration.CrateUnitTest)9 QualifiedName (io.crate.sql.tree.QualifiedName)7 WhereClause (io.crate.analyze.WhereClause)5 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)5 TableRelation (io.crate.analyze.relations.TableRelation)5 Before (org.junit.Before)5 Symbol (io.crate.analyze.symbol.Symbol)4 DocTableInfo (io.crate.metadata.doc.DocTableInfo)4 CrateRegexQuery (io.crate.lucene.match.CrateRegexQuery)3 Functions (io.crate.metadata.Functions)3 TableIdent (io.crate.metadata.TableIdent)3 ArrayType (io.crate.types.ArrayType)3 TermsQuery (org.apache.lucene.queries.TermsQuery)3 IntersectsPrefixTreeQuery (org.apache.lucene.spatial.prefix.IntersectsPrefixTreeQuery)3 WithinPrefixTreeQuery (org.apache.lucene.spatial.prefix.WithinPrefixTreeQuery)3 SessionContext (io.crate.action.sql.SessionContext)2 DocTableRelation (io.crate.analyze.relations.DocTableRelation)2 Function (io.crate.analyze.symbol.Function)2