Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Format cassandra collection types as JSON
By formatting collection types as JSON, we can use Presto's JSON functions to extract values.
- Loading branch information
1 parent
7c92286
commit 48b97df
Showing
4 changed files
with
79 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
presto-cassandra/src/test/java/com/facebook/presto/cassandra/TestCassandraType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters