Search in sources :

Example 21 with NlsString

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.NlsString in project calcite by apache.

the class SqlLiteral method unescapeUnicode.

/**
 * Transforms this literal (which must be of type character) into a new one
 * in which 4-digit Unicode escape sequences have been replaced with the
 * corresponding Unicode characters.
 *
 * @param unicodeEscapeChar escape character (e.g. backslash) for Unicode
 *                          numeric sequences; 0 implies no transformation
 * @return transformed literal
 */
public SqlLiteral unescapeUnicode(char unicodeEscapeChar) {
    if (unicodeEscapeChar == 0) {
        return this;
    }
    assert SqlTypeUtil.inCharFamily(getTypeName());
    NlsString ns = (NlsString) value;
    String s = ns.getValue();
    StringBuilder sb = new StringBuilder();
    int n = s.length();
    for (int i = 0; i < n; ++i) {
        char c = s.charAt(i);
        if (c == unicodeEscapeChar) {
            if (n > (i + 1)) {
                if (s.charAt(i + 1) == unicodeEscapeChar) {
                    sb.append(unicodeEscapeChar);
                    ++i;
                    continue;
                }
            }
            if ((i + 5) > n) {
                throw SqlUtil.newContextException(getParserPosition(), RESOURCE.unicodeEscapeMalformed(i));
            }
            final String u = s.substring(i + 1, i + 5);
            final int v;
            try {
                v = Integer.parseInt(u, 16);
            } catch (NumberFormatException ex) {
                throw SqlUtil.newContextException(getParserPosition(), RESOURCE.unicodeEscapeMalformed(i));
            }
            sb.append((char) (v & 0xFFFF));
            // skip hexits
            i += 4;
        } else {
            sb.append(c);
        }
    }
    ns = new NlsString(sb.toString(), ns.getCharsetName(), ns.getCollation());
    return new SqlCharStringLiteral(ns, getParserPosition());
}
Also used : NlsString(org.apache.calcite.util.NlsString) TimeString(org.apache.calcite.util.TimeString) DateString(org.apache.calcite.util.DateString) NlsString(org.apache.calcite.util.NlsString) TimestampString(org.apache.calcite.util.TimestampString) BitString(org.apache.calcite.util.BitString)

Example 22 with NlsString

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.NlsString in project calcite by apache.

the class SqlLiteral method createSqlType.

public RelDataType createSqlType(RelDataTypeFactory typeFactory) {
    BitString bitString;
    switch(typeName) {
        case NULL:
        case BOOLEAN:
            RelDataType ret = typeFactory.createSqlType(typeName);
            ret = typeFactory.createTypeWithNullability(ret, null == value);
            return ret;
        case BINARY:
            bitString = (BitString) value;
            int bitCount = bitString.getBitCount();
            return typeFactory.createSqlType(SqlTypeName.BINARY, bitCount / 8);
        case CHAR:
            NlsString string = (NlsString) value;
            Charset charset = string.getCharset();
            if (null == charset) {
                charset = typeFactory.getDefaultCharset();
            }
            SqlCollation collation = string.getCollation();
            if (null == collation) {
                collation = SqlCollation.COERCIBLE;
            }
            RelDataType type = typeFactory.createSqlType(SqlTypeName.CHAR, string.getValue().length());
            type = typeFactory.createTypeWithCharsetAndCollation(type, charset, collation);
            return type;
        case INTERVAL_YEAR:
        case INTERVAL_YEAR_MONTH:
        case INTERVAL_MONTH:
        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:
            SqlIntervalLiteral.IntervalValue intervalValue = (SqlIntervalLiteral.IntervalValue) value;
            return typeFactory.createSqlIntervalType(intervalValue.getIntervalQualifier());
        case SYMBOL:
            return typeFactory.createSqlType(SqlTypeName.SYMBOL);
        // handled in derived class
        case INTEGER:
        // handled in derived class
        case TIME:
        // should never happen
        case VARCHAR:
        // should never happen
        case VARBINARY:
        default:
            throw Util.needToImplement(toString() + ", operand=" + value);
    }
}
Also used : BitString(org.apache.calcite.util.BitString) NlsString(org.apache.calcite.util.NlsString) Charset(java.nio.charset.Charset) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 23 with NlsString

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.NlsString in project flink by apache.

