Search in sources :

Example 1 with NumericLiteral

use of org.locationtech.geowave.core.store.query.filter.expression.numeric.NumericLiteral in project geowave by locationtech.

the class FilterExpressionTest method testInvalidComparisons.

@Test
public void testInvalidComparisons() throws URISyntaxException {
    final TextLiteral textLit = TextLiteral.of("text");
    final NumericLiteral doubleLit = NumericLiteral.of(0.5);
    final NumericLiteral integerLit = NumericLiteral.of(1);
    final GenericLiteral dateLit = GenericLiteral.of(new Date(100));
    final GenericLiteral dateLit2 = GenericLiteral.of(new Date(500));
    final GenericLiteral uriLit = GenericLiteral.of(new URI("test"));
    final GenericLiteral nonComparable = GenericLiteral.of(Lists.newArrayList());
    try {
        doubleLit.isGreaterThan(textLit).evaluate(null, null);
        fail();
    } catch (RuntimeException e) {
    // Expected
    }
    try {
        textLit.isGreaterThan(doubleLit).evaluate(null, null);
    } catch (RuntimeException e) {
    // Expected
    }
    try {
        textLit.isLessThan(dateLit).evaluate(null, null);
    } catch (RuntimeException e) {
    // Expected
    }
    try {
        doubleLit.isBetween("test", 1).evaluate(null, null);
        fail();
    } catch (RuntimeException e) {
    // Expected
    }
    try {
        doubleLit.isBetween(0, "test").evaluate(null, null);
        fail();
    } catch (RuntimeException e) {
    // Expected
    }
    try {
        integerLit.isBetween("test", "test2").evaluate(null, null);
        fail();
    } catch (RuntimeException e) {
    // Expected
    }
    try {
        doubleLit.isBetween(dateLit2, uriLit).evaluate(null, null);
        fail();
    } catch (RuntimeException e) {
    // Expected
    }
}
Also used : NumericLiteral(org.locationtech.geowave.core.store.query.filter.expression.numeric.NumericLiteral) TextLiteral(org.locationtech.geowave.core.store.query.filter.expression.text.TextLiteral) URI(java.net.URI) Date(java.util.Date) Test(org.junit.Test)

Example 2 with NumericLiteral

use of org.locationtech.geowave.core.store.query.filter.expression.numeric.NumericLiteral in project geowave by locationtech.

the class FilterExpressionTest method testNumericExpressions.

