Skip to content

Commit

Permalink
Escape single quote in BigQuery string condition
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushbilala authored and ebyhr committed May 17, 2021
1 parent 3b5267d commit c540a8e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
Expand Up @@ -179,7 +179,8 @@ private static ZonedDateTime toZonedDateTime(long millisUtc, ZoneId zoneId)
static String stringToStringConverter(Object value)
{
Slice slice = (Slice) value;
return quote(slice.toStringUtf8());
// TODO (https://github.com/trinodb/trino/issues/7900) Add support for all String and Bytes literals
return quote(slice.toStringUtf8().replace("'", "\\'"));
}

static String numericToStringConverter(Object value)
Expand Down
Expand Up @@ -244,6 +244,21 @@ public void testSelectFromYearlyPartitionedTable()
assertEquals((long) actualValues.getOnlyValue(), 1L);
}

@Test(description = "regression test for https://github.com/trinodb/trino/issues/7784")
public void testSelectWithSingleQuoteInWhereClause()
{
String tableName = "test.select_with_single_quote";

onBigQuery("DROP TABLE IF EXISTS " + tableName);
onBigQuery("CREATE TABLE " + tableName + "(col INT64, val STRING)");
onBigQuery("INSERT INTO " + tableName + " VALUES (1,'escape\\'single quote')");

MaterializedResult actualValues = computeActual("SELECT val FROM " + tableName + " WHERE val = 'escape''single quote'");

assertEquals(actualValues.getRowCount(), 1);
assertEquals(actualValues.getOnlyValue(), "escape'single quote");
}

@Test(description = "regression test for https://github.com/trinodb/trino/issues/5618")
public void testPredicatePushdownPrunnedColumns()
{
Expand Down
Expand Up @@ -54,6 +54,10 @@ public void testStringToStringConverter()
assertThat(BigQueryType.stringToStringConverter(
utf8Slice("test")))
.isEqualTo("'test'");

assertThat(BigQueryType.stringToStringConverter(
utf8Slice("test's test")))
.isEqualTo("'test\\'s test'");
}

@Test
Expand Down

0 comments on commit c540a8e

Please sign in to comment.