Skip to content

Commit

Permalink
Format cassandra collection types as JSON
Browse files Browse the repository at this point in the history
By formatting collection types as JSON, we can use Presto's JSON
functions to extract values.
  • Loading branch information
brndnmtthws authored and dain committed Apr 29, 2014
1 parent 7c92286 commit 48b97df
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 5 deletions.
Expand Up @@ -22,6 +22,7 @@
import com.facebook.presto.spi.type.DoubleType;
import com.facebook.presto.spi.type.Type;
import com.facebook.presto.spi.type.VarcharType;
import com.google.common.annotations.VisibleForTesting;

import java.math.BigDecimal;
import java.math.BigInteger;
Expand Down Expand Up @@ -225,8 +226,7 @@ private static String buildListValue(Row row, int i, CassandraType elemType)
return buildArrayValue(row.getList(i, elemType.javaType), elemType);
}

private static String buildMapValue(Row row, int i, CassandraType keyType,
CassandraType valueType)
private static String buildMapValue(Row row, int i, CassandraType keyType, CassandraType valueType)
{
StringBuilder sb = new StringBuilder();
sb.append("{");
Expand All @@ -242,7 +242,8 @@ private static String buildMapValue(Row row, int i, CassandraType keyType,
return sb.toString();
}

private static String buildArrayValue(Collection<?> collection, CassandraType elemType)
@VisibleForTesting
static String buildArrayValue(Collection<?> collection, CassandraType elemType)
{
StringBuilder sb = new StringBuilder();
sb.append("[");
Expand Down Expand Up @@ -319,11 +320,11 @@ private static String objectToString(Object object, CassandraType elemType)
case TIMESTAMP:
case INET:
case VARINT:
return CassandraCqlUtils.quoteStringLiteral(object.toString());
return CassandraCqlUtils.quoteStringLiteralForJson(object.toString());

case BLOB:
case CUSTOM:
return CassandraCqlUtils.quoteStringLiteral(Bytes.toHexString((ByteBuffer) object));
return CassandraCqlUtils.quoteStringLiteralForJson(Bytes.toHexString((ByteBuffer) object));

case INT:
case BIGINT:
Expand Down
Expand Up @@ -19,6 +19,7 @@
import com.facebook.presto.cassandra.CassandraColumnHandle;
import com.facebook.presto.cassandra.CassandraTableHandle;
import com.facebook.presto.spi.ConnectorColumnHandle;
import com.fasterxml.jackson.core.io.JsonStringEncoder;

import java.util.Arrays;
import java.util.HashSet;
Expand Down Expand Up @@ -87,6 +88,11 @@ public static String quoteStringLiteral(String string)
return "'" + string.replace("'", "''") + "'";
}

public static String quoteStringLiteralForJson(String string)
{
return '"' + new String(JsonStringEncoder.getInstance().quoteAsUTF8(string)) + '"';
}

public static void appendSelectColumns(StringBuilder stringBuilder, List<? extends ConnectorColumnHandle> columns)
{
appendSelectColumns(stringBuilder, columns, true);
Expand Down
@@ -0,0 +1,58 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.cassandra;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import org.testng.annotations.Test;

import java.io.IOException;

import static org.testng.Assert.assertTrue;

public class TestCassandraType
{
@Test
public void testJsonMapEncoding()
{
assertTrue(isValidJson(CassandraType.buildArrayValue(Lists.newArrayList("one", "two", "three\""), CassandraType.VARCHAR)));
assertTrue(isValidJson(CassandraType.buildArrayValue(Lists.newArrayList(1, 2, 3), CassandraType.INT)));
assertTrue(isValidJson(CassandraType.buildArrayValue(Lists.newArrayList(100000L, 200000000L, 3000000000L), CassandraType.BIGINT)));
assertTrue(isValidJson(CassandraType.buildArrayValue(Lists.newArrayList(1.0, 2.0, 3.0), CassandraType.DOUBLE)));
}

private static void continueWhileNotNull(JsonParser parser, JsonToken token)
throws IOException
{
if (token != null) {
continueWhileNotNull(parser, parser.nextToken());
}
}

private static boolean isValidJson(String json)
{
boolean valid = false;
try {
JsonParser parser = new ObjectMapper().getFactory()
.createJsonParser(json);
continueWhileNotNull(parser, parser.nextToken());
valid = true;
}
catch (IOException ignored) {
}
return valid;
}
}
Expand Up @@ -21,6 +21,7 @@
import java.util.List;

import static com.facebook.presto.cassandra.util.CassandraCqlUtils.quoteStringLiteral;
import static com.facebook.presto.cassandra.util.CassandraCqlUtils.quoteStringLiteralForJson;
import static com.facebook.presto.cassandra.util.CassandraCqlUtils.validColumnName;
import static com.facebook.presto.cassandra.util.CassandraCqlUtils.validSchemaName;
import static com.facebook.presto.cassandra.util.CassandraCqlUtils.validTableName;
Expand Down Expand Up @@ -59,6 +60,14 @@ public void testQuote()
assertEquals("'Presto''s'", quoteStringLiteral("Presto's"));
}

@Test
public void testQuoteJson()
{
assertEquals("\"foo\"", quoteStringLiteralForJson("foo"));
assertEquals("\"Presto's\"", quoteStringLiteralForJson("Presto's"));
assertEquals("\"xx\\\"xx\"", quoteStringLiteralForJson("xx\"xx"));
}

@Test
public void testAppendSelectColumns()
{
Expand Down

0 comments on commit 48b97df

Please sign in to comment.