Skip to content

Commit bea4b68

Browse files
fix: Add WITH ORDER display in information_schema.views (#18282)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Closes #18267. /cc @NGA-TRAN ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> The `information_schema.views` does not have display `WITH ORDER` for the definition of a table. ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> Added condition for writing `WITH ORDER` for CreateExternalTable. ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> Did not add tests for this functionality as not other display functionality has tests and seems like a separate PR would be appropriate if this is needed. This was tested manually with: In `datafusion-cli` ``` -- Not sorted CREATE EXTERNAL TABLE dimension_csv STORED AS CSV LOCATION '/path/to/the/attached/dimension_1.csv' OPTIONS ('format.has_header' 'true'); -- Sorted CREATE EXTERNAL TABLE dimension_csv_sorted STORED AS CSV WITH ORDER (env, service, host) LOCATION '/path/to/the/attached/dimension_1.csv' OPTIONS ('format.has_header' 'true'); ``` Then running: ``` select * from information_schema.views; ``` With link to data: [dimension_1.csv](https://github.com/user-attachments/files/23124138/dimension_1.csv) ## Are there any user-facing changes? Yes, improves the information_schema.views display to include `WITH ORDER` <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. -->
1 parent 38f86c8 commit bea4b68

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

datafusion/sql/src/parser.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,19 @@ impl fmt::Display for CreateExternalTable {
243243
}
244244
write!(f, "{} ", self.name)?;
245245
write!(f, "STORED AS {} ", self.file_type)?;
246-
write!(f, "LOCATION {} ", self.location)
246+
if !self.order_exprs.is_empty() {
247+
write!(f, "WITH ORDER (")?;
248+
let mut first = true;
249+
for expr in self.order_exprs.iter().flatten() {
250+
if !first {
251+
write!(f, ", ")?;
252+
}
253+
write!(f, "{expr}")?;
254+
first = false;
255+
}
256+
write!(f, ") ")?;
257+
}
258+
write!(f, "LOCATION {}", self.location)
247259
}
248260
}
249261

datafusion/sqllogictest/test_files/information_schema.slt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,54 @@ SHOW CREATE TABLE abc;
705705
----
706706
datafusion public abc CREATE EXTERNAL TABLE abc STORED AS CSV LOCATION ../../testing/data/csv/aggregate_test_100.csv
707707

708+
# show_external_create_table_with_order
709+
statement ok
710+
CREATE EXTERNAL TABLE abc_ordered
711+
STORED AS CSV
712+
WITH ORDER (c1)
713+
LOCATION '../../testing/data/csv/aggregate_test_100.csv'
714+
OPTIONS ('format.has_header' 'true');
715+
716+
query TTTT
717+
SHOW CREATE TABLE abc_ordered;
718+
----
719+
datafusion public abc_ordered CREATE EXTERNAL TABLE abc_ordered STORED AS CSV WITH ORDER (c1) LOCATION ../../testing/data/csv/aggregate_test_100.csv
720+
721+
statement ok
722+
DROP TABLE abc_ordered;
723+
724+
# show_external_create_table_with_multiple_order_columns
725+
statement ok
726+
CREATE EXTERNAL TABLE abc_multi_order
727+
STORED AS CSV
728+
WITH ORDER (c1, c2 DESC)
729+
LOCATION '../../testing/data/csv/aggregate_test_100.csv'
730+
OPTIONS ('format.has_header' 'true');
731+
732+
query TTTT
733+
SHOW CREATE TABLE abc_multi_order;
734+
----
735+
datafusion public abc_multi_order CREATE EXTERNAL TABLE abc_multi_order STORED AS CSV WITH ORDER (c1, c2 DESC) LOCATION ../../testing/data/csv/aggregate_test_100.csv
736+
737+
statement ok
738+
DROP TABLE abc_multi_order;
739+
740+
# show_external_create_table_with_order_nulls
741+
statement ok
742+
CREATE EXTERNAL TABLE abc_order_nulls
743+
STORED AS CSV
744+
WITH ORDER (c1 NULLS LAST, c2 DESC NULLS FIRST)
745+
LOCATION '../../testing/data/csv/aggregate_test_100.csv'
746+
OPTIONS ('format.has_header' 'true');
747+
748+
query TTTT
749+
SHOW CREATE TABLE abc_order_nulls;
750+
----
751+
datafusion public abc_order_nulls CREATE EXTERNAL TABLE abc_order_nulls STORED AS CSV WITH ORDER (c1 NULLS LAST, c2 DESC NULLS FIRST) LOCATION ../../testing/data/csv/aggregate_test_100.csv
752+
753+
statement ok
754+
DROP TABLE abc_order_nulls;
755+
708756
# string_agg has different arg_types but same return type. Test avoiding duplicate entries for the same function.
709757
query TTT
710758
select routine_name, data_type, function_type from information_schema.routines where routine_name = 'string_agg';

0 commit comments

Comments
 (0)