Search in sources :

Example 91 with RexLiteral

use of org.apache.calcite.rex.RexLiteral in project drill by axbaretto.

the class PreProcessLogicalRel method visit.

@Override
public RelNode visit(LogicalProject project) {
    final List<RexNode> projExpr = Lists.newArrayList();
    for (RexNode rexNode : project.getChildExps()) {
        projExpr.add(rexNode.accept(unwrappingExpressionVisitor));
    }
    project = project.copy(project.getTraitSet(), project.getInput(), projExpr, project.getRowType());
    List<RexNode> exprList = new ArrayList<>();
    boolean rewrite = false;
    for (RexNode rex : project.getChildExps()) {
        RexNode newExpr = rex;
        if (rex instanceof RexCall) {
            RexCall function = (RexCall) rex;
            String functionName = function.getOperator().getName();
            int nArgs = function.getOperands().size();
            // check if its a convert_from or convert_to function
            if (functionName.equalsIgnoreCase("convert_from") || functionName.equalsIgnoreCase("convert_to")) {
                String literal;
                if (nArgs == 2) {
                    if (function.getOperands().get(1) instanceof RexLiteral) {
                        try {
                            literal = ((NlsString) (((RexLiteral) function.getOperands().get(1)).getValue())).getValue();
                        } catch (final ClassCastException e) {
                            // Caused by user entering a value with a non-string literal
                            throw getConvertFunctionInvalidTypeException(function);
                        }
                    } else {
                        // caused by user entering a non-literal
                        throw getConvertFunctionInvalidTypeException(function);
                    }
                } else {
                    // Second operand is missing
                    throw UserException.parseError().message("'%s' expects a string literal as a second argument.", functionName).build(logger);
                }
                RexBuilder builder = new RexBuilder(factory);
                // construct the new function name based on the input argument
                String newFunctionName = functionName + literal;
                // Look up the new function name in the drill operator table
                List<SqlOperator> operatorList = table.getSqlOperator(newFunctionName);
                if (operatorList.size() == 0) {
                    // User typed in an invalid type name
                    throw getConvertFunctionException(functionName, literal);
                }
                SqlFunction newFunction = null;
                // Find the SqlFunction with the correct args
                for (SqlOperator op : operatorList) {
                    if (op.getOperandTypeChecker().getOperandCountRange().isValidCount(nArgs - 1)) {
                        newFunction = (SqlFunction) op;
                        break;
                    }
                }
                if (newFunction == null) {
                    // we are here because we found some dummy convert function. (See DummyConvertFrom and DummyConvertTo)
                    throw getConvertFunctionException(functionName, literal);
                }
                // create the new expression to be used in the rewritten project
                newExpr = builder.makeCall(newFunction, function.getOperands().subList(0, 1));
                rewrite = true;
            }
        }
        exprList.add(newExpr);
    }
    if (rewrite == true) {
        LogicalProject newProject = project.copy(project.getTraitSet(), project.getInput(0), exprList, project.getRowType());
        return visitChild(newProject, 0, project.getInput());
    }
    return visitChild(project, 0, project.getInput());
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) SqlOperator(org.apache.calcite.sql.SqlOperator) ArrayList(java.util.ArrayList) NlsString(org.apache.calcite.util.NlsString) RexCall(org.apache.calcite.rex.RexCall) RexBuilder(org.apache.calcite.rex.RexBuilder) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) RexNode(org.apache.calcite.rex.RexNode) SqlFunction(org.apache.calcite.sql.SqlFunction)

Example 92 with RexLiteral

use of org.apache.calcite.rex.RexLiteral in project drill by axbaretto.

the class DrillValuesRelBase method convertToJsonNode.

