io.prestosql.spi.type.Type Java Examples

The following examples show how to use io.prestosql.spi.type.Type. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BindCodeGenerator.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
public BytecodeNode generateExpression(ResolvedFunction resolvedFunction, BytecodeGeneratorContext context, Type returnType, List<RowExpression> arguments)
{
    // Bind expression is used to generate captured lambda.
    // It takes the captured values and the uncaptured lambda, and produces captured lambda as the output.
    // The uncaptured lambda is just a method, and does not have a stack representation during execution.
    // As a result, the bind expression generates the captured lambda in one step.
    int numCaptures = arguments.size() - 1;
    LambdaDefinitionExpression lambda = (LambdaDefinitionExpression) arguments.get(numCaptures);
    checkState(compiledLambdaMap.containsKey(lambda), "lambda expressions map does not contain this lambda definition");
    CompiledLambda compiledLambda = compiledLambdaMap.get(lambda);

    return LambdaBytecodeGenerator.generateLambda(
            context,
            arguments.subList(0, numCaptures),
            compiledLambda,
            lambdaInterface);
}
 
Example #2
Source File: StateCompiler.java    From presto with Apache License 2.0 6 votes vote down vote up
private StateField(String name, Class<?> type, Object initialValue, String getterName, Optional<Type> sqlType)
{
    this.name = requireNonNull(name, "name is null");
    checkArgument(!name.isEmpty(), "name is empty");
    this.type = requireNonNull(type, "type is null");
    this.getterName = requireNonNull(getterName, "getterName is null");
    this.initialValue = initialValue;
    checkArgument(sqlType != null, "sqlType is null");
    if (sqlType.isPresent()) {
        checkArgument(
                type.isAssignableFrom(sqlType.get().getJavaType()) ||
                        ((type == byte.class) && TINYINT.equals(sqlType.get())) ||
                        ((type == int.class) && INTEGER.equals(sqlType.get())),
                "Stack type (%s) and provided sql type (%s) are incompatible", type.getName(), sqlType.get().getDisplayName());
    }
    else {
        sqlType = sqlTypeFromStackType(type);
    }
    this.sqlType = sqlType;
}
 
Example #3
Source File: StatementAnalyzer.java    From presto with Apache License 2.0 6 votes vote down vote up
private boolean hasNestedBoundedCharacterType(Type type)
{
    if (type instanceof ArrayType) {
        return hasBoundedCharacterType(((ArrayType) type).getElementType());
    }

    if (type instanceof MapType) {
        return hasBoundedCharacterType(((MapType) type).getKeyType()) || hasBoundedCharacterType(((MapType) type).getValueType());
    }

    if (type instanceof RowType) {
        for (Type fieldType : type.getTypeParameters()) {
            if (hasBoundedCharacterType(fieldType)) {
                return true;
            }
        }
    }

    return false;
}
 
Example #4
Source File: ArraysOverlapFunction.java    From presto with Apache License 2.0 6 votes vote down vote up
private static IntComparator intBlockCompare(Type type, Block block)
{
    return new AbstractIntComparator()
    {
        @Override
        public int compare(int left, int right)
        {
            if (block.isNull(left) && block.isNull(right)) {
                return 0;
            }
            if (block.isNull(left)) {
                return 1;
            }
            if (block.isNull(right)) {
                return -1;
            }
            return type.compareTo(block, left, block, right);
        }
    };
}
 
Example #5
Source File: JsonUtil.java    From presto with Apache License 2.0 6 votes vote down vote up
public static boolean canCastFromJson(Type type)
{
    if (type instanceof BooleanType ||
            type instanceof TinyintType ||
            type instanceof SmallintType ||
            type instanceof IntegerType ||
            type instanceof BigintType ||
            type instanceof RealType ||
            type instanceof DoubleType ||
            type instanceof DecimalType ||
            type instanceof VarcharType ||
            type instanceof JsonType) {
        return true;
    }
    if (type instanceof ArrayType) {
        return canCastFromJson(((ArrayType) type).getElementType());
    }
    if (type instanceof MapType) {
        return isValidJsonObjectKeyType(((MapType) type).getKeyType()) && canCastFromJson(((MapType) type).getValueType());
    }
    if (type instanceof RowType) {
        return type.getTypeParameters().stream().allMatch(JsonUtil::canCastFromJson);
    }
    return false;
}
 
