Search in sources :

Example 1 with QueriedRelation

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

the class SelectStatementAnalyzerTest method testOrderByOnAlias.

@Test
public void testOrderByOnAlias() throws Exception {
    QueriedRelation relation = analyze("select name as cluster_name from sys.cluster order by cluster_name").relation();
    List<String> outputNames = outputNames(relation);
    assertThat(outputNames.size(), is(1));
    assertThat(outputNames.get(0), is("cluster_name"));
    assertTrue(relation.querySpec().orderBy().isPresent());
    assertThat(relation.querySpec().orderBy().get().orderBySymbols().size(), is(1));
    assertThat(relation.querySpec().orderBy().get().orderBySymbols().get(0), is(relation.querySpec().outputs().get(0)));
}
Also used : QueriedRelation(io.crate.analyze.relations.QueriedRelation) TestingHelpers.mapToSortedString(io.crate.testing.TestingHelpers.mapToSortedString) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 2 with QueriedRelation

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

the class SelectStatementAnalyzerTest method testGroupByOnAlias.

@Test
public void testGroupByOnAlias() throws Exception {
    QueriedRelation relation = analyze("select count(*), name as n from sys.nodes group by n").relation();
    assertThat(relation.querySpec().groupBy().get().size(), is(1));
    assertThat(relation.fields().get(0).path().outputName(), is("count(*)"));
    assertThat(relation.fields().get(1).path().outputName(), is("n"));
    assertEquals(relation.querySpec().groupBy().get().get(0), relation.querySpec().outputs().get(1));
}
Also used : QueriedRelation(io.crate.analyze.relations.QueriedRelation) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 3 with QueriedRelation

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

the class SelectStatementAnalyzerTest method testWhereSelect.

@Test
public void testWhereSelect() throws Exception {
    QueriedRelation relation = analyze("select load from sys.nodes " + "where load['1'] = 1.2 or 1 >= load['5']").relation();
    assertFalse(relation.querySpec().groupBy().isPresent());
    Function whereClause = (Function) relation.querySpec().where().query();
    assertEquals(OrOperator.NAME, whereClause.info().ident().name());
    assertFalse(whereClause.info().type() == FunctionInfo.Type.AGGREGATE);
    Function left = (Function) whereClause.arguments().get(0);
    assertEquals(EqOperator.NAME, left.info().ident().name());
    assertThat(left.arguments().get(0), isReference("load['1']"));
    assertThat(left.arguments().get(1), IsInstanceOf.instanceOf(Literal.class));
    assertSame(left.arguments().get(1).valueType(), DataTypes.DOUBLE);
    Function right = (Function) whereClause.arguments().get(1);
    assertEquals(LteOperator.NAME, right.info().ident().name());
    assertThat(right.arguments().get(0), isReference("load['5']"));
    assertThat(right.arguments().get(1), IsInstanceOf.instanceOf(Literal.class));
    assertSame(left.arguments().get(1).valueType(), DataTypes.DOUBLE);
}
Also used : DistanceFunction(io.crate.operation.scalar.geo.DistanceFunction) SubscriptFunction(io.crate.operation.scalar.SubscriptFunction) MatchesFunction(io.crate.operation.scalar.regex.MatchesFunction) AddFunction(io.crate.operation.scalar.arithmetic.AddFunction) QueriedRelation(io.crate.analyze.relations.QueriedRelation) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 4 with QueriedRelation

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

the class SelectStatementAnalyzerTest method testRewriteNotEquals.

@Test
public void testRewriteNotEquals() {
    // should rewrite to:
    //    not(eq(sys.noes.name, 'something'))
    ImmutableList<String> statements = ImmutableList.of("select * from sys.nodes where sys.nodes.name <> 'something'", "select * from sys.nodes where sys.nodes.name != 'something'");
    for (String statement : statements) {
        QueriedRelation relation = analyze(statement).relation();
        WhereClause whereClause = relation.querySpec().where();
        Function notFunction = (Function) whereClause.query();
        assertThat(notFunction.info().ident().name(), is(NotPredicate.NAME));
        assertThat(notFunction.arguments().size(), is(1));
        Function eqFunction = (Function) notFunction.arguments().get(0);
        assertThat(eqFunction.info().ident().name(), is(EqOperator.NAME));
        assertThat(eqFunction.arguments().size(), is(2));
        List<Symbol> eqArguments = eqFunction.arguments();
        assertThat(eqArguments.get(1), isLiteral("something"));
    }
}
Also used : DistanceFunction(io.crate.operation.scalar.geo.DistanceFunction) SubscriptFunction(io.crate.operation.scalar.SubscriptFunction) MatchesFunction(io.crate.operation.scalar.regex.MatchesFunction) AddFunction(io.crate.operation.scalar.arithmetic.AddFunction) QueriedRelation(io.crate.analyze.relations.QueriedRelation) TestingHelpers.mapToSortedString(io.crate.testing.TestingHelpers.mapToSortedString) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 5 with QueriedRelation

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

the class SubqueryPlanner method planSubquery.

private void planSubquery(SelectSymbol selectSymbol) {
    AnalyzedRelation relation = selectSymbol.relation();
    SelectAnalyzedStatement selectAnalyzedStatement = new SelectAnalyzedStatement(((QueriedRelation) relation));
    Plan subPlan = plannerContext.planSingleRowSubselect(selectAnalyzedStatement);
    subQueries.put(subPlan, selectSymbol);
}
Also used : QueriedRelation(io.crate.analyze.relations.QueriedRelation) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) SelectAnalyzedStatement(io.crate.analyze.SelectAnalyzedStatement)

Aggregations

QueriedRelation (io.crate.analyze.relations.QueriedRelation)13 CrateUnitTest (io.crate.test.integration.CrateUnitTest)11 Test (org.junit.Test)11 SubscriptFunction (io.crate.operation.scalar.SubscriptFunction)5 AddFunction (io.crate.operation.scalar.arithmetic.AddFunction)5 DistanceFunction (io.crate.operation.scalar.geo.DistanceFunction)5 MatchesFunction (io.crate.operation.scalar.regex.MatchesFunction)5 TestingHelpers.mapToSortedString (io.crate.testing.TestingHelpers.mapToSortedString)3 QuerySpec (io.crate.analyze.QuerySpec)1 SelectAnalyzedStatement (io.crate.analyze.SelectAnalyzedStatement)1 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)1 SelectSymbol (io.crate.analyze.symbol.SelectSymbol)1 ValidationException (io.crate.exceptions.ValidationException)1 Nullable (javax.annotation.Nullable)1