use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperator in project calcite by apache.
the class RexProgramTest method testIsDeterministic.
@Test
public void testIsDeterministic() {
SqlOperator ndc = new SqlSpecialOperator("NDC", SqlKind.OTHER_FUNCTION, 0, false, ReturnTypes.BOOLEAN, null, null) {
@Override
public boolean isDeterministic() {
return false;
}
};
RexNode n = rexBuilder.makeCall(ndc);
assertFalse(RexUtil.isDeterministic(n));
assertEquals(0, RexUtil.retainDeterministic(RelOptUtil.conjunctions(n)).size());
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperator in project calcite by apache.
the class RexProgramTest method testSimplifyCaseNotNullableBoolean.
/**
* Unit test for
* <a href="https://issues.apache.org/jira/browse/CALCITE-1289">[CALCITE-1289]
* RexUtil.simplifyCase() should account for nullability</a>.
*/
@Test
public void testSimplifyCaseNotNullableBoolean() {
RexNode condition = eq(rexBuilder.makeInputRef(typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.VARCHAR), true), 0), rexBuilder.makeLiteral("S"));
RexCall caseNode = (RexCall) case_(condition, trueLiteral, falseLiteral);
RexCall result = (RexCall) simplify.simplify(caseNode);
assertThat(result.getType().isNullable(), is(false));
assertThat(result.getType().getSqlTypeName(), is(SqlTypeName.BOOLEAN));
assertThat(result.getOperator(), is((SqlOperator) SqlStdOperatorTable.CASE));
assertThat(result.getOperands().size(), is((Object) 3));
assertThat(result.getOperands().get(0), is(condition));
assertThat(result.getOperands().get(1), is((RexNode) trueLiteral));
assertThat(result.getOperands().get(2), is((RexNode) falseLiteral));
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperator in project calcite by apache.
the class DruidExpressions method toDruidExpression.
/**
* Translates Calcite rexNode to Druid Expression when possible
* @param rexNode rexNode to convert to a Druid Expression
* @param inputRowType input row type of the rexNode to translate
* @param druidRel Druid query
*
* @return Druid Expression or null when can not convert the RexNode
*/
@Nullable
public static String toDruidExpression(final RexNode rexNode, final RelDataType inputRowType, final DruidQuery druidRel) {
SqlKind kind = rexNode.getKind();
SqlTypeName sqlTypeName = rexNode.getType().getSqlTypeName();
if (kind == SqlKind.INPUT_REF) {
final RexInputRef ref = (RexInputRef) rexNode;
final String columnName = inputRowType.getFieldNames().get(ref.getIndex());
if (columnName == null) {
return null;
}
if (druidRel.getDruidTable().timestampFieldName.equals(columnName)) {
return DruidExpressions.fromColumn(DruidTable.DEFAULT_TIMESTAMP_COLUMN);
}
return DruidExpressions.fromColumn(columnName);
}
if (rexNode instanceof RexCall) {
final SqlOperator operator = ((RexCall) rexNode).getOperator();
final DruidSqlOperatorConverter conversion = druidRel.getOperatorConversionMap().get(operator);
if (conversion == null) {
// unknown operator can not translate
return null;
} else {
return conversion.toDruidExpression(rexNode, inputRowType, druidRel);
}
}
if (kind == SqlKind.LITERAL) {
// Translate literal.
if (RexLiteral.isNullLiteral(rexNode)) {
// case the filter/project might yield to unknown let Calcite deal with this for now
return null;
} else if (SqlTypeName.NUMERIC_TYPES.contains(sqlTypeName)) {
return DruidExpressions.numberLiteral((Number) RexLiteral.value(rexNode));
} else if (SqlTypeFamily.INTERVAL_DAY_TIME == sqlTypeName.getFamily()) {
// Calcite represents DAY-TIME intervals in milliseconds.
final long milliseconds = ((Number) RexLiteral.value(rexNode)).longValue();
return DruidExpressions.numberLiteral(milliseconds);
} else if (SqlTypeFamily.INTERVAL_YEAR_MONTH == sqlTypeName.getFamily()) {
// Calcite represents YEAR-MONTH intervals in months.
final long months = ((Number) RexLiteral.value(rexNode)).longValue();
return DruidExpressions.numberLiteral(months);
} else if (SqlTypeName.STRING_TYPES.contains(sqlTypeName)) {
return DruidExpressions.stringLiteral(RexLiteral.stringValue(rexNode));
} else if (SqlTypeName.TIMESTAMP == sqlTypeName || SqlTypeName.DATE == sqlTypeName || SqlTypeName.TIME_WITH_LOCAL_TIME_ZONE == sqlTypeName) {
return DruidExpressions.numberLiteral(DruidDateTimeUtils.literalValue(rexNode, TimeZone.getTimeZone(druidRel.getConnectionConfig().timeZone())).getMillisSinceEpoch());
} else if (SqlTypeName.BOOLEAN == sqlTypeName) {
return DruidExpressions.numberLiteral(RexLiteral.booleanValue(rexNode) ? 1 : 0);
}
}
// Not Literal/InputRef/RexCall or unknown type?
return null;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperator in project calcite by apache.
the class PlannerTest method testValidateUserDefinedAggregate.
@Test
public void testValidateUserDefinedAggregate() throws Exception {
final SqlStdOperatorTable stdOpTab = SqlStdOperatorTable.instance();
SqlOperatorTable opTab = ChainedSqlOperatorTable.of(stdOpTab, new ListSqlOperatorTable(ImmutableList.<SqlOperator>of(new MyCountAggFunction())));
final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
final FrameworkConfig config = Frameworks.newConfigBuilder().defaultSchema(CalciteAssert.addSchema(rootSchema, CalciteAssert.SchemaSpec.HR)).operatorTable(opTab).build();
final Planner planner = Frameworks.getPlanner(config);
SqlNode parse = planner.parse("select \"deptno\", my_count(\"empid\") from \"emps\"\n" + "group by \"deptno\"");
assertThat(Util.toLinux(parse.toString()), equalTo("SELECT `deptno`, `MY_COUNT`(`empid`)\n" + "FROM `emps`\n" + "GROUP BY `deptno`"));
// MY_COUNT is recognized as an aggregate function, and therefore it is OK
// that its argument empid is not in the GROUP BY clause.
SqlNode validate = planner.validate(parse);
assertThat(validate, notNullValue());
// The presence of an aggregate function in the SELECT clause causes it
// to become an aggregate query. Non-aggregate expressions become illegal.
planner.close();
planner.reset();
parse = planner.parse("select \"deptno\", count(1) from \"emps\"");
try {
validate = planner.validate(parse);
fail("expected exception, got " + validate);
} catch (ValidationException e) {
assertThat(e.getCause().getCause().getMessage(), containsString("Expression 'deptno' is not being grouped"));
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperator in project calcite by apache.
the class RexLiteral method findValue.
private static Comparable findValue(RexNode node) {
if (node instanceof RexLiteral) {
return ((RexLiteral) node).value;
}
if (node instanceof RexCall) {
final RexCall call = (RexCall) node;
final SqlOperator operator = call.getOperator();
if (operator == SqlStdOperatorTable.CAST) {
return findValue(call.getOperands().get(0));
}
if (operator == SqlStdOperatorTable.UNARY_MINUS) {
final BigDecimal value = (BigDecimal) findValue(call.getOperands().get(0));
return value.negate();
}
}
throw new AssertionError("not a literal: " + node);
}
Aggregations