Skip to content

Commit

Permalink
Merge pull request #5317 from sploiselle/sql-type-names
Browse files Browse the repository at this point in the history
sql: use SQL standard names in output consistently
  • Loading branch information
Sean Loiselle authored Jan 16, 2021
2 parents 76228ce + 96f6456 commit ee94205
Show file tree
Hide file tree
Showing 36 changed files with 279 additions and 240 deletions.
14 changes: 12 additions & 2 deletions src/coord/src/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1817,8 +1817,18 @@ impl ExprHumanizer for ConnCatalog<'_> {
.join(",")
),
ty => {
let full_name = self.get_item_by_oid(&pgrepr::Type::from(ty).oid()).name();
let res = self.minimal_qualification(full_name).to_string();
let pgrepr_type = pgrepr::Type::from(ty);
let res = if self
.search_path
.iter()
.any(|schema| schema == &PG_CATALOG_SCHEMA)
{
pgrepr_type.name().to_string()
} else {
// If PG_CATALOG_SCHEMA is not in search path, you need
// qualified object name to refer to type.
self.get_item_by_oid(&pgrepr_type.oid()).name().to_string()
};
if let ScalarType::Decimal(p, s) = typ {
format!("{}({},{})", res, p, s)
} else {
Expand Down
29 changes: 28 additions & 1 deletion src/pgrepr/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,34 @@ impl Type {

/// Returns the name that PostgreSQL uses for this type.
pub fn name(&self) -> &'static str {
self.inner().name()
// postgres_types' `name()` uses the pg_catalog name, and not the pretty
// SQL standard name.
match self.inner() {
&postgres_types::Type::BOOL_ARRAY => "boolean[]",
&postgres_types::Type::BYTEA_ARRAY => "bytea[]",
&postgres_types::Type::DATE_ARRAY => "date[]",
&postgres_types::Type::FLOAT4_ARRAY => "real[]",
&postgres_types::Type::FLOAT8_ARRAY => "double precision[]",
&postgres_types::Type::INT4_ARRAY => "integer[]",
&postgres_types::Type::INT8_ARRAY => "bigint[]",
&postgres_types::Type::INTERVAL_ARRAY => "interval[]",
&postgres_types::Type::JSONB_ARRAY => "jsonb[]",
&postgres_types::Type::NUMERIC_ARRAY => "numeric[]",
&postgres_types::Type::OID_ARRAY => "oid[]",
&postgres_types::Type::RECORD_ARRAY => "record[]",
&postgres_types::Type::TEXT_ARRAY => "text[]",
&postgres_types::Type::TIME_ARRAY => "time[]",
&postgres_types::Type::TIMESTAMP_ARRAY => "timestamp[]",
&postgres_types::Type::TIMESTAMPTZ_ARRAY => "timestamp with time zone[]",
&postgres_types::Type::UUID_ARRAY => "uuid[]",
&postgres_types::Type::BOOL => "boolean",
&postgres_types::Type::FLOAT4 => "real",
&postgres_types::Type::FLOAT8 => "double precision",
&postgres_types::Type::INT4 => "integer",
&postgres_types::Type::INT8 => "bigint",
&postgres_types::Type::TIMESTAMPTZ => "timestamp with time zone",
other => other.name(),
}
}

/// Returns the [OID] of this type.
Expand Down
12 changes: 6 additions & 6 deletions src/repr/src/strconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub fn parse_bool(s: &str) -> Result<bool, ParseError> {
match s.trim().to_lowercase().as_str() {
"t" | "tr" | "tru" | "true" | "y" | "ye" | "yes" | "on" | "1" => Ok(true),
"f" | "fa" | "fal" | "fals" | "false" | "n" | "no" | "of" | "off" | "0" => Ok(false),
_ => Err(ParseError::new("bool", s)),
_ => Err(ParseError::new("boolean", s)),
}
}

Expand Down Expand Up @@ -97,7 +97,7 @@ where
pub fn parse_int32(s: &str) -> Result<i32, ParseError> {
s.trim()
.parse()
.map_err(|e| ParseError::new("int4", s).with_details(e))
.map_err(|e| ParseError::new("integer", s).with_details(e))
}

/// Writes an [`i32`] to `buf`.
Expand All @@ -113,7 +113,7 @@ where
pub fn parse_int64(s: &str) -> Result<i64, ParseError> {
s.trim()
.parse()
.map_err(|e| ParseError::new("int8", s).with_details(e))
.map_err(|e| ParseError::new("bigint", s).with_details(e))
}

/// Writes an `i64` to `buf`.
Expand All @@ -137,7 +137,7 @@ pub fn parse_float32(s: &str) -> Result<f32, ParseError> {
} else {
s.as_str()
.parse()
.map_err(|e| ParseError::new("float4", s.as_str()).with_details(e))
.map_err(|e| ParseError::new("real", s.as_str()).with_details(e))
}
}

Expand Down Expand Up @@ -170,7 +170,7 @@ pub fn parse_float64(s: &str) -> Result<f64, ParseError> {
} else {
s.as_str()
.parse()
.map_err(|e| ParseError::new("float8", s.as_str()).with_details(e))
.map_err(|e| ParseError::new("double precision", s.as_str()).with_details(e))
}
}

