Search in sources :

Example 16 with SqlExpressions

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

the class InputCreatingVisitorTest method testNonDeterministicFunctionsReplacement.

@Test
public void testNonDeterministicFunctionsReplacement() throws Exception {
    SqlExpressions sqlExpressions = new SqlExpressions(T3.SOURCES, T3.TR_1);
    Function fn1 = (Function) sqlExpressions.asSymbol("random()");
    Function fn2 = (Function) sqlExpressions.asSymbol("random()");
    List<Symbol> inputSymbols = Arrays.<Symbol>asList(Literal.BOOLEAN_FALSE, sqlExpressions.asSymbol("upper(a)"), fn1, fn2);
    Function newSameFn = (Function) sqlExpressions.asSymbol("random()");
    Function newDifferentFn = (Function) sqlExpressions.asSymbol("random()");
    InputCreatingVisitor.Context context = new InputCreatingVisitor.Context(inputSymbols);
    Symbol replaced1 = InputCreatingVisitor.INSTANCE.process(fn1, context);
    assertThat(replaced1, is(instanceOf(InputColumn.class)));
    assertThat(((InputColumn) replaced1).index(), is(2));
    Symbol replaced2 = InputCreatingVisitor.INSTANCE.process(fn2, context);
    assertThat(replaced2, is(instanceOf(InputColumn.class)));
    assertThat(((InputColumn) replaced2).index(), is(3));
    Symbol replaced3 = InputCreatingVisitor.INSTANCE.process(newSameFn, context);
    // not replaced
    assertThat(replaced3, is(equalTo((Symbol) newSameFn)));
    Symbol replaced4 = InputCreatingVisitor.INSTANCE.process(newDifferentFn, context);
    // not replaced
    assertThat(replaced4, is(equalTo((Symbol) newDifferentFn)));
}
Also used : Function(io.crate.analyze.symbol.Function) Symbol(io.crate.analyze.symbol.Symbol) SqlExpressions(io.crate.testing.SqlExpressions) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 17 with SqlExpressions

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

the class InternalCountOperationTest method testCount.

@Test
public void testCount() throws Exception {
    execute("create table t (name string) clustered into 1 shards with (number_of_replicas = 0)");
    ensureYellow();
    execute("insert into t (name) values ('Marvin'), ('Arthur'), ('Trillian')");
    execute("refresh table t");
    CountOperation countOperation = internalCluster().getDataNodeInstance(CountOperation.class);
    assertThat(countOperation.count("t", 0, WhereClause.MATCH_ALL), is(3L));
    Schemas schemas = internalCluster().getInstance(Schemas.class);
    TableInfo tableInfo = schemas.getTableInfo(new TableIdent(null, "t"));
    TableRelation tableRelation = new TableRelation(tableInfo);
    Map<QualifiedName, AnalyzedRelation> tableSources = ImmutableMap.<QualifiedName, AnalyzedRelation>of(new QualifiedName(tableInfo.ident().name()), tableRelation);
    SqlExpressions sqlExpressions = new SqlExpressions(tableSources, tableRelation);
    WhereClause whereClause = new WhereClause(sqlExpressions.normalize(sqlExpressions.asSymbol("name = 'Marvin'")));
    assertThat(countOperation.count("t", 0, whereClause), is(1L));
}
Also used : QualifiedName(io.crate.sql.tree.QualifiedName) WhereClause(io.crate.analyze.WhereClause) TableInfo(io.crate.metadata.table.TableInfo) TableIdent(io.crate.metadata.TableIdent) Schemas(io.crate.metadata.Schemas) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) SqlExpressions(io.crate.testing.SqlExpressions) TableRelation(io.crate.analyze.relations.TableRelation) Test(org.junit.Test) SQLTransportIntegrationTest(io.crate.integrationtests.SQLTransportIntegrationTest)

Example 18 with SqlExpressions

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

the class CurrentSchemaFunctionTest method testNormalizeCurrentSchemaDefaultSchema.

@Test
public void testNormalizeCurrentSchemaDefaultSchema() throws Exception {
    sqlExpressions = new SqlExpressions(tableSources, new SessionContext(0, Option.NONE, null));
    functions = sqlExpressions.getInstance(Functions.class);
    assertNormalize("current_schema()", isLiteral("doc"), 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)

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