Yasuhiro Horimoto 2019-02-22 12:41:58 +0900 (Fri, 22 Feb 2019) Revision: 95be2b7b9cdddf6e0e5384aa924ec515106ed554 https://github.com/groonga/groonga-query-log/commit/95be2b7b9cdddf6e0e5384aa924ec515106ed554 Message: run-regression-test: support rewriting "column1 @ \"keyword\" && column2 @~ \"^(?!.*keyword1|keyword2|...).+$\"" (#41) * run-regression-test: support rewriting "column1 @ \"keyword\" && column2 @~ \"^(?!.*keyword1|keyword2|...).+$\"" * Use more naturally a variable name * Add test case for non-rewrite case * Use "-" as word separator * Improve option name Because "rewrite-regular-expression" is too generic name. So, we use the name that an easier to understand the feature of this option. * Fix wrong regular expressions * Improve variable names and method names Because "rewrite-regular-expression" is too generic name. So, we use the name that an easier to understand the feature. * Remove a needless test * Add a test for reference * Add a test for underscore * Remove needless variables Modified files: lib/groonga-query-log/command/run-regression-test.rb lib/groonga-query-log/command/verify-server.rb lib/groonga-query-log/filter-rewriter.rb lib/groonga-query-log/server-verifier.rb test/test-filter-rewriter.rb Modified: lib/groonga-query-log/command/run-regression-test.rb (+14 -0) =================================================================== --- lib/groonga-query-log/command/run-regression-test.rb 2019-01-21 13:15:11 +0900 (06624dd) +++ lib/groonga-query-log/command/run-regression-test.rb 2019-02-22 12:41:58 +0900 (32c7ee1) @@ -50,6 +50,7 @@ module GroongaQueryLog @vector_accessors = [] @rewrite_nullable_reference_number = false @nullable_reference_number_accessors = [] + @rewrite_not_or_regular_expression = false @care_order = true @ignored_drilldown_keys = [] @@ -187,6 +188,14 @@ module GroongaQueryLog "specifying this option multiple times") do |accessor| @nullable_reference_number_accessors << accessor end + parser.on("--[no-]rewrite-not-or-regular-expression", + "Rewrite 'column1 @ \"keyword1\" && column2 @~ " + + "\"^(?!.*keyword2|keyword3|...).+$\"' " + + "with 'column1 @ \"keyword1\" &! column2 @ \"keyword2\" " + + "&! column2 @ \"keyword3\" &! ...'", + "(#{@rewrite_not_or_regular_expression})") do |boolean| + @rewrite_not_or_regular_expression = boolean + end parser.separator("") parser.separator("Comparisons:") @@ -252,6 +261,8 @@ module GroongaQueryLog @rewrite_nullable_reference_number, :nullable_reference_number_accessors => @nullable_reference_number_accessors, + :rewrite_not_or_regular_expression => + @rewrite_not_or_regular_expression, :target_command_names => @target_command_names, :read_timeout => @read_timeout, } @@ -547,6 +558,9 @@ module GroongaQueryLog command_line << "--nullable-reference-number-accessor" command_line << accessor end + if @options[:rewrite_not_or_regular_expression] + command_line << "--rewrite-not-or-regular-expression" + end if @options[:target_command_names] command_line << "--target-command-names" command_line << @options[:target_command_names].join(",") Modified: lib/groonga-query-log/command/verify-server.rb (+9 -0) =================================================================== --- lib/groonga-query-log/command/verify-server.rb 2019-01-21 13:15:11 +0900 (a6ee4e8) +++ lib/groonga-query-log/command/verify-server.rb 2019-02-22 12:41:58 +0900 (ecbcc28) @@ -230,6 +230,15 @@ module GroongaQueryLog @options.rewrite_nullable_reference_number = boolean end + parser.on("--[no-]rewrite-not-or-regular-expression", + "Rewrite 'column1 @ \"keyword1\" && column2 @~ " + + "\"^(?!.*keyword2|keyword3|...).+$\"' " + + "with 'column1 @ \"keyword1\" &! column2 @ \"keyword2\" " + + "&! column2 @ \"keyword3\" &! ...'", + "(#{@options.rewrite_not_or_regular_expression?})") do |boolean| + @options.rewrite_not_or_regular_expression = boolean + end + parser.on("--nullable-reference-number-accessor=ACCESSOR", "Mark ACCESSOR as rewrite nullable reference number targets", "You can specify multiple accessors by", Modified: lib/groonga-query-log/filter-rewriter.rb (+23 -0) =================================================================== --- lib/groonga-query-log/filter-rewriter.rb 2019-01-21 13:15:11 +0900 (fd1cf9e) +++ lib/groonga-query-log/filter-rewriter.rb 2019-02-22 12:41:58 +0900 (a600454) @@ -35,6 +35,9 @@ module GroongaQueryLog if @options[:rewrite_nullable_reference_number] rewritten = rewrite_nullable_reference_number(rewritten) end + if @options[:rewrite_not_or_regular_expression] + rewritten = rewrite_not_or_regular_expression(rewritten) + end rewritten end @@ -77,5 +80,25 @@ module GroongaQueryLog end end end + + def rewrite_not_or_regular_expression(filter) + filter.gsub(/&& *(?<target_column>[a-zA-Z0-9_.]+) *@~ *"(?<pattern>.*?)"/) do |matched| + target_column = $LAST_MATCH_INFO[:target_column] + pattern = $LAST_MATCH_INFO[:pattern] + + case pattern + when /\A(?<header>(?:\^|\\A)\(\?\!\.\*) + (?<body>.+) + (?<footer>\)\.[+*](?:\$|\\z))\z/x + body = $LAST_MATCH_INFO[:body] + conditions = body.split("|").collect do |word| + "&! #{target_column} @ \"#{word.strip}\"" + end + conditions.join(" ") + else + matched + end + end + end end end Modified: lib/groonga-query-log/server-verifier.rb (+10 -1) =================================================================== --- lib/groonga-query-log/server-verifier.rb 2019-01-21 13:15:11 +0900 (089e0a7) +++ lib/groonga-query-log/server-verifier.rb 2019-02-22 12:41:58 +0900 (eb4e613) @@ -240,6 +240,7 @@ module GroongaQueryLog attr_accessor :vector_accessors attr_writer :rewrite_nullable_reference_number attr_accessor :nullable_reference_number_accessors + attr_writer :rewrite_not_or_regular_expression def initialize @groonga1 = GroongaOptions.new @groonga2 = GroongaOptions.new @@ -268,6 +269,7 @@ module GroongaQueryLog @vector_accessors = [] @rewrite_nullable_reference_number = false @nullable_reference_number_accessors = [] + @rewrite_not_or_regular_expression = false end def request_queue_size @@ -298,6 +300,10 @@ module GroongaQueryLog @rewrite_nullable_reference_number end + def rewrite_not_or_regular_expression? + @rewrite_not_or_regular_expression + end + def target_command_name?(name) return false if name.nil? @@ -326,7 +332,8 @@ module GroongaQueryLog def need_filter_rewrite? rewrite_vector_equal? or rewrite_vector_not_equal_empty_string? or - rewrite_nullable_reference_number? + rewrite_nullable_reference_number? or + rewrite_not_or_regular_expression end def to_filter_rewriter_options @@ -339,6 +346,8 @@ module GroongaQueryLog rewrite_nullable_reference_number?, :nullable_reference_number_accessors => nullable_reference_number_accessors, + :rewrite_not_or_regular_expression => + rewrite_not_or_regular_expression?, } end end Modified: test/test-filter-rewriter.rb (+27 -0) =================================================================== --- test/test-filter-rewriter.rb 2019-01-21 13:15:11 +0900 (49a964d) +++ test/test-filter-rewriter.rb 2019-02-22 12:41:58 +0900 (2d8019e) @@ -110,4 +110,31 @@ class FilterRewriterTest < Test::Unit::TestCase ["ref_column.number"])) end end + + class RegularExpressionTest < self + def rewrite(filter, enabled = true) + super(filter, + :rewrite_not_or_regular_expression => enabled) + end + + def test_rewrite_one + assert_equal("column1 @ \"value1\" &! column2 @ \"value2\"", + rewrite("column1 @ \"value1\" && column2 @~ \"^(?!.*value2).+$\"")) + end + + def test_reference + assert_equal("column1 @ \"value1\" &! reference.column2 @ \"value2\"", + rewrite("column1 @ \"value1\" && reference.column2 @~ \"^(?!.*value2).+$\"")) + end + + def test_under_score + assert_equal("column1 @ \"value1\" &! column_2 @ \"value_2\"", + rewrite("column1 @ \"value1\" && column_2 @~ \"^(?!.*value_2).+$\"")) + end + + def test_rewrite_multiple + assert_equal("column1 @ \"value1\" &! column2 @ \"value2\" &! column2 @ \"value3\" &! column2 @ \"value4\"", + rewrite("column1 @ \"value1\" && column2 @~ \"^(?!.*value2 | value3 | value4).+$\"")) + end + end end -------------- next part -------------- An HTML attachment was scrubbed... URL: <https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20190222/cfe24e04/attachment-0001.html>