STET

agent_patch

runs/2026-02-28__18-27-22__gpt-5-1-codex-mini/flux-pr-1495/flux-pr-1495.1-of-1.2026-02-28__18-27-22__gpt-5-1-codex-mini/agent-logs/agent.patch

5560 bytes

Back to adjudication
diff --git a/tmp/agent-patch-flux-pr-1495.1-of-1.2026-02-28__18-27-22__gpt-5-1-codex-mini/app/src/ast/query.rs b/app/src/ast/query.rs
index dc5966e..aa701e2 100644
--- a/tmp/agent-patch-flux-pr-1495.1-of-1.2026-02-28__18-27-22__gpt-5-1-codex-mini/app/src/ast/query.rs
+++ b/app/src/ast/query.rs
@@ -327,11 +327,22 @@ impl fmt::Display for Select {
             write!(f, " {value_table_mode}")?;
         }
 
-        if let Some(ref distinct) = self.distinct {
-            write!(f, " {distinct}")?;
-        }
-        if let Some(ref top) = self.top {
-            write!(f, " {top}")?;
+        let top_before_distinct =
+            self.top.as_ref().map(|top| top.before_distinct).unwrap_or(false);
+        if top_before_distinct {
+            if let Some(ref top) = self.top {
+                write!(f, " {top}")?;
+            }
+            if let Some(ref distinct) = self.distinct {
+                write!(f, " {distinct}")?;
+            }
+        } else {
+            if let Some(ref distinct) = self.distinct {
+                write!(f, " {distinct}")?;
+            }
+            if let Some(ref top) = self.top {
+                write!(f, " {top}")?;
+            }
         }
         write!(f, " {}", display_comma_separated(&self.projection))?;
 
@@ -1996,6 +2007,8 @@ pub struct Top {
     /// MSSQL only.
     pub percent: bool,
     pub quantity: Option<TopQuantity>,
+    /// `TOP` was parsed before `ALL`/`DISTINCT`.
+    pub before_distinct: bool,
 }
 
 #[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
diff --git a/tmp/agent-patch-flux-pr-1495.1-of-1.2026-02-28__18-27-22__gpt-5-1-codex-mini/app/src/dialect/mod.rs b/app/src/dialect/mod.rs
index 5abddba..b6ca163 100644
--- a/tmp/agent-patch-flux-pr-1495.1-of-1.2026-02-28__18-27-22__gpt-5-1-codex-mini/app/src/dialect/mod.rs
+++ b/app/src/dialect/mod.rs
@@ -216,6 +216,11 @@ pub trait Dialect: Debug + Any {
         false
     }
 
+    /// Returns true if `SELECT TOP` must appear before `ALL`/`DISTINCT`.
+    fn select_top_before_distinct(&self) -> bool {
+        false
+    }
+
     /// Returns true if the dialect supports the MATCH_RECOGNIZE operation.
     fn supports_match_recognize(&self) -> bool {
         false
diff --git a/tmp/agent-patch-flux-pr-1495.1-of-1.2026-02-28__18-27-22__gpt-5-1-codex-mini/app/src/dialect/redshift.rs b/app/src/dialect/redshift.rs
index 3bfdec3..7ce464a 100644
--- a/tmp/agent-patch-flux-pr-1495.1-of-1.2026-02-28__18-27-22__gpt-5-1-codex-mini/app/src/dialect/redshift.rs
+++ b/app/src/dialect/redshift.rs
@@ -68,4 +68,8 @@ impl Dialect for RedshiftSqlDialect {
     fn supports_connect_by(&self) -> bool {
         true
     }
+
+    fn select_top_before_distinct(&self) -> bool {
+        true
+    }
 }
diff --git a/tmp/agent-patch-flux-pr-1495.1-of-1.2026-02-28__18-27-22__gpt-5-1-codex-mini/app/src/parser/mod.rs b/app/src/parser/mod.rs
index fd7d1c5..e71d694 100644
--- a/tmp/agent-patch-flux-pr-1495.1-of-1.2026-02-28__18-27-22__gpt-5-1-codex-mini/app/src/parser/mod.rs
+++ b/app/src/parser/mod.rs
@@ -9193,12 +9193,24 @@ impl<'a> Parser<'a> {
                 None
             };
 
-        let distinct = self.parse_all_or_distinct()?;
-
-        let top = if self.parse_keyword(Keyword::TOP) {
-            Some(self.parse_top()?)
+        let (distinct, top) = if self.dialect.select_top_before_distinct() {
+            let top = if self.parse_keyword(Keyword::TOP) {
+                let mut top = self.parse_top()?;
+                top.before_distinct = true;
+                Some(top)
+            } else {
+                None
+            };
+            let distinct = self.parse_all_or_distinct()?;
+            (distinct, top)
         } else {
-            None
+            let distinct = self.parse_all_or_distinct()?;
+            let top = if self.parse_keyword(Keyword::TOP) {
+                Some(self.parse_top()?)
+            } else {
+                None
+            };
+            (distinct, top)
         };
 
         let projection = self.parse_projection()?;
@@ -11550,8 +11562,8 @@ impl<'a> Parser<'a> {
         Ok(InterpolateExpr { column, expr })
     }
 
-    /// Parse a TOP clause, MSSQL equivalent of LIMIT,
-    /// that follows after `SELECT [DISTINCT]`.
+    /// Parse a TOP clause, MSSQL equivalent of LIMIT.
+    /// The ordering relative to `SELECT ALL|DISTINCT` depends on the dialect.
     pub fn parse_top(&mut self) -> Result<Top, ParserError> {
         let quantity = if self.consume_token(&Token::LParen) {
             let quantity = self.parse_expr()?;
@@ -11574,6 +11586,7 @@ impl<'a> Parser<'a> {
             with_ties,
             percent,
             quantity,
+            before_distinct: false,
         })
     }
 
diff --git a/tmp/agent-patch-flux-pr-1495.1-of-1.2026-02-28__18-27-22__gpt-5-1-codex-mini/app/tests/sqlparser_redshift.rs b/app/tests/sqlparser_redshift.rs
index a25d506..a1e4ad4 100644
--- a/tmp/agent-patch-flux-pr-1495.1-of-1.2026-02-28__18-27-22__gpt-5-1-codex-mini/app/tests/sqlparser_redshift.rs
+++ b/app/tests/sqlparser_redshift.rs
@@ -196,3 +196,11 @@ fn test_create_view_with_no_schema_binding() {
     redshift_and_generic()
         .verified_stmt("CREATE VIEW myevent AS SELECT eventname FROM event WITH NO SCHEMA BINDING");
 }
+
+#[test]
+fn parse_redshift_top_before_distinct() {
+    let select = redshift().verified_only_select("SELECT TOP 5 DISTINCT eventname FROM event");
+    let top = select.top.unwrap();
+    assert!(top.before_distinct);
+    assert_eq!(select.distinct, Some(Distinct::Distinct));
+}