Example #6
Source File: PreparedStatementBuilder.java    From presto with Apache License 2.0 6 votes vote down vote up
public static PreparedStatement create(
        Connection connection,
        String sql,
        List<String> columnNames,
        List<Type> types,
        Set<Integer> uuidColumnIndexes,
        TupleDomain<Integer> tupleDomain)
        throws SQLException
{
    checkArgument(!isNullOrEmpty(sql), "sql is null or empty");

    List<ValueBuffer> bindValues = new ArrayList<>(256);
    sql += getWhereClause(tupleDomain, columnNames, types, uuidColumnIndexes, bindValues);

    PreparedStatement statement = connection.prepareStatement(sql, TYPE_FORWARD_ONLY, CONCUR_READ_ONLY);
    enableStreamingResults(statement);

    // bind values to statement
    int bindIndex = 1;
    for (ValueBuffer value : bindValues) {
        bindField(value, statement, bindIndex, uuidColumnIndexes.contains(value.getColumnIndex()));
        bindIndex++;
    }
    return statement;
}
 
Example #7
Source File: OrcTester.java    From presto with Apache License 2.0 6 votes vote down vote up
private void testRoundTripType(Type type, List<?> readValues)
        throws Exception
{
    // forward order
    assertRoundTrip(type, readValues);

    // reverse order
    if (reverseTestsEnabled) {
        assertRoundTrip(type, reverse(readValues));
    }

    if (nullTestsEnabled) {
        // forward order with nulls
        assertRoundTrip(type, insertNullEvery(5, readValues));

        // reverse order with nulls
        if (reverseTestsEnabled) {
            assertRoundTrip(type, insertNullEvery(5, reverse(readValues)));
        }
    }
}
 
Example #8
Source File: MongoPageSource.java    From presto with Apache License 2.0 6 votes vote down vote up
private void writeSlice(BlockBuilder output, Type type, Object value)
{
    if (type instanceof VarcharType) {
        type.writeSlice(output, utf8Slice(toVarcharValue(value)));
    }
    else if (type instanceof CharType) {
        type.writeSlice(output, truncateToLengthAndTrimSpaces(utf8Slice((String) value), ((CharType) type)));
    }
    else if (type.equals(OBJECT_ID)) {
        type.writeSlice(output, wrappedBuffer(((ObjectId) value).toByteArray()));
    }
    else if (type instanceof VarbinaryType) {
        if (value instanceof Binary) {
            type.writeSlice(output, wrappedBuffer(((Binary) value).getData()));
        }
        else {
            output.appendNull();
        }
    }
    else if (type instanceof DecimalType) {
        type.writeSlice(output, encodeScaledValue(((Decimal128) value).bigDecimalValue(), ((DecimalType) type).getScale()));
    }
    else {
        throw new PrestoException(GENERIC_INTERNAL_ERROR, "Unhandled type for Slice: " + type.getTypeSignature());
    }
}
 
Example #9
Source File: BenchmarkScanFilterAndProjectOperator.java    From presto with Apache License 2.0 6 votes vote down vote up
private List<RowExpression> getProjections(Type type)
{
    ImmutableList.Builder<RowExpression> builder = ImmutableList.builder();
    if (type == BIGINT) {
        for (int i = 0; i < columnCount; i++) {
            builder.add(rowExpression("bigint" + i + " + 5"));
        }
    }
    else if (type == VARCHAR) {
        for (int i = 0; i < columnCount; i++) {
            // alternatively use identity expression rowExpression("varchar" + i, type) or
            // rowExpression("substr(varchar" + i + ", 1, 1)", type)
            builder.add(rowExpression("concat(varchar" + i + ", 'foo')"));
        }
    }
    return builder.build();
}
 
Example #10
Source File: RcFileWriter.java    From presto with Apache License 2.0 6 votes vote down vote up
public RcFileWriter(
        SliceOutput output,
        List<Type> types,
        RcFileEncoding encoding,
        Optional<String> codecName,
        RcFileCodecFactory codecFactory,
        Map<String, String> metadata,
        boolean validate)
        throws IOException
{
    this(
            output,
            types,
            encoding,
            codecName,
            codecFactory,
            metadata,
            DEFAULT_TARGET_MIN_ROW_GROUP_SIZE,
            DEFAULT_TARGET_MAX_ROW_GROUP_SIZE,
            validate);
}
 