the class SqlPartitionUtils method getPartitionKVs.

/**
 * Get static partition key value pair as strings.
 *
 * <p>For character literals we return the unquoted and unescaped values. For other types we use
 * {@link SqlLiteral#toString()} to get the string format of the value literal.
 *
 * @return the mapping of column names to values of partition specifications, returns an empty
 *     map if there is no partition specifications.
 */
public static LinkedHashMap<String, String> getPartitionKVs(SqlNodeList partitionSpec) {
    if (partitionSpec == null) {
        return null;
    }
    LinkedHashMap<String, String> ret = new LinkedHashMap<>();
    if (partitionSpec.size() == 0) {
        return ret;
    }
    for (SqlNode node : partitionSpec.getList()) {
        SqlProperty sqlProperty = (SqlProperty) node;
        Comparable<?> comparable = SqlLiteral.value(sqlProperty.getValue());
        String value = comparable instanceof NlsString ? ((NlsString) comparable).getValue() : comparable.toString();
        ret.put(sqlProperty.getKey().getSimple(), value);
    }
    return ret;
}
Also used : NlsString(org.apache.calcite.util.NlsString) NlsString(org.apache.calcite.util.NlsString) LinkedHashMap(java.util.LinkedHashMap) SqlNode(org.apache.calcite.sql.SqlNode)

Example 24 with NlsString

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.NlsString in project flink by apache.

the class RexLiteral method appendAsJava.

/**
 * Appends the specified value in the provided destination as a Java string. The value must be
 * consistent with the type, as per {@link #valueMatchesType}.
 *
 * <p>Typical return values:
 *
 * <ul>
 *   <li>true
 *   <li>null
 *   <li>"Hello, world!"
 *   <li>1.25
 *   <li>1234ABCD
 * </ul>
 *
 * @param value Value to be appended to the provided destination as a Java string
 * @param sb Destination to which to append the specified value
 * @param typeName Type name to be used for the transformation of the value to a Java string
 * @param type Type to be used for the transformation of the value to a Java string
 * @param includeType Whether to include the data type in the Java representation
 */
