Java Code Examples for org.apache.calcite.runtime.SqlFunctions

The following examples show how to use org.apache.calcite.runtime.SqlFunctions. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: flink   Source File: FlinkAggregateRemoveRule.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean matches(RelOptRuleCall call) {
	final Aggregate aggregate = call.rel(0);
	final RelNode input = call.rel(1);
	if (aggregate.getGroupCount() == 0 || aggregate.indicator ||
			aggregate.getGroupType() != Aggregate.Group.SIMPLE) {
		return false;
	}
	for (AggregateCall aggCall : aggregate.getAggCallList()) {
		SqlKind aggCallKind = aggCall.getAggregation().getKind();
		// TODO supports more AggregateCalls
		boolean isAllowAggCall = aggCallKind == SqlKind.SUM ||
				aggCallKind == SqlKind.MIN ||
				aggCallKind == SqlKind.MAX ||
				aggCall.getAggregation() instanceof SqlAuxiliaryGroupAggFunction;
		if (!isAllowAggCall || aggCall.filterArg >= 0 || aggCall.getArgList().size() != 1) {
			return false;
		}
	}

	final RelMetadataQuery mq = call.getMetadataQuery();
	return SqlFunctions.isTrue(mq.areColumnsUnique(input, aggregate.getGroupSet()));
}
 
Example 2
Source Project: flink   Source File: FlinkAggregateRemoveRule.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean matches(RelOptRuleCall call) {
	final Aggregate aggregate = call.rel(0);
	final RelNode input = call.rel(1);
	if (aggregate.getGroupCount() == 0 || aggregate.indicator ||
			aggregate.getGroupType() != Aggregate.Group.SIMPLE) {
		return false;
	}
	for (AggregateCall aggCall : aggregate.getAggCallList()) {
		SqlKind aggCallKind = aggCall.getAggregation().getKind();
		// TODO supports more AggregateCalls
		boolean isAllowAggCall = aggCallKind == SqlKind.SUM ||
				aggCallKind == SqlKind.MIN ||
				aggCallKind == SqlKind.MAX ||
				aggCall.getAggregation() instanceof SqlAuxiliaryGroupAggFunction;
		if (!isAllowAggCall || aggCall.filterArg >= 0 || aggCall.getArgList().size() != 1) {
			return false;
		}
	}

	final RelMetadataQuery mq = call.getMetadataQuery();
	return SqlFunctions.isTrue(mq.areColumnsUnique(input, aggregate.getGroupSet()));
}
 
Example 3
Source Project: calcite   Source File: DateRangeRules.java    License: Apache License 2.0 6 votes vote down vote up
private Calendar timestampValue(RexLiteral timeLiteral) {
  switch (timeLiteral.getTypeName()) {
  case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
    final TimeZone tz = TimeZone.getTimeZone(this.timeZone);
    return Util.calendar(
        SqlFunctions.timestampWithLocalTimeZoneToTimestamp(
            timeLiteral.getValueAs(Long.class), tz));
  case TIMESTAMP:
    return Util.calendar(timeLiteral.getValueAs(Long.class));
  case DATE:
    // Cast date to timestamp with local time zone
    final DateString d = timeLiteral.getValueAs(DateString.class);
    return Util.calendar(d.getMillisSinceEpoch());
  default:
    throw Util.unexpected(timeLiteral.getTypeName());
  }
}
 
Example 4
Source Project: calcite   Source File: UtilTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test void testFlatListProduct() {
  final List<Enumerator<List<String>>> list = new ArrayList<>();
  list.add(Linq4j.enumerator(l2(l1("a"), l1("b"))));
  list.add(Linq4j.enumerator(l3(l2("x", "p"), l2("y", "q"), l2("z", "r"))));
  final Enumerable<FlatLists.ComparableList<String>> product =
      SqlFunctions.product(list, 3, false);
  int n = 0;
  FlatLists.ComparableList<String> previous = FlatLists.of();
  for (FlatLists.ComparableList<String> strings : product) {
    if (n++ == 1) {
      assertThat(strings.size(), is(3));
      assertThat(strings.get(0), is("a"));
      assertThat(strings.get(1), is("y"));
      assertThat(strings.get(2), is("q"));
    }
    if (previous != null) {
      assertTrue(previous.compareTo(strings) < 0);
    }
    previous = strings;
  }
  assertThat(n, is(6));
}
 
