Search in sources :

Example 61 with RelationName

use of io.crate.metadata.RelationName in project crate by crate.

the class WhereClauseAnalyzerTest method testGenColRoundingFunctionNoSwappingOperatorOptimization.

@Test
public void testGenColRoundingFunctionNoSwappingOperatorOptimization() throws Exception {
    WhereClause whereClause = analyzeSelectWhere("select * from generated_col where ts >= '2015-01-02T12:00:00'");
    assertThat(whereClause.partitions().size(), is(1));
    assertThat(whereClause.partitions().get(0), is(new PartitionName(new RelationName("doc", "generated_col"), Arrays.asList("1420156800000", "-2")).asIndexName()));
}
Also used : PartitionName(io.crate.metadata.PartitionName) WhereClause(io.crate.analyze.WhereClause) RelationName(io.crate.metadata.RelationName) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 62 with RelationName

use of io.crate.metadata.RelationName in project crate by crate.

the class WhereClauseAnalyzerTest method testSelectFromPartitionedTable.

@Test
public void testSelectFromPartitionedTable() throws Exception {
    String partition1 = new PartitionName(new RelationName("doc", "parted"), Arrays.asList("1395874800000")).asIndexName();
    String partition2 = new PartitionName(new RelationName("doc", "parted"), Arrays.asList("1395961200000")).asIndexName();
    String partition3 = new PartitionName(new RelationName("doc", "parted"), singletonList(null)).asIndexName();
    WhereClause whereClause = analyzeSelectWhere("select id, name from parted where date = 1395874800000");
    assertEquals(List.of(partition1), whereClause.partitions());
    assertThat(whereClause.queryOrFallback(), isLiteral(true));
    whereClause = analyzeSelectWhere("select id, name from parted where date = 1395874800000 " + "and substr(name, 0, 4) = 'this'");
    assertEquals(List.of(partition1), whereClause.partitions());
    assertThat(whereClause.hasQuery(), is(true));
    whereClause = analyzeSelectWhere("select id, name from parted where date >= 1395874800000");
    assertThat(whereClause.partitions(), containsInAnyOrder(partition1, partition2));
    assertThat(whereClause.queryOrFallback(), isLiteral(true));
    whereClause = analyzeSelectWhere("select id, name from parted where date < 1395874800000");
    assertEquals(List.of(), whereClause.partitions());
    assertThat(whereClause.queryOrFallback(), isLiteral(false));
    whereClause = analyzeSelectWhere("select id, name from parted where date = 1395874800000 and date = 1395961200000");
    assertEquals(List.of(), whereClause.partitions());
    assertThat(whereClause.queryOrFallback(), isLiteral(false));
    whereClause = analyzeSelectWhere("select id, name from parted where date = 1395874800000 or date = 1395961200000");
    assertThat(whereClause.partitions(), containsInAnyOrder(partition1, partition2));
    assertThat(whereClause.queryOrFallback(), isLiteral(true));
    whereClause = analyzeSelectWhere("select id, name from parted where date < 1395874800000 or date > 1395874800000");
    assertEquals(List.of(partition2), whereClause.partitions());
    assertThat(whereClause.queryOrFallback(), isLiteral(true));
    whereClause = analyzeSelectWhere("select id, name from parted where date in (1395874800000, 1395961200000)");
    assertThat(whereClause.partitions(), containsInAnyOrder(partition1, partition2));
    assertThat(whereClause.queryOrFallback(), isLiteral(true));
    whereClause = analyzeSelectWhere("select id, name from parted where date in (1395874800000, 1395961200000) and id = 1");
    assertThat(whereClause.partitions(), containsInAnyOrder(partition1, partition2));
    assertThat(whereClause.queryOrFallback(), isFunction("op_="));
    /**
     * 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.queryOrFallback(), isLiteral(true));
    whereClause = analyzeSelectWhere("select id, name from parted where date in (1395874800000) or date in (1395961200000)");
    assertThat(whereClause.partitions(), containsInAnyOrder(partition1, partition2));
    assertThat(whereClause.queryOrFallback(), isLiteral(true));
    whereClause = analyzeSelectWhere("select id, name from parted where date = 1395961200000 and id = 1");
    assertEquals(List.of(partition2), whereClause.partitions());
    assertThat(whereClause.queryOrFallback(), isFunction("op_="));
    whereClause = analyzeSelectWhere("select id, name from parted where (date =1395874800000 or date = 1395961200000) and id = 1");
    assertThat(whereClause.partitions(), containsInAnyOrder(partition1, partition2));
    assertThat(whereClause.queryOrFallback(), isFunction("op_="));
    whereClause = analyzeSelectWhere("select id, name from parted where date = 1395874800000 and id is null");
    assertEquals(List.of(partition1), whereClause.partitions());
    assertThat(whereClause.queryOrFallback(), isFunction("op_isnull"));
    whereClause = analyzeSelectWhere("select id, name from parted where date is null and id = 1");
    assertEquals(List.of(partition3), whereClause.partitions());
    assertThat(whereClause.queryOrFallback(), isFunction("op_="));
    whereClause = analyzeSelectWhere("select id, name from parted where 1395874700000 < date and date < 1395961200001");
    assertThat(whereClause.partitions(), containsInAnyOrder(partition1, partition2));
    assertThat(whereClause.queryOrFallback(), isLiteral(true));
    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));
    assertThat(whereClause.queryOrFallback(), isLiteral(true));
}
Also used : PartitionName(io.crate.metadata.PartitionName) WhereClause(io.crate.analyze.WhereClause) RelationName(io.crate.metadata.RelationName) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 63 with RelationName

use of io.crate.metadata.RelationName in project crate by crate.

the class ExpressionAnalyzerTest method testAnalyzeArraySliceFunctionCall.

@Test
public void testAnalyzeArraySliceFunctionCall() {
    ReferenceIdent arrayRefIdent = new ReferenceIdent(new RelationName("doc", "tarr"), "xs");
    Reference arrayRef = new Reference(arrayRefIdent, RowGranularity.DOC, DataTypes.INTEGER_ARRAY, ColumnPolicy.DYNAMIC, Reference.IndexType.PLAIN, true, true, 1, null);
    CoordinatorTxnCtx txnCtx = CoordinatorTxnCtx.systemTransactionContext();
    ExpressionAnalysisContext localContext = new ExpressionAnalysisContext(txnCtx.sessionContext());
    Symbol function = ExpressionAnalyzer.allocateFunction(ArraySliceFunction.NAME, List.of(arrayRef, Literal.of(1), Literal.of(3)), null, localContext, txnCtx, expressions.nodeCtx);
    var result = executor.asSymbol("tarr.xs[1:3]");
    assertThat(result, is(equalTo(function)));
}
Also used : CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) SymbolMatchers.isReference(io.crate.testing.SymbolMatchers.isReference) Reference(io.crate.metadata.Reference) ParameterSymbol(io.crate.expression.symbol.ParameterSymbol) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol) Symbol(io.crate.expression.symbol.Symbol) RelationName(io.crate.metadata.RelationName) ReferenceIdent(io.crate.metadata.ReferenceIdent) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 64 with RelationName

use of io.crate.metadata.RelationName in project crate by crate.

the class ExpressionAnalyzerTest method testInSelfJoinCaseFunctionsThatLookTheSameMustNotReuseFunctionAllocation.

@Test
public void testInSelfJoinCaseFunctionsThatLookTheSameMustNotReuseFunctionAllocation() throws Exception {
    TableInfo t1 = executor.resolveTableInfo("t1");
    TableRelation relation = new TableRelation(t1);
    RelationName a1 = new RelationName(null, "a1");
    RelationName a2 = new RelationName(null, "a2");
    Map<RelationName, AnalyzedRelation> sources = Map.of(a1, new AliasedAnalyzedRelation(relation, a1), a2, new AliasedAnalyzedRelation(relation, a2));
    SessionContext sessionContext = SessionContext.systemSessionContext();
    CoordinatorTxnCtx coordinatorTxnCtx = new CoordinatorTxnCtx(sessionContext);
    ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(coordinatorTxnCtx, expressions.nodeCtx, paramTypeHints, new FullQualifiedNameFieldProvider(sources, ParentRelations.NO_PARENTS, sessionContext.searchPath().currentSchema()), null);
    Function andFunction = (Function) expressionAnalyzer.convert(SqlParser.createExpression("not a1.x = 1 and not a2.x = 1"), context);
    ScopedSymbol t1Id = ((ScopedSymbol) ((Function) ((Function) andFunction.arguments().get(0)).arguments().get(0)).arguments().get(0));
    ScopedSymbol t2Id = ((ScopedSymbol) ((Function) ((Function) andFunction.arguments().get(1)).arguments().get(0)).arguments().get(0));
    assertThat(t1Id.relation(), is(not(t2Id.relation())));
}
Also used : CoalesceFunction(io.crate.expression.scalar.conditional.CoalesceFunction) SymbolMatchers.isFunction(io.crate.testing.SymbolMatchers.isFunction) ArraySliceFunction(io.crate.expression.scalar.ArraySliceFunction) ImplicitCastFunction(io.crate.expression.scalar.cast.ImplicitCastFunction) Function(io.crate.expression.symbol.Function) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) RelationName(io.crate.metadata.RelationName) SessionContext(io.crate.action.sql.SessionContext) TableInfo(io.crate.metadata.table.TableInfo) AliasedAnalyzedRelation(io.crate.analyze.relations.AliasedAnalyzedRelation) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) TableRelation(io.crate.analyze.relations.TableRelation) AliasedAnalyzedRelation(io.crate.analyze.relations.AliasedAnalyzedRelation) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol) FullQualifiedNameFieldProvider(io.crate.analyze.relations.FullQualifiedNameFieldProvider) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 65 with RelationName

use of io.crate.metadata.RelationName in project crate by crate.

the class SubSelectAnalyzerTest method testSubSelectWithNestedAlias.

@Test
public void testSubSelectWithNestedAlias() throws Exception {
    QueriedSelectRelation relation = analyze("select tt.aa, (tt.xi + 1::int)" + " from (select (x + i) as xi, concat(a, a) as aa, i from t1) as tt");
    assertThat(relation.outputs(), contains(isField("aa"), isFunction("add", isField("xi"), isLiteral(1))));
    QueriedSelectRelation innerQSR = (QueriedSelectRelation) ((AliasedAnalyzedRelation) relation.from().get(0)).relation();
    assertThat(innerQSR.from(), contains(isDocTable(new RelationName("doc", "t1"))));
}
Also used : RelationName(io.crate.metadata.RelationName) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Aggregations

RelationName (io.crate.metadata.RelationName)180 Test (org.junit.Test)100 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)55 PartitionName (io.crate.metadata.PartitionName)47 Symbol (io.crate.expression.symbol.Symbol)42 Reference (io.crate.metadata.Reference)37 ColumnIdent (io.crate.metadata.ColumnIdent)31 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)21 ReferenceIdent (io.crate.metadata.ReferenceIdent)21 DocTableInfo (io.crate.metadata.doc.DocTableInfo)21 Map (java.util.Map)20 HashMap (java.util.HashMap)19 SqlExpressions (io.crate.testing.SqlExpressions)18 ArrayList (java.util.ArrayList)18 List (java.util.List)17 Before (org.junit.Before)17 DocTableRelation (io.crate.analyze.relations.DocTableRelation)13 SQLExecutor (io.crate.testing.SQLExecutor)11 CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)10 IndexTemplateMetadata (org.elasticsearch.cluster.metadata.IndexTemplateMetadata)10