Skip to content

Commit

Permalink
Expand support of types handled in EXPLAIN (TYPE IO)
Browse files Browse the repository at this point in the history
It will now work with any type that has a cast to varchar.
  • Loading branch information
James Taylor authored and martint committed Apr 2, 2019
1 parent dc11eba commit 20b0a5f
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 36 deletions.
Expand Up @@ -33,8 +33,19 @@
import io.prestosql.spi.connector.Constraint;
import io.prestosql.spi.security.Identity;
import io.prestosql.spi.security.SelectedRole;
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.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;
import io.prestosql.sql.planner.plan.ExchangeNode;
import io.prestosql.sql.planner.planprinter.IoPlanPrinter.ColumnConstraint;
Expand Down Expand Up @@ -232,6 +243,51 @@ public void testIOExplain()
assertUpdate("DROP TABLE test_orders");
}

@Test
public void testIoExplainWithPrimitiveTypes()
{
Map<Object, Type> data = new HashMap<>();
data.put("foo", VarcharType.createUnboundedVarcharType());
data.put(Byte.toString((byte) (Byte.MAX_VALUE / 2)), TinyintType.TINYINT);
data.put(Short.toString((short) (Short.MAX_VALUE / 2)), SmallintType.SMALLINT);
data.put(Integer.toString(Integer.MAX_VALUE / 2), IntegerType.INTEGER);
data.put(Long.toString(Long.MAX_VALUE / 2), BigintType.BIGINT);
data.put(Boolean.TRUE.toString(), BooleanType.BOOLEAN);
data.put("bar", CharType.createCharType(3));
data.put("1.2345678901234578E14", DoubleType.DOUBLE);
data.put("123456789012345678901234.567", DecimalType.createDecimalType(30, 3));
data.put("2019-01-01", DateType.DATE);
data.put("2019-01-01 23:22:21.123", TimestampType.TIMESTAMP);
for (Map.Entry<Object, Type> entry : data.entrySet()) {
@Language("SQL") String query = String.format(
"CREATE TABLE test_types_table WITH (partitioned_by = ARRAY['my_col']) AS " +
"SELECT 'foo' my_non_partition_col, CAST('%s' AS %s) my_col",
entry.getKey(),
entry.getValue().getDisplayName());

assertUpdate(query, 1);
MaterializedResult result = computeActual("EXPLAIN (TYPE IO, FORMAT JSON) SELECT * FROM test_types_table");
assertEquals(
jsonCodec(IoPlan.class).fromJson((String) getOnlyElement(result.getOnlyColumnAsSet())),
new IoPlan(
ImmutableSet.of(new TableColumnInfo(
new CatalogSchemaTableName(catalog, "tpch", "test_types_table"),
ImmutableSet.of(
new ColumnConstraint(
"my_col",
entry.getValue().getTypeSignature(),
new FormattedDomain(
false,
ImmutableSet.of(
new FormattedRange(
new FormattedMarker(Optional.of(entry.getKey().toString()), EXACTLY),
new FormattedMarker(Optional.of(entry.getKey().toString()), EXACTLY)))))))),
Optional.empty()));

assertUpdate("DROP TABLE test_types_table");
}
}

