Search in sources :

Example 26 with SqlLiteral

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

the class SqlLiteralChainOperator method unparse.

public void unparse(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) {
    final SqlWriter.Frame frame = writer.startList("", "");
    SqlCollation collation = null;
    for (Ord<SqlNode> operand : Ord.zip(call.getOperandList())) {
        SqlLiteral rand = (SqlLiteral) operand.e;
        if (operand.i > 0) {
            // SQL:2003 says there must be a newline between string
            // fragments.
            writer.newlineAndIndent();
        }
        if (rand instanceof SqlCharStringLiteral) {
            NlsString nls = ((SqlCharStringLiteral) rand).getNlsString();
            if (operand.i == 0) {
                collation = nls.getCollation();
                // print with prefix
                writer.literal(nls.asSql(true, false));
            } else {
                // print without prefix
                writer.literal(nls.asSql(false, false));
            }
        } else if (operand.i == 0) {
            // print with prefix
            rand.unparse(writer, leftPrec, rightPrec);
        } else {
            // print without prefix
            if (rand.getTypeName() == SqlTypeName.BINARY) {
                BitString bs = (BitString) rand.getValue();
                writer.literal("'" + bs.toHexString() + "'");
            } else {
                writer.literal("'" + rand.toValue() + "'");
            }
        }
    }
    if (collation != null) {
        collation.unparse(writer, 0, 0);
    }
    writer.endList(frame);
}
Also used : SqlWriter(org.apache.calcite.sql.SqlWriter) BitString(org.apache.calcite.util.BitString) SqlCollation(org.apache.calcite.sql.SqlCollation) NlsString(org.apache.calcite.util.NlsString) SqlCharStringLiteral(org.apache.calcite.sql.SqlCharStringLiteral) SqlLiteral(org.apache.calcite.sql.SqlLiteral) SqlNode(org.apache.calcite.sql.SqlNode)

Example 27 with SqlLiteral

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

the class MssqlSqlDialect method unparseFloor.

/**
 * Unparses datetime floor for Microsoft SQL Server.
 * There is no TRUNC function, so simulate this using calls to CONVERT.
 *
 * @param writer Writer
 * @param call Call
 */
private void unparseFloor(SqlWriter writer, SqlCall call) {
    SqlLiteral node = call.operand(1);
    TimeUnitRange unit = (TimeUnitRange) node.getValue();
    switch(unit) {
        case YEAR:
            unparseFloorWithUnit(writer, call, 4, "-01-01");
            break;
        case MONTH:
            unparseFloorWithUnit(writer, call, 7, "-01");
            break;
        case WEEK:
            writer.print("CONVERT(DATETIME, CONVERT(VARCHAR(10), " + "DATEADD(day, - (6 + DATEPART(weekday, ");
            call.operand(0).unparse(writer, 0, 0);
            writer.print(")) % 7, ");
            call.operand(0).unparse(writer, 0, 0);
            writer.print("), 126))");
            break;
        case DAY:
            unparseFloorWithUnit(writer, call, 10, "");
            break;
        case HOUR:
            unparseFloorWithUnit(writer, call, 13, ":00:00");
            break;
        case MINUTE:
            unparseFloorWithUnit(writer, call, 16, ":00");
            break;
        case SECOND:
            unparseFloorWithUnit(writer, call, 19, ":00");
            break;
        default:
            throw new IllegalArgumentException("MSSQL does not support FLOOR for time unit: " + unit);
    }
}
Also used : TimeUnitRange(org.apache.calcite.avatica.util.TimeUnitRange) SqlLiteral(org.apache.calcite.sql.SqlLiteral)

Example 28 with SqlLiteral

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

the class MysqlSqlDialect method unparseFloor.

/**
 * Unparses datetime floor for MySQL. There is no TRUNC function, so simulate
 * this using calls to DATE_FORMAT.
 *
 * @param writer Writer
 * @param call Call
 */
private void unparseFloor(SqlWriter writer, SqlCall call) {
    SqlLiteral node = call.operand(1);
    TimeUnitRange unit = (TimeUnitRange) node.getValue();
    if (unit == TimeUnitRange.WEEK) {
        writer.print("STR_TO_DATE");
        SqlWriter.Frame frame = writer.startList("(", ")");
        writer.print("DATE_FORMAT(");
        call.operand(0).unparse(writer, 0, 0);
        writer.print(", '%x%v-1'), '%x%v-%w'");
        writer.endList(frame);
        return;
    }
    String format;
    switch(unit) {
        case YEAR:
            format = "%Y-01-01";
            break;
        case MONTH:
            format = "%Y-%m-01";
            break;
        case DAY:
            format = "%Y-%m-%d";
            break;
        case HOUR:
            format = "%Y-%m-%d %k:00:00";
            break;
        case MINUTE:
            format = "%Y-%m-%d %k:%i:00";
            break;
        case SECOND:
            format = "%Y-%m-%d %k:%i:%s";
            break;
        default:
            throw new AssertionError("MYSQL does not support FLOOR for time unit: " + unit);
    }
    writer.print("DATE_FORMAT");
    SqlWriter.Frame frame = writer.startList("(", ")");
    call.operand(0).unparse(writer, 0, 0);
    writer.sep(",", true);
    writer.print("'" + format + "'");
    writer.endList(frame);
}
Also used : SqlWriter(org.apache.calcite.sql.SqlWriter) TimeUnitRange(org.apache.calcite.avatica.util.TimeUnitRange) SqlLiteral(org.apache.calcite.sql.SqlLiteral)

Example 29 with SqlLiteral

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

the class MysqlSqlDialect 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 = new SqlSelect(SqlParserPos.ZERO, SqlNodeList.EMPTY, SqlNodeList.of(nullLiteral), null, null, null, null, SqlNodeList.EMPTY, null, null, null);
    // For MySQL, generate
    // CASE COUNT(*)
    // WHEN 0 THEN NULL
    // WHEN 1 THEN <result>
    // ELSE (SELECT NULL UNION ALL SELECT NULL)
    // 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, 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 : SqlSelect(org.apache.calcite.sql.SqlSelect) 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 30 with SqlLiteral

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

the class OracleSqlDialect method unparseCall.

@Override
public void unparseCall(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) {
    if (call.getOperator() == SqlStdOperatorTable.SUBSTRING) {
        SqlUtil.unparseFunctionSyntax(OracleSqlOperatorTable.SUBSTR, writer, call);
    } else {
        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, "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)

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