Skip to content
Open
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 @@ -129,6 +129,9 @@ public HiveSqlDialect(Context context) {
case TRIM:
RelToSqlConverterUtil.unparseHiveTrim(writer, call, leftPrec, rightPrec);
break;
case RLIKE:
RelToSqlConverterUtil.unparseRegexp(writer, call, leftPrec, rightPrec);
break;
default:
super.unparseCall(writer, call, leftPrec, rightPrec);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -705,11 +705,10 @@ static RelDataType deriveTypeSplit(SqlOperatorBinding operatorBinding,
public static final SqlFunction REGEXP_SUBSTR = REGEXP_EXTRACT.withName("REGEXP_SUBSTR");

/** The "REGEXP(value, regexp)" function, equivalent to {@link #RLIKE}. */
@LibraryOperator(libraries = {SPARK})
@LibraryOperator(libraries = {SPARK, HIVE})
Copy link
Member

Choose a reason for hiding this comment

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

Since it's been added to libraries in this PR, Hive should also be added to SqlOperatorTest.

Copy link
Author

Choose a reason for hiding this comment

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

Since it's been added to libraries in this PR, Hive should also be added to SqlOperatorTest.

done

public static final SqlFunction REGEXP =
SqlBasicFunction.create("REGEXP", ReturnTypes.BOOLEAN_NULLABLE,
OperandTypes.STRING_STRING,
SqlFunctionCategory.STRING);
SqlBasicFunction.create("REGEXP", SqlKind.RLIKE, ReturnTypes.BOOLEAN_NULLABLE,
OperandTypes.STRING_STRING);

/** The "REGEXP_LIKE(value, regexp)" function, equivalent to {@link #RLIKE}. */
@LibraryOperator(libraries = {SPARK, MYSQL, POSTGRESQL, ORACLE})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,4 +445,19 @@ public ClickHouseSqlArrayTypeNameSpec(SqlTypeNameSpec elementTypeName,
writer.endList(frame);
}
}

/**
* Unparses REGEXP function calls by converting from function call format
* (e.g., REGEXP(column, pattern)) to infix operator format (e.g., column REGEXP pattern).
*/
public static void unparseRegexp(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) {
if ("REGEXP".equals(call.getOperator().getName())) {
final SqlWriter.Frame frame = writer.startList(SqlWriter.FrameTypeEnum.SIMPLE, "(", ")");
call.operand(0).unparse(writer, leftPrec, rightPrec);
writer.sep("REGEXP", true);
call.operand(1).unparse(writer, leftPrec, rightPrec);
writer.endList(frame);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11761,4 +11761,36 @@ public Sql schema(CalciteAssert.SchemaSpec schemaSpec) {
.ok(expected);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-7428">[CALCITE-7428]
* Support regexp function change regexp operator for Hive library</a>. */
@Test void testRegexpWithHive() {
final String query = "select \"brand_name\"\n"
+ "from \"product\" where REGEXP(\"brand_name\",'[a-zA-Z]') ";
final String expectedHive = "SELECT `brand_name`\nFROM "
+ "`foodmart`.`product`\nWHERE (`brand_name` REGEXP '[a-zA-Z]')";
sql(query).withLibrary(SqlLibrary.HIVE).withHive().ok(expectedHive);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-7428">[CALCITE-7428]
* Support regexp function change regexp operator for Hive library</a>. */
@Test void testRegexpWithHiveIsNotNull() {
final String query = "select \"brand_name\"\n"
+ "from \"product\" where REGEXP(\"brand_name\",'[a-zA-Z]') is not null ";
final String expectedHive = "SELECT `brand_name`\nFROM "
+ "`foodmart`.`product`\nWHERE (`brand_name` REGEXP '[a-zA-Z]') IS NOT NULL";
sql(query).withLibrary(SqlLibrary.HIVE).withHive().ok(expectedHive);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-7428">[CALCITE-7428]
* Support regexp function change regexp operator for Hive library</a>. */
@Test void testSelectRegexpWithHiveIsNotNull() {
final String query = "select REGEXP(\"brand_name\",'[a-zA-Z]') is not null \n"
+ "from \"product\"";
final String expectedHive = "SELECT (`brand_name` REGEXP '[a-zA-Z]') IS NOT NULL\n"
+ "FROM `foodmart`.`product`";
sql(query).withLibrary(SqlLibrary.HIVE).withHive().ok(expectedHive);
}
}
2 changes: 1 addition & 1 deletion site/_docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -3004,7 +3004,7 @@ In the following:
| b s | POW(numeric1, numeric2) | Returns *numeric1* raised to the power *numeric2*
| b c h q m o f s p r | POWER(numeric1, numeric2) | Returns *numeric1* raised to the power of *numeric2*
| p r | RANDOM() | Generates a random double between 0 and 1 inclusive
| s | REGEXP(string, regexp) | Equivalent to `string1 RLIKE string2`
| s h | REGEXP(string, regexp) | Equivalent to `string1 RLIKE string2`
| b | REGEXP_CONTAINS(string, regexp) | Returns whether *string* is a partial match for the *regexp*
| b | REGEXP_EXTRACT(string, regexp [, position [, occurrence]]) | Returns the substring in *string* that matches the *regexp*, starting search at *position* (default 1), and until locating the nth *occurrence* (default 1). Returns NULL if there is no match
| b | REGEXP_EXTRACT_ALL(string, regexp) | Returns an array of all substrings in *string* that matches the *regexp*. Returns an empty array if there is no match
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4232,6 +4232,7 @@ void checkIsNull(SqlOperatorFixture f, SqlOperator operator) {
checkRlikeFunc(f, SqlLibrary.HIVE, SqlLibraryOperators.RLIKE);
checkRlikeFunc(f, SqlLibrary.SPARK, SqlLibraryOperators.RLIKE);
checkRlikeFunc(f, SqlLibrary.SPARK, SqlLibraryOperators.REGEXP);
checkRlikeFunc(f, SqlLibrary.HIVE, SqlLibraryOperators.REGEXP);
checkRlikeFunc(f, SqlLibrary.MYSQL, SqlLibraryOperators.RLIKE);
checkNotRlikeFunc(f.withLibrary(SqlLibrary.HIVE));
checkNotRlikeFunc(f.withLibrary(SqlLibrary.SPARK));
Expand Down
Loading