Search in sources :

Example 36 with CharType

use of io.trino.spi.type.CharType in project trino by trinodb.

the class ImplementCountDistinct method rewrite.

@Override
public Optional<JdbcExpression> rewrite(AggregateFunction aggregateFunction, Captures captures, RewriteContext<String> context) {
    Variable argument = captures.get(ARGUMENT);
    JdbcColumnHandle columnHandle = (JdbcColumnHandle) context.getAssignment(argument.getName());
    verify(aggregateFunction.getOutputType() == BIGINT);
    boolean isCaseSensitiveType = columnHandle.getColumnType() instanceof CharType || columnHandle.getColumnType() instanceof VarcharType;
    if (aggregateFunction.isDistinct() && !isRemoteCollationSensitive && isCaseSensitiveType) {
        // Remote database is case insensitive or compares values differently from Trino
        return Optional.empty();
    }
    return Optional.of(new JdbcExpression(format("count(DISTINCT %s)", context.rewriteExpression(argument).orElseThrow()), bigintTypeHandle));
}
Also used : Variable(io.trino.spi.expression.Variable) VarcharType(io.trino.spi.type.VarcharType) JdbcColumnHandle(io.trino.plugin.jdbc.JdbcColumnHandle) JdbcExpression(io.trino.plugin.jdbc.JdbcExpression) CharType(io.trino.spi.type.CharType)

Example 37 with CharType

use of io.trino.spi.type.CharType in project trino by trinodb.

the class MaterializedResult method writeValue.

