forked from prestodb/presto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve map(varchar, json) canonicalization bug
The map function will not sort a json object by its keys, despite the json_parse function sorting the same input. If implemented, this will sort json objects. Resolves prestodb#24207
- Loading branch information
Showing
2 changed files
with
78 additions
and
4 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
59 changes: 59 additions & 0 deletions
59
presto-main/src/test/java/com/facebook/presto/operator/TestMapVarcharJsonOperator.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,59 @@ | ||
package com.facebook.presto.operator; | ||
|
||
import com.facebook.presto.Session; | ||
import com.facebook.presto.testing.LocalQueryRunner; | ||
import com.facebook.presto.testing.MaterializedResult; | ||
import com.facebook.presto.testing.MaterializedRow; | ||
import com.facebook.presto.testing.QueryRunner; | ||
import com.google.common.collect.Iterables; | ||
import org.intellij.lang.annotations.Language; | ||
import org.testng.annotations.AfterClass; | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.Test; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static com.facebook.presto.testing.TestingSession.testSessionBuilder; | ||
import static java.lang.String.format; | ||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.assertTrue; | ||
|
||
public class TestMapVarcharJsonOperator | ||
{ | ||
private QueryRunner queryRunner; | ||
@BeforeClass | ||
public void setUp() | ||
{ | ||
Session session = testSessionBuilder().build(); | ||
this.queryRunner = new LocalQueryRunner(session); | ||
} | ||
|
||
@AfterClass(alwaysRun = true) | ||
public void tearDown() | ||
{ | ||
queryRunner.close(); | ||
queryRunner = null; | ||
} | ||
|
||
@Test | ||
public void testFunction() | ||
{ | ||
Map<String, String> map = new HashMap<>(); | ||
map.put("m", "[\"rn\",\"w\",\"a\"]"); | ||
assertThatQueryReturnsValue("SELECT TRY(CAST(json_parse(c0) AS map(varchar, json))) from (values ('{\"m\": [\"rn\", \"w\", \"a\"]}')) t(c0)", map); | ||
map.put("m", "{\"pl\":\"4\",\"rn\":\"w\"}"); | ||
assertThatQueryReturnsValue("SELECT TRY(CAST(json_parse(c0) AS map(varchar, json))) from (values ('{\"m\": {\"rn\": \"w\", \"pl\": \"4\"}}')) t(c0)", map); | ||
} | ||
|
||
private void assertThatQueryReturnsValue(@Language("SQL") String sql, Object expected) | ||
{ | ||
MaterializedResult rows = queryRunner.execute(sql); | ||
MaterializedRow materializedRow = Iterables.getOnlyElement(rows); | ||
int fieldCount = materializedRow.getFieldCount(); | ||
assertTrue(fieldCount == 1, format("Expected only one column, but got '%d'", fieldCount)); | ||
Object value = materializedRow.getField(0); | ||
assertEquals(value, expected); | ||
assertTrue(Iterables.getOnlyElement(rows).getFieldCount() == 1); | ||
} | ||
} |