Search in sources :

Example 6 with Literal

use of io.crate.analyze.symbol.Literal in project crate by crate.

the class AnyNotLikeOperatorTest method anyNotLike.

private Boolean anyNotLike(String pattern, String... expressions) {
    Literal patternLiteral = Literal.of(pattern);
    Object[] value = new Object[expressions.length];
    for (int i = 0; i < expressions.length; i++) {
        value[i] = expressions[i] == null ? null : new BytesRef(expressions[i]);
    }
    Literal valuesLiteral = Literal.of(new ArrayType(DataTypes.STRING), value);
    AnyNotLikeOperator impl = (AnyNotLikeOperator) new AnyNotLikeOperator.AnyNotLikeResolver().getForTypes(Arrays.asList(DataTypes.STRING, valuesLiteral.valueType()));
    return impl.evaluate(patternLiteral, valuesLiteral);
}
Also used : ArrayType(io.crate.types.ArrayType) Literal(io.crate.analyze.symbol.Literal) BytesRef(org.apache.lucene.util.BytesRef)

Example 7 with Literal

use of io.crate.analyze.symbol.Literal in project crate by crate.

the class AnyNotLikeOperatorTest method testNegateNotLike.

@Test
public void testNegateNotLike() throws Exception {
    Literal patternLiteral = Literal.of("A");
    Literal valuesLiteral = Literal.of(new ArrayType(DataTypes.STRING), new Object[] { new BytesRef("A"), new BytesRef("B") });
    FunctionImplementation impl = new AnyNotLikeOperator.AnyNotLikeResolver().getForTypes(Arrays.asList(DataTypes.STRING, valuesLiteral.valueType()));
    Function anyNotLikeFunction = new Function(impl.info(), Arrays.<Symbol>asList(patternLiteral, valuesLiteral));
    Input<Boolean> normalized = (Input<Boolean>) impl.normalizeSymbol(anyNotLikeFunction, new TransactionContext(SessionContext.SYSTEM_SESSION));
    assertThat(normalized.value(), is(true));
    assertThat(new NotPredicate().evaluate(normalized), is(false));
}
Also used : ArrayType(io.crate.types.ArrayType) Function(io.crate.analyze.symbol.Function) Input(io.crate.data.Input) TransactionContext(io.crate.metadata.TransactionContext) Literal(io.crate.analyze.symbol.Literal) FunctionImplementation(io.crate.metadata.FunctionImplementation) NotPredicate(io.crate.operation.predicate.NotPredicate) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 8 with Literal

use of io.crate.analyze.symbol.Literal in project crate by crate.

the class AnyNotLikeOperatorTest method normalizeSymbol.

private static Symbol normalizeSymbol(String pattern, String... expressions) {
    Literal patternLiteral = Literal.of(pattern);
    Object[] value = new Object[expressions.length];
    for (int i = 0; i < expressions.length; i++) {
        value[i] = expressions[i] == null ? null : new BytesRef(expressions[i]);
    }
    Literal valuesLiteral = Literal.of(new ArrayType(DataTypes.STRING), value);
    AnyNotLikeOperator impl = (AnyNotLikeOperator) new AnyNotLikeOperator.AnyNotLikeResolver().getForTypes(Arrays.asList(patternLiteral.valueType(), valuesLiteral.valueType()));
    Function function = new Function(impl.info(), Arrays.<Symbol>asList(patternLiteral, valuesLiteral));
    return impl.normalizeSymbol(function, new TransactionContext(SessionContext.SYSTEM_SESSION));
}
Also used : ArrayType(io.crate.types.ArrayType) Function(io.crate.analyze.symbol.Function) TransactionContext(io.crate.metadata.TransactionContext) Literal(io.crate.analyze.symbol.Literal) BytesRef(org.apache.lucene.util.BytesRef)

Example 9 with Literal

use of io.crate.analyze.symbol.Literal in project crate by crate.

the class MatchesFunction method compile.

@Override
public Scalar<BytesRef[], Object> compile(List<Symbol> arguments) {
    assert arguments.size() > 1 : "number of arguments must be > 1";
    String pattern = null;
    if (arguments.get(1).symbolType() == SymbolType.LITERAL) {
        Literal literal = (Literal) arguments.get(1);
        Object patternVal = literal.value();
        if (patternVal == null) {
            return this;
        }
        pattern = ((BytesRef) patternVal).utf8ToString();
    }
    BytesRef flags = null;
    if (arguments.size() == 3) {
        assert arguments.get(2).symbolType() == SymbolType.LITERAL : "3rd argument must be a " + SymbolType.LITERAL;
        flags = (BytesRef) ((Literal) arguments.get(2)).value();
    }
    if (pattern != null) {
        regexMatcher = new RegexMatcher(pattern, flags);
    } else {
        regexMatcher = null;
    }
    return this;
}
Also used : Literal(io.crate.analyze.symbol.Literal) BytesRef(org.apache.lucene.util.BytesRef)

Example 10 with Literal

use of io.crate.analyze.symbol.Literal in project crate by crate.

the class ReplaceFunction method compile.

@Override
public Scalar<BytesRef, Object> compile(List<Symbol> arguments) {
    assert arguments.size() >= 3 : "number of arguments muts be > 3";
    String pattern = null;
    if (arguments.get(1).symbolType() == SymbolType.LITERAL) {
        Literal literal = (Literal) arguments.get(1);
        Object patternVal = literal.value();
        if (patternVal == null) {
            return this;
        }
        pattern = ((BytesRef) patternVal).utf8ToString();
    }
    BytesRef flags = null;
    if (arguments.size() == 4) {
        assert arguments.get(3).symbolType() == SymbolType.LITERAL : "4th argument must be of type " + SymbolType.LITERAL;
        flags = (BytesRef) ((Literal) arguments.get(2)).value();
    }
    if (pattern != null) {
        regexMatcher = new RegexMatcher(pattern, flags);
    } else {
        regexMatcher = null;
    }
    return this;
}
Also used : Literal(io.crate.analyze.symbol.Literal) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

Literal (io.crate.analyze.symbol.Literal)15 BytesRef (org.apache.lucene.util.BytesRef)8 ArrayType (io.crate.types.ArrayType)6 Function (io.crate.analyze.symbol.Function)4 Symbol (io.crate.analyze.symbol.Symbol)4 Input (io.crate.data.Input)4 TransactionContext (io.crate.metadata.TransactionContext)4 CrateUnitTest (io.crate.test.integration.CrateUnitTest)3 Test (org.junit.Test)3 ImmutableList (com.google.common.collect.ImmutableList)2 WhereClause (io.crate.analyze.WhereClause)2 FunctionImplementation (io.crate.metadata.FunctionImplementation)2 NotPredicate (io.crate.operation.predicate.NotPredicate)2 EvaluatingNormalizer (io.crate.analyze.EvaluatingNormalizer)1 SymbolToTrueVisitor (io.crate.analyze.SymbolToTrueVisitor)1 ColumnValidationException (io.crate.exceptions.ColumnValidationException)1 ConversionException (io.crate.exceptions.ConversionException)1 InputFactory (io.crate.operation.InputFactory)1 CollectExpression (io.crate.operation.collect.CollectExpression)1 PartitionExpression (io.crate.operation.reference.partitioned.PartitionExpression)1