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));
}
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));
}
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));
}
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));
}
};
}
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);
}
Aggregations