Search in sources :

Example 16 with DocTableRelation

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

the class WhereClauseOptimizerTest method optimize.

private WhereClauseOptimizer.DetailedQuery optimize(String statement) {
    QueriedSelectRelation queriedTable = e.analyze(statement);
    DocTableRelation table = ((DocTableRelation) queriedTable.from().get(0));
    EvaluatingNormalizer normalizer = new EvaluatingNormalizer(e.nodeCtx, RowGranularity.CLUSTER, null, table);
    return WhereClauseOptimizer.optimize(normalizer, queriedTable.where(), table.tableInfo(), e.getPlannerContext(clusterService.state()).transactionContext(), e.nodeCtx);
}
Also used : EvaluatingNormalizer(io.crate.expression.eval.EvaluatingNormalizer) QueriedSelectRelation(io.crate.analyze.QueriedSelectRelation) DocTableRelation(io.crate.analyze.relations.DocTableRelation)

Example 17 with DocTableRelation

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

the class EqualityExtractorTest method test_primary_key_extraction_on_subscript_with_any.

@Test
public void test_primary_key_extraction_on_subscript_with_any() {
    Map<RelationName, AnalyzedRelation> sources = T3.sources(List.of(T3.T4), clusterService);
    DocTableRelation tr4 = (DocTableRelation) sources.get(T3.T4);
    var expressionsT4 = new SqlExpressions(sources, tr4);
    var pkCol = new ColumnIdent("obj");
    var query = expressionsT4.normalize(expressionsT4.asSymbol("obj = any([{i = 1}])"));
    List<List<Symbol>> matches = analyzeExact(query, List.of(pkCol));
    assertThat(matches, contains(contains(isLiteral(Map.of("i", 1)))));
}
Also used : ColumnIdent(io.crate.metadata.ColumnIdent) RelationName(io.crate.metadata.RelationName) DocTableRelation(io.crate.analyze.relations.DocTableRelation) List(java.util.List) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) SqlExpressions(io.crate.testing.SqlExpressions) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 18 with DocTableRelation

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

the class SelectStatementAnalyzerTest method test_match_with_geo_shape_is_streamed_as_text_type_to_4_1_8_nodes.

@Test
public void test_match_with_geo_shape_is_streamed_as_text_type_to_4_1_8_nodes() throws Exception {
    var executor = SQLExecutor.builder(clusterService).addTable("create table test (shape GEO_SHAPE)").build();
    String stmt = "SELECT * FROM test WHERE MATCH (shape, 'POINT(1.2 1.3)')";
    QueriedSelectRelation rel = executor.analyze(stmt);
    Symbol where = rel.where();
    assertThat(where, instanceOf(MatchPredicate.class));
    DocTableInfo table = executor.resolveTableInfo("test");
    EvaluatingNormalizer normalizer = new EvaluatingNormalizer(executor.nodeCtx, RowGranularity.DOC, null, new DocTableRelation(table));
    Symbol normalized = normalizer.normalize(where, CoordinatorTxnCtx.systemTransactionContext());
    assertThat(normalized, isFunction("match"));
    Function match = (Function) normalized;
    assertThat(match.arguments().get(1).valueType(), is(DataTypes.GEO_SHAPE));
    assertThat(match.info().ident().argumentTypes().get(1), is(DataTypes.GEO_SHAPE));
    BytesStreamOutput out = new BytesStreamOutput();
    out.setVersion(Version.V_4_1_8);
    match.writeTo(out);
    StreamInput in = out.bytes().streamInput();
    in.setVersion(Version.V_4_1_8);
    Function serializedTo41 = new Function(in);
    assertThat(serializedTo41.info().ident().argumentTypes().get(1), is(DataTypes.STRING));
}
Also used : DistanceFunction(io.crate.expression.scalar.geo.DistanceFunction) SymbolMatchers.isFunction(io.crate.testing.SymbolMatchers.isFunction) TryCastFunction(io.crate.expression.scalar.cast.TryCastFunction) ExplicitCastFunction(io.crate.expression.scalar.cast.ExplicitCastFunction) Function(io.crate.expression.symbol.Function) SubscriptFunction(io.crate.expression.scalar.SubscriptFunction) DocTableInfo(io.crate.metadata.doc.DocTableInfo) EvaluatingNormalizer(io.crate.expression.eval.EvaluatingNormalizer) ParameterSymbol(io.crate.expression.symbol.ParameterSymbol) SelectSymbol(io.crate.expression.symbol.SelectSymbol) Symbol(io.crate.expression.symbol.Symbol) AliasSymbol(io.crate.expression.symbol.AliasSymbol) StreamInput(org.elasticsearch.common.io.stream.StreamInput) DocTableRelation(io.crate.analyze.relations.DocTableRelation) BitString(io.crate.sql.tree.BitString) MatchPredicate(io.crate.expression.symbol.MatchPredicate) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 19 with DocTableRelation

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

