Search in sources :

Example 1 with CastExpression

use of org.apache.drill.common.expression.CastExpression in project drill by apache.

the class MapRDBFunctionalIndexInfo method init.

private void init() {
    int count = 0;
    for (LogicalExpression indexedExpr : indexDesc.getIndexColumns()) {
        if (!(indexedExpr instanceof SchemaPath)) {
            hasFunctionalField = true;
            SchemaPath functionalFieldPath = SchemaPath.getSimplePath("$" + count);
            newPathsForIndexedFunction.add(functionalFieldPath);
            // now we handle only cast expression
            if (indexedExpr instanceof CastExpression) {
                // We handle only CAST directly on SchemaPath for now.
                SchemaPath pathBeingCasted = (SchemaPath) ((CastExpression) indexedExpr).getInput();
                addTargetPathForOriginalPath(pathBeingCasted, functionalFieldPath);
                addPathInExpr(indexedExpr, pathBeingCasted);
                exprToConvert.put(indexedExpr, functionalFieldPath);
                allPathsInFunction.add(pathBeingCasted);
            }
            count++;
        }
    }
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) SchemaPath(org.apache.drill.common.expression.SchemaPath) CastExpression(org.apache.drill.common.expression.CastExpression)

Example 2 with CastExpression

use of org.apache.drill.common.expression.CastExpression in project drill by apache.

the class FunctionalIndexHelper method convertConditionForIndexScan.

public static RexNode convertConditionForIndexScan(RexNode idxCondition, RelNode origScan, RelDataType idxRowType, RexBuilder builder, FunctionalIndexInfo functionInfo) {
    IndexableExprMarker marker = new IndexableExprMarker(origScan);
    idxCondition.accept(marker);
    SimpleRexRemap remap = new SimpleRexRemap(origScan, idxRowType, builder);
    remap.setExpressionMap(functionInfo.getExprMap());
    if (functionInfo.supportEqualCharConvertToLike()) {
        final Map<LogicalExpression, LogicalExpression> indexedExprs = functionInfo.getExprMap();
        final Map<RexNode, LogicalExpression> equalCastMap = marker.getEqualOnCastChar();
        Map<RexNode, LogicalExpression> toRewriteEqualCastMap = Maps.newHashMap();
        // the marker collected all equal-cast-varchar, now check which one we should replace
        for (Map.Entry<RexNode, LogicalExpression> entry : equalCastMap.entrySet()) {
            CastExpression expr = (CastExpression) entry.getValue();
            // whether this cast varchar/char expression is indexed even the length is not the same
            for (LogicalExpression indexed : indexedExprs.keySet()) {
                if (indexed instanceof CastExpression) {
                    final CastExpression indexedCast = (CastExpression) indexed;
                    if (expr.getInput().equals(indexedCast.getInput()) && expr.getMajorType().getMinorType().equals(indexedCast.getMajorType().getMinorType()) && // if expr's length < indexedCast's length, we should convert equal to LIKE for this condition
                    expr.getMajorType().getPrecision() < indexedCast.getMajorType().getPrecision()) {
                        toRewriteEqualCastMap.put(entry.getKey(), entry.getValue());
                    }
                }
            }
        }
        if (toRewriteEqualCastMap.size() > 0) {
            idxCondition = remap.rewriteEqualOnCharToLike(idxCondition, toRewriteEqualCastMap);
        }
    }
    return remap.rewriteWithMap(idxCondition, marker.getIndexableExpression());
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) CastExpression(org.apache.drill.common.expression.CastExpression) Map(java.util.Map) RexNode(org.apache.calcite.rex.RexNode)

Example 3 with CastExpression

use of org.apache.drill.common.expression.CastExpression in project drill by apache.

the class RexSeparator method getSeparatedCondition.

public RexNode getSeparatedCondition(RexNode expr) {
    IndexableExprMarker marker = new IndexableExprMarker(inputRel);
    expr.accept(marker);
    final Map<RexNode, LogicalExpression> markMap = Maps.newHashMap();
    final Map<RexNode, LogicalExpression> relevantRexMap = marker.getIndexableExpression();
    for (Map.Entry<RexNode, LogicalExpression> entry : relevantRexMap.entrySet()) {
        // for the expressions found in expr, only these in relatedPaths is related
        LogicalExpression relevantExpr = entry.getValue();
        int idxFound = relatedPaths.indexOf(relevantExpr);
        if (idxFound >= 0) {
            if (relevantExpr instanceof SchemaPath) {
                // case sensitive comparison
                if (!((SchemaPath) relevantExpr).toExpr().equals(((SchemaPath) relatedPaths.get(idxFound)).toExpr())) {
                    continue;
                }
            } else if (relevantExpr instanceof CastExpression) {
                final CastExpression castExprInFilter = (CastExpression) relevantExpr;
                if (castExprInFilter.getMajorType().getMinorType() == TypeProtos.MinorType.VARCHAR && (castExprInFilter.getMajorType().getPrecision() > relatedPaths.get(idxFound).getMajorType().getPrecision())) {
                    continue;
                }
            }
            markMap.put(entry.getKey(), entry.getValue());
        }
    }
    ConditionSeparator separator = new ConditionSeparator(markMap, builder);
    separator.analyze(expr);
    return separator.getFinalCondition();
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) SchemaPath(org.apache.drill.common.expression.SchemaPath) CastExpression(org.apache.drill.common.expression.CastExpression) Map(java.util.Map) RexNode(org.apache.calcite.rex.RexNode)

