-
Notifications
You must be signed in to change notification settings - Fork 4.8k
HIVE-28280: SemanticException when querying VIEW with DISTINCT clause #6103
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
Open
soumyakanti3578
wants to merge
5
commits into
apache:master
Choose a base branch
from
soumyakanti3578:HIVE-28280
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+188
−37
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
651fc44
HIVE-28280: SemanticException when querying VIEW with DISTINCT clause
soumyakanti3578 d399943
fix failing test
soumyakanti3578 a406ce7
address review comments
soumyakanti3578 11f28be
remove unused import
soumyakanti3578 f68ad6a
get projects for any node
soumyakanti3578 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| import java.util.LinkedHashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.Set; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
|
|
@@ -102,8 +103,8 @@ public class HiveRelFieldTrimmer extends RelFieldTrimmer { | |
|
|
||
| private static final ThreadLocal<ColumnAccessInfo> COLUMN_ACCESS_INFO = | ||
| new ThreadLocal<>(); | ||
| private static final ThreadLocal<Map<HiveProject, Table>> VIEW_PROJECT_TO_TABLE_SCHEMA = | ||
| new ThreadLocal<>(); | ||
| private static final ThreadLocal<Map<RelNode, Pair<Table, List<RexNode>>>> | ||
| VIEW_RELNODE_TO_TABLE_AND_PROJECTS = new ThreadLocal<>(); | ||
|
|
||
|
|
||
| protected HiveRelFieldTrimmer(boolean fetchStats) { | ||
|
|
@@ -155,17 +156,18 @@ public RelNode trim(RelBuilder relBuilder, RelNode root) { | |
| } | ||
|
|
||
| public RelNode trim(RelBuilder relBuilder, RelNode root, | ||
| ColumnAccessInfo columnAccessInfo, Map<HiveProject, Table> viewToTableSchema) { | ||
| ColumnAccessInfo columnAccessInfo, | ||
| Map<RelNode, Pair<Table, List<RexNode>>> relNodeToTableAndProjects) { | ||
| try { | ||
| // Set local thread variables | ||
| COLUMN_ACCESS_INFO.set(columnAccessInfo); | ||
| VIEW_PROJECT_TO_TABLE_SCHEMA.set(viewToTableSchema); | ||
| VIEW_RELNODE_TO_TABLE_AND_PROJECTS.set(relNodeToTableAndProjects); | ||
| // Execute pruning | ||
| return super.trim(relBuilder, root); | ||
| } finally { | ||
| // Always remove the local thread variables to avoid leaks | ||
| COLUMN_ACCESS_INFO.remove(); | ||
| VIEW_PROJECT_TO_TABLE_SCHEMA.remove(); | ||
| VIEW_RELNODE_TO_TABLE_AND_PROJECTS.remove(); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -182,6 +184,7 @@ protected TrimResult trimChild( | |
| RelNode input, | ||
| final ImmutableBitSet fieldsUsed, | ||
| Set<RelDataTypeField> extraFields) { | ||
| setColumnAccessInfoForViews(rel, fieldsUsed); | ||
| final ImmutableBitSet.Builder fieldsUsedBuilder = fieldsUsed.rebuild(); | ||
|
|
||
| // Correlating variables are a means for other relational expressions to use | ||
|
|
@@ -203,6 +206,26 @@ protected RexNode handle(RexFieldAccess fieldAccess) { | |
| return dispatchTrimFields(input, fieldsUsedBuilder.build(), extraFields); | ||
| } | ||
|
|
||
| private static void setColumnAccessInfoForViews(RelNode rel, ImmutableBitSet fieldsUsed) { | ||
| final ColumnAccessInfo columnAccessInfo = COLUMN_ACCESS_INFO.get(); | ||
| final Map<RelNode, Pair<Table, List<RexNode>>> relNodeToTableAndProjects = VIEW_RELNODE_TO_TABLE_AND_PROJECTS.get(); | ||
|
|
||
| // HiveTableScans are handled separately in HiveTableScan's trimFields method. | ||
| if (!(rel instanceof HiveTableScan) && | ||
| columnAccessInfo != null && | ||
| relNodeToTableAndProjects != null && | ||
| relNodeToTableAndProjects.containsKey(rel)) { | ||
| Table table = Objects.requireNonNull(relNodeToTableAndProjects.get(rel).left); | ||
| List<RexNode> projects = relNodeToTableAndProjects.get(rel).right; | ||
| List<FieldSchema> tableAllCols = table.getAllCols(); | ||
| for (Ord<RexNode> ord : Ord.zip(projects)) { | ||
| if (fieldsUsed.get(ord.i)) { | ||
| columnAccessInfo.add(table.getCompleteName(), tableAllCols.get(ord.i).getName()); | ||
| } | ||
| } | ||
|
Comment on lines
+219
to
+225
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need the projects. We could just iterate over the fields from the row type of the RelNode. That would also simplify the map, i.e., just a |
||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Variant of {@link #trimFields(RelNode, ImmutableBitSet, Set)} for | ||
| * {@link org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveMultiJoin}. | ||
|
|
@@ -726,27 +749,6 @@ public TrimResult trimFields(Aggregate aggregate, ImmutableBitSet fieldsUsed, Se | |
| return result(relBuilder.build(), mapping); | ||
| } | ||
|
|
||
| /** | ||
| * Variant of {@link #trimFields(RelNode, ImmutableBitSet, Set)} for | ||
| * {@link org.apache.calcite.rel.logical.LogicalProject}. | ||
| */ | ||
| public TrimResult trimFields(Project project, ImmutableBitSet fieldsUsed, | ||
| Set<RelDataTypeField> extraFields) { | ||
| // set columnAccessInfo for ViewColumnAuthorization | ||
| final ColumnAccessInfo columnAccessInfo = COLUMN_ACCESS_INFO.get(); | ||
| final Map<HiveProject, Table> viewProjectToTableSchema = VIEW_PROJECT_TO_TABLE_SCHEMA.get(); | ||
| if (columnAccessInfo != null && viewProjectToTableSchema != null | ||
| && viewProjectToTableSchema.containsKey(project)) { | ||
| for (Ord<RexNode> ord : Ord.zip(project.getProjects())) { | ||
| if (fieldsUsed.get(ord.i)) { | ||
| Table tab = viewProjectToTableSchema.get(project); | ||
| columnAccessInfo.add(tab.getCompleteName(), tab.getAllCols().get(ord.i).getName()); | ||
| } | ||
| } | ||
| } | ||
| return super.trimFields(project, fieldsUsed, extraFields); | ||
| } | ||
|
|
||
| public TrimResult trimFields(HiveTableScan tableAccessRel, ImmutableBitSet fieldsUsed, | ||
| Set<RelDataTypeField> extraFields) { | ||
| final TrimResult result = super.trimFields(tableAccessRel, fieldsUsed, extraFields); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
ql/src/test/queries/clientpositive/view_top_relnode_not_project_authorization.q
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| set hive.security.authorization.enabled=true; | ||
| create table t1 (username string, id int); | ||
|
|
||
| create view vw_t1 as select distinct username from t1 limit 5; | ||
| explain cbo select * from vw_t1; | ||
| select * from vw_t1; | ||
|
|
||
| create view vw_t2 as | ||
| select username from (select username, id from t1 where id > 10 limit 1) x where username > 'a' order by id; | ||
| explain cbo select * from vw_t2; | ||
| select * from vw_t2; | ||
|
|
||
| create view vw_t3 as | ||
| select username from (select username, id from t1 where id > 10 limit 10) x where username > 'a' limit 5; | ||
| explain cbo select * from vw_t3; | ||
| select * from vw_t3; |
129 changes: 129 additions & 0 deletions
129
ql/src/test/results/clientpositive/llap/view_top_relnode_not_project_authorization.q.out
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| PREHOOK: query: create table t1 (username string, id int) | ||
| PREHOOK: type: CREATETABLE | ||
| PREHOOK: Output: database:default | ||
| PREHOOK: Output: default@t1 | ||
| POSTHOOK: query: create table t1 (username string, id int) | ||
| POSTHOOK: type: CREATETABLE | ||
| POSTHOOK: Output: database:default | ||
| POSTHOOK: Output: default@t1 | ||
| PREHOOK: query: create view vw_t1 as select distinct username from t1 limit 5 | ||
| PREHOOK: type: CREATEVIEW | ||
| PREHOOK: Input: default@t1 | ||
| PREHOOK: Output: database:default | ||
| PREHOOK: Output: default@vw_t1 | ||
| POSTHOOK: query: create view vw_t1 as select distinct username from t1 limit 5 | ||
| POSTHOOK: type: CREATEVIEW | ||
| POSTHOOK: Input: default@t1 | ||
| POSTHOOK: Output: database:default | ||
| POSTHOOK: Output: default@vw_t1 | ||
| POSTHOOK: Lineage: vw_t1.username SIMPLE [(t1)t1.FieldSchema(name:username, type:string, comment:null), ] | ||
| PREHOOK: query: explain cbo select * from vw_t1 | ||
| PREHOOK: type: QUERY | ||
| PREHOOK: Input: default@t1 | ||
| PREHOOK: Input: default@vw_t1 | ||
| #### A masked pattern was here #### | ||
| POSTHOOK: query: explain cbo select * from vw_t1 | ||
| POSTHOOK: type: QUERY | ||
| POSTHOOK: Input: default@t1 | ||
| POSTHOOK: Input: default@vw_t1 | ||
| #### A masked pattern was here #### | ||
| CBO PLAN: | ||
| HiveSortLimit(fetch=[5]) | ||
| HiveProject(username=[$0]) | ||
| HiveAggregate(group=[{0}]) | ||
| HiveTableScan(table=[[default, t1]], table:alias=[t1]) | ||
|
|
||
| PREHOOK: query: select * from vw_t1 | ||
| PREHOOK: type: QUERY | ||
| PREHOOK: Input: default@t1 | ||
| PREHOOK: Input: default@vw_t1 | ||
| #### A masked pattern was here #### | ||
| POSTHOOK: query: select * from vw_t1 | ||
| POSTHOOK: type: QUERY | ||
| POSTHOOK: Input: default@t1 | ||
| POSTHOOK: Input: default@vw_t1 | ||
| #### A masked pattern was here #### | ||
| PREHOOK: query: create view vw_t2 as | ||
| select username from (select username, id from t1 where id > 10 limit 1) x where username > 'a' order by id | ||
| PREHOOK: type: CREATEVIEW | ||
| PREHOOK: Input: default@t1 | ||
| PREHOOK: Output: database:default | ||
| PREHOOK: Output: default@vw_t2 | ||
| POSTHOOK: query: create view vw_t2 as | ||
| select username from (select username, id from t1 where id > 10 limit 1) x where username > 'a' order by id | ||
| POSTHOOK: type: CREATEVIEW | ||
| POSTHOOK: Input: default@t1 | ||
| POSTHOOK: Output: database:default | ||
| POSTHOOK: Output: default@vw_t2 | ||
| POSTHOOK: Lineage: vw_t2.username SIMPLE [(t1)t1.FieldSchema(name:username, type:string, comment:null), ] | ||
| PREHOOK: query: explain cbo select * from vw_t2 | ||
| PREHOOK: type: QUERY | ||
| PREHOOK: Input: default@t1 | ||
| PREHOOK: Input: default@vw_t2 | ||
| #### A masked pattern was here #### | ||
| POSTHOOK: query: explain cbo select * from vw_t2 | ||
| POSTHOOK: type: QUERY | ||
| POSTHOOK: Input: default@t1 | ||
| POSTHOOK: Input: default@vw_t2 | ||
| #### A masked pattern was here #### | ||
| CBO PLAN: | ||
| HiveFilter(condition=[>($0, _UTF-16LE'a')]) | ||
| HiveProject(username=[$0]) | ||
| HiveSortLimit(fetch=[1]) | ||
| HiveProject(username=[$0]) | ||
| HiveFilter(condition=[>($1, 10)]) | ||
| HiveTableScan(table=[[default, t1]], table:alias=[t1]) | ||
|
|
||
| PREHOOK: query: select * from vw_t2 | ||
| PREHOOK: type: QUERY | ||
| PREHOOK: Input: default@t1 | ||
| PREHOOK: Input: default@vw_t2 | ||
| #### A masked pattern was here #### | ||
| POSTHOOK: query: select * from vw_t2 | ||
| POSTHOOK: type: QUERY | ||
| POSTHOOK: Input: default@t1 | ||
| POSTHOOK: Input: default@vw_t2 | ||
| #### A masked pattern was here #### | ||
| PREHOOK: query: create view vw_t3 as | ||
| select username from (select username, id from t1 where id > 10 limit 10) x where username > 'a' limit 5 | ||
| PREHOOK: type: CREATEVIEW | ||
| PREHOOK: Input: default@t1 | ||
| PREHOOK: Output: database:default | ||
| PREHOOK: Output: default@vw_t3 | ||
| POSTHOOK: query: create view vw_t3 as | ||
| select username from (select username, id from t1 where id > 10 limit 10) x where username > 'a' limit 5 | ||
| POSTHOOK: type: CREATEVIEW | ||
| POSTHOOK: Input: default@t1 | ||
| POSTHOOK: Output: database:default | ||
| POSTHOOK: Output: default@vw_t3 | ||
| POSTHOOK: Lineage: vw_t3.username SIMPLE [(t1)t1.FieldSchema(name:username, type:string, comment:null), ] | ||
| PREHOOK: query: explain cbo select * from vw_t3 | ||
| PREHOOK: type: QUERY | ||
| PREHOOK: Input: default@t1 | ||
| PREHOOK: Input: default@vw_t3 | ||
| #### A masked pattern was here #### | ||
| POSTHOOK: query: explain cbo select * from vw_t3 | ||
| POSTHOOK: type: QUERY | ||
| POSTHOOK: Input: default@t1 | ||
| POSTHOOK: Input: default@vw_t3 | ||
| #### A masked pattern was here #### | ||
| CBO PLAN: | ||
| HiveSortLimit(fetch=[5]) | ||
| HiveProject(username=[$0]) | ||
| HiveFilter(condition=[>($0, _UTF-16LE'a')]) | ||
| HiveProject(username=[$0]) | ||
| HiveSortLimit(fetch=[10]) | ||
| HiveProject(username=[$0]) | ||
| HiveFilter(condition=[>($1, 10)]) | ||
| HiveTableScan(table=[[default, t1]], table:alias=[t1]) | ||
|
|
||
| PREHOOK: query: select * from vw_t3 | ||
| PREHOOK: type: QUERY | ||
| PREHOOK: Input: default@t1 | ||
| PREHOOK: Input: default@vw_t3 | ||
| #### A masked pattern was here #### | ||
| POSTHOOK: query: select * from vw_t3 | ||
| POSTHOOK: type: QUERY | ||
| POSTHOOK: Input: default@t1 | ||
| POSTHOOK: Input: default@vw_t3 | ||
| #### A masked pattern was here #### |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to add a call for the root node somewhere as well.