Skip to content

Commit

Permalink
add tests for aggregate fns
Browse files Browse the repository at this point in the history
  • Loading branch information
priyanshi-yb committed Dec 21, 2024
1 parent d5d2fd7 commit facaced
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 17 deletions.
12 changes: 0 additions & 12 deletions yb-voyager/src/query/queryissue/detectors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,6 @@ func TestFuncCallDetector(t *testing.T) {
`SELECT pg_advisory_unlock_all();`,
}

anyValAggSqls := []string{
`SELECT
department,
any_value(employee_name) AS any_employee
FROM employees
GROUP BY department;`,
}
loFunctionSqls := []string{
`UPDATE documents
SET content_oid = lo_import('/path/to/new/file.pdf')
Expand Down Expand Up @@ -130,11 +123,6 @@ WHERE title = 'Design Document';`,
assert.Equal(t, issues[0].Type, LARGE_OBJECT_FUNCTIONS, "Large Objects not detected in SQL: %s", sql)
}

for _, sql := range anyValAggSqls {
issues := getDetectorIssues(t, NewFuncCallDetector(sql), sql)
assert.Equal(t, 1, len(issues), "Expected 1 issue for SQL: %s", sql)
assert.Equal(t, AGGREGATE_FUNCTION, issues[0].Type, "Expected Advisory Locks issue for SQL: %s", sql)
}
}

