Skip to content

Commit

Permalink
Explicit unnecessary cast should be eliminated (#1984)
Browse files Browse the repository at this point in the history
* Explicit unnecessary cast should be eliminated

This commit can eliminate unnecessary explicit cast in where condition
and tune the query to choose index in those conditions

Task: BABEL-4176
Signed-off-by: Zhibai Song <[email protected]>
  • Loading branch information
forestkeeper authored Nov 6, 2023
1 parent 1ca1f8c commit 2104cc7
Show file tree
Hide file tree
Showing 26 changed files with 226 additions and 12 deletions.
66 changes: 66 additions & 0 deletions contrib/babelfishpg_tsql/src/hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "catalog/pg_trigger.h"
#include "catalog/pg_trigger_d.h"
#include "catalog/pg_type.h"
#include "catalog/pg_operator.h"
#include "commands/copy.h"
#include "commands/explain.h"
#include "commands/tablecmds.h"
Expand All @@ -38,6 +39,7 @@
#include "parser/parse_utilcmd.h"
#include "parser/parse_target.h"
#include "parser/parse_type.h"
#include "parser/parse_oper.h"
#include "parser/parser.h"
#include "parser/scanner.h"
#include "parser/scansup.h"
Expand Down Expand Up @@ -115,6 +117,7 @@ static void modify_RangeTblFunction_tupdesc(char *funcname, Node *expr, TupleDes
static void sort_nulls_first(SortGroupClause * sortcl, bool reverse);
static int getDefaultPosition(const List *default_positions, const ListCell *def_idx, int argPosition);
static List* replace_pltsql_function_defaults(HeapTuple func_tuple, List *defaults, List *fargs);
static Node* optimize_explicit_cast(ParseState *pstate, Node *node);

static ResTarget* make_restarget_from_colname(char * colName);
static void transform_pivot_clause(ParseState *pstate, SelectStmt *stmt);
Expand Down Expand Up @@ -220,6 +223,7 @@ static bbfCustomProcessUtility_hook_type prev_bbfCustomProcessUtility_hook = NUL
static bbfSelectIntoUtility_hook_type prev_bbfSelectIntoUtility_hook = NULL;
static bbfSelectIntoAddIdentity_hook_type prev_bbfSelectIntoAddIdentity_hook = NULL;
static sortby_nulls_hook_type prev_sortby_nulls_hook = NULL;
static optimize_explicit_cast_hook_type prev_optimize_explicit_cast_hook = NULL;
static table_variable_satisfies_visibility_hook_type prev_table_variable_satisfies_visibility = NULL;
static table_variable_satisfies_update_hook_type prev_table_variable_satisfies_update = NULL;
static table_variable_satisfies_vacuum_hook_type prev_table_variable_satisfies_vacuum = NULL;
Expand Down Expand Up @@ -394,6 +398,9 @@ InstallExtendedHooks(void)

pre_transform_pivot_clause_hook = transform_pivot_clause_hook;
transform_pivot_clause_hook = transform_pivot_clause;

prev_optimize_explicit_cast_hook = optimize_explicit_cast_hook;
optimize_explicit_cast_hook = optimize_explicit_cast;
}

void
Expand Down Expand Up @@ -457,6 +464,7 @@ UninstallExtendedHooks(void)
set_local_schema_for_func_hook = prev_set_local_schema_for_func_hook;
bbf_get_sysadmin_oid_hook = prev_bbf_get_sysadmin_oid_hook;
transform_pivot_clause_hook = pre_transform_pivot_clause_hook;
optimize_explicit_cast_hook = prev_optimize_explicit_cast_hook;
}

