-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Fix hive view case sensitivity #52694
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
66 changes: 66 additions & 0 deletions
66
fe/fe-core/src/main/java/org/apache/doris/common/util/HiveViewSqlTransformer.java
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,66 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| package org.apache.doris.common.util; | ||
|
|
||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| /** | ||
| * Utility class for transforming Hive view SQL to ensure case consistency. | ||
| * This transformer converts SQL keywords, table names, and column names to lowercase | ||
| * while preserving the case of string literals enclosed in quotes. | ||
| */ | ||
| public class HiveViewSqlTransformer { | ||
|
|
||
| // Pattern to match quoted strings (both single and double quotes) | ||
| private static final Pattern QUOTE_PATTERN = Pattern.compile("(\"[^\"]*\"|'[^']*')"); | ||
|
|
||
| /** | ||
| * Formats the input SQL by converting all non-quoted content to lowercase. | ||
| * This helps ensure case consistency when processing Hive view definitions, | ||
| * as Hive is case-insensitive but Doris may be case-sensitive in certain contexts. | ||
| * | ||
| * @param input the original SQL string from Hive view definition | ||
| * @return formatted SQL with non-quoted content in lowercase | ||
| */ | ||
| public static String format(String input) { | ||
| if (input == null) { | ||
| return null; | ||
| } | ||
|
|
||
| Matcher quoteMatcher = QUOTE_PATTERN.matcher(input); | ||
| StringBuffer result = new StringBuffer(); | ||
| int lastIndex = 0; | ||
|
|
||
| // Process each quoted string separately | ||
| while (quoteMatcher.find()) { | ||
| // Convert non-quoted content to lowercase | ||
| result.append(input.substring(lastIndex, quoteMatcher.start()).toLowerCase()); | ||
| // Preserve quoted content as-is | ||
| result.append(quoteMatcher.group()); | ||
| lastIndex = quoteMatcher.end(); | ||
| } | ||
|
|
||
| // Handle remaining non-quoted content | ||
| if (lastIndex < input.length()) { | ||
| result.append(input.substring(lastIndex).toLowerCase()); | ||
| } | ||
|
|
||
| return result.toString(); | ||
| } | ||
| } | ||
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
75 changes: 75 additions & 0 deletions
75
fe/fe-core/src/test/java/org/apache/doris/common/util/HiveViewSqlTransformerTest.java
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,75 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| package org.apache.doris.common.util; | ||
|
|
||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
|
|
||
| public class HiveViewSqlTransformerTest { | ||
|
|
||
| @Test | ||
| public void testFormat() { | ||
| // Test case 1: Simple string literal should remain unchanged | ||
| String sql1 = "select 'TEST1'"; | ||
| Assert.assertEquals(sql1, HiveViewSqlTransformer.format(sql1)); | ||
|
|
||
| // Test case 2: Mixed case SQL with double-quoted string literal | ||
| String sql2 = "SELECT col1, col2, col3 from test_db.TEST_TABLE where COL1 = \"TEST\""; | ||
| Assert.assertEquals("select col1, col2, col3 from test_db.test_table where col1 = \"TEST\"", | ||
| HiveViewSqlTransformer.format(sql2)); | ||
|
|
||
| // Test case 3: Complex SQL with aggregation functions and mixed quotes | ||
| String sql3 = "SELECT count(col1), SUM(col2), col3, col4 from TEST_DB.TEST_TABLE GROUP BY COL3, col4 " | ||
| + "where COL3 = \"TEST\" and COL4='YES'"; | ||
| Assert.assertEquals("select count(col1), sum(col2), col3, col4 from test_db.test_table group by " | ||
| + "col3, col4 where col3 = \"TEST\" and col4='YES'", HiveViewSqlTransformer.format(sql3)); | ||
|
|
||
| // Test case 4: Single-quoted string literals | ||
| String sql4 = "SELECT COL1, COL2 from TEST_DB.TEST_TABLE where COL1 = 'TEST' and COL2='YES'"; | ||
| Assert.assertEquals("select col1, col2 from test_db.test_table where col1 = 'TEST' and col2='YES'", | ||
| HiveViewSqlTransformer.format(sql4)); | ||
|
|
||
| // Test case 5: Double-quoted string literals | ||
| String sql5 = "SELECT COL1, COL2 from TEST_DB.TEST_TABLE where COL1 = \"TEST\" and COL2=\"YES\""; | ||
| Assert.assertEquals("select col1, col2 from test_db.test_table where col1 = \"TEST\" and col2=\"YES\"", | ||
| HiveViewSqlTransformer.format(sql5)); | ||
|
|
||
| // Test case 6: Mixed quote types | ||
| String sql6 = "SELECT COL1, COL2 from TEST_DB.TEST_TABLE where COL1 = 'TEST' and COL2=\"YES\""; | ||
| Assert.assertEquals("select col1, col2 from test_db.test_table where col1 = 'TEST' and col2=\"YES\"", | ||
| HiveViewSqlTransformer.format(sql6)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNullInput() { | ||
| Assert.assertNull(HiveViewSqlTransformer.format(null)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEmptyInput() { | ||
| Assert.assertEquals("", HiveViewSqlTransformer.format("")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testComplexQuotedStrings() { | ||
| // Test with quoted strings containing special characters | ||
| String sql = "SELECT * FROM TABLE_NAME WHERE col = 'It\\'s a test' AND col2 = \"Quote \\\"test\\\"\""; | ||
| String expected = "select * from table_name where col = 'It\\'s a test' and col2 = \"Quote \\\"test\\\"\""; | ||
| Assert.assertEquals(expected, HiveViewSqlTransformer.format(sql)); | ||
| } | ||
| } |
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.
convert SQL without parser and analyzer is not safe.
Recently I refactor the whole case sensibility issue: #52561
And for hive catalog, you can try adding
"only_test_lower_case_table_names" = "2"to see if it can solve your problem