-
Notifications
You must be signed in to change notification settings - Fork 96
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
Databricks dialect #284
Merged
Merged
Databricks dialect #284
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/usr/bin/python | ||
# | ||
# Copyright 2023 Logica Authors | ||
# | ||
# Licensed 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. | ||
|
||
library = """ | ||
->(left:, right:) = {arg: left, value: right}; | ||
ArgMin(a) = SqlExpr( | ||
"(ARRAY_AGG({arg} order by {value}))[1]", | ||
{arg: a.arg, value: a.value}); | ||
ArgMax(a) = SqlExpr( | ||
"(ARRAY_AGG({arg} order by {value} desc))[1]", | ||
{arg: a.arg, value: a.value}); | ||
ArgMaxK(a, l) = SqlExpr( | ||
"SLICE(ARRAY_AGG({arg} order by {value} desc), 1, {lim})", | ||
{arg: a.arg, value: a.value, lim: l}); | ||
ArgMinK(a, l) = SqlExpr( | ||
"SLICE(ARRAY_AGG({arg} order by {value}), 1, {lim})", | ||
{arg: a.arg, value: a.value, lim: l}); | ||
RMatch(s, p) = SqlExpr( | ||
"REGEXP_LIKE({s}, {p})", | ||
{s: s, p: p}); | ||
RExtract(s, p, g) = SqlExpr( | ||
"REGEXP_SUBSTR({s}, {p}, 1, 1, 'c', {g})", | ||
{s: s, p: p, g: g}); | ||
|
||
Array(a) = SqlExpr( | ||
"ARRAY_AGG({value} order by {arg})", | ||
{arg: a.arg, value: a.value}); | ||
|
||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,12 +24,14 @@ | |
from compiler.dialect_libraries import sqlite_library | ||
from compiler.dialect_libraries import trino_library | ||
from compiler.dialect_libraries import presto_library | ||
from compiler.dialect_libraries import databricks_library | ||
else: | ||
from ..compiler.dialect_libraries import bq_library | ||
from ..compiler.dialect_libraries import psql_library | ||
from ..compiler.dialect_libraries import sqlite_library | ||
from ..compiler.dialect_libraries import trino_library | ||
from ..compiler.dialect_libraries import presto_library | ||
from ..compiler.dialect_libraries import databricks_library | ||
|
||
def Get(engine): | ||
return DIALECTS[engine]() | ||
|
@@ -62,7 +64,7 @@ def InfixOperators(self): | |
|
||
def Subscript(self, record, subscript, record_is_table): | ||
return '%s.%s' % (record, subscript) | ||
|
||
def LibraryProgram(self): | ||
return bq_library.library | ||
|
||
|
@@ -164,7 +166,7 @@ def InfixOperators(self): | |
|
||
def Subscript(self, record, subscript, record_is_table): | ||
return '(%s).%s' % (record, subscript) | ||
|
||
def LibraryProgram(self): | ||
return psql_library.library | ||
|
||
|
@@ -207,7 +209,7 @@ def InfixOperators(self): | |
|
||
def Subscript(self, record, subscript, record_is_table): | ||
return '%s.%s' % (record, subscript) | ||
|
||
def LibraryProgram(self): | ||
return trino_library.library | ||
|
||
|
@@ -228,7 +230,7 @@ class Presto(Dialect): | |
|
||
def Name(self): | ||
return 'Presto' | ||
|
||
def BuiltInFunctions(self): | ||
return { | ||
'Range': 'SEQUENCE(0, %s - 1)', | ||
|
@@ -245,7 +247,7 @@ def InfixOperators(self): | |
|
||
def Subscript(self, record, subscript, record_is_table): | ||
return '%s.%s' % (record, subscript) | ||
|
||
def LibraryProgram(self): | ||
return presto_library.library | ||
|
||
|
@@ -330,11 +332,63 @@ def DecorateCombineRule(rule, var): | |
) | ||
return rule | ||
|
||
class Databricks(Dialect): | ||
"""Databricks dialect""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please add a brief comment explaining current state of the dialect. I.e. what are critical features yet to be implemented? E.g.:
I am assuming something is yet TODO, is that so? |
||
|
||
#TODO: add DATEDIFF and NOW function | ||
|
||
def Name(self): | ||
return 'Databricks' | ||
|
||
def BuiltInFunctions(self): | ||
return { | ||
'ToString': 'CAST(%s AS STRING)', | ||
'ToInt64': 'CAST(%s AS BIGINT)', | ||
'ToFloat64': 'CAST(%s AS DOUBLE)', | ||
'AnyValue': 'ANY_VALUE(%s)', | ||
'ILike': '({0}::string ILIKE {1})', | ||
'Like': '({0}::string LIKE {1})', | ||
'Replace': 'REPLACE({0}::string, {1}, {2})', | ||
'ArrayConcat': 'ARRAY_JOIN({0}, {1})', | ||
'JsonExtract': 'GET_JSON_OBJECT({0}, {1})', | ||
'JsonExtractScalar': 'GET_JSON_OBJECT({0}, {1})', | ||
'Length': 'ARRAY_SIZE(%s)', | ||
'DateDiff': 'DATEDIFF({0}, {1}, {2})', | ||
'IsNull': '({0} IS NULL)', | ||
'LogicalOr': 'BOOL_OR(%s)', | ||
'LogicalAnd': 'BOOL AND(%s)' | ||
} | ||
|
||
def InfixOperators(self): | ||
return { | ||
'++': 'CONCAT(%s, %s)', | ||
'in': 'ARRAY_CONTAINS(%s, %s)' | ||
} | ||
|
||
def Subscript(self, record, subscript): | ||
return '%s.%s' % (record, subscript) | ||
|
||
def LibraryProgram(self): | ||
return databricks_library.library | ||
|
||
def UnnestPhrase(self): | ||
return 'explode({0}) AS pushkin({1})' | ||
|
||
def ArrayPhrase(self): | ||
return 'ARRAY(%s)' | ||
|
||
def GroupBySpecBy(self): | ||
return 'index' | ||
|
||
def DecorateCombineRule(self, rule, var): | ||
return rule | ||
|
||
DIALECTS = { | ||
'bigquery': BigQueryDialect, | ||
'sqlite': SqLiteDialect, | ||
'psql': PostgreSQL, | ||
'presto': Presto, | ||
'trino': Trino | ||
'trino': Trino, | ||
'databricks': Databricks | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For Apache compliance please add leading comment: