Search in sources :

Example 1 with SearchPath

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

the class ScalarTestCase method assertEvaluate.

/**
 * asserts that the given functionExpression matches the given matcher.
 * If the functionExpression contains references the inputs will be used in the order the references appear.
 * <p>
 * E.g.
 * <code>
 * assertEvaluate("foo(name, age)", anyOf("expectedValue1", "expectedValue2"), inputForName, inputForAge)
 * </code>
 */
@SuppressWarnings("unchecked")
public <T> void assertEvaluate(String functionExpression, Matcher<T> expectedValue, Literal<?>... literals) {
    if (expectedValue == null) {
        expectedValue = (Matcher<T>) nullValue();
    }
    sqlExpressions.context().allowEagerNormalize(true);
    Symbol functionSymbol = sqlExpressions.asSymbol(functionExpression);
    functionSymbol = sqlExpressions.normalize(functionSymbol);
    if (functionSymbol instanceof Literal) {
        Object value = ((Literal) functionSymbol).value();
        assertThat((T) value, expectedValue);
        return;
    }
    LinkedList<Literal<?>> unusedLiterals = new LinkedList<>(Arrays.asList(literals));
    Function function = (Function) RefReplacer.replaceRefs(functionSymbol, r -> {
        if (unusedLiterals.isEmpty()) {
            throw new IllegalArgumentException("No value literal for reference=" + r + ", please add more literals");
        }
        // Can be null.
        Literal<?> literal = unusedLiterals.pollFirst();
        return literal;
    });
    if (unusedLiterals.size() == literals.length) {
        // Currently it's supposed that literals will be either references or parameters.
        // One of replaceRefs and bindParameters does nothing and doesn't consume unusedLiterals.
        function = (Function) ParameterBinder.bindParameters(function, p -> {
            if (unusedLiterals.isEmpty()) {
                throw new IllegalArgumentException("No value literal for parameter=" + p + ", please add more literals");
            }
            // Can be null.
            Literal<?> literal = unusedLiterals.pollFirst();
            return literal;
        });
    }
    Scalar scalar = (Scalar) sqlExpressions.nodeCtx.functions().getQualified(function, txnCtx.sessionSettings().searchPath());
    assertThat("Function implementation not found using full qualified lookup", scalar, Matchers.notNullValue());
    AssertMax1ValueCallInput[] arguments = new AssertMax1ValueCallInput[function.arguments().size()];
    InputFactory.Context<CollectExpression<Row, ?>> ctx = inputFactory.ctxForInputColumns(txnCtx);
    for (int i = 0; i < function.arguments().size(); i++) {
        Symbol arg = function.arguments().get(i);
        Input<?> input = ctx.add(arg);
        arguments[i] = new AssertMax1ValueCallInput(input);
    }
    Object actualValue = scalar.compile(function.arguments()).evaluate(txnCtx, sqlExpressions.nodeCtx, (Input[]) arguments);
    assertThat((T) actualValue, expectedValue);
    // Reset calls
    for (AssertMax1ValueCallInput argument : arguments) {
        argument.calls = 0;
    }
    actualValue = scalar.evaluate(txnCtx, sqlExpressions.nodeCtx, arguments);
    assertThat((T) actualValue, expectedValue);
}
Also used : Input(io.crate.data.Input) TransactionContext(io.crate.metadata.TransactionContext) SqlExpressions(io.crate.testing.SqlExpressions) InputColumn(io.crate.expression.symbol.InputColumn) Arrays(java.util.Arrays) ParameterBinder(io.crate.expression.symbol.ParameterBinder) RelationName(io.crate.metadata.RelationName) SessionSettings(io.crate.metadata.settings.SessionSettings) CollectExpression(io.crate.execution.engine.collect.CollectExpression) Matchers.not(org.hamcrest.Matchers.not) SearchPath(io.crate.metadata.SearchPath) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) RefReplacer(io.crate.expression.symbol.RefReplacer) Locale(java.util.Locale) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Is.is(org.hamcrest.core.Is.is) SQLExecutor(io.crate.testing.SQLExecutor) LinkedList(java.util.LinkedList) Before(org.junit.Before) DocTableInfo(io.crate.metadata.doc.DocTableInfo) DataType(io.crate.types.DataType) Matchers(org.hamcrest.Matchers) Function(io.crate.expression.symbol.Function) Lists2(io.crate.common.collections.Lists2) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) DocTableRelation(io.crate.analyze.relations.DocTableRelation) Row(io.crate.data.Row) Literal(io.crate.expression.symbol.Literal) Symbol(io.crate.expression.symbol.Symbol) FunctionImplementation(io.crate.metadata.FunctionImplementation) DocSchemaInfo(io.crate.metadata.doc.DocSchemaInfo) Matcher(org.hamcrest.Matcher) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) InputFactory(io.crate.expression.InputFactory) Scalar(io.crate.metadata.Scalar) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) InputFactory(io.crate.expression.InputFactory) Symbol(io.crate.expression.symbol.Symbol) CollectExpression(io.crate.execution.engine.collect.CollectExpression) LinkedList(java.util.LinkedList) Scalar(io.crate.metadata.Scalar) Function(io.crate.expression.symbol.Function) Input(io.crate.data.Input) Literal(io.crate.expression.symbol.Literal)