Example 5
Source Project: calcite   Source File: SqlFunctionsTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test void testTruncateInt() {
  assertThat(SqlFunctions.truncate(12345, 1000), is(12000));
  assertThat(SqlFunctions.truncate(12000, 1000), is(12000));
  assertThat(SqlFunctions.truncate(12001, 1000), is(12000));
  assertThat(SqlFunctions.truncate(11999, 1000), is(11000));

  assertThat(SqlFunctions.truncate(-12345, 1000), is(-13000));
  assertThat(SqlFunctions.truncate(-12000, 1000), is(-12000));
  assertThat(SqlFunctions.truncate(-12001, 1000), is(-13000));
  assertThat(SqlFunctions.truncate(-11999, 1000), is(-12000));

  assertThat(SqlFunctions.round(12345, 1000), is(12000));
  assertThat(SqlFunctions.round(12845, 1000), is(13000));
  assertThat(SqlFunctions.round(-12345, 1000), is(-12000));
  assertThat(SqlFunctions.round(-12845, 1000), is(-13000));
}
 
Example 6
Source Project: calcite   Source File: SqlFunctionsTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test void testSTruncateDouble() {
  assertThat(SqlFunctions.struncate(12.345d, 3), within(12.345d, 0.001));
  assertThat(SqlFunctions.struncate(12.345d, 2), within(12.340d, 0.001));
  assertThat(SqlFunctions.struncate(12.345d, 1), within(12.300d, 0.001));
  assertThat(SqlFunctions.struncate(12.999d, 0), within(12.000d, 0.001));

  assertThat(SqlFunctions.struncate(-12.345d, 3), within(-12.345d, 0.001));
  assertThat(SqlFunctions.struncate(-12.345d, 2), within(-12.340d, 0.001));
  assertThat(SqlFunctions.struncate(-12.345d, 1), within(-12.300d, 0.001));
  assertThat(SqlFunctions.struncate(-12.999d, 0), within(-12.000d, 0.001));

  assertThat(SqlFunctions.struncate(12345d, -3), within(12000d, 0.001));
  assertThat(SqlFunctions.struncate(12000d, -3), within(12000d, 0.001));
  assertThat(SqlFunctions.struncate(12001d, -3), within(12000d, 0.001));
  assertThat(SqlFunctions.struncate(12000d, -4), within(10000d, 0.001));
  assertThat(SqlFunctions.struncate(12000d, -5), within(0d, 0.001));
  assertThat(SqlFunctions.struncate(11999d, -3), within(11000d, 0.001));

  assertThat(SqlFunctions.struncate(-12345d, -3), within(-12000d, 0.001));
  assertThat(SqlFunctions.struncate(-12000d, -3), within(-12000d, 0.001));
  assertThat(SqlFunctions.struncate(-11999d, -3), within(-11000d, 0.001));
  assertThat(SqlFunctions.struncate(-12000d, -4), within(-10000d, 0.001));
  assertThat(SqlFunctions.struncate(-12000d, -5), within(0d, 0.001));
}
 
Example 7
Source Project: calcite   Source File: SqlFunctionsTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test void testSRoundLong() {
  assertThat(SqlFunctions.sround(12345L, -1), within(12350d, 0.001));
  assertThat(SqlFunctions.sround(12345L, -2), within(12300d, 0.001));
  assertThat(SqlFunctions.sround(12345L, -3), within(12000d, 0.001));
  assertThat(SqlFunctions.sround(12000L, -3), within(12000d, 0.001));
  assertThat(SqlFunctions.sround(12001L, -3), within(12000d, 0.001));
  assertThat(SqlFunctions.sround(12000L, -4), within(10000d, 0.001));
  assertThat(SqlFunctions.sround(12000L, -5), within(0d, 0.001));
  assertThat(SqlFunctions.sround(11999L, -3), within(12000d, 0.001));

  assertThat(SqlFunctions.sround(-12345L, -1), within(-12350d, 0.001));
  assertThat(SqlFunctions.sround(-12345L, -2), within(-12300d, 0.001));
  assertThat(SqlFunctions.sround(-12345L, -3), within(-12000d, 0.001));
  assertThat(SqlFunctions.sround(-12000L, -3), within(-12000d, 0.001));
  assertThat(SqlFunctions.sround(-11999L, -3), within(-12000d, 0.001));
  assertThat(SqlFunctions.sround(-12000L, -4), within(-10000d, 0.001));
  assertThat(SqlFunctions.sround(-12000L, -5), within(0d, 0.001));
}
 
