use of org.apache.calcite.sql.type.SqlTypeName in project hazelcast by hazelcast.
the class RexToExpression method extractRangeFromSearch.
@SuppressWarnings({ "unchecked", "UnstableApiUsage" })
public static RangeSet extractRangeFromSearch(RexLiteral literal) {
Sarg<?> sarg = literal.getValueAs(Sarg.class);
if (sarg == null) {
return null;
}
RelDataType literalType = literal.getType();
SqlTypeName sqlType = literalType.getSqlTypeName();
return RangeSets.copy(sarg.rangeSet, value -> convertSargValue(value, sqlType));
}
use of org.apache.calcite.sql.type.SqlTypeName in project hazelcast by hazelcast.
the class OptUtils method convert.
/**
* Converts a {@link TableField} to {@link RelDataType}.
*/
public static RelDataType convert(TableField field, RelDataTypeFactory typeFactory) {
QueryDataType fieldType = field.getType();
SqlTypeName sqlTypeName = HazelcastTypeUtils.toCalciteType(fieldType);
if (sqlTypeName == null) {
throw new IllegalStateException("Unsupported type family: " + fieldType + ", getSqlTypeName should never return null.");
}
if (sqlTypeName == SqlTypeName.OTHER) {
return convertCustomType(fieldType);
} else {
RelDataType relType = typeFactory.createSqlType(sqlTypeName);
return typeFactory.createTypeWithNullability(relType, true);
}
}
use of org.apache.calcite.sql.type.SqlTypeName in project hazelcast by hazelcast.
the class LiteralUtils method literal.
public static Literal literal(RexNode node) {
if (node.getKind() != SqlKind.LITERAL) {
// Not a literal
return null;
}
RexLiteral literal = (RexLiteral) node;
SqlTypeName typeName = literal.getTypeName();
// Intercept DATE, TIME, TIMESTAMP types to return (Offset, Local)(Date, Time) instead of Calendar class
Object value = (DATETIME_TYPES.contains(typeName) && typeName != SqlTypeName.TIME_WITH_LOCAL_TIME_ZONE) ? createDateTimeValue(typeName, literal) : literal.getValue();
return literal0(node.getType().getSqlTypeName(), value);
}
use of org.apache.calcite.sql.type.SqlTypeName in project apex-malhar by apache.
the class OperatorUtils method getFieldName.
/**
* This method gives field name for POJO class for given {@link RelDataTypeField} object.
*
* @param field field object that needs to be converted to POJO class field name
* @return Return field name from POJO class
*/
public static String getFieldName(RelDataTypeField field) {
SqlTypeName sqlTypeName = field.getType().getSqlTypeName();
String name = getValidFieldName(field);
name = (sqlTypeName == SqlTypeName.TIMESTAMP) ? (name + "Ms") : ((sqlTypeName == SqlTypeName.DATE) ? (name + "Sec") : name);
return name;
}
use of org.apache.calcite.sql.type.SqlTypeName in project beam by apache.
the class BeamSqlUnparseContext method toSql.
@Override
public SqlNode toSql(RexProgram program, RexNode rex) {
if (rex.getKind().equals(SqlKind.LITERAL)) {
final RexLiteral literal = (RexLiteral) rex;
SqlTypeName name = literal.getTypeName();
SqlTypeFamily family = name.getFamily();
if (SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE.equals(name)) {
TimestampString timestampString = literal.getValueAs(TimestampString.class);
return new SqlDateTimeLiteral(timestampString, POS);
} else if (SqlTypeFamily.BINARY.equals(family)) {
ByteString byteString = literal.getValueAs(ByteString.class);
BitString bitString = BitString.createFromHexString(byteString.toString(16));
return new SqlByteStringLiteral(bitString, POS);
} else if (SqlTypeFamily.CHARACTER.equals(family)) {
String escaped = ESCAPE_FOR_ZETA_SQL.translate(literal.getValueAs(String.class));
return SqlLiteral.createCharString(escaped, POS);
} else if (SqlTypeName.SYMBOL.equals(literal.getTypeName())) {
Enum symbol = literal.getValueAs(Enum.class);
if (TimeUnitRange.DOW.equals(symbol)) {
return new ReplaceLiteral(literal, POS, "DAYOFWEEK");
} else if (TimeUnitRange.DOY.equals(symbol)) {
return new ReplaceLiteral(literal, POS, "DAYOFYEAR");
} else if (TimeUnitRange.WEEK.equals(symbol)) {
return new ReplaceLiteral(literal, POS, "ISOWEEK");
}
}
} else if (rex.getKind().equals(SqlKind.DYNAMIC_PARAM)) {
final RexDynamicParam param = (RexDynamicParam) rex;
final int index = param.getIndex();
final String name = "null_param_" + index;
nullParams.put(name, param.getType());
return new NamedDynamicParam(index, POS, name);
} else if (SqlKind.SEARCH.equals(rex.getKind())) {
// Workaround CALCITE-4716
RexCall search = (RexCall) rex;
RexLocalRef ref = (RexLocalRef) search.operands.get(1);
RexLiteral literal = (RexLiteral) program.getExprList().get(ref.getIndex());
rex = search.clone(search.getType(), ImmutableList.of(search.operands.get(0), literal));
}
return super.toSql(program, rex);
}
Aggregations