@Test
public void testNumericExpressions() {
    final DataTypeAdapter<TestType> adapter = new TestTypeBasicDataAdapter();
    final TestType entry = new TestType("test", 1.3, 5, true);
    final TestType entryNulls = new TestType(null, null, null, null);
    final NumericLiteral doubleLit = NumericLiteral.of(0.5);
    final NumericLiteral integerLit = NumericLiteral.of(1);
    final NumericFieldValue doubleField = NumericFieldValue.of("doubleField");
    final NumericFieldValue intField = NumericFieldValue.of("intField");
    assertEquals(1.3, (double) doubleField.evaluateValue(adapter, entry), EPSILON);
    assertEquals(5, intField.evaluateValue(adapter, entry).intValue());
    // Test comparisons
    assertTrue(doubleLit.isLessThan(integerLit).evaluate(adapter, entry));
    assertFalse(integerLit.isLessThan(doubleLit).evaluate(adapter, entry));
    assertTrue(doubleField.isLessThan(1.5).evaluate(adapter, entry));
    assertFalse(doubleField.isLessThan(doubleLit).evaluate(adapter, entry));
    assertFalse(doubleField.isLessThan(integerLit).evaluate(adapter, entry));
    assertTrue(doubleField.isLessThan(intField).evaluate(adapter, entry));
    assertFalse(intField.isLessThan(doubleField).evaluate(adapter, entry));
    assertTrue(doubleLit.isGreaterThan(0).evaluate(adapter, entry));
    assertFalse(doubleLit.isGreaterThan(1).evaluate(adapter, entry));
    assertTrue(intField.isGreaterThan(1.0).evaluate(adapter, entry));
    assertTrue(intField.isGreaterThan(doubleLit).evaluate(adapter, entry));
    assertTrue(intField.isGreaterThan(integerLit).evaluate(adapter, entry));
    assertFalse(intField.isGreaterThan(6).evaluate(adapter, entry));
    assertTrue(intField.isGreaterThan(doubleField).evaluate(adapter, entry));
    assertFalse(doubleField.isGreaterThan(intField).evaluate(adapter, entry));
    assertTrue(integerLit.isGreaterThanOrEqualTo(0).evaluate(adapter, entry));
    assertTrue(integerLit.isGreaterThanOrEqualTo(integerLit).evaluate(adapter, entry));
    assertFalse(integerLit.isGreaterThanOrEqualTo(2).evaluate(adapter, entry));
    assertTrue(doubleLit.isLessThanOrEqualTo(1).evaluate(adapter, entry));
    assertTrue(doubleLit.isLessThanOrEqualTo(0.5).evaluate(adapter, entry));
    assertFalse(doubleLit.isLessThanOrEqualTo(0).evaluate(adapter, entry));
    assertTrue(doubleLit.isEqualTo(0.5).evaluate(adapter, entry));
    assertFalse(doubleLit.isEqualTo(0.4).evaluate(adapter, entry));
    assertTrue(doubleLit.isNotEqualTo(0.4).evaluate(adapter, entry));
    assertFalse(doubleLit.isNotEqualTo(0.5).evaluate(adapter, entry));
    assertFalse(doubleLit.isNull().evaluate(adapter, entry));
    assertFalse(integerLit.isNull().evaluate(adapter, entry));
    assertFalse(doubleField.isNull().evaluate(adapter, entry));
    assertFalse(intField.isNull().evaluate(adapter, entry));
    assertTrue(doubleField.isNull().evaluate(adapter, entryNulls));
    assertTrue(intField.isNull().evaluate(adapter, entryNulls));
    assertTrue(doubleLit.isNotNull().evaluate(adapter, entry));
    assertTrue(integerLit.isNotNull().evaluate(adapter, entry));
    assertTrue(doubleField.isNotNull().evaluate(adapter, entry));
    assertTrue(intField.isNotNull().evaluate(adapter, entry));
    assertFalse(doubleField.isNotNull().evaluate(adapter, entryNulls));
    assertFalse(intField.isNotNull().evaluate(adapter, entryNulls));
    assertFalse(doubleField.isLessThan(null).evaluate(adapter, entry));
    assertFalse(doubleField.isGreaterThan(null).evaluate(adapter, entry));
    assertFalse(doubleField.isLessThanOrEqualTo(null).evaluate(adapter, entry));
    assertFalse(doubleField.isGreaterThanOrEqualTo(null).evaluate(adapter, entry));
    assertFalse(doubleField.isEqualTo(null).evaluate(adapter, entry));
    assertTrue(doubleField.isNotEqualTo(null).evaluate(adapter, entry));
    assertTrue(doubleField.isEqualTo(intField).evaluate(adapter, entryNulls));
    assertFalse(doubleField.isEqualTo(doubleLit).evaluate(adapter, entryNulls));
    assertFalse(doubleField.isNotEqualTo(null).evaluate(adapter, entryNulls));
    assertTrue(doubleField.isNotEqualTo(doubleLit).evaluate(adapter, entryNulls));
    assertTrue(doubleLit.isBetween(0, 1).evaluate(adapter, entry));
    assertFalse(doubleLit.isBetween(integerLit, intField).evaluate(adapter, entry));
    assertTrue(doubleField.isBetween(doubleLit, intField).evaluate(adapter, entry));
    assertFalse(doubleField.isBetween(doubleLit, intField).evaluate(adapter, entryNulls));
    assertFalse(doubleLit.isBetween(integerLit, intField).evaluate(adapter, entryNulls));
    assertFalse(doubleLit.isBetween(intField, integerLit).evaluate(adapter, entryNulls));
    assertFalse(intField.isBetween(doubleLit, integerLit).evaluate(adapter, entry));
    assertTrue(integerLit.add(1).isLiteral());
    assertFalse(intField.add(1).isLiteral());
    assertTrue(integerLit.add(doubleLit).isLiteral());
    assertFalse(integerLit.add(doubleField).isLiteral());
    assertTrue(doubleLit.abs().isLiteral());
    assertFalse(doubleField.abs().isLiteral());
    // Test math
    assertNull(doubleField.abs().evaluateValue(adapter, entryNulls));
    assertEquals(5.3, (double) NumericLiteral.of(-5.3).abs().evaluateValue(null, null), EPSILON);
    assertEquals(5.3, (double) NumericLiteral.of(5.3).abs().evaluateValue(null, null), EPSILON);
    assertEquals(2.7, (double) doubleField.abs().evaluateValue(adapter, new TestType("test", -2.7, 5, true)), EPSILON);
    assertEquals(5, (double) intField.abs().evaluateValue(adapter, new TestType("test", -2.7, 5, true)), EPSILON);
    assertEquals(28, (double) NumericLiteral.of(5).add(15).divideBy(4).multiplyBy(8).subtract(12).evaluateValue(null, null), EPSILON);
    assertNull(doubleField.add(1).evaluateValue(adapter, entryNulls));
    assertNull(doubleLit.add(intField).evaluateValue(adapter, entryNulls));
    assertNull(doubleField.add(intField).evaluateValue(adapter, entryNulls));
    // Test complex
    // ((1.3 + 0.8) * (5 - 1)) / 3.2
    assertEquals(2.625, (double) doubleField.add(0.8).multiplyBy(intField.subtract(integerLit)).divideBy(3.2).evaluateValue(adapter, entry), EPSILON);
    try {
        integerLit.add("test");
        fail();
    } catch (RuntimeException e) {
    // Expected
    }
    // Test serialization
    byte[] bytes = PersistenceUtils.toBinary(doubleField.add(5));
    final Add add = (Add) PersistenceUtils.fromBinary(bytes);
    assertTrue(add.getExpression1() instanceof NumericFieldValue);
    assertEquals("doubleField", ((NumericFieldValue) add.getExpression1()).getFieldName());
    assertTrue(add.getExpression2() instanceof NumericLiteral);
    assertEquals(5L, ((Number) ((NumericLiteral) add.getExpression2()).getValue()).longValue());
    bytes = PersistenceUtils.toBinary(doubleField.subtract(5));
    final Subtract subtract = (Subtract) PersistenceUtils.fromBinary(bytes);
    assertTrue(subtract.getExpression1() instanceof NumericFieldValue);
    assertEquals("doubleField", ((NumericFieldValue) subtract.getExpression1()).getFieldName());
    assertTrue(subtract.getExpression2() instanceof NumericLiteral);
    assertEquals(5L, ((Number) ((NumericLiteral) subtract.getExpression2()).getValue()).longValue());
    bytes = PersistenceUtils.toBinary(doubleField.multiplyBy(5));
    final Multiply multiply = (Multiply) PersistenceUtils.fromBinary(bytes);
    assertTrue(multiply.getExpression1() instanceof NumericFieldValue);
    assertEquals("doubleField", ((NumericFieldValue) multiply.getExpression1()).getFieldName());
    assertTrue(multiply.getExpression2() instanceof NumericLiteral);
    assertEquals(5L, ((Number) ((NumericLiteral) multiply.getExpression2()).getValue()).longValue());
    bytes = PersistenceUtils.toBinary(doubleField.divideBy(null));
    final Divide divide = (Divide) PersistenceUtils.fromBinary(bytes);
    assertTrue(divide.getExpression1() instanceof NumericFieldValue);
    assertEquals("doubleField", ((NumericFieldValue) divide.getExpression1()).getFieldName());
    assertTrue(divide.getExpression2() instanceof NumericLiteral);
    assertNull(((NumericLiteral) divide.getExpression2()).getValue());
    bytes = PersistenceUtils.toBinary(doubleField.abs());
    final Abs abs = (Abs) PersistenceUtils.fromBinary(bytes);
    assertTrue(abs.getExpression() instanceof NumericFieldValue);
    assertEquals("doubleField", ((NumericFieldValue) abs.getExpression()).getFieldName());
    bytes = PersistenceUtils.toBinary(doubleField.isLessThan(5));
    NumericComparisonOperator compareOp = (NumericComparisonOperator) PersistenceUtils.fromBinary(bytes);
    assertEquals(CompareOp.LESS_THAN, compareOp.getCompareOp());
    assertTrue(compareOp.getExpression1() instanceof NumericFieldValue);
    assertEquals("doubleField", ((NumericFieldValue) compareOp.getExpression1()).getFieldName());
    assertTrue(compareOp.getExpression2() instanceof NumericLiteral);
    assertEquals(5L, ((Number) ((NumericLiteral) compareOp.getExpression2()).getValue()).longValue());
    bytes = PersistenceUtils.toBinary(doubleField.isLessThanOrEqualTo(5));
    compareOp = (NumericComparisonOperator) PersistenceUtils.fromBinary(bytes);
    assertEquals(CompareOp.LESS_THAN_OR_EQUAL, compareOp.getCompareOp());
    assertTrue(compareOp.getExpression1() instanceof NumericFieldValue);
    assertEquals("doubleField", ((NumericFieldValue) compareOp.getExpression1()).getFieldName());
    assertTrue(compareOp.getExpression2() instanceof NumericLiteral);
    assertEquals(5L, ((Number) ((NumericLiteral) compareOp.getExpression2()).getValue()).longValue());
    bytes = PersistenceUtils.toBinary(doubleField.isGreaterThan(5));
    compareOp = (NumericComparisonOperator) PersistenceUtils.fromBinary(bytes);
    assertEquals(CompareOp.GREATER_THAN, compareOp.getCompareOp());
    assertTrue(compareOp.getExpression1() instanceof NumericFieldValue);
    assertEquals("doubleField", ((NumericFieldValue) compareOp.getExpression1()).getFieldName());
    assertTrue(compareOp.getExpression2() instanceof NumericLiteral);
    assertEquals(5L, ((Number) ((NumericLiteral) compareOp.getExpression2()).getValue()).longValue());
    bytes = PersistenceUtils.toBinary(doubleField.isGreaterThanOrEqualTo(5));
    compareOp = (NumericComparisonOperator) PersistenceUtils.fromBinary(bytes);
    assertEquals(CompareOp.GREATER_THAN_OR_EQUAL, compareOp.getCompareOp());
    assertTrue(compareOp.getExpression1() instanceof NumericFieldValue);
    assertEquals("doubleField", ((NumericFieldValue) compareOp.getExpression1()).getFieldName());
    assertTrue(compareOp.getExpression2() instanceof NumericLiteral);
    assertEquals(5L, ((Number) ((NumericLiteral) compareOp.getExpression2()).getValue()).longValue());
    bytes = PersistenceUtils.toBinary(doubleField.isEqualTo(5));
    compareOp = (NumericComparisonOperator) PersistenceUtils.fromBinary(bytes);
    assertEquals(CompareOp.EQUAL_TO, compareOp.getCompareOp());
    assertTrue(compareOp.getExpression1() instanceof NumericFieldValue);
    assertEquals("doubleField", ((NumericFieldValue) compareOp.getExpression1()).getFieldName());
    assertTrue(compareOp.getExpression2() instanceof NumericLiteral);
    assertEquals(5L, ((Number) ((NumericLiteral) compareOp.getExpression2()).getValue()).longValue());
    bytes = PersistenceUtils.toBinary(doubleField.isNotEqualTo(5));
    compareOp = (NumericComparisonOperator) PersistenceUtils.fromBinary(bytes);
    assertEquals(CompareOp.NOT_EQUAL_TO, compareOp.getCompareOp());
    assertTrue(compareOp.getExpression1() instanceof NumericFieldValue);
    assertEquals("doubleField", ((NumericFieldValue) compareOp.getExpression1()).getFieldName());
    assertTrue(compareOp.getExpression2() instanceof NumericLiteral);
    assertEquals(5L, ((Number) ((NumericLiteral) compareOp.getExpression2()).getValue()).longValue());
    bytes = PersistenceUtils.toBinary(doubleField.isBetween(5, 10));
    final Between<?, ?> between = (Between<?, ?>) PersistenceUtils.fromBinary(bytes);
    assertTrue(between.getValue() instanceof NumericFieldValue);
    assertEquals("doubleField", ((NumericFieldValue) between.getValue()).getFieldName());
    assertTrue(between.getLowerBound() instanceof NumericLiteral);
    assertEquals(5L, ((Number) ((NumericLiteral) between.getLowerBound()).getValue()).longValue());
    assertTrue(between.getUpperBound() instanceof NumericLiteral);
    assertEquals(10L, ((Number) ((NumericLiteral) between.getUpperBound()).getValue()).longValue());
    bytes = PersistenceUtils.toBinary(doubleField.isNull());
    final IsNull isNull = (IsNull) PersistenceUtils.fromBinary(bytes);
    assertTrue(isNull.getExpression() instanceof NumericFieldValue);
    assertEquals("doubleField", ((NumericFieldValue) isNull.getExpression()).getFieldName());
    bytes = PersistenceUtils.toBinary(doubleField.isNotNull());
    final IsNotNull isNotNull = (IsNotNull) PersistenceUtils.fromBinary(bytes);
    assertTrue(isNotNull.getExpression() instanceof NumericFieldValue);
    assertEquals("doubleField", ((NumericFieldValue) isNotNull.getExpression()).getFieldName());
    try {
        NumericFieldValue.of("name").evaluateValue(adapter, entry);
        fail();
    } catch (RuntimeException e) {
    // expected
    }
}
Also used : Add(org.locationtech.geowave.core.store.query.filter.expression.numeric.Add) NumericLiteral(org.locationtech.geowave.core.store.query.filter.expression.numeric.NumericLiteral) NumericComparisonOperator(org.locationtech.geowave.core.store.query.filter.expression.numeric.NumericComparisonOperator) NumericFieldValue(org.locationtech.geowave.core.store.query.filter.expression.numeric.NumericFieldValue) TestType(org.locationtech.geowave.core.store.adapter.AbstractDataTypeAdapterTest.TestType) TestTypeBasicDataAdapter(org.locationtech.geowave.core.store.adapter.AbstractDataTypeAdapterTest.TestTypeBasicDataAdapter) Divide(org.locationtech.geowave.core.store.query.filter.expression.numeric.Divide) Abs(org.locationtech.geowave.core.store.query.filter.expression.numeric.Abs) Multiply(org.locationtech.geowave.core.store.query.filter.expression.numeric.Multiply) Subtract(org.locationtech.geowave.core.store.query.filter.expression.numeric.Subtract) Test(org.junit.Test)

