Skip to content

Commit

Permalink
fix: name resolution bug in case-sensitive mode (#17097)
Browse files Browse the repository at this point in the history
fix name resolution bug in case-sensitive mode
  • Loading branch information
zhyass authored Dec 23, 2024
1 parent 55a7360 commit b0bb940
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
9 changes: 3 additions & 6 deletions src/query/sql/src/executor/table_read_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ use databend_common_expression::Scalar;
use databend_common_expression::TableField;
use databend_common_license::license::Feature::DataMask;
use databend_common_license::license_manager::LicenseManagerSwitch;
use databend_common_meta_app::tenant::Tenant;
use databend_common_settings::Settings;
use databend_common_users::UserApiProvider;
use databend_enterprise_data_mask_feature::get_datamask_handler;
use log::info;
Expand Down Expand Up @@ -117,7 +115,8 @@ impl ToReadDataSourcePlan for dyn Table {
});

// We need the partition sha256 to specify the result cache.
if ctx.get_settings().get_enable_query_result_cache()? {
let settings = ctx.get_settings();
if settings.get_enable_query_result_cache()? {
let sha = parts.compute_sha256()?;
ctx.add_partitions_sha(sha);
}
Expand Down Expand Up @@ -221,10 +220,8 @@ impl ToReadDataSourcePlan for dyn Table {

let body = &policy.body;
let tokens = tokenize_sql(body)?;
let ast_expr =
parse_expr(&tokens, ctx.get_settings().get_sql_dialect()?)?;
let ast_expr = parse_expr(&tokens, settings.get_sql_dialect()?)?;
let mut bind_context = BindContext::new();
let settings = Settings::create(Tenant::new_literal("dummy"));
let name_resolution_ctx =
NameResolutionContext::try_from(settings.as_ref())?;
let metadata = Arc::new(RwLock::new(Metadata::default()));
Expand Down
28 changes: 13 additions & 15 deletions src/query/sql/src/planner/expression_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ use databend_common_expression::TableField;
use databend_common_expression::TableSchemaRef;
use databend_common_functions::BUILTIN_FUNCTIONS;
use databend_common_meta_app::schema::TableInfo;
use databend_common_meta_app::tenant::Tenant;
use databend_common_settings::Settings;
use derive_visitor::DriveMut;
use parking_lot::RwLock;

Expand Down Expand Up @@ -118,9 +116,8 @@ pub fn parse_exprs(
sql: &str,
) -> Result<Vec<Expr>> {
let (mut bind_context, metadata) = bind_table(table_meta)?;
let settings = Settings::create(Tenant::new_literal("dummy"));
let settings = ctx.get_settings();
let name_resolution_ctx = NameResolutionContext::try_from(settings.as_ref())?;
let sql_dialect = ctx.get_settings().get_sql_dialect().unwrap_or_default();
let mut type_checker = TypeChecker::try_create(
&mut bind_context,
ctx,
Expand All @@ -131,6 +128,7 @@ pub fn parse_exprs(
)?;

let tokens = tokenize_sql(sql)?;
let sql_dialect = settings.get_sql_dialect().unwrap_or_default();
let ast_exprs = parse_comma_separated_exprs(&tokens, sql_dialect)?;
let exprs = ast_exprs
.iter()
Expand Down Expand Up @@ -187,7 +185,6 @@ pub fn parse_computed_expr(
schema: DataSchemaRef,
sql: &str,
) -> Result<Expr> {
let settings = Settings::create(Tenant::new_literal("dummy"));
let mut bind_context = BindContext::new();
let mut metadata = Metadata::default();
let table_schema = infer_table_schema(&schema)?;
Expand All @@ -212,8 +209,8 @@ pub fn parse_computed_expr(
);
}

let settings = ctx.get_settings();
let name_resolution_ctx = NameResolutionContext::try_from(settings.as_ref())?;
let sql_dialect = ctx.get_settings().get_sql_dialect()?;
let mut type_checker = TypeChecker::try_create(
&mut bind_context,
ctx,
Expand All @@ -224,6 +221,7 @@ pub fn parse_computed_expr(
)?;

let tokens = tokenize_sql(sql)?;
let sql_dialect = settings.get_sql_dialect()?;
let mut asts = parse_comma_separated_exprs(&tokens, sql_dialect)?;
if asts.len() != 1 {
return Err(ErrorCode::BadDataValueType(format!(
Expand All @@ -242,10 +240,10 @@ pub fn parse_default_expr_to_string(
field: &TableField,
ast: &AExpr,
) -> Result<(String, bool)> {
let settings = Settings::create(Tenant::new_literal("dummy"));
let mut bind_context = BindContext::new();
let metadata = Metadata::default();

let settings = ctx.get_settings();
let name_resolution_ctx = NameResolutionContext::try_from(settings.as_ref())?;
let mut type_checker = TypeChecker::try_create(
&mut bind_context,
Expand Down Expand Up @@ -285,7 +283,6 @@ pub fn parse_computed_expr_to_string(
field: &TableField,
ast: &AExpr,
) -> Result<String> {
let settings = Settings::create(Tenant::new_literal("dummy"));
let mut bind_context = BindContext::new();
let mut metadata = Metadata::default();
for (index, field) in table_schema.fields().iter().enumerate() {
Expand All @@ -309,6 +306,7 @@ pub fn parse_computed_expr_to_string(
);
}

let settings = ctx.get_settings();
let name_resolution_ctx = NameResolutionContext::try_from(settings.as_ref())?;
let mut type_checker = TypeChecker::try_create(
&mut bind_context,
Expand Down Expand Up @@ -355,7 +353,6 @@ pub fn parse_lambda_expr(
columns: &[(String, DataType)],
ast: &AExpr,
) -> Result<Box<(ScalarExpr, DataType)>> {
let settings = Settings::create(Tenant::new_literal("dummy"));
let metadata = Metadata::default();
bind_context.set_expr_context(ExprContext::InLambdaFunction);

Expand All @@ -372,6 +369,7 @@ pub fn parse_lambda_expr(
);
}

let settings = ctx.get_settings();
let name_resolution_ctx = NameResolutionContext::try_from(settings.as_ref())?;
let mut type_checker = TypeChecker::try_create(
&mut bind_context,
Expand All @@ -391,9 +389,8 @@ pub fn parse_cluster_keys(
cluster_key_str: &str,
) -> Result<Vec<Expr>> {
let (mut bind_context, metadata) = bind_table(table_meta)?;
let settings = Settings::create(Tenant::new_literal("dummy"));
let settings = ctx.get_settings();
let name_resolution_ctx = NameResolutionContext::try_from(settings.as_ref())?;
let sql_dialect = ctx.get_settings().get_sql_dialect().unwrap_or_default();
let mut type_checker = TypeChecker::try_create(
&mut bind_context,
ctx,
Expand All @@ -404,6 +401,7 @@ pub fn parse_cluster_keys(
)?;

let tokens = tokenize_sql(cluster_key_str)?;
let sql_dialect = settings.get_sql_dialect().unwrap_or_default();
let mut ast_exprs = parse_comma_separated_exprs(&tokens, sql_dialect)?;
// unwrap tuple.
if ast_exprs.len() == 1 {
Expand Down Expand Up @@ -468,9 +466,8 @@ pub fn parse_hilbert_cluster_key(
cluster_key_str: &str,
) -> Result<Vec<Expr>> {
let (mut bind_context, metadata) = bind_table(table_meta)?;
let settings = Settings::create(Tenant::new_literal("dummy"));
let settings = ctx.get_settings();
let name_resolution_ctx = NameResolutionContext::try_from(settings.as_ref())?;
let sql_dialect = ctx.get_settings().get_sql_dialect().unwrap_or_default();
let mut type_checker = TypeChecker::try_create(
&mut bind_context,
ctx,
Expand All @@ -481,6 +478,7 @@ pub fn parse_hilbert_cluster_key(
)?;

let tokens = tokenize_sql(cluster_key_str)?;
let sql_dialect = settings.get_sql_dialect().unwrap_or_default();
let mut ast_exprs = parse_comma_separated_exprs(&tokens, sql_dialect)?;
// unwrap tuple.
if ast_exprs.len() == 1 {
Expand Down Expand Up @@ -611,7 +609,8 @@ pub fn analyze_cluster_keys(
table_meta: Arc<dyn Table>,
sql: &str,
) -> Result<(String, Vec<Expr>)> {
let sql_dialect = ctx.get_settings().get_sql_dialect().unwrap_or_default();
let settings = ctx.get_settings();
let sql_dialect = settings.get_sql_dialect().unwrap_or_default();
let tokens = tokenize_sql(sql)?;
let mut ast_exprs = parse_comma_separated_exprs(&tokens, sql_dialect)?;
// unwrap tuple.
Expand All @@ -622,7 +621,6 @@ pub fn analyze_cluster_keys(
}

let (mut bind_context, metadata) = bind_table(table_meta)?;
let settings = Settings::create(Tenant::new_literal("dummy"));
let name_resolution_ctx = NameResolutionContext::try_from(settings.as_ref())?;
let mut type_checker = TypeChecker::try_create(
&mut bind_context,
Expand Down
15 changes: 15 additions & 0 deletions tests/sqllogictests/suites/query/case_sensitivity/name_hit.test
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,18 @@ delete from student

statement error (?s)1025,.*Unknown table `default`\.`default`\.student \.
select * from student

# ISSUE 17094
statement ok
CREATE OR REPLACE TABLE T_17094 (City VARCHAR(255), Temperature DECIMAL(10, 2))

statement ok
alter table T_17094 cluster by(City)

query TTIIT
select cluster_key, type, total_block_count, constant_block_count, block_depth_histogram from clustering_information('default','T_17094')
----
(City) linear 0 0 {}

statement ok
drop table T_17094 all

0 comments on commit b0bb940

Please sign in to comment.