Example 8
Source Project: calcite   Source File: SqlFunctionsTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test void testSRoundInt() {
  assertThat(SqlFunctions.sround(12345, -1), within(12350d, 0.001));
  assertThat(SqlFunctions.sround(12345, -2), within(12300d, 0.001));
  assertThat(SqlFunctions.sround(12345, -3), within(12000d, 0.001));
  assertThat(SqlFunctions.sround(12000, -3), within(12000d, 0.001));
  assertThat(SqlFunctions.sround(12001, -3), within(12000d, 0.001));
  assertThat(SqlFunctions.sround(12000, -4), within(10000d, 0.001));
  assertThat(SqlFunctions.sround(12000, -5), within(0d, 0.001));
  assertThat(SqlFunctions.sround(11999, -3), within(12000d, 0.001));

  assertThat(SqlFunctions.sround(-12345, -1), within(-12350d, 0.001));
  assertThat(SqlFunctions.sround(-12345, -2), within(-12300d, 0.001));
  assertThat(SqlFunctions.sround(-12345, -3), within(-12000d, 0.001));
  assertThat(SqlFunctions.sround(-12000, -3), within(-12000d, 0.001));
  assertThat(SqlFunctions.sround(-11999, -3), within(-12000d, 0.001));
  assertThat(SqlFunctions.sround(-12000, -4), within(-10000d, 0.001));
  assertThat(SqlFunctions.sround(-12000, -5), within(0d, 0.001));
}
 
Example 9
Source Project: calcite   Source File: SqlFunctionsTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test void testEqWithAny() {
  // Non-numeric same type equality check
  assertThat(SqlFunctions.eqAny("hello", "hello"), is(true));

  // Numeric types equality check
  assertThat(SqlFunctions.eqAny(1, 1L), is(true));
  assertThat(SqlFunctions.eqAny(1, 1.0D), is(true));
  assertThat(SqlFunctions.eqAny(1L, 1.0D), is(true));
  assertThat(SqlFunctions.eqAny(new BigDecimal(1L), 1), is(true));
  assertThat(SqlFunctions.eqAny(new BigDecimal(1L), 1L), is(true));
  assertThat(SqlFunctions.eqAny(new BigDecimal(1L), 1.0D), is(true));
  assertThat(SqlFunctions.eqAny(new BigDecimal(1L), new BigDecimal(1.0D)),
      is(true));

  // Non-numeric different type equality check
  assertThat(SqlFunctions.eqAny("2", 2), is(false));
}
 
Example 10
Source Project: calcite   Source File: SqlFunctionsTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test void testNeWithAny() {
  // Non-numeric same type inequality check
  assertThat(SqlFunctions.neAny("hello", "world"), is(true));

  // Numeric types inequality check
  assertThat(SqlFunctions.neAny(1, 2L), is(true));
  assertThat(SqlFunctions.neAny(1, 2.0D), is(true));
  assertThat(SqlFunctions.neAny(1L, 2.0D), is(true));
  assertThat(SqlFunctions.neAny(new BigDecimal(2L), 1), is(true));
  assertThat(SqlFunctions.neAny(new BigDecimal(2L), 1L), is(true));
  assertThat(SqlFunctions.neAny(new BigDecimal(2L), 1.0D), is(true));
  assertThat(SqlFunctions.neAny(new BigDecimal(2L), new BigDecimal(1.0D)),
      is(true));

  // Non-numeric different type inequality check
  assertThat(SqlFunctions.neAny("2", 2), is(true));
}
 