private static void writeValue(Type type, BlockBuilder blockBuilder, Object value) {
    if (value == null) {
        blockBuilder.appendNull();
    } else if (BIGINT.equals(type)) {
        type.writeLong(blockBuilder, (Long) value);
    } else if (INTEGER.equals(type)) {
        type.writeLong(blockBuilder, (Integer) value);
    } else if (SMALLINT.equals(type)) {
        type.writeLong(blockBuilder, (Short) value);
    } else if (TINYINT.equals(type)) {
        type.writeLong(blockBuilder, (Byte) value);
    } else if (REAL.equals(type)) {
        type.writeLong(blockBuilder, floatToRawIntBits(((Float) value)));
    } else if (DOUBLE.equals(type)) {
        type.writeDouble(blockBuilder, (Double) value);
    } else if (BOOLEAN.equals(type)) {
        type.writeBoolean(blockBuilder, (Boolean) value);
    } else if (JSON.equals(type)) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
    } else if (type instanceof VarcharType) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
    } else if (type instanceof CharType) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
    } else if (VARBINARY.equals(type)) {
        type.writeSlice(blockBuilder, Slices.wrappedBuffer((byte[]) value));
    } else if (DATE.equals(type)) {
        int days = ((SqlDate) value).getDays();
        type.writeLong(blockBuilder, days);
    } else if (type instanceof TimeType) {
        SqlTime time = (SqlTime) value;
        type.writeLong(blockBuilder, time.getPicos());
    } else if (type instanceof TimeWithTimeZoneType) {
        long nanos = roundDiv(((SqlTimeWithTimeZone) value).getPicos(), PICOSECONDS_PER_NANOSECOND);
        int offsetMinutes = ((SqlTimeWithTimeZone) value).getOffsetMinutes();
        type.writeLong(blockBuilder, packTimeWithTimeZone(nanos, offsetMinutes));
    } else if (type instanceof TimestampType) {
        long micros = ((SqlTimestamp) value).getEpochMicros();
        if (((TimestampType) type).getPrecision() <= TimestampType.MAX_SHORT_PRECISION) {
            type.writeLong(blockBuilder, micros);
        } else {
            type.writeObject(blockBuilder, new LongTimestamp(micros, ((SqlTimestamp) value).getPicosOfMicros()));
        }
    } else if (TIMESTAMP_WITH_TIME_ZONE.equals(type)) {
        long millisUtc = ((SqlTimestampWithTimeZone) value).getMillisUtc();
        TimeZoneKey timeZoneKey = ((SqlTimestampWithTimeZone) value).getTimeZoneKey();
        type.writeLong(blockBuilder, packDateTimeWithZone(millisUtc, timeZoneKey));
    } else if (type instanceof ArrayType) {
        List<?> list = (List<?>) value;
        Type elementType = ((ArrayType) type).getElementType();
        BlockBuilder arrayBlockBuilder = blockBuilder.beginBlockEntry();
        for (Object element : list) {
            writeValue(elementType, arrayBlockBuilder, element);
        }
        blockBuilder.closeEntry();
    } else if (type instanceof MapType) {
        Map<?, ?> map = (Map<?, ?>) value;
        Type keyType = ((MapType) type).getKeyType();
        Type valueType = ((MapType) type).getValueType();
        BlockBuilder mapBlockBuilder = blockBuilder.beginBlockEntry();
        for (Entry<?, ?> entry : map.entrySet()) {
            writeValue(keyType, mapBlockBuilder, entry.getKey());
            writeValue(valueType, mapBlockBuilder, entry.getValue());
        }
        blockBuilder.closeEntry();
    } else if (type instanceof RowType) {
        List<?> row = (List<?>) value;
        List<Type> fieldTypes = type.getTypeParameters();
        BlockBuilder rowBlockBuilder = blockBuilder.beginBlockEntry();
        for (int field = 0; field < row.size(); field++) {
            writeValue(fieldTypes.get(field), rowBlockBuilder, row.get(field));
        }
        blockBuilder.closeEntry();
    } else {
        throw new IllegalArgumentException("Unsupported type " + type);
    }
}
Also used : VarcharType(io.trino.spi.type.VarcharType) SqlTime(io.trino.spi.type.SqlTime) TimeWithTimeZoneType(io.trino.spi.type.TimeWithTimeZoneType) RowType(io.trino.spi.type.RowType) MapType(io.trino.spi.type.MapType) TimeType(io.trino.spi.type.TimeType) ArrayType(io.trino.spi.type.ArrayType) TimestampType(io.trino.spi.type.TimestampType) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) TimeZoneKey(io.trino.spi.type.TimeZoneKey) BlockBuilder(io.trino.spi.block.BlockBuilder) LongTimestamp(io.trino.spi.type.LongTimestamp) SqlTimeWithTimeZone(io.trino.spi.type.SqlTimeWithTimeZone) RowType(io.trino.spi.type.RowType) ArrayType(io.trino.spi.type.ArrayType) TimeWithTimeZoneType(io.trino.spi.type.TimeWithTimeZoneType) TimeType(io.trino.spi.type.TimeType) Type(io.trino.spi.type.Type) TimestampType(io.trino.spi.type.TimestampType) VarcharType(io.trino.spi.type.VarcharType) MapType(io.trino.spi.type.MapType) CharType(io.trino.spi.type.CharType) SqlTimestampWithTimeZone(io.trino.spi.type.SqlTimestampWithTimeZone) SqlDate(io.trino.spi.type.SqlDate) OptionalLong(java.util.OptionalLong) CharType(io.trino.spi.type.CharType) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 38 with CharType

use of io.trino.spi.type.CharType in project trino by trinodb.

the class AbstractTestHive method assertValueTypes.

