Return-Path: X-Original-To: apmail-pig-commits-archive@www.apache.org Delivered-To: apmail-pig-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id EF7B7DBCD for ; Fri, 19 Oct 2012 00:00:39 +0000 (UTC) Received: (qmail 76984 invoked by uid 500); 19 Oct 2012 00:00:39 -0000 Delivered-To: apmail-pig-commits-archive@pig.apache.org Received: (qmail 76933 invoked by uid 500); 19 Oct 2012 00:00:39 -0000 Mailing-List: contact commits-help@pig.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@pig.apache.org Delivered-To: mailing list commits@pig.apache.org Received: (qmail 76926 invoked by uid 99); 19 Oct 2012 00:00:39 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 19 Oct 2012 00:00:39 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 19 Oct 2012 00:00:36 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id D1AEA238890D for ; Thu, 18 Oct 2012 23:59:51 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1399927 - in /pig/trunk: CHANGES.txt src/org/apache/pig/tools/parameters/PreprocessorContext.java test/org/apache/pig/test/TestParamSubPreproc.java Date: Thu, 18 Oct 2012 23:59:51 -0000 To: commits@pig.apache.org From: jcoveney@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20121018235951.D1AEA238890D@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: jcoveney Date: Thu Oct 18 23:59:51 2012 New Revision: 1399927 URL: http://svn.apache.org/viewvc?rev=1399927&view=rev Log: PIG-2931: $ signs in the replacement string make parameter substitution fail (cheolsoo via jcoveney) Modified: pig/trunk/CHANGES.txt pig/trunk/src/org/apache/pig/tools/parameters/PreprocessorContext.java pig/trunk/test/org/apache/pig/test/TestParamSubPreproc.java Modified: pig/trunk/CHANGES.txt URL: http://svn.apache.org/viewvc/pig/trunk/CHANGES.txt?rev=1399927&r1=1399926&r2=1399927&view=diff ============================================================================== --- pig/trunk/CHANGES.txt (original) +++ pig/trunk/CHANGES.txt Thu Oct 18 23:59:51 2012 @@ -42,6 +42,8 @@ Release 0.11.0 (unreleased) INCOMPATIBLE CHANGES +PIG-2931: $ signs in the replacement string make parameter substitution fail (cheolsoo via jcoveney) + PIG-1891 Enable StoreFunc to make intelligent decision based on job success or failure (initialcontext via gates) IMPROVEMENTS Modified: pig/trunk/src/org/apache/pig/tools/parameters/PreprocessorContext.java URL: http://svn.apache.org/viewvc/pig/trunk/src/org/apache/pig/tools/parameters/PreprocessorContext.java?rev=1399927&r1=1399926&r2=1399927&view=diff ============================================================================== --- pig/trunk/src/org/apache/pig/tools/parameters/PreprocessorContext.java (original) +++ pig/trunk/src/org/apache/pig/tools/parameters/PreprocessorContext.java Thu Oct 18 23:59:51 2012 @@ -16,7 +16,7 @@ * limitations under the License. */ -/** +/** * This is helper class for parameter substitution */ @@ -35,18 +35,18 @@ import java.util.regex.Pattern; public class PreprocessorContext { private Hashtable param_val ; - + private final Log log = LogFactory.getLog(getClass()); /** * @param limit - max number of parameters. Passing * smaller number only impacts performance - */ + */ public PreprocessorContext(int limit){ param_val = new Hashtable (limit); } - - /* + + /* public void processLiteral(String key, String val) { processLiteral(key, val, true); } */ @@ -107,7 +107,7 @@ public class PreprocessorContext { String sub_val = substitute(val); sub_val = executeShellCommand(sub_val); param_val.put(key, sub_val); - } + } /** * This method generates value for the specified key by @@ -129,13 +129,13 @@ public class PreprocessorContext { String sub_val = substitute(val); param_val.put(key, sub_val); - } + } /* * executes the 'cmd' in shell and returns result */ - private String executeShellCommand (String cmd) + private String executeShellCommand (String cmd) { Process p; String streamData=""; @@ -188,7 +188,7 @@ public class PreprocessorContext { } finally { if (br != null) try {br.close();} catch(Exception e) {} } - + try { InputStreamReader isr = new InputStreamReader(p.getErrorStream()); br = new BufferedReader(isr); @@ -211,14 +211,14 @@ public class PreprocessorContext { } private Pattern id_pattern = Pattern.compile("\\$[_]*[a-zA-Z][a-zA-Z_0-9]*"); - + public String substitute(String line) { int index = line.indexOf('$'); if (index == -1) return line; String replaced_line = line; - + Matcher keyMatcher = id_pattern.matcher( line ); String key=""; String val=""; @@ -232,8 +232,10 @@ public class PreprocessorContext { throw new RuntimeException("Undefined parameter : "+key); } val = param_val.get(key); - //String litVal = Matcher.quoteReplacement(val); - replaced_line = replaced_line.replaceFirst("\\$"+key, val); + if (val.contains("$")) { + val = val.replaceAll("\\$", "\\\\\\$"); + } + replaced_line = replaced_line.replaceFirst("\\$"+key, val); } } Modified: pig/trunk/test/org/apache/pig/test/TestParamSubPreproc.java URL: http://svn.apache.org/viewvc/pig/trunk/test/org/apache/pig/test/TestParamSubPreproc.java?rev=1399927&r1=1399926&r2=1399927&view=diff ============================================================================== --- pig/trunk/test/org/apache/pig/test/TestParamSubPreproc.java (original) +++ pig/trunk/test/org/apache/pig/test/TestParamSubPreproc.java Thu Oct 18 23:59:51 2012 @@ -1482,6 +1482,52 @@ public class TestParamSubPreproc extends log.info("Done"); } + /* Test case + * Test that $ signs in substitution value are treated as literal replacement strings. + */ + @Test + public void testSubstitutionWithDollarSign() throws Exception{ + try { + ParameterSubstitutionPreprocessor ps = new ParameterSubstitutionPreprocessor(50); + pigIStream = new BufferedReader(new FileReader(basedir + "/inputDollarSign.pig")); + pigOStream = new FileWriter(basedir + "/output1.pig"); + + String[] arg = {"filter=\"($0 == 'x') and ($1 == 'y')\""}; + String[] argFiles = null; + ps.genSubstitutedFile(pigIStream , pigOStream , arg , argFiles); + + FileInputStream pigResultStream = new FileInputStream(basedir + "/output1.pig"); + pigExResultStream = new FileInputStream(basedir + "/ExpectedResultDollarSign.pig"); + BufferedReader inExpected = new BufferedReader(new InputStreamReader(pigExResultStream)); + BufferedReader inResult = new BufferedReader(new InputStreamReader(pigResultStream)); + + String exLine; + String resLine; + int lineNum=0; + + while (true) { + lineNum++; + exLine = inExpected.readLine(); + resLine = inResult.readLine(); + if (exLine==null || resLine==null) + break; + assertEquals("Expected : "+exLine+" , but got : "+resLine+" in line num : "+lineNum ,exLine.trim(), resLine.trim()); + } + if (!(exLine==null && resLine==null)) { + fail ("Expected : "+exLine+" , but got : "+resLine+" in line num : "+lineNum); + } + + inExpected.close(); + inResult.close(); + } catch (ParseException e) { + fail ("Got ParseException : " + e.getMessage()); + } catch (RuntimeException e) { + fail ("Got RuntimeException : " + e.getMessage()); + } catch (Error e) { + fail ("Got error : " + e.getMessage()); + } + } + @Test public void testMacroDef() throws Exception{ log.info("Starting test testMacroDef() ...");