1. 24 4月, 2019 1 次提交
  2. 01 4月, 2019 1 次提交
    • R
      Revert unused code and re-using query annotation for `update_all` and `delete_all` · 6e43a207
      Ryuta Kamizono 提交于
      This partly reverts #35617.
      
      #35617 includes unused code (for `InsertStatement`) and re-using query
      annotation for `update_all` and `delete_all`, which has not been
      discussed yet.
      
      If a relation has any annotation, I think it is mostly for SELECT query,
      so re-using annotation by default is not always desired behavior for me.
      
      We should discuss about desired behavior before publishing the
      implementation.
      6e43a207
  3. 22 3月, 2019 1 次提交
    • M
      Add Relation#annotate for SQL commenting · f4182580
      Matt Yoho 提交于
      This patch has two main portions:
      
      1. Add SQL comment support to Arel via Arel::Nodes::Comment.
      2. Implement a Relation#annotate method on top of that.
      
      == Adding SQL comment support
      
      Adds a new Arel::Nodes::Comment node that represents an optional SQL
      comment and teachers the relevant visitors how to handle it.
      
      Comment nodes may be added to the basic CRUD statement nodes and set
      through any of the four (Select|Insert|Update|Delete)Manager objects.
      
      For example:
      
          manager = Arel::UpdateManager.new
          manager.table table
          manager.comment("annotation")
          manager.to_sql # UPDATE "users" /* annotation */
      
      This new node type will be used by ActiveRecord::Relation to enable
      query annotation via SQL comments.
      
      == Implementing the Relation#annotate method
      
      Implements `ActiveRecord::Relation#annotate`, which accepts a comment
      string that will be appeneded to any queries generated by the relation.
      
      Some examples:
      
          relation = Post.where(id: 123).annotate("metadata string")
          relation.first
          # SELECT "posts".* FROM "posts" WHERE "posts"."id" = 123
          # LIMIT 1 /* metadata string */
      
          class Tag < ActiveRecord::Base
            scope :foo_annotated, -> { annotate("foo") }
          end
          Tag.foo_annotated.annotate("bar").first
          # SELECT "tags".* FROM "tags" LIMIT 1 /* foo */ /* bar */
      
      Also wires up the plumbing so this works with `#update_all` and
      `#delete_all` as well.
      
      This feature is useful for instrumentation and general analysis of
      queries generated at runtime.
      f4182580
  4. 19 3月, 2019 3 次提交
  5. 18 3月, 2019 2 次提交
  6. 16 3月, 2019 1 次提交
    • R
      Support Optimizer Hints · 97347d8c
      Ryuta Kamizono 提交于
      We as Arm Treasure Data are using Optimizer Hints with a monkey patch
      (https://gist.github.com/kamipo/4c8539f0ce4acf85075cf5a6b0d9712e),
      especially in order to use `MAX_EXECUTION_TIME` (refer #31129).
      
      Example:
      
      ```ruby
      class Job < ApplicationRecord
        default_scope { optimizer_hints("MAX_EXECUTION_TIME(50000) NO_INDEX_MERGE(jobs)") }
      end
      ```
      
      Optimizer Hints is supported not only for MySQL but also for most
      databases (PostgreSQL on RDS, Oracle, SQL Server, etc), it is really
      helpful to turn heavy queries for large scale applications.
      97347d8c
  7. 18 1月, 2019 2 次提交
    • R
      All of queries should return correct result even if including large number · 31ffbf8d
      Ryuta Kamizono 提交于
      Currently several queries cannot return correct result due to incorrect
      `RangeError` handling.
      
      First example:
      
      ```ruby
      assert_equal true, Topic.where(id: [1, 9223372036854775808]).exists?
      assert_equal true, Topic.where.not(id: 9223372036854775808).exists?
      ```
      
      The first example is obviously to be true, but currently it returns
      false.
      
      Second example:
      
      ```ruby
      assert_equal topics(:first), Topic.where(id: 1..9223372036854775808).find(1)
      ```
      
      The second example also should return the object, but currently it
      raises `RecordNotFound`.
      
      It can be seen from the examples, the queries including large number
      assuming empty result is not always correct.
      
      Therefore, This change handles `RangeError` to generate executable SQL
      instead of raising `RangeError` to users to always return correct
      result. By this change, it is no longer raised `RangeError` to users.
      31ffbf8d
    • R
      Use `unboundable?` rather than `boundable?` · 5b6daff5
      Ryuta Kamizono 提交于
      The `unboundable?` behaves like the `infinite?`.
      
      ```ruby
      inf = Topic.predicate_builder.build_bind_attribute(:id, Float::INFINITY)
      inf.infinite?    # => 1
      
      oob = Topic.predicate_builder.build_bind_attribute(:id, 9999999999999999999999999999999)
      oob.unboundable? # => 1
      
      inf = Topic.predicate_builder.build_bind_attribute(:id, -Float::INFINITY)
      inf.infinite?    # => -1
      
      oob = Topic.predicate_builder.build_bind_attribute(:id, -9999999999999999999999999999999)
      oob.unboundable? # => -1
      ```
      5b6daff5
  8. 09 1月, 2019 1 次提交
  9. 16 11月, 2018 1 次提交
    • D
      Arel: Implemented DB-aware NULL-safe comparison (#34451) · b5302d5a
      Dmytro Shteflyuk 提交于
      * Arel: Implemented DB-aware NULL-safe comparison
      
      * Fixed where clause inversion for NULL-safe comparison
      
      * Renaming "null_safe_eq" to "is_not_distinct_from", "null_safe_not_eq" to "is_distinct_from"
      
      [Dmytro Shteflyuk + Rafael Mendonça França]
      b5302d5a
  10. 14 11月, 2018 1 次提交
    • K
      Emit single pair of parens for UNION and UNION ALL · 63dd8d8e
      Keenan Brock 提交于
      mysql has a great implementation to suppress multiple parens for union
      sql statements.
      
      This moves that functionality to the generic implementation
      This also introduces that functionality for UNION ALL
      63dd8d8e
  11. 03 11月, 2018 1 次提交
  12. 10 10月, 2018 2 次提交
  13. 03 10月, 2018 1 次提交
  14. 01 10月, 2018 1 次提交
  15. 30 9月, 2018 2 次提交
  16. 28 9月, 2018 2 次提交
    • R
      Remove `visit_Fixnum` and `visit_Bignum` · ae406cd6
      Ryuta Kamizono 提交于
      Since Ruby 2.4 unified Fixnum and Bignum into Integer.
      ae406cd6
    • R
      Make `update_counters` preparable · e5190aca
      Ryuta Kamizono 提交于
      Before:
      
      ```
        Topic Update All (0.4ms)  UPDATE `topics` SET `topics`.`replies_count` = COALESCE(`topics`.`replies_count`, 0) + 1, `topics`.`updated_at` = '2018-09-27 18:34:05.068774' WHERE `topics`.`id` = ?  [["id", 7]]
      
      ```
      
      After:
      
      ```
        Topic Update All (0.4ms)  UPDATE `topics` SET `topics`.`replies_count` = COALESCE(`topics`.`replies_count`, 0) + ?, `topics`.`updated_at` = ? WHERE `topics`.`id` = ?  [["replies_count", 1], ["updated_at", 2018-09-27 18:55:05 UTC], ["id", 7]]
      
      ```
      e5190aca
  17. 25 9月, 2018 1 次提交
    • V
      Abandon TOP support. · 41b92914
      Vladimir Kochnev 提交于
      Initially, `TOP` was introduced to support `limit` for MSSQL database.
      Unlike PostgreSQL/MySQL/SQLite, MSSQL does not have native `LIMIT`/`OFFSET` support.
      The commit adding `TOP` is 1a246f71.
      
      However, it figured out that `TOP` implementation was weak and it's not sufficient
      to also support `OFFSET`, then `TOP` was substituted with
      `ROW_NUMBER()` subquery in be48ed30.
      This is a well known trick in MSSQL -
      https://stackoverflow.com/questions/2135418/equivalent-of-limit-and-offset-for-sql-server.
      
      So now we don't need this `visit_Arel_Nodes_Top` at all.
      It does nothing useful but also adds an extra space after `SELECT` when `LIMIT` is being
      used for **any** database.
      41b92914
  18. 09 9月, 2018 1 次提交
  19. 24 2月, 2018 2 次提交