private static void assertValueTypes(MaterializedRow row, List<ColumnMetadata> schema) {
    for (int columnIndex = 0; columnIndex < schema.size(); columnIndex++) {
        ColumnMetadata column = schema.get(columnIndex);
        Object value = row.getField(columnIndex);
        if (value != null) {
            if (BOOLEAN.equals(column.getType())) {
                assertInstanceOf(value, Boolean.class);
            } else if (TINYINT.equals(column.getType())) {
                assertInstanceOf(value, Byte.class);
            } else if (SMALLINT.equals(column.getType())) {
                assertInstanceOf(value, Short.class);
            } else if (INTEGER.equals(column.getType())) {
                assertInstanceOf(value, Integer.class);
            } else if (BIGINT.equals(column.getType())) {
                assertInstanceOf(value, Long.class);
            } else if (DOUBLE.equals(column.getType())) {
                assertInstanceOf(value, Double.class);
            } else if (REAL.equals(column.getType())) {
                assertInstanceOf(value, Float.class);
            } else if (column.getType() instanceof VarcharType) {
                assertInstanceOf(value, String.class);
            } else if (column.getType() instanceof CharType) {
                assertInstanceOf(value, String.class);
            } else if (VARBINARY.equals(column.getType())) {
                assertInstanceOf(value, SqlVarbinary.class);
            } else if (TIMESTAMP_MILLIS.equals(column.getType())) {
                assertInstanceOf(value, SqlTimestamp.class);
            } else if (TIMESTAMP_WITH_TIME_ZONE.equals(column.getType())) {
                assertInstanceOf(value, SqlTimestampWithTimeZone.class);
            } else if (DATE.equals(column.getType())) {
                assertInstanceOf(value, SqlDate.class);
            } else if (column.getType() instanceof ArrayType || column.getType() instanceof RowType) {
                assertInstanceOf(value, List.class);
            } else if (column.getType() instanceof MapType) {
                assertInstanceOf(value, Map.class);
            } else {
                fail("Unknown primitive type " + columnIndex);
            }
        }
    }
}
Also used : ColumnMetadata(io.trino.spi.connector.ColumnMetadata) VarcharType.createVarcharType(io.trino.spi.type.VarcharType.createVarcharType) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) VarcharType(io.trino.spi.type.VarcharType) SqlVarbinary(io.trino.spi.type.SqlVarbinary) RowType(io.trino.spi.type.RowType) OptionalDouble(java.util.OptionalDouble) Constraint(io.trino.spi.connector.Constraint) MapType(io.trino.spi.type.MapType) ArrayType(io.trino.spi.type.ArrayType) SqlTimestampWithTimeZone(io.trino.spi.type.SqlTimestampWithTimeZone) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) CharType.createCharType(io.trino.spi.type.CharType.createCharType) CharType(io.trino.spi.type.CharType)

Example 39 with CharType

use of io.trino.spi.type.CharType in project trino by trinodb.

the class AbstractTestHiveFileFormats method checkPageSource.

