use of org.apache.calcite.avatica.util.TimeUnit in project calcite by apache.
the class IntervalSqlType method combine.
/**
* Combines two IntervalTypes and returns the result. E.g. the result of
* combining<br>
* <code>INTERVAL DAY TO HOUR</code><br>
* with<br>
* <code>INTERVAL SECOND</code> is<br>
* <code>INTERVAL DAY TO SECOND</code>
*/
public IntervalSqlType combine(RelDataTypeFactoryImpl typeFactory, IntervalSqlType that) {
assert this.typeName.isYearMonth() == that.typeName.isYearMonth();
boolean nullable = isNullable || that.isNullable;
TimeUnit thisStart = Preconditions.checkNotNull(typeName.getStartUnit());
TimeUnit thisEnd = typeName.getEndUnit();
final TimeUnit thatStart = Preconditions.checkNotNull(that.typeName.getStartUnit());
final TimeUnit thatEnd = that.typeName.getEndUnit();
int secondPrec = this.intervalQualifier.getStartPrecisionPreservingDefault();
final int fracPrec = SqlIntervalQualifier.combineFractionalSecondPrecisionPreservingDefault(typeSystem, this.intervalQualifier, that.intervalQualifier);
if (thisStart.ordinal() > thatStart.ordinal()) {
thisEnd = thisStart;
thisStart = thatStart;
secondPrec = that.intervalQualifier.getStartPrecisionPreservingDefault();
} else if (thisStart.ordinal() == thatStart.ordinal()) {
secondPrec = SqlIntervalQualifier.combineStartPrecisionPreservingDefault(typeFactory.getTypeSystem(), this.intervalQualifier, that.intervalQualifier);
} else if (null == thisEnd || thisEnd.ordinal() < thatStart.ordinal()) {
thisEnd = thatStart;
}
if (null != thatEnd) {
if (null == thisEnd || thisEnd.ordinal() < thatEnd.ordinal()) {
thisEnd = thatEnd;
}
}
RelDataType intervalType = typeFactory.createSqlIntervalType(new SqlIntervalQualifier(thisStart, secondPrec, thisEnd, fracPrec, SqlParserPos.ZERO));
intervalType = typeFactory.createTypeWithNullability(intervalType, nullable);
return (IntervalSqlType) intervalType;
}
use of org.apache.calcite.avatica.util.TimeUnit in project calcite by apache.
the class RexLiteral method intervalString.
private String intervalString(BigDecimal v) {
final List<TimeUnit> timeUnits = getTimeUnits(type.getSqlTypeName());
final StringBuilder b = new StringBuilder();
for (TimeUnit timeUnit : timeUnits) {
final BigDecimal[] result = v.divideAndRemainder(timeUnit.multiplier);
if (b.length() > 0) {
b.append(timeUnit.separator);
}
// don't pad 1st
final int width = b.length() == 0 ? -1 : width(timeUnit);
pad(b, result[0].toString(), width);
v = result[1];
}
if (Util.last(timeUnits) == TimeUnit.MILLISECOND) {
while (b.toString().matches(".*\\.[0-9]*0")) {
if (b.toString().endsWith(".0")) {
// remove ".0"
b.setLength(b.length() - 2);
} else {
// remove "0"
b.setLength(b.length() - 1);
}
}
}
return b.toString();
}
use of org.apache.calcite.avatica.util.TimeUnit in project flink by apache.
the class RexLiteral method intervalString.
private String intervalString(BigDecimal v) {
final List<TimeUnit> timeUnits = getTimeUnits(type.getSqlTypeName());
final StringBuilder b = new StringBuilder();
for (TimeUnit timeUnit : timeUnits) {
final BigDecimal[] result = v.divideAndRemainder(timeUnit.multiplier);
if (b.length() > 0) {
b.append(timeUnit.separator);
}
// don't pad 1st
final int width = b.length() == 0 ? -1 : width(timeUnit);
pad(b, result[0].toString(), width);
v = result[1];
}
if (Util.last(timeUnits) == TimeUnit.MILLISECOND) {
while (b.toString().matches(".*\\.[0-9]*0")) {
if (b.toString().endsWith(".0")) {
// remove ".0"
b.setLength(b.length() - 2);
} else {
// remove "0"
b.setLength(b.length() - 1);
}
}
}
return b.toString();
}
use of org.apache.calcite.avatica.util.TimeUnit in project beam by apache.
the class ZetaSqlCalciteTranslationUtils method enumValueToRexNode.
// internal only, used for DateTimestampPart
private static RexNode enumValueToRexNode(Value value, RexBuilder rexBuilder) {
String enumDescriptorName = value.getType().asEnum().getDescriptor().getFullName();
if (!"zetasql.functions.DateTimestampPart".equals(enumDescriptorName)) {
throw new UnsupportedOperationException("Unknown ZetaSQL Enum type: " + enumDescriptorName);
}
TimeUnit timeUnit = TIME_UNIT_CASTING_MAP.get(value.getEnumValue());
if (timeUnit == null) {
throw new UnsupportedOperationException("Unknown ZetaSQL Enum value: " + value.getEnumName());
}
return rexBuilder.makeFlag(TimeUnitRange.of(timeUnit, null));
}
use of org.apache.calcite.avatica.util.TimeUnit in project druid by alibaba.
the class CalciteMySqlNodeVisitor method visit.
public boolean visit(SQLMethodInvokeExpr x) {
List<SQLExpr> arguments = x.getArguments();
List<SqlNode> argNodes = new ArrayList<SqlNode>(arguments.size());
long nameHashCode64 = x.methodNameHashCode64();
SqlOperator functionOperator = func(nameHashCode64);
String methodName = x.getMethodName();
if (functionOperator == null) {
if (nameHashCode64 == FnvHash.Constants.TRIM) {
functionOperator = SqlStdOperatorTable.TRIM;
if (arguments.size() == 1) {
SqlNode sqlNode = convertToSqlNode(arguments.get(0));
this.sqlNode = new com.alibaba.druid.support.calcite.CalciteSqlBasicCall(functionOperator, new SqlNode[] { SqlLiteral.createSymbol(SqlTrimFunction.Flag.BOTH, SqlParserPos.ZERO), SqlCharStringLiteral.createCharString(" ", SqlParserPos.ZERO), sqlNode }, SqlParserPos.ZERO, false, null);
return false;
}
} else {
functionOperator = new SqlUnresolvedFunction(new SqlIdentifier(methodName, SqlParserPos.ZERO), null, null, null, null, SqlFunctionCategory.USER_DEFINED_FUNCTION);
}
}
SqlLiteral functionQualifier = null;
for (SQLExpr exp : arguments) {
argNodes.add(convertToSqlNode(exp));
}
if ((nameHashCode64 == FnvHash.Constants.TIMESTAMPDIFF || nameHashCode64 == FnvHash.Constants.TIMESTAMPADD) && argNodes.size() > 0 && argNodes.get(0) instanceof SqlIdentifier) {
SqlIdentifier arg0 = (SqlIdentifier) argNodes.get(0);
TimeUnit timeUnit = TimeUnit.valueOf(arg0.toString().toUpperCase());
argNodes.set(0, SqlLiteral.createSymbol(timeUnit, SqlParserPos.ZERO));
}
this.sqlNode = new com.alibaba.druid.support.calcite.CalciteSqlBasicCall(functionOperator, SqlParserUtil.toNodeArray(argNodes), SqlParserPos.ZERO, false, functionQualifier);
return false;
}
Aggregations