Skip to content

Commit

Permalink
Added tests for var and const functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
bziobrowski committed Jan 29, 2025
1 parent 3342760 commit 577c36b
Show file tree
Hide file tree
Showing 6 changed files with 445 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.pinot.common.function.scalar.regexp;

import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;


public class RegexpExtractConstFunctionsTest {

@Test
public void test() {
RegexpExtractConstFunctions f = new RegexpExtractConstFunctions();

assertEquals(f.regexpExtractConst("val abe eee", "(a[bcd]e)"), "abe");
assertEquals(f.regexpExtractConst("val ade eee", "(a[bcd]e)"), "ade");
assertEquals(f.regexpExtractConst("val age eee", "(a[bcd]e)"), "");
// f caches first pattern and ignores second argument
assertEquals(f.regexpExtractConst("val abe ace", "(a[bcd]e) (a[bcd]e)", 2), "");

f = new RegexpExtractConstFunctions();
assertEquals(f.regexpExtractConst("val abe ace", "(a[bcd]e) (a[bcd]e)", 2), "ace");

f = new RegexpExtractConstFunctions();
assertEquals(f.regexpExtractConst("abe ace ade", "(a[bcd]e) (a[bcd]e) (a[bcd]e)", 3), "ade");

f = new RegexpExtractConstFunctions();
assertEquals(f.regexpExtractConst("abe ace ade", "(a[bcd]e)", 5, "wrong"), "wrong");
assertEquals(f.regexpExtractConst("aa bb cc", "(a[bcd]e)", 1, "wrong"), "wrong");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.pinot.common.function.scalar.regexp;

import org.testng.annotations.Test;

import static org.apache.pinot.common.function.scalar.regexp.RegexpExtractVarFunctions.regexpExtract;
import static org.testng.Assert.assertEquals;


public class RegexpExtractVarFunctionsTest {

@Test
public void test() {
assertEquals(regexpExtract("val abe eee", "(a[bcd]e)"), "abe");
assertEquals(regexpExtract("val ade eee", "(a[bcd]e)"), "ade");
assertEquals(regexpExtract("val age eee", "(a[bcd]e)"), "");

assertEquals(regexpExtract("val abe ace", "(a[bcd]e) (a[bcd]e)", 2), "ace");
assertEquals(regexpExtract("abe ace ade", "(a[bcd]e) (a[bcd]e) (a[bcd]e)", 3), "ade");

assertEquals(regexpExtract("abe ace ade", "(a[bcd]e)", 5, "wrong"), "wrong");
assertEquals(regexpExtract("aa bb cc", "(a[bcd]e)", 1, "wrong"), "wrong");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.pinot.common.function.scalar.regexp;

import org.testng.annotations.Test;

import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;


public class RegexpLikeConstFunctionsTest {

@Test
public void testLike() {
RegexpLikeConstFunctions f = new RegexpLikeConstFunctions();

assertTrue(f.likeConst("ab", "%ab%"));
assertTrue(f.likeConst("aaba", "%ab%"));
assertTrue(f.likeConst("$ab$", "%ab%"));

assertFalse(f.likeConst("", "%ab%"));
assertFalse(f.likeConst("_", "%ab%"));
assertFalse(f.likeConst("a", "%ab%"));
assertFalse(f.likeConst("b", "%ab%"));

//returns true because function matches against first pattern
assertTrue(f.likeConst("aab", "abb"));
}

@Test
public void testRegexpLike() {
RegexpLikeConstFunctions f = new RegexpLikeConstFunctions();

assertTrue(f.regexpLikeConst("ab", ".*ab.*"));
assertTrue(f.regexpLikeConst("aaba", ".*ab.*"));
assertTrue(f.regexpLikeConst("$ab$", ".*ab.*"));

assertFalse(f.regexpLikeConst("", ".*ab.*"));
assertFalse(f.regexpLikeConst("_", ".*ab.*"));
assertFalse(f.regexpLikeConst("a", ".*ab.*"));
assertFalse(f.regexpLikeConst("b", ".*ab.*"));

//returns true because function matches against first pattern
assertTrue(f.regexpLikeConst("aab", "ab"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.pinot.common.function.scalar.regexp;

import org.testng.annotations.Test;

import static org.apache.pinot.common.function.scalar.regexp.RegexpLikeVarFunctions.like;
import static org.apache.pinot.common.function.scalar.regexp.RegexpLikeVarFunctions.regexpLike;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;


public class RegexpLikeVarFunctionsTest {

@Test
public void testLike() {
assertTrue(like("ab", "%ab%"));
assertTrue(like("aaba", "%ab%"));
assertTrue(like("$ab$", "%ab%"));

assertFalse(like("", "%ab%"));
assertFalse(like("_", "%ab%"));
assertFalse(like("a", "%ab%"));
assertFalse(like("b", "%ab%"));

assertFalse(like("aab", "ab"));
}

@Test
public void testRegexpLike() {
assertTrue(regexpLike("ab", ".*ab.*"));
assertTrue(regexpLike("aaba", ".*ab.*"));
assertTrue(regexpLike("$ab$", ".*ab.*"));

assertFalse(regexpLike("", ".*ab.*"));
assertFalse(regexpLike("_", ".*ab.*"));
assertFalse(regexpLike("a", ".*ab.*"));
assertFalse(regexpLike("b", ".*ab.*"));

//returns true because function matches against first pattern
assertFalse(regexpLike("aab", "abb"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,123 @@ public void testRegexpReplace()
assertEquals(count1, count2);
}

@Test
public void testRegexpReplaceConst()
throws Exception {
// Correctness tests of regexpReplaceConst.

// Test replace all.
String sqlQuery = "SELECT regexpReplaceConst('CA', 'C', 'TEST')";
JsonNode response = postQuery(sqlQuery);
String result = response.get("resultTable").get("rows").get(0).get(0).asText();
assertEquals(result, "TESTA");

sqlQuery = "SELECT regexpReplaceConst('foobarbaz', 'b', 'X')";
response = postQuery(sqlQuery);
result = response.get("resultTable").get("rows").get(0).get(0).asText();
assertEquals(result, "fooXarXaz");

sqlQuery = "SELECT regexpReplaceConst('foobarbaz', 'b', 'XY')";
response = postQuery(sqlQuery);
result = response.get("resultTable").get("rows").get(0).get(0).asText();
assertEquals(result, "fooXYarXYaz");

sqlQuery = "SELECT regexpReplaceConst('Argentina', '(.)', '$1 ')";
response = postQuery(sqlQuery);
result = response.get("resultTable").get("rows").get(0).get(0).asText();
assertEquals(result, "A r g e n t i n a ");

sqlQuery = "SELECT regexpReplaceConst('Pinot is blazing fast', '( ){2,}', ' ')";
response = postQuery(sqlQuery);
result = response.get("resultTable").get("rows").get(0).get(0).asText();
assertEquals(result, "Pinot is blazing fast");

sqlQuery = "SELECT regexpReplaceConst('healthy, wealthy, and wise','\\w+thy', 'something')";
response = postQuery(sqlQuery);
result = response.get("resultTable").get("rows").get(0).get(0).asText();
assertEquals(result, "something, something, and wise");

sqlQuery = "SELECT regexpReplaceConst('11234567898','(\\d)(\\d{3})(\\d{3})(\\d{4})', '$1-($2) $3-$4')";
response = postQuery(sqlQuery);
result = response.get("resultTable").get("rows").get(0).get(0).asText();
assertEquals(result, "1-(123) 456-7898");

// Test replace starting at index.

sqlQuery = "SELECT regexpReplaceConst('healthy, wealthy, stealthy and wise','\\w+thy', 'something', 4)";
response = postQuery(sqlQuery);
result = response.get("resultTable").get("rows").get(0).get(0).asText();
assertEquals(result, "healthy, something, something and wise");

sqlQuery = "SELECT regexpReplaceConst('healthy, wealthy, stealthy and wise','\\w+thy', 'something', 1)";
response = postQuery(sqlQuery);
result = response.get("resultTable").get("rows").get(0).get(0).asText();
assertEquals(result, "hsomething, something, something and wise");

// Test occurence
sqlQuery = "SELECT regexpReplaceConst('healthy, wealthy, stealthy and wise','\\w+thy', 'something', 0, 2)";
response = postQuery(sqlQuery);
result = response.get("resultTable").get("rows").get(0).get(0).asText();
assertEquals(result, "healthy, wealthy, something and wise");

sqlQuery = "SELECT regexpReplaceConst('healthy, wealthy, stealthy and wise','\\w+thy', 'something', 0, 0)";
response = postQuery(sqlQuery);
result = response.get("resultTable").get("rows").get(0).get(0).asText();
assertEquals(result, "something, wealthy, stealthy and wise");

// Test flags
sqlQuery = "SELECT regexpReplaceConst('healthy, wealthy, stealthy and wise','\\w+tHy', 'something', 0, 0, 'i')";
response = postQuery(sqlQuery);
result = response.get("resultTable").get("rows").get(0).get(0).asText();
assertEquals(result, "something, wealthy, stealthy and wise");

// Negative test. Pattern match not found.
sqlQuery = "SELECT regexpReplaceConst('healthy, wealthy, stealthy and wise','\\w+tHy', 'something')";
response = postQuery(sqlQuery);
result = response.get("resultTable").get("rows").get(0).get(0).asText();
assertEquals(result, "healthy, wealthy, stealthy and wise");

// Negative test. Pattern match not found.
sqlQuery = "SELECT regexpReplaceConst('healthy, wealthy, stealthy and wise','\\w+tHy', 'something', 3, 21, 'i')";
response = postQuery(sqlQuery);
result = response.get("resultTable").get("rows").get(0).get(0).asText();
assertEquals(result, "healthy, wealthy, stealthy and wise");

// Negative test - incorrect flag
sqlQuery = "SELECT regexpReplaceConst('healthy, wealthy, stealthy and wise','\\w+tHy', 'something', 3, 12, 'xyz')";
response = postQuery(sqlQuery);
result = response.get("resultTable").get("rows").get(0).get(0).asText();
assertEquals(result, "healthy, wealthy, stealthy and wise");

// Test in select clause with column values
sqlQuery = "SELECT regexpReplaceConst(DestCityName, ' ', '', 0, -1, 'i') from mytable where OriginState = 'CA'";
response = postQuery(sqlQuery);
JsonNode rows = response.get("resultTable").get("rows");
for (int i = 0; i < rows.size(); i++) {
JsonNode row = rows.get(i);
assertFalse(row.get(0).asText().contains(" "));
}

// Test in where clause
sqlQuery = "SELECT count(*) from mytable where regexpReplaceConst(OriginState, '[VC]A', 'TEST') = 'TEST'";
response = postQuery(sqlQuery);
int count1 = response.get("resultTable").get("rows").get(0).get(0).asInt();
sqlQuery = "SELECT count(*) from mytable where OriginState='CA' or OriginState='VA'";
response = postQuery(sqlQuery);
int count2 = response.get("resultTable").get("rows").get(0).get(0).asInt();
assertEquals(count1, count2);

// Test nested transform
sqlQuery =
"SELECT count(*) from mytable where contains(regexpReplaceConst(OriginState, '(C)(A)', '$1TEST$2'), 'CTESTA')";
response = postQuery(sqlQuery);
count1 = response.get("resultTable").get("rows").get(0).get(0).asInt();
sqlQuery = "SELECT count(*) from mytable where OriginState='CA'";
response = postQuery(sqlQuery);
count2 = response.get("resultTable").get("rows").get(0).get(0).asInt();
assertEquals(count1, count2);
}

@Test
public void testUrlFunc()
throws Exception {
Expand Down
Loading

0 comments on commit 577c36b

Please sign in to comment.