@Test
public void testReadNoColumns()
{
Expand Down
Expand Up @@ -16,9 +16,9 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.ImmutableSet;
import io.airlift.slice.Slice;
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 @@ -29,14 +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.IntegerType;
import io.prestosql.spi.type.SmallintType;
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 @@ -61,6 +55,7 @@
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.castToVarcharOrFail;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

Expand Down Expand Up @@ -565,16 +560,12 @@ private FormattedMarker formatMarker(Marker marker)

private String getVarcharValue(Type type, Object value)
{
if (type instanceof VarcharType) {
return ((Slice) value).toStringUtf8();
try {
return castToVarcharOrFail(type, value, metadata.getFunctionRegistry(), session);
}
if (type instanceof TinyintType || type instanceof SmallintType || type instanceof IntegerType || type instanceof BigintType) {
return ((Long) value).toString();
catch (OperatorNotFoundException e) {
throw new PrestoException(NOT_SUPPORTED, format("Unsupported data type in EXPLAIN (TYPE IO): %s", type.getDisplayName()), e);
}
if (type instanceof BooleanType) {
return ((Boolean) value).toString();
}
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
Expand Up @@ -21,7 +21,6 @@
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Streams;
import io.airlift.slice.Slice;
import io.airlift.units.Duration;
import io.prestosql.Session;
import io.prestosql.SystemSessionProperties;
Expand All @@ -32,8 +31,6 @@
import io.prestosql.execution.StageStats;
import io.prestosql.metadata.FunctionRegistry;
import io.prestosql.metadata.Metadata;
import io.prestosql.metadata.OperatorNotFoundException;
import io.prestosql.metadata.Signature;
import io.prestosql.metadata.TableHandle;
import io.prestosql.operator.StageExecutionDescriptor;
import io.prestosql.spi.connector.ColumnHandle;
Expand All @@ -45,7 +42,6 @@
import io.prestosql.spi.statistics.ColumnStatisticMetadata;
import io.prestosql.spi.statistics.TableStatisticType;
import io.prestosql.spi.type.Type;
import io.prestosql.sql.InterpretedFunctionInvoker;
import io.prestosql.sql.planner.OrderingScheme;
import io.prestosql.sql.planner.Partitioning;
import io.prestosql.sql.planner.PartitioningScheme;
Expand Down Expand Up @@ -123,9 +119,9 @@
import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static io.prestosql.execution.StageInfo.getAllStages;
import static io.prestosql.operator.StageExecutionDescriptor.ungroupedExecution;
import static io.prestosql.spi.type.VarcharType.VARCHAR;
import static io.prestosql.sql.planner.SystemPartitioningHandle.SINGLE_DISTRIBUTION;
import static io.prestosql.sql.planner.planprinter.PlanNodeStatsSummarizer.aggregateStageStats;
import static io.prestosql.sql.planner.planprinter.PlanPrinterUtil.castToVarchar;
import static io.prestosql.sql.planner.planprinter.TextRenderer.formatDouble;
import static io.prestosql.sql.planner.planprinter.TextRenderer.formatPositions;
import static io.prestosql.sql.planner.planprinter.TextRenderer.indentString;
Expand Down Expand Up @@ -1197,22 +1193,6 @@ public NodeRepresentation addNode(PlanNode rootNode, String name, String identif
}
}

private 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();
}
catch (OperatorNotFoundException e) {
return "<UNREPRESENTABLE VALUE>";
}
}

private static String formatFrame(WindowNode.Frame frame)
{
StringBuilder builder = new StringBuilder(frame.getType().toString());
Expand Down
@@ -0,0 +1,51 @@
/*
* 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.airlift.slice.Slice;
import io.prestosql.Session;
import io.prestosql.metadata.FunctionRegistry;
import io.prestosql.metadata.OperatorNotFoundException;
import io.prestosql.metadata.Signature;
import io.prestosql.spi.type.Type;
import io.prestosql.sql.InterpretedFunctionInvoker;

import static io.prestosql.spi.type.VarcharType.VARCHAR;

public final class PlanPrinterUtil
{
private PlanPrinterUtil() {}

static String castToVarchar(Type type, Object value, FunctionRegistry functionRegistry, Session session)
{
try {
return castToVarcharOrFail(type, value, functionRegistry, session);
}
catch (OperatorNotFoundException e) {
return "<UNREPRESENTABLE VALUE>";
}
}

static String castToVarcharOrFail(Type type, Object value, FunctionRegistry functionRegistry, Session session)
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();
}
}

0 comments on commit 20b0a5f

Please sign in to comment.