@Test public void testAlternateEscapedCharacters() { Regex regex = likePattern(utf8Slice("xxx%x_abcxx"), utf8Slice("x")); assertTrue(likeVarchar(utf8Slice("x%_abcx"), regex)); }
@SuppressWarnings("NumericCastThatLosesPrecision") @Test(timeOut = 1000) public void testLikeInvalidUtf8Value() { Slice value = Slices.wrappedBuffer(new byte[] {'a', 'b', 'c', (byte) 0xFF, 'x', 'y'}); Regex regex = likePattern(utf8Slice("%b%"), utf8Slice("\\")); assertTrue(likeVarchar(value, regex)); }
private static void assertMultiply256(Slice left, Slice right, Slice expected) { Slice actual = Slices.allocate(Long.BYTES * 4); multiply256(left, right, actual); assertEquals(actual, expected); }
@Override public Block getSingleValueBlock(int position) { checkReadablePosition(position); Slice copy = Slices.copyOf(getRawSlice(), valueOffset(position), fixedSize); Slice valueIsNull = null; if (isNull(position)) { valueIsNull = Slices.wrappedBooleanArray(true); } return new FixedWidthBlock(fixedSize, 1, copy, valueIsNull); }
@Test public void ruleAddedTableLayoutToFilterTableScan() { Map<String, Domain> filterConstraint = ImmutableMap.<String, Domain>builder() .put("orderstatus", singleValue(createVarcharType(1), utf8Slice("F"))) .build(); tester().assertThat(pickTableLayout.pickTableLayoutForPredicate()) .on(p -> p.filter(expression("orderstatus = CAST ('F' AS VARCHAR(1))"), p.tableScan( ordersTableHandle, ImmutableList.of(p.symbol("orderstatus", createVarcharType(1))), ImmutableMap.of(p.symbol("orderstatus", createVarcharType(1)), new TpchColumnHandle("orderstatus", createVarcharType(1)))))) .matches( constrainedTableScanWithTableLayout("orders", filterConstraint, ImmutableMap.of("orderstatus", "orderstatus"))); }
@Test public void testBuildQueryIn() { TupleDomain<ColumnHandle> tupleDomain = TupleDomain.withColumnDomains(ImmutableMap.of( COL2, Domain.create(ValueSet.ofRanges(equal(createUnboundedVarcharType(), utf8Slice("hello")), equal(createUnboundedVarcharType(), utf8Slice("world"))), false))); Document query = MongoSession.buildQuery(tupleDomain); Document expected = new Document(COL2.getName(), new Document("$in", ImmutableList.of("hello", "world"))); assertEquals(query, expected); }
@Test public void testCreateMixedPredicate() { ImmutableList.Builder<HivePartition> partitions = ImmutableList.builder(); for (int i = 0; i < 5; i++) { partitions.add(new HivePartition( new SchemaTableName("test", "test"), Integer.toString(i), ImmutableMap.of(TEST_COLUMN_HANDLE, NullableValue.of(VarcharType.VARCHAR, Slices.utf8Slice(Integer.toString(i)))))); } partitions.add(new HivePartition( new SchemaTableName("test", "test"), "null", ImmutableMap.of(TEST_COLUMN_HANDLE, NullableValue.asNull(VarcharType.VARCHAR)))); createPredicate(ImmutableList.of(TEST_COLUMN_HANDLE), partitions.build()); } }
@Test public void testExtractFixedValues() { assertEquals( TupleDomain.extractFixedValues(TupleDomain.withColumnDomains( ImmutableMap.<ColumnHandle, Domain>builder() .put(A, Domain.all(DOUBLE)) .put(B, Domain.singleValue(VARCHAR, utf8Slice("value"))) .put(C, Domain.onlyNull(BIGINT)) .put(D, Domain.create(ValueSet.ofRanges(Range.equal(BIGINT, 1L)), true)) .build())).get(), ImmutableMap.of( B, NullableValue.of(VARCHAR, utf8Slice("value")), C, NullableValue.asNull(BIGINT))); }
@Test public void testFromValueSetOf() { PrestoThriftValueSet thriftValueSet = fromValueSet(ValueSet.of(JSON, utf8Slice(JSON1), utf8Slice(JSON2))); assertNotNull(thriftValueSet.getEquatableValueSet()); assertTrue(thriftValueSet.getEquatableValueSet().isWhiteList()); assertEquals(thriftValueSet.getEquatableValueSet().getValues(), ImmutableList.of( jsonData(new PrestoThriftJson(null, new int[] {JSON1.length()}, JSON1.getBytes(UTF_8))), jsonData(new PrestoThriftJson(null, new int[] {JSON2.length()}, JSON2.getBytes(UTF_8))))); } }
@Test public void testMismatchedColumnWiseUnion() { TupleDomain<ColumnHandle> tupleDomain1 = TupleDomain.withColumnDomains( ImmutableMap.of( A, Domain.all(DOUBLE), B, Domain.singleValue(VARCHAR, utf8Slice("value")))); TupleDomain<ColumnHandle> tupleDomain2 = TupleDomain.withColumnDomains( ImmutableMap.of( A, Domain.create(ValueSet.ofRanges(Range.greaterThanOrEqual(DOUBLE, 0.0)), true), C, Domain.singleValue(BIGINT, 1L))); TupleDomain<ColumnHandle> expectedTupleDomain = TupleDomain.withColumnDomains(ImmutableMap.of(A, Domain.all(DOUBLE))); assertEquals(columnWiseUnion(tupleDomain1, tupleDomain2), expectedTupleDomain); }
@Test public void testSanityRLE() { PageProcessor processor = compiler.compilePageProcessor(Optional.empty(), ImmutableList.of(field(0, BIGINT), field(1, VARCHAR)), MAX_BATCH_SIZE).get(); Slice varcharValue = Slices.utf8Slice("hello"); Page page = new Page(RunLengthEncodedBlock.create(BIGINT, 123L, 100), RunLengthEncodedBlock.create(VARCHAR, varcharValue, 100)); Page outputPage = getOnlyElement( processor.process( null, new DriverYieldSignal(), newSimpleAggregatedMemoryContext().newLocalMemoryContext(PageProcessor.class.getSimpleName()), page)) .orElseThrow(() -> new AssertionError("page is not present")); assertEquals(outputPage.getPositionCount(), 100); assertTrue(outputPage.getBlock(0) instanceof RunLengthEncodedBlock); assertTrue(outputPage.getBlock(1) instanceof RunLengthEncodedBlock); RunLengthEncodedBlock rleBlock = (RunLengthEncodedBlock) outputPage.getBlock(0); assertEquals(BIGINT.getLong(rleBlock.getValue(), 0), 123L); RunLengthEncodedBlock rleBlock1 = (RunLengthEncodedBlock) outputPage.getBlock(1); assertEquals(VARCHAR.getSlice(rleBlock1.getValue(), 0), varcharValue); }
@Test public void testMapHashOperator() { assertMapHashOperator("MAP(ARRAY[1], ARRAY[2])", INTEGER, INTEGER, ImmutableList.of(1, 2)); assertMapHashOperator("MAP(ARRAY[1, 2147483647], ARRAY[2147483647, 2])", INTEGER, INTEGER, ImmutableList.of(1, 2147483647, 2147483647, 2)); assertMapHashOperator("MAP(ARRAY[8589934592], ARRAY[2])", BIGINT, INTEGER, ImmutableList.of(8589934592L, 2)); assertMapHashOperator("MAP(ARRAY[true], ARRAY[false])", BOOLEAN, BOOLEAN, ImmutableList.of(true, false)); assertMapHashOperator("MAP(ARRAY['123'], ARRAY['456'])", VARCHAR, VARCHAR, ImmutableList.of(utf8Slice("123"), utf8Slice("456"))); }
@Test public void testGetPartitionSplitsTableOfflinePartition() { try (Transaction transaction = newTransaction()) { ConnectorMetadata metadata = transaction.getMetadata(); ConnectorSession session = newSession(); ConnectorTableHandle tableHandle = getTableHandle(metadata, tableOfflinePartition); assertNotNull(tableHandle); ColumnHandle dsColumn = metadata.getColumnHandles(session, tableHandle).get("ds"); assertNotNull(dsColumn); Domain domain = Domain.singleValue(createUnboundedVarcharType(), utf8Slice("2012-12-30")); TupleDomain<ColumnHandle> tupleDomain = TupleDomain.withColumnDomains(ImmutableMap.of(dsColumn, domain)); List<ConnectorTableLayoutResult> tableLayoutResults = metadata.getTableLayouts(session, tableHandle, new Constraint<>(tupleDomain), Optional.empty()); try { getSplitCount(splitManager.getSplits(transaction.getTransactionHandle(), session, getOnlyElement(tableLayoutResults).getTableLayout().getHandle(), UNGROUPED_SCHEDULING)); fail("Expected PartitionOfflineException"); } catch (PartitionOfflineException e) { assertEquals(e.getTableName(), tableOfflinePartition); assertEquals(e.getPartition(), "ds=2012-12-30"); } } }
@Test public void testExtractValuesFromSingleDomain() { Map<Type, Object> testValues = ImmutableMap.<Type, Object>builder() .put(BOOLEAN, true) .put(INTEGER, 1234L) .put(BIGINT, 4321L) .put(DOUBLE, 0.123) .put(VARCHAR, utf8Slice(TEST_STRING)) .build(); for (Map.Entry<Type, Object> testValue : testValues.entrySet()) { Domain predicateDomain = Domain.singleValue(testValue.getKey(), testValue.getValue()); Optional<Collection<Object>> discreteValues = extractDiscreteValues(predicateDomain.getValues()); assertTrue(discreteValues.isPresent()); Collection<Object> objects = discreteValues.get(); assertEquals(objects.size(), 1); assertEquals(objects.iterator().next(), testValue.getValue()); } }
@Test public void testExpressionConstantFolding() { Expression originalExpression = comparison(GREATER_THAN, C_VARBINARY.toSymbolReference(), function("from_hex", stringLiteral("123456"))); ExtractionResult result = fromPredicate(originalExpression); assertEquals(result.getRemainingExpression(), TRUE_LITERAL); Slice value = Slices.wrappedBuffer(BaseEncoding.base16().decode("123456")); assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_VARBINARY, Domain.create(ValueSet.ofRanges(Range.greaterThan(VARBINARY, value)), false)))); Expression expression = toPredicate(result.getTupleDomain()); assertEquals(expression, comparison(GREATER_THAN, C_VARBINARY.toSymbolReference(), varbinaryLiteral(value))); }
@Test private void testSplitPageNonDecreasingPageSize() { int positionCount = 100; int maxPageSizeInBytes = 1; List<Type> types = ImmutableList.of(VARCHAR); Slice expectedValue = wrappedBuffer("test".getBytes()); BlockBuilder blockBuilder = VARCHAR.createBlockBuilder(null, 1, expectedValue.length()); blockBuilder.writeBytes(expectedValue, 0, expectedValue.length()).closeEntry(); Block rleBlock = new RunLengthEncodedBlock(blockBuilder.build(), positionCount); Page initialPage = new Page(rleBlock); List<Page> pages = splitPage(initialPage, maxPageSizeInBytes); // the page should only be split in half as the recursion should terminate // after seeing that the size of the Page doesn't decrease assertEquals(pages.size(), 2); Page first = pages.get(0); Page second = pages.get(1); // the size of the pages will remain the same and should be greater than the maxPageSizeInBytes assertGreaterThan((int) first.getSizeInBytes(), maxPageSizeInBytes); assertGreaterThan((int) second.getSizeInBytes(), maxPageSizeInBytes); assertPositionCount(pages, positionCount); MaterializedResult actual = toMaterializedResult(TEST_SESSION, types, pages); MaterializedResult expected = toMaterializedResult(TEST_SESSION, types, ImmutableList.of(initialPage)); assertEquals(actual, expected); } }
@Test public void testSingleValuesMapToDomain() { assertEquals( TupleDomain.fromFixedValues( ImmutableMap.<ColumnHandle, NullableValue>builder() .put(A, NullableValue.of(BIGINT, 1L)) .put(B, NullableValue.of(VARCHAR, utf8Slice("value"))) .put(C, NullableValue.of(DOUBLE, 0.01)) .put(D, NullableValue.asNull(BOOLEAN)) .build()), TupleDomain.withColumnDomains(ImmutableMap.<ColumnHandle, Domain>builder() .put(A, Domain.singleValue(BIGINT, 1L)) .put(B, Domain.singleValue(VARCHAR, utf8Slice("value"))) .put(C, Domain.singleValue(DOUBLE, 0.01)) .put(D, Domain.onlyNull(BOOLEAN)) .build())); }
@Test public void testLikeChar() { Regex regex = likePattern(utf8Slice("f%b__")); assertTrue(likeChar(6L, utf8Slice("foobar"), regex)); assertTrue(likeChar(6L, utf8Slice("foob"), regex)); assertFalse(likeChar(7L, utf8Slice("foob"), regex)); assertFunction("cast('foob' as char(6)) LIKE 'f%b__'", BOOLEAN, true); assertFunction("cast('foob' as char(7)) LIKE 'f%b__'", BOOLEAN, false); }
@Test public void testSkipReservedSlots() { Set<Integer> positions = new HashSet<>(); DictionaryBuilder dictionaryBuilder = new DictionaryBuilder(64); for (int i = 0; i < 64; i++) { positions.add(dictionaryBuilder.putIfAbsent(new TestHashCollisionBlock(1, wrappedBuffer(new byte[] {1}), new int[] {0, 1}, new boolean[] {false}), 0)); positions.add(dictionaryBuilder.putIfAbsent(new TestHashCollisionBlock(1, wrappedBuffer(new byte[] {2}), new int[] {0, 1}, new boolean[] {false}), 0)); } assertEquals(positions, ImmutableSet.of(1, 2)); }
private Map<ColumnHandle, NullableValue> asFixedValues(QualifiedObjectName objectName) { return ImmutableMap.of( CATALOG_COLUMN_HANDLE, new NullableValue(createUnboundedVarcharType(), utf8Slice(objectName.getCatalogName())), SCHEMA_COLUMN_HANDLE, new NullableValue(createUnboundedVarcharType(), utf8Slice(objectName.getSchemaName())), TABLE_NAME_COLUMN_HANDLE, new NullableValue(createUnboundedVarcharType(), utf8Slice(objectName.getObjectName()))); }