Skip to content

Commit

Permalink
HIVE-28560: AssertionError in SqlImplementor when query over JDBC tab…
Browse files Browse the repository at this point in the history
…le contains LIMIT (Soumyakanti Das reviewed by Stamatis Zampetakis)

Close #5491
  • Loading branch information
soumyakanti3578 authored and zabetak committed Oct 14, 2024
1 parent a64a4f6 commit 771b003
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,41 +65,6 @@ public HiveJdbcImplementor(SqlDialect dialect, JavaTypeFactory typeFactory) {
return builder.result();
}

@Override public Result visit(Sort e) {
Result x = visitInput(e, 0, Clause.ORDER_BY, Clause.FETCH, Clause.OFFSET);
Builder builder = x.builder(e);
List<SqlNode> orderByList = Expressions.list();
for (RelFieldCollation field : e.getCollation().getFieldCollations()) {
builder.addOrderItem(orderByList, field);
}

// Create select list as we want to keep the column aliases
// instead of producing STAR
final List<SqlNode> selectList = new ArrayList<>();
for (int i = 0; i < e.getRowType().getFieldCount(); i++) {
RexInputRef ref = RexInputRef.of(i, e.getRowType());
SqlNode sqlExpr = builder.context.toSql(null, ref);
addSelect(selectList, sqlExpr, e.getRowType());
}
builder.setSelect(new SqlNodeList(selectList, POS));

if (!orderByList.isEmpty()) {
builder.setOrderBy(new SqlNodeList(orderByList, POS));
x = builder.result();
}
if (e.fetch != null) {
builder = x.builder(e);
builder.setFetch(builder.context.toSql(null, e.fetch));
x = builder.result();
}
if (e.offset != null) {
builder = x.builder(e);
builder.setOffset(builder.context.toSql(null, e.offset));
x = builder.result();
}
return x;
}

@Override public Result visit(Join e) {
final Result leftResult = visitInput(e, 0).resetAlias();
final Result rightResult = visitInput(e, 1).resetAlias();
Expand Down
15 changes: 15 additions & 0 deletions ql/src/test/queries/clientpositive/jdbc_table_limit_postgres.q
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--! qt:database:postgres:q_test_country_table.sql

CREATE EXTERNAL TABLE country (id int, name varchar(20))
STORED BY 'org.apache.hive.storage.jdbc.JdbcStorageHandler'
TBLPROPERTIES (
"hive.sql.database.type" = "POSTGRES",
"hive.sql.jdbc.driver" = "org.postgresql.Driver",
"hive.sql.jdbc.url" = "jdbc:postgresql://localhost:5432/qtestDB",
"hive.sql.dbcp.username" = "qtestuser",
"hive.sql.dbcp.password" = "qtestpassword",
"hive.sql.table" = "country");

EXPLAIN CBO SELECT * FROM country ORDER BY id LIMIT 2;
EXPLAIN SELECT * FROM country ORDER BY id LIMIT 2;
SELECT * FROM country ORDER BY id LIMIT 2;
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ STAGE PLANS:
alias: db1_ext_auth1
properties:
hive.sql.query SELECT "IKEY", "bkey", "fkey", "dkey"
FROM (SELECT *
FROM (SELECT "IKEY", "bkey", "fkey", "dkey"
FROM "EXTERNAL_JDBC_SIMPLE_DERBY2_TABLE1"
FETCH NEXT 10 ROWS ONLY) AS "t"
hive.sql.query.fieldNames IKEY,bkey,fkey,dkey
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
PREHOOK: query: CREATE EXTERNAL TABLE country (id int, name varchar(20))
STORED BY 'org.apache.hive.storage.jdbc.JdbcStorageHandler'
TBLPROPERTIES (
"hive.sql.database.type" = "POSTGRES",
"hive.sql.jdbc.driver" = "org.postgresql.Driver",
"hive.sql.jdbc.url" = "jdbc:postgresql://localhost:5432/qtestDB",
"hive.sql.dbcp.username" = "qtestuser",
"hive.sql.dbcp.password" = "qtestpassword",
"hive.sql.table" = "country")
PREHOOK: type: CREATETABLE
PREHOOK: Output: database:default
PREHOOK: Output: default@country
POSTHOOK: query: CREATE EXTERNAL TABLE country (id int, name varchar(20))
STORED BY 'org.apache.hive.storage.jdbc.JdbcStorageHandler'
TBLPROPERTIES (
"hive.sql.database.type" = "POSTGRES",
"hive.sql.jdbc.driver" = "org.postgresql.Driver",
"hive.sql.jdbc.url" = "jdbc:postgresql://localhost:5432/qtestDB",
"hive.sql.dbcp.username" = "qtestuser",
"hive.sql.dbcp.password" = "qtestpassword",
"hive.sql.table" = "country")
POSTHOOK: type: CREATETABLE
POSTHOOK: Output: database:default
POSTHOOK: Output: default@country
PREHOOK: query: EXPLAIN CBO SELECT * FROM country ORDER BY id LIMIT 2
PREHOOK: type: QUERY
PREHOOK: Input: default@country
#### A masked pattern was here ####
POSTHOOK: query: EXPLAIN CBO SELECT * FROM country ORDER BY id LIMIT 2
POSTHOOK: type: QUERY
POSTHOOK: Input: default@country
#### A masked pattern was here ####
CBO PLAN:
HiveJdbcConverter(convention=[JDBC.POSTGRES])
JdbcSort(sort0=[$0], dir0=[ASC], fetch=[2])
JdbcHiveTableScan(table=[[default, country]], table:alias=[country])

PREHOOK: query: EXPLAIN SELECT * FROM country ORDER BY id LIMIT 2
PREHOOK: type: QUERY
PREHOOK: Input: default@country
#### A masked pattern was here ####
POSTHOOK: query: EXPLAIN SELECT * FROM country ORDER BY id LIMIT 2
POSTHOOK: type: QUERY
POSTHOOK: Input: default@country
#### A masked pattern was here ####
STAGE DEPENDENCIES:
Stage-0 is a root stage

STAGE PLANS:
Stage: Stage-0
Fetch Operator
limit: -1
Processor Tree:
TableScan
alias: country
properties:
hive.sql.query SELECT "id", "name"
FROM (SELECT "id", "name"
FROM "country"
ORDER BY "id"
FETCH NEXT 2 ROWS ONLY) AS "t"
hive.sql.query.fieldNames id,name
hive.sql.query.fieldTypes int,varchar(20)
hive.sql.query.split false
Select Operator
expressions: id (type: int), name (type: varchar(20))
outputColumnNames: _col0, _col1
ListSink

PREHOOK: query: SELECT * FROM country ORDER BY id LIMIT 2
PREHOOK: type: QUERY
PREHOOK: Input: default@country
#### A masked pattern was here ####
POSTHOOK: query: SELECT * FROM country ORDER BY id LIMIT 2
POSTHOOK: type: QUERY
POSTHOOK: Input: default@country
#### A masked pattern was here ####
1 India
2 Russia

0 comments on commit 771b003

Please sign in to comment.