@Override public Block toBlock(Type desiredType) { checkArgument(DOUBLE.equals(desiredType), "type doesn't match: %s", desiredType); int numberOfRecords = numberOfRecords(); long[] longs = new long[numberOfRecords]; if (doubles != null) { for (int i = 0; i < numberOfRecords; i++) { longs[i] = doubleToLongBits(doubles[i]); } } return new LongArrayBlock( numberOfRecords, Optional.ofNullable(nulls), longs); }
@Override public Block[] getSequenceBlocks(int start, int length) { BlockBuilder blockBuilder = DOUBLE.createBlockBuilder(null, length); for (int i = start; i < start + length; i++) { DOUBLE.writeDouble(blockBuilder, (double) i); } return new Block[] {blockBuilder.build()}; }
private static void initializePercentilesArray(@AggregationState DigestAndPercentileArrayState state, Block percentilesArrayBlock) { if (state.getPercentiles() == null) { ImmutableList.Builder<Double> percentilesListBuilder = ImmutableList.builder(); for (int i = 0; i < percentilesArrayBlock.getPositionCount(); i++) { checkCondition(!percentilesArrayBlock.isNull(i), INVALID_FUNCTION_ARGUMENT, "Percentile cannot be null"); double percentile = DOUBLE.getDouble(percentilesArrayBlock, i); checkCondition(0 <= percentile && percentile <= 1, INVALID_FUNCTION_ARGUMENT, "Percentile must be between 0 and 1"); percentilesListBuilder.add(percentile); } state.setPercentiles(percentilesListBuilder.build()); } }
@ScalarFunction("values_at_quantiles") @Description("For each input q between [0, 1], find the value whose rank in the sorted sequence of the n values represented by the qdigest is qn.") @SqlType("array(double)") public static Block valuesAtQuantilesDouble(@SqlType("qdigest(double)") Slice input, @SqlType("array(double)") Block percentilesArrayBlock) { QuantileDigest digest = new QuantileDigest(input); BlockBuilder output = DOUBLE.createBlockBuilder(null, percentilesArrayBlock.getPositionCount()); for (int i = 0; i < percentilesArrayBlock.getPositionCount(); i++) { DOUBLE.writeDouble(output, sortableLongToDouble(digest.getQuantile(DOUBLE.getDouble(percentilesArrayBlock, i)))); } return output.build(); }
private static void project(int position, PageBuilder pageBuilder, Block extendedPriceBlock, Block discountBlock) { pageBuilder.declarePosition(); if (discountBlock.isNull(position) || extendedPriceBlock.isNull(position)) { pageBuilder.getBlockBuilder(0).appendNull(); } else { DOUBLE.writeDouble(pageBuilder.getBlockBuilder(0), DOUBLE.getDouble(extendedPriceBlock, position) * DOUBLE.getDouble(discountBlock, position)); } }
@Test public void testStaticMethodScalarParse() { Signature expectedSignature = new Signature( "static_method_scalar", FunctionKind.SCALAR, DOUBLE.getTypeSignature(), ImmutableList.of(DOUBLE.getTypeSignature())); List<SqlScalarFunction> functions = ScalarFromAnnotationsParser.parseFunctionDefinitions(StaticMethodScalarFunction.class); assertEquals(functions.size(), 1); ParametricScalar scalar = (ParametricScalar) functions.get(0); assertEquals(scalar.getSignature(), expectedSignature); assertTrue(scalar.isDeterministic()); assertFalse(scalar.isHidden()); assertEquals(scalar.getDescription(), "Simple scalar with single implementation based on method"); }
public TestSwapAdjacentWindowsBySpecifications() { frame = new WindowNode.Frame( WindowFrame.Type.RANGE, UNBOUNDED_PRECEDING, Optional.empty(), CURRENT_ROW, Optional.empty(), Optional.empty(), Optional.empty()); signature = new Signature( "avg", FunctionKind.WINDOW, ImmutableList.of(), ImmutableList.of(), DOUBLE.getTypeSignature(), ImmutableList.of(BIGINT.getTypeSignature()), false); }
@Override protected List<? extends OperatorFactory> createOperatorFactories() { OperatorFactory tableScanOperator = createTableScanOperator(0, new PlanNodeId("test"), "orders", "totalprice"); InternalAggregationFunction doubleSum = MetadataManager.createTestMetadataManager().getFunctionRegistry().getAggregateFunctionImplementation( new Signature("sum", AGGREGATE, DOUBLE.getTypeSignature(), DOUBLE.getTypeSignature())); AggregationOperatorFactory aggregationOperator = new AggregationOperatorFactory(1, new PlanNodeId("test"), Step.SINGLE, ImmutableList.of(doubleSum.bind(ImmutableList.of(0), Optional.empty())), false); return ImmutableList.of(tableScanOperator, aggregationOperator); }
@Test public void testMultiScalarParse() { Signature expectedSignature1 = new Signature( "static_method_scalar_1", FunctionKind.SCALAR, DOUBLE.getTypeSignature(), ImmutableList.of(DOUBLE.getTypeSignature())); Signature expectedSignature2 = new Signature( "static_method_scalar_2", FunctionKind.SCALAR, BIGINT.getTypeSignature(), ImmutableList.of(BIGINT.getTypeSignature())); List<SqlScalarFunction> functions = ScalarFromAnnotationsParser.parseFunctionDefinitions(MultiScalarFunction.class); assertEquals(functions.size(), 2); ParametricScalar scalar1 = (ParametricScalar) functions.stream().filter(signature -> signature.getSignature().equals(expectedSignature1)).collect(toImmutableList()).get(0); ParametricScalar scalar2 = (ParametricScalar) functions.stream().filter(signature -> signature.getSignature().equals(expectedSignature2)).collect(toImmutableList()).get(0); assertImplementationCount(scalar1, 1, 0, 0); assertImplementationCount(scalar2, 1, 0, 0); assertEquals(scalar1.getSignature(), expectedSignature1); assertTrue(scalar1.isDeterministic()); assertFalse(scalar1.isHidden()); assertEquals(scalar1.getDescription(), "Simple scalar with single implementation based on method 1"); assertEquals(scalar2.getSignature(), expectedSignature2); assertFalse(scalar2.isDeterministic()); assertTrue(scalar2.isHidden()); assertEquals(scalar2.getDescription(), "Simple scalar with single implementation based on method 2"); }
@Override protected List<? extends OperatorFactory> createOperatorFactories() { OperatorFactory tableScanOperator = createTableScanOperator(0, new PlanNodeId("test"), "orders", "totalprice"); RowExpression filter = call( internalOperator(GREATER_THAN_OR_EQUAL, BOOLEAN.getTypeSignature(), ImmutableList.of(DOUBLE.getTypeSignature(), DOUBLE.getTypeSignature())), BOOLEAN, field(0, DOUBLE), constant(50000.0, DOUBLE)); ExpressionCompiler expressionCompiler = new ExpressionCompiler(localQueryRunner.getMetadata(), new PageFunctionCompiler(localQueryRunner.getMetadata(), 0)); Supplier<PageProcessor> pageProcessor = expressionCompiler.compilePageProcessor(Optional.of(filter), ImmutableList.of(field(0, DOUBLE))); FilterAndProjectOperator.FilterAndProjectOperatorFactory filterAndProjectOperator = new FilterAndProjectOperator.FilterAndProjectOperatorFactory( 1, new PlanNodeId("test"), pageProcessor, ImmutableList.of(DOUBLE), new DataSize(0, BYTE), 0); return ImmutableList.of(tableScanOperator, filterAndProjectOperator); }
@Test public void testFileVarbinarySpiller() throws Exception { List<Type> types = ImmutableList.of(BIGINT, DOUBLE, VARBINARY); BlockBuilder col1 = BIGINT.createBlockBuilder(null, 1); BlockBuilder col2 = DOUBLE.createBlockBuilder(null, 1); BlockBuilder col3 = VARBINARY.createBlockBuilder(null, 1); col1.writeLong(42).closeEntry(); col2.writeLong(doubleToLongBits(43.0)).closeEntry(); col3.writeLong(doubleToLongBits(43.0)).writeLong(1).closeEntry(); Page page = new Page(col1.build(), col2.build(), col3.build()); try (Spiller spiller = factory.create(TYPES, bytes -> {}, memoryContext)) { testSpiller(types, spiller, ImmutableList.of(page)); } }
private static Page makeInput(int numberOfBuckets) { PageBuilder builder = new PageBuilder(ImmutableList.of(BIGINT, DOUBLE, DOUBLE)); for (int i = 0; i < 100; i++) { builder.declarePosition(); BIGINT.writeLong(builder.getBlockBuilder(0), numberOfBuckets); DOUBLE.writeDouble(builder.getBlockBuilder(1), i); // value DOUBLE.writeDouble(builder.getBlockBuilder(2), 1); // weight } return builder.build(); } }
@Test public void testColumnHandleDeserialize() throws Exception { String json = objectMapper.writeValueAsString(COLUMN_HANDLE_AS_MAP); HiveColumnHandle columnHandle = objectMapper.readValue(json, HiveColumnHandle.class); assertEquals(columnHandle.getName(), "column"); assertEquals(columnHandle.getTypeSignature(), DOUBLE.getTypeSignature()); assertEquals(columnHandle.getHiveType(), HiveType.HIVE_FLOAT); assertEquals(columnHandle.getHiveColumnIndex(), -1); assertEquals(columnHandle.isPartitionKey(), true); }
@Test public void testBigint() { List<RowExpression> values = new ArrayList<>(); values.add(constant(Integer.MAX_VALUE + 1L, BIGINT)); values.add(constant(Integer.MIN_VALUE - 1L, BIGINT)); values.add(constant(3L, BIGINT)); assertEquals(checkSwitchGenerationCase(BIGINT, values), HASH_SWITCH); values.add(constant(null, BIGINT)); assertEquals(checkSwitchGenerationCase(BIGINT, values), HASH_SWITCH); values.add(new CallExpression( new Signature( CAST, SCALAR, BIGINT.getTypeSignature(), DOUBLE.getTypeSignature()), BIGINT, Collections.singletonList(constant(12345678901234.0, DOUBLE)))); assertEquals(checkSwitchGenerationCase(BIGINT, values), HASH_SWITCH); for (long i = 6; i <= 32; ++i) { values.add(constant(i, BIGINT)); } assertEquals(checkSwitchGenerationCase(BIGINT, values), HASH_SWITCH); values.add(constant(33L, BIGINT)); assertEquals(checkSwitchGenerationCase(BIGINT, values), SET_CONTAINS); }
@Test public void testInteger() { List<RowExpression> values = new ArrayList<>(); values.add(constant(Integer.MIN_VALUE, INTEGER)); values.add(constant(Integer.MAX_VALUE, INTEGER)); values.add(constant(3, INTEGER)); assertEquals(checkSwitchGenerationCase(INTEGER, values), DIRECT_SWITCH); values.add(constant(null, INTEGER)); assertEquals(checkSwitchGenerationCase(INTEGER, values), DIRECT_SWITCH); values.add(new CallExpression( new Signature( CAST, SCALAR, INTEGER.getTypeSignature(), DOUBLE.getTypeSignature()), INTEGER, Collections.singletonList(constant(12345678901234.0, DOUBLE)))); assertEquals(checkSwitchGenerationCase(INTEGER, values), DIRECT_SWITCH); for (int i = 6; i <= 32; ++i) { values.add(constant(i, INTEGER)); } assertEquals(checkSwitchGenerationCase(INTEGER, values), DIRECT_SWITCH); values.add(constant(33, INTEGER)); assertEquals(checkSwitchGenerationCase(INTEGER, values), SET_CONTAINS); }
.map(type -> format.supportsDate() || !DATE.equals(type) ? type : createUnboundedVarcharType()) .collect(toList()); ImmutableList.Builder<Page> pages = ImmutableList.builder(); long dataSize = 0; for (E row : tpchTable.createGenerator(10, 1, 1)) { switch (column.getType().getBase()) { case IDENTIFIER: BIGINT.writeLong(blockBuilder, column.getIdentifier(row)); break; case INTEGER: INTEGER.writeLong(blockBuilder, column.getInteger(row)); break; case DATE: if (format.supportsDate()) { DATE.writeLong(blockBuilder, column.getDate(row)); DOUBLE.writeDouble(blockBuilder, column.getDouble(row)); break; case VARCHAR:
private List<Page> createInputPages() { ImmutableList.Builder<Page> pages = ImmutableList.builder(); PageBuilder pageBuilder = new PageBuilder(TYPES); LineItemGenerator lineItemGenerator = new LineItemGenerator(1, 1, 1); for (int j = 0; j < pagesCount; j++) { Iterator<LineItem> iterator = lineItemGenerator.iterator(); for (int i = 0; i < rowsPerPage; i++) { pageBuilder.declarePosition(); LineItem lineItem = iterator.next(); BIGINT.writeLong(pageBuilder.getBlockBuilder(0), lineItem.getOrderKey()); BIGINT.writeLong(pageBuilder.getBlockBuilder(1), lineItem.getDiscountPercent()); DOUBLE.writeDouble(pageBuilder.getBlockBuilder(2), lineItem.getDiscount()); VARCHAR.writeString(pageBuilder.getBlockBuilder(3), lineItem.getReturnFlag()); DOUBLE.writeDouble(pageBuilder.getBlockBuilder(4), lineItem.getExtendedPrice()); } pages.add(pageBuilder.build()); pageBuilder.reset(); } return pages.build(); }
private static boolean convertibleToDoubleWithCast(Type type) { return type instanceof DecimalType || DoubleType.DOUBLE.equals(type) || RealType.REAL.equals(type) || BigintType.BIGINT.equals(type) || IntegerType.INTEGER.equals(type) || SmallintType.SMALLINT.equals(type) || TinyintType.TINYINT.equals(type) || BooleanType.BOOLEAN.equals(type); } }
@Override public Block[] getSequenceBlocks(int start, int length) { BlockBuilder blockBuilder = DOUBLE.createBlockBuilder(new BlockBuilderStatus(), length); for (int i = start; i < start + length; i++) { DOUBLE.writeDouble(blockBuilder, (double) i); } return new Block[] {blockBuilder.build()}; }
private Page buildPage() { BlockBuilder col1 = BIGINT.createBlockBuilder(null, 1); BlockBuilder col2 = DOUBLE.createBlockBuilder(null, 1); BlockBuilder col3 = VARBINARY.createBlockBuilder(null, 1); col1.writeLong(42).closeEntry(); col2.writeLong(doubleToLongBits(43.0)).closeEntry(); col3.writeLong(doubleToLongBits(43.0)).writeLong(1).closeEntry(); return new Page(col1.build(), col2.build(), col3.build()); } }