Search in sources :

Example 1 with BasicSqlType

use of org.apache.calcite.sql.type.BasicSqlType in project calcite by apache.

the class SqlLimitsTest method printLimit.

private void printLimit(PrintWriter pw, String desc, RelDataType type, boolean sign, SqlTypeName.Limit limit, boolean beyond) {
    Object o = ((BasicSqlType) type).getLimit(sign, limit, beyond);
    if (o == null) {
        return;
    }
    pw.print(desc);
    String s;
    if (o instanceof byte[]) {
        int k = 0;
        StringBuilder buf = new StringBuilder("{");
        for (byte b : (byte[]) o) {
            if (k++ > 0) {
                buf.append(", ");
            }
            buf.append(Integer.toHexString(b & 0xff));
        }
        buf.append("}");
        s = buf.toString();
    } else if (o instanceof Calendar) {
        Calendar calendar = (Calendar) o;
        DateFormat dateFormat = getDateFormat(type.getSqlTypeName());
        dateFormat.setTimeZone(DateTimeUtils.UTC_ZONE);
        s = dateFormat.format(calendar.getTime());
    } else {
        s = o.toString();
    }
    pw.print(s);
    SqlLiteral literal = type.getSqlTypeName().createLiteral(o, SqlParserPos.ZERO);
    pw.print("; as SQL: ");
    pw.print(literal.toSqlString(AnsiSqlDialect.DEFAULT));
    pw.println();
}
Also used : BasicSqlType(org.apache.calcite.sql.type.BasicSqlType) Calendar(java.util.Calendar) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SqlLiteral(org.apache.calcite.sql.SqlLiteral)

Example 2 with BasicSqlType

use of org.apache.calcite.sql.type.BasicSqlType in project druid by druid-io.

the class DruidRexExecutorTest method testArrayOfLongsReduction.

@Test
public void testArrayOfLongsReduction() {
    DruidRexExecutor rexy = new DruidRexExecutor(PLANNER_CONTEXT);
    List<RexNode> reduced = new ArrayList<>();
    BasicSqlType basicSqlType = new BasicSqlType(DruidTypeSystem.INSTANCE, SqlTypeName.INTEGER);
    ArraySqlType arraySqlType = new ArraySqlType(basicSqlType, false);
    List<BigDecimal> elements = ImmutableList.of(BigDecimal.valueOf(50), BigDecimal.valueOf(12));
    RexNode literal = rexBuilder.makeLiteral(elements, arraySqlType, true);
    rexy.reduce(rexBuilder, ImmutableList.of(literal), reduced);
    Assert.assertEquals(1, reduced.size());
    Assert.assertEquals(DruidExpression.ofExpression(ColumnType.LONG_ARRAY, DruidExpression.functionCall("array"), ImmutableList.of(DruidExpression.ofLiteral(ColumnType.LONG, "50"), DruidExpression.ofLiteral(ColumnType.LONG, "12"))), Expressions.toDruidExpression(PLANNER_CONTEXT, RowSignature.empty(), reduced.get(0)));
}
Also used : BasicSqlType(org.apache.calcite.sql.type.BasicSqlType) ArrayList(java.util.ArrayList) ArraySqlType(org.apache.calcite.sql.type.ArraySqlType) BigDecimal(java.math.BigDecimal) RexNode(org.apache.calcite.rex.RexNode) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 3 with BasicSqlType

use of org.apache.calcite.sql.type.BasicSqlType in project druid by druid-io.

the class DruidRexExecutorTest method testArrayOfDoublesReduction.

@Test
public void testArrayOfDoublesReduction() {
    DruidRexExecutor rexy = new DruidRexExecutor(PLANNER_CONTEXT);
    List<RexNode> reduced = new ArrayList<>();
    BasicSqlType basicSqlType = new BasicSqlType(DruidTypeSystem.INSTANCE, SqlTypeName.DECIMAL);
    ArraySqlType arraySqlType = new ArraySqlType(basicSqlType, false);
    List<BigDecimal> elements = ImmutableList.of(BigDecimal.valueOf(50.12), BigDecimal.valueOf(12.1));
    RexNode literal = rexBuilder.makeLiteral(elements, arraySqlType, true);
    rexy.reduce(rexBuilder, ImmutableList.of(literal), reduced);
    Assert.assertEquals(1, reduced.size());
    Assert.assertEquals(DruidExpression.ofExpression(ColumnType.DOUBLE_ARRAY, DruidExpression.functionCall("array"), ImmutableList.of(DruidExpression.ofLiteral(ColumnType.DOUBLE, "50.12"), DruidExpression.ofLiteral(ColumnType.DOUBLE, "12.1"))), Expressions.toDruidExpression(PLANNER_CONTEXT, RowSignature.empty(), reduced.get(0)));
}
Also used : BasicSqlType(org.apache.calcite.sql.type.BasicSqlType) ArrayList(java.util.ArrayList) ArraySqlType(org.apache.calcite.sql.type.ArraySqlType) BigDecimal(java.math.BigDecimal) RexNode(org.apache.calcite.rex.RexNode) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 4 with BasicSqlType

