Search in sources :

Example 91 with SqlTypeName

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.SqlTypeName in project calcite by apache.

the class SqlTypeNameTest method testLongvarbinary.

@Test
public void testLongvarbinary() {
    SqlTypeName tn = SqlTypeName.getNameForJdbcType(Types.LONGVARBINARY);
    assertEquals("LONGVARBINARY did not map to null", null, tn);
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) Test(org.junit.Test)

Example 92 with SqlTypeName

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.SqlTypeName in project calcite by apache.

the class SqlTypeNameTest method testOther.

@Test
public void testOther() {
    SqlTypeName tn = SqlTypeName.getNameForJdbcType(Types.OTHER);
    assertEquals("OTHER did not map to null", null, tn);
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) Test(org.junit.Test)

Example 93 with SqlTypeName

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.SqlTypeName in project calcite by apache.

the class SqlTypeNameTest method testBinary.

@Test
public void testBinary() {
    SqlTypeName tn = SqlTypeName.getNameForJdbcType(Types.BINARY);
    assertEquals("BINARY did not map to BINARY", SqlTypeName.BINARY, tn);
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) Test(org.junit.Test)

Example 94 with SqlTypeName

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.SqlTypeName in project calcite by apache.

the class SqlTypeNameTest method testBoolean.

@Test
public void testBoolean() {
    SqlTypeName tn = SqlTypeName.getNameForJdbcType(Types.BOOLEAN);
    assertEquals("BOOLEAN did not map to BOOLEAN", SqlTypeName.BOOLEAN, tn);
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) Test(org.junit.Test)

Example 95 with SqlTypeName

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.SqlTypeName in project calcite by apache.

the class DruidQuery method computeDruidJsonAgg.

/**
 * Translates aggregate calls to Druid {@link JsonAggregation}s when
 * possible.
 *
 * @param aggCalls List of AggregateCalls to translate
 * @param aggNames List of aggregate names
 * @param project Input project under the aggregate calls,
 *               or null if we have {@link TableScan} immediately under the
 *               {@link Aggregate}
 * @param druidQuery Druid Query Rel
 *
 * @return List of valid Druid {@link JsonAggregation}s, or null if any of the
 * aggregates is not supported
 */
@Nullable
protected static List<JsonAggregation> computeDruidJsonAgg(List<AggregateCall> aggCalls, List<String> aggNames, @Nullable Project project, DruidQuery druidQuery) {
    final List<JsonAggregation> aggregations = new ArrayList<>();
    for (Pair<AggregateCall, String> agg : Pair.zip(aggCalls, aggNames)) {
        final String fieldName;
        final String expression;
        final AggregateCall aggCall = agg.left;
        final RexNode filterNode;
        // Type check First
        final RelDataType type = aggCall.getType();
        final SqlTypeName sqlTypeName = type.getSqlTypeName();
        final boolean isNotAcceptedType;
        if (SqlTypeFamily.APPROXIMATE_NUMERIC.getTypeNames().contains(sqlTypeName) || SqlTypeFamily.INTEGER.getTypeNames().contains(sqlTypeName)) {
            isNotAcceptedType = false;
        } else if (SqlTypeFamily.EXACT_NUMERIC.getTypeNames().contains(sqlTypeName) && (type.getScale() == 0 || druidQuery.getConnectionConfig().approximateDecimal())) {
            // Decimal, If scale is zero or we allow approximating decimal, we can proceed
            isNotAcceptedType = false;
        } else {
            isNotAcceptedType = true;
        }
        if (isNotAcceptedType) {
            return null;
        }
        // Extract filters
        if (project != null && aggCall.hasFilter()) {
            filterNode = project.getProjects().get(aggCall.filterArg);
        } else {
            filterNode = null;
        }
        if (aggCall.getArgList().size() == 0) {
            fieldName = null;
            expression = null;
        } else {
            int index = Iterables.getOnlyElement(aggCall.getArgList());
            if (project == null) {
                fieldName = druidQuery.table.getRowType().getFieldNames().get(index);
                expression = null;
            } else {
                final RexNode rexNode = project.getProjects().get(index);
                final RelDataType inputRowType = project.getInput().getRowType();
                if (rexNode.isA(SqlKind.INPUT_REF)) {
                    expression = null;
                    fieldName = extractColumnName(rexNode, inputRowType, druidQuery);
                } else {
                    expression = DruidExpressions.toDruidExpression(rexNode, inputRowType, druidQuery);
                    if (Strings.isNullOrEmpty(expression)) {
                        return null;
                    }
                    fieldName = null;
                }
            }
            // One should be not null and the other should be null.
            assert expression == null ^ fieldName == null;
        }
        final JsonAggregation jsonAggregation = getJsonAggregation(agg.right, agg.left, filterNode, fieldName, expression, druidQuery);
        if (jsonAggregation == null) {
            return null;
        }
        aggregations.add(jsonAggregation);
    }
    return aggregations;
}
Also used : AggregateCall(org.apache.calcite.rel.core.AggregateCall) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) RexNode(org.apache.calcite.rex.RexNode) Nullable(javax.annotation.Nullable)

Aggregations

SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)117 Test (org.junit.Test)38 RelDataType (org.apache.calcite.rel.type.RelDataType)28 RexNode (org.apache.calcite.rex.RexNode)18 BigDecimal (java.math.BigDecimal)13 ArrayList (java.util.ArrayList)13 List (java.util.List)11 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)9 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)8 ImmutableList (com.google.common.collect.ImmutableList)7 Map (java.util.Map)7 DateString (org.apache.calcite.util.DateString)7 TimeString (org.apache.calcite.util.TimeString)7 TimestampString (org.apache.calcite.util.TimestampString)7 ISE (io.druid.java.util.common.ISE)6 SqlKind (org.apache.calcite.sql.SqlKind)6 NlsString (org.apache.calcite.util.NlsString)6 Calendar (java.util.Calendar)5 Nullable (javax.annotation.Nullable)5 RexBuilder (org.apache.calcite.rex.RexBuilder)5