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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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> sampleDataMap = new HashMap<>();
sampleDataMap.put("foo", VarcharType.createUnboundedVarcharType());
sampleDataMap.put(Byte.toString((byte) (Byte.MAX_VALUE / 2)), TinyintType.TINYINT);
sampleDataMap.put(Short.toString((short) (Short.MAX_VALUE / 2)), SmallintType.SMALLINT);
sampleDataMap.put(Integer.toString(Integer.MAX_VALUE / 2), IntegerType.INTEGER);
sampleDataMap.put(Long.toString(Long.MAX_VALUE / 2), BigintType.BIGINT);
sampleDataMap.put(Boolean.TRUE.toString(), BooleanType.BOOLEAN);
sampleDataMap.put("bar", CharType.createCharType(3));
sampleDataMap.put("1.2345678901234578E14", DoubleType.DOUBLE);
sampleDataMap.put("123456789012345678901234.567", DecimalType.createDecimalType(30, 3));
sampleDataMap.put("2019-01-01", DateType.DATE);
sampleDataMap.put("2019-01-01 23:22:21.123", TimestampType.TIMESTAMP);
for (Map.Entry<Object, Type> entry : sampleDataMap.entrySet()) {
@Language("SQL") String query = "" +
"CREATE TABLE test_types_table WITH (partitioned_by = ARRAY['my_col']) AS " +
"SELECT " +
"'foo' my_non_partition_col" +
",CAST('" + entry.getKey() + "' as " + entry.getValue().getDisplayName() + ") my_col";

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
@@ -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();
}
}
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