Expand Down Expand Up @@ -321,7 +321,7 @@ pub fn parse_timestamptz(s: &str) -> Result<DateTime<Utc>, ParseError> {
};
Ok(DateTime::from_utc(dt - offset, Utc))
})
.map_err(|e| ParseError::new("timestamptz", s).with_details(e))
.map_err(|e| ParseError::new("timestamp with time zone", s).with_details(e))
}

/// Writes a [`DateTime<Utc>`] timestamp to `buf`.
Expand Down
6 changes: 3 additions & 3 deletions src/repr/tests/strconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,17 +250,17 @@ fn test_parse_timestamptz() {
fn test_parse_timestamptz_errors() {
run_test_parse_timestamptz_errors(
"1999-01-01 01:23:34.555 +25:45",
"invalid input syntax for timestamptz: Invalid timezone string \
"invalid input syntax for timestamp with time zone: Invalid timezone string \
(+25:45): timezone hour invalid 25: \"1999-01-01 01:23:34.555 +25:45\"",
);
run_test_parse_timestamptz_errors(
"1999-01-01 01:23:34.555 +15:61",
"invalid input syntax for timestamptz: Invalid timezone string \
"invalid input syntax for timestamp with time zone: Invalid timezone string \
(+15:61): timezone minute invalid 61: \"1999-01-01 01:23:34.555 +15:61\"",
);
run_test_parse_timestamptz_errors(
"1999-01-01 01:23:34.555 4",
"invalid input syntax for timestamptz: Cannot parse timezone offset 4: \
"invalid input syntax for timestamp with time zone: Cannot parse timezone offset 4: \
\"1999-01-01 01:23:34.555 4\"",
);

Expand Down
10 changes: 5 additions & 5 deletions test/metabase/smoketest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ async fn main() -> Result<(), anyhow::Error> {
},
TableField {
name: "id".into(),
database_type: "int4".into(),
base_type: "type/Integer".into(),
database_type: "integer".into(),
base_type: "type/*".into(),
special_type: Some("type/PK".into()),
},
TableField {
name: "quantity".into(),
database_type: "int4".into(),
base_type: "type/Integer".into(),
special_type: Some("type/Quantity".into()),
database_type: "integer".into(),
base_type: "type/*".into(),
special_type: Some("type/Category".into()),
},
TableField {
name: "total".into(),
Expand Down
4 changes: 2 additions & 2 deletions test/sqllogictest/aggregates.slt
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ query TTT colnames
SHOW COLUMNS FROM t
----
name nullable type
a true int4
b true int4
a true integer
b true integer

# Tests on int8 sums to make sure we handle overflow and underflow correctly

Expand Down
16 changes: 8 additions & 8 deletions test/sqllogictest/arithmetic.slt
Original file line number Diff line number Diff line change
Expand Up @@ -464,36 +464,36 @@ SELECT round((SELECT * FROM nums));
----
NULL

query error Cannot call function round\(float8, int4\): arguments cannot be implicitly cast to any implementation's parameters; try providing explicit casts
query error Cannot call function round\(double precision, integer\): arguments cannot be implicitly cast to any implementation's parameters; try providing explicit casts
SELECT round((SELECT * FROM nums), 2)

query error Cannot call function round\(float8, float8\): arguments cannot be implicitly cast to any implementation's parameters; try providing explicit casts
query error Cannot call function round\(double precision, double precision\): arguments cannot be implicitly cast to any implementation's parameters; try providing explicit casts
SELECT round((SELECT * FROM nums), (SELECT * FROM nums))

query error Cannot call function round\(numeric\(38,1\), float8\): arguments cannot be implicitly cast to any implementation's parameters; try providing explicit casts
query error Cannot call function round\(numeric\(38,1\), double precision\): arguments cannot be implicitly cast to any implementation's parameters; try providing explicit casts
SELECT round(5.0, (SELECT * FROM nums))

query R
SELECT round(5.0, CAST ((SELECT * FROM nums) AS integer))
----
NULL

query error Cannot call function round\(float8, int4\): arguments cannot be implicitly cast to any implementation's parameters; try providing explicit casts
query error Cannot call function round\(double precision, integer\): arguments cannot be implicitly cast to any implementation's parameters; try providing explicit casts
SELECT round(CAST (5.0 AS double precision), 3)

query error Cannot call function round\(float8, int4\): arguments cannot be implicitly cast to any implementation's parameters; try providing explicit casts
query error Cannot call function round\(double precision, integer\): arguments cannot be implicitly cast to any implementation's parameters; try providing explicit casts
SELECT round(CAST (5.0 AS float), 3)

query error Cannot call function round\(bool, int4\): arguments cannot be implicitly cast to any implementation's parameters; try providing explicit casts
query error Cannot call function round\(boolean, integer\): arguments cannot be implicitly cast to any implementation's parameters; try providing explicit casts
SELECT round(true, 3)

query error
SELECT round(true)

query error Cannot call function round\(float8, numeric\(38,1\)\): arguments cannot be implicitly cast to any implementation's parameters; try providing explicit casts
query error Cannot call function round\(double precision, numeric\(38,1\)\): arguments cannot be implicitly cast to any implementation's parameters; try providing explicit casts
SELECT round(CAST (5.0 AS float), 3.0)

query error Cannot call function round\(float8, float8\): arguments cannot be implicitly cast to any implementation's parameters; try providing explicit casts
query error Cannot call function round\(double precision, double precision\): arguments cannot be implicitly cast to any implementation's parameters; try providing explicit casts
SELECT round(CAST (5.0 AS float), CAST (3.0 AS float))

query I
Expand Down
10 changes: 5 additions & 5 deletions test/sqllogictest/arrays.slt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mode cockroach

# Test coercion behavior of multidimensional arrays.

query error ARRAY expression cannot be cast to uniform type: int4\[\] vs text\[\]
query error ARRAY expression cannot be cast to uniform type: integer\[\] vs text\[\]
SELECT ARRAY[ARRAY[1, 2], ARRAY['3', '4']]

query T
Expand Down Expand Up @@ -51,15 +51,15 @@ SELECT 1 = ANY(ARRAY[2])
----
false

query error ANY operand array must have type int4\[\], not type text\[\]
query error ANY operand array must have type integer\[\], not type text\[\]
SELECT 1 = ANY(ARRAY['1', '2'])
----
true

query error ANY operand array must have type int4\[\], not type text\[\]
query error ANY operand array must have type integer\[\], not type text\[\]
SELECT 1 = ANY(ARRAY['hi'::text])

query error invalid input syntax for int4: invalid digit found in string: "hi"
query error invalid input syntax for integer: invalid digit found in string: "hi"
select 'hi' = any(array[1]);

query error cannot determine type of empty array
Expand All @@ -72,7 +72,7 @@ SELECT 'hi'::text = ANY(ARRAY[]::text[])
----
false

query error ARRAY expression cannot be cast to uniform type: int4 vs text
query error ARRAY expression cannot be cast to uniform type: integer vs text
SELECT 123.4 = ANY(ARRAY[1, true, 'hi'::text])

query B
Expand Down
22 changes: 11 additions & 11 deletions test/sqllogictest/boolean.slt
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,28 @@ tR true
tRuE true
TRUE true

query error invalid input syntax for bool: "blah"
query error invalid input syntax for boolean: "blah"
SELECT 'blah'::bool

query error NOT argument must have type bool, not type int4
query error NOT argument must have type boolean, not type integer
SELECT NOT 1

query error AND argument must have type bool, not type int4
query error AND argument must have type boolean, not type integer
SELECT 1 AND 1

query error OR argument must have type bool, not type int4
query error OR argument must have type boolean, not type integer
SELECT 1 OR 1

query error OR argument must have type bool, not type int4
query error OR argument must have type boolean, not type integer
SELECT 1 OR FALSE

query error OR argument must have type bool, not type int4
query error OR argument must have type boolean, not type integer
SELECT FALSE OR 1

query error AND argument must have type bool, not type int4
query error AND argument must have type boolean, not type integer
SELECT 1 AND FALSE

query error AND argument must have type bool, not type int4
query error AND argument must have type boolean, not type integer
SELECT FALSE AND 1

query B
Expand Down Expand Up @@ -159,7 +159,7 @@ SELECT NOT FALSE OR FALSE
----
true

### int4 to bool ###
### integer to bool ###

query B
SELECT 99::int::bool
Expand Down Expand Up @@ -193,7 +193,7 @@ SELECT NOT 0::bigint::bool
----
true

### bool to int4 ###
### bool to integer ###

query I
SELECT true::int
Expand Down Expand Up @@ -230,7 +230,7 @@ FROM x
EOF

statement ok
CREATE TABLE y (a bool, b bool)
CREATE TABLE y (a boolean, b bool)

# Bypass if statements with identical branches
query T multiline
Expand Down
2 changes: 1 addition & 1 deletion test/sqllogictest/cast.slt
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ int4_list
query T
SELECT pg_typeof('{1}'::int4_list::int4 list)
----
int4 list
integer list

query T
SELECT pg_typeof('{1}'::int4_list_list::int4_list_list_too)
Expand Down
2 changes: 1 addition & 1 deletion test/sqllogictest/cockroach/array.slt
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ SELECT ARRAY['a', 'b', 'c'][3.5]
query error could not parse "abc" as type int
SELECT ARRAY['a', 'b', 'c']['abc']

query error cannot subscript type int because it is not an array
query error cannot subscript type integer because it is not an array
SELECT (123)[2]

# array slicing
Expand Down
2 changes: 1 addition & 1 deletion test/sqllogictest/cockroach/order_by.slt
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ query I
query error pgcode 42601 multiple ORDER BY clauses not allowed
((SELECT a FROM t ORDER BY a)) ORDER BY a

query error CASE cannot be cast to uniform type: int4 vs bool
query error CASE cannot be cast to uniform type: integer vs boolean
SELECT CASE a WHEN 1 THEN b ELSE c END as val FROM t ORDER BY val

query error pgcode 42P10 column reference 0 in ORDER BY clause is out of range \(1 - 3\)
Expand Down
4 changes: 2 additions & 2 deletions test/sqllogictest/cockroach/tuple.slt
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ SELECT
a b c d
true NULL false NULL

statement error invalid input syntax for int4
statement error invalid input syntax for integer
SELECT (1, 2) > (1, 'hi') FROM tb

statement error unequal number of entries in row expressions
Expand Down Expand Up @@ -238,7 +238,7 @@ SELECT (ROW(sqrt(100.0)), 'ab') = (ROW(1 + 9), 'a' || 'b') AS a
a
true

query error invalid input syntax for int4
query error invalid input syntax for integer
SELECT ((1, 2), 'equal') = ((1, 'huh'), 'equal') FROM tb

# Issue #3568
Expand Down
8 changes: 4 additions & 4 deletions test/sqllogictest/cockroach/union.slt
Original file line number Diff line number Diff line change
Expand Up @@ -237,19 +237,19 @@ SELECT 1, 2 INTERSECT SELECT 3
query error pgcode 42601 each EXCEPT query must have the same number of columns: 2 vs 1
SELECT 1, 2 EXCEPT SELECT 3

query error pgcode 42804 UNION types int4 and text cannot be matched
query error pgcode 42804 UNION types integer and text cannot be matched
SELECT 1 UNION SELECT '3'

query error pgcode 42804 INTERSECT types int4 and text cannot be matched
query error pgcode 42804 INTERSECT types integer and text cannot be matched
SELECT 1 INTERSECT SELECT '3'

query error pgcode 42804 EXCEPT types int4 and text cannot be matched
query error pgcode 42804 EXCEPT types integer and text cannot be matched
SELECT 1 EXCEPT SELECT '3'

query error pgcode 42703 column "z" does not exist
SELECT 1 UNION SELECT 3 ORDER BY z

query error UNION types int4\[] and text\[] cannot be matched
query error UNION types integer\[] and text\[] cannot be matched
SELECT ARRAY[1] UNION ALL SELECT ARRAY['foo']

# Check that UNION permits columns of different visible types
Expand Down
2 changes: 1 addition & 1 deletion test/sqllogictest/cockroach/values.slt
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@ VALUES ((SELECT 1)), ((SELECT 2))
1
2

query error pgcode 42804 invalid input syntax for int4
query error pgcode 42804 invalid input syntax for integer
VALUES (NULL, 1), (2, NULL), (NULL, 'a')
Loading

0 comments on commit ee94205

Please sign in to comment.