Search in sources :

Example 26 with SqlOperator

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());
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator) SqlSpecialOperator(org.apache.calcite.sql.SqlSpecialOperator) RexNode(org.apache.calcite.rex.RexNode) Test(org.junit.Test)

Example 27 with SqlOperator

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));
}
Also used : RexCall(org.apache.calcite.rex.RexCall) SqlOperator(org.apache.calcite.sql.SqlOperator) RexNode(org.apache.calcite.rex.RexNode) Test(org.junit.Test)

Example 28 with SqlOperator

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;
}
Also used : RexCall(org.apache.calcite.rex.RexCall) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) SqlOperator(org.apache.calcite.sql.SqlOperator) RexInputRef(org.apache.calcite.rex.RexInputRef) SqlKind(org.apache.calcite.sql.SqlKind) Nullable(javax.annotation.Nullable)

Example 29 with SqlOperator

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"));
    }
}
Also used : ListSqlOperatorTable(org.apache.calcite.sql.util.ListSqlOperatorTable) SqlOperator(org.apache.calcite.sql.SqlOperator) ListSqlOperatorTable(org.apache.calcite.sql.util.ListSqlOperatorTable) ChainedSqlOperatorTable(org.apache.calcite.sql.util.ChainedSqlOperatorTable) SqlOperatorTable(org.apache.calcite.sql.SqlOperatorTable) SchemaPlus(org.apache.calcite.schema.SchemaPlus) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) SqlNode(org.apache.calcite.sql.SqlNode) Test(org.junit.Test)

Example 30 with SqlOperator

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);
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator) BigDecimal(java.math.BigDecimal)

Aggregations

SqlOperator (org.apache.calcite.sql.SqlOperator)129 ArrayList (java.util.ArrayList)44 RexNode (org.apache.calcite.rex.RexNode)41 RelDataType (org.apache.calcite.rel.type.RelDataType)25 SqlCall (org.apache.calcite.sql.SqlCall)24 RexCall (org.apache.calcite.rex.RexCall)21 SqlNode (org.apache.calcite.sql.SqlNode)21 SqlKind (org.apache.calcite.sql.SqlKind)15 List (java.util.List)13 SqlFunction (org.apache.calcite.sql.SqlFunction)13 RelNode (org.apache.calcite.rel.RelNode)12 RexBuilder (org.apache.calcite.rex.RexBuilder)11 RexInputRef (org.apache.calcite.rex.RexInputRef)11 Test (org.junit.Test)11 SqlBasicCall (org.apache.calcite.sql.SqlBasicCall)10 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)10 NlsString (org.apache.calcite.util.NlsString)10 RexNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode)9 SqlOperator (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperator)9 RexLiteral (org.apache.calcite.rex.RexLiteral)9