Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand support of types handled in EXPLAIN (TYPE IO) #509

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.common.collect.ImmutableSet;
import io.prestosql.Session;
import io.prestosql.metadata.Metadata;
import io.prestosql.metadata.OperatorNotFoundException;
import io.prestosql.metadata.TableHandle;
import io.prestosql.metadata.TableMetadata;
import io.prestosql.spi.PrestoException;
Expand All @@ -28,20 +29,8 @@
import io.prestosql.spi.predicate.Marker;
import io.prestosql.spi.predicate.Marker.Bound;
import io.prestosql.spi.predicate.TupleDomain;
import io.prestosql.spi.type.BigintType;
import io.prestosql.spi.type.BooleanType;
import io.prestosql.spi.type.CharType;
import io.prestosql.spi.type.DateType;
import io.prestosql.spi.type.DecimalType;
import io.prestosql.spi.type.DoubleType;
import io.prestosql.spi.type.IntegerType;
import io.prestosql.spi.type.SmallintType;
import io.prestosql.spi.type.TimeType;
import io.prestosql.spi.type.TimestampType;
import io.prestosql.spi.type.TinyintType;
import io.prestosql.spi.type.Type;
import io.prestosql.spi.type.TypeSignature;
import io.prestosql.spi.type.VarcharType;
import io.prestosql.sql.planner.plan.PlanNode;
import io.prestosql.sql.planner.plan.PlanVisitor;
import io.prestosql.sql.planner.plan.TableFinishNode;
Expand All @@ -66,21 +55,12 @@
import static io.airlift.json.JsonCodec.jsonCodec;
import static io.prestosql.spi.StandardErrorCode.NOT_SUPPORTED;
import static io.prestosql.spi.predicate.Marker.Bound.EXACTLY;
import static io.prestosql.sql.planner.planPrinter.PlanPrinterUtil.castToVarchar;
import static io.prestosql.sql.planner.planPrinter.PlanPrinterUtil.throwOrCastToVarchar;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

