Search in sources :

Example 1 with TrueExpression

use of org.apache.carbondata.core.scan.expression.logical.TrueExpression in project carbondata by apache.

the class RangeExpressionEvaluator method replaceWithRangeExpression.

public void replaceWithRangeExpression(Map<String, List<FilterModificationNode>> filterExpressionMap) {
    List<FilterModificationNode> deleteExp = new ArrayList<>();
    for (String colName : filterExpressionMap.keySet()) {
        // Check is there are multiple list for this Column.
        List<FilterModificationNode> filterExp = filterExpressionMap.get(colName);
        if (filterExp.size() > 1) {
            // There are multiple Expression for the same column traverse and check if they can
            // form a range.
            FilterModificationNode startMin = null;
            FilterModificationNode endMax = null;
            for (FilterModificationNode exp : filterExp) {
                if ((exp.getExpType() == GREATERTHAN) || (exp.getExpType() == GREATERTHAN_EQUALTO)) {
                    if ((null == endMax) || ((null != endMax) && (checkLiteralValue(exp.getCurrentExp(), endMax.getCurrentExp())))) {
                        if (null == startMin) {
                            startMin = exp;
                        } else {
                            // There is already some value in startMin so check which one is greater.
                            LiteralExpression srcLiteral = getChildLiteralExpression(startMin.getCurrentExp());
                            LiteralExpression tarLiteral = getChildLiteralExpression(exp.getCurrentExp());
                            ExpressionResult srcExpResult = srcLiteral.evaluate(null);
                            ExpressionResult tarExpResult = tarLiteral.evaluate(null);
                            if (srcExpResult.compareTo(tarExpResult) < 0) {
                                // Before replacing the startMin add the current StartMin into deleteExp List
                                // as they will be replaced with TRUE expression after RANGE formation.
                                deleteExp.add(startMin);
                                startMin = exp;
                            }
                        }
                    }
                }
                if ((exp.getExpType() == LESSTHAN) || (exp.getExpType() == LESSTHAN_EQUALTO)) {
                    if ((null == startMin) || ((null != startMin) && (checkLiteralValue(exp.getCurrentExp(), startMin.getCurrentExp())))) {
                        if (null == endMax) {
                            endMax = exp;
                        } else {
                            // There is already some value in endMax so check which one is less.
                            LiteralExpression srcLiteral = getChildLiteralExpression(endMax.getCurrentExp());
                            LiteralExpression tarLiteral = getChildLiteralExpression(exp.getCurrentExp());
                            ExpressionResult srcExpResult = srcLiteral.evaluate(null);
                            ExpressionResult tarExpResult = tarLiteral.evaluate(null);
                            if (srcExpResult.compareTo(tarExpResult) > 0) {
                                // Before replacing the endMax add the current endMax into deleteExp List
                                // as they will be replaced with TRUE expression after RANGE formation.
                                deleteExp.add(endMax);
                                endMax = exp;
                            }
                        }
                    }
                }
            }
            if ((null != startMin) && (null != endMax)) {
                LOG.info("GreaterThan and LessThan Filter Expression changed to Range Expression for column " + colName);
                // the node can be converted to RANGE.
                Expression n1 = startMin.getCurrentExp();
                Expression n2 = endMax.getCurrentExp();
                RangeExpression rangeTree = new RangeExpression(n1, n2);
                Expression srcParentNode = startMin.getParentExp();
                Expression tarParentNode = endMax.getParentExp();
                srcParentNode.findAndSetChild(startMin.getCurrentExp(), rangeTree);
                tarParentNode.findAndSetChild(endMax.getCurrentExp(), new TrueExpression(null));
                if (deleteExp.size() > 0) {
                    // There are some expression to Delete as they are Redundant after Range Formation.
                    for (FilterModificationNode trueExp : deleteExp) {
                        trueExp.getParentExp().findAndSetChild(trueExp.getCurrentExp(), new TrueExpression(null));
                    }
                }
            }
        }
    }
}
Also used : TrueExpression(org.apache.carbondata.core.scan.expression.logical.TrueExpression) LessThanEqualToExpression(org.apache.carbondata.core.scan.expression.conditional.LessThanEqualToExpression) OrExpression(org.apache.carbondata.core.scan.expression.logical.OrExpression) TrueExpression(org.apache.carbondata.core.scan.expression.logical.TrueExpression) GreaterThanExpression(org.apache.carbondata.core.scan.expression.conditional.GreaterThanExpression) LessThanExpression(org.apache.carbondata.core.scan.expression.conditional.LessThanExpression) AndExpression(org.apache.carbondata.core.scan.expression.logical.AndExpression) GreaterThanEqualToExpression(org.apache.carbondata.core.scan.expression.conditional.GreaterThanEqualToExpression) RangeExpression(org.apache.carbondata.core.scan.expression.logical.RangeExpression) ArrayList(java.util.ArrayList) RangeExpression(org.apache.carbondata.core.scan.expression.logical.RangeExpression)

