Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ public void testLiteralOnlyTransformBrokerRequestFromSQL() {
.compileToPinotQuery("SELECT ago('PT1H'), fromDateTime('2020-01-01 UTC', 'yyyy-MM-dd z') FROM myTable")));
Assert.assertFalse(BaseBrokerRequestHandler
.isLiteralOnlyQuery(CalciteSqlParser.compileToPinotQuery("SELECT count(*) from foo where bar > ago('PT1H')")));
Assert.assertTrue(BaseBrokerRequestHandler.isLiteralOnlyQuery(CalciteSqlParser
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we not use full URLs as inputs to encodeUrl / decodeUrl functions in tests ? From user perspective, they will use the full URL right ?

SELECT encodeUrl(<myURL>) FROM FOO .....

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jasperjiaguo - please add test cases with real URLs as well

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I merged the PR too soon. @jasperjiaguo Can you please address Sidd's comments in a separate PR?

.compileToPinotQuery("SELECT encodeUrl('key1=value 1&key2=value@!$2&key3=value%3'),"
+ " decodeUrl('key1%3Dvalue+1%26key2%3Dvalue%40%21%242%26key3%3Dvalue%253') FROM myTable")));
Assert.assertFalse(BaseBrokerRequestHandler.isLiteralOnlyQuery(CalciteSqlParser
.compileToPinotQuery("SELECT count(*) from foo "
+ "where bar = encodeUrl('key1=value 1&key2=value@!$2&key3=value%3')")));
Assert.assertFalse(BaseBrokerRequestHandler.isLiteralOnlyQuery(CalciteSqlParser
.compileToPinotQuery("SELECT count(*) from foo "
+ "where bar = decodeUrl('key1%3Dvalue+1%26key2%3Dvalue%40%21%242%26key3%3Dvalue%253')")));
}

@Test
Expand All @@ -102,6 +111,9 @@ public void testLiteralOnlyWithAsBrokerRequestFromSQL() {
"SELECT now() AS currentTs, fromDateTime('2020-01-01 UTC', 'yyyy-MM-dd z') AS firstDayOf2020")));
Assert.assertTrue(BaseBrokerRequestHandler.isLiteralOnlyQuery(CalciteSqlParser.compileToPinotQuery(
"SELECT ago('PT1H') AS currentTs, fromDateTime('2020-01-01 UTC', 'yyyy-MM-dd z') AS firstDayOf2020")));
Assert.assertTrue(BaseBrokerRequestHandler.isLiteralOnlyQuery(CalciteSqlParser.compileToPinotQuery(
"SELECT encodeUrl('key1=value 1&key2=value@!$2&key3=value%3') AS encoded, "
+ "decodeUrl('key1%3Dvalue+1%26key2%3Dvalue%40%21%242%26key3%3Dvalue%253') AS decoded")));
}

@Test
Expand Down Expand Up @@ -178,6 +190,26 @@ public void testBrokerRequestHandlerWithAsFunction()
.assertTrue(Long.parseLong(brokerResponse.getResultTable().getRows().get(0)[0].toString()) <= oneHourAgoTsMax);
Assert.assertEquals(brokerResponse.getResultTable().getRows().get(0)[1], 1577836800000L);
Assert.assertEquals(brokerResponse.getTotalDocs(), 0);

request = new ObjectMapper().readTree(
"{\"sql\":\"SELECT encodeUrl('key1=value 1&key2=value@!$2&key3=value%3') AS encoded, "
+ "decodeUrl('key1%3Dvalue+1%26key2%3Dvalue%40%21%242%26key3%3Dvalue%253') AS decoded\"}");
requestStats = new RequestStatistics();
brokerResponse = requestHandler.handleRequest(request, null, requestStats);
System.out.println(brokerResponse.getResultTable());
Assert.assertEquals(brokerResponse.getResultTable().getDataSchema().getColumnName(0), "encoded");
Assert.assertEquals(brokerResponse.getResultTable().getDataSchema().getColumnDataType(0),
DataSchema.ColumnDataType.STRING);
Assert.assertEquals(brokerResponse.getResultTable().getDataSchema().getColumnName(1), "decoded");
Assert.assertEquals(brokerResponse.getResultTable().getDataSchema().getColumnDataType(1),
DataSchema.ColumnDataType.STRING);
Assert.assertEquals(brokerResponse.getResultTable().getRows().size(), 1);
Assert.assertEquals(brokerResponse.getResultTable().getRows().get(0).length, 2);
Assert.assertEquals(brokerResponse.getResultTable().getRows().get(0)[0].toString(),
"key1%3Dvalue+1%26key2%3Dvalue%40%21%242%26key3%3Dvalue%253");
Assert.assertEquals(brokerResponse.getResultTable().getRows().get(0)[1].toString(),
"key1=value 1&key2=value@!$2&key3=value%3");
Assert.assertEquals(brokerResponse.getTotalDocs(), 0);
}

/** Tests for EXPLAIN PLAN for literal only queries. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/
package org.apache.pinot.common.function.scalar;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.Normalizer;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -443,4 +446,28 @@ public static boolean contains(String input, String substring) {
public static int strcmp(String input1, String input2) {
return input1.compareTo(input2);
}

/**
*
* @param input plaintext string
* @return url encoded string
* @throws UnsupportedEncodingException
*/
@ScalarFunction
public static String encodeUrl(String input)
throws UnsupportedEncodingException {
return URLEncoder.encode(input, StandardCharsets.UTF_8.toString());
}

