Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add parsing for AT TIME ZONE #9477

Merged
merged 3 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions pinot-common/src/main/codegen/config.fmpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,11 @@ data: {
"SqlInsertFromFile()"
]

# List of custom function syntax to add
extraBinaryExpressions: [
"SqlAtTimeZone"
]

# List of files in @includes directory that have parser method
implementationFiles: [
"parserImpls.ftl"
Expand Down
17 changes: 17 additions & 0 deletions pinot-common/src/main/codegen/includes/parserImpls.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,20 @@ SqlNodeList SqlStmtsEof() :
return stmts;
}
}

void SqlAtTimeZone(List<Object> list, ExprContext exprContext, Span s) :
{
List<Object> list2;
SqlOperator op;
}
{
{
checkNonQueryExpression(exprContext);
s.clear().add(this);
}
Comment on lines +112 to +115
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we need to check this. can't it be something like

SELECT * FROM tbl 
WHERE (SELECT MAX(timestampWithoutTimeZone) FROM anotherTbl) AT TIME ZONE 'Pacific' BETWEEN xxx AND xxx

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I modeled this after the LIKE syntax which has that check - I'm not 100% sure why they added that restriction, because I checked Postgres and SELECT (SELECT str FROM foo) LIKE 'foo' FROM foo; works and so does nested AT TIME ZONE like your example query.

I tried these in Pinot, though, and it looks like there are other limitations that prevent us from doing that...

SELECT (SELECT str FROM foo) LIKE 'foo' FROM foo; returns the following exception in Pinot:

java.lang.ClassCastException: class org.apache.calcite.sql.SqlSelect cannot be cast to class org.apache.calcite.sql.SqlBasicCall (org.apache.calcite.sql.SqlSelect and org.apache.calcite.sql.SqlBasicCall are in unnamed module of loader 'app')

	at org.apache.pinot.sql.parsers.CalciteSqlParser.toExpression(CalciteSqlParser.java:748)
	at org.apache.pinot.sql.parsers.CalciteSqlParser.compileFunctionExpression(CalciteSqlParser.java:806)
	at org.apache.pinot.sql.parsers.CalciteSqlParser.toExpression(CalciteSqlParser.java:748)

If you feel strongly, I can remove the check, but it won't work either way without additional work (which I think we can address in a follow-up if desired)

Copy link
Contributor

Choose a reason for hiding this comment

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

for now it should be fine. since this should probably go back to calcite we can adjust later

<AT> <TIME> <ZONE> { op = SqlAtTimeZone.INSTANCE; }
list2 = Expression2(ExprContext.ACCEPT_SUB_QUERY) {
list.add(new SqlParserUtil.ToTreeListItem(op, s.pos()));
list.addAll(list2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* 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.sql.parsers.parser;

import java.util.Arrays;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlSpecialOperator;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.sql.type.OperandTypes;
import org.apache.calcite.sql.type.SqlTypeFamily;
import org.apache.calcite.sql.type.SqlTypeName;

import static java.util.Objects.requireNonNull;


public class SqlAtTimeZone extends SqlSpecialOperator {
Copy link
Contributor

@walterddr walterddr Sep 28, 2022

Choose a reason for hiding this comment

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

lets model it similar to SqlTimestampAddFunction extends SqlFunction?
any specific reason we want to model it as special operator?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This has special syntax (it's not the usual FUN_NAME(params...)) which makes it a bit more complicated - I modeled after SqlLikeOperator which is more similar to this w.r.t syntax. I have to implement the reduceExpr to clearly indicate that the left argument comes before the operation and the right argument comes afterwards.


public static final SqlAtTimeZone INSTANCE = new SqlAtTimeZone();

private SqlAtTimeZone() {
super(
"AT_TIME_ZONE",
SqlKind.OTHER_FUNCTION,
32, // put this at the same precedence as LIKE (see SqlLikeOperator)
false,
opBinding -> opBinding.getTypeFactory().createSqlType(SqlTypeName.TIMESTAMP),
null,
OperandTypes.family(SqlTypeFamily.TIME)
);
}

@Override
public ReduceResult reduceExpr(int ordinal, TokenSequence list) {
SqlNode left = list.node(ordinal - 1);
SqlNode right = list.node(ordinal + 1);
return new ReduceResult(ordinal - 1,
ordinal + 2,
createCall(
SqlParserPos.sum(
Arrays.asList(requireNonNull(left, "left").getParserPosition(),
requireNonNull(right, "right").getParserPosition(),
list.pos(ordinal))),
left,
right));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2681,6 +2681,63 @@ public void testParserExtensionImpl() {
Assert.assertEquals(sqlNodeAndOptions.getSqlType(), PinotSqlType.DML);
}


@Test
public void shouldParseBasicAtTimeZoneExtension() {
// Given:
String sql = "SELECT ts AT TIME ZONE 'pst' FROM myTable;";

// When:
PinotQuery pinotQuery = CalciteSqlParser.compileToPinotQuery(sql);

// Then:
Assert.assertEquals(pinotQuery.getSelectListSize(), 1);
Function fun = pinotQuery.getSelectList().get(0).getFunctionCall();
Assert.assertEquals(fun.operator, "attimezone");
Assert.assertEquals(fun.operands.size(), 2);
Assert.assertEquals(fun.operands.get(0).getIdentifier().name, "ts");
Assert.assertEquals(fun.operands.get(1).getLiteral().getStringValue(), "pst");
}

@Test
public void shouldParseNestedTimeExprAtTimeZoneExtension() {
// Given:
String sql = "SELECT ts + 123 AT TIME ZONE 'pst' FROM myTable;";

// When:
PinotQuery pinotQuery = CalciteSqlParser.compileToPinotQuery(sql);

// Then:
Assert.assertEquals(pinotQuery.getSelectListSize(), 1);
Function fun = pinotQuery.getSelectList().get(0).getFunctionCall();
Assert.assertEquals(fun.operator, "attimezone");
Assert.assertEquals(fun.operands.size(), 2);
Assert.assertEquals(fun.operands.get(0).getFunctionCall().operator, "plus");
Assert.assertEquals(fun.operands.get(0).getFunctionCall().operands.size(), 2);
Assert.assertEquals(fun.operands.get(0).getFunctionCall().operands.get(0).getIdentifier().getName(), "ts");
Assert.assertEquals(fun.operands.get(0).getFunctionCall().operands.get(1).getLiteral().getLongValue(), 123L);
Assert.assertEquals(fun.operands.get(1).getLiteral().getStringValue(), "pst");
}

@Test
public void shouldParseOutsideExprAtTimeZoneExtension() {
// Given:
String sql = "SELECT ts AT TIME ZONE 'pst' > 123 FROM myTable;";

// When:
PinotQuery pinotQuery = CalciteSqlParser.compileToPinotQuery(sql);

// Then:
Assert.assertEquals(pinotQuery.getSelectListSize(), 1);
Function fun = pinotQuery.getSelectList().get(0).getFunctionCall();
Assert.assertEquals(fun.operator, "GREATER_THAN");
Assert.assertEquals(fun.operands.size(), 2);
Assert.assertEquals(fun.operands.get(0).getFunctionCall().operator, "attimezone");
Assert.assertEquals(fun.operands.get(0).getFunctionCall().operands.size(), 2);
Assert.assertEquals(fun.operands.get(0).getFunctionCall().operands.get(0).getIdentifier().getName(), "ts");
Assert.assertEquals(fun.operands.get(0).getFunctionCall().operands.get(1).getLiteral().getStringValue(), "pst");
}

private static SqlNodeAndOptions testSqlWithCustomSqlParser(String sqlString) {
try (StringReader inStream = new StringReader(sqlString)) {
SqlParserImpl sqlParser = CalciteSqlParser.newSqlParser(inStream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.apache.pinot.spi.data.FieldSpec.DataType;
import org.apache.pinot.spi.data.Schema;
import org.apache.pinot.spi.data.readers.GenericRow;
import org.apache.pinot.spi.exception.BadQueryRequestException;
import org.apache.pinot.spi.utils.ReadMode;
import org.apache.pinot.spi.utils.builder.TableConfigBuilder;
import org.testng.annotations.AfterClass;
Expand Down Expand Up @@ -222,6 +223,17 @@ public void testQueries() {
}
}

@Test(
expectedExceptions = BadQueryRequestException.class,
expectedExceptionsMessageRegExp = ".*attimezone not found.*"
)
public void shouldThrowOnAtTimeZone() {
// this isn't yet implemented but the syntax is supported, make sure the
// degradation experience is clean
String query = "SELECT timestampColumn AT TIME ZONE 'pst' FROM testTable";
getBrokerResponse(query);
}

@AfterClass
public void tearDown()
throws IOException {
Expand Down