Example #11
Source File: TestPagesSerde.java    From presto with Apache License 2.0 6 votes vote down vote up
private static int serializedSize(List<? extends Type> types, Page expectedPage)
{
    PagesSerde serde = new TestingPagesSerdeFactory().createPagesSerde();
    DynamicSliceOutput sliceOutput = new DynamicSliceOutput(1024);
    writePages(serde, sliceOutput, expectedPage);
    Slice slice = sliceOutput.slice();

    Iterator<Page> pageIterator = readPages(serde, slice.getInput());
    if (pageIterator.hasNext()) {
        assertPageEquals(types, pageIterator.next(), expectedPage);
    }
    else {
        assertEquals(expectedPage.getPositionCount(), 0);
    }
    assertFalse(pageIterator.hasNext());

    return slice.length();
}
 
Example #12
Source File: QueryStateMachine.java    From presto with Apache License 2.0 6 votes vote down vote up
public void setColumns(List<String> columnNames, List<Type> columnTypes)
{
    requireNonNull(columnNames, "columnNames is null");
    requireNonNull(columnTypes, "columnTypes is null");
    checkArgument(columnNames.size() == columnTypes.size(), "columnNames and columnTypes must be the same size");

    Optional<QueryOutputInfo> queryOutputInfo;
    List<Consumer<QueryOutputInfo>> outputInfoListeners;
    synchronized (this) {
        checkState(this.columnNames == null && this.columnTypes == null, "output fields already set");
        this.columnNames = ImmutableList.copyOf(columnNames);
        this.columnTypes = ImmutableList.copyOf(columnTypes);

        queryOutputInfo = getQueryOutputInfo();
        outputInfoListeners = ImmutableList.copyOf(this.outputInfoListeners);
    }
    queryOutputInfo.ifPresent(info -> fireStateChanged(info, outputInfoListeners));
}
 
Example #13
Source File: TypedSet.java    From presto with Apache License 2.0 6 votes vote down vote up
public TypedSet(Type elementType, Optional<MethodHandle> elementIsDistinctFrom, BlockBuilder blockBuilder, int expectedSize, String functionName, Optional<DataSize> maxBlockMemory)
{
    checkArgument(expectedSize >= 0, "expectedSize must not be negative");
    this.elementType = requireNonNull(elementType, "elementType must not be null");
    this.elementIsDistinctFrom = requireNonNull(elementIsDistinctFrom, "elementIsDistinctFrom is null");
    elementIsDistinctFrom.ifPresent(methodHandle -> checkArgument(methodHandle.type().equals(MethodType.methodType(boolean.class, Block.class, int.class, Block.class, int.class))));
    this.elementBlock = requireNonNull(blockBuilder, "blockBuilder must not be null");
    this.functionName = functionName;
    this.maxBlockMemoryInBytes = maxBlockMemory.map(value -> value.toBytes()).orElse(Long.MAX_VALUE);

    initialElementBlockOffset = elementBlock.getPositionCount();
    initialElementBlockSizeInBytes = elementBlock.getSizeInBytes();

    this.size = 0;
    this.hashCapacity = arraySize(expectedSize, FILL_RATIO);
    this.maxFill = calculateMaxFill(hashCapacity);
    this.hashMask = hashCapacity - 1;

    blockPositionByHash = new IntArrayList(hashCapacity);
    blockPositionByHash.size(hashCapacity);
    for (int i = 0; i < hashCapacity; i++) {
        blockPositionByHash.set(i, EMPTY_SLOT);
    }

    this.containsNullElement = false;
}
 
Example #14
Source File: StreamingAggregationOperator.java    From presto with Apache License 2.0 6 votes vote down vote up
public static OperatorFactory createOperatorFactory(
        int operatorId,
        PlanNodeId planNodeId,
        List<Type> sourceTypes,
        List<Type> groupByTypes,
        List<Integer> groupByChannels,
        Step step,
        List<AccumulatorFactory> accumulatorFactories,
        JoinCompiler joinCompiler)
{
    return createAdapterOperatorFactory(new Factory(
            operatorId,
            planNodeId,
            sourceTypes,
            groupByTypes,
            groupByChannels,
            step,
            accumulatorFactories,
            joinCompiler));
}
 