Example 2 with TrueExpression

use of org.apache.carbondata.core.scan.expression.logical.TrueExpression in project carbondata by apache.

the class RangeFilterProcessorTest method createFilterTree.

@Test
public void createFilterTree() {
    Expression inputFilter;
    boolean result = false;
    ColumnExpression cola = new ColumnExpression("a", DataType.STRING);
    cola.setDimension(true);
    ColumnSchema empColumnSchema = new ColumnSchema();
    empColumnSchema.setColumnName("empNameCol");
    empColumnSchema.setColumnUniqueId("empNameCol");
    empColumnSchema.setDimensionColumn(true);
    empColumnSchema.setEncodingList(Arrays.asList(Encoding.DICTIONARY));
    empColumnSchema.setDataType(DataType.STRING);
    CarbonDimension empDimension = new CarbonDimension(empColumnSchema, 0, 0, 0, 0, 0);
    cola.setDimension(empDimension);
    Expression greaterThan = new GreaterThanEqualToExpression(cola, new LiteralExpression("11", DataType.STRING));
    ColumnExpression colb = new ColumnExpression("a", DataType.STRING);
    colb.setDimension(true);
    colb.setDimension(empDimension);
    Expression lessThan = new LessThanEqualToExpression(colb, new LiteralExpression("20", DataType.STRING));
    inputFilter = new AndExpression(greaterThan, lessThan);
    Expression output = new AndExpression(new RangeExpression(new GreaterThanEqualToExpression(new ColumnExpression("a", DataType.STRING), new LiteralExpression("11", DataType.STRING)), new LessThanEqualToExpression(new ColumnExpression("a", DataType.STRING), new LiteralExpression("20", DataType.STRING))), new TrueExpression(null));
    FilterOptimizer rangeFilterOptimizer = new RangeFilterOptmizer(new FilterOptimizerBasic(), inputFilter);
    rangeFilterOptimizer.optimizeFilter();
    result = checkBothTrees(inputFilter, output);
    Assert.assertTrue(result);
}
Also used : FilterOptimizer(org.apache.carbondata.core.scan.filter.intf.FilterOptimizer) LiteralExpression(org.apache.carbondata.core.scan.expression.LiteralExpression) ColumnSchema(org.apache.carbondata.core.metadata.schema.table.column.ColumnSchema) RangeExpression(org.apache.carbondata.core.scan.expression.logical.RangeExpression) LessThanEqualToExpression(org.apache.carbondata.core.scan.expression.conditional.LessThanEqualToExpression) CarbonDimension(org.apache.carbondata.core.metadata.schema.table.column.CarbonDimension) GreaterThanEqualToExpression(org.apache.carbondata.core.scan.expression.conditional.GreaterThanEqualToExpression) TrueExpression(org.apache.carbondata.core.scan.expression.logical.TrueExpression) AndExpression(org.apache.carbondata.core.scan.expression.logical.AndExpression) LessThanEqualToExpression(org.apache.carbondata.core.scan.expression.conditional.LessThanEqualToExpression) ColumnExpression(org.apache.carbondata.core.scan.expression.ColumnExpression) GreaterThanExpression(org.apache.carbondata.core.scan.expression.conditional.GreaterThanExpression) AndExpression(org.apache.carbondata.core.scan.expression.logical.AndExpression) GreaterThanEqualToExpression(org.apache.carbondata.core.scan.expression.conditional.GreaterThanEqualToExpression) Expression(org.apache.carbondata.core.scan.expression.Expression) OrExpression(org.apache.carbondata.core.scan.expression.logical.OrExpression) TrueExpression(org.apache.carbondata.core.scan.expression.logical.TrueExpression) LiteralExpression(org.apache.carbondata.core.scan.expression.LiteralExpression) NotEqualsExpression(org.apache.carbondata.core.scan.expression.conditional.NotEqualsExpression) RangeExpression(org.apache.carbondata.core.scan.expression.logical.RangeExpression) ColumnExpression(org.apache.carbondata.core.scan.expression.ColumnExpression) RangeFilterOptmizer(org.apache.carbondata.core.scan.filter.optimizer.RangeFilterOptmizer) FilterOptimizerBasic(org.apache.carbondata.core.scan.filter.intf.FilterOptimizerBasic) Test(org.junit.Test)

