Skip to content

Commit 9ee9d65

Browse files
committed
[sidecar] Fix ANALYZE queries on varchar columns when sidecar is enabled
1 parent 4766404 commit 9ee9d65

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

presto-main-base/src/main/java/com/facebook/presto/sql/planner/StatisticsAggregationPlanner.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@
4343
import java.util.Optional;
4444
import java.util.stream.Collectors;
4545

46+
import static com.facebook.presto.SystemSessionProperties.isNativeExecutionEnabled;
4647
import static com.facebook.presto.SystemSessionProperties.shouldOptimizerUseHistograms;
4748
import static com.facebook.presto.common.type.BigintType.BIGINT;
49+
import static com.facebook.presto.common.type.StandardTypes.VARCHAR;
4850
import static com.facebook.presto.common.type.UnknownType.UNKNOWN;
4951
import static com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED;
5052
import static com.facebook.presto.spi.function.FunctionKind.AGGREGATE;
@@ -180,7 +182,17 @@ private ColumnStatisticsAggregation createColumnAggregationFromFunctionName(Colu
180182
FunctionMetadata functionMeta = functionAndTypeResolver.getFunctionMetadata(functionHandle);
181183
Type inputType = functionAndTypeResolver.getType(getOnlyElement(functionMeta.getArgumentTypes()));
182184
Type outputType = functionAndTypeResolver.getType(functionMeta.getReturnType());
183-
verify(inputType.equals(input.getType()) || input.getType().equals(UNKNOWN), "resolved function input type does not match the input type: %s != %s", inputType, input.getType());
185+
186+
// todo: fix this hack
187+
// In native clusters, we do not support parameterized varchar,
188+
// hence if its a varchar type, we just compare the type signature base.
189+
boolean isVarcharType = input.getType().getTypeSignature().getBase().equals(VARCHAR);
190+
boolean isTypeSignatureBaseMatching = inputType.getTypeSignature().getBase().equals(input.getType().getTypeSignature().getBase());
191+
verify(
192+
inputType.equals(input.getType()) ||
193+
input.getType().equals(UNKNOWN) ||
194+
isNativeExecutionEnabled(session) && isVarcharType && isTypeSignatureBaseMatching,
195+
"resolved function input type does not match the input type: %s != %s", inputType, input.getType());
184196
return new ColumnStatisticsAggregation(
185197
new AggregationNode.Aggregation(
186198
new CallExpression(

presto-native-sidecar-plugin/src/test/java/com/facebook/presto/sidecar/TestNativeSidecarPlugin.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import static com.facebook.presto.nativeworker.NativeQueryRunnerUtils.createNation;
3434
import static com.facebook.presto.nativeworker.NativeQueryRunnerUtils.createOrders;
3535
import static com.facebook.presto.nativeworker.NativeQueryRunnerUtils.createOrdersEx;
36+
import static com.facebook.presto.nativeworker.NativeQueryRunnerUtils.createRegion;
3637
import static com.facebook.presto.sidecar.NativeSidecarPluginQueryRunnerUtils.setupNativeSidecarPlugin;
3738
import static java.lang.String.format;
3839
import static org.testng.Assert.assertEquals;
@@ -54,6 +55,7 @@ protected void createTables()
5455
createNation(queryRunner);
5556
createOrders(queryRunner);
5657
createOrdersEx(queryRunner);
58+
createRegion(queryRunner);
5759
}
5860

5961
@Override
@@ -234,6 +236,49 @@ public void testShowStats()
234236
}
235237
}
236238

239+
@Test
240+
public void testAnalyzeStats()
241+
{
242+
assertUpdate("ANALYZE region", 5);
243+
244+
// Show stats returns the following stats for each column in region table:
245+
// column_name | data_size | distinct_values_count | nulls_fraction | row_count | low_value | high_value
246+
assertQuery("SHOW STATS FOR region",
247+
"SELECT * FROM (VALUES" +
248+
"('regionkey', NULL, 5e0, 0e0, NULL, '0', '4', NULL)," +
249+
"('name', 5.4e1, 5e0, 0e0, NULL, NULL, NULL, NULL)," +
250+
"('comment', 3.5e2, 5e0, 0e0, NULL, NULL, NULL, NULL)," +
251+
"(NULL, NULL, NULL, NULL, 5e0, NULL, NULL, NULL))");
252+
253+
// Create a partitioned table and run analyze on it.
254+
String tmpTableName = generateRandomTableName();
255+
try {
256+
getQueryRunner().execute(String.format("CREATE TABLE %s (name VARCHAR, regionkey BIGINT," +
257+
"nationkey BIGINT) WITH (partitioned_by = ARRAY['regionkey','nationkey'])", tmpTableName));
258+
getQueryRunner().execute(
259+
String.format("INSERT INTO %s SELECT name, regionkey, nationkey FROM nation", tmpTableName));
260+
assertQuery(String.format("SELECT * FROM %s", tmpTableName),
261+
"SELECT name, regionkey, nationkey FROM nation");
262+
assertUpdate(String.format("ANALYZE %s", tmpTableName), 25);
263+
assertQuery(String.format("SHOW STATS for %s", tmpTableName),
264+
"SELECT * FROM (VALUES" +
265+
"('name', 2.77e2, 1e0, 0e0, NULL, NULL, NULL, NULL)," +
266+
"('regionkey', NULL, 5e0, 0e0, NULL, '0', '4', NULL)," +
267+
"('nationkey', NULL, 2.5e1, 0e0, NULL, '0', '24', NULL)," +
268+
"(NULL, NULL, NULL, NULL, 2.5e1, NULL, NULL, NULL))");
269+
assertUpdate(String.format("ANALYZE %s WITH (partitions = ARRAY[ARRAY['0','0'],ARRAY['4', '11']])", tmpTableName), 2);
270+
assertQuery(String.format("SHOW STATS for (SELECT * FROM %s where regionkey=4 and nationkey=11)", tmpTableName),
271+
"SELECT * FROM (VALUES" +
272+
"('name', 8e0, 1e0, 0e0, NULL, NULL, NULL, NULL)," +
273+
"('regionkey', NULL, 1e0, 0e0, NULL, '4', '4', NULL)," +
274+
"('nationkey', NULL, 1e0, 0e0, NULL, '11', '11', NULL)," +
275+
"(NULL, NULL, NULL, NULL, 1e0, NULL, NULL, NULL))");
276+
}
277+
finally {
278+
dropTableIfExists(tmpTableName);
279+
}
280+
}
281+
237282
private String generateRandomTableName()
238283
{
239284
String tableName = "tmp_presto_" + UUID.randomUUID().toString().replace("-", "");

0 commit comments

Comments
 (0)