the class GeneratedReferenceTest method prepare.

@Before
public void prepare() throws Exception {
    executor = SQLExecutor.builder(clusterService).addTable(T1_DEFINITION).build();
    t1Info = executor.schemas().getTableInfo(T1);
    DocTableRelation tableRelation = new DocTableRelation(t1Info);
    // allocate field so it can be resolved
    tableRelation.getField(new ColumnIdent("a"));
    expressions = new SqlExpressions(Collections.emptyMap(), tableRelation);
}
Also used : DocTableRelation(io.crate.analyze.relations.DocTableRelation) SqlExpressions(io.crate.testing.SqlExpressions) Before(org.junit.Before)

Example 20 with DocTableRelation

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

the class CopyAnalyzer method convertCopyFrom.

CopyFromAnalyzedStatement convertCopyFrom(CopyFrom node, Analysis analysis) {
    DocTableInfo tableInfo = schemas.getWritableTable(TableIdent.of(node.table(), analysis.sessionContext().defaultSchema()));
    DocTableRelation tableRelation = new DocTableRelation(tableInfo);
    Operation.blockedRaiseException(tableInfo, Operation.INSERT);
    String partitionIdent = null;
    if (!node.table().partitionProperties().isEmpty()) {
        partitionIdent = PartitionPropertiesAnalyzer.toPartitionIdent(tableInfo, node.table().partitionProperties(), analysis.parameterContext().parameters());
    }
    EvaluatingNormalizer normalizer = new EvaluatingNormalizer(functions, RowGranularity.CLUSTER, ReplaceMode.MUTATE, null, tableRelation);
    ExpressionAnalyzer expressionAnalyzer = createExpressionAnalyzer(analysis, tableRelation);
    expressionAnalyzer.setResolveFieldsOperation(Operation.INSERT);
    ExpressionAnalysisContext expressionAnalysisContext = new ExpressionAnalysisContext();
    Predicate<DiscoveryNode> nodeFilters = Predicates.alwaysTrue();
    Settings settings = Settings.EMPTY;
    if (node.genericProperties().isPresent()) {
        // copy map as items are removed. The GenericProperties map is cached in the query cache and removing
        // items would cause subsequent queries that hit the cache to have different genericProperties
        Map<String, Expression> properties = new HashMap<>(node.genericProperties().get().properties());
        nodeFilters = discoveryNodePredicate(analysis.parameterContext().parameters(), properties.remove(NodeFilters.NAME));
        settings = settingsFromProperties(properties, expressionAnalyzer, expressionAnalysisContext);
    }
    Symbol uri = expressionAnalyzer.convert(node.path(), expressionAnalysisContext);
    uri = normalizer.normalize(uri, analysis.transactionContext());
    if (!(uri.valueType() == DataTypes.STRING || uri.valueType() instanceof CollectionType && ((CollectionType) uri.valueType()).innerType() == DataTypes.STRING)) {
        throw CopyFromAnalyzedStatement.raiseInvalidType(uri.valueType());
    }
    return new CopyFromAnalyzedStatement(tableInfo, settings, uri, partitionIdent, nodeFilters);
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) Symbol(io.crate.analyze.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) CollectionType(io.crate.types.CollectionType) DocTableRelation(io.crate.analyze.relations.DocTableRelation) Settings(org.elasticsearch.common.settings.Settings)

Aggregations

DocTableRelation (io.crate.analyze.relations.DocTableRelation)36 DocTableInfo (io.crate.metadata.doc.DocTableInfo)17 RelationName (io.crate.metadata.RelationName)13 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)10 Symbol (io.crate.expression.symbol.Symbol)10 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)10 SqlExpressions (io.crate.testing.SqlExpressions)10 Test (org.junit.Test)10 Before (org.junit.Before)9 ExpressionAnalysisContext (io.crate.analyze.expressions.ExpressionAnalysisContext)7 ExpressionAnalyzer (io.crate.analyze.expressions.ExpressionAnalyzer)7 EvaluatingNormalizer (io.crate.expression.eval.EvaluatingNormalizer)7 Reference (io.crate.metadata.Reference)6 SQLExecutor (io.crate.testing.SQLExecutor)6 WhereClause (io.crate.analyze.WhereClause)5 TableInfo (io.crate.metadata.table.TableInfo)4 QueriedSelectRelation (io.crate.analyze.QueriedSelectRelation)3 FullQualifiedNameFieldProvider (io.crate.analyze.relations.FullQualifiedNameFieldProvider)3 NameFieldProvider (io.crate.analyze.relations.NameFieldProvider)3 StatementAnalysisContext (io.crate.analyze.relations.StatementAnalysisContext)3