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

Looker Model metadata #37

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
0ac1f34
Looker instructions
julianhyde Mar 10, 2020
6be3292
Temporary workaround: change SqlInternalOperator's syntax back to FUN…
julianhyde Sep 16, 2020
fb35910
[CALCITE-5052] Allow Source based on a URL with jar: protocol
mkou Mar 22, 2022
e6b3c27
[CALCITE-5621] Support UDT declarations from root of schema model. (#7)
tjbanghart Nov 17, 2022
3099eef
Always check for aliases when deriving data types (#13)
tjbanghart Feb 6, 2023
23eccc5
Ensure timestamp literals with aliasing work
wnob Feb 23, 2023
8f4dfce
[CALCITE-5549] Set DATA_TYPE and IS_GENERATEDCOLUMN values for MEASUR…
wnob May 2, 2023
c669419
[CALCITE-5607] WIP: Serialize return type during RelJson.toJson(RexNo…
tjbanghart Mar 24, 2023
2a3dfa0
[CALCITE-3312] WIP: TIMESTAMPDIFF cannot be converted to SQL
tjbanghart Mar 28, 2023
c720ac7
[CALCITE-5449] Allow EXTRACT() to accept DAYOFWEEK, DAYOFYEAR, ISOWEE…
tjbanghart Mar 28, 2023
e39b61e
[CALCITE-5793] Use NULLS FIRST/LAST syntax for BigQuery
wnob Jun 21, 2023
829bd13
Use first datetime operand as DATE_TRUNC return type
tjbanghart Feb 28, 2023
91ccb01
Fix for sorting by literals
wnob Jun 28, 2023
76e7663
[CALCITE-5747] Conflicting FLOOR return type between Calcite and BigQ…
tanclary Jun 3, 2023
cefef06
Add passing through metadataMap to CalciteMetaTable constructor
olivrlee Jul 28, 2023
9492bf6
Add metadata fields to CalciteColumn()
olivrlee Aug 4, 2023
cbb02c6
Reorder columns and add javadoc
olivrlee Aug 4, 2023
513f3da
Allow CalciteMetaImpl to take in a CalciteMetaTable subclass to use w…
olivrlee Sep 2, 2023
160ec64
Cleanup and add tests
olivrlee Sep 6, 2023
3cc35ac
Fix function names and types
olivrlee Sep 6, 2023
8d471f3
Ignore RedundantModifier alert, may be a misfire
olivrlee Sep 6, 2023
86f8917
Extract into helper functions to remove redundancy to support MetaCol…
olivrlee Sep 7, 2023
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 @@ -120,6 +120,7 @@ public static void main(String[] args) throws Exception {
ConnectionFactories.addType("TIMESTAMP", typeFactory ->
typeFactory.createSqlType(
SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE)))
.with(CalciteConnectionProperty.TIME_ZONE, "UTC")
.connect();
case "scott-postgresql":
return CalciteAssert.that()
Expand Down
91 changes: 55 additions & 36 deletions babel/src/test/resources/sql/big-query.iq
Original file line number Diff line number Diff line change
Expand Up @@ -398,14 +398,12 @@ SELECT
# In the following example, EXTRACT returns values corresponding to
# different date parts from a column of dates near the end of the
# year.

!if (false) {
SELECT
d,
EXTRACT(ISOYEAR FROM d) AS isoyear,
EXTRACT(ISOWEEK FROM d) AS isoweek,
EXTRACT(YEAR FROM d) AS year,
EXTRACT(WEEK FROM d) AS week
EXTRACT(ISOWEEK FROM d) as isoweek,
EXTRACT(WEEK(TUESDAY) FROM d) AS week_tues,
EXTRACT(YEAR FROM d) AS year
FROM UNNEST(
ARRAY [DATE '2015-12-23',
DATE '2015-12-24',
Expand All @@ -426,48 +424,62 @@ FROM UNNEST(
DATE '2016-01-08',
DATE '2016-01-09']) AS d
ORDER BY d;
+------------+---------+---------+------+------+
| date | isoyear | isoweek | year | week |
+------------+---------+---------+------+------+
| 2015-12-23 | 2015 | 52 | 2015 | 51 |
| 2015-12-24 | 2015 | 52 | 2015 | 51 |
| 2015-12-25 | 2015 | 52 | 2015 | 51 |
| 2015-12-26 | 2015 | 52 | 2015 | 51 |
| 2015-12-27 | 2015 | 52 | 2015 | 52 |
| 2015-12-28 | 2015 | 53 | 2015 | 52 |
| 2015-12-29 | 2015 | 53 | 2015 | 52 |
| 2015-12-30 | 2015 | 53 | 2015 | 52 |
| 2015-12-31 | 2015 | 53 | 2015 | 52 |
| 2016-01-01 | 2015 | 53 | 2016 | 0 |
| 2016-01-02 | 2015 | 53 | 2016 | 0 |
| 2016-01-03 | 2015 | 53 | 2016 | 1 |
| 2016-01-04 | 2016 | 1 | 2016 | 1 |
| 2016-01-05 | 2016 | 1 | 2016 | 1 |
| 2016-01-06 | 2016 | 1 | 2016 | 1 |
| 2016-01-07 | 2016 | 1 | 2016 | 1 |
| 2016-01-08 | 2016 | 1 | 2016 | 1 |
| 2016-01-09 | 2016 | 1 | 2016 | 1 |
+------------+---------+---------+------+------+
+------------+---------+---------+-----------+------+
| d | isoyear | isoweek | week_tues | year |
+------------+---------+---------+-----------+------+
| 2015-12-23 | 2015 | 52 | 51 | 2015 |
| 2015-12-24 | 2015 | 52 | 51 | 2015 |
| 2015-12-25 | 2015 | 52 | 51 | 2015 |
| 2015-12-26 | 2015 | 52 | 51 | 2015 |
| 2015-12-27 | 2015 | 52 | 51 | 2015 |
| 2015-12-28 | 2015 | 53 | 51 | 2015 |
| 2015-12-29 | 2015 | 53 | 52 | 2015 |
| 2015-12-30 | 2015 | 53 | 52 | 2015 |
| 2015-12-31 | 2015 | 53 | 52 | 2015 |
| 2016-01-01 | 2015 | 53 | 0 | 2016 |
| 2016-01-02 | 2015 | 53 | 0 | 2016 |
| 2016-01-03 | 2015 | 53 | 0 | 2016 |
| 2016-01-04 | 2016 | 1 | 0 | 2016 |
| 2016-01-05 | 2016 | 1 | 1 | 2016 |
| 2016-01-06 | 2016 | 1 | 1 | 2016 |
| 2016-01-07 | 2016 | 1 | 1 | 2016 |
| 2016-01-08 | 2016 | 1 | 1 | 2016 |
| 2016-01-09 | 2016 | 1 | 1 | 2016 |
+------------+---------+---------+-----------+------+
(18 rows)

!ok
!}


# In the following example, date_expression falls on a Sunday. EXTRACT
# calculates the first column using weeks that begin on Sunday, and it
# calculates the second column using weeks that begin on Monday.

!if (false) {
WITH t AS (SELECT DATE('2017-11-05') AS d)
SELECT
d,
EXTRACT(ISOWEEK FROM d) AS isoweek,
EXTRACT(WEEK(SUNDAY) FROM d) AS week_sunday,
EXTRACT(WEEK(MONDAY) FROM d) AS week_monday FROM t;
+------------+-------------+-------------+
| date | week_sunday | week_monday |
+------------+-------------+-------------+
| 2017-11-05 | 45 | 44 |
+------------+-------------+-------------+
+------------+---------+-------------+-------------+
| d | isoweek | week_sunday | week_monday |
+------------+---------+-------------+-------------+
| 2017-11-05 | 44 | 45 | 44 |
+------------+---------+-------------+-------------+
(1 row)

!ok

SELECT EXTRACT(DAYOFWEEK FROM DATE '2008-12-25') as dow,
EXTRACT(DAYOFYEAR FROM DATE '2008-12-25') as doy;
+-----+-----+
| dow | doy |
+-----+-----+
| 5 | 360 |
+-----+-----+
(1 row)

!ok
!}

# In the following example, EXTRACT returns a value corresponding to
# the HOUR time part.
Expand Down Expand Up @@ -2085,7 +2097,8 @@ SELECT
(1 row)

!ok

# Bug.CALCITE_2539_FIXED
!if (false) {
#####################################################################
# DATE_DIFF
#
Expand Down Expand Up @@ -2279,6 +2292,9 @@ SELECT
# 1. DATETIME_DIFF with the date part ISOWEEK also returns 1 because
# ISO weeks begin on Monday.

!if (false) {
# TODO: Look into why these fail with the changes
# in TimestampWithTimeZoneString.java
SELECT
DATETIME_DIFF('2017-12-18', '2017-12-17', WEEK) AS week_diff,
DATETIME_DIFF('2017-12-18', '2017-12-17', WEEK(MONDAY)) AS week_weekday_diff,
Expand All @@ -2291,6 +2307,7 @@ SELECT
(1 row)

!ok
!}

#####################################################################
# TIME_DIFF
Expand Down Expand Up @@ -2396,6 +2413,8 @@ SELECT TIMESTAMP_DIFF(TIMESTAMP '2008-12-25', TIMESTAMP '2008-09-25', `quarter`)
(1 row)

!ok

!}
#####################################################################
# DATE_TRUNC
#
Expand Down
17 changes: 14 additions & 3 deletions core/src/main/codegen/templates/Parser.jj
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.sql.parser.SqlParserUtil;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.sql.validate.SqlConformance;
import org.apache.calcite.sql.validate.SqlConformanceEnum;
import org.apache.calcite.util.Glossary;
import org.apache.calcite.util.Pair;
import org.apache.calcite.util.SourceStringReader;
Expand Down Expand Up @@ -5969,6 +5970,7 @@ SqlTypeNameSpec DateTimeTypeName() :
{
int precision = -1;
SqlTypeName typeName;
SqlIdentifier userTimeType;
boolean withLocalTimeZone = false;
final Span s;
}
Expand All @@ -5977,6 +5979,17 @@ SqlTypeNameSpec DateTimeTypeName() :
typeName = SqlTypeName.DATE;
return new SqlBasicTypeNameSpec(typeName, getPos());
}
|
/**
* TODO: This is BAD. Find a way to map DATETIME to a user alias type.
* Because DATETIME is a recognized identifier but not listed here in
* DateTimeTypeName() we'll get a parse error when casting without this hack.
*/
<DATETIME> {
s = span();
userTimeType = new SqlIdentifier(unquotedIdentifier(), getPos());
return new SqlUserDefinedTypeNameSpec(userTimeType, s.end(this));
}
|
LOOKAHEAD(2)
<TIME> { s = span(); }
Expand Down Expand Up @@ -7335,9 +7348,7 @@ SqlNode StandardFloorCeilOptions(Span s, boolean floorFlag) :
}
)?
<RPAREN> {
SqlOperator op = floorFlag
? SqlStdOperatorTable.FLOOR
: SqlStdOperatorTable.CEIL;
SqlOperator op = SqlStdOperatorTable.floorCeil(floorFlag, (SqlConformanceEnum) this.conformance);
function = op.createCall(s.end(this), args);
}
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
import static org.apache.calcite.sql.fun.SqlLibraryOperators.ATANH;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.BOOL_AND;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.BOOL_OR;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.CEIL_BIG_QUERY;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.CHAR;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.CHR;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.COMPRESS;
Expand All @@ -163,6 +164,7 @@
import static org.apache.calcite.sql.fun.SqlLibraryOperators.EXISTS_NODE;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.EXTRACT_VALUE;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.EXTRACT_XML;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.FLOOR_BIG_QUERY;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.FORMAT_DATE;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.FORMAT_DATETIME;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.FORMAT_TIME;
Expand Down Expand Up @@ -618,6 +620,9 @@ Builder populate2() {
map.put(TIMESTAMP_TRUNC, map.get(FLOOR));
map.put(TIME_TRUNC, map.get(FLOOR));
map.put(DATETIME_TRUNC, map.get(FLOOR));
// BigQuery FLOOR and CEIL should use same implementation as standard
map.put(CEIL_BIG_QUERY, map.get(CEIL));
map.put(FLOOR_BIG_QUERY, map.get(FLOOR));

map.put(LAST_DAY,
new LastDayImplementor("lastDay", BuiltInMethod.LAST_DAY));
Expand Down Expand Up @@ -2846,6 +2851,10 @@ private static class ExtractImplementor extends AbstractRexCallImplementor {

@Override Expression implementSafe(final RexToLixTranslator translator,
final RexCall call, final List<Expression> argValueList) {
if (argValueList.get(0).getType() == String.class) {
return Expressions.call(BuiltInMethod.CUSTOM_DATE_EXTRACT.method,
translator.getRoot(), argValueList.get(0), argValueList.get(1));
}
final TimeUnitRange timeUnitRange =
(TimeUnitRange) translator.getLiteralValue(argValueList.get(0));
final TimeUnit unit = requireNonNull(timeUnitRange, "timeUnitRange").startUnit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,10 @@ public interface CalciteConnectionConfig extends ConnectionConfig {
boolean lenientOperatorLookup();
/** Returns the value of {@link CalciteConnectionProperty#TOPDOWN_OPT}. */
boolean topDownOpt();

/** Returns the value of {@link CalciteConnectionProperty#META_TABLE_CLASS}. */
Class<?> metaTableClass();

/** Returns the value of {@link CalciteConnectionProperty#META_COLUMN_CLASS}. */
Class<?> metaColumnClass();
}
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,12 @@ public boolean isSet(CalciteConnectionProperty property) {
@Override public boolean topDownOpt() {
return CalciteConnectionProperty.TOPDOWN_OPT.wrap(properties).getBoolean();
}

@Override public Class<?> metaTableClass() {
return CalciteConnectionProperty.META_TABLE_CLASS.wrap(properties).getClass();
}

@Override public Class<?> metaColumnClass() {
return CalciteConnectionProperty.META_COLUMN_CLASS.wrap(properties).getClass();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package org.apache.calcite.config;

import org.apache.calcite.avatica.ConnectionProperty;
import org.apache.calcite.avatica.MetaImpl.MetaColumn;
import org.apache.calcite.avatica.util.Casing;
import org.apache.calcite.avatica.util.Quoting;
import org.apache.calcite.jdbc.CalciteMetaImpl.CalciteMetaTable;
import org.apache.calcite.model.JsonSchema;
import org.apache.calcite.sql.validate.SqlConformanceEnum;

Expand Down Expand Up @@ -106,6 +108,12 @@
* {@link org.apache.calcite.sql.parser.SqlParserImplFactory}. */
PARSER_FACTORY("parserFactory", Type.PLUGIN, null, false),

/** The CalciteMetaTable class (or subclass) that will be used for the getTables() enumerable. */
META_TABLE_CLASS("metaTableClass", Type.CLASS, CalciteMetaTable.class, false),

Check failure on line 112 in core/src/main/java/org/apache/calcite/config/CalciteConnectionProperty.java

View workflow job for this annotation

GitHub Actions / Linux (JDK 11), Avatica main

[Task :core:compileJava] cannot find symbol META_TABLE_CLASS("metaTableClass", Type.CLASS, CalciteMetaTable.class, false), ^ symbol: variable CLASS

/** The MetaColumn class (or subclass) that will be used for the getColumns() enumerable. */
META_COLUMN_CLASS("metaColumnClass", Type.CLASS, MetaColumn.class, false),

Check failure on line 115 in core/src/main/java/org/apache/calcite/config/CalciteConnectionProperty.java

View workflow job for this annotation

GitHub Actions / Linux (JDK 11), Avatica main

[Task :core:compileJava] cannot find symbol META_COLUMN_CLASS("metaColumnClass", Type.CLASS, MetaColumn.class, false), ^ symbol: variable CLASS

/** Name of initial schema. */
SCHEMA("schema", Type.STRING, null, false),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.calcite.avatica.remote.TypedValue;
import org.apache.calcite.config.CalciteConnectionConfig;
import org.apache.calcite.config.CalciteConnectionConfigImpl;
import org.apache.calcite.config.CalciteConnectionProperty;
import org.apache.calcite.jdbc.CalcitePrepare.Context;
import org.apache.calcite.linq4j.BaseQueryable;
import org.apache.calcite.linq4j.Enumerable;
Expand Down Expand Up @@ -190,6 +191,23 @@ void init() {
return super.unwrap(iface);
}

/** If a subclass is passed in for CalciteMetaTable or MetaColumn for
* {@code CalciteConnectionProperty.META_TABLE_CLASS} or
* {@code CalciteConnectionProperty.META_COLUMN_CLASS} when connecting, calls to getTables()
* or getColumns() will create an enumerable using the subclass, otherwise it
* will default to creating CaliteMetaTable / MetaColumn. */
public Class<?> getMetaClass(CalciteConnectionProperty connectionProperty,
Class<?> defaultClass) {
String className =
(String) info.getOrDefault(connectionProperty.camelName(),
defaultClass.getName());
try {
return Class.forName(className);
} catch (ClassNotFoundException e) {
return defaultClass;
}
}

@Override public CalciteStatement createStatement(int resultSetType,
int resultSetConcurrency, int resultSetHoldability) throws SQLException {
return (CalciteStatement) super.createStatement(resultSetType,
Expand Down
Loading
Loading