protected void checkPageSource(ConnectorPageSource pageSource, List<TestColumn> testColumns, List<Type> types, int rowCount) throws IOException {
    try {
        MaterializedResult result = materializeSourceDataStream(SESSION, pageSource, types);
        assertEquals(result.getMaterializedRows().size(), rowCount);
        for (MaterializedRow row : result) {
            for (int i = 0, testColumnsSize = testColumns.size(); i < testColumnsSize; i++) {
                TestColumn testColumn = testColumns.get(i);
                Type type = types.get(i);
                Object actualValue = row.getField(i);
                Object expectedValue = testColumn.getExpectedValue();
                if (expectedValue instanceof Slice) {
                    expectedValue = ((Slice) expectedValue).toStringUtf8();
                }
                if (actualValue == null || expectedValue == null) {
                    assertEquals(actualValue, expectedValue, "Wrong value for column " + testColumn.getName());
                } else if (testColumn.getObjectInspector().getTypeName().equals("float")) {
                    assertEquals((float) actualValue, (float) expectedValue, EPSILON, "Wrong value for column " + testColumn.getName());
                } else if (testColumn.getObjectInspector().getTypeName().equals("double")) {
                    assertEquals((double) actualValue, (double) expectedValue, EPSILON, "Wrong value for column " + testColumn.getName());
                } else if (testColumn.getObjectInspector().getTypeName().equals("date")) {
                    SqlDate expectedDate = new SqlDate(((Long) expectedValue).intValue());
                    assertEquals(actualValue, expectedDate, "Wrong value for column " + testColumn.getName());
                } else if (testColumn.getObjectInspector().getTypeName().equals("int") || testColumn.getObjectInspector().getTypeName().equals("smallint") || testColumn.getObjectInspector().getTypeName().equals("tinyint")) {
                    assertEquals(actualValue, expectedValue);
                } else if (testColumn.getObjectInspector().getTypeName().equals("timestamp")) {
                    SqlTimestamp expectedTimestamp = sqlTimestampOf(floorDiv((Long) expectedValue, MICROSECONDS_PER_MILLISECOND));
                    assertEquals(actualValue, expectedTimestamp, "Wrong value for column " + testColumn.getName());
                } else if (testColumn.getObjectInspector().getTypeName().startsWith("char")) {
                    assertEquals(actualValue, padSpaces((String) expectedValue, (CharType) type), "Wrong value for column " + testColumn.getName());
                } else if (testColumn.getObjectInspector().getCategory() == Category.PRIMITIVE) {
                    if (expectedValue instanceof Slice) {
                        expectedValue = ((Slice) expectedValue).toStringUtf8();
                    }
                    if (actualValue instanceof Slice) {
                        actualValue = ((Slice) actualValue).toStringUtf8();
                    }
                    if (actualValue instanceof SqlVarbinary) {
                        actualValue = new String(((SqlVarbinary) actualValue).getBytes(), UTF_8);
                    }
                    if (actualValue instanceof SqlDecimal) {
                        actualValue = new BigDecimal(actualValue.toString());
                    }
                    assertEquals(actualValue, expectedValue, "Wrong value for column " + testColumn.getName());
                } else {
                    BlockBuilder builder = type.createBlockBuilder(null, 1);
                    type.writeObject(builder, expectedValue);
                    expectedValue = type.getObjectValue(SESSION, builder.build(), 0);
                    assertEquals(actualValue, expectedValue, "Wrong value for column " + testColumn.getName());
                }
            }
        }
    } finally {
        pageSource.close();
    }
}
Also used : SqlVarbinary(io.trino.spi.type.SqlVarbinary) SqlTimestamp(io.trino.spi.type.SqlTimestamp) SqlDecimal(io.trino.spi.type.SqlDecimal) BigDecimal(java.math.BigDecimal) DateType(io.trino.spi.type.DateType) CharType.createCharType(io.trino.spi.type.CharType.createCharType) TimestampType(io.trino.spi.type.TimestampType) HiveTestUtils.mapType(io.trino.plugin.hive.HiveTestUtils.mapType) CharType(io.trino.spi.type.CharType) VarcharType.createVarcharType(io.trino.spi.type.VarcharType.createVarcharType) RowType(io.trino.spi.type.RowType) ArrayType(io.trino.spi.type.ArrayType) DecimalType(io.trino.spi.type.DecimalType) Type(io.trino.spi.type.Type) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) VarcharType(io.trino.spi.type.VarcharType) HiveUtil.isStructuralType(io.trino.plugin.hive.util.HiveUtil.isStructuralType) Slice(io.airlift.slice.Slice) SqlDate(io.trino.spi.type.SqlDate) SerDeUtils.serializeObject(io.trino.plugin.hive.util.SerDeUtils.serializeObject) CharType.createCharType(io.trino.spi.type.CharType.createCharType) CharType(io.trino.spi.type.CharType) MaterializedResult(io.trino.testing.MaterializedResult) MaterializedRow(io.trino.testing.MaterializedRow) BlockBuilder(io.trino.spi.block.BlockBuilder)

Example 40 with CharType

use of io.trino.spi.type.CharType in project trino by trinodb.

the class TestDefaultJdbcQueryBuilder method setup.