Example 2 with SearchPath

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

the class RelationAnalyzer method visitTable.

@Override
protected AnalyzedRelation visitTable(Table<?> node, StatementAnalysisContext context) {
    QualifiedName tableQualifiedName = node.getName();
    SearchPath searchPath = context.sessionContext().searchPath();
    AnalyzedRelation relation;
    TableInfo tableInfo;
    try {
        tableInfo = schemas.resolveTableInfo(tableQualifiedName, context.currentOperation(), context.sessionContext().sessionUser(), searchPath);
        if (tableInfo instanceof DocTableInfo) {
            // Dispatching of doc relations is based on the returned class of the schema information.
            relation = new DocTableRelation((DocTableInfo) tableInfo);
        } else {
            relation = new TableRelation(tableInfo);
        }
    } catch (RelationUnknown e) {
        Tuple<ViewMetadata, RelationName> viewMetadata;
        try {
            viewMetadata = schemas.resolveView(tableQualifiedName, searchPath);
        } catch (RelationUnknown e1) {
            // don't shadow original exception, as looking for the view is just a fallback
            throw e;
        }
        ViewMetadata view = viewMetadata.v1();
        AnalyzedRelation resolvedView = SqlParser.createStatement(view.stmt()).accept(this, context);
        relation = new AnalyzedView(viewMetadata.v2(), view.owner(), resolvedView);
    }
    context.currentRelationContext().addSourceRelation(relation);
    return relation;
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) RelationUnknown(io.crate.exceptions.RelationUnknown) QualifiedName(io.crate.sql.tree.QualifiedName) SearchPath(io.crate.metadata.SearchPath) DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableInfo(io.crate.metadata.table.TableInfo) Tuple(io.crate.common.collections.Tuple) ViewMetadata(io.crate.metadata.view.ViewMetadata)

Example 3 with SearchPath

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

the class SwapTableAnalyzer method analyze.

public AnalyzedSwapTable analyze(SwapTable<Expression> swapTable, CoordinatorTxnCtx txnCtx, ParamTypeHints typeHints) {
    HashMap<String, Expression> properties = new HashMap<>(swapTable.properties().properties());
    Expression dropSourceExpr = properties.remove(DROP_SOURCE);
    if (!properties.isEmpty()) {
        throw new IllegalArgumentException("Invalid options for ALTER CLUSTER SWAP TABLE: " + String.join(", ", properties.keySet()));
    }
    Symbol dropSource;
    if (dropSourceExpr == null) {
        dropSource = Literal.BOOLEAN_FALSE;
    } else {
        ExpressionAnalyzer exprAnalyzer = new ExpressionAnalyzer(txnCtx, nodeCtx, typeHints, FieldProvider.UNSUPPORTED, null);
        dropSource = exprAnalyzer.convert(dropSourceExpr, new ExpressionAnalysisContext(txnCtx.sessionContext()));
    }
    SearchPath searchPath = txnCtx.sessionContext().searchPath();
    User user = txnCtx.sessionContext().sessionUser();
    return new AnalyzedSwapTable((DocTableInfo) schemas.resolveTableInfo(swapTable.source(), Operation.ALTER, user, searchPath), (DocTableInfo) schemas.resolveTableInfo(swapTable.target(), Operation.ALTER, user, searchPath), dropSource);
}
Also used : ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) User(io.crate.user.User) Expression(io.crate.sql.tree.Expression) HashMap(java.util.HashMap) Symbol(io.crate.expression.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) SearchPath(io.crate.metadata.SearchPath)

