Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert relabeltype into collateexpr in deparser #4580

Merged

Conversation

halilozanakgul
Copy link
Contributor

@halilozanakgul halilozanakgul commented Jan 26, 2021

Moves the RelabelTypeMutator calls to the get_rule_expr function

Fixes #4010

@halilozanakgul halilozanakgul force-pushed the convert-relabeltype-into-collateexpr-in-deparser branch from c3dd954 to 5357176 Compare January 26, 2021 10:54
@codecov
Copy link

codecov bot commented Jan 26, 2021

Codecov Report

Merging #4580 (5dd2a3d) into master (f96e93a) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #4580   +/-   ##
=======================================
  Coverage   92.69%   92.69%           
=======================================
  Files         212      212           
  Lines       42701    42707    +6     
=======================================
+ Hits        39580    39588    +8     
+ Misses       3121     3119    -2     

@halilozanakgul halilozanakgul marked this pull request as ready for review January 27, 2021 07:15
@halilozanakgul halilozanakgul force-pushed the convert-relabeltype-into-collateexpr-in-deparser branch from 5357176 to 11fdc26 Compare January 27, 2021 07:20
@agedemenli agedemenli force-pushed the convert-relabeltype-into-collateexpr-in-deparser branch 6 times, most recently from c3213b4 to 2944406 Compare January 27, 2021 15:47
@@ -4812,6 +4812,8 @@ get_rule_expr(Node *node, deparse_context *context,
CHECK_FOR_INTERRUPTS();
check_stack_depth();

node = RelabelTypeMutator(node);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_rule_expr gets called a lot, so this walker will likely cause excessive CPU.

Is there a way to integrate the logic from RelabelTypeMutator into the switch statement below?

case T_CollateExpr:
{
node = RelabelTypeMutator(node);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need a mutator if we already know this is a RelabelType?

@halilozanakgul halilozanakgul force-pushed the convert-relabeltype-into-collateexpr-in-deparser branch from f7a2508 to 4b885f9 Compare February 4, 2021 10:00
@@ -4881,6 +4881,12 @@ get_rule_expr(Node *node, deparse_context *context,
CHECK_FOR_INTERRUPTS();
check_stack_depth();

if (nodeTag(node) == T_RelabelType &&
((RelabelType *)node)->resultcollid != InvalidOid)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not do this in the switch statement below?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It didn't work. I guess the reason is: Switch statement checks the initial node only; however, the node we are looking for might be somewhere we can reach by recursive search. @halilozanakgul what do you think?

Copy link
Member

@marcocitus marcocitus Feb 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmz, I don't follow.

How can

if (nodeTag(node) == T_RelabelType && ((RelabelType *) node)->resultcollid != InvalidOid)
{
    node = RelabelTypeMutator(node);
}

switch (nodeTag(node))
{
    case T_RelabelType:
    {
        ...
    }
}

be different from

switch (nodeTag(node))
{
    case T_RelabelType:
    {
        RelabelType *relabel = (RelabelType *) node;
        Node	   *arg = (Node *) relabel->arg;

        if (relabel->resultcollid != InvalidOid)
        {
            node = RelabelTypeMutator(node);
        }
        ...
    }
}

?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, you're right. I've misunderstood

Copy link
Member

@marcocitus marcocitus Feb 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, because in the first case we go into the CollateExpr switch instead.

Copy link
Member

@marcocitus marcocitus Feb 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if we do:

case T_RelabelType:
{
    RelabelType *relabel = (RelabelType *) node;
    Node	   *arg = (Node *) relabel->arg;

    /*
     * Citus modification:
     * The planner rewrites CollateExpr to RelabelType, hence we print
     * RelabelType with a collation ID as a COLLATE expression.
     */
    if (relabel->resultcollid != InvalidOid)
    {
        if (!PRETTY_PAREN(context))
            appendStringInfoChar(buf, '(');
        get_rule_expr_paren(arg, context, showimplicit, node);
        appendStringInfo(buf, " COLLATE %s",
                         generate_collation_name(collate->collOid));
        if (!PRETTY_PAREN(context))
            appendStringInfoChar(buf, ')');
        break;
    }
    ...
}

return (Node *) query_tree_mutator((Query *) originalNode, RelabelTypeMutator,
(void *) NULL, 0);
}
return expression_tree_mutator(originalNode, RelabelTypeMutator,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since the nodes will be visited recursively by the deparsing logic, do we still need a mutator?

@halilozanakgul halilozanakgul force-pushed the convert-relabeltype-into-collateexpr-in-deparser branch from 3b05616 to 84a544b Compare February 4, 2021 14:19

if (relabel->relabelformat == COERCE_IMPLICIT_CAST &&
!showimplicit)
if (relabel->resultcollid != InvalidOid)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a comment that this is a Citus-specific modification? That will make adding support for future PG releases slightly easier.

relabel->resulttype,
relabel->resulttypmod,
node);
else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strange formatting

CollateExpr *
RelabelTypeMutator(RelabelType *relabelType)
{
if (relabelType == NULL)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need this?

{
/* don't show the implicit cast */
get_rule_expr_paren(arg, context, false, node);
CollateExpr *collate = RelabelTypeMutator(relabel);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe RelabelTypeMutator -> RelabelTypeToCollateExpr?

@halilozanakgul halilozanakgul force-pushed the convert-relabeltype-into-collateexpr-in-deparser branch from 84a544b to 0d31931 Compare February 4, 2021 14:51
@halilozanakgul halilozanakgul force-pushed the convert-relabeltype-into-collateexpr-in-deparser branch from 0d31931 to 5dd2a3d Compare February 5, 2021 09:09
@halilozanakgul halilozanakgul merged commit cbb95af into master Feb 5, 2021
@halilozanakgul halilozanakgul deleted the convert-relabeltype-into-collateexpr-in-deparser branch February 5, 2021 10:33
naisila added a commit that referenced this pull request May 4, 2023
… deparsing (#6391)

Adjusts query's CoerceViaIO & RelabelType nodes that are
improper for deparsing

The standard planner converts some `::text` casts to `::cstring` and
here we convert back because `cstring` is a pseudotype and it cannot be
casted to most types. This problem occurs in CoerceViaIO nodes.
There was another problem with RelabelType nodes fixed in the following
PR:
#4580
We undo the changes in that PR, and fix both CoerceViaIO and RelabelType
nodes in the planning phase (not in the deparsing phase in ruleutils)

Fixes #5646
Fixes #5033
Fixes #6061
emelsimsek pushed a commit that referenced this pull request May 24, 2023
… deparsing (#6391)

Adjusts query's CoerceViaIO & RelabelType nodes that are
improper for deparsing

The standard planner converts some `::text` casts to `::cstring` and
here we convert back because `cstring` is a pseudotype and it cannot be
casted to most types. This problem occurs in CoerceViaIO nodes.
There was another problem with RelabelType nodes fixed in the following
PR:
#4580
We undo the changes in that PR, and fix both CoerceViaIO and RelabelType
nodes in the planning phase (not in the deparsing phase in ruleutils)

Fixes #5646
Fixes #5033
Fixes #6061
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Collate in aggregate function not pushed down
3 participants