/*****************************************
Expand Down Expand Up @@ -4317,6 +4325,9 @@ transform_pivot_clause(ParseState *pstate, SelectStmt *stmt)
PLtsql_execstate *tsql_outmost_estat;
int nestlevel;

if (sql_dialect != SQL_DIALECT_TSQL)
return;

new_src_sql_targetist = NULL;
new_pivot_aliaslist = NULL;
src_sql_groupbylist = NULL;
Expand Down Expand Up @@ -4453,3 +4464,58 @@ transform_pivot_clause(ParseState *pstate, SelectStmt *stmt)
tsql_outmost_estat->pivot_number++;
MemoryContextSwitchTo(oldContext);
}

static Node* optimize_explicit_cast(ParseState *pstate, Node *node)
{
if (sql_dialect != SQL_DIALECT_TSQL)
return node;
if (node == NULL)
return NULL;
if (IsA(node, FuncExpr))
{
FuncExpr *f = (FuncExpr *) node;
if (f->funcformat == COERCE_EXPLICIT_CAST){
Node* arg = (Node*)linitial(f->args);
if (nodeTag(arg) == T_Var &&
((Var*) arg)->vartype == INT4OID &&
f->funcresulttype == INT8OID)
return optimize_explicit_cast(pstate, linitial(f->args));
}
}
else if (IsA(node, BoolExpr))
{
BoolExpr *r = (BoolExpr *) node;
ListCell *l;
foreach (l , r->args)
{
optimize_explicit_cast(pstate, (Node*)lfirst(l));
}
}
else if (IsA(node, OpExpr))
{
OpExpr *opExpr = (OpExpr*) node;
Form_pg_operator form;
HeapTuple tuple;
Node* node = optimize_explicit_cast(pstate, linitial(opExpr->args));
Node* result = NULL;
if (node != linitial(opExpr->args))
{
char *opname;

tuple = SearchSysCache1(OPEROID, ObjectIdGetDatum(opExpr->opno));
if (!HeapTupleIsValid(tuple))
return node; // no need to error out in here, stop transform and quite, keep the original node
form = (Form_pg_operator) GETSTRUCT(tuple);

opname = NameStr(form->oprname);
if (strcmp(opname, "=") == 0)
{
result =(Node*)make_op(pstate, list_make1(makeString(opname)), node, lsecond(opExpr->args), pstate->p_last_srf, -1);
}
ReleaseSysCache(tuple);
if (result)
return result;
}
}
return node;
}
5 changes: 5 additions & 0 deletions test/JDBC/expected/cast_eliminate-vu-cleanup.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
drop table cast_eliminate
GO

drop table cast_eliminate2
GO
6 changes: 6 additions & 0 deletions test/JDBC/expected/cast_eliminate-vu-prepare.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE cast_eliminate( [ROID] [int] NOT NULL PRIMARY KEY );
GO

CREATE TABLE cast_eliminate2( [ROID] [bigint] NOT NULL PRIMARY KEY );
GO

82 changes: 82 additions & 0 deletions test/JDBC/expected/cast_eliminate-vu-verify.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
set babelfish_showplan_all on
GO

SELECT 1 AS [C1] FROM cast_eliminate WHERE (CAST(ROID AS BIGINT) = 1)
GO
~~START~~
text
Query Text: SELECT 1 AS [C1] FROM cast_eliminate WHERE (CAST(ROID AS BIGINT) = 1)
Index Only Scan using cast_eliminate_pkey on cast_eliminate (cost=0.15..8.17 rows=1 width=4)
Index Cond: (roid = 1)
~~END~~


SELECT 1 AS [C1] FROM cast_eliminate WHERE (CAST(ROID AS int) = 1)
GO
~~START~~
text
Query Text: SELECT 1 AS [C1] FROM cast_eliminate WHERE (CAST(ROID AS int) = 1)
Index Only Scan using cast_eliminate_pkey on cast_eliminate (cost=0.15..8.17 rows=1 width=4)
Index Cond: (roid = 1)
~~END~~


SELECT 1 AS [C1] FROM cast_eliminate WHERE (ROID = cast(1 as bigint))
GO
~~START~~
text
Query Text: SELECT 1 AS [C1] FROM cast_eliminate WHERE (ROID = cast(1 as bigint))
Index Only Scan using cast_eliminate_pkey on cast_eliminate (cost=0.15..8.17 rows=1 width=4)
Index Cond: (roid = '1'::bigint)
~~END~~


SELECT 1 AS [C1] FROM cast_eliminate WHERE (CAST(ROID AS BIGINT) = cast( 1 as bigint ))
GO
~~START~~
text
Query Text: SELECT 1 AS [C1] FROM cast_eliminate WHERE (CAST(ROID AS BIGINT) = cast( 1 as bigint ))
Index Only Scan using cast_eliminate_pkey on cast_eliminate (cost=0.15..8.17 rows=1 width=4)
Index Cond: (roid = '1'::bigint)
~~END~~


SELECT 1 AS [C1] FROM cast_eliminate2 WHERE (CAST(ROID AS BIGINT) = 1)
GO
~~START~~
text
Query Text: SELECT 1 AS [C1] FROM cast_eliminate2 WHERE (CAST(ROID AS BIGINT) = 1)
Index Only Scan using cast_eliminate2_pkey on cast_eliminate2 (cost=0.15..8.17 rows=1 width=4)
Index Cond: (roid = 1)
~~END~~


SELECT 1 AS [C1] FROM cast_eliminate2 WHERE (CAST(ROID AS int) = 1)
GO
~~START~~
text
Query Text: SELECT 1 AS [C1] FROM cast_eliminate2 WHERE (CAST(ROID AS int) = 1)
Seq Scan on cast_eliminate2 (cost=0.00..43.90 rows=11 width=4)
Filter: ((roid)::integer = 1)
~~END~~


SELECT 1 AS [C1] FROM cast_eliminate2 WHERE (ROID = cast(1 as bigint))
GO
~~START~~
text
Query Text: SELECT 1 AS [C1] FROM cast_eliminate2 WHERE (ROID = cast(1 as bigint))
Index Only Scan using cast_eliminate2_pkey on cast_eliminate2 (cost=0.15..8.17 rows=1 width=4)
Index Cond: (roid = '1'::bigint)
~~END~~


SELECT 1 AS [C1] FROM cast_eliminate WHERE (CAST(ROID AS BIGINT) = cast( 1 as bigint ))
GO
~~START~~
text
Query Text: SELECT 1 AS [C1] FROM cast_eliminate WHERE (CAST(ROID AS BIGINT) = cast( 1 as bigint ))
Index Only Scan using cast_eliminate_pkey on cast_eliminate (cost=0.15..8.17 rows=1 width=4)
Index Cond: (roid = '1'::bigint)
~~END~~

5 changes: 5 additions & 0 deletions test/JDBC/input/cast_eliminate-vu-cleanup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
drop table cast_eliminate
GO

drop table cast_eliminate2
GO
6 changes: 6 additions & 0 deletions test/JDBC/input/cast_eliminate-vu-prepare.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE cast_eliminate( [ROID] [int] NOT NULL PRIMARY KEY );
GO

CREATE TABLE cast_eliminate2( [ROID] [bigint] NOT NULL PRIMARY KEY );
GO

26 changes: 26 additions & 0 deletions test/JDBC/input/cast_eliminate-vu-verify.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
set babelfish_showplan_all on
GO

SELECT 1 AS [C1] FROM cast_eliminate WHERE (CAST(ROID AS BIGINT) = 1)
GO

SELECT 1 AS [C1] FROM cast_eliminate WHERE (CAST(ROID AS int) = 1)
GO

SELECT 1 AS [C1] FROM cast_eliminate WHERE (ROID = cast(1 as bigint))
GO

SELECT 1 AS [C1] FROM cast_eliminate WHERE (CAST(ROID AS BIGINT) = cast( 1 as bigint ))
GO

SELECT 1 AS [C1] FROM cast_eliminate2 WHERE (CAST(ROID AS BIGINT) = 1)
GO

SELECT 1 AS [C1] FROM cast_eliminate2 WHERE (CAST(ROID AS int) = 1)
GO

SELECT 1 AS [C1] FROM cast_eliminate2 WHERE (ROID = cast(1 as bigint))
GO

SELECT 1 AS [C1] FROM cast_eliminate WHERE (CAST(ROID AS BIGINT) = cast( 1 as bigint ))
GO
1 change: 1 addition & 0 deletions test/JDBC/parallel_query_jdbc_schedule
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ ignore#!#BABEL-4261
ignore#!#babel_collection
ignore#!#binary-index-vu-verify
ignore#!#pgr_select_into
ignore#!#cast_eliminate-vu-verify

# Other or mixed issues
ignore#!#545_1
Expand Down
3 changes: 2 additions & 1 deletion test/JDBC/upgrade/13_4/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,5 @@ timefromparts
triggers_with_transaction
BABEL-4046
getdate
AUTO_ANALYZE-before-15-5-or-14-10
AUTO_ANALYZE-before-15-5-or-14-10
cast_eliminate
3 changes: 2 additions & 1 deletion test/JDBC/upgrade/13_5/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,5 @@ timefromparts
triggers_with_transaction
BABEL-4046
getdate
AUTO_ANALYZE-before-15-5-or-14-10
AUTO_ANALYZE-before-15-5-or-14-10
cast_eliminate
2 changes: 1 addition & 1 deletion test/JDBC/upgrade/13_6/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -325,4 +325,4 @@ BABEL-4046
getdate
BABEL-4410
GRANT_SCHEMA
AUTO_ANALYZE-before-15-5-or-14-10
AUTO_ANALYZE-before-15-5-or-14-10
3 changes: 2 additions & 1 deletion test/JDBC/upgrade/13_7/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,5 @@ timefromparts
triggers_with_transaction
BABEL-4046
getdate
AUTO_ANALYZE-before-15-5-or-14-10
AUTO_ANALYZE-before-15-5-or-14-10
cast_eliminate
3 changes: 2 additions & 1 deletion test/JDBC/upgrade/13_8/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,5 @@ timefromparts
triggers_with_transaction
BABEL-4046
getdate
AUTO_ANALYZE-before-15-5-or-14-10
AUTO_ANALYZE-before-15-5-or-14-10
cast_eliminate
1 change: 1 addition & 0 deletions test/JDBC/upgrade/13_9/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,4 @@ getdate
BABEL-4410
GRANT_SCHEMA
AUTO_ANALYZE-before-15-5-or-14-10
cast_eliminate
1 change: 1 addition & 0 deletions test/JDBC/upgrade/14_10/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -413,3 +413,4 @@ BABEL-4384
GRANT_SCHEMA
default_params
BABEL-3326
cast_eliminate
1 change: 1 addition & 0 deletions test/JDBC/upgrade/14_3/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,4 @@ BABEL-2619
BABEL-4410
GRANT_SCHEMA
AUTO_ANALYZE-before-15-5-or-14-10
cast_eliminate
1 change: 1 addition & 0 deletions test/JDBC/upgrade/14_5/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,4 @@ BABEL-2619
BABEL-4410
GRANT_SCHEMA
AUTO_ANALYZE-before-15-5-or-14-10
cast_eliminate
3 changes: 2 additions & 1 deletion test/JDBC/upgrade/14_6/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -388,4 +388,5 @@ BABEL_4330
BABEL-2619
BABEL-4410
GRANT_SCHEMA
AUTO_ANALYZE-before-15-5-or-14-10
AUTO_ANALYZE-before-15-5-or-14-10
cast_eliminate
3 changes: 2 additions & 1 deletion test/JDBC/upgrade/14_7/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -408,4 +408,5 @@ BABEL-4046
getdate
BABEL_4330
BABEL-2619
AUTO_ANALYZE-before-15-5-or-14-10
AUTO_ANALYZE-before-15-5-or-14-10
cast_eliminate
3 changes: 2 additions & 1 deletion test/JDBC/upgrade/14_8/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -407,4 +407,5 @@ getdate
BABEL_4330
BABEL-2619
AUTO_ANALYZE-before-15-5-or-14-10
default_params
default_params
cast_eliminate
3 changes: 2 additions & 1 deletion test/JDBC/upgrade/14_9/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,5 @@ BABEL_4330
BABEL-2619
BABEL-4410
AUTO_ANALYZE-before-15-5-or-14-10
default_params
default_params
cast_eliminate
3 changes: 2 additions & 1 deletion test/JDBC/upgrade/15_1/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -386,4 +386,5 @@ BABEL-4046
getdate
BABEL_4330
AUTO_ANALYZE-before-15-5-or-14-10
default_params
default_params
cast_eliminate
1 change: 1 addition & 0 deletions test/JDBC/upgrade/15_2/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,4 @@ BABEL-4410
GRANT_SCHEMA
AUTO_ANALYZE-before-15-5-or-14-10
default_params
cast_eliminate
3 changes: 2 additions & 1 deletion test/JDBC/upgrade/15_3/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -438,4 +438,5 @@ BABEL-4046
getdate
BABEL_4330
AUTO_ANALYZE-before-15-5-or-14-10
default_params
default_params
cast_eliminate
3 changes: 2 additions & 1 deletion test/JDBC/upgrade/15_4/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -451,4 +451,5 @@ BABEL_4330
BABEL-4410
GRANT_SCHEMA
AUTO_ANALYZE-before-15-5-or-14-10
default_params
default_params
cast_eliminate
Loading

0 comments on commit 2104cc7

Please sign in to comment.