func TestColumnRefDetector(t *testing.T) {
Expand Down
10 changes: 5 additions & 5 deletions yb-voyager/src/query/queryissue/issues_dml.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func NewXmlFunctionsIssue(objectType string, objectName string, sqlStatement str
return newQueryIssue(xmlFunctionsIssue, objectType, objectName, sqlStatement, map[string]interface{}{})
}

var anyValueAggFunctionIssue = issue.Issue{
var aggregateFunctionIssue = issue.Issue{
Type: AGGREGATE_FUNCTION,
TypeName: AGGREGATION_FUNCTIONS_NAME,
TypeDescription: "Postgresql 17 features not supported yet in YugabyteDB",
Expand All @@ -73,9 +73,9 @@ var anyValueAggFunctionIssue = issue.Issue{
func NewAggregationFunctionIssue(objectType string, objectName string, sqlStatement string, funcNames []string) QueryIssue {
sort.Strings(funcNames)
details := map[string]interface{}{
FUNCTION_NAMES: funcNames,//TODO USE it later when we start putting these in reports
FUNCTION_NAMES: funcNames, //TODO USE it later when we start putting these in reports
}
return newQueryIssue(anyValueAggFunctionIssue, objectType, objectName, sqlStatement, details)
return newQueryIssue(aggregateFunctionIssue, objectType, objectName, sqlStatement, details)
}

var jsonConstructorFunctionsIssue = issue.Issue{
Expand All @@ -90,7 +90,7 @@ var jsonConstructorFunctionsIssue = issue.Issue{
func NewJsonConstructorFunctionIssue(objectType string, objectName string, sqlStatement string, funcNames []string) QueryIssue {
sort.Strings(funcNames)
details := map[string]interface{}{
FUNCTION_NAMES: funcNames,//TODO USE it later when we start putting these in reports
FUNCTION_NAMES: funcNames, //TODO USE it later when we start putting these in reports
}
return newQueryIssue(jsonConstructorFunctionsIssue, objectType, objectName, sqlStatement, details)
}
Expand Down Expand Up @@ -124,7 +124,7 @@ var loFunctionsIssue = issue.Issue{
func NewLOFuntionsIssue(objectType string, objectName string, sqlStatement string, funcNames []string) QueryIssue {
sort.Strings(funcNames)
details := map[string]interface{}{
FUNCTION_NAMES: funcNames,//TODO USE it later when we start putting these in reports
FUNCTION_NAMES: funcNames, //TODO USE it later when we start putting these in reports
}
return newQueryIssue(loFunctionsIssue, objectType, objectName, sqlStatement, details)
}
29 changes: 29 additions & 0 deletions yb-voyager/src/query/queryissue/issues_dml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,35 @@ FROM books;`)
assertErrorCorrectlyThrownForIssueForYBVersion(t, err, `does not exist`, jsonConstructorFunctionsIssue)
}

func testAggFunctions(t *testing.T) {
sqls := `CREATE TABLE employees (
department TEXT,
employee_name TEXT,
salary NUMERIC
);
INSERT INTO employees VALUES
('HR', 'Alice', 50000),
('HR', 'Bob', 55000),
('IT', 'Charlie', 60000),
('IT', 'Diana', 62000);
SELECT
department,
any_value(employee_name) AS any_employee
FROM employees
GROUP BY department;`

ctx := context.Background()
conn, err := getConn()
assert.NoError(t, err)

defer conn.Close(context.Background())
_, err = conn.Exec(ctx, sqls)

assertErrorCorrectlyThrownForIssueForYBVersion(t, err, `does not exist`, aggregateFunctionIssue)
}

func TestDMLIssuesInYBVersion(t *testing.T) {
var err error
ybVersion := os.Getenv("YB_VERSION")
Expand Down
68 changes: 68 additions & 0 deletions yb-voyager/src/query/queryissue/parser_issue_detector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,3 +594,71 @@ WHERE JSON_EXISTS(details, '$.price ? (@ > $price)' PASSING 30 AS price);`,
}
}
}

func TestAggregateFunctions(t *testing.T) {
sqls := []string{
`SELECT
department,
any_value(employee_name) AS any_employee
FROM employees
GROUP BY department;`,
`SELECT range_intersect_agg(multi_event_range) AS intersection_of_multiranges
FROM multiranges;`,
`SELECT range_agg(multi_event_range) AS union_of_multiranges
FROM multiranges;`,
`SELECT range_intersect_agg(event_range) AS intersection_of_ranges
FROM events;`,
`SELECT range_agg(event_range) AS union_of_ranges
FROM events;`,
`CREATE OR REPLACE FUNCTION aggregate_ranges()
RETURNS INT4MULTIRANGE AS $$
DECLARE
aggregated_range INT4MULTIRANGE;
BEGIN
SELECT range_agg(range_value) INTO aggregated_range FROM ranges;
SELECT
department,
any_value(employee_name) AS any_employee
FROM employees
GROUP BY department;
RETURN aggregated_range;
END;
$$ LANGUAGE plpgsql;`,
}
aggregateSqls := map[string][]QueryIssue{
// sqls[0]: []QueryIssue{
// NewAggregationFunctionIssue(DML_QUERY_OBJECT_TYPE, "", sqls[0], []string{"any_value"}),
// },
// sqls[1]: []QueryIssue{
// NewAggregationFunctionIssue(DML_QUERY_OBJECT_TYPE, "", sqls[1], []string{"range_intersect_agg"}),
// },
// sqls[2]: []QueryIssue{
// NewAggregationFunctionIssue(DML_QUERY_OBJECT_TYPE, "", sqls[2], []string{"range_agg"}),
// },
// sqls[3]: []QueryIssue{
// NewAggregationFunctionIssue(DML_QUERY_OBJECT_TYPE, "", sqls[3], []string{"range_intersect_agg"}),
// },
// sqls[4]: []QueryIssue{
// NewAggregationFunctionIssue(DML_QUERY_OBJECT_TYPE, "", sqls[4], []string{"range_agg"}),
// },
sqls[5]: []QueryIssue{
NewAggregationFunctionIssue(DML_QUERY_OBJECT_TYPE, "", "SELECT range_agg(range_value) FROM ranges;", []string{"range_agg"}),
NewAggregationFunctionIssue(DML_QUERY_OBJECT_TYPE, "", sqls[0], []string{"any_value"}),
},
}
aggregateSqls[sqls[5]] = modifyiedIssuesforPLPGSQL(aggregateSqls[sqls[5]], "FUNCTION", "aggregate_ranges")

parserIssueDetector := NewParserIssueDetector()
for stmt, expectedIssues := range aggregateSqls {
issues, err := parserIssueDetector.GetAllIssues(stmt, ybversion.LatestStable)
assert.NoError(t, err, "Error detecting issues for statement: %s", stmt)
fmt.Printf("issues %v", issues)
assert.Equal(t, len(expectedIssues), len(issues), "Mismatch in issue count for statement: %s", stmt)
for _, expectedIssue := range expectedIssues {
found := slices.ContainsFunc(issues, func(queryIssue QueryIssue) bool {
return cmp.Equal(expectedIssue, queryIssue)
})
assert.True(t, found, "Expected issue not found: %v in statement: %s", expectedIssue, stmt)
}
}
}

0 comments on commit facaced

Please sign in to comment.