Example 3 with NumericLiteral

use of org.locationtech.geowave.core.store.query.filter.expression.numeric.NumericLiteral in project geowave by locationtech.

the class DeleteStatementTest method testComplexStatement.

@Test
public void testComplexStatement() {
    final DataStore dataStore = createDataStore();
    final Statement statement = GWQLParser.parseStatement(dataStore, "DELETE FROM type " + "WHERE (pop < 1) " + "AND ((pop > 48 OR pid > 'a') AND (pop BETWEEN 0 AND 10 OR pid <= 'b'))");
    assertTrue(statement instanceof DeleteStatement);
    final DeleteStatement<?> deleteStatement = (DeleteStatement<?>) statement;
    assertNotNull(deleteStatement.getAdapter());
    assertEquals("type", deleteStatement.getAdapter().getTypeName());
    assertNotNull(deleteStatement.getFilter());
    final Filter filter = deleteStatement.getFilter();
    assertTrue(filter instanceof And);
    And andFilter = (And) filter;
    assertTrue(andFilter.getChildren().length == 2);
    assertTrue(andFilter.getChildren()[0] instanceof NumericComparisonOperator);
    NumericComparisonOperator compareOp = (NumericComparisonOperator) andFilter.getChildren()[0];
    assertTrue(compareOp.getCompareOp().equals(CompareOp.LESS_THAN));
    assertTrue(compareOp.getExpression1() instanceof NumericFieldValue);
    assertEquals("pop", ((NumericFieldValue) compareOp.getExpression1()).getFieldName());
    assertTrue(compareOp.getExpression2() instanceof NumericLiteral);
    assertEquals(1, ((NumericLiteral) compareOp.getExpression2()).getValue(), 0.00001);
    assertTrue(andFilter.getChildren()[1] instanceof And);
    andFilter = (And) andFilter.getChildren()[1];
    assertTrue(andFilter.getChildren().length == 2);
    assertTrue(andFilter.getChildren()[0] instanceof Or);
    Or orFilter = (Or) andFilter.getChildren()[0];
    assertTrue(orFilter.getChildren().length == 2);
    assertTrue(orFilter.getChildren()[0] instanceof NumericComparisonOperator);
    final NumericComparisonOperator numericCompare = (NumericComparisonOperator) orFilter.getChildren()[0];
    assertTrue(numericCompare.getExpression1() instanceof NumericFieldValue);
    assertEquals("pop", ((NumericFieldValue) numericCompare.getExpression1()).getFieldName());
    assertTrue(numericCompare.getExpression2() instanceof NumericLiteral);
    assertEquals(48, ((NumericLiteral) numericCompare.getExpression2()).evaluateValue(null), 0.00001);
    assertTrue(orFilter.getChildren()[1] instanceof TextComparisonOperator);
    TextComparisonOperator textCompareOp = (TextComparisonOperator) orFilter.getChildren()[1];
    assertTrue(textCompareOp.getCompareOp().equals(CompareOp.GREATER_THAN));
    assertTrue(textCompareOp.getExpression1() instanceof TextFieldValue);
    assertEquals("pid", ((TextFieldValue) textCompareOp.getExpression1()).getFieldName());
    assertTrue(textCompareOp.getExpression2() instanceof TextLiteral);
    assertEquals("a", ((TextLiteral) textCompareOp.getExpression2()).getValue());
    assertTrue(andFilter.getChildren()[1] instanceof Or);
    orFilter = (Or) andFilter.getChildren()[1];
    assertTrue(orFilter.getChildren().length == 2);
    assertTrue(orFilter.getChildren()[0] instanceof NumericBetween);
    NumericBetween between = (NumericBetween) orFilter.getChildren()[0];
    assertTrue(between.getValue() instanceof NumericFieldValue);
    assertEquals("pop", ((NumericFieldValue) between.getValue()).getFieldName());
    assertTrue(between.getLowerBound() instanceof NumericLiteral);
    assertEquals(0, ((NumericLiteral) between.getLowerBound()).getValue(), 0.00001);
    assertTrue(between.getUpperBound() instanceof NumericLiteral);
    assertEquals(10, ((NumericLiteral) between.getUpperBound()).getValue(), 0.00001);
    assertTrue(orFilter.getChildren()[1] instanceof TextComparisonOperator);
    textCompareOp = (TextComparisonOperator) orFilter.getChildren()[1];
    assertTrue(textCompareOp.getCompareOp().equals(CompareOp.LESS_THAN_OR_EQUAL));
    assertTrue(textCompareOp.getExpression1() instanceof TextFieldValue);
    assertEquals("pid", ((TextFieldValue) textCompareOp.getExpression1()).getFieldName());
    assertTrue(textCompareOp.getExpression2() instanceof TextLiteral);
    assertEquals("b", ((TextLiteral) textCompareOp.getExpression2()).getValue());
}
Also used : Or(org.locationtech.geowave.core.store.query.filter.expression.Or) TextComparisonOperator(org.locationtech.geowave.core.store.query.filter.expression.text.TextComparisonOperator) NumericLiteral(org.locationtech.geowave.core.store.query.filter.expression.numeric.NumericLiteral) NumericComparisonOperator(org.locationtech.geowave.core.store.query.filter.expression.numeric.NumericComparisonOperator) DeleteStatement(org.locationtech.geowave.core.store.query.gwql.statement.DeleteStatement) Statement(org.locationtech.geowave.core.store.query.gwql.statement.Statement) NumericBetween(org.locationtech.geowave.core.store.query.filter.expression.numeric.NumericBetween) NumericFieldValue(org.locationtech.geowave.core.store.query.filter.expression.numeric.NumericFieldValue) DeleteStatement(org.locationtech.geowave.core.store.query.gwql.statement.DeleteStatement) TextFieldValue(org.locationtech.geowave.core.store.query.filter.expression.text.TextFieldValue) Filter(org.locationtech.geowave.core.store.query.filter.expression.Filter) And(org.locationtech.geowave.core.store.query.filter.expression.And) DataStore(org.locationtech.geowave.core.store.api.DataStore) TextLiteral(org.locationtech.geowave.core.store.query.filter.expression.text.TextLiteral) Test(org.junit.Test)