use of org.apache.calcite.sql.type.BasicSqlType in project samza by apache.

the class CheckerTest method testCheckOperandTypesShouldReturnTrueOnAnyTypeInArg.

@Test
public void testCheckOperandTypesShouldReturnTrueOnAnyTypeInArg() throws NoSuchMethodException {
    Method udfMethod = TestUdfWithAnyType.class.getMethod("execute", Object.class);
    UdfMetadata udfMetadata = new UdfMetadata("TestUdfWithAnyType", "TestUDFClass", udfMethod, new MapConfig(), ImmutableList.of(SamzaSqlFieldType.ANY), SamzaSqlFieldType.INT64, false);
    Checker operandTypeChecker = Checker.getChecker(1, 3, udfMetadata);
    SqlCallBinding callBinding = Mockito.mock(SqlCallBinding.class);
    Mockito.when(callBinding.getOperandCount()).thenReturn(1);
    Mockito.when(callBinding.getOperandType(0)).thenReturn(new BasicSqlType(RelDataTypeSystem.DEFAULT, SqlTypeName.ARRAY));
    assertTrue(operandTypeChecker.checkOperandTypes(callBinding, true));
}
Also used : BasicSqlType(org.apache.calcite.sql.type.BasicSqlType) SqlCallBinding(org.apache.calcite.sql.SqlCallBinding) SamzaSqlUdfMethod(org.apache.samza.sql.udfs.SamzaSqlUdfMethod) Method(java.lang.reflect.Method) MapConfig(org.apache.samza.config.MapConfig) UdfMetadata(org.apache.samza.sql.interfaces.UdfMetadata) Test(org.junit.Test)

Example 5 with BasicSqlType

use of org.apache.calcite.sql.type.BasicSqlType in project samza by apache.

the class CheckerTest method testCheckOperandTypesShouldReturnTrueWhenArgumentCheckIsDisabled.

@Test
public void testCheckOperandTypesShouldReturnTrueWhenArgumentCheckIsDisabled() throws NoSuchMethodException {
    Method udfMethod = TestUdfWithWrongTypes.class.getMethod("execute", String.class);
    UdfMetadata udfMetadata = new UdfMetadata("TestUdfWithWrongTypes", "TestUDFClass", udfMethod, new MapConfig(), ImmutableList.of(SamzaSqlFieldType.INT32), SamzaSqlFieldType.INT64, true);
    Checker operandTypeChecker = Checker.getChecker(1, 3, udfMetadata);
    SqlCallBinding callBinding = Mockito.mock(SqlCallBinding.class);
    Mockito.when(callBinding.getOperandCount()).thenReturn(1);
    Mockito.when(callBinding.getOperandType(0)).thenReturn(new BasicSqlType(RelDataTypeSystem.DEFAULT, SqlTypeName.VARCHAR, 12));
    assertTrue(operandTypeChecker.checkOperandTypes(callBinding, true));
}
Also used : BasicSqlType(org.apache.calcite.sql.type.BasicSqlType) SqlCallBinding(org.apache.calcite.sql.SqlCallBinding) SamzaSqlUdfMethod(org.apache.samza.sql.udfs.SamzaSqlUdfMethod) Method(java.lang.reflect.Method) MapConfig(org.apache.samza.config.MapConfig) UdfMetadata(org.apache.samza.sql.interfaces.UdfMetadata) Test(org.junit.Test)

Aggregations

BasicSqlType (org.apache.calcite.sql.type.BasicSqlType)8 Test (org.junit.Test)7 Method (java.lang.reflect.Method)5 SqlCallBinding (org.apache.calcite.sql.SqlCallBinding)5 MapConfig (org.apache.samza.config.MapConfig)5 UdfMetadata (org.apache.samza.sql.interfaces.UdfMetadata)5 SamzaSqlUdfMethod (org.apache.samza.sql.udfs.SamzaSqlUdfMethod)5 BigDecimal (java.math.BigDecimal)2 ArrayList (java.util.ArrayList)2 RexNode (org.apache.calcite.rex.RexNode)2 ArraySqlType (org.apache.calcite.sql.type.ArraySqlType)2 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)2 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Calendar (java.util.Calendar)1 SqlLiteral (org.apache.calcite.sql.SqlLiteral)1