Search in sources :

Example 21 with RexFieldAccess

use of org.apache.calcite.rex.RexFieldAccess in project calcite by apache.

the class RelFieldTrimmer method result.

protected TrimResult result(RelNode r, final Mapping mapping) {
    final RexBuilder rexBuilder = relBuilder.getRexBuilder();
    for (final CorrelationId correlation : r.getVariablesSet()) {
        r = r.accept(new CorrelationReferenceFinder() {

            protected RexNode handle(RexFieldAccess fieldAccess) {
                final RexCorrelVariable v = (RexCorrelVariable) fieldAccess.getReferenceExpr();
                if (v.id.equals(correlation) && v.getType().getFieldCount() == mapping.getSourceCount()) {
                    final int old = fieldAccess.getField().getIndex();
                    final int new_ = mapping.getTarget(old);
                    final RelDataTypeFactory.Builder typeBuilder = relBuilder.getTypeFactory().builder();
                    for (int target : Util.range(mapping.getTargetCount())) {
                        typeBuilder.add(v.getType().getFieldList().get(mapping.getSource(target)));
                    }
                    final RexNode newV = rexBuilder.makeCorrel(typeBuilder.build(), v.id);
                    if (old != new_) {
                        return rexBuilder.makeFieldAccess(newV, new_);
                    }
                }
                return fieldAccess;
            }
        });
    }
    return new TrimResult(r, mapping);
}
Also used : RexCorrelVariable(org.apache.calcite.rex.RexCorrelVariable) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RexBuilder(org.apache.calcite.rex.RexBuilder) CorrelationId(org.apache.calcite.rel.core.CorrelationId) RexFieldAccess(org.apache.calcite.rex.RexFieldAccess) RexNode(org.apache.calcite.rex.RexNode)

Example 22 with RexFieldAccess

use of org.apache.calcite.rex.RexFieldAccess in project calcite by apache.

the class RelDecorrelator method checkCorVars.

/**
 * Checks whether the correlations in projRel and filter are related to
 * the correlated variables provided by corRel.
 *
 * @param correlate    Correlate
 * @param project   The original Project as the RHS input of the join
 * @param filter    Filter
 * @param correlatedJoinKeys Correlated join keys
 * @return true if filter and proj only references corVar provided by corRel
 */
private boolean checkCorVars(LogicalCorrelate correlate, LogicalProject project, LogicalFilter filter, List<RexFieldAccess> correlatedJoinKeys) {
    if (filter != null) {
        assert correlatedJoinKeys != null;
        // check that all correlated refs in the filter condition are
        // used in the join(as field access).
        Set<CorRef> corVarInFilter = Sets.newHashSet(cm.mapRefRelToCorRef.get(filter));
        for (RexFieldAccess correlatedJoinKey : correlatedJoinKeys) {
            corVarInFilter.remove(cm.mapFieldAccessToCorRef.get(correlatedJoinKey));
        }
        if (!corVarInFilter.isEmpty()) {
            return false;
        }
        // Check that the correlated variables referenced in these
        // comparisons do come from the Correlate.
        corVarInFilter.addAll(cm.mapRefRelToCorRef.get(filter));
        for (CorRef corVar : corVarInFilter) {
            if (cm.mapCorToCorRel.get(corVar.corr) != correlate) {
                return false;
            }
        }
    }
    // of the correlate.
    if ((project != null) && cm.mapRefRelToCorRef.containsKey(project)) {
        for (CorRef corVar : cm.mapRefRelToCorRef.get(project)) {
            if (cm.mapCorToCorRel.get(corVar.corr) != correlate) {
                return false;
            }
        }
    }
    return true;
}
Also used : RexFieldAccess(org.apache.calcite.rex.RexFieldAccess)

Example 23 with RexFieldAccess

use of org.apache.calcite.rex.RexFieldAccess in project herddb by diennea.

the class SQLExpressionCompiler method compileExpression.

