Search in sources :

Example 41 with Function

use of io.crate.expression.symbol.Function in project crate by crate.

the class SelectStatementAnalyzerTest method testWhereSelect.

@Test
public void testWhereSelect() throws Exception {
    var executor = SQLExecutor.builder(clusterService).build();
    QueriedSelectRelation relation = executor.analyze("select load from sys.nodes where load['1'] = 1.2 or 1 >= load['5']");
    assertThat(relation.groupBy().isEmpty(), is(true));
    Function whereClause = (Function) relation.where();
    assertThat(whereClause.name(), is(OrOperator.NAME));
    assertThat(whereClause.signature().getKind() == FunctionType.AGGREGATE, is(false));
    Function left = (Function) whereClause.arguments().get(0);
    assertThat(left.name(), is(EqOperator.NAME));
    assertThat(left.arguments().get(0), isReference("load['1']"));
    assertThat(left.arguments().get(1), IsInstanceOf.instanceOf(Literal.class));
    assertThat(left.arguments().get(1).valueType(), is(DataTypes.DOUBLE));
    Function right = (Function) whereClause.arguments().get(1);
    assertThat(right.name(), is(LteOperator.NAME));
    assertThat(right.arguments().get(0), isReference("load['5']"));
    assertThat(right.arguments().get(1), IsInstanceOf.instanceOf(Literal.class));
    assertThat(left.arguments().get(1).valueType(), is(DataTypes.DOUBLE));
}
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) Literal(io.crate.expression.symbol.Literal) SymbolMatchers.isLiteral(io.crate.testing.SymbolMatchers.isLiteral) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 42 with Function

use of io.crate.expression.symbol.Function in project crate by crate.

the class SelectStatementAnalyzerTest method testIsNullQuery.

@Test
public void testIsNullQuery() {
    var executor = SQLExecutor.builder(clusterService).build();
    QueriedSelectRelation relation = executor.analyze("select * from sys.nodes where id is not null");
    Function query = (Function) relation.where();
    assertThat(query.name(), is(NotPredicate.NAME));
    assertThat(query.arguments().get(0), instanceOf(Function.class));
    Function isNull = (Function) query.arguments().get(0);
    assertThat(isNull.name(), is(IsNullPredicate.NAME));
}
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) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 43 with Function

use of io.crate.expression.symbol.Function in project crate by crate.

the class CommonQueryBuilderTest method test_eq_on_alias_inner_func_uses_termquery.

@Test
public void test_eq_on_alias_inner_func_uses_termquery() throws Exception {
    // Testing expression: f(col as alias) = 'foo'
    AliasSymbol alias = new AliasSymbol("aliased", createReference("arr", DataTypes.INTEGER_ARRAY));
    var innerFunction = new Function(Signature.scalar("array_length", parseTypeSignature("array(E)"), DataTypes.INTEGER.getTypeSignature(), DataTypes.INTEGER.getTypeSignature()), // 1 is a dummy argument for dimension.
    List.of(alias, Literal.of(1)), DataTypes.INTEGER);
    var func = new Function(EqOperator.SIGNATURE, List.of(innerFunction, Literal.of(5)), DataTypes.BOOLEAN);
    Query query = queryTester.toQuery(func);
    assertThat(query, instanceOf(BooleanQuery.class));
}
Also used : Function(io.crate.expression.symbol.Function) BooleanQuery(org.apache.lucene.search.BooleanQuery) AliasSymbol(io.crate.expression.symbol.AliasSymbol) CrateRegexQuery(io.crate.lucene.match.CrateRegexQuery) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) RegexpQuery(org.apache.lucene.search.RegexpQuery) PointInSetQuery(org.apache.lucene.search.PointInSetQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) IntersectsPrefixTreeQuery(org.apache.lucene.spatial.prefix.IntersectsPrefixTreeQuery) PointRangeQuery(org.apache.lucene.search.PointRangeQuery) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) DocValuesFieldExistsQuery(org.apache.lucene.search.DocValuesFieldExistsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) Test(org.junit.Test)

Example 44 with Function

use of io.crate.expression.symbol.Function in project crate by crate.

the class GenericFunctionQuery method createWeight.

