Skip to content

Commit

Permalink
feat: add scalar functions in postgres (#165)
Browse files Browse the repository at this point in the history
- Implemented only conditional and stringEquals function based on projection function support required for QS(as per attribute service)
  • Loading branch information
saxenakshitiz authored Aug 11, 2022
1 parent 4bf632a commit 7f4c943
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static org.hypertrace.core.query.service.QueryFunctionConstants.DATE_TIME_CONVERT;
import static org.hypertrace.core.query.service.QueryFunctionConstants.QUERY_FUNCTION_AVGRATE;
import static org.hypertrace.core.query.service.QueryFunctionConstants.QUERY_FUNCTION_CONCAT;
import static org.hypertrace.core.query.service.QueryFunctionConstants.QUERY_FUNCTION_CONDITIONAL;
import static org.hypertrace.core.query.service.QueryFunctionConstants.QUERY_FUNCTION_COUNT;
import static org.hypertrace.core.query.service.QueryFunctionConstants.QUERY_FUNCTION_DISTINCTCOUNT;
import static org.hypertrace.core.query.service.QueryFunctionConstants.QUERY_FUNCTION_PERCENTILE;
Expand Down Expand Up @@ -60,8 +59,6 @@ public String convert(
return this.functionToStringForAvgRate(function, argumentConverter, executionContext);
case DATE_TIME_CONVERT:
return this.functionToDateTimeConvert(function, argumentConverter);
case QUERY_FUNCTION_CONDITIONAL:
throw new UnsupportedOperationException("Unsupported function " + function);
default:
// TODO remove once postgres-specific logic removed from gateway - this normalization
// reverts that logic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -975,8 +975,17 @@ void testQueryWithNulls() {
TableDefinition tableDefinition = getDefaultTableDefinition();
defaultMockingForExecutionContext();

assertExceptionOnSQLQuery(
queryRequest, UnsupportedOperationException.class, "Unsupported function");
assertSQLQuery(
queryRequest,
"select conditional('true',encode(span_id, 'hex'),'null'), conditional('true',duration_millis,0)"
+ " from public.\"span-event-view\""
+ " where "
+ tableDefinition.getTenantIdColumn()
+ " = '"
+ TENANT_ID
+ "' limit 15",
tableDefinition,
executionContext);
}

@Test
Expand Down
32 changes: 31 additions & 1 deletion query-service/src/integrationTest/resources/sql/functions.sql
Original file line number Diff line number Diff line change
@@ -1 +1,31 @@
CREATE OR REPLACE FUNCTION dateTimeConvert (bigint, bigint) RETURNS bigint AS $$ select ((($1 + $2 - 1)/$2)*$2) $$ LANGUAGE SQL;
CREATE OR REPLACE FUNCTION dateTimeConvert (bigint, bigint) RETURNS bigint AS $$ select ((($1 + $2 - 1)/$2)*$2) $$ LANGUAGE SQL;

CREATE OR REPLACE FUNCTION conditional (condition text, first text, second text)
RETURNS text
AS $$
SELECT
CASE WHEN condition IS NOT NULL
THEN CASE WHEN CAST (condition AS BOOLEAN) THEN first ELSE second END
ELSE NULL
END
$$
LANGUAGE SQL;

CREATE OR REPLACE FUNCTION stringEquals (str1 text, str2 text)
RETURNS text
AS $$
SELECT
CASE WHEN str1 IS NULL AND str2 IS NULL
THEN 'true'
ELSE
CASE WHEN str1 IS NULL OR str2 IS NULL
THEN 'false'
ELSE
CASE WHEN str1 = str2
THEN 'true'
ELSE 'false'
END
END
END
$$
LANGUAGE SQL;

0 comments on commit 7f4c943

Please sign in to comment.