Example 11
Source Project: calcite   Source File: SqlFunctionsTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test void testLtWithAny() {
  // Non-numeric same type "less then" check
  assertThat(SqlFunctions.ltAny("apple", "banana"), is(true));

  // Numeric types "less than" check
  assertThat(SqlFunctions.ltAny(1, 2L), is(true));
  assertThat(SqlFunctions.ltAny(1, 2.0D), is(true));
  assertThat(SqlFunctions.ltAny(1L, 2.0D), is(true));
  assertThat(SqlFunctions.ltAny(new BigDecimal(1L), 2), is(true));
  assertThat(SqlFunctions.ltAny(new BigDecimal(1L), 2L), is(true));
  assertThat(SqlFunctions.ltAny(new BigDecimal(1L), 2.0D), is(true));
  assertThat(SqlFunctions.ltAny(new BigDecimal(1L), new BigDecimal(2.0D)),
      is(true));

  // Non-numeric different type but both implements Comparable
  // "less than" check
  try {
    assertThat(SqlFunctions.ltAny("1", 2L), is(false));
    fail("'lt' on non-numeric different type is not possible");
  } catch (CalciteException e) {
    assertThat(e.getMessage(),
        is("Invalid types for comparison: class java.lang.String < "
            + "class java.lang.Long"));
  }
}
 
Example 12
Source Project: calcite   Source File: SqlFunctionsTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test void testGtWithAny() {
  // Non-numeric same type "greater then" check
  assertThat(SqlFunctions.gtAny("banana", "apple"), is(true));

  // Numeric types "greater than" check
  assertThat(SqlFunctions.gtAny(2, 1L), is(true));
  assertThat(SqlFunctions.gtAny(2, 1.0D), is(true));
  assertThat(SqlFunctions.gtAny(2L, 1.0D), is(true));
  assertThat(SqlFunctions.gtAny(new BigDecimal(2L), 1), is(true));
  assertThat(SqlFunctions.gtAny(new BigDecimal(2L), 1L), is(true));
  assertThat(SqlFunctions.gtAny(new BigDecimal(2L), 1.0D), is(true));
  assertThat(SqlFunctions.gtAny(new BigDecimal(2L), new BigDecimal(1.0D)),
      is(true));

  // Non-numeric different type but both implements Comparable
  // "greater than" check
  try {
    assertThat(SqlFunctions.gtAny("2", 1L), is(false));
    fail("'gt' on non-numeric different type is not possible");
  } catch (CalciteException e) {
    assertThat(e.getMessage(),
        is("Invalid types for comparison: class java.lang.String > "
            + "class java.lang.Long"));
  }
}
 
Example 13
Source Project: marble   Source File: HiveTableEnvTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test public void testCurrentDate() {
  executeSql("select current_date");
  Assert.assertEquals(
      SqlFunctions.toInt(java.sql.Date.valueOf(
          SessionState.get()
              .getQueryCurrentTimestamp()
              .toString()
              .substring(0, 10)), TimeZone.getTimeZone("UTC")),
      executeSql("select current_date as c1").get(0)
          .get("c1"));
}
 
Example 14
Source Project: marble   Source File: TypeConvertUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static Integer toInteger(Object value) {
  if (value == null) {
    return null;
  }
  if (value instanceof Boolean) {
    return ((Boolean) value) ? 1 : 0;
  }
  return SqlFunctions.toInt(value);
}
 
Example 15
Source Project: marble   Source File: TypeConvertUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static Boolean toBoolean(Object value) {
  if (value == null) {
    return null;
  } else {
    return SqlFunctions.toBoolean(value);
  }
}
 
Example 16
Source Project: calcite   Source File: RexImpTable.java    License: Apache License 2.0 5 votes vote down vote up
private Expression call(Expression operand, Type type,
    TimeUnit timeUnit) {
  return Expressions.call(SqlFunctions.class, methodName,
      EnumUtils.convert(operand, type),
      EnumUtils.convert(
          Expressions.constant(timeUnit.multiplier), type));
}
 
Example 17
Source Project: calcite   Source File: RexImpTable.java    License: Apache License 2.0 5 votes vote down vote up
@Override Expression implementSafe(RexToLixTranslator translator,
    RexCall call, List<Expression> argValueList) {
  return EnumUtils.call(
      SqlFunctions.class,
      methodName,
      argValueList);
}
 
Example 18
Source Project: calcite   Source File: RexImpTable.java    License: Apache License 2.0 5 votes vote down vote up
private Expression callBackupMethodAnyType(RexToLixTranslator translator,
    RexCall call, List<Expression> expressions) {
  final String backupMethodNameForAnyType =
      backupMethodName + METHOD_POSTFIX_FOR_ANY_TYPE;

  // one or both of parameter(s) is(are) ANY type
  final Expression expression0 = maybeBox(expressions.get(0));
  final Expression expression1 = maybeBox(expressions.get(1));
  return Expressions.call(SqlFunctions.class, backupMethodNameForAnyType,
      expression0, expression1);
}
 