@BeforeMethod
public void setup() throws SQLException {
    database = new TestingDatabase();
    jdbcClient = database.getJdbcClient();
    CharType charType = CharType.createCharType(0);
    columns = ImmutableList.of(new JdbcColumnHandle("col_0", JDBC_BIGINT, BIGINT), new JdbcColumnHandle("col_1", JDBC_DOUBLE, DOUBLE), new JdbcColumnHandle("col_2", JDBC_BOOLEAN, BOOLEAN), new JdbcColumnHandle("col_3", JDBC_VARCHAR, VARCHAR), new JdbcColumnHandle("col_4", JDBC_DATE, DATE), new JdbcColumnHandle("col_5", JDBC_TIME, TIME), new JdbcColumnHandle("col_6", JDBC_TIMESTAMP, TIMESTAMP_MILLIS), new JdbcColumnHandle("col_7", JDBC_TINYINT, TINYINT), new JdbcColumnHandle("col_8", JDBC_SMALLINT, SMALLINT), new JdbcColumnHandle("col_9", JDBC_INTEGER, INTEGER), new JdbcColumnHandle("col_10", JDBC_REAL, REAL), new JdbcColumnHandle("col_11", JDBC_CHAR, charType));
    Connection connection = database.getConnection();
    try (PreparedStatement preparedStatement = connection.prepareStatement("create table \"test_table\" (" + "\"col_0\" BIGINT, " + "\"col_1\" DOUBLE, " + "\"col_2\" BOOLEAN, " + "\"col_3\" VARCHAR(128), " + "\"col_4\" DATE, " + "\"col_5\" TIME, " + "\"col_6\" TIMESTAMP, " + "\"col_7\" TINYINT, " + "\"col_8\" SMALLINT, " + "\"col_9\" INTEGER, " + "\"col_10\" REAL, " + "\"col_11\" CHAR(128) " + ")")) {
        preparedStatement.execute();
        StringBuilder stringBuilder = new StringBuilder("insert into \"test_table\" values ");
        int len = 1000;
        LocalDateTime dateTime = LocalDateTime.of(2016, 3, 23, 12, 23, 37);
        for (int i = 0; i < len; i++) {
            stringBuilder.append(format(Locale.ENGLISH, "(%d, %f, %b, 'test_str_%d', '%s', '%s', '%s', %d, %d, %d, %f, 'test_str_%d')", i, 200000.0 + i / 2.0, i % 2 == 0, i, Date.valueOf(dateTime.toLocalDate()), Time.valueOf(dateTime.toLocalTime()), Timestamp.valueOf(dateTime), i % 128, -i, i - 100, 100.0f + i, i));
            dateTime = dateTime.plusHours(26);
            if (i != len - 1) {
                stringBuilder.append(",");
            }
        }
        try (PreparedStatement preparedStatement2 = connection.prepareStatement(stringBuilder.toString())) {
            preparedStatement2.execute();
        }
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) CharType(io.trino.spi.type.CharType) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

CharType (io.trino.spi.type.CharType)50 VarcharType (io.trino.spi.type.VarcharType)45 DecimalType (io.trino.spi.type.DecimalType)39 TrinoException (io.trino.spi.TrinoException)28 ArrayType (io.trino.spi.type.ArrayType)20 TimestampType (io.trino.spi.type.TimestampType)20 Type (io.trino.spi.type.Type)19 DecimalType.createDecimalType (io.trino.spi.type.DecimalType.createDecimalType)16 Slice (io.airlift.slice.Slice)13 TimeType (io.trino.spi.type.TimeType)12 VarbinaryType (io.trino.spi.type.VarbinaryType)12 VarcharType.createUnboundedVarcharType (io.trino.spi.type.VarcharType.createUnboundedVarcharType)12 MapType (io.trino.spi.type.MapType)11 RowType (io.trino.spi.type.RowType)11 ImmutableList (com.google.common.collect.ImmutableList)10 TimestampWithTimeZoneType (io.trino.spi.type.TimestampWithTimeZoneType)10 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)9 ArrayList (java.util.ArrayList)9 List (java.util.List)9 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)8