Search in sources :

Example 31 with SqlLiteral

use of org.apache.calcite.sql.SqlLiteral in project calcite by apache.

the class HsqldbSqlDialect method unparseCall.

@Override
public void unparseCall(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) {
    switch(call.getKind()) {
        case FLOOR:
            if (call.operandCount() != 2) {
                super.unparseCall(writer, call, leftPrec, rightPrec);
                return;
            }
            final SqlLiteral timeUnitNode = call.operand(1);
            final TimeUnitRange timeUnit = timeUnitNode.getValueAs(TimeUnitRange.class);
            final String translatedLit = convertTimeUnit(timeUnit);
            SqlCall call2 = SqlFloorFunction.replaceTimeUnitOperand(call, translatedLit, timeUnitNode.getParserPosition());
            SqlFloorFunction.unparseDatetimeFunction(writer, call2, "TRUNC", true);
            break;
        default:
            super.unparseCall(writer, call, leftPrec, rightPrec);
    }
}
Also used : SqlCall(org.apache.calcite.sql.SqlCall) TimeUnitRange(org.apache.calcite.avatica.util.TimeUnitRange) SqlLiteral(org.apache.calcite.sql.SqlLiteral)

Example 32 with SqlLiteral

use of org.apache.calcite.sql.SqlLiteral in project calcite by apache.

the class HsqldbSqlDialect method rewriteSingleValueExpr.

@Override
public SqlNode rewriteSingleValueExpr(SqlNode aggCall) {
    final SqlNode operand = ((SqlBasicCall) aggCall).operand(0);
    final SqlLiteral nullLiteral = SqlLiteral.createNull(SqlParserPos.ZERO);
    final SqlNode unionOperand = SqlStdOperatorTable.VALUES.createCall(SqlParserPos.ZERO, SqlLiteral.createApproxNumeric("0", SqlParserPos.ZERO));
    // For hsqldb, generate
    // CASE COUNT(*)
    // WHEN 0 THEN NULL
    // WHEN 1 THEN MIN(<result>)
    // ELSE (VALUES 1 UNION ALL VALUES 1)
    // END
    final SqlNode caseExpr = new SqlCase(SqlParserPos.ZERO, SqlStdOperatorTable.COUNT.createCall(SqlParserPos.ZERO, operand), SqlNodeList.of(SqlLiteral.createExactNumeric("0", SqlParserPos.ZERO), SqlLiteral.createExactNumeric("1", SqlParserPos.ZERO)), SqlNodeList.of(nullLiteral, SqlStdOperatorTable.MIN.createCall(SqlParserPos.ZERO, operand)), SqlStdOperatorTable.SCALAR_QUERY.createCall(SqlParserPos.ZERO, SqlStdOperatorTable.UNION_ALL.createCall(SqlParserPos.ZERO, unionOperand, unionOperand)));
    LOGGER.debug("SINGLE_VALUE rewritten into [{}]", caseExpr);
    return caseExpr;
}
Also used : SqlBasicCall(org.apache.calcite.sql.SqlBasicCall) SqlCase(org.apache.calcite.sql.fun.SqlCase) SqlLiteral(org.apache.calcite.sql.SqlLiteral) SqlNode(org.apache.calcite.sql.SqlNode)

Example 33 with SqlLiteral

use of org.apache.calcite.sql.SqlLiteral in project calcite by apache.

the class PostgresqlSqlDialect method unparseCall.

@Override
public void unparseCall(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) {
    switch(call.getKind()) {
        case FLOOR:
            if (call.operandCount() != 2) {
                super.unparseCall(writer, call, leftPrec, rightPrec);
                return;
            }
            final SqlLiteral timeUnitNode = call.operand(1);
            final TimeUnitRange timeUnit = timeUnitNode.getValueAs(TimeUnitRange.class);
            SqlCall call2 = SqlFloorFunction.replaceTimeUnitOperand(call, timeUnit.name(), timeUnitNode.getParserPosition());
            SqlFloorFunction.unparseDatetimeFunction(writer, call2, "DATE_TRUNC", false);
            break;
        default:
            super.unparseCall(writer, call, leftPrec, rightPrec);
    }
}
Also used : SqlCall(org.apache.calcite.sql.SqlCall) TimeUnitRange(org.apache.calcite.avatica.util.TimeUnitRange) SqlLiteral(org.apache.calcite.sql.SqlLiteral)

Example 34 with SqlLiteral

use of org.apache.calcite.sql.SqlLiteral in project calcite by apache.

the class SqlToRelConverter method convertOver.