Example 19
Source Project: calcite   Source File: NlsString.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns a string the same as this but with spaces trimmed from the
 * right.
 */
public NlsString rtrim() {
  String trimmed = SqlFunctions.rtrim(getValue());
  if (!trimmed.equals(getValue())) {
    return new NlsString(trimmed, charsetName, collation);
  }
  return this;
}
 
Example 20
Source Project: calcite   Source File: EnumUtilsTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test void testMethodCallExpression() {
  // test for Object.class method parameter type
  final ConstantExpression arg0 = Expressions.constant(1, int.class);
  final ConstantExpression arg1 = Expressions.constant("x", String.class);
  final MethodCallExpression arrayMethodCall = EnumUtils.call(SqlFunctions.class,
      BuiltInMethod.ARRAY.getMethodName(), Arrays.asList(arg0, arg1));
  assertThat(Expressions.toString(arrayMethodCall),
      is("org.apache.calcite.runtime.SqlFunctions.array(1, \"x\")"));

  // test for Object.class argument type
  final ConstantExpression nullLiteral = Expressions.constant(null);
  final MethodCallExpression xmlExtractMethodCall = EnumUtils.call(
      XmlFunctions.class, BuiltInMethod.EXTRACT_VALUE.getMethodName(),
      Arrays.asList(arg1, nullLiteral));
  assertThat(Expressions.toString(xmlExtractMethodCall),
      is("org.apache.calcite.runtime.XmlFunctions.extractValue(\"x\", (String) null)"));

  // test "mod(decimal, long)" match to "mod(decimal, decimal)"
  final ConstantExpression arg2 = Expressions.constant(12.5, BigDecimal.class);
  final ConstantExpression arg3 = Expressions.constant(3, long.class);
  final MethodCallExpression modMethodCall = EnumUtils.call(
      SqlFunctions.class, "mod", Arrays.asList(arg2, arg3));
  assertThat(Expressions.toString(modMethodCall),
      is("org.apache.calcite.runtime.SqlFunctions.mod("
          + "java.math.BigDecimal.valueOf(125L, 1), "
          + "new java.math.BigDecimal(\n  3L))"));

  // test "ST_MakePoint(int, int)" match to "ST_MakePoint(decimal, decimal)"
  final ConstantExpression arg4 = Expressions.constant(1, int.class);
  final ConstantExpression arg5 = Expressions.constant(2, int.class);
  final MethodCallExpression geoMethodCall = EnumUtils.call(
      GeoFunctions.class, "ST_MakePoint", Arrays.asList(arg4, arg5));
  assertThat(Expressions.toString(geoMethodCall),
      is("org.apache.calcite.runtime.GeoFunctions.ST_MakePoint("
          + "new java.math.BigDecimal(\n  1), "
          + "new java.math.BigDecimal(\n  2))"));
}
 
Example 21
Source Project: calcite   Source File: RelMetadataTest.java    License: Apache License 2.0 5 votes vote down vote up
/** Asserts that {@link RelMetadataQuery#getUniqueKeys(RelNode)}
 * and {@link RelMetadataQuery#areColumnsUnique(RelNode, ImmutableBitSet)}
 * return consistent results. */
private void assertUniqueConsistent(RelNode rel) {
  final RelMetadataQuery mq = rel.getCluster().getMetadataQuery();
  final Set<ImmutableBitSet> uniqueKeys = mq.getUniqueKeys(rel);
  final ImmutableBitSet allCols =
      ImmutableBitSet.range(0, rel.getRowType().getFieldCount());
  for (ImmutableBitSet key : allCols.powerSet()) {
    Boolean result2 = mq.areColumnsUnique(rel, key);
    assertEquals(isUnique(uniqueKeys, key), SqlFunctions.isTrue(result2),
        () -> "areColumnsUnique. key: " + key + ", uniqueKeys: " + uniqueKeys
            + ", rel: " + RelOptUtil.toString(rel));
  }
}
 