/**
*
* @param input url encoded string
* @return plaintext string
* @throws UnsupportedEncodingException
*/
@ScalarFunction
public static String decodeUrl(String input)
throws UnsupportedEncodingException {
return URLDecoder.decode(input, StandardCharsets.UTF_8.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,31 @@ public void testBrokerConverterWithLiteral() {
pinotQuery = CalciteSqlParser.compileToPinotQuery("SELECT count(*) from mytable where bar > ago('PT1H')");
literal = pinotQuery.getSelectList().get(0).getLiteral();
Assert.assertNull(literal);

pinotQuery = CalciteSqlParser
.compileToPinotQuery("select encodeUrl('key1=value 1&key2=value@!$2&key3=value%3'), "
+ "decodeUrl('key1%3Dvalue+1%26key2%3Dvalue%40%21%242%26key3%3Dvalue%253') from mytable");
Literal literal1 = pinotQuery.getSelectList().get(0).getLiteral();
Literal literal2 = pinotQuery.getSelectList().get(1).getLiteral();
Assert.assertNotNull(literal1);
Assert.assertNotNull(literal2);
converter = new PinotQuery2BrokerRequestConverter();
tempBrokerRequest = converter.convert(pinotQuery);
Assert.assertEquals(tempBrokerRequest.getQuerySource().getTableName(), "mytable");
Assert.assertEquals(tempBrokerRequest.getSelections().getSelectionColumns().get(0),
String.format("'%s'", literal1.getFieldValue().toString()));
Assert.assertEquals(tempBrokerRequest.getSelections().getSelectionColumns().get(1),
String.format("'%s'", literal2.getFieldValue().toString()));

pinotQuery = CalciteSqlParser.compileToPinotQuery("SELECT count(*) from mytable "
+ "where bar = encodeUrl('key1=value 1&key2=value@!$2&key3=value%3')");
literal = pinotQuery.getSelectList().get(0).getLiteral();
Assert.assertNull(literal);

pinotQuery = CalciteSqlParser.compileToPinotQuery("SELECT count(*) from mytable "
+ "where bar = decodeUrl('key1%3Dvalue+1%26key2%3Dvalue%40%21%242%26key3%3Dvalue%253')");
literal = pinotQuery.getSelectList().get(0).getLiteral();
Assert.assertNull(literal);
}

@Test
Expand Down Expand Up @@ -1855,6 +1880,23 @@ public void testCompilationInvokedFunction() {
upperBound = System.currentTimeMillis() - ONE_HOUR_IN_MS;
Assert.assertTrue(nowTs >= lowerBound);
Assert.assertTrue(nowTs <= upperBound);

query = "select encodeUrl('key1=value 1&key2=value@!$2&key3=value%3'), "
+ "decodeUrl('key1%3Dvalue+1%26key2%3Dvalue%40%21%242%26key3%3Dvalue%253') from mytable";
pinotQuery = CalciteSqlParser.compileToPinotQuery(query);
String encoded = pinotQuery.getSelectList().get(0).getLiteral().getStringValue();
String decoded = pinotQuery.getSelectList().get(1).getLiteral().getStringValue();
Assert.assertEquals(encoded, "key1%3Dvalue+1%26key2%3Dvalue%40%21%242%26key3%3Dvalue%253");
Assert.assertEquals(decoded, "key1=value 1&key2=value@!$2&key3=value%3");

query = "select a from mytable where foo=encodeUrl('key1=value 1&key2=value@!$2&key3=value%3') and"
+ " bar=decodeUrl('key1%3Dvalue+1%26key2%3Dvalue%40%21%242%26key3%3Dvalue%253')";
pinotQuery = CalciteSqlParser.compileToPinotQuery(query);
Function and = pinotQuery.getFilterExpression().getFunctionCall();
encoded = and.getOperands().get(0).getFunctionCall().getOperands().get(1).getLiteral().getStringValue();
decoded = and.getOperands().get(1).getFunctionCall().getOperands().get(1).getLiteral().getStringValue();
Assert.assertEquals(encoded, "key1%3Dvalue+1%26key2%3Dvalue%40%21%242%26key3%3Dvalue%253");
Assert.assertEquals(decoded, "key1=value 1&key2=value@!$2&key3=value%3");
}

@Test
Expand Down Expand Up @@ -1926,6 +1968,25 @@ public void testCompileTimeExpression() {
Assert.assertEquals(expression.getFunctionCall().getOperands().get(0).getIdentifier().getName(),
"millisSinceEpoch");

expression = CalciteSqlParser.compileToExpression("encodeUrl('key1=value 1&key2=value@!$2&key3=value%3')");
Assert.assertNotNull(expression.getFunctionCall());
pinotQuery.setFilterExpression(expression);
pinotQuery = compileTimeFunctionsInvoker.rewrite(pinotQuery);
expression = pinotQuery.getFilterExpression();
Assert.assertNotNull(expression.getLiteral());
Assert.assertEquals(expression.getLiteral().getFieldValue(),
"key1%3Dvalue+1%26key2%3Dvalue%40%21%242%26key3%3Dvalue%253");

expression = CalciteSqlParser
.compileToExpression("decodeUrl('key1%3Dvalue+1%26key2%3Dvalue%40%21%242%26key3%3Dvalue%253')");
Assert.assertNotNull(expression.getFunctionCall());
pinotQuery.setFilterExpression(expression);
pinotQuery = compileTimeFunctionsInvoker.rewrite(pinotQuery);
expression = pinotQuery.getFilterExpression();
Assert.assertNotNull(expression.getLiteral());
Assert.assertEquals(expression.getLiteral().getFieldValue(),
"key1=value 1&key2=value@!$2&key3=value%3");

expression = CalciteSqlParser.compileToExpression("reverse(playerName)");
Assert.assertNotNull(expression.getFunctionCall());
pinotQuery.setFilterExpression(expression);
Expand Down
Loading