use of com.facebook.presto.spi.ColumnHandle in project carbondata by apache.
the class CarbondataRecordSetProvider method fillFilter2QueryModel.
// Build filter for QueryModel
private void fillFilter2QueryModel(QueryModel queryModel, TupleDomain<ColumnHandle> originalConstraint, CarbonTable carbonTable) {
//queryModel.setFilterExpressionResolverTree(new FilterResolverIntf());
//Build Predicate Expression
ImmutableList.Builder<Expression> filters = ImmutableList.builder();
Domain domain = null;
for (ColumnHandle c : originalConstraint.getDomains().get().keySet()) {
// Build ColumnExpresstion for Expresstion(Carbondata)
CarbondataColumnHandle cdch = (CarbondataColumnHandle) c;
Type type = cdch.getColumnType();
DataType coltype = Spi2CarbondataTypeMapper(cdch);
Expression colExpression = new ColumnExpression(cdch.getColumnName(), coltype);
domain = originalConstraint.getDomains().get().get(c);
checkArgument(domain.getType().isOrderable(), "Domain type must be orderable");
if (domain.getValues().isNone()) {
}
if (domain.getValues().isAll()) {
}
List<Object> singleValues = new ArrayList<>();
List<Expression> rangeFilter = new ArrayList<>();
for (Range range : domain.getValues().getRanges().getOrderedRanges()) {
// Already checked
checkState(!range.isAll());
if (range.isSingleValue()) {
singleValues.add(range.getLow().getValue());
} else {
List<String> rangeConjuncts = new ArrayList<>();
if (!range.getLow().isLowerUnbounded()) {
Object value = ConvertDataByType(range.getLow().getValue(), type);
switch(range.getLow().getBound()) {
case ABOVE:
if (type == TimestampType.TIMESTAMP) {
//todo not now
} else {
GreaterThanExpression greater = new GreaterThanExpression(colExpression, new LiteralExpression(value, coltype));
rangeFilter.add(greater);
}
break;
case EXACTLY:
GreaterThanEqualToExpression greater = new GreaterThanEqualToExpression(colExpression, new LiteralExpression(value, coltype));
rangeFilter.add(greater);
break;
case BELOW:
throw new IllegalArgumentException("Low marker should never use BELOW bound");
default:
throw new AssertionError("Unhandled bound: " + range.getLow().getBound());
}
}
if (!range.getHigh().isUpperUnbounded()) {
Object value = ConvertDataByType(range.getHigh().getValue(), type);
switch(range.getHigh().getBound()) {
case ABOVE:
throw new IllegalArgumentException("High marker should never use ABOVE bound");
case EXACTLY:
LessThanEqualToExpression less = new LessThanEqualToExpression(colExpression, new LiteralExpression(value, coltype));
rangeFilter.add(less);
break;
case BELOW:
LessThanExpression less2 = new LessThanExpression(colExpression, new LiteralExpression(value, coltype));
rangeFilter.add(less2);
break;
default:
throw new AssertionError("Unhandled bound: " + range.getHigh().getBound());
}
}
}
}
if (singleValues.size() == 1) {
Expression ex = null;
if (coltype.equals(DataType.STRING)) {
ex = new EqualToExpression(colExpression, new LiteralExpression(((Slice) singleValues.get(0)).toStringUtf8(), coltype));
} else if (coltype.equals(DataType.TIMESTAMP) || coltype.equals(DataType.DATE)) {
Long value = (Long) singleValues.get(0) * 1000;
ex = new EqualToExpression(colExpression, new LiteralExpression(value, coltype));
} else
ex = new EqualToExpression(colExpression, new LiteralExpression(singleValues.get(0), coltype));
filters.add(ex);
} else if (singleValues.size() > 1) {
ListExpression candidates = null;
List<Expression> exs = singleValues.stream().map((a) -> {
return new LiteralExpression(ConvertDataByType(a, type), coltype);
}).collect(Collectors.toList());
candidates = new ListExpression(exs);
if (candidates != null)
filters.add(new InExpression(colExpression, candidates));
} else if (rangeFilter.size() > 0) {
if (rangeFilter.size() > 1) {
Expression finalFilters = new OrExpression(rangeFilter.get(0), rangeFilter.get(1));
if (rangeFilter.size() > 2) {
for (int i = 2; i < rangeFilter.size(); i++) {
filters.add(new AndExpression(finalFilters, rangeFilter.get(i)));
}
}
} else if (rangeFilter.size() == 1)
filters.add(rangeFilter.get(0));
}
}
Expression finalFilters;
List<Expression> tmp = filters.build();
if (tmp.size() > 1) {
finalFilters = new AndExpression(tmp.get(0), tmp.get(1));
if (tmp.size() > 2) {
for (int i = 2; i < tmp.size(); i++) {
finalFilters = new AndExpression(finalFilters, tmp.get(i));
}
}
} else if (tmp.size() == 1)
finalFilters = tmp.get(0);
else
return;
// todo set into QueryModel
CarbonInputFormatUtil.processFilterExpression(finalFilters, carbonTable);
queryModel.setFilterExpressionResolverTree(CarbonInputFormatUtil.resolveFilter(finalFilters, queryModel.getAbsoluteTableIdentifier()));
}
use of com.facebook.presto.spi.ColumnHandle in project presto by prestodb.
the class BlackHolePageSourceProvider method createPageSource.
@Override
public ConnectorPageSource createPageSource(ConnectorTransactionHandle transactionHandle, ConnectorSession session, ConnectorSplit split, List<ColumnHandle> columns) {
BlackHoleSplit blackHoleSplit = (BlackHoleSplit) split;
ImmutableList.Builder<Type> builder = ImmutableList.builder();
for (ColumnHandle column : columns) {
builder.add(((BlackHoleColumnHandle) column).getColumnType());
}
List<Type> types = builder.build();
Page page = generateZeroPage(types, blackHoleSplit.getRowsPerPage(), blackHoleSplit.getFieldsLength());
return new BlackHolePageSource(page, blackHoleSplit.getPagesCount(), executorService, blackHoleSplit.getPageProcessingDelay());
}
use of com.facebook.presto.spi.ColumnHandle in project presto by prestodb.
the class TestJdbcQueryBuilder method testBuildSqlWithString.
@Test
public void testBuildSqlWithString() throws SQLException {
TupleDomain<ColumnHandle> tupleDomain = TupleDomain.withColumnDomains(ImmutableMap.of(columns.get(3), Domain.create(SortedRangeSet.copyOf(VARCHAR, ImmutableList.of(Range.range(VARCHAR, utf8Slice("test_str_700"), true, utf8Slice("test_str_702"), false), Range.equal(VARCHAR, utf8Slice("test_str_180")), Range.equal(VARCHAR, utf8Slice("test_str_196")))), false)));
Connection connection = database.getConnection();
try (PreparedStatement preparedStatement = new QueryBuilder("\"").buildSql(jdbcClient, connection, "", "", "test_table", columns, tupleDomain);
ResultSet resultSet = preparedStatement.executeQuery()) {
ImmutableSet.Builder<String> builder = ImmutableSet.builder();
while (resultSet.next()) {
builder.add((String) resultSet.getObject("col_3"));
}
assertEquals(builder.build(), ImmutableSet.of("test_str_700", "test_str_701", "test_str_180", "test_str_196"));
assertContains(preparedStatement.toString(), "\"col_3\" >= ?");
assertContains(preparedStatement.toString(), "\"col_3\" < ?");
assertContains(preparedStatement.toString(), "\"col_3\" IN (?,?)");
}
}
use of com.facebook.presto.spi.ColumnHandle in project presto by prestodb.
the class JmxRecordSetProvider method getLiveRow.
public List<Object> getLiveRow(JmxTableHandle tableHandle, List<? extends ColumnHandle> columns, long entryTimestamp) throws JMException {
ImmutableMap<String, Optional<Object>> attributes = getAttributes(getColumnNames(columns), tableHandle);
List<Object> row = new ArrayList<>();
for (ColumnHandle column : columns) {
JmxColumnHandle jmxColumn = (JmxColumnHandle) column;
if (jmxColumn.getColumnName().equals(JmxMetadata.NODE_COLUMN_NAME)) {
row.add(nodeId);
} else if (jmxColumn.getColumnName().equals(JmxMetadata.TIMESTAMP_COLUMN_NAME)) {
row.add(entryTimestamp);
} else {
Optional<Object> optionalValue = attributes.get(jmxColumn.getColumnName());
if (optionalValue == null || !optionalValue.isPresent()) {
row.add(null);
} else {
Object value = optionalValue.get();
Class<?> javaType = jmxColumn.getColumnType().getJavaType();
if (javaType == boolean.class) {
if (value instanceof Boolean) {
row.add(value);
} else {
// mbeans can lie about types
row.add(null);
}
} else if (javaType == long.class) {
if (value instanceof Number) {
row.add(((Number) value).longValue());
} else {
// mbeans can lie about types
row.add(null);
}
} else if (javaType == double.class) {
if (value instanceof Number) {
row.add(((Number) value).doubleValue());
} else {
// mbeans can lie about types
row.add(null);
}
} else if (javaType == Slice.class) {
if (value.getClass().isArray()) {
// return a string representation of the array
if (value.getClass().getComponentType() == boolean.class) {
row.add(Arrays.toString((boolean[]) value));
} else if (value.getClass().getComponentType() == byte.class) {
row.add(Arrays.toString((byte[]) value));
} else if (value.getClass().getComponentType() == char.class) {
row.add(Arrays.toString((char[]) value));
} else if (value.getClass().getComponentType() == double.class) {
row.add(Arrays.toString((double[]) value));
} else if (value.getClass().getComponentType() == float.class) {
row.add(Arrays.toString((float[]) value));
} else if (value.getClass().getComponentType() == int.class) {
row.add(Arrays.toString((int[]) value));
} else if (value.getClass().getComponentType() == long.class) {
row.add(Arrays.toString((long[]) value));
} else if (value.getClass().getComponentType() == short.class) {
row.add(Arrays.toString((short[]) value));
} else {
row.add(Arrays.toString((Object[]) value));
}
} else {
row.add(value.toString());
}
}
}
}
}
return row;
}
use of com.facebook.presto.spi.ColumnHandle in project presto by prestodb.
the class TableScanMatcher method domainMatches.
private boolean domainMatches(TableScanNode tableScanNode, Session session, Metadata metadata) {
if (!expectedConstraint.isPresent()) {
return true;
}
TupleDomain<ColumnHandle> actualConstraint = tableScanNode.getCurrentConstraint();
if (expectedConstraint.isPresent() && !actualConstraint.getDomains().isPresent()) {
return false;
}
Map<String, ColumnHandle> columnHandles = metadata.getColumnHandles(session, tableScanNode.getTable());
for (Map.Entry<String, Domain> expectedColumnConstraint : expectedConstraint.get().entrySet()) {
if (!columnHandles.containsKey(expectedColumnConstraint.getKey())) {
return false;
}
ColumnHandle columnHandle = columnHandles.get(expectedColumnConstraint.getKey());
if (!actualConstraint.getDomains().get().containsKey(columnHandle)) {
return false;
}
if (!expectedColumnConstraint.getValue().contains(actualConstraint.getDomains().get().get(columnHandle))) {
return false;
}
}
return true;
}
Aggregations