Search in sources :

Example 26 with NlsString

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

the class VisitorDataContext method getValue.

public static Pair<Integer, ?> getValue(RexNode inputRef, RexNode literal) {
    inputRef = removeCast(inputRef);
    literal = removeCast(literal);
    if (inputRef instanceof RexInputRef && literal instanceof RexLiteral) {
        final int index = ((RexInputRef) inputRef).getIndex();
        final RexLiteral rexLiteral = (RexLiteral) literal;
        final RelDataType type = inputRef.getType();
        if (type.getSqlTypeName() == null) {
            LOGGER.warn("{} returned null SqlTypeName", inputRef.toString());
            return null;
        }
        switch(type.getSqlTypeName()) {
            case INTEGER:
                return Pair.of(index, rexLiteral.getValueAs(Integer.class));
            case DOUBLE:
                return Pair.of(index, rexLiteral.getValueAs(Double.class));
            case REAL:
                return Pair.of(index, rexLiteral.getValueAs(Float.class));
            case BIGINT:
                return Pair.of(index, rexLiteral.getValueAs(Long.class));
            case SMALLINT:
                return Pair.of(index, rexLiteral.getValueAs(Short.class));
            case TINYINT:
                return Pair.of(index, rexLiteral.getValueAs(Byte.class));
            case DECIMAL:
                return Pair.of(index, rexLiteral.getValueAs(BigDecimal.class));
            case DATE:
            case TIME:
                return Pair.of(index, rexLiteral.getValueAs(Integer.class));
            case TIMESTAMP:
                return Pair.of(index, rexLiteral.getValueAs(Long.class));
            case CHAR:
                return Pair.of(index, rexLiteral.getValueAs(Character.class));
            case VARCHAR:
                return Pair.of(index, rexLiteral.getValueAs(String.class));
            default:
                // TODO: Support few more supported cases
                LOGGER.warn("{} for value of class {} is being handled in default way", type.getSqlTypeName(), rexLiteral.getValue().getClass());
                if (rexLiteral.getValue() instanceof NlsString) {
                    return Pair.of(index, ((NlsString) rexLiteral.getValue()).getValue());
                } else {
                    return Pair.of(index, rexLiteral.getValue());
                }
        }
    }
    // Unsupported Arguments
    return null;
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) RelDataType(org.apache.calcite.rel.type.RelDataType) NlsString(org.apache.calcite.util.NlsString) BigDecimal(java.math.BigDecimal) RexInputRef(org.apache.calcite.rex.RexInputRef) NlsString(org.apache.calcite.util.NlsString)

Example 27 with NlsString

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

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

the class SetOptionHandler method sqlLiteralToObject.

private static Object sqlLiteralToObject(SqlLiteral literal) {
    final Object object = literal.getValue();
    final SqlTypeName typeName = literal.getTypeName();
    switch(typeName) {
        case DECIMAL:
            {
                final BigDecimal bigDecimal = (BigDecimal) object;
                if (bigDecimal.scale() == 0) {
                    return bigDecimal.longValue();
                } else {
                    return bigDecimal.doubleValue();
                }
            }
        case DOUBLE:
        case FLOAT:
            return ((BigDecimal) object).doubleValue();
        case SMALLINT:
        case TINYINT:
        case BIGINT:
        case INTEGER:
            return ((BigDecimal) object).longValue();
        case VARBINARY:
        case VARCHAR:
        case CHAR:
            return ((NlsString) object).getValue();
        case BOOLEAN:
            return object;
        default:
            throw UserException.validationError().message("Drill doesn't support assigning literals of type %s in SET statements.", typeName).build(logger);
    }
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) NlsString(org.apache.calcite.util.NlsString) BigDecimal(java.math.BigDecimal)

Example 29 with NlsString

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

the class ShowSchemasHandler method rewrite.

/**
 * Rewrite the parse tree as SELECT ... FROM INFORMATION_SCHEMA.SCHEMATA ...
 */
@Override
public SqlNode rewrite(SqlNode sqlNode) throws ForemanSetupException {
    SqlShowSchemas node = unwrap(sqlNode, SqlShowSchemas.class);
    List<SqlNode> selectList = Collections.singletonList(new SqlIdentifier(SCHS_COL_SCHEMA_NAME, SqlParserPos.ZERO));
    SqlNode fromClause = new SqlIdentifier(Arrays.asList(IS_SCHEMA_NAME, InfoSchemaTableType.SCHEMATA.name()), SqlParserPos.ZERO);
    SqlNode where = null;
    SqlNode likePattern = node.getLikePattern();
    if (likePattern != null) {
        SqlNode column = new SqlIdentifier(SCHS_COL_SCHEMA_NAME, SqlParserPos.ZERO);
        // schema names are case insensitive, wrap column in lower function, pattern to lower case
        if (likePattern instanceof SqlCharStringLiteral) {
            NlsString conditionString = ((SqlCharStringLiteral) likePattern).getNlsString();
            likePattern = SqlCharStringLiteral.createCharString(conditionString.getValue().toLowerCase(), conditionString.getCharsetName(), likePattern.getParserPosition());
            column = SqlStdOperatorTable.LOWER.createCall(SqlParserPos.ZERO, column);
        }
        where = DrillParserUtil.createCondition(column, SqlStdOperatorTable.LIKE, likePattern);
    } else if (node.getWhereClause() != null) {
        where = node.getWhereClause();
    }
    return new SqlSelect(SqlParserPos.ZERO, null, new SqlNodeList(selectList, SqlParserPos.ZERO), fromClause, where, null, null, null, null, null, null);
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) SqlShowSchemas(org.apache.drill.exec.planner.sql.parser.SqlShowSchemas) NlsString(org.apache.calcite.util.NlsString) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlCharStringLiteral(org.apache.calcite.sql.SqlCharStringLiteral) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 30 with NlsString

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