Example 4 with NumericLiteral

use of org.locationtech.geowave.core.store.query.filter.expression.numeric.NumericLiteral in project geowave by locationtech.

the class GWQLParserTest method testExpressionFunctions.

@Test
public void testExpressionFunctions() {
    final DataStore dataStore = createDataStore();
    final String statement = "SELECT * FROM type WHERE abs(pop) > 10 AND strStartsWith(concat(pid, 'value'), 'abc')";
    final Statement gwStatement = GWQLParser.parseStatement(dataStore, statement);
    assertTrue(gwStatement instanceof SelectStatement);
    final SelectStatement<?> selectStatement = (SelectStatement<?>) gwStatement;
    assertFalse(selectStatement.isAggregation());
    assertNotNull(selectStatement.getAdapter());
    assertEquals("type", selectStatement.getAdapter().getTypeName());
    assertNotNull(selectStatement.getFilter());
    final Filter filter = selectStatement.getFilter();
    assertTrue(filter instanceof And);
    final And and = (And) filter;
    assertEquals(2, and.getChildren().length);
    assertTrue(and.getChildren()[0] instanceof NumericComparisonOperator);
    final NumericComparisonOperator compareOp = (NumericComparisonOperator) and.getChildren()[0];
    assertTrue(compareOp.getCompareOp().equals(CompareOp.GREATER_THAN));
    assertTrue(compareOp.getExpression1() instanceof Abs);
    assertTrue(((Abs) compareOp.getExpression1()).getExpression() instanceof NumericFieldValue);
    assertEquals("pop", ((NumericFieldValue) ((Abs) compareOp.getExpression1()).getExpression()).getFieldName());
    assertTrue(compareOp.getExpression2() instanceof NumericLiteral);
    assertEquals(10.0, ((NumericLiteral) compareOp.getExpression2()).getValue(), 0.00001);
    assertTrue(and.getChildren()[1] instanceof StartsWith);
    final StartsWith startsWith = (StartsWith) and.getChildren()[1];
    assertTrue(startsWith.getExpression1() instanceof Concat);
    assertTrue(((Concat) startsWith.getExpression1()).getExpression1() instanceof TextFieldValue);
    assertEquals("pid", ((TextFieldValue) ((Concat) startsWith.getExpression1()).getExpression1()).getFieldName());
    assertTrue(((Concat) startsWith.getExpression1()).getExpression2() instanceof TextLiteral);
    assertEquals("value", ((TextLiteral) ((Concat) startsWith.getExpression1()).getExpression2()).getValue());
    assertTrue(startsWith.getExpression2() instanceof TextLiteral);
    assertEquals("abc", ((TextLiteral) startsWith.getExpression2()).getValue());
}
Also used : NumericLiteral(org.locationtech.geowave.core.store.query.filter.expression.numeric.NumericLiteral) NumericComparisonOperator(org.locationtech.geowave.core.store.query.filter.expression.numeric.NumericComparisonOperator) Statement(org.locationtech.geowave.core.store.query.gwql.statement.Statement) SelectStatement(org.locationtech.geowave.core.store.query.gwql.statement.SelectStatement) NumericFieldValue(org.locationtech.geowave.core.store.query.filter.expression.numeric.NumericFieldValue) StartsWith(org.locationtech.geowave.core.store.query.filter.expression.text.StartsWith) TextFieldValue(org.locationtech.geowave.core.store.query.filter.expression.text.TextFieldValue) Concat(org.locationtech.geowave.core.store.query.filter.expression.text.Concat) SelectStatement(org.locationtech.geowave.core.store.query.gwql.statement.SelectStatement) Abs(org.locationtech.geowave.core.store.query.filter.expression.numeric.Abs) Filter(org.locationtech.geowave.core.store.query.filter.expression.Filter) And(org.locationtech.geowave.core.store.query.filter.expression.And) DataStore(org.locationtech.geowave.core.store.api.DataStore) TextLiteral(org.locationtech.geowave.core.store.query.filter.expression.text.TextLiteral) Test(org.junit.Test)

