title: Adding LIMIT fixes "Invalid digit, Value N" error in Amazon Redshift. Why?
body: <p>I have a standard <code>listings</code> table on Redshift table with all varchars (due to loading into database)</p>
  
  <p>This query (simplified) gives me error:</p>
  
  <pre><code>with AL as (
    select
      L.price::int as price,
    from listings L
    where L.price &lt;&gt; 'NULL'
      and L.listing_type &lt;&gt; 'NULL'
  )
  select price from AL
  where price &lt; 800
  </code></pre>
  
  <p>and the error:</p>
  
  <pre><code>  -----------------------------------------------
    error:  Invalid digit, Value 'N', Pos 0, Type: Integer 
    code:      1207
    context:   NULL
    query:     2422868
    location:  :0
    process:   query0_24 [pid=0]
    -----------------------------------------------
  </code></pre>
  
  <p>If I remove the <code>where price &lt; 800</code> condition, the query returns just fine... but I need the where condition to be there.</p>
  
  <p>I've also checked the number validity of the <code>price</code> field and all look good.</p>
  
  <p>After playing around, this actually makes it work, and I can't quite explain why.</p>
  
  <pre><code>with AL as (
    select
      L.price::int as price,
    from listings L
    where L.price &lt;&gt; 'NULL'
      and L.listing_type &lt;&gt; 'NULL'
    limit 10000000000
  )
  select price from AL
  where price &lt; 800
  </code></pre>
  
  <p>Note that the table has far less records than the number stated in limit.</p>
  
  <p>Can anyone (possibly from the Redshift engineer team) explain why this is the way it is? Possibly something to do with how the query plan being executed and parallelized?</p>