Search in sources :

Example 1 with AnalyzedRelation

use of io.crate.analyze.relations.AnalyzedRelation 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 2 with AnalyzedRelation

use of io.crate.analyze.relations.AnalyzedRelation 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)

Example 3 with AnalyzedRelation

use of io.crate.analyze.relations.AnalyzedRelation in project crate by crate.

the class NodeStatsCollectSourceTest method filterNodes.

private List<DiscoveryNode> filterNodes(String where) throws NoSuchFieldException, IllegalAccessException {
    // build where clause with id = ?
    SysNodesTableInfo tableInfo = mock(SysNodesTableInfo.class);
    when(tableInfo.ident()).thenReturn(new TableIdent("sys", "nodes"));
    when(tableInfo.getReference(new ColumnIdent("id"))).thenReturn(new Reference(new ReferenceIdent(new TableIdent("sys", "nodes"), "id"), RowGranularity.DOC, DataTypes.STRING));
    when(tableInfo.getReference(SysNodesTableInfo.Columns.NAME)).thenReturn(new Reference(new ReferenceIdent(SysNodesTableInfo.IDENT, SysNodesTableInfo.Columns.NAME), RowGranularity.DOC, DataTypes.STRING));
    when(tableInfo.getReference(SysNodesTableInfo.Columns.HOSTNAME)).thenReturn(new Reference(new ReferenceIdent(SysNodesTableInfo.IDENT, SysNodesTableInfo.Columns.HOSTNAME), RowGranularity.DOC, DataTypes.STRING));
    TableRelation tableRelation = new TableRelation(tableInfo);
    Map<QualifiedName, AnalyzedRelation> tableSources = ImmutableMap.<QualifiedName, AnalyzedRelation>of(new QualifiedName("sys.nodes"), tableRelation);
    SqlExpressions sqlExpressions = new SqlExpressions(tableSources, tableRelation);
    WhereClause whereClause = new WhereClause(sqlExpressions.normalize(sqlExpressions.asSymbol(where)));
    List<DiscoveryNode> nodes = Lists.newArrayList(NodeStatsCollectSource.nodeIds(whereClause, discoveryNodes, getFunctions()));
    Collections.sort(nodes, new Comparator<DiscoveryNode>() {

        @Override
        public int compare(DiscoveryNode o1, DiscoveryNode o2) {
            return o1.getId().compareTo(o2.getId());
        }
    });
    return nodes;
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) SysNodesTableInfo(io.crate.metadata.sys.SysNodesTableInfo) QualifiedName(io.crate.sql.tree.QualifiedName) WhereClause(io.crate.analyze.WhereClause) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) TableRelation(io.crate.analyze.relations.TableRelation) SqlExpressions(io.crate.testing.SqlExpressions)

Example 4 with AnalyzedRelation

use of io.crate.analyze.relations.AnalyzedRelation in project crate by crate.

the class ExpressionAnalyzerTest method testInSelfJoinCaseFunctionsThatLookTheSameMustNotReuseFunctionAllocation.

@Test
public void testInSelfJoinCaseFunctionsThatLookTheSameMustNotReuseFunctionAllocation() throws Exception {
    TableInfo tableInfo = mock(TableInfo.class);
    when(tableInfo.getReference(new ColumnIdent("id"))).thenReturn(new Reference(new ReferenceIdent(new TableIdent("doc", "t"), "id"), RowGranularity.DOC, DataTypes.INTEGER));
    when(tableInfo.ident()).thenReturn(new TableIdent("doc", "t"));
    TableRelation tr1 = new TableRelation(tableInfo);
    TableRelation tr2 = new TableRelation(tableInfo);
    Map<QualifiedName, AnalyzedRelation> sources = ImmutableMap.of(new QualifiedName("t1"), tr1, new QualifiedName("t2"), tr2);
    ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(functions, SessionContext.SYSTEM_SESSION, paramTypeHints, new FullQualifedNameFieldProvider(sources), null);
    Function andFunction = (Function) expressionAnalyzer.convert(SqlParser.createExpression("not t1.id = 1 and not t2.id = 1"), context);
    Field t1Id = ((Field) ((Function) ((Function) andFunction.arguments().get(0)).arguments().get(0)).arguments().get(0));
    Field t2Id = ((Field) ((Function) ((Function) andFunction.arguments().get(1)).arguments().get(0)).arguments().get(0));
    assertTrue(t1Id.relation() != t2Id.relation());
}
Also used : AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) TableRelation(io.crate.analyze.relations.TableRelation) Function(io.crate.analyze.symbol.Function) SymbolMatchers.isField(io.crate.testing.SymbolMatchers.isField) Field(io.crate.analyze.symbol.Field) TableInfo(io.crate.metadata.table.TableInfo) FullQualifedNameFieldProvider(io.crate.analyze.relations.FullQualifedNameFieldProvider) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 5 with AnalyzedRelation

use of io.crate.analyze.relations.AnalyzedRelation in project crate by crate.

the class SymbolPrinterTest method prepare.

@Before
public void prepare() throws Exception {
    DocTableInfo tableInfo = TestingTableInfo.builder(new TableIdent(DocSchemaInfo.NAME, TABLE_NAME), null).add("foo", DataTypes.STRING).add("bar", DataTypes.LONG).add("CraZy", DataTypes.IP).add("select", DataTypes.BYTE).add("idx", DataTypes.INTEGER).add("s_arr", new ArrayType(DataTypes.STRING)).build();
    Map<QualifiedName, AnalyzedRelation> sources = ImmutableMap.<QualifiedName, AnalyzedRelation>builder().put(QualifiedName.of(TABLE_NAME), new TableRelation(tableInfo)).build();
    sqlExpressions = new SqlExpressions(sources);
    printer = new SymbolPrinter(sqlExpressions.functions());
}
Also used : ArrayType(io.crate.types.ArrayType) DocTableInfo(io.crate.metadata.doc.DocTableInfo) QualifiedName(io.crate.sql.tree.QualifiedName) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) SqlExpressions(io.crate.testing.SqlExpressions) TableRelation(io.crate.analyze.relations.TableRelation) AbstractTableRelation(io.crate.analyze.relations.AbstractTableRelation) Before(org.junit.Before)

Aggregations

AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)8 TableRelation (io.crate.analyze.relations.TableRelation)5 QualifiedName (io.crate.sql.tree.QualifiedName)5 SqlExpressions (io.crate.testing.SqlExpressions)5 WhereClause (io.crate.analyze.WhereClause)3 Test (org.junit.Test)3 TableIdent (io.crate.metadata.TableIdent)2 DocTableInfo (io.crate.metadata.doc.DocTableInfo)2 TableInfo (io.crate.metadata.table.TableInfo)2 CrateUnitTest (io.crate.test.integration.CrateUnitTest)2 Before (org.junit.Before)2 ParameterContext (io.crate.analyze.ParameterContext)1 SelectAnalyzedStatement (io.crate.analyze.SelectAnalyzedStatement)1 AbstractTableRelation (io.crate.analyze.relations.AbstractTableRelation)1 FullQualifedNameFieldProvider (io.crate.analyze.relations.FullQualifedNameFieldProvider)1 QueriedRelation (io.crate.analyze.relations.QueriedRelation)1 Field (io.crate.analyze.symbol.Field)1 Function (io.crate.analyze.symbol.Function)1 RowN (io.crate.data.RowN)1 ReadOnlyException (io.crate.exceptions.ReadOnlyException)1