use of 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;
}
use of 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);
}
use of 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);
}
}
use of 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);
}
use of 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);
}
Aggregations