Example 5 with NumericLiteral

use of org.locationtech.geowave.core.store.query.filter.expression.numeric.NumericLiteral in project geowave by locationtech.

the class GWQLParserTest method testMathExpression.

@Test
public void testMathExpression() {
    final DataStore dataStore = createDataStore();
    String statement = "SELECT * FROM type WHERE pop + 5 > 25";
    Statement gwStatement = GWQLParser.parseStatement(dataStore, statement);
    assertTrue(gwStatement instanceof SelectStatement);
    SelectStatement<?> selectStatement = (SelectStatement<?>) gwStatement;
    assertNotNull(selectStatement.getFilter());
    Filter filter = selectStatement.getFilter();
    assertTrue(filter instanceof NumericComparisonOperator);
    NumericComparisonOperator compare = (NumericComparisonOperator) filter;
    assertEquals(CompareOp.GREATER_THAN, compare.getCompareOp());
    assertTrue(compare.getExpression1() instanceof Add);
    Add add = (Add) compare.getExpression1();
    assertTrue(add.getExpression1() instanceof NumericFieldValue);
    assertEquals("pop", ((NumericFieldValue) add.getExpression1()).getFieldName());
    assertTrue(add.getExpression2() instanceof NumericLiteral);
    assertEquals(5, ((NumericLiteral) add.getExpression2()).getValue(), 0.000001);
    assertTrue(compare.getExpression2() instanceof NumericLiteral);
    assertEquals(25, ((NumericLiteral) compare.getExpression2()).getValue(), 0.000001);
    statement = "SELECT * FROM type WHERE pop - 5 > 25";
    gwStatement = GWQLParser.parseStatement(dataStore, statement);
    assertTrue(gwStatement instanceof SelectStatement);
    selectStatement = (SelectStatement<?>) gwStatement;
    assertNotNull(selectStatement.getFilter());
    filter = selectStatement.getFilter();
    assertTrue(filter instanceof NumericComparisonOperator);
    compare = (NumericComparisonOperator) filter;
    assertEquals(CompareOp.GREATER_THAN, compare.getCompareOp());
    assertTrue(compare.getExpression1() instanceof Subtract);
    Subtract subtract = (Subtract) compare.getExpression1();
    assertTrue(subtract.getExpression1() instanceof NumericFieldValue);
    assertEquals("pop", ((NumericFieldValue) subtract.getExpression1()).getFieldName());
    assertTrue(subtract.getExpression2() instanceof NumericLiteral);
    assertEquals(5, ((NumericLiteral) subtract.getExpression2()).getValue(), 0.000001);
    assertTrue(compare.getExpression2() instanceof NumericLiteral);
    assertEquals(25, ((NumericLiteral) compare.getExpression2()).getValue(), 0.000001);
    statement = "SELECT * FROM type WHERE pop * 5 > 25";
    gwStatement = GWQLParser.parseStatement(dataStore, statement);
    assertTrue(gwStatement instanceof SelectStatement);
    selectStatement = (SelectStatement<?>) gwStatement;
    assertNotNull(selectStatement.getFilter());
    filter = selectStatement.getFilter();
    assertTrue(filter instanceof NumericComparisonOperator);
    compare = (NumericComparisonOperator) filter;
    assertEquals(CompareOp.GREATER_THAN, compare.getCompareOp());
    assertTrue(compare.getExpression1() instanceof Multiply);
    Multiply multiply = (Multiply) compare.getExpression1();
    assertTrue(multiply.getExpression1() instanceof NumericFieldValue);
    assertEquals("pop", ((NumericFieldValue) multiply.getExpression1()).getFieldName());
    assertTrue(multiply.getExpression2() instanceof NumericLiteral);
    assertEquals(5, ((NumericLiteral) multiply.getExpression2()).getValue(), 0.000001);
    assertTrue(compare.getExpression2() instanceof NumericLiteral);
    assertEquals(25, ((NumericLiteral) compare.getExpression2()).getValue(), 0.000001);
    statement = "SELECT * FROM type WHERE pop / 5 > 25";
    gwStatement = GWQLParser.parseStatement(dataStore, statement);
    assertTrue(gwStatement instanceof SelectStatement);
    selectStatement = (SelectStatement<?>) gwStatement;
    assertNotNull(selectStatement.getFilter());
    filter = selectStatement.getFilter();
    assertTrue(filter instanceof NumericComparisonOperator);
    compare = (NumericComparisonOperator) filter;
    assertEquals(CompareOp.GREATER_THAN, compare.getCompareOp());
    assertTrue(compare.getExpression1() instanceof Divide);
    Divide divide = (Divide) compare.getExpression1();
    assertTrue(divide.getExpression1() instanceof NumericFieldValue);
    assertEquals("pop", ((NumericFieldValue) divide.getExpression1()).getFieldName());
    assertTrue(divide.getExpression2() instanceof NumericLiteral);
    assertEquals(5, ((NumericLiteral) divide.getExpression2()).getValue(), 0.000001);
    assertTrue(compare.getExpression2() instanceof NumericLiteral);
    assertEquals(25, ((NumericLiteral) compare.getExpression2()).getValue(), 0.000001);
    // Test order of operations
    // (pop + ((5 * (pop - 8)) / 6))
    statement = "SELECT * FROM type WHERE pop + 5 * (pop - 8) / 6 > 25";
    gwStatement = GWQLParser.parseStatement(dataStore, statement);
    assertTrue(gwStatement instanceof SelectStatement);
    selectStatement = (SelectStatement<?>) gwStatement;
    assertNotNull(selectStatement.getFilter());
    filter = selectStatement.getFilter();
    assertTrue(filter instanceof NumericComparisonOperator);
    compare = (NumericComparisonOperator) filter;
    assertEquals(CompareOp.GREATER_THAN, compare.getCompareOp());
    assertTrue(compare.getExpression1() instanceof Add);
    add = (Add) compare.getExpression1();
    assertTrue(add.getExpression1() instanceof NumericFieldValue);
    assertEquals("pop", ((NumericFieldValue) add.getExpression1()).getFieldName());
    assertTrue(add.getExpression2() instanceof Divide);
    divide = (Divide) add.getExpression2();
    assertTrue(divide.getExpression1() instanceof Multiply);
    multiply = (Multiply) divide.getExpression1();
    assertTrue(multiply.getExpression1() instanceof NumericLiteral);
    assertEquals(5, ((NumericLiteral) multiply.getExpression1()).getValue(), 0.000001);
    assertTrue(multiply.getExpression2() instanceof Subtract);
    subtract = (Subtract) multiply.getExpression2();
    assertTrue(subtract.getExpression1() instanceof NumericFieldValue);
    assertEquals("pop", ((NumericFieldValue) subtract.getExpression1()).getFieldName());
    assertTrue(subtract.getExpression2() instanceof NumericLiteral);
    assertEquals(8, ((NumericLiteral) subtract.getExpression2()).getValue(), 0.000001);
    assertTrue(divide.getExpression2() instanceof NumericLiteral);
    assertEquals(6, ((NumericLiteral) divide.getExpression2()).getValue(), 0.000001);
    assertTrue(compare.getExpression2() instanceof NumericLiteral);
    assertEquals(25, ((NumericLiteral) compare.getExpression2()).getValue(), 0.000001);
}
Also used : Add(org.locationtech.geowave.core.store.query.filter.expression.numeric.Add) NumericLiteral(org.locationtech.geowave.core.store.query.filter.expression.numeric.NumericLiteral) NumericComparisonOperator(org.locationtech.geowave.core.store.query.filter.expression.numeric.NumericComparisonOperator) Statement(org.locationtech.geowave.core.store.query.gwql.statement.Statement) SelectStatement(org.locationtech.geowave.core.store.query.gwql.statement.SelectStatement) NumericFieldValue(org.locationtech.geowave.core.store.query.filter.expression.numeric.NumericFieldValue) SelectStatement(org.locationtech.geowave.core.store.query.gwql.statement.SelectStatement) Divide(org.locationtech.geowave.core.store.query.filter.expression.numeric.Divide) Filter(org.locationtech.geowave.core.store.query.filter.expression.Filter) DataStore(org.locationtech.geowave.core.store.api.DataStore) Multiply(org.locationtech.geowave.core.store.query.filter.expression.numeric.Multiply) Subtract(org.locationtech.geowave.core.store.query.filter.expression.numeric.Subtract) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)9 NumericLiteral (org.locationtech.geowave.core.store.query.filter.expression.numeric.NumericLiteral)9 NumericComparisonOperator (org.locationtech.geowave.core.store.query.filter.expression.numeric.NumericComparisonOperator)8 NumericFieldValue (org.locationtech.geowave.core.store.query.filter.expression.numeric.NumericFieldValue)8 Filter (org.locationtech.geowave.core.store.query.filter.expression.Filter)6 TextLiteral (org.locationtech.geowave.core.store.query.filter.expression.text.TextLiteral)6 TextFieldValue (org.locationtech.geowave.core.store.query.filter.expression.text.TextFieldValue)5 DataStore (org.locationtech.geowave.core.store.api.DataStore)4 And (org.locationtech.geowave.core.store.query.filter.expression.And)4 Statement (org.locationtech.geowave.core.store.query.gwql.statement.Statement)3 BBox (org.locationtech.geowave.core.geotime.store.query.filter.expression.spatial.BBox)2 SpatialFieldValue (org.locationtech.geowave.core.geotime.store.query.filter.expression.spatial.SpatialFieldValue)2 SpatialLiteral (org.locationtech.geowave.core.geotime.store.query.filter.expression.spatial.SpatialLiteral)2 UnpreparedFilterGeometry (org.locationtech.geowave.core.geotime.store.query.filter.expression.spatial.UnpreparedFilterGeometry)2 TestType (org.locationtech.geowave.core.store.adapter.AbstractDataTypeAdapterTest.TestType)2 TestTypeBasicDataAdapter (org.locationtech.geowave.core.store.adapter.AbstractDataTypeAdapterTest.TestTypeBasicDataAdapter)2 Or (org.locationtech.geowave.core.store.query.filter.expression.Or)2 Abs (org.locationtech.geowave.core.store.query.filter.expression.numeric.Abs)2 Add (org.locationtech.geowave.core.store.query.filter.expression.numeric.Add)2 Divide (org.locationtech.geowave.core.store.query.filter.expression.numeric.Divide)2