-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-17355 Add array_to_string function
- Loading branch information
Showing
19 changed files
with
358 additions
and
2 deletions.
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
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
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
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
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
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
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
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
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
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
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
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
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
60 changes: 60 additions & 0 deletions
60
hibernate-core/src/main/java/org/hibernate/dialect/function/array/ArrayToStringFunction.java
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,60 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html | ||
*/ | ||
package org.hibernate.dialect.function.array; | ||
|
||
import java.util.List; | ||
|
||
import org.hibernate.query.ReturnableType; | ||
import org.hibernate.query.sqm.function.AbstractSqmSelfRenderingFunctionDescriptor; | ||
import org.hibernate.query.sqm.function.FunctionKind; | ||
import org.hibernate.query.sqm.produce.function.ArgumentTypesValidator; | ||
import org.hibernate.query.sqm.produce.function.StandardArgumentsValidators; | ||
import org.hibernate.query.sqm.produce.function.StandardFunctionArgumentTypeResolvers; | ||
import org.hibernate.query.sqm.produce.function.StandardFunctionReturnTypeResolvers; | ||
import org.hibernate.sql.ast.SqlAstTranslator; | ||
import org.hibernate.sql.ast.spi.SqlAppender; | ||
import org.hibernate.sql.ast.tree.SqlAstNode; | ||
import org.hibernate.type.StandardBasicTypes; | ||
import org.hibernate.type.spi.TypeConfiguration; | ||
|
||
import static org.hibernate.query.sqm.produce.function.FunctionParameterType.ANY; | ||
import static org.hibernate.query.sqm.produce.function.FunctionParameterType.STRING; | ||
|
||
/** | ||
* @author Christian Beikov | ||
*/ | ||
public class ArrayToStringFunction extends AbstractSqmSelfRenderingFunctionDescriptor { | ||
|
||
public ArrayToStringFunction(TypeConfiguration typeConfiguration) { | ||
super( | ||
"array_to_string", | ||
FunctionKind.NORMAL, | ||
StandardArgumentsValidators.composite( | ||
new ArgumentTypesValidator( null, ANY, STRING ), | ||
ArrayArgumentValidator.DEFAULT_INSTANCE | ||
), | ||
StandardFunctionReturnTypeResolvers.invariant( | ||
typeConfiguration.getBasicTypeRegistry().resolve( StandardBasicTypes.STRING ) | ||
), | ||
StandardFunctionArgumentTypeResolvers.invariant( typeConfiguration, ANY, STRING ) | ||
); | ||
} | ||
|
||
@Override | ||
public void render( | ||
SqlAppender sqlAppender, | ||
List<? extends SqlAstNode> sqlAstArguments, | ||
ReturnableType<?> returnType, | ||
SqlAstTranslator<?> walker) { | ||
sqlAppender.appendSql( "array_to_string(" ); | ||
sqlAstArguments.get( 0 ).accept( walker ); | ||
sqlAppender.appendSql( ',' ); | ||
sqlAstArguments.get( 1 ).accept( walker ); | ||
sqlAppender.appendSql( ')' ); | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
...nate-core/src/main/java/org/hibernate/dialect/function/array/H2ArrayToStringFunction.java
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,52 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html | ||
*/ | ||
package org.hibernate.dialect.function.array; | ||
|
||
import java.util.List; | ||
|
||
import org.hibernate.query.ReturnableType; | ||
import org.hibernate.sql.ast.SqlAstTranslator; | ||
import org.hibernate.sql.ast.spi.SqlAppender; | ||
import org.hibernate.sql.ast.tree.SqlAstNode; | ||
import org.hibernate.sql.ast.tree.expression.Expression; | ||
import org.hibernate.type.spi.TypeConfiguration; | ||
|
||
/** | ||
* H2 requires a very special emulation, because {@code unnest} is pretty much useless, | ||
* due to https://github.com/h2database/h2database/issues/1815. | ||
* This emulation uses {@code array_get}, {@code array_length} and {@code system_range} functions to roughly achieve the same. | ||
*/ | ||
public class H2ArrayToStringFunction extends ArrayToStringFunction { | ||
|
||
private final int maximumArraySize; | ||
|
||
public H2ArrayToStringFunction(int maximumArraySize, TypeConfiguration typeConfiguration) { | ||
super( typeConfiguration ); | ||
this.maximumArraySize = maximumArraySize; | ||
} | ||
|
||
@Override | ||
public void render( | ||
SqlAppender sqlAppender, | ||
List<? extends SqlAstNode> sqlAstArguments, | ||
ReturnableType<?> returnType, | ||
SqlAstTranslator<?> walker) { | ||
final Expression arrayExpression = (Expression) sqlAstArguments.get( 0 ); | ||
final Expression separatorExpression = (Expression) sqlAstArguments.get( 1 ); | ||
sqlAppender.append( "case when " ); | ||
arrayExpression.accept( walker ); | ||
sqlAppender.append( " is not null then coalesce((select listagg(array_get(" ); | ||
arrayExpression.accept( walker ); | ||
sqlAppender.append(",i.idx)," ); | ||
separatorExpression.accept( walker ); | ||
sqlAppender.append( ") within group (order by i.idx) from system_range(1,"); | ||
sqlAppender.append( Integer.toString( maximumArraySize ) ); | ||
sqlAppender.append( ") i(idx) where i.idx<=coalesce(cardinality("); | ||
arrayExpression.accept( walker ); | ||
sqlAppender.append("),0)),'') end" ); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...te-core/src/main/java/org/hibernate/dialect/function/array/HSQLArrayToStringFunction.java
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,45 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html | ||
*/ | ||
package org.hibernate.dialect.function.array; | ||
|
||
import java.util.List; | ||
|
||
import org.hibernate.query.ReturnableType; | ||
import org.hibernate.sql.ast.SqlAstNodeRenderingMode; | ||
import org.hibernate.sql.ast.SqlAstTranslator; | ||
import org.hibernate.sql.ast.spi.SqlAppender; | ||
import org.hibernate.sql.ast.tree.SqlAstNode; | ||
import org.hibernate.sql.ast.tree.expression.Expression; | ||
import org.hibernate.type.spi.TypeConfiguration; | ||
|
||
/** | ||
* HSQLDB has a special syntax. | ||
*/ | ||
public class HSQLArrayToStringFunction extends ArrayToStringFunction { | ||
|
||
public HSQLArrayToStringFunction(TypeConfiguration typeConfiguration) { | ||
super( typeConfiguration ); | ||
} | ||
|
||
@Override | ||
public void render( | ||
SqlAppender sqlAppender, | ||
List<? extends SqlAstNode> sqlAstArguments, | ||
ReturnableType<?> returnType, | ||
SqlAstTranslator<?> walker) { | ||
final Expression arrayExpression = (Expression) sqlAstArguments.get( 0 ); | ||
final Expression separatorExpression = (Expression) sqlAstArguments.get( 1 ); | ||
sqlAppender.append( "case when " ); | ||
arrayExpression.accept( walker ); | ||
sqlAppender.append( " is not null then coalesce((select group_concat(t.val order by t.idx separator " ); | ||
// HSQLDB doesn't like non-literals as separator | ||
walker.render( separatorExpression, SqlAstNodeRenderingMode.INLINE_PARAMETERS ); | ||
sqlAppender.append( ") from unnest("); | ||
arrayExpression.accept( walker ); | ||
sqlAppender.append(") with ordinality t(val,idx)),'') end" ); | ||
} | ||
} |
Oops, something went wrong.