Example #15
Source File: TestHiveTypeTranslator.java    From presto with Apache License 2.0 6 votes vote down vote up
protected TestHiveTypeTranslator(TypeTranslator typeTranslator, Map<Type, HiveType> overwriteTranslation)
{
    this.typeTranslator = requireNonNull(typeTranslator, "typeTranslator is null");

    ImmutableMap<Type, HiveType> hiveTypeTranslationMap = ImmutableMap.<Type, HiveType>builder()
            .put(BIGINT, HiveType.HIVE_LONG)
            .put(INTEGER, HiveType.HIVE_INT)
            .put(SMALLINT, HiveType.HIVE_SHORT)
            .put(TINYINT, HiveType.HIVE_BYTE)
            .put(DOUBLE, HiveType.HIVE_DOUBLE)
            .put(createVarcharType(3), HiveType.valueOf("varchar(3)"))
            .put(VARCHAR, HiveType.HIVE_STRING)
            .put(DATE, HiveType.HIVE_DATE)
            .put(TIMESTAMP, HiveType.HIVE_TIMESTAMP)
            .put(createDecimalType(5, 3), HiveType.valueOf("decimal(5,3)"))
            .put(VARBINARY, HiveType.HIVE_BINARY)
            .put(new ArrayType(TIMESTAMP), HiveType.valueOf("array<timestamp>"))
            .put(TYPE_MANAGER.getType(mapType(BOOLEAN.getTypeSignature(), VARBINARY.getTypeSignature())), HiveType.valueOf("map<boolean,binary>"))
            .put(RowType.from(ImmutableList.of(field("col0", INTEGER), field("col1", VARBINARY))),
                    HiveType.valueOf("struct<col0:int,col1:binary>"))
            .build();

    typeTranslationMap = new HashMap<>();
    typeTranslationMap.putAll(hiveTypeTranslationMap);
    typeTranslationMap.putAll(overwriteTranslation);
}
 
Example #16
Source File: PlanBuilder.java    From presto with Apache License 2.0 6 votes vote down vote up
public PlanNode spatialJoin(
        SpatialJoinNode.Type type,
        PlanNode left,
        PlanNode right,
        List<Symbol> outputSymbols,
        Expression filter,
        Optional<Symbol> leftPartitionSymbol,
        Optional<Symbol> rightPartitionSymbol,
        Optional<String> kdbTree)
{
    return new SpatialJoinNode(
            idAllocator.getNextId(),
            type,
            left,
            right,
            outputSymbols,
            filter,
            leftPartitionSymbol,
            rightPartitionSymbol,
            kdbTree);
}
 
Example #17
Source File: PrestoThriftPageResult.java    From presto with Apache License 2.0 6 votes vote down vote up
@Nullable
public Page toPage(List<Type> columnTypes)
{
    if (rowCount == 0) {
        return null;
    }
    checkArgument(columnBlocks.size() == columnTypes.size(), "columns and types have different sizes");
    int numberOfColumns = columnBlocks.size();
    if (numberOfColumns == 0) {
        // request/response with no columns, used for queries like "select count star"
        return new Page(rowCount);
    }
    Block[] blocks = new Block[numberOfColumns];
    for (int i = 0; i < numberOfColumns; i++) {
        blocks[i] = columnBlocks.get(i).toBlock(columnTypes.get(i));
    }
    return new Page(blocks);
}
 
Example #18
Source File: AggregateFunction.java    From presto with Apache License 2.0 6 votes vote down vote up
public AggregateFunction(
        String aggregateFunctionName,
        Type outputType,
        List<ConnectorExpression> inputs,
        List<SortItem> sortItems,
        boolean isDistinct,
        Optional<ConnectorExpression> filter)
{
    if (isDistinct && inputs.isEmpty()) {
        throw new IllegalArgumentException("DISTINCT requires inputs");
    }

    this.functionName = requireNonNull(aggregateFunctionName, "name is null");
    this.outputType = requireNonNull(outputType, "outputType is null");
    requireNonNull(inputs, "inputs is null");
    requireNonNull(sortItems, "sortOrder is null");
    this.inputs = List.copyOf(inputs);
    this.sortItems = List.copyOf(sortItems);
    this.isDistinct = isDistinct;
    this.filter = requireNonNull(filter, "filter is null");
}
 
