Skip to content

Commit 73fd039

Browse files
authored
ide: fix column name inference for aliases (#731)
1 parent 981bcc8 commit 73fd039

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

crates/squawk_ide/src/column_name.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ use squawk_syntax::{
33
ast::{self, AstNode},
44
};
55

6+
fn normalize_identifier(text: &str) -> String {
7+
if text.starts_with('"') && text.ends_with('"') {
8+
text[1..text.len() - 1].to_string()
9+
} else {
10+
text.to_lowercase()
11+
}
12+
}
13+
614
#[derive(Clone, Debug, PartialEq)]
715
pub(crate) enum ColumnName {
816
Column(String),
@@ -27,10 +35,9 @@ impl ColumnName {
2735
if let Some(as_name) = target.as_name()
2836
&& let Some(name_node) = as_name.name()
2937
{
30-
return Some((
31-
ColumnName::Column(name_node.text().to_string()),
32-
name_node.syntax().clone(),
33-
));
38+
let text = name_node.text();
39+
let normalized = normalize_identifier(&text);
40+
return Some((ColumnName::Column(normalized), name_node.syntax().clone()));
3441
} else if let Some(expr) = target.expr()
3542
&& let Some(name) = name_from_expr(expr, false)
3643
{
@@ -350,8 +357,20 @@ fn examples() {
350357
assert_snapshot!(name("'{1}'::pg_catalog.varchar(1)[]::integer[];"), @"int4");
351358
assert_snapshot!(name("'1'::bigint::smallint"), @"int2");
352359

360+
// alias
361+
// with quoting
362+
assert_snapshot!(name(r#"'foo' as "FOO""#), @"FOO");
363+
assert_snapshot!(name(r#"'foo' as "foo""#), @"foo");
364+
// without quoting
365+
assert_snapshot!(name(r#"'foo' as FOO"#), @"foo");
366+
assert_snapshot!(name(r#"'foo' as foo"#), @"foo");
367+
353368
// tuple
354369
assert_snapshot!(name("(1, 2, 3)"), @"row");
370+
assert_snapshot!(name("(1, 2, 3)::address"), @"row");
371+
372+
// composite type
373+
assert_snapshot!(name("(x).city"), @"city");
355374

356375
// array types
357376
assert_snapshot!(name("'{{1, 2}, {3, 4}}'::int[]"), @"int4");

0 commit comments

Comments
 (0)