Example 3 with TrueExpression

use of org.apache.carbondata.core.scan.expression.logical.TrueExpression in project carbondata by apache.

the class RangeFilterProcessorTest method createFilterTree_flavor1.

@Test
public void createFilterTree_flavor1() {
    // Build 3rd BTree a >= '11' and a > '12' and a <= '20' and a <= '15'
    Expression inputFilter;
    boolean result = false;
    ColumnSchema empColumnSchema = new ColumnSchema();
    empColumnSchema.setColumnName("a");
    empColumnSchema.setColumnUniqueId("a");
    empColumnSchema.setDimensionColumn(true);
    empColumnSchema.setEncodingList(Arrays.asList(Encoding.DICTIONARY));
    empColumnSchema.setDataType(DataType.STRING);
    CarbonDimension empDimension = new CarbonDimension(empColumnSchema, 0, 0, 0, 0, 0);
    ColumnExpression cola1 = new ColumnExpression("a", DataType.STRING);
    cola1.setDimension(true);
    cola1.setDimension(empDimension);
    ColumnExpression cola2 = new ColumnExpression("a", DataType.STRING);
    cola2.setDimension(true);
    cola2.setDimension(empDimension);
    ColumnExpression cola3 = new ColumnExpression("a", DataType.STRING);
    cola3.setDimension(true);
    cola3.setDimension(empDimension);
    ColumnExpression cola4 = new ColumnExpression("a", DataType.STRING);
    cola4.setDimension(true);
    cola4.setDimension(empDimension);
    Expression lessThan1 = new LessThanEqualToExpression(cola1, new LiteralExpression("15", DataType.STRING));
    Expression lessThan2 = new LessThanEqualToExpression(cola2, new LiteralExpression("20", DataType.STRING));
    Expression greaterThan1 = new GreaterThanExpression(cola3, new LiteralExpression("12", DataType.STRING));
    Expression greaterThan2 = new GreaterThanEqualToExpression(cola4, new LiteralExpression("11", DataType.STRING));
    Expression And1 = new AndExpression(new NotEqualsExpression(null, null), greaterThan2);
    Expression And2 = new AndExpression(And1, greaterThan1);
    Expression And3 = new AndExpression(And2, lessThan2);
    inputFilter = new AndExpression(And3, lessThan1);
    // Build The output
    ColumnExpression colb1 = new ColumnExpression("a", DataType.STRING);
    cola1.setDimension(true);
    cola1.setDimension(empDimension);
    ColumnExpression colb2 = new ColumnExpression("a", DataType.STRING);
    cola2.setDimension(true);
    cola2.setDimension(empDimension);
    Expression greaterThanb1 = new GreaterThanExpression(cola3, new LiteralExpression("12", DataType.STRING));
    Expression lessThanb1 = new LessThanEqualToExpression(cola1, new LiteralExpression("15", DataType.STRING));
    Expression Andb1 = new AndExpression(new NotEqualsExpression(null, null), new TrueExpression(null));
    Expression Andb2 = new AndExpression(Andb1, new RangeExpression(greaterThanb1, lessThanb1));
    Expression Andb3 = new AndExpression(Andb2, new TrueExpression(null));
    FilterOptimizer rangeFilterOptimizer = new RangeFilterOptmizer(new FilterOptimizerBasic(), inputFilter);
    rangeFilterOptimizer.optimizeFilter();
    result = checkBothTrees(inputFilter, new AndExpression(Andb3, new TrueExpression(null)));
    // no change
    Assert.assertTrue(result);
}
Also used : FilterOptimizer(org.apache.carbondata.core.scan.filter.intf.FilterOptimizer) NotEqualsExpression(org.apache.carbondata.core.scan.expression.conditional.NotEqualsExpression) LiteralExpression(org.apache.carbondata.core.scan.expression.LiteralExpression) ColumnSchema(org.apache.carbondata.core.metadata.schema.table.column.ColumnSchema) GreaterThanExpression(org.apache.carbondata.core.scan.expression.conditional.GreaterThanExpression) RangeExpression(org.apache.carbondata.core.scan.expression.logical.RangeExpression) LessThanEqualToExpression(org.apache.carbondata.core.scan.expression.conditional.LessThanEqualToExpression) CarbonDimension(org.apache.carbondata.core.metadata.schema.table.column.CarbonDimension) GreaterThanEqualToExpression(org.apache.carbondata.core.scan.expression.conditional.GreaterThanEqualToExpression) TrueExpression(org.apache.carbondata.core.scan.expression.logical.TrueExpression) AndExpression(org.apache.carbondata.core.scan.expression.logical.AndExpression) LessThanEqualToExpression(org.apache.carbondata.core.scan.expression.conditional.LessThanEqualToExpression) ColumnExpression(org.apache.carbondata.core.scan.expression.ColumnExpression) GreaterThanExpression(org.apache.carbondata.core.scan.expression.conditional.GreaterThanExpression) AndExpression(org.apache.carbondata.core.scan.expression.logical.AndExpression) GreaterThanEqualToExpression(org.apache.carbondata.core.scan.expression.conditional.GreaterThanEqualToExpression) Expression(org.apache.carbondata.core.scan.expression.Expression) OrExpression(org.apache.carbondata.core.scan.expression.logical.OrExpression) TrueExpression(org.apache.carbondata.core.scan.expression.logical.TrueExpression) LiteralExpression(org.apache.carbondata.core.scan.expression.LiteralExpression) NotEqualsExpression(org.apache.carbondata.core.scan.expression.conditional.NotEqualsExpression) RangeExpression(org.apache.carbondata.core.scan.expression.logical.RangeExpression) ColumnExpression(org.apache.carbondata.core.scan.expression.ColumnExpression) RangeFilterOptmizer(org.apache.carbondata.core.scan.filter.optimizer.RangeFilterOptmizer) FilterOptimizerBasic(org.apache.carbondata.core.scan.filter.intf.FilterOptimizerBasic) Test(org.junit.Test)

