On Thursday, November 15, 2018 at 3:43:07 PM UTC-8, Artur Trofimov wrote:
>
> I fixed it with 
> Sequel.lit('(array_agg(grades.section_id))[1]')
>
> but maybe it can be done in a more beautiful way
>

One way to handle this is to have Sequel::SQL::Function#sql_subscript wrap 
the receiver in parentheses before passing it to SQL::Subscript:

 diff --git a/lib/sequel/sql.rb b/lib/sequel/sql.rb
index c1be60b2e..c004c4d5f 100644
--- a/lib/sequel/sql.rb
+++ b/lib/sequel/sql.rb
@@ -1427,6 +1427,10 @@ module Sequel
         with_opts(:quoted=>true)
       end
 
+      def sql_subscript(*sub)
+        Subscript.new(Sequel.lit('(?)', self), sub.flatten)
+      end
+
       # Return a new function where the function name will not be quoted 
even
       # if the database supports quoted functions:
       #

or maybe it is better to just always surround the expression with 
parentheses when literalizing, as this may handle more cases:

diff --git a/lib/sequel/dataset/sql.rb b/lib/sequel/dataset/sql.rb
index 2adc63db2..b40a127fd 100644
--- a/lib/sequel/dataset/sql.rb
+++ b/lib/sequel/dataset/sql.rb
@@ -717,8 +717,9 @@ module Sequel
 
     # Append literalization of subscripts (SQL array accesses) to SQL 
string.
     def subscript_sql_append(sql, s)
+      sql << '('
       literal_append(sql, s.expression)
-      sql << '['
+      sql << ')['
       sub = s.sub
       if sub.length == 1 && (range = sub.first).is_a?(Range)
         literal_append(sql, range.begin)

I'm guessing the second approach is better, but I'd appreciate feedback.  
Assuming it doesn't break the tests, I could probably accept either.

I wonder why PostgreSQL requires the extra parentheses in this case though.

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to