Skip to content

Commit

Permalink
[CBRD-24735] When attempting 'Multiple Key Ranges Optimization (MRO)'…
Browse files Browse the repository at this point in the history
…, An internal error occurs because the '1=1' predicate (data filer) generated by constant folding is not removed (#4240)

http://jira.cubrid.org/browse/CBRD-24735

When the pt_semantic_type function runs after rewriting the query, it runs pt_where_type after the fold constant to see if there are any predicates that can be removed.

In the query below, the `1=1` predicate generated after rewriting the query should be removed.
```
pt_semantic_check: select ... from t0 inner join t1 t on t0.c1=t.c1 and t0.c1=1         where t.c1=1                                    order by 1 limit 1;
mq_translate:      select ... from t0 inner join t1 t                                   where t.c1=1 and t0.c1=t.c1 and t0.c1=1         order by 1 limit 1;
mq_optimize:       select ... from t0 inner join t1 t                                   where t.c1=1 and t0.c1=t.c1 and t0.c1=1 and 1=1 order by 1 limit 1;
    qo_reduce_equality_terms: t.c1=1 and t0.c1=t.c1 and t0.c1=1 (join_term_list: t0.c1=t.c1)
    qo_reduce_equality_terms: t.c1=1 and 1=1 and t0.c1=1        (join_term_list: t0.c1=t.c1)
    qo_reduce_equality_terms: t.c1=1 and t0.c1=t.c1 and t0.c1=1 and t0.c1=t.c1
pt_semantic_type:  select ... from t0 inner join t1 t on t0.c1=t.c1 and t0.c1=1 and 1=1 where t.c1=1                                    order by 1 limit 1;
```
  • Loading branch information
youngjinj authored Apr 14, 2023
1 parent a1c6e2e commit 5d6d524
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/parser/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ extern "C"
extern void pt_resolve_object (PARSER_CONTEXT * parser, PT_NODE * node);
extern int pt_resolved (const PT_NODE * expr);
extern bool pt_false_where (PARSER_CONTEXT * parser, PT_NODE * statement);
extern PT_NODE *pt_do_where_type (PARSER_CONTEXT * parser, PT_NODE * node, void *arg, int *continue_walk);
extern PT_NODE *pt_where_type (PARSER_CONTEXT * parser, PT_NODE * where);
extern PT_NODE *pt_where_type_keep_true (PARSER_CONTEXT * parser, PT_NODE * where);
extern bool pt_false_search_condition (PARSER_CONTEXT * parser, const PT_NODE * statement);
Expand Down
52 changes: 52 additions & 0 deletions src/parser/type_checking.c
Original file line number Diff line number Diff line change
Expand Up @@ -6797,6 +6797,44 @@ pt_product_sets (PARSER_CONTEXT * parser, TP_DOMAIN * domain, DB_VALUE * set1, D
return (!pt_has_error (parser));
}

/*
* pt_do_where_type () -
* return:
* parser(in):
* node(in):
* arg(in):
* continue_walk(in):
*/
PT_NODE *
pt_do_where_type (PARSER_CONTEXT * parser, PT_NODE * node, void *arg, int *continue_walk)
{
PT_NODE *spec = NULL;

if (node == NULL)
{
return NULL;
}

switch (node->node_type)
{
case PT_SELECT:
for (spec = node->info.query.q.select.from; spec; spec = spec->next)
{
if (spec->node_type == PT_SPEC && spec->info.spec.on_cond)
{
spec->info.spec.on_cond = pt_where_type (parser, spec->info.spec.on_cond);
}
}

node->info.query.q.select.where = pt_where_type (parser, node->info.query.q.select.where);
break;

default:
break;
}

return node;
}

/*
* pt_where_type () - Test for constant folded where clause,
Expand Down Expand Up @@ -20363,6 +20401,20 @@ pt_semantic_type (PARSER_CONTEXT * parser, PT_NODE * tree, SEMANTIC_CHK_INFO * s
tree = NULL;
}

/* When qo_reduce_equality_terms is executed in mq_optimize, a removable predicate like '1=1' is generated.
* This predicate is removed by executing pt_where_type after pt_fold_const_expr has executed.
*
* If this predicate remains without being removed, it becomes a data filter and MRO (Multiple Key Ranges
* Optimization) cannot be performed.
*
* See CBRD-24735 for the details.
*/
tree = parser_walk_tree (parser, tree, NULL, NULL, pt_do_where_type, NULL);
if (pt_has_error (parser))
{
tree = NULL;
}

return tree;
}

Expand Down

0 comments on commit 5d6d524

Please sign in to comment.