Example 22
Source Project: calcite   Source File: SqlFunctionsTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test void testToString() {
  assertThat(SqlFunctions.toString(0f), is("0E0"));
  assertThat(SqlFunctions.toString(1f), is("1"));
  assertThat(SqlFunctions.toString(1.5f), is("1.5"));
  assertThat(SqlFunctions.toString(-1.5f), is("-1.5"));
  assertThat(SqlFunctions.toString(1.5e8f), is("1.5E8"));
  assertThat(SqlFunctions.toString(-0.0625f), is("-0.0625"));
  assertThat(SqlFunctions.toString(0.0625f), is("0.0625"));
  assertThat(SqlFunctions.toString(-5e-12f), is("-5E-12"));

  assertThat(SqlFunctions.toString(0d), is("0E0"));
  assertThat(SqlFunctions.toString(1d), is("1"));
  assertThat(SqlFunctions.toString(1.5d), is("1.5"));
  assertThat(SqlFunctions.toString(-1.5d), is("-1.5"));
  assertThat(SqlFunctions.toString(1.5e8d), is("1.5E8"));
  assertThat(SqlFunctions.toString(-0.0625d), is("-0.0625"));
  assertThat(SqlFunctions.toString(0.0625d), is("0.0625"));
  assertThat(SqlFunctions.toString(-5e-12d), is("-5E-12"));

  assertThat(SqlFunctions.toString(new BigDecimal("0")), is("0"));
  assertThat(SqlFunctions.toString(new BigDecimal("1")), is("1"));
  assertThat(SqlFunctions.toString(new BigDecimal("1.5")), is("1.5"));
  assertThat(SqlFunctions.toString(new BigDecimal("-1.5")), is("-1.5"));
  assertThat(SqlFunctions.toString(new BigDecimal("1.5e8")), is("1.5E+8"));
  assertThat(SqlFunctions.toString(new BigDecimal("-0.0625")), is("-.0625"));
  assertThat(SqlFunctions.toString(new BigDecimal("0.0625")), is(".0625"));
  assertThat(SqlFunctions.toString(new BigDecimal("-5e-12")), is("-5E-12"));
}
 
Example 23
Source Project: calcite   Source File: SqlFunctionsTest.java    License: Apache License 2.0 5 votes vote down vote up
private void checkFloor(int x, int y, int result) {
  assertThat(SqlFunctions.floor(x, y), is(result));
  assertThat(SqlFunctions.floor((long) x, (long) y), is((long) result));
  assertThat(SqlFunctions.floor((short) x, (short) y), is((short) result));
  assertThat(SqlFunctions.floor((byte) x, (byte) y), is((byte) result));
  assertThat(
      SqlFunctions.floor(BigDecimal.valueOf(x), BigDecimal.valueOf(y)),
      is(BigDecimal.valueOf(result)));
}
 
Example 24
Source Project: calcite   Source File: SqlFunctionsTest.java    License: Apache License 2.0 5 votes vote down vote up
private void checkCeil(int x, int y, int result) {
  assertThat(SqlFunctions.ceil(x, y), is(result));
  assertThat(SqlFunctions.ceil((long) x, (long) y), is((long) result));
  assertThat(SqlFunctions.ceil((short) x, (short) y), is((short) result));
  assertThat(SqlFunctions.ceil((byte) x, (byte) y), is((byte) result));
  assertThat(
      SqlFunctions.ceil(BigDecimal.valueOf(x), BigDecimal.valueOf(y)),
      is(BigDecimal.valueOf(result)));
}
 
Example 25
Source Project: calcite   Source File: SqlFunctionsTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test void testTruncateLong() {
  assertThat(SqlFunctions.truncate(12345L, 1000L), is(12000L));
  assertThat(SqlFunctions.truncate(12000L, 1000L), is(12000L));
  assertThat(SqlFunctions.truncate(12001L, 1000L), is(12000L));
  assertThat(SqlFunctions.truncate(11999L, 1000L), is(11000L));

  assertThat(SqlFunctions.truncate(-12345L, 1000L), is(-13000L));
  assertThat(SqlFunctions.truncate(-12000L, 1000L), is(-12000L));
  assertThat(SqlFunctions.truncate(-12001L, 1000L), is(-13000L));
  assertThat(SqlFunctions.truncate(-11999L, 1000L), is(-12000L));
}
 
