Search in sources :

Example 51 with SqlWriter

use of org.apache.calcite.sql.SqlWriter 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 52 with SqlWriter

use of org.apache.calcite.sql.SqlWriter 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)

Aggregations

SqlWriter (org.apache.calcite.sql.SqlWriter)48 SqlNode (org.apache.calcite.sql.SqlNode)40 SqlNodeList (org.apache.calcite.sql.SqlNodeList)5 SqlNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode)4 SqlWriter (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlWriter)4 SqlJsonConstructorNullClause (org.apache.calcite.sql.SqlJsonConstructorNullClause)3 SqlIdentifier (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier)2 SqlPrettyWriter (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.pretty.SqlPrettyWriter)2 SqlLiteral (org.apache.calcite.sql.SqlLiteral)2 SqlTableConstraint (org.apache.flink.sql.parser.ddl.constraint.SqlTableConstraint)2 Test (org.junit.Test)2 ColumnMetadata (com.datastax.driver.core.ColumnMetadata)1 MaterializedViewMetadata (com.datastax.driver.core.MaterializedViewMetadata)1 HazelcastSqlCase (com.hazelcast.jet.sql.impl.validate.operators.special.HazelcastSqlCase)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 TimeUnitRange (org.apache.calcite.avatica.util.TimeUnitRange)1 CalciteSchema (org.apache.calcite.jdbc.CalciteSchema)1 SchemaPlus (org.apache.calcite.schema.SchemaPlus)1