public static CompiledSQLExpression compileExpression(RexNode expression) {
    if (expression == null) {
        return null;
    }
    if (expression instanceof RexDynamicParam) {
        RexDynamicParam p = (RexDynamicParam) expression;
        return new TypedJdbcParameterExpression(p.getIndex(), CalcitePlanner.convertToHerdType(p.getType()));
    } else if (expression instanceof RexLiteral) {
        RexLiteral p = (RexLiteral) expression;
        if (p.isNull()) {
            return new ConstantExpression(null, ColumnTypes.NULL);
        } else {
            return new ConstantExpression(safeValue(p.getValue3(), p.getType(), p.getTypeName()), CalcitePlanner.convertToHerdType(p.getType()));
        }
    } else if (expression instanceof RexInputRef) {
        RexInputRef p = (RexInputRef) expression;
        return new AccessCurrentRowExpression(p.getIndex(), CalcitePlanner.convertToHerdType(p.getType()));
    } else if (expression instanceof RexCall) {
        RexCall p = (RexCall) expression;
        SqlOperator op = p.op;
        String name = op.getName();
        CompiledSQLExpression[] operands = new CompiledSQLExpression[p.operands.size()];
        int i = 0;
        for (RexNode operand : p.operands) {
            operands[i++] = compileExpression(operand);
        }
        switch(name) {
            case "=":
                return new CompiledEqualsExpression(operands[0], operands[1]);
            case "<>":
                return new CompiledNotEqualsExpression(operands[0], operands[1]);
            case ">":
                return new CompiledGreaterThanExpression(operands[0], operands[1]);
            case ">=":
                return new CompiledGreaterThanEqualsExpression(operands[0], operands[1]);
            case "<":
                return new CompiledMinorThanExpression(operands[0], operands[1]);
            case "<=":
                return new CompiledMinorThanEqualsExpression(operands[0], operands[1]);
            case "+":
                return new CompiledAddExpression(operands[0], operands[1]);
            case "MOD":
                return new CompiledModuloExpression(operands[0], operands[1]);
            case "-":
                if (operands.length == 1) {
                    return new CompiledSignedExpression('-', operands[0]);
                } else if (operands.length == 2) {
                    return new CompiledSubtractExpression(operands[0], operands[1]);
                }
                break;
            case "*":
                return new CompiledMultiplyExpression(operands[0], operands[1]);
            case "/":
                return new CompiledDivideExpression(operands[0], operands[1]);
            case "/INT":
                return new CompiledDivideIntExpression(operands[0], operands[1]);
            case "LIKE":
                if (operands.length == 2) {
                    return new CompiledLikeExpression(operands[0], operands[1]);
                } else {
                    // ESCAPE
                    return new CompiledLikeExpression(operands[0], operands[1], operands[2]);
                }
            case "AND":
                return new CompiledMultiAndExpression(operands);
            case "OR":
                return new CompiledMultiOrExpression(operands);
            case "NOT":
                return new CompiledParenthesisExpression(true, operands[0]);
            case "IS NOT NULL":
                return new CompiledIsNullExpression(true, operands[0]);
            case "IS NOT TRUE":
                return new CompiledIsNotTrueExpression(false, operands[0]);
            case "IS NULL":
                return new CompiledIsNullExpression(false, operands[0]);
            case "SEARCH":
                return convertSearchOperator(p);
            case "CAST":
                return operands[0].cast(CalcitePlanner.convertToHerdType(p.type));
            case "CASE":
                List<Map.Entry<CompiledSQLExpression, CompiledSQLExpression>> cases = new ArrayList<>(operands.length / 2);
                boolean hasElse = operands.length % 2 == 1;
                int numcases = hasElse ? ((operands.length - 1) / 2) : (operands.length / 2);
                for (int j = 0; j < numcases; j++) {
                    cases.add(new AbstractMap.SimpleImmutableEntry<>(operands[j * 2], operands[j * 2 + 1]));
                }
                CompiledSQLExpression elseExp = hasElse ? operands[operands.length - 1] : null;
                return new CompiledCaseExpression(cases, elseExp);
            case BuiltinFunctions.NAME_CURRENT_TIMESTAMP:
                return new CompiledFunction(BuiltinFunctions.CURRENT_TIMESTAMP, Collections.emptyList());
            case BuiltinFunctions.NAME_CURRENT_DATE:
                return new CompiledFunction(BuiltinFunctions.NAME_CURRENT_DATE, Collections.emptyList());
            case BuiltinFunctions.NAME_LOWERCASE:
                return new CompiledFunction(BuiltinFunctions.LOWER, Arrays.asList(operands));
            case BuiltinFunctions.NAME_UPPER:
                return new CompiledFunction(BuiltinFunctions.UPPER, Arrays.asList(operands));
            case BuiltinFunctions.NAME_ABS:
                return new CompiledFunction(BuiltinFunctions.ABS, Arrays.asList(operands));
            case BuiltinFunctions.NAME_ROUND:
                return new CompiledFunction(BuiltinFunctions.ROUND, Arrays.asList(operands));
            case BuiltinFunctions.NAME_EXTRACT:
                return new CompiledFunction(BuiltinFunctions.EXTRACT, Arrays.asList(operands));
            case BuiltinFunctions.NAME_FLOOR:
                return new CompiledFunction(BuiltinFunctions.FLOOR, Arrays.asList(operands));
            case BuiltinFunctions.NAME_RAND:
                return new CompiledFunction(BuiltinFunctions.RAND, Arrays.asList(operands));
            case BuiltinFunctions.NAME_REINTERPRET:
                if (operands.length != 1) {
                    throw new StatementExecutionException("unsupported use of Reinterpret with " + Arrays.toString(operands));
                }
                return (CompiledSQLExpression) operands[0];
            default:
                throw new StatementExecutionException("unsupported operator '" + name + "'");
        }
    } else if (expression instanceof RexFieldAccess) {
        RexFieldAccess p = (RexFieldAccess) expression;
        CompiledSQLExpression object = compileExpression(p.getReferenceExpr());
        return new AccessFieldExpression(object, p.getField().getName());
    } else if (expression instanceof RexCorrelVariable) {
        RexCorrelVariable p = (RexCorrelVariable) expression;
        return new AccessCorrelVariableExpression(p.id.getId(), p.id.getName());
    }
    throw new StatementExecutionException("not implemented expression type " + expression.getClass() + ": " + expression);
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) RexCorrelVariable(org.apache.calcite.rex.RexCorrelVariable) SqlOperator(org.apache.calcite.sql.SqlOperator) NlsString(org.apache.calcite.util.NlsString) StatementExecutionException(herddb.model.StatementExecutionException) RexCall(org.apache.calcite.rex.RexCall) ArrayList(java.util.ArrayList) List(java.util.List) RexFieldAccess(org.apache.calcite.rex.RexFieldAccess) RexInputRef(org.apache.calcite.rex.RexInputRef) Map(java.util.Map) AbstractMap(java.util.AbstractMap) RexDynamicParam(org.apache.calcite.rex.RexDynamicParam) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

RexFieldAccess (org.apache.calcite.rex.RexFieldAccess)21 RexNode (org.apache.calcite.rex.RexNode)15 RexCorrelVariable (org.apache.calcite.rex.RexCorrelVariable)9 RexInputRef (org.apache.calcite.rex.RexInputRef)8 RexCall (org.apache.calcite.rex.RexCall)7 CorrelationId (org.apache.calcite.rel.core.CorrelationId)5 ArrayList (java.util.ArrayList)4 RexBuilder (org.apache.calcite.rex.RexBuilder)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 RelNode (org.apache.calcite.rel.RelNode)3 NlsString (org.apache.calcite.util.NlsString)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 List (java.util.List)2 RelSubset (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.volcano.RelSubset)2 RelNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode)2 LogicalCorrelate (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.logical.LogicalCorrelate)2 LogicalProject (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.logical.LogicalProject)2 RexFieldAccess (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexFieldAccess)2 RexNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode)2