Example 4 with SearchPath

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

the class SQLIntegrationTestCase method assertFunctionIsCreatedOnAll.

public void assertFunctionIsCreatedOnAll(String schema, String name, List<DataType<?>> argTypes) throws Exception {
    SearchPath searchPath = SearchPath.pathWithPGCatalogAndDoc();
    assertBusy(() -> {
        Iterable<Functions> functions = internalCluster().getInstances(Functions.class);
        for (Functions function : functions) {
            FunctionImplementation func = function.get(schema, name, Lists2.map(argTypes, t -> Literal.of(t, null)), searchPath);
            assertThat(func, is(not(nullValue())));
            assertThat(func.info().ident().argumentTypes(), is(equalTo(argTypes)));
        }
    }, 20L, TimeUnit.SECONDS);
}
Also used : ParamTypeHints(io.crate.analyze.ParamTypeHints) SessionContext(io.crate.action.sql.SessionContext) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) SessionSettings(io.crate.metadata.settings.SessionSettings) Matchers.not(org.hamcrest.Matchers.not) Inherited(java.lang.annotation.Inherited) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) Analyzer(io.crate.analyze.Analyzer) NodeLimits(io.crate.execution.jobs.NodeLimits) DependencyCarrier(io.crate.planner.DependencyCarrier) Functions(io.crate.metadata.Functions) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Map(java.util.Map) SQLTransportExecutor(io.crate.testing.SQLTransportExecutor) RandomStrings(com.carrotsearch.randomizedtesting.generators.RandomStrings) TableInfo(io.crate.metadata.table.TableInfo) RandomizedContext(com.carrotsearch.randomizedtesting.RandomizedContext) TasksService(io.crate.execution.jobs.TasksService) Logger(org.apache.logging.log4j.Logger) Row(io.crate.data.Row) SubQueryResults(io.crate.planner.operators.SubQueryResults) ClusterStateRequest(org.elasticsearch.action.admin.cluster.state.ClusterStateRequest) Matchers.is(org.hamcrest.Matchers.is) XContentFactory(org.elasticsearch.common.xcontent.XContentFactory) ClusterService(org.elasticsearch.cluster.service.ClusterService) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) Metadata(org.elasticsearch.cluster.metadata.Metadata) TestName(org.junit.rules.TestName) Timeout(org.junit.rules.Timeout) IndicesService(org.elasticsearch.indices.IndicesService) Nullable(javax.annotation.Nullable) PSQL_PORT_SETTING(io.crate.protocols.postgres.PostgresNetty.PSQL_PORT_SETTING) Before(org.junit.Before) Identifiers(io.crate.sql.Identifiers) Client(org.elasticsearch.client.Client) IndexService(org.elasticsearch.index.IndexService) IndexShard(org.elasticsearch.index.shard.IndexShard) IOException(java.io.IOException) Planner(io.crate.planner.Planner) Field(java.lang.reflect.Field) RoutingProvider(io.crate.metadata.RoutingProvider) Literal(io.crate.expression.symbol.Literal) PlannerContext(io.crate.planner.PlannerContext) Matcher(org.hamcrest.Matcher) Plan(io.crate.planner.Plan) PostgresNetty(io.crate.protocols.postgres.PostgresNetty) KillableCallable(io.crate.execution.jobs.kill.KillableCallable) Schemas(io.crate.metadata.Schemas) UseJdbc(io.crate.testing.UseJdbc) Randomness(org.elasticsearch.common.Randomness) TransportShardAction(io.crate.execution.dml.TransportShardAction) TransportShardUpsertAction(io.crate.execution.dml.upsert.TransportShardUpsertAction) RelationName(io.crate.metadata.RelationName) UseRandomizedSchema(io.crate.testing.UseRandomizedSchema) Random(java.util.Random) RootTask(io.crate.execution.jobs.RootTask) UseHashJoins(io.crate.testing.UseHashJoins) Settings(org.elasticsearch.common.settings.Settings) TestGroup(com.carrotsearch.randomizedtesting.annotations.TestGroup) Locale(java.util.Locale) After(org.junit.After) Documented(java.lang.annotation.Documented) ThreadPool(org.elasticsearch.threadpool.ThreadPool) Method(java.lang.reflect.Method) SQLOperations(io.crate.action.sql.SQLOperations) NodeContext(io.crate.metadata.NodeContext) User(io.crate.user.User) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) UUID(java.util.UUID) Netty4Plugin(org.elasticsearch.transport.Netty4Plugin) InetSocketAddress(java.net.InetSocketAddress) Lists2(io.crate.common.collections.Lists2) List(java.util.List) Session(io.crate.action.sql.Session) ESIntegTestCase(org.elasticsearch.test.ESIntegTestCase) Symbol(io.crate.expression.symbol.Symbol) FunctionImplementation(io.crate.metadata.FunctionImplementation) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Annotation(java.lang.annotation.Annotation) TimeValue(io.crate.common.unit.TimeValue) Paging(io.crate.data.Paging) TestingRowConsumer(io.crate.testing.TestingRowConsumer) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) UserLookup(io.crate.user.UserLookup) SQLResponse(io.crate.testing.SQLResponse) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) TransportShardDeleteAction(io.crate.execution.dml.delete.TransportShardDeleteAction) SearchPath(io.crate.metadata.SearchPath) Index(org.elasticsearch.index.Index) SETTING_HTTP_COMPRESSION(org.elasticsearch.http.HttpTransportSettings.SETTING_HTTP_COMPRESSION) TaskId(org.elasticsearch.tasks.TaskId) Retention(java.lang.annotation.Retention) Strings(org.elasticsearch.common.Strings) ElasticsearchTimeoutException(org.elasticsearch.ElasticsearchTimeoutException) Constants(io.crate.Constants) Symbols(io.crate.expression.symbol.Symbols) SqlParser(io.crate.sql.parser.SqlParser) Requests(org.elasticsearch.client.Requests) Listeners(com.carrotsearch.randomizedtesting.annotations.Listeners) TestExecutionConfig(io.crate.testing.TestExecutionConfig) ColumnIdent(io.crate.metadata.ColumnIdent) Plugin(org.elasticsearch.plugins.Plugin) Matchers(org.hamcrest.Matchers) DataType(io.crate.types.DataType) TimeUnit(java.util.concurrent.TimeUnit) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) Rule(org.junit.Rule) SystemPropsTestLoggingListener(io.crate.test.integration.SystemPropsTestLoggingListener) LogManager(org.apache.logging.log4j.LogManager) ConfigurationException(org.elasticsearch.common.inject.ConfigurationException) RetentionPolicy(java.lang.annotation.RetentionPolicy) SearchPath(io.crate.metadata.SearchPath) Functions(io.crate.metadata.Functions) FunctionImplementation(io.crate.metadata.FunctionImplementation)

Aggregations

SearchPath (io.crate.metadata.SearchPath)4 Symbol (io.crate.expression.symbol.Symbol)3 Lists2 (io.crate.common.collections.Lists2)2 Row (io.crate.data.Row)2 Literal (io.crate.expression.symbol.Literal)2 CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)2 FunctionImplementation (io.crate.metadata.FunctionImplementation)2 RelationName (io.crate.metadata.RelationName)2 DocTableInfo (io.crate.metadata.doc.DocTableInfo)2 TableInfo (io.crate.metadata.table.TableInfo)2 User (io.crate.user.User)2 RandomizedContext (com.carrotsearch.randomizedtesting.RandomizedContext)1 Listeners (com.carrotsearch.randomizedtesting.annotations.Listeners)1 TestGroup (com.carrotsearch.randomizedtesting.annotations.TestGroup)1 RandomStrings (com.carrotsearch.randomizedtesting.generators.RandomStrings)1 Constants (io.crate.Constants)1 SQLOperations (io.crate.action.sql.SQLOperations)1 Session (io.crate.action.sql.Session)1 SessionContext (io.crate.action.sql.SessionContext)1 Analyzer (io.crate.analyze.Analyzer)1