From 60760b95f226e0c7d53a557ddd30311fddfea00d Mon Sep 17 00:00:00 2001
From: Koji Noguchi <knoguchi@apache.org>
Date: Thu, 15 Oct 2020 15:14:07 +0000
Subject: [PATCH] PIG-5243: describe with typecast on as-clause shows the types
 before the typecasting (knoguchi)

git-svn-id: https://svn.apache.org/repos/asf/pig/trunk@1882545 13f79535-47bb-0310-9956-ffa450edef68
---
 CHANGES.txt                                           |  2 ++
 src/org/apache/pig/PigServer.java                     |  6 +++++-
 .../pig/newplan/logical/relational/LOForEach.java     | 10 ++++++++++
 .../logical/visitor/ForEachUserSchemaVisitor.java     |  2 ++
 test/org/apache/pig/test/TestPigServer.java           | 11 +++++++++++
 5 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/CHANGES.txt b/CHANGES.txt
index 83351561f3..f2710f3e09 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -100,6 +100,8 @@ OPTIMIZATIONS
  
 BUG FIXES
 
+PIG-5243: describe with typecast on as-clause shows the types before the typecasting (knoguchi)
+
 PIG-5403: streaming job stuck with script failure when combined with ORDER BY (knoguchi)
 
 PIG-5362: Parameter substitution of shell cmd results doesn't handle backslash addendum (szita)
diff --git a/src/org/apache/pig/PigServer.java b/src/org/apache/pig/PigServer.java
index f8eab63623..bd99813b7a 100644
--- a/src/org/apache/pig/PigServer.java
+++ b/src/org/apache/pig/PigServer.java
@@ -1511,7 +1511,11 @@ private LogicalRelationalOperator getOperatorForAlias(String alias) throws IOExc
             String msg = "No plan for " + alias + " to describe";
             throw new FrontendException(msg, errCode, PigException.INPUT, false, null);
         }
-        return op;
+        if( op instanceof LOForEach && ((LOForEach)op).getCasterForAsClause() != null) {
+            return ((LOForEach)op).getCasterForAsClause();
+        } else {
+            return op;
+        }
     }
 
     /**
diff --git a/src/org/apache/pig/newplan/logical/relational/LOForEach.java b/src/org/apache/pig/newplan/logical/relational/LOForEach.java
index f9008ed1db..18728c89f5 100644
--- a/src/org/apache/pig/newplan/logical/relational/LOForEach.java
+++ b/src/org/apache/pig/newplan/logical/relational/LOForEach.java
@@ -36,11 +36,21 @@ public class LOForEach extends LogicalRelationalOperator {
     private static final long serialVersionUID = 2L;
 
     private LogicalPlan innerPlan;
+
+    private LOForEach casterForAsClause = null;
       
     public LOForEach(OperatorPlan plan) {
         super("LOForEach", plan);		
     }
 
+    public LOForEach getCasterForAsClause() {
+        return casterForAsClause;
+    }
+
+    public void setCasterForAsClause(LOForEach casterForAsClause) {
+        this.casterForAsClause = casterForAsClause;
+    }
+
     public LogicalPlan getInnerPlan() {
         return innerPlan;
     }
diff --git a/src/org/apache/pig/newplan/logical/visitor/ForEachUserSchemaVisitor.java b/src/org/apache/pig/newplan/logical/visitor/ForEachUserSchemaVisitor.java
index ee6c4c5ea7..dd76dc7b2c 100644
--- a/src/org/apache/pig/newplan/logical/visitor/ForEachUserSchemaVisitor.java
+++ b/src/org/apache/pig/newplan/logical/visitor/ForEachUserSchemaVisitor.java
@@ -209,6 +209,8 @@ public void visit(LOForEach foreach) throws FrontendException {
                 plan.connect(foreach,casterForEach);
             }
 
+            foreach.setCasterForAsClause(casterForEach);
+
             // Since the explict cast is now inserted after the original foreach,
             // throwing away the user defined "types" but keeping the user
             // defined names from the original foreach.
diff --git a/test/org/apache/pig/test/TestPigServer.java b/test/org/apache/pig/test/TestPigServer.java
index 0f0de75b5b..08f585840c 100644
--- a/test/org/apache/pig/test/TestPigServer.java
+++ b/test/org/apache/pig/test/TestPigServer.java
@@ -532,6 +532,17 @@ public void testDescribeComplex() throws Throwable {
         assertEquals(expectedSchema, dumpedSchema);
     }
 
+    // PIG-5243
+    @Test
+    public void testDescribeAsClause() throws Throwable {
+        PigServer pig = new PigServer(cluster.getExecType(), properties);
+        pig.registerQuery("a = load 'a' as (field1: int);");
+        pig.registerQuery("b = FOREACH a generate field1 as (new_field:chararray);") ;
+        Schema dumpedSchema = pig.dumpSchema("b") ;
+        Schema expectedSchema = Utils.getSchemaFromString("new_field: chararray");
+        assertEquals(expectedSchema, dumpedSchema);
+    }
+
     private void registerScalarScript(boolean useScalar, String expectedSchemaStr) throws IOException {
         PigServer pig = new PigServer(cluster.getExecType(), properties);
         pig.registerQuery("A = load 'adata' AS (a: int, b: int);");