private RexNode convertOver(Blackboard bb, SqlNode node) {
    SqlCall call = (SqlCall) node;
    SqlCall aggCall = call.operand(0);
    SqlNode windowOrRef = call.operand(1);
    final SqlWindow window = validator.resolveWindow(windowOrRef, bb.scope, true);
    // ROW_NUMBER() expects specific kind of framing.
    if (aggCall.getKind() == SqlKind.ROW_NUMBER) {
        window.setLowerBound(SqlWindow.createUnboundedPreceding(SqlParserPos.ZERO));
        window.setUpperBound(SqlWindow.createCurrentRow(SqlParserPos.ZERO));
        window.setRows(SqlLiteral.createBoolean(true, SqlParserPos.ZERO));
    }
    final SqlNodeList partitionList = window.getPartitionList();
    final ImmutableList.Builder<RexNode> partitionKeys = ImmutableList.builder();
    for (SqlNode partition : partitionList) {
        partitionKeys.add(bb.convertExpression(partition));
    }
    RexNode lowerBound = bb.convertExpression(window.getLowerBound());
    RexNode upperBound = bb.convertExpression(window.getUpperBound());
    SqlNodeList orderList = window.getOrderList();
    if ((orderList.size() == 0) && !window.isRows()) {
        // A logical range requires an ORDER BY clause. Use the implicit
        // ordering of this relation. There must be one, otherwise it would
        // have failed validation.
        orderList = bb.scope.getOrderList();
        if (orderList == null) {
            throw new AssertionError("Relation should have sort key for implicit ORDER BY");
        }
    }
    final ImmutableList.Builder<RexFieldCollation> orderKeys = ImmutableList.builder();
    final Set<SqlKind> flags = EnumSet.noneOf(SqlKind.class);
    for (SqlNode order : orderList) {
        flags.clear();
        RexNode e = bb.convertSortExpression(order, flags);
        orderKeys.add(new RexFieldCollation(e, flags));
    }
    try {
        Preconditions.checkArgument(bb.window == null, "already in window agg mode");
        bb.window = window;
        RexNode rexAgg = exprConverter.convertCall(bb, aggCall);
        rexAgg = rexBuilder.ensureType(validator.getValidatedNodeType(call), rexAgg, false);
        // Walk over the tree and apply 'over' to all agg functions. This is
        // necessary because the returned expression is not necessarily a call
        // to an agg function. For example, AVG(x) becomes SUM(x) / COUNT(x).
        final SqlLiteral q = aggCall.getFunctionQuantifier();
        final boolean isDistinct = q != null && q.getValue() == SqlSelectKeyword.DISTINCT;
        final RexShuttle visitor = new HistogramShuttle(partitionKeys.build(), orderKeys.build(), RexWindowBound.create(window.getLowerBound(), lowerBound), RexWindowBound.create(window.getUpperBound(), upperBound), window, isDistinct);
        RexNode overNode = rexAgg.accept(visitor);
        return overNode;
    } finally {
        bb.window = null;
    }
}
Also used : RexShuttle(org.apache.calcite.rex.RexShuttle) SqlCall(org.apache.calcite.sql.SqlCall) ImmutableList(com.google.common.collect.ImmutableList) SqlKind(org.apache.calcite.sql.SqlKind) RexFieldCollation(org.apache.calcite.rex.RexFieldCollation) SqlWindow(org.apache.calcite.sql.SqlWindow) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlLiteral(org.apache.calcite.sql.SqlLiteral) SqlNode(org.apache.calcite.sql.SqlNode) RexNode(org.apache.calcite.rex.RexNode)

Example 35 with SqlLiteral

use of org.apache.calcite.sql.SqlLiteral in project calcite by apache.

the class StandardConvertletTable method convertLiteralChain.

/**
 * Converts a LiteralChain expression: that is, concatenates the operands
 * immediately, to produce a single literal string.
 *
 * <p>Called automatically via reflection.
 */
public RexNode convertLiteralChain(SqlRexContext cx, SqlLiteralChainOperator op, SqlCall call) {
    Util.discard(cx);
    SqlLiteral sum = SqlLiteralChainOperator.concatenateOperands(call);
    return cx.convertLiteral(sum);
}
Also used : SqlLiteral(org.apache.calcite.sql.SqlLiteral)

Aggregations

SqlLiteral (org.apache.calcite.sql.SqlLiteral)42 SqlNode (org.apache.calcite.sql.SqlNode)19 RelDataType (org.apache.calcite.rel.type.RelDataType)12 SqlCall (org.apache.calcite.sql.SqlCall)8 SqlNodeList (org.apache.calcite.sql.SqlNodeList)7 RexNode (org.apache.calcite.rex.RexNode)6 ArrayList (java.util.ArrayList)5 TimeUnitRange (org.apache.calcite.avatica.util.TimeUnitRange)5 NlsString (org.apache.calcite.util.NlsString)5 SqlBasicCall (org.apache.calcite.sql.SqlBasicCall)4 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)4 SqlMatchRecognize (org.apache.calcite.sql.SqlMatchRecognize)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Map (java.util.Map)3 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)3 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)3 RexLiteral (org.apache.calcite.rex.RexLiteral)3 SqlCallBinding (org.apache.calcite.sql.SqlCallBinding)3 BitString (org.apache.calcite.util.BitString)3 HashMap (java.util.HashMap)2