Search in sources :

Example 6 with SqlCollation

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

use of org.apache.calcite.sql.SqlCollation in project flink by apache.

the class RexLiteral method fromJdbcString.

/**
 * Converts a Jdbc string into a RexLiteral. This method accepts a string, as returned by the
 * Jdbc method ResultSet.getString(), and restores the string into an equivalent RexLiteral. It
 * allows one to use Jdbc strings as a common format for data.
 *
 * <p>If a null literal is provided, then a null pointer will be returned.
 *
 * @param type data type of literal to be read
 * @param typeName type family of literal
 * @param literal the (non-SQL encoded) string representation, as returned by the Jdbc call to
 *     return a column as a string
 * @return a typed RexLiteral, or null
 */
public static RexLiteral fromJdbcString(RelDataType type, SqlTypeName typeName, String literal) {
    if (literal == null) {
        return null;
    }
    switch(typeName) {
        case CHAR:
            Charset charset = type.getCharset();
            SqlCollation collation = type.getCollation();
            NlsString str = new NlsString(literal, charset.name(), collation);
            return new RexLiteral(str, type, typeName);
        case BOOLEAN:
            boolean b = ConversionUtil.toBoolean(literal);
            return new RexLiteral(b, type, typeName);
        case DECIMAL:
        case DOUBLE:
            BigDecimal d = new BigDecimal(literal);
            return new RexLiteral(d, type, typeName);
        case BINARY:
            byte[] bytes = ConversionUtil.toByteArrayFromString(literal, 16);
            return new RexLiteral(new ByteString(bytes), type, typeName);
        case NULL:
            return new RexLiteral(null, type, typeName);
        case INTERVAL_DAY:
        case INTERVAL_DAY_HOUR:
        case INTERVAL_DAY_MINUTE:
        case INTERVAL_DAY_SECOND:
        case INTERVAL_HOUR:
        case INTERVAL_HOUR_MINUTE:
        case INTERVAL_HOUR_SECOND:
        case INTERVAL_MINUTE:
        case INTERVAL_MINUTE_SECOND:
        case INTERVAL_SECOND:
            long millis = SqlParserUtil.intervalToMillis(literal, type.getIntervalQualifier());
            return new RexLiteral(BigDecimal.valueOf(millis), type, typeName);
        case INTERVAL_YEAR:
        case INTERVAL_YEAR_MONTH:
        case INTERVAL_MONTH:
            long months = SqlParserUtil.intervalToMonths(literal, type.getIntervalQualifier());
            return new RexLiteral(BigDecimal.valueOf(months), type, typeName);
        case DATE:
        case TIME:
        case TIMESTAMP:
            String format = getCalendarFormat(typeName);
            TimeZone tz = DateTimeUtils.UTC_ZONE;
            final Comparable v;
            switch(typeName) {
                case DATE:
                    final Calendar cal = DateTimeUtils.parseDateFormat(literal, new SimpleDateFormat(format, Locale.ROOT), tz);
                    if (cal == null) {
                        throw new AssertionError("fromJdbcString: invalid date/time value '" + literal + "'");
                    }
                    v = DateString.fromCalendarFields(cal);
                    break;
                default:
                    // Allow fractional seconds for times and timestamps
                    assert format != null;
                    final DateTimeUtils.PrecisionTime ts = DateTimeUtils.parsePrecisionDateTimeLiteral(literal, new SimpleDateFormat(format, Locale.ROOT), tz, -1);
                    if (ts == null) {
                        throw new AssertionError("fromJdbcString: invalid date/time value '" + literal + "'");
                    }
                    switch(typeName) {
                        case TIMESTAMP:
                            v = TimestampString.fromCalendarFields(ts.getCalendar()).withFraction(ts.getFraction());
                            break;
                        case TIME:
                            v = TimeString.fromCalendarFields(ts.getCalendar()).withFraction(ts.getFraction());
                            break;
                        default:
                            throw new AssertionError();
                    }
            }
            return new RexLiteral(v, type, typeName);
        case SYMBOL:
        // Symbols are for internal use
        default:
            throw new AssertionError("fromJdbcString: unsupported type");
    }
}
Also used : ByteString(org.apache.calcite.avatica.util.ByteString) Calendar(java.util.Calendar) Charset(java.nio.charset.Charset) TimeString(org.apache.calcite.util.TimeString) DateString(org.apache.calcite.util.DateString) NlsString(org.apache.calcite.util.NlsString) ByteString(org.apache.calcite.avatica.util.ByteString) TimestampString(org.apache.calcite.util.TimestampString) BigDecimal(java.math.BigDecimal) TimeZone(java.util.TimeZone) SqlCollation(org.apache.calcite.sql.SqlCollation) NlsString(org.apache.calcite.util.NlsString) DateTimeUtils(org.apache.calcite.avatica.util.DateTimeUtils) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

SqlCollation (org.apache.calcite.sql.SqlCollation)7 Charset (java.nio.charset.Charset)4 NlsString (org.apache.calcite.util.NlsString)3 BigDecimal (java.math.BigDecimal)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Calendar (java.util.Calendar)2 TimeZone (java.util.TimeZone)2 ByteString (org.apache.calcite.avatica.util.ByteString)2 DateTimeUtils (org.apache.calcite.avatica.util.DateTimeUtils)2 RelDataType (org.apache.calcite.rel.type.RelDataType)2 DateString (org.apache.calcite.util.DateString)2 TimeString (org.apache.calcite.util.TimeString)2 TimestampString (org.apache.calcite.util.TimestampString)2 RelDataTypeFamily (org.apache.calcite.rel.type.RelDataTypeFamily)1 SqlCharStringLiteral (org.apache.calcite.sql.SqlCharStringLiteral)1 SqlLiteral (org.apache.calcite.sql.SqlLiteral)1 SqlNode (org.apache.calcite.sql.SqlNode)1 SqlWriter (org.apache.calcite.sql.SqlWriter)1 BitString (org.apache.calcite.util.BitString)1