From 3a02c78d7b605318f43e60fc6a12de1afba1a5a4 Mon Sep 17 00:00:00 2001 From: Pratik Joseph Dabre Date: Wed, 21 May 2025 13:43:04 -0700 Subject: [PATCH] Use Java built-in functions to create failure function --- .../facebook/presto/sql/planner/RowExpressionInterpreter.java | 4 +++- .../com/facebook/presto/sidecar/TestNativeSidecarPlugin.java | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/presto-main-base/src/main/java/com/facebook/presto/sql/planner/RowExpressionInterpreter.java b/presto-main-base/src/main/java/com/facebook/presto/sql/planner/RowExpressionInterpreter.java index fe2c124c5b54e..a4498a5434f27 100644 --- a/presto-main-base/src/main/java/com/facebook/presto/sql/planner/RowExpressionInterpreter.java +++ b/presto-main-base/src/main/java/com/facebook/presto/sql/planner/RowExpressionInterpreter.java @@ -15,6 +15,7 @@ import com.facebook.airlift.json.JsonCodec; import com.facebook.presto.client.FailureInfo; +import com.facebook.presto.common.QualifiedObjectName; import com.facebook.presto.common.block.BlockBuilder; import com.facebook.presto.common.block.RowBlockBuilder; import com.facebook.presto.common.function.OperatorType; @@ -71,6 +72,7 @@ import static com.facebook.presto.common.type.VarcharType.VARCHAR; import static com.facebook.presto.common.type.VarcharType.createVarcharType; import static com.facebook.presto.expressions.DynamicFilters.isDynamicFilter; +import static com.facebook.presto.metadata.BuiltInTypeAndFunctionNamespaceManager.JAVA_BUILTIN_NAMESPACE; import static com.facebook.presto.metadata.CastType.CAST; import static com.facebook.presto.metadata.CastType.JSON_TO_ARRAY_CAST; import static com.facebook.presto.metadata.CastType.JSON_TO_MAP_CAST; @@ -721,7 +723,7 @@ private RowExpression createFailureFunction(RuntimeException exception, Type typ requireNonNull(exception, "Exception is null"); String failureInfo = JsonCodec.jsonCodec(FailureInfo.class).toJson(Failures.toFailure(exception).toFailureInfo()); - FunctionHandle jsonParse = functionAndTypeManager.lookupFunction("json_parse", fromTypes(VARCHAR)); + FunctionHandle jsonParse = functionAndTypeManager.lookupFunction(QualifiedObjectName.valueOf(JAVA_BUILTIN_NAMESPACE, "json_parse"), fromTypes(VARCHAR)); Object json = functionInvoker.invoke(jsonParse, session.getSqlFunctionProperties(), utf8Slice(failureInfo)); FunctionHandle cast = functionAndTypeManager.lookupCast(CAST, UNKNOWN, type); if (exception instanceof PrestoException) { diff --git a/presto-native-sidecar-plugin/src/test/java/com/facebook/presto/sidecar/TestNativeSidecarPlugin.java b/presto-native-sidecar-plugin/src/test/java/com/facebook/presto/sidecar/TestNativeSidecarPlugin.java index 4e46960023a0e..1a22c05bdff4a 100644 --- a/presto-native-sidecar-plugin/src/test/java/com/facebook/presto/sidecar/TestNativeSidecarPlugin.java +++ b/presto-native-sidecar-plugin/src/test/java/com/facebook/presto/sidecar/TestNativeSidecarPlugin.java @@ -145,6 +145,7 @@ public void testGeneralQueries() "date_trunc('hour', from_unixtime(orderkey, '-09:30')), date_trunc('minute', from_unixtime(orderkey, '+05:30')), " + "date_trunc('second', from_unixtime(orderkey, '+00:00')) FROM orders"); assertQuery("SELECT mod(orderkey, linenumber) FROM lineitem"); + assertQueryFails("SELECT IF(true, 0/0, 1)", "[\\s\\S]*/ by zero native.default.fail[\\s\\S]*"); } @Test @@ -187,6 +188,8 @@ public void testWindowFunctions() assertQuery("SELECT min(orderkey) OVER (PARTITION BY orderdate ORDER BY orderdate, totalprice) FROM orders"); assertQuery("SELECT sum(rn) FROM (SELECT row_number() over() rn, * from orders) WHERE rn = 10"); assertQuery("SELECT * FROM (SELECT row_number() over(partition by orderstatus order by orderkey) rn, * from orders) WHERE rn = 1"); + assertQuery("SELECT first_value(orderdate) OVER (PARTITION BY orderkey ORDER BY totalprice RANGE BETWEEN 5 PRECEDING AND CURRENT ROW) FROM orders"); + assertQuery("SELECT lead(orderkey, 5) OVER (PARTITION BY custkey, orderdate ORDER BY totalprice desc ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) FROM orders"); } @Test