Example 26
Source Project: calcite   Source File: SqlFunctionsTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test void testSTruncateLong() {
  assertThat(SqlFunctions.struncate(12345L, -3), within(12000d, 0.001));
  assertThat(SqlFunctions.struncate(12000L, -3), within(12000d, 0.001));
  assertThat(SqlFunctions.struncate(12001L, -3), within(12000d, 0.001));
  assertThat(SqlFunctions.struncate(12000L, -4), within(10000d, 0.001));
  assertThat(SqlFunctions.struncate(12000L, -5), within(0d, 0.001));
  assertThat(SqlFunctions.struncate(11999L, -3), within(11000d, 0.001));

  assertThat(SqlFunctions.struncate(-12345L, -3), within(-12000d, 0.001));
  assertThat(SqlFunctions.struncate(-12000L, -3), within(-12000d, 0.001));
  assertThat(SqlFunctions.struncate(-11999L, -3), within(-11000d, 0.001));
  assertThat(SqlFunctions.struncate(-12000L, -4), within(-10000d, 0.001));
  assertThat(SqlFunctions.struncate(-12000L, -5), within(0d, 0.001));
}
 
Example 27
Source Project: calcite   Source File: SqlFunctionsTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test void testSTruncateInt() {
  assertThat(SqlFunctions.struncate(12345, -3), within(12000d, 0.001));
  assertThat(SqlFunctions.struncate(12000, -3), within(12000d, 0.001));
  assertThat(SqlFunctions.struncate(12001, -3), within(12000d, 0.001));
  assertThat(SqlFunctions.struncate(12000, -4), within(10000d, 0.001));
  assertThat(SqlFunctions.struncate(12000, -5), within(0d, 0.001));
  assertThat(SqlFunctions.struncate(11999, -3), within(11000d, 0.001));

  assertThat(SqlFunctions.struncate(-12345, -3), within(-12000d, 0.001));
  assertThat(SqlFunctions.struncate(-12000, -3), within(-12000d, 0.001));
  assertThat(SqlFunctions.struncate(-11999, -3), within(-11000d, 0.001));
  assertThat(SqlFunctions.struncate(-12000, -4), within(-10000d, 0.001));
  assertThat(SqlFunctions.struncate(-12000, -5), within(0d, 0.001));
}
 
Example 28
Source Project: calcite   Source File: SqlFunctionsTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test void testSRoundDouble() {
  assertThat(SqlFunctions.sround(12.345d, 3), within(12.345d, 0.001));
  assertThat(SqlFunctions.sround(12.345d, 2), within(12.350d, 0.001));
  assertThat(SqlFunctions.sround(12.345d, 1), within(12.300d, 0.001));
  assertThat(SqlFunctions.sround(12.999d, 2), within(13.000d, 0.001));
  assertThat(SqlFunctions.sround(12.999d, 1), within(13.000d, 0.001));
  assertThat(SqlFunctions.sround(12.999d, 0), within(13.000d, 0.001));

  assertThat(SqlFunctions.sround(-12.345d, 3), within(-12.345d, 0.001));
  assertThat(SqlFunctions.sround(-12.345d, 2), within(-12.350d, 0.001));
  assertThat(SqlFunctions.sround(-12.345d, 1), within(-12.300d, 0.001));
  assertThat(SqlFunctions.sround(-12.999d, 2), within(-13.000d, 0.001));
  assertThat(SqlFunctions.sround(-12.999d, 1), within(-13.000d, 0.001));
  assertThat(SqlFunctions.sround(-12.999d, 0), within(-13.000d, 0.001));

  assertThat(SqlFunctions.sround(12345d, -1), within(12350d, 0.001));
  assertThat(SqlFunctions.sround(12345d, -2), within(12300d, 0.001));
  assertThat(SqlFunctions.sround(12345d, -3), within(12000d, 0.001));
  assertThat(SqlFunctions.sround(12000d, -3), within(12000d, 0.001));
  assertThat(SqlFunctions.sround(12001d, -3), within(12000d, 0.001));
  assertThat(SqlFunctions.sround(12000d, -4), within(10000d, 0.001));
  assertThat(SqlFunctions.sround(12000d, -5), within(0d, 0.001));
  assertThat(SqlFunctions.sround(11999d, -3), within(12000d, 0.001));

  assertThat(SqlFunctions.sround(-12345d, -1), within(-12350d, 0.001));
  assertThat(SqlFunctions.sround(-12345d, -2), within(-12300d, 0.001));
  assertThat(SqlFunctions.sround(-12345d, -3), within(-12000d, 0.001));
  assertThat(SqlFunctions.sround(-12000d, -3), within(-12000d, 0.001));
  assertThat(SqlFunctions.sround(-11999d, -3), within(-12000d, 0.001));
  assertThat(SqlFunctions.sround(-12000d, -4), within(-10000d, 0.001));
  assertThat(SqlFunctions.sround(-12000d, -5), within(0d, 0.001));
}
 
