Author: olga Date: Mon Sep 22 10:50:22 2008 New Revision: 697917 URL: http://svn.apache.org/viewvc?rev=697917&view=rev Log: PIG-442: Disambiguated alias after a foreach flatten is not accessible a couple of statements after the foreach
Modified: incubator/pig/branches/types/CHANGES.txt incubator/pig/branches/types/src/org/apache/pig/impl/logicalLayer/LOForEach.java incubator/pig/branches/types/src/org/apache/pig/impl/logicalLayer/parser/QueryParser.jjt incubator/pig/branches/types/src/org/apache/pig/impl/logicalLayer/schema/Schema.java incubator/pig/branches/types/test/org/apache/pig/test/TestEvalPipeline.java incubator/pig/branches/types/test/org/apache/pig/test/TestLogicalPlanBuilder.java Modified: incubator/pig/branches/types/CHANGES.txt URL: http://svn.apache.org/viewvc/incubator/pig/branches/types/CHANGES.txt?rev=697917&r1=697916&r2=697917&view=diff ============================================================================== --- incubator/pig/branches/types/CHANGES.txt (original) +++ incubator/pig/branches/types/CHANGES.txt Mon Sep 22 10:50:22 2008 @@ -218,4 +218,7 @@ PIG-440: Exceptions from UDFs inside a foreach are not captured (pradeepk via olgan) + PIG-442: Disambiguated alias after a foreach flatten is not accessible a + couple of statements after the foreach (sms via olgan) + Modified: incubator/pig/branches/types/src/org/apache/pig/impl/logicalLayer/LOForEach.java URL: http://svn.apache.org/viewvc/incubator/pig/branches/types/src/org/apache/pig/impl/logicalLayer/LOForEach.java?rev=697917&r1=697916&r2=697917&view=diff ============================================================================== --- incubator/pig/branches/types/src/org/apache/pig/impl/logicalLayer/LOForEach.java (original) +++ incubator/pig/branches/types/src/org/apache/pig/impl/logicalLayer/LOForEach.java Mon Sep 22 10:50:22 2008 @@ -325,9 +325,6 @@ log.debug("alias: " + alias); if((null != alias) && (count == 1)) { mSchema.addAlias(alias, fs); - // alias is unambiguous - so set it - // as the alias in the field schema - fs.alias = alias; } } mIsSchemaComputed = true; Modified: incubator/pig/branches/types/src/org/apache/pig/impl/logicalLayer/parser/QueryParser.jjt URL: http://svn.apache.org/viewvc/incubator/pig/branches/types/src/org/apache/pig/impl/logicalLayer/parser/QueryParser.jjt?rev=697917&r1=697916&r2=697917&view=diff ============================================================================== --- incubator/pig/branches/types/src/org/apache/pig/impl/logicalLayer/parser/QueryParser.jjt (original) +++ incubator/pig/branches/types/src/org/apache/pig/impl/logicalLayer/parser/QueryParser.jjt Mon Sep 22 10:50:22 2008 @@ -1550,9 +1550,13 @@ t = <IDENTIFIER> { int i; - if ( over == null || (i = over.getPosition(t.image)) == -1) { - throw new ParseException("Invalid alias: " + t.image + " in " + over); - } + try { + if ( over == null || (i = over.getPosition(t.image)) == -1) { + throw new ParseException("Invalid alias: " + t.image + " in " + over); + } + } catch (FrontendException fee) { + throw new ParseException(fee.getMessage()); + } log.trace("Exiting ColNameOrNum"); return i; @@ -2011,9 +2015,13 @@ foreachInput = specs.get(t.image); } if(null == foreachInput) { - if ((null == over) || (i = over.getPosition(t.image)) == -1) { - throw new ParseException("Invalid alias: " + t.image + " in " + over); - } + try { + if ((null == over) || (i = over.getPosition(t.image)) == -1) { + throw new ParseException("Invalid alias: " + t.image + " in " + over); + } + } catch (FrontendException fee) { + throw new ParseException(fee.getMessage()); + } foreachInput = new LOProject(lp, new OperatorKey(scope, getNextId()), input, i); } @@ -3243,14 +3251,18 @@ if (item == null){ log.debug("item == null"); if (null == over) log.debug("over is null"); - if ( over == null || (i = over.getPosition(t1.image)) == -1) { - log.debug("Invalid alias: " + t1.image + " in " + over); - if(null != over) { - log.debug("Printing out the aliases in the schema"); - over.printAliases(); - } - throw new ParseException("Invalid alias: " + t1.image + " in " + over); - } + try { + if ( over == null || (i = over.getPosition(t1.image)) == -1) { + log.debug("Invalid alias: " + t1.image + " in " + over); + if(null != over) { + log.debug("Printing out the aliases in the schema"); + over.printAliases(); + } + throw new ParseException("Invalid alias: " + t1.image + " in " + over); + } + } catch (FrontendException fee) { + throw new ParseException(fee.getMessage()); + } log.debug("Position of " + t1.image + " = " + i); if(null != over) { log.debug("Printing out the aliases in the schema"); Modified: incubator/pig/branches/types/src/org/apache/pig/impl/logicalLayer/schema/Schema.java URL: http://svn.apache.org/viewvc/incubator/pig/branches/types/src/org/apache/pig/impl/logicalLayer/schema/Schema.java?rev=697917&r1=697916&r2=697917&view=diff ============================================================================== --- incubator/pig/branches/types/src/org/apache/pig/impl/logicalLayer/schema/Schema.java (original) +++ incubator/pig/branches/types/src/org/apache/pig/impl/logicalLayer/schema/Schema.java Mon Sep 22 10:50:22 2008 @@ -464,8 +464,53 @@ * @param alias Alias to look up. * @return FieldSchema, or null if no such alias is in this tuple. */ - public FieldSchema getField(String alias) { - return mAliases.get(alias); + public FieldSchema getField(String alias) throws FrontendException { + FieldSchema fs = mAliases.get(alias); + if(null == fs) { + String cocoPrefix = new String("::" + alias); + Map<String, Integer> aliasMatches = new HashMap<String, Integer>(); + //build the map of aliases that have cocoPrefix as the suffix + for(String key: mAliases.keySet()) { + if(key.endsWith(cocoPrefix)) { + Integer count = aliasMatches.get(key); + if(null == count) { + aliasMatches.put(key, 1); + } else { + aliasMatches.put(key, ++count); + } + } + } + //process the map to check if + //1. are there multiple keys with count == 1 + //2. are there keys with count > 1 --> should never occur + //3. if thers is a single key with count == 1 we have our match + + if(aliasMatches.keySet().size() == 0) { + return null; + } + if(aliasMatches.keySet().size() == 1) { + Object[] keys = aliasMatches.keySet().toArray(); + String key = (String)keys[0]; + if(aliasMatches.get(key) > 1) { + throw new FrontendException("Found duplicate aliases: " + key); + } + return mAliases.get(key); + } else { + boolean hasNext = false; + StringBuilder sb = new StringBuilder("Found more than one match: "); + for (String key: aliasMatches.keySet()) { + if(hasNext) { + sb.append(", "); + } else { + hasNext = true; + } + sb.append(key); + } + throw new FrontendException(sb.toString()); + } + } else { + return fs; + } } /** @@ -722,7 +767,7 @@ * alias of the FieldSchema. * @return position of the FieldSchema. */ - public int getPosition(String alias) { + public int getPosition(String alias) throws FrontendException{ FieldSchema fs = getField(alias); Modified: incubator/pig/branches/types/test/org/apache/pig/test/TestEvalPipeline.java URL: http://svn.apache.org/viewvc/incubator/pig/branches/types/test/org/apache/pig/test/TestEvalPipeline.java?rev=697917&r1=697916&r2=697917&view=diff ============================================================================== --- incubator/pig/branches/types/test/org/apache/pig/test/TestEvalPipeline.java (original) +++ incubator/pig/branches/types/test/org/apache/pig/test/TestEvalPipeline.java Mon Sep 22 10:50:22 2008 @@ -375,7 +375,7 @@ } - /*public void testNestedPlan() throws Exception{ + public void testNestedPlan() throws Exception{ int LOOP_COUNT = 10; File tmpFile = File.createTempFile("test", "txt"); PrintStream ps = new PrintStream(new FileOutputStream(tmpFile)); @@ -484,7 +484,7 @@ ++numIdentity; } assertEquals(5, numIdentity); - }*/ + } } Modified: incubator/pig/branches/types/test/org/apache/pig/test/TestLogicalPlanBuilder.java URL: http://svn.apache.org/viewvc/incubator/pig/branches/types/test/org/apache/pig/test/TestLogicalPlanBuilder.java?rev=697917&r1=697916&r2=697917&view=diff ============================================================================== --- incubator/pig/branches/types/test/org/apache/pig/test/TestLogicalPlanBuilder.java (original) +++ incubator/pig/branches/types/test/org/apache/pig/test/TestLogicalPlanBuilder.java Mon Sep 22 10:50:22 2008 @@ -1318,12 +1318,8 @@ buildPlan("b = group a by name;"); buildPlan("c = foreach b generate flatten(a);"); buildPlan("d = foreach c generate name;"); - // test that we can refer to "name" field and not a::name - try { - buildPlan("e = foreach d generate a::name;"); - } catch (AssertionFailedError e) { - assertTrue(e.getMessage().contains("Invalid alias: a::name in {name: bytearray}")); - } + // test that we can refer to "name" field and a::name + buildPlan("e = foreach d generate a::name;"); } @Test @@ -1350,7 +1346,7 @@ try { buildPlan("e = foreach d generate name;"); } catch (AssertionFailedError e) { - assertTrue(e.getMessage().contains("Invalid alias: name in {a::name: bytearray")); + assertTrue(e.getMessage().contains("Found more than one match:")); } }