-
Notifications
You must be signed in to change notification settings - Fork 702
[CARBONDATA-4138] Reordering Carbon Expression instead of Spark Filter #4100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.carbondata.core.scan.expression.optimize; | ||
|
|
||
| import org.apache.carbondata.core.metadata.schema.table.CarbonTable; | ||
| import org.apache.carbondata.core.scan.expression.Expression; | ||
| import org.apache.carbondata.core.scan.expression.optimize.reorder.ExpressionReorder; | ||
| import org.apache.carbondata.core.util.CarbonProperties; | ||
|
|
||
| /** | ||
| * optimize Carbon Expression | ||
| */ | ||
| public class ExpressionOptimizer { | ||
|
|
||
| private final OptimizeRule[] rules = { new ExpressionReorder() }; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need for private static class, make this final and use directly |
||
|
|
||
| public static Expression optimize(CarbonTable table, Expression expression) { | ||
| if (!CarbonProperties.isFilterOptimizeEnabled()) { | ||
| return expression; | ||
| } | ||
| for (OptimizeRule rule : ExpressionOptimizerHandler.INSTANCE.rules) { | ||
| expression = rule.optimize(table, expression); | ||
| } | ||
| return expression; | ||
| } | ||
|
|
||
| private static class ExpressionOptimizerHandler { | ||
| private static final ExpressionOptimizer INSTANCE = new ExpressionOptimizer(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.carbondata.core.scan.expression.optimize; | ||
|
|
||
| import org.apache.carbondata.core.metadata.schema.table.CarbonTable; | ||
| import org.apache.carbondata.core.scan.expression.Expression; | ||
|
|
||
| /** | ||
| * the base rule of ExpressionOptimizer | ||
| */ | ||
| public abstract class OptimizeRule { | ||
|
|
||
| public abstract Expression optimize(CarbonTable table, Expression expression); | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.carbondata.core.scan.expression.optimize.reorder; | ||
|
|
||
| import org.apache.carbondata.core.scan.expression.Expression; | ||
| import org.apache.carbondata.core.scan.expression.logical.AndExpression; | ||
|
|
||
| /** | ||
| * new And expression with multiple children (maybe more than two children). | ||
| */ | ||
| public class AndMultiExpression extends MultiExpression { | ||
|
|
||
| @Override | ||
| public boolean canMerge(Expression child) { | ||
| return child instanceof AndExpression; | ||
| } | ||
|
|
||
| @Override | ||
| public Expression toExpression() { | ||
| return children.stream() | ||
| .map(StorageOrdinal::toExpression) | ||
| .reduce(AndExpression::new) | ||
| .orElse(null); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.carbondata.core.scan.expression.optimize.reorder; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import org.apache.carbondata.core.metadata.schema.table.CarbonTable; | ||
| import org.apache.carbondata.core.metadata.schema.table.column.CarbonColumn; | ||
| import org.apache.carbondata.core.scan.expression.Expression; | ||
| import org.apache.carbondata.core.scan.expression.optimize.OptimizeRule; | ||
| import org.apache.carbondata.core.util.CarbonProperties; | ||
|
|
||
| /** | ||
| * reorder Expression by storage order | ||
| */ | ||
| public class ExpressionReorder extends OptimizeRule { | ||
|
|
||
| @Override | ||
| public Expression optimize(CarbonTable table, Expression expression) { | ||
| if (!CarbonProperties.isFilterReorderingEnabled()) { | ||
| return expression; | ||
| } | ||
| MultiExpression multiExpression = MultiExpression.build(expression); | ||
| // unsupported expression | ||
| if (multiExpression == null) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move this check above MultiExpression.build so that we dont enter the reorder code if null
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MultiExpression.build return "multiExpression " |
||
| return expression; | ||
| } | ||
| // remove redundancy filter | ||
| multiExpression.removeRedundant(); | ||
| // combine multiple filters to single filter | ||
| multiExpression.combine(); | ||
| // reorder Expression by storage ordinal of columns | ||
| multiExpression.updateMinOrdinal(columnMapOrdinal(table)); | ||
| multiExpression.sortChildrenByOrdinal(); | ||
| return multiExpression.toExpression(); | ||
| } | ||
|
|
||
| private Map<String, Integer> columnMapOrdinal(CarbonTable table) { | ||
| List<CarbonColumn> createOrderColumns = table.getCreateOrderColumn(); | ||
| Map<String, Integer> nameMapOrdinal = new HashMap<>(createOrderColumns.size()); | ||
| int dimensionCount = table.getAllDimensions().size(); | ||
| for (CarbonColumn column : createOrderColumns) { | ||
| if (column.isDimension()) { | ||
| nameMapOrdinal.put(column.getColName(), column.getOrdinal()); | ||
| } else { | ||
| nameMapOrdinal.put(column.getColName(), dimensionCount + column.getOrdinal()); | ||
| } | ||
| } | ||
| return nameMapOrdinal; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.carbondata.core.scan.expression.optimize.reorder; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import org.apache.carbondata.core.scan.expression.ColumnExpression; | ||
| import org.apache.carbondata.core.scan.expression.Expression; | ||
| import org.apache.carbondata.core.scan.expression.UnknownExpression; | ||
| import org.apache.carbondata.core.scan.expression.conditional.ConditionalExpression; | ||
|
|
||
| /** | ||
| * a wrapper class of Expression with storage ordinal | ||
| */ | ||
| public class ExpressionWithOrdinal extends StorageOrdinal { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all classes inside optimize package except ExpressionReorder should have default access modifier so that they are not used for creating expressions by mistake |
||
| protected Expression expression; | ||
|
|
||
| public ExpressionWithOrdinal(Expression expression) { | ||
| this.minOrdinal = Integer.MAX_VALUE; | ||
| this.expression = expression; | ||
| } | ||
|
|
||
| @Override | ||
| public void updateMinOrdinal(Map<String, Integer> columnMapOrdinal) { | ||
| updateMinOrdinal(expression, columnMapOrdinal); | ||
| } | ||
|
|
||
| private void updateMinOrdinal(Expression expression, Map<String, Integer> nameMapOrdinal) { | ||
| if (expression != null && expression.getChildren() != null) { | ||
| if (expression.getChildren().size() == 0) { | ||
| if (expression instanceof ConditionalExpression) { | ||
| List<ColumnExpression> columnList = | ||
| ((ConditionalExpression) expression).getColumnList(); | ||
| for (ColumnExpression columnExpression : columnList) { | ||
| updateMinOrdinal(columnExpression.getColumnName(), nameMapOrdinal); | ||
| } | ||
| } | ||
| } else { | ||
| for (Expression subExpression : expression.getChildren()) { | ||
| if (subExpression instanceof ColumnExpression) { | ||
| updateMinOrdinal(((ColumnExpression) subExpression).getColumnName(), nameMapOrdinal); | ||
| } else if (expression instanceof UnknownExpression) { | ||
| UnknownExpression exp = ((UnknownExpression) expression); | ||
| List<ColumnExpression> listOfColExpression = exp.getAllColumnList(); | ||
| for (ColumnExpression columnExpression : listOfColExpression) { | ||
| updateMinOrdinal(columnExpression.getColumnName(), nameMapOrdinal); | ||
| } | ||
| } else { | ||
| updateMinOrdinal(subExpression, nameMapOrdinal); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void updateMinOrdinal(String columnName, Map<String, Integer> nameMapOrdinal) { | ||
| Integer ordinal = nameMapOrdinal.get(columnName.toLowerCase()); | ||
| if (ordinal != null && ordinal < minOrdinal) { | ||
| minOrdinal = ordinal; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Expression toExpression() { | ||
| return expression; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"carbon.reorder.filter" and "carbon.optimize.filter" seem to be doing the same thing. please remove one of them
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reorder is one part of optimaztion