Skip to content

Commit

Permalink
feat: convert table name to lowercase when not quoted (apache#444)
Browse files Browse the repository at this point in the history
* normalize table name

* add testcase
  • Loading branch information
jiacai2050 authored Dec 2, 2022
1 parent 82488c5 commit e95b21f
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 2 deletions.
12 changes: 10 additions & 2 deletions sql/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! SQL statement

use sqlparser::ast::{
ColumnDef, ObjectName, SqlOption, Statement as SqlStatement, TableConstraint,
ColumnDef, Ident, ObjectName, SqlOption, Statement as SqlStatement, TableConstraint,
};

/// Statement representations
Expand Down Expand Up @@ -33,14 +33,22 @@ impl TableName {
pub fn is_empty(&self) -> bool {
self.0 .0.is_empty()
}

// Normalize an identifer to a lowercase string unless the identifier is quoted.
fn normalize_ident(id: &Ident) -> String {
match id.quote_style {
Some(_) => id.value.clone(),
None => id.value.to_ascii_lowercase(),
}
}
}

impl ToString for TableName {
fn to_string(&self) -> String {
self.0
.0
.iter()
.map(|ident| ident.value.as_str())
.map(Self::normalize_ident)
.collect::<Vec<_>>()
.join(".")
}
Expand Down
69 changes: 69 additions & 0 deletions tests/cases/local/03_dml/case_insensitive.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
DROP TABLE IF EXISTS case_insensitive_table1;

affected_rows: 0

CREATE TABLE case_insensitive_table1 (
ts timestamp NOT NULL,
value1 double,
timestamp KEY (ts)) ENGINE=Analytic
WITH(
enable_ttl='false'
);

affected_rows: 0

INSERT INTO case_insensitive_table1 (ts, value1)
VALUES (1, 10), (2, 20), (3, 30);

affected_rows: 3

SELECT
*
FROM
case_insensitive_table1;

ts,tsid,value1,
Timestamp(Timestamp(1)),Int64(0),Double(10.0),
Timestamp(Timestamp(2)),Int64(0),Double(20.0),
Timestamp(Timestamp(3)),Int64(0),Double(30.0),


SELECT
*
FROM
CASE_INSENSITIVE_TABLE1;

ts,tsid,value1,
Timestamp(Timestamp(1)),Int64(0),Double(10.0),
Timestamp(Timestamp(2)),Int64(0),Double(20.0),
Timestamp(Timestamp(3)),Int64(0),Double(30.0),


SHOW CREATE TABLE case_insensitive_table1;

Table,Create Table,
String(StringBytes(b"case_insensitive_table1")),String(StringBytes(b"CREATE TABLE `case_insensitive_table1` (`ts` timestamp NOT NULL, `tsid` uint64 NOT NULL, `value1` double, PRIMARY KEY(ts,tsid), TIMESTAMP KEY(ts)) ENGINE=Analytic WITH(arena_block_size='2097152', compaction_strategy='default', compression='ZSTD', enable_ttl='false', num_rows_per_row_group='8192', segment_duration='', storage_format='COLUMNAR', ttl='7d', update_mode='OVERWRITE', write_buffer_size='33554432')")),


SHOW CREATE TABLE CASE_INSENSITIVE_TABLE1;

Table,Create Table,
String(StringBytes(b"case_insensitive_table1")),String(StringBytes(b"CREATE TABLE `case_insensitive_table1` (`ts` timestamp NOT NULL, `tsid` uint64 NOT NULL, `value1` double, PRIMARY KEY(ts,tsid), TIMESTAMP KEY(ts)) ENGINE=Analytic WITH(arena_block_size='2097152', compaction_strategy='default', compression='ZSTD', enable_ttl='false', num_rows_per_row_group='8192', segment_duration='', storage_format='COLUMNAR', ttl='7d', update_mode='OVERWRITE', write_buffer_size='33554432')")),


DESC case_insensitive_table1;

name,type,is_primary,is_nullable,is_tag,
String(StringBytes(b"ts")),String(StringBytes(b"timestamp")),Boolean(true),Boolean(false),Boolean(false),
String(StringBytes(b"tsid")),String(StringBytes(b"uint64")),Boolean(true),Boolean(false),Boolean(false),
String(StringBytes(b"value1")),String(StringBytes(b"double")),Boolean(false),Boolean(true),Boolean(false),


DESC CASE_INSENSITIVE_TABLE1;

name,type,is_primary,is_nullable,is_tag,
String(StringBytes(b"ts")),String(StringBytes(b"timestamp")),Boolean(true),Boolean(false),Boolean(false),
String(StringBytes(b"tsid")),String(StringBytes(b"uint64")),Boolean(true),Boolean(false),Boolean(false),
String(StringBytes(b"value1")),String(StringBytes(b"double")),Boolean(false),Boolean(true),Boolean(false),


34 changes: 34 additions & 0 deletions tests/cases/local/03_dml/case_insensitive.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

DROP TABLE IF EXISTS case_insensitive_table1;


CREATE TABLE case_insensitive_table1 (
ts timestamp NOT NULL,
value1 double,
timestamp KEY (ts)) ENGINE=Analytic
WITH(
enable_ttl='false'
);

INSERT INTO case_insensitive_table1 (ts, value1)
VALUES (1, 10), (2, 20), (3, 30);


SELECT
*
FROM
case_insensitive_table1;

SELECT
*
FROM
CASE_INSENSITIVE_TABLE1;


SHOW CREATE TABLE case_insensitive_table1;

SHOW CREATE TABLE CASE_INSENSITIVE_TABLE1;

DESC case_insensitive_table1;

DESC CASE_INSENSITIVE_TABLE1;

0 comments on commit e95b21f

Please sign in to comment.