Aggregations

GreaterThanEqualToExpression (org.apache.carbondata.core.scan.expression.conditional.GreaterThanEqualToExpression)3 GreaterThanExpression (org.apache.carbondata.core.scan.expression.conditional.GreaterThanExpression)3 LessThanEqualToExpression (org.apache.carbondata.core.scan.expression.conditional.LessThanEqualToExpression)3 AndExpression (org.apache.carbondata.core.scan.expression.logical.AndExpression)3 OrExpression (org.apache.carbondata.core.scan.expression.logical.OrExpression)3 RangeExpression (org.apache.carbondata.core.scan.expression.logical.RangeExpression)3 TrueExpression (org.apache.carbondata.core.scan.expression.logical.TrueExpression)3 CarbonDimension (org.apache.carbondata.core.metadata.schema.table.column.CarbonDimension)2 ColumnSchema (org.apache.carbondata.core.metadata.schema.table.column.ColumnSchema)2 ColumnExpression (org.apache.carbondata.core.scan.expression.ColumnExpression)2 Expression (org.apache.carbondata.core.scan.expression.Expression)2 LiteralExpression (org.apache.carbondata.core.scan.expression.LiteralExpression)2 NotEqualsExpression (org.apache.carbondata.core.scan.expression.conditional.NotEqualsExpression)2 FilterOptimizer (org.apache.carbondata.core.scan.filter.intf.FilterOptimizer)2 FilterOptimizerBasic (org.apache.carbondata.core.scan.filter.intf.FilterOptimizerBasic)2 RangeFilterOptmizer (org.apache.carbondata.core.scan.filter.optimizer.RangeFilterOptmizer)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 LessThanExpression (org.apache.carbondata.core.scan.expression.conditional.LessThanExpression)1