Example 29
Source Project: calcite   Source File: SqlFunctionsTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test void testLeWithAny() {
  // Non-numeric same type "less or equal" check
  assertThat(SqlFunctions.leAny("apple", "banana"), is(true));
  assertThat(SqlFunctions.leAny("apple", "apple"), is(true));

  // Numeric types "less or equal" check
  assertThat(SqlFunctions.leAny(1, 2L), is(true));
  assertThat(SqlFunctions.leAny(1, 1L), is(true));
  assertThat(SqlFunctions.leAny(1, 2.0D), is(true));
  assertThat(SqlFunctions.leAny(1, 1.0D), is(true));
  assertThat(SqlFunctions.leAny(1L, 2.0D), is(true));
  assertThat(SqlFunctions.leAny(1L, 1.0D), is(true));
  assertThat(SqlFunctions.leAny(new BigDecimal(1L), 2), is(true));
  assertThat(SqlFunctions.leAny(new BigDecimal(1L), 1), is(true));
  assertThat(SqlFunctions.leAny(new BigDecimal(1L), 2L), is(true));
  assertThat(SqlFunctions.leAny(new BigDecimal(1L), 1L), is(true));
  assertThat(SqlFunctions.leAny(new BigDecimal(1L), 2.0D), is(true));
  assertThat(SqlFunctions.leAny(new BigDecimal(1L), 1.0D), is(true));
  assertThat(SqlFunctions.leAny(new BigDecimal(1L), new BigDecimal(2.0D)),
      is(true));
  assertThat(SqlFunctions.leAny(new BigDecimal(1L), new BigDecimal(1.0D)),
      is(true));

  // Non-numeric different type but both implements Comparable
  // "less or equal" check
  try {
    assertThat(SqlFunctions.leAny("2", 2L), is(false));
    fail("'le' on non-numeric different type is not possible");
  } catch (CalciteException e) {
    assertThat(e.getMessage(),
        is("Invalid types for comparison: class java.lang.String <= "
            + "class java.lang.Long"));
  }
}
 
Example 30
Source Project: calcite   Source File: SqlFunctionsTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test void testGeWithAny() {
  // Non-numeric same type "greater or equal" check
  assertThat(SqlFunctions.geAny("banana", "apple"), is(true));
  assertThat(SqlFunctions.geAny("apple", "apple"), is(true));

  // Numeric types "greater or equal" check
  assertThat(SqlFunctions.geAny(2, 1L), is(true));
  assertThat(SqlFunctions.geAny(1, 1L), is(true));
  assertThat(SqlFunctions.geAny(2, 1.0D), is(true));
  assertThat(SqlFunctions.geAny(1, 1.0D), is(true));
  assertThat(SqlFunctions.geAny(2L, 1.0D), is(true));
  assertThat(SqlFunctions.geAny(1L, 1.0D), is(true));
  assertThat(SqlFunctions.geAny(new BigDecimal(2L), 1), is(true));
  assertThat(SqlFunctions.geAny(new BigDecimal(1L), 1), is(true));
  assertThat(SqlFunctions.geAny(new BigDecimal(2L), 1L), is(true));
  assertThat(SqlFunctions.geAny(new BigDecimal(1L), 1L), is(true));
  assertThat(SqlFunctions.geAny(new BigDecimal(2L), 1.0D), is(true));
  assertThat(SqlFunctions.geAny(new BigDecimal(1L), 1.0D), is(true));
  assertThat(SqlFunctions.geAny(new BigDecimal(2L), new BigDecimal(1.0D)),
      is(true));
  assertThat(SqlFunctions.geAny(new BigDecimal(1L), new BigDecimal(1.0D)),
      is(true));

  // Non-numeric different type but both implements Comparable
  // "greater or equal" check
  try {
    assertThat(SqlFunctions.geAny("2", 2L), is(false));
    fail("'ge' on non-numeric different type is not possible");
  } catch (CalciteException e) {
    assertThat(e.getMessage(),
        is("Invalid types for comparison: class java.lang.String >= "
            + "class java.lang.Long"));
  }
}