Skip to content

Apache DataFusion SQL Query Engine

feat: Error when a SHOW command is passed in with an accompanying non-existant variable (#11540)

June47d5d1fe1ac7
diff --git a/datafusion/sql/src/statement.rs b/datafusion/sql/src/statement.rs
index 6df2508630..8eb4113f80 100644
--- a/datafusion/sql/src/statement.rs
+++ b/datafusion/sql/src/statement.rs
@@ -1146,6 +1146,22 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
// we could introduce alias in OptionDefinition if this string matching thing grows
format!("{base_query} WHERE name = 'datafusion.execution.time_zone'")
} else {
+ // These values are what are used to make the information_schema table, so we just
+ // check here, before actually planning or executing the query, if it would produce no
+ // results, and error preemptively if it would (for a better UX)
+ let is_valid_variable = self
+ .context_provider
+ .options()
+ .entries()
+ .iter()
+ .any(|opt| opt.key == variable);
+
+ if !is_valid_variable {
+ return plan_err!(
+ "'{variable}' is not a variable which can be viewed with 'SHOW'"
+ );
+ }
+
format!("{base_query} WHERE name = '{variable}'")
};
diff --git a/datafusion/sqllogictest/test_files/information_schema.slt b/datafusion/sqllogictest/test_files/information_schema.slt
index c8c0d1d45b..1c6ffd44b1 100644
--- a/datafusion/sqllogictest/test_files/information_schema.slt
+++ b/datafusion/sqllogictest/test_files/information_schema.slt
@@ -370,9 +370,12 @@ datafusion.execution.time_zone +00:00 The default time zone Some functions, e.g.
# show empty verbose
-query TTT
+statement error DataFusion error: Error during planning: '' is not a variable which can be viewed with 'SHOW'
SHOW VERBOSE
-----
+
+# show nonsense verbose
+statement error DataFusion error: Error during planning: 'nonsense' is not a variable which can be viewed with 'SHOW'
+SHOW NONSENSE VERBOSE
# information_schema_describe_table
@@ -508,9 +511,7 @@ SHOW columns from datafusion.public.t2
# show_non_existing_variable
-# FIXME
-# currently we cannot know whether a variable exists, this will output 0 row instead
-statement ok
+statement error DataFusion error: Error during planning: 'something_unknown' is not a variable which can be viewed with 'SHOW'
SHOW SOMETHING_UNKNOWN;
statement ok