|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.carbondata.core.scan.expression.optimize; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +import org.apache.carbondata.core.metadata.schema.table.CarbonTable; |
| 26 | +import org.apache.carbondata.core.metadata.schema.table.column.CarbonColumn; |
| 27 | +import org.apache.carbondata.core.scan.expression.Expression; |
| 28 | +import org.apache.carbondata.core.scan.expression.logical.AndExpression; |
| 29 | +import org.apache.carbondata.core.scan.expression.logical.OrExpression; |
| 30 | + |
| 31 | +public abstract class MultiExpression extends StorageOrdinal { |
| 32 | + |
| 33 | + public MultiExpression() { |
| 34 | + this.minOrdinal = Integer.MAX_VALUE; |
| 35 | + } |
| 36 | + |
| 37 | + protected List<StorageOrdinal> children = new ArrayList<>(); |
| 38 | + |
| 39 | + public static MultiExpression build(Expression expression) { |
| 40 | + MultiExpression multiExpression = null; |
| 41 | + if (expression instanceof AndExpression) { |
| 42 | + multiExpression = new AndMultiExpression(); |
| 43 | + } |
| 44 | + if (expression instanceof OrExpression) { |
| 45 | + multiExpression = new OrMultiExpression(); |
| 46 | + } |
| 47 | + if (multiExpression == null) { |
| 48 | + return null; |
| 49 | + } |
| 50 | + for (Expression child : expression.getChildren()) { |
| 51 | + buildChild(child, multiExpression); |
| 52 | + } |
| 53 | + return multiExpression; |
| 54 | + } |
| 55 | + |
| 56 | + private static void buildChild(Expression expression, MultiExpression parent) { |
| 57 | + if (parent.canMerge(expression)) { |
| 58 | + // multiple and(or) can be merge into same MultiExpression |
| 59 | + for (Expression child : expression.getChildren()) { |
| 60 | + buildChild(child, parent); |
| 61 | + } |
| 62 | + } else { |
| 63 | + MultiExpression multiExpression = build(expression); |
| 64 | + if (multiExpression == null) { |
| 65 | + // it is not and/or expression |
| 66 | + parent.addChild(expression); |
| 67 | + } else { |
| 68 | + // it is and, or expression |
| 69 | + parent.addChild(multiExpression); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public abstract boolean canMerge(Expression child); |
| 75 | + |
| 76 | + private void addChild(Expression child) { |
| 77 | + addChild(new ExpressionWithOrdinal(child)); |
| 78 | + } |
| 79 | + |
| 80 | + private void addChild(StorageOrdinal storageOrdinal) { |
| 81 | + children.add(storageOrdinal); |
| 82 | + } |
| 83 | + |
| 84 | + private Map<String, Integer> columnMapOrdinal(CarbonTable table) { |
| 85 | + List<CarbonColumn> createOrderColumns = table.getCreateOrderColumn(); |
| 86 | + Map<String, Integer> nameMapOrdinal = new HashMap<>(createOrderColumns.size()); |
| 87 | + int dimensionCount = table.getAllDimensions().size(); |
| 88 | + for (CarbonColumn column : createOrderColumns) { |
| 89 | + if (column.isDimension()) { |
| 90 | + nameMapOrdinal.put(column.getColName(), column.getOrdinal()); |
| 91 | + } else { |
| 92 | + nameMapOrdinal.put(column.getColName(), dimensionCount + column.getOrdinal()); |
| 93 | + } |
| 94 | + } |
| 95 | + return nameMapOrdinal; |
| 96 | + } |
| 97 | + |
| 98 | + public void removeRedundant() { |
| 99 | + // TODO remove redundancy filter if exists |
| 100 | + } |
| 101 | + |
| 102 | + public void combine() { |
| 103 | + // TODO combine multiple filters to single filter if needed |
| 104 | + } |
| 105 | + |
| 106 | + public void reorder(CarbonTable table) { |
| 107 | + updateMinOrdinal(columnMapOrdinal(table)); |
| 108 | + sortChildrenByOrdinal(); |
| 109 | + } |
| 110 | + |
| 111 | + public void sortChildrenByOrdinal() { |
| 112 | + children.sort(null); |
| 113 | + for (StorageOrdinal child : children) { |
| 114 | + if (child instanceof MultiExpression) { |
| 115 | + ((MultiExpression) child).sortChildrenByOrdinal(); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void updateMinOrdinal(Map<String, Integer> columnMapOrdinal) { |
| 122 | + for (StorageOrdinal child : children) { |
| 123 | + child.updateMinOrdinal(columnMapOrdinal); |
| 124 | + if (child.minOrdinal < this.minOrdinal) { |
| 125 | + this.minOrdinal = child.minOrdinal; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + public abstract Expression toBinaryExpression(); |
| 131 | + |
| 132 | +} |
0 commit comments