@Override
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
    return new Weight(this) {

        @Override
        public boolean isCacheable(LeafReaderContext ctx) {
            if (SymbolVisitors.any(s -> s instanceof Function && !((Function) s).isDeterministic(), function)) {
                return false;
            }
            var fields = new ArrayList<String>();
            RefVisitor.visitRefs(function, ref -> fields.add(ref.column().fqn()));
            return DocValues.isCacheable(ctx, fields.toArray(new String[0]));
        }

        @Override
        public void extractTerms(Set<Term> terms) {
        }

        @Override
        public Explanation explain(LeafReaderContext context, int doc) throws IOException {
            final Scorer s = scorer(context);
            final boolean match;
            final TwoPhaseIterator twoPhase = s.twoPhaseIterator();
            if (twoPhase == null) {
                match = s.iterator().advance(doc) == doc;
            } else {
                match = twoPhase.approximation().advance(doc) == doc && twoPhase.matches();
            }
            if (match) {
                assert s.score() == 0f : "score must be 0";
                return Explanation.match(0f, "Match on id " + doc);
            } else {
                return Explanation.match(0f, "No match on id " + doc);
            }
        }

        @Override
        public Scorer scorer(LeafReaderContext context) throws IOException {
            return new ConstantScoreScorer(this, 0f, scoreMode, getTwoPhaseIterator(context));
        }
    };
}
Also used : Function(io.crate.expression.symbol.Function) Set(java.util.Set) TwoPhaseIterator(org.apache.lucene.search.TwoPhaseIterator) ConstantScoreScorer(org.apache.lucene.search.ConstantScoreScorer) ArrayList(java.util.ArrayList) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) ConstantScoreScorer(org.apache.lucene.search.ConstantScoreScorer) Scorer(org.apache.lucene.search.Scorer) Weight(org.apache.lucene.search.Weight)

Example 45 with Function

use of io.crate.expression.symbol.Function in project crate by crate.

the class GroupHashAggregate method pruneOutputsExcept.

@Override
public LogicalPlan pruneOutputsExcept(TableStats tableStats, Collection<Symbol> outputsToKeep) {
    HashSet<Symbol> toKeep = new HashSet<>();
    // We cannot prune groupKeys, even if they are not used in the outputs, because it would change the result semantically
    for (Symbol groupKey : groupKeys) {
        SymbolVisitors.intersection(groupKey, source.outputs(), toKeep::add);
    }
    ArrayList<Function> newAggregates = new ArrayList<>();
    for (Symbol outputToKeep : outputsToKeep) {
        SymbolVisitors.intersection(outputToKeep, aggregates, newAggregates::add);
    }
    for (Function newAggregate : newAggregates) {
        SymbolVisitors.intersection(newAggregate, source.outputs(), toKeep::add);
    }
    LogicalPlan newSource = source.pruneOutputsExcept(tableStats, toKeep);
    if (newSource == source && aggregates.size() == newAggregates.size()) {
        return this;
    }
    return new GroupHashAggregate(newSource, groupKeys, newAggregates, numExpectedRows);
}
Also used : Function(io.crate.expression.symbol.Function) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol) Symbol(io.crate.expression.symbol.Symbol) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

Function (io.crate.expression.symbol.Function)80 Test (org.junit.Test)56 Symbol (io.crate.expression.symbol.Symbol)43 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)43 SymbolMatchers.isFunction (io.crate.testing.SymbolMatchers.isFunction)35 SubscriptFunction (io.crate.expression.scalar.SubscriptFunction)25 TryCastFunction (io.crate.expression.scalar.cast.TryCastFunction)24 ExplicitCastFunction (io.crate.expression.scalar.cast.ExplicitCastFunction)23 DistanceFunction (io.crate.expression.scalar.geo.DistanceFunction)23 AliasSymbol (io.crate.expression.symbol.AliasSymbol)13 ParameterSymbol (io.crate.expression.symbol.ParameterSymbol)13 SelectSymbol (io.crate.expression.symbol.SelectSymbol)13 InputColumn (io.crate.expression.symbol.InputColumn)10 ArrayList (java.util.ArrayList)10 Literal (io.crate.expression.symbol.Literal)9 ArraySliceFunction (io.crate.expression.scalar.ArraySliceFunction)8 FunctionImplementation (io.crate.metadata.FunctionImplementation)8 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)7 ScopedSymbol (io.crate.expression.symbol.ScopedSymbol)6 Reference (io.crate.metadata.Reference)6