Example 4 with CastExpression

use of org.apache.drill.common.expression.CastExpression in project drill by apache.

the class FunctionalIndexHelper method convertRowTypeForIndexScan.

/**
 * For IndexScan in non-covering case, rowType to return contains only row_key('_id') of primary table.
 * so the rowType for IndexScan should be converted from [Primary_table.row_key, primary_table.indexed_col]
 * to [indexTable.row_key(primary_table.indexed_col), indexTable.<primary_key.row_key> (Primary_table.row_key)]
 * This will impact the columns of scan, the rowType of ScanRel
 *
 * @param origScan
 * @param idxMarker  the IndexableExprMarker that has analyzed original index condition on top of index scan
 * @param idxScan
 * @return
 */
public static RelDataType convertRowTypeForIndexScan(DrillScanRelBase origScan, IndexableExprMarker idxMarker, IndexGroupScan idxScan, FunctionalIndexInfo functionInfo) {
    RelDataTypeFactory typeFactory = origScan.getCluster().getTypeFactory();
    List<RelDataTypeField> fields = new ArrayList<>();
    Set<SchemaPath> rowPaths = new LinkedHashSet<>();
    // row_key in the rowType of scan on primary table
    RelDataTypeField rowkey_primary;
    RelRecordType newRowType = null;
    DbGroupScan scan = (DbGroupScan) IndexPlanUtils.getGroupScan(origScan);
    // first add row_key of primary table,
    rowkey_primary = new RelDataTypeFieldImpl(scan.getRowKeyName(), fields.size(), typeFactory.createSqlType(SqlTypeName.ANY));
    fields.add(rowkey_primary);
    Map<RexNode, LogicalExpression> idxExprMap = idxMarker.getIndexableExpression();
    for (LogicalExpression indexedExpr : idxExprMap.values()) {
        if (indexedExpr instanceof SchemaPath) {
            rowPaths.add((SchemaPath) indexedExpr);
        } else if (indexedExpr instanceof CastExpression) {
            SchemaPath newPath = functionInfo.getNewPathFromExpr(indexedExpr);
            if (newPath != null) {
                rowPaths.add(newPath);
            }
        }
    }
    for (SchemaPath newPath : rowPaths) {
        fields.add(new RelDataTypeFieldImpl(newPath.getRootSegmentPath(), fields.size(), typeFactory.createSqlType(SqlTypeName.ANY)));
    }
    // update columns of groupscan accordingly
    Set<RelDataTypeField> rowfields = Sets.newLinkedHashSet();
    final List<SchemaPath> columns = Lists.newArrayList();
    for (RelDataTypeField f : fields) {
        SchemaPath path = SchemaPath.parseFromString(f.getName());
        rowfields.add(new RelDataTypeFieldImpl(path.getRootSegmentPath(), rowfields.size(), typeFactory.createSqlType(SqlTypeName.ANY)));
        columns.add(path);
    }
    idxScan.setColumns(columns);
    // rowtype does not take the whole path, but only the rootSegment of the SchemaPath
    newRowType = new RelRecordType(Lists.newArrayList(rowfields));
    return newRowType;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) RelRecordType(org.apache.calcite.rel.type.RelRecordType) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) SchemaPath(org.apache.drill.common.expression.SchemaPath) DbGroupScan(org.apache.drill.exec.physical.base.DbGroupScan) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RelDataTypeFieldImpl(org.apache.calcite.rel.type.RelDataTypeFieldImpl) CastExpression(org.apache.drill.common.expression.CastExpression) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

CastExpression (org.apache.drill.common.expression.CastExpression)4 LogicalExpression (org.apache.drill.common.expression.LogicalExpression)4 RexNode (org.apache.calcite.rex.RexNode)3 SchemaPath (org.apache.drill.common.expression.SchemaPath)3 Map (java.util.Map)2 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)1 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)1 RelDataTypeFieldImpl (org.apache.calcite.rel.type.RelDataTypeFieldImpl)1 RelRecordType (org.apache.calcite.rel.type.RelRecordType)1 DbGroupScan (org.apache.drill.exec.physical.base.DbGroupScan)1