private static void appendAsJava(Comparable value, StringBuilder sb, SqlTypeName typeName, RelDataType type, boolean java, RexDigestIncludeType includeType) {
    switch(typeName) {
        case CHAR:
            NlsString nlsString = (NlsString) value;
            if (java) {
                Util.printJavaString(sb, nlsString.getValue(), true);
            } else {
                boolean includeCharset = (nlsString.getCharsetName() != null) && !nlsString.getCharsetName().equals(CalciteSystemProperty.DEFAULT_CHARSET.value());
                sb.append(nlsString.asSql(includeCharset, false));
            }
            break;
        case BOOLEAN:
            assert value instanceof Boolean;
            sb.append(value.toString());
            break;
        case DECIMAL:
            assert value instanceof BigDecimal;
            sb.append(value.toString());
            break;
        case DOUBLE:
            assert value instanceof BigDecimal;
            sb.append(Util.toScientificNotation((BigDecimal) value));
            break;
        case BIGINT:
            assert value instanceof BigDecimal;
            long narrowLong = ((BigDecimal) value).longValue();
            sb.append(String.valueOf(narrowLong));
            sb.append('L');
            break;
        case BINARY:
            assert value instanceof ByteString;
            sb.append("X'");
            sb.append(((ByteString) value).toString(16));
            sb.append("'");
            break;
        case NULL:
            assert value == null;
            sb.append("null");
            break;
        case SARG:
            assert value instanceof Sarg;
            // noinspection unchecked,rawtypes
            Util.asStringBuilder(sb, sb2 -> printSarg(sb2, (Sarg) value, type));
            break;
        case SYMBOL:
            assert value instanceof Enum;
            sb.append("FLAG(");
            sb.append(value.toString());
            sb.append(")");
            break;
        case DATE:
            assert value instanceof DateString;
            sb.append(value.toString());
            break;
        case TIME:
        case TIME_WITH_LOCAL_TIME_ZONE:
            assert value instanceof TimeString;
            sb.append(value.toString());
            break;
        case TIMESTAMP:
        case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
            assert value instanceof TimestampString;
            sb.append(value.toString());
            break;
        case INTERVAL_YEAR:
        case INTERVAL_YEAR_MONTH:
        case INTERVAL_MONTH:
        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:
            assert value instanceof BigDecimal;
            sb.append(value.toString());
            break;
        case MULTISET:
        case ROW:
            final List<RexLiteral> list = (List) value;
            Util.asStringBuilder(sb, sb2 -> Util.printList(sb, list.size(), (sb3, i) -> sb3.append(list.get(i).computeDigest(includeType))));
            break;
        case GEOMETRY:
            final String wkt = GeoFunctions.ST_AsWKT((Geometries.Geom) value);
            sb.append(wkt);
            break;
        default:
            assert valueMatchesType(value, typeName, true);
            throw Util.needToImplement(typeName);
    }
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) TimeUnit(org.apache.calcite.avatica.util.TimeUnit) ByteBuffer(java.nio.ByteBuffer) TimeString(org.apache.calcite.util.TimeString) BigDecimal(java.math.BigDecimal) Calendar(java.util.Calendar) ImmutableList(com.google.common.collect.ImmutableList) Charset(java.nio.charset.Charset) DateTimeUtils(org.apache.calcite.avatica.util.DateTimeUtils) Geometries(org.apache.calcite.runtime.Geometries) Locale(java.util.Locale) Map(java.util.Map) SqlOperator(org.apache.calcite.sql.SqlOperator) CalciteSystemProperty(org.apache.calcite.config.CalciteSystemProperty) GeoFunctions(org.apache.calcite.runtime.GeoFunctions) Functions(org.apache.calcite.linq4j.function.Functions) PrintWriter(java.io.PrintWriter) RelDataType(org.apache.calcite.rel.type.RelDataType) Litmus(org.apache.calcite.util.Litmus) Sarg(org.apache.calcite.util.Sarg) SqlKind(org.apache.calcite.sql.SqlKind) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) DateString(org.apache.calcite.util.DateString) NlsString(org.apache.calcite.util.NlsString) ByteString(org.apache.calcite.avatica.util.ByteString) TimeZone(java.util.TimeZone) TimestampString(org.apache.calcite.util.TimestampString) ConversionUtil(org.apache.calcite.util.ConversionUtil) RelNode(org.apache.calcite.rel.RelNode) SqlCollation(org.apache.calcite.sql.SqlCollation) CompositeList(org.apache.calcite.util.CompositeList) Objects(java.util.Objects) List(java.util.List) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) FlatLists(org.apache.calcite.runtime.FlatLists) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) Preconditions(com.google.common.base.Preconditions) Util(org.apache.calcite.util.Util) SqlParserUtil(org.apache.calcite.sql.parser.SqlParserUtil) Sarg(org.apache.calcite.util.Sarg) TimeString(org.apache.calcite.util.TimeString) ByteString(org.apache.calcite.avatica.util.ByteString) Geometries(org.apache.calcite.runtime.Geometries) 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) DateString(org.apache.calcite.util.DateString) NlsString(org.apache.calcite.util.NlsString) ImmutableList(com.google.common.collect.ImmutableList) CompositeList(org.apache.calcite.util.CompositeList) List(java.util.List) TimestampString(org.apache.calcite.util.TimestampString)

Example 25 with NlsString

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.NlsString 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

NlsString (org.apache.calcite.util.NlsString)32 BigDecimal (java.math.BigDecimal)13 TimestampString (org.apache.calcite.util.TimestampString)9 DateString (org.apache.calcite.util.DateString)8 RelDataType (org.apache.calcite.rel.type.RelDataType)7 TimeString (org.apache.calcite.util.TimeString)7 ByteString (org.apache.calcite.avatica.util.ByteString)6 RexLiteral (org.apache.calcite.rex.RexLiteral)6 RexNode (org.apache.calcite.rex.RexNode)6 SqlNode (org.apache.calcite.sql.SqlNode)6 RexCall (org.apache.calcite.rex.RexCall)5 Charset (java.nio.charset.Charset)4 ArrayList (java.util.ArrayList)4 Calendar (java.util.Calendar)4 SqlCharStringLiteral (org.apache.calcite.sql.SqlCharStringLiteral)4 SqlCollation (org.apache.calcite.sql.SqlCollation)4 SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)4 ImmutableList (com.google.common.collect.ImmutableList)3 SimpleDateFormat (java.text.SimpleDateFormat)3 List (java.util.List)3