private static JsonNode convertToJsonNode(RelDataType rowType, ImmutableList<ImmutableList<RexLiteral>> tuples) throws IOException {
    TokenBuffer out = new TokenBuffer(MAPPER.getFactory().getCodec(), false);
    JsonOutput json = new ExtendedJsonOutput(out);
    json.writeStartArray();
    String[] fields = rowType.getFieldNames().toArray(new String[rowType.getFieldCount()]);
    for (List<RexLiteral> row : tuples) {
        json.writeStartObject();
        int i = 0;
        for (RexLiteral field : row) {
            json.writeFieldName(fields[i]);
            writeLiteral(field, json);
            i++;
        }
        json.writeEndObject();
    }
    json.writeEndArray();
    json.flush();
    return out.asParser().readValueAsTree();
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) ExtendedJsonOutput(org.apache.drill.exec.vector.complex.fn.ExtendedJsonOutput) JsonOutput(org.apache.drill.exec.vector.complex.fn.JsonOutput) ExtendedJsonOutput(org.apache.drill.exec.vector.complex.fn.ExtendedJsonOutput) TokenBuffer(com.fasterxml.jackson.databind.util.TokenBuffer) NlsString(org.apache.calcite.util.NlsString)

Example 93 with RexLiteral

use of org.apache.calcite.rex.RexLiteral in project drill by axbaretto.

the class WindowPrel method toDrill.

protected LogicalExpression toDrill(AggregateCall call, List<String> fn) {
    DrillParseContext context = new DrillParseContext(PrelUtil.getSettings(getCluster()));
    List<LogicalExpression> args = Lists.newArrayList();
    for (Integer i : call.getArgList()) {
        final int indexInConstants = i - fn.size();
        if (i < fn.size()) {
            args.add(new FieldReference(fn.get(i)));
        } else {
            final RexLiteral constant = constants.get(indexInConstants);
            LogicalExpression expr = DrillOptiq.toDrill(context, getInput(), constant);
            args.add(expr);
        }
    }
    // for count(1).
    if (args.isEmpty()) {
        args.add(new ValueExpressions.LongExpression(1l));
    }
    return new FunctionCall(call.getAggregation().getName().toLowerCase(), args, ExpressionPosition.UNKNOWN);
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) RexLiteral(org.apache.calcite.rex.RexLiteral) FieldReference(org.apache.drill.common.expression.FieldReference) ValueExpressions(org.apache.drill.common.expression.ValueExpressions) DrillParseContext(org.apache.drill.exec.planner.logical.DrillParseContext) FunctionCall(org.apache.drill.common.expression.FunctionCall)

Example 94 with RexLiteral

use of org.apache.calcite.rex.RexLiteral in project hazelcast by hazelcast.

the class RexToExpressionTest method test_tinyint.

@Test
public void test_tinyint() {
    RexLiteral literal = literal(new BigDecimal(1), new BigDecimal(2), SqlTypeName.TINYINT);
    Range<?> converted = convert(literal);
    assertThat(converted).isEqualToComparingFieldByField(range((byte) 1, (byte) 2));
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 95 with RexLiteral

use of org.apache.calcite.rex.RexLiteral in project hazelcast by hazelcast.

the class RexToExpressionTest method test_bigint.

@Test
public void test_bigint() {
    RexLiteral literal = literal(new BigDecimal(1), new BigDecimal(2), SqlTypeName.BIGINT);
    Range<?> converted = convert(literal);
    assertThat(converted).isEqualToComparingFieldByField(range(1L, 2L));
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Aggregations

RexLiteral (org.apache.calcite.rex.RexLiteral)150 RexNode (org.apache.calcite.rex.RexNode)92 ArrayList (java.util.ArrayList)51 RelDataType (org.apache.calcite.rel.type.RelDataType)45 RexCall (org.apache.calcite.rex.RexCall)45 Test (org.junit.Test)32 BigDecimal (java.math.BigDecimal)28 RexInputRef (org.apache.calcite.rex.RexInputRef)26 RelNode (org.apache.calcite.rel.RelNode)22 ImmutableList (com.google.common.collect.ImmutableList)18 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)17 List (java.util.List)16 Map (java.util.Map)16 RexBuilder (org.apache.calcite.rex.RexBuilder)16 AggregateCall (org.apache.calcite.rel.core.AggregateCall)15 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)12 RexLiteral (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexLiteral)11 NlsString (org.apache.calcite.util.NlsString)11 HashMap (java.util.HashMap)10 RexNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode)10