Example #19
Source File: TestCanonicalizeExpressionRewriter.java    From presto with Apache License 2.0 6 votes vote down vote up
private static void assertRewritten(String from, String to)
{
    assertExpressionEquals(
            rewrite(
                    PlanBuilder.expression(from),
                    TEST_SESSION,
                    METADATA,
                    TYPE_ANALYZER,
                    TypeProvider.copyOf(ImmutableMap.<Symbol, Type>builder()
                            .put(new Symbol("x"), BIGINT)
                            .put(new Symbol("a"), BIGINT)
                            .build())),
            PlanBuilder.expression(to),
            SymbolAliases.builder()
                    .put("x", new SymbolReference("x"))
                    .put("a", new SymbolReference("a"))
                    .build());
}
 
Example #20
Source File: TestDriver.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test(invocationCount = 1_000, timeOut = 10_000)
public void testConcurrentClose()
{
    List<Type> types = ImmutableList.of(VARCHAR, BIGINT, BIGINT);
    OperatorContext operatorContext = driverContext.addOperatorContext(0, new PlanNodeId("test"), "values");
    ValuesOperator source = new ValuesOperator(operatorContext, rowPagesBuilder(types)
            .addSequencePage(10, 20, 30, 40)
            .build());

    Operator sink = createSinkOperator(types);
    Driver driver = Driver.createDriver(driverContext, source, sink);
    // let these threads race
    scheduledExecutor.submit(() -> driver.processFor(new Duration(1, TimeUnit.NANOSECONDS))); // don't want to call isFinishedInternal in processFor
    scheduledExecutor.submit(() -> driver.close());
    while (!driverContext.isDone()) {
        Uninterruptibles.sleepUninterruptibly(1, TimeUnit.MILLISECONDS);
    }
}
 
Example #21
Source File: DynamicFilterSourceOperator.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
public void finish()
{
    if (finished) {
        // NOTE: finish() may be called multiple times (see comment at Driver::processInternal).
        return;
    }
    finished = true;
    if (valueSets == null) {
        return; // the predicate became too large.
    }

    ImmutableMap.Builder<DynamicFilterId, Domain> domainsBuilder = new ImmutableMap.Builder<>();
    for (int channelIndex = 0; channelIndex < channels.size(); ++channelIndex) {
        Block block = blockBuilders[channelIndex].build();
        Type type = channels.get(channelIndex).type;
        domainsBuilder.put(channels.get(channelIndex).filterId, convertToDomain(type, block));
    }
    valueSets = null;
    blockBuilders = null;
    dynamicPredicateConsumer.accept(TupleDomain.withColumnDomains(domainsBuilder.build()));
}
 
Example #22
Source File: MapElementAtFunction.java    From presto with Apache License 2.0 5 votes vote down vote up
@UsedByGeneratedCode
public static Object elementAt(MethodHandle keyEqualsMethod, Type keyType, Type valueType, Block map, boolean key)
{
    SingleMapBlock mapBlock = (SingleMapBlock) map;
    int valuePosition = mapBlock.seekKeyExact(key);
    if (valuePosition == -1) {
        return null;
    }
    return readNativeValue(valueType, mapBlock, valuePosition);
}
 
Example #23
Source File: TestScalarFunctionAdapter.java    From presto with Apache License 2.0 5 votes vote down vote up
private static List<Class<?>> toCallArgumentTypes(InvocationConvention callingConvention)
{
    List<Class<?>> expectedArguments = new ArrayList<>();
    for (int i = 0; i < callingConvention.getArgumentConventions().size(); i++) {
        Type argumentType = ARGUMENT_TYPES.get(i);
        InvocationArgumentConvention argumentConvention = callingConvention.getArgumentConvention(i);
        switch (argumentConvention) {
            case NEVER_NULL:
                expectedArguments.add(argumentType.getJavaType());
                break;
            case BOXED_NULLABLE:
                expectedArguments.add(Primitives.wrap(argumentType.getJavaType()));
                break;
            case NULL_FLAG:
                expectedArguments.add(argumentType.getJavaType());
                expectedArguments.add(boolean.class);
                break;
            case BLOCK_POSITION:
                expectedArguments.add(Block.class);
                expectedArguments.add(int.class);
                break;
            default:
                throw new IllegalArgumentException("Unsupported argument convention: " + argumentConvention);
        }
    }
    return expectedArguments;
}
 
