113113import org .apache .ignite .internal .catalog .commands .TableSortedPrimaryKey ;
114114import org .apache .ignite .internal .catalog .descriptors .CatalogColumnCollation ;
115115import org .apache .ignite .internal .catalog .descriptors .ConsistencyMode ;
116+ import org .apache .ignite .internal .partitiondistribution .DistributionAlgorithm ;
116117import org .apache .ignite .internal .sql .engine .prepare .IgnitePlanner ;
117118import org .apache .ignite .internal .sql .engine .prepare .IgniteSqlValidator ;
118119import org .apache .ignite .internal .sql .engine .prepare .PlanningContext ;
136137import org .apache .ignite .internal .sql .engine .sql .IgniteSqlPrimaryKeyIndexType ;
137138import org .apache .ignite .internal .sql .engine .sql .IgniteSqlTypeNameSpec ;
138139import org .apache .ignite .internal .sql .engine .sql .IgniteSqlZoneOption ;
140+ import org .apache .ignite .internal .sql .engine .sql .IgniteSqlZoneOptionMode ;
139141import org .apache .ignite .internal .sql .engine .type .UuidType ;
140142import org .apache .ignite .internal .sql .engine .util .Commons ;
141143import org .apache .ignite .lang .IgniteException ;
@@ -154,6 +156,12 @@ public class DdlSqlToCommandConverter {
154156 /** Mapping: Zone option ID -> DDL option info. */
155157 private final Map <ZoneOptionEnum , DdlOptionInfo <AlterZoneCommandBuilder , ?>> alterZoneOptionInfos ;
156158
159+ /** DDL option info for an integer CREATE ZONE REPLICAS option. */
160+ private final DdlOptionInfo <CreateZoneCommandBuilder , Integer > createReplicasOptionInfo ;
161+
162+ /** DDL option info for an integer ALTER ZONE REPLICAS option. */
163+ private final DdlOptionInfo <AlterZoneCommandBuilder , Integer > alterReplicasOptionInfo ;
164+
157165 /** Zone options set. */
158166 private final Set <String > knownZoneOptionNames ;
159167
@@ -168,7 +176,6 @@ public DdlSqlToCommandConverter() {
168176
169177 // CREATE ZONE options.
170178 zoneOptionInfos = new EnumMap <>(Map .of (
171- REPLICAS , new DdlOptionInfo <>(Integer .class , this ::checkPositiveNumber , CreateZoneCommandBuilder ::replicas ),
172179 PARTITIONS , new DdlOptionInfo <>(Integer .class , this ::checkPositiveNumber , CreateZoneCommandBuilder ::partitions ),
173180 // TODO https://issues.apache.org/jira/browse/IGNITE-22162 appropriate setter method should be used.
174181 DISTRIBUTION_ALGORITHM , new DdlOptionInfo <>(String .class , null , (builder , params ) -> {}),
@@ -185,9 +192,10 @@ public DdlSqlToCommandConverter() {
185192 (builder , params ) -> builder .consistencyModeParams (parseConsistencyMode (params )))
186193 ));
187194
195+ createReplicasOptionInfo = new DdlOptionInfo <>(Integer .class , this ::checkPositiveNumber , CreateZoneCommandBuilder ::replicas );
196+
188197 // ALTER ZONE options.
189198 alterZoneOptionInfos = new EnumMap <>(Map .of (
190- REPLICAS , new DdlOptionInfo <>(Integer .class , this ::checkPositiveNumber , AlterZoneCommandBuilder ::replicas ),
191199 PARTITIONS , new DdlOptionInfo <>(Integer .class , this ::checkPositiveNumber , AlterZoneCommandBuilder ::partitions ),
192200 DATA_NODES_FILTER , new DdlOptionInfo <>(String .class , null , AlterZoneCommandBuilder ::filter ),
193201 DATA_NODES_AUTO_ADJUST ,
@@ -197,6 +205,8 @@ public DdlSqlToCommandConverter() {
197205 DATA_NODES_AUTO_ADJUST_SCALE_DOWN ,
198206 new DdlOptionInfo <>(Integer .class , this ::checkPositiveNumber , AlterZoneCommandBuilder ::dataNodesAutoAdjustScaleDown )
199207 ));
208+
209+ alterReplicasOptionInfo = new DdlOptionInfo <>(Integer .class , this ::checkPositiveNumber , AlterZoneCommandBuilder ::replicas );
200210 }
201211
202212 /**
@@ -205,10 +215,10 @@ public DdlSqlToCommandConverter() {
205215 * @param consistencyMode String representation of consistency mode.
206216 * @return Consistency mode
207217 */
208- public static ConsistencyMode parseConsistencyMode (String consistencyMode ) {
218+ private static ConsistencyMode parseConsistencyMode (String consistencyMode ) {
209219 try {
210220 return ConsistencyMode .valueOf (consistencyMode );
211- } catch (IllegalArgumentException e ) {
221+ } catch (IllegalArgumentException ignored ) {
212222 throw new SqlException (STMT_VALIDATION_ERR , "Failed to parse consistency mode: " + consistencyMode
213223 + ". Valid values are: " + Arrays .toString (ConsistencyMode .values ()));
214224 }
@@ -678,23 +688,7 @@ private CatalogCommand convertCreateZone(IgniteSqlCreateZone createZoneNode, Pla
678688 for (SqlNode optionNode : createZoneNode .createOptionList ().getList ()) {
679689 IgniteSqlZoneOption option = (IgniteSqlZoneOption ) optionNode ;
680690
681- assert option .key ().isSimple () : option .key ();
682-
683- String optionName = option .key ().getSimple ().toUpperCase ();
684-
685- DdlOptionInfo <CreateZoneCommandBuilder , ?> zoneOptionInfo = null ;
686-
687- if (remainingKnownOptions .remove (optionName )) {
688- zoneOptionInfo = zoneOptionInfos .get (ZoneOptionEnum .valueOf (optionName ));
689- } else if (knownZoneOptionNames .contains (optionName )) {
690- throw duplicateZoneOption (ctx , optionName );
691- }
692-
693- if (zoneOptionInfo == null ) {
694- throw unexpectedZoneOption (ctx , optionName );
695- }
696-
697- updateCommandOption ("Zone" , optionName , option .value (), zoneOptionInfo , ctx .query (), builder );
691+ updateZoneOption (option , remainingKnownOptions , zoneOptionInfos , createReplicasOptionInfo , ctx , builder );
698692 }
699693
700694 if (remainingKnownOptions .contains (STORAGE_PROFILES .name ())) {
@@ -704,7 +698,6 @@ private CatalogCommand convertCreateZone(IgniteSqlCreateZone createZoneNode, Pla
704698 return builder .build ();
705699 }
706700
707-
708701 /**
709702 * Converts the given '{@code ALTER ZONE}' AST to the {@link AlterZoneCommand} catalog command.
710703 */
@@ -718,20 +711,8 @@ private CatalogCommand convertAlterZoneSet(IgniteSqlAlterZoneSet alterZoneSet, P
718711
719712 for (SqlNode optionNode : alterZoneSet .alterOptionsList ().getList ()) {
720713 IgniteSqlZoneOption option = (IgniteSqlZoneOption ) optionNode ;
721- String optionName = option .key ().getSimple ().toUpperCase ();
722-
723- if (!knownZoneOptionNames .contains (optionName )) {
724- throw unexpectedZoneOption (ctx , optionName );
725- } else if (!remainingKnownOptions .remove (optionName )) {
726- throw duplicateZoneOption (ctx , optionName );
727- }
728-
729- DdlOptionInfo <AlterZoneCommandBuilder , ?> zoneOptionInfo = alterZoneOptionInfos .get (ZoneOptionEnum .valueOf (optionName ));
730714
731- assert zoneOptionInfo != null : optionName ;
732- assert option .value () instanceof SqlLiteral : option .value ();
733-
734- updateCommandOption ("Zone" , optionName , option .value (), zoneOptionInfo , ctx .query (), builder );
715+ updateZoneOption (option , remainingKnownOptions , alterZoneOptionInfos , alterReplicasOptionInfo , ctx , builder );
735716 }
736717
737718 return builder .build ();
@@ -809,7 +790,56 @@ private String deriveObjectName(SqlIdentifier id, PlanningContext ctx, String ob
809790 return objId .getSimple ();
810791 }
811792
812- private <S , T > void updateCommandOption (
793+ private <S > void updateZoneOption (
794+ IgniteSqlZoneOption option ,
795+ Set <String > remainingKnownOptions ,
796+ Map <ZoneOptionEnum , DdlOptionInfo <S , ?>> optionInfos ,
797+ DdlOptionInfo <S , Integer > replicasOptionInfo ,
798+ PlanningContext ctx ,
799+ S target
800+ ) {
801+ assert option .key ().isSimple () : option .key ();
802+
803+ String optionName = option .key ().getSimple ().toUpperCase ();
804+
805+ if (!knownZoneOptionNames .contains (optionName )) {
806+ throw unexpectedZoneOption (ctx , optionName );
807+ } else if (!remainingKnownOptions .remove (optionName )) {
808+ throw duplicateZoneOption (ctx , optionName );
809+ }
810+
811+ ZoneOptionEnum zoneOption = ZoneOptionEnum .valueOf (optionName );
812+ DdlOptionInfo <S , ?> zoneOptionInfo = optionInfos .get (zoneOption );
813+
814+ // Options infos doesn't contain REPLICAS, it's handled separately
815+ assert zoneOptionInfo != null || zoneOption == REPLICAS : optionName ;
816+
817+ assert option .value () instanceof SqlLiteral : option .value ();
818+ SqlLiteral literal = (SqlLiteral ) option .value ();
819+
820+ if (zoneOption == REPLICAS ) {
821+ if (literal .getTypeName () == SqlTypeName .SYMBOL ) {
822+ IgniteSqlZoneOptionMode zoneOptionMode = literal .symbolValue (IgniteSqlZoneOptionMode .class );
823+
824+ if (zoneOptionMode != IgniteSqlZoneOptionMode .ALL ) {
825+ throw new SqlException (STMT_VALIDATION_ERR , format (
826+ "Unexpected value of zone replicas [expected ALL, was {}; query=\" {}\" " ,
827+ zoneOptionMode , ctx .query ()
828+ ));
829+ }
830+
831+ // Directly set the option value and return
832+ replicasOptionInfo .setter .accept (target , DistributionAlgorithm .ALL_REPLICAS );
833+ return ;
834+ } else {
835+ zoneOptionInfo = replicasOptionInfo ;
836+ }
837+ }
838+
839+ updateCommandOption ("Zone" , optionName , literal , zoneOptionInfo , ctx .query (), target );
840+ }
841+
842+ private static <S , T > void updateCommandOption (
813843 String sqlObjName ,
814844 Object optId ,
815845 SqlNode value ,
0 commit comments