Create RelNode for SELECT 1 - apache-calcite

Which API should I use in org.apache.calcite.tools.RelBuilder to produce RelNode representing query SELECT 1 in BigQuery?

RelNode r = builder.values(new String[] {"c"}, 1).build();

Related

I have to convert below SQL Query to informatica cloud logic

Out put of joiner transformation(TestData table name in SQL Query) as below
I need to load data in to target as below
I wrote SQL Query like
SELECT * FROM TestData AS A
WHERE SourceSystem = 'NC' AND
EXISTS
(SELECT * FROM TestData AS B
WHERE B.ClusterID = A.ClusterID AND B.SourceString = '50012559'
AND B.SourceSystem = 'ACB'
)
can you help me how to covert this SQL Query to Informatica cloud .
Assuming testData is a joiner output here is the solution. I assumed, cluster id is the unique key. if it not unique key, then this solution will cause duplicates.
First, sort your joiner output TestData by ClusterID.
Then put a filter(filter1) on SourceSystem = 'NC' and create pipeline 1.
Connect another filter(filter2) to sorter on SourceString = '50012559' AND B.SourceSystem = 'ACB' and create pipeline 2.
Add another joiner - conditions will be pipeline1.ClusterID =pipeline2.ClusterID.
The output from joiner will be your desired data.
This is how the mapping would look like -
|-Filter 1 ->|
TestData_Joiner... -SRT_ClusterID-->|-Filter 2 ->| -JNR_ClusterID-> <desired output>
Pls note, this will generate data just like below SQL.
``
SELECT * FROM TestData AS A WHERE SourceSystem = 'NC' AND EXISTS (SELECT * FROM TestData AS B WHERE B.ClusterID = A.ClusterID AND B.SourceString = '50012559' AND B.SourceSystem = 'ACB' )

RelNode of a query in which FROM clause itself has a query

I want to achieve result from a table where I ORDER BY column id and I don't want id to be present in the result. I can achieve this using the following query.
SELECT COALESCE (col1, '**')
FROM (select col1, id FROM myDataSet.myTable WHERE col4 = 'some filter' ORDER BY id);
Now, I want to create a RelNode of the above query. As far as I know, in calcite, to perform table scan, there are only two methods scan(String tableName) and scan(Iterable<String> tableNames). Is there a way to scan(RelNode ) ? How to do this ?
The query
select col1, col2, col2 FROM myDataSet.myTable WHERE col4 = 'some filter' ORDER BY id
should also give you the desired result.
If you want to represent the query you have written more directly, you would start by constructing a RelNode for the query in the from clause, starting with a scan of myDataSet.myTable, adding the filter, and the order. Then you can project the specific set of columns you want.
Just simply create a RelNode of inner subquery and create another projection on top of it. Like so.
builder.scan('myTable')
.filter(builder.call(SqlStdOperator.EQUALS, builder.field(col4), builder.literal('some filter') )))
.project(builder.field('col1'), builder.field('id'))
.sort(builder.field('id'))
.project(builder.call(SqlStdOperator.COALESCE(builder.field('col1'), builder.literal('**'))))
.build()

Create RelNode of a select query with concat

I went through the documentation of Apache Calcite. Is the relNode correct for the following query in BigQuery?
SELECT CONCAT('a or b',' ', '\n', first_name)
FROM foo.schema.employee
WHERE first_name = 'name';
relNode = builder
.scan("schema.employee")
.filter(builder.call(SqlStdOperatorTable.EQUALS,
builder.field("first_name"),
builder.literal("name"))
.project(builder.call(SqlStdOperatorTable.CONCAT,
builder.literal("a or b"),
builder.literal(" "),
builder.literal("\\n"),
builder.field(first_name)))
.build()
That looks correct at a glance. I would suggest you confirm by looking at the query results and also by converting your RelNode to SQL.

Using RelBuilder how to create SELECT and WHEREclause?

I have the following code where I am trying to create a relational algebra:
final RelNode relNode = relBuilder.scan(index).build();
I am looking for a sample code where I can create a SELECE and WHERE clause in the above code.
Have you read the documentation? There are several examples there.
final RelNode node = builder
.scan("EMP")
.aggregate(builder.groupKey("DEPTNO"),
builder.count(false, "C"),
builder.sum(false, "S", builder.field("SAL")))
.filter(
builder.call(SqlStdOperatorTable.GREATER_THAN,
builder.field("C"),
builder.literal(10)))
.build();
System.out.println(RelOptUtil.toString(node));
is equivalent to SQL
SELECT deptno, count(*) AS c, sum(sal) AS s
FROM emp
GROUP BY deptno
HAVING count(*) > 10

jpa 2.0: how to do a sql minus of a collection with a table

I am looking for a JPQL or Criteria expression to find a subset of integers(codes) in my collection which are not in the table.
Native Query for Oracle DB would be:
SELECT column_value
FROM Table(:listOfCodes)
MINUS
SELECT code
FROM table1;
This is similar to the post here but not exactly what i want jpa 2.0 criteria api expression for sql minus
Any pointers/help would be much appreciated.
You could just query for the values that are in the table and in your set, and then take the complement of what the query returns:
List<Integer> valuesInTheTable =
(List<Integer>) em.createQuery("select a.column from Entity a where a in (:values)")
.setParameter("values", values)
.getResultList();
Set<Integer> valuesNotInTheTable = new HashSet<Integer>(values);
valuesNotInTheTable.removeAll(valuesInTheTable);