Example #24
Source File: TypeOfFunction.java    From presto with Apache License 2.0 5 votes vote down vote up
@TypeParameter("T")
@SqlType(StandardTypes.VARCHAR)
public static Slice typeof(
        @TypeParameter("T") Type type,
        @SqlNullable @SqlType("T") Long value)
{
    return typeof(type, (Object) value);
}
 
Example #25
Source File: AbstractTestAccumuloRowSerializer.java    From presto with Apache License 2.0 5 votes vote down vote up
@Test
public void testTimestamp()
        throws Exception
{
    AccumuloRowSerializer serializer = serializerClass.getConstructor().newInstance();
    Type type = TIMESTAMP;
    Timestamp expected = new Timestamp(new java.util.Date().getTime());
    byte[] data = serializer.encode(type, expected);
    Timestamp actual = new Timestamp(serializer.decode(type, data));
    assertEquals(actual, expected);

    deserializeData(serializer, data);
    actual = serializer.getTimestamp(COLUMN_NAME);
    assertEquals(actual, expected);
}
 
Example #26
Source File: PhoenixClient.java    From presto with Apache License 2.0 5 votes vote down vote up
private static ObjectWriteFunction arrayWriteFunction(ConnectorSession session, Type elementType, String elementJdbcTypeName)
{
    return ObjectWriteFunction.of(Block.class, (statement, index, block) -> {
        Array jdbcArray = statement.getConnection().createArrayOf(elementJdbcTypeName, getJdbcObjectArray(session, elementType, block));
        statement.setArray(index, jdbcArray);
    });
}
 
Example #27
Source File: ArrayNoneMatchFunction.java    From presto with Apache License 2.0 5 votes vote down vote up
@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = long.class)
@SqlType(StandardTypes.BOOLEAN)
@SqlNullable
public static Boolean noneMatchLong(
        @TypeParameter("T") Type elementType,
        @SqlType("array(T)") Block arrayBlock,
        @SqlType("function(T, boolean)") LongToBooleanFunction function)
{
    Boolean anyMatchResult = ArrayAnyMatchFunction.anyMatchLong(elementType, arrayBlock, function);
    if (anyMatchResult == null) {
        return null;
    }
    return !anyMatchResult;
}
 
Example #28
Source File: GroupByHash.java    From presto with Apache License 2.0 5 votes vote down vote up
static GroupByHash createGroupByHash(
        Session session,
        List<? extends Type> hashTypes,
        int[] hashChannels,
        Optional<Integer> inputHashChannel,
        int expectedSize,
        JoinCompiler joinCompiler)
{
    return createGroupByHash(hashTypes, hashChannels, inputHashChannel, expectedSize, isDictionaryAggregationEnabled(session), joinCompiler, NOOP);
}
 
Example #29
Source File: SortedRangeSet.java    From presto with Apache License 2.0 5 votes vote down vote up
/**
 * Provided discrete values that are unioned together to form the SortedRangeSet
 */
static SortedRangeSet of(Type type, Object first, Object... rest)
{
    List<Range> ranges = new ArrayList<>(rest.length + 1);
    ranges.add(Range.equal(type, first));
    for (Object value : rest) {
        ranges.add(Range.equal(type, value));
    }
    return copyOf(type, ranges);
}
 
Example #30
Source File: InMemoryHashAggregationBuilder.java    From presto with Apache License 2.0 5 votes vote down vote up
public List<Type> buildTypes()
{
    ArrayList<Type> types = new ArrayList<>(groupByHash.getTypes());
    for (Aggregator aggregator : aggregators) {
        types.add(aggregator.getType());
    }
    return types;
}