the class ShowTablesHandler method rewrite.

/**
 * Rewrite the parse tree as SELECT ... FROM INFORMATION_SCHEMA.`TABLES` ...
 */
@Override
public SqlNode rewrite(SqlNode sqlNode) throws ForemanSetupException {
    SqlShowTables node = unwrap(sqlNode, SqlShowTables.class);
    List<SqlNode> selectList = Arrays.asList(new SqlIdentifier(SHRD_COL_TABLE_SCHEMA, SqlParserPos.ZERO), new SqlIdentifier(SHRD_COL_TABLE_NAME, SqlParserPos.ZERO));
    SqlNode fromClause = new SqlIdentifier(Arrays.asList(IS_SCHEMA_NAME, InfoSchemaTableType.TABLES.name()), SqlParserPos.ZERO);
    SchemaPlus schemaPlus;
    if (node.getDb() != null) {
        List<String> schemaNames = node.getDb().names;
        schemaPlus = SchemaUtilites.findSchema(config.getConverter().getDefaultSchema(), schemaNames);
        if (schemaPlus == null) {
            throw UserException.validationError().message("Invalid schema name [%s]", SchemaUtilites.getSchemaPath(schemaNames)).build(logger);
        }
    } else {
        // If no schema is given in SHOW TABLES command, list tables from current schema
        schemaPlus = config.getConverter().getDefaultSchema();
    }
    if (SchemaUtilites.isRootSchema(schemaPlus)) {
        // If the default schema is a root schema, throw an error to select a default schema
        throw UserException.validationError().message("No default schema selected. Select a schema using 'USE schema' command").build(logger);
    }
    AbstractSchema drillSchema = SchemaUtilites.unwrapAsDrillSchemaInstance(schemaPlus);
    SqlNode where = DrillParserUtil.createCondition(new SqlIdentifier(SHRD_COL_TABLE_SCHEMA, SqlParserPos.ZERO), SqlStdOperatorTable.EQUALS, SqlLiteral.createCharString(drillSchema.getFullSchemaName(), Util.getDefaultCharset().name(), SqlParserPos.ZERO));
    SqlNode filter = null;
    if (node.getLikePattern() != null) {
        SqlNode likePattern = node.getLikePattern();
        SqlNode column = new SqlIdentifier(SHRD_COL_TABLE_NAME, SqlParserPos.ZERO);
        // wrap columns name values and condition in lower function if case insensitive
        if (!drillSchema.areTableNamesCaseSensitive() && likePattern instanceof SqlCharStringLiteral) {
            NlsString conditionString = ((SqlCharStringLiteral) likePattern).getNlsString();
            likePattern = SqlCharStringLiteral.createCharString(conditionString.getValue().toLowerCase(), conditionString.getCharsetName(), likePattern.getParserPosition());
            column = SqlStdOperatorTable.LOWER.createCall(SqlParserPos.ZERO, column);
        }
        filter = DrillParserUtil.createCondition(column, SqlStdOperatorTable.LIKE, likePattern);
    } else if (node.getWhereClause() != null) {
        filter = node.getWhereClause();
    }
    where = DrillParserUtil.createCondition(where, SqlStdOperatorTable.AND, filter);
    return new SqlSelect(SqlParserPos.ZERO, null, new SqlNodeList(selectList, SqlParserPos.ZERO), fromClause, where, null, null, null, null, null, null);
}
Also used : SqlShowTables(org.apache.drill.exec.planner.sql.parser.SqlShowTables) SqlSelect(org.apache.calcite.sql.SqlSelect) AbstractSchema(org.apache.drill.exec.store.AbstractSchema) SchemaPlus(org.apache.calcite.schema.SchemaPlus) NlsString(org.apache.calcite.util.NlsString) SqlNodeList(org.apache.calcite.sql.SqlNodeList) NlsString(org.apache.calcite.util.NlsString) SqlCharStringLiteral(org.apache.calcite.sql.SqlCharStringLiteral) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

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