Showing posts with label Compress. Show all posts
Showing posts with label Compress. Show all posts

Monday, March 6, 2017

Illegal mix of collations for operation 'UNION'

I don't know for how many reason this error occurred but I found it when going to create a view using two tables using UNION. 

There are different ways to solve this problem. You can solve it by compress the value and then decompress as UNCOMPRESS(COMPRESS(x.name)) AS name.

You have another way to fix this problem. You can use first hex the value and then unhex as UNHEX(HEX(x.name)) AS name.

And finally, to fix this, you need to replace some column references in the SELECT list (in one or more of the queries) with an expression, something like CONVERT(name USING UTF8) AS name.

Some more convert functions are listed below:
CONVERT('2014-02-28', DATE)
CONVERT('2014-02-28 08:14:57', DATETIME)
CONVERT('08:14:57', TIME)
CONVERT(125, CHAR)
CONVERT(4-6, SIGNED)
CONVERT(4-6, UNSIGNED)
CONVERT('4', BINARY)
CONVERT('Some String' USING UTF8)
CONVERT('Some String' USING ASCII)
CONVERT('Some String' USING LATIN1)
CONVERT(x.price, DECIMAL)
CONVERT(x.price, DECIMAL(10,2))

Tuesday, June 28, 2016

Java code to compress & decompress of string using deflater & inflater

Code snippet for compress & decompress of string...

package com.pritom.kumar;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.ByteArrayOutputStream;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

/**
 * Created by pritom on 27/06/2016.
 */
public class CompressString {
    public static final String encoding = "UTF-8";

    public static void main (String[] args) throws Exception {
        String s = "123456789 a s d f g h j k l ; ' q w e r t y u i o p [ ] \\ 1 2 3 4 5 6 7 8 9 0 - = as df gh jk l; zx cv bn m, ./ qw y ui op []" +
                "qwe rty uio p[] asd fgh jkl zxc vbn m,. 123 456 789 1234 5678 90-= qwer tyui op[] asdf ghjk l;zx cvbn m,./" +
                "12345 67890 qwert yuiop asdfg hjklz xcvbn m,khg er xcgvdst 453 gd fyrt634 5 dg e653 545u7r ydf dgfsd fsart sdgdsg" +
                "sd fsdjf;sjduw[r sldfjsl;djrf2345203v sdfs'wse[rftu 2[3r50 j;lgjks'drf 2354-0u sadfas';dfmka][ ert02u9wsd's;lad fa'sdfjasdgf" +
                "4 wr[w09u jsd' af32 'QWRJE ajfsdj :ejwkiiuUUSJ W[9230 U'SFJKS JFJSF 'ASDP;GF SDFAW435]2 JKSD';GDALSJ GF'SDAGJFMA'L 2UWQ 'PS;DGJM" +
                "ju=[0a9 u0wr w435r]2345ru]2tu  s'adgjfa'sgf2uq]34u]23'g jfq4323o4 23w 52]5-2t4ewsjtrg'qwpe[fa7 2   03r/ dshgf" +
                "djl3r4 sjdf ;wq3r q3ra[d0aw'tj3 wr[w09u asd fgh zxc er gd swser9[0345r]2 3dfjsdfsd ]23-qg';gvas 'dgawejrt ]23trwasdg;mas'fgsnkd gasf " +
                "[w90rtusdmgkf zxc sadfs fa fsart sdgdsg wr JFJSF GF fgh xcgvdst dfmka SDAGJFMA 2345ru wse[rftu 2[3r50 j;l af32 sdfjasdgf jks'drf 2354-" +
                "djrf2345203v KSD';GDALSJ GF'";

        String compressed = compress(s);
        Integer savedLength = s.length() - compressed.length();
        Double saveRatio = (new Double(savedLength) * 100) / s.length();
        String ratioString = saveRatio.toString() + "00000000";
        ratioString = ratioString.substring(0, ratioString.indexOf(".") + 4);
        println("Original_String_Length=" + s.length());
        println("Compressed_String_Length=" + compressed.length() + ", Compression_Ratio=" + ratioString + "%");

        String decompressed = decompress(decodeBase64(compressed));
        println("Decompressed_String_Length=" + decompressed.length() + " == Original_String_Length (" + s.length() + ")");
        println("Original_String == Decompressed_String=" + (s.equals(decompressed) ? "True" : "False"));
    }

    public static String compress(String str) throws Exception {
        return compress(str.getBytes(encoding));
    }

    public static String compress(byte[] bytes) throws Exception {
        Deflater deflater = new Deflater();
        deflater.setInput(bytes);
        deflater.finish();
        ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length);
        byte[] buffer = new byte[1024];
        while(!deflater.finished()) {
            int count = deflater.deflate(buffer);
            bos.write(buffer, 0, count);
        }
        bos.close();
        byte[] output = bos.toByteArray();
        return encodeBase64(output);
    }

    public static String decompress(byte[] bytes) throws Exception {
        Inflater inflater = new Inflater();
        inflater.setInput(bytes);
        ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length);
        byte[] buffer = new byte[1024];
        while (!inflater.finished()) {
            int count = inflater.inflate(buffer);
            bos.write(buffer, 0, count);
        }
        bos.close();
        byte[] output = bos.toByteArray();
        return new String(output);
    }

    public static String encodeBase64(byte[] bytes) throws Exception {
        BASE64Encoder base64Encoder = new BASE64Encoder();
        return base64Encoder.encodeBuffer(bytes).replace("\r\n", "").replace("\n", "");
    }

    public static byte[] decodeBase64(String str) throws Exception {
        BASE64Decoder base64Decoder = new BASE64Decoder();
        return base64Decoder.decodeBuffer(str);
    }

    public static void println(Object o) {
        System.out.println("" + o);
    }
}

Output would be like this...

Original_String_Length=1000
Compressed_String_Length=824, Compression_Ratio=17.600%
Decompressed_String_Length=1000 == Original_String_Length (1000)
Original_String == Decompressed_String=True

** More big string more compression ratio
** More similar words more compression ratio

Ideone link