[
https://issues.apache.org/jira/browse/CALCITE-4963?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17464740#comment-17464740
]
Marco Jorge edited comment on CALCITE-4963 at 12/23/21, 8:51 PM:
-----------------------------------------------------------------
Please see [https://github.com/apache/calcite/pull/2658.]
Sample #1 reusability (if you just need to override the selection process):
{code:java}
import org.apache.calcite.sql.SqlDialect;
import org.apache.calcite.sql.SqlDialectFactoryImpl;
import org.apache.calcite.sql.dialect.AnsiSqlDialect;
import java.sql.DatabaseMetaData;
public class CustomSqlDialectFactory extends SqlDialectFactoryImpl {
/**
* CAVEAT: Due to the reflective access in
org.apache.calcite.avatica.AvaticaUtils#instantiatePlugin
* always make sure to declare a field with the name `INSTANCE` or the
default from
* org.apache.calcite.sql.SqlDialectFactoryImpl will be used instead of
objects
* produced by this factory.
*/
public static final CustomSqlDialectFactory INSTANCE = new
CustomSqlDialectFactory();
@Override
public SqlDialect create(DatabaseMetaData databaseMetaData,
SqlDialect.Context context, SqlDialect fallback) {
if (/* my particular conditions */) {
return /* my particular dialect */;
} else {
return super.create(databaseMetaData, context, fallback);
}
}
}
{code}
Sample #2 reusability (if you need to override the context creation):
{code:java}
import org.apache.calcite.sql.SqlDialect;
import org.apache.calcite.sql.SqlDialectFactoryImpl;
import org.apache.calcite.sql.dialect.AnsiSqlDialect;
import java.sql.DatabaseMetaData;
public class CustomSqlDialectFactory extends SqlDialectFactoryImpl {
/**
* CAVEAT: Due to the reflective access in
org.apache.calcite.avatica.AvaticaUtils#instantiatePlugin
* always make sure to declare a field with the name `INSTANCE` or the
default from
* org.apache.calcite.sql.SqlDialectFactoryImpl will be used instead of
objects
* produced by this factory.
*/
public static final CustomSqlDialectFactory INSTANCE = new
CustomSqlDialectFactory();
@Override
public SqlDialect create(DatabaseMetaData databaseMetaData) {
SqlDialect.Context context =
SqlDialectFactoryImpl.Utils.context(databaseMetaData);
if (/* my particular conditions */) {
return /* my particular dialect */;
} else {
return super.create(databaseMetaData, context, new
AnsiSqlDialect(context));
}
}
}
{code}
was (Author: marcobjorge):
Please see [https://github.com/apache/calcite/pull/2658.]
Sample #1 reusability (if you just need to override the selection process):
{code:java}
import org.apache.calcite.sql.SqlDialect;
import org.apache.calcite.sql.SqlDialectFactoryImpl;
import org.apache.calcite.sql.dialect.AnsiSqlDialect;
import java.sql.DatabaseMetaData;
public class MySqlDialectFactory extends SqlDialectFactoryImpl {
/**
* CAVEAT: Due to the reflective access in
org.apache.calcite.avatica.AvaticaUtils#instantiatePlugin
* always make sure to declare a field with the name `INSTANCE` or the
default from
* org.apache.calcite.sql.SqlDialectFactoryImpl will be used instead of
objects
* produced by this factory.
*/
public static final MySqlDialectFactory INSTANCE = new MySqlDialectFactory();
@Override
public SqlDialect create(DatabaseMetaData databaseMetaData,
SqlDialect.Context context, SqlDialect fallback) {
if (/* my particular conditions */) {
return /* my particular dialect */;
} else {
return super.create(databaseMetaData, context, fallback);
}
}
}
{code}
Sample #2 reusability (if you need to override the context creation):
{code:java}
import org.apache.calcite.sql.SqlDialect;
import org.apache.calcite.sql.SqlDialectFactoryImpl;
import org.apache.calcite.sql.dialect.AnsiSqlDialect;
import java.sql.DatabaseMetaData;
public class MySqlDialectFactory extends SqlDialectFactoryImpl {
/**
* CAVEAT: Due to the reflective access in
org.apache.calcite.avatica.AvaticaUtils#instantiatePlugin
* always make sure to declare a field with the name `INSTANCE` or the
default from
* org.apache.calcite.sql.SqlDialectFactoryImpl will be used instead of
objects
* produced by this factory.
*/
public static final MySqlDialectFactory INSTANCE = new MySqlDialectFactory();
@Override
public SqlDialect create(DatabaseMetaData databaseMetaData) {
SqlDialect.Context context =
SqlDialectFactoryImpl.Utils.context(databaseMetaData);
if (/* my particular conditions */) {
return /* my particular dialect */;
} else {
return super.create(databaseMetaData, context, new
AnsiSqlDialect(context));
}
}
}
{code}
> Extensibility of SqlDialectFactory lacks reusability of
> SqlDialectFactoryImpl.*
> -------------------------------------------------------------------------------
>
> Key: CALCITE-4963
> URL: https://issues.apache.org/jira/browse/CALCITE-4963
> Project: Calcite
> Issue Type: Improvement
> Components: core
> Reporter: Marco Jorge
> Priority: Major
> Labels: pull-request-available
> Time Spent: 10m
> Remaining Estimate: 0h
>
> _(nicetities first - great project and thanks all for the great work)_
> Although it's possible to extend the SqlDialectFactory, the custom
> implementation cannot reuse any of the behaviour of the default
> SqlDialectFactoryImpl. The default SqlDialectFactoryImpl has lots of
> important/reusable behaviour such as #getCasing, #isCaseSensitive,
> #getNullCollation or even the default decisioning in the #create method.
> If any user needs to provide a custom SqlDialect yet still support the
> existing SqlDialects the user needs to copy the whole SqlDialectFactoryImpl
> to make the custom changes.
> This request is to make the default behavior in SqlDialectFactoryImpl
> reusable so that extensions don't need to fork a whole class.
--
This message was sent by Atlassian Jira
(v8.20.1#820001)