public class IoPlanPrinter
{
/**
* List of supported data types for column constraints appearing in
* EXPLAIN (TYPE IO). Other types would cause a NOT_SUPPORTED
* Presto exception to be thrown.
*/
private static final Set<Class> WHITELISTED_TYPES_FOR_OUTPUT = ImmutableSet.of(
VarcharType.class, TinyintType.class, SmallintType.class, IntegerType.class, BigintType.class,
BooleanType.class, CharType.class, DecimalType.class, DoubleType.class, DateType.class,
TimeType.class, TimestampType.class);
private final Metadata metadata;
private final Session session;

Expand Down Expand Up @@ -579,12 +559,14 @@ private FormattedMarker formatMarker(Marker marker)

private String getVarcharValue(Type type, Object value)
{
for (Class<Type> whitelistedType : WHITELISTED_TYPES_FOR_OUTPUT) {
if (whitelistedType.getClass().isInstance(type.getClass())) {
return castToVarchar(type, value, metadata.getFunctionRegistry(), session);
}
try {
return throwOrCastToVarchar(type, value, metadata.getFunctionRegistry(), session);
}
catch (OperatorNotFoundException e) {
throw new PrestoException(
JamesRTaylor marked this conversation as resolved.
Show resolved Hide resolved
NOT_SUPPORTED,
format("Unsupported data type in EXPLAIN (TYPE IO): %s", type.getDisplayName()), e);
}
throw new PrestoException(NOT_SUPPORTED, format("Unsupported data type in EXPLAIN (TYPE IO): %s", type.getDisplayName()));
}

private Void processChildren(PlanNode node, IoPlanBuilder context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,23 @@ private PlanPrinterUtil() {}

static String castToVarchar(Type type, Object value, FunctionRegistry functionRegistry, Session session)
{
if (value == null) {
return "NULL";
}

try {
Signature coercion = functionRegistry.getCoercion(type, VARCHAR);
Slice coerced = (Slice) new InterpretedFunctionInvoker(functionRegistry).invoke(coercion, session.toConnectorSession(), value);
return coerced.toStringUtf8();
return throwOrCastToVarchar(type, value, functionRegistry, session);
}
catch (OperatorNotFoundException e) {
return "<UNREPRESENTABLE VALUE>";
}
}

static String throwOrCastToVarchar(Type type, Object value, FunctionRegistry functionRegistry, Session session)
JamesRTaylor marked this conversation as resolved.
Show resolved Hide resolved
throws OperatorNotFoundException
{
if (value == null) {
return "NULL";
}

Signature coercion = functionRegistry.getCoercion(type, VARCHAR);
Slice coerced = (Slice) new InterpretedFunctionInvoker(functionRegistry).invoke(coercion, session.toConnectorSession(), value);
return coerced.toStringUtf8();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.prestosql.sql.planner.planPrinter;

import io.prestosql.Session;
import io.prestosql.metadata.Metadata;
import io.prestosql.metadata.OperatorNotFoundException;
import io.prestosql.spi.block.Block;
import io.prestosql.spi.block.IntArrayBlock;
import io.prestosql.spi.block.MethodHandleUtil;
import io.prestosql.spi.type.ArrayType;
import io.prestosql.spi.type.IntegerType;
import io.prestosql.spi.type.MapType;
import io.prestosql.spi.type.Type;
import io.prestosql.tests.AbstractTestQueryFramework;
import io.prestosql.tests.TestLocalQueries;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static io.prestosql.block.BlockAssertions.createLongsBlock;
import static io.prestosql.block.BlockAssertions.createStringsBlock;
import static io.prestosql.spi.type.BigintType.BIGINT;
import static io.prestosql.spi.type.VarcharType.VARCHAR;
import static io.prestosql.util.StructuralTestUtil.mapType;

public class TestPlanPrinterUtil
extends AbstractTestQueryFramework
JamesRTaylor marked this conversation as resolved.
Show resolved Hide resolved
{
public TestPlanPrinterUtil()
{
super(TestLocalQueries::createLocalQueryRunner);
}

@Test(expectedExceptions = OperatorNotFoundException.class)
public void testCastArrayToVarcharFails()
{
Type type = new ArrayType(IntegerType.INTEGER);
Object value = new IntArrayBlock(3, java.util.Optional.empty(), new int[] {1, 2, 3});
Session session = getSession();
Metadata metadata = getQueryRunner().getMetadata();
PlanPrinterUtil.throwOrCastToVarchar(type, value, metadata.getFunctionRegistry(), session);
}

@Test(expectedExceptions = OperatorNotFoundException.class)
public void testCastMapToVarcharFails()
{
MapType type = new MapType(
BIGINT,
VARCHAR,
MethodHandleUtil.methodHandle(TestPlanPrinterUtil.class, "throwUnsupportedOperation"),
MethodHandleUtil.methodHandle(TestPlanPrinterUtil.class, "throwUnsupportedOperation"),
MethodHandleUtil.methodHandle(TestPlanPrinterUtil.class, "throwUnsupportedOperation"),
MethodHandleUtil.methodHandle(TestPlanPrinterUtil.class, "throwUnsupportedOperation"));
Object value = createBlockWithValuesFromKeyValueBlock(createTestMap(1));
Session session = getSession();
Metadata metadata = getQueryRunner().getMetadata();
PlanPrinterUtil.throwOrCastToVarchar(type, value, metadata.getFunctionRegistry(), session);
}

public static void throwUnsupportedOperation()
{
throw new UnsupportedOperationException();
}

private Map<String, Long>[] createTestMap(int... entryCounts)
{
Map<String, Long>[] result = new Map[entryCounts.length];
for (int rowNumber = 0; rowNumber < entryCounts.length; rowNumber++) {
int entryCount = entryCounts[rowNumber];
Map<String, Long> map = new HashMap<>();
for (int entryNumber = 0; entryNumber < entryCount; entryNumber++) {
map.put("key" + entryNumber, entryNumber == 5 ? null : rowNumber * 100L + entryNumber);
}
result[rowNumber] = map;
}
return result;
}

private Block createBlockWithValuesFromKeyValueBlock(Map<String, Long>[] maps)
{
List<String> keys = new ArrayList<>();
List<Long> values = new ArrayList<>();
int[] offsets = new int[maps.length + 1];
boolean[] mapIsNull = new boolean[maps.length];
for (int i = 0; i < maps.length; i++) {
Map<String, Long> map = maps[i];
mapIsNull[i] = map == null;
if (map == null) {
offsets[i + 1] = offsets[i];
}
else {
for (Map.Entry<String, Long> entry : map.entrySet()) {
keys.add(entry.getKey());
values.add(entry.getValue());
}
offsets[i + 1] = offsets[i] + map.size();
}
}
return mapType(VARCHAR, BIGINT).createBlockFromKeyValue(Optional.of(mapIsNull), offsets, createStringsBlock(keys), createLongsBlock(values));
}
}