Skip to content

Commit

Permalink
[CALCITE-6689] Add support for INSTR() in Hive SqlLibrary
Browse files Browse the repository at this point in the history
  • Loading branch information
vikramahuja1001 authored and mihaibudiu committed Nov 13, 2024
1 parent 4e4b246 commit 215577d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,14 @@ public HiveSqlDialect(Context context) {
writer.sep(",");
call.operand(0).unparse(writer, leftPrec, rightPrec);
if (3 == call.operandCount()) {
throw new RuntimeException("3rd operand Not Supported for Function INSTR in Hive");
writer.sep(",");
call.operand(2).unparse(writer, leftPrec, rightPrec);
}
if (4 == call.operandCount()) {
writer.sep(",");
call.operand(2).unparse(writer, leftPrec, rightPrec);
writer.sep(",");
call.operand(3).unparse(writer, leftPrec, rightPrec);
}
writer.endFunCall(frame);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ static RelDataType deriveTypeSplit(SqlOperatorBinding operatorBinding,
public static final SqlFunction STRPOS = new SqlPositionFunction("STRPOS");

/** The "INSTR(string, substring [, position [, occurrence]])" function. */
@LibraryOperator(libraries = {BIG_QUERY, MYSQL, ORACLE})
@LibraryOperator(libraries = {BIG_QUERY, HIVE, MYSQL, ORACLE})
public static final SqlFunction INSTR = new SqlPositionFunction("INSTR");

/** Generic "SUBSTR(string, position [, substringLength ])" function. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2937,24 +2937,51 @@ private SqlDialect nonOrdinalDialect() {
final String query = "SELECT INSTR('ABC', 'A', 1, 1) from \"product\"";
final String expectedBQ = "SELECT INSTR('ABC', 'A', 1, 1)\n"
+ "FROM foodmart.product";
final String expectedHive = "SELECT INSTR('ABC', 'A', 1, 1)\n"
+ "FROM `foodmart`.`product`";
final String expected_oracle = "SELECT INSTR('ABC', 'A', 1, 1)\n"
+ "FROM \"foodmart\".\"product\"";
final Sql sqlOracle = fixture().withOracle().withLibrary(SqlLibrary.ORACLE);
sqlOracle.withSql(query).withOracle().ok(expected_oracle);
final Sql sqlBQ = fixture().withBigQuery().withLibrary(SqlLibrary.BIG_QUERY);
sqlBQ.withSql(query).withBigQuery().ok(expectedBQ);
final Sql sqlHive = fixture().withHive().withLibrary(SqlLibrary.HIVE);
sqlHive.withSql(query).withHive().ok(expectedHive);
}

@Test void testInstrFunction3Operands() {
final String query = "SELECT INSTR('ABC', 'A', 1) from \"product\"";
final String expectedBQ = "SELECT INSTR('ABC', 'A', 1)\n"
+ "FROM foodmart.product";
final String expectedHive = "SELECT INSTR('ABC', 'A', 1)\n"
+ "FROM `foodmart`.`product`";
final String expectedOracle = "SELECT INSTR('ABC', 'A', 1)\n"
+ "FROM \"foodmart\".\"product\"";
final Sql sqlOracle = fixture().withOracle().withLibrary(SqlLibrary.ORACLE);
sqlOracle.withSql(query).withOracle().ok(expectedOracle);
final Sql sqlBQ = fixture().withBigQuery().withLibrary(SqlLibrary.BIG_QUERY);
sqlBQ.withSql(query).withBigQuery().ok(expectedBQ);
final Sql sqlHive = fixture().withHive().withLibrary(SqlLibrary.HIVE);
sqlHive.withSql(query).withHive().ok(expectedHive);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6689">[CALCITE-6689]
* Add support for INSTR() in Hive SqlLibrary</a>. */
@Test void testInstrFunction2Operands() {
final String query = "SELECT INSTR('ABC', 'A') from \"product\"";
final String expectedBQ = "SELECT INSTR('ABC', 'A')\n"
+ "FROM foodmart.product";
final String expectedHive = "SELECT INSTR('ABC', 'A')\n"
+ "FROM `foodmart`.`product`";
final String expectedOracle = "SELECT INSTR('ABC', 'A')\n"
+ "FROM \"foodmart\".\"product\"";
final Sql sqlOracle = fixture().withOracle().withLibrary(SqlLibrary.ORACLE);
sqlOracle.withSql(query).withOracle().ok(expectedOracle);
final Sql sqlBQ = fixture().withBigQuery().withLibrary(SqlLibrary.BIG_QUERY);
sqlBQ.withSql(query).withBigQuery().ok(expectedBQ);
final Sql sqlHive = fixture().withHive().withLibrary(SqlLibrary.HIVE);
sqlHive.withSql(query).withHive().ok(expectedHive);
}

/** Tests that we escape single-quotes in character literals using back-slash
Expand Down
4 changes: 2 additions & 2 deletions site/_docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -2833,8 +2833,8 @@ In the following:
| b s | IFNULL(value1, value2) | Equivalent to `NVL(value1, value2)`
| p | string1 ILIKE string2 [ ESCAPE string3 ] | Whether *string1* matches pattern *string2*, ignoring case (similar to `LIKE`)
| p | string1 NOT ILIKE string2 [ ESCAPE string3 ] | Whether *string1* does not match pattern *string2*, ignoring case (similar to `NOT LIKE`)
| b o | INSTR(string, substring [, from [, occurrence ] ]) | Returns the position of *substring* in *string*, searching starting at *from* (default 1), and until locating the nth *occurrence* (default 1) of *substring*
| b m o | INSTR(string, substring) | Equivalent to `POSITION(substring IN string)`
| b h o | INSTR(string, substring [, from [, occurrence ] ]) | Returns the position of *substring* in *string*, searching starting at *from* (default 1), and until locating the nth *occurrence* (default 1) of *substring*
| b h m o | INSTR(string, substring) | Equivalent to `POSITION(substring IN string)`
| b | IS_INF(value) | Returns whether *value* is infinite
| b | IS_NAN(value) | Returns whether *value* is NaN
| m | JSON_TYPE(jsonValue) | Returns a string value indicating the type of *jsonValue*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10836,7 +10836,7 @@ private void testSysDateFunc(Pair<String, Hook.Closeable> pair) {
f.checkNull("INSTR(x'', null, 1, 1)");
};
final List<SqlLibrary> libraries =
list(SqlLibrary.BIG_QUERY, SqlLibrary.MYSQL, SqlLibrary.ORACLE);
list(SqlLibrary.BIG_QUERY, SqlLibrary.HIVE, SqlLibrary.MYSQL, SqlLibrary.ORACLE);
f0.forEachLibrary(libraries, consumer);
}

Expand Down

0 comments on commit 215577d

Please sign in to comment.