Constant Folding during Query Rewrite phase

Database
Teradata Employee

Constant Folding during Query Rewrite phase

 

Does Teradata have a feature of constant folding?

 

e.g) The query 

 

select c1/(60*60*24*365) ...

is internally like this?

 

 

select c1/(31536000)...

 

 

I've read following manual, but couldn't find it.

https://www.info.teradata.com/HTMLPubs/DB_TTU_16_00/index.html#page/SQL_Reference%2FB035-1142-160K%2...


Accepted Solutions
Teradata Employee

Re: Constant Folding during Query Rewrite phase

It is correct that the select list details are not in the explain. But the result of the constant folding is visible when it is in an expression in the where clause. The same operations will be done on expressions in the select list even though the result is not visible.
1 ACCEPTED SOLUTION
7 REPLIES
Teradata Employee

Re: Constant Folding during Query Rewrite phase

I cannot say for certain, but if the optimizer did not do that it would be considered a bug. It works hard to eliminate logical equivalencies and simplify calculations.

Teradata Employee

Re: Constant Folding during Query Rewrite phase

Thanks for your reply. I'm relieved to hear that.

If I find the situation which does not optmize it, I will open the issue at T@YS.

Currently, I was just wondering.

 

Highlighted
Teradata Employee

Re: Constant Folding during Query Rewrite phase

The result of constant folding and general expression algebraic manipulations should be visible in the explain.
Teradata Employee

Re: Constant Folding during Query Rewrite phase

> The result of constant folding and general expression algebraic manipulations should be visible in the explain.

 

I've never seen that a explain refers to algebraic manipulations except for conditions of JOIN/WHERE. 

The explain of select 1;(simple calculation) and select 1*2*3*....;(complex calculation)  return same following result. Even if I use any table, the only difference is final spool size or completely same. Probably, It means that we can't judge if the optimization is executed. You know a explain doesn't show all detail of a query.

 

 

The row is sent directly back to the user as the result of statement 1

 

 

I would like to see the sample query or explain if you have it.

Teradata Employee

Re: Constant Folding during Query Rewrite phase

It is correct that the select list details are not in the explain. But the result of the constant folding is visible when it is in an expression in the where clause. The same operations will be done on expressions in the select list even though the result is not visible.
Teradata Employee

Re: Constant Folding during Query Rewrite phase

Sorry, my first post was not enough. I wanted to know if the optimizations are done on expressions in the select list. 

 

> The same operations will be done on expressions in the select list even though the result is not visible.

 

Thanks for the information. I hope that it become visible on the explain in future Cat Wink

Teradata Employee

Re: Constant Folding during Query Rewrite phase

Exactly the same expression processing is used for select list expresssions as for where clause expressions. If it does the constant evaluation in a where clause expression it will do it the same in the select list.

Since the select list is not critical to planning, showing the select list in the explain has not been a priority.