-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Enable using column identifiers with special characters when deleting table column statistics. #6149
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
Enable using column identifiers with special characters when deleting table column statistics. #6149
Changes from all commits
8b2b333
ae361bf
a4be9ad
7a94344
f4a0e34
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -133,6 +133,7 @@ | |
| */ | ||
| class MetaStoreDirectSql { | ||
| private static final int NO_BATCHING = -1, DETECT_BATCHING = 0; | ||
| private static final Set<String> ALLOWED_TABLES_TO_LOCK = Set.of("NOTIFICATION_SEQUENCE"); | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(MetaStoreDirectSql.class); | ||
| private final PersistenceManager pm; | ||
|
|
@@ -3203,6 +3204,11 @@ private void getStatsTableListResult( | |
| } | ||
|
|
||
| public void lockDbTable(String tableName) throws MetaException { | ||
| // Only certain tables are allowed to be locked, and the API should restrict them. | ||
| if (!ALLOWED_TABLES_TO_LOCK.contains(tableName)) { | ||
| throw new MetaException("Error while locking table " + tableName); | ||
| } | ||
|
|
||
| String lockCommand = "lock table \"" + tableName + "\" in exclusive mode"; | ||
| try { | ||
| executeNoResult(lockCommand); | ||
|
|
@@ -3243,19 +3249,26 @@ public void deleteColumnStatsState(long tbl_id) throws MetaException { | |
| } | ||
|
|
||
| public boolean deleteTableColumnStatistics(long tableId, List<String> colNames, String engine) { | ||
| String deleteSql = "delete from " + TAB_COL_STATS + " where \"TBL_ID\" = " + tableId; | ||
| String deleteSql = "delete from " + TAB_COL_STATS + " where \"TBL_ID\" = ?"; | ||
| List<Object> params = new ArrayList<>(colNames == null ? 2 : colNames.size() + 2); | ||
| params.add(tableId); | ||
|
|
||
| if (colNames != null && !colNames.isEmpty()) { | ||
| deleteSql += " and \"COLUMN_NAME\" in (" + colNames.stream().map(col -> "'" + col + "'").collect(Collectors.joining(",")) + ")"; | ||
| deleteSql += " and \"COLUMN_NAME\" in (" + makeParams(colNames.size()) + ")"; | ||
| params.addAll(colNames); | ||
| } | ||
|
|
||
| if (engine != null) { | ||
| deleteSql += " and \"ENGINE\" = '" + engine + "'"; | ||
| deleteSql += " and \"ENGINE\" = ?"; | ||
| params.add(engine); | ||
|
Contributor
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. Can we add a test case to add a harmful value?
Contributor
Author
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. added test case to test engine with special character |
||
| } | ||
| try { | ||
| executeNoResult(deleteSql); | ||
| } catch (SQLException e) { | ||
| LOG.warn("Error removing table column stats. ", e); | ||
|
|
||
| try (QueryWrapper queryParams = new QueryWrapper(pm.newQuery("javax.jdo.query.SQL", deleteSql))) { | ||
| executeWithArray(queryParams.getInnerQuery(), params.toArray(), deleteSql); | ||
| } catch (MetaException e) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
|
|
@@ -3269,17 +3282,20 @@ public List<Void> run(List<String> input) throws Exception { | |
| input, Collections.emptyList(), -1); | ||
| if (!partitionIds.isEmpty()) { | ||
| String deleteSql = "delete from " + PART_COL_STATS + " where \"PART_ID\" in ( " + getIdListForIn(partitionIds) + ")"; | ||
|
Contributor
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. This is just a pure question: I don't request any change in this pull request. Can we potentially replace
Member
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. the partitionIds is returned as long, and getIdListForIn(partitionIds) essentially calls Long.toString() which should output only digits, therefore no special characters can be introduced here. So, i think
Contributor
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 agree |
||
| List<Object> params = new ArrayList<>(colNames == null ? 1 : colNames.size() + 1); | ||
|
|
||
| if (colNames != null && !colNames.isEmpty()) { | ||
| deleteSql += " and \"COLUMN_NAME\" in (" + colNames.stream().map(col -> "'" + col + "'").collect(Collectors.joining(",")) + ")"; | ||
| deleteSql += " and \"COLUMN_NAME\" in (" + makeParams(colNames.size()) + ")"; | ||
| params.addAll(colNames); | ||
| } | ||
|
|
||
| if (engine != null) { | ||
| deleteSql += " and \"ENGINE\" = '" + engine + "'"; | ||
| deleteSql += " and \"ENGINE\" = ?"; | ||
| params.add(engine); | ||
| } | ||
| try { | ||
| executeNoResult(deleteSql); | ||
| } catch (SQLException e) { | ||
| LOG.warn("Error removing partition column stats. ", e); | ||
| throw new MetaException("Error removing partition column stats: " + e.getMessage()); | ||
|
|
||
| try (QueryWrapper queryParams = new QueryWrapper(pm.newQuery("javax.jdo.query.SQL", deleteSql))) { | ||
| executeWithArray(queryParams.getInnerQuery(), params.toArray(), deleteSql); | ||
| } | ||
| } | ||
| return null; | ||
|
|
||
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.
Let's add a test case for the exception if its easy/possible.
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 want a Java comment because I can't deserialize the intention about why we should throw an exception.
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.
Added test case and comment